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