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