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