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