1 /* Linker command language support.
2 Copyright (C) 1991-2023 Free Software Foundation, Inc.
4 This file is part of the GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
24 #include "libiberty.h"
25 #include "filenames.h"
26 #include "safe-ctype.h"
45 #if BFD_SUPPORTS_PLUGINS
47 #endif /* BFD_SUPPORTS_PLUGINS */
50 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
53 /* Convert between addresses in bytes and sizes in octets.
54 For currently supported targets, octets_per_byte is always a power
55 of two, so we can use shifts. */
56 #define TO_ADDR(X) ((X) >> opb_shift)
57 #define TO_SIZE(X) ((X) << opb_shift)
59 /* Local variables. */
60 static struct obstack stat_obstack
;
61 static struct obstack map_obstack
;
62 static struct obstack pt_obstack
;
64 #define obstack_chunk_alloc xmalloc
65 #define obstack_chunk_free free
66 static const char *entry_symbol_default
= "start";
67 static bool map_head_is_link_order
= false;
68 static lang_output_section_statement_type
*default_common_section
;
69 static bool map_option_f
;
70 static bfd_vma print_dot
;
71 static lang_input_statement_type
*first_file
;
72 static const char *current_target
;
73 static lang_statement_list_type
*stat_save
[10];
74 static lang_statement_list_type
**stat_save_ptr
= &stat_save
[0];
75 static struct unique_sections
*unique_section_list
;
76 static struct asneeded_minfo
*asneeded_list_head
;
77 static unsigned int opb_shift
= 0;
79 /* Forward declarations. */
80 static void exp_init_os (etree_type
*);
81 static lang_input_statement_type
*lookup_name (const char *);
82 static void insert_undefined (const char *);
83 static bool sort_def_symbol (struct bfd_link_hash_entry
*, void *);
84 static lang_statement_union_type
*new_statement (enum statement_enum type
,
86 lang_statement_list_type
*list
);
87 static void print_statement (lang_statement_union_type
*,
88 lang_output_section_statement_type
*);
89 static void print_statement_list (lang_statement_union_type
*,
90 lang_output_section_statement_type
*);
91 static void print_statements (void);
92 static void print_input_section (asection
*, bool);
93 static bool lang_one_common (struct bfd_link_hash_entry
*, void *);
94 static void lang_record_phdrs (void);
95 static void lang_do_version_exports_section (void);
96 static void lang_finalize_version_expr_head
97 (struct bfd_elf_version_expr_head
*);
98 static void lang_do_memory_regions (bool);
100 /* Exported variables. */
101 const char *output_target
;
102 lang_output_section_statement_type
*abs_output_section
;
103 /* Header for list of statements corresponding to any files involved in the
104 link, either specified from the command-line or added implicitely (eg.
105 archive member used to resolved undefined symbol, wildcard statement from
106 linker script, etc.). Next pointer is in next field of a
107 lang_statement_header_type (reached via header field in a
108 lang_statement_union). */
109 lang_statement_list_type statement_list
;
110 lang_statement_list_type lang_os_list
;
111 lang_statement_list_type
*stat_ptr
= &statement_list
;
112 /* Header for list of statements corresponding to files used in the final
113 executable. This can be either object file specified on the command-line
114 or library member resolving an undefined reference. Next pointer is in next
115 field of a lang_input_statement_type (reached via input_statement field in a
116 lang_statement_union). */
117 lang_statement_list_type file_chain
= { NULL
, NULL
};
118 /* Header for list of statements corresponding to files specified on the
119 command-line for linking. It thus contains real object files and archive
120 but not archive members. Next pointer is in next_real_file field of a
121 lang_input_statement_type statement (reached via input_statement field in a
122 lang_statement_union). */
123 lang_statement_list_type input_file_chain
;
124 static const char *current_input_file
;
125 struct bfd_elf_dynamic_list
**current_dynamic_list_p
;
126 struct bfd_sym_chain entry_symbol
= { NULL
, NULL
};
127 const char *entry_section
= ".text";
128 struct lang_input_statement_flags input_flags
;
129 bool entry_from_cmdline
;
130 bool lang_has_input_file
= false;
131 bool had_output_filename
= false;
132 bool lang_float_flag
= false;
133 bool delete_output_file_on_failure
= false;
134 struct lang_phdr
*lang_phdr_list
;
135 struct lang_nocrossrefs
*nocrossref_list
;
136 struct asneeded_minfo
**asneeded_list_tail
;
138 static ctf_dict_t
*ctf_output
;
141 /* Functions that traverse the linker script and might evaluate
142 DEFINED() need to increment this at the start of the traversal. */
143 int lang_statement_iteration
= 0;
145 /* Count times through one_lang_size_sections_pass after mark phase. */
146 static int lang_sizing_iteration
= 0;
148 /* Return TRUE if the PATTERN argument is a wildcard pattern.
149 Although backslashes are treated specially if a pattern contains
150 wildcards, we do not consider the mere presence of a backslash to
151 be enough to cause the pattern to be treated as a wildcard.
152 That lets us handle DOS filenames more naturally. */
153 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
155 #define new_stat(x, y) \
156 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
158 #define outside_section_address(q) \
159 ((q)->output_offset + (q)->output_section->vma)
161 #define outside_symbol_address(q) \
162 ((q)->value + outside_section_address (q->section))
164 /* CTF sections smaller than this are not compressed: compression of
165 dictionaries this small doesn't gain much, and this lets consumers mmap the
166 sections directly out of the ELF file and use them with no decompression
167 overhead if they want to. */
168 #define CTF_COMPRESSION_THRESHOLD 4096
171 stat_alloc (size_t size
)
173 return obstack_alloc (&stat_obstack
, size
);
176 /* Code for handling simple wildcards without going through fnmatch,
177 which can be expensive because of charset translations etc. */
179 /* A simple wild is a literal string followed by a single '*',
180 where the literal part is at least 4 characters long. */
183 is_simple_wild (const char *name
)
185 size_t len
= strcspn (name
, "*?[");
186 return len
>= 4 && name
[len
] == '*' && name
[len
+ 1] == '\0';
190 match_simple_wild (const char *pattern
, const char *name
)
192 /* The first four characters of the pattern are guaranteed valid
193 non-wildcard characters. So we can go faster. */
194 if (pattern
[0] != name
[0] || pattern
[1] != name
[1]
195 || pattern
[2] != name
[2] || pattern
[3] != name
[3])
200 while (*pattern
!= '*')
201 if (*name
++ != *pattern
++)
208 name_match (const char *pattern
, const char *name
)
210 if (is_simple_wild (pattern
))
211 return !match_simple_wild (pattern
, name
);
212 if (wildcardp (pattern
))
213 return fnmatch (pattern
, name
, 0);
214 return strcmp (pattern
, name
);
217 /* Given an analyzed wildcard_spec SPEC, match it against NAME,
218 returns zero on a match, non-zero if there's no match. */
221 spec_match (const struct wildcard_spec
*spec
, const char *name
)
223 size_t nl
= spec
->namelen
;
224 size_t pl
= spec
->prefixlen
;
225 size_t sl
= spec
->suffixlen
;
226 size_t inputlen
= strlen (name
);
234 r
= memcmp (spec
->name
, name
, pl
);
244 r
= memcmp (spec
->name
+ nl
- sl
, name
+ inputlen
- sl
, sl
);
249 if (nl
== pl
+ sl
+ 1 && spec
->name
[pl
] == '*')
253 return fnmatch (spec
->name
+ pl
, name
+ pl
, 0);
262 ldirname (const char *name
)
264 const char *base
= lbasename (name
);
267 while (base
> name
&& IS_DIR_SEPARATOR (base
[-1]))
271 dirname
= strdup (name
);
272 dirname
[base
- name
] = '\0';
276 /* If PATTERN is of the form archive:file, return a pointer to the
277 separator. If not, return NULL. */
280 archive_path (const char *pattern
)
284 if (link_info
.path_separator
== 0)
287 p
= strchr (pattern
, link_info
.path_separator
);
288 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
289 if (p
== NULL
|| link_info
.path_separator
!= ':')
292 /* Assume a match on the second char is part of drive specifier,
293 as in "c:\silly.dos". */
294 if (p
== pattern
+ 1 && ISALPHA (*pattern
))
295 p
= strchr (p
+ 1, link_info
.path_separator
);
300 /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path,
301 return whether F matches FILE_SPEC. */
304 input_statement_is_archive_path (const char *file_spec
, char *sep
,
305 lang_input_statement_type
*f
)
310 || name_match (sep
+ 1, f
->filename
) == 0)
311 && ((sep
!= file_spec
)
312 == (f
->the_bfd
!= NULL
&& f
->the_bfd
->my_archive
!= NULL
)))
316 if (sep
!= file_spec
)
318 const char *aname
= bfd_get_filename (f
->the_bfd
->my_archive
);
320 match
= name_match (file_spec
, aname
) == 0;
321 *sep
= link_info
.path_separator
;
328 unique_section_p (const asection
*sec
,
329 const lang_output_section_statement_type
*os
)
331 struct unique_sections
*unam
;
334 if (!link_info
.resolve_section_groups
335 && sec
->owner
!= NULL
336 && bfd_is_group_section (sec
->owner
, sec
))
338 && strcmp (os
->name
, DISCARD_SECTION_NAME
) == 0);
341 for (unam
= unique_section_list
; unam
; unam
= unam
->next
)
342 if (name_match (unam
->name
, secnam
) == 0)
348 /* Generic traversal routines for finding matching sections. */
350 /* Return true if FILE matches a pattern in EXCLUDE_LIST, otherwise return
354 walk_wild_file_in_exclude_list (struct name_list
*exclude_list
,
355 lang_input_statement_type
*file
)
357 struct name_list
*list_tmp
;
359 for (list_tmp
= exclude_list
;
361 list_tmp
= list_tmp
->next
)
363 char *p
= archive_path (list_tmp
->name
);
367 if (input_statement_is_archive_path (list_tmp
->name
, p
, file
))
371 else if (name_match (list_tmp
->name
, file
->filename
) == 0)
374 /* FIXME: Perhaps remove the following at some stage? Matching
375 unadorned archives like this was never documented and has
376 been superceded by the archive:path syntax. */
377 else if (file
->the_bfd
!= NULL
378 && file
->the_bfd
->my_archive
!= NULL
379 && name_match (list_tmp
->name
,
380 bfd_get_filename (file
->the_bfd
->my_archive
)) == 0)
387 /* Add SECTION (from input FILE) to the list of matching sections
388 within PTR (the matching wildcard is SEC). */
391 add_matching_section (lang_wild_statement_type
*ptr
,
392 struct wildcard_list
*sec
,
394 lang_input_statement_type
*file
)
396 lang_input_matcher_type
*new_section
;
397 /* Add a section reference to the list. */
398 new_section
= new_stat (lang_input_matcher
, &ptr
->matching_sections
);
399 new_section
->section
= section
;
400 new_section
->pattern
= sec
;
401 new_section
->input_stmt
= file
;
404 /* Process section S (from input file FILE) in relation to wildcard
405 statement PTR. We already know that a prefix of the name of S matches
406 some wildcard in PTR's wildcard list. Here we check if the filename
407 matches as well (if it's specified) and if any of the wildcards in fact
411 walk_wild_section_match (lang_wild_statement_type
*ptr
,
412 lang_input_statement_type
*file
,
415 struct wildcard_list
*sec
;
416 const char *file_spec
= ptr
->filename
;
419 /* Check if filenames match. */
420 if (file_spec
== NULL
)
422 else if ((p
= archive_path (file_spec
)) != NULL
)
424 if (!input_statement_is_archive_path (file_spec
, p
, file
))
427 else if (wildcardp (file_spec
))
429 if (fnmatch (file_spec
, file
->filename
, 0) != 0)
434 lang_input_statement_type
*f
;
435 /* Perform the iteration over a single file. */
436 f
= lookup_name (file_spec
);
441 /* If filename is excluded we're done. */
442 if (walk_wild_file_in_exclude_list (ptr
->exclude_name_list
, file
))
445 /* Check section name against each wildcard spec. If there's no
446 wildcard all sections match. */
447 sec
= ptr
->section_list
;
449 add_matching_section (ptr
, sec
, s
, file
);
452 const char *sname
= bfd_section_name (s
);
453 for (; sec
!= NULL
; sec
= sec
->next
)
455 if (sec
->spec
.name
!= NULL
456 && spec_match (&sec
->spec
, sname
) != 0)
459 /* Don't process sections from files which were excluded. */
460 if (!walk_wild_file_in_exclude_list (sec
->spec
.exclude_name_list
,
462 add_matching_section (ptr
, sec
, s
, file
);
467 /* Return the numerical value of the init_priority attribute from
468 section name NAME. */
471 get_init_priority (const asection
*sec
)
473 const char *name
= bfd_section_name (sec
);
476 /* GCC uses the following section names for the init_priority
477 attribute with numerical values 101 to 65535 inclusive. A
478 lower value means a higher priority.
480 1: .init_array.NNNNN/.fini_array.NNNNN: Where NNNNN is the
481 decimal numerical value of the init_priority attribute.
482 The order of execution in .init_array is forward and
483 .fini_array is backward.
484 2: .ctors.NNNNN/.dtors.NNNNN: Where NNNNN is 65535 minus the
485 decimal numerical value of the init_priority attribute.
486 The order of execution in .ctors is backward and .dtors
489 .init_array.NNNNN sections would normally be placed in an output
490 .init_array section, .fini_array.NNNNN in .fini_array,
491 .ctors.NNNNN in .ctors, and .dtors.NNNNN in .dtors. This means
492 we should sort by increasing number (and could just use
493 SORT_BY_NAME in scripts). However if .ctors.NNNNN sections are
494 being placed in .init_array (which may also contain
495 .init_array.NNNNN sections) or .dtors.NNNNN sections are being
496 placed in .fini_array then we need to extract the init_priority
497 attribute and sort on that. */
498 dot
= strrchr (name
, '.');
499 if (dot
!= NULL
&& ISDIGIT (dot
[1]))
502 unsigned long init_priority
= strtoul (dot
+ 1, &end
, 10);
506 && (strncmp (name
, ".ctors", 6) == 0
507 || strncmp (name
, ".dtors", 6) == 0))
508 init_priority
= 65535 - init_priority
;
509 if (init_priority
<= INT_MAX
)
510 return init_priority
;
516 /* Compare sections ASEC and BSEC according to SORT. */
519 compare_section (sort_type sort
, asection
*asec
, asection
*bsec
)
522 int a_priority
, b_priority
;
529 case by_init_priority
:
530 a_priority
= get_init_priority (asec
);
531 b_priority
= get_init_priority (bsec
);
532 if (a_priority
< 0 || b_priority
< 0)
534 ret
= a_priority
- b_priority
;
540 case by_alignment_name
:
541 ret
= bfd_section_alignment (bsec
) - bfd_section_alignment (asec
);
548 ret
= strcmp (bfd_section_name (asec
), bfd_section_name (bsec
));
551 case by_name_alignment
:
552 ret
= strcmp (bfd_section_name (asec
), bfd_section_name (bsec
));
558 ret
= bfd_section_alignment (bsec
) - bfd_section_alignment (asec
);
565 /* PE puts the sort key in the input statement. */
568 sort_filename (bfd
*abfd
)
570 lang_input_statement_type
*is
= bfd_usrdata (abfd
);
573 return bfd_get_filename (abfd
);
576 /* Handle wildcard sorting. This returns the place in a binary search tree
577 where this FILE:SECTION should be inserted for wild statement WILD where
578 the spec SEC was the matching one. The tree is later linearized. */
580 static lang_section_bst_type
**
581 wild_sort (lang_wild_statement_type
*wild
,
582 struct wildcard_list
*sec
,
583 lang_input_statement_type
*file
,
586 lang_section_bst_type
**tree
;
588 if (!wild
->filenames_sorted
589 && (sec
== NULL
|| sec
->spec
.sorted
== none
590 || sec
->spec
.sorted
== by_none
))
592 /* We might be called even if _this_ spec doesn't need sorting,
593 in which case we simply append at the right end of tree. */
594 return wild
->rightmost
;
600 /* Sorting by filename takes precedence over sorting by section
603 if (wild
->filenames_sorted
)
608 asection
*lsec
= (*tree
)->section
;
610 /* The PE support for the .idata section as generated by
611 dlltool assumes that files will be sorted by the name of
612 the archive and then the name of the file within the
615 fa
= file
->the_bfd
->my_archive
!= NULL
;
617 fn
= sort_filename (file
->the_bfd
->my_archive
);
619 fn
= sort_filename (file
->the_bfd
);
621 la
= lsec
->owner
->my_archive
!= NULL
;
623 ln
= sort_filename (lsec
->owner
->my_archive
);
625 ln
= sort_filename (lsec
->owner
);
627 i
= filename_cmp (fn
, ln
);
629 { tree
= &((*tree
)->right
); continue; }
631 { tree
= &((*tree
)->left
); continue; }
636 fn
= sort_filename (file
->the_bfd
);
638 ln
= sort_filename (lsec
->owner
);
640 i
= filename_cmp (fn
, ln
);
642 { tree
= &((*tree
)->right
); continue; }
644 { tree
= &((*tree
)->left
); continue; }
648 /* Here either the files are not sorted by name, or we are
649 looking at the sections for this file. */
651 /* Find the correct node to append this section. */
652 if (sec
&& sec
->spec
.sorted
!= none
&& sec
->spec
.sorted
!= by_none
653 && compare_section (sec
->spec
.sorted
, section
, (*tree
)->section
) < 0)
654 tree
= &((*tree
)->left
);
656 tree
= &((*tree
)->right
);
662 /* Use wild_sort to build a BST to sort sections. */
665 output_section_callback_sort (lang_wild_statement_type
*ptr
,
666 struct wildcard_list
*sec
,
668 lang_input_statement_type
*file
,
671 lang_section_bst_type
*node
;
672 lang_section_bst_type
**tree
;
673 lang_output_section_statement_type
*os
;
675 os
= (lang_output_section_statement_type
*) output
;
677 if (unique_section_p (section
, os
))
680 node
= (lang_section_bst_type
*) xmalloc (sizeof (lang_section_bst_type
));
683 node
->section
= section
;
684 node
->pattern
= ptr
->section_list
;
686 tree
= wild_sort (ptr
, sec
, file
, section
);
690 if (tree
== ptr
->rightmost
)
691 ptr
->rightmost
= &node
->right
;
695 /* Convert a sorted sections' BST back to list form. */
698 output_section_callback_tree_to_list (lang_wild_statement_type
*ptr
,
699 lang_section_bst_type
*tree
,
703 output_section_callback_tree_to_list (ptr
, tree
->left
, output
);
705 lang_add_section (&ptr
->children
, tree
->section
, tree
->pattern
,
706 ptr
->section_flag_list
,
707 (lang_output_section_statement_type
*) output
);
710 output_section_callback_tree_to_list (ptr
, tree
->right
, output
);
716 /* Sections are matched against wildcard statements via a prefix tree.
717 The prefix tree holds prefixes of all matching patterns (up to the first
718 wildcard character), and the wild statement from which those patterns
719 came. When matching a section name against the tree we're walking through
720 the tree character by character. Each statement we hit is one that
721 potentially matches. This is checked by actually going through the
722 (glob) matching routines.
724 When the section name turns out to actually match we record that section
725 in the wild statements list of matching sections. */
727 /* A prefix can be matched by multiple statement, so we need a list of them. */
728 struct wild_stmt_list
730 lang_wild_statement_type
*stmt
;
731 struct wild_stmt_list
*next
;
734 /* The prefix tree itself. */
737 /* The list of all children (linked via .next). */
738 struct prefixtree
*child
;
739 struct prefixtree
*next
;
740 /* This tree node is responsible for the prefix of parent plus 'c'. */
742 /* The statements that potentially can match this prefix. */
743 struct wild_stmt_list
*stmt
;
746 /* We always have a root node in the prefix tree. It corresponds to the
747 empty prefix. E.g. a glob like "*" would sit in this root. */
748 static struct prefixtree the_root
, *ptroot
= &the_root
;
750 /* Given a prefix tree in *TREE, corresponding to prefix P, find or
751 INSERT the tree node corresponding to prefix P+C. */
753 static struct prefixtree
*
754 get_prefix_tree (struct prefixtree
**tree
, char c
, bool insert
)
756 struct prefixtree
*t
;
757 for (t
= *tree
; t
; t
= t
->next
)
762 t
= (struct prefixtree
*) obstack_alloc (&pt_obstack
, sizeof *t
);
771 /* Add STMT to the set of statements that can be matched by the prefix
772 corresponding to prefix tree T. */
775 pt_add_stmt (struct prefixtree
*t
, lang_wild_statement_type
*stmt
)
777 struct wild_stmt_list
*sl
, **psl
;
778 sl
= (struct wild_stmt_list
*) obstack_alloc (&pt_obstack
, sizeof *sl
);
787 /* Insert STMT into the global prefix tree. */
790 insert_prefix_tree (lang_wild_statement_type
*stmt
)
792 struct wildcard_list
*sec
;
793 struct prefixtree
*t
;
795 if (!stmt
->section_list
)
797 /* If we have no section_list (no wildcards in the wild STMT),
798 then every section name will match, so add this to the root. */
799 pt_add_stmt (ptroot
, stmt
);
803 for (sec
= stmt
->section_list
; sec
; sec
= sec
->next
)
805 const char *name
= sec
->spec
.name
? sec
->spec
.name
: "*";
808 for (; (c
= *name
); name
++)
810 if (c
== '*' || c
== '[' || c
== '?')
812 t
= get_prefix_tree (&t
->child
, c
, true);
814 /* If we hit a glob character, the matching prefix is what we saw
815 until now. If we hit the end of pattern (hence it's no glob) then
816 we can do better: we only need to record a match when a section name
817 completely matches, not merely a prefix, so record the trailing 0
820 t
= get_prefix_tree (&t
->child
, 0, true);
821 pt_add_stmt (t
, stmt
);
825 /* Dump T indented by INDENT spaces. */
828 debug_prefix_tree_rec (struct prefixtree
*t
, int indent
)
830 for (; t
; t
= t
->next
)
832 struct wild_stmt_list
*sl
;
833 printf ("%*s %c", indent
, "", t
->c
);
834 for (sl
= t
->stmt
; sl
; sl
= sl
->next
)
836 struct wildcard_list
*curr
;
837 printf (" %p ", sl
->stmt
);
838 for (curr
= sl
->stmt
->section_list
; curr
; curr
= curr
->next
)
839 printf ("%s ", curr
->spec
.name
? curr
->spec
.name
: "*");
842 debug_prefix_tree_rec (t
->child
, indent
+ 2);
846 /* Dump the global prefix tree. */
849 debug_prefix_tree (void)
851 debug_prefix_tree_rec (ptroot
, 2);
854 /* Like strcspn() but start to look from the end to beginning of
855 S. Returns the length of the suffix of S consisting entirely
856 of characters not in REJECT. */
859 rstrcspn (const char *s
, const char *reject
)
861 size_t len
= strlen (s
), sufflen
= 0;
865 if (strchr (reject
, c
) != 0)
872 /* Analyze the wildcards in wild statement PTR to setup various
873 things for quick matching. */
876 analyze_walk_wild_section_handler (lang_wild_statement_type
*ptr
)
878 struct wildcard_list
*sec
;
881 ptr
->rightmost
= &ptr
->tree
;
883 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
887 sec
->spec
.namelen
= strlen (sec
->spec
.name
);
888 sec
->spec
.prefixlen
= strcspn (sec
->spec
.name
, "?*[");
889 sec
->spec
.suffixlen
= rstrcspn (sec
->spec
.name
+ sec
->spec
.prefixlen
,
893 sec
->spec
.namelen
= sec
->spec
.prefixlen
= sec
->spec
.suffixlen
= 0;
896 insert_prefix_tree (ptr
);
899 /* Match all sections from FILE against the global prefix tree,
900 and record them into each wild statement that has a match. */
903 resolve_wild_sections (lang_input_statement_type
*file
)
907 if (file
->flags
.just_syms
)
910 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
912 const char *sname
= bfd_section_name (s
);
914 struct prefixtree
*t
= ptroot
;
915 //printf (" YYY consider %s of %s\n", sname, file->the_bfd->filename);
920 struct wild_stmt_list
*sl
;
921 for (sl
= t
->stmt
; sl
; sl
= sl
->next
)
923 walk_wild_section_match (sl
->stmt
, file
, s
);
924 //printf (" ZZZ maybe place into %p\n", sl->stmt);
930 t
= get_prefix_tree (&t
->child
, c
, false);
936 /* Match all sections from all input files against the global prefix tree. */
941 LANG_FOR_EACH_INPUT_STATEMENT (f
)
943 //printf("XXX %s\n", f->filename);
944 if (f
->the_bfd
== NULL
945 || !bfd_check_format (f
->the_bfd
, bfd_archive
))
946 resolve_wild_sections (f
);
951 /* This is an archive file. We must map each member of the
952 archive separately. */
953 member
= bfd_openr_next_archived_file (f
->the_bfd
, NULL
);
954 while (member
!= NULL
)
956 /* When lookup_name is called, it will call the add_symbols
957 entry point for the archive. For each element of the
958 archive which is included, BFD will call ldlang_add_file,
959 which will set the usrdata field of the member to the
960 lang_input_statement. */
961 if (bfd_usrdata (member
) != NULL
)
962 resolve_wild_sections (bfd_usrdata (member
));
964 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
970 /* For each input section that matches wild statement S calls
971 CALLBACK with DATA. */
974 walk_wild (lang_wild_statement_type
*s
, callback_t callback
, void *data
)
976 lang_statement_union_type
*l
;
978 for (l
= s
->matching_sections
.head
; l
; l
= l
->header
.next
)
980 (*callback
) (s
, l
->input_matcher
.pattern
, l
->input_matcher
.section
,
981 l
->input_matcher
.input_stmt
, data
);
985 /* lang_for_each_statement walks the parse tree and calls the provided
986 function for each node, except those inside output section statements
987 with constraint set to -1. */
990 lang_for_each_statement_worker (void (*func
) (lang_statement_union_type
*),
991 lang_statement_union_type
*s
)
993 for (; s
!= NULL
; s
= s
->header
.next
)
997 switch (s
->header
.type
)
999 case lang_constructors_statement_enum
:
1000 lang_for_each_statement_worker (func
, constructor_list
.head
);
1002 case lang_output_section_statement_enum
:
1003 if (s
->output_section_statement
.constraint
!= -1)
1004 lang_for_each_statement_worker
1005 (func
, s
->output_section_statement
.children
.head
);
1007 case lang_wild_statement_enum
:
1008 lang_for_each_statement_worker (func
,
1009 s
->wild_statement
.children
.head
);
1011 case lang_group_statement_enum
:
1012 lang_for_each_statement_worker (func
,
1013 s
->group_statement
.children
.head
);
1015 case lang_data_statement_enum
:
1016 case lang_reloc_statement_enum
:
1017 case lang_object_symbols_statement_enum
:
1018 case lang_output_statement_enum
:
1019 case lang_target_statement_enum
:
1020 case lang_input_section_enum
:
1021 case lang_input_statement_enum
:
1022 case lang_assignment_statement_enum
:
1023 case lang_padding_statement_enum
:
1024 case lang_address_statement_enum
:
1025 case lang_fill_statement_enum
:
1026 case lang_insert_statement_enum
:
1036 lang_for_each_statement (void (*func
) (lang_statement_union_type
*))
1038 lang_for_each_statement_worker (func
, statement_list
.head
);
1041 /*----------------------------------------------------------------------*/
1044 lang_list_init (lang_statement_list_type
*list
)
1047 list
->tail
= &list
->head
;
1051 lang_statement_append (lang_statement_list_type
*list
,
1055 *(list
->tail
) = element
;
1060 push_stat_ptr (lang_statement_list_type
*new_ptr
)
1062 if (stat_save_ptr
>= stat_save
+ sizeof (stat_save
) / sizeof (stat_save
[0]))
1064 *stat_save_ptr
++ = stat_ptr
;
1071 if (stat_save_ptr
<= stat_save
)
1073 stat_ptr
= *--stat_save_ptr
;
1076 /* Build a new statement node for the parse tree. */
1078 static lang_statement_union_type
*
1079 new_statement (enum statement_enum type
,
1081 lang_statement_list_type
*list
)
1083 lang_statement_union_type
*new_stmt
;
1085 new_stmt
= stat_alloc (size
);
1086 new_stmt
->header
.type
= type
;
1087 new_stmt
->header
.next
= NULL
;
1088 lang_statement_append (list
, new_stmt
, &new_stmt
->header
.next
);
1092 /* Build a new input file node for the language. There are several
1093 ways in which we treat an input file, eg, we only look at symbols,
1094 or prefix it with a -l etc.
1096 We can be supplied with requests for input files more than once;
1097 they may, for example be split over several lines like foo.o(.text)
1098 foo.o(.data) etc, so when asked for a file we check that we haven't
1099 got it already so we don't duplicate the bfd. */
1101 static lang_input_statement_type
*
1102 new_afile (const char *name
,
1103 lang_input_file_enum_type file_type
,
1105 const char *from_filename
)
1107 lang_input_statement_type
*p
;
1109 lang_has_input_file
= true;
1111 p
= new_stat (lang_input_statement
, stat_ptr
);
1112 memset (&p
->the_bfd
, 0,
1113 sizeof (*p
) - offsetof (lang_input_statement_type
, the_bfd
));
1114 p
->extra_search_path
= NULL
;
1116 p
->flags
.dynamic
= input_flags
.dynamic
;
1117 p
->flags
.add_DT_NEEDED_for_dynamic
= input_flags
.add_DT_NEEDED_for_dynamic
;
1118 p
->flags
.add_DT_NEEDED_for_regular
= input_flags
.add_DT_NEEDED_for_regular
;
1119 p
->flags
.whole_archive
= input_flags
.whole_archive
;
1120 p
->flags
.sysrooted
= input_flags
.sysrooted
;
1125 case lang_input_file_is_symbols_only_enum
:
1127 p
->local_sym_name
= name
;
1128 p
->flags
.real
= true;
1129 p
->flags
.just_syms
= true;
1131 case lang_input_file_is_fake_enum
:
1133 p
->local_sym_name
= name
;
1135 case lang_input_file_is_l_enum
:
1136 if (name
[0] == ':' && name
[1] != '\0')
1138 p
->filename
= name
+ 1;
1139 p
->flags
.full_name_provided
= true;
1143 p
->local_sym_name
= concat ("-l", name
, (const char *) NULL
);
1144 p
->flags
.maybe_archive
= true;
1145 p
->flags
.real
= true;
1146 p
->flags
.search_dirs
= true;
1148 case lang_input_file_is_marker_enum
:
1150 p
->local_sym_name
= name
;
1151 p
->flags
.search_dirs
= true;
1153 case lang_input_file_is_search_file_enum
:
1155 p
->local_sym_name
= name
;
1156 /* If name is a relative path, search the directory of the current linker
1158 if (from_filename
&& !IS_ABSOLUTE_PATH (name
))
1159 p
->extra_search_path
= ldirname (from_filename
);
1160 p
->flags
.real
= true;
1161 p
->flags
.search_dirs
= true;
1163 case lang_input_file_is_file_enum
:
1165 p
->local_sym_name
= name
;
1166 p
->flags
.real
= true;
1172 lang_statement_append (&input_file_chain
, p
, &p
->next_real_file
);
1176 lang_input_statement_type
*
1177 lang_add_input_file (const char *name
,
1178 lang_input_file_enum_type file_type
,
1182 && (*name
== '=' || startswith (name
, "$SYSROOT")))
1184 lang_input_statement_type
*ret
;
1185 char *sysrooted_name
1186 = concat (ld_sysroot
,
1187 name
+ (*name
== '=' ? 1 : strlen ("$SYSROOT")),
1188 (const char *) NULL
);
1190 /* We've now forcibly prepended the sysroot, making the input
1191 file independent of the context. Therefore, temporarily
1192 force a non-sysrooted context for this statement, so it won't
1193 get the sysroot prepended again when opened. (N.B. if it's a
1194 script, any child nodes with input files starting with "/"
1195 will be handled as "sysrooted" as they'll be found to be
1196 within the sysroot subdirectory.) */
1197 unsigned int outer_sysrooted
= input_flags
.sysrooted
;
1198 input_flags
.sysrooted
= 0;
1199 ret
= new_afile (sysrooted_name
, file_type
, target
, NULL
);
1200 input_flags
.sysrooted
= outer_sysrooted
;
1204 return new_afile (name
, file_type
, target
, current_input_file
);
1207 struct out_section_hash_entry
1209 struct bfd_hash_entry root
;
1210 lang_statement_union_type s
;
1213 /* The hash table. */
1215 static struct bfd_hash_table output_section_statement_table
;
1217 /* Support routines for the hash table used by lang_output_section_find,
1218 initialize the table, fill in an entry and remove the table. */
1220 static struct bfd_hash_entry
*
1221 output_section_statement_newfunc (struct bfd_hash_entry
*entry
,
1222 struct bfd_hash_table
*table
,
1225 lang_output_section_statement_type
**nextp
;
1226 struct out_section_hash_entry
*ret
;
1230 entry
= (struct bfd_hash_entry
*) bfd_hash_allocate (table
,
1236 entry
= bfd_hash_newfunc (entry
, table
, string
);
1240 ret
= (struct out_section_hash_entry
*) entry
;
1241 memset (&ret
->s
, 0, sizeof (ret
->s
));
1242 ret
->s
.header
.type
= lang_output_section_statement_enum
;
1243 ret
->s
.output_section_statement
.subsection_alignment
= NULL
;
1244 ret
->s
.output_section_statement
.section_alignment
= NULL
;
1245 ret
->s
.output_section_statement
.block_value
= 1;
1246 lang_list_init (&ret
->s
.output_section_statement
.children
);
1247 lang_statement_append (stat_ptr
, &ret
->s
, &ret
->s
.header
.next
);
1249 /* For every output section statement added to the list, except the
1250 first one, lang_os_list.tail points to the "next"
1251 field of the last element of the list. */
1252 if (lang_os_list
.head
!= NULL
)
1253 ret
->s
.output_section_statement
.prev
1254 = ((lang_output_section_statement_type
*)
1255 ((char *) lang_os_list
.tail
1256 - offsetof (lang_output_section_statement_type
, next
)));
1258 /* GCC's strict aliasing rules prevent us from just casting the
1259 address, so we store the pointer in a variable and cast that
1261 nextp
= &ret
->s
.output_section_statement
.next
;
1262 lang_statement_append (&lang_os_list
, &ret
->s
, nextp
);
1267 output_section_statement_table_init (void)
1269 if (!bfd_hash_table_init_n (&output_section_statement_table
,
1270 output_section_statement_newfunc
,
1271 sizeof (struct out_section_hash_entry
),
1273 einfo (_("%F%P: can not create hash table: %E\n"));
1277 output_section_statement_table_free (void)
1279 bfd_hash_table_free (&output_section_statement_table
);
1282 /* Build enough state so that the parser can build its tree. */
1287 obstack_begin (&stat_obstack
, 1000);
1288 obstack_init (&pt_obstack
);
1290 stat_ptr
= &statement_list
;
1292 output_section_statement_table_init ();
1294 lang_list_init (stat_ptr
);
1296 lang_list_init (&input_file_chain
);
1297 lang_list_init (&lang_os_list
);
1298 lang_list_init (&file_chain
);
1299 first_file
= lang_add_input_file (NULL
, lang_input_file_is_marker_enum
,
1301 abs_output_section
=
1302 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
, 0, 1);
1304 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
1306 asneeded_list_head
= NULL
;
1307 asneeded_list_tail
= &asneeded_list_head
;
1313 output_section_statement_table_free ();
1316 /*----------------------------------------------------------------------
1317 A region is an area of memory declared with the
1318 MEMORY { name:org=exp, len=exp ... }
1321 We maintain a list of all the regions here.
1323 If no regions are specified in the script, then the default is used
1324 which is created when looked up to be the entire data space.
1326 If create is true we are creating a region inside a MEMORY block.
1327 In this case it is probably an error to create a region that has
1328 already been created. If we are not inside a MEMORY block it is
1329 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
1330 and so we issue a warning.
1332 Each region has at least one name. The first name is either
1333 DEFAULT_MEMORY_REGION or the name given in the MEMORY block. You can add
1334 alias names to an existing region within a script with
1335 REGION_ALIAS (alias, region_name). Each name corresponds to at most one
1338 static lang_memory_region_type
*lang_memory_region_list
;
1339 static lang_memory_region_type
**lang_memory_region_list_tail
1340 = &lang_memory_region_list
;
1342 lang_memory_region_type
*
1343 lang_memory_region_lookup (const char *const name
, bool create
)
1345 lang_memory_region_name
*n
;
1346 lang_memory_region_type
*r
;
1347 lang_memory_region_type
*new_region
;
1349 /* NAME is NULL for LMA memspecs if no region was specified. */
1353 for (r
= lang_memory_region_list
; r
!= NULL
; r
= r
->next
)
1354 for (n
= &r
->name_list
; n
!= NULL
; n
= n
->next
)
1355 if (strcmp (n
->name
, name
) == 0)
1358 einfo (_("%P:%pS: warning: redeclaration of memory region `%s'\n"),
1363 if (!create
&& strcmp (name
, DEFAULT_MEMORY_REGION
))
1364 einfo (_("%P:%pS: warning: memory region `%s' not declared\n"),
1367 new_region
= stat_alloc (sizeof (lang_memory_region_type
));
1369 new_region
->name_list
.name
= xstrdup (name
);
1370 new_region
->name_list
.next
= NULL
;
1371 new_region
->next
= NULL
;
1372 new_region
->origin_exp
= NULL
;
1373 new_region
->origin
= 0;
1374 new_region
->length_exp
= NULL
;
1375 new_region
->length
= ~(bfd_size_type
) 0;
1376 new_region
->current
= 0;
1377 new_region
->last_os
= NULL
;
1378 new_region
->flags
= 0;
1379 new_region
->not_flags
= 0;
1380 new_region
->had_full_message
= false;
1382 *lang_memory_region_list_tail
= new_region
;
1383 lang_memory_region_list_tail
= &new_region
->next
;
1389 lang_memory_region_alias (const char *alias
, const char *region_name
)
1391 lang_memory_region_name
*n
;
1392 lang_memory_region_type
*r
;
1393 lang_memory_region_type
*region
;
1395 /* The default region must be unique. This ensures that it is not necessary
1396 to iterate through the name list if someone wants the check if a region is
1397 the default memory region. */
1398 if (strcmp (region_name
, DEFAULT_MEMORY_REGION
) == 0
1399 || strcmp (alias
, DEFAULT_MEMORY_REGION
) == 0)
1400 einfo (_("%F%P:%pS: error: alias for default memory region\n"), NULL
);
1402 /* Look for the target region and check if the alias is not already
1405 for (r
= lang_memory_region_list
; r
!= NULL
; r
= r
->next
)
1406 for (n
= &r
->name_list
; n
!= NULL
; n
= n
->next
)
1408 if (region
== NULL
&& strcmp (n
->name
, region_name
) == 0)
1410 if (strcmp (n
->name
, alias
) == 0)
1411 einfo (_("%F%P:%pS: error: redefinition of memory region "
1416 /* Check if the target region exists. */
1418 einfo (_("%F%P:%pS: error: memory region `%s' "
1419 "for alias `%s' does not exist\n"),
1420 NULL
, region_name
, alias
);
1422 /* Add alias to region name list. */
1423 n
= stat_alloc (sizeof (lang_memory_region_name
));
1424 n
->name
= xstrdup (alias
);
1425 n
->next
= region
->name_list
.next
;
1426 region
->name_list
.next
= n
;
1429 static lang_memory_region_type
*
1430 lang_memory_default (asection
*section
)
1432 lang_memory_region_type
*p
;
1434 flagword sec_flags
= section
->flags
;
1436 /* Override SEC_DATA to mean a writable section. */
1437 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
1438 sec_flags
|= SEC_DATA
;
1440 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
1442 if ((p
->flags
& sec_flags
) != 0
1443 && (p
->not_flags
& sec_flags
) == 0)
1448 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, false);
1451 /* Get the output section statement directly from the userdata. */
1453 lang_output_section_statement_type
*
1454 lang_output_section_get (const asection
*output_section
)
1456 return bfd_section_userdata (output_section
);
1459 /* Find or create an output_section_statement with the given NAME.
1460 If CONSTRAINT is non-zero match one with that constraint, otherwise
1461 match any non-negative constraint. If CREATE is 0 return NULL when
1462 no match exists. If CREATE is 1, create an output_section_statement
1463 when no match exists or if CONSTRAINT is SPECIAL. If CREATE is 2,
1464 always make a new output_section_statement. */
1466 lang_output_section_statement_type
*
1467 lang_output_section_statement_lookup (const char *name
,
1471 struct out_section_hash_entry
*entry
;
1473 entry
= ((struct out_section_hash_entry
*)
1474 bfd_hash_lookup (&output_section_statement_table
, name
,
1475 create
!= 0, false));
1479 einfo (_("%F%P: failed creating section `%s': %E\n"), name
);
1483 if (entry
->s
.output_section_statement
.name
!= NULL
)
1485 /* We have a section of this name, but it might not have the correct
1487 struct out_section_hash_entry
*last_ent
;
1489 name
= entry
->s
.output_section_statement
.name
;
1493 && !(create
&& constraint
== SPECIAL
)
1494 && (constraint
== entry
->s
.output_section_statement
.constraint
1496 && entry
->s
.output_section_statement
.constraint
>= 0)))
1497 return &entry
->s
.output_section_statement
;
1499 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1501 while (entry
!= NULL
1502 && name
== entry
->s
.output_section_statement
.name
);
1508 = ((struct out_section_hash_entry
*)
1509 output_section_statement_newfunc (NULL
,
1510 &output_section_statement_table
,
1514 einfo (_("%F%P: failed creating section `%s': %E\n"), name
);
1517 entry
->root
= last_ent
->root
;
1518 last_ent
->root
.next
= &entry
->root
;
1521 entry
->s
.output_section_statement
.name
= name
;
1522 entry
->s
.output_section_statement
.constraint
= constraint
;
1523 entry
->s
.output_section_statement
.dup_output
= (create
== 2
1524 || constraint
== SPECIAL
);
1525 return &entry
->s
.output_section_statement
;
1528 /* Find the next output_section_statement with the same name as OS.
1529 If CONSTRAINT is non-zero, find one with that constraint otherwise
1530 match any non-negative constraint. */
1532 lang_output_section_statement_type
*
1533 next_matching_output_section_statement (lang_output_section_statement_type
*os
,
1536 /* All output_section_statements are actually part of a
1537 struct out_section_hash_entry. */
1538 struct out_section_hash_entry
*entry
= (struct out_section_hash_entry
*)
1540 - offsetof (struct out_section_hash_entry
, s
.output_section_statement
));
1541 const char *name
= os
->name
;
1543 ASSERT (name
== entry
->root
.string
);
1546 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1548 || name
!= entry
->s
.output_section_statement
.name
)
1551 while (constraint
!= entry
->s
.output_section_statement
.constraint
1553 || entry
->s
.output_section_statement
.constraint
< 0));
1555 return &entry
->s
.output_section_statement
;
1558 /* A variant of lang_output_section_find used by place_orphan.
1559 Returns the output statement that should precede a new output
1560 statement for SEC. If an exact match is found on certain flags,
1563 lang_output_section_statement_type
*
1564 lang_output_section_find_by_flags (const asection
*sec
,
1566 lang_output_section_statement_type
**exact
,
1567 lang_match_sec_type_func match_type
)
1569 lang_output_section_statement_type
*first
, *look
, *found
;
1570 flagword look_flags
, differ
;
1572 /* We know the first statement on this list is *ABS*. May as well
1574 first
= (void *) lang_os_list
.head
;
1575 first
= first
->next
;
1577 /* First try for an exact match. */
1579 for (look
= first
; look
; look
= look
->next
)
1581 look_flags
= look
->flags
;
1582 if (look
->bfd_section
!= NULL
)
1584 look_flags
= look
->bfd_section
->flags
;
1585 if (match_type
&& !match_type (link_info
.output_bfd
,
1590 differ
= look_flags
^ sec_flags
;
1591 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
1592 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1602 if ((sec_flags
& SEC_CODE
) != 0
1603 && (sec_flags
& SEC_ALLOC
) != 0)
1605 /* Try for a rw code section. */
1606 for (look
= first
; look
; look
= look
->next
)
1608 look_flags
= look
->flags
;
1609 if (look
->bfd_section
!= NULL
)
1611 look_flags
= look
->bfd_section
->flags
;
1612 if (match_type
&& !match_type (link_info
.output_bfd
,
1617 differ
= look_flags
^ sec_flags
;
1618 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1619 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1623 else if ((sec_flags
& SEC_READONLY
) != 0
1624 && (sec_flags
& SEC_ALLOC
) != 0)
1626 /* .rodata can go after .text, .sdata2 after .rodata. */
1627 for (look
= first
; look
; look
= look
->next
)
1629 look_flags
= look
->flags
;
1630 if (look
->bfd_section
!= NULL
)
1632 look_flags
= look
->bfd_section
->flags
;
1633 if (match_type
&& !match_type (link_info
.output_bfd
,
1638 differ
= look_flags
^ sec_flags
;
1639 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1640 | SEC_READONLY
| SEC_SMALL_DATA
))
1641 || (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1643 && !(look_flags
& SEC_SMALL_DATA
)))
1647 else if ((sec_flags
& SEC_THREAD_LOCAL
) != 0
1648 && (sec_flags
& SEC_ALLOC
) != 0)
1650 /* .tdata can go after .data, .tbss after .tdata. Treat .tbss
1651 as if it were a loaded section, and don't use match_type. */
1652 bool seen_thread_local
= false;
1655 for (look
= first
; look
; look
= look
->next
)
1657 look_flags
= look
->flags
;
1658 if (look
->bfd_section
!= NULL
)
1659 look_flags
= look
->bfd_section
->flags
;
1661 differ
= look_flags
^ (sec_flags
| SEC_LOAD
| SEC_HAS_CONTENTS
);
1662 if (!(differ
& (SEC_THREAD_LOCAL
| SEC_ALLOC
)))
1664 /* .tdata and .tbss must be adjacent and in that order. */
1665 if (!(look_flags
& SEC_LOAD
)
1666 && (sec_flags
& SEC_LOAD
))
1667 /* ..so if we're at a .tbss section and we're placing
1668 a .tdata section stop looking and return the
1669 previous section. */
1672 seen_thread_local
= true;
1674 else if (seen_thread_local
)
1676 else if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
)))
1680 else if ((sec_flags
& SEC_SMALL_DATA
) != 0
1681 && (sec_flags
& SEC_ALLOC
) != 0)
1683 /* .sdata goes after .data, .sbss after .sdata. */
1684 for (look
= first
; look
; look
= look
->next
)
1686 look_flags
= look
->flags
;
1687 if (look
->bfd_section
!= NULL
)
1689 look_flags
= look
->bfd_section
->flags
;
1690 if (match_type
&& !match_type (link_info
.output_bfd
,
1695 differ
= look_flags
^ sec_flags
;
1696 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1697 | SEC_THREAD_LOCAL
))
1698 || ((look_flags
& SEC_SMALL_DATA
)
1699 && !(sec_flags
& SEC_HAS_CONTENTS
)))
1703 else if ((sec_flags
& SEC_HAS_CONTENTS
) != 0
1704 && (sec_flags
& SEC_ALLOC
) != 0)
1706 /* .data goes after .rodata. */
1707 for (look
= first
; look
; look
= look
->next
)
1709 look_flags
= look
->flags
;
1710 if (look
->bfd_section
!= NULL
)
1712 look_flags
= look
->bfd_section
->flags
;
1713 if (match_type
&& !match_type (link_info
.output_bfd
,
1718 differ
= look_flags
^ sec_flags
;
1719 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1720 | SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1724 else if ((sec_flags
& SEC_ALLOC
) != 0)
1726 /* .bss goes after any other alloc section. */
1727 for (look
= first
; look
; look
= look
->next
)
1729 look_flags
= look
->flags
;
1730 if (look
->bfd_section
!= NULL
)
1732 look_flags
= look
->bfd_section
->flags
;
1733 if (match_type
&& !match_type (link_info
.output_bfd
,
1738 differ
= look_flags
^ sec_flags
;
1739 if (!(differ
& SEC_ALLOC
))
1745 /* non-alloc go last. */
1746 for (look
= first
; look
; look
= look
->next
)
1748 look_flags
= look
->flags
;
1749 if (look
->bfd_section
!= NULL
)
1750 look_flags
= look
->bfd_section
->flags
;
1751 differ
= look_flags
^ sec_flags
;
1752 if (!(differ
& SEC_DEBUGGING
))
1758 if (found
|| !match_type
)
1761 return lang_output_section_find_by_flags (sec
, sec_flags
, NULL
, NULL
);
1764 /* Find the last output section before given output statement.
1765 Used by place_orphan. */
1768 output_prev_sec_find (lang_output_section_statement_type
*os
)
1770 lang_output_section_statement_type
*lookup
;
1772 for (lookup
= os
->prev
; lookup
!= NULL
; lookup
= lookup
->prev
)
1774 if (lookup
->constraint
< 0)
1777 if (lookup
->bfd_section
!= NULL
)
1778 return lookup
->bfd_section
;
1784 /* Look for a suitable place for a new output section statement. The
1785 idea is to skip over anything that might be inside a SECTIONS {}
1786 statement in a script, before we find another output section
1787 statement. Assignments to "dot" before an output section statement
1788 are assumed to belong to it, except in two cases; The first
1789 assignment to dot, and assignments before non-alloc sections.
1790 Otherwise we might put an orphan before . = . + SIZEOF_HEADERS or
1791 similar assignments that set the initial address, or we might
1792 insert non-alloc note sections among assignments setting end of
1795 static lang_statement_union_type
**
1796 insert_os_after (lang_statement_union_type
*after
)
1798 lang_statement_union_type
**where
;
1799 lang_statement_union_type
**assign
= NULL
;
1802 ignore_first
= after
== lang_os_list
.head
;
1804 for (where
= &after
->header
.next
;
1806 where
= &(*where
)->header
.next
)
1808 switch ((*where
)->header
.type
)
1810 case lang_assignment_statement_enum
:
1813 lang_assignment_statement_type
*ass
;
1815 ass
= &(*where
)->assignment_statement
;
1816 if (ass
->exp
->type
.node_class
!= etree_assert
1817 && ass
->exp
->assign
.dst
[0] == '.'
1818 && ass
->exp
->assign
.dst
[1] == 0)
1822 ignore_first
= false;
1826 case lang_wild_statement_enum
:
1827 case lang_input_section_enum
:
1828 case lang_object_symbols_statement_enum
:
1829 case lang_fill_statement_enum
:
1830 case lang_data_statement_enum
:
1831 case lang_reloc_statement_enum
:
1832 case lang_padding_statement_enum
:
1833 case lang_constructors_statement_enum
:
1835 ignore_first
= false;
1837 case lang_output_section_statement_enum
:
1840 asection
*s
= (*where
)->output_section_statement
.bfd_section
;
1843 || s
->map_head
.s
== NULL
1844 || (s
->flags
& SEC_ALLOC
) != 0)
1848 case lang_input_statement_enum
:
1849 case lang_address_statement_enum
:
1850 case lang_target_statement_enum
:
1851 case lang_output_statement_enum
:
1852 case lang_group_statement_enum
:
1853 case lang_insert_statement_enum
:
1855 case lang_input_matcher_enum
:
1864 lang_output_section_statement_type
*
1865 lang_insert_orphan (asection
*s
,
1866 const char *secname
,
1868 lang_output_section_statement_type
*after
,
1869 struct orphan_save
*place
,
1870 etree_type
*address
,
1871 lang_statement_list_type
*add_child
)
1873 lang_statement_list_type add
;
1874 lang_output_section_statement_type
*os
;
1875 lang_output_section_statement_type
**os_tail
;
1877 /* If we have found an appropriate place for the output section
1878 statements for this orphan, add them to our own private list,
1879 inserting them later into the global statement list. */
1882 lang_list_init (&add
);
1883 push_stat_ptr (&add
);
1886 if (bfd_link_relocatable (&link_info
)
1887 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0)
1888 address
= exp_intop (0);
1890 os_tail
= (lang_output_section_statement_type
**) lang_os_list
.tail
;
1891 os
= lang_enter_output_section_statement (
1892 secname
, address
, normal_section
, 0, NULL
, NULL
, NULL
, constraint
, 0);
1894 if (add_child
== NULL
)
1895 add_child
= &os
->children
;
1896 lang_add_section (add_child
, s
, NULL
, NULL
, os
);
1898 if (after
&& (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) != 0)
1900 const char *region
= (after
->region
1901 ? after
->region
->name_list
.name
1902 : DEFAULT_MEMORY_REGION
);
1903 const char *lma_region
= (after
->lma_region
1904 ? after
->lma_region
->name_list
.name
1906 lang_leave_output_section_statement (NULL
, region
, after
->phdrs
,
1910 lang_leave_output_section_statement (NULL
, DEFAULT_MEMORY_REGION
, NULL
,
1913 /* Restore the global list pointer. */
1917 if (after
!= NULL
&& os
->bfd_section
!= NULL
)
1919 asection
*snew
, *as
;
1920 bool place_after
= place
->stmt
== NULL
;
1921 bool insert_after
= true;
1923 snew
= os
->bfd_section
;
1925 /* Shuffle the bfd section list to make the output file look
1926 neater. This is really only cosmetic. */
1927 if (place
->section
== NULL
1928 && after
!= (void *) lang_os_list
.head
)
1930 asection
*bfd_section
= after
->bfd_section
;
1932 /* If the output statement hasn't been used to place any input
1933 sections (and thus doesn't have an output bfd_section),
1934 look for the closest prior output statement having an
1936 if (bfd_section
== NULL
)
1937 bfd_section
= output_prev_sec_find (after
);
1939 if (bfd_section
!= NULL
1940 && bfd_section
->owner
!= NULL
1941 && bfd_section
!= snew
)
1942 place
->section
= &bfd_section
->next
;
1945 if (place
->section
== NULL
)
1946 place
->section
= &link_info
.output_bfd
->sections
;
1948 as
= *place
->section
;
1952 /* Put the section at the end of the list. */
1954 /* Unlink the section. */
1955 bfd_section_list_remove (link_info
.output_bfd
, snew
);
1957 /* Now tack it back on in the right place. */
1958 bfd_section_list_append (link_info
.output_bfd
, snew
);
1960 else if ((bfd_get_flavour (link_info
.output_bfd
)
1961 == bfd_target_elf_flavour
)
1962 && (bfd_get_flavour (s
->owner
)
1963 == bfd_target_elf_flavour
)
1964 && ((elf_section_type (s
) == SHT_NOTE
1965 && (s
->flags
& SEC_LOAD
) != 0)
1966 || (elf_section_type (as
) == SHT_NOTE
1967 && (as
->flags
& SEC_LOAD
) != 0)))
1969 /* Make sure that output note sections are grouped and sorted
1970 by alignments when inserting a note section or insert a
1971 section after a note section, */
1973 /* A specific section after which the output note section
1974 should be placed. */
1975 asection
*after_sec
;
1976 /* True if we need to insert the orphan section after a
1977 specific section to maintain output note section order. */
1978 bool after_sec_note
= false;
1980 static asection
*first_orphan_note
= NULL
;
1982 /* Group and sort output note section by alignments in
1985 if (elf_section_type (s
) == SHT_NOTE
1986 && (s
->flags
& SEC_LOAD
) != 0)
1988 /* Search from the beginning for the last output note
1989 section with equal or larger alignments. NB: Don't
1990 place orphan note section after non-note sections. */
1992 first_orphan_note
= NULL
;
1993 for (sec
= link_info
.output_bfd
->sections
;
1995 && !bfd_is_abs_section (sec
));
1998 && elf_section_type (sec
) == SHT_NOTE
1999 && (sec
->flags
& SEC_LOAD
) != 0)
2001 if (!first_orphan_note
)
2002 first_orphan_note
= sec
;
2003 if (sec
->alignment_power
>= s
->alignment_power
)
2006 else if (first_orphan_note
)
2008 /* Stop if there is non-note section after the first
2009 orphan note section. */
2013 /* If this will be the first orphan note section, it can
2014 be placed at the default location. */
2015 after_sec_note
= first_orphan_note
!= NULL
;
2016 if (after_sec
== NULL
&& after_sec_note
)
2018 /* If all output note sections have smaller
2019 alignments, place the section before all
2020 output orphan note sections. */
2021 after_sec
= first_orphan_note
;
2022 insert_after
= false;
2025 else if (first_orphan_note
)
2027 /* Don't place non-note sections in the middle of orphan
2029 after_sec_note
= true;
2031 for (sec
= as
->next
;
2033 && !bfd_is_abs_section (sec
));
2035 if (elf_section_type (sec
) == SHT_NOTE
2036 && (sec
->flags
& SEC_LOAD
) != 0)
2044 /* Search forward to insert OS after AFTER_SEC output
2046 lang_output_section_statement_type
*stmt
, *next
;
2048 for (stmt
= after
; stmt
!= NULL
; stmt
= next
)
2053 if (stmt
->bfd_section
== after_sec
)
2063 /* If INSERT_AFTER is FALSE, place OS before
2064 AFTER_SEC output statement. */
2065 if (next
&& next
->bfd_section
== after_sec
)
2075 /* Search backward to insert OS after AFTER_SEC output
2078 for (stmt
= after
; stmt
!= NULL
; stmt
= stmt
->prev
)
2082 if (stmt
->bfd_section
== after_sec
)
2091 /* If INSERT_AFTER is FALSE, place OS before
2092 AFTER_SEC output statement. */
2093 if (stmt
->next
->bfd_section
== after_sec
)
2103 if (after_sec
== NULL
2104 || (insert_after
&& after_sec
->next
!= snew
)
2105 || (!insert_after
&& after_sec
->prev
!= snew
))
2107 /* Unlink the section. */
2108 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2110 /* Place SNEW after AFTER_SEC. If AFTER_SEC is NULL,
2115 bfd_section_list_insert_after (link_info
.output_bfd
,
2118 bfd_section_list_insert_before (link_info
.output_bfd
,
2122 bfd_section_list_prepend (link_info
.output_bfd
, snew
);
2125 else if (as
!= snew
&& as
->prev
!= snew
)
2127 /* Unlink the section. */
2128 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2130 /* Now tack it back on in the right place. */
2131 bfd_section_list_insert_before (link_info
.output_bfd
,
2135 else if (as
!= snew
&& as
->prev
!= snew
)
2137 /* Unlink the section. */
2138 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2140 /* Now tack it back on in the right place. */
2141 bfd_section_list_insert_before (link_info
.output_bfd
, as
, snew
);
2144 /* Save the end of this list. Further ophans of this type will
2145 follow the one we've just added. */
2146 place
->section
= &snew
->next
;
2148 /* The following is non-cosmetic. We try to put the output
2149 statements in some sort of reasonable order here, because they
2150 determine the final load addresses of the orphan sections.
2151 In addition, placing output statements in the wrong order may
2152 require extra segments. For instance, given a typical
2153 situation of all read-only sections placed in one segment and
2154 following that a segment containing all the read-write
2155 sections, we wouldn't want to place an orphan read/write
2156 section before or amongst the read-only ones. */
2157 if (add
.head
!= NULL
)
2159 lang_output_section_statement_type
*newly_added_os
;
2161 /* Place OS after AFTER if AFTER_NOTE is TRUE. */
2164 lang_statement_union_type
**where
;
2166 where
= insert_os_after ((lang_statement_union_type
*) after
);
2170 place
->os_tail
= &after
->next
;
2174 /* Put it after the last orphan statement we added. */
2175 *add
.tail
= *place
->stmt
;
2176 *place
->stmt
= add
.head
;
2179 /* Fix the global list pointer if we happened to tack our
2180 new list at the tail. */
2181 if (*stat_ptr
->tail
== add
.head
)
2182 stat_ptr
->tail
= add
.tail
;
2184 /* Save the end of this list. */
2185 place
->stmt
= add
.tail
;
2187 /* Do the same for the list of output section statements. */
2188 newly_added_os
= *os_tail
;
2190 newly_added_os
->prev
= (lang_output_section_statement_type
*)
2191 ((char *) place
->os_tail
2192 - offsetof (lang_output_section_statement_type
, next
));
2193 newly_added_os
->next
= *place
->os_tail
;
2194 if (newly_added_os
->next
!= NULL
)
2195 newly_added_os
->next
->prev
= newly_added_os
;
2196 *place
->os_tail
= newly_added_os
;
2197 place
->os_tail
= &newly_added_os
->next
;
2199 /* Fixing the global list pointer here is a little different.
2200 We added to the list in lang_enter_output_section_statement,
2201 trimmed off the new output_section_statment above when
2202 assigning *os_tail = NULL, but possibly added it back in
2203 the same place when assigning *place->os_tail. */
2204 if (*os_tail
== NULL
)
2205 lang_os_list
.tail
= (lang_statement_union_type
**) os_tail
;
2212 lang_print_asneeded (void)
2214 struct asneeded_minfo
*m
;
2216 if (asneeded_list_head
== NULL
)
2219 minfo (_("\nAs-needed library included to satisfy reference by file (symbol)\n\n"));
2221 for (m
= asneeded_list_head
; m
!= NULL
; m
= m
->next
)
2225 minfo ("%s", m
->soname
);
2226 len
= strlen (m
->soname
);
2233 print_spaces (30 - len
);
2236 minfo ("%pB ", m
->ref
);
2237 minfo ("(%pT)\n", m
->name
);
2242 lang_map_flags (flagword flag
)
2244 if (flag
& SEC_ALLOC
)
2247 if (flag
& SEC_CODE
)
2250 if (flag
& SEC_READONLY
)
2253 if (flag
& SEC_DATA
)
2256 if (flag
& SEC_LOAD
)
2263 lang_memory_region_type
*m
;
2264 bool dis_header_printed
= false;
2266 LANG_FOR_EACH_INPUT_STATEMENT (file
)
2270 if ((file
->the_bfd
->flags
& (BFD_LINKER_CREATED
| DYNAMIC
)) != 0
2271 || file
->flags
.just_syms
)
2274 if (config
.print_map_discarded
)
2275 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
2276 if ((s
->output_section
== NULL
2277 || s
->output_section
->owner
!= link_info
.output_bfd
)
2278 && (s
->flags
& (SEC_LINKER_CREATED
| SEC_KEEP
)) == 0)
2280 if (! dis_header_printed
)
2282 fprintf (config
.map_file
, _("\nDiscarded input sections\n\n"));
2283 dis_header_printed
= true;
2286 print_input_section (s
, true);
2290 minfo (_("\nMemory Configuration\n\n"));
2291 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
2292 _("Name"), _("Origin"), _("Length"), _("Attributes"));
2294 for (m
= lang_memory_region_list
; m
!= NULL
; m
= m
->next
)
2296 fprintf (config
.map_file
, "%-16s", m
->name_list
.name
);
2299 bfd_sprintf_vma (link_info
.output_bfd
, buf
, m
->origin
);
2300 fprintf (config
.map_file
, " 0x%-16s", buf
);
2301 bfd_sprintf_vma (link_info
.output_bfd
, buf
, m
->length
);
2302 fprintf (config
.map_file
,
2303 " 0x%*s", m
->flags
|| m
->not_flags
? -17 : 0, buf
);
2305 lang_map_flags (m
->flags
);
2310 lang_map_flags (m
->not_flags
);
2316 fprintf (config
.map_file
, _("\nLinker script and memory map\n\n"));
2318 if (!link_info
.reduce_memory_overheads
)
2320 obstack_begin (&map_obstack
, 1000);
2321 bfd_link_hash_traverse (link_info
.hash
, sort_def_symbol
, 0);
2323 expld
.phase
= lang_fixed_phase_enum
;
2324 lang_statement_iteration
++;
2325 print_statements ();
2327 ldemul_extra_map_file_text (link_info
.output_bfd
, &link_info
,
2332 sort_def_symbol (struct bfd_link_hash_entry
*hash_entry
,
2333 void *info ATTRIBUTE_UNUSED
)
2335 if ((hash_entry
->type
== bfd_link_hash_defined
2336 || hash_entry
->type
== bfd_link_hash_defweak
)
2337 && hash_entry
->u
.def
.section
->owner
!= link_info
.output_bfd
2338 && hash_entry
->u
.def
.section
->owner
!= NULL
)
2340 input_section_userdata_type
*ud
;
2341 struct map_symbol_def
*def
;
2343 ud
= bfd_section_userdata (hash_entry
->u
.def
.section
);
2346 ud
= stat_alloc (sizeof (*ud
));
2347 bfd_set_section_userdata (hash_entry
->u
.def
.section
, ud
);
2348 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
2349 ud
->map_symbol_def_count
= 0;
2351 else if (!ud
->map_symbol_def_tail
)
2352 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
2354 def
= (struct map_symbol_def
*) obstack_alloc (&map_obstack
, sizeof *def
);
2355 def
->entry
= hash_entry
;
2356 *(ud
->map_symbol_def_tail
) = def
;
2357 ud
->map_symbol_def_tail
= &def
->next
;
2358 ud
->map_symbol_def_count
++;
2363 /* Initialize an output section. */
2366 init_os (lang_output_section_statement_type
*s
, flagword flags
)
2368 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
2369 einfo (_("%F%P: illegal use of `%s' section\n"), DISCARD_SECTION_NAME
);
2372 s
->bfd_section
= bfd_get_section_by_name (link_info
.output_bfd
, s
->name
);
2373 if (s
->bfd_section
== NULL
)
2374 s
->bfd_section
= bfd_make_section_anyway_with_flags (link_info
.output_bfd
,
2376 if (s
->bfd_section
== NULL
)
2378 einfo (_("%F%P: output format %s cannot represent section"
2379 " called %s: %E\n"),
2380 link_info
.output_bfd
->xvec
->name
, s
->name
);
2382 s
->bfd_section
->output_section
= s
->bfd_section
;
2383 s
->bfd_section
->output_offset
= 0;
2385 /* Set the userdata of the output section to the output section
2386 statement to avoid lookup. */
2387 bfd_set_section_userdata (s
->bfd_section
, s
);
2389 /* If there is a base address, make sure that any sections it might
2390 mention are initialized. */
2391 if (s
->addr_tree
!= NULL
)
2392 exp_init_os (s
->addr_tree
);
2394 if (s
->load_base
!= NULL
)
2395 exp_init_os (s
->load_base
);
2397 /* If supplied an alignment, set it. */
2398 if (s
->section_alignment
!= NULL
)
2399 s
->bfd_section
->alignment_power
= exp_get_power (s
->section_alignment
,
2400 "section alignment");
2403 /* Make sure that all output sections mentioned in an expression are
2407 exp_init_os (etree_type
*exp
)
2409 switch (exp
->type
.node_class
)
2413 case etree_provided
:
2414 exp_init_os (exp
->assign
.src
);
2418 exp_init_os (exp
->binary
.lhs
);
2419 exp_init_os (exp
->binary
.rhs
);
2423 exp_init_os (exp
->trinary
.cond
);
2424 exp_init_os (exp
->trinary
.lhs
);
2425 exp_init_os (exp
->trinary
.rhs
);
2429 exp_init_os (exp
->assert_s
.child
);
2433 exp_init_os (exp
->unary
.child
);
2437 switch (exp
->type
.node_code
)
2442 lang_output_section_statement_type
*os
;
2444 os
= lang_output_section_find (exp
->name
.name
);
2445 if (os
!= NULL
&& os
->bfd_section
== NULL
)
2457 section_already_linked (bfd
*abfd
, asection
*sec
, void *data
)
2459 lang_input_statement_type
*entry
= (lang_input_statement_type
*) data
;
2461 /* If we are only reading symbols from this object, then we want to
2462 discard all sections. */
2463 if (entry
->flags
.just_syms
)
2465 bfd_link_just_syms (abfd
, sec
, &link_info
);
2469 /* Deal with SHF_EXCLUDE ELF sections. */
2470 if (!bfd_link_relocatable (&link_info
)
2471 && (abfd
->flags
& BFD_PLUGIN
) == 0
2472 && (sec
->flags
& (SEC_GROUP
| SEC_KEEP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
2473 sec
->output_section
= bfd_abs_section_ptr
;
2475 if (!(abfd
->flags
& DYNAMIC
))
2476 bfd_section_already_linked (abfd
, sec
, &link_info
);
2480 /* Returns true if SECTION is one we know will be discarded based on its
2481 section flags, otherwise returns false. */
2484 lang_discard_section_p (asection
*section
)
2487 flagword flags
= section
->flags
;
2489 /* Discard sections marked with SEC_EXCLUDE. */
2490 discard
= (flags
& SEC_EXCLUDE
) != 0;
2492 /* Discard the group descriptor sections when we're finally placing the
2493 sections from within the group. */
2494 if ((flags
& SEC_GROUP
) != 0
2495 && link_info
.resolve_section_groups
)
2498 /* Discard debugging sections if we are stripping debugging
2500 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
2501 && (flags
& SEC_DEBUGGING
) != 0)
2507 /* The wild routines.
2509 These expand statements like *(.text) and foo.o to a list of
2510 explicit actions, like foo.o(.text), bar.o(.text) and
2511 foo.o(.text, .data). */
2513 /* Add SECTION to the output section OUTPUT. Do this by creating a
2514 lang_input_section statement which is placed at PTR. */
2517 lang_add_section (lang_statement_list_type
*ptr
,
2519 struct wildcard_list
*pattern
,
2520 struct flag_info
*sflag_info
,
2521 lang_output_section_statement_type
*output
)
2523 flagword flags
= section
->flags
;
2526 lang_input_section_type
*new_section
;
2527 bfd
*abfd
= link_info
.output_bfd
;
2529 /* Is this section one we know should be discarded? */
2530 discard
= lang_discard_section_p (section
);
2532 /* Discard input sections which are assigned to a section named
2533 DISCARD_SECTION_NAME. */
2534 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
2539 if (section
->output_section
== NULL
)
2541 /* This prevents future calls from assigning this section. */
2542 section
->output_section
= bfd_abs_section_ptr
;
2544 else if (link_info
.non_contiguous_regions_warnings
)
2545 einfo (_("%P:%pS: warning: --enable-non-contiguous-regions makes "
2546 "section `%pA' from `%pB' match /DISCARD/ clause.\n"),
2547 NULL
, section
, section
->owner
);
2556 keep
= bfd_lookup_section_flags (&link_info
, sflag_info
, section
);
2561 if (section
->output_section
!= NULL
)
2563 if (!link_info
.non_contiguous_regions
)
2566 /* SECTION has already been handled in a special way
2567 (eg. LINK_ONCE): skip it. */
2568 if (bfd_is_abs_section (section
->output_section
))
2571 /* Already assigned to the same output section, do not process
2572 it again, to avoid creating loops between duplicate sections
2574 if (section
->output_section
== output
->bfd_section
)
2577 if (link_info
.non_contiguous_regions_warnings
&& output
->bfd_section
)
2578 einfo (_("%P:%pS: warning: --enable-non-contiguous-regions may "
2579 "change behaviour for section `%pA' from `%pB' (assigned to "
2580 "%pA, but additional match: %pA)\n"),
2581 NULL
, section
, section
->owner
, section
->output_section
,
2582 output
->bfd_section
);
2584 /* SECTION has already been assigned to an output section, but
2585 the user allows it to be mapped to another one in case it
2586 overflows. We'll later update the actual output section in
2587 size_input_section as appropriate. */
2590 /* We don't copy the SEC_NEVER_LOAD flag from an input section
2591 to an output section, because we want to be able to include a
2592 SEC_NEVER_LOAD section in the middle of an otherwise loaded
2593 section (I don't know why we want to do this, but we do).
2594 build_link_order in ldwrite.c handles this case by turning
2595 the embedded SEC_NEVER_LOAD section into a fill. */
2596 flags
&= ~ SEC_NEVER_LOAD
;
2598 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
2599 already been processed. One reason to do this is that on pe
2600 format targets, .text$foo sections go into .text and it's odd
2601 to see .text with SEC_LINK_ONCE set. */
2602 if ((flags
& (SEC_LINK_ONCE
| SEC_GROUP
)) == (SEC_LINK_ONCE
| SEC_GROUP
))
2604 if (link_info
.resolve_section_groups
)
2605 flags
&= ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
);
2607 flags
&= ~(SEC_LINK_DUPLICATES
| SEC_RELOC
);
2609 else if (!bfd_link_relocatable (&link_info
))
2610 flags
&= ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
);
2612 switch (output
->sectype
)
2614 case normal_section
:
2615 case overlay_section
:
2616 case first_overlay_section
:
2619 case noalloc_section
:
2620 flags
&= ~SEC_ALLOC
;
2622 case typed_readonly_section
:
2623 case readonly_section
:
2624 flags
|= SEC_READONLY
;
2626 case noload_section
:
2628 flags
|= SEC_NEVER_LOAD
;
2629 /* Unfortunately GNU ld has managed to evolve two different
2630 meanings to NOLOAD in scripts. ELF gets a .bss style noload,
2631 alloc, no contents section. All others get a noload, noalloc
2633 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
)
2634 flags
&= ~SEC_HAS_CONTENTS
;
2636 flags
&= ~SEC_ALLOC
;
2640 if (output
->bfd_section
== NULL
)
2641 init_os (output
, flags
);
2643 /* If SEC_READONLY is not set in the input section, then clear
2644 it from the output section. */
2645 output
->bfd_section
->flags
&= flags
| ~SEC_READONLY
;
2647 if (output
->bfd_section
->linker_has_input
)
2649 /* Only set SEC_READONLY flag on the first input section. */
2650 flags
&= ~ SEC_READONLY
;
2652 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
2653 if ((output
->bfd_section
->flags
& (SEC_MERGE
| SEC_STRINGS
))
2654 != (flags
& (SEC_MERGE
| SEC_STRINGS
))
2655 || ((flags
& SEC_MERGE
) != 0
2656 && output
->bfd_section
->entsize
!= section
->entsize
))
2658 output
->bfd_section
->flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2659 flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2662 output
->bfd_section
->flags
|= flags
;
2664 if (!output
->bfd_section
->linker_has_input
)
2666 output
->bfd_section
->linker_has_input
= 1;
2667 /* This must happen after flags have been updated. The output
2668 section may have been created before we saw its first input
2669 section, eg. for a data statement. */
2670 bfd_init_private_section_data (section
->owner
, section
,
2671 link_info
.output_bfd
,
2672 output
->bfd_section
,
2674 if ((flags
& SEC_MERGE
) != 0)
2675 output
->bfd_section
->entsize
= section
->entsize
;
2678 if ((flags
& SEC_TIC54X_BLOCK
) != 0
2679 && bfd_get_arch (section
->owner
) == bfd_arch_tic54x
)
2681 /* FIXME: This value should really be obtained from the bfd... */
2682 output
->block_value
= 128;
2685 /* When a .ctors section is placed in .init_array it must be copied
2686 in reverse order. Similarly for .dtors. Set that up. */
2687 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
2688 && ((startswith (section
->name
, ".ctors")
2689 && strcmp (output
->bfd_section
->name
, ".init_array") == 0)
2690 || (startswith (section
->name
, ".dtors")
2691 && strcmp (output
->bfd_section
->name
, ".fini_array") == 0))
2692 && (section
->name
[6] == 0 || section
->name
[6] == '.'))
2693 section
->flags
|= SEC_ELF_REVERSE_COPY
;
2695 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
2696 output
->bfd_section
->alignment_power
= section
->alignment_power
;
2698 section
->output_section
= output
->bfd_section
;
2700 if (!map_head_is_link_order
)
2702 asection
*s
= output
->bfd_section
->map_tail
.s
;
2703 output
->bfd_section
->map_tail
.s
= section
;
2704 section
->map_head
.s
= NULL
;
2705 section
->map_tail
.s
= s
;
2707 s
->map_head
.s
= section
;
2709 output
->bfd_section
->map_head
.s
= section
;
2712 /* Add a section reference to the list. */
2713 new_section
= new_stat (lang_input_section
, ptr
);
2714 new_section
->section
= section
;
2715 new_section
->pattern
= pattern
;
2718 /* Expand a wild statement for a particular FILE. SECTION may be
2719 NULL, in which case it is a wild card. This assumes that the
2720 wild statement doesn't need any sorting (of filenames or sections). */
2723 output_section_callback_nosort (lang_wild_statement_type
*ptr
,
2724 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
2726 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
2729 lang_output_section_statement_type
*os
;
2731 os
= (lang_output_section_statement_type
*) output
;
2733 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2734 if (unique_section_p (section
, os
))
2737 lang_add_section (&ptr
->children
, section
, ptr
->section_list
,
2738 ptr
->section_flag_list
, os
);
2741 /* Check if all sections in a wild statement for a particular FILE
2745 check_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
2746 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
2748 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
2751 lang_output_section_statement_type
*os
;
2753 os
= (lang_output_section_statement_type
*) output
;
2755 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2756 if (unique_section_p (section
, os
))
2759 if (section
->output_section
== NULL
&& (section
->flags
& SEC_READONLY
) == 0)
2760 os
->all_input_readonly
= false;
2763 /* This is passed a file name which must have been seen already and
2764 added to the statement tree. We will see if it has been opened
2765 already and had its symbols read. If not then we'll read it. */
2767 static lang_input_statement_type
*
2768 lookup_name (const char *name
)
2770 lang_input_statement_type
*search
;
2772 for (search
= (void *) input_file_chain
.head
;
2774 search
= search
->next_real_file
)
2776 /* Use the local_sym_name as the name of the file that has
2777 already been loaded as filename might have been transformed
2778 via the search directory lookup mechanism. */
2779 const char *filename
= search
->local_sym_name
;
2781 if (filename
!= NULL
2782 && filename_cmp (filename
, name
) == 0)
2788 /* Arrange to splice the input statement added by new_afile into
2789 statement_list after the current input_file_chain tail.
2790 We know input_file_chain is not an empty list, and that
2791 lookup_name was called via open_input_bfds. Later calls to
2792 lookup_name should always match an existing input_statement. */
2793 lang_statement_union_type
**tail
= stat_ptr
->tail
;
2794 lang_statement_union_type
**after
2795 = (void *) ((char *) input_file_chain
.tail
2796 - offsetof (lang_input_statement_type
, next_real_file
)
2797 + offsetof (lang_input_statement_type
, header
.next
));
2798 lang_statement_union_type
*rest
= *after
;
2799 stat_ptr
->tail
= after
;
2800 search
= new_afile (name
, lang_input_file_is_search_file_enum
,
2801 default_target
, NULL
);
2802 *stat_ptr
->tail
= rest
;
2804 stat_ptr
->tail
= tail
;
2807 /* If we have already added this file, or this file is not real
2808 don't add this file. */
2809 if (search
->flags
.loaded
|| !search
->flags
.real
)
2812 if (!load_symbols (search
, NULL
))
2818 /* Save LIST as a list of libraries whose symbols should not be exported. */
2823 struct excluded_lib
*next
;
2825 static struct excluded_lib
*excluded_libs
;
2828 add_excluded_libs (const char *list
)
2830 const char *p
= list
, *end
;
2834 struct excluded_lib
*entry
;
2835 end
= strpbrk (p
, ",:");
2837 end
= p
+ strlen (p
);
2838 entry
= (struct excluded_lib
*) xmalloc (sizeof (*entry
));
2839 entry
->next
= excluded_libs
;
2840 entry
->name
= (char *) xmalloc (end
- p
+ 1);
2841 memcpy (entry
->name
, p
, end
- p
);
2842 entry
->name
[end
- p
] = '\0';
2843 excluded_libs
= entry
;
2851 check_excluded_libs (bfd
*abfd
)
2853 struct excluded_lib
*lib
= excluded_libs
;
2857 int len
= strlen (lib
->name
);
2858 const char *filename
= lbasename (bfd_get_filename (abfd
));
2860 if (strcmp (lib
->name
, "ALL") == 0)
2862 abfd
->no_export
= true;
2866 if (filename_ncmp (lib
->name
, filename
, len
) == 0
2867 && (filename
[len
] == '\0'
2868 || (filename
[len
] == '.' && filename
[len
+ 1] == 'a'
2869 && filename
[len
+ 2] == '\0')))
2871 abfd
->no_export
= true;
2879 /* Get the symbols for an input file. */
2882 load_symbols (lang_input_statement_type
*entry
,
2883 lang_statement_list_type
*place
)
2887 if (entry
->flags
.loaded
)
2890 ldfile_open_file (entry
);
2892 /* Do not process further if the file was missing. */
2893 if (entry
->flags
.missing_file
)
2896 if (trace_files
|| verbose
)
2897 info_msg ("%pI\n", entry
);
2899 if (!bfd_check_format (entry
->the_bfd
, bfd_archive
)
2900 && !bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
2903 struct lang_input_statement_flags save_flags
;
2906 err
= bfd_get_error ();
2908 /* See if the emulation has some special knowledge. */
2909 if (ldemul_unrecognized_file (entry
))
2911 if (err
== bfd_error_file_ambiguously_recognized
)
2916 if (err
== bfd_error_file_ambiguously_recognized
)
2920 einfo (_("%P: %pB: file not recognized: %E;"
2921 " matching formats:"), entry
->the_bfd
);
2922 for (p
= matching
; *p
!= NULL
; p
++)
2927 else if (err
!= bfd_error_file_not_recognized
2929 einfo (_("%F%P: %pB: file not recognized: %E\n"), entry
->the_bfd
);
2931 bfd_close (entry
->the_bfd
);
2932 entry
->the_bfd
= NULL
;
2934 /* Try to interpret the file as a linker script. */
2935 save_flags
= input_flags
;
2936 ldfile_open_command_file (entry
->filename
);
2938 push_stat_ptr (place
);
2939 input_flags
.add_DT_NEEDED_for_regular
2940 = entry
->flags
.add_DT_NEEDED_for_regular
;
2941 input_flags
.add_DT_NEEDED_for_dynamic
2942 = entry
->flags
.add_DT_NEEDED_for_dynamic
;
2943 input_flags
.whole_archive
= entry
->flags
.whole_archive
;
2944 input_flags
.dynamic
= entry
->flags
.dynamic
;
2946 ldfile_assumed_script
= true;
2947 parser_input
= input_script
;
2948 current_input_file
= entry
->filename
;
2950 current_input_file
= NULL
;
2951 ldfile_assumed_script
= false;
2953 /* missing_file is sticky. sysrooted will already have been
2954 restored when seeing EOF in yyparse, but no harm to restore
2956 save_flags
.missing_file
|= input_flags
.missing_file
;
2957 input_flags
= save_flags
;
2961 entry
->flags
.loaded
= true;
2966 if (ldemul_recognized_file (entry
))
2969 /* We don't call ldlang_add_file for an archive. Instead, the
2970 add_symbols entry point will call ldlang_add_file, via the
2971 add_archive_element callback, for each element of the archive
2973 switch (bfd_get_format (entry
->the_bfd
))
2979 if (!entry
->flags
.reload
)
2980 ldlang_add_file (entry
);
2984 check_excluded_libs (entry
->the_bfd
);
2986 bfd_set_usrdata (entry
->the_bfd
, entry
);
2987 if (entry
->flags
.whole_archive
)
2995 member
= bfd_openr_next_archived_file (entry
->the_bfd
, member
);
3000 if (!bfd_check_format (member
, bfd_object
))
3002 einfo (_("%F%P: %pB: member %pB in archive is not an object\n"),
3003 entry
->the_bfd
, member
);
3008 if (!(*link_info
.callbacks
3009 ->add_archive_element
) (&link_info
, member
,
3010 "--whole-archive", &subsbfd
))
3013 /* Potentially, the add_archive_element hook may have set a
3014 substitute BFD for us. */
3015 if (!bfd_link_add_symbols (subsbfd
, &link_info
))
3017 einfo (_("%F%P: %pB: error adding symbols: %E\n"), member
);
3022 entry
->flags
.loaded
= loaded
;
3028 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
3029 entry
->flags
.loaded
= true;
3031 einfo (_("%F%P: %pB: error adding symbols: %E\n"), entry
->the_bfd
);
3033 return entry
->flags
.loaded
;
3036 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
3037 may be NULL, indicating that it is a wildcard. Separate
3038 lang_input_section statements are created for each part of the
3039 expansion; they are added after the wild statement S. OUTPUT is
3040 the output section. */
3043 wild (lang_wild_statement_type
*s
,
3044 const char *target ATTRIBUTE_UNUSED
,
3045 lang_output_section_statement_type
*output
)
3047 struct wildcard_list
*sec
;
3049 if (s
->filenames_sorted
|| s
->any_specs_sorted
)
3051 lang_section_bst_type
*tree
;
3053 walk_wild (s
, output_section_callback_sort
, output
);
3058 output_section_callback_tree_to_list (s
, tree
, output
);
3060 s
->rightmost
= &s
->tree
;
3064 walk_wild (s
, output_section_callback_nosort
, output
);
3066 if (default_common_section
== NULL
)
3067 for (sec
= s
->section_list
; sec
!= NULL
; sec
= sec
->next
)
3068 if (sec
->spec
.name
!= NULL
&& strcmp (sec
->spec
.name
, "COMMON") == 0)
3070 /* Remember the section that common is going to in case we
3071 later get something which doesn't know where to put it. */
3072 default_common_section
= output
;
3077 /* Return TRUE iff target is the sought target. */
3080 get_target (const bfd_target
*target
, void *data
)
3082 const char *sought
= (const char *) data
;
3084 return strcmp (target
->name
, sought
) == 0;
3087 /* Like strcpy() but convert to lower case as well. */
3090 stricpy (char *dest
, const char *src
)
3094 while ((c
= *src
++) != 0)
3095 *dest
++ = TOLOWER (c
);
3100 /* Remove the first occurrence of needle (if any) in haystack
3104 strcut (char *haystack
, const char *needle
)
3106 haystack
= strstr (haystack
, needle
);
3112 for (src
= haystack
+ strlen (needle
); *src
;)
3113 *haystack
++ = *src
++;
3119 /* Compare two target format name strings.
3120 Return a value indicating how "similar" they are. */
3123 name_compare (const char *first
, const char *second
)
3129 copy1
= (char *) xmalloc (strlen (first
) + 1);
3130 copy2
= (char *) xmalloc (strlen (second
) + 1);
3132 /* Convert the names to lower case. */
3133 stricpy (copy1
, first
);
3134 stricpy (copy2
, second
);
3136 /* Remove size and endian strings from the name. */
3137 strcut (copy1
, "big");
3138 strcut (copy1
, "little");
3139 strcut (copy2
, "big");
3140 strcut (copy2
, "little");
3142 /* Return a value based on how many characters match,
3143 starting from the beginning. If both strings are
3144 the same then return 10 * their length. */
3145 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
3146 if (copy1
[result
] == 0)
3158 /* Set by closest_target_match() below. */
3159 static const bfd_target
*winner
;
3161 /* Scan all the valid bfd targets looking for one that has the endianness
3162 requirement that was specified on the command line, and is the nearest
3163 match to the original output target. */
3166 closest_target_match (const bfd_target
*target
, void *data
)
3168 const bfd_target
*original
= (const bfd_target
*) data
;
3170 if (command_line
.endian
== ENDIAN_BIG
3171 && target
->byteorder
!= BFD_ENDIAN_BIG
)
3174 if (command_line
.endian
== ENDIAN_LITTLE
3175 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
3178 /* Must be the same flavour. */
3179 if (target
->flavour
!= original
->flavour
)
3182 /* Ignore generic big and little endian elf vectors. */
3183 if (strcmp (target
->name
, "elf32-big") == 0
3184 || strcmp (target
->name
, "elf64-big") == 0
3185 || strcmp (target
->name
, "elf32-little") == 0
3186 || strcmp (target
->name
, "elf64-little") == 0)
3189 /* If we have not found a potential winner yet, then record this one. */
3196 /* Oh dear, we now have two potential candidates for a successful match.
3197 Compare their names and choose the better one. */
3198 if (name_compare (target
->name
, original
->name
)
3199 > name_compare (winner
->name
, original
->name
))
3202 /* Keep on searching until wqe have checked them all. */
3206 /* Return the BFD target format of the first input file. */
3209 get_first_input_target (void)
3211 const char *target
= NULL
;
3213 LANG_FOR_EACH_INPUT_STATEMENT (s
)
3215 if (s
->header
.type
== lang_input_statement_enum
3218 ldfile_open_file (s
);
3220 if (s
->the_bfd
!= NULL
3221 && bfd_check_format (s
->the_bfd
, bfd_object
))
3223 target
= bfd_get_target (s
->the_bfd
);
3235 lang_get_output_target (void)
3239 /* Has the user told us which output format to use? */
3240 if (output_target
!= NULL
)
3241 return output_target
;
3243 /* No - has the current target been set to something other than
3245 if (current_target
!= default_target
&& current_target
!= NULL
)
3246 return current_target
;
3248 /* No - can we determine the format of the first input file? */
3249 target
= get_first_input_target ();
3253 /* Failed - use the default output target. */
3254 return default_target
;
3257 /* Open the output file. */
3260 open_output (const char *name
)
3262 lang_input_statement_type
*f
;
3263 char *out
= lrealpath (name
);
3265 for (f
= (void *) input_file_chain
.head
;
3267 f
= f
->next_real_file
)
3270 char *in
= lrealpath (f
->local_sym_name
);
3271 if (filename_cmp (in
, out
) == 0)
3272 einfo (_("%F%P: input file '%s' is the same as output file\n"),
3278 output_target
= lang_get_output_target ();
3280 /* Has the user requested a particular endianness on the command
3282 if (command_line
.endian
!= ENDIAN_UNSET
)
3284 /* Get the chosen target. */
3285 const bfd_target
*target
3286 = bfd_iterate_over_targets (get_target
, (void *) output_target
);
3288 /* If the target is not supported, we cannot do anything. */
3291 enum bfd_endian desired_endian
;
3293 if (command_line
.endian
== ENDIAN_BIG
)
3294 desired_endian
= BFD_ENDIAN_BIG
;
3296 desired_endian
= BFD_ENDIAN_LITTLE
;
3298 /* See if the target has the wrong endianness. This should
3299 not happen if the linker script has provided big and
3300 little endian alternatives, but some scrips don't do
3302 if (target
->byteorder
!= desired_endian
)
3304 /* If it does, then see if the target provides
3305 an alternative with the correct endianness. */
3306 if (target
->alternative_target
!= NULL
3307 && (target
->alternative_target
->byteorder
== desired_endian
))
3308 output_target
= target
->alternative_target
->name
;
3311 /* Try to find a target as similar as possible to
3312 the default target, but which has the desired
3313 endian characteristic. */
3314 bfd_iterate_over_targets (closest_target_match
,
3317 /* Oh dear - we could not find any targets that
3318 satisfy our requirements. */
3320 einfo (_("%P: warning: could not find any targets"
3321 " that match endianness requirement\n"));
3323 output_target
= winner
->name
;
3329 link_info
.output_bfd
= bfd_openw (name
, output_target
);
3331 if (link_info
.output_bfd
== NULL
)
3333 if (bfd_get_error () == bfd_error_invalid_target
)
3334 einfo (_("%F%P: target %s not found\n"), output_target
);
3336 einfo (_("%F%P: cannot open output file %s: %E\n"), name
);
3339 delete_output_file_on_failure
= true;
3341 if (!bfd_set_format (link_info
.output_bfd
, bfd_object
))
3342 einfo (_("%F%P: %s: can not make object file: %E\n"), name
);
3343 if (!bfd_set_arch_mach (link_info
.output_bfd
,
3344 ldfile_output_architecture
,
3345 ldfile_output_machine
))
3346 einfo (_("%F%P: %s: can not set architecture: %E\n"), name
);
3348 link_info
.hash
= bfd_link_hash_table_create (link_info
.output_bfd
);
3349 if (link_info
.hash
== NULL
)
3350 einfo (_("%F%P: can not create hash table: %E\n"));
3352 bfd_set_gp_size (link_info
.output_bfd
, g_switch_value
);
3356 ldlang_open_output (lang_statement_union_type
*statement
)
3358 switch (statement
->header
.type
)
3360 case lang_output_statement_enum
:
3361 ASSERT (link_info
.output_bfd
== NULL
);
3362 open_output (statement
->output_statement
.name
);
3363 ldemul_set_output_arch ();
3364 if (config
.magic_demand_paged
3365 && !bfd_link_relocatable (&link_info
))
3366 link_info
.output_bfd
->flags
|= D_PAGED
;
3368 link_info
.output_bfd
->flags
&= ~D_PAGED
;
3369 if (config
.text_read_only
)
3370 link_info
.output_bfd
->flags
|= WP_TEXT
;
3372 link_info
.output_bfd
->flags
&= ~WP_TEXT
;
3373 if (link_info
.traditional_format
)
3374 link_info
.output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
3376 link_info
.output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
3379 case lang_target_statement_enum
:
3380 current_target
= statement
->target_statement
.target
;
3388 init_opb (asection
*s
)
3393 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
3395 && (s
->flags
& SEC_ELF_OCTETS
) != 0)
3398 x
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3399 ldfile_output_machine
);
3401 while ((x
& 1) == 0)
3409 /* Open all the input files. */
3413 OPEN_BFD_NORMAL
= 0,
3417 #if BFD_SUPPORTS_PLUGINS
3418 static lang_input_statement_type
*plugin_insert
= NULL
;
3419 static struct bfd_link_hash_entry
*plugin_undefs
= NULL
;
3423 open_input_bfds (lang_statement_union_type
*s
, enum open_bfd_mode mode
)
3425 for (; s
!= NULL
; s
= s
->header
.next
)
3427 switch (s
->header
.type
)
3429 case lang_constructors_statement_enum
:
3430 open_input_bfds (constructor_list
.head
, mode
);
3432 case lang_output_section_statement_enum
:
3433 open_input_bfds (s
->output_section_statement
.children
.head
, mode
);
3435 case lang_wild_statement_enum
:
3436 /* Maybe we should load the file's symbols. */
3437 if ((mode
& OPEN_BFD_RESCAN
) == 0
3438 && s
->wild_statement
.filename
3439 && !wildcardp (s
->wild_statement
.filename
)
3440 && !archive_path (s
->wild_statement
.filename
))
3441 lookup_name (s
->wild_statement
.filename
);
3442 open_input_bfds (s
->wild_statement
.children
.head
, mode
);
3444 case lang_group_statement_enum
:
3446 struct bfd_link_hash_entry
*undefs
;
3447 #if BFD_SUPPORTS_PLUGINS
3448 lang_input_statement_type
*plugin_insert_save
;
3451 /* We must continually search the entries in the group
3452 until no new symbols are added to the list of undefined
3457 #if BFD_SUPPORTS_PLUGINS
3458 plugin_insert_save
= plugin_insert
;
3460 undefs
= link_info
.hash
->undefs_tail
;
3461 open_input_bfds (s
->group_statement
.children
.head
,
3462 mode
| OPEN_BFD_FORCE
);
3464 while (undefs
!= link_info
.hash
->undefs_tail
3465 #if BFD_SUPPORTS_PLUGINS
3466 /* Objects inserted by a plugin, which are loaded
3467 before we hit this loop, may have added new
3469 || (plugin_insert
!= plugin_insert_save
&& plugin_undefs
)
3474 case lang_target_statement_enum
:
3475 current_target
= s
->target_statement
.target
;
3477 case lang_input_statement_enum
:
3478 if (s
->input_statement
.flags
.real
)
3480 lang_statement_union_type
**os_tail
;
3481 lang_statement_list_type add
;
3484 s
->input_statement
.target
= current_target
;
3486 /* If we are being called from within a group, and this
3487 is an archive which has already been searched, then
3488 force it to be researched unless the whole archive
3489 has been loaded already. Do the same for a rescan.
3490 Likewise reload --as-needed shared libs. */
3491 if (mode
!= OPEN_BFD_NORMAL
3492 #if BFD_SUPPORTS_PLUGINS
3493 && ((mode
& OPEN_BFD_RESCAN
) == 0
3494 || plugin_insert
== NULL
)
3496 && s
->input_statement
.flags
.loaded
3497 && (abfd
= s
->input_statement
.the_bfd
) != NULL
3498 && ((bfd_get_format (abfd
) == bfd_archive
3499 && !s
->input_statement
.flags
.whole_archive
)
3500 || (bfd_get_format (abfd
) == bfd_object
3501 && ((abfd
->flags
) & DYNAMIC
) != 0
3502 && s
->input_statement
.flags
.add_DT_NEEDED_for_regular
3503 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
3504 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)))
3506 s
->input_statement
.flags
.loaded
= false;
3507 s
->input_statement
.flags
.reload
= true;
3510 os_tail
= lang_os_list
.tail
;
3511 lang_list_init (&add
);
3513 if (!load_symbols (&s
->input_statement
, &add
))
3514 config
.make_executable
= false;
3516 if (add
.head
!= NULL
)
3518 /* If this was a script with output sections then
3519 tack any added statements on to the end of the
3520 list. This avoids having to reorder the output
3521 section statement list. Very likely the user
3522 forgot -T, and whatever we do here will not meet
3523 naive user expectations. */
3524 if (os_tail
!= lang_os_list
.tail
)
3526 einfo (_("%P: warning: %s contains output sections;"
3527 " did you forget -T?\n"),
3528 s
->input_statement
.filename
);
3529 *stat_ptr
->tail
= add
.head
;
3530 stat_ptr
->tail
= add
.tail
;
3534 *add
.tail
= s
->header
.next
;
3535 s
->header
.next
= add
.head
;
3539 #if BFD_SUPPORTS_PLUGINS
3540 /* If we have found the point at which a plugin added new
3541 files, clear plugin_insert to enable archive rescan. */
3542 if (&s
->input_statement
== plugin_insert
)
3543 plugin_insert
= NULL
;
3546 case lang_assignment_statement_enum
:
3547 if (s
->assignment_statement
.exp
->type
.node_class
!= etree_assert
)
3548 exp_fold_tree_no_dot (s
->assignment_statement
.exp
);
3555 /* Exit if any of the files were missing. */
3556 if (input_flags
.missing_file
)
3560 #ifdef ENABLE_LIBCTF
3561 /* Emit CTF errors and warnings. fp can be NULL to report errors/warnings
3562 that happened specifically at CTF open time. */
3564 lang_ctf_errs_warnings (ctf_dict_t
*fp
)
3566 ctf_next_t
*i
= NULL
;
3571 while ((text
= ctf_errwarning_next (fp
, &i
, &is_warning
, &err
)) != NULL
)
3573 einfo (_("%s: %s\n"), is_warning
? _("CTF warning"): _("CTF error"),
3577 if (err
!= ECTF_NEXT_END
)
3579 einfo (_("CTF error: cannot get CTF errors: `%s'\n"),
3583 /* `err' returns errors from the error/warning iterator in particular.
3584 These never assert. But if we have an fp, that could have recorded
3585 an assertion failure: assert if it has done so. */
3586 ASSERT (!fp
|| ctf_errno (fp
) != ECTF_INTERNAL
);
3589 /* Open the CTF sections in the input files with libctf: if any were opened,
3590 create a fake input file that we'll write the merged CTF data to later
3594 ldlang_open_ctf (void)
3599 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3603 /* Incoming files from the compiler have a single ctf_dict_t in them
3604 (which is presented to us by the libctf API in a ctf_archive_t
3605 wrapper): files derived from a previous relocatable link have a CTF
3606 archive containing possibly many CTF files. */
3608 if ((file
->the_ctf
= ctf_bfdopen (file
->the_bfd
, &err
)) == NULL
)
3610 if (err
!= ECTF_NOCTFDATA
)
3612 lang_ctf_errs_warnings (NULL
);
3613 einfo (_("%P: warning: CTF section in %pB not loaded; "
3614 "its types will be discarded: %s\n"), file
->the_bfd
,
3620 /* Prevent the contents of this section from being written, while
3621 requiring the section itself to be duplicated in the output, but only
3623 /* This section must exist if ctf_bfdopen() succeeded. */
3624 sect
= bfd_get_section_by_name (file
->the_bfd
, ".ctf");
3626 sect
->flags
|= SEC_NEVER_LOAD
| SEC_HAS_CONTENTS
| SEC_LINKER_CREATED
;
3629 sect
->flags
|= SEC_EXCLUDE
;
3639 if ((ctf_output
= ctf_create (&err
)) != NULL
)
3642 einfo (_("%P: warning: CTF output not created: `%s'\n"),
3645 LANG_FOR_EACH_INPUT_STATEMENT (errfile
)
3646 ctf_close (errfile
->the_ctf
);
3649 /* Merge together CTF sections. After this, only the symtab-dependent
3650 function and data object sections need adjustment. */
3653 lang_merge_ctf (void)
3655 asection
*output_sect
;
3661 output_sect
= bfd_get_section_by_name (link_info
.output_bfd
, ".ctf");
3663 /* If the section was discarded, don't waste time merging. */
3664 if (output_sect
== NULL
)
3666 ctf_dict_close (ctf_output
);
3669 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3671 ctf_close (file
->the_ctf
);
3672 file
->the_ctf
= NULL
;
3677 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3682 /* Takes ownership of file->the_ctf. */
3683 if (ctf_link_add_ctf (ctf_output
, file
->the_ctf
, file
->filename
) < 0)
3685 einfo (_("%P: warning: CTF section in %pB cannot be linked: `%s'\n"),
3686 file
->the_bfd
, ctf_errmsg (ctf_errno (ctf_output
)));
3687 ctf_close (file
->the_ctf
);
3688 file
->the_ctf
= NULL
;
3693 if (!config
.ctf_share_duplicated
)
3694 flags
= CTF_LINK_SHARE_UNCONFLICTED
;
3696 flags
= CTF_LINK_SHARE_DUPLICATED
;
3697 if (!config
.ctf_variables
)
3698 flags
|= CTF_LINK_OMIT_VARIABLES_SECTION
;
3699 if (bfd_link_relocatable (&link_info
))
3700 flags
|= CTF_LINK_NO_FILTER_REPORTED_SYMS
;
3702 if (ctf_link (ctf_output
, flags
) < 0)
3704 lang_ctf_errs_warnings (ctf_output
);
3705 einfo (_("%P: warning: CTF linking failed; "
3706 "output will have no CTF section: %s\n"),
3707 ctf_errmsg (ctf_errno (ctf_output
)));
3710 output_sect
->size
= 0;
3711 output_sect
->flags
|= SEC_EXCLUDE
;
3714 /* Output any lingering errors that didn't come from ctf_link. */
3715 lang_ctf_errs_warnings (ctf_output
);
3718 /* Let the emulation acquire strings from the dynamic strtab to help it optimize
3719 the CTF, if supported. */
3722 ldlang_ctf_acquire_strings (struct elf_strtab_hash
*dynstrtab
)
3724 ldemul_acquire_strings_for_ctf (ctf_output
, dynstrtab
);
3727 /* Inform the emulation about the addition of a new dynamic symbol, in BFD
3729 void ldlang_ctf_new_dynsym (int symidx
, struct elf_internal_sym
*sym
)
3731 ldemul_new_dynsym_for_ctf (ctf_output
, symidx
, sym
);
3734 /* Write out the CTF section. Called early, if the emulation isn't going to
3735 need to dedup against the strtab and symtab, then possibly called from the
3736 target linker code if the dedup has happened. */
3738 lang_write_ctf (int late
)
3741 asection
*output_sect
;
3748 /* Emit CTF late if this emulation says it can do so. */
3749 if (ldemul_emit_ctf_early ())
3754 if (!ldemul_emit_ctf_early ())
3758 /* Inform the emulation that all the symbols that will be received have
3761 ldemul_new_dynsym_for_ctf (ctf_output
, 0, NULL
);
3765 output_sect
= bfd_get_section_by_name (link_info
.output_bfd
, ".ctf");
3768 output_sect
->contents
= ctf_link_write (ctf_output
, &output_size
,
3769 CTF_COMPRESSION_THRESHOLD
);
3770 output_sect
->size
= output_size
;
3771 output_sect
->flags
|= SEC_IN_MEMORY
| SEC_KEEP
;
3773 lang_ctf_errs_warnings (ctf_output
);
3774 if (!output_sect
->contents
)
3776 einfo (_("%P: warning: CTF section emission failed; "
3777 "output will have no CTF section: %s\n"),
3778 ctf_errmsg (ctf_errno (ctf_output
)));
3779 output_sect
->size
= 0;
3780 output_sect
->flags
|= SEC_EXCLUDE
;
3784 /* This also closes every CTF input file used in the link. */
3785 ctf_dict_close (ctf_output
);
3788 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3789 file
->the_ctf
= NULL
;
3792 /* Write out the CTF section late, if the emulation needs that. */
3795 ldlang_write_ctf_late (void)
3797 /* Trigger a "late call", if the emulation needs one. */
3803 ldlang_open_ctf (void)
3805 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3809 /* If built without CTF, warn and delete all CTF sections from the output.
3810 (The alternative would be to simply concatenate them, which does not
3811 yield a valid CTF section.) */
3813 if ((sect
= bfd_get_section_by_name (file
->the_bfd
, ".ctf")) != NULL
)
3815 einfo (_("%P: warning: CTF section in %pB not linkable: "
3816 "%P was built without support for CTF\n"), file
->the_bfd
);
3818 sect
->flags
|= SEC_EXCLUDE
;
3823 static void lang_merge_ctf (void) {}
3825 ldlang_ctf_acquire_strings (struct elf_strtab_hash
*dynstrtab
3826 ATTRIBUTE_UNUSED
) {}
3828 ldlang_ctf_new_dynsym (int symidx ATTRIBUTE_UNUSED
,
3829 struct elf_internal_sym
*sym ATTRIBUTE_UNUSED
) {}
3830 static void lang_write_ctf (int late ATTRIBUTE_UNUSED
) {}
3831 void ldlang_write_ctf_late (void) {}
3834 /* Add the supplied name to the symbol table as an undefined reference.
3835 This is a two step process as the symbol table doesn't even exist at
3836 the time the ld command line is processed. First we put the name
3837 on a list, then, once the output file has been opened, transfer the
3838 name to the symbol table. */
3840 typedef struct bfd_sym_chain ldlang_undef_chain_list_type
;
3842 #define ldlang_undef_chain_list_head entry_symbol.next
3845 ldlang_add_undef (const char *const name
, bool cmdline ATTRIBUTE_UNUSED
)
3847 ldlang_undef_chain_list_type
*new_undef
;
3849 new_undef
= stat_alloc (sizeof (*new_undef
));
3850 new_undef
->next
= ldlang_undef_chain_list_head
;
3851 ldlang_undef_chain_list_head
= new_undef
;
3853 new_undef
->name
= xstrdup (name
);
3855 if (link_info
.output_bfd
!= NULL
)
3856 insert_undefined (new_undef
->name
);
3859 /* Insert NAME as undefined in the symbol table. */
3862 insert_undefined (const char *name
)
3864 struct bfd_link_hash_entry
*h
;
3866 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, false, true);
3868 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
3869 if (h
->type
== bfd_link_hash_new
)
3871 h
->type
= bfd_link_hash_undefined
;
3872 h
->u
.undef
.abfd
= NULL
;
3873 h
->non_ir_ref_regular
= true;
3874 bfd_link_add_undef (link_info
.hash
, h
);
3878 /* Run through the list of undefineds created above and place them
3879 into the linker hash table as undefined symbols belonging to the
3883 lang_place_undefineds (void)
3885 ldlang_undef_chain_list_type
*ptr
;
3887 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
3888 insert_undefined (ptr
->name
);
3891 /* Mark -u symbols against garbage collection. */
3894 lang_mark_undefineds (void)
3896 ldlang_undef_chain_list_type
*ptr
;
3898 if (is_elf_hash_table (link_info
.hash
))
3899 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
3901 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*)
3902 bfd_link_hash_lookup (link_info
.hash
, ptr
->name
, false, false, true);
3908 /* Structure used to build the list of symbols that the user has required
3911 struct require_defined_symbol
3914 struct require_defined_symbol
*next
;
3917 /* The list of symbols that the user has required be defined. */
3919 static struct require_defined_symbol
*require_defined_symbol_list
;
3921 /* Add a new symbol NAME to the list of symbols that are required to be
3925 ldlang_add_require_defined (const char *const name
)
3927 struct require_defined_symbol
*ptr
;
3929 ldlang_add_undef (name
, true);
3930 ptr
= stat_alloc (sizeof (*ptr
));
3931 ptr
->next
= require_defined_symbol_list
;
3932 ptr
->name
= strdup (name
);
3933 require_defined_symbol_list
= ptr
;
3936 /* Check that all symbols the user required to be defined, are defined,
3937 raise an error if we find a symbol that is not defined. */
3940 ldlang_check_require_defined_symbols (void)
3942 struct require_defined_symbol
*ptr
;
3944 for (ptr
= require_defined_symbol_list
; ptr
!= NULL
; ptr
= ptr
->next
)
3946 struct bfd_link_hash_entry
*h
;
3948 h
= bfd_link_hash_lookup (link_info
.hash
, ptr
->name
,
3949 false, false, true);
3951 || (h
->type
!= bfd_link_hash_defined
3952 && h
->type
!= bfd_link_hash_defweak
))
3953 einfo(_("%X%P: required symbol `%s' not defined\n"), ptr
->name
);
3957 /* Check for all readonly or some readwrite sections. */
3960 check_input_sections
3961 (lang_statement_union_type
*s
,
3962 lang_output_section_statement_type
*output_section_statement
)
3964 for (; s
!= NULL
; s
= s
->header
.next
)
3966 switch (s
->header
.type
)
3968 case lang_wild_statement_enum
:
3969 walk_wild (&s
->wild_statement
, check_section_callback
,
3970 output_section_statement
);
3971 if (!output_section_statement
->all_input_readonly
)
3974 case lang_constructors_statement_enum
:
3975 check_input_sections (constructor_list
.head
,
3976 output_section_statement
);
3977 if (!output_section_statement
->all_input_readonly
)
3980 case lang_group_statement_enum
:
3981 check_input_sections (s
->group_statement
.children
.head
,
3982 output_section_statement
);
3983 if (!output_section_statement
->all_input_readonly
)
3992 /* Update wildcard statements if needed. */
3995 update_wild_statements (lang_statement_union_type
*s
)
3997 struct wildcard_list
*sec
;
3999 switch (sort_section
)
4009 for (; s
!= NULL
; s
= s
->header
.next
)
4011 switch (s
->header
.type
)
4016 case lang_wild_statement_enum
:
4017 for (sec
= s
->wild_statement
.section_list
; sec
!= NULL
;
4019 /* Don't sort .init/.fini sections. */
4020 if (strcmp (sec
->spec
.name
, ".init") != 0
4021 && strcmp (sec
->spec
.name
, ".fini") != 0)
4023 switch (sec
->spec
.sorted
)
4026 sec
->spec
.sorted
= sort_section
;
4029 if (sort_section
== by_alignment
)
4030 sec
->spec
.sorted
= by_name_alignment
;
4033 if (sort_section
== by_name
)
4034 sec
->spec
.sorted
= by_alignment_name
;
4039 s
->wild_statement
.any_specs_sorted
= true;
4043 case lang_constructors_statement_enum
:
4044 update_wild_statements (constructor_list
.head
);
4047 case lang_output_section_statement_enum
:
4048 update_wild_statements
4049 (s
->output_section_statement
.children
.head
);
4052 case lang_group_statement_enum
:
4053 update_wild_statements (s
->group_statement
.children
.head
);
4061 /* Open input files and attach to output sections. */
4064 map_input_to_output_sections
4065 (lang_statement_union_type
*s
, const char *target
,
4066 lang_output_section_statement_type
*os
)
4068 for (; s
!= NULL
; s
= s
->header
.next
)
4070 lang_output_section_statement_type
*tos
;
4072 unsigned int type
= 0;
4074 switch (s
->header
.type
)
4076 case lang_wild_statement_enum
:
4077 wild (&s
->wild_statement
, target
, os
);
4079 case lang_constructors_statement_enum
:
4080 map_input_to_output_sections (constructor_list
.head
,
4084 case lang_output_section_statement_enum
:
4085 tos
= &s
->output_section_statement
;
4086 if (tos
->constraint
== ONLY_IF_RW
4087 || tos
->constraint
== ONLY_IF_RO
)
4089 tos
->all_input_readonly
= true;
4090 check_input_sections (tos
->children
.head
, tos
);
4091 if (tos
->all_input_readonly
!= (tos
->constraint
== ONLY_IF_RO
))
4092 tos
->constraint
= -1;
4094 if (tos
->constraint
>= 0)
4095 map_input_to_output_sections (tos
->children
.head
,
4099 case lang_output_statement_enum
:
4101 case lang_target_statement_enum
:
4102 target
= s
->target_statement
.target
;
4104 case lang_group_statement_enum
:
4105 map_input_to_output_sections (s
->group_statement
.children
.head
,
4109 case lang_data_statement_enum
:
4110 /* Make sure that any sections mentioned in the expression
4112 exp_init_os (s
->data_statement
.exp
);
4113 /* The output section gets CONTENTS, ALLOC and LOAD, but
4114 these may be overridden by the script. */
4115 flags
= SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
;
4116 switch (os
->sectype
)
4118 case normal_section
:
4119 case overlay_section
:
4120 case first_overlay_section
:
4122 case noalloc_section
:
4123 flags
= SEC_HAS_CONTENTS
;
4125 case readonly_section
:
4126 flags
|= SEC_READONLY
;
4128 case typed_readonly_section
:
4129 flags
|= SEC_READONLY
;
4132 if (os
->sectype_value
->type
.node_class
== etree_name
4133 && os
->sectype_value
->type
.node_code
== NAME
)
4135 const char *name
= os
->sectype_value
->name
.name
;
4136 if (strcmp (name
, "SHT_PROGBITS") == 0)
4137 type
= SHT_PROGBITS
;
4138 else if (strcmp (name
, "SHT_STRTAB") == 0)
4140 else if (strcmp (name
, "SHT_NOTE") == 0)
4142 else if (strcmp (name
, "SHT_NOBITS") == 0)
4144 else if (strcmp (name
, "SHT_INIT_ARRAY") == 0)
4145 type
= SHT_INIT_ARRAY
;
4146 else if (strcmp (name
, "SHT_FINI_ARRAY") == 0)
4147 type
= SHT_FINI_ARRAY
;
4148 else if (strcmp (name
, "SHT_PREINIT_ARRAY") == 0)
4149 type
= SHT_PREINIT_ARRAY
;
4151 einfo (_ ("%F%P: invalid type for output section `%s'\n"),
4156 exp_fold_tree_no_dot (os
->sectype_value
);
4157 if (expld
.result
.valid_p
)
4158 type
= expld
.result
.value
;
4160 einfo (_ ("%F%P: invalid type for output section `%s'\n"),
4164 case noload_section
:
4165 if (bfd_get_flavour (link_info
.output_bfd
)
4166 == bfd_target_elf_flavour
)
4167 flags
= SEC_NEVER_LOAD
| SEC_ALLOC
;
4169 flags
= SEC_NEVER_LOAD
| SEC_HAS_CONTENTS
;
4172 if (os
->bfd_section
== NULL
)
4173 init_os (os
, flags
| SEC_READONLY
);
4175 os
->bfd_section
->flags
|= flags
;
4176 os
->bfd_section
->type
= type
;
4178 case lang_input_section_enum
:
4180 case lang_fill_statement_enum
:
4181 case lang_object_symbols_statement_enum
:
4182 case lang_reloc_statement_enum
:
4183 case lang_padding_statement_enum
:
4184 case lang_input_statement_enum
:
4185 if (os
!= NULL
&& os
->bfd_section
== NULL
)
4188 case lang_assignment_statement_enum
:
4189 if (os
!= NULL
&& os
->bfd_section
== NULL
)
4192 /* Make sure that any sections mentioned in the assignment
4194 exp_init_os (s
->assignment_statement
.exp
);
4196 case lang_address_statement_enum
:
4197 /* Mark the specified section with the supplied address.
4198 If this section was actually a segment marker, then the
4199 directive is ignored if the linker script explicitly
4200 processed the segment marker. Originally, the linker
4201 treated segment directives (like -Ttext on the
4202 command-line) as section directives. We honor the
4203 section directive semantics for backwards compatibility;
4204 linker scripts that do not specifically check for
4205 SEGMENT_START automatically get the old semantics. */
4206 if (!s
->address_statement
.segment
4207 || !s
->address_statement
.segment
->used
)
4209 const char *name
= s
->address_statement
.section_name
;
4211 /* Create the output section statement here so that
4212 orphans with a set address will be placed after other
4213 script sections. If we let the orphan placement code
4214 place them in amongst other sections then the address
4215 will affect following script sections, which is
4216 likely to surprise naive users. */
4217 tos
= lang_output_section_statement_lookup (name
, 0, 1);
4218 tos
->addr_tree
= s
->address_statement
.address
;
4219 if (tos
->bfd_section
== NULL
)
4223 case lang_insert_statement_enum
:
4225 case lang_input_matcher_enum
:
4231 /* An insert statement snips out all the linker statements from the
4232 start of the list and places them after the output section
4233 statement specified by the insert. This operation is complicated
4234 by the fact that we keep a doubly linked list of output section
4235 statements as well as the singly linked list of all statements.
4236 FIXME someday: Twiddling with the list not only moves statements
4237 from the user's script but also input and group statements that are
4238 built from command line object files and --start-group. We only
4239 get away with this because the list pointers used by file_chain
4240 and input_file_chain are not reordered, and processing via
4241 statement_list after this point mostly ignores input statements.
4242 One exception is the map file, where LOAD and START GROUP/END GROUP
4243 can end up looking odd. */
4246 process_insert_statements (lang_statement_union_type
**start
)
4248 lang_statement_union_type
**s
;
4249 lang_output_section_statement_type
*first_os
= NULL
;
4250 lang_output_section_statement_type
*last_os
= NULL
;
4251 lang_output_section_statement_type
*os
;
4256 if ((*s
)->header
.type
== lang_output_section_statement_enum
)
4258 /* Keep pointers to the first and last output section
4259 statement in the sequence we may be about to move. */
4260 os
= &(*s
)->output_section_statement
;
4262 ASSERT (last_os
== NULL
|| last_os
->next
== os
);
4265 /* Set constraint negative so that lang_output_section_find
4266 won't match this output section statement. At this
4267 stage in linking constraint has values in the range
4268 [-1, ONLY_IN_RW]. */
4269 last_os
->constraint
= -2 - last_os
->constraint
;
4270 if (first_os
== NULL
)
4273 else if ((*s
)->header
.type
== lang_group_statement_enum
)
4275 /* A user might put -T between --start-group and
4276 --end-group. One way this odd construct might arise is
4277 from a wrapper around ld to change library search
4278 behaviour. For example:
4280 exec real_ld --start-group "$@" --end-group
4281 This isn't completely unreasonable so go looking inside a
4282 group statement for insert statements. */
4283 process_insert_statements (&(*s
)->group_statement
.children
.head
);
4285 else if ((*s
)->header
.type
== lang_insert_statement_enum
)
4287 lang_insert_statement_type
*i
= &(*s
)->insert_statement
;
4288 lang_output_section_statement_type
*where
;
4289 lang_statement_union_type
**ptr
;
4290 lang_statement_union_type
*first
;
4292 if (link_info
.non_contiguous_regions
)
4294 einfo (_("warning: INSERT statement in linker script is "
4295 "incompatible with --enable-non-contiguous-regions.\n"));
4298 where
= lang_output_section_find (i
->where
);
4299 if (where
!= NULL
&& i
->is_before
)
4302 where
= where
->prev
;
4303 while (where
!= NULL
&& where
->constraint
< 0);
4307 einfo (_("%F%P: %s not found for insert\n"), i
->where
);
4311 /* Deal with reordering the output section statement list. */
4312 if (last_os
!= NULL
)
4314 asection
*first_sec
, *last_sec
;
4315 struct lang_output_section_statement_struct
**next
;
4317 /* Snip out the output sections we are moving. */
4318 first_os
->prev
->next
= last_os
->next
;
4319 if (last_os
->next
== NULL
)
4321 next
= &first_os
->prev
->next
;
4322 lang_os_list
.tail
= (lang_statement_union_type
**) next
;
4325 last_os
->next
->prev
= first_os
->prev
;
4326 /* Add them in at the new position. */
4327 last_os
->next
= where
->next
;
4328 if (where
->next
== NULL
)
4330 next
= &last_os
->next
;
4331 lang_os_list
.tail
= (lang_statement_union_type
**) next
;
4334 where
->next
->prev
= last_os
;
4335 first_os
->prev
= where
;
4336 where
->next
= first_os
;
4338 /* Move the bfd sections in the same way. */
4341 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
4343 os
->constraint
= -2 - os
->constraint
;
4344 if (os
->bfd_section
!= NULL
4345 && os
->bfd_section
->owner
!= NULL
)
4347 last_sec
= os
->bfd_section
;
4348 if (first_sec
== NULL
)
4349 first_sec
= last_sec
;
4354 if (last_sec
!= NULL
)
4356 asection
*sec
= where
->bfd_section
;
4358 sec
= output_prev_sec_find (where
);
4360 /* The place we want to insert must come after the
4361 sections we are moving. So if we find no
4362 section or if the section is the same as our
4363 last section, then no move is needed. */
4364 if (sec
!= NULL
&& sec
!= last_sec
)
4366 /* Trim them off. */
4367 if (first_sec
->prev
!= NULL
)
4368 first_sec
->prev
->next
= last_sec
->next
;
4370 link_info
.output_bfd
->sections
= last_sec
->next
;
4371 if (last_sec
->next
!= NULL
)
4372 last_sec
->next
->prev
= first_sec
->prev
;
4374 link_info
.output_bfd
->section_last
= first_sec
->prev
;
4376 if (sec
->owner
== NULL
)
4377 /* SEC is the absolute section, from the
4378 first dummy output section statement. Add
4379 back the sections we trimmed off to the
4380 start of the bfd sections. */
4383 last_sec
->next
= sec
->next
;
4385 last_sec
->next
= link_info
.output_bfd
->sections
;
4386 if (last_sec
->next
!= NULL
)
4387 last_sec
->next
->prev
= last_sec
;
4389 link_info
.output_bfd
->section_last
= last_sec
;
4390 first_sec
->prev
= sec
;
4391 if (first_sec
->prev
!= NULL
)
4392 first_sec
->prev
->next
= first_sec
;
4394 link_info
.output_bfd
->sections
= first_sec
;
4399 lang_statement_union_type
*after
= (void *) where
;
4400 if (where
== &lang_os_list
.head
->output_section_statement
4401 && where
->next
== first_os
)
4403 /* PR30155. Handle a corner case where the statement
4404 list is something like the following:
4406 . .data 0x0000000000000000 0x0
4407 . [0x0000000000000000] b = .
4409 . .data 0x0000000000000000 0x0 t.o
4410 . 0x0000000000000000 0x4 LONG 0x0
4411 . INSERT BEFORE .text.start
4412 . [0x0000000000000004] a = .
4413 . .text.start 0x0000000000000000 0x0
4414 . [0x0000000000000000] c = .
4415 . OUTPUT(a.out elf64-x86-64)
4416 Here we do not want to allow insert_os_after to
4417 choose a point inside the list we are moving.
4418 That would lose the list. Instead, let
4419 insert_os_after work from the INSERT, which in this
4420 particular example will result in inserting after
4421 the assignment "a = .". */
4424 ptr
= insert_os_after (after
);
4425 /* Snip everything from the start of the list, up to and
4426 including the insert statement we are currently processing. */
4428 *start
= (*s
)->header
.next
;
4429 /* Add them back where they belong, minus the insert. */
4432 statement_list
.tail
= s
;
4439 s
= &(*s
)->header
.next
;
4442 /* Undo constraint twiddling. */
4443 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
4445 os
->constraint
= -2 - os
->constraint
;
4451 /* An output section might have been removed after its statement was
4452 added. For example, ldemul_before_allocation can remove dynamic
4453 sections if they turn out to be not needed. Clean them up here. */
4456 strip_excluded_output_sections (void)
4458 lang_output_section_statement_type
*os
;
4460 /* Run lang_size_sections (if not already done). */
4461 if (expld
.phase
!= lang_mark_phase_enum
)
4463 expld
.phase
= lang_mark_phase_enum
;
4464 expld
.dataseg
.phase
= exp_seg_none
;
4465 one_lang_size_sections_pass (NULL
, false);
4466 lang_reset_memory_regions ();
4469 for (os
= (void *) lang_os_list
.head
;
4473 asection
*output_section
;
4476 if (os
->constraint
< 0)
4479 output_section
= os
->bfd_section
;
4480 if (output_section
== NULL
)
4483 exclude
= (output_section
->rawsize
== 0
4484 && (output_section
->flags
& SEC_KEEP
) == 0
4485 && !bfd_section_removed_from_list (link_info
.output_bfd
,
4488 /* Some sections have not yet been sized, notably .gnu.version,
4489 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
4490 input sections, so don't drop output sections that have such
4491 input sections unless they are also marked SEC_EXCLUDE. */
4492 if (exclude
&& output_section
->map_head
.s
!= NULL
)
4496 for (s
= output_section
->map_head
.s
; s
!= NULL
; s
= s
->map_head
.s
)
4497 if ((s
->flags
& SEC_EXCLUDE
) == 0
4498 && ((s
->flags
& SEC_LINKER_CREATED
) != 0
4499 || link_info
.emitrelocations
))
4508 /* We don't set bfd_section to NULL since bfd_section of the
4509 removed output section statement may still be used. */
4510 if (!os
->update_dot
)
4512 output_section
->flags
|= SEC_EXCLUDE
;
4513 bfd_section_list_remove (link_info
.output_bfd
, output_section
);
4514 link_info
.output_bfd
->section_count
--;
4519 /* Called from ldwrite to clear out asection.map_head and
4520 asection.map_tail for use as link_orders in ldwrite. */
4523 lang_clear_os_map (void)
4525 lang_output_section_statement_type
*os
;
4527 if (map_head_is_link_order
)
4530 for (os
= (void *) lang_os_list
.head
;
4534 asection
*output_section
;
4536 if (os
->constraint
< 0)
4539 output_section
= os
->bfd_section
;
4540 if (output_section
== NULL
)
4543 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
4544 output_section
->map_head
.link_order
= NULL
;
4545 output_section
->map_tail
.link_order
= NULL
;
4548 /* Stop future calls to lang_add_section from messing with map_head
4549 and map_tail link_order fields. */
4550 map_head_is_link_order
= true;
4554 print_output_section_statement
4555 (lang_output_section_statement_type
*output_section_statement
)
4557 asection
*section
= output_section_statement
->bfd_section
;
4560 if (output_section_statement
!= abs_output_section
)
4562 minfo ("\n%s", output_section_statement
->name
);
4564 if (section
!= NULL
)
4566 print_dot
= section
->vma
;
4568 len
= strlen (output_section_statement
->name
);
4569 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
4574 print_spaces (SECTION_NAME_MAP_LENGTH
- len
);
4576 minfo ("0x%V %W", section
->vma
, TO_ADDR (section
->size
));
4578 if (section
->vma
!= section
->lma
)
4579 minfo (_(" load address 0x%V"), section
->lma
);
4581 if (output_section_statement
->update_dot_tree
!= NULL
)
4582 exp_fold_tree (output_section_statement
->update_dot_tree
,
4583 bfd_abs_section_ptr
, &print_dot
);
4589 print_statement_list (output_section_statement
->children
.head
,
4590 output_section_statement
);
4594 print_assignment (lang_assignment_statement_type
*assignment
,
4595 lang_output_section_statement_type
*output_section
)
4601 print_spaces (SECTION_NAME_MAP_LENGTH
);
4603 if (assignment
->exp
->type
.node_class
== etree_assert
)
4606 tree
= assignment
->exp
->assert_s
.child
;
4610 const char *dst
= assignment
->exp
->assign
.dst
;
4612 is_dot
= (dst
[0] == '.' && dst
[1] == 0);
4613 tree
= assignment
->exp
;
4616 osec
= output_section
->bfd_section
;
4618 osec
= bfd_abs_section_ptr
;
4620 if (assignment
->exp
->type
.node_class
!= etree_provide
)
4621 exp_fold_tree (tree
, osec
, &print_dot
);
4623 expld
.result
.valid_p
= false;
4626 const char *str
= buf
;
4627 if (expld
.result
.valid_p
)
4631 if (assignment
->exp
->type
.node_class
== etree_assert
4633 || expld
.assign_name
!= NULL
)
4635 value
= expld
.result
.value
;
4637 if (expld
.result
.section
!= NULL
)
4638 value
+= expld
.result
.section
->vma
;
4642 bfd_sprintf_vma (link_info
.output_bfd
, buf
+ 2, value
);
4648 struct bfd_link_hash_entry
*h
;
4650 h
= bfd_link_hash_lookup (link_info
.hash
, assignment
->exp
->assign
.dst
,
4651 false, false, true);
4653 && (h
->type
== bfd_link_hash_defined
4654 || h
->type
== bfd_link_hash_defweak
))
4656 value
= h
->u
.def
.value
;
4657 value
+= h
->u
.def
.section
->output_section
->vma
;
4658 value
+= h
->u
.def
.section
->output_offset
;
4663 bfd_sprintf_vma (link_info
.output_bfd
, buf
+ 3, value
);
4667 str
= "[unresolved]";
4672 if (assignment
->exp
->type
.node_class
== etree_provide
)
4677 expld
.assign_name
= NULL
;
4679 fprintf (config
.map_file
, "%-34s", str
);
4680 exp_print_tree (assignment
->exp
);
4685 print_input_statement (lang_input_statement_type
*statm
)
4687 if (statm
->filename
!= NULL
)
4688 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
4691 /* Print all symbols defined in a particular section. This is called
4692 via bfd_link_hash_traverse, or by print_all_symbols. */
4695 print_one_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
)
4697 asection
*sec
= (asection
*) ptr
;
4699 if ((hash_entry
->type
== bfd_link_hash_defined
4700 || hash_entry
->type
== bfd_link_hash_defweak
)
4701 && sec
== hash_entry
->u
.def
.section
)
4703 print_spaces (SECTION_NAME_MAP_LENGTH
);
4705 (hash_entry
->u
.def
.value
4706 + hash_entry
->u
.def
.section
->output_offset
4707 + hash_entry
->u
.def
.section
->output_section
->vma
));
4709 minfo (" %pT\n", hash_entry
->root
.string
);
4716 hash_entry_addr_cmp (const void *a
, const void *b
)
4718 const struct bfd_link_hash_entry
*l
= *(const struct bfd_link_hash_entry
**)a
;
4719 const struct bfd_link_hash_entry
*r
= *(const struct bfd_link_hash_entry
**)b
;
4721 if (l
->u
.def
.value
< r
->u
.def
.value
)
4723 else if (l
->u
.def
.value
> r
->u
.def
.value
)
4730 print_all_symbols (asection
*sec
)
4732 input_section_userdata_type
*ud
= bfd_section_userdata (sec
);
4733 struct map_symbol_def
*def
;
4734 struct bfd_link_hash_entry
**entries
;
4740 *ud
->map_symbol_def_tail
= 0;
4742 /* Sort the symbols by address. */
4743 entries
= (struct bfd_link_hash_entry
**)
4744 obstack_alloc (&map_obstack
,
4745 ud
->map_symbol_def_count
* sizeof (*entries
));
4747 for (i
= 0, def
= ud
->map_symbol_def_head
; def
; def
= def
->next
, i
++)
4748 entries
[i
] = def
->entry
;
4750 qsort (entries
, ud
->map_symbol_def_count
, sizeof (*entries
),
4751 hash_entry_addr_cmp
);
4753 /* Print the symbols. */
4754 for (i
= 0; i
< ud
->map_symbol_def_count
; i
++)
4755 ldemul_print_symbol (entries
[i
], sec
);
4757 obstack_free (&map_obstack
, entries
);
4760 /* Print information about an input section to the map file. */
4763 print_input_section (asection
*i
, bool is_discarded
)
4765 bfd_size_type size
= i
->size
;
4771 minfo (" %s", i
->name
);
4773 len
= 1 + strlen (i
->name
);
4774 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
4779 print_spaces (SECTION_NAME_MAP_LENGTH
- len
);
4781 if (i
->output_section
!= NULL
4782 && i
->output_section
->owner
== link_info
.output_bfd
)
4783 addr
= i
->output_section
->vma
+ i
->output_offset
;
4792 bfd_sprintf_vma (link_info
.output_bfd
, buf
, addr
);
4793 minfo ("0x%s %W %pB\n", buf
, TO_ADDR (size
), i
->owner
);
4795 if (size
!= i
->rawsize
&& i
->rawsize
!= 0)
4797 len
= SECTION_NAME_MAP_LENGTH
+ 3 + strlen (buf
);
4799 minfo (_("%W (size before relaxing)\n"), TO_ADDR (i
->rawsize
));
4802 if (i
->output_section
!= NULL
4803 && i
->output_section
->owner
== link_info
.output_bfd
)
4805 if (link_info
.reduce_memory_overheads
)
4806 bfd_link_hash_traverse (link_info
.hash
, ldemul_print_symbol
, i
);
4808 print_all_symbols (i
);
4810 /* Update print_dot, but make sure that we do not move it
4811 backwards - this could happen if we have overlays and a
4812 later overlay is shorter than an earier one. */
4813 if (addr
+ TO_ADDR (size
) > print_dot
)
4814 print_dot
= addr
+ TO_ADDR (size
);
4819 print_fill_statement (lang_fill_statement_type
*fill
)
4823 fputs (" FILL mask 0x", config
.map_file
);
4824 for (p
= fill
->fill
->data
, size
= fill
->fill
->size
; size
!= 0; p
++, size
--)
4825 fprintf (config
.map_file
, "%02x", *p
);
4826 fputs ("\n", config
.map_file
);
4830 print_data_statement (lang_data_statement_type
*data
)
4836 init_opb (data
->output_section
);
4837 print_spaces (SECTION_NAME_MAP_LENGTH
);
4839 addr
= data
->output_offset
;
4840 if (data
->output_section
!= NULL
)
4841 addr
+= data
->output_section
->vma
;
4869 if (size
< TO_SIZE ((unsigned) 1))
4870 size
= TO_SIZE ((unsigned) 1);
4871 minfo ("0x%V %W %s 0x%v", addr
, TO_ADDR (size
), name
, data
->value
);
4873 if (data
->exp
->type
.node_class
!= etree_value
)
4876 exp_print_tree (data
->exp
);
4881 print_dot
= addr
+ TO_ADDR (size
);
4884 /* Print an address statement. These are generated by options like
4888 print_address_statement (lang_address_statement_type
*address
)
4890 minfo (_("Address of section %s set to "), address
->section_name
);
4891 exp_print_tree (address
->address
);
4895 /* Print a reloc statement. */
4898 print_reloc_statement (lang_reloc_statement_type
*reloc
)
4903 init_opb (reloc
->output_section
);
4904 print_spaces (SECTION_NAME_MAP_LENGTH
);
4906 addr
= reloc
->output_offset
;
4907 if (reloc
->output_section
!= NULL
)
4908 addr
+= reloc
->output_section
->vma
;
4910 size
= bfd_get_reloc_size (reloc
->howto
);
4912 minfo ("0x%V %W RELOC %s ", addr
, TO_ADDR (size
), reloc
->howto
->name
);
4914 if (reloc
->name
!= NULL
)
4915 minfo ("%s+", reloc
->name
);
4917 minfo ("%s+", reloc
->section
->name
);
4919 exp_print_tree (reloc
->addend_exp
);
4923 print_dot
= addr
+ TO_ADDR (size
);
4927 print_padding_statement (lang_padding_statement_type
*s
)
4932 init_opb (s
->output_section
);
4935 len
= sizeof " *fill*" - 1;
4936 print_spaces (SECTION_NAME_MAP_LENGTH
- len
);
4938 addr
= s
->output_offset
;
4939 if (s
->output_section
!= NULL
)
4940 addr
+= s
->output_section
->vma
;
4941 minfo ("0x%V %W ", addr
, TO_ADDR (s
->size
));
4943 if (s
->fill
->size
!= 0)
4947 for (p
= s
->fill
->data
, size
= s
->fill
->size
; size
!= 0; p
++, size
--)
4948 fprintf (config
.map_file
, "%02x", *p
);
4953 print_dot
= addr
+ TO_ADDR (s
->size
);
4957 print_wild_statement (lang_wild_statement_type
*w
,
4958 lang_output_section_statement_type
*os
)
4960 struct wildcard_list
*sec
;
4964 if (w
->exclude_name_list
)
4967 minfo ("EXCLUDE_FILE(%s", w
->exclude_name_list
->name
);
4968 for (tmp
= w
->exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
4969 minfo (" %s", tmp
->name
);
4973 if (w
->filenames_sorted
)
4974 minfo ("SORT_BY_NAME(");
4975 if (w
->filename
!= NULL
)
4976 minfo ("%s", w
->filename
);
4979 if (w
->filenames_sorted
)
4983 for (sec
= w
->section_list
; sec
; sec
= sec
->next
)
4985 int closing_paren
= 0;
4987 switch (sec
->spec
.sorted
)
4993 minfo ("SORT_BY_NAME(");
4998 minfo ("SORT_BY_ALIGNMENT(");
5002 case by_name_alignment
:
5003 minfo ("SORT_BY_NAME(SORT_BY_ALIGNMENT(");
5007 case by_alignment_name
:
5008 minfo ("SORT_BY_ALIGNMENT(SORT_BY_NAME(");
5013 minfo ("SORT_NONE(");
5017 case by_init_priority
:
5018 minfo ("SORT_BY_INIT_PRIORITY(");
5023 if (sec
->spec
.exclude_name_list
!= NULL
)
5026 minfo ("EXCLUDE_FILE(%s", sec
->spec
.exclude_name_list
->name
);
5027 for (tmp
= sec
->spec
.exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
5028 minfo (" %s", tmp
->name
);
5031 if (sec
->spec
.name
!= NULL
)
5032 minfo ("%s", sec
->spec
.name
);
5035 for (;closing_paren
> 0; closing_paren
--)
5044 print_statement_list (w
->children
.head
, os
);
5047 /* Print a group statement. */
5050 print_group (lang_group_statement_type
*s
,
5051 lang_output_section_statement_type
*os
)
5053 fprintf (config
.map_file
, "START GROUP\n");
5054 print_statement_list (s
->children
.head
, os
);
5055 fprintf (config
.map_file
, "END GROUP\n");
5058 /* Print the list of statements in S.
5059 This can be called for any statement type. */
5062 print_statement_list (lang_statement_union_type
*s
,
5063 lang_output_section_statement_type
*os
)
5067 print_statement (s
, os
);
5072 /* Print the first statement in statement list S.
5073 This can be called for any statement type. */
5076 print_statement (lang_statement_union_type
*s
,
5077 lang_output_section_statement_type
*os
)
5079 switch (s
->header
.type
)
5082 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
5085 case lang_constructors_statement_enum
:
5086 if (constructor_list
.head
!= NULL
)
5088 if (constructors_sorted
)
5089 minfo (" SORT (CONSTRUCTORS)\n");
5091 minfo (" CONSTRUCTORS\n");
5092 print_statement_list (constructor_list
.head
, os
);
5095 case lang_wild_statement_enum
:
5096 print_wild_statement (&s
->wild_statement
, os
);
5098 case lang_address_statement_enum
:
5099 print_address_statement (&s
->address_statement
);
5101 case lang_object_symbols_statement_enum
:
5102 minfo (" CREATE_OBJECT_SYMBOLS\n");
5104 case lang_fill_statement_enum
:
5105 print_fill_statement (&s
->fill_statement
);
5107 case lang_data_statement_enum
:
5108 print_data_statement (&s
->data_statement
);
5110 case lang_reloc_statement_enum
:
5111 print_reloc_statement (&s
->reloc_statement
);
5113 case lang_input_section_enum
:
5114 print_input_section (s
->input_section
.section
, false);
5116 case lang_padding_statement_enum
:
5117 print_padding_statement (&s
->padding_statement
);
5119 case lang_output_section_statement_enum
:
5120 print_output_section_statement (&s
->output_section_statement
);
5122 case lang_assignment_statement_enum
:
5123 print_assignment (&s
->assignment_statement
, os
);
5125 case lang_target_statement_enum
:
5126 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
5128 case lang_output_statement_enum
:
5129 minfo ("OUTPUT(%s", s
->output_statement
.name
);
5130 if (output_target
!= NULL
)
5131 minfo (" %s", output_target
);
5134 case lang_input_statement_enum
:
5135 print_input_statement (&s
->input_statement
);
5137 case lang_group_statement_enum
:
5138 print_group (&s
->group_statement
, os
);
5140 case lang_insert_statement_enum
:
5141 minfo ("INSERT %s %s\n",
5142 s
->insert_statement
.is_before
? "BEFORE" : "AFTER",
5143 s
->insert_statement
.where
);
5149 print_statements (void)
5151 print_statement_list (statement_list
.head
, abs_output_section
);
5154 /* Print the first N statements in statement list S to STDERR.
5155 If N == 0, nothing is printed.
5156 If N < 0, the entire list is printed.
5157 Intended to be called from GDB. */
5160 dprint_statement (lang_statement_union_type
*s
, int n
)
5162 FILE *map_save
= config
.map_file
;
5164 config
.map_file
= stderr
;
5167 print_statement_list (s
, abs_output_section
);
5170 while (s
&& --n
>= 0)
5172 print_statement (s
, abs_output_section
);
5177 config
.map_file
= map_save
;
5181 insert_pad (lang_statement_union_type
**ptr
,
5183 bfd_size_type alignment_needed
,
5184 asection
*output_section
,
5187 static fill_type zero_fill
;
5188 lang_statement_union_type
*pad
= NULL
;
5190 if (ptr
!= &statement_list
.head
)
5191 pad
= ((lang_statement_union_type
*)
5192 ((char *) ptr
- offsetof (lang_statement_union_type
, header
.next
)));
5194 && pad
->header
.type
== lang_padding_statement_enum
5195 && pad
->padding_statement
.output_section
== output_section
)
5197 /* Use the existing pad statement. */
5199 else if ((pad
= *ptr
) != NULL
5200 && pad
->header
.type
== lang_padding_statement_enum
5201 && pad
->padding_statement
.output_section
== output_section
)
5203 /* Use the existing pad statement. */
5207 /* Make a new padding statement, linked into existing chain. */
5208 pad
= stat_alloc (sizeof (lang_padding_statement_type
));
5209 pad
->header
.next
= *ptr
;
5211 pad
->header
.type
= lang_padding_statement_enum
;
5212 pad
->padding_statement
.output_section
= output_section
;
5215 pad
->padding_statement
.fill
= fill
;
5217 pad
->padding_statement
.output_offset
= dot
- output_section
->vma
;
5218 pad
->padding_statement
.size
= alignment_needed
;
5219 if (!(output_section
->flags
& SEC_FIXED_SIZE
))
5220 output_section
->size
= TO_SIZE (dot
+ TO_ADDR (alignment_needed
)
5221 - output_section
->vma
);
5224 /* Work out how much this section will move the dot point. */
5228 (lang_statement_union_type
**this_ptr
,
5229 lang_output_section_statement_type
*output_section_statement
,
5234 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
5235 asection
*i
= is
->section
;
5236 asection
*o
= output_section_statement
->bfd_section
;
5239 if (link_info
.non_contiguous_regions
)
5241 /* If the input section I has already been successfully assigned
5242 to an output section other than O, don't bother with it and
5243 let the caller remove it from the list. Keep processing in
5244 case we have already handled O, because the repeated passes
5245 have reinitialized its size. */
5246 if (i
->already_assigned
&& i
->already_assigned
!= o
)
5253 if (i
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
5254 i
->output_offset
= i
->vma
- o
->vma
;
5255 else if (((i
->flags
& SEC_EXCLUDE
) != 0)
5256 || output_section_statement
->ignored
)
5257 i
->output_offset
= dot
- o
->vma
;
5260 bfd_size_type alignment_needed
;
5262 /* Align this section first to the input sections requirement,
5263 then to the output section's requirement. If this alignment
5264 is greater than any seen before, then record it too. Perform
5265 the alignment by inserting a magic 'padding' statement. */
5267 if (output_section_statement
->subsection_alignment
!= NULL
)
5269 = exp_get_power (output_section_statement
->subsection_alignment
,
5270 "subsection alignment");
5272 if (o
->alignment_power
< i
->alignment_power
)
5273 o
->alignment_power
= i
->alignment_power
;
5275 alignment_needed
= align_power (dot
, i
->alignment_power
) - dot
;
5277 if (alignment_needed
!= 0)
5279 insert_pad (this_ptr
, fill
, TO_SIZE (alignment_needed
), o
, dot
);
5280 dot
+= alignment_needed
;
5283 if (link_info
.non_contiguous_regions
)
5285 /* If I would overflow O, let the caller remove I from the
5287 if (output_section_statement
->region
)
5289 bfd_vma end
= output_section_statement
->region
->origin
5290 + output_section_statement
->region
->length
;
5292 if (dot
+ TO_ADDR (i
->size
) > end
)
5294 if (i
->flags
& SEC_LINKER_CREATED
)
5295 einfo (_("%F%P: Output section `%pA' not large enough for "
5296 "the linker-created stubs section `%pA'.\n"),
5297 i
->output_section
, i
);
5299 if (i
->rawsize
&& i
->rawsize
!= i
->size
)
5300 einfo (_("%F%P: Relaxation not supported with "
5301 "--enable-non-contiguous-regions (section `%pA' "
5302 "would overflow `%pA' after it changed size).\n"),
5303 i
, i
->output_section
);
5307 i
->output_section
= NULL
;
5313 /* Remember where in the output section this input section goes. */
5314 i
->output_offset
= dot
- o
->vma
;
5316 /* Mark how big the output section must be to contain this now. */
5317 dot
+= TO_ADDR (i
->size
);
5318 if (!(o
->flags
& SEC_FIXED_SIZE
))
5319 o
->size
= TO_SIZE (dot
- o
->vma
);
5321 if (link_info
.non_contiguous_regions
)
5323 /* Record that I was successfully assigned to O, and update
5324 its actual output section too. */
5325 i
->already_assigned
= o
;
5326 i
->output_section
= o
;
5340 sort_sections_by_lma (const void *arg1
, const void *arg2
)
5342 const asection
*sec1
= ((const struct check_sec
*) arg1
)->sec
;
5343 const asection
*sec2
= ((const struct check_sec
*) arg2
)->sec
;
5345 if (sec1
->lma
< sec2
->lma
)
5347 else if (sec1
->lma
> sec2
->lma
)
5349 else if (sec1
->id
< sec2
->id
)
5351 else if (sec1
->id
> sec2
->id
)
5358 sort_sections_by_vma (const void *arg1
, const void *arg2
)
5360 const asection
*sec1
= ((const struct check_sec
*) arg1
)->sec
;
5361 const asection
*sec2
= ((const struct check_sec
*) arg2
)->sec
;
5363 if (sec1
->vma
< sec2
->vma
)
5365 else if (sec1
->vma
> sec2
->vma
)
5367 else if (sec1
->id
< sec2
->id
)
5369 else if (sec1
->id
> sec2
->id
)
5375 #define IS_TBSS(s) \
5376 ((s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == SEC_THREAD_LOCAL)
5378 #define IGNORE_SECTION(s) \
5379 ((s->flags & SEC_ALLOC) == 0 || IS_TBSS (s))
5381 /* Check to see if any allocated sections overlap with other allocated
5382 sections. This can happen if a linker script specifies the output
5383 section addresses of the two sections. Also check whether any memory
5384 region has overflowed. */
5387 lang_check_section_addresses (void)
5390 struct check_sec
*sections
;
5395 bfd_vma p_start
= 0;
5397 lang_memory_region_type
*m
;
5400 /* Detect address space overflow on allocated sections. */
5401 addr_mask
= ((bfd_vma
) 1 <<
5402 (bfd_arch_bits_per_address (link_info
.output_bfd
) - 1)) - 1;
5403 addr_mask
= (addr_mask
<< 1) + 1;
5404 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5405 if ((s
->flags
& SEC_ALLOC
) != 0)
5407 s_end
= (s
->vma
+ s
->size
) & addr_mask
;
5408 if (s_end
!= 0 && s_end
< (s
->vma
& addr_mask
))
5409 einfo (_("%X%P: section %s VMA wraps around address space\n"),
5413 s_end
= (s
->lma
+ s
->size
) & addr_mask
;
5414 if (s_end
!= 0 && s_end
< (s
->lma
& addr_mask
))
5415 einfo (_("%X%P: section %s LMA wraps around address space\n"),
5420 if (bfd_count_sections (link_info
.output_bfd
) <= 1)
5423 count
= bfd_count_sections (link_info
.output_bfd
);
5424 sections
= XNEWVEC (struct check_sec
, count
);
5426 /* Scan all sections in the output list. */
5428 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5430 if (IGNORE_SECTION (s
)
5434 sections
[count
].sec
= s
;
5435 sections
[count
].warned
= false;
5445 qsort (sections
, count
, sizeof (*sections
), sort_sections_by_lma
);
5447 /* First check section LMAs. There should be no overlap of LMAs on
5448 loadable sections, even with overlays. */
5449 for (p
= NULL
, i
= 0; i
< count
; i
++)
5451 s
= sections
[i
].sec
;
5453 if ((s
->flags
& SEC_LOAD
) != 0)
5456 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
5458 /* Look for an overlap. We have sorted sections by lma, so
5459 we know that s_start >= p_start. Besides the obvious
5460 case of overlap when the current section starts before
5461 the previous one ends, we also must have overlap if the
5462 previous section wraps around the address space. */
5464 && (s_start
<= p_end
5465 || p_end
< p_start
))
5467 einfo (_("%X%P: section %s LMA [%V,%V]"
5468 " overlaps section %s LMA [%V,%V]\n"),
5469 s
->name
, s_start
, s_end
, p
->name
, p_start
, p_end
);
5470 sections
[i
].warned
= true;
5478 /* If any non-zero size allocated section (excluding tbss) starts at
5479 exactly the same VMA as another such section, then we have
5480 overlays. Overlays generated by the OVERLAY keyword will have
5481 this property. It is possible to intentionally generate overlays
5482 that fail this test, but it would be unusual. */
5483 qsort (sections
, count
, sizeof (*sections
), sort_sections_by_vma
);
5485 p_start
= sections
[0].sec
->vma
;
5486 for (i
= 1; i
< count
; i
++)
5488 s_start
= sections
[i
].sec
->vma
;
5489 if (p_start
== s_start
)
5497 /* Now check section VMAs if no overlays were detected. */
5500 for (p
= NULL
, i
= 0; i
< count
; i
++)
5502 s
= sections
[i
].sec
;
5505 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
5508 && !sections
[i
].warned
5509 && (s_start
<= p_end
5510 || p_end
< p_start
))
5511 einfo (_("%X%P: section %s VMA [%V,%V]"
5512 " overlaps section %s VMA [%V,%V]\n"),
5513 s
->name
, s_start
, s_end
, p
->name
, p_start
, p_end
);
5522 /* If any memory region has overflowed, report by how much.
5523 We do not issue this diagnostic for regions that had sections
5524 explicitly placed outside their bounds; os_region_check's
5525 diagnostics are adequate for that case.
5527 FIXME: It is conceivable that m->current - (m->origin + m->length)
5528 might overflow a 32-bit integer. There is, alas, no way to print
5529 a bfd_vma quantity in decimal. */
5530 for (m
= lang_memory_region_list
; m
; m
= m
->next
)
5531 if (m
->had_full_message
)
5533 unsigned long over
= m
->current
- (m
->origin
+ m
->length
);
5534 einfo (ngettext ("%X%P: region `%s' overflowed by %lu byte\n",
5535 "%X%P: region `%s' overflowed by %lu bytes\n",
5537 m
->name_list
.name
, over
);
5541 /* Make sure the new address is within the region. We explicitly permit the
5542 current address to be at the exact end of the region when the address is
5543 non-zero, in case the region is at the end of addressable memory and the
5544 calculation wraps around. */
5547 os_region_check (lang_output_section_statement_type
*os
,
5548 lang_memory_region_type
*region
,
5552 if ((region
->current
< region
->origin
5553 || (region
->current
- region
->origin
> region
->length
))
5554 && ((region
->current
!= region
->origin
+ region
->length
)
5559 einfo (_("%X%P: address 0x%v of %pB section `%s'"
5560 " is not within region `%s'\n"),
5562 os
->bfd_section
->owner
,
5563 os
->bfd_section
->name
,
5564 region
->name_list
.name
);
5566 else if (!region
->had_full_message
)
5568 region
->had_full_message
= true;
5570 einfo (_("%X%P: %pB section `%s' will not fit in region `%s'\n"),
5571 os
->bfd_section
->owner
,
5572 os
->bfd_section
->name
,
5573 region
->name_list
.name
);
5579 ldlang_check_relro_region (lang_statement_union_type
*s
)
5581 seg_align_type
*seg
= &expld
.dataseg
;
5583 if (seg
->relro
== exp_seg_relro_start
)
5585 if (!seg
->relro_start_stat
)
5586 seg
->relro_start_stat
= s
;
5589 ASSERT (seg
->relro_start_stat
== s
);
5592 else if (seg
->relro
== exp_seg_relro_end
)
5594 if (!seg
->relro_end_stat
)
5595 seg
->relro_end_stat
= s
;
5598 ASSERT (seg
->relro_end_stat
== s
);
5603 /* Set the sizes for all the output sections. */
5606 lang_size_sections_1
5607 (lang_statement_union_type
**prev
,
5608 lang_output_section_statement_type
*output_section_statement
,
5614 lang_statement_union_type
*s
;
5615 lang_statement_union_type
*prev_s
= NULL
;
5616 bool removed_prev_s
= false;
5618 /* Size up the sections from their constituent parts. */
5619 for (s
= *prev
; s
!= NULL
; prev_s
= s
, s
= s
->header
.next
)
5621 bool removed
= false;
5623 switch (s
->header
.type
)
5625 case lang_output_section_statement_enum
:
5627 bfd_vma newdot
, after
, dotdelta
;
5628 lang_output_section_statement_type
*os
;
5629 lang_memory_region_type
*r
;
5630 int section_alignment
= 0;
5632 os
= &s
->output_section_statement
;
5633 init_opb (os
->bfd_section
);
5634 if (os
->constraint
== -1)
5637 /* FIXME: We shouldn't need to zero section vmas for ld -r
5638 here, in lang_insert_orphan, or in the default linker scripts.
5639 This is covering for coff backend linker bugs. See PR6945. */
5640 if (os
->addr_tree
== NULL
5641 && bfd_link_relocatable (&link_info
)
5642 && (bfd_get_flavour (link_info
.output_bfd
)
5643 == bfd_target_coff_flavour
))
5644 os
->addr_tree
= exp_intop (0);
5645 if (os
->addr_tree
!= NULL
)
5647 exp_fold_tree (os
->addr_tree
, bfd_abs_section_ptr
, &dot
);
5649 if (expld
.result
.valid_p
)
5651 dot
= expld
.result
.value
;
5652 if (expld
.result
.section
!= NULL
)
5653 dot
+= expld
.result
.section
->vma
;
5655 else if (expld
.phase
!= lang_mark_phase_enum
)
5656 einfo (_("%F%P:%pS: non constant or forward reference"
5657 " address expression for section %s\n"),
5658 os
->addr_tree
, os
->name
);
5661 if (os
->bfd_section
== NULL
)
5662 /* This section was removed or never actually created. */
5665 /* If this is a COFF shared library section, use the size and
5666 address from the input section. FIXME: This is COFF
5667 specific; it would be cleaner if there were some other way
5668 to do this, but nothing simple comes to mind. */
5669 if (((bfd_get_flavour (link_info
.output_bfd
)
5670 == bfd_target_ecoff_flavour
)
5671 || (bfd_get_flavour (link_info
.output_bfd
)
5672 == bfd_target_coff_flavour
))
5673 && (os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
5677 if (os
->children
.head
== NULL
5678 || os
->children
.head
->header
.next
!= NULL
5679 || (os
->children
.head
->header
.type
5680 != lang_input_section_enum
))
5681 einfo (_("%X%P: internal error on COFF shared library"
5682 " section %s\n"), os
->name
);
5684 input
= os
->children
.head
->input_section
.section
;
5685 bfd_set_section_vma (os
->bfd_section
,
5686 bfd_section_vma (input
));
5687 if (!(os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
5688 os
->bfd_section
->size
= input
->size
;
5694 if (bfd_is_abs_section (os
->bfd_section
))
5696 /* No matter what happens, an abs section starts at zero. */
5697 ASSERT (os
->bfd_section
->vma
== 0);
5701 if (os
->addr_tree
== NULL
)
5703 /* No address specified for this section, get one
5704 from the region specification. */
5705 if (os
->region
== NULL
5706 || ((os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))
5707 && os
->region
->name_list
.name
[0] == '*'
5708 && strcmp (os
->region
->name_list
.name
,
5709 DEFAULT_MEMORY_REGION
) == 0))
5711 os
->region
= lang_memory_default (os
->bfd_section
);
5714 /* If a loadable section is using the default memory
5715 region, and some non default memory regions were
5716 defined, issue an error message. */
5718 && !IGNORE_SECTION (os
->bfd_section
)
5719 && !bfd_link_relocatable (&link_info
)
5721 && strcmp (os
->region
->name_list
.name
,
5722 DEFAULT_MEMORY_REGION
) == 0
5723 && lang_memory_region_list
!= NULL
5724 && (strcmp (lang_memory_region_list
->name_list
.name
,
5725 DEFAULT_MEMORY_REGION
) != 0
5726 || lang_memory_region_list
->next
!= NULL
)
5727 && lang_sizing_iteration
== 1)
5729 /* By default this is an error rather than just a
5730 warning because if we allocate the section to the
5731 default memory region we can end up creating an
5732 excessively large binary, or even seg faulting when
5733 attempting to perform a negative seek. See
5734 sources.redhat.com/ml/binutils/2003-04/msg00423.html
5735 for an example of this. This behaviour can be
5736 overridden by the using the --no-check-sections
5738 if (command_line
.check_section_addresses
)
5739 einfo (_("%F%P: error: no memory region specified"
5740 " for loadable section `%s'\n"),
5741 bfd_section_name (os
->bfd_section
));
5743 einfo (_("%P: warning: no memory region specified"
5744 " for loadable section `%s'\n"),
5745 bfd_section_name (os
->bfd_section
));
5748 newdot
= os
->region
->current
;
5749 section_alignment
= os
->bfd_section
->alignment_power
;
5752 section_alignment
= exp_get_power (os
->section_alignment
,
5753 "section alignment");
5755 /* Align to what the section needs. */
5756 if (section_alignment
> 0)
5758 bfd_vma savedot
= newdot
;
5761 newdot
= align_power (newdot
, section_alignment
);
5762 dotdelta
= newdot
- savedot
;
5764 if (lang_sizing_iteration
== 1)
5766 else if (lang_sizing_iteration
> 1)
5768 /* Only report adjustments that would change
5769 alignment from what we have already reported. */
5770 diff
= newdot
- os
->bfd_section
->vma
;
5771 if (!(diff
& (((bfd_vma
) 1 << section_alignment
) - 1)))
5775 && (config
.warn_section_align
5776 || os
->addr_tree
!= NULL
))
5777 einfo (_("%P: warning: "
5778 "start of section %s changed by %ld\n"),
5779 os
->name
, (long) diff
);
5782 bfd_set_section_vma (os
->bfd_section
, newdot
);
5784 os
->bfd_section
->output_offset
= 0;
5787 lang_size_sections_1 (&os
->children
.head
, os
,
5788 os
->fill
, newdot
, relax
, check_regions
);
5790 os
->processed_vma
= true;
5792 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
5793 /* Except for some special linker created sections,
5794 no output section should change from zero size
5795 after strip_excluded_output_sections. A non-zero
5796 size on an ignored section indicates that some
5797 input section was not sized early enough. */
5798 ASSERT (os
->bfd_section
->size
== 0);
5801 dot
= os
->bfd_section
->vma
;
5803 /* Put the section within the requested block size, or
5804 align at the block boundary. */
5806 + TO_ADDR (os
->bfd_section
->size
)
5807 + os
->block_value
- 1)
5808 & - (bfd_vma
) os
->block_value
);
5810 if (!(os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
5811 os
->bfd_section
->size
= TO_SIZE (after
5812 - os
->bfd_section
->vma
);
5815 /* Set section lma. */
5818 r
= lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, false);
5822 bfd_vma lma
= exp_get_abs_int (os
->load_base
, 0, "load base");
5823 os
->bfd_section
->lma
= lma
;
5825 else if (os
->lma_region
!= NULL
)
5827 bfd_vma lma
= os
->lma_region
->current
;
5829 if (os
->align_lma_with_input
)
5833 /* When LMA_REGION is the same as REGION, align the LMA
5834 as we did for the VMA, possibly including alignment
5835 from the bfd section. If a different region, then
5836 only align according to the value in the output
5838 if (os
->lma_region
!= os
->region
)
5839 section_alignment
= exp_get_power (os
->section_alignment
,
5840 "section alignment");
5841 if (section_alignment
> 0)
5842 lma
= align_power (lma
, section_alignment
);
5844 os
->bfd_section
->lma
= lma
;
5846 else if (r
->last_os
!= NULL
5847 && (os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
5852 last
= r
->last_os
->output_section_statement
.bfd_section
;
5854 /* A backwards move of dot should be accompanied by
5855 an explicit assignment to the section LMA (ie.
5856 os->load_base set) because backwards moves can
5857 create overlapping LMAs. */
5859 && os
->bfd_section
->size
!= 0
5860 && dot
+ TO_ADDR (os
->bfd_section
->size
) <= last
->vma
)
5862 /* If dot moved backwards then leave lma equal to
5863 vma. This is the old default lma, which might
5864 just happen to work when the backwards move is
5865 sufficiently large. Nag if this changes anything,
5866 so people can fix their linker scripts. */
5868 if (last
->vma
!= last
->lma
)
5869 einfo (_("%P: warning: dot moved backwards "
5870 "before `%s'\n"), os
->name
);
5874 /* If this is an overlay, set the current lma to that
5875 at the end of the previous section. */
5876 if (os
->sectype
== overlay_section
)
5877 lma
= last
->lma
+ TO_ADDR (last
->size
);
5879 /* Otherwise, keep the same lma to vma relationship
5880 as the previous section. */
5882 lma
= os
->bfd_section
->vma
+ last
->lma
- last
->vma
;
5884 if (section_alignment
> 0)
5885 lma
= align_power (lma
, section_alignment
);
5886 os
->bfd_section
->lma
= lma
;
5889 os
->processed_lma
= true;
5891 /* Keep track of normal sections using the default
5892 lma region. We use this to set the lma for
5893 following sections. Overlays or other linker
5894 script assignment to lma might mean that the
5895 default lma == vma is incorrect.
5896 To avoid warnings about dot moving backwards when using
5897 -Ttext, don't start tracking sections until we find one
5898 of non-zero size or with lma set differently to vma.
5899 Do this tracking before we short-cut the loop so that we
5900 track changes for the case where the section size is zero,
5901 but the lma is set differently to the vma. This is
5902 important, if an orphan section is placed after an
5903 otherwise empty output section that has an explicit lma
5904 set, we want that lma reflected in the orphans lma. */
5905 if (((!IGNORE_SECTION (os
->bfd_section
)
5906 && (os
->bfd_section
->size
!= 0
5907 || (r
->last_os
== NULL
5908 && os
->bfd_section
->vma
!= os
->bfd_section
->lma
)
5909 || (r
->last_os
!= NULL
5910 && dot
>= (r
->last_os
->output_section_statement
5911 .bfd_section
->vma
))))
5912 || os
->sectype
== first_overlay_section
)
5913 && os
->lma_region
== NULL
5914 && !bfd_link_relocatable (&link_info
))
5917 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
5920 /* .tbss sections effectively have zero size. */
5921 if (!IS_TBSS (os
->bfd_section
)
5922 || bfd_link_relocatable (&link_info
))
5923 dotdelta
= TO_ADDR (os
->bfd_section
->size
);
5928 if (os
->update_dot_tree
!= 0)
5929 exp_fold_tree (os
->update_dot_tree
, bfd_abs_section_ptr
, &dot
);
5931 /* Update dot in the region ?
5932 We only do this if the section is going to be allocated,
5933 since unallocated sections do not contribute to the region's
5934 overall size in memory. */
5935 if (os
->region
!= NULL
5936 && (os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
)))
5938 os
->region
->current
= dot
;
5941 /* Make sure the new address is within the region. */
5942 os_region_check (os
, os
->region
, os
->addr_tree
,
5943 os
->bfd_section
->vma
);
5945 if (os
->lma_region
!= NULL
&& os
->lma_region
!= os
->region
5946 && ((os
->bfd_section
->flags
& SEC_LOAD
)
5947 || os
->align_lma_with_input
))
5949 os
->lma_region
->current
= os
->bfd_section
->lma
+ dotdelta
;
5952 os_region_check (os
, os
->lma_region
, NULL
,
5953 os
->bfd_section
->lma
);
5959 case lang_constructors_statement_enum
:
5960 dot
= lang_size_sections_1 (&constructor_list
.head
,
5961 output_section_statement
,
5962 fill
, dot
, relax
, check_regions
);
5965 case lang_data_statement_enum
:
5967 unsigned int size
= 0;
5969 s
->data_statement
.output_offset
=
5970 dot
- output_section_statement
->bfd_section
->vma
;
5971 s
->data_statement
.output_section
=
5972 output_section_statement
->bfd_section
;
5974 /* We might refer to provided symbols in the expression, and
5975 need to mark them as needed. */
5976 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
5978 switch (s
->data_statement
.type
)
5996 if (size
< TO_SIZE ((unsigned) 1))
5997 size
= TO_SIZE ((unsigned) 1);
5998 dot
+= TO_ADDR (size
);
5999 if (!(output_section_statement
->bfd_section
->flags
6001 output_section_statement
->bfd_section
->size
6002 = TO_SIZE (dot
- output_section_statement
->bfd_section
->vma
);
6007 case lang_reloc_statement_enum
:
6011 s
->reloc_statement
.output_offset
=
6012 dot
- output_section_statement
->bfd_section
->vma
;
6013 s
->reloc_statement
.output_section
=
6014 output_section_statement
->bfd_section
;
6015 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
6016 dot
+= TO_ADDR (size
);
6017 if (!(output_section_statement
->bfd_section
->flags
6019 output_section_statement
->bfd_section
->size
6020 = TO_SIZE (dot
- output_section_statement
->bfd_section
->vma
);
6024 case lang_wild_statement_enum
:
6025 dot
= lang_size_sections_1 (&s
->wild_statement
.children
.head
,
6026 output_section_statement
,
6027 fill
, dot
, relax
, check_regions
);
6030 case lang_object_symbols_statement_enum
:
6031 link_info
.create_object_symbols_section
6032 = output_section_statement
->bfd_section
;
6033 output_section_statement
->bfd_section
->flags
|= SEC_KEEP
;
6036 case lang_output_statement_enum
:
6037 case lang_target_statement_enum
:
6040 case lang_input_section_enum
:
6044 i
= s
->input_section
.section
;
6049 if (!bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
6050 einfo (_("%F%P: can't relax section: %E\n"));
6054 dot
= size_input_section (prev
, output_section_statement
,
6055 fill
, &removed
, dot
);
6059 case lang_input_statement_enum
:
6062 case lang_fill_statement_enum
:
6063 s
->fill_statement
.output_section
=
6064 output_section_statement
->bfd_section
;
6066 fill
= s
->fill_statement
.fill
;
6069 case lang_assignment_statement_enum
:
6071 bfd_vma newdot
= dot
;
6072 etree_type
*tree
= s
->assignment_statement
.exp
;
6074 expld
.dataseg
.relro
= exp_seg_relro_none
;
6076 exp_fold_tree (tree
,
6077 output_section_statement
->bfd_section
,
6080 ldlang_check_relro_region (s
);
6082 expld
.dataseg
.relro
= exp_seg_relro_none
;
6084 /* This symbol may be relative to this section. */
6085 if ((tree
->type
.node_class
== etree_provided
6086 || tree
->type
.node_class
== etree_assign
)
6087 && (tree
->assign
.dst
[0] != '.'
6088 || tree
->assign
.dst
[1] != '\0'))
6089 output_section_statement
->update_dot
= 1;
6091 if (!output_section_statement
->ignored
)
6093 if (output_section_statement
== abs_output_section
)
6095 /* If we don't have an output section, then just adjust
6096 the default memory address. */
6097 lang_memory_region_lookup (DEFAULT_MEMORY_REGION
,
6098 false)->current
= newdot
;
6100 else if (newdot
!= dot
)
6102 /* Insert a pad after this statement. We can't
6103 put the pad before when relaxing, in case the
6104 assignment references dot. */
6105 insert_pad (&s
->header
.next
, fill
, TO_SIZE (newdot
- dot
),
6106 output_section_statement
->bfd_section
, dot
);
6108 /* Don't neuter the pad below when relaxing. */
6111 /* If dot is advanced, this implies that the section
6112 should have space allocated to it, unless the
6113 user has explicitly stated that the section
6114 should not be allocated. */
6115 if (output_section_statement
->sectype
!= noalloc_section
6116 && (output_section_statement
->sectype
!= noload_section
6117 || (bfd_get_flavour (link_info
.output_bfd
)
6118 == bfd_target_elf_flavour
)))
6119 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
;
6126 case lang_padding_statement_enum
:
6127 /* If this is the first time lang_size_sections is called,
6128 we won't have any padding statements. If this is the
6129 second or later passes when relaxing, we should allow
6130 padding to shrink. If padding is needed on this pass, it
6131 will be added back in. */
6132 s
->padding_statement
.size
= 0;
6134 /* Make sure output_offset is valid. If relaxation shrinks
6135 the section and this pad isn't needed, it's possible to
6136 have output_offset larger than the final size of the
6137 section. bfd_set_section_contents will complain even for
6138 a pad size of zero. */
6139 s
->padding_statement
.output_offset
6140 = dot
- output_section_statement
->bfd_section
->vma
;
6143 case lang_group_statement_enum
:
6144 dot
= lang_size_sections_1 (&s
->group_statement
.children
.head
,
6145 output_section_statement
,
6146 fill
, dot
, relax
, check_regions
);
6149 case lang_insert_statement_enum
:
6152 /* We can only get here when relaxing is turned on. */
6153 case lang_address_statement_enum
:
6161 /* If an input section doesn't fit in the current output
6162 section, remove it from the list. Handle the case where we
6163 have to remove an input_section statement here: there is a
6164 special case to remove the first element of the list. */
6165 if (link_info
.non_contiguous_regions
&& removed
)
6167 /* If we removed the first element during the previous
6168 iteration, override the loop assignment of prev_s. */
6174 /* If there was a real previous input section, just skip
6176 prev_s
->header
.next
=s
->header
.next
;
6178 removed_prev_s
= false;
6182 /* Remove the first input section of the list. */
6183 *prev
= s
->header
.next
;
6184 removed_prev_s
= true;
6187 /* Move to next element, unless we removed the head of the
6189 if (!removed_prev_s
)
6190 prev
= &s
->header
.next
;
6194 prev
= &s
->header
.next
;
6195 removed_prev_s
= false;
6201 /* Callback routine that is used in _bfd_elf_map_sections_to_segments.
6202 The BFD library has set NEW_SEGMENT to TRUE iff it thinks that
6203 CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different
6204 segments. We are allowed an opportunity to override this decision. */
6207 ldlang_override_segment_assignment (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6208 bfd
*abfd ATTRIBUTE_UNUSED
,
6209 asection
*current_section
,
6210 asection
*previous_section
,
6213 lang_output_section_statement_type
*cur
;
6214 lang_output_section_statement_type
*prev
;
6216 /* The checks below are only necessary when the BFD library has decided
6217 that the two sections ought to be placed into the same segment. */
6221 /* Paranoia checks. */
6222 if (current_section
== NULL
|| previous_section
== NULL
)
6225 /* If this flag is set, the target never wants code and non-code
6226 sections comingled in the same segment. */
6227 if (config
.separate_code
6228 && ((current_section
->flags
^ previous_section
->flags
) & SEC_CODE
))
6231 /* Find the memory regions associated with the two sections.
6232 We call lang_output_section_find() here rather than scanning the list
6233 of output sections looking for a matching section pointer because if
6234 we have a large number of sections then a hash lookup is faster. */
6235 cur
= lang_output_section_find (current_section
->name
);
6236 prev
= lang_output_section_find (previous_section
->name
);
6238 /* More paranoia. */
6239 if (cur
== NULL
|| prev
== NULL
)
6242 /* If the regions are different then force the sections to live in
6243 different segments. See the email thread starting at the following
6244 URL for the reasons why this is necessary:
6245 http://sourceware.org/ml/binutils/2007-02/msg00216.html */
6246 return cur
->region
!= prev
->region
;
6250 one_lang_size_sections_pass (bool *relax
, bool check_regions
)
6252 lang_statement_iteration
++;
6253 if (expld
.phase
!= lang_mark_phase_enum
)
6254 lang_sizing_iteration
++;
6255 lang_size_sections_1 (&statement_list
.head
, abs_output_section
,
6256 0, 0, relax
, check_regions
);
6260 lang_size_segment (void)
6262 /* If XXX_SEGMENT_ALIGN XXX_SEGMENT_END pair was seen, check whether
6263 a page could be saved in the data segment. */
6264 seg_align_type
*seg
= &expld
.dataseg
;
6265 bfd_vma first
, last
;
6267 first
= -seg
->base
& (seg
->commonpagesize
- 1);
6268 last
= seg
->end
& (seg
->commonpagesize
- 1);
6270 && ((seg
->base
& ~(seg
->commonpagesize
- 1))
6271 != (seg
->end
& ~(seg
->commonpagesize
- 1)))
6272 && first
+ last
<= seg
->commonpagesize
)
6274 seg
->phase
= exp_seg_adjust
;
6278 seg
->phase
= exp_seg_done
;
6283 lang_size_relro_segment_1 (void)
6285 seg_align_type
*seg
= &expld
.dataseg
;
6286 bfd_vma relro_end
, desired_end
;
6289 /* Compute the expected PT_GNU_RELRO/PT_LOAD segment end. */
6290 relro_end
= (seg
->relro_end
+ seg
->relropagesize
- 1) & -seg
->relropagesize
;
6292 /* Adjust by the offset arg of XXX_SEGMENT_RELRO_END. */
6293 desired_end
= relro_end
- seg
->relro_offset
;
6295 /* For sections in the relro segment.. */
6296 for (sec
= link_info
.output_bfd
->section_last
; sec
; sec
= sec
->prev
)
6297 if ((sec
->flags
& SEC_ALLOC
) != 0
6298 && sec
->vma
>= seg
->base
6299 && sec
->vma
< seg
->relro_end
- seg
->relro_offset
)
6301 /* Where do we want to put this section so that it ends as
6303 bfd_vma start
, end
, bump
;
6305 end
= start
= sec
->vma
;
6307 end
+= TO_ADDR (sec
->size
);
6308 bump
= desired_end
- end
;
6309 /* We'd like to increase START by BUMP, but we must heed
6310 alignment so the increase might be less than optimum. */
6312 start
&= ~(((bfd_vma
) 1 << sec
->alignment_power
) - 1);
6313 /* This is now the desired end for the previous section. */
6314 desired_end
= start
;
6317 seg
->phase
= exp_seg_relro_adjust
;
6318 ASSERT (desired_end
>= seg
->base
);
6319 seg
->base
= desired_end
;
6324 lang_size_relro_segment (bool *relax
, bool check_regions
)
6326 bool do_reset
= false;
6328 if (link_info
.relro
&& expld
.dataseg
.relro_end
)
6330 bfd_vma data_initial_base
= expld
.dataseg
.base
;
6331 bfd_vma data_relro_end
= lang_size_relro_segment_1 ();
6333 lang_reset_memory_regions ();
6334 one_lang_size_sections_pass (relax
, check_regions
);
6336 /* Assignments to dot, or to output section address in a user
6337 script have increased padding over the original. Revert. */
6338 if (expld
.dataseg
.relro_end
> data_relro_end
)
6340 expld
.dataseg
.base
= data_initial_base
;
6344 else if (lang_size_segment ())
6351 lang_size_sections (bool *relax
, bool check_regions
)
6353 expld
.phase
= lang_allocating_phase_enum
;
6354 expld
.dataseg
.phase
= exp_seg_none
;
6356 one_lang_size_sections_pass (relax
, check_regions
);
6358 if (expld
.dataseg
.phase
!= exp_seg_end_seen
)
6359 expld
.dataseg
.phase
= exp_seg_done
;
6361 if (expld
.dataseg
.phase
== exp_seg_end_seen
)
6364 = lang_size_relro_segment (relax
, check_regions
);
6368 lang_reset_memory_regions ();
6369 one_lang_size_sections_pass (relax
, check_regions
);
6372 if (link_info
.relro
&& expld
.dataseg
.relro_end
)
6374 link_info
.relro_start
= expld
.dataseg
.base
;
6375 link_info
.relro_end
= expld
.dataseg
.relro_end
;
6380 static lang_output_section_statement_type
*current_section
;
6381 static lang_assignment_statement_type
*current_assign
;
6382 static bool prefer_next_section
;
6384 /* Worker function for lang_do_assignments. Recursiveness goes here. */
6387 lang_do_assignments_1 (lang_statement_union_type
*s
,
6388 lang_output_section_statement_type
*current_os
,
6393 for (; s
!= NULL
; s
= s
->header
.next
)
6395 switch (s
->header
.type
)
6397 case lang_constructors_statement_enum
:
6398 dot
= lang_do_assignments_1 (constructor_list
.head
,
6399 current_os
, fill
, dot
, found_end
);
6402 case lang_output_section_statement_enum
:
6404 lang_output_section_statement_type
*os
;
6407 os
= &(s
->output_section_statement
);
6408 os
->after_end
= *found_end
;
6409 init_opb (os
->bfd_section
);
6411 if (os
->bfd_section
!= NULL
)
6413 if (!os
->ignored
&& (os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
6415 current_section
= os
;
6416 prefer_next_section
= false;
6418 newdot
= os
->bfd_section
->vma
;
6420 newdot
= lang_do_assignments_1 (os
->children
.head
,
6421 os
, os
->fill
, newdot
, found_end
);
6424 if (os
->bfd_section
!= NULL
)
6426 newdot
= os
->bfd_section
->vma
;
6428 /* .tbss sections effectively have zero size. */
6429 if (!IS_TBSS (os
->bfd_section
)
6430 || bfd_link_relocatable (&link_info
))
6431 newdot
+= TO_ADDR (os
->bfd_section
->size
);
6433 if (os
->update_dot_tree
!= NULL
)
6434 exp_fold_tree (os
->update_dot_tree
,
6435 bfd_abs_section_ptr
, &newdot
);
6442 case lang_wild_statement_enum
:
6444 dot
= lang_do_assignments_1 (s
->wild_statement
.children
.head
,
6445 current_os
, fill
, dot
, found_end
);
6448 case lang_object_symbols_statement_enum
:
6449 case lang_output_statement_enum
:
6450 case lang_target_statement_enum
:
6453 case lang_data_statement_enum
:
6454 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
6455 if (expld
.result
.valid_p
)
6457 s
->data_statement
.value
= expld
.result
.value
;
6458 if (expld
.result
.section
!= NULL
)
6459 s
->data_statement
.value
+= expld
.result
.section
->vma
;
6461 else if (expld
.phase
== lang_final_phase_enum
)
6462 einfo (_("%F%P: invalid data statement\n"));
6465 switch (s
->data_statement
.type
)
6483 if (size
< TO_SIZE ((unsigned) 1))
6484 size
= TO_SIZE ((unsigned) 1);
6485 dot
+= TO_ADDR (size
);
6489 case lang_reloc_statement_enum
:
6490 exp_fold_tree (s
->reloc_statement
.addend_exp
,
6491 bfd_abs_section_ptr
, &dot
);
6492 if (expld
.result
.valid_p
)
6493 s
->reloc_statement
.addend_value
= expld
.result
.value
;
6494 else if (expld
.phase
== lang_final_phase_enum
)
6495 einfo (_("%F%P: invalid reloc statement\n"));
6496 dot
+= TO_ADDR (bfd_get_reloc_size (s
->reloc_statement
.howto
));
6499 case lang_input_section_enum
:
6501 asection
*in
= s
->input_section
.section
;
6503 if ((in
->flags
& SEC_EXCLUDE
) == 0)
6504 dot
+= TO_ADDR (in
->size
);
6508 case lang_input_statement_enum
:
6511 case lang_fill_statement_enum
:
6512 fill
= s
->fill_statement
.fill
;
6515 case lang_assignment_statement_enum
:
6516 current_assign
= &s
->assignment_statement
;
6517 if (current_assign
->exp
->type
.node_class
!= etree_assert
)
6519 const char *p
= current_assign
->exp
->assign
.dst
;
6521 if (current_os
== abs_output_section
&& p
[0] == '.' && p
[1] == 0)
6522 prefer_next_section
= true;
6526 if (strcmp (p
, "end") == 0)
6529 exp_fold_tree (s
->assignment_statement
.exp
,
6530 (current_os
->bfd_section
!= NULL
6531 ? current_os
->bfd_section
: bfd_und_section_ptr
),
6535 case lang_padding_statement_enum
:
6536 dot
+= TO_ADDR (s
->padding_statement
.size
);
6539 case lang_group_statement_enum
:
6540 dot
= lang_do_assignments_1 (s
->group_statement
.children
.head
,
6541 current_os
, fill
, dot
, found_end
);
6544 case lang_insert_statement_enum
:
6547 case lang_address_statement_enum
:
6559 lang_do_assignments (lang_phase_type phase
)
6561 bool found_end
= false;
6563 current_section
= NULL
;
6564 prefer_next_section
= false;
6565 expld
.phase
= phase
;
6566 lang_statement_iteration
++;
6567 lang_do_assignments_1 (statement_list
.head
,
6568 abs_output_section
, NULL
, 0, &found_end
);
6571 /* For an assignment statement outside of an output section statement,
6572 choose the best of neighbouring output sections to use for values
6576 section_for_dot (void)
6580 /* Assignments belong to the previous output section, unless there
6581 has been an assignment to "dot", in which case following
6582 assignments belong to the next output section. (The assumption
6583 is that an assignment to "dot" is setting up the address for the
6584 next output section.) Except that past the assignment to "_end"
6585 we always associate with the previous section. This exception is
6586 for targets like SH that define an alloc .stack or other
6587 weirdness after non-alloc sections. */
6588 if (current_section
== NULL
|| prefer_next_section
)
6590 lang_statement_union_type
*stmt
;
6591 lang_output_section_statement_type
*os
;
6593 for (stmt
= (lang_statement_union_type
*) current_assign
;
6595 stmt
= stmt
->header
.next
)
6596 if (stmt
->header
.type
== lang_output_section_statement_enum
)
6599 os
= stmt
? &stmt
->output_section_statement
: NULL
;
6602 && (os
->bfd_section
== NULL
6603 || (os
->bfd_section
->flags
& SEC_EXCLUDE
) != 0
6604 || bfd_section_removed_from_list (link_info
.output_bfd
,
6608 if (current_section
== NULL
|| os
== NULL
|| !os
->after_end
)
6611 s
= os
->bfd_section
;
6613 s
= link_info
.output_bfd
->section_last
;
6615 && ((s
->flags
& SEC_ALLOC
) == 0
6616 || (s
->flags
& SEC_THREAD_LOCAL
) != 0))
6621 return bfd_abs_section_ptr
;
6625 s
= current_section
->bfd_section
;
6627 /* The section may have been stripped. */
6629 && ((s
->flags
& SEC_EXCLUDE
) != 0
6630 || (s
->flags
& SEC_ALLOC
) == 0
6631 || (s
->flags
& SEC_THREAD_LOCAL
) != 0
6632 || bfd_section_removed_from_list (link_info
.output_bfd
, s
)))
6635 s
= link_info
.output_bfd
->sections
;
6637 && ((s
->flags
& SEC_ALLOC
) == 0
6638 || (s
->flags
& SEC_THREAD_LOCAL
) != 0))
6643 return bfd_abs_section_ptr
;
6646 /* Array of __start/__stop/.startof./.sizeof/ symbols. */
6648 static struct bfd_link_hash_entry
**start_stop_syms
;
6649 static size_t start_stop_count
= 0;
6650 static size_t start_stop_alloc
= 0;
6652 /* Give start/stop SYMBOL for SEC a preliminary definition, and add it
6653 to start_stop_syms. */
6656 lang_define_start_stop (const char *symbol
, asection
*sec
)
6658 struct bfd_link_hash_entry
*h
;
6660 h
= bfd_define_start_stop (link_info
.output_bfd
, &link_info
, symbol
, sec
);
6663 if (start_stop_count
== start_stop_alloc
)
6665 start_stop_alloc
= 2 * start_stop_alloc
+ 10;
6667 = xrealloc (start_stop_syms
,
6668 start_stop_alloc
* sizeof (*start_stop_syms
));
6670 start_stop_syms
[start_stop_count
++] = h
;
6674 /* Check for input sections whose names match references to
6675 __start_SECNAME or __stop_SECNAME symbols. Give the symbols
6676 preliminary definitions. */
6679 lang_init_start_stop (void)
6683 char leading_char
= bfd_get_symbol_leading_char (link_info
.output_bfd
);
6685 for (abfd
= link_info
.input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
6686 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
6689 const char *secname
= s
->name
;
6691 for (ps
= secname
; *ps
!= '\0'; ps
++)
6692 if (!ISALNUM ((unsigned char) *ps
) && *ps
!= '_')
6696 char *symbol
= (char *) xmalloc (10 + strlen (secname
));
6698 symbol
[0] = leading_char
;
6699 sprintf (symbol
+ (leading_char
!= 0), "__start_%s", secname
);
6700 lang_define_start_stop (symbol
, s
);
6702 symbol
[1] = leading_char
;
6703 memcpy (symbol
+ 1 + (leading_char
!= 0), "__stop", 6);
6704 lang_define_start_stop (symbol
+ 1, s
);
6711 /* Iterate over start_stop_syms. */
6714 foreach_start_stop (void (*func
) (struct bfd_link_hash_entry
*))
6718 for (i
= 0; i
< start_stop_count
; ++i
)
6719 func (start_stop_syms
[i
]);
6722 /* __start and __stop symbols are only supposed to be defined by the
6723 linker for orphan sections, but we now extend that to sections that
6724 map to an output section of the same name. The symbols were
6725 defined early for --gc-sections, before we mapped input to output
6726 sections, so undo those that don't satisfy this rule. */
6729 undef_start_stop (struct bfd_link_hash_entry
*h
)
6731 if (h
->ldscript_def
)
6734 if (h
->u
.def
.section
->output_section
== NULL
6735 || h
->u
.def
.section
->output_section
->owner
!= link_info
.output_bfd
6736 || strcmp (h
->u
.def
.section
->name
,
6737 h
->u
.def
.section
->output_section
->name
) != 0)
6739 asection
*sec
= bfd_get_section_by_name (link_info
.output_bfd
,
6740 h
->u
.def
.section
->name
);
6743 /* When there are more than one input sections with the same
6744 section name, SECNAME, linker picks the first one to define
6745 __start_SECNAME and __stop_SECNAME symbols. When the first
6746 input section is removed by comdat group, we need to check
6747 if there is still an output section with section name
6750 for (i
= sec
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
6751 if (strcmp (h
->u
.def
.section
->name
, i
->name
) == 0)
6753 h
->u
.def
.section
= i
;
6757 h
->type
= bfd_link_hash_undefined
;
6758 h
->u
.undef
.abfd
= NULL
;
6759 if (is_elf_hash_table (link_info
.hash
))
6761 const struct elf_backend_data
*bed
;
6762 struct elf_link_hash_entry
*eh
= (struct elf_link_hash_entry
*) h
;
6763 unsigned int was_forced
= eh
->forced_local
;
6765 bed
= get_elf_backend_data (link_info
.output_bfd
);
6766 (*bed
->elf_backend_hide_symbol
) (&link_info
, eh
, true);
6767 if (!eh
->ref_regular_nonweak
)
6768 h
->type
= bfd_link_hash_undefweak
;
6769 eh
->def_regular
= 0;
6770 eh
->forced_local
= was_forced
;
6776 lang_undef_start_stop (void)
6778 foreach_start_stop (undef_start_stop
);
6781 /* Check for output sections whose names match references to
6782 .startof.SECNAME or .sizeof.SECNAME symbols. Give the symbols
6783 preliminary definitions. */
6786 lang_init_startof_sizeof (void)
6790 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6792 const char *secname
= s
->name
;
6793 char *symbol
= (char *) xmalloc (10 + strlen (secname
));
6795 sprintf (symbol
, ".startof.%s", secname
);
6796 lang_define_start_stop (symbol
, s
);
6798 memcpy (symbol
+ 1, ".size", 5);
6799 lang_define_start_stop (symbol
+ 1, s
);
6804 /* Set .startof., .sizeof., __start and __stop symbols final values. */
6807 set_start_stop (struct bfd_link_hash_entry
*h
)
6810 || h
->type
!= bfd_link_hash_defined
)
6813 if (h
->root
.string
[0] == '.')
6815 /* .startof. or .sizeof. symbol.
6816 .startof. already has final value. */
6817 if (h
->root
.string
[2] == 'i')
6820 h
->u
.def
.value
= TO_ADDR (h
->u
.def
.section
->size
);
6821 h
->u
.def
.section
= bfd_abs_section_ptr
;
6826 /* __start or __stop symbol. */
6827 int has_lead
= bfd_get_symbol_leading_char (link_info
.output_bfd
) != 0;
6829 h
->u
.def
.section
= h
->u
.def
.section
->output_section
;
6830 if (h
->root
.string
[4 + has_lead
] == 'o')
6833 h
->u
.def
.value
= TO_ADDR (h
->u
.def
.section
->size
);
6839 lang_finalize_start_stop (void)
6841 foreach_start_stop (set_start_stop
);
6845 lang_symbol_tweaks (void)
6847 /* Give initial values for __start and __stop symbols, so that ELF
6848 gc_sections will keep sections referenced by these symbols. Must
6849 be done before lang_do_assignments. */
6850 if (config
.build_constructors
)
6851 lang_init_start_stop ();
6853 /* Make __ehdr_start hidden, and set def_regular even though it is
6854 likely undefined at this stage. For lang_check_relocs. */
6855 if (is_elf_hash_table (link_info
.hash
)
6856 && !bfd_link_relocatable (&link_info
))
6858 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*)
6859 bfd_link_hash_lookup (link_info
.hash
, "__ehdr_start",
6860 false, false, true);
6862 /* Only adjust the export class if the symbol was referenced
6863 and not defined, otherwise leave it alone. */
6865 && (h
->root
.type
== bfd_link_hash_new
6866 || h
->root
.type
== bfd_link_hash_undefined
6867 || h
->root
.type
== bfd_link_hash_undefweak
6868 || h
->root
.type
== bfd_link_hash_common
))
6870 const struct elf_backend_data
*bed
;
6871 bed
= get_elf_backend_data (link_info
.output_bfd
);
6872 (*bed
->elf_backend_hide_symbol
) (&link_info
, h
, true);
6873 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
6874 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
6876 h
->root
.linker_def
= 1;
6877 h
->root
.rel_from_abs
= 1;
6885 struct bfd_link_hash_entry
*h
;
6888 if ((bfd_link_relocatable (&link_info
) && !link_info
.gc_sections
)
6889 || bfd_link_dll (&link_info
))
6890 warn
= entry_from_cmdline
;
6894 /* Force the user to specify a root when generating a relocatable with
6895 --gc-sections, unless --gc-keep-exported was also given. */
6896 if (bfd_link_relocatable (&link_info
)
6897 && link_info
.gc_sections
6898 && !link_info
.gc_keep_exported
)
6900 struct bfd_sym_chain
*sym
;
6902 for (sym
= link_info
.gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
6904 h
= bfd_link_hash_lookup (link_info
.hash
, sym
->name
,
6905 false, false, false);
6907 && (h
->type
== bfd_link_hash_defined
6908 || h
->type
== bfd_link_hash_defweak
)
6909 && !bfd_is_const_section (h
->u
.def
.section
))
6913 einfo (_("%F%P: --gc-sections requires a defined symbol root "
6914 "specified by -e or -u\n"));
6917 if (entry_symbol
.name
== NULL
)
6919 /* No entry has been specified. Look for the default entry, but
6920 don't warn if we don't find it. */
6921 entry_symbol
.name
= entry_symbol_default
;
6925 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
.name
,
6926 false, false, true);
6928 && (h
->type
== bfd_link_hash_defined
6929 || h
->type
== bfd_link_hash_defweak
)
6930 && h
->u
.def
.section
->output_section
!= NULL
)
6934 val
= (h
->u
.def
.value
6935 + bfd_section_vma (h
->u
.def
.section
->output_section
)
6936 + h
->u
.def
.section
->output_offset
);
6937 if (!bfd_set_start_address (link_info
.output_bfd
, val
))
6938 einfo (_("%F%P: %s: can't set start address\n"), entry_symbol
.name
);
6945 /* We couldn't find the entry symbol. Try parsing it as a
6947 val
= bfd_scan_vma (entry_symbol
.name
, &send
, 0);
6950 if (!bfd_set_start_address (link_info
.output_bfd
, val
))
6951 einfo (_("%F%P: can't set start address\n"));
6953 /* BZ 2004952: Only use the start of the entry section for executables. */
6954 else if bfd_link_executable (&link_info
)
6958 /* Can't find the entry symbol, and it's not a number. Use
6959 the first address in the text section. */
6960 ts
= bfd_get_section_by_name (link_info
.output_bfd
, entry_section
);
6964 einfo (_("%P: warning: cannot find entry symbol %s;"
6965 " defaulting to %V\n"),
6967 bfd_section_vma (ts
));
6968 if (!bfd_set_start_address (link_info
.output_bfd
,
6969 bfd_section_vma (ts
)))
6970 einfo (_("%F%P: can't set start address\n"));
6975 einfo (_("%P: warning: cannot find entry symbol %s;"
6976 " not setting start address\n"),
6983 einfo (_("%P: warning: cannot find entry symbol %s;"
6984 " not setting start address\n"),
6990 /* This is a small function used when we want to ignore errors from
6994 ignore_bfd_errors (const char *fmt ATTRIBUTE_UNUSED
,
6995 va_list ap ATTRIBUTE_UNUSED
)
6997 /* Don't do anything. */
7000 /* Check that the architecture of all the input files is compatible
7001 with the output file. Also call the backend to let it do any
7002 other checking that is needed. */
7007 lang_input_statement_type
*file
;
7009 const bfd_arch_info_type
*compatible
;
7011 for (file
= (void *) file_chain
.head
;
7015 #if BFD_SUPPORTS_PLUGINS
7016 /* Don't check format of files claimed by plugin. */
7017 if (file
->flags
.claimed
)
7019 #endif /* BFD_SUPPORTS_PLUGINS */
7020 input_bfd
= file
->the_bfd
;
7022 = bfd_arch_get_compatible (input_bfd
, link_info
.output_bfd
,
7023 command_line
.accept_unknown_input_arch
);
7025 /* In general it is not possible to perform a relocatable
7026 link between differing object formats when the input
7027 file has relocations, because the relocations in the
7028 input format may not have equivalent representations in
7029 the output format (and besides BFD does not translate
7030 relocs for other link purposes than a final link). */
7031 if (!file
->flags
.just_syms
7032 && (bfd_link_relocatable (&link_info
)
7033 || link_info
.emitrelocations
)
7034 && (compatible
== NULL
7035 || (bfd_get_flavour (input_bfd
)
7036 != bfd_get_flavour (link_info
.output_bfd
)))
7037 && (bfd_get_file_flags (input_bfd
) & HAS_RELOC
) != 0)
7039 einfo (_("%F%P: relocatable linking with relocations from"
7040 " format %s (%pB) to format %s (%pB) is not supported\n"),
7041 bfd_get_target (input_bfd
), input_bfd
,
7042 bfd_get_target (link_info
.output_bfd
), link_info
.output_bfd
);
7043 /* einfo with %F exits. */
7046 if (compatible
== NULL
)
7048 if (command_line
.warn_mismatch
)
7049 einfo (_("%X%P: %s architecture of input file `%pB'"
7050 " is incompatible with %s output\n"),
7051 bfd_printable_name (input_bfd
), input_bfd
,
7052 bfd_printable_name (link_info
.output_bfd
));
7055 /* If the input bfd has no contents, it shouldn't set the
7056 private data of the output bfd. */
7057 else if (!file
->flags
.just_syms
7058 && ((input_bfd
->flags
& DYNAMIC
) != 0
7059 || bfd_count_sections (input_bfd
) != 0))
7061 bfd_error_handler_type pfn
= NULL
;
7063 /* If we aren't supposed to warn about mismatched input
7064 files, temporarily set the BFD error handler to a
7065 function which will do nothing. We still want to call
7066 bfd_merge_private_bfd_data, since it may set up
7067 information which is needed in the output file. */
7068 if (!command_line
.warn_mismatch
)
7069 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
7070 if (!bfd_merge_private_bfd_data (input_bfd
, &link_info
))
7072 if (command_line
.warn_mismatch
)
7073 einfo (_("%X%P: failed to merge target specific data"
7074 " of file %pB\n"), input_bfd
);
7076 if (!command_line
.warn_mismatch
)
7077 bfd_set_error_handler (pfn
);
7082 /* Look through all the global common symbols and attach them to the
7083 correct section. The -sort-common command line switch may be used
7084 to roughly sort the entries by alignment. */
7089 if (link_info
.inhibit_common_definition
)
7091 if (bfd_link_relocatable (&link_info
)
7092 && !command_line
.force_common_definition
)
7095 if (!config
.sort_common
)
7096 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, NULL
);
7101 if (config
.sort_common
== sort_descending
)
7103 for (power
= 4; power
> 0; power
--)
7104 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7107 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7111 for (power
= 0; power
<= 4; power
++)
7112 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7114 power
= (unsigned int) -1;
7115 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7120 /* Place one common symbol in the correct section. */
7123 lang_one_common (struct bfd_link_hash_entry
*h
, void *info
)
7125 unsigned int power_of_two
;
7129 if (h
->type
!= bfd_link_hash_common
)
7133 power_of_two
= h
->u
.c
.p
->alignment_power
;
7135 if (config
.sort_common
== sort_descending
7136 && power_of_two
< *(unsigned int *) info
)
7138 else if (config
.sort_common
== sort_ascending
7139 && power_of_two
> *(unsigned int *) info
)
7142 section
= h
->u
.c
.p
->section
;
7143 if (!bfd_define_common_symbol (link_info
.output_bfd
, &link_info
, h
))
7144 einfo (_("%F%P: could not define common symbol `%pT': %E\n"),
7147 if (config
.map_file
!= NULL
)
7149 static bool header_printed
;
7154 if (!header_printed
)
7156 minfo (_("\nAllocating common symbols\n"));
7157 minfo (_("Common symbol size file\n\n"));
7158 header_printed
= true;
7161 name
= bfd_demangle (link_info
.output_bfd
, h
->root
.string
,
7162 DMGL_ANSI
| DMGL_PARAMS
);
7165 minfo ("%s", h
->root
.string
);
7166 len
= strlen (h
->root
.string
);
7171 len
= strlen (name
);
7181 sprintf (buf
, "%" PRIx64
, (uint64_t) size
);
7182 fprintf (config
.map_file
, "%*s0x%-16s", 20 - len
, "", buf
);
7184 minfo ("%pB\n", section
->owner
);
7190 /* Handle a single orphan section S, placing the orphan into an appropriate
7191 output section. The effects of the --orphan-handling command line
7192 option are handled here. */
7195 ldlang_place_orphan (asection
*s
)
7197 if (config
.orphan_handling
== orphan_handling_discard
)
7199 lang_output_section_statement_type
*os
;
7200 os
= lang_output_section_statement_lookup (DISCARD_SECTION_NAME
, 0, 1);
7201 if (os
->addr_tree
== NULL
7202 && (bfd_link_relocatable (&link_info
)
7203 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0))
7204 os
->addr_tree
= exp_intop (0);
7205 lang_add_section (&os
->children
, s
, NULL
, NULL
, os
);
7209 lang_output_section_statement_type
*os
;
7210 const char *name
= s
->name
;
7213 if (config
.orphan_handling
== orphan_handling_error
)
7214 einfo (_("%X%P: error: unplaced orphan section `%pA' from `%pB'\n"),
7217 if (config
.unique_orphan_sections
|| unique_section_p (s
, NULL
))
7218 constraint
= SPECIAL
;
7220 os
= ldemul_place_orphan (s
, name
, constraint
);
7223 os
= lang_output_section_statement_lookup (name
, constraint
, 1);
7224 if (os
->addr_tree
== NULL
7225 && (bfd_link_relocatable (&link_info
)
7226 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0))
7227 os
->addr_tree
= exp_intop (0);
7228 lang_add_section (&os
->children
, s
, NULL
, NULL
, os
);
7231 if (config
.orphan_handling
== orphan_handling_warn
)
7232 einfo (_("%P: warning: orphan section `%pA' from `%pB' being "
7233 "placed in section `%s'\n"),
7234 s
, s
->owner
, os
->name
);
7238 /* Run through the input files and ensure that every input section has
7239 somewhere to go. If one is found without a destination then create
7240 an input request and place it into the statement tree. */
7243 lang_place_orphans (void)
7245 LANG_FOR_EACH_INPUT_STATEMENT (file
)
7249 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7251 if (s
->output_section
== NULL
)
7253 /* This section of the file is not attached, root
7254 around for a sensible place for it to go. */
7256 if (file
->flags
.just_syms
)
7257 bfd_link_just_syms (file
->the_bfd
, s
, &link_info
);
7258 else if (lang_discard_section_p (s
))
7259 s
->output_section
= bfd_abs_section_ptr
;
7260 else if (strcmp (s
->name
, "COMMON") == 0)
7262 /* This is a lonely common section which must have
7263 come from an archive. We attach to the section
7264 with the wildcard. */
7265 if (!bfd_link_relocatable (&link_info
)
7266 || command_line
.force_common_definition
)
7268 if (default_common_section
== NULL
)
7269 default_common_section
7270 = lang_output_section_statement_lookup (".bss", 0, 1);
7271 lang_add_section (&default_common_section
->children
, s
,
7272 NULL
, NULL
, default_common_section
);
7276 ldlang_place_orphan (s
);
7283 lang_set_flags (lang_memory_region_type
*ptr
, const char *flags
, int invert
)
7285 flagword
*ptr_flags
;
7287 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
7293 /* PR 17900: An exclamation mark in the attributes reverses
7294 the sense of any of the attributes that follow. */
7297 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
7301 *ptr_flags
|= SEC_ALLOC
;
7305 *ptr_flags
|= SEC_READONLY
;
7309 *ptr_flags
|= SEC_DATA
;
7313 *ptr_flags
|= SEC_CODE
;
7318 *ptr_flags
|= SEC_LOAD
;
7322 einfo (_("%F%P: invalid character %c (%d) in flags\n"),
7330 /* Call a function on each real input file. This function will be
7331 called on an archive, but not on the elements. */
7334 lang_for_each_input_file (void (*func
) (lang_input_statement_type
*))
7336 lang_input_statement_type
*f
;
7338 for (f
= (void *) input_file_chain
.head
;
7340 f
= f
->next_real_file
)
7345 /* Call a function on each real file. The function will be called on
7346 all the elements of an archive which are included in the link, but
7347 will not be called on the archive file itself. */
7350 lang_for_each_file (void (*func
) (lang_input_statement_type
*))
7352 LANG_FOR_EACH_INPUT_STATEMENT (f
)
7360 ldlang_add_file (lang_input_statement_type
*entry
)
7362 lang_statement_append (&file_chain
, entry
, &entry
->next
);
7364 /* The BFD linker needs to have a list of all input BFDs involved in
7366 ASSERT (link_info
.input_bfds_tail
!= &entry
->the_bfd
->link
.next
7367 && entry
->the_bfd
->link
.next
== NULL
);
7368 ASSERT (entry
->the_bfd
!= link_info
.output_bfd
);
7370 *link_info
.input_bfds_tail
= entry
->the_bfd
;
7371 link_info
.input_bfds_tail
= &entry
->the_bfd
->link
.next
;
7372 bfd_set_usrdata (entry
->the_bfd
, entry
);
7373 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
7375 /* Look through the sections and check for any which should not be
7376 included in the link. We need to do this now, so that we can
7377 notice when the backend linker tries to report multiple
7378 definition errors for symbols which are in sections we aren't
7379 going to link. FIXME: It might be better to entirely ignore
7380 symbols which are defined in sections which are going to be
7381 discarded. This would require modifying the backend linker for
7382 each backend which might set the SEC_LINK_ONCE flag. If we do
7383 this, we should probably handle SEC_EXCLUDE in the same way. */
7385 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, entry
);
7389 lang_add_output (const char *name
, int from_script
)
7391 /* Make -o on command line override OUTPUT in script. */
7392 if (!had_output_filename
|| !from_script
)
7394 output_filename
= name
;
7395 had_output_filename
= true;
7399 lang_output_section_statement_type
*
7400 lang_enter_output_section_statement (const char *output_section_statement_name
,
7401 etree_type
*address_exp
,
7402 enum section_type sectype
,
7403 etree_type
*sectype_value
,
7405 etree_type
*subalign
,
7408 int align_with_input
)
7410 lang_output_section_statement_type
*os
;
7412 os
= lang_output_section_statement_lookup (output_section_statement_name
,
7414 current_section
= os
;
7416 if (os
->addr_tree
== NULL
)
7418 os
->addr_tree
= address_exp
;
7420 os
->sectype
= sectype
;
7421 if (sectype
== type_section
|| sectype
== typed_readonly_section
)
7422 os
->sectype_value
= sectype_value
;
7423 else if (sectype
== noload_section
)
7424 os
->flags
= SEC_NEVER_LOAD
;
7426 os
->flags
= SEC_NO_FLAGS
;
7427 os
->block_value
= 1;
7429 /* Make next things chain into subchain of this. */
7430 push_stat_ptr (&os
->children
);
7432 os
->align_lma_with_input
= align_with_input
== ALIGN_WITH_INPUT
;
7433 if (os
->align_lma_with_input
&& align
!= NULL
)
7434 einfo (_("%F%P:%pS: error: align with input and explicit align specified\n"),
7437 os
->subsection_alignment
= subalign
;
7438 os
->section_alignment
= align
;
7440 os
->load_base
= ebase
;
7447 lang_output_statement_type
*new_stmt
;
7449 new_stmt
= new_stat (lang_output_statement
, stat_ptr
);
7450 new_stmt
->name
= output_filename
;
7453 /* Reset the current counters in the regions. */
7456 lang_reset_memory_regions (void)
7458 lang_memory_region_type
*p
= lang_memory_region_list
;
7460 lang_output_section_statement_type
*os
;
7462 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
7464 p
->current
= p
->origin
;
7468 for (os
= (void *) lang_os_list
.head
;
7472 os
->processed_vma
= false;
7473 os
->processed_lma
= false;
7476 for (o
= link_info
.output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
7478 /* Save the last size for possible use by bfd_relax_section. */
7479 o
->rawsize
= o
->size
;
7480 if (!(o
->flags
& SEC_FIXED_SIZE
))
7485 /* Worker for lang_gc_sections_1. */
7488 gc_section_callback (lang_wild_statement_type
*ptr
,
7489 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
7491 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
7492 void *data ATTRIBUTE_UNUSED
)
7494 /* If the wild pattern was marked KEEP, the member sections
7495 should be as well. */
7496 if (ptr
->keep_sections
)
7497 section
->flags
|= SEC_KEEP
;
7500 /* Iterate over sections marking them against GC. */
7503 lang_gc_sections_1 (lang_statement_union_type
*s
)
7505 for (; s
!= NULL
; s
= s
->header
.next
)
7507 switch (s
->header
.type
)
7509 case lang_wild_statement_enum
:
7510 walk_wild (&s
->wild_statement
, gc_section_callback
, NULL
);
7512 case lang_constructors_statement_enum
:
7513 lang_gc_sections_1 (constructor_list
.head
);
7515 case lang_output_section_statement_enum
:
7516 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
7518 case lang_group_statement_enum
:
7519 lang_gc_sections_1 (s
->group_statement
.children
.head
);
7528 lang_gc_sections (void)
7530 /* Keep all sections so marked in the link script. */
7531 lang_gc_sections_1 (statement_list
.head
);
7533 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
7534 the special case of .stabstr debug info. (See bfd/stabs.c)
7535 Twiddle the flag here, to simplify later linker code. */
7536 if (bfd_link_relocatable (&link_info
))
7538 LANG_FOR_EACH_INPUT_STATEMENT (f
)
7541 #if BFD_SUPPORTS_PLUGINS
7542 if (f
->flags
.claimed
)
7545 for (sec
= f
->the_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7546 if ((sec
->flags
& SEC_DEBUGGING
) == 0
7547 || strcmp (sec
->name
, ".stabstr") != 0)
7548 sec
->flags
&= ~SEC_EXCLUDE
;
7552 if (link_info
.gc_sections
)
7553 bfd_gc_sections (link_info
.output_bfd
, &link_info
);
7556 /* Worker for lang_find_relro_sections_1. */
7559 find_relro_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
7560 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
7562 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
7565 /* Discarded, excluded and ignored sections effectively have zero
7567 if (section
->output_section
!= NULL
7568 && section
->output_section
->owner
== link_info
.output_bfd
7569 && (section
->output_section
->flags
& SEC_EXCLUDE
) == 0
7570 && !IGNORE_SECTION (section
)
7571 && section
->size
!= 0)
7573 bool *has_relro_section
= (bool *) data
;
7574 *has_relro_section
= true;
7578 /* Iterate over sections for relro sections. */
7581 lang_find_relro_sections_1 (lang_statement_union_type
*s
,
7582 bool *has_relro_section
)
7584 if (*has_relro_section
)
7587 for (; s
!= NULL
; s
= s
->header
.next
)
7589 if (s
== expld
.dataseg
.relro_end_stat
)
7592 switch (s
->header
.type
)
7594 case lang_wild_statement_enum
:
7595 walk_wild (&s
->wild_statement
,
7596 find_relro_section_callback
,
7599 case lang_constructors_statement_enum
:
7600 lang_find_relro_sections_1 (constructor_list
.head
,
7603 case lang_output_section_statement_enum
:
7604 lang_find_relro_sections_1 (s
->output_section_statement
.children
.head
,
7607 case lang_group_statement_enum
:
7608 lang_find_relro_sections_1 (s
->group_statement
.children
.head
,
7618 lang_find_relro_sections (void)
7620 bool has_relro_section
= false;
7622 /* Check all sections in the link script. */
7624 lang_find_relro_sections_1 (expld
.dataseg
.relro_start_stat
,
7625 &has_relro_section
);
7627 if (!has_relro_section
)
7628 link_info
.relro
= false;
7631 /* Relax all sections until bfd_relax_section gives up. */
7634 lang_relax_sections (bool need_layout
)
7636 /* NB: Also enable relaxation to layout sections for DT_RELR. */
7637 if (RELAXATION_ENABLED
|| link_info
.enable_dt_relr
)
7639 /* We may need more than one relaxation pass. */
7640 int i
= link_info
.relax_pass
;
7642 /* The backend can use it to determine the current pass. */
7643 link_info
.relax_pass
= 0;
7647 /* Keep relaxing until bfd_relax_section gives up. */
7650 link_info
.relax_trip
= -1;
7653 link_info
.relax_trip
++;
7655 /* Note: pe-dll.c does something like this also. If you find
7656 you need to change this code, you probably need to change
7657 pe-dll.c also. DJ */
7659 /* Do all the assignments with our current guesses as to
7661 lang_do_assignments (lang_assigning_phase_enum
);
7663 /* We must do this after lang_do_assignments, because it uses
7665 lang_reset_memory_regions ();
7667 /* Perform another relax pass - this time we know where the
7668 globals are, so can make a better guess. */
7669 relax_again
= false;
7670 lang_size_sections (&relax_again
, false);
7672 while (relax_again
);
7674 link_info
.relax_pass
++;
7681 /* Final extra sizing to report errors. */
7682 lang_do_assignments (lang_assigning_phase_enum
);
7683 lang_reset_memory_regions ();
7684 lang_size_sections (NULL
, true);
7688 #if BFD_SUPPORTS_PLUGINS
7689 /* Find the insert point for the plugin's replacement files. We
7690 place them after the first claimed real object file, or if the
7691 first claimed object is an archive member, after the last real
7692 object file immediately preceding the archive. In the event
7693 no objects have been claimed at all, we return the first dummy
7694 object file on the list as the insert point; that works, but
7695 the callee must be careful when relinking the file_chain as it
7696 is not actually on that chain, only the statement_list and the
7697 input_file list; in that case, the replacement files must be
7698 inserted at the head of the file_chain. */
7700 static lang_input_statement_type
*
7701 find_replacements_insert_point (bool *before
)
7703 lang_input_statement_type
*claim1
, *lastobject
;
7704 lastobject
= (void *) input_file_chain
.head
;
7705 for (claim1
= (void *) file_chain
.head
;
7707 claim1
= claim1
->next
)
7709 if (claim1
->flags
.claimed
)
7711 *before
= claim1
->flags
.claim_archive
;
7712 return claim1
->flags
.claim_archive
? lastobject
: claim1
;
7714 /* Update lastobject if this is a real object file. */
7715 if (claim1
->the_bfd
!= NULL
&& claim1
->the_bfd
->my_archive
== NULL
)
7716 lastobject
= claim1
;
7718 /* No files were claimed by the plugin. Choose the last object
7719 file found on the list (maybe the first, dummy entry) as the
7725 /* Find where to insert ADD, an archive element or shared library
7726 added during a rescan. */
7728 static lang_input_statement_type
**
7729 find_rescan_insertion (lang_input_statement_type
*add
)
7731 bfd
*add_bfd
= add
->the_bfd
;
7732 lang_input_statement_type
*f
;
7733 lang_input_statement_type
*last_loaded
= NULL
;
7734 lang_input_statement_type
*before
= NULL
;
7735 lang_input_statement_type
**iter
= NULL
;
7737 if (add_bfd
->my_archive
!= NULL
)
7738 add_bfd
= add_bfd
->my_archive
;
7740 /* First look through the input file chain, to find an object file
7741 before the one we've rescanned. Normal object files always
7742 appear on both the input file chain and the file chain, so this
7743 lets us get quickly to somewhere near the correct place on the
7744 file chain if it is full of archive elements. Archives don't
7745 appear on the file chain, but if an element has been extracted
7746 then their input_statement->next points at it. */
7747 for (f
= (void *) input_file_chain
.head
;
7749 f
= f
->next_real_file
)
7751 if (f
->the_bfd
== add_bfd
)
7753 before
= last_loaded
;
7754 if (f
->next
!= NULL
)
7755 return &f
->next
->next
;
7757 if (f
->the_bfd
!= NULL
&& f
->next
!= NULL
)
7761 for (iter
= before
? &before
->next
: &file_chain
.head
->input_statement
.next
;
7763 iter
= &(*iter
)->next
)
7764 if (!(*iter
)->flags
.claim_archive
7765 && (*iter
)->the_bfd
->my_archive
== NULL
)
7771 /* Insert SRCLIST into DESTLIST after given element by chaining
7772 on FIELD as the next-pointer. (Counterintuitively does not need
7773 a pointer to the actual after-node itself, just its chain field.) */
7776 lang_list_insert_after (lang_statement_list_type
*destlist
,
7777 lang_statement_list_type
*srclist
,
7778 lang_statement_union_type
**field
)
7780 *(srclist
->tail
) = *field
;
7781 *field
= srclist
->head
;
7782 if (destlist
->tail
== field
)
7783 destlist
->tail
= srclist
->tail
;
7786 /* Detach new nodes added to DESTLIST since the time ORIGLIST
7787 was taken as a copy of it and leave them in ORIGLIST. */
7790 lang_list_remove_tail (lang_statement_list_type
*destlist
,
7791 lang_statement_list_type
*origlist
)
7793 union lang_statement_union
**savetail
;
7794 /* Check that ORIGLIST really is an earlier state of DESTLIST. */
7795 ASSERT (origlist
->head
== destlist
->head
);
7796 savetail
= origlist
->tail
;
7797 origlist
->head
= *(savetail
);
7798 origlist
->tail
= destlist
->tail
;
7799 destlist
->tail
= savetail
;
7803 static lang_statement_union_type
**
7804 find_next_input_statement (lang_statement_union_type
**s
)
7806 for ( ; *s
; s
= &(*s
)->header
.next
)
7808 lang_statement_union_type
**t
;
7809 switch ((*s
)->header
.type
)
7811 case lang_input_statement_enum
:
7813 case lang_wild_statement_enum
:
7814 t
= &(*s
)->wild_statement
.children
.head
;
7816 case lang_group_statement_enum
:
7817 t
= &(*s
)->group_statement
.children
.head
;
7819 case lang_output_section_statement_enum
:
7820 t
= &(*s
)->output_section_statement
.children
.head
;
7825 t
= find_next_input_statement (t
);
7831 #endif /* BFD_SUPPORTS_PLUGINS */
7833 /* Add NAME to the list of garbage collection entry points. */
7836 lang_add_gc_name (const char *name
)
7838 struct bfd_sym_chain
*sym
;
7843 sym
= stat_alloc (sizeof (*sym
));
7845 sym
->next
= link_info
.gc_sym_list
;
7847 link_info
.gc_sym_list
= sym
;
7850 /* Check relocations. */
7853 lang_check_relocs (void)
7855 if (link_info
.check_relocs_after_open_input
)
7859 for (abfd
= link_info
.input_bfds
;
7860 abfd
!= (bfd
*) NULL
; abfd
= abfd
->link
.next
)
7861 if (!bfd_link_check_relocs (abfd
, &link_info
))
7863 /* No object output, fail return. */
7864 config
.make_executable
= false;
7865 /* Note: we do not abort the loop, but rather
7866 continue the scan in case there are other
7867 bad relocations to report. */
7872 /* Look through all output sections looking for places where we can
7873 propagate forward the lma region. */
7876 lang_propagate_lma_regions (void)
7878 lang_output_section_statement_type
*os
;
7880 for (os
= (void *) lang_os_list
.head
;
7884 if (os
->prev
!= NULL
7885 && os
->lma_region
== NULL
7886 && os
->load_base
== NULL
7887 && os
->addr_tree
== NULL
7888 && os
->region
== os
->prev
->region
)
7889 os
->lma_region
= os
->prev
->lma_region
;
7894 warn_non_contiguous_discards (void)
7896 LANG_FOR_EACH_INPUT_STATEMENT (file
)
7898 if ((file
->the_bfd
->flags
& (BFD_LINKER_CREATED
| DYNAMIC
)) != 0
7899 || file
->flags
.just_syms
)
7902 for (asection
*s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7903 if (s
->output_section
== NULL
7904 && (s
->flags
& SEC_LINKER_CREATED
) == 0)
7905 einfo (_("%P: warning: --enable-non-contiguous-regions "
7906 "discards section `%pA' from `%pB'\n"),
7912 reset_one_wild (lang_statement_union_type
*statement
)
7914 if (statement
->header
.type
== lang_wild_statement_enum
)
7916 lang_wild_statement_type
*stmt
= &statement
->wild_statement
;
7917 lang_list_init (&stmt
->matching_sections
);
7922 reset_resolved_wilds (void)
7924 lang_for_each_statement (reset_one_wild
);
7930 /* Finalize dynamic list. */
7931 if (link_info
.dynamic_list
)
7932 lang_finalize_version_expr_head (&link_info
.dynamic_list
->head
);
7934 current_target
= default_target
;
7936 /* Open the output file. */
7937 lang_for_each_statement (ldlang_open_output
);
7940 ldemul_create_output_section_statements ();
7942 /* Add to the hash table all undefineds on the command line. */
7943 lang_place_undefineds ();
7945 if (!bfd_section_already_linked_table_init ())
7946 einfo (_("%F%P: can not create hash table: %E\n"));
7948 /* A first pass through the memory regions ensures that if any region
7949 references a symbol for its origin or length then this symbol will be
7950 added to the symbol table. Having these symbols in the symbol table
7951 means that when we call open_input_bfds PROVIDE statements will
7952 trigger to provide any needed symbols. The regions origins and
7953 lengths are not assigned as a result of this call. */
7954 lang_do_memory_regions (false);
7956 /* Create a bfd for each input file. */
7957 current_target
= default_target
;
7958 lang_statement_iteration
++;
7959 open_input_bfds (statement_list
.head
, OPEN_BFD_NORMAL
);
7961 /* Now that open_input_bfds has processed assignments and provide
7962 statements we can give values to symbolic origin/length now. */
7963 lang_do_memory_regions (true);
7965 #if BFD_SUPPORTS_PLUGINS
7966 if (link_info
.lto_plugin_active
)
7968 lang_statement_list_type added
;
7969 lang_statement_list_type files
, inputfiles
;
7971 ldemul_before_plugin_all_symbols_read ();
7973 /* Now all files are read, let the plugin(s) decide if there
7974 are any more to be added to the link before we call the
7975 emulation's after_open hook. We create a private list of
7976 input statements for this purpose, which we will eventually
7977 insert into the global statement list after the first claimed
7980 /* We need to manipulate all three chains in synchrony. */
7982 inputfiles
= input_file_chain
;
7983 if (plugin_call_all_symbols_read ())
7984 einfo (_("%F%P: %s: plugin reported error after all symbols read\n"),
7985 plugin_error_plugin ());
7986 link_info
.lto_all_symbols_read
= true;
7987 /* Open any newly added files, updating the file chains. */
7988 plugin_undefs
= link_info
.hash
->undefs_tail
;
7989 open_input_bfds (*added
.tail
, OPEN_BFD_NORMAL
);
7990 if (plugin_undefs
== link_info
.hash
->undefs_tail
)
7991 plugin_undefs
= NULL
;
7992 /* Restore the global list pointer now they have all been added. */
7993 lang_list_remove_tail (stat_ptr
, &added
);
7994 /* And detach the fresh ends of the file lists. */
7995 lang_list_remove_tail (&file_chain
, &files
);
7996 lang_list_remove_tail (&input_file_chain
, &inputfiles
);
7997 /* Were any new files added? */
7998 if (added
.head
!= NULL
)
8000 /* If so, we will insert them into the statement list immediately
8001 after the first input file that was claimed by the plugin,
8002 unless that file was an archive in which case it is inserted
8003 immediately before. */
8005 lang_statement_union_type
**prev
;
8006 plugin_insert
= find_replacements_insert_point (&before
);
8007 /* If a plugin adds input files without having claimed any, we
8008 don't really have a good idea where to place them. Just putting
8009 them at the start or end of the list is liable to leave them
8010 outside the crtbegin...crtend range. */
8011 ASSERT (plugin_insert
!= NULL
);
8012 /* Splice the new statement list into the old one. */
8013 prev
= &plugin_insert
->header
.next
;
8016 prev
= find_next_input_statement (prev
);
8017 if (*prev
!= (void *) plugin_insert
->next_real_file
)
8019 /* We didn't find the expected input statement.
8020 Fall back to adding after plugin_insert. */
8021 prev
= &plugin_insert
->header
.next
;
8024 lang_list_insert_after (stat_ptr
, &added
, prev
);
8025 /* Likewise for the file chains. */
8026 lang_list_insert_after (&input_file_chain
, &inputfiles
,
8027 (void *) &plugin_insert
->next_real_file
);
8028 /* We must be careful when relinking file_chain; we may need to
8029 insert the new files at the head of the list if the insert
8030 point chosen is the dummy first input file. */
8031 if (plugin_insert
->filename
)
8032 lang_list_insert_after (&file_chain
, &files
,
8033 (void *) &plugin_insert
->next
);
8035 lang_list_insert_after (&file_chain
, &files
, &file_chain
.head
);
8037 /* Rescan archives in case new undefined symbols have appeared. */
8039 lang_statement_iteration
++;
8040 open_input_bfds (statement_list
.head
, OPEN_BFD_RESCAN
);
8041 lang_list_remove_tail (&file_chain
, &files
);
8042 while (files
.head
!= NULL
)
8044 lang_input_statement_type
**insert
;
8045 lang_input_statement_type
**iter
, *temp
;
8048 insert
= find_rescan_insertion (&files
.head
->input_statement
);
8049 /* All elements from an archive can be added at once. */
8050 iter
= &files
.head
->input_statement
.next
;
8051 my_arch
= files
.head
->input_statement
.the_bfd
->my_archive
;
8052 if (my_arch
!= NULL
)
8053 for (; *iter
!= NULL
; iter
= &(*iter
)->next
)
8054 if ((*iter
)->the_bfd
->my_archive
!= my_arch
)
8057 *insert
= &files
.head
->input_statement
;
8058 files
.head
= (lang_statement_union_type
*) *iter
;
8060 if (file_chain
.tail
== (lang_statement_union_type
**) insert
)
8061 file_chain
.tail
= (lang_statement_union_type
**) iter
;
8062 if (my_arch
!= NULL
)
8064 lang_input_statement_type
*parent
= bfd_usrdata (my_arch
);
8066 parent
->next
= (lang_input_statement_type
*)
8068 - offsetof (lang_input_statement_type
, next
));
8073 #endif /* BFD_SUPPORTS_PLUGINS */
8075 /* Make sure that nobody has tried to add a symbol to this list
8077 ASSERT (link_info
.gc_sym_list
== NULL
);
8079 link_info
.gc_sym_list
= &entry_symbol
;
8081 if (entry_symbol
.name
== NULL
)
8083 link_info
.gc_sym_list
= ldlang_undef_chain_list_head
;
8085 /* entry_symbol is normally initialied by a ENTRY definition in the
8086 linker script or the -e command line option. But if neither of
8087 these have been used, the target specific backend may still have
8088 provided an entry symbol via a call to lang_default_entry().
8089 Unfortunately this value will not be processed until lang_end()
8090 is called, long after this function has finished. So detect this
8091 case here and add the target's entry symbol to the list of starting
8092 points for garbage collection resolution. */
8093 lang_add_gc_name (entry_symbol_default
);
8096 lang_add_gc_name (link_info
.init_function
);
8097 lang_add_gc_name (link_info
.fini_function
);
8099 ldemul_after_open ();
8100 if (config
.map_file
!= NULL
)
8101 lang_print_asneeded ();
8105 bfd_section_already_linked_table_free ();
8107 /* Make sure that we're not mixing architectures. We call this
8108 after all the input files have been opened, but before we do any
8109 other processing, so that any operations merge_private_bfd_data
8110 does on the output file will be known during the rest of the
8114 /* Handle .exports instead of a version script if we're told to do so. */
8115 if (command_line
.version_exports_section
)
8116 lang_do_version_exports_section ();
8118 /* Build all sets based on the information gathered from the input
8120 ldctor_build_sets ();
8122 lang_symbol_tweaks ();
8124 /* PR 13683: We must rerun the assignments prior to running garbage
8125 collection in order to make sure that all symbol aliases are resolved. */
8126 lang_do_assignments (lang_mark_phase_enum
);
8127 expld
.phase
= lang_first_phase_enum
;
8129 /* Size up the common data. */
8133 debug_prefix_tree ();
8137 /* Remove unreferenced sections if asked to. */
8138 lang_gc_sections ();
8140 lang_mark_undefineds ();
8142 /* Check relocations. */
8143 lang_check_relocs ();
8145 ldemul_after_check_relocs ();
8147 /* There might have been new sections created (e.g. as result of
8148 checking relocs to need a .got, or suchlike), so to properly order
8149 them into our lists of matching sections reset them here. */
8150 reset_resolved_wilds ();
8153 /* Update wild statements in case the user gave --sort-section.
8154 Note how the option might have come after the linker script and
8155 so couldn't have been set when the wild statements were created. */
8156 update_wild_statements (statement_list
.head
);
8158 /* Run through the contours of the script and attach input sections
8159 to the correct output sections. */
8160 lang_statement_iteration
++;
8161 map_input_to_output_sections (statement_list
.head
, NULL
, NULL
);
8163 /* Start at the statement immediately after the special abs_section
8164 output statement, so that it isn't reordered. */
8165 process_insert_statements (&lang_os_list
.head
->header
.next
);
8167 ldemul_before_place_orphans ();
8169 /* Find any sections not attached explicitly and handle them. */
8170 lang_place_orphans ();
8172 if (!bfd_link_relocatable (&link_info
))
8176 /* Merge SEC_MERGE sections. This has to be done after GC of
8177 sections, so that GCed sections are not merged, but before
8178 assigning dynamic symbols, since removing whole input sections
8180 bfd_merge_sections (link_info
.output_bfd
, &link_info
);
8182 /* Look for a text section and set the readonly attribute in it. */
8183 found
= bfd_get_section_by_name (link_info
.output_bfd
, ".text");
8187 if (config
.text_read_only
)
8188 found
->flags
|= SEC_READONLY
;
8190 found
->flags
&= ~SEC_READONLY
;
8194 /* Merge together CTF sections. After this, only the symtab-dependent
8195 function and data object sections need adjustment. */
8198 /* Emit the CTF, iff the emulation doesn't need to do late emission after
8199 examining things laid out late, like the strtab. */
8202 /* Copy forward lma regions for output sections in same lma region. */
8203 lang_propagate_lma_regions ();
8205 /* Defining __start/__stop symbols early for --gc-sections to work
8206 around a glibc build problem can result in these symbols being
8207 defined when they should not be. Fix them now. */
8208 if (config
.build_constructors
)
8209 lang_undef_start_stop ();
8211 /* Define .startof./.sizeof. symbols with preliminary values before
8212 dynamic symbols are created. */
8213 if (!bfd_link_relocatable (&link_info
))
8214 lang_init_startof_sizeof ();
8216 /* Do anything special before sizing sections. This is where ELF
8217 and other back-ends size dynamic sections. */
8218 ldemul_before_allocation ();
8220 /* We must record the program headers before we try to fix the
8221 section positions, since they will affect SIZEOF_HEADERS. */
8222 lang_record_phdrs ();
8224 /* Check relro sections. */
8225 if (link_info
.relro
&& !bfd_link_relocatable (&link_info
))
8226 lang_find_relro_sections ();
8228 /* Size up the sections. */
8229 lang_size_sections (NULL
, !RELAXATION_ENABLED
);
8231 /* See if anything special should be done now we know how big
8232 everything is. This is where relaxation is done. */
8233 ldemul_after_allocation ();
8235 /* Fix any __start, __stop, .startof. or .sizeof. symbols. */
8236 lang_finalize_start_stop ();
8238 /* Do all the assignments again, to report errors. Assignment
8239 statements are processed multiple times, updating symbols; In
8240 open_input_bfds, lang_do_assignments, and lang_size_sections.
8241 Since lang_relax_sections calls lang_do_assignments, symbols are
8242 also updated in ldemul_after_allocation. */
8243 lang_do_assignments (lang_final_phase_enum
);
8247 /* Convert absolute symbols to section relative. */
8248 ldexp_finalize_syms ();
8250 /* Make sure that the section addresses make sense. */
8251 if (command_line
.check_section_addresses
)
8252 lang_check_section_addresses ();
8254 if (link_info
.non_contiguous_regions
8255 && link_info
.non_contiguous_regions_warnings
)
8256 warn_non_contiguous_discards ();
8258 /* Check any required symbols are known. */
8259 ldlang_check_require_defined_symbols ();
8264 /* EXPORTED TO YACC */
8267 lang_add_wild (struct wildcard_spec
*filespec
,
8268 struct wildcard_list
*section_list
,
8271 struct wildcard_list
*curr
, *next
;
8272 lang_wild_statement_type
*new_stmt
;
8273 bool any_specs_sorted
= false;
8275 /* Reverse the list as the parser puts it back to front. */
8276 for (curr
= section_list
, section_list
= NULL
;
8278 section_list
= curr
, curr
= next
)
8280 if (curr
->spec
.sorted
!= none
&& curr
->spec
.sorted
!= by_none
)
8281 any_specs_sorted
= true;
8283 curr
->next
= section_list
;
8286 if (filespec
!= NULL
&& filespec
->name
!= NULL
)
8288 if (strcmp (filespec
->name
, "*") == 0)
8289 filespec
->name
= NULL
;
8290 else if (!wildcardp (filespec
->name
))
8291 lang_has_input_file
= true;
8294 new_stmt
= new_stat (lang_wild_statement
, stat_ptr
);
8295 new_stmt
->filename
= NULL
;
8296 new_stmt
->filenames_sorted
= false;
8297 new_stmt
->any_specs_sorted
= any_specs_sorted
;
8298 new_stmt
->section_flag_list
= NULL
;
8299 new_stmt
->exclude_name_list
= NULL
;
8300 if (filespec
!= NULL
)
8302 new_stmt
->filename
= filespec
->name
;
8303 new_stmt
->filenames_sorted
= filespec
->sorted
== by_name
;
8304 new_stmt
->section_flag_list
= filespec
->section_flag_list
;
8305 new_stmt
->exclude_name_list
= filespec
->exclude_name_list
;
8307 new_stmt
->section_list
= section_list
;
8308 new_stmt
->keep_sections
= keep_sections
;
8309 lang_list_init (&new_stmt
->children
);
8310 lang_list_init (&new_stmt
->matching_sections
);
8311 analyze_walk_wild_section_handler (new_stmt
);
8314 printf ("wild %s(", new_stmt
->filename
? new_stmt
->filename
: "*");
8315 for (curr
= new_stmt
->section_list
; curr
; curr
= curr
->next
)
8316 printf ("%s ", curr
->spec
.name
? curr
->spec
.name
: "*");
8322 lang_section_start (const char *name
, etree_type
*address
,
8323 const segment_type
*segment
)
8325 lang_address_statement_type
*ad
;
8327 ad
= new_stat (lang_address_statement
, stat_ptr
);
8328 ad
->section_name
= name
;
8329 ad
->address
= address
;
8330 ad
->segment
= segment
;
8333 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
8334 because of a -e argument on the command line, or zero if this is
8335 called by ENTRY in a linker script. Command line arguments take
8339 lang_add_entry (const char *name
, bool cmdline
)
8341 if (entry_symbol
.name
== NULL
8343 || !entry_from_cmdline
)
8345 entry_symbol
.name
= name
;
8346 entry_from_cmdline
= cmdline
;
8350 /* Set the default start symbol to NAME. .em files should use this,
8351 not lang_add_entry, to override the use of "start" if neither the
8352 linker script nor the command line specifies an entry point. NAME
8353 must be permanently allocated. */
8355 lang_default_entry (const char *name
)
8357 entry_symbol_default
= name
;
8361 lang_add_target (const char *name
)
8363 lang_target_statement_type
*new_stmt
;
8365 new_stmt
= new_stat (lang_target_statement
, stat_ptr
);
8366 new_stmt
->target
= name
;
8370 lang_add_map (const char *name
)
8377 map_option_f
= true;
8385 lang_add_fill (fill_type
*fill
)
8387 lang_fill_statement_type
*new_stmt
;
8389 new_stmt
= new_stat (lang_fill_statement
, stat_ptr
);
8390 new_stmt
->fill
= fill
;
8394 lang_add_data (int type
, union etree_union
*exp
)
8396 lang_data_statement_type
*new_stmt
;
8398 new_stmt
= new_stat (lang_data_statement
, stat_ptr
);
8399 new_stmt
->exp
= exp
;
8400 new_stmt
->type
= type
;
8403 /* Convert escape codes in S.
8404 Supports \n, \r, \t and \NNN octals.
8405 Returns a copy of S in a malloc'ed buffer. */
8408 convert_string (const char * s
)
8410 size_t len
= strlen (s
);
8412 bool escape
= false;
8413 char * buffer
= malloc (len
+ 1);
8416 for (i
= 0, b
= buffer
; i
< len
; i
++)
8425 /* Ignore the escape. */
8428 case 'n': c
= '\n'; break;
8429 case 'r': c
= '\r'; break;
8430 case 't': c
= '\t'; break;
8440 /* We have an octal number. */
8442 unsigned int value
= c
- '0';
8445 if ((c
>= '0') && (c
<= '7'))
8453 if ((c
>= '0') && (c
<= '7'))
8464 /* octal: \777 is treated as '\077' + '7' */
8493 lang_add_string (size_t size
, const char *s
)
8499 string
= convert_string (s
);
8500 len
= strlen (string
);
8502 /* Check if it is ASCIZ command (len == 0) */
8504 /* Make sure that we include the terminating nul byte. */
8506 else if (len
>= size
)
8510 einfo (_("%P:%pS: warning: ASCII string does not fit in allocated space,"
8511 " truncated\n"), NULL
);
8514 for (i
= 0 ; i
< len
; i
++)
8515 lang_add_data (BYTE
, exp_intop (string
[i
]));
8518 lang_add_data (BYTE
, exp_intop ('\0'));
8523 /* Create a new reloc statement. RELOC is the BFD relocation type to
8524 generate. HOWTO is the corresponding howto structure (we could
8525 look this up, but the caller has already done so). SECTION is the
8526 section to generate a reloc against, or NAME is the name of the
8527 symbol to generate a reloc against. Exactly one of SECTION and
8528 NAME must be NULL. ADDEND is an expression for the addend. */
8531 lang_add_reloc (bfd_reloc_code_real_type reloc
,
8532 reloc_howto_type
*howto
,
8535 union etree_union
*addend
)
8537 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
8541 p
->section
= section
;
8543 p
->addend_exp
= addend
;
8545 p
->addend_value
= 0;
8546 p
->output_section
= NULL
;
8547 p
->output_offset
= 0;
8550 lang_assignment_statement_type
*
8551 lang_add_assignment (etree_type
*exp
)
8553 lang_assignment_statement_type
*new_stmt
;
8555 new_stmt
= new_stat (lang_assignment_statement
, stat_ptr
);
8556 new_stmt
->exp
= exp
;
8561 lang_add_attribute (enum statement_enum attribute
)
8563 new_statement (attribute
, sizeof (lang_statement_header_type
), stat_ptr
);
8567 lang_startup (const char *name
)
8569 if (first_file
->filename
!= NULL
)
8571 einfo (_("%F%P: multiple STARTUP files\n"));
8573 first_file
->filename
= name
;
8574 first_file
->local_sym_name
= name
;
8575 first_file
->flags
.real
= true;
8579 lang_float (bool maybe
)
8581 lang_float_flag
= maybe
;
8585 /* Work out the load- and run-time regions from a script statement, and
8586 store them in *LMA_REGION and *REGION respectively.
8588 MEMSPEC is the name of the run-time region, or the value of
8589 DEFAULT_MEMORY_REGION if the statement didn't specify one.
8590 LMA_MEMSPEC is the name of the load-time region, or null if the
8591 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
8592 had an explicit load address.
8594 It is an error to specify both a load region and a load address. */
8597 lang_get_regions (lang_memory_region_type
**region
,
8598 lang_memory_region_type
**lma_region
,
8599 const char *memspec
,
8600 const char *lma_memspec
,
8604 *lma_region
= lang_memory_region_lookup (lma_memspec
, false);
8606 /* If no runtime region or VMA has been specified, but the load region
8607 has been specified, then use the load region for the runtime region
8609 if (lma_memspec
!= NULL
8611 && strcmp (memspec
, DEFAULT_MEMORY_REGION
) == 0)
8612 *region
= *lma_region
;
8614 *region
= lang_memory_region_lookup (memspec
, false);
8616 if (have_lma
&& lma_memspec
!= 0)
8617 einfo (_("%X%P:%pS: section has both a load address and a load region\n"),
8622 lang_leave_output_section_statement (fill_type
*fill
, const char *memspec
,
8623 lang_output_section_phdr_list
*phdrs
,
8624 const char *lma_memspec
)
8626 lang_get_regions (¤t_section
->region
,
8627 ¤t_section
->lma_region
,
8628 memspec
, lma_memspec
,
8629 current_section
->load_base
!= NULL
,
8630 current_section
->addr_tree
!= NULL
);
8632 current_section
->fill
= fill
;
8633 current_section
->phdrs
= phdrs
;
8637 /* Set the output format type. -oformat overrides scripts. */
8640 lang_add_output_format (const char *format
,
8645 if (output_target
== NULL
|| !from_script
)
8647 if (command_line
.endian
== ENDIAN_BIG
8650 else if (command_line
.endian
== ENDIAN_LITTLE
8654 output_target
= format
;
8659 lang_add_insert (const char *where
, int is_before
)
8661 lang_insert_statement_type
*new_stmt
;
8663 new_stmt
= new_stat (lang_insert_statement
, stat_ptr
);
8664 new_stmt
->where
= where
;
8665 new_stmt
->is_before
= is_before
;
8666 saved_script_handle
= previous_script_handle
;
8669 /* Enter a group. This creates a new lang_group_statement, and sets
8670 stat_ptr to build new statements within the group. */
8673 lang_enter_group (void)
8675 lang_group_statement_type
*g
;
8677 g
= new_stat (lang_group_statement
, stat_ptr
);
8678 lang_list_init (&g
->children
);
8679 push_stat_ptr (&g
->children
);
8682 /* Leave a group. This just resets stat_ptr to start writing to the
8683 regular list of statements again. Note that this will not work if
8684 groups can occur inside anything else which can adjust stat_ptr,
8685 but currently they can't. */
8688 lang_leave_group (void)
8693 /* Add a new program header. This is called for each entry in a PHDRS
8694 command in a linker script. */
8697 lang_new_phdr (const char *name
,
8704 struct lang_phdr
*n
, **pp
;
8707 n
= stat_alloc (sizeof (struct lang_phdr
));
8710 n
->type
= exp_get_vma (type
, 0, "program header type");
8711 n
->filehdr
= filehdr
;
8716 hdrs
= n
->type
== 1 && (phdrs
|| filehdr
);
8718 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
8721 && !((*pp
)->filehdr
|| (*pp
)->phdrs
))
8723 einfo (_("%X%P:%pS: PHDRS and FILEHDR are not supported"
8724 " when prior PT_LOAD headers lack them\n"), NULL
);
8731 /* Record the program header information in the output BFD. FIXME: We
8732 should not be calling an ELF specific function here. */
8735 lang_record_phdrs (void)
8739 lang_output_section_phdr_list
*last
;
8740 struct lang_phdr
*l
;
8741 lang_output_section_statement_type
*os
;
8744 secs
= (asection
**) xmalloc (alc
* sizeof (asection
*));
8747 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
8754 for (os
= (void *) lang_os_list
.head
;
8758 lang_output_section_phdr_list
*pl
;
8760 if (os
->constraint
< 0)
8768 if (os
->sectype
== noload_section
8769 || os
->bfd_section
== NULL
8770 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
8773 /* Don't add orphans to PT_INTERP header. */
8779 lang_output_section_statement_type
*tmp_os
;
8781 /* If we have not run across a section with a program
8782 header assigned to it yet, then scan forwards to find
8783 one. This prevents inconsistencies in the linker's
8784 behaviour when a script has specified just a single
8785 header and there are sections in that script which are
8786 not assigned to it, and which occur before the first
8787 use of that header. See here for more details:
8788 http://sourceware.org/ml/binutils/2007-02/msg00291.html */
8789 for (tmp_os
= os
; tmp_os
; tmp_os
= tmp_os
->next
)
8792 last
= tmp_os
->phdrs
;
8796 einfo (_("%F%P: no sections assigned to phdrs\n"));
8801 if (os
->bfd_section
== NULL
)
8804 for (; pl
!= NULL
; pl
= pl
->next
)
8806 if (strcmp (pl
->name
, l
->name
) == 0)
8811 secs
= (asection
**) xrealloc (secs
,
8812 alc
* sizeof (asection
*));
8814 secs
[c
] = os
->bfd_section
;
8821 if (l
->flags
== NULL
)
8824 flags
= exp_get_vma (l
->flags
, 0, "phdr flags");
8829 at
= exp_get_vma (l
->at
, 0, "phdr load address");
8831 if (!bfd_record_phdr (link_info
.output_bfd
, l
->type
,
8832 l
->flags
!= NULL
, flags
, l
->at
!= NULL
,
8833 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
8834 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
8839 /* Make sure all the phdr assignments succeeded. */
8840 for (os
= (void *) lang_os_list
.head
;
8844 lang_output_section_phdr_list
*pl
;
8846 if (os
->constraint
< 0
8847 || os
->bfd_section
== NULL
)
8850 for (pl
= os
->phdrs
;
8853 if (!pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
8854 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
8855 os
->name
, pl
->name
);
8859 /* Record a list of sections which may not be cross referenced. */
8862 lang_add_nocrossref (lang_nocrossref_type
*l
)
8864 struct lang_nocrossrefs
*n
;
8866 n
= (struct lang_nocrossrefs
*) xmalloc (sizeof *n
);
8867 n
->next
= nocrossref_list
;
8869 n
->onlyfirst
= false;
8870 nocrossref_list
= n
;
8872 /* Set notice_all so that we get informed about all symbols. */
8873 link_info
.notice_all
= true;
8876 /* Record a section that cannot be referenced from a list of sections. */
8879 lang_add_nocrossref_to (lang_nocrossref_type
*l
)
8881 lang_add_nocrossref (l
);
8882 nocrossref_list
->onlyfirst
= true;
8885 /* Overlay handling. We handle overlays with some static variables. */
8887 /* The overlay virtual address. */
8888 static etree_type
*overlay_vma
;
8889 /* And subsection alignment. */
8890 static etree_type
*overlay_subalign
;
8892 /* An expression for the maximum section size seen so far. */
8893 static etree_type
*overlay_max
;
8895 /* A list of all the sections in this overlay. */
8897 struct overlay_list
{
8898 struct overlay_list
*next
;
8899 lang_output_section_statement_type
*os
;
8902 static struct overlay_list
*overlay_list
;
8904 /* Start handling an overlay. */
8907 lang_enter_overlay (etree_type
*vma_expr
, etree_type
*subalign
)
8909 /* The grammar should prevent nested overlays from occurring. */
8910 ASSERT (overlay_vma
== NULL
8911 && overlay_subalign
== NULL
8912 && overlay_max
== NULL
);
8914 overlay_vma
= vma_expr
;
8915 overlay_subalign
= subalign
;
8918 /* Start a section in an overlay. We handle this by calling
8919 lang_enter_output_section_statement with the correct VMA.
8920 lang_leave_overlay sets up the LMA and memory regions. */
8923 lang_enter_overlay_section (const char *name
)
8925 struct overlay_list
*n
;
8928 lang_enter_output_section_statement (name
, overlay_vma
, overlay_section
,
8929 0, 0, overlay_subalign
, 0, 0, 0);
8931 /* If this is the first section, then base the VMA of future
8932 sections on this one. This will work correctly even if `.' is
8933 used in the addresses. */
8934 if (overlay_list
== NULL
)
8935 overlay_vma
= exp_nameop (ADDR
, name
);
8937 /* Remember the section. */
8938 n
= (struct overlay_list
*) xmalloc (sizeof *n
);
8939 n
->os
= current_section
;
8940 n
->next
= overlay_list
;
8943 size
= exp_nameop (SIZEOF
, name
);
8945 /* Arrange to work out the maximum section end address. */
8946 if (overlay_max
== NULL
)
8949 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
8952 /* Finish a section in an overlay. There isn't any special to do
8956 lang_leave_overlay_section (fill_type
*fill
,
8957 lang_output_section_phdr_list
*phdrs
)
8964 name
= current_section
->name
;
8966 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
8967 region and that no load-time region has been specified. It doesn't
8968 really matter what we say here, since lang_leave_overlay will
8970 lang_leave_output_section_statement (fill
, DEFAULT_MEMORY_REGION
, phdrs
, 0);
8972 /* Define the magic symbols. */
8974 clean
= (char *) xmalloc (strlen (name
) + 1);
8976 for (s1
= name
; *s1
!= '\0'; s1
++)
8977 if (ISALNUM (*s1
) || *s1
== '_')
8981 buf
= (char *) xmalloc (strlen (clean
) + sizeof "__load_start_");
8982 sprintf (buf
, "__load_start_%s", clean
);
8983 lang_add_assignment (exp_provide (buf
,
8984 exp_nameop (LOADADDR
, name
),
8987 buf
= (char *) xmalloc (strlen (clean
) + sizeof "__load_stop_");
8988 sprintf (buf
, "__load_stop_%s", clean
);
8989 lang_add_assignment (exp_provide (buf
,
8991 exp_nameop (LOADADDR
, name
),
8992 exp_nameop (SIZEOF
, name
)),
8998 /* Finish an overlay. If there are any overlay wide settings, this
8999 looks through all the sections in the overlay and sets them. */
9002 lang_leave_overlay (etree_type
*lma_expr
,
9005 const char *memspec
,
9006 lang_output_section_phdr_list
*phdrs
,
9007 const char *lma_memspec
)
9009 lang_memory_region_type
*region
;
9010 lang_memory_region_type
*lma_region
;
9011 struct overlay_list
*l
;
9012 lang_nocrossref_type
*nocrossref
;
9014 lang_get_regions (®ion
, &lma_region
,
9015 memspec
, lma_memspec
,
9016 lma_expr
!= NULL
, false);
9020 /* After setting the size of the last section, set '.' to end of the
9022 if (overlay_list
!= NULL
)
9024 overlay_list
->os
->update_dot
= 1;
9025 overlay_list
->os
->update_dot_tree
9026 = exp_assign (".", exp_binop ('+', overlay_vma
, overlay_max
), false);
9032 struct overlay_list
*next
;
9034 if (fill
!= NULL
&& l
->os
->fill
== NULL
)
9037 l
->os
->region
= region
;
9038 l
->os
->lma_region
= lma_region
;
9040 /* The first section has the load address specified in the
9041 OVERLAY statement. The rest are worked out from that.
9042 The base address is not needed (and should be null) if
9043 an LMA region was specified. */
9046 l
->os
->load_base
= lma_expr
;
9047 l
->os
->sectype
= first_overlay_section
;
9049 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
9050 l
->os
->phdrs
= phdrs
;
9054 lang_nocrossref_type
*nc
;
9056 nc
= (lang_nocrossref_type
*) xmalloc (sizeof *nc
);
9057 nc
->name
= l
->os
->name
;
9058 nc
->next
= nocrossref
;
9067 if (nocrossref
!= NULL
)
9068 lang_add_nocrossref (nocrossref
);
9071 overlay_list
= NULL
;
9073 overlay_subalign
= NULL
;
9076 /* Version handling. This is only useful for ELF. */
9078 /* If PREV is NULL, return first version pattern matching particular symbol.
9079 If PREV is non-NULL, return first version pattern matching particular
9080 symbol after PREV (previously returned by lang_vers_match). */
9082 static struct bfd_elf_version_expr
*
9083 lang_vers_match (struct bfd_elf_version_expr_head
*head
,
9084 struct bfd_elf_version_expr
*prev
,
9088 const char *cxx_sym
= sym
;
9089 const char *java_sym
= sym
;
9090 struct bfd_elf_version_expr
*expr
= NULL
;
9091 enum demangling_styles curr_style
;
9093 curr_style
= CURRENT_DEMANGLING_STYLE
;
9094 cplus_demangle_set_style (no_demangling
);
9095 c_sym
= bfd_demangle (link_info
.output_bfd
, sym
, DMGL_NO_OPTS
);
9098 cplus_demangle_set_style (curr_style
);
9100 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
9102 cxx_sym
= bfd_demangle (link_info
.output_bfd
, sym
,
9103 DMGL_PARAMS
| DMGL_ANSI
);
9107 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
9109 java_sym
= bfd_demangle (link_info
.output_bfd
, sym
, DMGL_JAVA
);
9114 if (head
->htab
&& (prev
== NULL
|| prev
->literal
))
9116 struct bfd_elf_version_expr e
;
9118 switch (prev
? prev
->mask
: 0)
9121 if (head
->mask
& BFD_ELF_VERSION_C_TYPE
)
9124 expr
= (struct bfd_elf_version_expr
*)
9125 htab_find ((htab_t
) head
->htab
, &e
);
9126 while (expr
&& strcmp (expr
->pattern
, c_sym
) == 0)
9127 if (expr
->mask
== BFD_ELF_VERSION_C_TYPE
)
9133 case BFD_ELF_VERSION_C_TYPE
:
9134 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
9136 e
.pattern
= cxx_sym
;
9137 expr
= (struct bfd_elf_version_expr
*)
9138 htab_find ((htab_t
) head
->htab
, &e
);
9139 while (expr
&& strcmp (expr
->pattern
, cxx_sym
) == 0)
9140 if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
9146 case BFD_ELF_VERSION_CXX_TYPE
:
9147 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
9149 e
.pattern
= java_sym
;
9150 expr
= (struct bfd_elf_version_expr
*)
9151 htab_find ((htab_t
) head
->htab
, &e
);
9152 while (expr
&& strcmp (expr
->pattern
, java_sym
) == 0)
9153 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
9164 /* Finally, try the wildcards. */
9165 if (prev
== NULL
|| prev
->literal
)
9166 expr
= head
->remaining
;
9169 for (; expr
; expr
= expr
->next
)
9176 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
9179 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
9181 else if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
9185 if (fnmatch (expr
->pattern
, s
, 0) == 0)
9191 free ((char *) c_sym
);
9193 free ((char *) cxx_sym
);
9194 if (java_sym
!= sym
)
9195 free ((char *) java_sym
);
9199 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
9200 return a pointer to the symbol name with any backslash quotes removed. */
9203 realsymbol (const char *pattern
)
9206 bool changed
= false, backslash
= false;
9207 char *s
, *symbol
= (char *) xmalloc (strlen (pattern
) + 1);
9209 for (p
= pattern
, s
= symbol
; *p
!= '\0'; ++p
)
9211 /* It is a glob pattern only if there is no preceding
9215 /* Remove the preceding backslash. */
9222 if (*p
== '?' || *p
== '*' || *p
== '[')
9229 backslash
= *p
== '\\';
9245 /* This is called for each variable name or match expression. NEW_NAME is
9246 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
9247 pattern to be matched against symbol names. */
9249 struct bfd_elf_version_expr
*
9250 lang_new_vers_pattern (struct bfd_elf_version_expr
*orig
,
9251 const char *new_name
,
9255 struct bfd_elf_version_expr
*ret
;
9257 ret
= (struct bfd_elf_version_expr
*) xmalloc (sizeof *ret
);
9261 ret
->literal
= true;
9262 ret
->pattern
= literal_p
? new_name
: realsymbol (new_name
);
9263 if (ret
->pattern
== NULL
)
9265 ret
->pattern
= new_name
;
9266 ret
->literal
= false;
9269 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
9270 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
9271 else if (strcasecmp (lang
, "C++") == 0)
9272 ret
->mask
= BFD_ELF_VERSION_CXX_TYPE
;
9273 else if (strcasecmp (lang
, "Java") == 0)
9274 ret
->mask
= BFD_ELF_VERSION_JAVA_TYPE
;
9277 einfo (_("%X%P: unknown language `%s' in version information\n"),
9279 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
9282 return ldemul_new_vers_pattern (ret
);
9285 /* This is called for each set of variable names and match
9288 struct bfd_elf_version_tree
*
9289 lang_new_vers_node (struct bfd_elf_version_expr
*globals
,
9290 struct bfd_elf_version_expr
*locals
)
9292 struct bfd_elf_version_tree
*ret
;
9294 ret
= (struct bfd_elf_version_tree
*) xcalloc (1, sizeof *ret
);
9295 ret
->globals
.list
= globals
;
9296 ret
->locals
.list
= locals
;
9297 ret
->match
= lang_vers_match
;
9298 ret
->name_indx
= (unsigned int) -1;
9302 /* This static variable keeps track of version indices. */
9304 static int version_index
;
9307 version_expr_head_hash (const void *p
)
9309 const struct bfd_elf_version_expr
*e
=
9310 (const struct bfd_elf_version_expr
*) p
;
9312 return htab_hash_string (e
->pattern
);
9316 version_expr_head_eq (const void *p1
, const void *p2
)
9318 const struct bfd_elf_version_expr
*e1
=
9319 (const struct bfd_elf_version_expr
*) p1
;
9320 const struct bfd_elf_version_expr
*e2
=
9321 (const struct bfd_elf_version_expr
*) p2
;
9323 return strcmp (e1
->pattern
, e2
->pattern
) == 0;
9327 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head
*head
)
9330 struct bfd_elf_version_expr
*e
, *next
;
9331 struct bfd_elf_version_expr
**list_loc
, **remaining_loc
;
9333 for (e
= head
->list
; e
; e
= e
->next
)
9337 head
->mask
|= e
->mask
;
9342 head
->htab
= htab_create (count
* 2, version_expr_head_hash
,
9343 version_expr_head_eq
, NULL
);
9344 list_loc
= &head
->list
;
9345 remaining_loc
= &head
->remaining
;
9346 for (e
= head
->list
; e
; e
= next
)
9352 remaining_loc
= &e
->next
;
9356 void **loc
= htab_find_slot ((htab_t
) head
->htab
, e
, INSERT
);
9360 struct bfd_elf_version_expr
*e1
, *last
;
9362 e1
= (struct bfd_elf_version_expr
*) *loc
;
9366 if (e1
->mask
== e
->mask
)
9374 while (e1
&& strcmp (e1
->pattern
, e
->pattern
) == 0);
9378 /* This is a duplicate. */
9379 /* FIXME: Memory leak. Sometimes pattern is not
9380 xmalloced alone, but in larger chunk of memory. */
9381 /* free (e->pattern); */
9386 e
->next
= last
->next
;
9394 list_loc
= &e
->next
;
9398 *remaining_loc
= NULL
;
9399 *list_loc
= head
->remaining
;
9402 head
->remaining
= head
->list
;
9405 /* This is called when we know the name and dependencies of the
9409 lang_register_vers_node (const char *name
,
9410 struct bfd_elf_version_tree
*version
,
9411 struct bfd_elf_version_deps
*deps
)
9413 struct bfd_elf_version_tree
*t
, **pp
;
9414 struct bfd_elf_version_expr
*e1
;
9419 if (link_info
.version_info
!= NULL
9420 && (name
[0] == '\0' || link_info
.version_info
->name
[0] == '\0'))
9422 einfo (_("%X%P: anonymous version tag cannot be combined"
9423 " with other version tags\n"));
9428 /* Make sure this node has a unique name. */
9429 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9430 if (strcmp (t
->name
, name
) == 0)
9431 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
9433 lang_finalize_version_expr_head (&version
->globals
);
9434 lang_finalize_version_expr_head (&version
->locals
);
9436 /* Check the global and local match names, and make sure there
9437 aren't any duplicates. */
9439 for (e1
= version
->globals
.list
; e1
!= NULL
; e1
= e1
->next
)
9441 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9443 struct bfd_elf_version_expr
*e2
;
9445 if (t
->locals
.htab
&& e1
->literal
)
9447 e2
= (struct bfd_elf_version_expr
*)
9448 htab_find ((htab_t
) t
->locals
.htab
, e1
);
9449 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
9451 if (e1
->mask
== e2
->mask
)
9452 einfo (_("%X%P: duplicate expression `%s'"
9453 " in version information\n"), e1
->pattern
);
9457 else if (!e1
->literal
)
9458 for (e2
= t
->locals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
9459 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
9460 && e1
->mask
== e2
->mask
)
9461 einfo (_("%X%P: duplicate expression `%s'"
9462 " in version information\n"), e1
->pattern
);
9466 for (e1
= version
->locals
.list
; e1
!= NULL
; e1
= e1
->next
)
9468 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9470 struct bfd_elf_version_expr
*e2
;
9472 if (t
->globals
.htab
&& e1
->literal
)
9474 e2
= (struct bfd_elf_version_expr
*)
9475 htab_find ((htab_t
) t
->globals
.htab
, e1
);
9476 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
9478 if (e1
->mask
== e2
->mask
)
9479 einfo (_("%X%P: duplicate expression `%s'"
9480 " in version information\n"),
9485 else if (!e1
->literal
)
9486 for (e2
= t
->globals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
9487 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
9488 && e1
->mask
== e2
->mask
)
9489 einfo (_("%X%P: duplicate expression `%s'"
9490 " in version information\n"), e1
->pattern
);
9494 version
->deps
= deps
;
9495 version
->name
= name
;
9496 if (name
[0] != '\0')
9499 version
->vernum
= version_index
;
9502 version
->vernum
= 0;
9504 for (pp
= &link_info
.version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
9509 /* This is called when we see a version dependency. */
9511 struct bfd_elf_version_deps
*
9512 lang_add_vers_depend (struct bfd_elf_version_deps
*list
, const char *name
)
9514 struct bfd_elf_version_deps
*ret
;
9515 struct bfd_elf_version_tree
*t
;
9517 ret
= (struct bfd_elf_version_deps
*) xmalloc (sizeof *ret
);
9520 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9522 if (strcmp (t
->name
, name
) == 0)
9524 ret
->version_needed
= t
;
9529 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
9531 ret
->version_needed
= NULL
;
9536 lang_do_version_exports_section (void)
9538 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
9540 LANG_FOR_EACH_INPUT_STATEMENT (is
)
9542 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
9550 contents
= (char *) xmalloc (len
);
9551 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
9552 einfo (_("%X%P: unable to read .exports section contents\n"), sec
);
9555 while (p
< contents
+ len
)
9557 greg
= lang_new_vers_pattern (greg
, p
, NULL
, false);
9558 p
= strchr (p
, '\0') + 1;
9561 /* Do not free the contents, as we used them creating the regex. */
9563 /* Do not include this section in the link. */
9564 sec
->flags
|= SEC_EXCLUDE
| SEC_KEEP
;
9567 lreg
= lang_new_vers_pattern (NULL
, "*", NULL
, false);
9568 lang_register_vers_node (command_line
.version_exports_section
,
9569 lang_new_vers_node (greg
, lreg
), NULL
);
9572 /* Evaluate LENGTH and ORIGIN parts of MEMORY spec. This is initially
9573 called with UPDATE_REGIONS_P set to FALSE, in this case no errors are
9574 thrown, however, references to symbols in the origin and length fields
9575 will be pushed into the symbol table, this allows PROVIDE statements to
9576 then provide these symbols. This function is called a second time with
9577 UPDATE_REGIONS_P set to TRUE, this time the we update the actual region
9578 data structures, and throw errors if missing symbols are encountered. */
9581 lang_do_memory_regions (bool update_regions_p
)
9583 lang_memory_region_type
*r
= lang_memory_region_list
;
9585 for (; r
!= NULL
; r
= r
->next
)
9589 exp_fold_tree_no_dot (r
->origin_exp
);
9590 if (update_regions_p
)
9592 if (expld
.result
.valid_p
)
9594 r
->origin
= expld
.result
.value
;
9595 r
->current
= r
->origin
;
9598 einfo (_("%P: invalid origin for memory region %s\n"),
9604 exp_fold_tree_no_dot (r
->length_exp
);
9605 if (update_regions_p
)
9607 if (expld
.result
.valid_p
)
9608 r
->length
= expld
.result
.value
;
9610 einfo (_("%P: invalid length for memory region %s\n"),
9618 lang_add_unique (const char *name
)
9620 struct unique_sections
*ent
;
9622 for (ent
= unique_section_list
; ent
; ent
= ent
->next
)
9623 if (strcmp (ent
->name
, name
) == 0)
9626 ent
= (struct unique_sections
*) xmalloc (sizeof *ent
);
9627 ent
->name
= xstrdup (name
);
9628 ent
->next
= unique_section_list
;
9629 unique_section_list
= ent
;
9632 /* Append the list of dynamic symbols to the existing one. */
9635 lang_append_dynamic_list (struct bfd_elf_dynamic_list
**list_p
,
9636 struct bfd_elf_version_expr
*dynamic
)
9640 struct bfd_elf_version_expr
*tail
;
9641 for (tail
= dynamic
; tail
->next
!= NULL
; tail
= tail
->next
)
9643 tail
->next
= (*list_p
)->head
.list
;
9644 (*list_p
)->head
.list
= dynamic
;
9648 struct bfd_elf_dynamic_list
*d
;
9650 d
= (struct bfd_elf_dynamic_list
*) xcalloc (1, sizeof *d
);
9651 d
->head
.list
= dynamic
;
9652 d
->match
= lang_vers_match
;
9657 /* Append the list of C++ typeinfo dynamic symbols to the existing
9661 lang_append_dynamic_list_cpp_typeinfo (void)
9663 const char *symbols
[] =
9665 "typeinfo name for*",
9668 struct bfd_elf_version_expr
*dynamic
= NULL
;
9671 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
9672 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
9675 lang_append_dynamic_list (&link_info
.dynamic_list
, dynamic
);
9678 /* Append the list of C++ operator new and delete dynamic symbols to the
9682 lang_append_dynamic_list_cpp_new (void)
9684 const char *symbols
[] =
9689 struct bfd_elf_version_expr
*dynamic
= NULL
;
9692 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
9693 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
9696 lang_append_dynamic_list (&link_info
.dynamic_list
, dynamic
);
9699 /* Scan a space and/or comma separated string of features. */
9702 lang_ld_feature (char *str
)
9710 while (*p
== ',' || ISSPACE (*p
))
9715 while (*q
&& *q
!= ',' && !ISSPACE (*q
))
9719 if (strcasecmp (p
, "SANE_EXPR") == 0)
9720 config
.sane_expr
= true;
9722 einfo (_("%X%P: unknown feature `%s'\n"), p
);
9728 /* Pretty print memory amount. */
9731 lang_print_memory_size (uint64_t sz
)
9733 if ((sz
& 0x3fffffff) == 0)
9734 printf ("%10" PRIu64
" GB", sz
>> 30);
9735 else if ((sz
& 0xfffff) == 0)
9736 printf ("%10" PRIu64
" MB", sz
>> 20);
9737 else if ((sz
& 0x3ff) == 0)
9738 printf ("%10" PRIu64
" KB", sz
>> 10);
9740 printf (" %10" PRIu64
" B", sz
);
9743 /* Implement --print-memory-usage: disply per region memory usage. */
9746 lang_print_memory_usage (void)
9748 lang_memory_region_type
*r
;
9750 printf ("Memory region Used Size Region Size %%age Used\n");
9751 for (r
= lang_memory_region_list
; r
->next
!= NULL
; r
= r
->next
)
9753 bfd_vma used_length
= r
->current
- r
->origin
;
9755 printf ("%16s: ",r
->name_list
.name
);
9756 lang_print_memory_size (used_length
);
9757 lang_print_memory_size (r
->length
);
9761 double percent
= used_length
* 100.0 / r
->length
;
9762 printf (" %6.2f%%", percent
);