Include bfd.h before sysdep.h, so ansidecl and PROTO() get defined first.
[binutils-gdb.git] / ld / ldlang.c
1 /* Copyright (C) 1991 Free Software Foundation, Inc.
2
3 This file is part of GLD, the Gnu Linker.
4
5 GLD is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 1, or (at your option)
8 any later version.
9
10 GLD is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GLD; see the file COPYING. If not, write to
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
18
19 /* $Id$
20 *
21 */
22
23 #include "bfd.h"
24 #include "sysdep.h"
25
26 #include "ld.h"
27 #include "ldmain.h"
28 #include "ldsym.h"
29 #include "ldgram.h"
30 #include "ldwarn.h"
31 #include "ldlang.h"
32 #include "ldexp.h"
33 #include "ldemul.h"
34 #include "ldlex.h"
35 #include "ldmisc.h"
36 /* FORWARDS */
37 PROTO(static void, print_statements,(void));
38 PROTO(static void, print_statement,(lang_statement_union_type *,
39 lang_output_section_statement_type *));
40
41
42 /* LOCALS */
43 static CONST char *startup_file;
44 static lang_statement_list_type input_file_chain;
45
46 /* Points to the last statement in the .data section, so we can add
47 stuff to the data section without pain */
48 static lang_statement_list_type end_of_data_section_statement_list;
49
50 /* List of statements needed to handle consxtructors */
51 static lang_statement_list_type constructor_list;
52
53 static boolean placed_commons = false;
54 static lang_output_section_statement_type *default_common_section;
55 static boolean map_option_f;
56 static bfd_vma print_dot;
57 static lang_input_statement_type *first_file;
58 static lang_statement_list_type lang_output_section_statement;
59 static CONST char *current_target;
60 static CONST char *output_target;
61 static size_t longest_section_name = 8;
62 static asection common_section;
63 static section_userdata_type common_section_userdata;
64 static lang_statement_list_type statement_list;
65 /* EXPORTS */
66
67 lang_statement_list_type *stat_ptr = &statement_list;
68 lang_input_statement_type *script_file = 0;
69 boolean option_longmap = false;
70 lang_statement_list_type file_chain = {0};
71 CONST char *entry_symbol = 0;
72 bfd_size_type largest_section = 0;
73 boolean lang_has_input_file = false;
74 lang_output_section_statement_type *create_object_symbols = 0;
75 boolean had_output_filename = false;
76 boolean lang_float_flag = false;
77 /* IMPORTS */
78 extern char *default_target;
79
80 extern unsigned int undefined_global_sym_count;
81 extern char *current_file;
82 extern bfd *output_bfd;
83 extern enum bfd_architecture ldfile_output_architecture;
84 extern unsigned long ldfile_output_machine;
85 extern char *ldfile_output_machine_name;
86 extern ldsym_type *symbol_head;
87 extern unsigned int commons_pending;
88 extern args_type command_line;
89 extern ld_config_type config;
90 extern boolean had_script;
91 extern boolean write_map;
92
93
94 #ifdef __STDC__
95 #define cat(a,b) a##b
96 #else
97 #define cat(a,b) a/**/b
98 #endif
99
100 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
101
102 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
103
104 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
105
106 void EXFUN(lang_add_data,( int type , union etree_union *exp));
107
108 static void
109 DEFUN(print_size,(value),
110 size_t value)
111 {
112 printf("%5x", (unsigned)value);
113 }
114 static void
115 DEFUN(print_alignment,(value),
116 unsigned int value)
117 {
118 printf("2**%1u",value);
119 }
120 static void
121 DEFUN(print_fill,(value),
122 fill_type value)
123 {
124 printf("%04x",(unsigned)value);
125 }
126
127
128 static void
129 DEFUN(print_section,(name),
130 CONST char *CONST name)
131 {
132 printf("%*s", -longest_section_name, name);
133 }
134
135 /*----------------------------------------------------------------------
136 lang_for_each_statement walks the parse tree and calls the provided
137 function for each node
138 */
139
140 static void
141 DEFUN(lang_for_each_statement_worker,(func, s),
142 void (*func)() AND
143 lang_statement_union_type *s)
144 {
145 for (; s != (lang_statement_union_type *)NULL ; s = s->next)
146 {
147 func(s);
148
149 switch (s->header.type) {
150 case lang_constructors_statement_enum:
151 lang_for_each_statement_worker(func, constructor_list.head);
152 break;
153 case lang_output_section_statement_enum:
154 lang_for_each_statement_worker
155 (func,
156 s->output_section_statement.children.head);
157 break;
158 case lang_wild_statement_enum:
159 lang_for_each_statement_worker
160 (func,
161 s->wild_statement.children.head);
162 break;
163 case lang_data_statement_enum:
164 case lang_object_symbols_statement_enum:
165 case lang_output_statement_enum:
166 case lang_target_statement_enum:
167 case lang_input_section_enum:
168 case lang_input_statement_enum:
169 case lang_fill_statement_enum:
170 case lang_assignment_statement_enum:
171 case lang_padding_statement_enum:
172 case lang_address_statement_enum:
173 break;
174 default:
175 FAIL();
176 break;
177 }
178 }
179 }
180
181 void
182 DEFUN(lang_for_each_statement,(func),
183 void (*func)())
184 {
185 lang_for_each_statement_worker(func,
186 statement_list.head);
187 }
188 /*----------------------------------------------------------------------*/
189 static void
190 DEFUN(lang_list_init,(list),
191 lang_statement_list_type *list)
192 {
193 list->head = (lang_statement_union_type *)NULL;
194 list->tail = &list->head;
195 }
196
197
198 /*----------------------------------------------------------------------
199
200 build a new statement node for the parse tree
201
202 */
203
204 static
205 lang_statement_union_type*
206 DEFUN(new_statement,(type, size, list),
207 enum statement_enum type AND
208 bfd_size_type size AND
209 lang_statement_list_type *list)
210 {
211 lang_statement_union_type *new = (lang_statement_union_type *)
212 ldmalloc(size);
213 new->header.type = type;
214 new->header.next = (lang_statement_union_type *)NULL;
215 lang_statement_append(list, new, &new->header.next);
216 return new;
217 }
218
219 /*
220 Build a new input file node for the language. There are several ways
221 in which we treat an input file, eg, we only look at symbols, or
222 prefix it with a -l etc.
223
224 We can be supplied with requests for input files more than once;
225 they may, for example be split over serveral lines like foo.o(.text)
226 foo.o(.data) etc, so when asked for a file we check that we havn't
227 got it already so we don't duplicate the bfd.
228
229 */
230 static lang_input_statement_type *
231 DEFUN(new_afile, (name, file_type, target),
232 CONST char *CONST name AND
233 CONST lang_input_file_enum_type file_type AND
234 CONST char *CONST target)
235 {
236 lang_input_statement_type *p = new_stat(lang_input_statement,
237 stat_ptr);
238 lang_has_input_file = true;
239 p->target = target;
240 switch (file_type) {
241 case lang_input_file_is_symbols_only_enum:
242 p->filename = name;
243 p->is_archive =false;
244 p->real = true;
245 p->local_sym_name= name;
246 p->just_syms_flag = true;
247 p->search_dirs_flag = false;
248 break;
249 case lang_input_file_is_fake_enum:
250 p->filename = name;
251 p->is_archive =false;
252 p->real = false;
253 p->local_sym_name= name;
254 p->just_syms_flag = false;
255 p->search_dirs_flag =false;
256 break;
257 case lang_input_file_is_l_enum:
258 p->is_archive = true;
259 p->filename = name;
260 p->real = true;
261 p->local_sym_name = concat("-l",name,"");
262 p->just_syms_flag = false;
263 p->search_dirs_flag = true;
264 break;
265 case lang_input_file_is_search_file_enum:
266 case lang_input_file_is_marker_enum:
267 p->filename = name;
268 p->is_archive =false;
269 p->real = true;
270 p->local_sym_name= name;
271 p->just_syms_flag = false;
272 p->search_dirs_flag =true;
273 break;
274 case lang_input_file_is_file_enum:
275 p->filename = name;
276 p->is_archive =false;
277 p->real = true;
278 p->local_sym_name= name;
279 p->just_syms_flag = false;
280 p->search_dirs_flag =false;
281 break;
282 default:
283 FAIL();
284 }
285 p->asymbols = (asymbol **)NULL;
286 p->superfile = (lang_input_statement_type *)NULL;
287 p->next_real_file = (lang_statement_union_type*)NULL;
288 p->next = (lang_statement_union_type*)NULL;
289 p->symbol_count = 0;
290 p->common_output_section = (asection *)NULL;
291 lang_statement_append(&input_file_chain,
292 (lang_statement_union_type *)p,
293 &p->next_real_file);
294 return p;
295 }
296
297
298
299 lang_input_statement_type *
300 DEFUN(lang_add_input_file,(name, file_type, target),
301 char *name AND
302 lang_input_file_enum_type file_type AND
303 char *target)
304 {
305 /* Look it up or build a new one */
306 lang_has_input_file = true;
307 #if 0
308 lang_input_statement_type *p;
309
310 for (p = (lang_input_statement_type *)input_file_chain.head;
311 p != (lang_input_statement_type *)NULL;
312 p = (lang_input_statement_type *)(p->next_real_file))
313 {
314 /* Sometimes we have incomplete entries in here */
315 if (p->filename != (char *)NULL) {
316 if(strcmp(name,p->filename) == 0) return p;
317 }
318
319 }
320 #endif
321 return new_afile(name, file_type, target);
322 }
323
324
325 /* Build enough state so that the parser can build its tree */
326 void
327 DEFUN_VOID(lang_init)
328 {
329
330 stat_ptr= &statement_list;
331 lang_list_init(stat_ptr);
332
333 lang_list_init(&input_file_chain);
334 lang_list_init(&lang_output_section_statement);
335 lang_list_init(&file_chain);
336 first_file = lang_add_input_file((char *)NULL,
337 lang_input_file_is_marker_enum,
338 (char *)NULL);
339 }
340
341
342 /*----------------------------------------------------------------------
343 A region is an area of memory declared with the
344 MEMORY { name:org=exp, len=exp ... }
345 syntax.
346
347 We maintain a list of all the regions here
348
349 If no regions are specified in the script, then the default is used
350 which is created when looked up to be the entire data space
351 */
352
353 static lang_memory_region_type *lang_memory_region_list;
354 static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
355
356 lang_memory_region_type *
357 DEFUN(lang_memory_region_lookup,(name),
358 CONST char *CONST name)
359 {
360
361 lang_memory_region_type *p = lang_memory_region_list;
362 for (p = lang_memory_region_list;
363 p != ( lang_memory_region_type *)NULL;
364 p = p->next) {
365 if (strcmp(p->name, name) == 0) {
366 return p;
367 }
368 }
369 if (strcmp(name,"*default*")==0) {
370 /* This is the default region, dig out first one on the list */
371 if (lang_memory_region_list != (lang_memory_region_type*)NULL){
372 return lang_memory_region_list;
373 }
374 }
375 {
376 lang_memory_region_type *new =
377 (lang_memory_region_type *)ldmalloc((bfd_size_type)(sizeof(lang_memory_region_type)));
378 new->name = buystring(name);
379 new->next = (lang_memory_region_type *)NULL;
380
381 *lang_memory_region_list_tail = new;
382 lang_memory_region_list_tail = &new->next;
383 new->origin = 0;
384 new->length = ~0;
385 new->current = 0;
386 return new;
387 }
388 }
389
390
391 lang_output_section_statement_type *
392 DEFUN(lang_output_section_find,(name),
393 CONST char * CONST name)
394 {
395 lang_statement_union_type *u;
396 lang_output_section_statement_type *lookup;
397
398 for (u = lang_output_section_statement.head;
399 u != (lang_statement_union_type *)NULL;
400 u = lookup->next)
401 {
402 lookup = &u->output_section_statement;
403 if (strcmp(name, lookup->name)==0) {
404 return lookup;
405 }
406 }
407 return (lang_output_section_statement_type *)NULL;
408 }
409
410 lang_output_section_statement_type *
411 DEFUN(lang_output_section_statement_lookup,(name),
412 CONST char * CONST name)
413 {
414 lang_output_section_statement_type *lookup;
415 lookup =lang_output_section_find(name);
416 if (lookup == (lang_output_section_statement_type *)NULL) {
417
418 lookup =(lang_output_section_statement_type *)
419 new_stat(lang_output_section_statement, stat_ptr);
420 lookup->region = (lang_memory_region_type *)NULL;
421 lookup->fill = 0;
422 lookup->block_value = 1;
423 lookup->name = name;
424
425 lookup->next = (lang_statement_union_type*)NULL;
426 lookup->bfd_section = (asection *)NULL;
427 lookup->processed = false;
428 lookup->addr_tree = (etree_type *)NULL;
429 lang_list_init(&lookup->children);
430
431 lang_statement_append(&lang_output_section_statement,
432 (lang_statement_union_type *)lookup,
433 &lookup->next);
434 }
435 return lookup;
436 }
437
438
439
440
441
442 static void
443 DEFUN(print_flags, (outfile, ignore_flags),
444 FILE *outfile AND
445 int *ignore_flags)
446 {
447 fprintf(outfile,"(");
448 #if 0
449 if (flags->flag_read) fprintf(outfile,"R");
450 if (flags->flag_write) fprintf(outfile,"W");
451 if (flags->flag_executable) fprintf(outfile,"X");
452 if (flags->flag_loadable) fprintf(outfile,"L");
453 #endif
454 fprintf(outfile,")");
455 }
456
457 void
458 DEFUN(lang_map,(outfile),
459 FILE *outfile)
460 {
461 lang_memory_region_type *m;
462 fprintf(outfile,"**MEMORY CONFIGURATION**\n\n");
463 #ifdef HOST_64_BIT
464 fprintf(outfile,"name\t\torigin\t\tlength\t\tattributes\n");
465 #else
466 fprintf(outfile,"name\t\torigin length\t\tattributes\n");
467 #endif
468 for (m = lang_memory_region_list;
469 m != (lang_memory_region_type *)NULL;
470 m = m->next)
471 {
472 fprintf(outfile,"%-16s", m->name);
473 print_address(m->origin);
474 print_space();
475 print_address(m->length);
476 print_space();
477 print_flags(outfile, &m->flags);
478 fprintf(outfile,"\n");
479 }
480 fprintf(outfile,"\n\n**LINK EDITOR MEMORY MAP**\n\n");
481 fprintf(outfile,"output input virtual\n");
482 fprintf(outfile,"section section address tsize\n\n");
483
484 print_statements();
485
486 }
487
488 /*
489 *
490 */
491 static void
492 DEFUN(init_os,(s),
493 lang_output_section_statement_type *s)
494 {
495 section_userdata_type *new =
496 (section_userdata_type *)
497 ldmalloc((bfd_size_type)(sizeof(section_userdata_type)));
498
499 s->bfd_section = bfd_make_section(output_bfd, s->name);
500 if (s->bfd_section == (asection *)NULL) {
501 info("%P%F output format %s cannot represent section called %s\n",
502 output_bfd->xvec->name,
503 s->name);
504 }
505 s->bfd_section->output_section = s->bfd_section;
506 /* s->bfd_section->flags = s->flags;*/
507
508 /* We initialize an output sections output offset to minus its own */
509 /* vma to allow us to output a section through itself */
510 s->bfd_section->output_offset = 0;
511 get_userdata( s->bfd_section) = (PTR)new;
512 }
513
514 /***********************************************************************
515 The wild routines.
516
517 These expand statements like *(.text) and foo.o to a list of
518 explicit actions, like foo.o(.text), bar.o(.text) and
519 foo.o(.text,.data) .
520
521 The toplevel routine, wild, takes a statement, section, file and
522 target. If either the section or file is null it is taken to be the
523 wildcard. Seperate lang_input_section statements are created for
524 each part of the expanstion, and placed after the statement provided.
525
526 */
527
528 static void
529 DEFUN(wild_doit,(ptr, section, output, file),
530 lang_statement_list_type *ptr AND
531 asection *section AND
532 lang_output_section_statement_type *output AND
533 lang_input_statement_type *file)
534 {
535 if(output->bfd_section == (asection *)NULL)
536 {
537 init_os(output);
538 }
539
540 if (section != (asection *)NULL
541 && section->output_section == (asection *)NULL) {
542 /* Add a section reference to the list */
543 lang_input_section_type *new = new_stat(lang_input_section, ptr);
544
545 new->section = section;
546 new->ifile = file;
547 section->output_section = output->bfd_section;
548 section->output_section->flags |= section->flags;
549 if (section->alignment_power > output->bfd_section->alignment_power) {
550 output->bfd_section->alignment_power = section->alignment_power;
551 }
552 }
553 }
554
555 static asection *
556 DEFUN(our_bfd_get_section_by_name,(abfd, section),
557 bfd *abfd AND
558 CONST char *section)
559 {
560 return bfd_get_section_by_name(abfd, section);
561 }
562
563 static void
564 DEFUN(wild_section,(ptr, section, file , output),
565 lang_wild_statement_type *ptr AND
566 CONST char *section AND
567 lang_input_statement_type *file AND
568 lang_output_section_statement_type *output)
569 {
570 asection *s;
571 if (file->just_syms_flag == false) {
572 if (section == (char *)NULL) {
573 /* Do the creation to all sections in the file */
574 for (s = file->the_bfd->sections; s != (asection *)NULL; s=s->next) {
575 wild_doit(&ptr->children, s, output, file);
576 }
577 }
578 else {
579 /* Do the creation to the named section only */
580 wild_doit(&ptr->children,
581 our_bfd_get_section_by_name(file->the_bfd, section),
582 output, file);
583 }
584 }
585 }
586
587
588 /* passed a file name (which must have been seen already and added to
589 the statement tree. We will see if it has been opened already and
590 had its symbols read. If not then we'll read it.
591
592 Archives are pecuilar here. We may open them once, but if they do
593 not define anything we need at the time, they won't have all their
594 symbols read. If we need them later, we'll have to redo it.
595 */
596 static
597 lang_input_statement_type *
598 DEFUN(lookup_name,(name),
599 CONST char * CONST name)
600 {
601 lang_input_statement_type *search;
602 for(search = (lang_input_statement_type *)input_file_chain.head;
603 search != (lang_input_statement_type *)NULL;
604 search = (lang_input_statement_type *)search->next_real_file)
605 {
606 if (search->filename == (char *)NULL && name == (char *)NULL) {
607 return search;
608 }
609 if (search->filename != (char *)NULL && name != (char *)NULL) {
610 if (strcmp(search->filename, name) == 0) {
611 ldmain_open_file_read_symbol(search);
612 return search;
613 }
614 }
615 }
616
617 /* There isn't an afile entry for this file yet, this must be
618 because the name has only appeared inside a load script and not
619 on the command line */
620 search = new_afile(name, lang_input_file_is_file_enum, default_target);
621 ldmain_open_file_read_symbol(search);
622 return search;
623
624
625 }
626
627 static void
628 DEFUN(wild,(s, section, file, target, output),
629 lang_wild_statement_type *s AND
630 CONST char *CONST section AND
631 CONST char *CONST file AND
632 CONST char *CONST target AND
633 lang_output_section_statement_type *output)
634 {
635 lang_input_statement_type *f;
636 if (file == (char *)NULL) {
637 /* Perform the iteration over all files in the list */
638 for (f = (lang_input_statement_type *)file_chain.head;
639 f != (lang_input_statement_type *)NULL;
640 f = (lang_input_statement_type *)f->next) {
641 wild_section(s, section, f, output);
642 }
643 }
644 else {
645 /* Perform the iteration over a single file */
646 wild_section( s, section, lookup_name(file), output);
647 }
648 if (section != (char *)NULL
649 && strcmp(section,"COMMON") == 0
650 && default_common_section == (lang_output_section_statement_type*)NULL)
651 {
652 /* Remember the section that common is going to incase we later
653 get something which doesn't know where to put it */
654 default_common_section = output;
655 }
656 }
657
658 /*
659 read in all the files
660 */
661 static bfd *
662 DEFUN(open_output,(name),
663 CONST char *CONST name)
664 {
665 extern CONST char *output_filename;
666 bfd *output;
667 if (output_target == (char *)NULL) {
668 if (current_target != (char *)NULL)
669 output_target = current_target;
670 else
671 output_target = default_target;
672 }
673 output = bfd_openw(name, output_target);
674 output_filename = name;
675
676 if (output == (bfd *)NULL)
677 {
678 if (bfd_error == invalid_target) {
679 info("%P%F target %s not found\n", output_target);
680 }
681 info("%P%F problem opening output file %s, %E", name);
682 }
683
684 output->flags |= D_PAGED;
685 bfd_set_format(output, bfd_object);
686 return output;
687 }
688
689
690
691
692 static void
693 DEFUN(ldlang_open_output,(statement),
694 lang_statement_union_type *statement)
695 {
696 switch (statement->header.type)
697 {
698 case lang_output_statement_enum:
699 output_bfd = open_output(statement->output_statement.name);
700 ldemul_set_output_arch();
701 break;
702
703 case lang_target_statement_enum:
704 current_target = statement->target_statement.target;
705 break;
706 default:
707 break;
708 }
709 }
710
711 static void
712 DEFUN(open_input_bfds,(statement),
713 lang_statement_union_type *statement)
714 {
715 switch (statement->header.type)
716 {
717 case lang_target_statement_enum:
718 current_target = statement->target_statement.target;
719 break;
720 case lang_wild_statement_enum:
721 /* Maybe we should load the file's symbols */
722 if (statement->wild_statement.filename)
723 {
724 (void) lookup_name(statement->wild_statement.filename);
725 }
726 break;
727 case lang_input_statement_enum:
728 if (statement->input_statement.real == true)
729 {
730 statement->input_statement.target = current_target;
731 lookup_name(statement->input_statement.filename);
732 }
733 break;
734 default:
735 break;
736 }
737 }
738 /* If there are [COMMONS] statements, put a wild one into the bss section */
739
740 static void
741 lang_reasonable_defaults()
742 {
743 #if 0
744 lang_output_section_statement_lookup(".text");
745 lang_output_section_statement_lookup(".data");
746
747 default_common_section =
748 lang_output_section_statement_lookup(".bss");
749
750
751 if (placed_commons == false) {
752 lang_wild_statement_type *new =
753 new_stat(lang_wild_statement,
754 &default_common_section->children);
755 new->section_name = "COMMON";
756 new->filename = (char *)NULL;
757 lang_list_init(&new->children);
758 }
759 #endif
760
761 }
762
763 /*
764 Add the supplied name to the symbol table as an undefined reference.
765 Remove items from the chain as we open input bfds
766 */
767 typedef struct ldlang_undef_chain_list {
768 struct ldlang_undef_chain_list *next;
769 char *name;
770 } ldlang_undef_chain_list_type;
771
772 static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head;
773
774 void
775 DEFUN(ldlang_add_undef,(name),
776 CONST char *CONST name)
777 {
778 ldlang_undef_chain_list_type *new =
779 (ldlang_undef_chain_list_type
780 *)ldmalloc((bfd_size_type)(sizeof(ldlang_undef_chain_list_type)));
781
782 new->next = ldlang_undef_chain_list_head;
783 ldlang_undef_chain_list_head = new;
784
785 new->name = buystring(name);
786 }
787 /* Run through the list of undefineds created above and place them
788 into the linker hash table as undefined symbols belonging to the
789 script file.
790 */
791 static void
792 DEFUN_VOID(lang_place_undefineds)
793 {
794 ldlang_undef_chain_list_type *ptr = ldlang_undef_chain_list_head;
795 while (ptr != (ldlang_undef_chain_list_type*)NULL) {
796 ldsym_type *sy = ldsym_get(ptr->name);
797 asymbol *def;
798 asymbol **def_ptr = (asymbol **)ldmalloc((bfd_size_type)(sizeof(asymbol **)));
799 def = (asymbol *)bfd_make_empty_symbol(script_file->the_bfd);
800 *def_ptr= def;
801 def->name = ptr->name;
802 def->flags = BSF_UNDEFINED;
803 def->section = (asection *)NULL;
804 Q_enter_global_ref(def_ptr);
805 ptr = ptr->next;
806 }
807 }
808
809
810
811 /* Copy important data from out internal form to the bfd way. Also
812 create a section for the dummy file
813 */
814
815 static void
816 DEFUN_VOID(lang_create_output_section_statements)
817 {
818 lang_statement_union_type*os;
819 for (os = lang_output_section_statement.head;
820 os != (lang_statement_union_type*)NULL;
821 os = os->output_section_statement.next) {
822 lang_output_section_statement_type *s =
823 &os->output_section_statement;
824 init_os(s);
825 }
826
827 }
828
829 static void
830 DEFUN_VOID(lang_init_script_file)
831 {
832 script_file = lang_add_input_file("script file",
833 lang_input_file_is_fake_enum,
834 (char *)NULL);
835 script_file->the_bfd = bfd_create("script file", output_bfd);
836 script_file->symbol_count = 0;
837 script_file->the_bfd->sections = output_bfd->sections;
838 }
839
840
841
842
843 /* Open input files and attatch to output sections */
844 static void
845 DEFUN(map_input_to_output_sections,(s, target, output_section_statement),
846 lang_statement_union_type *s AND
847 CONST char *target AND
848 lang_output_section_statement_type *output_section_statement)
849 {
850 for (; s != (lang_statement_union_type *)NULL ; s = s->next)
851 {
852 switch (s->header.type) {
853
854
855 case lang_wild_statement_enum:
856 wild(&s->wild_statement, s->wild_statement.section_name,
857 s->wild_statement.filename, target,
858 output_section_statement);
859
860 break;
861 case lang_constructors_statement_enum:
862 map_input_to_output_sections(constructor_list.head,
863 target,
864 output_section_statement);
865 break;
866 case lang_output_section_statement_enum:
867 map_input_to_output_sections(s->output_section_statement.children.head,
868 target,
869 &s->output_section_statement);
870 break;
871 case lang_output_statement_enum:
872 break;
873 case lang_target_statement_enum:
874 target = s->target_statement.target;
875 break;
876 case lang_fill_statement_enum:
877 case lang_input_section_enum:
878 case lang_object_symbols_statement_enum:
879 case lang_data_statement_enum:
880 case lang_assignment_statement_enum:
881 case lang_padding_statement_enum:
882 break;
883 case lang_afile_asection_pair_statement_enum:
884 FAIL();
885 break;
886 case lang_address_statement_enum:
887 /* Mark the specified section with the supplied address */
888 {
889 lang_output_section_statement_type *os =
890 lang_output_section_statement_lookup
891 (s->address_statement.section_name);
892 os->addr_tree = s->address_statement.address;
893 if (os->bfd_section == (asection *)NULL) {
894 info("%P%F can't set the address of undefined section %s\n",
895 s->address_statement.section_name);
896 }
897 }
898 break;
899 case lang_input_statement_enum:
900 /* A standard input statement, has no wildcards */
901 /* ldmain_open_file_read_symbol(&s->input_statement);*/
902 break;
903 }
904 }
905 }
906
907
908
909
910
911 static void
912 DEFUN(print_output_section_statement,(output_section_statement),
913 lang_output_section_statement_type *output_section_statement)
914 {
915 asection *section = output_section_statement->bfd_section;
916 print_nl();
917 print_section(output_section_statement->name);
918
919 if (section) {
920 print_dot = section->vma;
921 print_space();
922 print_section("");
923 print_space();
924 print_address(section->vma);
925 print_space();
926 print_size(section->size);
927 print_space();
928 print_alignment(section->alignment_power);
929 print_space();
930 #if 0
931 printf("%s flags", output_section_statement->region->name);
932 print_flags(stdout, &output_section_statement->flags);
933 #endif
934 if (section->flags & SEC_LOAD)
935 printf("load ");
936 if (section->flags & SEC_ALLOC)
937 printf("alloc ");
938 if (section->flags & SEC_RELOC)
939 printf("reloc ");
940 if (section->flags & SEC_HAS_CONTENTS)
941 printf("contents ");
942
943 }
944 else {
945 printf("No attached output section");
946 }
947 print_nl();
948 print_statement(output_section_statement->children.head,
949 output_section_statement);
950
951 }
952
953 static void
954 DEFUN(print_assignment,(assignment, output_section),
955 lang_assignment_statement_type *assignment AND
956 lang_output_section_statement_type *output_section)
957 {
958 etree_value_type result;
959 print_section("");
960 print_space();
961 print_section("");
962 print_space();
963 print_address(print_dot);
964 print_space();
965 result = exp_fold_tree(assignment->exp->assign.src,
966 output_section,
967 lang_final_phase_enum,
968 print_dot,
969 &print_dot);
970
971 if (result.valid) {
972 print_address(result.value);
973 }
974 else
975 {
976 printf("*undefined*");
977 }
978 print_space();
979 exp_print_tree(stdout, assignment->exp);
980 printf("\n");
981 }
982
983 static void
984 DEFUN(print_input_statement,(statm),
985 lang_input_statement_type *statm)
986 {
987 if (statm->filename != (char *)NULL) {
988 printf("LOAD %s\n",statm->filename);
989 }
990 }
991
992 static void
993 DEFUN(print_symbol,(q),
994 asymbol *q)
995 {
996 print_section("");
997 printf(" ");
998 print_section("");
999 printf(" ");
1000 print_address(outside_symbol_address(q));
1001 printf(" %s", q->name ? q->name : " ");
1002 print_nl();
1003 }
1004
1005 static void
1006 DEFUN(print_input_section,(in),
1007 lang_input_section_type *in)
1008 {
1009 asection *i = in->section;
1010
1011 if(i->size != 0) {
1012 print_section("");
1013 printf(" ");
1014 print_section(i->name);
1015 printf(" ");
1016 if (i->output_section) {
1017 print_address(i->output_section->vma + i->output_offset);
1018 printf(" ");
1019 print_size(i->size);
1020 printf(" ");
1021 print_alignment(i->alignment_power);
1022 printf(" ");
1023 if (in->ifile) {
1024
1025 bfd *abfd = in->ifile->the_bfd;
1026 if (in->ifile->just_syms_flag == true) {
1027 printf("symbols only ");
1028 }
1029
1030 printf(" %s ",abfd->xvec->name);
1031 if(abfd->my_archive != (bfd *)NULL) {
1032 printf("[%s]%s", abfd->my_archive->filename,
1033 abfd->filename);
1034 }
1035 else {
1036 printf("%s", abfd->filename);
1037 }
1038 printf("(overhead %d bytes)", (int)bfd_alloc_size(abfd));
1039 print_nl();
1040
1041 /* Find all the symbols in this file defined in this section */
1042 {
1043 asymbol **p;
1044 for (p = in->ifile->asymbols; *p; p++) {
1045 asymbol *q = *p;
1046
1047 if (bfd_get_section(q) == i && q->flags & BSF_GLOBAL) {
1048 print_symbol(q);
1049 }
1050 }
1051 }
1052 }
1053 else {
1054 print_nl();
1055 }
1056
1057
1058 print_dot = outside_section_address(i) + i->size;
1059 }
1060 else {
1061 printf("No output section allocated\n");
1062 }
1063 }
1064 }
1065
1066 static void
1067 DEFUN(print_fill_statement,(fill),
1068 lang_fill_statement_type *fill)
1069 {
1070 printf("FILL mask ");
1071 print_fill( fill->fill);
1072 }
1073
1074 static void
1075 DEFUN(print_data_statement,(data),
1076 lang_data_statement_type *data)
1077 {
1078 /* bfd_vma value; */
1079 print_section("");
1080 print_space();
1081 print_section("");
1082 print_space();
1083 /* ASSERT(print_dot == data->output_vma);*/
1084
1085 print_address(data->output_vma);
1086 print_space();
1087 print_address(data->value);
1088 print_space();
1089 switch (data->type) {
1090 case BYTE :
1091 printf("BYTE ");
1092 print_dot += BYTE_SIZE;
1093 break;
1094 case SHORT:
1095 printf("SHORT ");
1096 print_dot += SHORT_SIZE;
1097 break;
1098 case LONG:
1099 printf("LONG ");
1100 print_dot += LONG_SIZE;
1101 break;
1102 }
1103
1104 exp_print_tree(stdout, data->exp);
1105
1106 printf("\n");
1107 }
1108
1109
1110 static void
1111 DEFUN(print_padding_statement,(s),
1112 lang_padding_statement_type *s)
1113 {
1114 print_section("");
1115 print_space();
1116 print_section("*fill*");
1117 print_space();
1118 print_address(s->output_offset + s->output_section->vma);
1119 print_space();
1120 print_size(s->size);
1121 print_space();
1122 print_fill(s->fill);
1123 print_nl();
1124 }
1125
1126 static void
1127 DEFUN(print_wild_statement,(w,os),
1128 lang_wild_statement_type *w AND
1129 lang_output_section_statement_type *os)
1130 {
1131 printf(" from ");
1132 if (w->filename != (char *)NULL) {
1133 printf("%s",w->filename);
1134 }
1135 else {
1136 printf("*");
1137 }
1138 if (w->section_name != (char *)NULL) {
1139 printf("(%s)",w->section_name);
1140 }
1141 else {
1142 printf("(*)");
1143 }
1144 print_nl();
1145 print_statement(w->children.head, os);
1146
1147 }
1148 static void
1149 DEFUN(print_statement,(s, os),
1150 lang_statement_union_type *s AND
1151 lang_output_section_statement_type *os)
1152 {
1153 while (s) {
1154 switch (s->header.type) {
1155 case lang_constructors_statement_enum:
1156 printf("constructors:\n");
1157 print_statement(constructor_list.head, os);
1158 break;
1159
1160 case lang_wild_statement_enum:
1161 print_wild_statement(&s->wild_statement, os);
1162 break;
1163 default:
1164 printf("Fail with %d\n",s->header.type);
1165 FAIL();
1166 break;
1167 case lang_address_statement_enum:
1168 printf("address\n");
1169 break;
1170 break;
1171 case lang_object_symbols_statement_enum:
1172 printf("object symbols\n");
1173 break;
1174 case lang_fill_statement_enum:
1175 print_fill_statement(&s->fill_statement);
1176 break;
1177 case lang_data_statement_enum:
1178 print_data_statement(&s->data_statement);
1179 break;
1180 case lang_input_section_enum:
1181 print_input_section(&s->input_section);
1182 break;
1183 case lang_padding_statement_enum:
1184 print_padding_statement(&s->padding_statement);
1185 break;
1186 case lang_output_section_statement_enum:
1187 print_output_section_statement(&s->output_section_statement);
1188 break;
1189 case lang_assignment_statement_enum:
1190 print_assignment(&s->assignment_statement,
1191 os);
1192 break;
1193
1194
1195 case lang_target_statement_enum:
1196 printf("TARGET(%s)\n", s->target_statement.target);
1197 break;
1198 case lang_output_statement_enum:
1199 printf("OUTPUT(%s %s)\n",
1200 s->output_statement.name,
1201 output_target);
1202 break;
1203 case lang_input_statement_enum:
1204 print_input_statement(&s->input_statement);
1205 break;
1206 case lang_afile_asection_pair_statement_enum:
1207 FAIL();
1208 break;
1209 }
1210 s = s->next;
1211 }
1212 }
1213
1214
1215 static void
1216 DEFUN_VOID(print_statements)
1217 {
1218 print_statement(statement_list.head,
1219 (lang_output_section_statement_type *)NULL);
1220 }
1221
1222 static bfd_vma
1223 DEFUN(insert_pad,(this_ptr, fill, power, output_section_statement, dot),
1224 lang_statement_union_type **this_ptr AND
1225 fill_type fill AND
1226 unsigned int power AND
1227 asection * output_section_statement AND
1228 bfd_vma dot)
1229 {
1230 /* Align this section first to the
1231 input sections requirement, then
1232 to the output section's requirement.
1233 If this alignment is > than any seen before,
1234 then record it too. Perform the alignment by
1235 inserting a magic 'padding' statement.
1236 */
1237
1238 unsigned int alignment_needed = align_power(dot, power) - dot;
1239
1240 if (alignment_needed != 0)
1241 {
1242 lang_statement_union_type *new =
1243 (lang_statement_union_type *)
1244 ldmalloc((bfd_size_type)(sizeof(lang_padding_statement_type)));
1245 /* Link into existing chain */
1246 new->header.next = *this_ptr;
1247 *this_ptr = new;
1248 new->header.type = lang_padding_statement_enum;
1249 new->padding_statement.output_section = output_section_statement;
1250 new->padding_statement.output_offset =
1251 dot - output_section_statement->vma;
1252 new->padding_statement.fill = fill;
1253 new->padding_statement.size = alignment_needed;
1254 }
1255
1256
1257 /* Remember the most restrictive alignment */
1258 if (power > output_section_statement->alignment_power) {
1259 output_section_statement->alignment_power = power;
1260 }
1261 output_section_statement->size += alignment_needed;
1262 return alignment_needed + dot;
1263
1264 }
1265
1266 /* Work out how much this section will move the dot point */
1267 static bfd_vma
1268 DEFUN(size_input_section, (this_ptr, output_section_statement, fill, dot),
1269 lang_statement_union_type **this_ptr AND
1270 lang_output_section_statement_type*output_section_statement AND
1271 unsigned short fill AND
1272 bfd_vma dot)
1273 {
1274 lang_input_section_type *is = &((*this_ptr)->input_section);
1275 asection *i = is->section;
1276
1277 if (is->ifile->just_syms_flag == false) {
1278 dot = insert_pad(this_ptr, fill, i->alignment_power,
1279 output_section_statement->bfd_section, dot);
1280
1281 /* remember the largest size so we can malloc the largest area
1282 needed for the output stage. Only remember the size of sections
1283 which we will actually allocate */
1284 if (((i->flags &
1285 (SEC_HAS_CONTENTS | SEC_LOAD)) == (SEC_HAS_CONTENTS | SEC_LOAD))
1286 && (i->size > largest_section)) {
1287 largest_section = i->size;
1288 }
1289
1290 /* Remember where in the output section this input section goes */
1291
1292 i->output_offset = dot - output_section_statement->bfd_section->vma;
1293
1294 /* Mark how big the output section must be to contain this now */
1295 dot += i->size;
1296 output_section_statement->bfd_section->size =
1297 dot - output_section_statement->bfd_section->vma;
1298 }
1299 else
1300 {
1301 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
1302 }
1303
1304 return dot ;
1305 }
1306
1307
1308 /* Work out the size of the output sections
1309 from the sizes of the input sections */
1310 static bfd_vma
1311 DEFUN(lang_size_sections,(s, output_section_statement, prev, fill, dot),
1312 lang_statement_union_type *s AND
1313 lang_output_section_statement_type * output_section_statement AND
1314 lang_statement_union_type **prev AND
1315 unsigned short fill AND
1316 bfd_vma dot)
1317 {
1318 /* Size up the sections from their constituent parts */
1319 for (; s != (lang_statement_union_type *)NULL ; s = s->next)
1320 {
1321 switch (s->header.type) {
1322
1323
1324 case lang_output_section_statement_enum:
1325 {
1326 bfd_vma after;
1327 lang_output_section_statement_type *os =
1328 &(s->output_section_statement);
1329 /* The start of a section */
1330
1331 if (os->addr_tree == (etree_type *)NULL) {
1332 /* No address specified for this section, get one
1333 from the region specification
1334 */
1335 if (os->region == (lang_memory_region_type *)NULL) {
1336 os->region = lang_memory_region_lookup("*default*");
1337 }
1338 dot = os->region->current;
1339 }
1340 else {
1341 etree_value_type r ;
1342 r = exp_fold_tree(os->addr_tree,
1343 (lang_output_section_statement_type *)NULL,
1344 lang_allocating_phase_enum,
1345 dot, &dot);
1346 if (r.valid == false) {
1347 info("%F%S: non constant address expression for section %s\n",
1348 os->name);
1349 }
1350 dot = r.value;
1351 }
1352 /* The section starts here */
1353 /* First, align to what the section needs */
1354
1355 dot = align_power(dot, os->bfd_section->alignment_power);
1356 os->bfd_section->vma = dot;
1357 os->bfd_section->output_offset = 0;
1358
1359 (void) lang_size_sections(os->children.head, os, &os->children.head,
1360 os->fill, dot);
1361 /* Ignore the size of the input sections, use the vma and size to */
1362 /* align against */
1363
1364
1365 after = ALIGN(os->bfd_section->vma +
1366 os->bfd_section->size,
1367 os->block_value) ;
1368
1369
1370 os->bfd_section->size = after - os->bfd_section->vma;
1371 dot = os->bfd_section->vma + os->bfd_section->size;
1372 os->processed = true;
1373
1374 /* Replace into region ? */
1375 if (os->addr_tree == (etree_type *)NULL
1376 && os->region !=(lang_memory_region_type*)NULL ) {
1377 os->region->current = dot;
1378 }
1379 }
1380
1381 break;
1382 case lang_constructors_statement_enum:
1383 dot = lang_size_sections(constructor_list.head,
1384 output_section_statement,
1385 &s->wild_statement.children.head,
1386 fill,
1387 dot);
1388 break;
1389
1390 case lang_data_statement_enum:
1391 {
1392 unsigned int size;
1393 s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma;
1394 s->data_statement.output_section =
1395 output_section_statement->bfd_section;
1396
1397 switch (s->data_statement.type) {
1398 case LONG:
1399 size = LONG_SIZE;
1400 break;
1401 case SHORT:
1402 size = SHORT_SIZE;
1403 break;
1404 case BYTE:
1405 size = BYTE_SIZE;
1406 break;
1407
1408 }
1409 dot += size;
1410 output_section_statement->bfd_section->size += size;
1411 }
1412 break;
1413
1414 case lang_wild_statement_enum:
1415
1416 dot = lang_size_sections(s->wild_statement.children.head,
1417 output_section_statement,
1418 &s->wild_statement.children.head,
1419
1420 fill, dot);
1421
1422 break;
1423
1424 case lang_object_symbols_statement_enum:
1425 create_object_symbols = output_section_statement;
1426 break;
1427 case lang_output_statement_enum:
1428 case lang_target_statement_enum:
1429 break;
1430 case lang_input_section_enum:
1431 dot = size_input_section(prev,
1432 output_section_statement,
1433 output_section_statement->fill, dot);
1434 break;
1435 case lang_input_statement_enum:
1436 break;
1437 case lang_fill_statement_enum:
1438 fill = s->fill_statement.fill;
1439 break;
1440 case lang_assignment_statement_enum:
1441 {
1442 bfd_vma newdot = dot;
1443 exp_fold_tree(s->assignment_statement.exp,
1444 output_section_statement,
1445 lang_allocating_phase_enum,
1446 dot,
1447 &newdot);
1448
1449 if (newdot != dot)
1450 /* We've been moved ! so insert a pad */
1451 {
1452 lang_statement_union_type *new =
1453 (lang_statement_union_type *)
1454 ldmalloc((bfd_size_type)(sizeof(lang_padding_statement_type)));
1455 /* Link into existing chain */
1456 new->header.next = *prev;
1457 *prev = new;
1458 new->header.type = lang_padding_statement_enum;
1459 new->padding_statement.output_section =
1460 output_section_statement->bfd_section;
1461 new->padding_statement.output_offset =
1462 dot - output_section_statement->bfd_section->vma;
1463 new->padding_statement.fill = fill;
1464 new->padding_statement.size = newdot - dot;
1465 output_section_statement->bfd_section->size +=
1466 new->padding_statement.size;
1467 dot = newdot;
1468 }
1469 }
1470
1471 break;
1472 case lang_padding_statement_enum:
1473 FAIL();
1474 break;
1475 default:
1476 FAIL();
1477 break;
1478 case lang_address_statement_enum:
1479 break;
1480 }
1481 prev = &s->header.next;
1482 }
1483 return dot;
1484 }
1485
1486
1487 static bfd_vma
1488 DEFUN(lang_do_assignments,(s, output_section_statement, fill, dot),
1489 lang_statement_union_type *s AND
1490 lang_output_section_statement_type * output_section_statement AND
1491 unsigned short fill AND
1492 bfd_vma dot)
1493 {
1494
1495 for (; s != (lang_statement_union_type *)NULL ; s = s->next)
1496 {
1497 switch (s->header.type) {
1498 case lang_constructors_statement_enum:
1499 dot = lang_do_assignments(constructor_list.head,
1500 output_section_statement,
1501 fill,
1502 dot);
1503 break;
1504
1505 case lang_output_section_statement_enum:
1506 {
1507 lang_output_section_statement_type *os =
1508 &(s->output_section_statement);
1509 dot = os->bfd_section->vma;
1510 (void) lang_do_assignments(os->children.head, os, os->fill, dot);
1511 dot = os->bfd_section->vma + os->bfd_section->size;
1512 }
1513 break;
1514 case lang_wild_statement_enum:
1515
1516 dot = lang_do_assignments(s->wild_statement.children.head,
1517 output_section_statement,
1518 fill, dot);
1519
1520 break;
1521
1522 case lang_object_symbols_statement_enum:
1523 case lang_output_statement_enum:
1524 case lang_target_statement_enum:
1525 #if 0
1526 case lang_common_statement_enum:
1527 #endif
1528 break;
1529 case lang_data_statement_enum:
1530 {
1531 etree_value_type value ;
1532 value = exp_fold_tree(s->data_statement.exp,
1533 0, lang_final_phase_enum, dot, &dot);
1534 s->data_statement.value = value.value;
1535 if (value.valid == false) info("%F%P: Invalid data statement\n");
1536 }
1537 switch (s->data_statement.type) {
1538 case LONG:
1539 dot += LONG_SIZE;
1540 break;
1541 case SHORT:
1542 dot += SHORT_SIZE;
1543 break;
1544 case BYTE:
1545 dot += BYTE_SIZE;
1546 break;
1547 }
1548 break;
1549 case lang_input_section_enum:
1550 {
1551 asection *in = s->input_section.section;
1552 dot += in->size;
1553 }
1554 break;
1555
1556 case lang_input_statement_enum:
1557 break;
1558 case lang_fill_statement_enum:
1559 fill = s->fill_statement.fill;
1560 break;
1561 case lang_assignment_statement_enum:
1562 {
1563 exp_fold_tree(s->assignment_statement.exp,
1564 output_section_statement,
1565 lang_final_phase_enum,
1566 dot,
1567 &dot);
1568 }
1569
1570 break;
1571 case lang_padding_statement_enum:
1572 dot += s->padding_statement.size;
1573 break;
1574 default:
1575 FAIL();
1576 break;
1577 case lang_address_statement_enum:
1578 break;
1579 }
1580
1581 }
1582 return dot;
1583 }
1584
1585
1586
1587 static void
1588 DEFUN_VOID(lang_relocate_globals)
1589 {
1590
1591 /*
1592 Each ldsym_type maintains a chain of pointers to asymbols which
1593 references the definition. Replace each pointer to the referenence
1594 with a pointer to only one place, preferably the definition. If
1595 the defintion isn't available then the common symbol, and if
1596 there isn't one of them then choose one reference.
1597 */
1598
1599 FOR_EACH_LDSYM(lgs) {
1600 asymbol *it;
1601 if (lgs->sdefs_chain) {
1602 it = *(lgs->sdefs_chain);
1603 }
1604 else if (lgs->scoms_chain != (asymbol **)NULL) {
1605 it = *(lgs->scoms_chain);
1606 }
1607 else if (lgs->srefs_chain != (asymbol **)NULL) {
1608 it = *(lgs->srefs_chain);
1609 }
1610 else {
1611 /* This can happen when the command line asked for a symbol to
1612 be -u */
1613 it = (asymbol *)NULL;
1614 }
1615 if (it != (asymbol *)NULL)
1616 {
1617 asymbol **ptr = lgs->srefs_chain;;
1618 if (lgs->flags & SYM_WARNING)
1619 {
1620 produce_warnings(lgs, it);
1621 }
1622
1623 while (ptr != (asymbol **)NULL) {
1624 asymbol *ref = *ptr;
1625 *ptr = it;
1626 ptr = (asymbol **)(ref->udata);
1627 }
1628 }
1629 }
1630 }
1631
1632
1633
1634 static void
1635 DEFUN_VOID(lang_finish)
1636 {
1637 ldsym_type *lgs;
1638
1639 if (entry_symbol == (char *)NULL) {
1640 /* No entry has been specified, look for start */
1641 entry_symbol = "start";
1642 }
1643 lgs = ldsym_get_soft(entry_symbol);
1644 if (lgs && lgs->sdefs_chain) {
1645 asymbol *sy = *(lgs->sdefs_chain);
1646 /* We can set the entry address*/
1647 bfd_set_start_address(output_bfd,
1648 outside_symbol_address(sy));
1649
1650 }
1651 else {
1652 /* Can't find anything reasonable,
1653 use the first address in the text section
1654 */
1655 asection *ts = bfd_get_section_by_name(output_bfd, ".text");
1656 if (ts) {
1657 bfd_set_start_address(output_bfd, ts->vma);
1658 }
1659 }
1660 }
1661
1662 /* By now we know the target architecture, and we may have an */
1663 /* ldfile_output_machine_name */
1664 static void
1665 DEFUN_VOID(lang_check)
1666 {
1667 lang_statement_union_type *file;
1668
1669 bfd * input_bfd;
1670 unsigned long input_machine;
1671 enum bfd_architecture input_architecture;
1672
1673 CONST bfd_arch_info_type *compatible;
1674
1675 for (file = file_chain.head;
1676 file != (lang_statement_union_type *)NULL;
1677 file=file->input_statement.next)
1678 {
1679 unsigned long ldfile_new_output_machine = 0;
1680 enum bfd_architecture ldfile_new_output_architecture = bfd_arch_unknown;
1681
1682 input_bfd = file->input_statement.the_bfd;
1683
1684 input_machine = bfd_get_mach(input_bfd);
1685 input_architecture = bfd_get_arch(input_bfd);
1686
1687
1688 /* Inspect the architecture and ensure we're linking like with
1689 like */
1690
1691 compatible=bfd_arch_get_compatible(input_bfd,
1692 output_bfd);
1693
1694 if (compatible)
1695 {
1696 ldfile_output_machine = compatible->mach;
1697 ldfile_output_architecture = compatible->arch;
1698 }
1699 else
1700 {
1701
1702 info("%P: warning, %s architecture of input file `%B' incompatible with %s output\n",
1703 bfd_printable_name(input_bfd), input_bfd,
1704 bfd_printable_name(output_bfd));
1705
1706 bfd_set_arch_mach(output_bfd,
1707 ldfile_new_output_architecture,
1708 ldfile_new_output_machine);
1709 }
1710
1711 }
1712 }
1713
1714
1715 /*
1716 * run through all the global common symbols and tie them
1717 * to the output section requested.
1718 *
1719 As an experiment we do this 4 times, once for all the byte sizes,
1720 then all the two bytes, all the four bytes and then everything else
1721 */
1722
1723 static void
1724 DEFUN_VOID(lang_common)
1725 {
1726 ldsym_type *lgs;
1727 size_t power;
1728 if (config.relocateable_output == false ||
1729 command_line.force_common_definition== true) {
1730 for (power = 1; (config.sort_common == true && power == 1) || (power <= 16); power <<=1) {
1731 for (lgs = symbol_head;
1732 lgs != (ldsym_type *)NULL;
1733 lgs=lgs->next)
1734 {
1735 asymbol *com ;
1736 unsigned int power_of_two;
1737 size_t size;
1738 size_t align;
1739 if (lgs->scoms_chain != (asymbol **)NULL) {
1740 com = *(lgs->scoms_chain);
1741 size = com->value;
1742 switch (size) {
1743 case 0:
1744 case 1:
1745 align = 1;
1746 power_of_two = 0;
1747 break;
1748 case 2:
1749 power_of_two = 1;
1750 align = 2;
1751 break;
1752 case 3:
1753 case 4:
1754 power_of_two =2;
1755 align = 4;
1756 break;
1757 case 5:
1758 case 6:
1759 case 7:
1760 case 8:
1761 power_of_two = 3;
1762 align = 8;
1763 break;
1764 default:
1765 power_of_two = 4;
1766 align = 16;
1767 break;
1768 }
1769 if (config.sort_common == false || align == power) {
1770 /* Change from a common symbol into a definition of
1771 a symbol */
1772 lgs->sdefs_chain = lgs->scoms_chain;
1773 lgs->scoms_chain = (asymbol **)NULL;
1774 commons_pending--;
1775 /* Point to the correct common section */
1776 com->section =
1777 ((lang_input_statement_type *)
1778 (com->the_bfd->usrdata))->common_section;
1779 /* Fix the size of the common section */
1780 com->section->size = ALIGN(com->section->size, align);
1781
1782 /* Remember if this is the biggest alignment ever seen */
1783 if (power_of_two > com->section->alignment_power) {
1784 com->section->alignment_power = power_of_two;
1785 }
1786
1787 /* Symbol stops being common and starts being global, but
1788 we remember that it was common once. */
1789
1790 com->flags = BSF_EXPORT | BSF_GLOBAL | BSF_OLD_COMMON;
1791 com->value = com->section->size;
1792
1793 if (write_map)
1794 {
1795 printf ("Allocating common %s: %x at %x %s\n",
1796 lgs->name,
1797 (unsigned) size,
1798 (unsigned) com->value,
1799 com->the_bfd->filename);
1800 }
1801
1802 com->section->size += size;
1803
1804 }
1805 }
1806
1807 }
1808 }
1809 }
1810
1811
1812 }
1813
1814 /*
1815 run through the input files and ensure that every input
1816 section has somewhere to go. If one is found without
1817 a destination then create an input request and place it
1818 into the statement tree.
1819 */
1820
1821 static void
1822 DEFUN_VOID(lang_place_orphans)
1823 {
1824 lang_input_statement_type *file;
1825 for (file = (lang_input_statement_type*)file_chain.head;
1826 file != (lang_input_statement_type*)NULL;
1827 file = (lang_input_statement_type*)file->next) {
1828 asection *s;
1829 for (s = file->the_bfd->sections;
1830 s != (asection *)NULL;
1831 s = s->next) {
1832 if ( s->output_section == (asection *)NULL) {
1833 /* This section of the file is not attatched, root
1834 around for a sensible place for it to go */
1835
1836 if (file->common_section == s) {
1837 /* This is a lonely common section which must
1838 have come from an archive. We attatch to the
1839 section with the wildcard */
1840 if (config.relocateable_output != true
1841 && command_line.force_common_definition == false) {
1842 if (default_common_section ==
1843 (lang_output_section_statement_type *)NULL) {
1844 info("%P: No [COMMON] command, defaulting to .bss\n");
1845
1846 default_common_section =
1847 lang_output_section_statement_lookup(".bss");
1848
1849 }
1850 wild_doit(&default_common_section->children, s,
1851 default_common_section, file);
1852 }
1853 }
1854 else {
1855 lang_output_section_statement_type *os =
1856 lang_output_section_statement_lookup(s->name);
1857
1858 wild_doit(&os->children, s, os, file);
1859 }
1860 }
1861 }
1862 }
1863 }
1864
1865
1866 void
1867 DEFUN(lang_set_flags,(ptr, flags),
1868 int *ptr AND
1869 CONST char *flags)
1870 {
1871 boolean state = true;
1872 *ptr= 0;
1873 while (*flags)
1874 {
1875 if (*flags == '!') {
1876 state = false;
1877 flags++;
1878 }
1879 else state = true;
1880 switch (*flags) {
1881 case 'R':
1882 /* ptr->flag_read = state; */
1883 break;
1884 case 'W':
1885 /* ptr->flag_write = state; */
1886 break;
1887 case 'X':
1888 /* ptr->flag_executable= state;*/
1889 break;
1890 case 'L':
1891 case 'I':
1892 /* ptr->flag_loadable= state;*/
1893 break;
1894 default:
1895 info("%P%F illegal syntax in flags\n");
1896 break;
1897 }
1898 flags++;
1899 }
1900 }
1901
1902
1903
1904 void
1905 DEFUN(lang_for_each_file,(func),
1906 PROTO(void, (*func),(lang_input_statement_type *)))
1907 {
1908 lang_input_statement_type *f;
1909 for (f = (lang_input_statement_type *)file_chain.head;
1910 f != (lang_input_statement_type *)NULL;
1911 f = (lang_input_statement_type *)f->next)
1912 {
1913 func(f);
1914 }
1915 }
1916
1917
1918 void
1919 DEFUN(lang_for_each_input_section, (func),
1920 PROTO(void ,(*func),(bfd *ab, asection*as)))
1921 {
1922 lang_input_statement_type *f;
1923 for (f = (lang_input_statement_type *)file_chain.head;
1924 f != (lang_input_statement_type *)NULL;
1925 f = (lang_input_statement_type *)f->next)
1926 {
1927 asection *s;
1928 for (s = f->the_bfd->sections;
1929 s != (asection *)NULL;
1930 s = s->next) {
1931 func(f->the_bfd, s);
1932 }
1933 }
1934 }
1935
1936
1937
1938 void
1939 DEFUN(ldlang_add_file,(entry),
1940 lang_input_statement_type *entry)
1941 {
1942
1943 lang_statement_append(&file_chain,
1944 (lang_statement_union_type *)entry,
1945 &entry->next);
1946 }
1947
1948
1949
1950 void
1951 DEFUN(lang_add_output,(name),
1952 CONST char *name)
1953 {
1954 lang_output_statement_type *new = new_stat(lang_output_statement,
1955 stat_ptr);
1956 new->name = name;
1957 had_output_filename = true;
1958 }
1959
1960
1961 static lang_output_section_statement_type *current_section;
1962
1963 void
1964 DEFUN(lang_enter_output_section_statement,
1965 (output_section_statement_name,
1966 address_exp,
1967 flags,
1968 block_value),
1969 char *output_section_statement_name AND
1970 etree_type *address_exp AND
1971 int flags AND
1972 bfd_vma block_value)
1973 {
1974 lang_output_section_statement_type *os;
1975 current_section =
1976 os =
1977 lang_output_section_statement_lookup(output_section_statement_name);
1978
1979
1980
1981 /* Add this statement to tree */
1982 /* add_statement(lang_output_section_statement_enum,
1983 output_section_statement);*/
1984 /* Make next things chain into subchain of this */
1985
1986 if (os->addr_tree ==
1987 (etree_type *)NULL) {
1988 os->addr_tree =
1989 address_exp;
1990 }
1991 os->flags = flags;
1992 os->block_value = block_value;
1993 stat_ptr = & os->children;
1994
1995 }
1996
1997
1998 void
1999 DEFUN_VOID(lang_final)
2000 {
2001 if (had_output_filename == false) {
2002 extern CONST char *output_filename;
2003 lang_add_output(output_filename);
2004 }
2005 }
2006
2007
2008
2009
2010
2011 asymbol *
2012 DEFUN(create_symbol,(name, flags, section),
2013 CONST char *name AND
2014 flagword flags AND
2015 asection *section)
2016 {
2017 extern lang_input_statement_type *script_file;
2018 asymbol **def_ptr = (asymbol **)ldmalloc((bfd_size_type)(sizeof(asymbol **)));
2019 /* Add this definition to script file */
2020 asymbol *def = (asymbol *)bfd_make_empty_symbol(script_file->the_bfd);
2021 def->name = buystring(name);
2022 def->udata = 0;
2023 def->flags = flags;
2024 def->section = section;
2025
2026 *def_ptr = def;
2027 Q_enter_global_ref(def_ptr);
2028 return def;
2029 }
2030
2031 /* run through the symbol table, find all the symbols which are
2032 constructors and for each one, create statements to do something
2033 like..
2034
2035 for
2036 __CTOR_LIST__, foo
2037
2038 __CTOR_LIST__ = . ;
2039 LONG(__CTOR_LIST_END - . / 4 - 2)
2040 *(foo)
2041 __CTOR_LIST_END= .
2042
2043 Put these statements onto a special list.
2044
2045 */
2046
2047 typedef struct constructor_list
2048 {
2049 ldsym_type *sym;
2050 struct constructor_list *next;
2051 } constructor_list_type;
2052
2053 static constructor_list_type *constructor_name_list;
2054
2055 void
2056 DEFUN(ldlang_add_constructor,(name),
2057 ldsym_type *name)
2058 {
2059
2060 constructor_list_type *next = constructor_name_list;
2061
2062 if (name->flags & SYM_CONSTRUCTOR) return;
2063
2064 next = (constructor_list_type *) ldmalloc(sizeof(constructor_list_type));
2065 next->next= constructor_name_list;
2066 next->sym= name;
2067 name->flags |= SYM_CONSTRUCTOR;
2068 constructor_name_list = next;
2069
2070 }
2071
2072 void
2073 DEFUN_VOID(find_constructors)
2074 {
2075 lang_statement_list_type *old = stat_ptr;
2076 constructor_list_type *p = constructor_name_list;
2077 stat_ptr = & constructor_list;
2078 lang_list_init(stat_ptr);
2079 while (p != (constructor_list_type *)NULL)
2080 {
2081 /* Have we already done this one ? */
2082 CONST char *name = p->sym->name;
2083 int len = strlen(name);
2084 char *end = ldmalloc(len+3);
2085 strcpy(end, name);
2086 strcat(end,"$e");
2087
2088 lang_add_assignment
2089 ( exp_assop('=',name, exp_nameop(NAME,".")));
2090
2091 lang_add_data
2092 (LONG, exp_binop('-',
2093 exp_binop ( '/',
2094 exp_binop ( '-',
2095 exp_nameop(NAME, end),
2096 exp_nameop(NAME,".")),
2097 exp_intop(4)),
2098
2099 exp_intop(2)));
2100
2101
2102 lang_add_wild(name, (char *)NULL);
2103 lang_add_data(LONG, exp_intop(0));
2104 lang_add_assignment
2105 (exp_assop('=', end, exp_nameop(NAME,".")));
2106 p = p->next;
2107 }
2108
2109
2110
2111
2112 stat_ptr = old;
2113 }
2114 void
2115 DEFUN_VOID(lang_process)
2116 {
2117 if (had_script == false) {
2118 parse_line(ldemul_get_script());
2119 }
2120 lang_reasonable_defaults();
2121 current_target = default_target;
2122
2123 lang_for_each_statement(ldlang_open_output); /* Open the output file */
2124 /* For each output section statement, create a section in the output
2125 file */
2126 lang_create_output_section_statements();
2127
2128 /* Create a dummy bfd for the script */
2129 lang_init_script_file();
2130
2131 /* Add to the hash table all undefineds on the command line */
2132 lang_place_undefineds();
2133
2134 /* Create a bfd for each input file */
2135 current_target = default_target;
2136 lang_for_each_statement(open_input_bfds);
2137
2138 common_section.userdata = (PTR)&common_section_userdata;
2139
2140
2141 /* Run through the contours of the script and attatch input sections
2142 to the correct output sections
2143 */
2144 find_constructors();
2145 map_input_to_output_sections(statement_list.head, (char *)NULL,
2146 ( lang_output_section_statement_type *)NULL);
2147
2148
2149 /* Find any sections not attatched explicitly and handle them */
2150 lang_place_orphans();
2151
2152 /* Size up the common data */
2153 lang_common();
2154
2155 ldemul_before_allocation();
2156
2157 /* Size up the sections */
2158 lang_size_sections(statement_list.head,
2159 (lang_output_section_statement_type *)NULL,
2160 &(statement_list.head), 0, (bfd_vma)0);
2161
2162 /* See if anything special should be done now we know how big
2163 everything is */
2164 ldemul_after_allocation();
2165
2166 /* Do all the assignments, now that we know the final restingplaces
2167 of all the symbols */
2168
2169 lang_do_assignments(statement_list.head,
2170 (lang_output_section_statement_type *)NULL,
2171 0, (bfd_vma)0);
2172
2173 /* Make sure that we're not mixing architectures */
2174
2175 lang_check();
2176
2177 /* Move the global symbols around */
2178 lang_relocate_globals();
2179
2180 /* Final stuffs */
2181 lang_finish();
2182 }
2183
2184
2185 /* EXPORTED TO YACC */
2186
2187 void
2188 DEFUN(lang_add_wild,(section_name, filename),
2189 CONST char *CONST section_name AND
2190 CONST char *CONST filename)
2191 {
2192 lang_wild_statement_type *new = new_stat(lang_wild_statement,
2193 stat_ptr);
2194
2195 if (section_name != (char *)NULL && strcmp(section_name,"COMMON") == 0)
2196 {
2197 placed_commons = true;
2198 }
2199 if (filename != (char *)NULL) {
2200 lang_has_input_file = true;
2201 }
2202 new->section_name = section_name;
2203 new->filename = filename;
2204 lang_list_init(&new->children);
2205 }
2206 void
2207 DEFUN(lang_section_start,(name, address),
2208 CONST char *name AND
2209 etree_type *address)
2210 {
2211 lang_address_statement_type *ad =new_stat(lang_address_statement, stat_ptr);
2212 ad->section_name = name;
2213 ad->address = address;
2214 }
2215
2216 void
2217 DEFUN(lang_add_entry,(name),
2218 CONST char *name)
2219 {
2220 entry_symbol = name;
2221 }
2222
2223 void
2224 DEFUN(lang_add_target,(name),
2225 CONST char *name)
2226 {
2227 lang_target_statement_type *new = new_stat(lang_target_statement,
2228 stat_ptr);
2229 new->target = name;
2230
2231 }
2232
2233
2234
2235
2236 void
2237 DEFUN(lang_add_map,(name),
2238 CONST char *name)
2239 {
2240 while (*name) {
2241 switch (*name) {
2242 case 'F':
2243 map_option_f = true;
2244 break;
2245 }
2246 name++;
2247 }
2248 }
2249
2250 void
2251 DEFUN(lang_add_fill,(exp),
2252 int exp)
2253 {
2254 lang_fill_statement_type *new = new_stat(lang_fill_statement,
2255 stat_ptr);
2256 new->fill = exp;
2257 }
2258
2259 void
2260 DEFUN(lang_add_data,(type, exp),
2261 int type AND
2262 union etree_union *exp)
2263 {
2264
2265 lang_data_statement_type *new = new_stat(lang_data_statement,
2266 stat_ptr);
2267 new->exp = exp;
2268 new->type = type;
2269
2270 }
2271 void
2272 DEFUN(lang_add_assignment,(exp),
2273 etree_type *exp)
2274 {
2275 lang_assignment_statement_type *new = new_stat(lang_assignment_statement,
2276 stat_ptr);
2277 new->exp = exp;
2278 }
2279
2280 void
2281 DEFUN(lang_add_attribute,(attribute),
2282 enum statement_enum attribute)
2283 {
2284 new_statement(attribute, sizeof(lang_statement_union_type),stat_ptr);
2285 }
2286
2287
2288
2289 void
2290 DEFUN(lang_startup,(name),
2291 CONST char *name)
2292 {
2293 if (startup_file != (char *)NULL) {
2294 info("%P%FMultiple STARTUP files\n");
2295 }
2296 first_file->filename = name;
2297 first_file->local_sym_name = name;
2298
2299 startup_file= name;
2300 }
2301 void
2302 DEFUN(lang_float,(maybe),
2303 boolean maybe)
2304 {
2305 lang_float_flag = maybe;
2306 }
2307
2308 void
2309 DEFUN(lang_leave_output_section_statement,(fill, memspec),
2310 bfd_vma fill AND
2311 CONST char *memspec)
2312 {
2313 current_section->fill = fill;
2314 current_section->region = lang_memory_region_lookup(memspec);
2315 stat_ptr = &statement_list;
2316
2317 /* We remember if we are closing a .data section, since we use it to
2318 store constructors in */
2319 if (strcmp(current_section->name, ".data") ==0) {
2320 end_of_data_section_statement_list = statement_list;
2321
2322 }
2323 }
2324 /*
2325 Create an absolute symbol with the given name with the value of the
2326 address of first byte of the section named.
2327
2328 If the symbol already exists, then do nothing.
2329 */
2330 void
2331 DEFUN(lang_abs_symbol_at_beginning_of,(section, name),
2332 CONST char *section AND
2333 CONST char *name)
2334 {
2335 if (ldsym_undefined(name)) {
2336 extern bfd *output_bfd;
2337 extern asymbol *create_symbol();
2338 asection *s = bfd_get_section_by_name(output_bfd, section);
2339 asymbol *def = create_symbol(name,
2340 BSF_GLOBAL | BSF_EXPORT |
2341 BSF_ABSOLUTE,
2342 (asection *)NULL);
2343 if (s != (asection *)NULL) {
2344 def->value = s->vma;
2345 }
2346 else {
2347 def->value = 0;
2348 }
2349 }
2350 }
2351
2352 /*
2353 Create an absolute symbol with the given name with the value of the
2354 address of the first byte after the end of the section named.
2355
2356 If the symbol already exists, then do nothing.
2357 */
2358 void
2359 DEFUN(lang_abs_symbol_at_end_of,(section, name),
2360 CONST char *section AND
2361 CONST char *name)
2362 {
2363 if (ldsym_undefined(name)){
2364 extern bfd *output_bfd;
2365 extern asymbol *create_symbol();
2366 asection *s = bfd_get_section_by_name(output_bfd, section);
2367 /* Add a symbol called _end */
2368 asymbol *def = create_symbol(name,
2369 BSF_GLOBAL | BSF_EXPORT |
2370 BSF_ABSOLUTE,
2371 (asection *)NULL);
2372 if (s != (asection *)NULL) {
2373 def->value = s->vma + s->size;
2374 }
2375 else {
2376 def->value = 0;
2377 }
2378 }
2379 }
2380
2381 void
2382 DEFUN(lang_statement_append,(list, element, field),
2383 lang_statement_list_type *list AND
2384 lang_statement_union_type *element AND
2385 lang_statement_union_type **field)
2386 {
2387 *(list->tail) = element;
2388 list->tail = field;
2389 }
2390
2391 /* Set the output format type */
2392 void
2393 DEFUN(lang_add_output_format,(format),
2394 CONST char *format)
2395 {
2396 output_target = format;
2397 }
2398