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