483472c6df355263a7ef2c3bce917a2c9d3bd917
[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 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 print_nl();
1039
1040 /* Find all the symbols in this file defined in this section */
1041 {
1042 asymbol **p;
1043 for (p = in->ifile->asymbols; *p; p++) {
1044 asymbol *q = *p;
1045
1046 if (bfd_get_section(q) == i && q->flags & BSF_GLOBAL) {
1047 print_symbol(q);
1048 }
1049 }
1050 }
1051 }
1052 else {
1053 print_nl();
1054 }
1055
1056
1057 print_dot = outside_section_address(i) + i->size;
1058 }
1059 else {
1060 printf("No output section allocated\n");
1061 }
1062 }
1063 }
1064
1065 static void
1066 DEFUN(print_fill_statement,(fill),
1067 lang_fill_statement_type *fill)
1068 {
1069 printf("FILL mask ");
1070 print_fill( fill->fill);
1071 }
1072
1073 static void
1074 DEFUN(print_data_statement,(data),
1075 lang_data_statement_type *data)
1076 {
1077 /* bfd_vma value; */
1078 print_section("");
1079 print_space();
1080 print_section("");
1081 print_space();
1082 ASSERT(print_dot == data->output_vma);
1083
1084 print_address(data->output_vma);
1085 print_space();
1086 print_address(data->value);
1087 print_space();
1088 switch (data->type) {
1089 case BYTE :
1090 printf("BYTE ");
1091 print_dot += BYTE_SIZE;
1092 break;
1093 case SHORT:
1094 printf("SHORT ");
1095 print_dot += SHORT_SIZE;
1096 break;
1097 case LONG:
1098 printf("LONG ");
1099 print_dot += LONG_SIZE;
1100 break;
1101 }
1102
1103 exp_print_tree(stdout, data->exp);
1104
1105 printf("\n");
1106 }
1107
1108
1109 static void
1110 DEFUN(print_padding_statement,(s),
1111 lang_padding_statement_type *s)
1112 {
1113 print_section("");
1114 print_space();
1115 print_section("*fill*");
1116 print_space();
1117 print_address(s->output_offset + s->output_section->vma);
1118 print_space();
1119 print_size(s->size);
1120 print_space();
1121 print_fill(s->fill);
1122 print_nl();
1123 }
1124
1125 static void
1126 DEFUN(print_wild_statement,(w,os),
1127 lang_wild_statement_type *w AND
1128 lang_output_section_statement_type *os)
1129 {
1130 if (w->filename != (char *)NULL) {
1131 printf("%s",w->filename);
1132 }
1133 else {
1134 printf("*");
1135 }
1136 if (w->section_name != (char *)NULL) {
1137 printf("(%s)",w->section_name);
1138 }
1139 else {
1140 printf("(*)");
1141 }
1142 print_nl();
1143 print_statement(w->children.head, os);
1144
1145 }
1146 static void
1147 DEFUN(print_statement,(s, os),
1148 lang_statement_union_type *s AND
1149 lang_output_section_statement_type *os)
1150 {
1151 while (s) {
1152 switch (s->header.type) {
1153 case lang_wild_statement_enum:
1154 print_wild_statement(&s->wild_statement, os);
1155 break;
1156 default:
1157 printf("Fail with %d\n",s->header.type);
1158 FAIL();
1159 break;
1160 case lang_address_statement_enum:
1161 printf("address\n");
1162 break;
1163 break;
1164 case lang_object_symbols_statement_enum:
1165 printf("object symbols\n");
1166 break;
1167 case lang_fill_statement_enum:
1168 print_fill_statement(&s->fill_statement);
1169 break;
1170 case lang_data_statement_enum:
1171 print_data_statement(&s->data_statement);
1172 break;
1173 case lang_input_section_enum:
1174 print_input_section(&s->input_section);
1175 break;
1176 case lang_padding_statement_enum:
1177 print_padding_statement(&s->padding_statement);
1178 break;
1179 case lang_output_section_statement_enum:
1180 print_output_section_statement(&s->output_section_statement);
1181 break;
1182 case lang_assignment_statement_enum:
1183 print_assignment(&s->assignment_statement,
1184 os);
1185 break;
1186
1187
1188 case lang_target_statement_enum:
1189 printf("TARGET(%s)\n", s->target_statement.target);
1190 break;
1191 case lang_output_statement_enum:
1192 printf("OUTPUT(%s)\n", s->output_statement.name);
1193 break;
1194 case lang_input_statement_enum:
1195 print_input_statement(&s->input_statement);
1196 break;
1197 case lang_afile_asection_pair_statement_enum:
1198 FAIL();
1199 break;
1200 }
1201 s = s->next;
1202 }
1203 }
1204
1205
1206 static void
1207 DEFUN_VOID(print_statements)
1208 {
1209 print_statement(statement_list.head,
1210 (lang_output_section_statement_type *)NULL);
1211 }
1212
1213 static bfd_vma
1214 DEFUN(insert_pad,(this_ptr, fill, power, output_section_statement, dot),
1215 lang_statement_union_type **this_ptr AND
1216 fill_type fill AND
1217 unsigned int power AND
1218 asection * output_section_statement AND
1219 bfd_vma dot)
1220 {
1221 /* Align this section first to the
1222 input sections requirement, then
1223 to the output section's requirement.
1224 If this alignment is > than any seen before,
1225 then record it too. Perform the alignment by
1226 inserting a magic 'padding' statement.
1227 */
1228
1229 unsigned int alignment_needed = align_power(dot, power) - dot;
1230
1231 if (alignment_needed != 0)
1232 {
1233 lang_statement_union_type *new =
1234 (lang_statement_union_type *)
1235 ldmalloc(sizeof(lang_padding_statement_type));
1236 /* Link into existing chain */
1237 new->header.next = *this_ptr;
1238 *this_ptr = new;
1239 new->header.type = lang_padding_statement_enum;
1240 new->padding_statement.output_section = output_section_statement;
1241 new->padding_statement.output_offset =
1242 dot - output_section_statement->vma;
1243 new->padding_statement.fill = fill;
1244 new->padding_statement.size = alignment_needed;
1245 }
1246
1247
1248 /* Remember the most restrictive alignment */
1249 if (power > output_section_statement->alignment_power) {
1250 output_section_statement->alignment_power = power;
1251 }
1252 output_section_statement->size += alignment_needed;
1253 return alignment_needed + dot;
1254
1255 }
1256
1257 /* Work out how much this section will move the dot point */
1258 static bfd_vma
1259 DEFUN(size_input_section, (this_ptr, output_section_statement, fill, dot),
1260 lang_statement_union_type **this_ptr AND
1261 lang_output_section_statement_type*output_section_statement AND
1262 unsigned short fill AND
1263 bfd_vma dot)
1264 {
1265 lang_input_section_type *is = &((*this_ptr)->input_section);
1266 asection *i = is->section;
1267
1268 if (is->ifile->just_syms_flag == false) {
1269 dot = insert_pad(this_ptr, fill, i->alignment_power,
1270 output_section_statement->bfd_section, dot);
1271
1272 /* remember the largest size so we can malloc the largest area */
1273 /* needed for the output stage */
1274 if (i->size > largest_section) {
1275 largest_section = i->size;
1276 }
1277
1278 /* Remember where in the output section this input section goes */
1279 i->output_offset = dot - output_section_statement->bfd_section->vma;
1280
1281 /* Mark how big the output section must be to contain this now */
1282 dot += i->size;
1283 output_section_statement->bfd_section->size =
1284 dot - output_section_statement->bfd_section->vma;
1285 }
1286
1287 return dot ;
1288 }
1289
1290
1291 /* Work out the size of the output sections
1292 from the sizes of the input sections */
1293 static bfd_vma
1294 DEFUN(lang_size_sections,(s, output_section_statement, prev, fill, dot),
1295 lang_statement_union_type *s AND
1296 lang_output_section_statement_type * output_section_statement AND
1297 lang_statement_union_type **prev AND
1298 unsigned short fill AND
1299 bfd_vma dot)
1300 {
1301 /* Size up the sections from their constituent parts */
1302 for (; s != (lang_statement_union_type *)NULL ; s = s->next)
1303 {
1304 switch (s->header.type) {
1305 case lang_output_section_statement_enum:
1306 {
1307 bfd_vma after;
1308 lang_output_section_statement_type *os =
1309 &(s->output_section_statement);
1310 /* The start of a section */
1311
1312 if (os->addr_tree == (etree_type *)NULL) {
1313 /* No address specified for this section, get one
1314 from the region specification
1315 */
1316 if (os->region == (lang_memory_region_type *)NULL) {
1317 os->region = lang_memory_region_lookup("*default*");
1318 }
1319 dot = os->region->current;
1320 }
1321 else {
1322 etree_value_type r ;
1323 r = exp_fold_tree(os->addr_tree,
1324 (lang_output_section_statement_type *)NULL,
1325 lang_allocating_phase_enum,
1326 dot, &dot);
1327 if (r.valid == false) {
1328 info("%F%S: non constant address expression for section %s\n",
1329 os->name);
1330 }
1331 dot = r.value;
1332 }
1333 /* The section starts here */
1334 /* First, align to what the section needs */
1335
1336 dot = align_power(dot, os->bfd_section->alignment_power);
1337 os->bfd_section->vma = dot;
1338 os->bfd_section->output_offset = 0;
1339
1340 (void) lang_size_sections(os->children.head, os, &os->children.head,
1341 os->fill, dot);
1342 /* Ignore the size of the input sections, use the vma and size to */
1343 /* align against */
1344
1345
1346 after = ALIGN(os->bfd_section->vma +
1347 os->bfd_section->size,
1348 os->block_value) ;
1349
1350
1351 os->bfd_section->size = after - os->bfd_section->vma;
1352 dot = os->bfd_section->vma + os->bfd_section->size;
1353 os->processed = true;
1354
1355 /* Replace into region ? */
1356 if (os->addr_tree == (etree_type *)NULL
1357 && os->region !=(lang_memory_region_type*)NULL ) {
1358 os->region->current = dot;
1359 }
1360 }
1361
1362 break;
1363
1364 case lang_data_statement_enum:
1365 {
1366 unsigned int size;
1367 s->data_statement.output_vma = dot;
1368 s->data_statement.output_section =
1369 output_section_statement->bfd_section;
1370
1371 switch (s->data_statement.type) {
1372 case LONG:
1373 size = LONG_SIZE;
1374 break;
1375 case SHORT:
1376 size = SHORT_SIZE;
1377 break;
1378 case BYTE:
1379 size = BYTE_SIZE;
1380 break;
1381
1382 }
1383 dot += size;
1384 output_section_statement->bfd_section->size += size;
1385 }
1386 break;
1387
1388 case lang_wild_statement_enum:
1389
1390 dot = lang_size_sections(s->wild_statement.children.head,
1391 output_section_statement,
1392 &s->wild_statement.children.head,
1393
1394 fill, dot);
1395
1396 break;
1397
1398 case lang_object_symbols_statement_enum:
1399 create_object_symbols = output_section_statement;
1400 break;
1401 case lang_output_statement_enum:
1402 case lang_target_statement_enum:
1403 break;
1404 case lang_input_section_enum:
1405 dot = size_input_section(prev,
1406 output_section_statement,
1407 output_section_statement->fill, dot);
1408 break;
1409 case lang_input_statement_enum:
1410 break;
1411 case lang_fill_statement_enum:
1412 fill = s->fill_statement.fill;
1413 break;
1414 case lang_assignment_statement_enum:
1415 {
1416 bfd_vma newdot = dot;
1417 exp_fold_tree(s->assignment_statement.exp,
1418 output_section_statement,
1419 lang_allocating_phase_enum,
1420 dot,
1421 &newdot);
1422
1423 if (newdot != dot)
1424 /* We've been moved ! so insert a pad */
1425 {
1426 lang_statement_union_type *new =
1427 (lang_statement_union_type *)
1428 ldmalloc(sizeof(lang_padding_statement_type));
1429 /* Link into existing chain */
1430 new->header.next = *prev;
1431 *prev = new;
1432 new->header.type = lang_padding_statement_enum;
1433 new->padding_statement.output_section =
1434 output_section_statement->bfd_section;
1435 new->padding_statement.output_offset =
1436 dot - output_section_statement->bfd_section->vma;
1437 new->padding_statement.fill = fill;
1438 new->padding_statement.size = newdot - dot;
1439 output_section_statement->bfd_section->size +=
1440 new->padding_statement.size;
1441 dot = newdot;
1442 }
1443 }
1444
1445 break;
1446 case lang_padding_statement_enum:
1447 FAIL();
1448 break;
1449 default:
1450 FAIL();
1451 break;
1452 case lang_address_statement_enum:
1453 break;
1454 }
1455 prev = &s->header.next;
1456 }
1457 return dot;
1458 }
1459
1460
1461 static bfd_vma
1462 DEFUN(lang_do_assignments,(s, output_section_statement, fill, dot),
1463 lang_statement_union_type *s AND
1464 lang_output_section_statement_type * output_section_statement AND
1465 unsigned short fill AND
1466 bfd_vma dot)
1467 {
1468
1469 for (; s != (lang_statement_union_type *)NULL ; s = s->next)
1470 {
1471 switch (s->header.type) {
1472 case lang_output_section_statement_enum:
1473 {
1474 lang_output_section_statement_type *os =
1475 &(s->output_section_statement);
1476 dot = os->bfd_section->vma;
1477 (void) lang_do_assignments(os->children.head, os, os->fill, dot);
1478 dot = os->bfd_section->vma + os->bfd_section->size;
1479 }
1480 break;
1481 case lang_wild_statement_enum:
1482
1483 dot = lang_do_assignments(s->wild_statement.children.head,
1484 output_section_statement,
1485 fill, dot);
1486
1487 break;
1488
1489 case lang_object_symbols_statement_enum:
1490 case lang_output_statement_enum:
1491 case lang_target_statement_enum:
1492 #if 0
1493 case lang_common_statement_enum:
1494 #endif
1495 break;
1496 case lang_data_statement_enum:
1497 {
1498 etree_value_type value ;
1499 value = exp_fold_tree(s->data_statement.exp,
1500 0, lang_final_phase_enum, dot, &dot);
1501 s->data_statement.value = value.value;
1502 if (value.valid == false) info("%F%P: Invalid data statement\n");
1503 }
1504 switch (s->data_statement.type) {
1505 case LONG:
1506 dot += LONG_SIZE;
1507 break;
1508 case SHORT:
1509 dot += SHORT_SIZE;
1510 break;
1511 case BYTE:
1512 dot += BYTE_SIZE;
1513 break;
1514 }
1515 break;
1516 case lang_input_section_enum:
1517 {
1518 asection *in = s->input_section.section;
1519 dot += in->size;
1520 }
1521 break;
1522
1523 case lang_input_statement_enum:
1524 break;
1525 case lang_fill_statement_enum:
1526 fill = s->fill_statement.fill;
1527 break;
1528 case lang_assignment_statement_enum:
1529 {
1530 exp_fold_tree(s->assignment_statement.exp,
1531 output_section_statement,
1532 lang_final_phase_enum,
1533 dot,
1534 &dot);
1535 }
1536
1537 break;
1538 case lang_padding_statement_enum:
1539 dot += s->padding_statement.size;
1540 break;
1541 default:
1542 FAIL();
1543 break;
1544 case lang_address_statement_enum:
1545 break;
1546 }
1547
1548 }
1549 return dot;
1550 }
1551
1552
1553
1554 static void
1555 DEFUN_VOID(lang_relocate_globals)
1556 {
1557
1558 /*
1559 Each ldsym_type maintains a chain of pointers to asymbols which
1560 references the definition. Replace each pointer to the referenence
1561 with a pointer to only one place, preferably the definition. If
1562 the defintion isn't available then the common symbol, and if
1563 there isn't one of them then choose one reference.
1564 */
1565
1566 FOR_EACH_LDSYM(lgs) {
1567 asymbol *it;
1568 if (lgs->sdefs_chain) {
1569 it = *(lgs->sdefs_chain);
1570 }
1571 else if (lgs->scoms_chain != (asymbol **)NULL) {
1572 it = *(lgs->scoms_chain);
1573 }
1574 else if (lgs->srefs_chain != (asymbol **)NULL) {
1575 it = *(lgs->srefs_chain);
1576 }
1577 else {
1578 /* This can happen when the command line asked for a symbol to
1579 be -u */
1580 it = (asymbol *)NULL;
1581 }
1582 if (it != (asymbol *)NULL)
1583 {
1584 asymbol **ptr= lgs->srefs_chain;
1585
1586 while (ptr != (asymbol **)NULL) {
1587 asymbol *ref = *ptr;
1588 *ptr = it;
1589 ptr = (asymbol **)(ref->udata);
1590 }
1591 }
1592 }
1593 }
1594
1595
1596
1597 static void
1598 DEFUN_VOID(lang_finish)
1599 {
1600 ldsym_type *lgs;
1601
1602 if (entry_symbol == (char *)NULL) {
1603 /* No entry has been specified, look for start */
1604 entry_symbol = "start";
1605 }
1606 lgs = ldsym_get_soft(entry_symbol);
1607 if (lgs && lgs->sdefs_chain) {
1608 asymbol *sy = *(lgs->sdefs_chain);
1609 /* We can set the entry address*/
1610 bfd_set_start_address(output_bfd,
1611 outside_symbol_address(sy));
1612
1613 }
1614 else {
1615 /* Can't find anything reasonable,
1616 use the first address in the text section
1617 */
1618 asection *ts = bfd_get_section_by_name(output_bfd, ".text");
1619 if (ts) {
1620 bfd_set_start_address(output_bfd, ts->vma);
1621 }
1622 }
1623 }
1624
1625 /* By now we know the target architecture, and we may have an */
1626 /* ldfile_output_machine_name */
1627 static void
1628 DEFUN_VOID(lang_check)
1629 {
1630 lang_statement_union_type *file;
1631
1632
1633 for (file = file_chain.head;
1634 file != (lang_statement_union_type *)NULL;
1635 file=file->input_statement.next)
1636 {
1637 /* Inspect the architecture and ensure we're linking like
1638 with like
1639 */
1640
1641 if (bfd_arch_compatible( file->input_statement.the_bfd,
1642 output_bfd,
1643 &ldfile_output_architecture,
1644 &ldfile_output_machine)) {
1645 bfd_set_arch_mach(output_bfd,
1646 ldfile_output_architecture, ldfile_output_machine);
1647 }
1648 else {
1649 enum bfd_architecture this_architecture =
1650 bfd_get_architecture(file->input_statement.the_bfd);
1651 unsigned long this_machine =
1652 bfd_get_machine(file->input_statement.the_bfd);
1653
1654 info("%I: architecture %s",
1655 file,
1656 bfd_printable_arch_mach(this_architecture, this_machine));
1657 info(" incompatible with output %s\n",
1658 bfd_printable_arch_mach(ldfile_output_architecture,
1659 ldfile_output_machine));
1660 ldfile_output_architecture = this_architecture;
1661 ldfile_output_machine = this_machine;
1662 bfd_set_arch_mach(output_bfd,
1663 ldfile_output_architecture,
1664 ldfile_output_machine);
1665
1666
1667 }
1668 }
1669 }
1670
1671
1672 /*
1673 * run through all the global common symbols and tie them
1674 * to the output section requested.
1675 */
1676
1677 static void
1678 DEFUN_VOID(lang_common)
1679 {
1680 ldsym_type *lgs;
1681 if (config.relocateable_output == false ||
1682 command_line.force_common_definition== true) {
1683 for (lgs = symbol_head;
1684 lgs != (ldsym_type *)NULL;
1685 lgs=lgs->next)
1686 {
1687 asymbol *com ;
1688 unsigned int power_of_two;
1689 size_t size;
1690 size_t align;
1691 if (lgs->scoms_chain != (asymbol **)NULL) {
1692 com = *(lgs->scoms_chain);
1693 size = com->value;
1694 switch (size) {
1695 case 0:
1696 case 1:
1697 align = 1;
1698 power_of_two = 0;
1699 break;
1700 case 2:
1701 power_of_two = 1;
1702 align = 2;
1703 break;
1704 case 3:
1705 case 4:
1706 power_of_two =2;
1707 align = 4;
1708 break;
1709 case 5:
1710 case 6:
1711 case 7:
1712 case 8:
1713 power_of_two = 3;
1714 align = 8;
1715 break;
1716 default:
1717 power_of_two = 4;
1718 align = 16;
1719 break;
1720 }
1721
1722
1723 /* Change from a common symbol into a definition of
1724 a symbol */
1725 lgs->sdefs_chain = lgs->scoms_chain;
1726 lgs->scoms_chain = (asymbol **)NULL;
1727 commons_pending--;
1728 /* Point to the correct common section */
1729 com->section =
1730 ((lang_input_statement_type *)
1731 (com->the_bfd->usrdata))->common_section;
1732 /* Fix the size of the common section */
1733 com->section->size = ALIGN(com->section->size, align);
1734
1735 /* Remember if this is the biggest alignment ever seen */
1736 if (power_of_two > com->section->alignment_power) {
1737 com->section->alignment_power = power_of_two;
1738 }
1739
1740
1741 com->flags = BSF_EXPORT | BSF_GLOBAL;
1742
1743 if (write_map)
1744 {
1745 printf ("Allocating common %s: %x at %x\n",
1746 lgs->name,
1747 (unsigned) size,
1748 (unsigned) com->section->size);
1749 }
1750 com->value = com->section->size;
1751 com->section->size += size;
1752
1753 }
1754 }
1755 }
1756
1757
1758 }
1759
1760 /*
1761 run through the input files and ensure that every input
1762 section has somewhere to go. If one is found without
1763 a destination then create an input request and place it
1764 into the statement tree.
1765 */
1766
1767 static void
1768 DEFUN_VOID(lang_place_orphans)
1769 {
1770 lang_input_statement_type *file;
1771 for (file = (lang_input_statement_type*)file_chain.head;
1772 file != (lang_input_statement_type*)NULL;
1773 file = (lang_input_statement_type*)file->next) {
1774 asection *s;
1775 for (s = file->the_bfd->sections;
1776 s != (asection *)NULL;
1777 s = s->next) {
1778 if ( s->output_section == (asection *)NULL) {
1779 /* This section of the file is not attatched, root
1780 around for a sensible place for it to go */
1781
1782 if (file->common_section == s) {
1783 /* This is a lonely common section which must
1784 have come from an archive. We attatch to the
1785 section with the wildcard */
1786 if (config.relocateable_output != true
1787 && command_line.force_common_definition == false) {
1788 if (default_common_section ==
1789 (lang_output_section_statement_type *)NULL) {
1790 info("%P: No [COMMON] command, defaulting to .bss\n");
1791
1792 default_common_section =
1793 lang_output_section_statement_lookup(".bss");
1794
1795 }
1796 wild_doit(&default_common_section->children, s,
1797 default_common_section, file);
1798 }
1799 }
1800 else {
1801 lang_output_section_statement_type *os =
1802 lang_output_section_statement_lookup(s->name);
1803
1804 wild_doit(&os->children, s, os, file);
1805 }
1806 }
1807 }
1808 }
1809 }
1810
1811
1812 void
1813 DEFUN(lang_set_flags,(ptr, flags),
1814 lang_section_flags_type *ptr AND
1815 CONST char *flags)
1816 {
1817 boolean state = true;
1818 ptr->flag_read = false;
1819 ptr->flag_write = false;
1820 ptr->flag_executable = false;
1821 ptr->flag_loadable= false;
1822 while (*flags)
1823 {
1824 if (*flags == '!') {
1825 state = false;
1826 flags++;
1827 }
1828 else state = true;
1829 switch (*flags) {
1830 case 'R':
1831 ptr->flag_read = state;
1832 break;
1833 case 'W':
1834 ptr->flag_write = state;
1835 break;
1836 case 'X':
1837 ptr->flag_executable= state;
1838 break;
1839 case 'L':
1840 ptr->flag_loadable= state;
1841 break;
1842 default:
1843 info("%P%F illegal syntax in flags\n");
1844 break;
1845 }
1846 flags++;
1847 }
1848 }
1849
1850
1851
1852 void
1853 DEFUN(lang_for_each_file,(func),
1854 PROTO(void, (*func),(lang_input_statement_type *)))
1855 {
1856 lang_input_statement_type *f;
1857 for (f = (lang_input_statement_type *)file_chain.head;
1858 f != (lang_input_statement_type *)NULL;
1859 f = (lang_input_statement_type *)f->next)
1860 {
1861 func(f);
1862 }
1863 }
1864
1865
1866 void
1867 DEFUN(lang_for_each_input_section, (func),
1868 PROTO(void ,(*func),(bfd *ab, asection*as)))
1869 {
1870 lang_input_statement_type *f;
1871 for (f = (lang_input_statement_type *)file_chain.head;
1872 f != (lang_input_statement_type *)NULL;
1873 f = (lang_input_statement_type *)f->next)
1874 {
1875 asection *s;
1876 for (s = f->the_bfd->sections;
1877 s != (asection *)NULL;
1878 s = s->next) {
1879 func(f->the_bfd, s);
1880 }
1881 }
1882 }
1883
1884
1885
1886 void
1887 DEFUN(ldlang_add_file,(entry),
1888 lang_input_statement_type *entry)
1889 {
1890
1891 lang_statement_append(&file_chain,
1892 (lang_statement_union_type *)entry,
1893 &entry->next);
1894 }
1895
1896
1897
1898 void
1899 DEFUN(lang_add_output,(name),
1900 CONST char *name)
1901 {
1902 lang_output_statement_type *new = new_stat(lang_output_statement,
1903 stat_ptr);
1904 new->name = name;
1905 had_output_filename = true;
1906 }
1907
1908
1909 static lang_output_section_statement_type *current_section;
1910
1911 void
1912 DEFUN(lang_enter_output_section_statement,
1913 (output_section_statement_name,
1914 address_exp,
1915 block_value),
1916 char *output_section_statement_name AND
1917 etree_type *address_exp AND
1918 bfd_vma block_value)
1919 {
1920 lang_output_section_statement_type *os;
1921 current_section =
1922 os =
1923 lang_output_section_statement_lookup(output_section_statement_name);
1924
1925
1926 /* Add this statement to tree */
1927 /* add_statement(lang_output_section_statement_enum,
1928 output_section_statement);*/
1929 /* Make next things chain into subchain of this */
1930
1931 if (os->addr_tree ==
1932 (etree_type *)NULL) {
1933 os->addr_tree =
1934 address_exp;
1935 }
1936 os->block_value = block_value;
1937 stat_ptr = & os->children;
1938
1939 }
1940
1941
1942 void
1943 DEFUN_VOID(lang_final)
1944 {
1945 if (had_output_filename == false) {
1946 lang_add_output("a.out");
1947 }
1948 }
1949
1950
1951
1952
1953
1954 asymbol *
1955 DEFUN(create_symbol,(name, flags, section),
1956 CONST char *name AND
1957 flagword flags AND
1958 asection *section)
1959 {
1960 extern lang_input_statement_type *script_file;
1961 asymbol **def_ptr = (asymbol **)ldmalloc(sizeof(asymbol **));
1962 /* Add this definition to script file */
1963 asymbol *def = (asymbol *)bfd_make_empty_symbol(script_file->the_bfd);
1964 def->name = buystring(name);
1965 def->udata = 0;
1966 def->flags = flags;
1967 def->section = section;
1968
1969 *def_ptr = def;
1970 Q_enter_global_ref(def_ptr);
1971 return def;
1972 }
1973
1974
1975 void
1976 DEFUN_VOID(lang_process)
1977 {
1978 if (had_script == false) {
1979 parse_line(ldemul_get_script());
1980 }
1981 lang_reasonable_defaults();
1982 current_target = default_target;
1983
1984 lang_for_each_statement(ldlang_open_output); /* Open the output file */
1985 /* For each output section statement, create a section in the output
1986 file */
1987 lang_create_output_section_statements();
1988
1989 /* Create a dummy bfd for the script */
1990 lang_init_script_file();
1991
1992 /* Add to the hash table all undefineds on the command line */
1993 lang_place_undefineds();
1994
1995 /* Create a bfd for each input file */
1996 current_target = default_target;
1997 lang_for_each_statement(open_input_bfds);
1998
1999 common_section.userdata = &common_section_userdata;
2000
2001 /* Run through the contours of the script and attatch input sections
2002 to the correct output sections
2003 */
2004 map_input_to_output_sections(statement_list.head, (char *)NULL,
2005 ( lang_output_section_statement_type *)NULL);
2006
2007 /* Find any sections not attatched explicitly and handle them */
2008 lang_place_orphans();
2009
2010 /* Size up the common data */
2011 lang_common();
2012
2013 ldemul_before_allocation();
2014
2015 /* Size up the sections */
2016 lang_size_sections(statement_list.head,
2017 (lang_output_section_statement_type *)NULL,
2018 &(statement_list.head), 0, (bfd_vma)0);
2019
2020 /* See if anything special should be done now we know how big
2021 everything is */
2022 ldemul_after_allocation();
2023
2024 /* Do all the assignments, now that we know the final restingplaces
2025 of all the symbols */
2026
2027 lang_do_assignments(statement_list.head,
2028 (lang_output_section_statement_type *)NULL,
2029 0, (bfd_vma)0);
2030
2031 /* Make sure that we're not mixing architectures */
2032
2033 lang_check();
2034
2035 /* Move the global symbols around */
2036 lang_relocate_globals();
2037
2038 /* Final stuffs */
2039 lang_finish();
2040 }
2041
2042
2043 /* EXPORTED TO YACC */
2044
2045 void
2046 DEFUN(lang_add_wild,(section_name, filename),
2047 CONST char *CONST section_name AND
2048 CONST char *CONST filename)
2049 {
2050 lang_wild_statement_type *new = new_stat(lang_wild_statement,
2051 stat_ptr);
2052
2053 if (section_name != (char *)NULL && strcmp(section_name,"COMMON") == 0)
2054 {
2055 placed_commons = true;
2056 }
2057 if (filename != (char *)NULL) {
2058 lang_has_input_file = true;
2059 }
2060 new->section_name = section_name;
2061 new->filename = filename;
2062 lang_list_init(&new->children);
2063 }
2064 void
2065 DEFUN(lang_section_start,(name, address),
2066 CONST char *name AND
2067 etree_type *address)
2068 {
2069 lang_address_statement_type *ad =new_stat(lang_address_statement, stat_ptr);
2070 ad->section_name = name;
2071 ad->address = address;
2072 }
2073
2074 void
2075 DEFUN(lang_add_entry,(name),
2076 CONST char *name)
2077 {
2078 entry_symbol = name;
2079 }
2080
2081 void
2082 DEFUN(lang_add_target,(name),
2083 CONST char *name)
2084 {
2085 lang_target_statement_type *new = new_stat(lang_target_statement,
2086 stat_ptr);
2087 new->target = name;
2088
2089 }
2090
2091
2092
2093
2094 void
2095 DEFUN(lang_add_map,(name),
2096 CONST char *name)
2097 {
2098 while (*name) {
2099 switch (*name) {
2100 case 'F':
2101 map_option_f = true;
2102 break;
2103 }
2104 name++;
2105 }
2106 }
2107
2108 void
2109 DEFUN(lang_add_fill,(exp),
2110 int exp)
2111 {
2112 lang_fill_statement_type *new = new_stat(lang_fill_statement,
2113 stat_ptr);
2114 new->fill = exp;
2115 }
2116
2117 void
2118 DEFUN(lang_add_data,(type, exp),
2119 int type AND
2120 union etree_union *exp)
2121 {
2122
2123 lang_data_statement_type *new = new_stat(lang_data_statement,
2124 stat_ptr);
2125 new->exp = exp;
2126 new->type = type;
2127
2128 }
2129 void
2130 DEFUN(lang_add_assignment,(exp),
2131 etree_type *exp)
2132 {
2133 lang_assignment_statement_type *new = new_stat(lang_assignment_statement,
2134 stat_ptr);
2135 new->exp = exp;
2136 }
2137
2138 void
2139 DEFUN(lang_add_attribute,(attribute),
2140 enum statement_enum attribute)
2141 {
2142 new_statement(attribute, sizeof(lang_statement_union_type),stat_ptr);
2143 }
2144
2145
2146
2147 void
2148 DEFUN(lang_startup,(name),
2149 CONST char *name)
2150 {
2151 if (startup_file != (char *)NULL) {
2152 info("%P%FMultiple STARTUP files\n");
2153 }
2154 first_file->filename = name;
2155 first_file->local_sym_name = name;
2156
2157 startup_file= name;
2158 }
2159 void
2160 DEFUN(lang_float,(maybe),
2161 boolean maybe)
2162 {
2163 lang_float_flag = maybe;
2164 }
2165
2166 void
2167 DEFUN(lang_leave_output_section_statement,(fill, memspec),
2168 bfd_vma fill AND
2169 CONST char *memspec)
2170 {
2171 current_section->fill = fill;
2172 current_section->region = lang_memory_region_lookup(memspec);
2173 stat_ptr = &statement_list;
2174 }
2175 /*
2176 Create an absolute symbol with the given name with the value of the
2177 address of first byte of the section named.
2178
2179 If the symbol already exists, then do nothing.
2180 */
2181 void
2182 DEFUN(lang_abs_symbol_at_beginning_of,(section, name),
2183 CONST char *section AND
2184 CONST char *name)
2185 {
2186 if (ldsym_undefined(name)) {
2187 extern bfd *output_bfd;
2188 extern asymbol *create_symbol();
2189 asection *s = bfd_get_section_by_name(output_bfd, section);
2190 asymbol *def = create_symbol(name,
2191 BSF_GLOBAL | BSF_EXPORT |
2192 BSF_ABSOLUTE,
2193 (asection *)NULL);
2194 if (s != (asection *)NULL) {
2195 def->value = s->vma;
2196 }
2197 else {
2198 def->value = 0;
2199 }
2200 }
2201 }
2202
2203 /*
2204 Create an absolute symbol with the given name with the value of the
2205 address of the first byte after the end of the section named.
2206
2207 If the symbol already exists, then do nothing.
2208 */
2209 void
2210 DEFUN(lang_abs_symbol_at_end_of,(section, name),
2211 CONST char *section AND
2212 CONST char *name)
2213 {
2214 if (ldsym_undefined(name)){
2215 extern bfd *output_bfd;
2216 extern asymbol *create_symbol();
2217 asection *s = bfd_get_section_by_name(output_bfd, section);
2218 /* Add a symbol called _end */
2219 asymbol *def = create_symbol(name,
2220 BSF_GLOBAL | BSF_EXPORT |
2221 BSF_ABSOLUTE,
2222 (asection *)NULL);
2223 if (s != (asection *)NULL) {
2224 def->value = s->vma + s->size;
2225 }
2226 else {
2227 def->value = 0;
2228 }
2229 }
2230 }
2231
2232 void
2233 DEFUN(lang_statement_append,(list, element, field),
2234 lang_statement_list_type *list AND
2235 lang_statement_union_type *element AND
2236 lang_statement_union_type **field)
2237 {
2238 *(list->tail) = element;
2239 list->tail = field;
2240 }
2241
2242
2243
This page took 0.077243 seconds and 3 git commands to generate.