1 /* Linker command language support.
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 1998
3 Free Software Foundation, Inc.
5 This file is part of GLD, the Gnu Linker.
7 GLD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GLD is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GLD; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "libiberty.h"
43 static lang_statement_union_type *new_statement PARAMS ((enum statement_enum,
45 lang_statement_list_type*));
49 static struct obstack stat_obstack;
51 #define obstack_chunk_alloc xmalloc
52 #define obstack_chunk_free free
53 static CONST char *startup_file;
54 static lang_statement_list_type input_file_chain;
55 static boolean placed_commons = false;
56 static lang_output_section_statement_type *default_common_section;
57 static boolean map_option_f;
58 static bfd_vma print_dot;
59 static lang_input_statement_type *first_file;
60 static lang_statement_list_type lang_output_section_statement;
61 static CONST char *current_target;
62 static CONST char *output_target;
63 static lang_statement_list_type statement_list;
64 static struct lang_phdr *lang_phdr_list;
66 static void lang_for_each_statement_worker
67 PARAMS ((void (*func) (lang_statement_union_type *),
68 lang_statement_union_type *s));
69 static lang_input_statement_type *new_afile
70 PARAMS ((const char *name, lang_input_file_enum_type file_type,
71 const char *target, boolean add_to_list));
72 static void init_os PARAMS ((lang_output_section_statement_type *s));
73 static void exp_init_os PARAMS ((etree_type *));
74 static void section_already_linked PARAMS ((bfd *, asection *, PTR));
75 static boolean wildcardp PARAMS ((const char *));
76 static lang_statement_union_type *wild_sort
77 PARAMS ((lang_wild_statement_type *, lang_input_statement_type *,
79 static void wild_section PARAMS ((lang_wild_statement_type *ptr,
81 lang_input_statement_type *file,
82 lang_output_section_statement_type *output));
83 static lang_input_statement_type *lookup_name PARAMS ((const char *name));
84 static void load_symbols PARAMS ((lang_input_statement_type *entry,
85 lang_statement_list_type *));
86 static void wild_file PARAMS ((lang_wild_statement_type *, const char *,
87 lang_input_statement_type *,
88 lang_output_section_statement_type *));
89 static void wild PARAMS ((lang_wild_statement_type *s,
90 const char *section, const char *file,
92 lang_output_section_statement_type *output));
93 static bfd *open_output PARAMS ((const char *name));
94 static void ldlang_open_output PARAMS ((lang_statement_union_type *statement));
95 static void open_input_bfds
96 PARAMS ((lang_statement_union_type *statement, boolean));
97 static void lang_reasonable_defaults PARAMS ((void));
98 static void lang_place_undefineds PARAMS ((void));
99 static void map_input_to_output_sections
100 PARAMS ((lang_statement_union_type *s,
102 lang_output_section_statement_type *output_section_statement));
103 static void print_output_section_statement
104 PARAMS ((lang_output_section_statement_type *output_section_statement));
105 static void print_assignment
106 PARAMS ((lang_assignment_statement_type *assignment,
107 lang_output_section_statement_type *output_section));
108 static void print_input_statement PARAMS ((lang_input_statement_type *statm));
109 static boolean print_one_symbol PARAMS ((struct bfd_link_hash_entry *, PTR));
110 static void print_input_section PARAMS ((lang_input_section_type *in));
111 static void print_fill_statement PARAMS ((lang_fill_statement_type *fill));
112 static void print_data_statement PARAMS ((lang_data_statement_type *data));
113 static void print_address_statement PARAMS ((lang_address_statement_type *));
114 static void print_reloc_statement PARAMS ((lang_reloc_statement_type *reloc));
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_group
120 PARAMS ((lang_group_statement_type *, lang_output_section_statement_type *));
121 static void print_statement PARAMS ((lang_statement_union_type *s,
122 lang_output_section_statement_type *os));
123 static void print_statement_list PARAMS ((lang_statement_union_type *s,
124 lang_output_section_statement_type *os));
125 static void print_statements PARAMS ((void));
126 static bfd_vma insert_pad PARAMS ((lang_statement_union_type **this_ptr,
127 fill_type fill, unsigned int power,
128 asection *output_section_statement,
130 static bfd_vma size_input_section
131 PARAMS ((lang_statement_union_type **this_ptr,
132 lang_output_section_statement_type *output_section_statement,
133 fill_type fill, bfd_vma dot, boolean relax));
134 static void lang_finish PARAMS ((void));
135 static void ignore_bfd_errors PARAMS ((const char *, ...));
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 lang_set_startof PARAMS ((void));
142 static void reset_memory_regions PARAMS ((void));
143 static void lang_record_phdrs PARAMS ((void));
146 lang_output_section_statement_type *abs_output_section;
147 lang_statement_list_type *stat_ptr = &statement_list;
148 lang_statement_list_type file_chain = { 0 };
149 const char *entry_symbol = NULL;
150 boolean entry_from_cmdline;
151 boolean lang_has_input_file = false;
152 boolean had_output_filename = false;
153 boolean lang_float_flag = false;
154 boolean delete_output_file_on_failure = false;
155 struct lang_nocrossrefs *nocrossref_list;
157 etree_type *base; /* Relocation base - or null */
160 #if defined(__STDC__) || defined(ALMOST_STDC)
161 #define cat(a,b) a##b
163 #define cat(a,b) a/**/b
166 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
168 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
170 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
172 #define SECTION_NAME_MAP_LENGTH (16)
178 return obstack_alloc (&stat_obstack, size);
181 /*----------------------------------------------------------------------
182 lang_for_each_statement walks the parse tree and calls the provided
183 function for each node
187 lang_for_each_statement_worker (func, s)
188 void (*func) PARAMS ((lang_statement_union_type *));
189 lang_statement_union_type *s;
191 for (; s != (lang_statement_union_type *) NULL; s = s->next)
195 switch (s->header.type)
197 case lang_constructors_statement_enum:
198 lang_for_each_statement_worker (func, constructor_list.head);
200 case lang_output_section_statement_enum:
201 lang_for_each_statement_worker
203 s->output_section_statement.children.head);
205 case lang_wild_statement_enum:
206 lang_for_each_statement_worker
208 s->wild_statement.children.head);
210 case lang_group_statement_enum:
211 lang_for_each_statement_worker (func,
212 s->group_statement.children.head);
214 case lang_data_statement_enum:
215 case lang_reloc_statement_enum:
216 case lang_object_symbols_statement_enum:
217 case lang_output_statement_enum:
218 case lang_target_statement_enum:
219 case lang_input_section_enum:
220 case lang_input_statement_enum:
221 case lang_assignment_statement_enum:
222 case lang_padding_statement_enum:
223 case lang_address_statement_enum:
224 case lang_fill_statement_enum:
234 lang_for_each_statement (func)
235 void (*func) PARAMS ((lang_statement_union_type *));
237 lang_for_each_statement_worker (func,
238 statement_list.head);
241 /*----------------------------------------------------------------------*/
243 lang_list_init (list)
244 lang_statement_list_type *list;
246 list->head = (lang_statement_union_type *) NULL;
247 list->tail = &list->head;
250 /*----------------------------------------------------------------------
252 build a new statement node for the parse tree
257 lang_statement_union_type *
258 new_statement (type, size, list)
259 enum statement_enum type;
261 lang_statement_list_type * list;
263 lang_statement_union_type *new = (lang_statement_union_type *)
266 new->header.type = type;
267 new->header.next = (lang_statement_union_type *) NULL;
268 lang_statement_append (list, new, &new->header.next);
273 Build a new input file node for the language. There are several ways
274 in which we treat an input file, eg, we only look at symbols, or
275 prefix it with a -l etc.
277 We can be supplied with requests for input files more than once;
278 they may, for example be split over serveral lines like foo.o(.text)
279 foo.o(.data) etc, so when asked for a file we check that we havn't
280 got it already so we don't duplicate the bfd.
283 static lang_input_statement_type *
284 new_afile (name, file_type, target, add_to_list)
286 lang_input_file_enum_type file_type;
290 lang_input_statement_type *p;
293 p = new_stat (lang_input_statement, stat_ptr);
296 p = ((lang_input_statement_type *)
297 stat_alloc (sizeof (lang_input_statement_type)));
298 p->header.next = NULL;
301 lang_has_input_file = true;
305 case lang_input_file_is_symbols_only_enum:
307 p->is_archive = false;
309 p->local_sym_name = name;
310 p->just_syms_flag = true;
311 p->search_dirs_flag = false;
313 case lang_input_file_is_fake_enum:
315 p->is_archive = false;
317 p->local_sym_name = name;
318 p->just_syms_flag = false;
319 p->search_dirs_flag = false;
321 case lang_input_file_is_l_enum:
322 p->is_archive = true;
325 p->local_sym_name = concat ("-l", name, (const char *) NULL);
326 p->just_syms_flag = false;
327 p->search_dirs_flag = true;
329 case lang_input_file_is_marker_enum:
331 p->is_archive = false;
333 p->local_sym_name = name;
334 p->just_syms_flag = false;
335 p->search_dirs_flag = true;
337 case lang_input_file_is_search_file_enum:
339 p->is_archive = false;
341 p->local_sym_name = name;
342 p->just_syms_flag = false;
343 p->search_dirs_flag = true;
345 case lang_input_file_is_file_enum:
347 p->is_archive = false;
349 p->local_sym_name = name;
350 p->just_syms_flag = false;
351 p->search_dirs_flag = false;
356 p->the_bfd = (bfd *) NULL;
357 p->asymbols = (asymbol **) NULL;
358 p->next_real_file = (lang_statement_union_type *) NULL;
359 p->next = (lang_statement_union_type *) NULL;
361 p->dynamic = config.dynamic_link;
362 p->whole_archive = whole_archive;
364 lang_statement_append (&input_file_chain,
365 (lang_statement_union_type *) p,
370 lang_input_statement_type *
371 lang_add_input_file (name, file_type, target)
373 lang_input_file_enum_type file_type;
376 lang_has_input_file = true;
377 return new_afile (name, file_type, target, true);
380 /* Build enough state so that the parser can build its tree */
384 obstack_begin (&stat_obstack, 1000);
386 stat_ptr = &statement_list;
388 lang_list_init (stat_ptr);
390 lang_list_init (&input_file_chain);
391 lang_list_init (&lang_output_section_statement);
392 lang_list_init (&file_chain);
393 first_file = lang_add_input_file ((char *) NULL,
394 lang_input_file_is_marker_enum,
396 abs_output_section = lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
398 abs_output_section->bfd_section = bfd_abs_section_ptr;
402 /*----------------------------------------------------------------------
403 A region is an area of memory declared with the
404 MEMORY { name:org=exp, len=exp ... }
407 We maintain a list of all the regions here
409 If no regions are specified in the script, then the default is used
410 which is created when looked up to be the entire data space
413 static lang_memory_region_type *lang_memory_region_list;
414 static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
416 lang_memory_region_type *
417 lang_memory_region_lookup (name)
418 CONST char *CONST name;
420 lang_memory_region_type *p;
422 for (p = lang_memory_region_list;
423 p != (lang_memory_region_type *) NULL;
426 if (strcmp (p->name, name) == 0)
433 /* This code used to always use the first region in the list as the
434 default region. I changed it to instead use a region
435 encompassing all of memory as the default region. This permits
436 NOLOAD sections to work reasonably without requiring a region.
437 People should specify what region they mean, if they really want
439 if (strcmp (name, "*default*") == 0)
441 if (lang_memory_region_list != (lang_memory_region_type *) NULL)
443 return lang_memory_region_list;
449 lang_memory_region_type *new =
450 (lang_memory_region_type *) stat_alloc (sizeof (lang_memory_region_type));
452 new->name = buystring (name);
453 new->next = (lang_memory_region_type *) NULL;
455 *lang_memory_region_list_tail = new;
456 lang_memory_region_list_tail = &new->next;
460 new->length = ~(bfd_size_type)0;
462 new->had_full_message = false;
469 lang_memory_region_type *
470 lang_memory_default (section)
473 lang_memory_region_type *p;
475 flagword sec_flags = section->flags;
477 /* Override SEC_DATA to mean a writable section. */
478 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
479 sec_flags |= SEC_DATA;
481 for (p = lang_memory_region_list;
482 p != (lang_memory_region_type *) NULL;
485 if ((p->flags & sec_flags) != 0
486 && (p->not_flags & sec_flags) == 0)
491 return lang_memory_region_lookup ("*default*");
494 lang_output_section_statement_type *
495 lang_output_section_find (name)
496 CONST char *CONST name;
498 lang_statement_union_type *u;
499 lang_output_section_statement_type *lookup;
501 for (u = lang_output_section_statement.head;
502 u != (lang_statement_union_type *) NULL;
505 lookup = &u->output_section_statement;
506 if (strcmp (name, lookup->name) == 0)
511 return (lang_output_section_statement_type *) NULL;
514 lang_output_section_statement_type *
515 lang_output_section_statement_lookup (name)
516 CONST char *CONST name;
518 lang_output_section_statement_type *lookup;
520 lookup = lang_output_section_find (name);
521 if (lookup == (lang_output_section_statement_type *) NULL)
524 lookup = (lang_output_section_statement_type *)
525 new_stat (lang_output_section_statement, stat_ptr);
526 lookup->region = (lang_memory_region_type *) NULL;
528 lookup->block_value = 1;
531 lookup->next = (lang_statement_union_type *) NULL;
532 lookup->bfd_section = (asection *) NULL;
533 lookup->processed = false;
534 lookup->sectype = normal_section;
535 lookup->addr_tree = (etree_type *) NULL;
536 lang_list_init (&lookup->children);
538 lookup->memspec = (CONST char *) NULL;
540 lookup->subsection_alignment = -1;
541 lookup->section_alignment = -1;
542 lookup->load_base = (union etree_union *) NULL;
543 lookup->phdrs = NULL;
545 lang_statement_append (&lang_output_section_statement,
546 (lang_statement_union_type *) lookup,
553 lang_map_flags (flag)
556 if (flag & SEC_ALLOC)
562 if (flag & SEC_READONLY)
575 lang_memory_region_type *m;
577 minfo (_("\nMemory Configuration\n\n"));
578 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
579 _("Name"), _("Origin"), _("Length"), _("Attributes"));
581 for (m = lang_memory_region_list;
582 m != (lang_memory_region_type *) NULL;
588 fprintf (config.map_file, "%-16s ", m->name);
590 sprintf_vma (buf, m->origin);
591 minfo ("0x%s ", buf);
599 minfo ("0x%V", m->length);
600 if (m->flags || m->not_flags)
608 lang_map_flags (m->flags);
614 lang_map_flags (m->not_flags);
621 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
626 /* Initialize an output section. */
630 lang_output_section_statement_type *s;
632 section_userdata_type *new;
634 if (s->bfd_section != NULL)
637 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
638 einfo (_("%P%F: Illegal use of `%s' section"), DISCARD_SECTION_NAME);
640 new = ((section_userdata_type *)
641 stat_alloc (sizeof (section_userdata_type)));
643 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
644 if (s->bfd_section == (asection *) NULL)
645 s->bfd_section = bfd_make_section (output_bfd, s->name);
646 if (s->bfd_section == (asection *) NULL)
648 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
649 output_bfd->xvec->name, s->name);
651 s->bfd_section->output_section = s->bfd_section;
653 /* We initialize an output sections output offset to minus its own */
654 /* vma to allow us to output a section through itself */
655 s->bfd_section->output_offset = 0;
656 get_userdata (s->bfd_section) = (PTR) new;
658 /* If there is a base address, make sure that any sections it might
659 mention are initialized. */
660 if (s->addr_tree != NULL)
661 exp_init_os (s->addr_tree);
664 /* Make sure that all output sections mentioned in an expression are
671 switch (exp->type.node_class)
674 exp_init_os (exp->assign.src);
678 exp_init_os (exp->binary.lhs);
679 exp_init_os (exp->binary.rhs);
683 exp_init_os (exp->trinary.cond);
684 exp_init_os (exp->trinary.lhs);
685 exp_init_os (exp->trinary.rhs);
689 exp_init_os (exp->unary.child);
693 switch (exp->type.node_code)
699 lang_output_section_statement_type *os;
701 os = lang_output_section_find (exp->name.name);
702 if (os != NULL && os->bfd_section == NULL)
713 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
714 once into the output. This routine checks each sections, and
715 arranges to discard it if a section of the same name has already
716 been linked. This code assumes that all relevant sections have the
717 SEC_LINK_ONCE flag set; that is, it does not depend solely upon the
718 section name. This is called via bfd_map_over_sections. */
722 section_already_linked (abfd, sec, data)
727 lang_input_statement_type *entry = (lang_input_statement_type *) data;
730 struct sec_link_once *next;
733 static struct sec_link_once *sec_link_once_list;
736 struct sec_link_once *l;
738 /* If we are only reading symbols from this object, then we want to
739 discard all sections. */
740 if (entry->just_syms_flag)
742 sec->output_section = bfd_abs_section_ptr;
743 sec->output_offset = sec->vma;
747 flags = bfd_get_section_flags (abfd, sec);
749 if ((flags & SEC_LINK_ONCE) == 0)
752 name = bfd_get_section_name (abfd, sec);
754 for (l = sec_link_once_list; l != NULL; l = l->next)
756 if (strcmp (name, bfd_get_section_name (l->sec->owner, l->sec)) == 0)
758 /* The section has already been linked. See if we should
760 switch (flags & SEC_LINK_DUPLICATES)
765 case SEC_LINK_DUPLICATES_DISCARD:
768 case SEC_LINK_DUPLICATES_ONE_ONLY:
769 einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
773 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
774 /* FIXME: We should really dig out the contents of both
775 sections and memcmp them. The COFF/PE spec says that
776 the Microsoft linker does not implement this
777 correctly, so I'm not going to bother doing it
780 case SEC_LINK_DUPLICATES_SAME_SIZE:
781 if (bfd_section_size (abfd, sec)
782 != bfd_section_size (l->sec->owner, l->sec))
783 einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
788 /* Set the output_section field so that wild_doit does not
789 create a lang_input_section structure for this section. */
790 sec->output_section = bfd_abs_section_ptr;
796 /* This is the first section with this name. Record it. */
798 l = (struct sec_link_once *) xmalloc (sizeof *l);
800 l->next = sec_link_once_list;
801 sec_link_once_list = l;
804 /* The wild routines.
806 These expand statements like *(.text) and foo.o to a list of
807 explicit actions, like foo.o(.text), bar.o(.text) and
808 foo.o(.text, .data). */
810 /* Return true if the PATTERN argument is a wildcard pattern.
811 Although backslashes are treated specially if a pattern contains
812 wildcards, we do not consider the mere presence of a backslash to
813 be enough to cause the the pattern to be treated as a wildcard.
814 That lets us handle DOS filenames more naturally. */
822 for (s = pattern; *s != '\0'; ++s)
830 /* Add SECTION to the output section OUTPUT. Do this by creating a
831 lang_input_section statement which is placed at PTR. FILE is the
832 input file which holds SECTION. */
835 wild_doit (ptr, section, output, file)
836 lang_statement_list_type *ptr;
838 lang_output_section_statement_type *output;
839 lang_input_statement_type *file;
844 flags = bfd_get_section_flags (section->owner, section);
848 /* If we are doing a final link, discard sections marked with
850 if (! link_info.relocateable
851 && (flags & SEC_EXCLUDE) != 0)
854 /* Discard input sections which are assigned to a section named
855 DISCARD_SECTION_NAME. */
856 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
859 /* Discard debugging sections if we are stripping debugging
861 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
862 && (flags & SEC_DEBUGGING) != 0)
867 if (section->output_section == NULL)
869 /* This prevents future calls from assigning this section. */
870 section->output_section = bfd_abs_section_ptr;
875 if (section->output_section == NULL)
878 lang_input_section_type *new;
881 if (output->bfd_section == NULL)
889 /* Add a section reference to the list */
890 new = new_stat (lang_input_section, ptr);
892 new->section = section;
894 section->output_section = output->bfd_section;
896 flags = section->flags;
898 /* We don't copy the SEC_NEVER_LOAD flag from an input section
899 to an output section, because we want to be able to include a
900 SEC_NEVER_LOAD section in the middle of an otherwise loaded
901 section (I don't know why we want to do this, but we do).
902 build_link_order in ldwrite.c handles this case by turning
903 the embedded SEC_NEVER_LOAD section into a fill. */
905 flags &= ~ SEC_NEVER_LOAD;
907 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
908 already been processed. One reason to do this is that on pe
909 format targets, .text$foo sections go into .text and it's odd
910 to see .text with SEC_LINK_ONCE set. */
912 if (! link_info.relocateable)
913 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
915 /* If this is not the first input section, and the SEC_READONLY
916 flag is not currently set, then don't set it just because the
917 input section has it set. */
919 if (! first && (section->output_section->flags & SEC_READONLY) == 0)
920 flags &= ~ SEC_READONLY;
922 section->output_section->flags |= flags;
924 /* If SEC_READONLY is not set in the input section, then clear
925 it from the output section. */
926 if ((section->flags & SEC_READONLY) == 0)
927 section->output_section->flags &= ~SEC_READONLY;
929 switch (output->sectype)
936 case overlay_section:
937 output->bfd_section->flags &= ~SEC_ALLOC;
940 output->bfd_section->flags &= ~SEC_LOAD;
941 output->bfd_section->flags |= SEC_NEVER_LOAD;
945 if (section->alignment_power > output->bfd_section->alignment_power)
946 output->bfd_section->alignment_power = section->alignment_power;
948 /* If supplied an aligment, then force it. */
949 if (output->section_alignment != -1)
950 output->bfd_section->alignment_power = output->section_alignment;
954 /* Handle wildcard sorting. This returns the lang_input_section which
955 should follow the one we are going to create for SECTION and FILE,
956 based on the sorting requirements of WILD. It returns NULL if the
957 new section should just go at the end of the current list. */
959 static lang_statement_union_type *
960 wild_sort (wild, file, section)
961 lang_wild_statement_type *wild;
962 lang_input_statement_type *file;
965 const char *section_name;
966 lang_statement_union_type *l;
968 if (! wild->filenames_sorted && ! wild->sections_sorted)
971 section_name = bfd_get_section_name (file->the_bfd, section);
972 for (l = wild->children.head; l != NULL; l = l->next)
974 lang_input_section_type *ls;
976 if (l->header.type != lang_input_section_enum)
978 ls = &l->input_section;
980 /* Sorting by filename takes precedence over sorting by section
983 if (wild->filenames_sorted)
987 i = strcmp (file->filename, ls->ifile->filename);
994 /* Here either the files are not sorted by name, or we are
995 looking at the sections for this file. */
997 if (wild->sections_sorted)
999 if (strcmp (section_name,
1000 bfd_get_section_name (ls->ifile->the_bfd,
1010 /* Expand a wild statement for a particular FILE. SECTION may be
1011 NULL, in which case it is a wild card. */
1014 wild_section (ptr, section, file, output)
1015 lang_wild_statement_type *ptr;
1016 const char *section;
1017 lang_input_statement_type *file;
1018 lang_output_section_statement_type *output;
1020 if (file->just_syms_flag == false)
1022 register asection *s;
1025 if (section == NULL)
1028 wildcard = wildcardp (section);
1030 for (s = file->the_bfd->sections; s != NULL; s = s->next)
1034 /* Attach all sections named SECTION. If SECTION is NULL,
1035 then attach all sections.
1037 Previously, if SECTION was NULL, this code did not call
1038 wild_doit if the SEC_IS_COMMON flag was set for the
1039 section. I did not understand that, and I took it out.
1040 --ian@cygnus.com. */
1042 if (section == NULL)
1048 name = bfd_get_section_name (file->the_bfd, s);
1050 match = fnmatch (section, name, 0) == 0 ? true : false;
1052 match = strcmp (section, name) == 0 ? true : false;
1057 lang_statement_union_type *before;
1059 before = wild_sort (ptr, file, s);
1061 /* Here BEFORE points to the lang_input_section which
1062 should follow the one we are about to add. If BEFORE
1063 is NULL, then the section should just go at the end
1064 of the current list. */
1067 wild_doit (&ptr->children, s, output, file);
1070 lang_statement_list_type list;
1071 lang_statement_union_type **pp;
1073 lang_list_init (&list);
1074 wild_doit (&list, s, output, file);
1075 ASSERT (list.head != NULL && list.head->next == NULL);
1077 for (pp = &ptr->children.head;
1080 ASSERT (*pp != NULL);
1082 list.head->next = *pp;
1090 /* This is passed a file name which must have been seen already and
1091 added to the statement tree. We will see if it has been opened
1092 already and had its symbols read. If not then we'll read it. */
1094 static lang_input_statement_type *
1098 lang_input_statement_type *search;
1100 for (search = (lang_input_statement_type *) input_file_chain.head;
1101 search != (lang_input_statement_type *) NULL;
1102 search = (lang_input_statement_type *) search->next_real_file)
1104 if (search->filename == (char *) NULL && name == (char *) NULL)
1106 if (search->filename != (char *) NULL
1107 && name != (char *) NULL
1108 && strcmp (search->filename, name) == 0)
1112 if (search == (lang_input_statement_type *) NULL)
1113 search = new_afile (name, lang_input_file_is_file_enum, default_target,
1116 /* If we have already added this file, or this file is not real
1117 (FIXME: can that ever actually happen?) or the name is NULL
1118 (FIXME: can that ever actually happen?) don't add this file. */
1121 || search->filename == (const char *) NULL)
1124 load_symbols (search, (lang_statement_list_type *) NULL);
1129 /* Get the symbols for an input file. */
1132 load_symbols (entry, place)
1133 lang_input_statement_type *entry;
1134 lang_statement_list_type *place;
1141 ldfile_open_file (entry);
1143 if (! bfd_check_format (entry->the_bfd, bfd_archive)
1144 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
1147 lang_statement_list_type *hold;
1149 err = bfd_get_error ();
1150 if (err == bfd_error_file_ambiguously_recognized)
1154 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
1155 einfo (_("%B: matching formats:"), entry->the_bfd);
1156 for (p = matching; *p != NULL; p++)
1160 else if (err != bfd_error_file_not_recognized
1162 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
1164 bfd_close (entry->the_bfd);
1165 entry->the_bfd = NULL;
1167 /* See if the emulation has some special knowledge. */
1169 if (ldemul_unrecognized_file (entry))
1172 /* Try to interpret the file as a linker script. */
1174 ldfile_open_command_file (entry->filename);
1179 ldfile_assumed_script = true;
1180 parser_input = input_script;
1182 ldfile_assumed_script = false;
1189 /* We don't call ldlang_add_file for an archive. Instead, the
1190 add_symbols entry point will call ldlang_add_file, via the
1191 add_archive_element callback, for each element of the archive
1193 switch (bfd_get_format (entry->the_bfd))
1199 ldlang_add_file (entry);
1200 if (trace_files || trace_file_tries)
1201 info_msg ("%I\n", entry);
1205 if (entry->whole_archive)
1207 bfd *member = bfd_openr_next_archived_file (entry->the_bfd,
1209 while (member != NULL)
1211 if (! bfd_check_format (member, bfd_object))
1212 einfo (_("%F%B: object %B in archive is not object\n"),
1213 entry->the_bfd, member);
1214 if (! ((*link_info.callbacks->add_archive_element)
1215 (&link_info, member, "--whole-archive")))
1217 if (! bfd_link_add_symbols (member, &link_info))
1218 einfo (_("%F%B: could not read symbols: %E\n"), member);
1219 member = bfd_openr_next_archived_file (entry->the_bfd,
1223 entry->loaded = true;
1229 if (! bfd_link_add_symbols (entry->the_bfd, &link_info))
1230 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
1232 entry->loaded = true;
1235 /* Handle a wild statement for a single file F. */
1238 wild_file (s, section, f, output)
1239 lang_wild_statement_type *s;
1240 const char *section;
1241 lang_input_statement_type *f;
1242 lang_output_section_statement_type *output;
1244 if (f->the_bfd == NULL
1245 || ! bfd_check_format (f->the_bfd, bfd_archive))
1246 wild_section (s, section, f, output);
1251 /* This is an archive file. We must map each member of the
1252 archive separately. */
1253 member = bfd_openr_next_archived_file (f->the_bfd, (bfd *) NULL);
1254 while (member != NULL)
1256 /* When lookup_name is called, it will call the add_symbols
1257 entry point for the archive. For each element of the
1258 archive which is included, BFD will call ldlang_add_file,
1259 which will set the usrdata field of the member to the
1260 lang_input_statement. */
1261 if (member->usrdata != NULL)
1263 wild_section (s, section,
1264 (lang_input_statement_type *) member->usrdata,
1268 member = bfd_openr_next_archived_file (f->the_bfd, member);
1273 /* Handle a wild statement. SECTION or FILE or both may be NULL,
1274 indicating that it is a wildcard. Separate lang_input_section
1275 statements are created for each part of the expansion; they are
1276 added after the wild statement S. OUTPUT is the output section. */
1279 wild (s, section, file, target, output)
1280 lang_wild_statement_type *s;
1281 const char *section;
1284 lang_output_section_statement_type *output;
1286 lang_input_statement_type *f;
1288 if (file == (char *) NULL)
1290 /* Perform the iteration over all files in the list */
1291 for (f = (lang_input_statement_type *) file_chain.head;
1292 f != (lang_input_statement_type *) NULL;
1293 f = (lang_input_statement_type *) f->next)
1295 wild_file (s, section, f, output);
1298 else if (wildcardp (file))
1300 for (f = (lang_input_statement_type *) file_chain.head;
1301 f != (lang_input_statement_type *) NULL;
1302 f = (lang_input_statement_type *) f->next)
1304 if (fnmatch (file, f->filename, FNM_FILE_NAME) == 0)
1305 wild_file (s, section, f, output);
1310 /* Perform the iteration over a single file */
1311 f = lookup_name (file);
1312 wild_file (s, section, f, output);
1315 if (section != (char *) NULL
1316 && strcmp (section, "COMMON") == 0
1317 && default_common_section == NULL)
1319 /* Remember the section that common is going to in case we later
1320 get something which doesn't know where to put it. */
1321 default_common_section = output;
1325 /* Open the output file. */
1333 if (output_target == (char *) NULL)
1335 if (current_target != (char *) NULL)
1336 output_target = current_target;
1338 output_target = default_target;
1340 output = bfd_openw (name, output_target);
1342 if (output == (bfd *) NULL)
1344 if (bfd_get_error () == bfd_error_invalid_target)
1346 einfo (_("%P%F: target %s not found\n"), output_target);
1348 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
1351 delete_output_file_on_failure = true;
1353 /* output->flags |= D_PAGED;*/
1355 if (! bfd_set_format (output, bfd_object))
1356 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
1357 if (! bfd_set_arch_mach (output,
1358 ldfile_output_architecture,
1359 ldfile_output_machine))
1360 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
1362 link_info.hash = bfd_link_hash_table_create (output);
1363 if (link_info.hash == (struct bfd_link_hash_table *) NULL)
1364 einfo (_("%P%F: can not create link hash table: %E\n"));
1366 bfd_set_gp_size (output, g_switch_value);
1374 ldlang_open_output (statement)
1375 lang_statement_union_type * statement;
1377 switch (statement->header.type)
1379 case lang_output_statement_enum:
1380 ASSERT (output_bfd == (bfd *) NULL);
1381 output_bfd = open_output (statement->output_statement.name);
1382 ldemul_set_output_arch ();
1383 if (config.magic_demand_paged && !link_info.relocateable)
1384 output_bfd->flags |= D_PAGED;
1386 output_bfd->flags &= ~D_PAGED;
1387 if (config.text_read_only)
1388 output_bfd->flags |= WP_TEXT;
1390 output_bfd->flags &= ~WP_TEXT;
1391 if (link_info.traditional_format)
1392 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
1394 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
1397 case lang_target_statement_enum:
1398 current_target = statement->target_statement.target;
1405 /* Open all the input files. */
1408 open_input_bfds (s, force)
1409 lang_statement_union_type *s;
1412 for (; s != (lang_statement_union_type *) NULL; s = s->next)
1414 switch (s->header.type)
1416 case lang_constructors_statement_enum:
1417 open_input_bfds (constructor_list.head, force);
1419 case lang_output_section_statement_enum:
1420 open_input_bfds (s->output_section_statement.children.head, force);
1422 case lang_wild_statement_enum:
1423 /* Maybe we should load the file's symbols */
1424 if (s->wild_statement.filename
1425 && ! wildcardp (s->wild_statement.filename))
1426 (void) lookup_name (s->wild_statement.filename);
1427 open_input_bfds (s->wild_statement.children.head, force);
1429 case lang_group_statement_enum:
1431 struct bfd_link_hash_entry *undefs;
1433 /* We must continually search the entries in the group
1434 until no new symbols are added to the list of undefined
1439 undefs = link_info.hash->undefs_tail;
1440 open_input_bfds (s->group_statement.children.head, true);
1442 while (undefs != link_info.hash->undefs_tail);
1445 case lang_target_statement_enum:
1446 current_target = s->target_statement.target;
1448 case lang_input_statement_enum:
1449 if (s->input_statement.real == true)
1451 lang_statement_list_type add;
1453 s->input_statement.target = current_target;
1455 /* If we are being called from within a group, and this
1456 is an archive which has already been searched, then
1457 force it to be researched. */
1459 && s->input_statement.loaded
1460 && bfd_check_format (s->input_statement.the_bfd,
1462 s->input_statement.loaded = false;
1464 lang_list_init (&add);
1466 load_symbols (&s->input_statement, &add);
1468 if (add.head != NULL)
1470 *add.tail = s->next;
1481 /* If there are [COMMONS] statements, put a wild one into the bss section */
1484 lang_reasonable_defaults ()
1487 lang_output_section_statement_lookup (".text");
1488 lang_output_section_statement_lookup (".data");
1490 default_common_section =
1491 lang_output_section_statement_lookup (".bss");
1494 if (placed_commons == false)
1496 lang_wild_statement_type *new =
1497 new_stat (lang_wild_statement,
1498 &default_common_section->children);
1500 new->section_name = "COMMON";
1501 new->filename = (char *) NULL;
1502 lang_list_init (&new->children);
1509 Add the supplied name to the symbol table as an undefined reference.
1510 Remove items from the chain as we open input bfds
1512 typedef struct ldlang_undef_chain_list
1514 struct ldlang_undef_chain_list *next;
1516 } ldlang_undef_chain_list_type;
1518 static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head;
1521 ldlang_add_undef (name)
1522 CONST char *CONST name;
1524 ldlang_undef_chain_list_type *new =
1525 ((ldlang_undef_chain_list_type *)
1526 stat_alloc (sizeof (ldlang_undef_chain_list_type)));
1528 new->next = ldlang_undef_chain_list_head;
1529 ldlang_undef_chain_list_head = new;
1531 new->name = buystring (name);
1534 /* Run through the list of undefineds created above and place them
1535 into the linker hash table as undefined symbols belonging to the
1539 lang_place_undefineds ()
1541 ldlang_undef_chain_list_type *ptr;
1543 for (ptr = ldlang_undef_chain_list_head;
1544 ptr != (ldlang_undef_chain_list_type *) NULL;
1547 struct bfd_link_hash_entry *h;
1549 h = bfd_link_hash_lookup (link_info.hash, ptr->name, true, false, true);
1550 if (h == (struct bfd_link_hash_entry *) NULL)
1551 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1552 if (h->type == bfd_link_hash_new)
1554 h->type = bfd_link_hash_undefined;
1555 h->u.undef.abfd = NULL;
1556 bfd_link_add_undef (link_info.hash, h);
1561 /* Open input files and attatch to output sections */
1563 map_input_to_output_sections (s, target, output_section_statement)
1564 lang_statement_union_type * s;
1566 lang_output_section_statement_type * output_section_statement;
1568 for (; s != (lang_statement_union_type *) NULL; s = s->next)
1570 switch (s->header.type)
1574 case lang_wild_statement_enum:
1575 wild (&s->wild_statement, s->wild_statement.section_name,
1576 s->wild_statement.filename, target,
1577 output_section_statement);
1580 case lang_constructors_statement_enum:
1581 map_input_to_output_sections (constructor_list.head,
1583 output_section_statement);
1585 case lang_output_section_statement_enum:
1586 map_input_to_output_sections (s->output_section_statement.children.head,
1588 &s->output_section_statement);
1590 case lang_output_statement_enum:
1592 case lang_target_statement_enum:
1593 target = s->target_statement.target;
1595 case lang_group_statement_enum:
1596 map_input_to_output_sections (s->group_statement.children.head,
1598 output_section_statement);
1600 case lang_fill_statement_enum:
1601 case lang_input_section_enum:
1602 case lang_object_symbols_statement_enum:
1603 case lang_data_statement_enum:
1604 case lang_reloc_statement_enum:
1605 case lang_padding_statement_enum:
1606 case lang_input_statement_enum:
1607 if (output_section_statement != NULL
1608 && output_section_statement->bfd_section == NULL)
1609 init_os (output_section_statement);
1611 case lang_assignment_statement_enum:
1612 if (output_section_statement != NULL
1613 && output_section_statement->bfd_section == NULL)
1614 init_os (output_section_statement);
1616 /* Make sure that any sections mentioned in the assignment
1618 exp_init_os (s->assignment_statement.exp);
1620 case lang_afile_asection_pair_statement_enum:
1623 case lang_address_statement_enum:
1624 /* Mark the specified section with the supplied address */
1626 lang_output_section_statement_type *os =
1627 lang_output_section_statement_lookup
1628 (s->address_statement.section_name);
1630 if (os->bfd_section == NULL)
1632 os->addr_tree = s->address_statement.address;
1640 print_output_section_statement (output_section_statement)
1641 lang_output_section_statement_type * output_section_statement;
1643 asection *section = output_section_statement->bfd_section;
1646 if (output_section_statement != abs_output_section)
1648 minfo ("\n%s", output_section_statement->name);
1650 if (section != NULL)
1652 print_dot = section->vma;
1654 len = strlen (output_section_statement->name);
1655 if (len >= SECTION_NAME_MAP_LENGTH - 1)
1660 while (len < SECTION_NAME_MAP_LENGTH)
1666 minfo ("0x%V %W", section->vma, section->_raw_size);
1668 if (output_section_statement->load_base != NULL)
1672 addr = exp_get_abs_int (output_section_statement->load_base, 0,
1673 "load base", lang_final_phase_enum);
1674 minfo (_(" load address 0x%V"), addr);
1681 print_statement_list (output_section_statement->children.head,
1682 output_section_statement);
1686 print_assignment (assignment, output_section)
1687 lang_assignment_statement_type * assignment;
1688 lang_output_section_statement_type * output_section;
1691 etree_value_type result;
1693 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1696 result = exp_fold_tree (assignment->exp->assign.src, output_section,
1697 lang_final_phase_enum, print_dot, &print_dot);
1699 minfo ("0x%V", result.value + result.section->bfd_section->vma);
1710 exp_print_tree (assignment->exp);
1716 print_input_statement (statm)
1717 lang_input_statement_type * statm;
1719 if (statm->filename != (char *) NULL)
1721 fprintf (config.map_file, "LOAD %s\n", statm->filename);
1725 /* Print all symbols defined in a particular section. This is called
1726 via bfd_link_hash_traverse. */
1729 print_one_symbol (hash_entry, ptr)
1730 struct bfd_link_hash_entry *hash_entry;
1733 asection *sec = (asection *) ptr;
1735 if ((hash_entry->type == bfd_link_hash_defined
1736 || hash_entry->type == bfd_link_hash_defweak)
1737 && sec == hash_entry->u.def.section)
1741 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1744 (hash_entry->u.def.value
1745 + hash_entry->u.def.section->output_offset
1746 + hash_entry->u.def.section->output_section->vma));
1748 minfo (" %T\n", hash_entry->root.string);
1754 /* Print information about an input section to the map file. */
1757 print_input_section (in)
1758 lang_input_section_type * in;
1760 asection *i = in->section;
1761 bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
1767 minfo ("%s", i->name);
1769 if (i->output_section != NULL)
1773 len = 1 + strlen (i->name);
1774 if (len >= SECTION_NAME_MAP_LENGTH - 1)
1779 while (len < SECTION_NAME_MAP_LENGTH)
1785 minfo ("0x%V %W %B\n",
1786 i->output_section->vma + i->output_offset, size,
1789 if (i->_cooked_size != 0 && i->_cooked_size != i->_raw_size)
1791 len = SECTION_NAME_MAP_LENGTH + 3;
1803 minfo (_("%W (size before relaxing)\n"), i->_raw_size);
1806 bfd_link_hash_traverse (link_info.hash, print_one_symbol, (PTR) i);
1808 print_dot = i->output_section->vma + i->output_offset + size;
1814 print_fill_statement (fill)
1815 lang_fill_statement_type * fill;
1817 fprintf (config.map_file, " FILL mask 0x%x\n", fill->fill);
1821 print_data_statement (data)
1822 lang_data_statement_type * data;
1829 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1832 addr = data->output_vma;
1833 if (data->output_section != NULL)
1834 addr += data->output_section->vma;
1862 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
1864 if (data->exp->type.node_class != etree_value)
1867 exp_print_tree (data->exp);
1872 print_dot = addr + size;
1875 /* Print an address statement. These are generated by options like
1879 print_address_statement (address)
1880 lang_address_statement_type *address;
1882 minfo (_("Address of section %s set to "), address->section_name);
1883 exp_print_tree (address->address);
1887 /* Print a reloc statement. */
1890 print_reloc_statement (reloc)
1891 lang_reloc_statement_type *reloc;
1897 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1900 addr = reloc->output_vma;
1901 if (reloc->output_section != NULL)
1902 addr += reloc->output_section->vma;
1904 size = bfd_get_reloc_size (reloc->howto);
1906 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
1908 if (reloc->name != NULL)
1909 minfo ("%s+", reloc->name);
1911 minfo ("%s+", reloc->section->name);
1913 exp_print_tree (reloc->addend_exp);
1917 print_dot = addr + size;
1921 print_padding_statement (s)
1922 lang_padding_statement_type *s;
1929 len = sizeof " *fill*" - 1;
1930 while (len < SECTION_NAME_MAP_LENGTH)
1936 addr = s->output_offset;
1937 if (s->output_section != NULL)
1938 addr += s->output_section->vma;
1939 minfo ("0x%V %W", addr, s->size);
1942 minfo (" %u", s->fill);
1946 print_dot = addr + s->size;
1950 print_wild_statement (w, os)
1951 lang_wild_statement_type * w;
1952 lang_output_section_statement_type * os;
1956 if (w->filenames_sorted)
1958 if (w->filename != NULL)
1959 minfo ("%s", w->filename);
1962 if (w->filenames_sorted)
1966 if (w->sections_sorted)
1968 if (w->section_name != NULL)
1969 minfo ("%s", w->section_name);
1972 if (w->sections_sorted)
1978 print_statement_list (w->children.head, os);
1981 /* Print a group statement. */
1985 lang_group_statement_type *s;
1986 lang_output_section_statement_type *os;
1988 fprintf (config.map_file, "START GROUP\n");
1989 print_statement_list (s->children.head, os);
1990 fprintf (config.map_file, "END GROUP\n");
1993 /* Print the list of statements in S.
1994 This can be called for any statement type. */
1997 print_statement_list (s, os)
1998 lang_statement_union_type *s;
1999 lang_output_section_statement_type *os;
2003 print_statement (s, os);
2008 /* Print the first statement in statement list S.
2009 This can be called for any statement type. */
2012 print_statement (s, os)
2013 lang_statement_union_type *s;
2014 lang_output_section_statement_type *os;
2016 switch (s->header.type)
2019 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
2022 case lang_constructors_statement_enum:
2023 if (constructor_list.head != NULL)
2025 minfo (" CONSTRUCTORS\n");
2026 print_statement_list (constructor_list.head, os);
2029 case lang_wild_statement_enum:
2030 print_wild_statement (&s->wild_statement, os);
2032 case lang_address_statement_enum:
2033 print_address_statement (&s->address_statement);
2035 case lang_object_symbols_statement_enum:
2036 minfo (" CREATE_OBJECT_SYMBOLS\n");
2038 case lang_fill_statement_enum:
2039 print_fill_statement (&s->fill_statement);
2041 case lang_data_statement_enum:
2042 print_data_statement (&s->data_statement);
2044 case lang_reloc_statement_enum:
2045 print_reloc_statement (&s->reloc_statement);
2047 case lang_input_section_enum:
2048 print_input_section (&s->input_section);
2050 case lang_padding_statement_enum:
2051 print_padding_statement (&s->padding_statement);
2053 case lang_output_section_statement_enum:
2054 print_output_section_statement (&s->output_section_statement);
2056 case lang_assignment_statement_enum:
2057 print_assignment (&s->assignment_statement, os);
2059 case lang_target_statement_enum:
2060 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
2062 case lang_output_statement_enum:
2063 minfo ("OUTPUT(%s", s->output_statement.name);
2064 if (output_target != NULL)
2065 minfo (" %s", output_target);
2068 case lang_input_statement_enum:
2069 print_input_statement (&s->input_statement);
2071 case lang_group_statement_enum:
2072 print_group (&s->group_statement, os);
2074 case lang_afile_asection_pair_statement_enum:
2083 print_statement_list (statement_list.head, abs_output_section);
2086 /* Print the first N statements in statement list S to STDERR.
2087 If N == 0, nothing is printed.
2088 If N < 0, the entire list is printed.
2089 Intended to be called from GDB. */
2092 dprint_statement (s, n)
2093 lang_statement_union_type * s;
2096 FILE *map_save = config.map_file;
2098 config.map_file = stderr;
2101 print_statement_list (s, abs_output_section);
2104 while (s && --n >= 0)
2106 print_statement (s, abs_output_section);
2111 config.map_file = map_save;
2115 insert_pad (this_ptr, fill, power, output_section_statement, dot)
2116 lang_statement_union_type ** this_ptr;
2119 asection * output_section_statement;
2122 /* Align this section first to the
2123 input sections requirement, then
2124 to the output section's requirement.
2125 If this alignment is > than any seen before,
2126 then record it too. Perform the alignment by
2127 inserting a magic 'padding' statement.
2130 unsigned int alignment_needed = align_power (dot, power) - dot;
2132 if (alignment_needed != 0)
2134 lang_statement_union_type *new =
2135 ((lang_statement_union_type *)
2136 stat_alloc (sizeof (lang_padding_statement_type)));
2138 /* Link into existing chain */
2139 new->header.next = *this_ptr;
2141 new->header.type = lang_padding_statement_enum;
2142 new->padding_statement.output_section = output_section_statement;
2143 new->padding_statement.output_offset =
2144 dot - output_section_statement->vma;
2145 new->padding_statement.fill = fill;
2146 new->padding_statement.size = alignment_needed;
2150 /* Remember the most restrictive alignment */
2151 if (power > output_section_statement->alignment_power)
2153 output_section_statement->alignment_power = power;
2155 output_section_statement->_raw_size += alignment_needed;
2156 return alignment_needed + dot;
2160 /* Work out how much this section will move the dot point */
2162 size_input_section (this_ptr, output_section_statement, fill, dot, relax)
2163 lang_statement_union_type ** this_ptr;
2164 lang_output_section_statement_type * output_section_statement;
2169 lang_input_section_type *is = &((*this_ptr)->input_section);
2170 asection *i = is->section;
2172 if (is->ifile->just_syms_flag == false)
2174 if (output_section_statement->subsection_alignment != -1)
2175 i->alignment_power =
2176 output_section_statement->subsection_alignment;
2178 dot = insert_pad (this_ptr, fill, i->alignment_power,
2179 output_section_statement->bfd_section, dot);
2181 /* Remember where in the output section this input section goes */
2183 i->output_offset = dot - output_section_statement->bfd_section->vma;
2185 /* Mark how big the output section must be to contain this now
2187 if (i->_cooked_size != 0)
2188 dot += i->_cooked_size;
2190 dot += i->_raw_size;
2191 output_section_statement->bfd_section->_raw_size = dot - output_section_statement->bfd_section->vma;
2195 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
2201 /* This variable indicates whether bfd_relax_section should be called
2204 static boolean relax_again;
2206 /* Set the sizes for all the output sections. */
2209 lang_size_sections (s, output_section_statement, prev, fill, dot, relax)
2210 lang_statement_union_type * s;
2211 lang_output_section_statement_type * output_section_statement;
2212 lang_statement_union_type ** prev;
2217 /* Size up the sections from their constituent parts */
2218 for (; s != (lang_statement_union_type *) NULL; s = s->next)
2220 switch (s->header.type)
2223 case lang_output_section_statement_enum:
2226 lang_output_section_statement_type *os = &s->output_section_statement;
2228 if (os->bfd_section == NULL)
2230 /* This section was never actually created. */
2234 /* If this is a COFF shared library section, use the size and
2235 address from the input section. FIXME: This is COFF
2236 specific; it would be cleaner if there were some other way
2237 to do this, but nothing simple comes to mind. */
2238 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
2242 if (os->children.head == NULL
2243 || os->children.head->next != NULL
2244 || os->children.head->header.type != lang_input_section_enum)
2245 einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
2248 input = os->children.head->input_section.section;
2249 bfd_set_section_vma (os->bfd_section->owner,
2251 bfd_section_vma (input->owner, input));
2252 os->bfd_section->_raw_size = input->_raw_size;
2256 if (bfd_is_abs_section (os->bfd_section))
2258 /* No matter what happens, an abs section starts at zero */
2259 ASSERT (os->bfd_section->vma == 0);
2263 if (os->addr_tree == (etree_type *) NULL)
2265 /* No address specified for this section, get one
2266 from the region specification
2268 if (os->region == (lang_memory_region_type *) NULL
2269 || (((bfd_get_section_flags (output_bfd, os->bfd_section)
2270 & (SEC_ALLOC | SEC_LOAD)) != 0)
2271 && os->region->name[0] == '*'
2272 && strcmp (os->region->name, "*default*") == 0))
2274 os->region = lang_memory_default (os->bfd_section);
2277 /* If a loadable section is using the default memory
2278 region, and some non default memory regions were
2279 defined, issue a warning. */
2280 if ((bfd_get_section_flags (output_bfd, os->bfd_section)
2281 & (SEC_ALLOC | SEC_LOAD)) != 0
2282 && ! link_info.relocateable
2283 && strcmp (os->region->name, "*default*") == 0
2284 && lang_memory_region_list != NULL
2285 && (strcmp (lang_memory_region_list->name, "*default*") != 0
2286 || lang_memory_region_list->next != NULL))
2287 einfo (_("%P: warning: no memory region specified for section `%s'\n"),
2288 bfd_get_section_name (output_bfd, os->bfd_section));
2290 dot = os->region->current;
2291 if (os->section_alignment == -1)
2296 dot = align_power (dot, os->bfd_section->alignment_power);
2297 if (dot != olddot && config.warn_section_align)
2298 einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
2299 os->name, (unsigned int) (dot - olddot));
2306 r = exp_fold_tree (os->addr_tree,
2308 lang_allocating_phase_enum,
2310 if (r.valid == false)
2312 einfo (_("%F%S: non constant address expression for section %s\n"),
2315 dot = r.value + r.section->bfd_section->vma;
2317 /* The section starts here */
2318 /* First, align to what the section needs */
2320 if (os->section_alignment != -1)
2321 dot = align_power (dot, os->section_alignment);
2323 bfd_set_section_vma (0, os->bfd_section, dot);
2325 os->bfd_section->output_offset = 0;
2328 (void) lang_size_sections (os->children.head, os, &os->children.head,
2329 os->fill, dot, relax);
2330 /* Ignore the size of the input sections, use the vma and size to */
2333 after = ALIGN_N (os->bfd_section->vma +
2334 os->bfd_section->_raw_size,
2335 /* The coercion here is important, see ld.h. */
2336 (bfd_vma) os->block_value);
2338 if (bfd_is_abs_section (os->bfd_section))
2339 ASSERT (after == os->bfd_section->vma);
2341 os->bfd_section->_raw_size = after - os->bfd_section->vma;
2342 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
2343 os->processed = true;
2345 /* Replace into region ? */
2346 if (os->region != (lang_memory_region_type *) NULL)
2348 os->region->current = dot;
2349 /* Make sure this isn't silly. */
2350 if (os->region->current < os->region->origin
2351 || (os->region->current - os->region->origin
2352 > os->region->length))
2354 if (os->addr_tree != (etree_type *) NULL)
2356 einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2357 os->region->current,
2358 os->bfd_section->owner,
2359 os->bfd_section->name,
2364 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2366 os->bfd_section->owner,
2367 os->bfd_section->name);
2369 /* Reset the region pointer. */
2370 os->region->current = os->region->origin;
2376 case lang_constructors_statement_enum:
2377 dot = lang_size_sections (constructor_list.head,
2378 output_section_statement,
2379 &s->wild_statement.children.head,
2384 case lang_data_statement_enum:
2386 unsigned int size = 0;
2388 s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma;
2389 s->data_statement.output_section =
2390 output_section_statement->bfd_section;
2392 switch (s->data_statement.type)
2410 output_section_statement->bfd_section->_raw_size += size;
2411 /* The output section gets contents, and then we inspect for
2412 any flags set in the input script which override any ALLOC */
2413 output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
2414 if (!(output_section_statement->flags & SEC_NEVER_LOAD)) {
2415 output_section_statement->bfd_section->flags |= SEC_ALLOC | SEC_LOAD;
2420 case lang_reloc_statement_enum:
2424 s->reloc_statement.output_vma =
2425 dot - output_section_statement->bfd_section->vma;
2426 s->reloc_statement.output_section =
2427 output_section_statement->bfd_section;
2428 size = bfd_get_reloc_size (s->reloc_statement.howto);
2430 output_section_statement->bfd_section->_raw_size += size;
2434 case lang_wild_statement_enum:
2436 dot = lang_size_sections (s->wild_statement.children.head,
2437 output_section_statement,
2438 &s->wild_statement.children.head,
2444 case lang_object_symbols_statement_enum:
2445 link_info.create_object_symbols_section =
2446 output_section_statement->bfd_section;
2448 case lang_output_statement_enum:
2449 case lang_target_statement_enum:
2451 case lang_input_section_enum:
2455 i = (*prev)->input_section.section;
2458 if (i->_cooked_size == 0)
2459 i->_cooked_size = i->_raw_size;
2465 if (! bfd_relax_section (i->owner, i, &link_info, &again))
2466 einfo (_("%P%F: can't relax section: %E\n"));
2470 dot = size_input_section (prev,
2471 output_section_statement,
2472 output_section_statement->fill,
2476 case lang_input_statement_enum:
2478 case lang_fill_statement_enum:
2479 s->fill_statement.output_section = output_section_statement->bfd_section;
2481 fill = s->fill_statement.fill;
2483 case lang_assignment_statement_enum:
2485 bfd_vma newdot = dot;
2487 exp_fold_tree (s->assignment_statement.exp,
2488 output_section_statement,
2489 lang_allocating_phase_enum,
2495 /* The assignment changed dot. Insert a pad. */
2496 if (output_section_statement == abs_output_section)
2498 /* If we don't have an output section, then just adjust
2499 the default memory address. */
2500 lang_memory_region_lookup ("*default*")->current = newdot;
2504 lang_statement_union_type *new =
2505 ((lang_statement_union_type *)
2506 stat_alloc (sizeof (lang_padding_statement_type)));
2508 /* Link into existing chain */
2509 new->header.next = *prev;
2511 new->header.type = lang_padding_statement_enum;
2512 new->padding_statement.output_section =
2513 output_section_statement->bfd_section;
2514 new->padding_statement.output_offset =
2515 dot - output_section_statement->bfd_section->vma;
2516 new->padding_statement.fill = fill;
2517 new->padding_statement.size = newdot - dot;
2518 output_section_statement->bfd_section->_raw_size +=
2519 new->padding_statement.size;
2527 case lang_padding_statement_enum:
2528 /* If we are relaxing, and this is not the first pass, some
2529 padding statements may have been inserted during previous
2530 passes. We may have to move the padding statement to a new
2531 location if dot has a different value at this point in this
2532 pass than it did at this point in the previous pass. */
2533 s->padding_statement.output_offset =
2534 dot - output_section_statement->bfd_section->vma;
2535 dot += s->padding_statement.size;
2536 output_section_statement->bfd_section->_raw_size +=
2537 s->padding_statement.size;
2540 case lang_group_statement_enum:
2541 dot = lang_size_sections (s->group_statement.children.head,
2542 output_section_statement,
2543 &s->group_statement.children.head,
2551 /* This can only get here when relaxing is turned on */
2553 case lang_address_statement_enum:
2556 prev = &s->header.next;
2562 lang_do_assignments (s, output_section_statement, fill, dot)
2563 lang_statement_union_type * s;
2564 lang_output_section_statement_type * output_section_statement;
2568 for (; s != (lang_statement_union_type *) NULL; s = s->next)
2570 switch (s->header.type)
2572 case lang_constructors_statement_enum:
2573 dot = lang_do_assignments (constructor_list.head,
2574 output_section_statement,
2579 case lang_output_section_statement_enum:
2581 lang_output_section_statement_type *os =
2582 &(s->output_section_statement);
2584 if (os->bfd_section != NULL)
2586 dot = os->bfd_section->vma;
2587 (void) lang_do_assignments (os->children.head, os,
2589 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
2593 /* If nothing has been placed into the output section then
2594 it won't have a bfd_section. */
2595 if (os->bfd_section)
2597 os->bfd_section->lma
2598 = exp_get_abs_int(os->load_base, 0,"load base", lang_final_phase_enum);
2603 case lang_wild_statement_enum:
2605 dot = lang_do_assignments (s->wild_statement.children.head,
2606 output_section_statement,
2611 case lang_object_symbols_statement_enum:
2612 case lang_output_statement_enum:
2613 case lang_target_statement_enum:
2615 case lang_common_statement_enum:
2618 case lang_data_statement_enum:
2620 etree_value_type value;
2622 value = exp_fold_tree (s->data_statement.exp,
2624 lang_final_phase_enum, dot, &dot);
2625 s->data_statement.value = value.value;
2626 if (value.valid == false)
2627 einfo (_("%F%P: invalid data statement\n"));
2629 switch (s->data_statement.type)
2647 case lang_reloc_statement_enum:
2649 etree_value_type value;
2651 value = exp_fold_tree (s->reloc_statement.addend_exp,
2653 lang_final_phase_enum, dot, &dot);
2654 s->reloc_statement.addend_value = value.value;
2655 if (value.valid == false)
2656 einfo (_("%F%P: invalid reloc statement\n"));
2658 dot += bfd_get_reloc_size (s->reloc_statement.howto);
2661 case lang_input_section_enum:
2663 asection *in = s->input_section.section;
2665 if (in->_cooked_size != 0)
2666 dot += in->_cooked_size;
2668 dot += in->_raw_size;
2672 case lang_input_statement_enum:
2674 case lang_fill_statement_enum:
2675 fill = s->fill_statement.fill;
2677 case lang_assignment_statement_enum:
2679 exp_fold_tree (s->assignment_statement.exp,
2680 output_section_statement,
2681 lang_final_phase_enum,
2687 case lang_padding_statement_enum:
2688 dot += s->padding_statement.size;
2691 case lang_group_statement_enum:
2692 dot = lang_do_assignments (s->group_statement.children.head,
2693 output_section_statement,
2701 case lang_address_statement_enum:
2709 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
2710 operator .startof. (section_name), it produces an undefined symbol
2711 .startof.section_name. Similarly, when it sees
2712 .sizeof. (section_name), it produces an undefined symbol
2713 .sizeof.section_name. For all the output sections, we look for
2714 such symbols, and set them to the correct value. */
2721 if (link_info.relocateable)
2724 for (s = output_bfd->sections; s != NULL; s = s->next)
2726 const char *secname;
2728 struct bfd_link_hash_entry *h;
2730 secname = bfd_get_section_name (output_bfd, s);
2731 buf = xmalloc (10 + strlen (secname));
2733 sprintf (buf, ".startof.%s", secname);
2734 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
2735 if (h != NULL && h->type == bfd_link_hash_undefined)
2737 h->type = bfd_link_hash_defined;
2738 h->u.def.value = bfd_get_section_vma (output_bfd, s);
2739 h->u.def.section = bfd_abs_section_ptr;
2742 sprintf (buf, ".sizeof.%s", secname);
2743 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
2744 if (h != NULL && h->type == bfd_link_hash_undefined)
2746 h->type = bfd_link_hash_defined;
2747 if (s->_cooked_size != 0)
2748 h->u.def.value = s->_cooked_size;
2750 h->u.def.value = s->_raw_size;
2751 h->u.def.section = bfd_abs_section_ptr;
2761 struct bfd_link_hash_entry *h;
2764 if (link_info.relocateable || link_info.shared)
2769 if (entry_symbol == (char *) NULL)
2771 /* No entry has been specified. Look for start, but don't warn
2772 if we don't find it. */
2773 entry_symbol = "start";
2777 h = bfd_link_hash_lookup (link_info.hash, entry_symbol, false, false, true);
2778 if (h != (struct bfd_link_hash_entry *) NULL
2779 && (h->type == bfd_link_hash_defined
2780 || h->type == bfd_link_hash_defweak)
2781 && h->u.def.section->output_section != NULL)
2785 val = (h->u.def.value
2786 + bfd_get_section_vma (output_bfd,
2787 h->u.def.section->output_section)
2788 + h->u.def.section->output_offset);
2789 if (! bfd_set_start_address (output_bfd, val))
2790 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol);
2797 /* We couldn't find the entry symbol. Try parsing it as a
2799 val = bfd_scan_vma (entry_symbol, &send, 0);
2802 if (! bfd_set_start_address (output_bfd, val))
2803 einfo (_("%P%F: can't set start address\n"));
2809 /* Can't find the entry symbol, and it's not a number. Use
2810 the first address in the text section. */
2811 ts = bfd_get_section_by_name (output_bfd, ".text");
2812 if (ts != (asection *) NULL)
2815 einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
2816 entry_symbol, bfd_get_section_vma (output_bfd, ts));
2817 if (! bfd_set_start_address (output_bfd,
2818 bfd_get_section_vma (output_bfd,
2820 einfo (_("%P%F: can't set start address\n"));
2825 einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
2832 /* This is a small function used when we want to ignore errors from
2836 #ifdef ANSI_PROTOTYPES
2837 ignore_bfd_errors (const char *s, ...)
2839 ignore_bfd_errors (s)
2843 /* Don't do anything. */
2846 /* Check that the architecture of all the input files is compatible
2847 with the output file. Also call the backend to let it do any
2848 other checking that is needed. */
2853 lang_statement_union_type *file;
2855 CONST bfd_arch_info_type *compatible;
2857 for (file = file_chain.head;
2858 file != (lang_statement_union_type *) NULL;
2859 file = file->input_statement.next)
2861 input_bfd = file->input_statement.the_bfd;
2862 compatible = bfd_arch_get_compatible (input_bfd,
2864 if (compatible == NULL)
2866 if (command_line.warn_mismatch)
2867 einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
2868 bfd_printable_name (input_bfd), input_bfd,
2869 bfd_printable_name (output_bfd));
2873 bfd_error_handler_type pfn = NULL;
2875 /* If we aren't supposed to warn about mismatched input
2876 files, temporarily set the BFD error handler to a
2877 function which will do nothing. We still want to call
2878 bfd_merge_private_bfd_data, since it may set up
2879 information which is needed in the output file. */
2880 if (! command_line.warn_mismatch)
2881 pfn = bfd_set_error_handler (ignore_bfd_errors);
2882 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
2884 if (command_line.warn_mismatch)
2885 einfo (_("%E%X: failed to merge target specific data of file %B\n"),
2888 if (! command_line.warn_mismatch)
2889 bfd_set_error_handler (pfn);
2894 /* Look through all the global common symbols and attach them to the
2895 correct section. The -sort-common command line switch may be used
2896 to roughly sort the entries by size. */
2901 if (link_info.relocateable
2902 && ! command_line.force_common_definition)
2905 if (! config.sort_common)
2906 bfd_link_hash_traverse (link_info.hash, lang_one_common, (PTR) NULL);
2911 for (power = 4; power >= 0; power--)
2912 bfd_link_hash_traverse (link_info.hash, lang_one_common,
2917 /* Place one common symbol in the correct section. */
2920 lang_one_common (h, info)
2921 struct bfd_link_hash_entry *h;
2924 unsigned int power_of_two;
2928 if (h->type != bfd_link_hash_common)
2932 power_of_two = h->u.c.p->alignment_power;
2934 if (config.sort_common
2935 && power_of_two < (unsigned int) *(int *) info)
2938 section = h->u.c.p->section;
2940 /* Increase the size of the section. */
2941 section->_cooked_size = ALIGN_N (section->_cooked_size,
2942 (bfd_size_type) (1 << power_of_two));
2944 /* Adjust the alignment if necessary. */
2945 if (power_of_two > section->alignment_power)
2946 section->alignment_power = power_of_two;
2948 /* Change the symbol from common to defined. */
2949 h->type = bfd_link_hash_defined;
2950 h->u.def.section = section;
2951 h->u.def.value = section->_cooked_size;
2953 /* Increase the size of the section. */
2954 section->_cooked_size += size;
2956 /* Make sure the section is allocated in memory, and make sure that
2957 it is no longer a common section. */
2958 section->flags |= SEC_ALLOC;
2959 section->flags &= ~ SEC_IS_COMMON;
2961 if (config.map_file != NULL)
2963 static boolean header_printed;
2968 if (! header_printed)
2970 minfo (_("\nAllocating common symbols\n"));
2971 minfo (_("Common symbol size file\n\n"));
2972 header_printed = true;
2975 name = demangle (h->root.string);
2977 len = strlen (name);
2992 if (size <= 0xffffffff)
2993 sprintf (buf, "%lx", (unsigned long) size);
2995 sprintf_vma (buf, size);
3005 minfo ("%B\n", section->owner);
3012 run through the input files and ensure that every input
3013 section has somewhere to go. If one is found without
3014 a destination then create an input request and place it
3015 into the statement tree.
3019 lang_place_orphans ()
3021 lang_input_statement_type *file;
3023 for (file = (lang_input_statement_type *) file_chain.head;
3024 file != (lang_input_statement_type *) NULL;
3025 file = (lang_input_statement_type *) file->next)
3029 for (s = file->the_bfd->sections;
3030 s != (asection *) NULL;
3033 if (s->output_section == (asection *) NULL)
3035 /* This section of the file is not attatched, root
3036 around for a sensible place for it to go */
3038 if (file->just_syms_flag)
3040 /* We are only retrieving symbol values from this
3041 file. We want the symbols to act as though the
3042 values in the file are absolute. */
3043 s->output_section = bfd_abs_section_ptr;
3044 s->output_offset = s->vma;
3046 else if (strcmp (s->name, "COMMON") == 0)
3048 /* This is a lonely common section which must have
3049 come from an archive. We attach to the section
3050 with the wildcard. */
3051 if (! link_info.relocateable
3052 || command_line.force_common_definition)
3054 if (default_common_section == NULL)
3057 /* This message happens when using the
3058 svr3.ifile linker script, so I have
3060 info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
3062 default_common_section =
3063 lang_output_section_statement_lookup (".bss");
3066 wild_doit (&default_common_section->children, s,
3067 default_common_section, file);
3070 else if (ldemul_place_orphan (file, s))
3074 lang_output_section_statement_type *os =
3075 lang_output_section_statement_lookup (s->name);
3077 wild_doit (&os->children, s, os, file);
3086 lang_set_flags (ptr, flags)
3087 lang_memory_region_type *ptr;
3090 flagword *ptr_flags = &ptr->flags;
3092 ptr->flags = ptr->not_flags = 0;
3098 ptr_flags = (ptr_flags == &ptr->flags) ? &ptr->not_flags : &ptr->flags;
3102 *ptr_flags |= SEC_ALLOC;
3106 *ptr_flags |= SEC_READONLY;
3110 *ptr_flags |= SEC_DATA;
3114 *ptr_flags |= SEC_CODE;
3119 *ptr_flags |= SEC_LOAD;
3123 einfo (_("%P%F: invalid syntax in flags\n"));
3130 /* Call a function on each input file. This function will be called
3131 on an archive, but not on the elements. */
3134 lang_for_each_input_file (func)
3135 void (*func) PARAMS ((lang_input_statement_type *));
3137 lang_input_statement_type *f;
3139 for (f = (lang_input_statement_type *) input_file_chain.head;
3141 f = (lang_input_statement_type *) f->next_real_file)
3145 /* Call a function on each file. The function will be called on all
3146 the elements of an archive which are included in the link, but will
3147 not be called on the archive file itself. */
3150 lang_for_each_file (func)
3151 void (*func) PARAMS ((lang_input_statement_type *));
3153 lang_input_statement_type *f;
3155 for (f = (lang_input_statement_type *) file_chain.head;
3156 f != (lang_input_statement_type *) NULL;
3157 f = (lang_input_statement_type *) f->next)
3168 lang_for_each_input_section (func)
3169 void (*func) PARAMS ((bfd * ab, asection * as));
3171 lang_input_statement_type *f;
3173 for (f = (lang_input_statement_type *) file_chain.head;
3174 f != (lang_input_statement_type *) NULL;
3175 f = (lang_input_statement_type *) f->next)
3179 for (s = f->the_bfd->sections;
3180 s != (asection *) NULL;
3183 func (f->the_bfd, s);
3191 ldlang_add_file (entry)
3192 lang_input_statement_type * entry;
3196 lang_statement_append (&file_chain,
3197 (lang_statement_union_type *) entry,
3200 /* The BFD linker needs to have a list of all input BFDs involved in
3202 ASSERT (entry->the_bfd->link_next == (bfd *) NULL);
3203 ASSERT (entry->the_bfd != output_bfd);
3204 for (pp = &link_info.input_bfds;
3205 *pp != (bfd *) NULL;
3206 pp = &(*pp)->link_next)
3208 *pp = entry->the_bfd;
3209 entry->the_bfd->usrdata = (PTR) entry;
3210 bfd_set_gp_size (entry->the_bfd, g_switch_value);
3212 /* Look through the sections and check for any which should not be
3213 included in the link. We need to do this now, so that we can
3214 notice when the backend linker tries to report multiple
3215 definition errors for symbols which are in sections we aren't
3216 going to link. FIXME: It might be better to entirely ignore
3217 symbols which are defined in sections which are going to be
3218 discarded. This would require modifying the backend linker for
3219 each backend which might set the SEC_LINK_ONCE flag. If we do
3220 this, we should probably handle SEC_EXCLUDE in the same way. */
3222 bfd_map_over_sections (entry->the_bfd, section_already_linked, (PTR) entry);
3226 lang_add_output (name, from_script)
3230 /* Make -o on command line override OUTPUT in script. */
3231 if (had_output_filename == false || !from_script)
3233 output_filename = name;
3234 had_output_filename = true;
3239 static lang_output_section_statement_type *current_section;
3251 for (l = 0; l < 32; l++)
3253 if (i >= (unsigned int) x)
3262 lang_enter_output_section_statement (output_section_statement_name,
3263 address_exp, sectype, block_value,
3264 align, subalign, ebase)
3265 const char *output_section_statement_name;
3266 etree_type * address_exp;
3267 enum section_type sectype;
3268 bfd_vma block_value;
3270 etree_type *subalign;
3273 lang_output_section_statement_type *os;
3277 lang_output_section_statement_lookup (output_section_statement_name);
3281 /* Add this statement to tree */
3282 /* add_statement(lang_output_section_statement_enum,
3283 output_section_statement);*/
3284 /* Make next things chain into subchain of this */
3286 if (os->addr_tree ==
3287 (etree_type *) NULL)
3292 os->sectype = sectype;
3293 if (sectype != noload_section)
3294 os->flags = SEC_NO_FLAGS;
3296 os->flags = SEC_NEVER_LOAD;
3297 os->block_value = block_value ? block_value : 1;
3298 stat_ptr = &os->children;
3300 os->subsection_alignment = topower(
3301 exp_get_value_int(subalign, -1,
3302 "subsection alignment",
3304 os->section_alignment = topower(
3305 exp_get_value_int(align, -1,
3306 "section alignment", 0));
3308 os->load_base = ebase;
3315 lang_output_statement_type *new =
3316 new_stat (lang_output_statement, stat_ptr);
3318 new->name = output_filename;
3321 /* Reset the current counters in the regions */
3323 reset_memory_regions ()
3325 lang_memory_region_type *p = lang_memory_region_list;
3327 for (p = lang_memory_region_list;
3328 p != (lang_memory_region_type *) NULL;
3331 p->old_length = (bfd_size_type) (p->current - p->origin);
3332 p->current = p->origin;
3339 lang_reasonable_defaults ();
3340 current_target = default_target;
3342 lang_for_each_statement (ldlang_open_output); /* Open the output file */
3344 ldemul_create_output_section_statements ();
3346 /* Add to the hash table all undefineds on the command line */
3347 lang_place_undefineds ();
3349 /* Create a bfd for each input file */
3350 current_target = default_target;
3351 open_input_bfds (statement_list.head, false);
3353 ldemul_after_open ();
3355 /* Make sure that we're not mixing architectures. We call this
3356 after all the input files have been opened, but before we do any
3357 other processing, so that any operations merge_private_bfd_data
3358 does on the output file will be known during the rest of the
3362 /* Build all sets based on the information gathered from the input
3364 ldctor_build_sets ();
3366 /* Size up the common data */
3369 /* Run through the contours of the script and attach input sections
3370 to the correct output sections
3372 map_input_to_output_sections (statement_list.head, (char *) NULL,
3373 (lang_output_section_statement_type *) NULL);
3376 /* Find any sections not attached explicitly and handle them */
3377 lang_place_orphans ();
3379 ldemul_before_allocation ();
3381 /* We must record the program headers before we try to fix the
3382 section positions, since they will affect SIZEOF_HEADERS. */
3383 lang_record_phdrs ();
3385 /* Now run around and relax if we can */
3386 if (command_line.relax)
3388 /* First time round is a trial run to get the 'worst case'
3389 addresses of the objects if there was no relaxing. */
3390 lang_size_sections (statement_list.head,
3392 &(statement_list.head), 0, (bfd_vma) 0, false);
3394 /* Keep relaxing until bfd_relax_section gives up. */
3397 reset_memory_regions ();
3399 relax_again = false;
3401 /* Do all the assignments with our current guesses as to
3403 lang_do_assignments (statement_list.head,
3405 (fill_type) 0, (bfd_vma) 0);
3407 /* Perform another relax pass - this time we know where the
3408 globals are, so can make better guess. */
3409 lang_size_sections (statement_list.head,
3411 &(statement_list.head), 0, (bfd_vma) 0, true);
3413 while (relax_again);
3417 /* Size up the sections. */
3418 lang_size_sections (statement_list.head,
3420 &(statement_list.head), 0, (bfd_vma) 0, false);
3423 /* See if anything special should be done now we know how big
3425 ldemul_after_allocation ();
3427 /* Fix any .startof. or .sizeof. symbols. */
3428 lang_set_startof ();
3430 /* Do all the assignments, now that we know the final restingplaces
3431 of all the symbols */
3433 lang_do_assignments (statement_list.head,
3435 (fill_type) 0, (bfd_vma) 0);
3443 /* EXPORTED TO YACC */
3446 lang_add_wild (section_name, sections_sorted, filename, filenames_sorted)
3447 const char *const section_name;
3448 boolean sections_sorted;
3449 const char *const filename;
3450 boolean filenames_sorted;
3452 lang_wild_statement_type *new = new_stat (lang_wild_statement,
3455 if (section_name != (char *) NULL && strcmp (section_name, "COMMON") == 0)
3457 placed_commons = true;
3459 if (filename != (char *) NULL)
3461 lang_has_input_file = true;
3463 new->section_name = section_name;
3464 new->sections_sorted = sections_sorted;
3465 new->filename = filename;
3466 new->filenames_sorted = filenames_sorted;
3467 lang_list_init (&new->children);
3471 lang_section_start (name, address)
3473 etree_type * address;
3475 lang_address_statement_type *ad = new_stat (lang_address_statement, stat_ptr);
3477 ad->section_name = name;
3478 ad->address = address;
3481 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
3482 because of a -e argument on the command line, or zero if this is
3483 called by ENTRY in a linker script. Command line arguments take
3486 /* WINDOWS_NT. When an entry point has been specified, we will also force
3487 this symbol to be defined by calling ldlang_add_undef (equivalent to
3488 having switch -u entry_name on the command line). The reason we do
3489 this is so that the user doesn't have to because they would have to use
3490 the -u switch if they were specifying an entry point other than
3491 _mainCRTStartup. Specifically, if creating a windows application, entry
3492 point _WinMainCRTStartup must be specified.
3493 What I have found for non console applications (entry not _mainCRTStartup)
3494 is that the .obj that contains mainCRTStartup is brought in since it is
3495 the first encountered in libc.lib and it has other symbols in it which will
3496 be pulled in by the link process. To avoid this, adding -u with the entry
3497 point name specified forces the correct .obj to be used. We can avoid
3498 making the user do this by always adding the entry point name as an
3499 undefined symbol. */
3502 lang_add_entry (name, cmdline)
3506 if (entry_symbol == NULL
3508 || ! entry_from_cmdline)
3510 entry_symbol = name;
3511 entry_from_cmdline = cmdline;
3514 /* don't do this yet. It seems to work (the executables run), but the
3515 image created is very different from what I was getting before indicating
3516 that something else is being pulled in. When everything else is working,
3517 then try to put this back in to see if it will do the right thing for
3518 other more complicated applications */
3519 ldlang_add_undef (name);
3524 lang_add_target (name)
3527 lang_target_statement_type *new = new_stat (lang_target_statement,
3543 map_option_f = true;
3554 lang_fill_statement_type *new = new_stat (lang_fill_statement,
3561 lang_add_data (type, exp)
3563 union etree_union *exp;
3566 lang_data_statement_type *new = new_stat (lang_data_statement,
3574 /* Create a new reloc statement. RELOC is the BFD relocation type to
3575 generate. HOWTO is the corresponding howto structure (we could
3576 look this up, but the caller has already done so). SECTION is the
3577 section to generate a reloc against, or NAME is the name of the
3578 symbol to generate a reloc against. Exactly one of SECTION and
3579 NAME must be NULL. ADDEND is an expression for the addend. */
3582 lang_add_reloc (reloc, howto, section, name, addend)
3583 bfd_reloc_code_real_type reloc;
3584 reloc_howto_type *howto;
3587 union etree_union *addend;
3589 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
3593 p->section = section;
3595 p->addend_exp = addend;
3597 p->addend_value = 0;
3598 p->output_section = NULL;
3603 lang_add_assignment (exp)
3606 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
3613 lang_add_attribute (attribute)
3614 enum statement_enum attribute;
3616 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
3623 if (startup_file != (char *) NULL)
3625 einfo (_("%P%Fmultiple STARTUP files\n"));
3627 first_file->filename = name;
3628 first_file->local_sym_name = name;
3629 first_file->real = true;
3631 startup_file = name;
3638 lang_float_flag = maybe;
3642 lang_leave_output_section_statement (fill, memspec, phdrs)
3644 const char *memspec;
3645 struct lang_output_section_phdr_list *phdrs;
3647 current_section->fill = fill;
3648 current_section->region = lang_memory_region_lookup (memspec);
3649 current_section->phdrs = phdrs;
3650 stat_ptr = &statement_list;
3654 Create an absolute symbol with the given name with the value of the
3655 address of first byte of the section named.
3657 If the symbol already exists, then do nothing.
3660 lang_abs_symbol_at_beginning_of (secname, name)
3661 const char *secname;
3664 struct bfd_link_hash_entry *h;
3666 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
3667 if (h == (struct bfd_link_hash_entry *) NULL)
3668 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
3670 if (h->type == bfd_link_hash_new
3671 || h->type == bfd_link_hash_undefined)
3675 h->type = bfd_link_hash_defined;
3677 sec = bfd_get_section_by_name (output_bfd, secname);
3678 if (sec == (asection *) NULL)
3681 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
3683 h->u.def.section = bfd_abs_section_ptr;
3688 Create an absolute symbol with the given name with the value of the
3689 address of the first byte after the end of the section named.
3691 If the symbol already exists, then do nothing.
3694 lang_abs_symbol_at_end_of (secname, name)
3695 const char *secname;
3698 struct bfd_link_hash_entry *h;
3700 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
3701 if (h == (struct bfd_link_hash_entry *) NULL)
3702 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
3704 if (h->type == bfd_link_hash_new
3705 || h->type == bfd_link_hash_undefined)
3709 h->type = bfd_link_hash_defined;
3711 sec = bfd_get_section_by_name (output_bfd, secname);
3712 if (sec == (asection *) NULL)
3715 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
3716 + bfd_section_size (output_bfd, sec));
3718 h->u.def.section = bfd_abs_section_ptr;
3723 lang_statement_append (list, element, field)
3724 lang_statement_list_type * list;
3725 lang_statement_union_type * element;
3726 lang_statement_union_type ** field;
3728 *(list->tail) = element;
3732 /* Set the output format type. -oformat overrides scripts. */
3735 lang_add_output_format (format, big, little, from_script)
3741 if (output_target == NULL || !from_script)
3743 if (command_line.endian == ENDIAN_BIG
3746 else if (command_line.endian == ENDIAN_LITTLE
3750 output_target = format;
3754 /* Enter a group. This creates a new lang_group_statement, and sets
3755 stat_ptr to build new statements within the group. */
3760 lang_group_statement_type *g;
3762 g = new_stat (lang_group_statement, stat_ptr);
3763 lang_list_init (&g->children);
3764 stat_ptr = &g->children;
3767 /* Leave a group. This just resets stat_ptr to start writing to the
3768 regular list of statements again. Note that this will not work if
3769 groups can occur inside anything else which can adjust stat_ptr,
3770 but currently they can't. */
3775 stat_ptr = &statement_list;
3778 /* Add a new program header. This is called for each entry in a PHDRS
3779 command in a linker script. */
3782 lang_new_phdr (name, type, filehdr, phdrs, at, flags)
3790 struct lang_phdr *n, **pp;
3792 n = (struct lang_phdr *) stat_alloc (sizeof (struct lang_phdr));
3795 n->type = exp_get_value_int (type, 0, "program header type",
3796 lang_final_phase_enum);
3797 n->filehdr = filehdr;
3802 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
3807 /* Record the program header information in the output BFD. FIXME: We
3808 should not be calling an ELF specific function here. */
3811 lang_record_phdrs ()
3815 struct lang_output_section_phdr_list *last;
3816 struct lang_phdr *l;
3817 lang_statement_union_type *u;
3820 secs = (asection **) xmalloc (alc * sizeof (asection *));
3822 for (l = lang_phdr_list; l != NULL; l = l->next)
3829 for (u = lang_output_section_statement.head;
3831 u = u->output_section_statement.next)
3833 lang_output_section_statement_type *os;
3834 struct lang_output_section_phdr_list *pl;
3836 os = &u->output_section_statement;
3843 if (os->sectype == noload_section
3844 || os->bfd_section == NULL
3845 || (os->bfd_section->flags & SEC_ALLOC) == 0)
3850 if (os->bfd_section == NULL)
3853 for (; pl != NULL; pl = pl->next)
3855 if (strcmp (pl->name, l->name) == 0)
3860 secs = ((asection **)
3861 xrealloc (secs, alc * sizeof (asection *)));
3863 secs[c] = os->bfd_section;
3870 if (l->flags == NULL)
3873 flags = exp_get_vma (l->flags, 0, "phdr flags",
3874 lang_final_phase_enum);
3879 at = exp_get_vma (l->at, 0, "phdr load address",
3880 lang_final_phase_enum);
3882 if (! bfd_record_phdr (output_bfd, l->type,
3883 l->flags == NULL ? false : true,
3885 l->at == NULL ? false : true,
3886 at, l->filehdr, l->phdrs, c, secs))
3887 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
3892 /* Make sure all the phdr assignments succeeded. */
3893 for (u = lang_output_section_statement.head;
3895 u = u->output_section_statement.next)
3897 struct lang_output_section_phdr_list *pl;
3899 if (u->output_section_statement.bfd_section == NULL)
3902 for (pl = u->output_section_statement.phdrs;
3905 if (! pl->used && strcmp (pl->name, "NONE") != 0)
3906 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
3907 u->output_section_statement.name, pl->name);
3911 /* Record a list of sections which may not be cross referenced. */
3914 lang_add_nocrossref (l)
3915 struct lang_nocrossref *l;
3917 struct lang_nocrossrefs *n;
3919 n = (struct lang_nocrossrefs *) xmalloc (sizeof *n);
3920 n->next = nocrossref_list;
3922 nocrossref_list = n;
3924 /* Set notice_all so that we get informed about all symbols. */
3925 link_info.notice_all = true;
3928 /* Overlay handling. We handle overlays with some static variables. */
3930 /* The overlay virtual address. */
3931 static etree_type *overlay_vma;
3933 /* The overlay load address. */
3934 static etree_type *overlay_lma;
3936 /* Whether nocrossrefs is set for this overlay. */
3937 static int overlay_nocrossrefs;
3939 /* An expression for the maximum section size seen so far. */
3940 static etree_type *overlay_max;
3942 /* A list of all the sections in this overlay. */
3946 struct overlay_list *next;
3947 lang_output_section_statement_type *os;
3950 static struct overlay_list *overlay_list;
3952 /* Start handling an overlay. */
3955 lang_enter_overlay (vma_expr, lma_expr, nocrossrefs)
3956 etree_type *vma_expr;
3957 etree_type *lma_expr;
3960 /* The grammar should prevent nested overlays from occurring. */
3961 ASSERT (overlay_vma == NULL
3962 && overlay_lma == NULL
3963 && overlay_list == NULL
3964 && overlay_max == NULL);
3966 overlay_vma = vma_expr;
3967 overlay_lma = lma_expr;
3968 overlay_nocrossrefs = nocrossrefs;
3971 /* Start a section in an overlay. We handle this by calling
3972 lang_enter_output_section_statement with the correct VMA and LMA. */
3975 lang_enter_overlay_section (name)
3978 struct overlay_list *n;
3981 lang_enter_output_section_statement (name, overlay_vma, normal_section,
3982 0, 0, 0, overlay_lma);
3984 /* If this is the first section, then base the VMA and LMA of future
3985 sections on this one. This will work correctly even if `.' is
3986 used in the addresses. */
3987 if (overlay_list == NULL)
3989 overlay_vma = exp_nameop (ADDR, name);
3990 overlay_lma = exp_nameop (LOADADDR, name);
3993 /* Remember the section. */
3994 n = (struct overlay_list *) xmalloc (sizeof *n);
3995 n->os = current_section;
3996 n->next = overlay_list;
3999 size = exp_nameop (SIZEOF, name);
4001 /* Adjust the LMA for the next section. */
4002 overlay_lma = exp_binop ('+', overlay_lma, size);
4004 /* Arrange to work out the maximum section end address. */
4005 if (overlay_max == NULL)
4008 overlay_max = exp_binop (MAX, overlay_max, size);
4011 /* Finish a section in an overlay. There isn't any special to do
4015 lang_leave_overlay_section (fill, phdrs)
4017 struct lang_output_section_phdr_list *phdrs;
4024 name = current_section->name;
4026 lang_leave_output_section_statement (fill, "*default*", phdrs);
4028 /* Define the magic symbols. */
4030 clean = xmalloc (strlen (name) + 1);
4032 for (s1 = name; *s1 != '\0'; s1++)
4033 if (isalnum ((unsigned char) *s1) || *s1 == '_')
4037 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
4038 sprintf (buf, "__load_start_%s", clean);
4039 lang_add_assignment (exp_assop ('=', buf,
4040 exp_nameop (LOADADDR, name)));
4042 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
4043 sprintf (buf, "__load_stop_%s", clean);
4044 lang_add_assignment (exp_assop ('=', buf,
4046 exp_nameop (LOADADDR, name),
4047 exp_nameop (SIZEOF, name))));
4052 /* Finish an overlay. If there are any overlay wide settings, this
4053 looks through all the sections in the overlay and sets them. */
4056 lang_leave_overlay (fill, memspec, phdrs)
4058 const char *memspec;
4059 struct lang_output_section_phdr_list *phdrs;
4061 lang_memory_region_type *region;
4062 struct overlay_list *l;
4063 struct lang_nocrossref *nocrossref;
4065 if (memspec == NULL)
4068 region = lang_memory_region_lookup (memspec);
4075 struct overlay_list *next;
4077 if (fill != 0 && l->os->fill == 0)
4079 if (region != NULL && l->os->region == NULL)
4080 l->os->region = region;
4081 if (phdrs != NULL && l->os->phdrs == NULL)
4082 l->os->phdrs = phdrs;
4084 if (overlay_nocrossrefs)
4086 struct lang_nocrossref *nc;
4088 nc = (struct lang_nocrossref *) xmalloc (sizeof *nc);
4089 nc->name = l->os->name;
4090 nc->next = nocrossref;
4099 if (nocrossref != NULL)
4100 lang_add_nocrossref (nocrossref);
4102 /* Update . for the end of the overlay. */
4103 lang_add_assignment (exp_assop ('=', ".",
4104 exp_binop ('+', overlay_vma, overlay_max)));
4108 overlay_nocrossrefs = 0;
4109 overlay_list = NULL;
4113 /* Version handling. This is only useful for ELF. */
4115 /* This global variable holds the version tree that we build. */
4117 struct bfd_elf_version_tree *lang_elf_version_info;
4119 /* This is called for each variable name or match expression. */
4121 struct bfd_elf_version_expr *
4122 lang_new_vers_regex (orig, new)
4123 struct bfd_elf_version_expr *orig;
4126 struct bfd_elf_version_expr *ret;
4128 ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret);
4134 /* This is called for each set of variable names and match
4137 struct bfd_elf_version_tree *
4138 lang_new_vers_node (globals, locals)
4139 struct bfd_elf_version_expr *globals;
4140 struct bfd_elf_version_expr *locals;
4142 struct bfd_elf_version_tree *ret;
4144 ret = (struct bfd_elf_version_tree *) xmalloc (sizeof *ret);
4148 ret->globals = globals;
4149 ret->locals = locals;
4151 ret->name_indx = (unsigned int) -1;
4156 /* This static variable keeps track of version indices. */
4158 static int version_index;
4160 /* This is called when we know the name and dependencies of the
4164 lang_register_vers_node (name, version, deps)
4166 struct bfd_elf_version_tree *version;
4167 struct bfd_elf_version_deps *deps;
4169 struct bfd_elf_version_tree *t, **pp;
4170 struct bfd_elf_version_expr *e1;
4172 /* Make sure this node has a unique name. */
4173 for (t = lang_elf_version_info; t != NULL; t = t->next)
4174 if (strcmp (t->name, name) == 0)
4175 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
4177 /* Check the global and local match names, and make sure there
4178 aren't any duplicates. */
4180 for (e1 = version->globals; e1 != NULL; e1 = e1->next)
4182 for (t = lang_elf_version_info; t != NULL; t = t->next)
4184 struct bfd_elf_version_expr *e2;
4186 for (e2 = t->locals; e2 != NULL; e2 = e2->next)
4187 if (strcmp (e1->match, e2->match) == 0)
4188 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
4193 for (e1 = version->locals; e1 != NULL; e1 = e1->next)
4195 for (t = lang_elf_version_info; t != NULL; t = t->next)
4197 struct bfd_elf_version_expr *e2;
4199 for (e2 = t->globals; e2 != NULL; e2 = e2->next)
4200 if (strcmp (e1->match, e2->match) == 0)
4201 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
4206 version->deps = deps;
4207 version->name = name;
4209 version->vernum = version_index;
4211 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
4216 /* This is called when we see a version dependency. */
4218 struct bfd_elf_version_deps *
4219 lang_add_vers_depend (list, name)
4220 struct bfd_elf_version_deps *list;
4223 struct bfd_elf_version_deps *ret;
4224 struct bfd_elf_version_tree *t;
4226 ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret);
4229 for (t = lang_elf_version_info; t != NULL; t = t->next)
4231 if (strcmp (t->name, name) == 0)
4233 ret->version_needed = t;
4238 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);