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