1 /* Linker command language support.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 This file is part of the GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
25 #include "libiberty.h"
26 #include "safe-ctype.h"
46 #endif /* ENABLE_PLUGINS */
49 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
52 /* Locals variables. */
53 static struct obstack stat_obstack;
54 static struct obstack map_obstack;
56 #define obstack_chunk_alloc xmalloc
57 #define obstack_chunk_free free
58 static const char *startup_file;
59 static const char *entry_symbol_default = "start";
60 static bfd_boolean placed_commons = FALSE;
61 static bfd_boolean stripped_excluded_sections = FALSE;
62 static lang_output_section_statement_type *default_common_section;
63 static bfd_boolean map_option_f;
64 static bfd_vma print_dot;
65 static lang_input_statement_type *first_file;
66 static const char *current_target;
67 static lang_statement_list_type statement_list;
68 static struct bfd_hash_table lang_definedness_table;
69 static lang_statement_list_type *stat_save[10];
70 static lang_statement_list_type **stat_save_ptr = &stat_save[0];
71 static struct unique_sections *unique_section_list;
72 static bfd_boolean ldlang_sysrooted_script = FALSE;
74 /* Forward declarations. */
75 static void exp_init_os (etree_type *);
76 static void init_map_userdata (bfd *, asection *, void *);
77 static lang_input_statement_type *lookup_name (const char *);
78 static struct bfd_hash_entry *lang_definedness_newfunc
79 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
80 static void insert_undefined (const char *);
81 static bfd_boolean sort_def_symbol (struct bfd_link_hash_entry *, void *);
82 static void print_statement (lang_statement_union_type *,
83 lang_output_section_statement_type *);
84 static void print_statement_list (lang_statement_union_type *,
85 lang_output_section_statement_type *);
86 static void print_statements (void);
87 static void print_input_section (asection *, bfd_boolean);
88 static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *);
89 static void lang_record_phdrs (void);
90 static void lang_do_version_exports_section (void);
91 static void lang_finalize_version_expr_head
92 (struct bfd_elf_version_expr_head *);
94 /* Exported variables. */
95 const char *output_target;
96 lang_output_section_statement_type *abs_output_section;
97 lang_statement_list_type lang_output_section_statement;
98 lang_statement_list_type *stat_ptr = &statement_list;
99 lang_statement_list_type file_chain = { NULL, NULL };
100 lang_statement_list_type input_file_chain;
101 struct bfd_sym_chain entry_symbol = { NULL, NULL };
102 const char *entry_section = ".text";
103 bfd_boolean entry_from_cmdline;
104 bfd_boolean undef_from_cmdline;
105 bfd_boolean lang_has_input_file = FALSE;
106 bfd_boolean had_output_filename = FALSE;
107 bfd_boolean lang_float_flag = FALSE;
108 bfd_boolean delete_output_file_on_failure = FALSE;
109 struct lang_phdr *lang_phdr_list;
110 struct lang_nocrossrefs *nocrossref_list;
111 bfd_boolean missing_file = FALSE;
113 /* Functions that traverse the linker script and might evaluate
114 DEFINED() need to increment this. */
115 int lang_statement_iteration = 0;
117 etree_type *base; /* Relocation base - or null */
119 /* Return TRUE if the PATTERN argument is a wildcard pattern.
120 Although backslashes are treated specially if a pattern contains
121 wildcards, we do not consider the mere presence of a backslash to
122 be enough to cause the pattern to be treated as a wildcard.
123 That lets us handle DOS filenames more naturally. */
124 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
126 #define new_stat(x, y) \
127 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
129 #define outside_section_address(q) \
130 ((q)->output_offset + (q)->output_section->vma)
132 #define outside_symbol_address(q) \
133 ((q)->value + outside_section_address (q->section))
135 #define SECTION_NAME_MAP_LENGTH (16)
138 stat_alloc (size_t size)
140 return obstack_alloc (&stat_obstack, size);
144 name_match (const char *pattern, const char *name)
146 if (wildcardp (pattern))
147 return fnmatch (pattern, name, 0);
148 return strcmp (pattern, name);
151 /* If PATTERN is of the form archive:file, return a pointer to the
152 separator. If not, return NULL. */
155 archive_path (const char *pattern)
159 if (link_info.path_separator == 0)
162 p = strchr (pattern, link_info.path_separator);
163 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
164 if (p == NULL || link_info.path_separator != ':')
167 /* Assume a match on the second char is part of drive specifier,
168 as in "c:\silly.dos". */
169 if (p == pattern + 1 && ISALPHA (*pattern))
170 p = strchr (p + 1, link_info.path_separator);
175 /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path,
176 return whether F matches FILE_SPEC. */
179 input_statement_is_archive_path (const char *file_spec, char *sep,
180 lang_input_statement_type *f)
182 bfd_boolean match = FALSE;
185 || name_match (sep + 1, f->filename) == 0)
186 && ((sep != file_spec)
187 == (f->the_bfd != NULL && f->the_bfd->my_archive != NULL)))
191 if (sep != file_spec)
193 const char *aname = f->the_bfd->my_archive->filename;
195 match = name_match (file_spec, aname) == 0;
196 *sep = link_info.path_separator;
203 unique_section_p (const asection *sec,
204 const lang_output_section_statement_type *os)
206 struct unique_sections *unam;
209 if (link_info.relocatable
210 && sec->owner != NULL
211 && bfd_is_group_section (sec->owner, sec))
213 && strcmp (os->name, DISCARD_SECTION_NAME) == 0);
216 for (unam = unique_section_list; unam; unam = unam->next)
217 if (name_match (unam->name, secnam) == 0)
223 /* Generic traversal routines for finding matching sections. */
225 /* Try processing a section against a wildcard. This just calls
226 the callback unless the filename exclusion list is present
227 and excludes the file. It's hardly ever present so this
228 function is very fast. */
231 walk_wild_consider_section (lang_wild_statement_type *ptr,
232 lang_input_statement_type *file,
234 struct wildcard_list *sec,
238 struct name_list *list_tmp;
240 /* Don't process sections from files which were excluded. */
241 for (list_tmp = sec->spec.exclude_name_list;
243 list_tmp = list_tmp->next)
245 char *p = archive_path (list_tmp->name);
249 if (input_statement_is_archive_path (list_tmp->name, p, file))
253 else if (name_match (list_tmp->name, file->filename) == 0)
256 /* FIXME: Perhaps remove the following at some stage? Matching
257 unadorned archives like this was never documented and has
258 been superceded by the archive:path syntax. */
259 else if (file->the_bfd != NULL
260 && file->the_bfd->my_archive != NULL
261 && name_match (list_tmp->name,
262 file->the_bfd->my_archive->filename) == 0)
266 (*callback) (ptr, sec, s, file, data);
269 /* Lowest common denominator routine that can handle everything correctly,
273 walk_wild_section_general (lang_wild_statement_type *ptr,
274 lang_input_statement_type *file,
279 struct wildcard_list *sec;
281 for (s = file->the_bfd->sections; s != NULL; s = s->next)
283 sec = ptr->section_list;
285 (*callback) (ptr, sec, s, file, data);
289 bfd_boolean skip = FALSE;
291 if (sec->spec.name != NULL)
293 const char *sname = bfd_get_section_name (file->the_bfd, s);
295 skip = name_match (sec->spec.name, sname) != 0;
299 walk_wild_consider_section (ptr, file, s, sec, callback, data);
306 /* Routines to find a single section given its name. If there's more
307 than one section with that name, we report that. */
311 asection *found_section;
312 bfd_boolean multiple_sections_found;
313 } section_iterator_callback_data;
316 section_iterator_callback (bfd *abfd ATTRIBUTE_UNUSED, asection *s, void *data)
318 section_iterator_callback_data *d = (section_iterator_callback_data *) data;
320 if (d->found_section != NULL)
322 d->multiple_sections_found = TRUE;
326 d->found_section = s;
331 find_section (lang_input_statement_type *file,
332 struct wildcard_list *sec,
333 bfd_boolean *multiple_sections_found)
335 section_iterator_callback_data cb_data = { NULL, FALSE };
337 bfd_get_section_by_name_if (file->the_bfd, sec->spec.name,
338 section_iterator_callback, &cb_data);
339 *multiple_sections_found = cb_data.multiple_sections_found;
340 return cb_data.found_section;
343 /* Code for handling simple wildcards without going through fnmatch,
344 which can be expensive because of charset translations etc. */
346 /* A simple wild is a literal string followed by a single '*',
347 where the literal part is at least 4 characters long. */
350 is_simple_wild (const char *name)
352 size_t len = strcspn (name, "*?[");
353 return len >= 4 && name[len] == '*' && name[len + 1] == '\0';
357 match_simple_wild (const char *pattern, const char *name)
359 /* The first four characters of the pattern are guaranteed valid
360 non-wildcard characters. So we can go faster. */
361 if (pattern[0] != name[0] || pattern[1] != name[1]
362 || pattern[2] != name[2] || pattern[3] != name[3])
367 while (*pattern != '*')
368 if (*name++ != *pattern++)
374 /* Compare sections ASEC and BSEC according to SORT. */
377 compare_section (sort_type sort, asection *asec, asection *bsec)
386 case by_alignment_name:
387 ret = (bfd_section_alignment (bsec->owner, bsec)
388 - bfd_section_alignment (asec->owner, asec));
394 ret = strcmp (bfd_get_section_name (asec->owner, asec),
395 bfd_get_section_name (bsec->owner, bsec));
398 case by_name_alignment:
399 ret = strcmp (bfd_get_section_name (asec->owner, asec),
400 bfd_get_section_name (bsec->owner, bsec));
406 ret = (bfd_section_alignment (bsec->owner, bsec)
407 - bfd_section_alignment (asec->owner, asec));
414 /* Build a Binary Search Tree to sort sections, unlike insertion sort
415 used in wild_sort(). BST is considerably faster if the number of
416 of sections are large. */
418 static lang_section_bst_type **
419 wild_sort_fast (lang_wild_statement_type *wild,
420 struct wildcard_list *sec,
421 lang_input_statement_type *file ATTRIBUTE_UNUSED,
424 lang_section_bst_type **tree;
427 if (!wild->filenames_sorted
428 && (sec == NULL || sec->spec.sorted == none))
430 /* Append at the right end of tree. */
432 tree = &((*tree)->right);
438 /* Find the correct node to append this section. */
439 if (compare_section (sec->spec.sorted, section, (*tree)->section) < 0)
440 tree = &((*tree)->left);
442 tree = &((*tree)->right);
448 /* Use wild_sort_fast to build a BST to sort sections. */
451 output_section_callback_fast (lang_wild_statement_type *ptr,
452 struct wildcard_list *sec,
454 lang_input_statement_type *file,
457 lang_section_bst_type *node;
458 lang_section_bst_type **tree;
459 lang_output_section_statement_type *os;
461 os = (lang_output_section_statement_type *) output;
463 if (unique_section_p (section, os))
466 node = (lang_section_bst_type *) xmalloc (sizeof (lang_section_bst_type));
469 node->section = section;
471 tree = wild_sort_fast (ptr, sec, file, section);
476 /* Convert a sorted sections' BST back to list form. */
479 output_section_callback_tree_to_list (lang_wild_statement_type *ptr,
480 lang_section_bst_type *tree,
484 output_section_callback_tree_to_list (ptr, tree->left, output);
486 lang_add_section (&ptr->children, tree->section,
487 (lang_output_section_statement_type *) output);
490 output_section_callback_tree_to_list (ptr, tree->right, output);
495 /* Specialized, optimized routines for handling different kinds of
499 walk_wild_section_specs1_wild0 (lang_wild_statement_type *ptr,
500 lang_input_statement_type *file,
504 /* We can just do a hash lookup for the section with the right name.
505 But if that lookup discovers more than one section with the name
506 (should be rare), we fall back to the general algorithm because
507 we would otherwise have to sort the sections to make sure they
508 get processed in the bfd's order. */
509 bfd_boolean multiple_sections_found;
510 struct wildcard_list *sec0 = ptr->handler_data[0];
511 asection *s0 = find_section (file, sec0, &multiple_sections_found);
513 if (multiple_sections_found)
514 walk_wild_section_general (ptr, file, callback, data);
516 walk_wild_consider_section (ptr, file, s0, sec0, callback, data);
520 walk_wild_section_specs1_wild1 (lang_wild_statement_type *ptr,
521 lang_input_statement_type *file,
526 struct wildcard_list *wildsec0 = ptr->handler_data[0];
528 for (s = file->the_bfd->sections; s != NULL; s = s->next)
530 const char *sname = bfd_get_section_name (file->the_bfd, s);
531 bfd_boolean skip = !match_simple_wild (wildsec0->spec.name, sname);
534 walk_wild_consider_section (ptr, file, s, wildsec0, callback, data);
539 walk_wild_section_specs2_wild1 (lang_wild_statement_type *ptr,
540 lang_input_statement_type *file,
545 struct wildcard_list *sec0 = ptr->handler_data[0];
546 struct wildcard_list *wildsec1 = ptr->handler_data[1];
547 bfd_boolean multiple_sections_found;
548 asection *s0 = find_section (file, sec0, &multiple_sections_found);
550 if (multiple_sections_found)
552 walk_wild_section_general (ptr, file, callback, data);
556 /* Note that if the section was not found, s0 is NULL and
557 we'll simply never succeed the s == s0 test below. */
558 for (s = file->the_bfd->sections; s != NULL; s = s->next)
560 /* Recall that in this code path, a section cannot satisfy more
561 than one spec, so if s == s0 then it cannot match
564 walk_wild_consider_section (ptr, file, s, sec0, callback, data);
567 const char *sname = bfd_get_section_name (file->the_bfd, s);
568 bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname);
571 walk_wild_consider_section (ptr, file, s, wildsec1, callback,
578 walk_wild_section_specs3_wild2 (lang_wild_statement_type *ptr,
579 lang_input_statement_type *file,
584 struct wildcard_list *sec0 = ptr->handler_data[0];
585 struct wildcard_list *wildsec1 = ptr->handler_data[1];
586 struct wildcard_list *wildsec2 = ptr->handler_data[2];
587 bfd_boolean multiple_sections_found;
588 asection *s0 = find_section (file, sec0, &multiple_sections_found);
590 if (multiple_sections_found)
592 walk_wild_section_general (ptr, file, callback, data);
596 for (s = file->the_bfd->sections; s != NULL; s = s->next)
599 walk_wild_consider_section (ptr, file, s, sec0, callback, data);
602 const char *sname = bfd_get_section_name (file->the_bfd, s);
603 bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname);
606 walk_wild_consider_section (ptr, file, s, wildsec1, callback, data);
609 skip = !match_simple_wild (wildsec2->spec.name, sname);
611 walk_wild_consider_section (ptr, file, s, wildsec2, callback,
619 walk_wild_section_specs4_wild2 (lang_wild_statement_type *ptr,
620 lang_input_statement_type *file,
625 struct wildcard_list *sec0 = ptr->handler_data[0];
626 struct wildcard_list *sec1 = ptr->handler_data[1];
627 struct wildcard_list *wildsec2 = ptr->handler_data[2];
628 struct wildcard_list *wildsec3 = ptr->handler_data[3];
629 bfd_boolean multiple_sections_found;
630 asection *s0 = find_section (file, sec0, &multiple_sections_found), *s1;
632 if (multiple_sections_found)
634 walk_wild_section_general (ptr, file, callback, data);
638 s1 = find_section (file, sec1, &multiple_sections_found);
639 if (multiple_sections_found)
641 walk_wild_section_general (ptr, file, callback, data);
645 for (s = file->the_bfd->sections; s != NULL; s = s->next)
648 walk_wild_consider_section (ptr, file, s, sec0, callback, data);
651 walk_wild_consider_section (ptr, file, s, sec1, callback, data);
654 const char *sname = bfd_get_section_name (file->the_bfd, s);
655 bfd_boolean skip = !match_simple_wild (wildsec2->spec.name,
659 walk_wild_consider_section (ptr, file, s, wildsec2, callback,
663 skip = !match_simple_wild (wildsec3->spec.name, sname);
665 walk_wild_consider_section (ptr, file, s, wildsec3,
673 walk_wild_section (lang_wild_statement_type *ptr,
674 lang_input_statement_type *file,
678 if (file->just_syms_flag)
681 (*ptr->walk_wild_section_handler) (ptr, file, callback, data);
684 /* Returns TRUE when name1 is a wildcard spec that might match
685 something name2 can match. We're conservative: we return FALSE
686 only if the prefixes of name1 and name2 are different up to the
687 first wildcard character. */
690 wild_spec_can_overlap (const char *name1, const char *name2)
692 size_t prefix1_len = strcspn (name1, "?*[");
693 size_t prefix2_len = strcspn (name2, "?*[");
694 size_t min_prefix_len;
696 /* Note that if there is no wildcard character, then we treat the
697 terminating 0 as part of the prefix. Thus ".text" won't match
698 ".text." or ".text.*", for example. */
699 if (name1[prefix1_len] == '\0')
701 if (name2[prefix2_len] == '\0')
704 min_prefix_len = prefix1_len < prefix2_len ? prefix1_len : prefix2_len;
706 return memcmp (name1, name2, min_prefix_len) == 0;
709 /* Select specialized code to handle various kinds of wildcard
713 analyze_walk_wild_section_handler (lang_wild_statement_type *ptr)
716 int wild_name_count = 0;
717 struct wildcard_list *sec;
721 ptr->walk_wild_section_handler = walk_wild_section_general;
722 ptr->handler_data[0] = NULL;
723 ptr->handler_data[1] = NULL;
724 ptr->handler_data[2] = NULL;
725 ptr->handler_data[3] = NULL;
728 /* Count how many wildcard_specs there are, and how many of those
729 actually use wildcards in the name. Also, bail out if any of the
730 wildcard names are NULL. (Can this actually happen?
731 walk_wild_section used to test for it.) And bail out if any
732 of the wildcards are more complex than a simple string
733 ending in a single '*'. */
734 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
737 if (sec->spec.name == NULL)
739 if (wildcardp (sec->spec.name))
742 if (!is_simple_wild (sec->spec.name))
747 /* The zero-spec case would be easy to optimize but it doesn't
748 happen in practice. Likewise, more than 4 specs doesn't
749 happen in practice. */
750 if (sec_count == 0 || sec_count > 4)
753 /* Check that no two specs can match the same section. */
754 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
756 struct wildcard_list *sec2;
757 for (sec2 = sec->next; sec2 != NULL; sec2 = sec2->next)
759 if (wild_spec_can_overlap (sec->spec.name, sec2->spec.name))
764 signature = (sec_count << 8) + wild_name_count;
768 ptr->walk_wild_section_handler = walk_wild_section_specs1_wild0;
771 ptr->walk_wild_section_handler = walk_wild_section_specs1_wild1;
774 ptr->walk_wild_section_handler = walk_wild_section_specs2_wild1;
777 ptr->walk_wild_section_handler = walk_wild_section_specs3_wild2;
780 ptr->walk_wild_section_handler = walk_wild_section_specs4_wild2;
786 /* Now fill the data array with pointers to the specs, first the
787 specs with non-wildcard names, then the specs with wildcard
788 names. It's OK to process the specs in different order from the
789 given order, because we've already determined that no section
790 will match more than one spec. */
792 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
793 if (!wildcardp (sec->spec.name))
794 ptr->handler_data[data_counter++] = sec;
795 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
796 if (wildcardp (sec->spec.name))
797 ptr->handler_data[data_counter++] = sec;
800 /* Handle a wild statement for a single file F. */
803 walk_wild_file (lang_wild_statement_type *s,
804 lang_input_statement_type *f,
808 if (f->the_bfd == NULL
809 || ! bfd_check_format (f->the_bfd, bfd_archive))
810 walk_wild_section (s, f, callback, data);
815 /* This is an archive file. We must map each member of the
816 archive separately. */
817 member = bfd_openr_next_archived_file (f->the_bfd, NULL);
818 while (member != NULL)
820 /* When lookup_name is called, it will call the add_symbols
821 entry point for the archive. For each element of the
822 archive which is included, BFD will call ldlang_add_file,
823 which will set the usrdata field of the member to the
824 lang_input_statement. */
825 if (member->usrdata != NULL)
827 walk_wild_section (s,
828 (lang_input_statement_type *) member->usrdata,
832 member = bfd_openr_next_archived_file (f->the_bfd, member);
838 walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
840 const char *file_spec = s->filename;
843 if (file_spec == NULL)
845 /* Perform the iteration over all files in the list. */
846 LANG_FOR_EACH_INPUT_STATEMENT (f)
848 walk_wild_file (s, f, callback, data);
851 else if ((p = archive_path (file_spec)) != NULL)
853 LANG_FOR_EACH_INPUT_STATEMENT (f)
855 if (input_statement_is_archive_path (file_spec, p, f))
856 walk_wild_file (s, f, callback, data);
859 else if (wildcardp (file_spec))
861 LANG_FOR_EACH_INPUT_STATEMENT (f)
863 if (fnmatch (file_spec, f->filename, 0) == 0)
864 walk_wild_file (s, f, callback, data);
869 lang_input_statement_type *f;
871 /* Perform the iteration over a single file. */
872 f = lookup_name (file_spec);
874 walk_wild_file (s, f, callback, data);
878 /* lang_for_each_statement walks the parse tree and calls the provided
879 function for each node, except those inside output section statements
880 with constraint set to -1. */
883 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
884 lang_statement_union_type *s)
886 for (; s != NULL; s = s->header.next)
890 switch (s->header.type)
892 case lang_constructors_statement_enum:
893 lang_for_each_statement_worker (func, constructor_list.head);
895 case lang_output_section_statement_enum:
896 if (s->output_section_statement.constraint != -1)
897 lang_for_each_statement_worker
898 (func, s->output_section_statement.children.head);
900 case lang_wild_statement_enum:
901 lang_for_each_statement_worker (func,
902 s->wild_statement.children.head);
904 case lang_group_statement_enum:
905 lang_for_each_statement_worker (func,
906 s->group_statement.children.head);
908 case lang_data_statement_enum:
909 case lang_reloc_statement_enum:
910 case lang_object_symbols_statement_enum:
911 case lang_output_statement_enum:
912 case lang_target_statement_enum:
913 case lang_input_section_enum:
914 case lang_input_statement_enum:
915 case lang_assignment_statement_enum:
916 case lang_padding_statement_enum:
917 case lang_address_statement_enum:
918 case lang_fill_statement_enum:
919 case lang_insert_statement_enum:
929 lang_for_each_statement (void (*func) (lang_statement_union_type *))
931 lang_for_each_statement_worker (func, statement_list.head);
934 /*----------------------------------------------------------------------*/
937 lang_list_init (lang_statement_list_type *list)
940 list->tail = &list->head;
944 push_stat_ptr (lang_statement_list_type *new_ptr)
946 if (stat_save_ptr >= stat_save + sizeof (stat_save) / sizeof (stat_save[0]))
948 *stat_save_ptr++ = stat_ptr;
955 if (stat_save_ptr <= stat_save)
957 stat_ptr = *--stat_save_ptr;
960 /* Build a new statement node for the parse tree. */
962 static lang_statement_union_type *
963 new_statement (enum statement_enum type,
965 lang_statement_list_type *list)
967 lang_statement_union_type *new_stmt;
969 new_stmt = (lang_statement_union_type *) stat_alloc (size);
970 new_stmt->header.type = type;
971 new_stmt->header.next = NULL;
972 lang_statement_append (list, new_stmt, &new_stmt->header.next);
976 /* Build a new input file node for the language. There are several
977 ways in which we treat an input file, eg, we only look at symbols,
978 or prefix it with a -l etc.
980 We can be supplied with requests for input files more than once;
981 they may, for example be split over several lines like foo.o(.text)
982 foo.o(.data) etc, so when asked for a file we check that we haven't
983 got it already so we don't duplicate the bfd. */
985 static lang_input_statement_type *
986 new_afile (const char *name,
987 lang_input_file_enum_type file_type,
989 bfd_boolean add_to_list)
991 lang_input_statement_type *p;
994 p = (lang_input_statement_type *) new_stat (lang_input_statement, stat_ptr);
997 p = (lang_input_statement_type *)
998 stat_alloc (sizeof (lang_input_statement_type));
999 p->header.type = lang_input_statement_enum;
1000 p->header.next = NULL;
1003 lang_has_input_file = TRUE;
1005 p->sysrooted = FALSE;
1007 if (file_type == lang_input_file_is_l_enum
1008 && name[0] == ':' && name[1] != '\0')
1010 file_type = lang_input_file_is_search_file_enum;
1016 case lang_input_file_is_symbols_only_enum:
1018 p->maybe_archive = FALSE;
1020 p->local_sym_name = name;
1021 p->just_syms_flag = TRUE;
1022 p->search_dirs_flag = FALSE;
1024 case lang_input_file_is_fake_enum:
1026 p->maybe_archive = FALSE;
1028 p->local_sym_name = name;
1029 p->just_syms_flag = FALSE;
1030 p->search_dirs_flag = FALSE;
1032 case lang_input_file_is_l_enum:
1033 p->maybe_archive = TRUE;
1036 p->local_sym_name = concat ("-l", name, (const char *) NULL);
1037 p->just_syms_flag = FALSE;
1038 p->search_dirs_flag = TRUE;
1040 case lang_input_file_is_marker_enum:
1042 p->maybe_archive = FALSE;
1044 p->local_sym_name = name;
1045 p->just_syms_flag = FALSE;
1046 p->search_dirs_flag = TRUE;
1048 case lang_input_file_is_search_file_enum:
1049 p->sysrooted = ldlang_sysrooted_script;
1051 p->maybe_archive = FALSE;
1053 p->local_sym_name = name;
1054 p->just_syms_flag = FALSE;
1055 p->search_dirs_flag = TRUE;
1057 case lang_input_file_is_file_enum:
1059 p->maybe_archive = FALSE;
1061 p->local_sym_name = name;
1062 p->just_syms_flag = FALSE;
1063 p->search_dirs_flag = FALSE;
1069 p->next_real_file = NULL;
1071 p->dynamic = config.dynamic_link;
1072 p->add_DT_NEEDED_for_dynamic = add_DT_NEEDED_for_dynamic;
1073 p->add_DT_NEEDED_for_regular = add_DT_NEEDED_for_regular;
1074 p->whole_archive = whole_archive;
1076 p->missing_file = FALSE;
1077 #ifdef ENABLE_PLUGINS
1079 p->claim_archive = FALSE;
1080 #endif /* ENABLE_PLUGINS */
1082 lang_statement_append (&input_file_chain,
1083 (lang_statement_union_type *) p,
1084 &p->next_real_file);
1088 lang_input_statement_type *
1089 lang_add_input_file (const char *name,
1090 lang_input_file_enum_type file_type,
1093 return new_afile (name, file_type, target, TRUE);
1096 struct out_section_hash_entry
1098 struct bfd_hash_entry root;
1099 lang_statement_union_type s;
1102 /* The hash table. */
1104 static struct bfd_hash_table output_section_statement_table;
1106 /* Support routines for the hash table used by lang_output_section_find,
1107 initialize the table, fill in an entry and remove the table. */
1109 static struct bfd_hash_entry *
1110 output_section_statement_newfunc (struct bfd_hash_entry *entry,
1111 struct bfd_hash_table *table,
1114 lang_output_section_statement_type **nextp;
1115 struct out_section_hash_entry *ret;
1119 entry = (struct bfd_hash_entry *) bfd_hash_allocate (table,
1125 entry = bfd_hash_newfunc (entry, table, string);
1129 ret = (struct out_section_hash_entry *) entry;
1130 memset (&ret->s, 0, sizeof (ret->s));
1131 ret->s.header.type = lang_output_section_statement_enum;
1132 ret->s.output_section_statement.subsection_alignment = -1;
1133 ret->s.output_section_statement.section_alignment = -1;
1134 ret->s.output_section_statement.block_value = 1;
1135 lang_list_init (&ret->s.output_section_statement.children);
1136 lang_statement_append (stat_ptr, &ret->s, &ret->s.header.next);
1138 /* For every output section statement added to the list, except the
1139 first one, lang_output_section_statement.tail points to the "next"
1140 field of the last element of the list. */
1141 if (lang_output_section_statement.head != NULL)
1142 ret->s.output_section_statement.prev
1143 = ((lang_output_section_statement_type *)
1144 ((char *) lang_output_section_statement.tail
1145 - offsetof (lang_output_section_statement_type, next)));
1147 /* GCC's strict aliasing rules prevent us from just casting the
1148 address, so we store the pointer in a variable and cast that
1150 nextp = &ret->s.output_section_statement.next;
1151 lang_statement_append (&lang_output_section_statement,
1153 (lang_statement_union_type **) nextp);
1158 output_section_statement_table_init (void)
1160 if (!bfd_hash_table_init_n (&output_section_statement_table,
1161 output_section_statement_newfunc,
1162 sizeof (struct out_section_hash_entry),
1164 einfo (_("%P%F: can not create hash table: %E\n"));
1168 output_section_statement_table_free (void)
1170 bfd_hash_table_free (&output_section_statement_table);
1173 /* Build enough state so that the parser can build its tree. */
1178 obstack_begin (&stat_obstack, 1000);
1180 stat_ptr = &statement_list;
1182 output_section_statement_table_init ();
1184 lang_list_init (stat_ptr);
1186 lang_list_init (&input_file_chain);
1187 lang_list_init (&lang_output_section_statement);
1188 lang_list_init (&file_chain);
1189 first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum,
1191 abs_output_section =
1192 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME, 0, TRUE);
1194 abs_output_section->bfd_section = bfd_abs_section_ptr;
1196 /* The value "3" is ad-hoc, somewhat related to the expected number of
1197 DEFINED expressions in a linker script. For most default linker
1198 scripts, there are none. Why a hash table then? Well, it's somewhat
1199 simpler to re-use working machinery than using a linked list in terms
1200 of code-complexity here in ld, besides the initialization which just
1201 looks like other code here. */
1202 if (!bfd_hash_table_init_n (&lang_definedness_table,
1203 lang_definedness_newfunc,
1204 sizeof (struct lang_definedness_hash_entry),
1206 einfo (_("%P%F: can not create hash table: %E\n"));
1212 output_section_statement_table_free ();
1215 /*----------------------------------------------------------------------
1216 A region is an area of memory declared with the
1217 MEMORY { name:org=exp, len=exp ... }
1220 We maintain a list of all the regions here.
1222 If no regions are specified in the script, then the default is used
1223 which is created when looked up to be the entire data space.
1225 If create is true we are creating a region inside a MEMORY block.
1226 In this case it is probably an error to create a region that has
1227 already been created. If we are not inside a MEMORY block it is
1228 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
1229 and so we issue a warning.
1231 Each region has at least one name. The first name is either
1232 DEFAULT_MEMORY_REGION or the name given in the MEMORY block. You can add
1233 alias names to an existing region within a script with
1234 REGION_ALIAS (alias, region_name). Each name corresponds to at most one
1237 static lang_memory_region_type *lang_memory_region_list;
1238 static lang_memory_region_type **lang_memory_region_list_tail
1239 = &lang_memory_region_list;
1241 lang_memory_region_type *
1242 lang_memory_region_lookup (const char *const name, bfd_boolean create)
1244 lang_memory_region_name *n;
1245 lang_memory_region_type *r;
1246 lang_memory_region_type *new_region;
1248 /* NAME is NULL for LMA memspecs if no region was specified. */
1252 for (r = lang_memory_region_list; r != NULL; r = r->next)
1253 for (n = &r->name_list; n != NULL; n = n->next)
1254 if (strcmp (n->name, name) == 0)
1257 einfo (_("%P:%S: warning: redeclaration of memory region `%s'\n"),
1262 if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
1263 einfo (_("%P:%S: warning: memory region `%s' not declared\n"), name);
1265 new_region = (lang_memory_region_type *)
1266 stat_alloc (sizeof (lang_memory_region_type));
1268 new_region->name_list.name = xstrdup (name);
1269 new_region->name_list.next = NULL;
1270 new_region->next = NULL;
1271 new_region->origin = 0;
1272 new_region->length = ~(bfd_size_type) 0;
1273 new_region->current = 0;
1274 new_region->last_os = NULL;
1275 new_region->flags = 0;
1276 new_region->not_flags = 0;
1277 new_region->had_full_message = FALSE;
1279 *lang_memory_region_list_tail = new_region;
1280 lang_memory_region_list_tail = &new_region->next;
1286 lang_memory_region_alias (const char * alias, const char * region_name)
1288 lang_memory_region_name * n;
1289 lang_memory_region_type * r;
1290 lang_memory_region_type * region;
1292 /* The default region must be unique. This ensures that it is not necessary
1293 to iterate through the name list if someone wants the check if a region is
1294 the default memory region. */
1295 if (strcmp (region_name, DEFAULT_MEMORY_REGION) == 0
1296 || strcmp (alias, DEFAULT_MEMORY_REGION) == 0)
1297 einfo (_("%F%P:%S: error: alias for default memory region\n"));
1299 /* Look for the target region and check if the alias is not already
1302 for (r = lang_memory_region_list; r != NULL; r = r->next)
1303 for (n = &r->name_list; n != NULL; n = n->next)
1305 if (region == NULL && strcmp (n->name, region_name) == 0)
1307 if (strcmp (n->name, alias) == 0)
1308 einfo (_("%F%P:%S: error: redefinition of memory region "
1313 /* Check if the target region exists. */
1315 einfo (_("%F%P:%S: error: memory region `%s' "
1316 "for alias `%s' does not exist\n"),
1320 /* Add alias to region name list. */
1321 n = (lang_memory_region_name *) stat_alloc (sizeof (lang_memory_region_name));
1322 n->name = xstrdup (alias);
1323 n->next = region->name_list.next;
1324 region->name_list.next = n;
1327 static lang_memory_region_type *
1328 lang_memory_default (asection * section)
1330 lang_memory_region_type *p;
1332 flagword sec_flags = section->flags;
1334 /* Override SEC_DATA to mean a writable section. */
1335 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
1336 sec_flags |= SEC_DATA;
1338 for (p = lang_memory_region_list; p != NULL; p = p->next)
1340 if ((p->flags & sec_flags) != 0
1341 && (p->not_flags & sec_flags) == 0)
1346 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
1349 /* Find or create an output_section_statement with the given NAME.
1350 If CONSTRAINT is non-zero match one with that constraint, otherwise
1351 match any non-negative constraint. If CREATE, always make a
1352 new output_section_statement for SPECIAL CONSTRAINT. */
1354 lang_output_section_statement_type *
1355 lang_output_section_statement_lookup (const char *name,
1359 struct out_section_hash_entry *entry;
1361 entry = ((struct out_section_hash_entry *)
1362 bfd_hash_lookup (&output_section_statement_table, name,
1367 einfo (_("%P%F: failed creating section `%s': %E\n"), name);
1371 if (entry->s.output_section_statement.name != NULL)
1373 /* We have a section of this name, but it might not have the correct
1375 struct out_section_hash_entry *last_ent;
1377 name = entry->s.output_section_statement.name;
1378 if (create && constraint == SPECIAL)
1379 /* Not traversing to the end reverses the order of the second
1380 and subsequent SPECIAL sections in the hash table chain,
1381 but that shouldn't matter. */
1386 if (constraint == entry->s.output_section_statement.constraint
1388 && entry->s.output_section_statement.constraint >= 0))
1389 return &entry->s.output_section_statement;
1391 entry = (struct out_section_hash_entry *) entry->root.next;
1393 while (entry != NULL
1394 && name == entry->s.output_section_statement.name);
1400 = ((struct out_section_hash_entry *)
1401 output_section_statement_newfunc (NULL,
1402 &output_section_statement_table,
1406 einfo (_("%P%F: failed creating section `%s': %E\n"), name);
1409 entry->root = last_ent->root;
1410 last_ent->root.next = &entry->root;
1413 entry->s.output_section_statement.name = name;
1414 entry->s.output_section_statement.constraint = constraint;
1415 return &entry->s.output_section_statement;
1418 /* Find the next output_section_statement with the same name as OS.
1419 If CONSTRAINT is non-zero, find one with that constraint otherwise
1420 match any non-negative constraint. */
1422 lang_output_section_statement_type *
1423 next_matching_output_section_statement (lang_output_section_statement_type *os,
1426 /* All output_section_statements are actually part of a
1427 struct out_section_hash_entry. */
1428 struct out_section_hash_entry *entry = (struct out_section_hash_entry *)
1430 - offsetof (struct out_section_hash_entry, s.output_section_statement));
1431 const char *name = os->name;
1433 ASSERT (name == entry->root.string);
1436 entry = (struct out_section_hash_entry *) entry->root.next;
1438 || name != entry->s.output_section_statement.name)
1441 while (constraint != entry->s.output_section_statement.constraint
1443 || entry->s.output_section_statement.constraint < 0));
1445 return &entry->s.output_section_statement;
1448 /* A variant of lang_output_section_find used by place_orphan.
1449 Returns the output statement that should precede a new output
1450 statement for SEC. If an exact match is found on certain flags,
1453 lang_output_section_statement_type *
1454 lang_output_section_find_by_flags (const asection *sec,
1455 lang_output_section_statement_type **exact,
1456 lang_match_sec_type_func match_type)
1458 lang_output_section_statement_type *first, *look, *found;
1461 /* We know the first statement on this list is *ABS*. May as well
1463 first = &lang_output_section_statement.head->output_section_statement;
1464 first = first->next;
1466 /* First try for an exact match. */
1468 for (look = first; look; look = look->next)
1470 flags = look->flags;
1471 if (look->bfd_section != NULL)
1473 flags = look->bfd_section->flags;
1474 if (match_type && !match_type (link_info.output_bfd,
1479 flags ^= sec->flags;
1480 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY
1481 | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1491 if ((sec->flags & SEC_CODE) != 0
1492 && (sec->flags & SEC_ALLOC) != 0)
1494 /* Try for a rw code section. */
1495 for (look = first; look; look = look->next)
1497 flags = look->flags;
1498 if (look->bfd_section != NULL)
1500 flags = look->bfd_section->flags;
1501 if (match_type && !match_type (link_info.output_bfd,
1506 flags ^= sec->flags;
1507 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1508 | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1512 else if ((sec->flags & (SEC_READONLY | SEC_THREAD_LOCAL)) != 0
1513 && (sec->flags & SEC_ALLOC) != 0)
1515 /* .rodata can go after .text, .sdata2 after .rodata. */
1516 for (look = first; look; look = look->next)
1518 flags = look->flags;
1519 if (look->bfd_section != NULL)
1521 flags = look->bfd_section->flags;
1522 if (match_type && !match_type (link_info.output_bfd,
1527 flags ^= sec->flags;
1528 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1529 | SEC_READONLY | SEC_SMALL_DATA))
1530 || (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1532 && !(look->flags & SEC_SMALL_DATA))
1533 || (!(flags & (SEC_THREAD_LOCAL | SEC_ALLOC))
1534 && (look->flags & SEC_THREAD_LOCAL)
1535 && (!(flags & SEC_LOAD)
1536 || (look->flags & SEC_LOAD))))
1540 else if ((sec->flags & SEC_SMALL_DATA) != 0
1541 && (sec->flags & SEC_ALLOC) != 0)
1543 /* .sdata goes after .data, .sbss after .sdata. */
1544 for (look = first; look; look = look->next)
1546 flags = look->flags;
1547 if (look->bfd_section != NULL)
1549 flags = look->bfd_section->flags;
1550 if (match_type && !match_type (link_info.output_bfd,
1555 flags ^= sec->flags;
1556 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1557 | SEC_THREAD_LOCAL))
1558 || ((look->flags & SEC_SMALL_DATA)
1559 && !(sec->flags & SEC_HAS_CONTENTS)))
1563 else if ((sec->flags & SEC_HAS_CONTENTS) != 0
1564 && (sec->flags & SEC_ALLOC) != 0)
1566 /* .data goes after .rodata. */
1567 for (look = first; look; look = look->next)
1569 flags = look->flags;
1570 if (look->bfd_section != NULL)
1572 flags = look->bfd_section->flags;
1573 if (match_type && !match_type (link_info.output_bfd,
1578 flags ^= sec->flags;
1579 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1580 | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1584 else if ((sec->flags & SEC_ALLOC) != 0)
1586 /* .bss goes after any other alloc section. */
1587 for (look = first; look; look = look->next)
1589 flags = look->flags;
1590 if (look->bfd_section != NULL)
1592 flags = look->bfd_section->flags;
1593 if (match_type && !match_type (link_info.output_bfd,
1598 flags ^= sec->flags;
1599 if (!(flags & SEC_ALLOC))
1605 /* non-alloc go last. */
1606 for (look = first; look; look = look->next)
1608 flags = look->flags;
1609 if (look->bfd_section != NULL)
1610 flags = look->bfd_section->flags;
1611 flags ^= sec->flags;
1612 if (!(flags & SEC_DEBUGGING))
1618 if (found || !match_type)
1621 return lang_output_section_find_by_flags (sec, NULL, NULL);
1624 /* Find the last output section before given output statement.
1625 Used by place_orphan. */
1628 output_prev_sec_find (lang_output_section_statement_type *os)
1630 lang_output_section_statement_type *lookup;
1632 for (lookup = os->prev; lookup != NULL; lookup = lookup->prev)
1634 if (lookup->constraint < 0)
1637 if (lookup->bfd_section != NULL && lookup->bfd_section->owner != NULL)
1638 return lookup->bfd_section;
1644 /* Look for a suitable place for a new output section statement. The
1645 idea is to skip over anything that might be inside a SECTIONS {}
1646 statement in a script, before we find another output section
1647 statement. Assignments to "dot" before an output section statement
1648 are assumed to belong to it, except in two cases; The first
1649 assignment to dot, and assignments before non-alloc sections.
1650 Otherwise we might put an orphan before . = . + SIZEOF_HEADERS or
1651 similar assignments that set the initial address, or we might
1652 insert non-alloc note sections among assignments setting end of
1655 static lang_statement_union_type **
1656 insert_os_after (lang_output_section_statement_type *after)
1658 lang_statement_union_type **where;
1659 lang_statement_union_type **assign = NULL;
1660 bfd_boolean ignore_first;
1663 = after == &lang_output_section_statement.head->output_section_statement;
1665 for (where = &after->header.next;
1667 where = &(*where)->header.next)
1669 switch ((*where)->header.type)
1671 case lang_assignment_statement_enum:
1674 lang_assignment_statement_type *ass;
1676 ass = &(*where)->assignment_statement;
1677 if (ass->exp->type.node_class != etree_assert
1678 && ass->exp->assign.dst[0] == '.'
1679 && ass->exp->assign.dst[1] == 0
1683 ignore_first = FALSE;
1685 case lang_wild_statement_enum:
1686 case lang_input_section_enum:
1687 case lang_object_symbols_statement_enum:
1688 case lang_fill_statement_enum:
1689 case lang_data_statement_enum:
1690 case lang_reloc_statement_enum:
1691 case lang_padding_statement_enum:
1692 case lang_constructors_statement_enum:
1695 case lang_output_section_statement_enum:
1698 asection *s = (*where)->output_section_statement.bfd_section;
1701 || s->map_head.s == NULL
1702 || (s->flags & SEC_ALLOC) != 0)
1706 case lang_input_statement_enum:
1707 case lang_address_statement_enum:
1708 case lang_target_statement_enum:
1709 case lang_output_statement_enum:
1710 case lang_group_statement_enum:
1711 case lang_insert_statement_enum:
1720 lang_output_section_statement_type *
1721 lang_insert_orphan (asection *s,
1722 const char *secname,
1724 lang_output_section_statement_type *after,
1725 struct orphan_save *place,
1726 etree_type *address,
1727 lang_statement_list_type *add_child)
1729 lang_statement_list_type add;
1731 lang_output_section_statement_type *os;
1732 lang_output_section_statement_type **os_tail;
1734 /* If we have found an appropriate place for the output section
1735 statements for this orphan, add them to our own private list,
1736 inserting them later into the global statement list. */
1739 lang_list_init (&add);
1740 push_stat_ptr (&add);
1743 if (link_info.relocatable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
1744 address = exp_intop (0);
1746 os_tail = ((lang_output_section_statement_type **)
1747 lang_output_section_statement.tail);
1748 os = lang_enter_output_section_statement (secname, address, normal_section,
1749 NULL, NULL, NULL, constraint);
1752 if (config.build_constructors && *os_tail == os)
1754 /* If the name of the section is representable in C, then create
1755 symbols to mark the start and the end of the section. */
1756 for (ps = secname; *ps != '\0'; ps++)
1757 if (! ISALNUM ((unsigned char) *ps) && *ps != '_')
1762 etree_type *e_align;
1764 symname = (char *) xmalloc (ps - secname + sizeof "__start_" + 1);
1765 symname[0] = bfd_get_symbol_leading_char (link_info.output_bfd);
1766 sprintf (symname + (symname[0] != 0), "__start_%s", secname);
1767 e_align = exp_unop (ALIGN_K,
1768 exp_intop ((bfd_vma) 1 << s->alignment_power));
1769 lang_add_assignment (exp_assign (".", e_align));
1770 lang_add_assignment (exp_provide (symname,
1772 exp_nameop (NAME, ".")),
1777 if (add_child == NULL)
1778 add_child = &os->children;
1779 lang_add_section (add_child, s, os);
1781 if (after && (s->flags & (SEC_LOAD | SEC_ALLOC)) != 0)
1783 const char *region = (after->region
1784 ? after->region->name_list.name
1785 : DEFAULT_MEMORY_REGION);
1786 const char *lma_region = (after->lma_region
1787 ? after->lma_region->name_list.name
1789 lang_leave_output_section_statement (NULL, region, after->phdrs,
1793 lang_leave_output_section_statement (NULL, DEFAULT_MEMORY_REGION, NULL,
1796 if (ps != NULL && *ps == '\0')
1800 symname = (char *) xmalloc (ps - secname + sizeof "__stop_" + 1);
1801 symname[0] = bfd_get_symbol_leading_char (link_info.output_bfd);
1802 sprintf (symname + (symname[0] != 0), "__stop_%s", secname);
1803 lang_add_assignment (exp_provide (symname,
1804 exp_nameop (NAME, "."),
1808 /* Restore the global list pointer. */
1812 if (after != NULL && os->bfd_section != NULL)
1814 asection *snew, *as;
1816 snew = os->bfd_section;
1818 /* Shuffle the bfd section list to make the output file look
1819 neater. This is really only cosmetic. */
1820 if (place->section == NULL
1821 && after != (&lang_output_section_statement.head
1822 ->output_section_statement))
1824 asection *bfd_section = after->bfd_section;
1826 /* If the output statement hasn't been used to place any input
1827 sections (and thus doesn't have an output bfd_section),
1828 look for the closest prior output statement having an
1830 if (bfd_section == NULL)
1831 bfd_section = output_prev_sec_find (after);
1833 if (bfd_section != NULL && bfd_section != snew)
1834 place->section = &bfd_section->next;
1837 if (place->section == NULL)
1838 place->section = &link_info.output_bfd->sections;
1840 as = *place->section;
1844 /* Put the section at the end of the list. */
1846 /* Unlink the section. */
1847 bfd_section_list_remove (link_info.output_bfd, snew);
1849 /* Now tack it back on in the right place. */
1850 bfd_section_list_append (link_info.output_bfd, snew);
1852 else if (as != snew && as->prev != snew)
1854 /* Unlink the section. */
1855 bfd_section_list_remove (link_info.output_bfd, snew);
1857 /* Now tack it back on in the right place. */
1858 bfd_section_list_insert_before (link_info.output_bfd, as, snew);
1861 /* Save the end of this list. Further ophans of this type will
1862 follow the one we've just added. */
1863 place->section = &snew->next;
1865 /* The following is non-cosmetic. We try to put the output
1866 statements in some sort of reasonable order here, because they
1867 determine the final load addresses of the orphan sections.
1868 In addition, placing output statements in the wrong order may
1869 require extra segments. For instance, given a typical
1870 situation of all read-only sections placed in one segment and
1871 following that a segment containing all the read-write
1872 sections, we wouldn't want to place an orphan read/write
1873 section before or amongst the read-only ones. */
1874 if (add.head != NULL)
1876 lang_output_section_statement_type *newly_added_os;
1878 if (place->stmt == NULL)
1880 lang_statement_union_type **where = insert_os_after (after);
1885 place->os_tail = &after->next;
1889 /* Put it after the last orphan statement we added. */
1890 *add.tail = *place->stmt;
1891 *place->stmt = add.head;
1894 /* Fix the global list pointer if we happened to tack our
1895 new list at the tail. */
1896 if (*stat_ptr->tail == add.head)
1897 stat_ptr->tail = add.tail;
1899 /* Save the end of this list. */
1900 place->stmt = add.tail;
1902 /* Do the same for the list of output section statements. */
1903 newly_added_os = *os_tail;
1905 newly_added_os->prev = (lang_output_section_statement_type *)
1906 ((char *) place->os_tail
1907 - offsetof (lang_output_section_statement_type, next));
1908 newly_added_os->next = *place->os_tail;
1909 if (newly_added_os->next != NULL)
1910 newly_added_os->next->prev = newly_added_os;
1911 *place->os_tail = newly_added_os;
1912 place->os_tail = &newly_added_os->next;
1914 /* Fixing the global list pointer here is a little different.
1915 We added to the list in lang_enter_output_section_statement,
1916 trimmed off the new output_section_statment above when
1917 assigning *os_tail = NULL, but possibly added it back in
1918 the same place when assigning *place->os_tail. */
1919 if (*os_tail == NULL)
1920 lang_output_section_statement.tail
1921 = (lang_statement_union_type **) os_tail;
1928 lang_map_flags (flagword flag)
1930 if (flag & SEC_ALLOC)
1933 if (flag & SEC_CODE)
1936 if (flag & SEC_READONLY)
1939 if (flag & SEC_DATA)
1942 if (flag & SEC_LOAD)
1949 lang_memory_region_type *m;
1950 bfd_boolean dis_header_printed = FALSE;
1953 LANG_FOR_EACH_INPUT_STATEMENT (file)
1957 if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0
1958 || file->just_syms_flag)
1961 for (s = file->the_bfd->sections; s != NULL; s = s->next)
1962 if ((s->output_section == NULL
1963 || s->output_section->owner != link_info.output_bfd)
1964 && (s->flags & (SEC_LINKER_CREATED | SEC_KEEP)) == 0)
1966 if (! dis_header_printed)
1968 fprintf (config.map_file, _("\nDiscarded input sections\n\n"));
1969 dis_header_printed = TRUE;
1972 print_input_section (s, TRUE);
1976 minfo (_("\nMemory Configuration\n\n"));
1977 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
1978 _("Name"), _("Origin"), _("Length"), _("Attributes"));
1980 for (m = lang_memory_region_list; m != NULL; m = m->next)
1985 fprintf (config.map_file, "%-16s ", m->name_list.name);
1987 sprintf_vma (buf, m->origin);
1988 minfo ("0x%s ", buf);
1996 minfo ("0x%V", m->length);
1997 if (m->flags || m->not_flags)
2005 lang_map_flags (m->flags);
2011 lang_map_flags (m->not_flags);
2018 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
2020 if (! link_info.reduce_memory_overheads)
2022 obstack_begin (&map_obstack, 1000);
2023 for (p = link_info.input_bfds; p != (bfd *) NULL; p = p->link_next)
2024 bfd_map_over_sections (p, init_map_userdata, 0);
2025 bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0);
2027 lang_statement_iteration ++;
2028 print_statements ();
2032 init_map_userdata (bfd *abfd ATTRIBUTE_UNUSED,
2034 void *data ATTRIBUTE_UNUSED)
2036 fat_section_userdata_type *new_data
2037 = ((fat_section_userdata_type *) (stat_alloc
2038 (sizeof (fat_section_userdata_type))));
2040 ASSERT (get_userdata (sec) == NULL);
2041 get_userdata (sec) = new_data;
2042 new_data->map_symbol_def_tail = &new_data->map_symbol_def_head;
2043 new_data->map_symbol_def_count = 0;
2047 sort_def_symbol (struct bfd_link_hash_entry *hash_entry,
2048 void *info ATTRIBUTE_UNUSED)
2050 if (hash_entry->type == bfd_link_hash_warning)
2051 hash_entry = (struct bfd_link_hash_entry *) hash_entry->u.i.link;
2053 if (hash_entry->type == bfd_link_hash_defined
2054 || hash_entry->type == bfd_link_hash_defweak)
2056 struct fat_user_section_struct *ud;
2057 struct map_symbol_def *def;
2059 ud = (struct fat_user_section_struct *)
2060 get_userdata (hash_entry->u.def.section);
2063 /* ??? What do we have to do to initialize this beforehand? */
2064 /* The first time we get here is bfd_abs_section... */
2065 init_map_userdata (0, hash_entry->u.def.section, 0);
2066 ud = (struct fat_user_section_struct *)
2067 get_userdata (hash_entry->u.def.section);
2069 else if (!ud->map_symbol_def_tail)
2070 ud->map_symbol_def_tail = &ud->map_symbol_def_head;
2072 def = (struct map_symbol_def *) obstack_alloc (&map_obstack, sizeof *def);
2073 def->entry = hash_entry;
2074 *(ud->map_symbol_def_tail) = def;
2075 ud->map_symbol_def_tail = &def->next;
2076 ud->map_symbol_def_count++;
2081 /* Initialize an output section. */
2084 init_os (lang_output_section_statement_type *s, flagword flags)
2086 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
2087 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
2089 if (s->constraint != SPECIAL)
2090 s->bfd_section = bfd_get_section_by_name (link_info.output_bfd, s->name);
2091 if (s->bfd_section == NULL)
2092 s->bfd_section = bfd_make_section_anyway_with_flags (link_info.output_bfd,
2094 if (s->bfd_section == NULL)
2096 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
2097 link_info.output_bfd->xvec->name, s->name);
2099 s->bfd_section->output_section = s->bfd_section;
2100 s->bfd_section->output_offset = 0;
2102 if (!link_info.reduce_memory_overheads)
2104 fat_section_userdata_type *new_userdata = (fat_section_userdata_type *)
2105 stat_alloc (sizeof (fat_section_userdata_type));
2106 memset (new_userdata, 0, sizeof (fat_section_userdata_type));
2107 get_userdata (s->bfd_section) = new_userdata;
2110 /* If there is a base address, make sure that any sections it might
2111 mention are initialized. */
2112 if (s->addr_tree != NULL)
2113 exp_init_os (s->addr_tree);
2115 if (s->load_base != NULL)
2116 exp_init_os (s->load_base);
2118 /* If supplied an alignment, set it. */
2119 if (s->section_alignment != -1)
2120 s->bfd_section->alignment_power = s->section_alignment;
2123 /* Make sure that all output sections mentioned in an expression are
2127 exp_init_os (etree_type *exp)
2129 switch (exp->type.node_class)
2133 exp_init_os (exp->assign.src);
2137 exp_init_os (exp->binary.lhs);
2138 exp_init_os (exp->binary.rhs);
2142 exp_init_os (exp->trinary.cond);
2143 exp_init_os (exp->trinary.lhs);
2144 exp_init_os (exp->trinary.rhs);
2148 exp_init_os (exp->assert_s.child);
2152 exp_init_os (exp->unary.child);
2156 switch (exp->type.node_code)
2162 lang_output_section_statement_type *os;
2164 os = lang_output_section_find (exp->name.name);
2165 if (os != NULL && os->bfd_section == NULL)
2177 section_already_linked (bfd *abfd, asection *sec, void *data)
2179 lang_input_statement_type *entry = (lang_input_statement_type *) data;
2181 /* If we are only reading symbols from this object, then we want to
2182 discard all sections. */
2183 if (entry->just_syms_flag)
2185 bfd_link_just_syms (abfd, sec, &link_info);
2189 if (!(abfd->flags & DYNAMIC))
2190 bfd_section_already_linked (abfd, sec, &link_info);
2193 /* The wild routines.
2195 These expand statements like *(.text) and foo.o to a list of
2196 explicit actions, like foo.o(.text), bar.o(.text) and
2197 foo.o(.text, .data). */
2199 /* Add SECTION to the output section OUTPUT. Do this by creating a
2200 lang_input_section statement which is placed at PTR. FILE is the
2201 input file which holds SECTION. */
2204 lang_add_section (lang_statement_list_type *ptr,
2206 lang_output_section_statement_type *output)
2208 flagword flags = section->flags;
2209 bfd_boolean discard;
2210 lang_input_section_type *new_section;
2212 /* Discard sections marked with SEC_EXCLUDE. */
2213 discard = (flags & SEC_EXCLUDE) != 0;
2215 /* Discard input sections which are assigned to a section named
2216 DISCARD_SECTION_NAME. */
2217 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
2220 /* Discard debugging sections if we are stripping debugging
2222 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
2223 && (flags & SEC_DEBUGGING) != 0)
2228 if (section->output_section == NULL)
2230 /* This prevents future calls from assigning this section. */
2231 section->output_section = bfd_abs_section_ptr;
2236 if (section->output_section != NULL)
2239 /* We don't copy the SEC_NEVER_LOAD flag from an input section
2240 to an output section, because we want to be able to include a
2241 SEC_NEVER_LOAD section in the middle of an otherwise loaded
2242 section (I don't know why we want to do this, but we do).
2243 build_link_order in ldwrite.c handles this case by turning
2244 the embedded SEC_NEVER_LOAD section into a fill. */
2245 flags &= ~ SEC_NEVER_LOAD;
2247 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
2248 already been processed. One reason to do this is that on pe
2249 format targets, .text$foo sections go into .text and it's odd
2250 to see .text with SEC_LINK_ONCE set. */
2252 if (!link_info.relocatable)
2253 flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC);
2255 switch (output->sectype)
2257 case normal_section:
2258 case overlay_section:
2260 case noalloc_section:
2261 flags &= ~SEC_ALLOC;
2263 case noload_section:
2265 flags |= SEC_NEVER_LOAD;
2266 /* Unfortunately GNU ld has managed to evolve two different
2267 meanings to NOLOAD in scripts. ELF gets a .bss style noload,
2268 alloc, no contents section. All others get a noload, noalloc
2270 if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
2271 flags &= ~SEC_HAS_CONTENTS;
2273 flags &= ~SEC_ALLOC;
2277 if (output->bfd_section == NULL)
2278 init_os (output, flags);
2280 /* If SEC_READONLY is not set in the input section, then clear
2281 it from the output section. */
2282 output->bfd_section->flags &= flags | ~SEC_READONLY;
2284 if (output->bfd_section->linker_has_input)
2286 /* Only set SEC_READONLY flag on the first input section. */
2287 flags &= ~ SEC_READONLY;
2289 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
2290 if ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS))
2291 != (flags & (SEC_MERGE | SEC_STRINGS))
2292 || ((flags & SEC_MERGE) != 0
2293 && output->bfd_section->entsize != section->entsize))
2295 output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
2296 flags &= ~ (SEC_MERGE | SEC_STRINGS);
2299 output->bfd_section->flags |= flags;
2301 if (!output->bfd_section->linker_has_input)
2303 output->bfd_section->linker_has_input = 1;
2304 /* This must happen after flags have been updated. The output
2305 section may have been created before we saw its first input
2306 section, eg. for a data statement. */
2307 bfd_init_private_section_data (section->owner, section,
2308 link_info.output_bfd,
2309 output->bfd_section,
2311 if ((flags & SEC_MERGE) != 0)
2312 output->bfd_section->entsize = section->entsize;
2315 if ((flags & SEC_TIC54X_BLOCK) != 0
2316 && bfd_get_arch (section->owner) == bfd_arch_tic54x)
2318 /* FIXME: This value should really be obtained from the bfd... */
2319 output->block_value = 128;
2322 if (section->alignment_power > output->bfd_section->alignment_power)
2323 output->bfd_section->alignment_power = section->alignment_power;
2325 section->output_section = output->bfd_section;
2327 if (!link_info.relocatable
2328 && !stripped_excluded_sections)
2330 asection *s = output->bfd_section->map_tail.s;
2331 output->bfd_section->map_tail.s = section;
2332 section->map_head.s = NULL;
2333 section->map_tail.s = s;
2335 s->map_head.s = section;
2337 output->bfd_section->map_head.s = section;
2340 /* Add a section reference to the list. */
2341 new_section = new_stat (lang_input_section, ptr);
2342 new_section->section = section;
2345 /* Handle wildcard sorting. This returns the lang_input_section which
2346 should follow the one we are going to create for SECTION and FILE,
2347 based on the sorting requirements of WILD. It returns NULL if the
2348 new section should just go at the end of the current list. */
2350 static lang_statement_union_type *
2351 wild_sort (lang_wild_statement_type *wild,
2352 struct wildcard_list *sec,
2353 lang_input_statement_type *file,
2356 lang_statement_union_type *l;
2358 if (!wild->filenames_sorted
2359 && (sec == NULL || sec->spec.sorted == none))
2362 for (l = wild->children.head; l != NULL; l = l->header.next)
2364 lang_input_section_type *ls;
2366 if (l->header.type != lang_input_section_enum)
2368 ls = &l->input_section;
2370 /* Sorting by filename takes precedence over sorting by section
2373 if (wild->filenames_sorted)
2375 const char *fn, *ln;
2379 /* The PE support for the .idata section as generated by
2380 dlltool assumes that files will be sorted by the name of
2381 the archive and then the name of the file within the
2384 if (file->the_bfd != NULL
2385 && bfd_my_archive (file->the_bfd) != NULL)
2387 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
2392 fn = file->filename;
2396 if (bfd_my_archive (ls->section->owner) != NULL)
2398 ln = bfd_get_filename (bfd_my_archive (ls->section->owner));
2403 ln = ls->section->owner->filename;
2407 i = strcmp (fn, ln);
2416 fn = file->filename;
2418 ln = ls->section->owner->filename;
2420 i = strcmp (fn, ln);
2428 /* Here either the files are not sorted by name, or we are
2429 looking at the sections for this file. */
2431 if (sec != NULL && sec->spec.sorted != none)
2432 if (compare_section (sec->spec.sorted, section, ls->section) < 0)
2439 /* Expand a wild statement for a particular FILE. SECTION may be
2440 NULL, in which case it is a wild card. */
2443 output_section_callback (lang_wild_statement_type *ptr,
2444 struct wildcard_list *sec,
2446 lang_input_statement_type *file,
2449 lang_statement_union_type *before;
2450 lang_output_section_statement_type *os;
2452 os = (lang_output_section_statement_type *) output;
2454 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2455 if (unique_section_p (section, os))
2458 before = wild_sort (ptr, sec, file, section);
2460 /* Here BEFORE points to the lang_input_section which
2461 should follow the one we are about to add. If BEFORE
2462 is NULL, then the section should just go at the end
2463 of the current list. */
2466 lang_add_section (&ptr->children, section, os);
2469 lang_statement_list_type list;
2470 lang_statement_union_type **pp;
2472 lang_list_init (&list);
2473 lang_add_section (&list, section, os);
2475 /* If we are discarding the section, LIST.HEAD will
2477 if (list.head != NULL)
2479 ASSERT (list.head->header.next == NULL);
2481 for (pp = &ptr->children.head;
2483 pp = &(*pp)->header.next)
2484 ASSERT (*pp != NULL);
2486 list.head->header.next = *pp;
2492 /* Check if all sections in a wild statement for a particular FILE
2496 check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
2497 struct wildcard_list *sec ATTRIBUTE_UNUSED,
2499 lang_input_statement_type *file ATTRIBUTE_UNUSED,
2502 lang_output_section_statement_type *os;
2504 os = (lang_output_section_statement_type *) output;
2506 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2507 if (unique_section_p (section, os))
2510 if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0)
2511 os->all_input_readonly = FALSE;
2514 /* This is passed a file name which must have been seen already and
2515 added to the statement tree. We will see if it has been opened
2516 already and had its symbols read. If not then we'll read it. */
2518 static lang_input_statement_type *
2519 lookup_name (const char *name)
2521 lang_input_statement_type *search;
2523 for (search = (lang_input_statement_type *) input_file_chain.head;
2525 search = (lang_input_statement_type *) search->next_real_file)
2527 /* Use the local_sym_name as the name of the file that has
2528 already been loaded as filename might have been transformed
2529 via the search directory lookup mechanism. */
2530 const char *filename = search->local_sym_name;
2532 if (filename != NULL
2533 && strcmp (filename, name) == 0)
2538 search = new_afile (name, lang_input_file_is_search_file_enum,
2539 default_target, FALSE);
2541 /* If we have already added this file, or this file is not real
2542 don't add this file. */
2543 if (search->loaded || !search->real)
2546 if (! load_symbols (search, NULL))
2552 /* Save LIST as a list of libraries whose symbols should not be exported. */
2557 struct excluded_lib *next;
2559 static struct excluded_lib *excluded_libs;
2562 add_excluded_libs (const char *list)
2564 const char *p = list, *end;
2568 struct excluded_lib *entry;
2569 end = strpbrk (p, ",:");
2571 end = p + strlen (p);
2572 entry = (struct excluded_lib *) xmalloc (sizeof (*entry));
2573 entry->next = excluded_libs;
2574 entry->name = (char *) xmalloc (end - p + 1);
2575 memcpy (entry->name, p, end - p);
2576 entry->name[end - p] = '\0';
2577 excluded_libs = entry;
2585 check_excluded_libs (bfd *abfd)
2587 struct excluded_lib *lib = excluded_libs;
2591 int len = strlen (lib->name);
2592 const char *filename = lbasename (abfd->filename);
2594 if (strcmp (lib->name, "ALL") == 0)
2596 abfd->no_export = TRUE;
2600 if (strncmp (lib->name, filename, len) == 0
2601 && (filename[len] == '\0'
2602 || (filename[len] == '.' && filename[len + 1] == 'a'
2603 && filename[len + 2] == '\0')))
2605 abfd->no_export = TRUE;
2613 /* Get the symbols for an input file. */
2616 load_symbols (lang_input_statement_type *entry,
2617 lang_statement_list_type *place)
2624 ldfile_open_file (entry);
2626 /* Do not process further if the file was missing. */
2627 if (entry->missing_file)
2630 if (! bfd_check_format (entry->the_bfd, bfd_archive)
2631 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
2634 bfd_boolean save_ldlang_sysrooted_script;
2635 bfd_boolean save_add_DT_NEEDED_for_regular;
2636 bfd_boolean save_add_DT_NEEDED_for_dynamic;
2637 bfd_boolean save_whole_archive;
2639 err = bfd_get_error ();
2641 /* See if the emulation has some special knowledge. */
2642 if (ldemul_unrecognized_file (entry))
2645 if (err == bfd_error_file_ambiguously_recognized)
2649 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
2650 einfo (_("%B: matching formats:"), entry->the_bfd);
2651 for (p = matching; *p != NULL; p++)
2655 else if (err != bfd_error_file_not_recognized
2657 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
2659 bfd_close (entry->the_bfd);
2660 entry->the_bfd = NULL;
2662 /* Try to interpret the file as a linker script. */
2663 ldfile_open_command_file (entry->filename);
2665 push_stat_ptr (place);
2666 save_ldlang_sysrooted_script = ldlang_sysrooted_script;
2667 ldlang_sysrooted_script = entry->sysrooted;
2668 save_add_DT_NEEDED_for_regular = add_DT_NEEDED_for_regular;
2669 add_DT_NEEDED_for_regular = entry->add_DT_NEEDED_for_regular;
2670 save_add_DT_NEEDED_for_dynamic = add_DT_NEEDED_for_dynamic;
2671 add_DT_NEEDED_for_dynamic = entry->add_DT_NEEDED_for_dynamic;
2672 save_whole_archive = whole_archive;
2673 whole_archive = entry->whole_archive;
2675 ldfile_assumed_script = TRUE;
2676 parser_input = input_script;
2677 /* We want to use the same -Bdynamic/-Bstatic as the one for
2679 config.dynamic_link = entry->dynamic;
2681 ldfile_assumed_script = FALSE;
2683 ldlang_sysrooted_script = save_ldlang_sysrooted_script;
2684 add_DT_NEEDED_for_regular = save_add_DT_NEEDED_for_regular;
2685 add_DT_NEEDED_for_dynamic = save_add_DT_NEEDED_for_dynamic;
2686 whole_archive = save_whole_archive;
2692 if (ldemul_recognized_file (entry))
2695 /* We don't call ldlang_add_file for an archive. Instead, the
2696 add_symbols entry point will call ldlang_add_file, via the
2697 add_archive_element callback, for each element of the archive
2699 switch (bfd_get_format (entry->the_bfd))
2705 ldlang_add_file (entry);
2706 if (trace_files || trace_file_tries)
2707 info_msg ("%I\n", entry);
2711 check_excluded_libs (entry->the_bfd);
2713 if (entry->whole_archive)
2716 bfd_boolean loaded = TRUE;
2721 member = bfd_openr_next_archived_file (entry->the_bfd, member);
2726 if (! bfd_check_format (member, bfd_object))
2728 einfo (_("%F%B: member %B in archive is not an object\n"),
2729 entry->the_bfd, member);
2734 if (!(*link_info.callbacks
2735 ->add_archive_element) (&link_info, member,
2736 "--whole-archive", &subsbfd))
2739 /* Potentially, the add_archive_element hook may have set a
2740 substitute BFD for us. */
2741 if (!bfd_link_add_symbols (subsbfd, &link_info))
2743 einfo (_("%F%B: could not read symbols: %E\n"), member);
2748 entry->loaded = loaded;
2754 if (bfd_link_add_symbols (entry->the_bfd, &link_info))
2755 entry->loaded = TRUE;
2757 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
2759 return entry->loaded;
2762 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
2763 may be NULL, indicating that it is a wildcard. Separate
2764 lang_input_section statements are created for each part of the
2765 expansion; they are added after the wild statement S. OUTPUT is
2766 the output section. */
2769 wild (lang_wild_statement_type *s,
2770 const char *target ATTRIBUTE_UNUSED,
2771 lang_output_section_statement_type *output)
2773 struct wildcard_list *sec;
2775 if (s->handler_data[0]
2776 && s->handler_data[0]->spec.sorted == by_name
2777 && !s->filenames_sorted)
2779 lang_section_bst_type *tree;
2781 walk_wild (s, output_section_callback_fast, output);
2786 output_section_callback_tree_to_list (s, tree, output);
2791 walk_wild (s, output_section_callback, output);
2793 if (default_common_section == NULL)
2794 for (sec = s->section_list; sec != NULL; sec = sec->next)
2795 if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
2797 /* Remember the section that common is going to in case we
2798 later get something which doesn't know where to put it. */
2799 default_common_section = output;
2804 /* Return TRUE iff target is the sought target. */
2807 get_target (const bfd_target *target, void *data)
2809 const char *sought = (const char *) data;
2811 return strcmp (target->name, sought) == 0;
2814 /* Like strcpy() but convert to lower case as well. */
2817 stricpy (char *dest, char *src)
2821 while ((c = *src++) != 0)
2822 *dest++ = TOLOWER (c);
2827 /* Remove the first occurrence of needle (if any) in haystack
2831 strcut (char *haystack, char *needle)
2833 haystack = strstr (haystack, needle);
2839 for (src = haystack + strlen (needle); *src;)
2840 *haystack++ = *src++;
2846 /* Compare two target format name strings.
2847 Return a value indicating how "similar" they are. */
2850 name_compare (char *first, char *second)
2856 copy1 = (char *) xmalloc (strlen (first) + 1);
2857 copy2 = (char *) xmalloc (strlen (second) + 1);
2859 /* Convert the names to lower case. */
2860 stricpy (copy1, first);
2861 stricpy (copy2, second);
2863 /* Remove size and endian strings from the name. */
2864 strcut (copy1, "big");
2865 strcut (copy1, "little");
2866 strcut (copy2, "big");
2867 strcut (copy2, "little");
2869 /* Return a value based on how many characters match,
2870 starting from the beginning. If both strings are
2871 the same then return 10 * their length. */
2872 for (result = 0; copy1[result] == copy2[result]; result++)
2873 if (copy1[result] == 0)
2885 /* Set by closest_target_match() below. */
2886 static const bfd_target *winner;
2888 /* Scan all the valid bfd targets looking for one that has the endianness
2889 requirement that was specified on the command line, and is the nearest
2890 match to the original output target. */
2893 closest_target_match (const bfd_target *target, void *data)
2895 const bfd_target *original = (const bfd_target *) data;
2897 if (command_line.endian == ENDIAN_BIG
2898 && target->byteorder != BFD_ENDIAN_BIG)
2901 if (command_line.endian == ENDIAN_LITTLE
2902 && target->byteorder != BFD_ENDIAN_LITTLE)
2905 /* Must be the same flavour. */
2906 if (target->flavour != original->flavour)
2909 /* Ignore generic big and little endian elf vectors. */
2910 if (strcmp (target->name, "elf32-big") == 0
2911 || strcmp (target->name, "elf64-big") == 0
2912 || strcmp (target->name, "elf32-little") == 0
2913 || strcmp (target->name, "elf64-little") == 0)
2916 /* If we have not found a potential winner yet, then record this one. */
2923 /* Oh dear, we now have two potential candidates for a successful match.
2924 Compare their names and choose the better one. */
2925 if (name_compare (target->name, original->name)
2926 > name_compare (winner->name, original->name))
2929 /* Keep on searching until wqe have checked them all. */
2933 /* Return the BFD target format of the first input file. */
2936 get_first_input_target (void)
2938 char *target = NULL;
2940 LANG_FOR_EACH_INPUT_STATEMENT (s)
2942 if (s->header.type == lang_input_statement_enum
2945 ldfile_open_file (s);
2947 if (s->the_bfd != NULL
2948 && bfd_check_format (s->the_bfd, bfd_object))
2950 target = bfd_get_target (s->the_bfd);
2962 lang_get_output_target (void)
2966 /* Has the user told us which output format to use? */
2967 if (output_target != NULL)
2968 return output_target;
2970 /* No - has the current target been set to something other than
2972 if (current_target != default_target)
2973 return current_target;
2975 /* No - can we determine the format of the first input file? */
2976 target = get_first_input_target ();
2980 /* Failed - use the default output target. */
2981 return default_target;
2984 /* Open the output file. */
2987 open_output (const char *name)
2989 output_target = lang_get_output_target ();
2991 /* Has the user requested a particular endianness on the command
2993 if (command_line.endian != ENDIAN_UNSET)
2995 const bfd_target *target;
2996 enum bfd_endian desired_endian;
2998 /* Get the chosen target. */
2999 target = bfd_search_for_target (get_target, (void *) output_target);
3001 /* If the target is not supported, we cannot do anything. */
3004 if (command_line.endian == ENDIAN_BIG)
3005 desired_endian = BFD_ENDIAN_BIG;
3007 desired_endian = BFD_ENDIAN_LITTLE;
3009 /* See if the target has the wrong endianness. This should
3010 not happen if the linker script has provided big and
3011 little endian alternatives, but some scrips don't do
3013 if (target->byteorder != desired_endian)
3015 /* If it does, then see if the target provides
3016 an alternative with the correct endianness. */
3017 if (target->alternative_target != NULL
3018 && (target->alternative_target->byteorder == desired_endian))
3019 output_target = target->alternative_target->name;
3022 /* Try to find a target as similar as possible to
3023 the default target, but which has the desired
3024 endian characteristic. */
3025 bfd_search_for_target (closest_target_match,
3028 /* Oh dear - we could not find any targets that
3029 satisfy our requirements. */
3031 einfo (_("%P: warning: could not find any targets"
3032 " that match endianness requirement\n"));
3034 output_target = winner->name;
3040 link_info.output_bfd = bfd_openw (name, output_target);
3042 if (link_info.output_bfd == NULL)
3044 if (bfd_get_error () == bfd_error_invalid_target)
3045 einfo (_("%P%F: target %s not found\n"), output_target);
3047 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
3050 delete_output_file_on_failure = TRUE;
3052 if (! bfd_set_format (link_info.output_bfd, bfd_object))
3053 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
3054 if (! bfd_set_arch_mach (link_info.output_bfd,
3055 ldfile_output_architecture,
3056 ldfile_output_machine))
3057 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
3059 link_info.hash = bfd_link_hash_table_create (link_info.output_bfd);
3060 if (link_info.hash == NULL)
3061 einfo (_("%P%F: can not create hash table: %E\n"));
3063 bfd_set_gp_size (link_info.output_bfd, g_switch_value);
3067 ldlang_open_output (lang_statement_union_type *statement)
3069 switch (statement->header.type)
3071 case lang_output_statement_enum:
3072 ASSERT (link_info.output_bfd == NULL);
3073 open_output (statement->output_statement.name);
3074 ldemul_set_output_arch ();
3075 if (config.magic_demand_paged && !link_info.relocatable)
3076 link_info.output_bfd->flags |= D_PAGED;
3078 link_info.output_bfd->flags &= ~D_PAGED;
3079 if (config.text_read_only)
3080 link_info.output_bfd->flags |= WP_TEXT;
3082 link_info.output_bfd->flags &= ~WP_TEXT;
3083 if (link_info.traditional_format)
3084 link_info.output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
3086 link_info.output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
3089 case lang_target_statement_enum:
3090 current_target = statement->target_statement.target;
3097 /* Convert between addresses in bytes and sizes in octets.
3098 For currently supported targets, octets_per_byte is always a power
3099 of two, so we can use shifts. */
3100 #define TO_ADDR(X) ((X) >> opb_shift)
3101 #define TO_SIZE(X) ((X) << opb_shift)
3103 /* Support the above. */
3104 static unsigned int opb_shift = 0;
3109 unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3110 ldfile_output_machine);
3113 while ((x & 1) == 0)
3121 /* Open all the input files. */
3125 OPEN_BFD_NORMAL = 0,
3129 #ifdef ENABLE_PLUGINS
3130 static lang_input_statement_type *plugin_insert = NULL;
3134 open_input_bfds (lang_statement_union_type *s, enum open_bfd_mode mode)
3136 for (; s != NULL; s = s->header.next)
3138 switch (s->header.type)
3140 case lang_constructors_statement_enum:
3141 open_input_bfds (constructor_list.head, mode);
3143 case lang_output_section_statement_enum:
3144 open_input_bfds (s->output_section_statement.children.head, mode);
3146 case lang_wild_statement_enum:
3147 /* Maybe we should load the file's symbols. */
3148 if ((mode & OPEN_BFD_RESCAN) == 0
3149 && s->wild_statement.filename
3150 && !wildcardp (s->wild_statement.filename)
3151 && !archive_path (s->wild_statement.filename))
3152 lookup_name (s->wild_statement.filename);
3153 open_input_bfds (s->wild_statement.children.head, mode);
3155 case lang_group_statement_enum:
3157 struct bfd_link_hash_entry *undefs;
3159 /* We must continually search the entries in the group
3160 until no new symbols are added to the list of undefined
3165 undefs = link_info.hash->undefs_tail;
3166 open_input_bfds (s->group_statement.children.head,
3167 mode | OPEN_BFD_FORCE);
3169 while (undefs != link_info.hash->undefs_tail);
3172 case lang_target_statement_enum:
3173 current_target = s->target_statement.target;
3175 case lang_input_statement_enum:
3176 if (s->input_statement.real)
3178 lang_statement_union_type **os_tail;
3179 lang_statement_list_type add;
3181 s->input_statement.target = current_target;
3183 /* If we are being called from within a group, and this
3184 is an archive which has already been searched, then
3185 force it to be researched unless the whole archive
3186 has been loaded already. Do the same for a rescan. */
3187 if (mode != OPEN_BFD_NORMAL
3188 #ifdef ENABLE_PLUGINS
3189 && ((mode & OPEN_BFD_RESCAN) == 0
3190 || plugin_insert == NULL)
3192 && !s->input_statement.whole_archive
3193 && s->input_statement.loaded
3194 && bfd_check_format (s->input_statement.the_bfd,
3196 s->input_statement.loaded = FALSE;
3198 os_tail = lang_output_section_statement.tail;
3199 lang_list_init (&add);
3201 if (! load_symbols (&s->input_statement, &add))
3202 config.make_executable = FALSE;
3204 if (add.head != NULL)
3206 /* If this was a script with output sections then
3207 tack any added statements on to the end of the
3208 list. This avoids having to reorder the output
3209 section statement list. Very likely the user
3210 forgot -T, and whatever we do here will not meet
3211 naive user expectations. */
3212 if (os_tail != lang_output_section_statement.tail)
3214 einfo (_("%P: warning: %s contains output sections;"
3215 " did you forget -T?\n"),
3216 s->input_statement.filename);
3217 *stat_ptr->tail = add.head;
3218 stat_ptr->tail = add.tail;
3222 *add.tail = s->header.next;
3223 s->header.next = add.head;
3227 #ifdef ENABLE_PLUGINS
3228 /* If we have found the point at which a plugin added new
3229 files, clear plugin_insert to enable archive rescan. */
3230 if (&s->input_statement == plugin_insert)
3231 plugin_insert = NULL;
3234 case lang_assignment_statement_enum:
3235 if (s->assignment_statement.exp->assign.hidden)
3236 /* This is from a --defsym on the command line. */
3237 exp_fold_tree_no_dot (s->assignment_statement.exp);
3244 /* Exit if any of the files were missing. */
3249 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
3252 lang_track_definedness (const char *name)
3254 if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL)
3255 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
3258 /* New-function for the definedness hash table. */
3260 static struct bfd_hash_entry *
3261 lang_definedness_newfunc (struct bfd_hash_entry *entry,
3262 struct bfd_hash_table *table ATTRIBUTE_UNUSED,
3263 const char *name ATTRIBUTE_UNUSED)
3265 struct lang_definedness_hash_entry *ret
3266 = (struct lang_definedness_hash_entry *) entry;
3269 ret = (struct lang_definedness_hash_entry *)
3270 bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry));
3273 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
3275 ret->iteration = -1;
3279 /* Return the iteration when the definition of NAME was last updated. A
3280 value of -1 means that the symbol is not defined in the linker script
3281 or the command line, but may be defined in the linker symbol table. */
3284 lang_symbol_definition_iteration (const char *name)
3286 struct lang_definedness_hash_entry *defentry
3287 = (struct lang_definedness_hash_entry *)
3288 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
3290 /* We've already created this one on the presence of DEFINED in the
3291 script, so it can't be NULL unless something is borked elsewhere in
3293 if (defentry == NULL)
3296 return defentry->iteration;
3299 /* Update the definedness state of NAME. */
3302 lang_update_definedness (const char *name, struct bfd_link_hash_entry *h)
3304 struct lang_definedness_hash_entry *defentry
3305 = (struct lang_definedness_hash_entry *)
3306 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
3308 /* We don't keep track of symbols not tested with DEFINED. */
3309 if (defentry == NULL)
3312 /* If the symbol was already defined, and not from an earlier statement
3313 iteration, don't update the definedness iteration, because that'd
3314 make the symbol seem defined in the linker script at this point, and
3315 it wasn't; it was defined in some object. If we do anyway, DEFINED
3316 would start to yield false before this point and the construct "sym =
3317 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
3319 if (h->type != bfd_link_hash_undefined
3320 && h->type != bfd_link_hash_common
3321 && h->type != bfd_link_hash_new
3322 && defentry->iteration == -1)
3325 defentry->iteration = lang_statement_iteration;
3328 /* Add the supplied name to the symbol table as an undefined reference.
3329 This is a two step process as the symbol table doesn't even exist at
3330 the time the ld command line is processed. First we put the name
3331 on a list, then, once the output file has been opened, transfer the
3332 name to the symbol table. */
3334 typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
3336 #define ldlang_undef_chain_list_head entry_symbol.next
3339 ldlang_add_undef (const char *const name, bfd_boolean cmdline)
3341 ldlang_undef_chain_list_type *new_undef;
3343 undef_from_cmdline = undef_from_cmdline || cmdline;
3344 new_undef = (ldlang_undef_chain_list_type *) stat_alloc (sizeof (*new_undef));
3345 new_undef->next = ldlang_undef_chain_list_head;
3346 ldlang_undef_chain_list_head = new_undef;
3348 new_undef->name = xstrdup (name);
3350 if (link_info.output_bfd != NULL)
3351 insert_undefined (new_undef->name);
3354 /* Insert NAME as undefined in the symbol table. */
3357 insert_undefined (const char *name)
3359 struct bfd_link_hash_entry *h;
3361 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
3363 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
3364 if (h->type == bfd_link_hash_new)
3366 h->type = bfd_link_hash_undefined;
3367 h->u.undef.abfd = NULL;
3368 bfd_link_add_undef (link_info.hash, h);
3372 /* Run through the list of undefineds created above and place them
3373 into the linker hash table as undefined symbols belonging to the
3377 lang_place_undefineds (void)
3379 ldlang_undef_chain_list_type *ptr;
3381 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
3382 insert_undefined (ptr->name);
3385 /* Check for all readonly or some readwrite sections. */
3388 check_input_sections
3389 (lang_statement_union_type *s,
3390 lang_output_section_statement_type *output_section_statement)
3392 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
3394 switch (s->header.type)
3396 case lang_wild_statement_enum:
3397 walk_wild (&s->wild_statement, check_section_callback,
3398 output_section_statement);
3399 if (! output_section_statement->all_input_readonly)
3402 case lang_constructors_statement_enum:
3403 check_input_sections (constructor_list.head,
3404 output_section_statement);
3405 if (! output_section_statement->all_input_readonly)
3408 case lang_group_statement_enum:
3409 check_input_sections (s->group_statement.children.head,
3410 output_section_statement);
3411 if (! output_section_statement->all_input_readonly)
3420 /* Update wildcard statements if needed. */
3423 update_wild_statements (lang_statement_union_type *s)
3425 struct wildcard_list *sec;
3427 switch (sort_section)
3437 for (; s != NULL; s = s->header.next)
3439 switch (s->header.type)
3444 case lang_wild_statement_enum:
3445 sec = s->wild_statement.section_list;
3446 for (sec = s->wild_statement.section_list; sec != NULL;
3449 switch (sec->spec.sorted)
3452 sec->spec.sorted = sort_section;
3455 if (sort_section == by_alignment)
3456 sec->spec.sorted = by_name_alignment;
3459 if (sort_section == by_name)
3460 sec->spec.sorted = by_alignment_name;
3468 case lang_constructors_statement_enum:
3469 update_wild_statements (constructor_list.head);
3472 case lang_output_section_statement_enum:
3473 update_wild_statements
3474 (s->output_section_statement.children.head);
3477 case lang_group_statement_enum:
3478 update_wild_statements (s->group_statement.children.head);
3486 /* Open input files and attach to output sections. */
3489 map_input_to_output_sections
3490 (lang_statement_union_type *s, const char *target,
3491 lang_output_section_statement_type *os)
3493 for (; s != NULL; s = s->header.next)
3495 lang_output_section_statement_type *tos;
3498 switch (s->header.type)
3500 case lang_wild_statement_enum:
3501 wild (&s->wild_statement, target, os);
3503 case lang_constructors_statement_enum:
3504 map_input_to_output_sections (constructor_list.head,
3508 case lang_output_section_statement_enum:
3509 tos = &s->output_section_statement;
3510 if (tos->constraint != 0)
3512 if (tos->constraint != ONLY_IF_RW
3513 && tos->constraint != ONLY_IF_RO)
3515 tos->all_input_readonly = TRUE;
3516 check_input_sections (tos->children.head, tos);
3517 if (tos->all_input_readonly != (tos->constraint == ONLY_IF_RO))
3519 tos->constraint = -1;
3523 map_input_to_output_sections (tos->children.head,
3527 case lang_output_statement_enum:
3529 case lang_target_statement_enum:
3530 target = s->target_statement.target;
3532 case lang_group_statement_enum:
3533 map_input_to_output_sections (s->group_statement.children.head,
3537 case lang_data_statement_enum:
3538 /* Make sure that any sections mentioned in the expression
3540 exp_init_os (s->data_statement.exp);
3541 /* The output section gets CONTENTS, ALLOC and LOAD, but
3542 these may be overridden by the script. */
3543 flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD;
3544 switch (os->sectype)
3546 case normal_section:
3547 case overlay_section:
3549 case noalloc_section:
3550 flags = SEC_HAS_CONTENTS;
3552 case noload_section:
3553 if (bfd_get_flavour (link_info.output_bfd)
3554 == bfd_target_elf_flavour)
3555 flags = SEC_NEVER_LOAD | SEC_ALLOC;
3557 flags = SEC_NEVER_LOAD | SEC_HAS_CONTENTS;
3560 if (os->bfd_section == NULL)
3561 init_os (os, flags);
3563 os->bfd_section->flags |= flags;
3565 case lang_input_section_enum:
3567 case lang_fill_statement_enum:
3568 case lang_object_symbols_statement_enum:
3569 case lang_reloc_statement_enum:
3570 case lang_padding_statement_enum:
3571 case lang_input_statement_enum:
3572 if (os != NULL && os->bfd_section == NULL)
3575 case lang_assignment_statement_enum:
3576 if (os != NULL && os->bfd_section == NULL)
3579 /* Make sure that any sections mentioned in the assignment
3581 exp_init_os (s->assignment_statement.exp);
3583 case lang_address_statement_enum:
3584 /* Mark the specified section with the supplied address.
3585 If this section was actually a segment marker, then the
3586 directive is ignored if the linker script explicitly
3587 processed the segment marker. Originally, the linker
3588 treated segment directives (like -Ttext on the
3589 command-line) as section directives. We honor the
3590 section directive semantics for backwards compatibilty;
3591 linker scripts that do not specifically check for
3592 SEGMENT_START automatically get the old semantics. */
3593 if (!s->address_statement.segment
3594 || !s->address_statement.segment->used)
3596 const char *name = s->address_statement.section_name;
3598 /* Create the output section statement here so that
3599 orphans with a set address will be placed after other
3600 script sections. If we let the orphan placement code
3601 place them in amongst other sections then the address
3602 will affect following script sections, which is
3603 likely to surprise naive users. */
3604 tos = lang_output_section_statement_lookup (name, 0, TRUE);
3605 tos->addr_tree = s->address_statement.address;
3606 if (tos->bfd_section == NULL)
3610 case lang_insert_statement_enum:
3616 /* An insert statement snips out all the linker statements from the
3617 start of the list and places them after the output section
3618 statement specified by the insert. This operation is complicated
3619 by the fact that we keep a doubly linked list of output section
3620 statements as well as the singly linked list of all statements. */
3623 process_insert_statements (void)
3625 lang_statement_union_type **s;
3626 lang_output_section_statement_type *first_os = NULL;
3627 lang_output_section_statement_type *last_os = NULL;
3628 lang_output_section_statement_type *os;
3630 /* "start of list" is actually the statement immediately after
3631 the special abs_section output statement, so that it isn't
3633 s = &lang_output_section_statement.head;
3634 while (*(s = &(*s)->header.next) != NULL)
3636 if ((*s)->header.type == lang_output_section_statement_enum)
3638 /* Keep pointers to the first and last output section
3639 statement in the sequence we may be about to move. */
3640 os = &(*s)->output_section_statement;
3642 ASSERT (last_os == NULL || last_os->next == os);
3645 /* Set constraint negative so that lang_output_section_find
3646 won't match this output section statement. At this
3647 stage in linking constraint has values in the range
3648 [-1, ONLY_IN_RW]. */
3649 last_os->constraint = -2 - last_os->constraint;
3650 if (first_os == NULL)
3653 else if ((*s)->header.type == lang_insert_statement_enum)
3655 lang_insert_statement_type *i = &(*s)->insert_statement;
3656 lang_output_section_statement_type *where;
3657 lang_statement_union_type **ptr;
3658 lang_statement_union_type *first;
3660 where = lang_output_section_find (i->where);
3661 if (where != NULL && i->is_before)
3664 where = where->prev;
3665 while (where != NULL && where->constraint < 0);
3669 einfo (_("%F%P: %s not found for insert\n"), i->where);
3673 /* Deal with reordering the output section statement list. */
3674 if (last_os != NULL)
3676 asection *first_sec, *last_sec;
3677 struct lang_output_section_statement_struct **next;
3679 /* Snip out the output sections we are moving. */
3680 first_os->prev->next = last_os->next;
3681 if (last_os->next == NULL)
3683 next = &first_os->prev->next;
3684 lang_output_section_statement.tail
3685 = (lang_statement_union_type **) next;
3688 last_os->next->prev = first_os->prev;
3689 /* Add them in at the new position. */
3690 last_os->next = where->next;
3691 if (where->next == NULL)
3693 next = &last_os->next;
3694 lang_output_section_statement.tail
3695 = (lang_statement_union_type **) next;
3698 where->next->prev = last_os;
3699 first_os->prev = where;
3700 where->next = first_os;
3702 /* Move the bfd sections in the same way. */
3705 for (os = first_os; os != NULL; os = os->next)
3707 os->constraint = -2 - os->constraint;
3708 if (os->bfd_section != NULL
3709 && os->bfd_section->owner != NULL)
3711 last_sec = os->bfd_section;
3712 if (first_sec == NULL)
3713 first_sec = last_sec;
3718 if (last_sec != NULL)
3720 asection *sec = where->bfd_section;
3722 sec = output_prev_sec_find (where);
3724 /* The place we want to insert must come after the
3725 sections we are moving. So if we find no
3726 section or if the section is the same as our
3727 last section, then no move is needed. */
3728 if (sec != NULL && sec != last_sec)
3730 /* Trim them off. */
3731 if (first_sec->prev != NULL)
3732 first_sec->prev->next = last_sec->next;
3734 link_info.output_bfd->sections = last_sec->next;
3735 if (last_sec->next != NULL)
3736 last_sec->next->prev = first_sec->prev;
3738 link_info.output_bfd->section_last = first_sec->prev;
3740 last_sec->next = sec->next;
3741 if (sec->next != NULL)
3742 sec->next->prev = last_sec;
3744 link_info.output_bfd->section_last = last_sec;
3745 first_sec->prev = sec;
3746 sec->next = first_sec;
3754 ptr = insert_os_after (where);
3755 /* Snip everything after the abs_section output statement we
3756 know is at the start of the list, up to and including
3757 the insert statement we are currently processing. */
3758 first = lang_output_section_statement.head->header.next;
3759 lang_output_section_statement.head->header.next = (*s)->header.next;
3760 /* Add them back where they belong. */
3763 statement_list.tail = s;
3765 s = &lang_output_section_statement.head;
3769 /* Undo constraint twiddling. */
3770 for (os = first_os; os != NULL; os = os->next)
3772 os->constraint = -2 - os->constraint;
3778 /* An output section might have been removed after its statement was
3779 added. For example, ldemul_before_allocation can remove dynamic
3780 sections if they turn out to be not needed. Clean them up here. */
3783 strip_excluded_output_sections (void)
3785 lang_output_section_statement_type *os;
3787 /* Run lang_size_sections (if not already done). */
3788 if (expld.phase != lang_mark_phase_enum)
3790 expld.phase = lang_mark_phase_enum;
3791 expld.dataseg.phase = exp_dataseg_none;
3792 one_lang_size_sections_pass (NULL, FALSE);
3793 lang_reset_memory_regions ();
3796 for (os = &lang_output_section_statement.head->output_section_statement;
3800 asection *output_section;
3801 bfd_boolean exclude;
3803 if (os->constraint < 0)
3806 output_section = os->bfd_section;
3807 if (output_section == NULL)
3810 exclude = (output_section->rawsize == 0
3811 && (output_section->flags & SEC_KEEP) == 0
3812 && !bfd_section_removed_from_list (link_info.output_bfd,
3815 /* Some sections have not yet been sized, notably .gnu.version,
3816 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
3817 input sections, so don't drop output sections that have such
3818 input sections unless they are also marked SEC_EXCLUDE. */
3819 if (exclude && output_section->map_head.s != NULL)
3823 for (s = output_section->map_head.s; s != NULL; s = s->map_head.s)
3824 if ((s->flags & SEC_LINKER_CREATED) != 0
3825 && (s->flags & SEC_EXCLUDE) == 0)
3832 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
3833 output_section->map_head.link_order = NULL;
3834 output_section->map_tail.link_order = NULL;
3838 /* We don't set bfd_section to NULL since bfd_section of the
3839 removed output section statement may still be used. */
3840 if (!os->section_relative_symbol
3841 && !os->update_dot_tree)
3843 output_section->flags |= SEC_EXCLUDE;
3844 bfd_section_list_remove (link_info.output_bfd, output_section);
3845 link_info.output_bfd->section_count--;
3849 /* Stop future calls to lang_add_section from messing with map_head
3850 and map_tail link_order fields. */
3851 stripped_excluded_sections = TRUE;
3855 print_output_section_statement
3856 (lang_output_section_statement_type *output_section_statement)
3858 asection *section = output_section_statement->bfd_section;
3861 if (output_section_statement != abs_output_section)
3863 minfo ("\n%s", output_section_statement->name);
3865 if (section != NULL)
3867 print_dot = section->vma;
3869 len = strlen (output_section_statement->name);
3870 if (len >= SECTION_NAME_MAP_LENGTH - 1)
3875 while (len < SECTION_NAME_MAP_LENGTH)
3881 minfo ("0x%V %W", section->vma, section->size);
3883 if (section->vma != section->lma)
3884 minfo (_(" load address 0x%V"), section->lma);
3886 if (output_section_statement->update_dot_tree != NULL)
3887 exp_fold_tree (output_section_statement->update_dot_tree,
3888 bfd_abs_section_ptr, &print_dot);
3894 print_statement_list (output_section_statement->children.head,
3895 output_section_statement);
3898 /* Scan for the use of the destination in the right hand side
3899 of an expression. In such cases we will not compute the
3900 correct expression, since the value of DST that is used on
3901 the right hand side will be its final value, not its value
3902 just before this expression is evaluated. */
3905 scan_for_self_assignment (const char * dst, etree_type * rhs)
3907 if (rhs == NULL || dst == NULL)
3910 switch (rhs->type.node_class)
3913 return (scan_for_self_assignment (dst, rhs->binary.lhs)
3914 || scan_for_self_assignment (dst, rhs->binary.rhs));
3917 return (scan_for_self_assignment (dst, rhs->trinary.lhs)
3918 || scan_for_self_assignment (dst, rhs->trinary.rhs));
3921 case etree_provided:
3923 if (strcmp (dst, rhs->assign.dst) == 0)
3925 return scan_for_self_assignment (dst, rhs->assign.src);
3928 return scan_for_self_assignment (dst, rhs->unary.child);
3932 return strcmp (dst, rhs->value.str) == 0;
3937 return strcmp (dst, rhs->name.name) == 0;
3949 print_assignment (lang_assignment_statement_type *assignment,
3950 lang_output_section_statement_type *output_section)
3954 bfd_boolean computation_is_valid = TRUE;
3958 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3961 if (assignment->exp->type.node_class == etree_assert)
3964 tree = assignment->exp->assert_s.child;
3965 computation_is_valid = TRUE;
3969 const char *dst = assignment->exp->assign.dst;
3971 is_dot = (dst[0] == '.' && dst[1] == 0);
3972 tree = assignment->exp->assign.src;
3973 computation_is_valid = is_dot || !scan_for_self_assignment (dst, tree);
3976 osec = output_section->bfd_section;
3978 osec = bfd_abs_section_ptr;
3979 exp_fold_tree (tree, osec, &print_dot);
3980 if (expld.result.valid_p)
3984 if (computation_is_valid)
3986 value = expld.result.value;
3988 if (expld.result.section != NULL)
3989 value += expld.result.section->vma;
3991 minfo ("0x%V", value);
3997 struct bfd_link_hash_entry *h;
3999 h = bfd_link_hash_lookup (link_info.hash, assignment->exp->assign.dst,
4000 FALSE, FALSE, TRUE);
4003 value = h->u.def.value;
4004 value += h->u.def.section->output_section->vma;
4005 value += h->u.def.section->output_offset;
4007 minfo ("[0x%V]", value);
4010 minfo ("[unresolved]");
4022 exp_print_tree (assignment->exp);
4027 print_input_statement (lang_input_statement_type *statm)
4029 if (statm->filename != NULL
4030 && (statm->the_bfd == NULL
4031 || (statm->the_bfd->flags & BFD_LINKER_CREATED) == 0))
4032 fprintf (config.map_file, "LOAD %s\n", statm->filename);
4035 /* Print all symbols defined in a particular section. This is called
4036 via bfd_link_hash_traverse, or by print_all_symbols. */
4039 print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
4041 asection *sec = (asection *) ptr;
4043 if ((hash_entry->type == bfd_link_hash_defined
4044 || hash_entry->type == bfd_link_hash_defweak)
4045 && sec == hash_entry->u.def.section)
4049 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
4052 (hash_entry->u.def.value
4053 + hash_entry->u.def.section->output_offset
4054 + hash_entry->u.def.section->output_section->vma));
4056 minfo (" %T\n", hash_entry->root.string);
4063 hash_entry_addr_cmp (const void *a, const void *b)
4065 const struct bfd_link_hash_entry *l = *(const struct bfd_link_hash_entry **)a;
4066 const struct bfd_link_hash_entry *r = *(const struct bfd_link_hash_entry **)b;
4068 if (l->u.def.value < r->u.def.value)
4070 else if (l->u.def.value > r->u.def.value)
4077 print_all_symbols (asection *sec)
4079 struct fat_user_section_struct *ud =
4080 (struct fat_user_section_struct *) get_userdata (sec);
4081 struct map_symbol_def *def;
4082 struct bfd_link_hash_entry **entries;
4088 *ud->map_symbol_def_tail = 0;
4090 /* Sort the symbols by address. */
4091 entries = (struct bfd_link_hash_entry **)
4092 obstack_alloc (&map_obstack, ud->map_symbol_def_count * sizeof (*entries));
4094 for (i = 0, def = ud->map_symbol_def_head; def; def = def->next, i++)
4095 entries[i] = def->entry;
4097 qsort (entries, ud->map_symbol_def_count, sizeof (*entries),
4098 hash_entry_addr_cmp);
4100 /* Print the symbols. */
4101 for (i = 0; i < ud->map_symbol_def_count; i++)
4102 print_one_symbol (entries[i], sec);
4104 obstack_free (&map_obstack, entries);
4107 /* Print information about an input section to the map file. */
4110 print_input_section (asection *i, bfd_boolean is_discarded)
4112 bfd_size_type size = i->size;
4119 minfo ("%s", i->name);
4121 len = 1 + strlen (i->name);
4122 if (len >= SECTION_NAME_MAP_LENGTH - 1)
4127 while (len < SECTION_NAME_MAP_LENGTH)
4133 if (i->output_section != NULL
4134 && i->output_section->owner == link_info.output_bfd)
4135 addr = i->output_section->vma + i->output_offset;
4143 minfo ("0x%V %W %B\n", addr, TO_ADDR (size), i->owner);
4145 if (size != i->rawsize && i->rawsize != 0)
4147 len = SECTION_NAME_MAP_LENGTH + 3;
4159 minfo (_("%W (size before relaxing)\n"), i->rawsize);
4162 if (i->output_section != NULL
4163 && i->output_section->owner == link_info.output_bfd)
4165 if (link_info.reduce_memory_overheads)
4166 bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
4168 print_all_symbols (i);
4170 /* Update print_dot, but make sure that we do not move it
4171 backwards - this could happen if we have overlays and a
4172 later overlay is shorter than an earier one. */
4173 if (addr + TO_ADDR (size) > print_dot)
4174 print_dot = addr + TO_ADDR (size);
4179 print_fill_statement (lang_fill_statement_type *fill)
4183 fputs (" FILL mask 0x", config.map_file);
4184 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
4185 fprintf (config.map_file, "%02x", *p);
4186 fputs ("\n", config.map_file);
4190 print_data_statement (lang_data_statement_type *data)
4198 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
4201 addr = data->output_offset;
4202 if (data->output_section != NULL)
4203 addr += data->output_section->vma;
4231 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
4233 if (data->exp->type.node_class != etree_value)
4236 exp_print_tree (data->exp);
4241 print_dot = addr + TO_ADDR (size);
4244 /* Print an address statement. These are generated by options like
4248 print_address_statement (lang_address_statement_type *address)
4250 minfo (_("Address of section %s set to "), address->section_name);
4251 exp_print_tree (address->address);
4255 /* Print a reloc statement. */
4258 print_reloc_statement (lang_reloc_statement_type *reloc)
4265 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
4268 addr = reloc->output_offset;
4269 if (reloc->output_section != NULL)
4270 addr += reloc->output_section->vma;
4272 size = bfd_get_reloc_size (reloc->howto);
4274 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
4276 if (reloc->name != NULL)
4277 minfo ("%s+", reloc->name);
4279 minfo ("%s+", reloc->section->name);
4281 exp_print_tree (reloc->addend_exp);
4285 print_dot = addr + TO_ADDR (size);
4289 print_padding_statement (lang_padding_statement_type *s)
4297 len = sizeof " *fill*" - 1;
4298 while (len < SECTION_NAME_MAP_LENGTH)
4304 addr = s->output_offset;
4305 if (s->output_section != NULL)
4306 addr += s->output_section->vma;
4307 minfo ("0x%V %W ", addr, (bfd_vma) s->size);
4309 if (s->fill->size != 0)
4313 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
4314 fprintf (config.map_file, "%02x", *p);
4319 print_dot = addr + TO_ADDR (s->size);
4323 print_wild_statement (lang_wild_statement_type *w,
4324 lang_output_section_statement_type *os)
4326 struct wildcard_list *sec;
4330 if (w->filenames_sorted)
4332 if (w->filename != NULL)
4333 minfo ("%s", w->filename);
4336 if (w->filenames_sorted)
4340 for (sec = w->section_list; sec; sec = sec->next)
4342 if (sec->spec.sorted)
4344 if (sec->spec.exclude_name_list != NULL)
4347 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
4348 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
4349 minfo (" %s", tmp->name);
4352 if (sec->spec.name != NULL)
4353 minfo ("%s", sec->spec.name);
4356 if (sec->spec.sorted)
4365 print_statement_list (w->children.head, os);
4368 /* Print a group statement. */
4371 print_group (lang_group_statement_type *s,
4372 lang_output_section_statement_type *os)
4374 fprintf (config.map_file, "START GROUP\n");
4375 print_statement_list (s->children.head, os);
4376 fprintf (config.map_file, "END GROUP\n");
4379 /* Print the list of statements in S.
4380 This can be called for any statement type. */
4383 print_statement_list (lang_statement_union_type *s,
4384 lang_output_section_statement_type *os)
4388 print_statement (s, os);
4393 /* Print the first statement in statement list S.
4394 This can be called for any statement type. */
4397 print_statement (lang_statement_union_type *s,
4398 lang_output_section_statement_type *os)
4400 switch (s->header.type)
4403 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
4406 case lang_constructors_statement_enum:
4407 if (constructor_list.head != NULL)
4409 if (constructors_sorted)
4410 minfo (" SORT (CONSTRUCTORS)\n");
4412 minfo (" CONSTRUCTORS\n");
4413 print_statement_list (constructor_list.head, os);
4416 case lang_wild_statement_enum:
4417 print_wild_statement (&s->wild_statement, os);
4419 case lang_address_statement_enum:
4420 print_address_statement (&s->address_statement);
4422 case lang_object_symbols_statement_enum:
4423 minfo (" CREATE_OBJECT_SYMBOLS\n");
4425 case lang_fill_statement_enum:
4426 print_fill_statement (&s->fill_statement);
4428 case lang_data_statement_enum:
4429 print_data_statement (&s->data_statement);
4431 case lang_reloc_statement_enum:
4432 print_reloc_statement (&s->reloc_statement);
4434 case lang_input_section_enum:
4435 print_input_section (s->input_section.section, FALSE);
4437 case lang_padding_statement_enum:
4438 print_padding_statement (&s->padding_statement);
4440 case lang_output_section_statement_enum:
4441 print_output_section_statement (&s->output_section_statement);
4443 case lang_assignment_statement_enum:
4444 print_assignment (&s->assignment_statement, os);
4446 case lang_target_statement_enum:
4447 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
4449 case lang_output_statement_enum:
4450 minfo ("OUTPUT(%s", s->output_statement.name);
4451 if (output_target != NULL)
4452 minfo (" %s", output_target);
4455 case lang_input_statement_enum:
4456 print_input_statement (&s->input_statement);
4458 case lang_group_statement_enum:
4459 print_group (&s->group_statement, os);
4461 case lang_insert_statement_enum:
4462 minfo ("INSERT %s %s\n",
4463 s->insert_statement.is_before ? "BEFORE" : "AFTER",
4464 s->insert_statement.where);
4470 print_statements (void)
4472 print_statement_list (statement_list.head, abs_output_section);
4475 /* Print the first N statements in statement list S to STDERR.
4476 If N == 0, nothing is printed.
4477 If N < 0, the entire list is printed.
4478 Intended to be called from GDB. */
4481 dprint_statement (lang_statement_union_type *s, int n)
4483 FILE *map_save = config.map_file;
4485 config.map_file = stderr;
4488 print_statement_list (s, abs_output_section);
4491 while (s && --n >= 0)
4493 print_statement (s, abs_output_section);
4498 config.map_file = map_save;
4502 insert_pad (lang_statement_union_type **ptr,
4504 unsigned int alignment_needed,
4505 asection *output_section,
4508 static fill_type zero_fill = { 1, { 0 } };
4509 lang_statement_union_type *pad = NULL;
4511 if (ptr != &statement_list.head)
4512 pad = ((lang_statement_union_type *)
4513 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
4515 && pad->header.type == lang_padding_statement_enum
4516 && pad->padding_statement.output_section == output_section)
4518 /* Use the existing pad statement. */
4520 else if ((pad = *ptr) != NULL
4521 && pad->header.type == lang_padding_statement_enum
4522 && pad->padding_statement.output_section == output_section)
4524 /* Use the existing pad statement. */
4528 /* Make a new padding statement, linked into existing chain. */
4529 pad = (lang_statement_union_type *)
4530 stat_alloc (sizeof (lang_padding_statement_type));
4531 pad->header.next = *ptr;
4533 pad->header.type = lang_padding_statement_enum;
4534 pad->padding_statement.output_section = output_section;
4537 pad->padding_statement.fill = fill;
4539 pad->padding_statement.output_offset = dot - output_section->vma;
4540 pad->padding_statement.size = alignment_needed;
4541 output_section->size += alignment_needed;
4544 /* Work out how much this section will move the dot point. */
4548 (lang_statement_union_type **this_ptr,
4549 lang_output_section_statement_type *output_section_statement,
4553 lang_input_section_type *is = &((*this_ptr)->input_section);
4554 asection *i = is->section;
4556 if (!((lang_input_statement_type *) i->owner->usrdata)->just_syms_flag
4557 && (i->flags & SEC_EXCLUDE) == 0)
4559 unsigned int alignment_needed;
4562 /* Align this section first to the input sections requirement,
4563 then to the output section's requirement. If this alignment
4564 is greater than any seen before, then record it too. Perform
4565 the alignment by inserting a magic 'padding' statement. */
4567 if (output_section_statement->subsection_alignment != -1)
4568 i->alignment_power = output_section_statement->subsection_alignment;
4570 o = output_section_statement->bfd_section;
4571 if (o->alignment_power < i->alignment_power)
4572 o->alignment_power = i->alignment_power;
4574 alignment_needed = align_power (dot, i->alignment_power) - dot;
4576 if (alignment_needed != 0)
4578 insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
4579 dot += alignment_needed;
4582 /* Remember where in the output section this input section goes. */
4584 i->output_offset = dot - o->vma;
4586 /* Mark how big the output section must be to contain this now. */
4587 dot += TO_ADDR (i->size);
4588 o->size = TO_SIZE (dot - o->vma);
4592 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
4599 sort_sections_by_lma (const void *arg1, const void *arg2)
4601 const asection *sec1 = *(const asection **) arg1;
4602 const asection *sec2 = *(const asection **) arg2;
4604 if (bfd_section_lma (sec1->owner, sec1)
4605 < bfd_section_lma (sec2->owner, sec2))
4607 else if (bfd_section_lma (sec1->owner, sec1)
4608 > bfd_section_lma (sec2->owner, sec2))
4610 else if (sec1->id < sec2->id)
4612 else if (sec1->id > sec2->id)
4618 #define IGNORE_SECTION(s) \
4619 ((s->flags & SEC_ALLOC) == 0 \
4620 || ((s->flags & SEC_THREAD_LOCAL) != 0 \
4621 && (s->flags & SEC_LOAD) == 0))
4623 /* Check to see if any allocated sections overlap with other allocated
4624 sections. This can happen if a linker script specifies the output
4625 section addresses of the two sections. Also check whether any memory
4626 region has overflowed. */
4629 lang_check_section_addresses (void)
4632 asection **sections, **spp;
4639 lang_memory_region_type *m;
4641 if (bfd_count_sections (link_info.output_bfd) <= 1)
4644 amt = bfd_count_sections (link_info.output_bfd) * sizeof (asection *);
4645 sections = (asection **) xmalloc (amt);
4647 /* Scan all sections in the output list. */
4649 for (s = link_info.output_bfd->sections; s != NULL; s = s->next)
4651 /* Only consider loadable sections with real contents. */
4652 if (!(s->flags & SEC_LOAD)
4653 || !(s->flags & SEC_ALLOC)
4657 sections[count] = s;
4664 qsort (sections, (size_t) count, sizeof (asection *),
4665 sort_sections_by_lma);
4670 s_end = s_start + TO_ADDR (s->size) - 1;
4671 for (count--; count; count--)
4673 /* We must check the sections' LMA addresses not their VMA
4674 addresses because overlay sections can have overlapping VMAs
4675 but they must have distinct LMAs. */
4681 s_end = s_start + TO_ADDR (s->size) - 1;
4683 /* Look for an overlap. We have sorted sections by lma, so we
4684 know that s_start >= p_start. Besides the obvious case of
4685 overlap when the current section starts before the previous
4686 one ends, we also must have overlap if the previous section
4687 wraps around the address space. */
4688 if (s_start <= p_end
4690 einfo (_("%X%P: section %s loaded at [%V,%V] overlaps section %s loaded at [%V,%V]\n"),
4691 s->name, s_start, s_end, p->name, p_start, p_end);
4696 /* If any memory region has overflowed, report by how much.
4697 We do not issue this diagnostic for regions that had sections
4698 explicitly placed outside their bounds; os_region_check's
4699 diagnostics are adequate for that case.
4701 FIXME: It is conceivable that m->current - (m->origin + m->length)
4702 might overflow a 32-bit integer. There is, alas, no way to print
4703 a bfd_vma quantity in decimal. */
4704 for (m = lang_memory_region_list; m; m = m->next)
4705 if (m->had_full_message)
4706 einfo (_("%X%P: region `%s' overflowed by %ld bytes\n"),
4707 m->name_list.name, (long)(m->current - (m->origin + m->length)));
4711 /* Make sure the new address is within the region. We explicitly permit the
4712 current address to be at the exact end of the region when the address is
4713 non-zero, in case the region is at the end of addressable memory and the
4714 calculation wraps around. */
4717 os_region_check (lang_output_section_statement_type *os,
4718 lang_memory_region_type *region,
4722 if ((region->current < region->origin
4723 || (region->current - region->origin > region->length))
4724 && ((region->current != region->origin + region->length)
4729 einfo (_("%X%P: address 0x%v of %B section `%s'"
4730 " is not within region `%s'\n"),
4732 os->bfd_section->owner,
4733 os->bfd_section->name,
4734 region->name_list.name);
4736 else if (!region->had_full_message)
4738 region->had_full_message = TRUE;
4740 einfo (_("%X%P: %B section `%s' will not fit in region `%s'\n"),
4741 os->bfd_section->owner,
4742 os->bfd_section->name,
4743 region->name_list.name);
4748 /* Set the sizes for all the output sections. */
4751 lang_size_sections_1
4752 (lang_statement_union_type **prev,
4753 lang_output_section_statement_type *output_section_statement,
4757 bfd_boolean check_regions)
4759 lang_statement_union_type *s;
4761 /* Size up the sections from their constituent parts. */
4762 for (s = *prev; s != NULL; s = s->header.next)
4764 switch (s->header.type)
4766 case lang_output_section_statement_enum:
4768 bfd_vma newdot, after;
4769 lang_output_section_statement_type *os;
4770 lang_memory_region_type *r;
4771 int section_alignment = 0;
4773 os = &s->output_section_statement;
4774 if (os->constraint == -1)
4777 /* FIXME: We shouldn't need to zero section vmas for ld -r
4778 here, in lang_insert_orphan, or in the default linker scripts.
4779 This is covering for coff backend linker bugs. See PR6945. */
4780 if (os->addr_tree == NULL
4781 && link_info.relocatable
4782 && (bfd_get_flavour (link_info.output_bfd)
4783 == bfd_target_coff_flavour))
4784 os->addr_tree = exp_intop (0);
4785 if (os->addr_tree != NULL)
4787 os->processed_vma = FALSE;
4788 exp_fold_tree (os->addr_tree, bfd_abs_section_ptr, &dot);
4790 if (expld.result.valid_p)
4792 dot = expld.result.value;
4793 if (expld.result.section != NULL)
4794 dot += expld.result.section->vma;
4796 else if (expld.phase != lang_mark_phase_enum)
4797 einfo (_("%F%S: non constant or forward reference"
4798 " address expression for section %s\n"),
4802 if (os->bfd_section == NULL)
4803 /* This section was removed or never actually created. */
4806 /* If this is a COFF shared library section, use the size and
4807 address from the input section. FIXME: This is COFF
4808 specific; it would be cleaner if there were some other way
4809 to do this, but nothing simple comes to mind. */
4810 if (((bfd_get_flavour (link_info.output_bfd)
4811 == bfd_target_ecoff_flavour)
4812 || (bfd_get_flavour (link_info.output_bfd)
4813 == bfd_target_coff_flavour))
4814 && (os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
4818 if (os->children.head == NULL
4819 || os->children.head->header.next != NULL
4820 || (os->children.head->header.type
4821 != lang_input_section_enum))
4822 einfo (_("%P%X: Internal error on COFF shared library"
4823 " section %s\n"), os->name);
4825 input = os->children.head->input_section.section;
4826 bfd_set_section_vma (os->bfd_section->owner,
4828 bfd_section_vma (input->owner, input));
4829 os->bfd_section->size = input->size;
4834 if (bfd_is_abs_section (os->bfd_section))
4836 /* No matter what happens, an abs section starts at zero. */
4837 ASSERT (os->bfd_section->vma == 0);
4841 if (os->addr_tree == NULL)
4843 /* No address specified for this section, get one
4844 from the region specification. */
4845 if (os->region == NULL
4846 || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))
4847 && os->region->name_list.name[0] == '*'
4848 && strcmp (os->region->name_list.name,
4849 DEFAULT_MEMORY_REGION) == 0))
4851 os->region = lang_memory_default (os->bfd_section);
4854 /* If a loadable section is using the default memory
4855 region, and some non default memory regions were
4856 defined, issue an error message. */
4858 && !IGNORE_SECTION (os->bfd_section)
4859 && ! link_info.relocatable
4861 && strcmp (os->region->name_list.name,
4862 DEFAULT_MEMORY_REGION) == 0
4863 && lang_memory_region_list != NULL
4864 && (strcmp (lang_memory_region_list->name_list.name,
4865 DEFAULT_MEMORY_REGION) != 0
4866 || lang_memory_region_list->next != NULL)
4867 && expld.phase != lang_mark_phase_enum)
4869 /* By default this is an error rather than just a
4870 warning because if we allocate the section to the
4871 default memory region we can end up creating an
4872 excessively large binary, or even seg faulting when
4873 attempting to perform a negative seek. See
4874 sources.redhat.com/ml/binutils/2003-04/msg00423.html
4875 for an example of this. This behaviour can be
4876 overridden by the using the --no-check-sections
4878 if (command_line.check_section_addresses)
4879 einfo (_("%P%F: error: no memory region specified"
4880 " for loadable section `%s'\n"),
4881 bfd_get_section_name (link_info.output_bfd,
4884 einfo (_("%P: warning: no memory region specified"
4885 " for loadable section `%s'\n"),
4886 bfd_get_section_name (link_info.output_bfd,
4890 newdot = os->region->current;
4891 section_alignment = os->bfd_section->alignment_power;
4894 section_alignment = os->section_alignment;
4896 /* Align to what the section needs. */
4897 if (section_alignment > 0)
4899 bfd_vma savedot = newdot;
4900 newdot = align_power (newdot, section_alignment);
4902 if (newdot != savedot
4903 && (config.warn_section_align
4904 || os->addr_tree != NULL)
4905 && expld.phase != lang_mark_phase_enum)
4906 einfo (_("%P: warning: changing start of section"
4907 " %s by %lu bytes\n"),
4908 os->name, (unsigned long) (newdot - savedot));
4911 bfd_set_section_vma (0, os->bfd_section, newdot);
4913 os->bfd_section->output_offset = 0;
4916 lang_size_sections_1 (&os->children.head, os,
4917 os->fill, newdot, relax, check_regions);
4919 os->processed_vma = TRUE;
4921 if (bfd_is_abs_section (os->bfd_section) || os->ignored)
4922 /* Except for some special linker created sections,
4923 no output section should change from zero size
4924 after strip_excluded_output_sections. A non-zero
4925 size on an ignored section indicates that some
4926 input section was not sized early enough. */
4927 ASSERT (os->bfd_section->size == 0);
4930 dot = os->bfd_section->vma;
4932 /* Put the section within the requested block size, or
4933 align at the block boundary. */
4935 + TO_ADDR (os->bfd_section->size)
4936 + os->block_value - 1)
4937 & - (bfd_vma) os->block_value);
4939 os->bfd_section->size = TO_SIZE (after - os->bfd_section->vma);
4942 /* Set section lma. */
4945 r = lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
4949 bfd_vma lma = exp_get_abs_int (os->load_base, 0, "load base");
4950 os->bfd_section->lma = lma;
4952 else if (os->lma_region != NULL)
4954 bfd_vma lma = os->lma_region->current;
4956 if (section_alignment > 0)
4957 lma = align_power (lma, section_alignment);
4958 os->bfd_section->lma = lma;
4960 else if (r->last_os != NULL
4961 && (os->bfd_section->flags & SEC_ALLOC) != 0)
4966 last = r->last_os->output_section_statement.bfd_section;
4968 /* A backwards move of dot should be accompanied by
4969 an explicit assignment to the section LMA (ie.
4970 os->load_base set) because backwards moves can
4971 create overlapping LMAs. */
4973 && os->bfd_section->size != 0
4974 && dot + os->bfd_section->size <= last->vma)
4976 /* If dot moved backwards then leave lma equal to
4977 vma. This is the old default lma, which might
4978 just happen to work when the backwards move is
4979 sufficiently large. Nag if this changes anything,
4980 so people can fix their linker scripts. */
4982 if (last->vma != last->lma)
4983 einfo (_("%P: warning: dot moved backwards before `%s'\n"),
4988 /* If this is an overlay, set the current lma to that
4989 at the end of the previous section. */
4990 if (os->sectype == overlay_section)
4991 lma = last->lma + last->size;
4993 /* Otherwise, keep the same lma to vma relationship
4994 as the previous section. */
4996 lma = dot + last->lma - last->vma;
4998 if (section_alignment > 0)
4999 lma = align_power (lma, section_alignment);
5000 os->bfd_section->lma = lma;
5003 os->processed_lma = TRUE;
5005 if (bfd_is_abs_section (os->bfd_section) || os->ignored)
5008 /* Keep track of normal sections using the default
5009 lma region. We use this to set the lma for
5010 following sections. Overlays or other linker
5011 script assignment to lma might mean that the
5012 default lma == vma is incorrect.
5013 To avoid warnings about dot moving backwards when using
5014 -Ttext, don't start tracking sections until we find one
5015 of non-zero size or with lma set differently to vma. */
5016 if (((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
5017 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0)
5018 && (os->bfd_section->flags & SEC_ALLOC) != 0
5019 && (os->bfd_section->size != 0
5020 || (r->last_os == NULL
5021 && os->bfd_section->vma != os->bfd_section->lma)
5022 || (r->last_os != NULL
5023 && dot >= (r->last_os->output_section_statement
5024 .bfd_section->vma)))
5025 && os->lma_region == NULL
5026 && !link_info.relocatable)
5029 /* .tbss sections effectively have zero size. */
5030 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
5031 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
5032 || link_info.relocatable)
5033 dot += TO_ADDR (os->bfd_section->size);
5035 if (os->update_dot_tree != 0)
5036 exp_fold_tree (os->update_dot_tree, bfd_abs_section_ptr, &dot);
5038 /* Update dot in the region ?
5039 We only do this if the section is going to be allocated,
5040 since unallocated sections do not contribute to the region's
5041 overall size in memory. */
5042 if (os->region != NULL
5043 && (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD)))
5045 os->region->current = dot;
5048 /* Make sure the new address is within the region. */
5049 os_region_check (os, os->region, os->addr_tree,
5050 os->bfd_section->vma);
5052 if (os->lma_region != NULL && os->lma_region != os->region
5053 && (os->bfd_section->flags & SEC_LOAD))
5055 os->lma_region->current
5056 = os->bfd_section->lma + TO_ADDR (os->bfd_section->size);
5059 os_region_check (os, os->lma_region, NULL,
5060 os->bfd_section->lma);
5066 case lang_constructors_statement_enum:
5067 dot = lang_size_sections_1 (&constructor_list.head,
5068 output_section_statement,
5069 fill, dot, relax, check_regions);
5072 case lang_data_statement_enum:
5074 unsigned int size = 0;
5076 s->data_statement.output_offset =
5077 dot - output_section_statement->bfd_section->vma;
5078 s->data_statement.output_section =
5079 output_section_statement->bfd_section;
5081 /* We might refer to provided symbols in the expression, and
5082 need to mark them as needed. */
5083 exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
5085 switch (s->data_statement.type)
5103 if (size < TO_SIZE ((unsigned) 1))
5104 size = TO_SIZE ((unsigned) 1);
5105 dot += TO_ADDR (size);
5106 output_section_statement->bfd_section->size += size;
5110 case lang_reloc_statement_enum:
5114 s->reloc_statement.output_offset =
5115 dot - output_section_statement->bfd_section->vma;
5116 s->reloc_statement.output_section =
5117 output_section_statement->bfd_section;
5118 size = bfd_get_reloc_size (s->reloc_statement.howto);
5119 dot += TO_ADDR (size);
5120 output_section_statement->bfd_section->size += size;
5124 case lang_wild_statement_enum:
5125 dot = lang_size_sections_1 (&s->wild_statement.children.head,
5126 output_section_statement,
5127 fill, dot, relax, check_regions);
5130 case lang_object_symbols_statement_enum:
5131 link_info.create_object_symbols_section =
5132 output_section_statement->bfd_section;
5135 case lang_output_statement_enum:
5136 case lang_target_statement_enum:
5139 case lang_input_section_enum:
5143 i = s->input_section.section;
5148 if (! bfd_relax_section (i->owner, i, &link_info, &again))
5149 einfo (_("%P%F: can't relax section: %E\n"));
5153 dot = size_input_section (prev, output_section_statement,
5154 output_section_statement->fill, dot);
5158 case lang_input_statement_enum:
5161 case lang_fill_statement_enum:
5162 s->fill_statement.output_section =
5163 output_section_statement->bfd_section;
5165 fill = s->fill_statement.fill;
5168 case lang_assignment_statement_enum:
5170 bfd_vma newdot = dot;
5171 etree_type *tree = s->assignment_statement.exp;
5173 expld.dataseg.relro = exp_dataseg_relro_none;
5175 exp_fold_tree (tree,
5176 output_section_statement->bfd_section,
5179 if (expld.dataseg.relro == exp_dataseg_relro_start)
5181 if (!expld.dataseg.relro_start_stat)
5182 expld.dataseg.relro_start_stat = s;
5185 ASSERT (expld.dataseg.relro_start_stat == s);
5188 else if (expld.dataseg.relro == exp_dataseg_relro_end)
5190 if (!expld.dataseg.relro_end_stat)
5191 expld.dataseg.relro_end_stat = s;
5194 ASSERT (expld.dataseg.relro_end_stat == s);
5197 expld.dataseg.relro = exp_dataseg_relro_none;
5199 /* This symbol is relative to this section. */
5200 if ((tree->type.node_class == etree_provided
5201 || tree->type.node_class == etree_assign)
5202 && (tree->assign.dst [0] != '.'
5203 || tree->assign.dst [1] != '\0'))
5204 output_section_statement->section_relative_symbol = 1;
5206 if (!output_section_statement->ignored)
5208 if (output_section_statement == abs_output_section)
5210 /* If we don't have an output section, then just adjust
5211 the default memory address. */
5212 lang_memory_region_lookup (DEFAULT_MEMORY_REGION,
5213 FALSE)->current = newdot;
5215 else if (newdot != dot)
5217 /* Insert a pad after this statement. We can't
5218 put the pad before when relaxing, in case the
5219 assignment references dot. */
5220 insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
5221 output_section_statement->bfd_section, dot);
5223 /* Don't neuter the pad below when relaxing. */
5226 /* If dot is advanced, this implies that the section
5227 should have space allocated to it, unless the
5228 user has explicitly stated that the section
5229 should not be allocated. */
5230 if (output_section_statement->sectype != noalloc_section
5231 && (output_section_statement->sectype != noload_section
5232 || (bfd_get_flavour (link_info.output_bfd)
5233 == bfd_target_elf_flavour)))
5234 output_section_statement->bfd_section->flags |= SEC_ALLOC;
5241 case lang_padding_statement_enum:
5242 /* If this is the first time lang_size_sections is called,
5243 we won't have any padding statements. If this is the
5244 second or later passes when relaxing, we should allow
5245 padding to shrink. If padding is needed on this pass, it
5246 will be added back in. */
5247 s->padding_statement.size = 0;
5249 /* Make sure output_offset is valid. If relaxation shrinks
5250 the section and this pad isn't needed, it's possible to
5251 have output_offset larger than the final size of the
5252 section. bfd_set_section_contents will complain even for
5253 a pad size of zero. */
5254 s->padding_statement.output_offset
5255 = dot - output_section_statement->bfd_section->vma;
5258 case lang_group_statement_enum:
5259 dot = lang_size_sections_1 (&s->group_statement.children.head,
5260 output_section_statement,
5261 fill, dot, relax, check_regions);
5264 case lang_insert_statement_enum:
5267 /* We can only get here when relaxing is turned on. */
5268 case lang_address_statement_enum:
5275 prev = &s->header.next;
5280 /* Callback routine that is used in _bfd_elf_map_sections_to_segments.
5281 The BFD library has set NEW_SEGMENT to TRUE iff it thinks that
5282 CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different
5283 segments. We are allowed an opportunity to override this decision. */
5286 ldlang_override_segment_assignment (struct bfd_link_info * info ATTRIBUTE_UNUSED,
5287 bfd * abfd ATTRIBUTE_UNUSED,
5288 asection * current_section,
5289 asection * previous_section,
5290 bfd_boolean new_segment)
5292 lang_output_section_statement_type * cur;
5293 lang_output_section_statement_type * prev;
5295 /* The checks below are only necessary when the BFD library has decided
5296 that the two sections ought to be placed into the same segment. */
5300 /* Paranoia checks. */
5301 if (current_section == NULL || previous_section == NULL)
5304 /* Find the memory regions associated with the two sections.
5305 We call lang_output_section_find() here rather than scanning the list
5306 of output sections looking for a matching section pointer because if
5307 we have a large number of sections then a hash lookup is faster. */
5308 cur = lang_output_section_find (current_section->name);
5309 prev = lang_output_section_find (previous_section->name);
5311 /* More paranoia. */
5312 if (cur == NULL || prev == NULL)
5315 /* If the regions are different then force the sections to live in
5316 different segments. See the email thread starting at the following
5317 URL for the reasons why this is necessary:
5318 http://sourceware.org/ml/binutils/2007-02/msg00216.html */
5319 return cur->region != prev->region;
5323 one_lang_size_sections_pass (bfd_boolean *relax, bfd_boolean check_regions)
5325 lang_statement_iteration++;
5326 lang_size_sections_1 (&statement_list.head, abs_output_section,
5327 0, 0, relax, check_regions);
5331 lang_size_sections (bfd_boolean *relax, bfd_boolean check_regions)
5333 expld.phase = lang_allocating_phase_enum;
5334 expld.dataseg.phase = exp_dataseg_none;
5336 one_lang_size_sections_pass (relax, check_regions);
5337 if (expld.dataseg.phase == exp_dataseg_end_seen
5338 && link_info.relro && expld.dataseg.relro_end)
5340 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
5341 to put expld.dataseg.relro on a (common) page boundary. */
5342 bfd_vma min_base, old_base, relro_end, maxpage;
5344 expld.dataseg.phase = exp_dataseg_relro_adjust;
5345 maxpage = expld.dataseg.maxpagesize;
5346 /* MIN_BASE is the absolute minimum address we are allowed to start the
5347 read-write segment (byte before will be mapped read-only). */
5348 min_base = (expld.dataseg.min_base + maxpage - 1) & ~(maxpage - 1);
5349 /* OLD_BASE is the address for a feasible minimum address which will
5350 still not cause a data overlap inside MAXPAGE causing file offset skip
5352 old_base = expld.dataseg.base;
5353 expld.dataseg.base += (-expld.dataseg.relro_end
5354 & (expld.dataseg.pagesize - 1));
5355 /* Compute the expected PT_GNU_RELRO segment end. */
5356 relro_end = ((expld.dataseg.relro_end + expld.dataseg.pagesize - 1)
5357 & ~(expld.dataseg.pagesize - 1));
5358 if (min_base + maxpage < expld.dataseg.base)
5360 expld.dataseg.base -= maxpage;
5361 relro_end -= maxpage;
5363 lang_reset_memory_regions ();
5364 one_lang_size_sections_pass (relax, check_regions);
5365 if (expld.dataseg.relro_end > relro_end)
5367 /* The alignment of sections between DATA_SEGMENT_ALIGN
5368 and DATA_SEGMENT_RELRO_END caused huge padding to be
5369 inserted at DATA_SEGMENT_RELRO_END. Try to start a bit lower so
5370 that the section alignments will fit in. */
5372 unsigned int max_alignment_power = 0;
5374 /* Find maximum alignment power of sections between
5375 DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END. */
5376 for (sec = link_info.output_bfd->sections; sec; sec = sec->next)
5377 if (sec->vma >= expld.dataseg.base
5378 && sec->vma < expld.dataseg.relro_end
5379 && sec->alignment_power > max_alignment_power)
5380 max_alignment_power = sec->alignment_power;
5382 if (((bfd_vma) 1 << max_alignment_power) < expld.dataseg.pagesize)
5384 if (expld.dataseg.base - (1 << max_alignment_power) < old_base)
5385 expld.dataseg.base += expld.dataseg.pagesize;
5386 expld.dataseg.base -= (1 << max_alignment_power);
5387 lang_reset_memory_regions ();
5388 one_lang_size_sections_pass (relax, check_regions);
5391 link_info.relro_start = expld.dataseg.base;
5392 link_info.relro_end = expld.dataseg.relro_end;
5394 else if (expld.dataseg.phase == exp_dataseg_end_seen)
5396 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
5397 a page could be saved in the data segment. */
5398 bfd_vma first, last;
5400 first = -expld.dataseg.base & (expld.dataseg.pagesize - 1);
5401 last = expld.dataseg.end & (expld.dataseg.pagesize - 1);
5403 && ((expld.dataseg.base & ~(expld.dataseg.pagesize - 1))
5404 != (expld.dataseg.end & ~(expld.dataseg.pagesize - 1)))
5405 && first + last <= expld.dataseg.pagesize)
5407 expld.dataseg.phase = exp_dataseg_adjust;
5408 lang_reset_memory_regions ();
5409 one_lang_size_sections_pass (relax, check_regions);
5412 expld.dataseg.phase = exp_dataseg_done;
5415 expld.dataseg.phase = exp_dataseg_done;
5418 /* Worker function for lang_do_assignments. Recursiveness goes here. */
5421 lang_do_assignments_1 (lang_statement_union_type *s,
5422 lang_output_section_statement_type *current_os,
5426 for (; s != NULL; s = s->header.next)
5428 switch (s->header.type)
5430 case lang_constructors_statement_enum:
5431 dot = lang_do_assignments_1 (constructor_list.head,
5432 current_os, fill, dot);
5435 case lang_output_section_statement_enum:
5437 lang_output_section_statement_type *os;
5439 os = &(s->output_section_statement);
5440 if (os->bfd_section != NULL && !os->ignored)
5442 dot = os->bfd_section->vma;
5444 lang_do_assignments_1 (os->children.head, os, os->fill, dot);
5446 /* .tbss sections effectively have zero size. */
5447 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
5448 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
5449 || link_info.relocatable)
5450 dot += TO_ADDR (os->bfd_section->size);
5452 if (os->update_dot_tree != NULL)
5453 exp_fold_tree (os->update_dot_tree, bfd_abs_section_ptr, &dot);
5458 case lang_wild_statement_enum:
5460 dot = lang_do_assignments_1 (s->wild_statement.children.head,
5461 current_os, fill, dot);
5464 case lang_object_symbols_statement_enum:
5465 case lang_output_statement_enum:
5466 case lang_target_statement_enum:
5469 case lang_data_statement_enum:
5470 exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
5471 if (expld.result.valid_p)
5473 s->data_statement.value = expld.result.value;
5474 if (expld.result.section != NULL)
5475 s->data_statement.value += expld.result.section->vma;
5478 einfo (_("%F%P: invalid data statement\n"));
5481 switch (s->data_statement.type)
5499 if (size < TO_SIZE ((unsigned) 1))
5500 size = TO_SIZE ((unsigned) 1);
5501 dot += TO_ADDR (size);
5505 case lang_reloc_statement_enum:
5506 exp_fold_tree (s->reloc_statement.addend_exp,
5507 bfd_abs_section_ptr, &dot);
5508 if (expld.result.valid_p)
5509 s->reloc_statement.addend_value = expld.result.value;
5511 einfo (_("%F%P: invalid reloc statement\n"));
5512 dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
5515 case lang_input_section_enum:
5517 asection *in = s->input_section.section;
5519 if ((in->flags & SEC_EXCLUDE) == 0)
5520 dot += TO_ADDR (in->size);
5524 case lang_input_statement_enum:
5527 case lang_fill_statement_enum:
5528 fill = s->fill_statement.fill;
5531 case lang_assignment_statement_enum:
5532 exp_fold_tree (s->assignment_statement.exp,
5533 current_os->bfd_section,
5537 case lang_padding_statement_enum:
5538 dot += TO_ADDR (s->padding_statement.size);
5541 case lang_group_statement_enum:
5542 dot = lang_do_assignments_1 (s->group_statement.children.head,
5543 current_os, fill, dot);
5546 case lang_insert_statement_enum:
5549 case lang_address_statement_enum:
5561 lang_do_assignments (lang_phase_type phase)
5563 expld.phase = phase;
5564 lang_statement_iteration++;
5565 lang_do_assignments_1 (statement_list.head, abs_output_section, NULL, 0);
5568 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
5569 operator .startof. (section_name), it produces an undefined symbol
5570 .startof.section_name. Similarly, when it sees
5571 .sizeof. (section_name), it produces an undefined symbol
5572 .sizeof.section_name. For all the output sections, we look for
5573 such symbols, and set them to the correct value. */
5576 lang_set_startof (void)
5580 if (link_info.relocatable)
5583 for (s = link_info.output_bfd->sections; s != NULL; s = s->next)
5585 const char *secname;
5587 struct bfd_link_hash_entry *h;
5589 secname = bfd_get_section_name (link_info.output_bfd, s);
5590 buf = (char *) xmalloc (10 + strlen (secname));
5592 sprintf (buf, ".startof.%s", secname);
5593 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
5594 if (h != NULL && h->type == bfd_link_hash_undefined)
5596 h->type = bfd_link_hash_defined;
5597 h->u.def.value = bfd_get_section_vma (link_info.output_bfd, s);
5598 h->u.def.section = bfd_abs_section_ptr;
5601 sprintf (buf, ".sizeof.%s", secname);
5602 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
5603 if (h != NULL && h->type == bfd_link_hash_undefined)
5605 h->type = bfd_link_hash_defined;
5606 h->u.def.value = TO_ADDR (s->size);
5607 h->u.def.section = bfd_abs_section_ptr;
5617 struct bfd_link_hash_entry *h;
5620 if ((link_info.relocatable && !link_info.gc_sections)
5621 || (link_info.shared && !link_info.executable))
5622 warn = entry_from_cmdline;
5626 /* Force the user to specify a root when generating a relocatable with
5628 if (link_info.gc_sections && link_info.relocatable
5629 && !(entry_from_cmdline || undef_from_cmdline))
5630 einfo (_("%P%F: gc-sections requires either an entry or "
5631 "an undefined symbol\n"));
5633 if (entry_symbol.name == NULL)
5635 /* No entry has been specified. Look for the default entry, but
5636 don't warn if we don't find it. */
5637 entry_symbol.name = entry_symbol_default;
5641 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
5642 FALSE, FALSE, TRUE);
5644 && (h->type == bfd_link_hash_defined
5645 || h->type == bfd_link_hash_defweak)
5646 && h->u.def.section->output_section != NULL)
5650 val = (h->u.def.value
5651 + bfd_get_section_vma (link_info.output_bfd,
5652 h->u.def.section->output_section)
5653 + h->u.def.section->output_offset);
5654 if (! bfd_set_start_address (link_info.output_bfd, val))
5655 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
5662 /* We couldn't find the entry symbol. Try parsing it as a
5664 val = bfd_scan_vma (entry_symbol.name, &send, 0);
5667 if (! bfd_set_start_address (link_info.output_bfd, val))
5668 einfo (_("%P%F: can't set start address\n"));
5674 /* Can't find the entry symbol, and it's not a number. Use
5675 the first address in the text section. */
5676 ts = bfd_get_section_by_name (link_info.output_bfd, entry_section);
5680 einfo (_("%P: warning: cannot find entry symbol %s;"
5681 " defaulting to %V\n"),
5683 bfd_get_section_vma (link_info.output_bfd, ts));
5684 if (!(bfd_set_start_address
5685 (link_info.output_bfd,
5686 bfd_get_section_vma (link_info.output_bfd, ts))))
5687 einfo (_("%P%F: can't set start address\n"));
5692 einfo (_("%P: warning: cannot find entry symbol %s;"
5693 " not setting start address\n"),
5699 /* Don't bfd_hash_table_free (&lang_definedness_table);
5700 map file output may result in a call of lang_track_definedness. */
5703 /* This is a small function used when we want to ignore errors from
5707 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
5709 /* Don't do anything. */
5712 /* Check that the architecture of all the input files is compatible
5713 with the output file. Also call the backend to let it do any
5714 other checking that is needed. */
5719 lang_statement_union_type *file;
5721 const bfd_arch_info_type *compatible;
5723 for (file = file_chain.head; file != NULL; file = file->input_statement.next)
5725 #ifdef ENABLE_PLUGINS
5726 /* Don't check format of files claimed by plugin. */
5727 if (file->input_statement.claimed)
5729 #endif /* ENABLE_PLUGINS */
5730 input_bfd = file->input_statement.the_bfd;
5732 = bfd_arch_get_compatible (input_bfd, link_info.output_bfd,
5733 command_line.accept_unknown_input_arch);
5735 /* In general it is not possible to perform a relocatable
5736 link between differing object formats when the input
5737 file has relocations, because the relocations in the
5738 input format may not have equivalent representations in
5739 the output format (and besides BFD does not translate
5740 relocs for other link purposes than a final link). */
5741 if ((link_info.relocatable || link_info.emitrelocations)
5742 && (compatible == NULL
5743 || (bfd_get_flavour (input_bfd)
5744 != bfd_get_flavour (link_info.output_bfd)))
5745 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
5747 einfo (_("%P%F: Relocatable linking with relocations from"
5748 " format %s (%B) to format %s (%B) is not supported\n"),
5749 bfd_get_target (input_bfd), input_bfd,
5750 bfd_get_target (link_info.output_bfd), link_info.output_bfd);
5751 /* einfo with %F exits. */
5754 if (compatible == NULL)
5756 if (command_line.warn_mismatch)
5757 einfo (_("%P%X: %s architecture of input file `%B'"
5758 " is incompatible with %s output\n"),
5759 bfd_printable_name (input_bfd), input_bfd,
5760 bfd_printable_name (link_info.output_bfd));
5762 else if (bfd_count_sections (input_bfd))
5764 /* If the input bfd has no contents, it shouldn't set the
5765 private data of the output bfd. */
5767 bfd_error_handler_type pfn = NULL;
5769 /* If we aren't supposed to warn about mismatched input
5770 files, temporarily set the BFD error handler to a
5771 function which will do nothing. We still want to call
5772 bfd_merge_private_bfd_data, since it may set up
5773 information which is needed in the output file. */
5774 if (! command_line.warn_mismatch)
5775 pfn = bfd_set_error_handler (ignore_bfd_errors);
5776 if (! bfd_merge_private_bfd_data (input_bfd, link_info.output_bfd))
5778 if (command_line.warn_mismatch)
5779 einfo (_("%P%X: failed to merge target specific data"
5780 " of file %B\n"), input_bfd);
5782 if (! command_line.warn_mismatch)
5783 bfd_set_error_handler (pfn);
5788 /* Look through all the global common symbols and attach them to the
5789 correct section. The -sort-common command line switch may be used
5790 to roughly sort the entries by alignment. */
5795 if (command_line.inhibit_common_definition)
5797 if (link_info.relocatable
5798 && ! command_line.force_common_definition)
5801 if (! config.sort_common)
5802 bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
5807 if (config.sort_common == sort_descending)
5809 for (power = 4; power > 0; power--)
5810 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
5813 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
5817 for (power = 0; power <= 4; power++)
5818 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
5821 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
5826 /* Place one common symbol in the correct section. */
5829 lang_one_common (struct bfd_link_hash_entry *h, void *info)
5831 unsigned int power_of_two;
5835 if (h->type != bfd_link_hash_common)
5839 power_of_two = h->u.c.p->alignment_power;
5841 if (config.sort_common == sort_descending
5842 && power_of_two < *(unsigned int *) info)
5844 else if (config.sort_common == sort_ascending
5845 && power_of_two > *(unsigned int *) info)
5848 section = h->u.c.p->section;
5849 if (!bfd_define_common_symbol (link_info.output_bfd, &link_info, h))
5850 einfo (_("%P%F: Could not define common symbol `%T': %E\n"),
5853 if (config.map_file != NULL)
5855 static bfd_boolean header_printed;
5860 if (! header_printed)
5862 minfo (_("\nAllocating common symbols\n"));
5863 minfo (_("Common symbol size file\n\n"));
5864 header_printed = TRUE;
5867 name = bfd_demangle (link_info.output_bfd, h->root.string,
5868 DMGL_ANSI | DMGL_PARAMS);
5871 minfo ("%s", h->root.string);
5872 len = strlen (h->root.string);
5877 len = strlen (name);
5893 if (size <= 0xffffffff)
5894 sprintf (buf, "%lx", (unsigned long) size);
5896 sprintf_vma (buf, size);
5906 minfo ("%B\n", section->owner);
5912 /* Run through the input files and ensure that every input section has
5913 somewhere to go. If one is found without a destination then create
5914 an input request and place it into the statement tree. */
5917 lang_place_orphans (void)
5919 LANG_FOR_EACH_INPUT_STATEMENT (file)
5923 for (s = file->the_bfd->sections; s != NULL; s = s->next)
5925 if (s->output_section == NULL)
5927 /* This section of the file is not attached, root
5928 around for a sensible place for it to go. */
5930 if (file->just_syms_flag)
5931 bfd_link_just_syms (file->the_bfd, s, &link_info);
5932 else if ((s->flags & SEC_EXCLUDE) != 0)
5933 s->output_section = bfd_abs_section_ptr;
5934 else if (strcmp (s->name, "COMMON") == 0)
5936 /* This is a lonely common section which must have
5937 come from an archive. We attach to the section
5938 with the wildcard. */
5939 if (! link_info.relocatable
5940 || command_line.force_common_definition)
5942 if (default_common_section == NULL)
5943 default_common_section
5944 = lang_output_section_statement_lookup (".bss", 0,
5946 lang_add_section (&default_common_section->children, s,
5947 default_common_section);
5952 const char *name = s->name;
5955 if (config.unique_orphan_sections
5956 || unique_section_p (s, NULL))
5957 constraint = SPECIAL;
5959 if (!ldemul_place_orphan (s, name, constraint))
5961 lang_output_section_statement_type *os;
5962 os = lang_output_section_statement_lookup (name,
5965 if (os->addr_tree == NULL
5966 && (link_info.relocatable
5967 || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0))
5968 os->addr_tree = exp_intop (0);
5969 lang_add_section (&os->children, s, os);
5978 lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
5980 flagword *ptr_flags;
5982 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
5988 *ptr_flags |= SEC_ALLOC;
5992 *ptr_flags |= SEC_READONLY;
5996 *ptr_flags |= SEC_DATA;
6000 *ptr_flags |= SEC_CODE;
6005 *ptr_flags |= SEC_LOAD;
6009 einfo (_("%P%F: invalid syntax in flags\n"));
6016 /* Call a function on each input file. This function will be called
6017 on an archive, but not on the elements. */
6020 lang_for_each_input_file (void (*func) (lang_input_statement_type *))
6022 lang_input_statement_type *f;
6024 for (f = (lang_input_statement_type *) input_file_chain.head;
6026 f = (lang_input_statement_type *) f->next_real_file)
6030 /* Call a function on each file. The function will be called on all
6031 the elements of an archive which are included in the link, but will
6032 not be called on the archive file itself. */
6035 lang_for_each_file (void (*func) (lang_input_statement_type *))
6037 LANG_FOR_EACH_INPUT_STATEMENT (f)
6044 ldlang_add_file (lang_input_statement_type *entry)
6046 lang_statement_append (&file_chain,
6047 (lang_statement_union_type *) entry,
6050 /* The BFD linker needs to have a list of all input BFDs involved in
6052 ASSERT (entry->the_bfd->link_next == NULL);
6053 ASSERT (entry->the_bfd != link_info.output_bfd);
6055 *link_info.input_bfds_tail = entry->the_bfd;
6056 link_info.input_bfds_tail = &entry->the_bfd->link_next;
6057 entry->the_bfd->usrdata = entry;
6058 bfd_set_gp_size (entry->the_bfd, g_switch_value);
6060 /* Look through the sections and check for any which should not be
6061 included in the link. We need to do this now, so that we can
6062 notice when the backend linker tries to report multiple
6063 definition errors for symbols which are in sections we aren't
6064 going to link. FIXME: It might be better to entirely ignore
6065 symbols which are defined in sections which are going to be
6066 discarded. This would require modifying the backend linker for
6067 each backend which might set the SEC_LINK_ONCE flag. If we do
6068 this, we should probably handle SEC_EXCLUDE in the same way. */
6070 bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
6074 lang_add_output (const char *name, int from_script)
6076 /* Make -o on command line override OUTPUT in script. */
6077 if (!had_output_filename || !from_script)
6079 output_filename = name;
6080 had_output_filename = TRUE;
6084 static lang_output_section_statement_type *current_section;
6095 for (l = 0; l < 32; l++)
6097 if (i >= (unsigned int) x)
6105 lang_output_section_statement_type *
6106 lang_enter_output_section_statement (const char *output_section_statement_name,
6107 etree_type *address_exp,
6108 enum section_type sectype,
6110 etree_type *subalign,
6114 lang_output_section_statement_type *os;
6116 os = lang_output_section_statement_lookup (output_section_statement_name,
6118 current_section = os;
6120 if (os->addr_tree == NULL)
6122 os->addr_tree = address_exp;
6124 os->sectype = sectype;
6125 if (sectype != noload_section)
6126 os->flags = SEC_NO_FLAGS;
6128 os->flags = SEC_NEVER_LOAD;
6129 os->block_value = 1;
6131 /* Make next things chain into subchain of this. */
6132 push_stat_ptr (&os->children);
6134 os->subsection_alignment =
6135 topower (exp_get_value_int (subalign, -1, "subsection alignment"));
6136 os->section_alignment =
6137 topower (exp_get_value_int (align, -1, "section alignment"));
6139 os->load_base = ebase;
6146 lang_output_statement_type *new_stmt;
6148 new_stmt = new_stat (lang_output_statement, stat_ptr);
6149 new_stmt->name = output_filename;
6153 /* Reset the current counters in the regions. */
6156 lang_reset_memory_regions (void)
6158 lang_memory_region_type *p = lang_memory_region_list;
6160 lang_output_section_statement_type *os;
6162 for (p = lang_memory_region_list; p != NULL; p = p->next)
6164 p->current = p->origin;
6168 for (os = &lang_output_section_statement.head->output_section_statement;
6172 os->processed_vma = FALSE;
6173 os->processed_lma = FALSE;
6176 for (o = link_info.output_bfd->sections; o != NULL; o = o->next)
6178 /* Save the last size for possible use by bfd_relax_section. */
6179 o->rawsize = o->size;
6184 /* Worker for lang_gc_sections_1. */
6187 gc_section_callback (lang_wild_statement_type *ptr,
6188 struct wildcard_list *sec ATTRIBUTE_UNUSED,
6190 lang_input_statement_type *file ATTRIBUTE_UNUSED,
6191 void *data ATTRIBUTE_UNUSED)
6193 /* If the wild pattern was marked KEEP, the member sections
6194 should be as well. */
6195 if (ptr->keep_sections)
6196 section->flags |= SEC_KEEP;
6199 /* Iterate over sections marking them against GC. */
6202 lang_gc_sections_1 (lang_statement_union_type *s)
6204 for (; s != NULL; s = s->header.next)
6206 switch (s->header.type)
6208 case lang_wild_statement_enum:
6209 walk_wild (&s->wild_statement, gc_section_callback, NULL);
6211 case lang_constructors_statement_enum:
6212 lang_gc_sections_1 (constructor_list.head);
6214 case lang_output_section_statement_enum:
6215 lang_gc_sections_1 (s->output_section_statement.children.head);
6217 case lang_group_statement_enum:
6218 lang_gc_sections_1 (s->group_statement.children.head);
6227 lang_gc_sections (void)
6229 /* Keep all sections so marked in the link script. */
6231 lang_gc_sections_1 (statement_list.head);
6233 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
6234 the special case of debug info. (See bfd/stabs.c)
6235 Twiddle the flag here, to simplify later linker code. */
6236 if (link_info.relocatable)
6238 LANG_FOR_EACH_INPUT_STATEMENT (f)
6241 #ifdef ENABLE_PLUGINS
6245 for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next)
6246 if ((sec->flags & SEC_DEBUGGING) == 0)
6247 sec->flags &= ~SEC_EXCLUDE;
6251 if (link_info.gc_sections)
6252 bfd_gc_sections (link_info.output_bfd, &link_info);
6255 /* Worker for lang_find_relro_sections_1. */
6258 find_relro_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
6259 struct wildcard_list *sec ATTRIBUTE_UNUSED,
6261 lang_input_statement_type *file ATTRIBUTE_UNUSED,
6264 /* Discarded, excluded and ignored sections effectively have zero
6266 if (section->output_section != NULL
6267 && section->output_section->owner == link_info.output_bfd
6268 && (section->output_section->flags & SEC_EXCLUDE) == 0
6269 && !IGNORE_SECTION (section)
6270 && section->size != 0)
6272 bfd_boolean *has_relro_section = (bfd_boolean *) data;
6273 *has_relro_section = TRUE;
6277 /* Iterate over sections for relro sections. */
6280 lang_find_relro_sections_1 (lang_statement_union_type *s,
6281 bfd_boolean *has_relro_section)
6283 if (*has_relro_section)
6286 for (; s != NULL; s = s->header.next)
6288 if (s == expld.dataseg.relro_end_stat)
6291 switch (s->header.type)
6293 case lang_wild_statement_enum:
6294 walk_wild (&s->wild_statement,
6295 find_relro_section_callback,
6298 case lang_constructors_statement_enum:
6299 lang_find_relro_sections_1 (constructor_list.head,
6302 case lang_output_section_statement_enum:
6303 lang_find_relro_sections_1 (s->output_section_statement.children.head,
6306 case lang_group_statement_enum:
6307 lang_find_relro_sections_1 (s->group_statement.children.head,
6317 lang_find_relro_sections (void)
6319 bfd_boolean has_relro_section = FALSE;
6321 /* Check all sections in the link script. */
6323 lang_find_relro_sections_1 (expld.dataseg.relro_start_stat,
6324 &has_relro_section);
6326 if (!has_relro_section)
6327 link_info.relro = FALSE;
6330 /* Relax all sections until bfd_relax_section gives up. */
6333 lang_relax_sections (bfd_boolean need_layout)
6335 if (RELAXATION_ENABLED)
6337 /* We may need more than one relaxation pass. */
6338 int i = link_info.relax_pass;
6340 /* The backend can use it to determine the current pass. */
6341 link_info.relax_pass = 0;
6345 /* Keep relaxing until bfd_relax_section gives up. */
6346 bfd_boolean relax_again;
6348 link_info.relax_trip = -1;
6351 link_info.relax_trip++;
6353 /* Note: pe-dll.c does something like this also. If you find
6354 you need to change this code, you probably need to change
6355 pe-dll.c also. DJ */
6357 /* Do all the assignments with our current guesses as to
6359 lang_do_assignments (lang_assigning_phase_enum);
6361 /* We must do this after lang_do_assignments, because it uses
6363 lang_reset_memory_regions ();
6365 /* Perform another relax pass - this time we know where the
6366 globals are, so can make a better guess. */
6367 relax_again = FALSE;
6368 lang_size_sections (&relax_again, FALSE);
6370 while (relax_again);
6372 link_info.relax_pass++;
6379 /* Final extra sizing to report errors. */
6380 lang_do_assignments (lang_assigning_phase_enum);
6381 lang_reset_memory_regions ();
6382 lang_size_sections (NULL, TRUE);
6386 #ifdef ENABLE_PLUGINS
6387 /* Find the insert point for the plugin's replacement files. We
6388 place them after the first claimed real object file, or if the
6389 first claimed object is an archive member, after the last real
6390 object file immediately preceding the archive. In the event
6391 no objects have been claimed at all, we return the first dummy
6392 object file on the list as the insert point; that works, but
6393 the callee must be careful when relinking the file_chain as it
6394 is not actually on that chain, only the statement_list and the
6395 input_file list; in that case, the replacement files must be
6396 inserted at the head of the file_chain. */
6398 static lang_input_statement_type *
6399 find_replacements_insert_point (void)
6401 lang_input_statement_type *claim1, *lastobject;
6402 lastobject = &input_file_chain.head->input_statement;
6403 for (claim1 = &file_chain.head->input_statement;
6405 claim1 = &claim1->next->input_statement)
6407 if (claim1->claimed)
6408 return claim1->claim_archive ? lastobject : claim1;
6409 /* Update lastobject if this is a real object file. */
6410 if (claim1->the_bfd && (claim1->the_bfd->my_archive == NULL))
6411 lastobject = claim1;
6413 /* No files were claimed by the plugin. Choose the last object
6414 file found on the list (maybe the first, dummy entry) as the
6419 /* Insert SRCLIST into DESTLIST after given element by chaining
6420 on FIELD as the next-pointer. (Counterintuitively does not need
6421 a pointer to the actual after-node itself, just its chain field.) */
6424 lang_list_insert_after (lang_statement_list_type *destlist,
6425 lang_statement_list_type *srclist,
6426 lang_statement_union_type **field)
6428 *(srclist->tail) = *field;
6429 *field = srclist->head;
6430 if (destlist->tail == field)
6431 destlist->tail = srclist->tail;
6434 /* Detach new nodes added to DESTLIST since the time ORIGLIST
6435 was taken as a copy of it and leave them in ORIGLIST. */
6438 lang_list_remove_tail (lang_statement_list_type *destlist,
6439 lang_statement_list_type *origlist)
6441 union lang_statement_union **savetail;
6442 /* Check that ORIGLIST really is an earlier state of DESTLIST. */
6443 ASSERT (origlist->head == destlist->head);
6444 savetail = origlist->tail;
6445 origlist->head = *(savetail);
6446 origlist->tail = destlist->tail;
6447 destlist->tail = savetail;
6450 #endif /* ENABLE_PLUGINS */
6455 /* Finalize dynamic list. */
6456 if (link_info.dynamic_list)
6457 lang_finalize_version_expr_head (&link_info.dynamic_list->head);
6459 current_target = default_target;
6461 /* Open the output file. */
6462 lang_for_each_statement (ldlang_open_output);
6465 ldemul_create_output_section_statements ();
6467 /* Add to the hash table all undefineds on the command line. */
6468 lang_place_undefineds ();
6470 if (!bfd_section_already_linked_table_init ())
6471 einfo (_("%P%F: Failed to create hash table\n"));
6473 /* Create a bfd for each input file. */
6474 current_target = default_target;
6475 open_input_bfds (statement_list.head, OPEN_BFD_NORMAL);
6477 #ifdef ENABLE_PLUGINS
6478 if (plugin_active_plugins_p ())
6480 lang_statement_list_type added;
6481 lang_statement_list_type files, inputfiles;
6483 /* Now all files are read, let the plugin(s) decide if there
6484 are any more to be added to the link before we call the
6485 emulation's after_open hook. We create a private list of
6486 input statements for this purpose, which we will eventually
6487 insert into the global statment list after the first claimed
6490 /* We need to manipulate all three chains in synchrony. */
6492 inputfiles = input_file_chain;
6493 if (plugin_call_all_symbols_read ())
6494 einfo (_("%P%F: %s: plugin reported error after all symbols read\n"),
6495 plugin_error_plugin ());
6496 /* Open any newly added files, updating the file chains. */
6497 open_input_bfds (added.head, OPEN_BFD_NORMAL);
6498 /* Restore the global list pointer now they have all been added. */
6499 lang_list_remove_tail (stat_ptr, &added);
6500 /* And detach the fresh ends of the file lists. */
6501 lang_list_remove_tail (&file_chain, &files);
6502 lang_list_remove_tail (&input_file_chain, &inputfiles);
6503 /* Were any new files added? */
6504 if (added.head != NULL)
6506 /* If so, we will insert them into the statement list immediately
6507 after the first input file that was claimed by the plugin. */
6508 plugin_insert = find_replacements_insert_point ();
6509 /* If a plugin adds input files without having claimed any, we
6510 don't really have a good idea where to place them. Just putting
6511 them at the start or end of the list is liable to leave them
6512 outside the crtbegin...crtend range. */
6513 ASSERT (plugin_insert != NULL);
6514 /* Splice the new statement list into the old one. */
6515 lang_list_insert_after (stat_ptr, &added,
6516 &plugin_insert->header.next);
6517 /* Likewise for the file chains. */
6518 lang_list_insert_after (&input_file_chain, &inputfiles,
6519 &plugin_insert->next_real_file);
6520 /* We must be careful when relinking file_chain; we may need to
6521 insert the new files at the head of the list if the insert
6522 point chosen is the dummy first input file. */
6523 if (plugin_insert->filename)
6524 lang_list_insert_after (&file_chain, &files, &plugin_insert->next);
6526 lang_list_insert_after (&file_chain, &files, &file_chain.head);
6528 /* Rescan archives in case new undefined symbols have appeared. */
6529 open_input_bfds (statement_list.head, OPEN_BFD_RESCAN);
6532 #endif /* ENABLE_PLUGINS */
6534 link_info.gc_sym_list = &entry_symbol;
6535 if (entry_symbol.name == NULL)
6536 link_info.gc_sym_list = ldlang_undef_chain_list_head;
6538 ldemul_after_open ();
6540 bfd_section_already_linked_table_free ();
6542 /* Make sure that we're not mixing architectures. We call this
6543 after all the input files have been opened, but before we do any
6544 other processing, so that any operations merge_private_bfd_data
6545 does on the output file will be known during the rest of the
6549 /* Handle .exports instead of a version script if we're told to do so. */
6550 if (command_line.version_exports_section)
6551 lang_do_version_exports_section ();
6553 /* Build all sets based on the information gathered from the input
6555 ldctor_build_sets ();
6557 /* Remove unreferenced sections if asked to. */
6558 lang_gc_sections ();
6560 /* Size up the common data. */
6563 /* Update wild statements. */
6564 update_wild_statements (statement_list.head);
6566 /* Run through the contours of the script and attach input sections
6567 to the correct output sections. */
6568 lang_statement_iteration++;
6569 map_input_to_output_sections (statement_list.head, NULL, NULL);
6571 process_insert_statements ();
6573 /* Find any sections not attached explicitly and handle them. */
6574 lang_place_orphans ();
6576 if (! link_info.relocatable)
6580 /* Merge SEC_MERGE sections. This has to be done after GC of
6581 sections, so that GCed sections are not merged, but before
6582 assigning dynamic symbols, since removing whole input sections
6584 bfd_merge_sections (link_info.output_bfd, &link_info);
6586 /* Look for a text section and set the readonly attribute in it. */
6587 found = bfd_get_section_by_name (link_info.output_bfd, ".text");
6591 if (config.text_read_only)
6592 found->flags |= SEC_READONLY;
6594 found->flags &= ~SEC_READONLY;
6598 /* Do anything special before sizing sections. This is where ELF
6599 and other back-ends size dynamic sections. */
6600 ldemul_before_allocation ();
6602 /* We must record the program headers before we try to fix the
6603 section positions, since they will affect SIZEOF_HEADERS. */
6604 lang_record_phdrs ();
6606 /* Check relro sections. */
6607 if (link_info.relro && ! link_info.relocatable)
6608 lang_find_relro_sections ();
6610 /* Size up the sections. */
6611 lang_size_sections (NULL, ! RELAXATION_ENABLED);
6613 /* See if anything special should be done now we know how big
6614 everything is. This is where relaxation is done. */
6615 ldemul_after_allocation ();
6617 /* Fix any .startof. or .sizeof. symbols. */
6618 lang_set_startof ();
6620 /* Do all the assignments, now that we know the final resting places
6621 of all the symbols. */
6622 lang_do_assignments (lang_final_phase_enum);
6626 /* Make sure that the section addresses make sense. */
6627 if (command_line.check_section_addresses)
6628 lang_check_section_addresses ();
6633 /* EXPORTED TO YACC */
6636 lang_add_wild (struct wildcard_spec *filespec,
6637 struct wildcard_list *section_list,
6638 bfd_boolean keep_sections)
6640 struct wildcard_list *curr, *next;
6641 lang_wild_statement_type *new_stmt;
6643 /* Reverse the list as the parser puts it back to front. */
6644 for (curr = section_list, section_list = NULL;
6646 section_list = curr, curr = next)
6648 if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
6649 placed_commons = TRUE;
6652 curr->next = section_list;
6655 if (filespec != NULL && filespec->name != NULL)
6657 if (strcmp (filespec->name, "*") == 0)
6658 filespec->name = NULL;
6659 else if (! wildcardp (filespec->name))
6660 lang_has_input_file = TRUE;
6663 new_stmt = new_stat (lang_wild_statement, stat_ptr);
6664 new_stmt->filename = NULL;
6665 new_stmt->filenames_sorted = FALSE;
6666 if (filespec != NULL)
6668 new_stmt->filename = filespec->name;
6669 new_stmt->filenames_sorted = filespec->sorted == by_name;
6671 new_stmt->section_list = section_list;
6672 new_stmt->keep_sections = keep_sections;
6673 lang_list_init (&new_stmt->children);
6674 analyze_walk_wild_section_handler (new_stmt);
6678 lang_section_start (const char *name, etree_type *address,
6679 const segment_type *segment)
6681 lang_address_statement_type *ad;
6683 ad = new_stat (lang_address_statement, stat_ptr);
6684 ad->section_name = name;
6685 ad->address = address;
6686 ad->segment = segment;
6689 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
6690 because of a -e argument on the command line, or zero if this is
6691 called by ENTRY in a linker script. Command line arguments take
6695 lang_add_entry (const char *name, bfd_boolean cmdline)
6697 if (entry_symbol.name == NULL
6699 || ! entry_from_cmdline)
6701 entry_symbol.name = name;
6702 entry_from_cmdline = cmdline;
6706 /* Set the default start symbol to NAME. .em files should use this,
6707 not lang_add_entry, to override the use of "start" if neither the
6708 linker script nor the command line specifies an entry point. NAME
6709 must be permanently allocated. */
6711 lang_default_entry (const char *name)
6713 entry_symbol_default = name;
6717 lang_add_target (const char *name)
6719 lang_target_statement_type *new_stmt;
6721 new_stmt = new_stat (lang_target_statement, stat_ptr);
6722 new_stmt->target = name;
6726 lang_add_map (const char *name)
6733 map_option_f = TRUE;
6741 lang_add_fill (fill_type *fill)
6743 lang_fill_statement_type *new_stmt;
6745 new_stmt = new_stat (lang_fill_statement, stat_ptr);
6746 new_stmt->fill = fill;
6750 lang_add_data (int type, union etree_union *exp)
6752 lang_data_statement_type *new_stmt;
6754 new_stmt = new_stat (lang_data_statement, stat_ptr);
6755 new_stmt->exp = exp;
6756 new_stmt->type = type;
6759 /* Create a new reloc statement. RELOC is the BFD relocation type to
6760 generate. HOWTO is the corresponding howto structure (we could
6761 look this up, but the caller has already done so). SECTION is the
6762 section to generate a reloc against, or NAME is the name of the
6763 symbol to generate a reloc against. Exactly one of SECTION and
6764 NAME must be NULL. ADDEND is an expression for the addend. */
6767 lang_add_reloc (bfd_reloc_code_real_type reloc,
6768 reloc_howto_type *howto,
6771 union etree_union *addend)
6773 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
6777 p->section = section;
6779 p->addend_exp = addend;
6781 p->addend_value = 0;
6782 p->output_section = NULL;
6783 p->output_offset = 0;
6786 lang_assignment_statement_type *
6787 lang_add_assignment (etree_type *exp)
6789 lang_assignment_statement_type *new_stmt;
6791 new_stmt = new_stat (lang_assignment_statement, stat_ptr);
6792 new_stmt->exp = exp;
6797 lang_add_attribute (enum statement_enum attribute)
6799 new_statement (attribute, sizeof (lang_statement_header_type), stat_ptr);
6803 lang_startup (const char *name)
6805 if (startup_file != NULL)
6807 einfo (_("%P%F: multiple STARTUP files\n"));
6809 first_file->filename = name;
6810 first_file->local_sym_name = name;
6811 first_file->real = TRUE;
6813 startup_file = name;
6817 lang_float (bfd_boolean maybe)
6819 lang_float_flag = maybe;
6823 /* Work out the load- and run-time regions from a script statement, and
6824 store them in *LMA_REGION and *REGION respectively.
6826 MEMSPEC is the name of the run-time region, or the value of
6827 DEFAULT_MEMORY_REGION if the statement didn't specify one.
6828 LMA_MEMSPEC is the name of the load-time region, or null if the
6829 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
6830 had an explicit load address.
6832 It is an error to specify both a load region and a load address. */
6835 lang_get_regions (lang_memory_region_type **region,
6836 lang_memory_region_type **lma_region,
6837 const char *memspec,
6838 const char *lma_memspec,
6839 bfd_boolean have_lma,
6840 bfd_boolean have_vma)
6842 *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
6844 /* If no runtime region or VMA has been specified, but the load region
6845 has been specified, then use the load region for the runtime region
6847 if (lma_memspec != NULL
6849 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
6850 *region = *lma_region;
6852 *region = lang_memory_region_lookup (memspec, FALSE);
6854 if (have_lma && lma_memspec != 0)
6855 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
6859 lang_leave_output_section_statement (fill_type *fill, const char *memspec,
6860 lang_output_section_phdr_list *phdrs,
6861 const char *lma_memspec)
6863 lang_get_regions (¤t_section->region,
6864 ¤t_section->lma_region,
6865 memspec, lma_memspec,
6866 current_section->load_base != NULL,
6867 current_section->addr_tree != NULL);
6869 /* If this section has no load region or base, but uses the same
6870 region as the previous section, then propagate the previous
6871 section's load region. */
6873 if (current_section->lma_region == NULL
6874 && current_section->load_base == NULL
6875 && current_section->addr_tree == NULL
6876 && current_section->region == current_section->prev->region)
6877 current_section->lma_region = current_section->prev->lma_region;
6879 current_section->fill = fill;
6880 current_section->phdrs = phdrs;
6884 /* Create an absolute symbol with the given name with the value of the
6885 address of first byte of the section named.
6887 If the symbol already exists, then do nothing. */
6890 lang_abs_symbol_at_beginning_of (const char *secname, const char *name)
6892 struct bfd_link_hash_entry *h;
6894 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
6896 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
6898 if (h->type == bfd_link_hash_new
6899 || h->type == bfd_link_hash_undefined)
6903 h->type = bfd_link_hash_defined;
6905 sec = bfd_get_section_by_name (link_info.output_bfd, secname);
6909 h->u.def.value = bfd_get_section_vma (link_info.output_bfd, sec);
6911 h->u.def.section = bfd_abs_section_ptr;
6915 /* Create an absolute symbol with the given name with the value of the
6916 address of the first byte after the end of the section named.
6918 If the symbol already exists, then do nothing. */
6921 lang_abs_symbol_at_end_of (const char *secname, const char *name)
6923 struct bfd_link_hash_entry *h;
6925 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
6927 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
6929 if (h->type == bfd_link_hash_new
6930 || h->type == bfd_link_hash_undefined)
6934 h->type = bfd_link_hash_defined;
6936 sec = bfd_get_section_by_name (link_info.output_bfd, secname);
6940 h->u.def.value = (bfd_get_section_vma (link_info.output_bfd, sec)
6941 + TO_ADDR (sec->size));
6943 h->u.def.section = bfd_abs_section_ptr;
6948 lang_statement_append (lang_statement_list_type *list,
6949 lang_statement_union_type *element,
6950 lang_statement_union_type **field)
6952 *(list->tail) = element;
6956 /* Set the output format type. -oformat overrides scripts. */
6959 lang_add_output_format (const char *format,
6964 if (output_target == NULL || !from_script)
6966 if (command_line.endian == ENDIAN_BIG
6969 else if (command_line.endian == ENDIAN_LITTLE
6973 output_target = format;
6978 lang_add_insert (const char *where, int is_before)
6980 lang_insert_statement_type *new_stmt;
6982 new_stmt = new_stat (lang_insert_statement, stat_ptr);
6983 new_stmt->where = where;
6984 new_stmt->is_before = is_before;
6985 saved_script_handle = previous_script_handle;
6988 /* Enter a group. This creates a new lang_group_statement, and sets
6989 stat_ptr to build new statements within the group. */
6992 lang_enter_group (void)
6994 lang_group_statement_type *g;
6996 g = new_stat (lang_group_statement, stat_ptr);
6997 lang_list_init (&g->children);
6998 push_stat_ptr (&g->children);
7001 /* Leave a group. This just resets stat_ptr to start writing to the
7002 regular list of statements again. Note that this will not work if
7003 groups can occur inside anything else which can adjust stat_ptr,
7004 but currently they can't. */
7007 lang_leave_group (void)
7012 /* Add a new program header. This is called for each entry in a PHDRS
7013 command in a linker script. */
7016 lang_new_phdr (const char *name,
7018 bfd_boolean filehdr,
7023 struct lang_phdr *n, **pp;
7026 n = (struct lang_phdr *) stat_alloc (sizeof (struct lang_phdr));
7029 n->type = exp_get_value_int (type, 0, "program header type");
7030 n->filehdr = filehdr;
7035 hdrs = n->type == 1 && (phdrs || filehdr);
7037 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
7040 && !((*pp)->filehdr || (*pp)->phdrs))
7042 einfo (_("%X%P:%S: PHDRS and FILEHDR are not supported when prior PT_LOAD headers lack them\n"));
7049 /* Record the program header information in the output BFD. FIXME: We
7050 should not be calling an ELF specific function here. */
7053 lang_record_phdrs (void)
7057 lang_output_section_phdr_list *last;
7058 struct lang_phdr *l;
7059 lang_output_section_statement_type *os;
7062 secs = (asection **) xmalloc (alc * sizeof (asection *));
7065 for (l = lang_phdr_list; l != NULL; l = l->next)
7072 for (os = &lang_output_section_statement.head->output_section_statement;
7076 lang_output_section_phdr_list *pl;
7078 if (os->constraint < 0)
7086 if (os->sectype == noload_section
7087 || os->bfd_section == NULL
7088 || (os->bfd_section->flags & SEC_ALLOC) == 0)
7091 /* Don't add orphans to PT_INTERP header. */
7097 lang_output_section_statement_type * tmp_os;
7099 /* If we have not run across a section with a program
7100 header assigned to it yet, then scan forwards to find
7101 one. This prevents inconsistencies in the linker's
7102 behaviour when a script has specified just a single
7103 header and there are sections in that script which are
7104 not assigned to it, and which occur before the first
7105 use of that header. See here for more details:
7106 http://sourceware.org/ml/binutils/2007-02/msg00291.html */
7107 for (tmp_os = os; tmp_os; tmp_os = tmp_os->next)
7110 last = tmp_os->phdrs;
7114 einfo (_("%F%P: no sections assigned to phdrs\n"));
7119 if (os->bfd_section == NULL)
7122 for (; pl != NULL; pl = pl->next)
7124 if (strcmp (pl->name, l->name) == 0)
7129 secs = (asection **) xrealloc (secs,
7130 alc * sizeof (asection *));
7132 secs[c] = os->bfd_section;
7139 if (l->flags == NULL)
7142 flags = exp_get_vma (l->flags, 0, "phdr flags");
7147 at = exp_get_vma (l->at, 0, "phdr load address");
7149 if (! bfd_record_phdr (link_info.output_bfd, l->type,
7150 l->flags != NULL, flags, l->at != NULL,
7151 at, l->filehdr, l->phdrs, c, secs))
7152 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
7157 /* Make sure all the phdr assignments succeeded. */
7158 for (os = &lang_output_section_statement.head->output_section_statement;
7162 lang_output_section_phdr_list *pl;
7164 if (os->constraint < 0
7165 || os->bfd_section == NULL)
7168 for (pl = os->phdrs;
7171 if (! pl->used && strcmp (pl->name, "NONE") != 0)
7172 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
7173 os->name, pl->name);
7177 /* Record a list of sections which may not be cross referenced. */
7180 lang_add_nocrossref (lang_nocrossref_type *l)
7182 struct lang_nocrossrefs *n;
7184 n = (struct lang_nocrossrefs *) xmalloc (sizeof *n);
7185 n->next = nocrossref_list;
7187 nocrossref_list = n;
7189 /* Set notice_all so that we get informed about all symbols. */
7190 link_info.notice_all = TRUE;
7193 /* Overlay handling. We handle overlays with some static variables. */
7195 /* The overlay virtual address. */
7196 static etree_type *overlay_vma;
7197 /* And subsection alignment. */
7198 static etree_type *overlay_subalign;
7200 /* An expression for the maximum section size seen so far. */
7201 static etree_type *overlay_max;
7203 /* A list of all the sections in this overlay. */
7205 struct overlay_list {
7206 struct overlay_list *next;
7207 lang_output_section_statement_type *os;
7210 static struct overlay_list *overlay_list;
7212 /* Start handling an overlay. */
7215 lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
7217 /* The grammar should prevent nested overlays from occurring. */
7218 ASSERT (overlay_vma == NULL
7219 && overlay_subalign == NULL
7220 && overlay_max == NULL);
7222 overlay_vma = vma_expr;
7223 overlay_subalign = subalign;
7226 /* Start a section in an overlay. We handle this by calling
7227 lang_enter_output_section_statement with the correct VMA.
7228 lang_leave_overlay sets up the LMA and memory regions. */
7231 lang_enter_overlay_section (const char *name)
7233 struct overlay_list *n;
7236 lang_enter_output_section_statement (name, overlay_vma, overlay_section,
7237 0, overlay_subalign, 0, 0);
7239 /* If this is the first section, then base the VMA of future
7240 sections on this one. This will work correctly even if `.' is
7241 used in the addresses. */
7242 if (overlay_list == NULL)
7243 overlay_vma = exp_nameop (ADDR, name);
7245 /* Remember the section. */
7246 n = (struct overlay_list *) xmalloc (sizeof *n);
7247 n->os = current_section;
7248 n->next = overlay_list;
7251 size = exp_nameop (SIZEOF, name);
7253 /* Arrange to work out the maximum section end address. */
7254 if (overlay_max == NULL)
7257 overlay_max = exp_binop (MAX_K, overlay_max, size);
7260 /* Finish a section in an overlay. There isn't any special to do
7264 lang_leave_overlay_section (fill_type *fill,
7265 lang_output_section_phdr_list *phdrs)
7272 name = current_section->name;
7274 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
7275 region and that no load-time region has been specified. It doesn't
7276 really matter what we say here, since lang_leave_overlay will
7278 lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
7280 /* Define the magic symbols. */
7282 clean = (char *) xmalloc (strlen (name) + 1);
7284 for (s1 = name; *s1 != '\0'; s1++)
7285 if (ISALNUM (*s1) || *s1 == '_')
7289 buf = (char *) xmalloc (strlen (clean) + sizeof "__load_start_");
7290 sprintf (buf, "__load_start_%s", clean);
7291 lang_add_assignment (exp_provide (buf,
7292 exp_nameop (LOADADDR, name),
7295 buf = (char *) xmalloc (strlen (clean) + sizeof "__load_stop_");
7296 sprintf (buf, "__load_stop_%s", clean);
7297 lang_add_assignment (exp_provide (buf,
7299 exp_nameop (LOADADDR, name),
7300 exp_nameop (SIZEOF, name)),
7306 /* Finish an overlay. If there are any overlay wide settings, this
7307 looks through all the sections in the overlay and sets them. */
7310 lang_leave_overlay (etree_type *lma_expr,
7313 const char *memspec,
7314 lang_output_section_phdr_list *phdrs,
7315 const char *lma_memspec)
7317 lang_memory_region_type *region;
7318 lang_memory_region_type *lma_region;
7319 struct overlay_list *l;
7320 lang_nocrossref_type *nocrossref;
7322 lang_get_regions (®ion, &lma_region,
7323 memspec, lma_memspec,
7324 lma_expr != NULL, FALSE);
7328 /* After setting the size of the last section, set '.' to end of the
7330 if (overlay_list != NULL)
7331 overlay_list->os->update_dot_tree
7332 = exp_assign (".", exp_binop ('+', overlay_vma, overlay_max));
7337 struct overlay_list *next;
7339 if (fill != NULL && l->os->fill == NULL)
7342 l->os->region = region;
7343 l->os->lma_region = lma_region;
7345 /* The first section has the load address specified in the
7346 OVERLAY statement. The rest are worked out from that.
7347 The base address is not needed (and should be null) if
7348 an LMA region was specified. */
7351 l->os->load_base = lma_expr;
7352 l->os->sectype = normal_section;
7354 if (phdrs != NULL && l->os->phdrs == NULL)
7355 l->os->phdrs = phdrs;
7359 lang_nocrossref_type *nc;
7361 nc = (lang_nocrossref_type *) xmalloc (sizeof *nc);
7362 nc->name = l->os->name;
7363 nc->next = nocrossref;
7372 if (nocrossref != NULL)
7373 lang_add_nocrossref (nocrossref);
7376 overlay_list = NULL;
7380 /* Version handling. This is only useful for ELF. */
7382 /* This global variable holds the version tree that we build. */
7384 struct bfd_elf_version_tree *lang_elf_version_info;
7386 /* If PREV is NULL, return first version pattern matching particular symbol.
7387 If PREV is non-NULL, return first version pattern matching particular
7388 symbol after PREV (previously returned by lang_vers_match). */
7390 static struct bfd_elf_version_expr *
7391 lang_vers_match (struct bfd_elf_version_expr_head *head,
7392 struct bfd_elf_version_expr *prev,
7396 const char *cxx_sym = sym;
7397 const char *java_sym = sym;
7398 struct bfd_elf_version_expr *expr = NULL;
7399 enum demangling_styles curr_style;
7401 curr_style = CURRENT_DEMANGLING_STYLE;
7402 cplus_demangle_set_style (no_demangling);
7403 c_sym = bfd_demangle (link_info.output_bfd, sym, DMGL_NO_OPTS);
7406 cplus_demangle_set_style (curr_style);
7408 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
7410 cxx_sym = bfd_demangle (link_info.output_bfd, sym,
7411 DMGL_PARAMS | DMGL_ANSI);
7415 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
7417 java_sym = bfd_demangle (link_info.output_bfd, sym, DMGL_JAVA);
7422 if (head->htab && (prev == NULL || prev->literal))
7424 struct bfd_elf_version_expr e;
7426 switch (prev ? prev->mask : 0)
7429 if (head->mask & BFD_ELF_VERSION_C_TYPE)
7432 expr = (struct bfd_elf_version_expr *)
7433 htab_find ((htab_t) head->htab, &e);
7434 while (expr && strcmp (expr->pattern, c_sym) == 0)
7435 if (expr->mask == BFD_ELF_VERSION_C_TYPE)
7441 case BFD_ELF_VERSION_C_TYPE:
7442 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
7444 e.pattern = cxx_sym;
7445 expr = (struct bfd_elf_version_expr *)
7446 htab_find ((htab_t) head->htab, &e);
7447 while (expr && strcmp (expr->pattern, cxx_sym) == 0)
7448 if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
7454 case BFD_ELF_VERSION_CXX_TYPE:
7455 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
7457 e.pattern = java_sym;
7458 expr = (struct bfd_elf_version_expr *)
7459 htab_find ((htab_t) head->htab, &e);
7460 while (expr && strcmp (expr->pattern, java_sym) == 0)
7461 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
7472 /* Finally, try the wildcards. */
7473 if (prev == NULL || prev->literal)
7474 expr = head->remaining;
7477 for (; expr; expr = expr->next)
7484 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
7487 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
7489 else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
7493 if (fnmatch (expr->pattern, s, 0) == 0)
7499 free ((char *) c_sym);
7501 free ((char *) cxx_sym);
7502 if (java_sym != sym)
7503 free ((char *) java_sym);
7507 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
7508 return a pointer to the symbol name with any backslash quotes removed. */
7511 realsymbol (const char *pattern)
7514 bfd_boolean changed = FALSE, backslash = FALSE;
7515 char *s, *symbol = (char *) xmalloc (strlen (pattern) + 1);
7517 for (p = pattern, s = symbol; *p != '\0'; ++p)
7519 /* It is a glob pattern only if there is no preceding
7523 /* Remove the preceding backslash. */
7530 if (*p == '?' || *p == '*' || *p == '[')
7537 backslash = *p == '\\';
7553 /* This is called for each variable name or match expression. NEW_NAME is
7554 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
7555 pattern to be matched against symbol names. */
7557 struct bfd_elf_version_expr *
7558 lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
7559 const char *new_name,
7561 bfd_boolean literal_p)
7563 struct bfd_elf_version_expr *ret;
7565 ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret);
7569 ret->literal = TRUE;
7570 ret->pattern = literal_p ? new_name : realsymbol (new_name);
7571 if (ret->pattern == NULL)
7573 ret->pattern = new_name;
7574 ret->literal = FALSE;
7577 if (lang == NULL || strcasecmp (lang, "C") == 0)
7578 ret->mask = BFD_ELF_VERSION_C_TYPE;
7579 else if (strcasecmp (lang, "C++") == 0)
7580 ret->mask = BFD_ELF_VERSION_CXX_TYPE;
7581 else if (strcasecmp (lang, "Java") == 0)
7582 ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
7585 einfo (_("%X%P: unknown language `%s' in version information\n"),
7587 ret->mask = BFD_ELF_VERSION_C_TYPE;
7590 return ldemul_new_vers_pattern (ret);
7593 /* This is called for each set of variable names and match
7596 struct bfd_elf_version_tree *
7597 lang_new_vers_node (struct bfd_elf_version_expr *globals,
7598 struct bfd_elf_version_expr *locals)
7600 struct bfd_elf_version_tree *ret;
7602 ret = (struct bfd_elf_version_tree *) xcalloc (1, sizeof *ret);
7603 ret->globals.list = globals;
7604 ret->locals.list = locals;
7605 ret->match = lang_vers_match;
7606 ret->name_indx = (unsigned int) -1;
7610 /* This static variable keeps track of version indices. */
7612 static int version_index;
7615 version_expr_head_hash (const void *p)
7617 const struct bfd_elf_version_expr *e =
7618 (const struct bfd_elf_version_expr *) p;
7620 return htab_hash_string (e->pattern);
7624 version_expr_head_eq (const void *p1, const void *p2)
7626 const struct bfd_elf_version_expr *e1 =
7627 (const struct bfd_elf_version_expr *) p1;
7628 const struct bfd_elf_version_expr *e2 =
7629 (const struct bfd_elf_version_expr *) p2;
7631 return strcmp (e1->pattern, e2->pattern) == 0;
7635 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
7638 struct bfd_elf_version_expr *e, *next;
7639 struct bfd_elf_version_expr **list_loc, **remaining_loc;
7641 for (e = head->list; e; e = e->next)
7645 head->mask |= e->mask;
7650 head->htab = htab_create (count * 2, version_expr_head_hash,
7651 version_expr_head_eq, NULL);
7652 list_loc = &head->list;
7653 remaining_loc = &head->remaining;
7654 for (e = head->list; e; e = next)
7660 remaining_loc = &e->next;
7664 void **loc = htab_find_slot ((htab_t) head->htab, e, INSERT);
7668 struct bfd_elf_version_expr *e1, *last;
7670 e1 = (struct bfd_elf_version_expr *) *loc;
7674 if (e1->mask == e->mask)
7682 while (e1 && strcmp (e1->pattern, e->pattern) == 0);
7686 /* This is a duplicate. */
7687 /* FIXME: Memory leak. Sometimes pattern is not
7688 xmalloced alone, but in larger chunk of memory. */
7689 /* free (e->pattern); */
7694 e->next = last->next;
7702 list_loc = &e->next;
7706 *remaining_loc = NULL;
7707 *list_loc = head->remaining;
7710 head->remaining = head->list;
7713 /* This is called when we know the name and dependencies of the
7717 lang_register_vers_node (const char *name,
7718 struct bfd_elf_version_tree *version,
7719 struct bfd_elf_version_deps *deps)
7721 struct bfd_elf_version_tree *t, **pp;
7722 struct bfd_elf_version_expr *e1;
7727 if ((name[0] == '\0' && lang_elf_version_info != NULL)
7728 || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
7730 einfo (_("%X%P: anonymous version tag cannot be combined"
7731 " with other version tags\n"));
7736 /* Make sure this node has a unique name. */
7737 for (t = lang_elf_version_info; t != NULL; t = t->next)
7738 if (strcmp (t->name, name) == 0)
7739 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
7741 lang_finalize_version_expr_head (&version->globals);
7742 lang_finalize_version_expr_head (&version->locals);
7744 /* Check the global and local match names, and make sure there
7745 aren't any duplicates. */
7747 for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
7749 for (t = lang_elf_version_info; t != NULL; t = t->next)
7751 struct bfd_elf_version_expr *e2;
7753 if (t->locals.htab && e1->literal)
7755 e2 = (struct bfd_elf_version_expr *)
7756 htab_find ((htab_t) t->locals.htab, e1);
7757 while (e2 && strcmp (e1->pattern, e2->pattern) == 0)
7759 if (e1->mask == e2->mask)
7760 einfo (_("%X%P: duplicate expression `%s'"
7761 " in version information\n"), e1->pattern);
7765 else if (!e1->literal)
7766 for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
7767 if (strcmp (e1->pattern, e2->pattern) == 0
7768 && e1->mask == e2->mask)
7769 einfo (_("%X%P: duplicate expression `%s'"
7770 " in version information\n"), e1->pattern);
7774 for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
7776 for (t = lang_elf_version_info; t != NULL; t = t->next)
7778 struct bfd_elf_version_expr *e2;
7780 if (t->globals.htab && e1->literal)
7782 e2 = (struct bfd_elf_version_expr *)
7783 htab_find ((htab_t) t->globals.htab, e1);
7784 while (e2 && strcmp (e1->pattern, e2->pattern) == 0)
7786 if (e1->mask == e2->mask)
7787 einfo (_("%X%P: duplicate expression `%s'"
7788 " in version information\n"),
7793 else if (!e1->literal)
7794 for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
7795 if (strcmp (e1->pattern, e2->pattern) == 0
7796 && e1->mask == e2->mask)
7797 einfo (_("%X%P: duplicate expression `%s'"
7798 " in version information\n"), e1->pattern);
7802 version->deps = deps;
7803 version->name = name;
7804 if (name[0] != '\0')
7807 version->vernum = version_index;
7810 version->vernum = 0;
7812 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
7817 /* This is called when we see a version dependency. */
7819 struct bfd_elf_version_deps *
7820 lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
7822 struct bfd_elf_version_deps *ret;
7823 struct bfd_elf_version_tree *t;
7825 ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret);
7828 for (t = lang_elf_version_info; t != NULL; t = t->next)
7830 if (strcmp (t->name, name) == 0)
7832 ret->version_needed = t;
7837 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
7839 ret->version_needed = NULL;
7844 lang_do_version_exports_section (void)
7846 struct bfd_elf_version_expr *greg = NULL, *lreg;
7848 LANG_FOR_EACH_INPUT_STATEMENT (is)
7850 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
7858 contents = (char *) xmalloc (len);
7859 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
7860 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
7863 while (p < contents + len)
7865 greg = lang_new_vers_pattern (greg, p, NULL, FALSE);
7866 p = strchr (p, '\0') + 1;
7869 /* Do not free the contents, as we used them creating the regex. */
7871 /* Do not include this section in the link. */
7872 sec->flags |= SEC_EXCLUDE | SEC_KEEP;
7875 lreg = lang_new_vers_pattern (NULL, "*", NULL, FALSE);
7876 lang_register_vers_node (command_line.version_exports_section,
7877 lang_new_vers_node (greg, lreg), NULL);
7881 lang_add_unique (const char *name)
7883 struct unique_sections *ent;
7885 for (ent = unique_section_list; ent; ent = ent->next)
7886 if (strcmp (ent->name, name) == 0)
7889 ent = (struct unique_sections *) xmalloc (sizeof *ent);
7890 ent->name = xstrdup (name);
7891 ent->next = unique_section_list;
7892 unique_section_list = ent;
7895 /* Append the list of dynamic symbols to the existing one. */
7898 lang_append_dynamic_list (struct bfd_elf_version_expr *dynamic)
7900 if (link_info.dynamic_list)
7902 struct bfd_elf_version_expr *tail;
7903 for (tail = dynamic; tail->next != NULL; tail = tail->next)
7905 tail->next = link_info.dynamic_list->head.list;
7906 link_info.dynamic_list->head.list = dynamic;
7910 struct bfd_elf_dynamic_list *d;
7912 d = (struct bfd_elf_dynamic_list *) xcalloc (1, sizeof *d);
7913 d->head.list = dynamic;
7914 d->match = lang_vers_match;
7915 link_info.dynamic_list = d;
7919 /* Append the list of C++ typeinfo dynamic symbols to the existing
7923 lang_append_dynamic_list_cpp_typeinfo (void)
7925 const char * symbols [] =
7927 "typeinfo name for*",
7930 struct bfd_elf_version_expr *dynamic = NULL;
7933 for (i = 0; i < ARRAY_SIZE (symbols); i++)
7934 dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++",
7937 lang_append_dynamic_list (dynamic);
7940 /* Append the list of C++ operator new and delete dynamic symbols to the
7944 lang_append_dynamic_list_cpp_new (void)
7946 const char * symbols [] =
7951 struct bfd_elf_version_expr *dynamic = NULL;
7954 for (i = 0; i < ARRAY_SIZE (symbols); i++)
7955 dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++",
7958 lang_append_dynamic_list (dynamic);
7961 /* Scan a space and/or comma separated string of features. */
7964 lang_ld_feature (char *str)
7972 while (*p == ',' || ISSPACE (*p))
7977 while (*q && *q != ',' && !ISSPACE (*q))
7981 if (strcasecmp (p, "SANE_EXPR") == 0)
7982 config.sane_expr = TRUE;
7984 einfo (_("%X%P: unknown feature `%s'\n"), p);