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