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