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