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