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