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
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"
45 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
48 /* Locals variables. */
49 static struct obstack stat_obstack;
50 static struct obstack map_obstack;
52 #define obstack_chunk_alloc xmalloc
53 #define obstack_chunk_free free
54 static const char *startup_file;
55 static const char *entry_symbol_default = "start";
56 static bfd_boolean placed_commons = FALSE;
57 static bfd_boolean stripped_excluded_sections = FALSE;
58 static lang_output_section_statement_type *default_common_section;
59 static bfd_boolean map_option_f;
60 static bfd_vma print_dot;
61 static lang_input_statement_type *first_file;
62 static const char *current_target;
63 static lang_statement_list_type statement_list;
64 static struct bfd_hash_table lang_definedness_table;
65 static lang_statement_list_type *stat_save[10];
66 static lang_statement_list_type **stat_save_ptr = &stat_save[0];
67 static struct unique_sections *unique_section_list;
68 static bfd_boolean ldlang_sysrooted_script = FALSE;
70 /* Forward declarations. */
71 static void init_map_userdata (bfd *, asection *, void *);
72 static lang_input_statement_type *lookup_name (const char *);
73 static struct bfd_hash_entry *lang_definedness_newfunc
74 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
75 static void insert_undefined (const char *);
76 static bfd_boolean sort_def_symbol (struct bfd_link_hash_entry *, void *);
77 static void print_statement (lang_statement_union_type *,
78 lang_output_section_statement_type *);
79 static void print_statement_list (lang_statement_union_type *,
80 lang_output_section_statement_type *);
81 static void print_statements (void);
82 static void print_input_section (asection *, bfd_boolean);
83 static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *);
84 static void lang_record_phdrs (void);
85 static void lang_do_version_exports_section (void);
86 static void lang_finalize_version_expr_head
87 (struct bfd_elf_version_expr_head *);
89 /* Exported variables. */
90 const char *output_target;
91 lang_output_section_statement_type *abs_output_section;
92 lang_statement_list_type lang_output_section_statement;
93 lang_statement_list_type *stat_ptr = &statement_list;
94 lang_statement_list_type file_chain = { NULL, NULL };
95 lang_statement_list_type input_file_chain;
96 struct bfd_sym_chain entry_symbol = { NULL, NULL };
97 const char *entry_section = ".text";
98 bfd_boolean entry_from_cmdline;
99 bfd_boolean lang_has_input_file = FALSE;
100 bfd_boolean had_output_filename = FALSE;
101 bfd_boolean lang_float_flag = FALSE;
102 bfd_boolean delete_output_file_on_failure = FALSE;
103 struct lang_phdr *lang_phdr_list;
104 struct lang_nocrossrefs *nocrossref_list;
105 bfd_boolean missing_file = FALSE;
107 /* Functions that traverse the linker script and might evaluate
108 DEFINED() need to increment this. */
109 int lang_statement_iteration = 0;
111 etree_type *base; /* Relocation base - or null */
113 /* Return TRUE if the PATTERN argument is a wildcard pattern.
114 Although backslashes are treated specially if a pattern contains
115 wildcards, we do not consider the mere presence of a backslash to
116 be enough to cause the pattern to be treated as a wildcard.
117 That lets us handle DOS filenames more naturally. */
118 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
120 #define new_stat(x, y) \
121 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
123 #define outside_section_address(q) \
124 ((q)->output_offset + (q)->output_section->vma)
126 #define outside_symbol_address(q) \
127 ((q)->value + outside_section_address (q->section))
129 #define SECTION_NAME_MAP_LENGTH (16)
132 stat_alloc (size_t size)
134 return obstack_alloc (&stat_obstack, size);
138 name_match (const char *pattern, const char *name)
140 if (wildcardp (pattern))
141 return fnmatch (pattern, name, 0);
142 return strcmp (pattern, name);
145 /* If PATTERN is of the form archive:file, return a pointer to the
146 separator. If not, return NULL. */
149 archive_path (const char *pattern)
153 if (link_info.path_separator == 0)
156 p = strchr (pattern, link_info.path_separator);
157 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
158 if (p == NULL || link_info.path_separator != ':')
161 /* Assume a match on the second char is part of drive specifier,
162 as in "c:\silly.dos". */
163 if (p == pattern + 1 && ISALPHA (*pattern))
164 p = strchr (p + 1, link_info.path_separator);
169 /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path,
170 return whether F matches FILE_SPEC. */
173 input_statement_is_archive_path (const char *file_spec, char *sep,
174 lang_input_statement_type *f)
176 bfd_boolean match = FALSE;
179 || name_match (sep + 1, f->filename) == 0)
180 && ((sep != file_spec)
181 == (f->the_bfd != NULL && f->the_bfd->my_archive != NULL)))
185 if (sep != file_spec)
187 const char *aname = f->the_bfd->my_archive->filename;
189 match = name_match (file_spec, aname) == 0;
190 *sep = link_info.path_separator;
197 unique_section_p (const asection *sec,
198 const lang_output_section_statement_type *os)
200 struct unique_sections *unam;
203 if (link_info.relocatable
204 && sec->owner != NULL
205 && bfd_is_group_section (sec->owner, sec))
207 && strcmp (os->name, DISCARD_SECTION_NAME) == 0);
210 for (unam = unique_section_list; unam; unam = unam->next)
211 if (name_match (unam->name, secnam) == 0)
217 /* Generic traversal routines for finding matching sections. */
219 /* Try processing a section against a wildcard. This just calls
220 the callback unless the filename exclusion list is present
221 and excludes the file. It's hardly ever present so this
222 function is very fast. */
225 walk_wild_consider_section (lang_wild_statement_type *ptr,
226 lang_input_statement_type *file,
228 struct wildcard_list *sec,
232 struct name_list *list_tmp;
234 /* Don't process sections from files which were excluded. */
235 for (list_tmp = sec->spec.exclude_name_list;
237 list_tmp = list_tmp->next)
239 char *p = archive_path (list_tmp->name);
243 if (input_statement_is_archive_path (list_tmp->name, p, file))
247 else if (name_match (list_tmp->name, file->filename) == 0)
250 /* FIXME: Perhaps remove the following at some stage? Matching
251 unadorned archives like this was never documented and has
252 been superceded by the archive:path syntax. */
253 else if (file->the_bfd != NULL
254 && file->the_bfd->my_archive != NULL
255 && name_match (list_tmp->name,
256 file->the_bfd->my_archive->filename) == 0)
260 (*callback) (ptr, sec, s, file, data);
263 /* Lowest common denominator routine that can handle everything correctly,
267 walk_wild_section_general (lang_wild_statement_type *ptr,
268 lang_input_statement_type *file,
273 struct wildcard_list *sec;
275 for (s = file->the_bfd->sections; s != NULL; s = s->next)
277 sec = ptr->section_list;
279 (*callback) (ptr, sec, s, file, data);
283 bfd_boolean skip = FALSE;
285 if (sec->spec.name != NULL)
287 const char *sname = bfd_get_section_name (file->the_bfd, s);
289 skip = name_match (sec->spec.name, sname) != 0;
293 walk_wild_consider_section (ptr, file, s, sec, callback, data);
300 /* Routines to find a single section given its name. If there's more
301 than one section with that name, we report that. */
305 asection *found_section;
306 bfd_boolean multiple_sections_found;
307 } section_iterator_callback_data;
310 section_iterator_callback (bfd *abfd ATTRIBUTE_UNUSED, asection *s, void *data)
312 section_iterator_callback_data *d = (section_iterator_callback_data *) data;
314 if (d->found_section != NULL)
316 d->multiple_sections_found = TRUE;
320 d->found_section = s;
325 find_section (lang_input_statement_type *file,
326 struct wildcard_list *sec,
327 bfd_boolean *multiple_sections_found)
329 section_iterator_callback_data cb_data = { NULL, FALSE };
331 bfd_get_section_by_name_if (file->the_bfd, sec->spec.name,
332 section_iterator_callback, &cb_data);
333 *multiple_sections_found = cb_data.multiple_sections_found;
334 return cb_data.found_section;
337 /* Code for handling simple wildcards without going through fnmatch,
338 which can be expensive because of charset translations etc. */
340 /* A simple wild is a literal string followed by a single '*',
341 where the literal part is at least 4 characters long. */
344 is_simple_wild (const char *name)
346 size_t len = strcspn (name, "*?[");
347 return len >= 4 && name[len] == '*' && name[len + 1] == '\0';
351 match_simple_wild (const char *pattern, const char *name)
353 /* The first four characters of the pattern are guaranteed valid
354 non-wildcard characters. So we can go faster. */
355 if (pattern[0] != name[0] || pattern[1] != name[1]
356 || pattern[2] != name[2] || pattern[3] != name[3])
361 while (*pattern != '*')
362 if (*name++ != *pattern++)
368 /* Compare sections ASEC and BSEC according to SORT. */
371 compare_section (sort_type sort, asection *asec, asection *bsec)
380 case by_alignment_name:
381 ret = (bfd_section_alignment (bsec->owner, bsec)
382 - bfd_section_alignment (asec->owner, asec));
388 ret = strcmp (bfd_get_section_name (asec->owner, asec),
389 bfd_get_section_name (bsec->owner, bsec));
392 case by_name_alignment:
393 ret = strcmp (bfd_get_section_name (asec->owner, asec),
394 bfd_get_section_name (bsec->owner, bsec));
400 ret = (bfd_section_alignment (bsec->owner, bsec)
401 - bfd_section_alignment (asec->owner, asec));
408 /* Build a Binary Search Tree to sort sections, unlike insertion sort
409 used in wild_sort(). BST is considerably faster if the number of
410 of sections are large. */
412 static lang_section_bst_type **
413 wild_sort_fast (lang_wild_statement_type *wild,
414 struct wildcard_list *sec,
415 lang_input_statement_type *file ATTRIBUTE_UNUSED,
418 lang_section_bst_type **tree;
421 if (!wild->filenames_sorted
422 && (sec == NULL || sec->spec.sorted == none))
424 /* Append at the right end of tree. */
426 tree = &((*tree)->right);
432 /* Find the correct node to append this section. */
433 if (compare_section (sec->spec.sorted, section, (*tree)->section) < 0)
434 tree = &((*tree)->left);
436 tree = &((*tree)->right);
442 /* Use wild_sort_fast to build a BST to sort sections. */
445 output_section_callback_fast (lang_wild_statement_type *ptr,
446 struct wildcard_list *sec,
448 lang_input_statement_type *file,
451 lang_section_bst_type *node;
452 lang_section_bst_type **tree;
453 lang_output_section_statement_type *os;
455 os = (lang_output_section_statement_type *) output;
457 if (unique_section_p (section, os))
460 node = (lang_section_bst_type *) xmalloc (sizeof (lang_section_bst_type));
463 node->section = section;
465 tree = wild_sort_fast (ptr, sec, file, section);
470 /* Convert a sorted sections' BST back to list form. */
473 output_section_callback_tree_to_list (lang_wild_statement_type *ptr,
474 lang_section_bst_type *tree,
478 output_section_callback_tree_to_list (ptr, tree->left, output);
480 lang_add_section (&ptr->children, tree->section,
481 (lang_output_section_statement_type *) output);
484 output_section_callback_tree_to_list (ptr, tree->right, output);
489 /* Specialized, optimized routines for handling different kinds of
493 walk_wild_section_specs1_wild0 (lang_wild_statement_type *ptr,
494 lang_input_statement_type *file,
498 /* We can just do a hash lookup for the section with the right name.
499 But if that lookup discovers more than one section with the name
500 (should be rare), we fall back to the general algorithm because
501 we would otherwise have to sort the sections to make sure they
502 get processed in the bfd's order. */
503 bfd_boolean multiple_sections_found;
504 struct wildcard_list *sec0 = ptr->handler_data[0];
505 asection *s0 = find_section (file, sec0, &multiple_sections_found);
507 if (multiple_sections_found)
508 walk_wild_section_general (ptr, file, callback, data);
510 walk_wild_consider_section (ptr, file, s0, sec0, callback, data);
514 walk_wild_section_specs1_wild1 (lang_wild_statement_type *ptr,
515 lang_input_statement_type *file,
520 struct wildcard_list *wildsec0 = ptr->handler_data[0];
522 for (s = file->the_bfd->sections; s != NULL; s = s->next)
524 const char *sname = bfd_get_section_name (file->the_bfd, s);
525 bfd_boolean skip = !match_simple_wild (wildsec0->spec.name, sname);
528 walk_wild_consider_section (ptr, file, s, wildsec0, callback, data);
533 walk_wild_section_specs2_wild1 (lang_wild_statement_type *ptr,
534 lang_input_statement_type *file,
539 struct wildcard_list *sec0 = ptr->handler_data[0];
540 struct wildcard_list *wildsec1 = ptr->handler_data[1];
541 bfd_boolean multiple_sections_found;
542 asection *s0 = find_section (file, sec0, &multiple_sections_found);
544 if (multiple_sections_found)
546 walk_wild_section_general (ptr, file, callback, data);
550 /* Note that if the section was not found, s0 is NULL and
551 we'll simply never succeed the s == s0 test below. */
552 for (s = file->the_bfd->sections; s != NULL; s = s->next)
554 /* Recall that in this code path, a section cannot satisfy more
555 than one spec, so if s == s0 then it cannot match
558 walk_wild_consider_section (ptr, file, s, sec0, callback, data);
561 const char *sname = bfd_get_section_name (file->the_bfd, s);
562 bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname);
565 walk_wild_consider_section (ptr, file, s, wildsec1, callback,
572 walk_wild_section_specs3_wild2 (lang_wild_statement_type *ptr,
573 lang_input_statement_type *file,
578 struct wildcard_list *sec0 = ptr->handler_data[0];
579 struct wildcard_list *wildsec1 = ptr->handler_data[1];
580 struct wildcard_list *wildsec2 = ptr->handler_data[2];
581 bfd_boolean multiple_sections_found;
582 asection *s0 = find_section (file, sec0, &multiple_sections_found);
584 if (multiple_sections_found)
586 walk_wild_section_general (ptr, file, callback, data);
590 for (s = file->the_bfd->sections; s != NULL; s = s->next)
593 walk_wild_consider_section (ptr, file, s, sec0, callback, data);
596 const char *sname = bfd_get_section_name (file->the_bfd, s);
597 bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname);
600 walk_wild_consider_section (ptr, file, s, wildsec1, callback, data);
603 skip = !match_simple_wild (wildsec2->spec.name, sname);
605 walk_wild_consider_section (ptr, file, s, wildsec2, callback,
613 walk_wild_section_specs4_wild2 (lang_wild_statement_type *ptr,
614 lang_input_statement_type *file,
619 struct wildcard_list *sec0 = ptr->handler_data[0];
620 struct wildcard_list *sec1 = ptr->handler_data[1];
621 struct wildcard_list *wildsec2 = ptr->handler_data[2];
622 struct wildcard_list *wildsec3 = ptr->handler_data[3];
623 bfd_boolean multiple_sections_found;
624 asection *s0 = find_section (file, sec0, &multiple_sections_found), *s1;
626 if (multiple_sections_found)
628 walk_wild_section_general (ptr, file, callback, data);
632 s1 = find_section (file, sec1, &multiple_sections_found);
633 if (multiple_sections_found)
635 walk_wild_section_general (ptr, file, callback, data);
639 for (s = file->the_bfd->sections; s != NULL; s = s->next)
642 walk_wild_consider_section (ptr, file, s, sec0, callback, data);
645 walk_wild_consider_section (ptr, file, s, sec1, callback, data);
648 const char *sname = bfd_get_section_name (file->the_bfd, s);
649 bfd_boolean skip = !match_simple_wild (wildsec2->spec.name,
653 walk_wild_consider_section (ptr, file, s, wildsec2, callback,
657 skip = !match_simple_wild (wildsec3->spec.name, sname);
659 walk_wild_consider_section (ptr, file, s, wildsec3,
667 walk_wild_section (lang_wild_statement_type *ptr,
668 lang_input_statement_type *file,
672 if (file->just_syms_flag)
675 (*ptr->walk_wild_section_handler) (ptr, file, callback, data);
678 /* Returns TRUE when name1 is a wildcard spec that might match
679 something name2 can match. We're conservative: we return FALSE
680 only if the prefixes of name1 and name2 are different up to the
681 first wildcard character. */
684 wild_spec_can_overlap (const char *name1, const char *name2)
686 size_t prefix1_len = strcspn (name1, "?*[");
687 size_t prefix2_len = strcspn (name2, "?*[");
688 size_t min_prefix_len;
690 /* Note that if there is no wildcard character, then we treat the
691 terminating 0 as part of the prefix. Thus ".text" won't match
692 ".text." or ".text.*", for example. */
693 if (name1[prefix1_len] == '\0')
695 if (name2[prefix2_len] == '\0')
698 min_prefix_len = prefix1_len < prefix2_len ? prefix1_len : prefix2_len;
700 return memcmp (name1, name2, min_prefix_len) == 0;
703 /* Select specialized code to handle various kinds of wildcard
707 analyze_walk_wild_section_handler (lang_wild_statement_type *ptr)
710 int wild_name_count = 0;
711 struct wildcard_list *sec;
715 ptr->walk_wild_section_handler = walk_wild_section_general;
716 ptr->handler_data[0] = NULL;
717 ptr->handler_data[1] = NULL;
718 ptr->handler_data[2] = NULL;
719 ptr->handler_data[3] = NULL;
722 /* Count how many wildcard_specs there are, and how many of those
723 actually use wildcards in the name. Also, bail out if any of the
724 wildcard names are NULL. (Can this actually happen?
725 walk_wild_section used to test for it.) And bail out if any
726 of the wildcards are more complex than a simple string
727 ending in a single '*'. */
728 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
731 if (sec->spec.name == NULL)
733 if (wildcardp (sec->spec.name))
736 if (!is_simple_wild (sec->spec.name))
741 /* The zero-spec case would be easy to optimize but it doesn't
742 happen in practice. Likewise, more than 4 specs doesn't
743 happen in practice. */
744 if (sec_count == 0 || sec_count > 4)
747 /* Check that no two specs can match the same section. */
748 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
750 struct wildcard_list *sec2;
751 for (sec2 = sec->next; sec2 != NULL; sec2 = sec2->next)
753 if (wild_spec_can_overlap (sec->spec.name, sec2->spec.name))
758 signature = (sec_count << 8) + wild_name_count;
762 ptr->walk_wild_section_handler = walk_wild_section_specs1_wild0;
765 ptr->walk_wild_section_handler = walk_wild_section_specs1_wild1;
768 ptr->walk_wild_section_handler = walk_wild_section_specs2_wild1;
771 ptr->walk_wild_section_handler = walk_wild_section_specs3_wild2;
774 ptr->walk_wild_section_handler = walk_wild_section_specs4_wild2;
780 /* Now fill the data array with pointers to the specs, first the
781 specs with non-wildcard names, then the specs with wildcard
782 names. It's OK to process the specs in different order from the
783 given order, because we've already determined that no section
784 will match more than one spec. */
786 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
787 if (!wildcardp (sec->spec.name))
788 ptr->handler_data[data_counter++] = sec;
789 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
790 if (wildcardp (sec->spec.name))
791 ptr->handler_data[data_counter++] = sec;
794 /* Handle a wild statement for a single file F. */
797 walk_wild_file (lang_wild_statement_type *s,
798 lang_input_statement_type *f,
802 if (f->the_bfd == NULL
803 || ! bfd_check_format (f->the_bfd, bfd_archive))
804 walk_wild_section (s, f, callback, data);
809 /* This is an archive file. We must map each member of the
810 archive separately. */
811 member = bfd_openr_next_archived_file (f->the_bfd, NULL);
812 while (member != NULL)
814 /* When lookup_name is called, it will call the add_symbols
815 entry point for the archive. For each element of the
816 archive which is included, BFD will call ldlang_add_file,
817 which will set the usrdata field of the member to the
818 lang_input_statement. */
819 if (member->usrdata != NULL)
821 walk_wild_section (s,
822 (lang_input_statement_type *) member->usrdata,
826 member = bfd_openr_next_archived_file (f->the_bfd, member);
832 walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
834 const char *file_spec = s->filename;
837 if (file_spec == NULL)
839 /* Perform the iteration over all files in the list. */
840 LANG_FOR_EACH_INPUT_STATEMENT (f)
842 walk_wild_file (s, f, callback, data);
845 else if ((p = archive_path (file_spec)) != NULL)
847 LANG_FOR_EACH_INPUT_STATEMENT (f)
849 if (input_statement_is_archive_path (file_spec, p, f))
850 walk_wild_file (s, f, callback, data);
853 else if (wildcardp (file_spec))
855 LANG_FOR_EACH_INPUT_STATEMENT (f)
857 if (fnmatch (file_spec, f->filename, 0) == 0)
858 walk_wild_file (s, f, callback, data);
863 lang_input_statement_type *f;
865 /* Perform the iteration over a single file. */
866 f = lookup_name (file_spec);
868 walk_wild_file (s, f, callback, data);
872 /* lang_for_each_statement walks the parse tree and calls the provided
873 function for each node. */
876 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
877 lang_statement_union_type *s)
879 for (; s != NULL; s = s->header.next)
883 switch (s->header.type)
885 case lang_constructors_statement_enum:
886 lang_for_each_statement_worker (func, constructor_list.head);
888 case lang_output_section_statement_enum:
889 lang_for_each_statement_worker
890 (func, s->output_section_statement.children.head);
892 case lang_wild_statement_enum:
893 lang_for_each_statement_worker (func,
894 s->wild_statement.children.head);
896 case lang_group_statement_enum:
897 lang_for_each_statement_worker (func,
898 s->group_statement.children.head);
900 case lang_data_statement_enum:
901 case lang_reloc_statement_enum:
902 case lang_object_symbols_statement_enum:
903 case lang_output_statement_enum:
904 case lang_target_statement_enum:
905 case lang_input_section_enum:
906 case lang_input_statement_enum:
907 case lang_assignment_statement_enum:
908 case lang_padding_statement_enum:
909 case lang_address_statement_enum:
910 case lang_fill_statement_enum:
911 case lang_insert_statement_enum:
921 lang_for_each_statement (void (*func) (lang_statement_union_type *))
923 lang_for_each_statement_worker (func, statement_list.head);
926 /*----------------------------------------------------------------------*/
929 lang_list_init (lang_statement_list_type *list)
932 list->tail = &list->head;
936 push_stat_ptr (lang_statement_list_type *new_ptr)
938 if (stat_save_ptr >= stat_save + sizeof (stat_save) / sizeof (stat_save[0]))
940 *stat_save_ptr++ = stat_ptr;
947 if (stat_save_ptr <= stat_save)
949 stat_ptr = *--stat_save_ptr;
952 /* Build a new statement node for the parse tree. */
954 static lang_statement_union_type *
955 new_statement (enum statement_enum type,
957 lang_statement_list_type *list)
959 lang_statement_union_type *new_stmt;
961 new_stmt = (lang_statement_union_type *) stat_alloc (size);
962 new_stmt->header.type = type;
963 new_stmt->header.next = NULL;
964 lang_statement_append (list, new_stmt, &new_stmt->header.next);
968 /* Build a new input file node for the language. There are several
969 ways in which we treat an input file, eg, we only look at symbols,
970 or prefix it with a -l etc.
972 We can be supplied with requests for input files more than once;
973 they may, for example be split over several lines like foo.o(.text)
974 foo.o(.data) etc, so when asked for a file we check that we haven't
975 got it already so we don't duplicate the bfd. */
977 static lang_input_statement_type *
978 new_afile (const char *name,
979 lang_input_file_enum_type file_type,
981 bfd_boolean add_to_list)
983 lang_input_statement_type *p;
986 p = (lang_input_statement_type *) new_stat (lang_input_statement, stat_ptr);
989 p = (lang_input_statement_type *)
990 stat_alloc (sizeof (lang_input_statement_type));
991 p->header.type = lang_input_statement_enum;
992 p->header.next = NULL;
995 lang_has_input_file = TRUE;
997 p->sysrooted = FALSE;
999 if (file_type == lang_input_file_is_l_enum
1000 && name[0] == ':' && name[1] != '\0')
1002 file_type = lang_input_file_is_search_file_enum;
1008 case lang_input_file_is_symbols_only_enum:
1010 p->is_archive = FALSE;
1012 p->local_sym_name = name;
1013 p->just_syms_flag = TRUE;
1014 p->search_dirs_flag = FALSE;
1016 case lang_input_file_is_fake_enum:
1018 p->is_archive = FALSE;
1020 p->local_sym_name = name;
1021 p->just_syms_flag = FALSE;
1022 p->search_dirs_flag = FALSE;
1024 case lang_input_file_is_l_enum:
1025 p->is_archive = TRUE;
1028 p->local_sym_name = concat ("-l", name, (const char *) NULL);
1029 p->just_syms_flag = FALSE;
1030 p->search_dirs_flag = TRUE;
1032 case lang_input_file_is_marker_enum:
1034 p->is_archive = FALSE;
1036 p->local_sym_name = name;
1037 p->just_syms_flag = FALSE;
1038 p->search_dirs_flag = TRUE;
1040 case lang_input_file_is_search_file_enum:
1041 p->sysrooted = ldlang_sysrooted_script;
1043 p->is_archive = FALSE;
1045 p->local_sym_name = name;
1046 p->just_syms_flag = FALSE;
1047 p->search_dirs_flag = TRUE;
1049 case lang_input_file_is_file_enum:
1051 p->is_archive = FALSE;
1053 p->local_sym_name = name;
1054 p->just_syms_flag = FALSE;
1055 p->search_dirs_flag = FALSE;
1061 p->next_real_file = NULL;
1063 p->dynamic = config.dynamic_link;
1064 p->add_DT_NEEDED_for_dynamic = add_DT_NEEDED_for_dynamic;
1065 p->add_DT_NEEDED_for_regular = add_DT_NEEDED_for_regular;
1066 p->whole_archive = whole_archive;
1068 p->missing_file = FALSE;
1070 lang_statement_append (&input_file_chain,
1071 (lang_statement_union_type *) p,
1072 &p->next_real_file);
1076 lang_input_statement_type *
1077 lang_add_input_file (const char *name,
1078 lang_input_file_enum_type file_type,
1081 return new_afile (name, file_type, target, TRUE);
1084 struct out_section_hash_entry
1086 struct bfd_hash_entry root;
1087 lang_statement_union_type s;
1090 /* The hash table. */
1092 static struct bfd_hash_table output_section_statement_table;
1094 /* Support routines for the hash table used by lang_output_section_find,
1095 initialize the table, fill in an entry and remove the table. */
1097 static struct bfd_hash_entry *
1098 output_section_statement_newfunc (struct bfd_hash_entry *entry,
1099 struct bfd_hash_table *table,
1102 lang_output_section_statement_type **nextp;
1103 struct out_section_hash_entry *ret;
1107 entry = (struct bfd_hash_entry *) bfd_hash_allocate (table,
1113 entry = bfd_hash_newfunc (entry, table, string);
1117 ret = (struct out_section_hash_entry *) entry;
1118 memset (&ret->s, 0, sizeof (ret->s));
1119 ret->s.header.type = lang_output_section_statement_enum;
1120 ret->s.output_section_statement.subsection_alignment = -1;
1121 ret->s.output_section_statement.section_alignment = -1;
1122 ret->s.output_section_statement.block_value = 1;
1123 lang_list_init (&ret->s.output_section_statement.children);
1124 lang_statement_append (stat_ptr, &ret->s, &ret->s.header.next);
1126 /* For every output section statement added to the list, except the
1127 first one, lang_output_section_statement.tail points to the "next"
1128 field of the last element of the list. */
1129 if (lang_output_section_statement.head != NULL)
1130 ret->s.output_section_statement.prev
1131 = ((lang_output_section_statement_type *)
1132 ((char *) lang_output_section_statement.tail
1133 - offsetof (lang_output_section_statement_type, next)));
1135 /* GCC's strict aliasing rules prevent us from just casting the
1136 address, so we store the pointer in a variable and cast that
1138 nextp = &ret->s.output_section_statement.next;
1139 lang_statement_append (&lang_output_section_statement,
1141 (lang_statement_union_type **) nextp);
1146 output_section_statement_table_init (void)
1148 if (!bfd_hash_table_init_n (&output_section_statement_table,
1149 output_section_statement_newfunc,
1150 sizeof (struct out_section_hash_entry),
1152 einfo (_("%P%F: can not create hash table: %E\n"));
1156 output_section_statement_table_free (void)
1158 bfd_hash_table_free (&output_section_statement_table);
1161 /* Build enough state so that the parser can build its tree. */
1166 obstack_begin (&stat_obstack, 1000);
1168 stat_ptr = &statement_list;
1170 output_section_statement_table_init ();
1172 lang_list_init (stat_ptr);
1174 lang_list_init (&input_file_chain);
1175 lang_list_init (&lang_output_section_statement);
1176 lang_list_init (&file_chain);
1177 first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum,
1179 abs_output_section =
1180 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME, 0, TRUE);
1182 abs_output_section->bfd_section = bfd_abs_section_ptr;
1184 /* The value "3" is ad-hoc, somewhat related to the expected number of
1185 DEFINED expressions in a linker script. For most default linker
1186 scripts, there are none. Why a hash table then? Well, it's somewhat
1187 simpler to re-use working machinery than using a linked list in terms
1188 of code-complexity here in ld, besides the initialization which just
1189 looks like other code here. */
1190 if (!bfd_hash_table_init_n (&lang_definedness_table,
1191 lang_definedness_newfunc,
1192 sizeof (struct lang_definedness_hash_entry),
1194 einfo (_("%P%F: can not create hash table: %E\n"));
1200 output_section_statement_table_free ();
1203 /*----------------------------------------------------------------------
1204 A region is an area of memory declared with the
1205 MEMORY { name:org=exp, len=exp ... }
1208 We maintain a list of all the regions here.
1210 If no regions are specified in the script, then the default is used
1211 which is created when looked up to be the entire data space.
1213 If create is true we are creating a region inside a MEMORY block.
1214 In this case it is probably an error to create a region that has
1215 already been created. If we are not inside a MEMORY block it is
1216 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
1217 and so we issue a warning.
1219 Each region has at least one name. The first name is either
1220 DEFAULT_MEMORY_REGION or the name given in the MEMORY block. You can add
1221 alias names to an existing region within a script with
1222 REGION_ALIAS (alias, region_name). Each name corresponds to at most one
1225 static lang_memory_region_type *lang_memory_region_list;
1226 static lang_memory_region_type **lang_memory_region_list_tail
1227 = &lang_memory_region_list;
1229 lang_memory_region_type *
1230 lang_memory_region_lookup (const char *const name, bfd_boolean create)
1232 lang_memory_region_name *n;
1233 lang_memory_region_type *r;
1234 lang_memory_region_type *new_region;
1236 /* NAME is NULL for LMA memspecs if no region was specified. */
1240 for (r = lang_memory_region_list; r != NULL; r = r->next)
1241 for (n = &r->name_list; n != NULL; n = n->next)
1242 if (strcmp (n->name, name) == 0)
1245 einfo (_("%P:%S: warning: redeclaration of memory region `%s'\n"),
1250 if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
1251 einfo (_("%P:%S: warning: memory region `%s' not declared\n"), name);
1253 new_region = (lang_memory_region_type *)
1254 stat_alloc (sizeof (lang_memory_region_type));
1256 new_region->name_list.name = xstrdup (name);
1257 new_region->name_list.next = NULL;
1258 new_region->next = NULL;
1259 new_region->origin = 0;
1260 new_region->length = ~(bfd_size_type) 0;
1261 new_region->current = 0;
1262 new_region->last_os = NULL;
1263 new_region->flags = 0;
1264 new_region->not_flags = 0;
1265 new_region->had_full_message = FALSE;
1267 *lang_memory_region_list_tail = new_region;
1268 lang_memory_region_list_tail = &new_region->next;
1274 lang_memory_region_alias (const char * alias, const char * region_name)
1276 lang_memory_region_name * n;
1277 lang_memory_region_type * r;
1278 lang_memory_region_type * region;
1280 /* The default region must be unique. This ensures that it is not necessary
1281 to iterate through the name list if someone wants the check if a region is
1282 the default memory region. */
1283 if (strcmp (region_name, DEFAULT_MEMORY_REGION) == 0
1284 || strcmp (alias, DEFAULT_MEMORY_REGION) == 0)
1285 einfo (_("%F%P:%S: error: alias for default memory region\n"));
1287 /* Look for the target region and check if the alias is not already
1290 for (r = lang_memory_region_list; r != NULL; r = r->next)
1291 for (n = &r->name_list; n != NULL; n = n->next)
1293 if (region == NULL && strcmp (n->name, region_name) == 0)
1295 if (strcmp (n->name, alias) == 0)
1296 einfo (_("%F%P:%S: error: redefinition of memory region "
1301 /* Check if the target region exists. */
1303 einfo (_("%F%P:%S: error: memory region `%s' "
1304 "for alias `%s' does not exist\n"),
1308 /* Add alias to region name list. */
1309 n = (lang_memory_region_name *) stat_alloc (sizeof (lang_memory_region_name));
1310 n->name = xstrdup (alias);
1311 n->next = region->name_list.next;
1312 region->name_list.next = n;
1315 static lang_memory_region_type *
1316 lang_memory_default (asection * section)
1318 lang_memory_region_type *p;
1320 flagword sec_flags = section->flags;
1322 /* Override SEC_DATA to mean a writable section. */
1323 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
1324 sec_flags |= SEC_DATA;
1326 for (p = lang_memory_region_list; p != NULL; p = p->next)
1328 if ((p->flags & sec_flags) != 0
1329 && (p->not_flags & sec_flags) == 0)
1334 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
1337 /* Find or create an output_section_statement with the given NAME.
1338 If CONSTRAINT is non-zero match one with that constraint, otherwise
1339 match any non-negative constraint. If CREATE, always make a
1340 new output_section_statement for SPECIAL CONSTRAINT. */
1342 lang_output_section_statement_type *
1343 lang_output_section_statement_lookup (const char *name,
1347 struct out_section_hash_entry *entry;
1349 entry = ((struct out_section_hash_entry *)
1350 bfd_hash_lookup (&output_section_statement_table, name,
1355 einfo (_("%P%F: failed creating section `%s': %E\n"), name);
1359 if (entry->s.output_section_statement.name != NULL)
1361 /* We have a section of this name, but it might not have the correct
1363 struct out_section_hash_entry *last_ent;
1365 name = entry->s.output_section_statement.name;
1366 if (create && constraint == SPECIAL)
1367 /* Not traversing to the end reverses the order of the second
1368 and subsequent SPECIAL sections in the hash table chain,
1369 but that shouldn't matter. */
1374 if (constraint == entry->s.output_section_statement.constraint
1376 && entry->s.output_section_statement.constraint >= 0))
1377 return &entry->s.output_section_statement;
1379 entry = (struct out_section_hash_entry *) entry->root.next;
1381 while (entry != NULL
1382 && name == entry->s.output_section_statement.name);
1388 = ((struct out_section_hash_entry *)
1389 output_section_statement_newfunc (NULL,
1390 &output_section_statement_table,
1394 einfo (_("%P%F: failed creating section `%s': %E\n"), name);
1397 entry->root = last_ent->root;
1398 last_ent->root.next = &entry->root;
1401 entry->s.output_section_statement.name = name;
1402 entry->s.output_section_statement.constraint = constraint;
1403 return &entry->s.output_section_statement;
1406 /* Find the next output_section_statement with the same name as OS.
1407 If CONSTRAINT is non-zero, find one with that constraint otherwise
1408 match any non-negative constraint. */
1410 lang_output_section_statement_type *
1411 next_matching_output_section_statement (lang_output_section_statement_type *os,
1414 /* All output_section_statements are actually part of a
1415 struct out_section_hash_entry. */
1416 struct out_section_hash_entry *entry = (struct out_section_hash_entry *)
1418 - offsetof (struct out_section_hash_entry, s.output_section_statement));
1419 const char *name = os->name;
1421 ASSERT (name == entry->root.string);
1424 entry = (struct out_section_hash_entry *) entry->root.next;
1426 || name != entry->s.output_section_statement.name)
1429 while (constraint != entry->s.output_section_statement.constraint
1431 || entry->s.output_section_statement.constraint < 0));
1433 return &entry->s.output_section_statement;
1436 /* A variant of lang_output_section_find used by place_orphan.
1437 Returns the output statement that should precede a new output
1438 statement for SEC. If an exact match is found on certain flags,
1441 lang_output_section_statement_type *
1442 lang_output_section_find_by_flags (const asection *sec,
1443 lang_output_section_statement_type **exact,
1444 lang_match_sec_type_func match_type)
1446 lang_output_section_statement_type *first, *look, *found;
1449 /* We know the first statement on this list is *ABS*. May as well
1451 first = &lang_output_section_statement.head->output_section_statement;
1452 first = first->next;
1454 /* First try for an exact match. */
1456 for (look = first; look; look = look->next)
1458 flags = look->flags;
1459 if (look->bfd_section != NULL)
1461 flags = look->bfd_section->flags;
1462 if (match_type && !match_type (link_info.output_bfd,
1467 flags ^= sec->flags;
1468 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY
1469 | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1479 if ((sec->flags & SEC_CODE) != 0
1480 && (sec->flags & SEC_ALLOC) != 0)
1482 /* Try for a rw code section. */
1483 for (look = first; look; look = look->next)
1485 flags = look->flags;
1486 if (look->bfd_section != NULL)
1488 flags = look->bfd_section->flags;
1489 if (match_type && !match_type (link_info.output_bfd,
1494 flags ^= sec->flags;
1495 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1496 | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1500 else if ((sec->flags & (SEC_READONLY | SEC_THREAD_LOCAL)) != 0
1501 && (sec->flags & SEC_ALLOC) != 0)
1503 /* .rodata can go after .text, .sdata2 after .rodata. */
1504 for (look = first; look; look = look->next)
1506 flags = look->flags;
1507 if (look->bfd_section != NULL)
1509 flags = look->bfd_section->flags;
1510 if (match_type && !match_type (link_info.output_bfd,
1515 flags ^= sec->flags;
1516 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1518 && !(look->flags & (SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1522 else if ((sec->flags & SEC_SMALL_DATA) != 0
1523 && (sec->flags & SEC_ALLOC) != 0)
1525 /* .sdata goes after .data, .sbss after .sdata. */
1526 for (look = first; look; look = look->next)
1528 flags = look->flags;
1529 if (look->bfd_section != NULL)
1531 flags = look->bfd_section->flags;
1532 if (match_type && !match_type (link_info.output_bfd,
1537 flags ^= sec->flags;
1538 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1539 | SEC_THREAD_LOCAL))
1540 || ((look->flags & SEC_SMALL_DATA)
1541 && !(sec->flags & SEC_HAS_CONTENTS)))
1545 else if ((sec->flags & SEC_HAS_CONTENTS) != 0
1546 && (sec->flags & SEC_ALLOC) != 0)
1548 /* .data goes after .rodata. */
1549 for (look = first; look; look = look->next)
1551 flags = look->flags;
1552 if (look->bfd_section != NULL)
1554 flags = look->bfd_section->flags;
1555 if (match_type && !match_type (link_info.output_bfd,
1560 flags ^= sec->flags;
1561 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1562 | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1566 else if ((sec->flags & SEC_ALLOC) != 0)
1568 /* .bss goes after any other alloc section. */
1569 for (look = first; look; look = look->next)
1571 flags = look->flags;
1572 if (look->bfd_section != NULL)
1574 flags = look->bfd_section->flags;
1575 if (match_type && !match_type (link_info.output_bfd,
1580 flags ^= sec->flags;
1581 if (!(flags & SEC_ALLOC))
1587 /* non-alloc go last. */
1588 for (look = first; look; look = look->next)
1590 flags = look->flags;
1591 if (look->bfd_section != NULL)
1592 flags = look->bfd_section->flags;
1593 flags ^= sec->flags;
1594 if (!(flags & SEC_DEBUGGING))
1600 if (found || !match_type)
1603 return lang_output_section_find_by_flags (sec, NULL, NULL);
1606 /* Find the last output section before given output statement.
1607 Used by place_orphan. */
1610 output_prev_sec_find (lang_output_section_statement_type *os)
1612 lang_output_section_statement_type *lookup;
1614 for (lookup = os->prev; lookup != NULL; lookup = lookup->prev)
1616 if (lookup->constraint < 0)
1619 if (lookup->bfd_section != NULL && lookup->bfd_section->owner != NULL)
1620 return lookup->bfd_section;
1626 /* Look for a suitable place for a new output section statement. The
1627 idea is to skip over anything that might be inside a SECTIONS {}
1628 statement in a script, before we find another output section
1629 statement. Assignments to "dot" before an output section statement
1630 are assumed to belong to it, except in two cases; The first
1631 assignment to dot, and assignments before non-alloc sections.
1632 Otherwise we might put an orphan before . = . + SIZEOF_HEADERS or
1633 similar assignments that set the initial address, or we might
1634 insert non-alloc note sections among assignments setting end of
1637 static lang_statement_union_type **
1638 insert_os_after (lang_output_section_statement_type *after)
1640 lang_statement_union_type **where;
1641 lang_statement_union_type **assign = NULL;
1642 bfd_boolean ignore_first;
1645 = after == &lang_output_section_statement.head->output_section_statement;
1647 for (where = &after->header.next;
1649 where = &(*where)->header.next)
1651 switch ((*where)->header.type)
1653 case lang_assignment_statement_enum:
1656 lang_assignment_statement_type *ass;
1658 ass = &(*where)->assignment_statement;
1659 if (ass->exp->type.node_class != etree_assert
1660 && ass->exp->assign.dst[0] == '.'
1661 && ass->exp->assign.dst[1] == 0
1665 ignore_first = FALSE;
1667 case lang_wild_statement_enum:
1668 case lang_input_section_enum:
1669 case lang_object_symbols_statement_enum:
1670 case lang_fill_statement_enum:
1671 case lang_data_statement_enum:
1672 case lang_reloc_statement_enum:
1673 case lang_padding_statement_enum:
1674 case lang_constructors_statement_enum:
1677 case lang_output_section_statement_enum:
1680 asection *s = (*where)->output_section_statement.bfd_section;
1683 || s->map_head.s == NULL
1684 || (s->flags & SEC_ALLOC) != 0)
1688 case lang_input_statement_enum:
1689 case lang_address_statement_enum:
1690 case lang_target_statement_enum:
1691 case lang_output_statement_enum:
1692 case lang_group_statement_enum:
1693 case lang_insert_statement_enum:
1702 lang_output_section_statement_type *
1703 lang_insert_orphan (asection *s,
1704 const char *secname,
1706 lang_output_section_statement_type *after,
1707 struct orphan_save *place,
1708 etree_type *address,
1709 lang_statement_list_type *add_child)
1711 lang_statement_list_type add;
1713 lang_output_section_statement_type *os;
1714 lang_output_section_statement_type **os_tail;
1716 /* If we have found an appropriate place for the output section
1717 statements for this orphan, add them to our own private list,
1718 inserting them later into the global statement list. */
1721 lang_list_init (&add);
1722 push_stat_ptr (&add);
1725 if (link_info.relocatable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
1726 address = exp_intop (0);
1728 os_tail = ((lang_output_section_statement_type **)
1729 lang_output_section_statement.tail);
1730 os = lang_enter_output_section_statement (secname, address, normal_section,
1731 NULL, NULL, NULL, constraint);
1734 if (config.build_constructors && *os_tail == os)
1736 /* If the name of the section is representable in C, then create
1737 symbols to mark the start and the end of the section. */
1738 for (ps = secname; *ps != '\0'; ps++)
1739 if (! ISALNUM ((unsigned char) *ps) && *ps != '_')
1744 etree_type *e_align;
1746 symname = (char *) xmalloc (ps - secname + sizeof "__start_" + 1);
1747 symname[0] = bfd_get_symbol_leading_char (link_info.output_bfd);
1748 sprintf (symname + (symname[0] != 0), "__start_%s", secname);
1749 e_align = exp_unop (ALIGN_K,
1750 exp_intop ((bfd_vma) 1 << s->alignment_power));
1751 lang_add_assignment (exp_assop ('=', ".", e_align));
1752 lang_add_assignment (exp_provide (symname,
1754 exp_nameop (NAME, ".")),
1759 if (add_child == NULL)
1760 add_child = &os->children;
1761 lang_add_section (add_child, s, os);
1763 lang_leave_output_section_statement (0, "*default*", NULL, NULL);
1765 if (ps != NULL && *ps == '\0')
1769 symname = (char *) xmalloc (ps - secname + sizeof "__stop_" + 1);
1770 symname[0] = bfd_get_symbol_leading_char (link_info.output_bfd);
1771 sprintf (symname + (symname[0] != 0), "__stop_%s", secname);
1772 lang_add_assignment (exp_provide (symname,
1773 exp_nameop (NAME, "."),
1777 /* Restore the global list pointer. */
1781 if (after != NULL && os->bfd_section != NULL)
1783 asection *snew, *as;
1785 snew = os->bfd_section;
1787 /* Shuffle the bfd section list to make the output file look
1788 neater. This is really only cosmetic. */
1789 if (place->section == NULL
1790 && after != (&lang_output_section_statement.head
1791 ->output_section_statement))
1793 asection *bfd_section = after->bfd_section;
1795 /* If the output statement hasn't been used to place any input
1796 sections (and thus doesn't have an output bfd_section),
1797 look for the closest prior output statement having an
1799 if (bfd_section == NULL)
1800 bfd_section = output_prev_sec_find (after);
1802 if (bfd_section != NULL && bfd_section != snew)
1803 place->section = &bfd_section->next;
1806 if (place->section == NULL)
1807 place->section = &link_info.output_bfd->sections;
1809 as = *place->section;
1813 /* Put the section at the end of the list. */
1815 /* Unlink the section. */
1816 bfd_section_list_remove (link_info.output_bfd, snew);
1818 /* Now tack it back on in the right place. */
1819 bfd_section_list_append (link_info.output_bfd, snew);
1821 else if (as != snew && as->prev != snew)
1823 /* Unlink the section. */
1824 bfd_section_list_remove (link_info.output_bfd, snew);
1826 /* Now tack it back on in the right place. */
1827 bfd_section_list_insert_before (link_info.output_bfd, as, snew);
1830 /* Save the end of this list. Further ophans of this type will
1831 follow the one we've just added. */
1832 place->section = &snew->next;
1834 /* The following is non-cosmetic. We try to put the output
1835 statements in some sort of reasonable order here, because they
1836 determine the final load addresses of the orphan sections.
1837 In addition, placing output statements in the wrong order may
1838 require extra segments. For instance, given a typical
1839 situation of all read-only sections placed in one segment and
1840 following that a segment containing all the read-write
1841 sections, we wouldn't want to place an orphan read/write
1842 section before or amongst the read-only ones. */
1843 if (add.head != NULL)
1845 lang_output_section_statement_type *newly_added_os;
1847 if (place->stmt == NULL)
1849 lang_statement_union_type **where = insert_os_after (after);
1854 place->os_tail = &after->next;
1858 /* Put it after the last orphan statement we added. */
1859 *add.tail = *place->stmt;
1860 *place->stmt = add.head;
1863 /* Fix the global list pointer if we happened to tack our
1864 new list at the tail. */
1865 if (*stat_ptr->tail == add.head)
1866 stat_ptr->tail = add.tail;
1868 /* Save the end of this list. */
1869 place->stmt = add.tail;
1871 /* Do the same for the list of output section statements. */
1872 newly_added_os = *os_tail;
1874 newly_added_os->prev = (lang_output_section_statement_type *)
1875 ((char *) place->os_tail
1876 - offsetof (lang_output_section_statement_type, next));
1877 newly_added_os->next = *place->os_tail;
1878 if (newly_added_os->next != NULL)
1879 newly_added_os->next->prev = newly_added_os;
1880 *place->os_tail = newly_added_os;
1881 place->os_tail = &newly_added_os->next;
1883 /* Fixing the global list pointer here is a little different.
1884 We added to the list in lang_enter_output_section_statement,
1885 trimmed off the new output_section_statment above when
1886 assigning *os_tail = NULL, but possibly added it back in
1887 the same place when assigning *place->os_tail. */
1888 if (*os_tail == NULL)
1889 lang_output_section_statement.tail
1890 = (lang_statement_union_type **) os_tail;
1897 lang_map_flags (flagword flag)
1899 if (flag & SEC_ALLOC)
1902 if (flag & SEC_CODE)
1905 if (flag & SEC_READONLY)
1908 if (flag & SEC_DATA)
1911 if (flag & SEC_LOAD)
1918 lang_memory_region_type *m;
1919 bfd_boolean dis_header_printed = FALSE;
1922 LANG_FOR_EACH_INPUT_STATEMENT (file)
1926 if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0
1927 || file->just_syms_flag)
1930 for (s = file->the_bfd->sections; s != NULL; s = s->next)
1931 if ((s->output_section == NULL
1932 || s->output_section->owner != link_info.output_bfd)
1933 && (s->flags & (SEC_LINKER_CREATED | SEC_KEEP)) == 0)
1935 if (! dis_header_printed)
1937 fprintf (config.map_file, _("\nDiscarded input sections\n\n"));
1938 dis_header_printed = TRUE;
1941 print_input_section (s, TRUE);
1945 minfo (_("\nMemory Configuration\n\n"));
1946 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
1947 _("Name"), _("Origin"), _("Length"), _("Attributes"));
1949 for (m = lang_memory_region_list; m != NULL; m = m->next)
1954 fprintf (config.map_file, "%-16s ", m->name_list.name);
1956 sprintf_vma (buf, m->origin);
1957 minfo ("0x%s ", buf);
1965 minfo ("0x%V", m->length);
1966 if (m->flags || m->not_flags)
1974 lang_map_flags (m->flags);
1980 lang_map_flags (m->not_flags);
1987 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
1989 if (! link_info.reduce_memory_overheads)
1991 obstack_begin (&map_obstack, 1000);
1992 for (p = link_info.input_bfds; p != (bfd *) NULL; p = p->link_next)
1993 bfd_map_over_sections (p, init_map_userdata, 0);
1994 bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0);
1996 lang_statement_iteration ++;
1997 print_statements ();
2001 init_map_userdata (bfd *abfd ATTRIBUTE_UNUSED,
2003 void *data ATTRIBUTE_UNUSED)
2005 fat_section_userdata_type *new_data
2006 = ((fat_section_userdata_type *) (stat_alloc
2007 (sizeof (fat_section_userdata_type))));
2009 ASSERT (get_userdata (sec) == NULL);
2010 get_userdata (sec) = new_data;
2011 new_data->map_symbol_def_tail = &new_data->map_symbol_def_head;
2012 new_data->map_symbol_def_count = 0;
2016 sort_def_symbol (struct bfd_link_hash_entry *hash_entry,
2017 void *info ATTRIBUTE_UNUSED)
2019 if (hash_entry->type == bfd_link_hash_defined
2020 || hash_entry->type == bfd_link_hash_defweak)
2022 struct fat_user_section_struct *ud;
2023 struct map_symbol_def *def;
2025 ud = (struct fat_user_section_struct *)
2026 get_userdata (hash_entry->u.def.section);
2029 /* ??? What do we have to do to initialize this beforehand? */
2030 /* The first time we get here is bfd_abs_section... */
2031 init_map_userdata (0, hash_entry->u.def.section, 0);
2032 ud = (struct fat_user_section_struct *)
2033 get_userdata (hash_entry->u.def.section);
2035 else if (!ud->map_symbol_def_tail)
2036 ud->map_symbol_def_tail = &ud->map_symbol_def_head;
2038 def = (struct map_symbol_def *) obstack_alloc (&map_obstack, sizeof *def);
2039 def->entry = hash_entry;
2040 *(ud->map_symbol_def_tail) = def;
2041 ud->map_symbol_def_tail = &def->next;
2042 ud->map_symbol_def_count++;
2047 /* Initialize an output section. */
2050 init_os (lang_output_section_statement_type *s, asection *isec, flagword flags)
2052 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
2053 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
2055 if (s->constraint != SPECIAL)
2056 s->bfd_section = bfd_get_section_by_name (link_info.output_bfd, s->name);
2057 if (s->bfd_section == NULL)
2058 s->bfd_section = bfd_make_section_anyway_with_flags (link_info.output_bfd,
2060 if (s->bfd_section == NULL)
2062 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
2063 link_info.output_bfd->xvec->name, s->name);
2065 s->bfd_section->output_section = s->bfd_section;
2066 s->bfd_section->output_offset = 0;
2068 if (!link_info.reduce_memory_overheads)
2070 fat_section_userdata_type *new_userdata = (fat_section_userdata_type *)
2071 stat_alloc (sizeof (fat_section_userdata_type));
2072 memset (new_userdata, 0, sizeof (fat_section_userdata_type));
2073 get_userdata (s->bfd_section) = new_userdata;
2076 /* If supplied an alignment, set it. */
2077 if (s->section_alignment != -1)
2078 s->bfd_section->alignment_power = s->section_alignment;
2081 bfd_init_private_section_data (isec->owner, isec,
2082 link_info.output_bfd, s->bfd_section,
2086 /* Make sure that all output sections mentioned in an expression are
2090 exp_init_os (etree_type *exp)
2092 switch (exp->type.node_class)
2096 exp_init_os (exp->assign.src);
2100 exp_init_os (exp->binary.lhs);
2101 exp_init_os (exp->binary.rhs);
2105 exp_init_os (exp->trinary.cond);
2106 exp_init_os (exp->trinary.lhs);
2107 exp_init_os (exp->trinary.rhs);
2111 exp_init_os (exp->assert_s.child);
2115 exp_init_os (exp->unary.child);
2119 switch (exp->type.node_code)
2125 lang_output_section_statement_type *os;
2127 os = lang_output_section_find (exp->name.name);
2128 if (os != NULL && os->bfd_section == NULL)
2129 init_os (os, NULL, 0);
2140 section_already_linked (bfd *abfd, asection *sec, void *data)
2142 lang_input_statement_type *entry = (lang_input_statement_type *) data;
2144 /* If we are only reading symbols from this object, then we want to
2145 discard all sections. */
2146 if (entry->just_syms_flag)
2148 bfd_link_just_syms (abfd, sec, &link_info);
2152 if (!(abfd->flags & DYNAMIC))
2153 bfd_section_already_linked (abfd, sec, &link_info);
2156 /* The wild routines.
2158 These expand statements like *(.text) and foo.o to a list of
2159 explicit actions, like foo.o(.text), bar.o(.text) and
2160 foo.o(.text, .data). */
2162 /* Add SECTION to the output section OUTPUT. Do this by creating a
2163 lang_input_section statement which is placed at PTR. FILE is the
2164 input file which holds SECTION. */
2167 lang_add_section (lang_statement_list_type *ptr,
2169 lang_output_section_statement_type *output)
2171 flagword flags = section->flags;
2172 bfd_boolean discard;
2174 /* Discard sections marked with SEC_EXCLUDE. */
2175 discard = (flags & SEC_EXCLUDE) != 0;
2177 /* Discard input sections which are assigned to a section named
2178 DISCARD_SECTION_NAME. */
2179 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
2182 /* Discard debugging sections if we are stripping debugging
2184 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
2185 && (flags & SEC_DEBUGGING) != 0)
2190 if (section->output_section == NULL)
2192 /* This prevents future calls from assigning this section. */
2193 section->output_section = bfd_abs_section_ptr;
2198 if (section->output_section == NULL)
2201 lang_input_section_type *new_section;
2203 /* We don't copy the SEC_NEVER_LOAD flag from an input section
2204 to an output section, because we want to be able to include a
2205 SEC_NEVER_LOAD section in the middle of an otherwise loaded
2206 section (I don't know why we want to do this, but we do).
2207 build_link_order in ldwrite.c handles this case by turning
2208 the embedded SEC_NEVER_LOAD section into a fill. */
2209 flags &= ~ SEC_NEVER_LOAD;
2211 switch (output->sectype)
2213 case normal_section:
2214 case overlay_section:
2216 case noalloc_section:
2217 flags &= ~SEC_ALLOC;
2219 case noload_section:
2221 flags |= SEC_NEVER_LOAD;
2225 if (output->bfd_section == NULL)
2226 init_os (output, section, flags);
2228 first = ! output->bfd_section->linker_has_input;
2229 output->bfd_section->linker_has_input = 1;
2231 if (!link_info.relocatable
2232 && !stripped_excluded_sections)
2234 asection *s = output->bfd_section->map_tail.s;
2235 output->bfd_section->map_tail.s = section;
2236 section->map_head.s = NULL;
2237 section->map_tail.s = s;
2239 s->map_head.s = section;
2241 output->bfd_section->map_head.s = section;
2244 /* Add a section reference to the list. */
2245 new_section = new_stat (lang_input_section, ptr);
2247 new_section->section = section;
2248 section->output_section = output->bfd_section;
2250 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
2251 already been processed. One reason to do this is that on pe
2252 format targets, .text$foo sections go into .text and it's odd
2253 to see .text with SEC_LINK_ONCE set. */
2255 if (! link_info.relocatable)
2256 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
2258 /* If this is not the first input section, and the SEC_READONLY
2259 flag is not currently set, then don't set it just because the
2260 input section has it set. */
2262 if (! first && (output->bfd_section->flags & SEC_READONLY) == 0)
2263 flags &= ~ SEC_READONLY;
2265 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
2267 && ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS))
2268 != (flags & (SEC_MERGE | SEC_STRINGS))
2269 || ((flags & SEC_MERGE)
2270 && output->bfd_section->entsize != section->entsize)))
2272 output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
2273 flags &= ~ (SEC_MERGE | SEC_STRINGS);
2276 output->bfd_section->flags |= flags;
2278 if (flags & SEC_MERGE)
2279 output->bfd_section->entsize = section->entsize;
2281 /* If SEC_READONLY is not set in the input section, then clear
2282 it from the output section. */
2283 if ((section->flags & SEC_READONLY) == 0)
2284 output->bfd_section->flags &= ~SEC_READONLY;
2286 /* Copy over SEC_SMALL_DATA. */
2287 if (section->flags & SEC_SMALL_DATA)
2288 output->bfd_section->flags |= SEC_SMALL_DATA;
2290 if (section->alignment_power > output->bfd_section->alignment_power)
2291 output->bfd_section->alignment_power = section->alignment_power;
2293 if (bfd_get_arch (section->owner) == bfd_arch_tic54x
2294 && (section->flags & SEC_TIC54X_BLOCK) != 0)
2296 output->bfd_section->flags |= SEC_TIC54X_BLOCK;
2297 /* FIXME: This value should really be obtained from the bfd... */
2298 output->block_value = 128;
2303 /* Handle wildcard sorting. This returns the lang_input_section which
2304 should follow the one we are going to create for SECTION and FILE,
2305 based on the sorting requirements of WILD. It returns NULL if the
2306 new section should just go at the end of the current list. */
2308 static lang_statement_union_type *
2309 wild_sort (lang_wild_statement_type *wild,
2310 struct wildcard_list *sec,
2311 lang_input_statement_type *file,
2314 const char *section_name;
2315 lang_statement_union_type *l;
2317 if (!wild->filenames_sorted
2318 && (sec == NULL || sec->spec.sorted == none))
2321 section_name = bfd_get_section_name (file->the_bfd, section);
2322 for (l = wild->children.head; l != NULL; l = l->header.next)
2324 lang_input_section_type *ls;
2326 if (l->header.type != lang_input_section_enum)
2328 ls = &l->input_section;
2330 /* Sorting by filename takes precedence over sorting by section
2333 if (wild->filenames_sorted)
2335 const char *fn, *ln;
2339 /* The PE support for the .idata section as generated by
2340 dlltool assumes that files will be sorted by the name of
2341 the archive and then the name of the file within the
2344 if (file->the_bfd != NULL
2345 && bfd_my_archive (file->the_bfd) != NULL)
2347 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
2352 fn = file->filename;
2356 if (bfd_my_archive (ls->section->owner) != NULL)
2358 ln = bfd_get_filename (bfd_my_archive (ls->section->owner));
2363 ln = ls->section->owner->filename;
2367 i = strcmp (fn, ln);
2376 fn = file->filename;
2378 ln = ls->section->owner->filename;
2380 i = strcmp (fn, ln);
2388 /* Here either the files are not sorted by name, or we are
2389 looking at the sections for this file. */
2391 if (sec != NULL && sec->spec.sorted != none)
2392 if (compare_section (sec->spec.sorted, section, ls->section) < 0)
2399 /* Expand a wild statement for a particular FILE. SECTION may be
2400 NULL, in which case it is a wild card. */
2403 output_section_callback (lang_wild_statement_type *ptr,
2404 struct wildcard_list *sec,
2406 lang_input_statement_type *file,
2409 lang_statement_union_type *before;
2410 lang_output_section_statement_type *os;
2412 os = (lang_output_section_statement_type *) output;
2414 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2415 if (unique_section_p (section, os))
2418 before = wild_sort (ptr, sec, file, section);
2420 /* Here BEFORE points to the lang_input_section which
2421 should follow the one we are about to add. If BEFORE
2422 is NULL, then the section should just go at the end
2423 of the current list. */
2426 lang_add_section (&ptr->children, section, os);
2429 lang_statement_list_type list;
2430 lang_statement_union_type **pp;
2432 lang_list_init (&list);
2433 lang_add_section (&list, section, os);
2435 /* If we are discarding the section, LIST.HEAD will
2437 if (list.head != NULL)
2439 ASSERT (list.head->header.next == NULL);
2441 for (pp = &ptr->children.head;
2443 pp = &(*pp)->header.next)
2444 ASSERT (*pp != NULL);
2446 list.head->header.next = *pp;
2452 /* Check if all sections in a wild statement for a particular FILE
2456 check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
2457 struct wildcard_list *sec ATTRIBUTE_UNUSED,
2459 lang_input_statement_type *file ATTRIBUTE_UNUSED,
2462 lang_output_section_statement_type *os;
2464 os = (lang_output_section_statement_type *) output;
2466 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2467 if (unique_section_p (section, os))
2470 if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0)
2471 os->all_input_readonly = FALSE;
2474 /* This is passed a file name which must have been seen already and
2475 added to the statement tree. We will see if it has been opened
2476 already and had its symbols read. If not then we'll read it. */
2478 static lang_input_statement_type *
2479 lookup_name (const char *name)
2481 lang_input_statement_type *search;
2483 for (search = (lang_input_statement_type *) input_file_chain.head;
2485 search = (lang_input_statement_type *) search->next_real_file)
2487 /* Use the local_sym_name as the name of the file that has
2488 already been loaded as filename might have been transformed
2489 via the search directory lookup mechanism. */
2490 const char *filename = search->local_sym_name;
2492 if (filename != NULL
2493 && strcmp (filename, name) == 0)
2498 search = new_afile (name, lang_input_file_is_search_file_enum,
2499 default_target, FALSE);
2501 /* If we have already added this file, or this file is not real
2502 don't add this file. */
2503 if (search->loaded || !search->real)
2506 if (! load_symbols (search, NULL))
2512 /* Save LIST as a list of libraries whose symbols should not be exported. */
2517 struct excluded_lib *next;
2519 static struct excluded_lib *excluded_libs;
2522 add_excluded_libs (const char *list)
2524 const char *p = list, *end;
2528 struct excluded_lib *entry;
2529 end = strpbrk (p, ",:");
2531 end = p + strlen (p);
2532 entry = (struct excluded_lib *) xmalloc (sizeof (*entry));
2533 entry->next = excluded_libs;
2534 entry->name = (char *) xmalloc (end - p + 1);
2535 memcpy (entry->name, p, end - p);
2536 entry->name[end - p] = '\0';
2537 excluded_libs = entry;
2545 check_excluded_libs (bfd *abfd)
2547 struct excluded_lib *lib = excluded_libs;
2551 int len = strlen (lib->name);
2552 const char *filename = lbasename (abfd->filename);
2554 if (strcmp (lib->name, "ALL") == 0)
2556 abfd->no_export = TRUE;
2560 if (strncmp (lib->name, filename, len) == 0
2561 && (filename[len] == '\0'
2562 || (filename[len] == '.' && filename[len + 1] == 'a'
2563 && filename[len + 2] == '\0')))
2565 abfd->no_export = TRUE;
2573 /* Get the symbols for an input file. */
2576 load_symbols (lang_input_statement_type *entry,
2577 lang_statement_list_type *place)
2584 ldfile_open_file (entry);
2586 /* Do not process further if the file was missing. */
2587 if (entry->missing_file)
2590 if (! bfd_check_format (entry->the_bfd, bfd_archive)
2591 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
2594 bfd_boolean save_ldlang_sysrooted_script;
2595 bfd_boolean save_add_DT_NEEDED_for_regular;
2596 bfd_boolean save_add_DT_NEEDED_for_dynamic;
2597 bfd_boolean save_whole_archive;
2599 err = bfd_get_error ();
2601 /* See if the emulation has some special knowledge. */
2602 if (ldemul_unrecognized_file (entry))
2605 if (err == bfd_error_file_ambiguously_recognized)
2609 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
2610 einfo (_("%B: matching formats:"), entry->the_bfd);
2611 for (p = matching; *p != NULL; p++)
2615 else if (err != bfd_error_file_not_recognized
2617 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
2619 bfd_close (entry->the_bfd);
2620 entry->the_bfd = NULL;
2622 /* Try to interpret the file as a linker script. */
2623 ldfile_open_command_file (entry->filename);
2625 push_stat_ptr (place);
2626 save_ldlang_sysrooted_script = ldlang_sysrooted_script;
2627 ldlang_sysrooted_script = entry->sysrooted;
2628 save_add_DT_NEEDED_for_regular = add_DT_NEEDED_for_regular;
2629 add_DT_NEEDED_for_regular = entry->add_DT_NEEDED_for_regular;
2630 save_add_DT_NEEDED_for_dynamic = add_DT_NEEDED_for_dynamic;
2631 add_DT_NEEDED_for_dynamic = entry->add_DT_NEEDED_for_dynamic;
2632 save_whole_archive = whole_archive;
2633 whole_archive = entry->whole_archive;
2635 ldfile_assumed_script = TRUE;
2636 parser_input = input_script;
2637 /* We want to use the same -Bdynamic/-Bstatic as the one for
2639 config.dynamic_link = entry->dynamic;
2641 ldfile_assumed_script = FALSE;
2643 ldlang_sysrooted_script = save_ldlang_sysrooted_script;
2644 add_DT_NEEDED_for_regular = save_add_DT_NEEDED_for_regular;
2645 add_DT_NEEDED_for_dynamic = save_add_DT_NEEDED_for_dynamic;
2646 whole_archive = save_whole_archive;
2652 if (ldemul_recognized_file (entry))
2655 /* We don't call ldlang_add_file for an archive. Instead, the
2656 add_symbols entry point will call ldlang_add_file, via the
2657 add_archive_element callback, for each element of the archive
2659 switch (bfd_get_format (entry->the_bfd))
2665 ldlang_add_file (entry);
2666 if (trace_files || trace_file_tries)
2667 info_msg ("%I\n", entry);
2671 check_excluded_libs (entry->the_bfd);
2673 if (entry->whole_archive)
2676 bfd_boolean loaded = TRUE;
2680 member = bfd_openr_next_archived_file (entry->the_bfd, member);
2685 if (! bfd_check_format (member, bfd_object))
2687 einfo (_("%F%B: member %B in archive is not an object\n"),
2688 entry->the_bfd, member);
2692 if (! ((*link_info.callbacks->add_archive_element)
2693 (&link_info, member, "--whole-archive")))
2696 if (! bfd_link_add_symbols (member, &link_info))
2698 einfo (_("%F%B: could not read symbols: %E\n"), member);
2703 entry->loaded = loaded;
2709 if (bfd_link_add_symbols (entry->the_bfd, &link_info))
2710 entry->loaded = TRUE;
2712 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
2714 return entry->loaded;
2717 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
2718 may be NULL, indicating that it is a wildcard. Separate
2719 lang_input_section statements are created for each part of the
2720 expansion; they are added after the wild statement S. OUTPUT is
2721 the output section. */
2724 wild (lang_wild_statement_type *s,
2725 const char *target ATTRIBUTE_UNUSED,
2726 lang_output_section_statement_type *output)
2728 struct wildcard_list *sec;
2730 if (s->handler_data[0]
2731 && s->handler_data[0]->spec.sorted == by_name
2732 && !s->filenames_sorted)
2734 lang_section_bst_type *tree;
2736 walk_wild (s, output_section_callback_fast, output);
2741 output_section_callback_tree_to_list (s, tree, output);
2746 walk_wild (s, output_section_callback, output);
2748 if (default_common_section == NULL)
2749 for (sec = s->section_list; sec != NULL; sec = sec->next)
2750 if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
2752 /* Remember the section that common is going to in case we
2753 later get something which doesn't know where to put it. */
2754 default_common_section = output;
2759 /* Return TRUE iff target is the sought target. */
2762 get_target (const bfd_target *target, void *data)
2764 const char *sought = (const char *) data;
2766 return strcmp (target->name, sought) == 0;
2769 /* Like strcpy() but convert to lower case as well. */
2772 stricpy (char *dest, char *src)
2776 while ((c = *src++) != 0)
2777 *dest++ = TOLOWER (c);
2782 /* Remove the first occurrence of needle (if any) in haystack
2786 strcut (char *haystack, char *needle)
2788 haystack = strstr (haystack, needle);
2794 for (src = haystack + strlen (needle); *src;)
2795 *haystack++ = *src++;
2801 /* Compare two target format name strings.
2802 Return a value indicating how "similar" they are. */
2805 name_compare (char *first, char *second)
2811 copy1 = (char *) xmalloc (strlen (first) + 1);
2812 copy2 = (char *) xmalloc (strlen (second) + 1);
2814 /* Convert the names to lower case. */
2815 stricpy (copy1, first);
2816 stricpy (copy2, second);
2818 /* Remove size and endian strings from the name. */
2819 strcut (copy1, "big");
2820 strcut (copy1, "little");
2821 strcut (copy2, "big");
2822 strcut (copy2, "little");
2824 /* Return a value based on how many characters match,
2825 starting from the beginning. If both strings are
2826 the same then return 10 * their length. */
2827 for (result = 0; copy1[result] == copy2[result]; result++)
2828 if (copy1[result] == 0)
2840 /* Set by closest_target_match() below. */
2841 static const bfd_target *winner;
2843 /* Scan all the valid bfd targets looking for one that has the endianness
2844 requirement that was specified on the command line, and is the nearest
2845 match to the original output target. */
2848 closest_target_match (const bfd_target *target, void *data)
2850 const bfd_target *original = (const bfd_target *) data;
2852 if (command_line.endian == ENDIAN_BIG
2853 && target->byteorder != BFD_ENDIAN_BIG)
2856 if (command_line.endian == ENDIAN_LITTLE
2857 && target->byteorder != BFD_ENDIAN_LITTLE)
2860 /* Must be the same flavour. */
2861 if (target->flavour != original->flavour)
2864 /* Ignore generic big and little endian elf vectors. */
2865 if (strcmp (target->name, "elf32-big") == 0
2866 || strcmp (target->name, "elf64-big") == 0
2867 || strcmp (target->name, "elf32-little") == 0
2868 || strcmp (target->name, "elf64-little") == 0)
2871 /* If we have not found a potential winner yet, then record this one. */
2878 /* Oh dear, we now have two potential candidates for a successful match.
2879 Compare their names and choose the better one. */
2880 if (name_compare (target->name, original->name)
2881 > name_compare (winner->name, original->name))
2884 /* Keep on searching until wqe have checked them all. */
2888 /* Return the BFD target format of the first input file. */
2891 get_first_input_target (void)
2893 char *target = NULL;
2895 LANG_FOR_EACH_INPUT_STATEMENT (s)
2897 if (s->header.type == lang_input_statement_enum
2900 ldfile_open_file (s);
2902 if (s->the_bfd != NULL
2903 && bfd_check_format (s->the_bfd, bfd_object))
2905 target = bfd_get_target (s->the_bfd);
2917 lang_get_output_target (void)
2921 /* Has the user told us which output format to use? */
2922 if (output_target != NULL)
2923 return output_target;
2925 /* No - has the current target been set to something other than
2927 if (current_target != default_target)
2928 return current_target;
2930 /* No - can we determine the format of the first input file? */
2931 target = get_first_input_target ();
2935 /* Failed - use the default output target. */
2936 return default_target;
2939 /* Open the output file. */
2942 open_output (const char *name)
2944 output_target = lang_get_output_target ();
2946 /* Has the user requested a particular endianness on the command
2948 if (command_line.endian != ENDIAN_UNSET)
2950 const bfd_target *target;
2951 enum bfd_endian desired_endian;
2953 /* Get the chosen target. */
2954 target = bfd_search_for_target (get_target, (void *) output_target);
2956 /* If the target is not supported, we cannot do anything. */
2959 if (command_line.endian == ENDIAN_BIG)
2960 desired_endian = BFD_ENDIAN_BIG;
2962 desired_endian = BFD_ENDIAN_LITTLE;
2964 /* See if the target has the wrong endianness. This should
2965 not happen if the linker script has provided big and
2966 little endian alternatives, but some scrips don't do
2968 if (target->byteorder != desired_endian)
2970 /* If it does, then see if the target provides
2971 an alternative with the correct endianness. */
2972 if (target->alternative_target != NULL
2973 && (target->alternative_target->byteorder == desired_endian))
2974 output_target = target->alternative_target->name;
2977 /* Try to find a target as similar as possible to
2978 the default target, but which has the desired
2979 endian characteristic. */
2980 bfd_search_for_target (closest_target_match,
2983 /* Oh dear - we could not find any targets that
2984 satisfy our requirements. */
2986 einfo (_("%P: warning: could not find any targets"
2987 " that match endianness requirement\n"));
2989 output_target = winner->name;
2995 link_info.output_bfd = bfd_openw (name, output_target);
2997 if (link_info.output_bfd == NULL)
2999 if (bfd_get_error () == bfd_error_invalid_target)
3000 einfo (_("%P%F: target %s not found\n"), output_target);
3002 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
3005 delete_output_file_on_failure = TRUE;
3007 if (! bfd_set_format (link_info.output_bfd, bfd_object))
3008 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
3009 if (! bfd_set_arch_mach (link_info.output_bfd,
3010 ldfile_output_architecture,
3011 ldfile_output_machine))
3012 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
3014 link_info.hash = bfd_link_hash_table_create (link_info.output_bfd);
3015 if (link_info.hash == NULL)
3016 einfo (_("%P%F: can not create hash table: %E\n"));
3018 bfd_set_gp_size (link_info.output_bfd, g_switch_value);
3022 ldlang_open_output (lang_statement_union_type *statement)
3024 switch (statement->header.type)
3026 case lang_output_statement_enum:
3027 ASSERT (link_info.output_bfd == NULL);
3028 open_output (statement->output_statement.name);
3029 ldemul_set_output_arch ();
3030 if (config.magic_demand_paged && !link_info.relocatable)
3031 link_info.output_bfd->flags |= D_PAGED;
3033 link_info.output_bfd->flags &= ~D_PAGED;
3034 if (config.text_read_only)
3035 link_info.output_bfd->flags |= WP_TEXT;
3037 link_info.output_bfd->flags &= ~WP_TEXT;
3038 if (link_info.traditional_format)
3039 link_info.output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
3041 link_info.output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
3044 case lang_target_statement_enum:
3045 current_target = statement->target_statement.target;
3052 /* Convert between addresses in bytes and sizes in octets.
3053 For currently supported targets, octets_per_byte is always a power
3054 of two, so we can use shifts. */
3055 #define TO_ADDR(X) ((X) >> opb_shift)
3056 #define TO_SIZE(X) ((X) << opb_shift)
3058 /* Support the above. */
3059 static unsigned int opb_shift = 0;
3064 unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3065 ldfile_output_machine);
3068 while ((x & 1) == 0)
3076 /* Open all the input files. */
3079 open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
3081 for (; s != NULL; s = s->header.next)
3083 switch (s->header.type)
3085 case lang_constructors_statement_enum:
3086 open_input_bfds (constructor_list.head, force);
3088 case lang_output_section_statement_enum:
3089 open_input_bfds (s->output_section_statement.children.head, force);
3091 case lang_wild_statement_enum:
3092 /* Maybe we should load the file's symbols. */
3093 if (s->wild_statement.filename
3094 && !wildcardp (s->wild_statement.filename)
3095 && !archive_path (s->wild_statement.filename))
3096 lookup_name (s->wild_statement.filename);
3097 open_input_bfds (s->wild_statement.children.head, force);
3099 case lang_group_statement_enum:
3101 struct bfd_link_hash_entry *undefs;
3103 /* We must continually search the entries in the group
3104 until no new symbols are added to the list of undefined
3109 undefs = link_info.hash->undefs_tail;
3110 open_input_bfds (s->group_statement.children.head, TRUE);
3112 while (undefs != link_info.hash->undefs_tail);
3115 case lang_target_statement_enum:
3116 current_target = s->target_statement.target;
3118 case lang_input_statement_enum:
3119 if (s->input_statement.real)
3121 lang_statement_union_type **os_tail;
3122 lang_statement_list_type add;
3124 s->input_statement.target = current_target;
3126 /* If we are being called from within a group, and this
3127 is an archive which has already been searched, then
3128 force it to be researched unless the whole archive
3129 has been loaded already. */
3131 && !s->input_statement.whole_archive
3132 && s->input_statement.loaded
3133 && bfd_check_format (s->input_statement.the_bfd,
3135 s->input_statement.loaded = FALSE;
3137 os_tail = lang_output_section_statement.tail;
3138 lang_list_init (&add);
3140 if (! load_symbols (&s->input_statement, &add))
3141 config.make_executable = FALSE;
3143 if (add.head != NULL)
3145 /* If this was a script with output sections then
3146 tack any added statements on to the end of the
3147 list. This avoids having to reorder the output
3148 section statement list. Very likely the user
3149 forgot -T, and whatever we do here will not meet
3150 naive user expectations. */
3151 if (os_tail != lang_output_section_statement.tail)
3153 einfo (_("%P: warning: %s contains output sections;"
3154 " did you forget -T?\n"),
3155 s->input_statement.filename);
3156 *stat_ptr->tail = add.head;
3157 stat_ptr->tail = add.tail;
3161 *add.tail = s->header.next;
3162 s->header.next = add.head;
3172 /* Exit if any of the files were missing. */
3177 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
3180 lang_track_definedness (const char *name)
3182 if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL)
3183 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
3186 /* New-function for the definedness hash table. */
3188 static struct bfd_hash_entry *
3189 lang_definedness_newfunc (struct bfd_hash_entry *entry,
3190 struct bfd_hash_table *table ATTRIBUTE_UNUSED,
3191 const char *name ATTRIBUTE_UNUSED)
3193 struct lang_definedness_hash_entry *ret
3194 = (struct lang_definedness_hash_entry *) entry;
3197 ret = (struct lang_definedness_hash_entry *)
3198 bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry));
3201 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
3203 ret->iteration = -1;
3207 /* Return the iteration when the definition of NAME was last updated. A
3208 value of -1 means that the symbol is not defined in the linker script
3209 or the command line, but may be defined in the linker symbol table. */
3212 lang_symbol_definition_iteration (const char *name)
3214 struct lang_definedness_hash_entry *defentry
3215 = (struct lang_definedness_hash_entry *)
3216 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
3218 /* We've already created this one on the presence of DEFINED in the
3219 script, so it can't be NULL unless something is borked elsewhere in
3221 if (defentry == NULL)
3224 return defentry->iteration;
3227 /* Update the definedness state of NAME. */
3230 lang_update_definedness (const char *name, struct bfd_link_hash_entry *h)
3232 struct lang_definedness_hash_entry *defentry
3233 = (struct lang_definedness_hash_entry *)
3234 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
3236 /* We don't keep track of symbols not tested with DEFINED. */
3237 if (defentry == NULL)
3240 /* If the symbol was already defined, and not from an earlier statement
3241 iteration, don't update the definedness iteration, because that'd
3242 make the symbol seem defined in the linker script at this point, and
3243 it wasn't; it was defined in some object. If we do anyway, DEFINED
3244 would start to yield false before this point and the construct "sym =
3245 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
3247 if (h->type != bfd_link_hash_undefined
3248 && h->type != bfd_link_hash_common
3249 && h->type != bfd_link_hash_new
3250 && defentry->iteration == -1)
3253 defentry->iteration = lang_statement_iteration;
3256 /* Add the supplied name to the symbol table as an undefined reference.
3257 This is a two step process as the symbol table doesn't even exist at
3258 the time the ld command line is processed. First we put the name
3259 on a list, then, once the output file has been opened, transfer the
3260 name to the symbol table. */
3262 typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
3264 #define ldlang_undef_chain_list_head entry_symbol.next
3267 ldlang_add_undef (const char *const name)
3269 ldlang_undef_chain_list_type *new_undef = (ldlang_undef_chain_list_type *)
3270 stat_alloc (sizeof (ldlang_undef_chain_list_type));
3272 new_undef->next = ldlang_undef_chain_list_head;
3273 ldlang_undef_chain_list_head = new_undef;
3275 new_undef->name = xstrdup (name);
3277 if (link_info.output_bfd != NULL)
3278 insert_undefined (new_undef->name);
3281 /* Insert NAME as undefined in the symbol table. */
3284 insert_undefined (const char *name)
3286 struct bfd_link_hash_entry *h;
3288 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
3290 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
3291 if (h->type == bfd_link_hash_new)
3293 h->type = bfd_link_hash_undefined;
3294 h->u.undef.abfd = NULL;
3295 bfd_link_add_undef (link_info.hash, h);
3299 /* Run through the list of undefineds created above and place them
3300 into the linker hash table as undefined symbols belonging to the
3304 lang_place_undefineds (void)
3306 ldlang_undef_chain_list_type *ptr;
3308 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
3309 insert_undefined (ptr->name);
3312 /* Check for all readonly or some readwrite sections. */
3315 check_input_sections
3316 (lang_statement_union_type *s,
3317 lang_output_section_statement_type *output_section_statement)
3319 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
3321 switch (s->header.type)
3323 case lang_wild_statement_enum:
3324 walk_wild (&s->wild_statement, check_section_callback,
3325 output_section_statement);
3326 if (! output_section_statement->all_input_readonly)
3329 case lang_constructors_statement_enum:
3330 check_input_sections (constructor_list.head,
3331 output_section_statement);
3332 if (! output_section_statement->all_input_readonly)
3335 case lang_group_statement_enum:
3336 check_input_sections (s->group_statement.children.head,
3337 output_section_statement);
3338 if (! output_section_statement->all_input_readonly)
3347 /* Update wildcard statements if needed. */
3350 update_wild_statements (lang_statement_union_type *s)
3352 struct wildcard_list *sec;
3354 switch (sort_section)
3364 for (; s != NULL; s = s->header.next)
3366 switch (s->header.type)
3371 case lang_wild_statement_enum:
3372 sec = s->wild_statement.section_list;
3373 for (sec = s->wild_statement.section_list; sec != NULL;
3376 switch (sec->spec.sorted)
3379 sec->spec.sorted = sort_section;
3382 if (sort_section == by_alignment)
3383 sec->spec.sorted = by_name_alignment;
3386 if (sort_section == by_name)
3387 sec->spec.sorted = by_alignment_name;
3395 case lang_constructors_statement_enum:
3396 update_wild_statements (constructor_list.head);
3399 case lang_output_section_statement_enum:
3400 update_wild_statements
3401 (s->output_section_statement.children.head);
3404 case lang_group_statement_enum:
3405 update_wild_statements (s->group_statement.children.head);
3413 /* Open input files and attach to output sections. */
3416 map_input_to_output_sections
3417 (lang_statement_union_type *s, const char *target,
3418 lang_output_section_statement_type *os)
3420 for (; s != NULL; s = s->header.next)
3422 lang_output_section_statement_type *tos;
3424 switch (s->header.type)
3426 case lang_wild_statement_enum:
3427 wild (&s->wild_statement, target, os);
3429 case lang_constructors_statement_enum:
3430 map_input_to_output_sections (constructor_list.head,
3434 case lang_output_section_statement_enum:
3435 tos = &s->output_section_statement;
3436 if (tos->constraint != 0)
3438 if (tos->constraint != ONLY_IF_RW
3439 && tos->constraint != ONLY_IF_RO)
3441 tos->all_input_readonly = TRUE;
3442 check_input_sections (tos->children.head, tos);
3443 if (tos->all_input_readonly != (tos->constraint == ONLY_IF_RO))
3445 tos->constraint = -1;
3449 map_input_to_output_sections (tos->children.head,
3453 case lang_target_statement_enum:
3454 target = s->target_statement.target;
3456 case lang_group_statement_enum:
3457 map_input_to_output_sections (s->group_statement.children.head,
3461 case lang_address_statement_enum:
3462 /* Mark the specified section with the supplied address.
3463 If this section was actually a segment marker, then the
3464 directive is ignored if the linker script explicitly
3465 processed the segment marker. Originally, the linker
3466 treated segment directives (like -Ttext on the
3467 command-line) as section directives. We honor the
3468 section directive semantics for backwards compatibilty;
3469 linker scripts that do not specifically check for
3470 SEGMENT_START automatically get the old semantics. */
3471 if (!s->address_statement.segment
3472 || !s->address_statement.segment->used)
3474 const char *name = s->address_statement.section_name;
3476 /* Create the output section statement here rather than
3477 in create_other_output_sections so that orphans with
3478 a set address will be placed after other script
3479 sections. If we let the orphan placement code place
3480 them in amongst other sections then the address will
3481 affect following script sections, which is likely to
3482 surprise naive users. */
3483 tos = lang_output_section_statement_lookup (name, 0, TRUE);
3484 tos->addr_tree = s->address_statement.address;
3487 case lang_output_statement_enum:
3488 case lang_data_statement_enum:
3489 case lang_input_section_enum:
3490 case lang_fill_statement_enum:
3491 case lang_object_symbols_statement_enum:
3492 case lang_reloc_statement_enum:
3493 case lang_padding_statement_enum:
3494 case lang_input_statement_enum:
3495 case lang_assignment_statement_enum:
3496 case lang_insert_statement_enum:
3502 /* Create any other output sections, such as needed for data statements
3503 or those referenced in expressions. */
3506 create_other_output_sections
3507 (lang_statement_union_type *s,
3508 lang_output_section_statement_type *os)
3510 lang_output_section_statement_type *tos;
3513 for (; s != NULL; s = s->header.next)
3515 switch (s->header.type)
3517 case lang_wild_statement_enum:
3519 case lang_constructors_statement_enum:
3520 create_other_output_sections (constructor_list.head, os);
3522 case lang_output_section_statement_enum:
3523 tos = &s->output_section_statement;
3524 if (tos->constraint != 0
3525 && tos->constraint != ONLY_IF_RW
3526 && tos->constraint != ONLY_IF_RO)
3528 create_other_output_sections (tos->children.head, tos);
3529 if (tos->bfd_section != NULL)
3531 /* If there is a base address, make sure that any sections
3532 it might mention are initialized. */
3533 if (tos->addr_tree != NULL)
3534 exp_init_os (tos->addr_tree);
3535 if (tos->load_base != NULL)
3536 exp_init_os (tos->load_base);
3539 case lang_output_statement_enum:
3540 case lang_target_statement_enum:
3542 case lang_group_statement_enum:
3543 create_other_output_sections (s->group_statement.children.head, os);
3545 case lang_data_statement_enum:
3546 /* Make sure that any sections mentioned in the expression
3548 exp_init_os (s->data_statement.exp);
3549 if (os->bfd_section == NULL)
3550 init_os (os, NULL, 0);
3551 flags = SEC_HAS_CONTENTS;
3552 /* The output section gets contents, and then we inspect for
3553 any flags set in the input script which override any ALLOC. */
3554 if (!(os->flags & SEC_NEVER_LOAD))
3555 flags |= SEC_ALLOC | SEC_LOAD;
3556 os->bfd_section->flags |= flags;
3558 case lang_input_section_enum:
3560 case lang_fill_statement_enum:
3561 case lang_object_symbols_statement_enum:
3562 case lang_reloc_statement_enum:
3563 case lang_padding_statement_enum:
3564 case lang_input_statement_enum:
3565 if (os != NULL && os->bfd_section == NULL)
3566 init_os (os, NULL, 0);
3568 case lang_assignment_statement_enum:
3569 if (os != NULL && os->bfd_section == NULL)
3570 init_os (os, NULL, 0);
3572 /* Make sure that any sections mentioned in the assignment
3574 exp_init_os (s->assignment_statement.exp);
3576 case lang_address_statement_enum:
3577 if (!s->address_statement.segment
3578 || !s->address_statement.segment->used)
3580 const char *name = s->address_statement.section_name;
3582 tos = lang_output_section_find (name);
3583 if (tos->bfd_section == NULL)
3584 init_os (tos, NULL, 0);
3587 case lang_insert_statement_enum:
3593 /* An insert statement snips out all the linker statements from the
3594 start of the list and places them after the output section
3595 statement specified by the insert. This operation is complicated
3596 by the fact that we keep a doubly linked list of output section
3597 statements as well as the singly linked list of all statements. */
3600 process_insert_statements (void)
3602 lang_statement_union_type **s;
3603 lang_output_section_statement_type *first_os = NULL;
3604 lang_output_section_statement_type *last_os = NULL;
3605 lang_output_section_statement_type *os;
3607 /* "start of list" is actually the statement immediately after
3608 the special abs_section output statement, so that it isn't
3610 s = &lang_output_section_statement.head;
3611 while (*(s = &(*s)->header.next) != NULL)
3613 if ((*s)->header.type == lang_output_section_statement_enum)
3615 /* Keep pointers to the first and last output section
3616 statement in the sequence we may be about to move. */
3617 os = &(*s)->output_section_statement;
3619 ASSERT (last_os == NULL || last_os->next == os);
3622 /* Set constraint negative so that lang_output_section_find
3623 won't match this output section statement. At this
3624 stage in linking constraint has values in the range
3625 [-1, ONLY_IN_RW]. */
3626 last_os->constraint = -2 - last_os->constraint;
3627 if (first_os == NULL)
3630 else if ((*s)->header.type == lang_insert_statement_enum)
3632 lang_insert_statement_type *i = &(*s)->insert_statement;
3633 lang_output_section_statement_type *where;
3634 lang_statement_union_type **ptr;
3635 lang_statement_union_type *first;
3637 where = lang_output_section_find (i->where);
3638 if (where != NULL && i->is_before)
3641 where = where->prev;
3642 while (where != NULL && where->constraint < 0);
3646 einfo (_("%F%P: %s not found for insert\n"), i->where);
3650 /* Deal with reordering the output section statement list. */
3651 if (last_os != NULL)
3653 asection *first_sec, *last_sec;
3654 struct lang_output_section_statement_struct **next;
3656 /* Snip out the output sections we are moving. */
3657 first_os->prev->next = last_os->next;
3658 if (last_os->next == NULL)
3660 next = &first_os->prev->next;
3661 lang_output_section_statement.tail
3662 = (lang_statement_union_type **) next;
3665 last_os->next->prev = first_os->prev;
3666 /* Add them in at the new position. */
3667 last_os->next = where->next;
3668 if (where->next == NULL)
3670 next = &last_os->next;
3671 lang_output_section_statement.tail
3672 = (lang_statement_union_type **) next;
3675 where->next->prev = last_os;
3676 first_os->prev = where;
3677 where->next = first_os;
3679 /* Move the bfd sections in the same way. */
3682 for (os = first_os; os != NULL; os = os->next)
3684 os->constraint = -2 - os->constraint;
3685 if (os->bfd_section != NULL
3686 && os->bfd_section->owner != NULL)
3688 last_sec = os->bfd_section;
3689 if (first_sec == NULL)
3690 first_sec = last_sec;
3695 if (last_sec != NULL)
3697 asection *sec = where->bfd_section;
3699 sec = output_prev_sec_find (where);
3701 /* The place we want to insert must come after the
3702 sections we are moving. So if we find no
3703 section or if the section is the same as our
3704 last section, then no move is needed. */
3705 if (sec != NULL && sec != last_sec)
3707 /* Trim them off. */
3708 if (first_sec->prev != NULL)
3709 first_sec->prev->next = last_sec->next;
3711 link_info.output_bfd->sections = last_sec->next;
3712 if (last_sec->next != NULL)
3713 last_sec->next->prev = first_sec->prev;
3715 link_info.output_bfd->section_last = first_sec->prev;
3717 last_sec->next = sec->next;
3718 if (sec->next != NULL)
3719 sec->next->prev = last_sec;
3721 link_info.output_bfd->section_last = last_sec;
3722 first_sec->prev = sec;
3723 sec->next = first_sec;
3731 ptr = insert_os_after (where);
3732 /* Snip everything after the abs_section output statement we
3733 know is at the start of the list, up to and including
3734 the insert statement we are currently processing. */
3735 first = lang_output_section_statement.head->header.next;
3736 lang_output_section_statement.head->header.next = (*s)->header.next;
3737 /* Add them back where they belong. */
3740 statement_list.tail = s;
3742 s = &lang_output_section_statement.head;
3746 /* Undo constraint twiddling. */
3747 for (os = first_os; os != NULL; os = os->next)
3749 os->constraint = -2 - os->constraint;
3755 /* An output section might have been removed after its statement was
3756 added. For example, ldemul_before_allocation can remove dynamic
3757 sections if they turn out to be not needed. Clean them up here. */
3760 strip_excluded_output_sections (void)
3762 lang_output_section_statement_type *os;
3764 /* Run lang_size_sections (if not already done). */
3765 if (expld.phase != lang_mark_phase_enum)
3767 expld.phase = lang_mark_phase_enum;
3768 expld.dataseg.phase = exp_dataseg_none;
3769 one_lang_size_sections_pass (NULL, FALSE);
3770 lang_reset_memory_regions ();
3773 for (os = &lang_output_section_statement.head->output_section_statement;
3777 asection *output_section;
3778 bfd_boolean exclude;
3780 if (os->constraint < 0)
3783 output_section = os->bfd_section;
3784 if (output_section == NULL)
3787 exclude = (output_section->rawsize == 0
3788 && (output_section->flags & SEC_KEEP) == 0
3789 && !bfd_section_removed_from_list (link_info.output_bfd,
3792 /* Some sections have not yet been sized, notably .gnu.version,
3793 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
3794 input sections, so don't drop output sections that have such
3795 input sections unless they are also marked SEC_EXCLUDE. */
3796 if (exclude && output_section->map_head.s != NULL)
3800 for (s = output_section->map_head.s; s != NULL; s = s->map_head.s)
3801 if ((s->flags & SEC_LINKER_CREATED) != 0
3802 && (s->flags & SEC_EXCLUDE) == 0)
3809 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
3810 output_section->map_head.link_order = NULL;
3811 output_section->map_tail.link_order = NULL;
3815 /* We don't set bfd_section to NULL since bfd_section of the
3816 removed output section statement may still be used. */
3817 if (!os->section_relative_symbol
3818 && !os->update_dot_tree)
3820 output_section->flags |= SEC_EXCLUDE;
3821 bfd_section_list_remove (link_info.output_bfd, output_section);
3822 link_info.output_bfd->section_count--;
3826 /* Stop future calls to lang_add_section from messing with map_head
3827 and map_tail link_order fields. */
3828 stripped_excluded_sections = TRUE;
3832 print_output_section_statement
3833 (lang_output_section_statement_type *output_section_statement)
3835 asection *section = output_section_statement->bfd_section;
3838 if (output_section_statement != abs_output_section)
3840 minfo ("\n%s", output_section_statement->name);
3842 if (section != NULL)
3844 print_dot = section->vma;
3846 len = strlen (output_section_statement->name);
3847 if (len >= SECTION_NAME_MAP_LENGTH - 1)
3852 while (len < SECTION_NAME_MAP_LENGTH)
3858 minfo ("0x%V %W", section->vma, section->size);
3860 if (section->vma != section->lma)
3861 minfo (_(" load address 0x%V"), section->lma);
3863 if (output_section_statement->update_dot_tree != NULL)
3864 exp_fold_tree (output_section_statement->update_dot_tree,
3865 bfd_abs_section_ptr, &print_dot);
3871 print_statement_list (output_section_statement->children.head,
3872 output_section_statement);
3875 /* Scan for the use of the destination in the right hand side
3876 of an expression. In such cases we will not compute the
3877 correct expression, since the value of DST that is used on
3878 the right hand side will be its final value, not its value
3879 just before this expression is evaluated. */
3882 scan_for_self_assignment (const char * dst, etree_type * rhs)
3884 if (rhs == NULL || dst == NULL)
3887 switch (rhs->type.node_class)
3890 return scan_for_self_assignment (dst, rhs->binary.lhs)
3891 || scan_for_self_assignment (dst, rhs->binary.rhs);
3894 return scan_for_self_assignment (dst, rhs->trinary.lhs)
3895 || scan_for_self_assignment (dst, rhs->trinary.rhs);
3898 case etree_provided:
3900 if (strcmp (dst, rhs->assign.dst) == 0)
3902 return scan_for_self_assignment (dst, rhs->assign.src);
3905 return scan_for_self_assignment (dst, rhs->unary.child);
3909 return strcmp (dst, rhs->value.str) == 0;
3914 return strcmp (dst, rhs->name.name) == 0;
3926 print_assignment (lang_assignment_statement_type *assignment,
3927 lang_output_section_statement_type *output_section)
3931 bfd_boolean computation_is_valid = TRUE;
3934 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3937 if (assignment->exp->type.node_class == etree_assert)
3940 tree = assignment->exp->assert_s.child;
3941 computation_is_valid = TRUE;
3945 const char *dst = assignment->exp->assign.dst;
3947 is_dot = (dst[0] == '.' && dst[1] == 0);
3948 tree = assignment->exp->assign.src;
3949 computation_is_valid = is_dot || (scan_for_self_assignment (dst, tree) == FALSE);
3952 exp_fold_tree (tree, output_section->bfd_section, &print_dot);
3953 if (expld.result.valid_p)
3957 if (computation_is_valid)
3959 value = expld.result.value;
3961 if (expld.result.section)
3962 value += expld.result.section->vma;
3964 minfo ("0x%V", value);
3970 struct bfd_link_hash_entry *h;
3972 h = bfd_link_hash_lookup (link_info.hash, assignment->exp->assign.dst,
3973 FALSE, FALSE, TRUE);
3976 value = h->u.def.value;
3978 if (expld.result.section)
3979 value += expld.result.section->vma;
3981 minfo ("[0x%V]", value);
3984 minfo ("[unresolved]");
3996 exp_print_tree (assignment->exp);
4001 print_input_statement (lang_input_statement_type *statm)
4003 if (statm->filename != NULL
4004 && (statm->the_bfd == NULL
4005 || (statm->the_bfd->flags & BFD_LINKER_CREATED) == 0))
4006 fprintf (config.map_file, "LOAD %s\n", statm->filename);
4009 /* Print all symbols defined in a particular section. This is called
4010 via bfd_link_hash_traverse, or by print_all_symbols. */
4013 print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
4015 asection *sec = (asection *) ptr;
4017 if ((hash_entry->type == bfd_link_hash_defined
4018 || hash_entry->type == bfd_link_hash_defweak)
4019 && sec == hash_entry->u.def.section)
4023 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
4026 (hash_entry->u.def.value
4027 + hash_entry->u.def.section->output_offset
4028 + hash_entry->u.def.section->output_section->vma));
4030 minfo (" %T\n", hash_entry->root.string);
4037 hash_entry_addr_cmp (const void *a, const void *b)
4039 const struct bfd_link_hash_entry *l = *(const struct bfd_link_hash_entry **)a;
4040 const struct bfd_link_hash_entry *r = *(const struct bfd_link_hash_entry **)b;
4042 if (l->u.def.value < r->u.def.value)
4044 else if (l->u.def.value > r->u.def.value)
4051 print_all_symbols (asection *sec)
4053 struct fat_user_section_struct *ud =
4054 (struct fat_user_section_struct *) get_userdata (sec);
4055 struct map_symbol_def *def;
4056 struct bfd_link_hash_entry **entries;
4062 *ud->map_symbol_def_tail = 0;
4064 /* Sort the symbols by address. */
4065 entries = (struct bfd_link_hash_entry **)
4066 obstack_alloc (&map_obstack, ud->map_symbol_def_count * sizeof (*entries));
4068 for (i = 0, def = ud->map_symbol_def_head; def; def = def->next, i++)
4069 entries[i] = def->entry;
4071 qsort (entries, ud->map_symbol_def_count, sizeof (*entries),
4072 hash_entry_addr_cmp);
4074 /* Print the symbols. */
4075 for (i = 0; i < ud->map_symbol_def_count; i++)
4076 print_one_symbol (entries[i], sec);
4078 obstack_free (&map_obstack, entries);
4081 /* Print information about an input section to the map file. */
4084 print_input_section (asection *i, bfd_boolean is_discarded)
4086 bfd_size_type size = i->size;
4093 minfo ("%s", i->name);
4095 len = 1 + strlen (i->name);
4096 if (len >= SECTION_NAME_MAP_LENGTH - 1)
4101 while (len < SECTION_NAME_MAP_LENGTH)
4107 if (i->output_section != NULL
4108 && i->output_section->owner == link_info.output_bfd)
4109 addr = i->output_section->vma + i->output_offset;
4117 minfo ("0x%V %W %B\n", addr, TO_ADDR (size), i->owner);
4119 if (size != i->rawsize && i->rawsize != 0)
4121 len = SECTION_NAME_MAP_LENGTH + 3;
4133 minfo (_("%W (size before relaxing)\n"), i->rawsize);
4136 if (i->output_section != NULL
4137 && i->output_section->owner == link_info.output_bfd)
4139 if (link_info.reduce_memory_overheads)
4140 bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
4142 print_all_symbols (i);
4144 /* Update print_dot, but make sure that we do not move it
4145 backwards - this could happen if we have overlays and a
4146 later overlay is shorter than an earier one. */
4147 if (addr + TO_ADDR (size) > print_dot)
4148 print_dot = addr + TO_ADDR (size);
4153 print_fill_statement (lang_fill_statement_type *fill)
4157 fputs (" FILL mask 0x", config.map_file);
4158 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
4159 fprintf (config.map_file, "%02x", *p);
4160 fputs ("\n", config.map_file);
4164 print_data_statement (lang_data_statement_type *data)
4172 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
4175 addr = data->output_offset;
4176 if (data->output_section != NULL)
4177 addr += data->output_section->vma;
4205 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
4207 if (data->exp->type.node_class != etree_value)
4210 exp_print_tree (data->exp);
4215 print_dot = addr + TO_ADDR (size);
4218 /* Print an address statement. These are generated by options like
4222 print_address_statement (lang_address_statement_type *address)
4224 minfo (_("Address of section %s set to "), address->section_name);
4225 exp_print_tree (address->address);
4229 /* Print a reloc statement. */
4232 print_reloc_statement (lang_reloc_statement_type *reloc)
4239 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
4242 addr = reloc->output_offset;
4243 if (reloc->output_section != NULL)
4244 addr += reloc->output_section->vma;
4246 size = bfd_get_reloc_size (reloc->howto);
4248 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
4250 if (reloc->name != NULL)
4251 minfo ("%s+", reloc->name);
4253 minfo ("%s+", reloc->section->name);
4255 exp_print_tree (reloc->addend_exp);
4259 print_dot = addr + TO_ADDR (size);
4263 print_padding_statement (lang_padding_statement_type *s)
4271 len = sizeof " *fill*" - 1;
4272 while (len < SECTION_NAME_MAP_LENGTH)
4278 addr = s->output_offset;
4279 if (s->output_section != NULL)
4280 addr += s->output_section->vma;
4281 minfo ("0x%V %W ", addr, (bfd_vma) s->size);
4283 if (s->fill->size != 0)
4287 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
4288 fprintf (config.map_file, "%02x", *p);
4293 print_dot = addr + TO_ADDR (s->size);
4297 print_wild_statement (lang_wild_statement_type *w,
4298 lang_output_section_statement_type *os)
4300 struct wildcard_list *sec;
4304 if (w->filenames_sorted)
4306 if (w->filename != NULL)
4307 minfo ("%s", w->filename);
4310 if (w->filenames_sorted)
4314 for (sec = w->section_list; sec; sec = sec->next)
4316 if (sec->spec.sorted)
4318 if (sec->spec.exclude_name_list != NULL)
4321 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
4322 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
4323 minfo (" %s", tmp->name);
4326 if (sec->spec.name != NULL)
4327 minfo ("%s", sec->spec.name);
4330 if (sec->spec.sorted)
4339 print_statement_list (w->children.head, os);
4342 /* Print a group statement. */
4345 print_group (lang_group_statement_type *s,
4346 lang_output_section_statement_type *os)
4348 fprintf (config.map_file, "START GROUP\n");
4349 print_statement_list (s->children.head, os);
4350 fprintf (config.map_file, "END GROUP\n");
4353 /* Print the list of statements in S.
4354 This can be called for any statement type. */
4357 print_statement_list (lang_statement_union_type *s,
4358 lang_output_section_statement_type *os)
4362 print_statement (s, os);
4367 /* Print the first statement in statement list S.
4368 This can be called for any statement type. */
4371 print_statement (lang_statement_union_type *s,
4372 lang_output_section_statement_type *os)
4374 switch (s->header.type)
4377 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
4380 case lang_constructors_statement_enum:
4381 if (constructor_list.head != NULL)
4383 if (constructors_sorted)
4384 minfo (" SORT (CONSTRUCTORS)\n");
4386 minfo (" CONSTRUCTORS\n");
4387 print_statement_list (constructor_list.head, os);
4390 case lang_wild_statement_enum:
4391 print_wild_statement (&s->wild_statement, os);
4393 case lang_address_statement_enum:
4394 print_address_statement (&s->address_statement);
4396 case lang_object_symbols_statement_enum:
4397 minfo (" CREATE_OBJECT_SYMBOLS\n");
4399 case lang_fill_statement_enum:
4400 print_fill_statement (&s->fill_statement);
4402 case lang_data_statement_enum:
4403 print_data_statement (&s->data_statement);
4405 case lang_reloc_statement_enum:
4406 print_reloc_statement (&s->reloc_statement);
4408 case lang_input_section_enum:
4409 print_input_section (s->input_section.section, FALSE);
4411 case lang_padding_statement_enum:
4412 print_padding_statement (&s->padding_statement);
4414 case lang_output_section_statement_enum:
4415 print_output_section_statement (&s->output_section_statement);
4417 case lang_assignment_statement_enum:
4418 print_assignment (&s->assignment_statement, os);
4420 case lang_target_statement_enum:
4421 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
4423 case lang_output_statement_enum:
4424 minfo ("OUTPUT(%s", s->output_statement.name);
4425 if (output_target != NULL)
4426 minfo (" %s", output_target);
4429 case lang_input_statement_enum:
4430 print_input_statement (&s->input_statement);
4432 case lang_group_statement_enum:
4433 print_group (&s->group_statement, os);
4435 case lang_insert_statement_enum:
4436 minfo ("INSERT %s %s\n",
4437 s->insert_statement.is_before ? "BEFORE" : "AFTER",
4438 s->insert_statement.where);
4444 print_statements (void)
4446 print_statement_list (statement_list.head, abs_output_section);
4449 /* Print the first N statements in statement list S to STDERR.
4450 If N == 0, nothing is printed.
4451 If N < 0, the entire list is printed.
4452 Intended to be called from GDB. */
4455 dprint_statement (lang_statement_union_type *s, int n)
4457 FILE *map_save = config.map_file;
4459 config.map_file = stderr;
4462 print_statement_list (s, abs_output_section);
4465 while (s && --n >= 0)
4467 print_statement (s, abs_output_section);
4472 config.map_file = map_save;
4476 insert_pad (lang_statement_union_type **ptr,
4478 unsigned int alignment_needed,
4479 asection *output_section,
4482 static fill_type zero_fill = { 1, { 0 } };
4483 lang_statement_union_type *pad = NULL;
4485 if (ptr != &statement_list.head)
4486 pad = ((lang_statement_union_type *)
4487 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
4489 && pad->header.type == lang_padding_statement_enum
4490 && pad->padding_statement.output_section == output_section)
4492 /* Use the existing pad statement. */
4494 else if ((pad = *ptr) != NULL
4495 && pad->header.type == lang_padding_statement_enum
4496 && pad->padding_statement.output_section == output_section)
4498 /* Use the existing pad statement. */
4502 /* Make a new padding statement, linked into existing chain. */
4503 pad = (lang_statement_union_type *)
4504 stat_alloc (sizeof (lang_padding_statement_type));
4505 pad->header.next = *ptr;
4507 pad->header.type = lang_padding_statement_enum;
4508 pad->padding_statement.output_section = output_section;
4511 pad->padding_statement.fill = fill;
4513 pad->padding_statement.output_offset = dot - output_section->vma;
4514 pad->padding_statement.size = alignment_needed;
4515 output_section->size += alignment_needed;
4518 /* Work out how much this section will move the dot point. */
4522 (lang_statement_union_type **this_ptr,
4523 lang_output_section_statement_type *output_section_statement,
4527 lang_input_section_type *is = &((*this_ptr)->input_section);
4528 asection *i = is->section;
4530 if (!((lang_input_statement_type *) i->owner->usrdata)->just_syms_flag
4531 && (i->flags & SEC_EXCLUDE) == 0)
4533 unsigned int alignment_needed;
4536 /* Align this section first to the input sections requirement,
4537 then to the output section's requirement. If this alignment
4538 is greater than any seen before, then record it too. Perform
4539 the alignment by inserting a magic 'padding' statement. */
4541 if (output_section_statement->subsection_alignment != -1)
4542 i->alignment_power = output_section_statement->subsection_alignment;
4544 o = output_section_statement->bfd_section;
4545 if (o->alignment_power < i->alignment_power)
4546 o->alignment_power = i->alignment_power;
4548 alignment_needed = align_power (dot, i->alignment_power) - dot;
4550 if (alignment_needed != 0)
4552 insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
4553 dot += alignment_needed;
4556 /* Remember where in the output section this input section goes. */
4558 i->output_offset = dot - o->vma;
4560 /* Mark how big the output section must be to contain this now. */
4561 dot += TO_ADDR (i->size);
4562 o->size = TO_SIZE (dot - o->vma);
4566 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
4573 sort_sections_by_lma (const void *arg1, const void *arg2)
4575 const asection *sec1 = *(const asection **) arg1;
4576 const asection *sec2 = *(const asection **) arg2;
4578 if (bfd_section_lma (sec1->owner, sec1)
4579 < bfd_section_lma (sec2->owner, sec2))
4581 else if (bfd_section_lma (sec1->owner, sec1)
4582 > bfd_section_lma (sec2->owner, sec2))
4584 else if (sec1->id < sec2->id)
4586 else if (sec1->id > sec2->id)
4592 #define IGNORE_SECTION(s) \
4593 ((s->flags & SEC_NEVER_LOAD) != 0 \
4594 || (s->flags & SEC_ALLOC) == 0 \
4595 || ((s->flags & SEC_THREAD_LOCAL) != 0 \
4596 && (s->flags & SEC_LOAD) == 0))
4598 /* Check to see if any allocated sections overlap with other allocated
4599 sections. This can happen if a linker script specifies the output
4600 section addresses of the two sections. Also check whether any memory
4601 region has overflowed. */
4604 lang_check_section_addresses (void)
4607 asection **sections, **spp;
4614 lang_memory_region_type *m;
4616 if (bfd_count_sections (link_info.output_bfd) <= 1)
4619 amt = bfd_count_sections (link_info.output_bfd) * sizeof (asection *);
4620 sections = (asection **) xmalloc (amt);
4622 /* Scan all sections in the output list. */
4624 for (s = link_info.output_bfd->sections; s != NULL; s = s->next)
4626 /* Only consider loadable sections with real contents. */
4627 if ((s->flags & SEC_NEVER_LOAD)
4628 || !(s->flags & SEC_LOAD)
4629 || !(s->flags & SEC_ALLOC)
4633 sections[count] = s;
4640 qsort (sections, (size_t) count, sizeof (asection *),
4641 sort_sections_by_lma);
4645 s_start = bfd_section_lma (link_info.output_bfd, s);
4646 s_end = s_start + TO_ADDR (s->size) - 1;
4647 for (count--; count; count--)
4649 /* We must check the sections' LMA addresses not their VMA
4650 addresses because overlay sections can have overlapping VMAs
4651 but they must have distinct LMAs. */
4656 s_start = bfd_section_lma (link_info.output_bfd, s);
4657 s_end = s_start + TO_ADDR (s->size) - 1;
4659 /* Look for an overlap. */
4660 if (s_end >= os_start && s_start <= os_end)
4661 einfo (_("%X%P: section %s loaded at [%V,%V] overlaps section %s loaded at [%V,%V]\n"),
4662 s->name, s_start, s_end, os->name, os_start, os_end);
4667 /* If any memory region has overflowed, report by how much.
4668 We do not issue this diagnostic for regions that had sections
4669 explicitly placed outside their bounds; os_region_check's
4670 diagnostics are adequate for that case.
4672 FIXME: It is conceivable that m->current - (m->origin + m->length)
4673 might overflow a 32-bit integer. There is, alas, no way to print
4674 a bfd_vma quantity in decimal. */
4675 for (m = lang_memory_region_list; m; m = m->next)
4676 if (m->had_full_message)
4677 einfo (_("%X%P: region `%s' overflowed by %ld bytes\n"),
4678 m->name_list.name, (long)(m->current - (m->origin + m->length)));
4682 /* Make sure the new address is within the region. We explicitly permit the
4683 current address to be at the exact end of the region when the address is
4684 non-zero, in case the region is at the end of addressable memory and the
4685 calculation wraps around. */
4688 os_region_check (lang_output_section_statement_type *os,
4689 lang_memory_region_type *region,
4693 if ((region->current < region->origin
4694 || (region->current - region->origin > region->length))
4695 && ((region->current != region->origin + region->length)
4700 einfo (_("%X%P: address 0x%v of %B section `%s'"
4701 " is not within region `%s'\n"),
4703 os->bfd_section->owner,
4704 os->bfd_section->name,
4705 region->name_list.name);
4707 else if (!region->had_full_message)
4709 region->had_full_message = TRUE;
4711 einfo (_("%X%P: %B section `%s' will not fit in region `%s'\n"),
4712 os->bfd_section->owner,
4713 os->bfd_section->name,
4714 region->name_list.name);
4719 /* Set the sizes for all the output sections. */
4722 lang_size_sections_1
4723 (lang_statement_union_type *s,
4724 lang_output_section_statement_type *output_section_statement,
4725 lang_statement_union_type **prev,
4729 bfd_boolean check_regions)
4731 /* Size up the sections from their constituent parts. */
4732 for (; s != NULL; s = s->header.next)
4734 switch (s->header.type)
4736 case lang_output_section_statement_enum:
4738 bfd_vma newdot, after;
4739 lang_output_section_statement_type *os;
4740 lang_memory_region_type *r;
4742 os = &s->output_section_statement;
4743 /* FIXME: We shouldn't need to zero section vmas for ld -r
4744 here, in lang_insert_orphan, or in the default linker scripts.
4745 This is covering for coff backend linker bugs. See PR6945. */
4746 if (os->addr_tree == NULL
4747 && link_info.relocatable
4748 && (bfd_get_flavour (link_info.output_bfd)
4749 == bfd_target_coff_flavour))
4750 os->addr_tree = exp_intop (0);
4751 if (os->addr_tree != NULL)
4753 os->processed_vma = FALSE;
4754 exp_fold_tree (os->addr_tree, bfd_abs_section_ptr, &dot);
4756 if (expld.result.valid_p)
4757 dot = expld.result.value + expld.result.section->vma;
4758 else if (expld.phase != lang_mark_phase_enum)
4759 einfo (_("%F%S: non constant or forward reference"
4760 " address expression for section %s\n"),
4764 if (os->bfd_section == NULL)
4765 /* This section was removed or never actually created. */
4768 /* If this is a COFF shared library section, use the size and
4769 address from the input section. FIXME: This is COFF
4770 specific; it would be cleaner if there were some other way
4771 to do this, but nothing simple comes to mind. */
4772 if (((bfd_get_flavour (link_info.output_bfd)
4773 == bfd_target_ecoff_flavour)
4774 || (bfd_get_flavour (link_info.output_bfd)
4775 == bfd_target_coff_flavour))
4776 && (os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
4780 if (os->children.head == NULL
4781 || os->children.head->header.next != NULL
4782 || (os->children.head->header.type
4783 != lang_input_section_enum))
4784 einfo (_("%P%X: Internal error on COFF shared library"
4785 " section %s\n"), os->name);
4787 input = os->children.head->input_section.section;
4788 bfd_set_section_vma (os->bfd_section->owner,
4790 bfd_section_vma (input->owner, input));
4791 os->bfd_section->size = input->size;
4796 if (bfd_is_abs_section (os->bfd_section))
4798 /* No matter what happens, an abs section starts at zero. */
4799 ASSERT (os->bfd_section->vma == 0);
4805 if (os->addr_tree == NULL)
4807 /* No address specified for this section, get one
4808 from the region specification. */
4809 if (os->region == NULL
4810 || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))
4811 && os->region->name_list.name[0] == '*'
4812 && strcmp (os->region->name_list.name,
4813 DEFAULT_MEMORY_REGION) == 0))
4815 os->region = lang_memory_default (os->bfd_section);
4818 /* If a loadable section is using the default memory
4819 region, and some non default memory regions were
4820 defined, issue an error message. */
4822 && !IGNORE_SECTION (os->bfd_section)
4823 && ! link_info.relocatable
4825 && strcmp (os->region->name_list.name,
4826 DEFAULT_MEMORY_REGION) == 0
4827 && lang_memory_region_list != NULL
4828 && (strcmp (lang_memory_region_list->name_list.name,
4829 DEFAULT_MEMORY_REGION) != 0
4830 || lang_memory_region_list->next != NULL)
4831 && expld.phase != lang_mark_phase_enum)
4833 /* By default this is an error rather than just a
4834 warning because if we allocate the section to the
4835 default memory region we can end up creating an
4836 excessively large binary, or even seg faulting when
4837 attempting to perform a negative seek. See
4838 sources.redhat.com/ml/binutils/2003-04/msg00423.html
4839 for an example of this. This behaviour can be
4840 overridden by the using the --no-check-sections
4842 if (command_line.check_section_addresses)
4843 einfo (_("%P%F: error: no memory region specified"
4844 " for loadable section `%s'\n"),
4845 bfd_get_section_name (link_info.output_bfd,
4848 einfo (_("%P: warning: no memory region specified"
4849 " for loadable section `%s'\n"),
4850 bfd_get_section_name (link_info.output_bfd,
4854 newdot = os->region->current;
4855 align = os->bfd_section->alignment_power;
4858 align = os->section_alignment;
4860 /* Align to what the section needs. */
4863 bfd_vma savedot = newdot;
4864 newdot = align_power (newdot, align);
4866 if (newdot != savedot
4867 && (config.warn_section_align
4868 || os->addr_tree != NULL)
4869 && expld.phase != lang_mark_phase_enum)
4870 einfo (_("%P: warning: changing start of section"
4871 " %s by %lu bytes\n"),
4872 os->name, (unsigned long) (newdot - savedot));
4875 bfd_set_section_vma (0, os->bfd_section, newdot);
4877 os->bfd_section->output_offset = 0;
4880 lang_size_sections_1 (os->children.head, os, &os->children.head,
4881 os->fill, newdot, relax, check_regions);
4883 os->processed_vma = TRUE;
4885 if (bfd_is_abs_section (os->bfd_section) || os->ignored)
4886 /* Except for some special linker created sections,
4887 no output section should change from zero size
4888 after strip_excluded_output_sections. A non-zero
4889 size on an ignored section indicates that some
4890 input section was not sized early enough. */
4891 ASSERT (os->bfd_section->size == 0);
4894 dot = os->bfd_section->vma;
4896 /* Put the section within the requested block size, or
4897 align at the block boundary. */
4899 + TO_ADDR (os->bfd_section->size)
4900 + os->block_value - 1)
4901 & - (bfd_vma) os->block_value);
4903 os->bfd_section->size = TO_SIZE (after - os->bfd_section->vma);
4906 /* Set section lma. */
4909 r = lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
4913 bfd_vma lma = exp_get_abs_int (os->load_base, 0, "load base");
4914 os->bfd_section->lma = lma;
4916 else if (os->lma_region != NULL)
4918 bfd_vma lma = os->lma_region->current;
4920 if (os->section_alignment != -1)
4921 lma = align_power (lma, os->section_alignment);
4922 os->bfd_section->lma = lma;
4924 else if (r->last_os != NULL
4925 && (os->bfd_section->flags & SEC_ALLOC) != 0)
4930 last = r->last_os->output_section_statement.bfd_section;
4932 /* A backwards move of dot should be accompanied by
4933 an explicit assignment to the section LMA (ie.
4934 os->load_base set) because backwards moves can
4935 create overlapping LMAs. */
4937 && os->bfd_section->size != 0
4938 && dot + os->bfd_section->size <= last->vma)
4940 /* If dot moved backwards then leave lma equal to
4941 vma. This is the old default lma, which might
4942 just happen to work when the backwards move is
4943 sufficiently large. Nag if this changes anything,
4944 so people can fix their linker scripts. */
4946 if (last->vma != last->lma)
4947 einfo (_("%P: warning: dot moved backwards before `%s'\n"),
4952 /* If this is an overlay, set the current lma to that
4953 at the end of the previous section. */
4954 if (os->sectype == overlay_section)
4955 lma = last->lma + last->size;
4957 /* Otherwise, keep the same lma to vma relationship
4958 as the previous section. */
4960 lma = dot + last->lma - last->vma;
4962 if (os->section_alignment != -1)
4963 lma = align_power (lma, os->section_alignment);
4964 os->bfd_section->lma = lma;
4967 os->processed_lma = TRUE;
4969 if (bfd_is_abs_section (os->bfd_section) || os->ignored)
4972 /* Keep track of normal sections using the default
4973 lma region. We use this to set the lma for
4974 following sections. Overlays or other linker
4975 script assignment to lma might mean that the
4976 default lma == vma is incorrect.
4977 To avoid warnings about dot moving backwards when using
4978 -Ttext, don't start tracking sections until we find one
4979 of non-zero size or with lma set differently to vma. */
4980 if (((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
4981 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0)
4982 && (os->bfd_section->flags & SEC_ALLOC) != 0
4983 && (os->bfd_section->size != 0
4984 || (r->last_os == NULL
4985 && os->bfd_section->vma != os->bfd_section->lma)
4986 || (r->last_os != NULL
4987 && dot >= (r->last_os->output_section_statement
4988 .bfd_section->vma)))
4989 && os->lma_region == NULL
4990 && !link_info.relocatable)
4993 /* .tbss sections effectively have zero size. */
4994 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
4995 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
4996 || link_info.relocatable)
4997 dot += TO_ADDR (os->bfd_section->size);
4999 if (os->update_dot_tree != 0)
5000 exp_fold_tree (os->update_dot_tree, bfd_abs_section_ptr, &dot);
5002 /* Update dot in the region ?
5003 We only do this if the section is going to be allocated,
5004 since unallocated sections do not contribute to the region's
5005 overall size in memory.
5007 If the SEC_NEVER_LOAD bit is not set, it will affect the
5008 addresses of sections after it. We have to update
5010 if (os->region != NULL
5011 && ((os->bfd_section->flags & SEC_NEVER_LOAD) == 0
5012 || (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))))
5014 os->region->current = dot;
5017 /* Make sure the new address is within the region. */
5018 os_region_check (os, os->region, os->addr_tree,
5019 os->bfd_section->vma);
5021 if (os->lma_region != NULL && os->lma_region != os->region
5022 && (os->bfd_section->flags & SEC_LOAD))
5024 os->lma_region->current
5025 = os->bfd_section->lma + TO_ADDR (os->bfd_section->size);
5028 os_region_check (os, os->lma_region, NULL,
5029 os->bfd_section->lma);
5035 case lang_constructors_statement_enum:
5036 dot = lang_size_sections_1 (constructor_list.head,
5037 output_section_statement,
5038 &s->wild_statement.children.head,
5039 fill, dot, relax, check_regions);
5042 case lang_data_statement_enum:
5044 unsigned int size = 0;
5046 s->data_statement.output_offset =
5047 dot - output_section_statement->bfd_section->vma;
5048 s->data_statement.output_section =
5049 output_section_statement->bfd_section;
5051 /* We might refer to provided symbols in the expression, and
5052 need to mark them as needed. */
5053 exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
5055 switch (s->data_statement.type)
5073 if (size < TO_SIZE ((unsigned) 1))
5074 size = TO_SIZE ((unsigned) 1);
5075 dot += TO_ADDR (size);
5076 output_section_statement->bfd_section->size += size;
5080 case lang_reloc_statement_enum:
5084 s->reloc_statement.output_offset =
5085 dot - output_section_statement->bfd_section->vma;
5086 s->reloc_statement.output_section =
5087 output_section_statement->bfd_section;
5088 size = bfd_get_reloc_size (s->reloc_statement.howto);
5089 dot += TO_ADDR (size);
5090 output_section_statement->bfd_section->size += size;
5094 case lang_wild_statement_enum:
5095 dot = lang_size_sections_1 (s->wild_statement.children.head,
5096 output_section_statement,
5097 &s->wild_statement.children.head,
5098 fill, dot, relax, check_regions);
5101 case lang_object_symbols_statement_enum:
5102 link_info.create_object_symbols_section =
5103 output_section_statement->bfd_section;
5106 case lang_output_statement_enum:
5107 case lang_target_statement_enum:
5110 case lang_input_section_enum:
5114 i = (*prev)->input_section.section;
5119 if (! bfd_relax_section (i->owner, i, &link_info, &again))
5120 einfo (_("%P%F: can't relax section: %E\n"));
5124 dot = size_input_section (prev, output_section_statement,
5125 output_section_statement->fill, dot);
5129 case lang_input_statement_enum:
5132 case lang_fill_statement_enum:
5133 s->fill_statement.output_section =
5134 output_section_statement->bfd_section;
5136 fill = s->fill_statement.fill;
5139 case lang_assignment_statement_enum:
5141 bfd_vma newdot = dot;
5142 etree_type *tree = s->assignment_statement.exp;
5144 expld.dataseg.relro = exp_dataseg_relro_none;
5146 exp_fold_tree (tree,
5147 output_section_statement->bfd_section,
5150 if (expld.dataseg.relro == exp_dataseg_relro_start)
5152 if (!expld.dataseg.relro_start_stat)
5153 expld.dataseg.relro_start_stat = s;
5156 ASSERT (expld.dataseg.relro_start_stat == s);
5159 else if (expld.dataseg.relro == exp_dataseg_relro_end)
5161 if (!expld.dataseg.relro_end_stat)
5162 expld.dataseg.relro_end_stat = s;
5165 ASSERT (expld.dataseg.relro_end_stat == s);
5168 expld.dataseg.relro = exp_dataseg_relro_none;
5170 /* This symbol is relative to this section. */
5171 if ((tree->type.node_class == etree_provided
5172 || tree->type.node_class == etree_assign)
5173 && (tree->assign.dst [0] != '.'
5174 || tree->assign.dst [1] != '\0'))
5175 output_section_statement->section_relative_symbol = 1;
5177 if (!output_section_statement->ignored)
5179 if (output_section_statement == abs_output_section)
5181 /* If we don't have an output section, then just adjust
5182 the default memory address. */
5183 lang_memory_region_lookup (DEFAULT_MEMORY_REGION,
5184 FALSE)->current = newdot;
5186 else if (newdot != dot)
5188 /* Insert a pad after this statement. We can't
5189 put the pad before when relaxing, in case the
5190 assignment references dot. */
5191 insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
5192 output_section_statement->bfd_section, dot);
5194 /* Don't neuter the pad below when relaxing. */
5197 /* If dot is advanced, this implies that the section
5198 should have space allocated to it, unless the
5199 user has explicitly stated that the section
5200 should never be loaded. */
5201 if (!(output_section_statement->flags & SEC_NEVER_LOAD))
5202 output_section_statement->bfd_section->flags |= SEC_ALLOC;
5209 case lang_padding_statement_enum:
5210 /* If this is the first time lang_size_sections is called,
5211 we won't have any padding statements. If this is the
5212 second or later passes when relaxing, we should allow
5213 padding to shrink. If padding is needed on this pass, it
5214 will be added back in. */
5215 s->padding_statement.size = 0;
5217 /* Make sure output_offset is valid. If relaxation shrinks
5218 the section and this pad isn't needed, it's possible to
5219 have output_offset larger than the final size of the
5220 section. bfd_set_section_contents will complain even for
5221 a pad size of zero. */
5222 s->padding_statement.output_offset
5223 = dot - output_section_statement->bfd_section->vma;
5226 case lang_group_statement_enum:
5227 dot = lang_size_sections_1 (s->group_statement.children.head,
5228 output_section_statement,
5229 &s->group_statement.children.head,
5230 fill, dot, relax, check_regions);
5233 case lang_insert_statement_enum:
5236 /* We can only get here when relaxing is turned on. */
5237 case lang_address_statement_enum:
5244 prev = &s->header.next;
5249 /* Callback routine that is used in _bfd_elf_map_sections_to_segments.
5250 The BFD library has set NEW_SEGMENT to TRUE iff it thinks that
5251 CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different
5252 segments. We are allowed an opportunity to override this decision. */
5255 ldlang_override_segment_assignment (struct bfd_link_info * info ATTRIBUTE_UNUSED,
5256 bfd * abfd ATTRIBUTE_UNUSED,
5257 asection * current_section,
5258 asection * previous_section,
5259 bfd_boolean new_segment)
5261 lang_output_section_statement_type * cur;
5262 lang_output_section_statement_type * prev;
5264 /* The checks below are only necessary when the BFD library has decided
5265 that the two sections ought to be placed into the same segment. */
5269 /* Paranoia checks. */
5270 if (current_section == NULL || previous_section == NULL)
5273 /* Find the memory regions associated with the two sections.
5274 We call lang_output_section_find() here rather than scanning the list
5275 of output sections looking for a matching section pointer because if
5276 we have a large number of sections then a hash lookup is faster. */
5277 cur = lang_output_section_find (current_section->name);
5278 prev = lang_output_section_find (previous_section->name);
5280 /* More paranoia. */
5281 if (cur == NULL || prev == NULL)
5284 /* If the regions are different then force the sections to live in
5285 different segments. See the email thread starting at the following
5286 URL for the reasons why this is necessary:
5287 http://sourceware.org/ml/binutils/2007-02/msg00216.html */
5288 return cur->region != prev->region;
5292 one_lang_size_sections_pass (bfd_boolean *relax, bfd_boolean check_regions)
5294 lang_statement_iteration++;
5295 lang_size_sections_1 (statement_list.head, abs_output_section,
5296 &statement_list.head, 0, 0, relax, check_regions);
5300 lang_size_sections (bfd_boolean *relax, bfd_boolean check_regions)
5302 expld.phase = lang_allocating_phase_enum;
5303 expld.dataseg.phase = exp_dataseg_none;
5305 one_lang_size_sections_pass (relax, check_regions);
5306 if (expld.dataseg.phase == exp_dataseg_end_seen
5307 && link_info.relro && expld.dataseg.relro_end)
5309 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
5310 to put expld.dataseg.relro on a (common) page boundary. */
5311 bfd_vma min_base, old_base, relro_end, maxpage;
5313 expld.dataseg.phase = exp_dataseg_relro_adjust;
5314 maxpage = expld.dataseg.maxpagesize;
5315 /* MIN_BASE is the absolute minimum address we are allowed to start the
5316 read-write segment (byte before will be mapped read-only). */
5317 min_base = (expld.dataseg.min_base + maxpage - 1) & ~(maxpage - 1);
5318 /* OLD_BASE is the address for a feasible minimum address which will
5319 still not cause a data overlap inside MAXPAGE causing file offset skip
5321 old_base = expld.dataseg.base;
5322 expld.dataseg.base += (-expld.dataseg.relro_end
5323 & (expld.dataseg.pagesize - 1));
5324 /* Compute the expected PT_GNU_RELRO segment end. */
5325 relro_end = ((expld.dataseg.relro_end + expld.dataseg.pagesize - 1)
5326 & ~(expld.dataseg.pagesize - 1));
5327 if (min_base + maxpage < expld.dataseg.base)
5329 expld.dataseg.base -= maxpage;
5330 relro_end -= maxpage;
5332 lang_reset_memory_regions ();
5333 one_lang_size_sections_pass (relax, check_regions);
5334 if (expld.dataseg.relro_end > relro_end)
5336 /* The alignment of sections between DATA_SEGMENT_ALIGN
5337 and DATA_SEGMENT_RELRO_END caused huge padding to be
5338 inserted at DATA_SEGMENT_RELRO_END. Try to start a bit lower so
5339 that the section alignments will fit in. */
5341 unsigned int max_alignment_power = 0;
5343 /* Find maximum alignment power of sections between
5344 DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END. */
5345 for (sec = link_info.output_bfd->sections; sec; sec = sec->next)
5346 if (sec->vma >= expld.dataseg.base
5347 && sec->vma < expld.dataseg.relro_end
5348 && sec->alignment_power > max_alignment_power)
5349 max_alignment_power = sec->alignment_power;
5351 if (((bfd_vma) 1 << max_alignment_power) < expld.dataseg.pagesize)
5353 if (expld.dataseg.base - (1 << max_alignment_power) < old_base)
5354 expld.dataseg.base += expld.dataseg.pagesize;
5355 expld.dataseg.base -= (1 << max_alignment_power);
5356 lang_reset_memory_regions ();
5357 one_lang_size_sections_pass (relax, check_regions);
5360 link_info.relro_start = expld.dataseg.base;
5361 link_info.relro_end = expld.dataseg.relro_end;
5363 else if (expld.dataseg.phase == exp_dataseg_end_seen)
5365 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
5366 a page could be saved in the data segment. */
5367 bfd_vma first, last;
5369 first = -expld.dataseg.base & (expld.dataseg.pagesize - 1);
5370 last = expld.dataseg.end & (expld.dataseg.pagesize - 1);
5372 && ((expld.dataseg.base & ~(expld.dataseg.pagesize - 1))
5373 != (expld.dataseg.end & ~(expld.dataseg.pagesize - 1)))
5374 && first + last <= expld.dataseg.pagesize)
5376 expld.dataseg.phase = exp_dataseg_adjust;
5377 lang_reset_memory_regions ();
5378 one_lang_size_sections_pass (relax, check_regions);
5382 expld.phase = lang_final_phase_enum;
5385 /* Worker function for lang_do_assignments. Recursiveness goes here. */
5388 lang_do_assignments_1 (lang_statement_union_type *s,
5389 lang_output_section_statement_type *current_os,
5393 for (; s != NULL; s = s->header.next)
5395 switch (s->header.type)
5397 case lang_constructors_statement_enum:
5398 dot = lang_do_assignments_1 (constructor_list.head,
5399 current_os, fill, dot);
5402 case lang_output_section_statement_enum:
5404 lang_output_section_statement_type *os;
5406 os = &(s->output_section_statement);
5407 if (os->bfd_section != NULL && !os->ignored)
5409 dot = os->bfd_section->vma;
5411 lang_do_assignments_1 (os->children.head, os, os->fill, dot);
5413 /* .tbss sections effectively have zero size. */
5414 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
5415 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
5416 || link_info.relocatable)
5417 dot += TO_ADDR (os->bfd_section->size);
5419 if (os->update_dot_tree != NULL)
5420 exp_fold_tree (os->update_dot_tree, bfd_abs_section_ptr, &dot);
5425 case lang_wild_statement_enum:
5427 dot = lang_do_assignments_1 (s->wild_statement.children.head,
5428 current_os, fill, dot);
5431 case lang_object_symbols_statement_enum:
5432 case lang_output_statement_enum:
5433 case lang_target_statement_enum:
5436 case lang_data_statement_enum:
5437 exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
5438 if (expld.result.valid_p)
5439 s->data_statement.value = (expld.result.value
5440 + expld.result.section->vma);
5442 einfo (_("%F%P: invalid data statement\n"));
5445 switch (s->data_statement.type)
5463 if (size < TO_SIZE ((unsigned) 1))
5464 size = TO_SIZE ((unsigned) 1);
5465 dot += TO_ADDR (size);
5469 case lang_reloc_statement_enum:
5470 exp_fold_tree (s->reloc_statement.addend_exp,
5471 bfd_abs_section_ptr, &dot);
5472 if (expld.result.valid_p)
5473 s->reloc_statement.addend_value = expld.result.value;
5475 einfo (_("%F%P: invalid reloc statement\n"));
5476 dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
5479 case lang_input_section_enum:
5481 asection *in = s->input_section.section;
5483 if ((in->flags & SEC_EXCLUDE) == 0)
5484 dot += TO_ADDR (in->size);
5488 case lang_input_statement_enum:
5491 case lang_fill_statement_enum:
5492 fill = s->fill_statement.fill;
5495 case lang_assignment_statement_enum:
5496 exp_fold_tree (s->assignment_statement.exp,
5497 current_os->bfd_section,
5501 case lang_padding_statement_enum:
5502 dot += TO_ADDR (s->padding_statement.size);
5505 case lang_group_statement_enum:
5506 dot = lang_do_assignments_1 (s->group_statement.children.head,
5507 current_os, fill, dot);
5510 case lang_insert_statement_enum:
5513 case lang_address_statement_enum:
5525 lang_do_assignments (void)
5527 lang_statement_iteration++;
5528 lang_do_assignments_1 (statement_list.head, abs_output_section, NULL, 0);
5531 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
5532 operator .startof. (section_name), it produces an undefined symbol
5533 .startof.section_name. Similarly, when it sees
5534 .sizeof. (section_name), it produces an undefined symbol
5535 .sizeof.section_name. For all the output sections, we look for
5536 such symbols, and set them to the correct value. */
5539 lang_set_startof (void)
5543 if (link_info.relocatable)
5546 for (s = link_info.output_bfd->sections; s != NULL; s = s->next)
5548 const char *secname;
5550 struct bfd_link_hash_entry *h;
5552 secname = bfd_get_section_name (link_info.output_bfd, s);
5553 buf = (char *) xmalloc (10 + strlen (secname));
5555 sprintf (buf, ".startof.%s", secname);
5556 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
5557 if (h != NULL && h->type == bfd_link_hash_undefined)
5559 h->type = bfd_link_hash_defined;
5560 h->u.def.value = bfd_get_section_vma (link_info.output_bfd, s);
5561 h->u.def.section = bfd_abs_section_ptr;
5564 sprintf (buf, ".sizeof.%s", secname);
5565 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
5566 if (h != NULL && h->type == bfd_link_hash_undefined)
5568 h->type = bfd_link_hash_defined;
5569 h->u.def.value = TO_ADDR (s->size);
5570 h->u.def.section = bfd_abs_section_ptr;
5580 struct bfd_link_hash_entry *h;
5583 if ((link_info.relocatable && !link_info.gc_sections)
5584 || (link_info.shared && !link_info.executable))
5585 warn = entry_from_cmdline;
5589 /* Force the user to specify a root when generating a relocatable with
5591 if (link_info.gc_sections && link_info.relocatable
5592 && (entry_symbol.name == NULL
5593 && ldlang_undef_chain_list_head == NULL))
5594 einfo (_("%P%F: gc-sections requires either an entry or "
5595 "an undefined symbol\n"));
5597 if (entry_symbol.name == NULL)
5599 /* No entry has been specified. Look for the default entry, but
5600 don't warn if we don't find it. */
5601 entry_symbol.name = entry_symbol_default;
5605 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
5606 FALSE, FALSE, TRUE);
5608 && (h->type == bfd_link_hash_defined
5609 || h->type == bfd_link_hash_defweak)
5610 && h->u.def.section->output_section != NULL)
5614 val = (h->u.def.value
5615 + bfd_get_section_vma (link_info.output_bfd,
5616 h->u.def.section->output_section)
5617 + h->u.def.section->output_offset);
5618 if (! bfd_set_start_address (link_info.output_bfd, val))
5619 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
5626 /* We couldn't find the entry symbol. Try parsing it as a
5628 val = bfd_scan_vma (entry_symbol.name, &send, 0);
5631 if (! bfd_set_start_address (link_info.output_bfd, val))
5632 einfo (_("%P%F: can't set start address\n"));
5638 /* Can't find the entry symbol, and it's not a number. Use
5639 the first address in the text section. */
5640 ts = bfd_get_section_by_name (link_info.output_bfd, entry_section);
5644 einfo (_("%P: warning: cannot find entry symbol %s;"
5645 " defaulting to %V\n"),
5647 bfd_get_section_vma (link_info.output_bfd, ts));
5648 if (!(bfd_set_start_address
5649 (link_info.output_bfd,
5650 bfd_get_section_vma (link_info.output_bfd, ts))))
5651 einfo (_("%P%F: can't set start address\n"));
5656 einfo (_("%P: warning: cannot find entry symbol %s;"
5657 " not setting start address\n"),
5663 /* Don't bfd_hash_table_free (&lang_definedness_table);
5664 map file output may result in a call of lang_track_definedness. */
5667 /* This is a small function used when we want to ignore errors from
5671 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
5673 /* Don't do anything. */
5676 /* Check that the architecture of all the input files is compatible
5677 with the output file. Also call the backend to let it do any
5678 other checking that is needed. */
5683 lang_statement_union_type *file;
5685 const bfd_arch_info_type *compatible;
5687 for (file = file_chain.head; file != NULL; file = file->input_statement.next)
5689 input_bfd = file->input_statement.the_bfd;
5691 = bfd_arch_get_compatible (input_bfd, link_info.output_bfd,
5692 command_line.accept_unknown_input_arch);
5694 /* In general it is not possible to perform a relocatable
5695 link between differing object formats when the input
5696 file has relocations, because the relocations in the
5697 input format may not have equivalent representations in
5698 the output format (and besides BFD does not translate
5699 relocs for other link purposes than a final link). */
5700 if ((link_info.relocatable || link_info.emitrelocations)
5701 && (compatible == NULL
5702 || (bfd_get_flavour (input_bfd)
5703 != bfd_get_flavour (link_info.output_bfd)))
5704 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
5706 einfo (_("%P%F: Relocatable linking with relocations from"
5707 " format %s (%B) to format %s (%B) is not supported\n"),
5708 bfd_get_target (input_bfd), input_bfd,
5709 bfd_get_target (link_info.output_bfd), link_info.output_bfd);
5710 /* einfo with %F exits. */
5713 if (compatible == NULL)
5715 if (command_line.warn_mismatch)
5716 einfo (_("%P%X: %s architecture of input file `%B'"
5717 " is incompatible with %s output\n"),
5718 bfd_printable_name (input_bfd), input_bfd,
5719 bfd_printable_name (link_info.output_bfd));
5721 else if (bfd_count_sections (input_bfd))
5723 /* If the input bfd has no contents, it shouldn't set the
5724 private data of the output bfd. */
5726 bfd_error_handler_type pfn = NULL;
5728 /* If we aren't supposed to warn about mismatched input
5729 files, temporarily set the BFD error handler to a
5730 function which will do nothing. We still want to call
5731 bfd_merge_private_bfd_data, since it may set up
5732 information which is needed in the output file. */
5733 if (! command_line.warn_mismatch)
5734 pfn = bfd_set_error_handler (ignore_bfd_errors);
5735 if (! bfd_merge_private_bfd_data (input_bfd, link_info.output_bfd))
5737 if (command_line.warn_mismatch)
5738 einfo (_("%P%X: failed to merge target specific data"
5739 " of file %B\n"), input_bfd);
5741 if (! command_line.warn_mismatch)
5742 bfd_set_error_handler (pfn);
5747 /* Look through all the global common symbols and attach them to the
5748 correct section. The -sort-common command line switch may be used
5749 to roughly sort the entries by alignment. */
5754 if (command_line.inhibit_common_definition)
5756 if (link_info.relocatable
5757 && ! command_line.force_common_definition)
5760 if (! config.sort_common)
5761 bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
5766 if (config.sort_common == sort_descending)
5768 for (power = 4; power > 0; power--)
5769 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
5772 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
5776 for (power = 0; power <= 4; power++)
5777 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
5780 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
5785 /* Place one common symbol in the correct section. */
5788 lang_one_common (struct bfd_link_hash_entry *h, void *info)
5790 unsigned int power_of_two;
5794 if (h->type != bfd_link_hash_common)
5798 power_of_two = h->u.c.p->alignment_power;
5800 if (config.sort_common == sort_descending
5801 && power_of_two < *(unsigned int *) info)
5803 else if (config.sort_common == sort_ascending
5804 && power_of_two > *(unsigned int *) info)
5807 section = h->u.c.p->section;
5808 if (!bfd_define_common_symbol (link_info.output_bfd, &link_info, h))
5809 einfo (_("%P%F: Could not define common symbol `%T': %E\n"),
5812 if (config.map_file != NULL)
5814 static bfd_boolean header_printed;
5819 if (! header_printed)
5821 minfo (_("\nAllocating common symbols\n"));
5822 minfo (_("Common symbol size file\n\n"));
5823 header_printed = TRUE;
5826 name = bfd_demangle (link_info.output_bfd, h->root.string,
5827 DMGL_ANSI | DMGL_PARAMS);
5830 minfo ("%s", h->root.string);
5831 len = strlen (h->root.string);
5836 len = strlen (name);
5852 if (size <= 0xffffffff)
5853 sprintf (buf, "%lx", (unsigned long) size);
5855 sprintf_vma (buf, size);
5865 minfo ("%B\n", section->owner);
5871 /* Run through the input files and ensure that every input section has
5872 somewhere to go. If one is found without a destination then create
5873 an input request and place it into the statement tree. */
5876 lang_place_orphans (void)
5878 LANG_FOR_EACH_INPUT_STATEMENT (file)
5882 for (s = file->the_bfd->sections; s != NULL; s = s->next)
5884 if (s->output_section == NULL)
5886 /* This section of the file is not attached, root
5887 around for a sensible place for it to go. */
5889 if (file->just_syms_flag)
5890 bfd_link_just_syms (file->the_bfd, s, &link_info);
5891 else if ((s->flags & SEC_EXCLUDE) != 0)
5892 s->output_section = bfd_abs_section_ptr;
5893 else if (strcmp (s->name, "COMMON") == 0)
5895 /* This is a lonely common section which must have
5896 come from an archive. We attach to the section
5897 with the wildcard. */
5898 if (! link_info.relocatable
5899 || command_line.force_common_definition)
5901 if (default_common_section == NULL)
5902 default_common_section
5903 = lang_output_section_statement_lookup (".bss", 0,
5905 lang_add_section (&default_common_section->children, s,
5906 default_common_section);
5911 const char *name = s->name;
5914 if (config.unique_orphan_sections
5915 || unique_section_p (s, NULL))
5916 constraint = SPECIAL;
5918 if (!ldemul_place_orphan (s, name, constraint))
5920 lang_output_section_statement_type *os;
5921 os = lang_output_section_statement_lookup (name,
5924 lang_add_section (&os->children, s, os);
5933 lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
5935 flagword *ptr_flags;
5937 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
5943 *ptr_flags |= SEC_ALLOC;
5947 *ptr_flags |= SEC_READONLY;
5951 *ptr_flags |= SEC_DATA;
5955 *ptr_flags |= SEC_CODE;
5960 *ptr_flags |= SEC_LOAD;
5964 einfo (_("%P%F: invalid syntax in flags\n"));
5971 /* Call a function on each input file. This function will be called
5972 on an archive, but not on the elements. */
5975 lang_for_each_input_file (void (*func) (lang_input_statement_type *))
5977 lang_input_statement_type *f;
5979 for (f = (lang_input_statement_type *) input_file_chain.head;
5981 f = (lang_input_statement_type *) f->next_real_file)
5985 /* Call a function on each file. The function will be called on all
5986 the elements of an archive which are included in the link, but will
5987 not be called on the archive file itself. */
5990 lang_for_each_file (void (*func) (lang_input_statement_type *))
5992 LANG_FOR_EACH_INPUT_STATEMENT (f)
5999 ldlang_add_file (lang_input_statement_type *entry)
6001 lang_statement_append (&file_chain,
6002 (lang_statement_union_type *) entry,
6005 /* The BFD linker needs to have a list of all input BFDs involved in
6007 ASSERT (entry->the_bfd->link_next == NULL);
6008 ASSERT (entry->the_bfd != link_info.output_bfd);
6010 *link_info.input_bfds_tail = entry->the_bfd;
6011 link_info.input_bfds_tail = &entry->the_bfd->link_next;
6012 entry->the_bfd->usrdata = entry;
6013 bfd_set_gp_size (entry->the_bfd, g_switch_value);
6015 /* Look through the sections and check for any which should not be
6016 included in the link. We need to do this now, so that we can
6017 notice when the backend linker tries to report multiple
6018 definition errors for symbols which are in sections we aren't
6019 going to link. FIXME: It might be better to entirely ignore
6020 symbols which are defined in sections which are going to be
6021 discarded. This would require modifying the backend linker for
6022 each backend which might set the SEC_LINK_ONCE flag. If we do
6023 this, we should probably handle SEC_EXCLUDE in the same way. */
6025 bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
6029 lang_add_output (const char *name, int from_script)
6031 /* Make -o on command line override OUTPUT in script. */
6032 if (!had_output_filename || !from_script)
6034 output_filename = name;
6035 had_output_filename = TRUE;
6039 static lang_output_section_statement_type *current_section;
6050 for (l = 0; l < 32; l++)
6052 if (i >= (unsigned int) x)
6060 lang_output_section_statement_type *
6061 lang_enter_output_section_statement (const char *output_section_statement_name,
6062 etree_type *address_exp,
6063 enum section_type sectype,
6065 etree_type *subalign,
6069 lang_output_section_statement_type *os;
6071 os = lang_output_section_statement_lookup (output_section_statement_name,
6073 current_section = os;
6075 if (os->addr_tree == NULL)
6077 os->addr_tree = address_exp;
6079 os->sectype = sectype;
6080 if (sectype != noload_section)
6081 os->flags = SEC_NO_FLAGS;
6083 os->flags = SEC_NEVER_LOAD;
6084 os->block_value = 1;
6086 /* Make next things chain into subchain of this. */
6087 push_stat_ptr (&os->children);
6089 os->subsection_alignment =
6090 topower (exp_get_value_int (subalign, -1, "subsection alignment"));
6091 os->section_alignment =
6092 topower (exp_get_value_int (align, -1, "section alignment"));
6094 os->load_base = ebase;
6101 lang_output_statement_type *new_stmt;
6103 new_stmt = new_stat (lang_output_statement, stat_ptr);
6104 new_stmt->name = output_filename;
6108 /* Reset the current counters in the regions. */
6111 lang_reset_memory_regions (void)
6113 lang_memory_region_type *p = lang_memory_region_list;
6115 lang_output_section_statement_type *os;
6117 for (p = lang_memory_region_list; p != NULL; p = p->next)
6119 p->current = p->origin;
6123 for (os = &lang_output_section_statement.head->output_section_statement;
6127 os->processed_vma = FALSE;
6128 os->processed_lma = FALSE;
6131 for (o = link_info.output_bfd->sections; o != NULL; o = o->next)
6133 /* Save the last size for possible use by bfd_relax_section. */
6134 o->rawsize = o->size;
6139 /* Worker for lang_gc_sections_1. */
6142 gc_section_callback (lang_wild_statement_type *ptr,
6143 struct wildcard_list *sec ATTRIBUTE_UNUSED,
6145 lang_input_statement_type *file ATTRIBUTE_UNUSED,
6146 void *data ATTRIBUTE_UNUSED)
6148 /* If the wild pattern was marked KEEP, the member sections
6149 should be as well. */
6150 if (ptr->keep_sections)
6151 section->flags |= SEC_KEEP;
6154 /* Iterate over sections marking them against GC. */
6157 lang_gc_sections_1 (lang_statement_union_type *s)
6159 for (; s != NULL; s = s->header.next)
6161 switch (s->header.type)
6163 case lang_wild_statement_enum:
6164 walk_wild (&s->wild_statement, gc_section_callback, NULL);
6166 case lang_constructors_statement_enum:
6167 lang_gc_sections_1 (constructor_list.head);
6169 case lang_output_section_statement_enum:
6170 lang_gc_sections_1 (s->output_section_statement.children.head);
6172 case lang_group_statement_enum:
6173 lang_gc_sections_1 (s->group_statement.children.head);
6182 lang_gc_sections (void)
6184 /* Keep all sections so marked in the link script. */
6186 lang_gc_sections_1 (statement_list.head);
6188 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
6189 the special case of debug info. (See bfd/stabs.c)
6190 Twiddle the flag here, to simplify later linker code. */
6191 if (link_info.relocatable)
6193 LANG_FOR_EACH_INPUT_STATEMENT (f)
6196 for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next)
6197 if ((sec->flags & SEC_DEBUGGING) == 0)
6198 sec->flags &= ~SEC_EXCLUDE;
6202 if (link_info.gc_sections)
6203 bfd_gc_sections (link_info.output_bfd, &link_info);
6206 /* Worker for lang_find_relro_sections_1. */
6209 find_relro_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
6210 struct wildcard_list *sec ATTRIBUTE_UNUSED,
6212 lang_input_statement_type *file ATTRIBUTE_UNUSED,
6215 /* Discarded, excluded and ignored sections effectively have zero
6217 if (section->output_section != NULL
6218 && section->output_section->owner == link_info.output_bfd
6219 && (section->output_section->flags & SEC_EXCLUDE) == 0
6220 && !IGNORE_SECTION (section)
6221 && section->size != 0)
6223 bfd_boolean *has_relro_section = (bfd_boolean *) data;
6224 *has_relro_section = TRUE;
6228 /* Iterate over sections for relro sections. */
6231 lang_find_relro_sections_1 (lang_statement_union_type *s,
6232 bfd_boolean *has_relro_section)
6234 if (*has_relro_section)
6237 for (; s != NULL; s = s->header.next)
6239 if (s == expld.dataseg.relro_end_stat)
6242 switch (s->header.type)
6244 case lang_wild_statement_enum:
6245 walk_wild (&s->wild_statement,
6246 find_relro_section_callback,
6249 case lang_constructors_statement_enum:
6250 lang_find_relro_sections_1 (constructor_list.head,
6253 case lang_output_section_statement_enum:
6254 lang_find_relro_sections_1 (s->output_section_statement.children.head,
6257 case lang_group_statement_enum:
6258 lang_find_relro_sections_1 (s->group_statement.children.head,
6268 lang_find_relro_sections (void)
6270 bfd_boolean has_relro_section = FALSE;
6272 /* Check all sections in the link script. */
6274 lang_find_relro_sections_1 (expld.dataseg.relro_start_stat,
6275 &has_relro_section);
6277 if (!has_relro_section)
6278 link_info.relro = FALSE;
6281 /* Relax all sections until bfd_relax_section gives up. */
6284 lang_relax_sections (bfd_boolean need_layout)
6286 if (RELAXATION_ENABLED)
6288 /* We may need more than one relaxation pass. */
6289 int i = link_info.relax_pass;
6291 /* The backend can use it to determine the current pass. */
6292 link_info.relax_pass = 0;
6296 /* Keep relaxing until bfd_relax_section gives up. */
6297 bfd_boolean relax_again;
6299 link_info.relax_trip = -1;
6302 link_info.relax_trip++;
6304 /* Note: pe-dll.c does something like this also. If you find
6305 you need to change this code, you probably need to change
6306 pe-dll.c also. DJ */
6308 /* Do all the assignments with our current guesses as to
6310 lang_do_assignments ();
6312 /* We must do this after lang_do_assignments, because it uses
6314 lang_reset_memory_regions ();
6316 /* Perform another relax pass - this time we know where the
6317 globals are, so can make a better guess. */
6318 relax_again = FALSE;
6319 lang_size_sections (&relax_again, FALSE);
6321 while (relax_again);
6323 link_info.relax_pass++;
6330 /* Final extra sizing to report errors. */
6331 lang_do_assignments ();
6332 lang_reset_memory_regions ();
6333 lang_size_sections (NULL, TRUE);
6340 /* Finalize dynamic list. */
6341 if (link_info.dynamic_list)
6342 lang_finalize_version_expr_head (&link_info.dynamic_list->head);
6344 current_target = default_target;
6346 /* Open the output file. */
6347 lang_for_each_statement (ldlang_open_output);
6350 ldemul_create_output_section_statements ();
6352 /* Add to the hash table all undefineds on the command line. */
6353 lang_place_undefineds ();
6355 if (!bfd_section_already_linked_table_init ())
6356 einfo (_("%P%F: Failed to create hash table\n"));
6358 /* Create a bfd for each input file. */
6359 current_target = default_target;
6360 open_input_bfds (statement_list.head, FALSE);
6362 link_info.gc_sym_list = &entry_symbol;
6363 if (entry_symbol.name == NULL)
6364 link_info.gc_sym_list = ldlang_undef_chain_list_head;
6366 ldemul_after_open ();
6368 bfd_section_already_linked_table_free ();
6370 /* Make sure that we're not mixing architectures. We call this
6371 after all the input files have been opened, but before we do any
6372 other processing, so that any operations merge_private_bfd_data
6373 does on the output file will be known during the rest of the
6377 /* Handle .exports instead of a version script if we're told to do so. */
6378 if (command_line.version_exports_section)
6379 lang_do_version_exports_section ();
6381 /* Build all sets based on the information gathered from the input
6383 ldctor_build_sets ();
6385 /* Remove unreferenced sections if asked to. */
6386 lang_gc_sections ();
6388 /* Size up the common data. */
6391 /* Update wild statements. */
6392 update_wild_statements (statement_list.head);
6394 /* Run through the contours of the script and attach input sections
6395 to the correct output sections. */
6396 map_input_to_output_sections (statement_list.head, NULL, NULL);
6398 process_insert_statements ();
6400 /* Find any sections not attached explicitly and handle them. */
6401 lang_place_orphans ();
6403 /* Create sections for data statements and those referenced in
6404 expressions but otherwise empty. */
6405 create_other_output_sections (statement_list.head, NULL);
6407 if (! link_info.relocatable)
6411 /* Merge SEC_MERGE sections. This has to be done after GC of
6412 sections, so that GCed sections are not merged, but before
6413 assigning dynamic symbols, since removing whole input sections
6415 bfd_merge_sections (link_info.output_bfd, &link_info);
6417 /* Look for a text section and set the readonly attribute in it. */
6418 found = bfd_get_section_by_name (link_info.output_bfd, ".text");
6422 if (config.text_read_only)
6423 found->flags |= SEC_READONLY;
6425 found->flags &= ~SEC_READONLY;
6429 /* Do anything special before sizing sections. This is where ELF
6430 and other back-ends size dynamic sections. */
6431 ldemul_before_allocation ();
6433 /* We must record the program headers before we try to fix the
6434 section positions, since they will affect SIZEOF_HEADERS. */
6435 lang_record_phdrs ();
6437 /* Check relro sections. */
6438 if (link_info.relro && ! link_info.relocatable)
6439 lang_find_relro_sections ();
6441 /* Size up the sections. */
6442 lang_size_sections (NULL, ! RELAXATION_ENABLED);
6444 /* See if anything special should be done now we know how big
6445 everything is. This is where relaxation is done. */
6446 ldemul_after_allocation ();
6448 /* Fix any .startof. or .sizeof. symbols. */
6449 lang_set_startof ();
6451 /* Do all the assignments, now that we know the final resting places
6452 of all the symbols. */
6454 lang_do_assignments ();
6458 /* Make sure that the section addresses make sense. */
6459 if (command_line.check_section_addresses)
6460 lang_check_section_addresses ();
6465 /* EXPORTED TO YACC */
6468 lang_add_wild (struct wildcard_spec *filespec,
6469 struct wildcard_list *section_list,
6470 bfd_boolean keep_sections)
6472 struct wildcard_list *curr, *next;
6473 lang_wild_statement_type *new_stmt;
6475 /* Reverse the list as the parser puts it back to front. */
6476 for (curr = section_list, section_list = NULL;
6478 section_list = curr, curr = next)
6480 if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
6481 placed_commons = TRUE;
6484 curr->next = section_list;
6487 if (filespec != NULL && filespec->name != NULL)
6489 if (strcmp (filespec->name, "*") == 0)
6490 filespec->name = NULL;
6491 else if (! wildcardp (filespec->name))
6492 lang_has_input_file = TRUE;
6495 new_stmt = new_stat (lang_wild_statement, stat_ptr);
6496 new_stmt->filename = NULL;
6497 new_stmt->filenames_sorted = FALSE;
6498 if (filespec != NULL)
6500 new_stmt->filename = filespec->name;
6501 new_stmt->filenames_sorted = filespec->sorted == by_name;
6503 new_stmt->section_list = section_list;
6504 new_stmt->keep_sections = keep_sections;
6505 lang_list_init (&new_stmt->children);
6506 analyze_walk_wild_section_handler (new_stmt);
6510 lang_section_start (const char *name, etree_type *address,
6511 const segment_type *segment)
6513 lang_address_statement_type *ad;
6515 ad = new_stat (lang_address_statement, stat_ptr);
6516 ad->section_name = name;
6517 ad->address = address;
6518 ad->segment = segment;
6521 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
6522 because of a -e argument on the command line, or zero if this is
6523 called by ENTRY in a linker script. Command line arguments take
6527 lang_add_entry (const char *name, bfd_boolean cmdline)
6529 if (entry_symbol.name == NULL
6531 || ! entry_from_cmdline)
6533 entry_symbol.name = name;
6534 entry_from_cmdline = cmdline;
6538 /* Set the default start symbol to NAME. .em files should use this,
6539 not lang_add_entry, to override the use of "start" if neither the
6540 linker script nor the command line specifies an entry point. NAME
6541 must be permanently allocated. */
6543 lang_default_entry (const char *name)
6545 entry_symbol_default = name;
6549 lang_add_target (const char *name)
6551 lang_target_statement_type *new_stmt;
6553 new_stmt = new_stat (lang_target_statement, stat_ptr);
6554 new_stmt->target = name;
6558 lang_add_map (const char *name)
6565 map_option_f = TRUE;
6573 lang_add_fill (fill_type *fill)
6575 lang_fill_statement_type *new_stmt;
6577 new_stmt = new_stat (lang_fill_statement, stat_ptr);
6578 new_stmt->fill = fill;
6582 lang_add_data (int type, union etree_union *exp)
6584 lang_data_statement_type *new_stmt;
6586 new_stmt = new_stat (lang_data_statement, stat_ptr);
6587 new_stmt->exp = exp;
6588 new_stmt->type = type;
6591 /* Create a new reloc statement. RELOC is the BFD relocation type to
6592 generate. HOWTO is the corresponding howto structure (we could
6593 look this up, but the caller has already done so). SECTION is the
6594 section to generate a reloc against, or NAME is the name of the
6595 symbol to generate a reloc against. Exactly one of SECTION and
6596 NAME must be NULL. ADDEND is an expression for the addend. */
6599 lang_add_reloc (bfd_reloc_code_real_type reloc,
6600 reloc_howto_type *howto,
6603 union etree_union *addend)
6605 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
6609 p->section = section;
6611 p->addend_exp = addend;
6613 p->addend_value = 0;
6614 p->output_section = NULL;
6615 p->output_offset = 0;
6618 lang_assignment_statement_type *
6619 lang_add_assignment (etree_type *exp)
6621 lang_assignment_statement_type *new_stmt;
6623 new_stmt = new_stat (lang_assignment_statement, stat_ptr);
6624 new_stmt->exp = exp;
6629 lang_add_attribute (enum statement_enum attribute)
6631 new_statement (attribute, sizeof (lang_statement_header_type), stat_ptr);
6635 lang_startup (const char *name)
6637 if (startup_file != NULL)
6639 einfo (_("%P%F: multiple STARTUP files\n"));
6641 first_file->filename = name;
6642 first_file->local_sym_name = name;
6643 first_file->real = TRUE;
6645 startup_file = name;
6649 lang_float (bfd_boolean maybe)
6651 lang_float_flag = maybe;
6655 /* Work out the load- and run-time regions from a script statement, and
6656 store them in *LMA_REGION and *REGION respectively.
6658 MEMSPEC is the name of the run-time region, or the value of
6659 DEFAULT_MEMORY_REGION if the statement didn't specify one.
6660 LMA_MEMSPEC is the name of the load-time region, or null if the
6661 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
6662 had an explicit load address.
6664 It is an error to specify both a load region and a load address. */
6667 lang_get_regions (lang_memory_region_type **region,
6668 lang_memory_region_type **lma_region,
6669 const char *memspec,
6670 const char *lma_memspec,
6671 bfd_boolean have_lma,
6672 bfd_boolean have_vma)
6674 *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
6676 /* If no runtime region or VMA has been specified, but the load region
6677 has been specified, then use the load region for the runtime region
6679 if (lma_memspec != NULL
6681 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
6682 *region = *lma_region;
6684 *region = lang_memory_region_lookup (memspec, FALSE);
6686 if (have_lma && lma_memspec != 0)
6687 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
6691 lang_leave_output_section_statement (fill_type *fill, const char *memspec,
6692 lang_output_section_phdr_list *phdrs,
6693 const char *lma_memspec)
6695 lang_get_regions (¤t_section->region,
6696 ¤t_section->lma_region,
6697 memspec, lma_memspec,
6698 current_section->load_base != NULL,
6699 current_section->addr_tree != NULL);
6701 /* If this section has no load region or base, but has the same
6702 region as the previous section, then propagate the previous
6703 section's load region. */
6705 if (!current_section->lma_region && !current_section->load_base
6706 && current_section->region == current_section->prev->region)
6707 current_section->lma_region = current_section->prev->lma_region;
6709 current_section->fill = fill;
6710 current_section->phdrs = phdrs;
6714 /* Create an absolute symbol with the given name with the value of the
6715 address of first byte of the section named.
6717 If the symbol already exists, then do nothing. */
6720 lang_abs_symbol_at_beginning_of (const char *secname, const char *name)
6722 struct bfd_link_hash_entry *h;
6724 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
6726 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
6728 if (h->type == bfd_link_hash_new
6729 || h->type == bfd_link_hash_undefined)
6733 h->type = bfd_link_hash_defined;
6735 sec = bfd_get_section_by_name (link_info.output_bfd, secname);
6739 h->u.def.value = bfd_get_section_vma (link_info.output_bfd, sec);
6741 h->u.def.section = bfd_abs_section_ptr;
6745 /* Create an absolute symbol with the given name with the value of the
6746 address of the first byte after the end of the section named.
6748 If the symbol already exists, then do nothing. */
6751 lang_abs_symbol_at_end_of (const char *secname, const char *name)
6753 struct bfd_link_hash_entry *h;
6755 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
6757 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
6759 if (h->type == bfd_link_hash_new
6760 || h->type == bfd_link_hash_undefined)
6764 h->type = bfd_link_hash_defined;
6766 sec = bfd_get_section_by_name (link_info.output_bfd, secname);
6770 h->u.def.value = (bfd_get_section_vma (link_info.output_bfd, sec)
6771 + TO_ADDR (sec->size));
6773 h->u.def.section = bfd_abs_section_ptr;
6778 lang_statement_append (lang_statement_list_type *list,
6779 lang_statement_union_type *element,
6780 lang_statement_union_type **field)
6782 *(list->tail) = element;
6786 /* Set the output format type. -oformat overrides scripts. */
6789 lang_add_output_format (const char *format,
6794 if (output_target == NULL || !from_script)
6796 if (command_line.endian == ENDIAN_BIG
6799 else if (command_line.endian == ENDIAN_LITTLE
6803 output_target = format;
6808 lang_add_insert (const char *where, int is_before)
6810 lang_insert_statement_type *new_stmt;
6812 new_stmt = new_stat (lang_insert_statement, stat_ptr);
6813 new_stmt->where = where;
6814 new_stmt->is_before = is_before;
6815 saved_script_handle = previous_script_handle;
6818 /* Enter a group. This creates a new lang_group_statement, and sets
6819 stat_ptr to build new statements within the group. */
6822 lang_enter_group (void)
6824 lang_group_statement_type *g;
6826 g = new_stat (lang_group_statement, stat_ptr);
6827 lang_list_init (&g->children);
6828 push_stat_ptr (&g->children);
6831 /* Leave a group. This just resets stat_ptr to start writing to the
6832 regular list of statements again. Note that this will not work if
6833 groups can occur inside anything else which can adjust stat_ptr,
6834 but currently they can't. */
6837 lang_leave_group (void)
6842 /* Add a new program header. This is called for each entry in a PHDRS
6843 command in a linker script. */
6846 lang_new_phdr (const char *name,
6848 bfd_boolean filehdr,
6853 struct lang_phdr *n, **pp;
6856 n = (struct lang_phdr *) stat_alloc (sizeof (struct lang_phdr));
6859 n->type = exp_get_value_int (type, 0, "program header type");
6860 n->filehdr = filehdr;
6865 hdrs = n->type == 1 && (phdrs || filehdr);
6867 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
6870 && !((*pp)->filehdr || (*pp)->phdrs))
6872 einfo (_("%X%P:%S: PHDRS and FILEHDR are not supported when prior PT_LOAD headers lack them\n"));
6879 /* Record the program header information in the output BFD. FIXME: We
6880 should not be calling an ELF specific function here. */
6883 lang_record_phdrs (void)
6887 lang_output_section_phdr_list *last;
6888 struct lang_phdr *l;
6889 lang_output_section_statement_type *os;
6892 secs = (asection **) xmalloc (alc * sizeof (asection *));
6895 for (l = lang_phdr_list; l != NULL; l = l->next)
6902 for (os = &lang_output_section_statement.head->output_section_statement;
6906 lang_output_section_phdr_list *pl;
6908 if (os->constraint < 0)
6916 if (os->sectype == noload_section
6917 || os->bfd_section == NULL
6918 || (os->bfd_section->flags & SEC_ALLOC) == 0)
6921 /* Don't add orphans to PT_INTERP header. */
6927 lang_output_section_statement_type * tmp_os;
6929 /* If we have not run across a section with a program
6930 header assigned to it yet, then scan forwards to find
6931 one. This prevents inconsistencies in the linker's
6932 behaviour when a script has specified just a single
6933 header and there are sections in that script which are
6934 not assigned to it, and which occur before the first
6935 use of that header. See here for more details:
6936 http://sourceware.org/ml/binutils/2007-02/msg00291.html */
6937 for (tmp_os = os; tmp_os; tmp_os = tmp_os->next)
6940 last = tmp_os->phdrs;
6944 einfo (_("%F%P: no sections assigned to phdrs\n"));
6949 if (os->bfd_section == NULL)
6952 for (; pl != NULL; pl = pl->next)
6954 if (strcmp (pl->name, l->name) == 0)
6959 secs = (asection **) xrealloc (secs,
6960 alc * sizeof (asection *));
6962 secs[c] = os->bfd_section;
6969 if (l->flags == NULL)
6972 flags = exp_get_vma (l->flags, 0, "phdr flags");
6977 at = exp_get_vma (l->at, 0, "phdr load address");
6979 if (! bfd_record_phdr (link_info.output_bfd, l->type,
6980 l->flags != NULL, flags, l->at != NULL,
6981 at, l->filehdr, l->phdrs, c, secs))
6982 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
6987 /* Make sure all the phdr assignments succeeded. */
6988 for (os = &lang_output_section_statement.head->output_section_statement;
6992 lang_output_section_phdr_list *pl;
6994 if (os->constraint < 0
6995 || os->bfd_section == NULL)
6998 for (pl = os->phdrs;
7001 if (! pl->used && strcmp (pl->name, "NONE") != 0)
7002 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
7003 os->name, pl->name);
7007 /* Record a list of sections which may not be cross referenced. */
7010 lang_add_nocrossref (lang_nocrossref_type *l)
7012 struct lang_nocrossrefs *n;
7014 n = (struct lang_nocrossrefs *) xmalloc (sizeof *n);
7015 n->next = nocrossref_list;
7017 nocrossref_list = n;
7019 /* Set notice_all so that we get informed about all symbols. */
7020 link_info.notice_all = TRUE;
7023 /* Overlay handling. We handle overlays with some static variables. */
7025 /* The overlay virtual address. */
7026 static etree_type *overlay_vma;
7027 /* And subsection alignment. */
7028 static etree_type *overlay_subalign;
7030 /* An expression for the maximum section size seen so far. */
7031 static etree_type *overlay_max;
7033 /* A list of all the sections in this overlay. */
7035 struct overlay_list {
7036 struct overlay_list *next;
7037 lang_output_section_statement_type *os;
7040 static struct overlay_list *overlay_list;
7042 /* Start handling an overlay. */
7045 lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
7047 /* The grammar should prevent nested overlays from occurring. */
7048 ASSERT (overlay_vma == NULL
7049 && overlay_subalign == NULL
7050 && overlay_max == NULL);
7052 overlay_vma = vma_expr;
7053 overlay_subalign = subalign;
7056 /* Start a section in an overlay. We handle this by calling
7057 lang_enter_output_section_statement with the correct VMA.
7058 lang_leave_overlay sets up the LMA and memory regions. */
7061 lang_enter_overlay_section (const char *name)
7063 struct overlay_list *n;
7066 lang_enter_output_section_statement (name, overlay_vma, overlay_section,
7067 0, overlay_subalign, 0, 0);
7069 /* If this is the first section, then base the VMA of future
7070 sections on this one. This will work correctly even if `.' is
7071 used in the addresses. */
7072 if (overlay_list == NULL)
7073 overlay_vma = exp_nameop (ADDR, name);
7075 /* Remember the section. */
7076 n = (struct overlay_list *) xmalloc (sizeof *n);
7077 n->os = current_section;
7078 n->next = overlay_list;
7081 size = exp_nameop (SIZEOF, name);
7083 /* Arrange to work out the maximum section end address. */
7084 if (overlay_max == NULL)
7087 overlay_max = exp_binop (MAX_K, overlay_max, size);
7090 /* Finish a section in an overlay. There isn't any special to do
7094 lang_leave_overlay_section (fill_type *fill,
7095 lang_output_section_phdr_list *phdrs)
7102 name = current_section->name;
7104 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
7105 region and that no load-time region has been specified. It doesn't
7106 really matter what we say here, since lang_leave_overlay will
7108 lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
7110 /* Define the magic symbols. */
7112 clean = (char *) xmalloc (strlen (name) + 1);
7114 for (s1 = name; *s1 != '\0'; s1++)
7115 if (ISALNUM (*s1) || *s1 == '_')
7119 buf = (char *) xmalloc (strlen (clean) + sizeof "__load_start_");
7120 sprintf (buf, "__load_start_%s", clean);
7121 lang_add_assignment (exp_provide (buf,
7122 exp_nameop (LOADADDR, name),
7125 buf = (char *) xmalloc (strlen (clean) + sizeof "__load_stop_");
7126 sprintf (buf, "__load_stop_%s", clean);
7127 lang_add_assignment (exp_provide (buf,
7129 exp_nameop (LOADADDR, name),
7130 exp_nameop (SIZEOF, name)),
7136 /* Finish an overlay. If there are any overlay wide settings, this
7137 looks through all the sections in the overlay and sets them. */
7140 lang_leave_overlay (etree_type *lma_expr,
7143 const char *memspec,
7144 lang_output_section_phdr_list *phdrs,
7145 const char *lma_memspec)
7147 lang_memory_region_type *region;
7148 lang_memory_region_type *lma_region;
7149 struct overlay_list *l;
7150 lang_nocrossref_type *nocrossref;
7152 lang_get_regions (®ion, &lma_region,
7153 memspec, lma_memspec,
7154 lma_expr != NULL, FALSE);
7158 /* After setting the size of the last section, set '.' to end of the
7160 if (overlay_list != NULL)
7161 overlay_list->os->update_dot_tree
7162 = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
7167 struct overlay_list *next;
7169 if (fill != NULL && l->os->fill == NULL)
7172 l->os->region = region;
7173 l->os->lma_region = lma_region;
7175 /* The first section has the load address specified in the
7176 OVERLAY statement. The rest are worked out from that.
7177 The base address is not needed (and should be null) if
7178 an LMA region was specified. */
7181 l->os->load_base = lma_expr;
7182 l->os->sectype = normal_section;
7184 if (phdrs != NULL && l->os->phdrs == NULL)
7185 l->os->phdrs = phdrs;
7189 lang_nocrossref_type *nc;
7191 nc = (lang_nocrossref_type *) xmalloc (sizeof *nc);
7192 nc->name = l->os->name;
7193 nc->next = nocrossref;
7202 if (nocrossref != NULL)
7203 lang_add_nocrossref (nocrossref);
7206 overlay_list = NULL;
7210 /* Version handling. This is only useful for ELF. */
7212 /* This global variable holds the version tree that we build. */
7214 struct bfd_elf_version_tree *lang_elf_version_info;
7216 /* If PREV is NULL, return first version pattern matching particular symbol.
7217 If PREV is non-NULL, return first version pattern matching particular
7218 symbol after PREV (previously returned by lang_vers_match). */
7220 static struct bfd_elf_version_expr *
7221 lang_vers_match (struct bfd_elf_version_expr_head *head,
7222 struct bfd_elf_version_expr *prev,
7225 const char *cxx_sym = sym;
7226 const char *java_sym = sym;
7227 struct bfd_elf_version_expr *expr = NULL;
7229 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
7231 cxx_sym = cplus_demangle (sym, DMGL_PARAMS | DMGL_ANSI);
7235 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
7237 java_sym = cplus_demangle (sym, DMGL_JAVA);
7242 if (head->htab && (prev == NULL || prev->literal))
7244 struct bfd_elf_version_expr e;
7246 switch (prev ? prev->mask : 0)
7249 if (head->mask & BFD_ELF_VERSION_C_TYPE)
7252 expr = (struct bfd_elf_version_expr *)
7253 htab_find ((htab_t) head->htab, &e);
7254 while (expr && strcmp (expr->pattern, sym) == 0)
7255 if (expr->mask == BFD_ELF_VERSION_C_TYPE)
7261 case BFD_ELF_VERSION_C_TYPE:
7262 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
7264 e.pattern = cxx_sym;
7265 expr = (struct bfd_elf_version_expr *)
7266 htab_find ((htab_t) head->htab, &e);
7267 while (expr && strcmp (expr->pattern, cxx_sym) == 0)
7268 if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
7274 case BFD_ELF_VERSION_CXX_TYPE:
7275 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
7277 e.pattern = java_sym;
7278 expr = (struct bfd_elf_version_expr *)
7279 htab_find ((htab_t) head->htab, &e);
7280 while (expr && strcmp (expr->pattern, java_sym) == 0)
7281 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
7292 /* Finally, try the wildcards. */
7293 if (prev == NULL || prev->literal)
7294 expr = head->remaining;
7297 for (; expr; expr = expr->next)
7304 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
7307 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
7309 else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
7313 if (fnmatch (expr->pattern, s, 0) == 0)
7319 free ((char *) cxx_sym);
7320 if (java_sym != sym)
7321 free ((char *) java_sym);
7325 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
7326 return a pointer to the symbol name with any backslash quotes removed. */
7329 realsymbol (const char *pattern)
7332 bfd_boolean changed = FALSE, backslash = FALSE;
7333 char *s, *symbol = (char *) xmalloc (strlen (pattern) + 1);
7335 for (p = pattern, s = symbol; *p != '\0'; ++p)
7337 /* It is a glob pattern only if there is no preceding
7341 /* Remove the preceding backslash. */
7348 if (*p == '?' || *p == '*' || *p == '[')
7355 backslash = *p == '\\';
7371 /* This is called for each variable name or match expression. NEW_NAME is
7372 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
7373 pattern to be matched against symbol names. */
7375 struct bfd_elf_version_expr *
7376 lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
7377 const char *new_name,
7379 bfd_boolean literal_p)
7381 struct bfd_elf_version_expr *ret;
7383 ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret);
7387 ret->literal = TRUE;
7388 ret->pattern = literal_p ? new_name : realsymbol (new_name);
7389 if (ret->pattern == NULL)
7391 ret->pattern = new_name;
7392 ret->literal = FALSE;
7395 if (lang == NULL || strcasecmp (lang, "C") == 0)
7396 ret->mask = BFD_ELF_VERSION_C_TYPE;
7397 else if (strcasecmp (lang, "C++") == 0)
7398 ret->mask = BFD_ELF_VERSION_CXX_TYPE;
7399 else if (strcasecmp (lang, "Java") == 0)
7400 ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
7403 einfo (_("%X%P: unknown language `%s' in version information\n"),
7405 ret->mask = BFD_ELF_VERSION_C_TYPE;
7408 return ldemul_new_vers_pattern (ret);
7411 /* This is called for each set of variable names and match
7414 struct bfd_elf_version_tree *
7415 lang_new_vers_node (struct bfd_elf_version_expr *globals,
7416 struct bfd_elf_version_expr *locals)
7418 struct bfd_elf_version_tree *ret;
7420 ret = (struct bfd_elf_version_tree *) xcalloc (1, sizeof *ret);
7421 ret->globals.list = globals;
7422 ret->locals.list = locals;
7423 ret->match = lang_vers_match;
7424 ret->name_indx = (unsigned int) -1;
7428 /* This static variable keeps track of version indices. */
7430 static int version_index;
7433 version_expr_head_hash (const void *p)
7435 const struct bfd_elf_version_expr *e =
7436 (const struct bfd_elf_version_expr *) p;
7438 return htab_hash_string (e->pattern);
7442 version_expr_head_eq (const void *p1, const void *p2)
7444 const struct bfd_elf_version_expr *e1 =
7445 (const struct bfd_elf_version_expr *) p1;
7446 const struct bfd_elf_version_expr *e2 =
7447 (const struct bfd_elf_version_expr *) p2;
7449 return strcmp (e1->pattern, e2->pattern) == 0;
7453 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
7456 struct bfd_elf_version_expr *e, *next;
7457 struct bfd_elf_version_expr **list_loc, **remaining_loc;
7459 for (e = head->list; e; e = e->next)
7463 head->mask |= e->mask;
7468 head->htab = htab_create (count * 2, version_expr_head_hash,
7469 version_expr_head_eq, NULL);
7470 list_loc = &head->list;
7471 remaining_loc = &head->remaining;
7472 for (e = head->list; e; e = next)
7478 remaining_loc = &e->next;
7482 void **loc = htab_find_slot ((htab_t) head->htab, e, INSERT);
7486 struct bfd_elf_version_expr *e1, *last;
7488 e1 = (struct bfd_elf_version_expr *) *loc;
7492 if (e1->mask == e->mask)
7500 while (e1 && strcmp (e1->pattern, e->pattern) == 0);
7504 /* This is a duplicate. */
7505 /* FIXME: Memory leak. Sometimes pattern is not
7506 xmalloced alone, but in larger chunk of memory. */
7507 /* free (e->pattern); */
7512 e->next = last->next;
7520 list_loc = &e->next;
7524 *remaining_loc = NULL;
7525 *list_loc = head->remaining;
7528 head->remaining = head->list;
7531 /* This is called when we know the name and dependencies of the
7535 lang_register_vers_node (const char *name,
7536 struct bfd_elf_version_tree *version,
7537 struct bfd_elf_version_deps *deps)
7539 struct bfd_elf_version_tree *t, **pp;
7540 struct bfd_elf_version_expr *e1;
7545 if ((name[0] == '\0' && lang_elf_version_info != NULL)
7546 || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
7548 einfo (_("%X%P: anonymous version tag cannot be combined"
7549 " with other version tags\n"));
7554 /* Make sure this node has a unique name. */
7555 for (t = lang_elf_version_info; t != NULL; t = t->next)
7556 if (strcmp (t->name, name) == 0)
7557 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
7559 lang_finalize_version_expr_head (&version->globals);
7560 lang_finalize_version_expr_head (&version->locals);
7562 /* Check the global and local match names, and make sure there
7563 aren't any duplicates. */
7565 for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
7567 for (t = lang_elf_version_info; t != NULL; t = t->next)
7569 struct bfd_elf_version_expr *e2;
7571 if (t->locals.htab && e1->literal)
7573 e2 = (struct bfd_elf_version_expr *)
7574 htab_find ((htab_t) t->locals.htab, e1);
7575 while (e2 && strcmp (e1->pattern, e2->pattern) == 0)
7577 if (e1->mask == e2->mask)
7578 einfo (_("%X%P: duplicate expression `%s'"
7579 " in version information\n"), e1->pattern);
7583 else if (!e1->literal)
7584 for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
7585 if (strcmp (e1->pattern, e2->pattern) == 0
7586 && e1->mask == e2->mask)
7587 einfo (_("%X%P: duplicate expression `%s'"
7588 " in version information\n"), e1->pattern);
7592 for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
7594 for (t = lang_elf_version_info; t != NULL; t = t->next)
7596 struct bfd_elf_version_expr *e2;
7598 if (t->globals.htab && e1->literal)
7600 e2 = (struct bfd_elf_version_expr *)
7601 htab_find ((htab_t) t->globals.htab, e1);
7602 while (e2 && strcmp (e1->pattern, e2->pattern) == 0)
7604 if (e1->mask == e2->mask)
7605 einfo (_("%X%P: duplicate expression `%s'"
7606 " in version information\n"),
7611 else if (!e1->literal)
7612 for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
7613 if (strcmp (e1->pattern, e2->pattern) == 0
7614 && e1->mask == e2->mask)
7615 einfo (_("%X%P: duplicate expression `%s'"
7616 " in version information\n"), e1->pattern);
7620 version->deps = deps;
7621 version->name = name;
7622 if (name[0] != '\0')
7625 version->vernum = version_index;
7628 version->vernum = 0;
7630 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
7635 /* This is called when we see a version dependency. */
7637 struct bfd_elf_version_deps *
7638 lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
7640 struct bfd_elf_version_deps *ret;
7641 struct bfd_elf_version_tree *t;
7643 ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret);
7646 for (t = lang_elf_version_info; t != NULL; t = t->next)
7648 if (strcmp (t->name, name) == 0)
7650 ret->version_needed = t;
7655 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
7661 lang_do_version_exports_section (void)
7663 struct bfd_elf_version_expr *greg = NULL, *lreg;
7665 LANG_FOR_EACH_INPUT_STATEMENT (is)
7667 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
7675 contents = (char *) xmalloc (len);
7676 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
7677 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
7680 while (p < contents + len)
7682 greg = lang_new_vers_pattern (greg, p, NULL, FALSE);
7683 p = strchr (p, '\0') + 1;
7686 /* Do not free the contents, as we used them creating the regex. */
7688 /* Do not include this section in the link. */
7689 sec->flags |= SEC_EXCLUDE | SEC_KEEP;
7692 lreg = lang_new_vers_pattern (NULL, "*", NULL, FALSE);
7693 lang_register_vers_node (command_line.version_exports_section,
7694 lang_new_vers_node (greg, lreg), NULL);
7698 lang_add_unique (const char *name)
7700 struct unique_sections *ent;
7702 for (ent = unique_section_list; ent; ent = ent->next)
7703 if (strcmp (ent->name, name) == 0)
7706 ent = (struct unique_sections *) xmalloc (sizeof *ent);
7707 ent->name = xstrdup (name);
7708 ent->next = unique_section_list;
7709 unique_section_list = ent;
7712 /* Append the list of dynamic symbols to the existing one. */
7715 lang_append_dynamic_list (struct bfd_elf_version_expr *dynamic)
7717 if (link_info.dynamic_list)
7719 struct bfd_elf_version_expr *tail;
7720 for (tail = dynamic; tail->next != NULL; tail = tail->next)
7722 tail->next = link_info.dynamic_list->head.list;
7723 link_info.dynamic_list->head.list = dynamic;
7727 struct bfd_elf_dynamic_list *d;
7729 d = (struct bfd_elf_dynamic_list *) xcalloc (1, sizeof *d);
7730 d->head.list = dynamic;
7731 d->match = lang_vers_match;
7732 link_info.dynamic_list = d;
7736 /* Append the list of C++ typeinfo dynamic symbols to the existing
7740 lang_append_dynamic_list_cpp_typeinfo (void)
7742 const char * symbols [] =
7744 "typeinfo name for*",
7747 struct bfd_elf_version_expr *dynamic = NULL;
7750 for (i = 0; i < ARRAY_SIZE (symbols); i++)
7751 dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++",
7754 lang_append_dynamic_list (dynamic);
7757 /* Append the list of C++ operator new and delete dynamic symbols to the
7761 lang_append_dynamic_list_cpp_new (void)
7763 const char * symbols [] =
7768 struct bfd_elf_version_expr *dynamic = NULL;
7771 for (i = 0; i < ARRAY_SIZE (symbols); i++)
7772 dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++",
7775 lang_append_dynamic_list (dynamic);