1 /* Linker command language support.
2 Copyright (C) 1991-2022 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
;
227 if (pl
&& (r
= memcmp (spec
->name
, name
, pl
)))
231 size_t inputlen
= strlen (name
);
234 r
= memcmp (spec
->name
+ nl
- sl
, name
+ inputlen
- sl
, sl
);
238 if (nl
== pl
+ sl
+ 1 && spec
->name
[pl
] == '*')
241 return fnmatch (spec
->name
+ pl
, name
+ pl
, 0);
246 ldirname (const char *name
)
248 const char *base
= lbasename (name
);
251 while (base
> name
&& IS_DIR_SEPARATOR (base
[-1]))
255 dirname
= strdup (name
);
256 dirname
[base
- name
] = '\0';
260 /* If PATTERN is of the form archive:file, return a pointer to the
261 separator. If not, return NULL. */
264 archive_path (const char *pattern
)
268 if (link_info
.path_separator
== 0)
271 p
= strchr (pattern
, link_info
.path_separator
);
272 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
273 if (p
== NULL
|| link_info
.path_separator
!= ':')
276 /* Assume a match on the second char is part of drive specifier,
277 as in "c:\silly.dos". */
278 if (p
== pattern
+ 1 && ISALPHA (*pattern
))
279 p
= strchr (p
+ 1, link_info
.path_separator
);
284 /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path,
285 return whether F matches FILE_SPEC. */
288 input_statement_is_archive_path (const char *file_spec
, char *sep
,
289 lang_input_statement_type
*f
)
294 || name_match (sep
+ 1, f
->filename
) == 0)
295 && ((sep
!= file_spec
)
296 == (f
->the_bfd
!= NULL
&& f
->the_bfd
->my_archive
!= NULL
)))
300 if (sep
!= file_spec
)
302 const char *aname
= bfd_get_filename (f
->the_bfd
->my_archive
);
304 match
= name_match (file_spec
, aname
) == 0;
305 *sep
= link_info
.path_separator
;
312 unique_section_p (const asection
*sec
,
313 const lang_output_section_statement_type
*os
)
315 struct unique_sections
*unam
;
318 if (!link_info
.resolve_section_groups
319 && sec
->owner
!= NULL
320 && bfd_is_group_section (sec
->owner
, sec
))
322 && strcmp (os
->name
, DISCARD_SECTION_NAME
) == 0);
325 for (unam
= unique_section_list
; unam
; unam
= unam
->next
)
326 if (name_match (unam
->name
, secnam
) == 0)
332 /* Generic traversal routines for finding matching sections. */
334 /* Return true if FILE matches a pattern in EXCLUDE_LIST, otherwise return
338 walk_wild_file_in_exclude_list (struct name_list
*exclude_list
,
339 lang_input_statement_type
*file
)
341 struct name_list
*list_tmp
;
343 for (list_tmp
= exclude_list
;
345 list_tmp
= list_tmp
->next
)
347 char *p
= archive_path (list_tmp
->name
);
351 if (input_statement_is_archive_path (list_tmp
->name
, p
, file
))
355 else if (name_match (list_tmp
->name
, file
->filename
) == 0)
358 /* FIXME: Perhaps remove the following at some stage? Matching
359 unadorned archives like this was never documented and has
360 been superceded by the archive:path syntax. */
361 else if (file
->the_bfd
!= NULL
362 && file
->the_bfd
->my_archive
!= NULL
363 && name_match (list_tmp
->name
,
364 bfd_get_filename (file
->the_bfd
->my_archive
)) == 0)
371 /* Add SECTION (from input FILE) to the list of matching sections
372 within PTR (the matching wildcard is SEC). */
375 add_matching_section (lang_wild_statement_type
*ptr
,
376 struct wildcard_list
*sec
,
378 lang_input_statement_type
*file
)
380 lang_input_matcher_type
*new_section
;
381 /* Add a section reference to the list. */
382 new_section
= new_stat (lang_input_matcher
, &ptr
->matching_sections
);
383 new_section
->section
= section
;
384 new_section
->pattern
= sec
;
385 new_section
->input_stmt
= file
;
388 /* Process section S (from input file FILE) in relation to wildcard
389 statement PTR. We already know that a prefix of the name of S matches
390 some wildcard in PTR's wildcard list. Here we check if the filename
391 matches as well (if it's specified) and if any of the wildcards in fact
395 walk_wild_section_match (lang_wild_statement_type
*ptr
,
396 lang_input_statement_type
*file
,
399 struct wildcard_list
*sec
;
400 const char *file_spec
= ptr
->filename
;
403 /* Check if filenames match. */
404 if (file_spec
== NULL
)
406 else if ((p
= archive_path (file_spec
)) != NULL
)
408 if (!input_statement_is_archive_path (file_spec
, p
, file
))
411 else if (wildcardp (file_spec
))
413 if (fnmatch (file_spec
, file
->filename
, 0) != 0)
418 lang_input_statement_type
*f
;
419 /* Perform the iteration over a single file. */
420 f
= lookup_name (file_spec
);
425 /* If filename is excluded we're done. */
426 if (walk_wild_file_in_exclude_list (ptr
->exclude_name_list
, file
))
429 /* Check section name against each wildcard spec. If there's no
430 wildcard all sections match. */
431 sec
= ptr
->section_list
;
433 add_matching_section (ptr
, sec
, s
, file
);
436 const char *sname
= bfd_section_name (s
);
437 for (; sec
!= NULL
; sec
= sec
->next
)
439 if (sec
->spec
.name
!= NULL
440 && spec_match (&sec
->spec
, sname
) != 0)
443 /* Don't process sections from files which were excluded. */
444 if (!walk_wild_file_in_exclude_list (sec
->spec
.exclude_name_list
,
446 add_matching_section (ptr
, sec
, s
, file
);
451 /* Return the numerical value of the init_priority attribute from
452 section name NAME. */
455 get_init_priority (const asection
*sec
)
457 const char *name
= bfd_section_name (sec
);
460 /* GCC uses the following section names for the init_priority
461 attribute with numerical values 101 to 65535 inclusive. A
462 lower value means a higher priority.
464 1: .init_array.NNNNN/.fini_array.NNNNN: Where NNNNN is the
465 decimal numerical value of the init_priority attribute.
466 The order of execution in .init_array is forward and
467 .fini_array is backward.
468 2: .ctors.NNNNN/.dtors.NNNNN: Where NNNNN is 65535 minus the
469 decimal numerical value of the init_priority attribute.
470 The order of execution in .ctors is backward and .dtors
473 .init_array.NNNNN sections would normally be placed in an output
474 .init_array section, .fini_array.NNNNN in .fini_array,
475 .ctors.NNNNN in .ctors, and .dtors.NNNNN in .dtors. This means
476 we should sort by increasing number (and could just use
477 SORT_BY_NAME in scripts). However if .ctors.NNNNN sections are
478 being placed in .init_array (which may also contain
479 .init_array.NNNNN sections) or .dtors.NNNNN sections are being
480 placed in .fini_array then we need to extract the init_priority
481 attribute and sort on that. */
482 dot
= strrchr (name
, '.');
483 if (dot
!= NULL
&& ISDIGIT (dot
[1]))
486 unsigned long init_priority
= strtoul (dot
+ 1, &end
, 10);
490 && (strncmp (name
, ".ctors", 6) == 0
491 || strncmp (name
, ".dtors", 6) == 0))
492 init_priority
= 65535 - init_priority
;
493 if (init_priority
<= INT_MAX
)
494 return init_priority
;
500 /* Compare sections ASEC and BSEC according to SORT. */
503 compare_section (sort_type sort
, asection
*asec
, asection
*bsec
)
506 int a_priority
, b_priority
;
513 case by_init_priority
:
514 a_priority
= get_init_priority (asec
);
515 b_priority
= get_init_priority (bsec
);
516 if (a_priority
< 0 || b_priority
< 0)
518 ret
= a_priority
- b_priority
;
524 case by_alignment_name
:
525 ret
= bfd_section_alignment (bsec
) - bfd_section_alignment (asec
);
532 ret
= strcmp (bfd_section_name (asec
), bfd_section_name (bsec
));
535 case by_name_alignment
:
536 ret
= strcmp (bfd_section_name (asec
), bfd_section_name (bsec
));
542 ret
= bfd_section_alignment (bsec
) - bfd_section_alignment (asec
);
549 /* PE puts the sort key in the input statement. */
552 sort_filename (bfd
*abfd
)
554 lang_input_statement_type
*is
= bfd_usrdata (abfd
);
557 return bfd_get_filename (abfd
);
560 /* Handle wildcard sorting. This returns the place in a binary search tree
561 where this FILE:SECTION should be inserted for wild statement WILD where
562 the spec SEC was the matching one. The tree is later linearized. */
564 static lang_section_bst_type
**
565 wild_sort (lang_wild_statement_type
*wild
,
566 struct wildcard_list
*sec
,
567 lang_input_statement_type
*file
,
570 lang_section_bst_type
**tree
;
572 if (!wild
->filenames_sorted
573 && (sec
== NULL
|| sec
->spec
.sorted
== none
574 || sec
->spec
.sorted
== by_none
))
576 /* We might be called even if _this_ spec doesn't need sorting,
577 in which case we simply append at the right end of tree. */
578 return wild
->rightmost
;
584 /* Sorting by filename takes precedence over sorting by section
587 if (wild
->filenames_sorted
)
592 asection
*lsec
= (*tree
)->section
;
594 /* The PE support for the .idata section as generated by
595 dlltool assumes that files will be sorted by the name of
596 the archive and then the name of the file within the
599 fa
= file
->the_bfd
->my_archive
!= NULL
;
601 fn
= sort_filename (file
->the_bfd
->my_archive
);
603 fn
= sort_filename (file
->the_bfd
);
605 la
= lsec
->owner
->my_archive
!= NULL
;
607 ln
= sort_filename (lsec
->owner
->my_archive
);
609 ln
= sort_filename (lsec
->owner
);
611 i
= filename_cmp (fn
, ln
);
613 { tree
= &((*tree
)->right
); continue; }
615 { tree
= &((*tree
)->left
); continue; }
620 fn
= sort_filename (file
->the_bfd
);
622 ln
= sort_filename (lsec
->owner
);
624 i
= filename_cmp (fn
, ln
);
626 { tree
= &((*tree
)->right
); continue; }
628 { tree
= &((*tree
)->left
); continue; }
632 /* Here either the files are not sorted by name, or we are
633 looking at the sections for this file. */
635 /* Find the correct node to append this section. */
636 if (compare_section (sec
->spec
.sorted
, section
, (*tree
)->section
) < 0)
637 tree
= &((*tree
)->left
);
639 tree
= &((*tree
)->right
);
645 /* Use wild_sort to build a BST to sort sections. */
648 output_section_callback_sort (lang_wild_statement_type
*ptr
,
649 struct wildcard_list
*sec
,
651 lang_input_statement_type
*file
,
654 lang_section_bst_type
*node
;
655 lang_section_bst_type
**tree
;
656 lang_output_section_statement_type
*os
;
658 os
= (lang_output_section_statement_type
*) output
;
660 if (unique_section_p (section
, os
))
663 node
= (lang_section_bst_type
*) xmalloc (sizeof (lang_section_bst_type
));
666 node
->section
= section
;
667 node
->pattern
= ptr
->section_list
;
669 tree
= wild_sort (ptr
, sec
, file
, section
);
673 if (tree
== ptr
->rightmost
)
674 ptr
->rightmost
= &node
->right
;
678 /* Convert a sorted sections' BST back to list form. */
681 output_section_callback_tree_to_list (lang_wild_statement_type
*ptr
,
682 lang_section_bst_type
*tree
,
686 output_section_callback_tree_to_list (ptr
, tree
->left
, output
);
688 lang_add_section (&ptr
->children
, tree
->section
, tree
->pattern
,
689 ptr
->section_flag_list
,
690 (lang_output_section_statement_type
*) output
);
693 output_section_callback_tree_to_list (ptr
, tree
->right
, output
);
699 /* Sections are matched against wildcard statements via a prefix tree.
700 The prefix tree holds prefixes of all matching patterns (up to the first
701 wildcard character), and the wild statement from which those patterns
702 came. When matching a section name against the tree we're walking through
703 the tree character by character. Each statement we hit is one that
704 potentially matches. This is checked by actually going through the
705 (glob) matching routines.
707 When the section name turns out to actually match we record that section
708 in the wild statements list of matching sections. */
710 /* A prefix can be matched by multiple statement, so we need a list of them. */
711 struct wild_stmt_list
713 lang_wild_statement_type
*stmt
;
714 struct wild_stmt_list
*next
;
717 /* The prefix tree itself. */
720 /* The list of all children (linked via .next). */
721 struct prefixtree
*child
;
722 struct prefixtree
*next
;
723 /* This tree node is responsible for the prefix of parent plus 'c'. */
725 /* The statements that potentially can match this prefix. */
726 struct wild_stmt_list
*stmt
;
729 /* We always have a root node in the prefix tree. It corresponds to the
730 empty prefix. E.g. a glob like "*" would sit in this root. */
731 static struct prefixtree the_root
, *ptroot
= &the_root
;
733 /* Given a prefix tree in *TREE, corresponding to prefix P, find or
734 INSERT the tree node corresponding to prefix P+C. */
736 static struct prefixtree
*
737 get_prefix_tree (struct prefixtree
**tree
, char c
, bool insert
)
739 struct prefixtree
*t
;
740 for (t
= *tree
; t
; t
= t
->next
)
745 t
= (struct prefixtree
*) obstack_alloc (&pt_obstack
, sizeof *t
);
754 /* Add STMT to the set of statements that can be matched by the prefix
755 corresponding to prefix tree T. */
758 pt_add_stmt (struct prefixtree
*t
, lang_wild_statement_type
*stmt
)
760 struct wild_stmt_list
*sl
, **psl
;
761 sl
= (struct wild_stmt_list
*) obstack_alloc (&pt_obstack
, sizeof *sl
);
770 /* Insert STMT into the global prefix tree. */
773 insert_prefix_tree (lang_wild_statement_type
*stmt
)
775 struct wildcard_list
*sec
;
776 struct prefixtree
*t
;
778 if (!stmt
->section_list
)
780 /* If we have no section_list (no wildcards in the wild STMT),
781 then every section name will match, so add this to the root. */
782 pt_add_stmt (ptroot
, stmt
);
786 for (sec
= stmt
->section_list
; sec
; sec
= sec
->next
)
788 const char *name
= sec
->spec
.name
? sec
->spec
.name
: "*";
791 for (; (c
= *name
); name
++)
793 if (c
== '*' || c
== '[' || c
== '?')
795 t
= get_prefix_tree (&t
->child
, c
, true);
797 /* If we hit a glob character, the matching prefix is what we saw
798 until now. If we hit the end of pattern (hence it's no glob) then
799 we can do better: we only need to record a match when a section name
800 completely matches, not merely a prefix, so record the trailing 0
803 t
= get_prefix_tree (&t
->child
, 0, true);
804 pt_add_stmt (t
, stmt
);
808 /* Dump T indented by INDENT spaces. */
811 debug_prefix_tree_rec (struct prefixtree
*t
, int indent
)
813 for (; t
; t
= t
->next
)
815 struct wild_stmt_list
*sl
;
816 printf ("%*s %c", indent
, "", t
->c
);
817 for (sl
= t
->stmt
; sl
; sl
= sl
->next
)
819 struct wildcard_list
*curr
;
820 printf (" %p ", sl
->stmt
);
821 for (curr
= sl
->stmt
->section_list
; curr
; curr
= curr
->next
)
822 printf ("%s ", curr
->spec
.name
? curr
->spec
.name
: "*");
825 debug_prefix_tree_rec (t
->child
, indent
+ 2);
829 /* Dump the global prefix tree. */
832 debug_prefix_tree (void)
834 debug_prefix_tree_rec (ptroot
, 2);
837 /* Like strcspn() but start to look from the end to beginning of
838 S. Returns the length of the suffix of S consisting entirely
839 of characters not in REJECT. */
842 rstrcspn (const char *s
, const char *reject
)
844 size_t len
= strlen (s
), sufflen
= 0;
848 if (strchr (reject
, c
) != 0)
855 /* Analyze the wildcards in wild statement PTR to setup various
856 things for quick matching. */
859 analyze_walk_wild_section_handler (lang_wild_statement_type
*ptr
)
861 struct wildcard_list
*sec
;
864 ptr
->rightmost
= &ptr
->tree
;
866 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
870 sec
->spec
.namelen
= strlen (sec
->spec
.name
);
871 sec
->spec
.prefixlen
= strcspn (sec
->spec
.name
, "?*[");
872 sec
->spec
.suffixlen
= rstrcspn (sec
->spec
.name
+ sec
->spec
.prefixlen
,
876 sec
->spec
.namelen
= sec
->spec
.prefixlen
= sec
->spec
.suffixlen
= 0;
879 insert_prefix_tree (ptr
);
882 /* Match all sections from FILE against the global prefix tree,
883 and record them into each wild statement that has a match. */
886 resolve_wild_sections (lang_input_statement_type
*file
)
890 if (file
->flags
.just_syms
)
893 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
895 const char *sname
= bfd_section_name (s
);
897 struct prefixtree
*t
= ptroot
;
898 //printf (" YYY consider %s of %s\n", sname, file->the_bfd->filename);
903 struct wild_stmt_list
*sl
;
904 for (sl
= t
->stmt
; sl
; sl
= sl
->next
)
906 walk_wild_section_match (sl
->stmt
, file
, s
);
907 //printf (" ZZZ maybe place into %p\n", sl->stmt);
913 t
= get_prefix_tree (&t
->child
, c
, false);
919 /* Match all sections from all input files against the global prefix tree. */
924 LANG_FOR_EACH_INPUT_STATEMENT (f
)
926 //printf("XXX %s\n", f->filename);
927 if (f
->the_bfd
== NULL
928 || !bfd_check_format (f
->the_bfd
, bfd_archive
))
929 resolve_wild_sections (f
);
934 /* This is an archive file. We must map each member of the
935 archive separately. */
936 member
= bfd_openr_next_archived_file (f
->the_bfd
, NULL
);
937 while (member
!= NULL
)
939 /* When lookup_name is called, it will call the add_symbols
940 entry point for the archive. For each element of the
941 archive which is included, BFD will call ldlang_add_file,
942 which will set the usrdata field of the member to the
943 lang_input_statement. */
944 if (bfd_usrdata (member
) != NULL
)
945 resolve_wild_sections (bfd_usrdata (member
));
947 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
953 /* For each input section that matches wild statement S calls
954 CALLBACK with DATA. */
957 walk_wild (lang_wild_statement_type
*s
, callback_t callback
, void *data
)
959 lang_statement_union_type
*l
;
961 for (l
= s
->matching_sections
.head
; l
; l
= l
->header
.next
)
963 (*callback
) (s
, l
->input_matcher
.pattern
, l
->input_matcher
.section
,
964 l
->input_matcher
.input_stmt
, data
);
968 /* lang_for_each_statement walks the parse tree and calls the provided
969 function for each node, except those inside output section statements
970 with constraint set to -1. */
973 lang_for_each_statement_worker (void (*func
) (lang_statement_union_type
*),
974 lang_statement_union_type
*s
)
976 for (; s
!= NULL
; s
= s
->header
.next
)
980 switch (s
->header
.type
)
982 case lang_constructors_statement_enum
:
983 lang_for_each_statement_worker (func
, constructor_list
.head
);
985 case lang_output_section_statement_enum
:
986 if (s
->output_section_statement
.constraint
!= -1)
987 lang_for_each_statement_worker
988 (func
, s
->output_section_statement
.children
.head
);
990 case lang_wild_statement_enum
:
991 lang_for_each_statement_worker (func
,
992 s
->wild_statement
.children
.head
);
994 case lang_group_statement_enum
:
995 lang_for_each_statement_worker (func
,
996 s
->group_statement
.children
.head
);
998 case lang_data_statement_enum
:
999 case lang_reloc_statement_enum
:
1000 case lang_object_symbols_statement_enum
:
1001 case lang_output_statement_enum
:
1002 case lang_target_statement_enum
:
1003 case lang_input_section_enum
:
1004 case lang_input_statement_enum
:
1005 case lang_assignment_statement_enum
:
1006 case lang_padding_statement_enum
:
1007 case lang_address_statement_enum
:
1008 case lang_fill_statement_enum
:
1009 case lang_insert_statement_enum
:
1019 lang_for_each_statement (void (*func
) (lang_statement_union_type
*))
1021 lang_for_each_statement_worker (func
, statement_list
.head
);
1024 /*----------------------------------------------------------------------*/
1027 lang_list_init (lang_statement_list_type
*list
)
1030 list
->tail
= &list
->head
;
1034 lang_statement_append (lang_statement_list_type
*list
,
1038 *(list
->tail
) = element
;
1043 push_stat_ptr (lang_statement_list_type
*new_ptr
)
1045 if (stat_save_ptr
>= stat_save
+ sizeof (stat_save
) / sizeof (stat_save
[0]))
1047 *stat_save_ptr
++ = stat_ptr
;
1054 if (stat_save_ptr
<= stat_save
)
1056 stat_ptr
= *--stat_save_ptr
;
1059 /* Build a new statement node for the parse tree. */
1061 static lang_statement_union_type
*
1062 new_statement (enum statement_enum type
,
1064 lang_statement_list_type
*list
)
1066 lang_statement_union_type
*new_stmt
;
1068 new_stmt
= stat_alloc (size
);
1069 new_stmt
->header
.type
= type
;
1070 new_stmt
->header
.next
= NULL
;
1071 lang_statement_append (list
, new_stmt
, &new_stmt
->header
.next
);
1075 /* Build a new input file node for the language. There are several
1076 ways in which we treat an input file, eg, we only look at symbols,
1077 or prefix it with a -l etc.
1079 We can be supplied with requests for input files more than once;
1080 they may, for example be split over several lines like foo.o(.text)
1081 foo.o(.data) etc, so when asked for a file we check that we haven't
1082 got it already so we don't duplicate the bfd. */
1084 static lang_input_statement_type
*
1085 new_afile (const char *name
,
1086 lang_input_file_enum_type file_type
,
1088 const char *from_filename
)
1090 lang_input_statement_type
*p
;
1092 lang_has_input_file
= true;
1094 p
= new_stat (lang_input_statement
, stat_ptr
);
1095 memset (&p
->the_bfd
, 0,
1096 sizeof (*p
) - offsetof (lang_input_statement_type
, the_bfd
));
1097 p
->extra_search_path
= NULL
;
1099 p
->flags
.dynamic
= input_flags
.dynamic
;
1100 p
->flags
.add_DT_NEEDED_for_dynamic
= input_flags
.add_DT_NEEDED_for_dynamic
;
1101 p
->flags
.add_DT_NEEDED_for_regular
= input_flags
.add_DT_NEEDED_for_regular
;
1102 p
->flags
.whole_archive
= input_flags
.whole_archive
;
1103 p
->flags
.sysrooted
= input_flags
.sysrooted
;
1108 case lang_input_file_is_symbols_only_enum
:
1110 p
->local_sym_name
= name
;
1111 p
->flags
.real
= true;
1112 p
->flags
.just_syms
= true;
1114 case lang_input_file_is_fake_enum
:
1116 p
->local_sym_name
= name
;
1118 case lang_input_file_is_l_enum
:
1119 if (name
[0] == ':' && name
[1] != '\0')
1121 p
->filename
= name
+ 1;
1122 p
->flags
.full_name_provided
= true;
1126 p
->local_sym_name
= concat ("-l", name
, (const char *) NULL
);
1127 p
->flags
.maybe_archive
= true;
1128 p
->flags
.real
= true;
1129 p
->flags
.search_dirs
= true;
1131 case lang_input_file_is_marker_enum
:
1133 p
->local_sym_name
= name
;
1134 p
->flags
.search_dirs
= true;
1136 case lang_input_file_is_search_file_enum
:
1138 p
->local_sym_name
= name
;
1139 /* If name is a relative path, search the directory of the current linker
1141 if (from_filename
&& !IS_ABSOLUTE_PATH (name
))
1142 p
->extra_search_path
= ldirname (from_filename
);
1143 p
->flags
.real
= true;
1144 p
->flags
.search_dirs
= true;
1146 case lang_input_file_is_file_enum
:
1148 p
->local_sym_name
= name
;
1149 p
->flags
.real
= true;
1155 lang_statement_append (&input_file_chain
, p
, &p
->next_real_file
);
1159 lang_input_statement_type
*
1160 lang_add_input_file (const char *name
,
1161 lang_input_file_enum_type file_type
,
1165 && (*name
== '=' || startswith (name
, "$SYSROOT")))
1167 lang_input_statement_type
*ret
;
1168 char *sysrooted_name
1169 = concat (ld_sysroot
,
1170 name
+ (*name
== '=' ? 1 : strlen ("$SYSROOT")),
1171 (const char *) NULL
);
1173 /* We've now forcibly prepended the sysroot, making the input
1174 file independent of the context. Therefore, temporarily
1175 force a non-sysrooted context for this statement, so it won't
1176 get the sysroot prepended again when opened. (N.B. if it's a
1177 script, any child nodes with input files starting with "/"
1178 will be handled as "sysrooted" as they'll be found to be
1179 within the sysroot subdirectory.) */
1180 unsigned int outer_sysrooted
= input_flags
.sysrooted
;
1181 input_flags
.sysrooted
= 0;
1182 ret
= new_afile (sysrooted_name
, file_type
, target
, NULL
);
1183 input_flags
.sysrooted
= outer_sysrooted
;
1187 return new_afile (name
, file_type
, target
, current_input_file
);
1190 struct out_section_hash_entry
1192 struct bfd_hash_entry root
;
1193 lang_statement_union_type s
;
1196 /* The hash table. */
1198 static struct bfd_hash_table output_section_statement_table
;
1200 /* Support routines for the hash table used by lang_output_section_find,
1201 initialize the table, fill in an entry and remove the table. */
1203 static struct bfd_hash_entry
*
1204 output_section_statement_newfunc (struct bfd_hash_entry
*entry
,
1205 struct bfd_hash_table
*table
,
1208 lang_output_section_statement_type
**nextp
;
1209 struct out_section_hash_entry
*ret
;
1213 entry
= (struct bfd_hash_entry
*) bfd_hash_allocate (table
,
1219 entry
= bfd_hash_newfunc (entry
, table
, string
);
1223 ret
= (struct out_section_hash_entry
*) entry
;
1224 memset (&ret
->s
, 0, sizeof (ret
->s
));
1225 ret
->s
.header
.type
= lang_output_section_statement_enum
;
1226 ret
->s
.output_section_statement
.subsection_alignment
= NULL
;
1227 ret
->s
.output_section_statement
.section_alignment
= NULL
;
1228 ret
->s
.output_section_statement
.block_value
= 1;
1229 lang_list_init (&ret
->s
.output_section_statement
.children
);
1230 lang_statement_append (stat_ptr
, &ret
->s
, &ret
->s
.header
.next
);
1232 /* For every output section statement added to the list, except the
1233 first one, lang_os_list.tail points to the "next"
1234 field of the last element of the list. */
1235 if (lang_os_list
.head
!= NULL
)
1236 ret
->s
.output_section_statement
.prev
1237 = ((lang_output_section_statement_type
*)
1238 ((char *) lang_os_list
.tail
1239 - offsetof (lang_output_section_statement_type
, next
)));
1241 /* GCC's strict aliasing rules prevent us from just casting the
1242 address, so we store the pointer in a variable and cast that
1244 nextp
= &ret
->s
.output_section_statement
.next
;
1245 lang_statement_append (&lang_os_list
, &ret
->s
, nextp
);
1250 output_section_statement_table_init (void)
1252 if (!bfd_hash_table_init_n (&output_section_statement_table
,
1253 output_section_statement_newfunc
,
1254 sizeof (struct out_section_hash_entry
),
1256 einfo (_("%F%P: can not create hash table: %E\n"));
1260 output_section_statement_table_free (void)
1262 bfd_hash_table_free (&output_section_statement_table
);
1265 /* Build enough state so that the parser can build its tree. */
1270 obstack_begin (&stat_obstack
, 1000);
1271 obstack_init (&pt_obstack
);
1273 stat_ptr
= &statement_list
;
1275 output_section_statement_table_init ();
1277 lang_list_init (stat_ptr
);
1279 lang_list_init (&input_file_chain
);
1280 lang_list_init (&lang_os_list
);
1281 lang_list_init (&file_chain
);
1282 first_file
= lang_add_input_file (NULL
, lang_input_file_is_marker_enum
,
1284 abs_output_section
=
1285 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
, 0, 1);
1287 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
1289 asneeded_list_head
= NULL
;
1290 asneeded_list_tail
= &asneeded_list_head
;
1296 output_section_statement_table_free ();
1299 /*----------------------------------------------------------------------
1300 A region is an area of memory declared with the
1301 MEMORY { name:org=exp, len=exp ... }
1304 We maintain a list of all the regions here.
1306 If no regions are specified in the script, then the default is used
1307 which is created when looked up to be the entire data space.
1309 If create is true we are creating a region inside a MEMORY block.
1310 In this case it is probably an error to create a region that has
1311 already been created. If we are not inside a MEMORY block it is
1312 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
1313 and so we issue a warning.
1315 Each region has at least one name. The first name is either
1316 DEFAULT_MEMORY_REGION or the name given in the MEMORY block. You can add
1317 alias names to an existing region within a script with
1318 REGION_ALIAS (alias, region_name). Each name corresponds to at most one
1321 static lang_memory_region_type
*lang_memory_region_list
;
1322 static lang_memory_region_type
**lang_memory_region_list_tail
1323 = &lang_memory_region_list
;
1325 lang_memory_region_type
*
1326 lang_memory_region_lookup (const char *const name
, bool create
)
1328 lang_memory_region_name
*n
;
1329 lang_memory_region_type
*r
;
1330 lang_memory_region_type
*new_region
;
1332 /* NAME is NULL for LMA memspecs if no region was specified. */
1336 for (r
= lang_memory_region_list
; r
!= NULL
; r
= r
->next
)
1337 for (n
= &r
->name_list
; n
!= NULL
; n
= n
->next
)
1338 if (strcmp (n
->name
, name
) == 0)
1341 einfo (_("%P:%pS: warning: redeclaration of memory region `%s'\n"),
1346 if (!create
&& strcmp (name
, DEFAULT_MEMORY_REGION
))
1347 einfo (_("%P:%pS: warning: memory region `%s' not declared\n"),
1350 new_region
= stat_alloc (sizeof (lang_memory_region_type
));
1352 new_region
->name_list
.name
= xstrdup (name
);
1353 new_region
->name_list
.next
= NULL
;
1354 new_region
->next
= NULL
;
1355 new_region
->origin_exp
= NULL
;
1356 new_region
->origin
= 0;
1357 new_region
->length_exp
= NULL
;
1358 new_region
->length
= ~(bfd_size_type
) 0;
1359 new_region
->current
= 0;
1360 new_region
->last_os
= NULL
;
1361 new_region
->flags
= 0;
1362 new_region
->not_flags
= 0;
1363 new_region
->had_full_message
= false;
1365 *lang_memory_region_list_tail
= new_region
;
1366 lang_memory_region_list_tail
= &new_region
->next
;
1372 lang_memory_region_alias (const char *alias
, const char *region_name
)
1374 lang_memory_region_name
*n
;
1375 lang_memory_region_type
*r
;
1376 lang_memory_region_type
*region
;
1378 /* The default region must be unique. This ensures that it is not necessary
1379 to iterate through the name list if someone wants the check if a region is
1380 the default memory region. */
1381 if (strcmp (region_name
, DEFAULT_MEMORY_REGION
) == 0
1382 || strcmp (alias
, DEFAULT_MEMORY_REGION
) == 0)
1383 einfo (_("%F%P:%pS: error: alias for default memory region\n"), NULL
);
1385 /* Look for the target region and check if the alias is not already
1388 for (r
= lang_memory_region_list
; r
!= NULL
; r
= r
->next
)
1389 for (n
= &r
->name_list
; n
!= NULL
; n
= n
->next
)
1391 if (region
== NULL
&& strcmp (n
->name
, region_name
) == 0)
1393 if (strcmp (n
->name
, alias
) == 0)
1394 einfo (_("%F%P:%pS: error: redefinition of memory region "
1399 /* Check if the target region exists. */
1401 einfo (_("%F%P:%pS: error: memory region `%s' "
1402 "for alias `%s' does not exist\n"),
1403 NULL
, region_name
, alias
);
1405 /* Add alias to region name list. */
1406 n
= stat_alloc (sizeof (lang_memory_region_name
));
1407 n
->name
= xstrdup (alias
);
1408 n
->next
= region
->name_list
.next
;
1409 region
->name_list
.next
= n
;
1412 static lang_memory_region_type
*
1413 lang_memory_default (asection
*section
)
1415 lang_memory_region_type
*p
;
1417 flagword sec_flags
= section
->flags
;
1419 /* Override SEC_DATA to mean a writable section. */
1420 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
1421 sec_flags
|= SEC_DATA
;
1423 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
1425 if ((p
->flags
& sec_flags
) != 0
1426 && (p
->not_flags
& sec_flags
) == 0)
1431 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, false);
1434 /* Get the output section statement directly from the userdata. */
1436 lang_output_section_statement_type
*
1437 lang_output_section_get (const asection
*output_section
)
1439 return bfd_section_userdata (output_section
);
1442 /* Find or create an output_section_statement with the given NAME.
1443 If CONSTRAINT is non-zero match one with that constraint, otherwise
1444 match any non-negative constraint. If CREATE is 0 return NULL when
1445 no match exists. If CREATE is 1, create an output_section_statement
1446 when no match exists or if CONSTRAINT is SPECIAL. If CREATE is 2,
1447 always make a new output_section_statement. */
1449 lang_output_section_statement_type
*
1450 lang_output_section_statement_lookup (const char *name
,
1454 struct out_section_hash_entry
*entry
;
1456 entry
= ((struct out_section_hash_entry
*)
1457 bfd_hash_lookup (&output_section_statement_table
, name
,
1458 create
!= 0, false));
1462 einfo (_("%F%P: failed creating section `%s': %E\n"), name
);
1466 if (entry
->s
.output_section_statement
.name
!= NULL
)
1468 /* We have a section of this name, but it might not have the correct
1470 struct out_section_hash_entry
*last_ent
;
1472 name
= entry
->s
.output_section_statement
.name
;
1476 && !(create
&& constraint
== SPECIAL
)
1477 && (constraint
== entry
->s
.output_section_statement
.constraint
1479 && entry
->s
.output_section_statement
.constraint
>= 0)))
1480 return &entry
->s
.output_section_statement
;
1482 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1484 while (entry
!= NULL
1485 && name
== entry
->s
.output_section_statement
.name
);
1491 = ((struct out_section_hash_entry
*)
1492 output_section_statement_newfunc (NULL
,
1493 &output_section_statement_table
,
1497 einfo (_("%F%P: failed creating section `%s': %E\n"), name
);
1500 entry
->root
= last_ent
->root
;
1501 last_ent
->root
.next
= &entry
->root
;
1504 entry
->s
.output_section_statement
.name
= name
;
1505 entry
->s
.output_section_statement
.constraint
= constraint
;
1506 entry
->s
.output_section_statement
.dup_output
= (create
== 2
1507 || constraint
== SPECIAL
);
1508 return &entry
->s
.output_section_statement
;
1511 /* Find the next output_section_statement with the same name as OS.
1512 If CONSTRAINT is non-zero, find one with that constraint otherwise
1513 match any non-negative constraint. */
1515 lang_output_section_statement_type
*
1516 next_matching_output_section_statement (lang_output_section_statement_type
*os
,
1519 /* All output_section_statements are actually part of a
1520 struct out_section_hash_entry. */
1521 struct out_section_hash_entry
*entry
= (struct out_section_hash_entry
*)
1523 - offsetof (struct out_section_hash_entry
, s
.output_section_statement
));
1524 const char *name
= os
->name
;
1526 ASSERT (name
== entry
->root
.string
);
1529 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1531 || name
!= entry
->s
.output_section_statement
.name
)
1534 while (constraint
!= entry
->s
.output_section_statement
.constraint
1536 || entry
->s
.output_section_statement
.constraint
< 0));
1538 return &entry
->s
.output_section_statement
;
1541 /* A variant of lang_output_section_find used by place_orphan.
1542 Returns the output statement that should precede a new output
1543 statement for SEC. If an exact match is found on certain flags,
1546 lang_output_section_statement_type
*
1547 lang_output_section_find_by_flags (const asection
*sec
,
1549 lang_output_section_statement_type
**exact
,
1550 lang_match_sec_type_func match_type
)
1552 lang_output_section_statement_type
*first
, *look
, *found
;
1553 flagword look_flags
, differ
;
1555 /* We know the first statement on this list is *ABS*. May as well
1557 first
= (void *) lang_os_list
.head
;
1558 first
= first
->next
;
1560 /* First try for an exact match. */
1562 for (look
= first
; look
; look
= look
->next
)
1564 look_flags
= look
->flags
;
1565 if (look
->bfd_section
!= NULL
)
1567 look_flags
= look
->bfd_section
->flags
;
1568 if (match_type
&& !match_type (link_info
.output_bfd
,
1573 differ
= look_flags
^ sec_flags
;
1574 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
1575 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1585 if ((sec_flags
& SEC_CODE
) != 0
1586 && (sec_flags
& SEC_ALLOC
) != 0)
1588 /* Try for a rw code section. */
1589 for (look
= first
; look
; look
= look
->next
)
1591 look_flags
= look
->flags
;
1592 if (look
->bfd_section
!= NULL
)
1594 look_flags
= look
->bfd_section
->flags
;
1595 if (match_type
&& !match_type (link_info
.output_bfd
,
1600 differ
= look_flags
^ sec_flags
;
1601 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1602 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1606 else if ((sec_flags
& SEC_READONLY
) != 0
1607 && (sec_flags
& SEC_ALLOC
) != 0)
1609 /* .rodata can go after .text, .sdata2 after .rodata. */
1610 for (look
= first
; look
; look
= look
->next
)
1612 look_flags
= look
->flags
;
1613 if (look
->bfd_section
!= NULL
)
1615 look_flags
= look
->bfd_section
->flags
;
1616 if (match_type
&& !match_type (link_info
.output_bfd
,
1621 differ
= look_flags
^ sec_flags
;
1622 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1623 | SEC_READONLY
| SEC_SMALL_DATA
))
1624 || (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1626 && !(look_flags
& SEC_SMALL_DATA
)))
1630 else if ((sec_flags
& SEC_THREAD_LOCAL
) != 0
1631 && (sec_flags
& SEC_ALLOC
) != 0)
1633 /* .tdata can go after .data, .tbss after .tdata. Treat .tbss
1634 as if it were a loaded section, and don't use match_type. */
1635 bool seen_thread_local
= false;
1638 for (look
= first
; look
; look
= look
->next
)
1640 look_flags
= look
->flags
;
1641 if (look
->bfd_section
!= NULL
)
1642 look_flags
= look
->bfd_section
->flags
;
1644 differ
= look_flags
^ (sec_flags
| SEC_LOAD
| SEC_HAS_CONTENTS
);
1645 if (!(differ
& (SEC_THREAD_LOCAL
| SEC_ALLOC
)))
1647 /* .tdata and .tbss must be adjacent and in that order. */
1648 if (!(look_flags
& SEC_LOAD
)
1649 && (sec_flags
& SEC_LOAD
))
1650 /* ..so if we're at a .tbss section and we're placing
1651 a .tdata section stop looking and return the
1652 previous section. */
1655 seen_thread_local
= true;
1657 else if (seen_thread_local
)
1659 else if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
)))
1663 else if ((sec_flags
& SEC_SMALL_DATA
) != 0
1664 && (sec_flags
& SEC_ALLOC
) != 0)
1666 /* .sdata goes after .data, .sbss after .sdata. */
1667 for (look
= first
; look
; look
= look
->next
)
1669 look_flags
= look
->flags
;
1670 if (look
->bfd_section
!= NULL
)
1672 look_flags
= look
->bfd_section
->flags
;
1673 if (match_type
&& !match_type (link_info
.output_bfd
,
1678 differ
= look_flags
^ sec_flags
;
1679 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1680 | SEC_THREAD_LOCAL
))
1681 || ((look_flags
& SEC_SMALL_DATA
)
1682 && !(sec_flags
& SEC_HAS_CONTENTS
)))
1686 else if ((sec_flags
& SEC_HAS_CONTENTS
) != 0
1687 && (sec_flags
& SEC_ALLOC
) != 0)
1689 /* .data goes after .rodata. */
1690 for (look
= first
; look
; look
= look
->next
)
1692 look_flags
= look
->flags
;
1693 if (look
->bfd_section
!= NULL
)
1695 look_flags
= look
->bfd_section
->flags
;
1696 if (match_type
&& !match_type (link_info
.output_bfd
,
1701 differ
= look_flags
^ sec_flags
;
1702 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1703 | SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1707 else if ((sec_flags
& SEC_ALLOC
) != 0)
1709 /* .bss goes after any other alloc section. */
1710 for (look
= first
; look
; look
= look
->next
)
1712 look_flags
= look
->flags
;
1713 if (look
->bfd_section
!= NULL
)
1715 look_flags
= look
->bfd_section
->flags
;
1716 if (match_type
&& !match_type (link_info
.output_bfd
,
1721 differ
= look_flags
^ sec_flags
;
1722 if (!(differ
& SEC_ALLOC
))
1728 /* non-alloc go last. */
1729 for (look
= first
; look
; look
= look
->next
)
1731 look_flags
= look
->flags
;
1732 if (look
->bfd_section
!= NULL
)
1733 look_flags
= look
->bfd_section
->flags
;
1734 differ
= look_flags
^ sec_flags
;
1735 if (!(differ
& SEC_DEBUGGING
))
1741 if (found
|| !match_type
)
1744 return lang_output_section_find_by_flags (sec
, sec_flags
, NULL
, NULL
);
1747 /* Find the last output section before given output statement.
1748 Used by place_orphan. */
1751 output_prev_sec_find (lang_output_section_statement_type
*os
)
1753 lang_output_section_statement_type
*lookup
;
1755 for (lookup
= os
->prev
; lookup
!= NULL
; lookup
= lookup
->prev
)
1757 if (lookup
->constraint
< 0)
1760 if (lookup
->bfd_section
!= NULL
&& lookup
->bfd_section
->owner
!= NULL
)
1761 return lookup
->bfd_section
;
1767 /* Look for a suitable place for a new output section statement. The
1768 idea is to skip over anything that might be inside a SECTIONS {}
1769 statement in a script, before we find another output section
1770 statement. Assignments to "dot" before an output section statement
1771 are assumed to belong to it, except in two cases; The first
1772 assignment to dot, and assignments before non-alloc sections.
1773 Otherwise we might put an orphan before . = . + SIZEOF_HEADERS or
1774 similar assignments that set the initial address, or we might
1775 insert non-alloc note sections among assignments setting end of
1778 static lang_statement_union_type
**
1779 insert_os_after (lang_output_section_statement_type
*after
)
1781 lang_statement_union_type
**where
;
1782 lang_statement_union_type
**assign
= NULL
;
1785 ignore_first
= after
== (void *) lang_os_list
.head
;
1787 for (where
= &after
->header
.next
;
1789 where
= &(*where
)->header
.next
)
1791 switch ((*where
)->header
.type
)
1793 case lang_assignment_statement_enum
:
1796 lang_assignment_statement_type
*ass
;
1798 ass
= &(*where
)->assignment_statement
;
1799 if (ass
->exp
->type
.node_class
!= etree_assert
1800 && ass
->exp
->assign
.dst
[0] == '.'
1801 && ass
->exp
->assign
.dst
[1] == 0)
1805 ignore_first
= false;
1809 case lang_wild_statement_enum
:
1810 case lang_input_section_enum
:
1811 case lang_object_symbols_statement_enum
:
1812 case lang_fill_statement_enum
:
1813 case lang_data_statement_enum
:
1814 case lang_reloc_statement_enum
:
1815 case lang_padding_statement_enum
:
1816 case lang_constructors_statement_enum
:
1818 ignore_first
= false;
1820 case lang_output_section_statement_enum
:
1823 asection
*s
= (*where
)->output_section_statement
.bfd_section
;
1826 || s
->map_head
.s
== NULL
1827 || (s
->flags
& SEC_ALLOC
) != 0)
1831 case lang_input_statement_enum
:
1832 case lang_address_statement_enum
:
1833 case lang_target_statement_enum
:
1834 case lang_output_statement_enum
:
1835 case lang_group_statement_enum
:
1836 case lang_insert_statement_enum
:
1838 case lang_input_matcher_enum
:
1847 lang_output_section_statement_type
*
1848 lang_insert_orphan (asection
*s
,
1849 const char *secname
,
1851 lang_output_section_statement_type
*after
,
1852 struct orphan_save
*place
,
1853 etree_type
*address
,
1854 lang_statement_list_type
*add_child
)
1856 lang_statement_list_type add
;
1857 lang_output_section_statement_type
*os
;
1858 lang_output_section_statement_type
**os_tail
;
1860 /* If we have found an appropriate place for the output section
1861 statements for this orphan, add them to our own private list,
1862 inserting them later into the global statement list. */
1865 lang_list_init (&add
);
1866 push_stat_ptr (&add
);
1869 if (bfd_link_relocatable (&link_info
)
1870 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0)
1871 address
= exp_intop (0);
1873 os_tail
= (lang_output_section_statement_type
**) lang_os_list
.tail
;
1874 os
= lang_enter_output_section_statement (
1875 secname
, address
, normal_section
, 0, NULL
, NULL
, NULL
, constraint
, 0);
1877 if (add_child
== NULL
)
1878 add_child
= &os
->children
;
1879 lang_add_section (add_child
, s
, NULL
, NULL
, os
);
1881 if (after
&& (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) != 0)
1883 const char *region
= (after
->region
1884 ? after
->region
->name_list
.name
1885 : DEFAULT_MEMORY_REGION
);
1886 const char *lma_region
= (after
->lma_region
1887 ? after
->lma_region
->name_list
.name
1889 lang_leave_output_section_statement (NULL
, region
, after
->phdrs
,
1893 lang_leave_output_section_statement (NULL
, DEFAULT_MEMORY_REGION
, NULL
,
1896 /* Restore the global list pointer. */
1900 if (after
!= NULL
&& os
->bfd_section
!= NULL
)
1902 asection
*snew
, *as
;
1903 bool place_after
= place
->stmt
== NULL
;
1904 bool insert_after
= true;
1906 snew
= os
->bfd_section
;
1908 /* Shuffle the bfd section list to make the output file look
1909 neater. This is really only cosmetic. */
1910 if (place
->section
== NULL
1911 && after
!= (void *) lang_os_list
.head
)
1913 asection
*bfd_section
= after
->bfd_section
;
1915 /* If the output statement hasn't been used to place any input
1916 sections (and thus doesn't have an output bfd_section),
1917 look for the closest prior output statement having an
1919 if (bfd_section
== NULL
)
1920 bfd_section
= output_prev_sec_find (after
);
1922 if (bfd_section
!= NULL
&& bfd_section
!= snew
)
1923 place
->section
= &bfd_section
->next
;
1926 if (place
->section
== NULL
)
1927 place
->section
= &link_info
.output_bfd
->sections
;
1929 as
= *place
->section
;
1933 /* Put the section at the end of the list. */
1935 /* Unlink the section. */
1936 bfd_section_list_remove (link_info
.output_bfd
, snew
);
1938 /* Now tack it back on in the right place. */
1939 bfd_section_list_append (link_info
.output_bfd
, snew
);
1941 else if ((bfd_get_flavour (link_info
.output_bfd
)
1942 == bfd_target_elf_flavour
)
1943 && (bfd_get_flavour (s
->owner
)
1944 == bfd_target_elf_flavour
)
1945 && ((elf_section_type (s
) == SHT_NOTE
1946 && (s
->flags
& SEC_LOAD
) != 0)
1947 || (elf_section_type (as
) == SHT_NOTE
1948 && (as
->flags
& SEC_LOAD
) != 0)))
1950 /* Make sure that output note sections are grouped and sorted
1951 by alignments when inserting a note section or insert a
1952 section after a note section, */
1954 /* A specific section after which the output note section
1955 should be placed. */
1956 asection
*after_sec
;
1957 /* True if we need to insert the orphan section after a
1958 specific section to maintain output note section order. */
1959 bool after_sec_note
= false;
1961 static asection
*first_orphan_note
= NULL
;
1963 /* Group and sort output note section by alignments in
1966 if (elf_section_type (s
) == SHT_NOTE
1967 && (s
->flags
& SEC_LOAD
) != 0)
1969 /* Search from the beginning for the last output note
1970 section with equal or larger alignments. NB: Don't
1971 place orphan note section after non-note sections. */
1973 first_orphan_note
= NULL
;
1974 for (sec
= link_info
.output_bfd
->sections
;
1976 && !bfd_is_abs_section (sec
));
1979 && elf_section_type (sec
) == SHT_NOTE
1980 && (sec
->flags
& SEC_LOAD
) != 0)
1982 if (!first_orphan_note
)
1983 first_orphan_note
= sec
;
1984 if (sec
->alignment_power
>= s
->alignment_power
)
1987 else if (first_orphan_note
)
1989 /* Stop if there is non-note section after the first
1990 orphan note section. */
1994 /* If this will be the first orphan note section, it can
1995 be placed at the default location. */
1996 after_sec_note
= first_orphan_note
!= NULL
;
1997 if (after_sec
== NULL
&& after_sec_note
)
1999 /* If all output note sections have smaller
2000 alignments, place the section before all
2001 output orphan note sections. */
2002 after_sec
= first_orphan_note
;
2003 insert_after
= false;
2006 else if (first_orphan_note
)
2008 /* Don't place non-note sections in the middle of orphan
2010 after_sec_note
= true;
2012 for (sec
= as
->next
;
2014 && !bfd_is_abs_section (sec
));
2016 if (elf_section_type (sec
) == SHT_NOTE
2017 && (sec
->flags
& SEC_LOAD
) != 0)
2025 /* Search forward to insert OS after AFTER_SEC output
2027 lang_output_section_statement_type
*stmt
, *next
;
2029 for (stmt
= after
; stmt
!= NULL
; stmt
= next
)
2034 if (stmt
->bfd_section
== after_sec
)
2044 /* If INSERT_AFTER is FALSE, place OS before
2045 AFTER_SEC output statement. */
2046 if (next
&& next
->bfd_section
== after_sec
)
2056 /* Search backward to insert OS after AFTER_SEC output
2059 for (stmt
= after
; stmt
!= NULL
; stmt
= stmt
->prev
)
2063 if (stmt
->bfd_section
== after_sec
)
2072 /* If INSERT_AFTER is FALSE, place OS before
2073 AFTER_SEC output statement. */
2074 if (stmt
->next
->bfd_section
== after_sec
)
2084 if (after_sec
== NULL
2085 || (insert_after
&& after_sec
->next
!= snew
)
2086 || (!insert_after
&& after_sec
->prev
!= snew
))
2088 /* Unlink the section. */
2089 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2091 /* Place SNEW after AFTER_SEC. If AFTER_SEC is NULL,
2096 bfd_section_list_insert_after (link_info
.output_bfd
,
2099 bfd_section_list_insert_before (link_info
.output_bfd
,
2103 bfd_section_list_prepend (link_info
.output_bfd
, snew
);
2106 else if (as
!= snew
&& as
->prev
!= snew
)
2108 /* Unlink the section. */
2109 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2111 /* Now tack it back on in the right place. */
2112 bfd_section_list_insert_before (link_info
.output_bfd
,
2116 else if (as
!= snew
&& as
->prev
!= snew
)
2118 /* Unlink the section. */
2119 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2121 /* Now tack it back on in the right place. */
2122 bfd_section_list_insert_before (link_info
.output_bfd
, as
, snew
);
2125 /* Save the end of this list. Further ophans of this type will
2126 follow the one we've just added. */
2127 place
->section
= &snew
->next
;
2129 /* The following is non-cosmetic. We try to put the output
2130 statements in some sort of reasonable order here, because they
2131 determine the final load addresses of the orphan sections.
2132 In addition, placing output statements in the wrong order may
2133 require extra segments. For instance, given a typical
2134 situation of all read-only sections placed in one segment and
2135 following that a segment containing all the read-write
2136 sections, we wouldn't want to place an orphan read/write
2137 section before or amongst the read-only ones. */
2138 if (add
.head
!= NULL
)
2140 lang_output_section_statement_type
*newly_added_os
;
2142 /* Place OS after AFTER if AFTER_NOTE is TRUE. */
2145 lang_statement_union_type
**where
= insert_os_after (after
);
2150 place
->os_tail
= &after
->next
;
2154 /* Put it after the last orphan statement we added. */
2155 *add
.tail
= *place
->stmt
;
2156 *place
->stmt
= add
.head
;
2159 /* Fix the global list pointer if we happened to tack our
2160 new list at the tail. */
2161 if (*stat_ptr
->tail
== add
.head
)
2162 stat_ptr
->tail
= add
.tail
;
2164 /* Save the end of this list. */
2165 place
->stmt
= add
.tail
;
2167 /* Do the same for the list of output section statements. */
2168 newly_added_os
= *os_tail
;
2170 newly_added_os
->prev
= (lang_output_section_statement_type
*)
2171 ((char *) place
->os_tail
2172 - offsetof (lang_output_section_statement_type
, next
));
2173 newly_added_os
->next
= *place
->os_tail
;
2174 if (newly_added_os
->next
!= NULL
)
2175 newly_added_os
->next
->prev
= newly_added_os
;
2176 *place
->os_tail
= newly_added_os
;
2177 place
->os_tail
= &newly_added_os
->next
;
2179 /* Fixing the global list pointer here is a little different.
2180 We added to the list in lang_enter_output_section_statement,
2181 trimmed off the new output_section_statment above when
2182 assigning *os_tail = NULL, but possibly added it back in
2183 the same place when assigning *place->os_tail. */
2184 if (*os_tail
== NULL
)
2185 lang_os_list
.tail
= (lang_statement_union_type
**) os_tail
;
2192 lang_print_asneeded (void)
2194 struct asneeded_minfo
*m
;
2196 if (asneeded_list_head
== NULL
)
2199 minfo (_("\nAs-needed library included to satisfy reference by file (symbol)\n\n"));
2201 for (m
= asneeded_list_head
; m
!= NULL
; m
= m
->next
)
2205 minfo ("%s", m
->soname
);
2206 len
= strlen (m
->soname
);
2213 print_spaces (30 - len
);
2216 minfo ("%pB ", m
->ref
);
2217 minfo ("(%pT)\n", m
->name
);
2222 lang_map_flags (flagword flag
)
2224 if (flag
& SEC_ALLOC
)
2227 if (flag
& SEC_CODE
)
2230 if (flag
& SEC_READONLY
)
2233 if (flag
& SEC_DATA
)
2236 if (flag
& SEC_LOAD
)
2243 lang_memory_region_type
*m
;
2244 bool dis_header_printed
= false;
2246 LANG_FOR_EACH_INPUT_STATEMENT (file
)
2250 if ((file
->the_bfd
->flags
& (BFD_LINKER_CREATED
| DYNAMIC
)) != 0
2251 || file
->flags
.just_syms
)
2254 if (config
.print_map_discarded
)
2255 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
2256 if ((s
->output_section
== NULL
2257 || s
->output_section
->owner
!= link_info
.output_bfd
)
2258 && (s
->flags
& (SEC_LINKER_CREATED
| SEC_KEEP
)) == 0)
2260 if (! dis_header_printed
)
2262 fprintf (config
.map_file
, _("\nDiscarded input sections\n\n"));
2263 dis_header_printed
= true;
2266 print_input_section (s
, true);
2270 minfo (_("\nMemory Configuration\n\n"));
2271 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
2272 _("Name"), _("Origin"), _("Length"), _("Attributes"));
2274 for (m
= lang_memory_region_list
; m
!= NULL
; m
= m
->next
)
2276 fprintf (config
.map_file
, "%-16s", m
->name_list
.name
);
2279 bfd_sprintf_vma (link_info
.output_bfd
, buf
, m
->origin
);
2280 fprintf (config
.map_file
, " 0x%-16s", buf
);
2281 bfd_sprintf_vma (link_info
.output_bfd
, buf
, m
->length
);
2282 fprintf (config
.map_file
,
2283 " 0x%*s", m
->flags
|| m
->not_flags
? -17 : 0, buf
);
2285 lang_map_flags (m
->flags
);
2290 lang_map_flags (m
->not_flags
);
2296 fprintf (config
.map_file
, _("\nLinker script and memory map\n\n"));
2298 if (!link_info
.reduce_memory_overheads
)
2300 obstack_begin (&map_obstack
, 1000);
2301 bfd_link_hash_traverse (link_info
.hash
, sort_def_symbol
, 0);
2303 expld
.phase
= lang_fixed_phase_enum
;
2304 lang_statement_iteration
++;
2305 print_statements ();
2307 ldemul_extra_map_file_text (link_info
.output_bfd
, &link_info
,
2312 sort_def_symbol (struct bfd_link_hash_entry
*hash_entry
,
2313 void *info ATTRIBUTE_UNUSED
)
2315 if ((hash_entry
->type
== bfd_link_hash_defined
2316 || hash_entry
->type
== bfd_link_hash_defweak
)
2317 && hash_entry
->u
.def
.section
->owner
!= link_info
.output_bfd
2318 && hash_entry
->u
.def
.section
->owner
!= NULL
)
2320 input_section_userdata_type
*ud
;
2321 struct map_symbol_def
*def
;
2323 ud
= bfd_section_userdata (hash_entry
->u
.def
.section
);
2326 ud
= stat_alloc (sizeof (*ud
));
2327 bfd_set_section_userdata (hash_entry
->u
.def
.section
, ud
);
2328 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
2329 ud
->map_symbol_def_count
= 0;
2331 else if (!ud
->map_symbol_def_tail
)
2332 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
2334 def
= (struct map_symbol_def
*) obstack_alloc (&map_obstack
, sizeof *def
);
2335 def
->entry
= hash_entry
;
2336 *(ud
->map_symbol_def_tail
) = def
;
2337 ud
->map_symbol_def_tail
= &def
->next
;
2338 ud
->map_symbol_def_count
++;
2343 /* Initialize an output section. */
2346 init_os (lang_output_section_statement_type
*s
, flagword flags
)
2348 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
2349 einfo (_("%F%P: illegal use of `%s' section\n"), DISCARD_SECTION_NAME
);
2352 s
->bfd_section
= bfd_get_section_by_name (link_info
.output_bfd
, s
->name
);
2353 if (s
->bfd_section
== NULL
)
2354 s
->bfd_section
= bfd_make_section_anyway_with_flags (link_info
.output_bfd
,
2356 if (s
->bfd_section
== NULL
)
2358 einfo (_("%F%P: output format %s cannot represent section"
2359 " called %s: %E\n"),
2360 link_info
.output_bfd
->xvec
->name
, s
->name
);
2362 s
->bfd_section
->output_section
= s
->bfd_section
;
2363 s
->bfd_section
->output_offset
= 0;
2365 /* Set the userdata of the output section to the output section
2366 statement to avoid lookup. */
2367 bfd_set_section_userdata (s
->bfd_section
, s
);
2369 /* If there is a base address, make sure that any sections it might
2370 mention are initialized. */
2371 if (s
->addr_tree
!= NULL
)
2372 exp_init_os (s
->addr_tree
);
2374 if (s
->load_base
!= NULL
)
2375 exp_init_os (s
->load_base
);
2377 /* If supplied an alignment, set it. */
2378 if (s
->section_alignment
!= NULL
)
2379 s
->bfd_section
->alignment_power
= exp_get_power (s
->section_alignment
,
2380 "section alignment");
2383 /* Make sure that all output sections mentioned in an expression are
2387 exp_init_os (etree_type
*exp
)
2389 switch (exp
->type
.node_class
)
2393 case etree_provided
:
2394 exp_init_os (exp
->assign
.src
);
2398 exp_init_os (exp
->binary
.lhs
);
2399 exp_init_os (exp
->binary
.rhs
);
2403 exp_init_os (exp
->trinary
.cond
);
2404 exp_init_os (exp
->trinary
.lhs
);
2405 exp_init_os (exp
->trinary
.rhs
);
2409 exp_init_os (exp
->assert_s
.child
);
2413 exp_init_os (exp
->unary
.child
);
2417 switch (exp
->type
.node_code
)
2422 lang_output_section_statement_type
*os
;
2424 os
= lang_output_section_find (exp
->name
.name
);
2425 if (os
!= NULL
&& os
->bfd_section
== NULL
)
2437 section_already_linked (bfd
*abfd
, asection
*sec
, void *data
)
2439 lang_input_statement_type
*entry
= (lang_input_statement_type
*) data
;
2441 /* If we are only reading symbols from this object, then we want to
2442 discard all sections. */
2443 if (entry
->flags
.just_syms
)
2445 bfd_link_just_syms (abfd
, sec
, &link_info
);
2449 /* Deal with SHF_EXCLUDE ELF sections. */
2450 if (!bfd_link_relocatable (&link_info
)
2451 && (abfd
->flags
& BFD_PLUGIN
) == 0
2452 && (sec
->flags
& (SEC_GROUP
| SEC_KEEP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
2453 sec
->output_section
= bfd_abs_section_ptr
;
2455 if (!(abfd
->flags
& DYNAMIC
))
2456 bfd_section_already_linked (abfd
, sec
, &link_info
);
2460 /* Returns true if SECTION is one we know will be discarded based on its
2461 section flags, otherwise returns false. */
2464 lang_discard_section_p (asection
*section
)
2467 flagword flags
= section
->flags
;
2469 /* Discard sections marked with SEC_EXCLUDE. */
2470 discard
= (flags
& SEC_EXCLUDE
) != 0;
2472 /* Discard the group descriptor sections when we're finally placing the
2473 sections from within the group. */
2474 if ((flags
& SEC_GROUP
) != 0
2475 && link_info
.resolve_section_groups
)
2478 /* Discard debugging sections if we are stripping debugging
2480 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
2481 && (flags
& SEC_DEBUGGING
) != 0)
2487 /* The wild routines.
2489 These expand statements like *(.text) and foo.o to a list of
2490 explicit actions, like foo.o(.text), bar.o(.text) and
2491 foo.o(.text, .data). */
2493 /* Add SECTION to the output section OUTPUT. Do this by creating a
2494 lang_input_section statement which is placed at PTR. */
2497 lang_add_section (lang_statement_list_type
*ptr
,
2499 struct wildcard_list
*pattern
,
2500 struct flag_info
*sflag_info
,
2501 lang_output_section_statement_type
*output
)
2503 flagword flags
= section
->flags
;
2506 lang_input_section_type
*new_section
;
2507 bfd
*abfd
= link_info
.output_bfd
;
2509 /* Is this section one we know should be discarded? */
2510 discard
= lang_discard_section_p (section
);
2512 /* Discard input sections which are assigned to a section named
2513 DISCARD_SECTION_NAME. */
2514 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
2519 if (section
->output_section
== NULL
)
2521 /* This prevents future calls from assigning this section. */
2522 section
->output_section
= bfd_abs_section_ptr
;
2524 else if (link_info
.non_contiguous_regions_warnings
)
2525 einfo (_("%P:%pS: warning: --enable-non-contiguous-regions makes "
2526 "section `%pA' from '%pB' match /DISCARD/ clause.\n"),
2527 NULL
, section
, section
->owner
);
2536 keep
= bfd_lookup_section_flags (&link_info
, sflag_info
, section
);
2541 if (section
->output_section
!= NULL
)
2543 if (!link_info
.non_contiguous_regions
)
2546 /* SECTION has already been handled in a special way
2547 (eg. LINK_ONCE): skip it. */
2548 if (bfd_is_abs_section (section
->output_section
))
2551 /* Already assigned to the same output section, do not process
2552 it again, to avoid creating loops between duplicate sections
2554 if (section
->output_section
== output
->bfd_section
)
2557 if (link_info
.non_contiguous_regions_warnings
&& output
->bfd_section
)
2558 einfo (_("%P:%pS: warning: --enable-non-contiguous-regions may "
2559 "change behaviour for section `%pA' from '%pB' (assigned to "
2560 "%pA, but additional match: %pA)\n"),
2561 NULL
, section
, section
->owner
, section
->output_section
,
2562 output
->bfd_section
);
2564 /* SECTION has already been assigned to an output section, but
2565 the user allows it to be mapped to another one in case it
2566 overflows. We'll later update the actual output section in
2567 size_input_section as appropriate. */
2570 /* We don't copy the SEC_NEVER_LOAD flag from an input section
2571 to an output section, because we want to be able to include a
2572 SEC_NEVER_LOAD section in the middle of an otherwise loaded
2573 section (I don't know why we want to do this, but we do).
2574 build_link_order in ldwrite.c handles this case by turning
2575 the embedded SEC_NEVER_LOAD section into a fill. */
2576 flags
&= ~ SEC_NEVER_LOAD
;
2578 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
2579 already been processed. One reason to do this is that on pe
2580 format targets, .text$foo sections go into .text and it's odd
2581 to see .text with SEC_LINK_ONCE set. */
2582 if ((flags
& (SEC_LINK_ONCE
| SEC_GROUP
)) == (SEC_LINK_ONCE
| SEC_GROUP
))
2584 if (link_info
.resolve_section_groups
)
2585 flags
&= ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
);
2587 flags
&= ~(SEC_LINK_DUPLICATES
| SEC_RELOC
);
2589 else if (!bfd_link_relocatable (&link_info
))
2590 flags
&= ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
);
2592 switch (output
->sectype
)
2594 case normal_section
:
2595 case overlay_section
:
2596 case first_overlay_section
:
2599 case noalloc_section
:
2600 flags
&= ~SEC_ALLOC
;
2602 case typed_readonly_section
:
2603 case readonly_section
:
2604 flags
|= SEC_READONLY
;
2606 case noload_section
:
2608 flags
|= SEC_NEVER_LOAD
;
2609 /* Unfortunately GNU ld has managed to evolve two different
2610 meanings to NOLOAD in scripts. ELF gets a .bss style noload,
2611 alloc, no contents section. All others get a noload, noalloc
2613 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
)
2614 flags
&= ~SEC_HAS_CONTENTS
;
2616 flags
&= ~SEC_ALLOC
;
2620 if (output
->bfd_section
== NULL
)
2621 init_os (output
, flags
);
2623 /* If SEC_READONLY is not set in the input section, then clear
2624 it from the output section. */
2625 output
->bfd_section
->flags
&= flags
| ~SEC_READONLY
;
2627 if (output
->bfd_section
->linker_has_input
)
2629 /* Only set SEC_READONLY flag on the first input section. */
2630 flags
&= ~ SEC_READONLY
;
2632 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
2633 if ((output
->bfd_section
->flags
& (SEC_MERGE
| SEC_STRINGS
))
2634 != (flags
& (SEC_MERGE
| SEC_STRINGS
))
2635 || ((flags
& SEC_MERGE
) != 0
2636 && output
->bfd_section
->entsize
!= section
->entsize
))
2638 output
->bfd_section
->flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2639 flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2642 output
->bfd_section
->flags
|= flags
;
2644 if (!output
->bfd_section
->linker_has_input
)
2646 output
->bfd_section
->linker_has_input
= 1;
2647 /* This must happen after flags have been updated. The output
2648 section may have been created before we saw its first input
2649 section, eg. for a data statement. */
2650 bfd_init_private_section_data (section
->owner
, section
,
2651 link_info
.output_bfd
,
2652 output
->bfd_section
,
2654 if ((flags
& SEC_MERGE
) != 0)
2655 output
->bfd_section
->entsize
= section
->entsize
;
2658 if ((flags
& SEC_TIC54X_BLOCK
) != 0
2659 && bfd_get_arch (section
->owner
) == bfd_arch_tic54x
)
2661 /* FIXME: This value should really be obtained from the bfd... */
2662 output
->block_value
= 128;
2665 /* When a .ctors section is placed in .init_array it must be copied
2666 in reverse order. Similarly for .dtors. Set that up. */
2667 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
2668 && ((startswith (section
->name
, ".ctors")
2669 && strcmp (output
->bfd_section
->name
, ".init_array") == 0)
2670 || (startswith (section
->name
, ".dtors")
2671 && strcmp (output
->bfd_section
->name
, ".fini_array") == 0))
2672 && (section
->name
[6] == 0 || section
->name
[6] == '.'))
2673 section
->flags
|= SEC_ELF_REVERSE_COPY
;
2675 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
2676 output
->bfd_section
->alignment_power
= section
->alignment_power
;
2678 section
->output_section
= output
->bfd_section
;
2680 if (!map_head_is_link_order
)
2682 asection
*s
= output
->bfd_section
->map_tail
.s
;
2683 output
->bfd_section
->map_tail
.s
= section
;
2684 section
->map_head
.s
= NULL
;
2685 section
->map_tail
.s
= s
;
2687 s
->map_head
.s
= section
;
2689 output
->bfd_section
->map_head
.s
= section
;
2692 /* Add a section reference to the list. */
2693 new_section
= new_stat (lang_input_section
, ptr
);
2694 new_section
->section
= section
;
2695 new_section
->pattern
= pattern
;
2698 /* Expand a wild statement for a particular FILE. SECTION may be
2699 NULL, in which case it is a wild card. This assumes that the
2700 wild statement doesn't need any sorting (of filenames or sections). */
2703 output_section_callback_nosort (lang_wild_statement_type
*ptr
,
2704 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
2706 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
2709 lang_output_section_statement_type
*os
;
2711 os
= (lang_output_section_statement_type
*) output
;
2713 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2714 if (unique_section_p (section
, os
))
2717 lang_add_section (&ptr
->children
, section
, ptr
->section_list
,
2718 ptr
->section_flag_list
, os
);
2721 /* Check if all sections in a wild statement for a particular FILE
2725 check_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
2726 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
2728 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
2731 lang_output_section_statement_type
*os
;
2733 os
= (lang_output_section_statement_type
*) output
;
2735 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2736 if (unique_section_p (section
, os
))
2739 if (section
->output_section
== NULL
&& (section
->flags
& SEC_READONLY
) == 0)
2740 os
->all_input_readonly
= false;
2743 /* This is passed a file name which must have been seen already and
2744 added to the statement tree. We will see if it has been opened
2745 already and had its symbols read. If not then we'll read it. */
2747 static lang_input_statement_type
*
2748 lookup_name (const char *name
)
2750 lang_input_statement_type
*search
;
2752 for (search
= (void *) input_file_chain
.head
;
2754 search
= search
->next_real_file
)
2756 /* Use the local_sym_name as the name of the file that has
2757 already been loaded as filename might have been transformed
2758 via the search directory lookup mechanism. */
2759 const char *filename
= search
->local_sym_name
;
2761 if (filename
!= NULL
2762 && filename_cmp (filename
, name
) == 0)
2768 /* Arrange to splice the input statement added by new_afile into
2769 statement_list after the current input_file_chain tail.
2770 We know input_file_chain is not an empty list, and that
2771 lookup_name was called via open_input_bfds. Later calls to
2772 lookup_name should always match an existing input_statement. */
2773 lang_statement_union_type
**tail
= stat_ptr
->tail
;
2774 lang_statement_union_type
**after
2775 = (void *) ((char *) input_file_chain
.tail
2776 - offsetof (lang_input_statement_type
, next_real_file
)
2777 + offsetof (lang_input_statement_type
, header
.next
));
2778 lang_statement_union_type
*rest
= *after
;
2779 stat_ptr
->tail
= after
;
2780 search
= new_afile (name
, lang_input_file_is_search_file_enum
,
2781 default_target
, NULL
);
2782 *stat_ptr
->tail
= rest
;
2784 stat_ptr
->tail
= tail
;
2787 /* If we have already added this file, or this file is not real
2788 don't add this file. */
2789 if (search
->flags
.loaded
|| !search
->flags
.real
)
2792 if (!load_symbols (search
, NULL
))
2798 /* Save LIST as a list of libraries whose symbols should not be exported. */
2803 struct excluded_lib
*next
;
2805 static struct excluded_lib
*excluded_libs
;
2808 add_excluded_libs (const char *list
)
2810 const char *p
= list
, *end
;
2814 struct excluded_lib
*entry
;
2815 end
= strpbrk (p
, ",:");
2817 end
= p
+ strlen (p
);
2818 entry
= (struct excluded_lib
*) xmalloc (sizeof (*entry
));
2819 entry
->next
= excluded_libs
;
2820 entry
->name
= (char *) xmalloc (end
- p
+ 1);
2821 memcpy (entry
->name
, p
, end
- p
);
2822 entry
->name
[end
- p
] = '\0';
2823 excluded_libs
= entry
;
2831 check_excluded_libs (bfd
*abfd
)
2833 struct excluded_lib
*lib
= excluded_libs
;
2837 int len
= strlen (lib
->name
);
2838 const char *filename
= lbasename (bfd_get_filename (abfd
));
2840 if (strcmp (lib
->name
, "ALL") == 0)
2842 abfd
->no_export
= true;
2846 if (filename_ncmp (lib
->name
, filename
, len
) == 0
2847 && (filename
[len
] == '\0'
2848 || (filename
[len
] == '.' && filename
[len
+ 1] == 'a'
2849 && filename
[len
+ 2] == '\0')))
2851 abfd
->no_export
= true;
2859 /* Get the symbols for an input file. */
2862 load_symbols (lang_input_statement_type
*entry
,
2863 lang_statement_list_type
*place
)
2867 if (entry
->flags
.loaded
)
2870 ldfile_open_file (entry
);
2872 /* Do not process further if the file was missing. */
2873 if (entry
->flags
.missing_file
)
2876 if (trace_files
|| verbose
)
2877 info_msg ("%pI\n", entry
);
2879 if (!bfd_check_format (entry
->the_bfd
, bfd_archive
)
2880 && !bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
2883 struct lang_input_statement_flags save_flags
;
2886 err
= bfd_get_error ();
2888 /* See if the emulation has some special knowledge. */
2889 if (ldemul_unrecognized_file (entry
))
2891 if (err
== bfd_error_file_ambiguously_recognized
)
2896 if (err
== bfd_error_file_ambiguously_recognized
)
2900 einfo (_("%P: %pB: file not recognized: %E;"
2901 " matching formats:"), entry
->the_bfd
);
2902 for (p
= matching
; *p
!= NULL
; p
++)
2907 else if (err
!= bfd_error_file_not_recognized
2909 einfo (_("%F%P: %pB: file not recognized: %E\n"), entry
->the_bfd
);
2911 bfd_close (entry
->the_bfd
);
2912 entry
->the_bfd
= NULL
;
2914 /* Try to interpret the file as a linker script. */
2915 save_flags
= input_flags
;
2916 ldfile_open_command_file (entry
->filename
);
2918 push_stat_ptr (place
);
2919 input_flags
.add_DT_NEEDED_for_regular
2920 = entry
->flags
.add_DT_NEEDED_for_regular
;
2921 input_flags
.add_DT_NEEDED_for_dynamic
2922 = entry
->flags
.add_DT_NEEDED_for_dynamic
;
2923 input_flags
.whole_archive
= entry
->flags
.whole_archive
;
2924 input_flags
.dynamic
= entry
->flags
.dynamic
;
2926 ldfile_assumed_script
= true;
2927 parser_input
= input_script
;
2928 current_input_file
= entry
->filename
;
2930 current_input_file
= NULL
;
2931 ldfile_assumed_script
= false;
2933 /* missing_file is sticky. sysrooted will already have been
2934 restored when seeing EOF in yyparse, but no harm to restore
2936 save_flags
.missing_file
|= input_flags
.missing_file
;
2937 input_flags
= save_flags
;
2941 entry
->flags
.loaded
= true;
2946 if (ldemul_recognized_file (entry
))
2949 /* We don't call ldlang_add_file for an archive. Instead, the
2950 add_symbols entry point will call ldlang_add_file, via the
2951 add_archive_element callback, for each element of the archive
2953 switch (bfd_get_format (entry
->the_bfd
))
2959 if (!entry
->flags
.reload
)
2960 ldlang_add_file (entry
);
2964 check_excluded_libs (entry
->the_bfd
);
2966 bfd_set_usrdata (entry
->the_bfd
, entry
);
2967 if (entry
->flags
.whole_archive
)
2975 member
= bfd_openr_next_archived_file (entry
->the_bfd
, member
);
2980 if (!bfd_check_format (member
, bfd_object
))
2982 einfo (_("%F%P: %pB: member %pB in archive is not an object\n"),
2983 entry
->the_bfd
, member
);
2988 if (!(*link_info
.callbacks
2989 ->add_archive_element
) (&link_info
, member
,
2990 "--whole-archive", &subsbfd
))
2993 /* Potentially, the add_archive_element hook may have set a
2994 substitute BFD for us. */
2995 if (!bfd_link_add_symbols (subsbfd
, &link_info
))
2997 einfo (_("%F%P: %pB: error adding symbols: %E\n"), member
);
3002 entry
->flags
.loaded
= loaded
;
3008 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
3009 entry
->flags
.loaded
= true;
3011 einfo (_("%F%P: %pB: error adding symbols: %E\n"), entry
->the_bfd
);
3013 return entry
->flags
.loaded
;
3016 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
3017 may be NULL, indicating that it is a wildcard. Separate
3018 lang_input_section statements are created for each part of the
3019 expansion; they are added after the wild statement S. OUTPUT is
3020 the output section. */
3023 wild (lang_wild_statement_type
*s
,
3024 const char *target ATTRIBUTE_UNUSED
,
3025 lang_output_section_statement_type
*output
)
3027 struct wildcard_list
*sec
;
3029 if (s
->filenames_sorted
|| s
->any_specs_sorted
)
3031 lang_section_bst_type
*tree
;
3033 walk_wild (s
, output_section_callback_sort
, output
);
3038 output_section_callback_tree_to_list (s
, tree
, output
);
3040 s
->rightmost
= &s
->tree
;
3044 walk_wild (s
, output_section_callback_nosort
, output
);
3046 if (default_common_section
== NULL
)
3047 for (sec
= s
->section_list
; sec
!= NULL
; sec
= sec
->next
)
3048 if (sec
->spec
.name
!= NULL
&& strcmp (sec
->spec
.name
, "COMMON") == 0)
3050 /* Remember the section that common is going to in case we
3051 later get something which doesn't know where to put it. */
3052 default_common_section
= output
;
3057 /* Return TRUE iff target is the sought target. */
3060 get_target (const bfd_target
*target
, void *data
)
3062 const char *sought
= (const char *) data
;
3064 return strcmp (target
->name
, sought
) == 0;
3067 /* Like strcpy() but convert to lower case as well. */
3070 stricpy (char *dest
, const char *src
)
3074 while ((c
= *src
++) != 0)
3075 *dest
++ = TOLOWER (c
);
3080 /* Remove the first occurrence of needle (if any) in haystack
3084 strcut (char *haystack
, const char *needle
)
3086 haystack
= strstr (haystack
, needle
);
3092 for (src
= haystack
+ strlen (needle
); *src
;)
3093 *haystack
++ = *src
++;
3099 /* Compare two target format name strings.
3100 Return a value indicating how "similar" they are. */
3103 name_compare (const char *first
, const char *second
)
3109 copy1
= (char *) xmalloc (strlen (first
) + 1);
3110 copy2
= (char *) xmalloc (strlen (second
) + 1);
3112 /* Convert the names to lower case. */
3113 stricpy (copy1
, first
);
3114 stricpy (copy2
, second
);
3116 /* Remove size and endian strings from the name. */
3117 strcut (copy1
, "big");
3118 strcut (copy1
, "little");
3119 strcut (copy2
, "big");
3120 strcut (copy2
, "little");
3122 /* Return a value based on how many characters match,
3123 starting from the beginning. If both strings are
3124 the same then return 10 * their length. */
3125 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
3126 if (copy1
[result
] == 0)
3138 /* Set by closest_target_match() below. */
3139 static const bfd_target
*winner
;
3141 /* Scan all the valid bfd targets looking for one that has the endianness
3142 requirement that was specified on the command line, and is the nearest
3143 match to the original output target. */
3146 closest_target_match (const bfd_target
*target
, void *data
)
3148 const bfd_target
*original
= (const bfd_target
*) data
;
3150 if (command_line
.endian
== ENDIAN_BIG
3151 && target
->byteorder
!= BFD_ENDIAN_BIG
)
3154 if (command_line
.endian
== ENDIAN_LITTLE
3155 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
3158 /* Must be the same flavour. */
3159 if (target
->flavour
!= original
->flavour
)
3162 /* Ignore generic big and little endian elf vectors. */
3163 if (strcmp (target
->name
, "elf32-big") == 0
3164 || strcmp (target
->name
, "elf64-big") == 0
3165 || strcmp (target
->name
, "elf32-little") == 0
3166 || strcmp (target
->name
, "elf64-little") == 0)
3169 /* If we have not found a potential winner yet, then record this one. */
3176 /* Oh dear, we now have two potential candidates for a successful match.
3177 Compare their names and choose the better one. */
3178 if (name_compare (target
->name
, original
->name
)
3179 > name_compare (winner
->name
, original
->name
))
3182 /* Keep on searching until wqe have checked them all. */
3186 /* Return the BFD target format of the first input file. */
3189 get_first_input_target (void)
3191 const char *target
= NULL
;
3193 LANG_FOR_EACH_INPUT_STATEMENT (s
)
3195 if (s
->header
.type
== lang_input_statement_enum
3198 ldfile_open_file (s
);
3200 if (s
->the_bfd
!= NULL
3201 && bfd_check_format (s
->the_bfd
, bfd_object
))
3203 target
= bfd_get_target (s
->the_bfd
);
3215 lang_get_output_target (void)
3219 /* Has the user told us which output format to use? */
3220 if (output_target
!= NULL
)
3221 return output_target
;
3223 /* No - has the current target been set to something other than
3225 if (current_target
!= default_target
&& current_target
!= NULL
)
3226 return current_target
;
3228 /* No - can we determine the format of the first input file? */
3229 target
= get_first_input_target ();
3233 /* Failed - use the default output target. */
3234 return default_target
;
3237 /* Open the output file. */
3240 open_output (const char *name
)
3242 lang_input_statement_type
*f
;
3243 char *out
= lrealpath (name
);
3245 for (f
= (void *) input_file_chain
.head
;
3247 f
= f
->next_real_file
)
3250 char *in
= lrealpath (f
->local_sym_name
);
3251 if (filename_cmp (in
, out
) == 0)
3252 einfo (_("%F%P: input file '%s' is the same as output file\n"),
3258 output_target
= lang_get_output_target ();
3260 /* Has the user requested a particular endianness on the command
3262 if (command_line
.endian
!= ENDIAN_UNSET
)
3264 /* Get the chosen target. */
3265 const bfd_target
*target
3266 = bfd_iterate_over_targets (get_target
, (void *) output_target
);
3268 /* If the target is not supported, we cannot do anything. */
3271 enum bfd_endian desired_endian
;
3273 if (command_line
.endian
== ENDIAN_BIG
)
3274 desired_endian
= BFD_ENDIAN_BIG
;
3276 desired_endian
= BFD_ENDIAN_LITTLE
;
3278 /* See if the target has the wrong endianness. This should
3279 not happen if the linker script has provided big and
3280 little endian alternatives, but some scrips don't do
3282 if (target
->byteorder
!= desired_endian
)
3284 /* If it does, then see if the target provides
3285 an alternative with the correct endianness. */
3286 if (target
->alternative_target
!= NULL
3287 && (target
->alternative_target
->byteorder
== desired_endian
))
3288 output_target
= target
->alternative_target
->name
;
3291 /* Try to find a target as similar as possible to
3292 the default target, but which has the desired
3293 endian characteristic. */
3294 bfd_iterate_over_targets (closest_target_match
,
3297 /* Oh dear - we could not find any targets that
3298 satisfy our requirements. */
3300 einfo (_("%P: warning: could not find any targets"
3301 " that match endianness requirement\n"));
3303 output_target
= winner
->name
;
3309 link_info
.output_bfd
= bfd_openw (name
, output_target
);
3311 if (link_info
.output_bfd
== NULL
)
3313 if (bfd_get_error () == bfd_error_invalid_target
)
3314 einfo (_("%F%P: target %s not found\n"), output_target
);
3316 einfo (_("%F%P: cannot open output file %s: %E\n"), name
);
3319 delete_output_file_on_failure
= true;
3321 if (!bfd_set_format (link_info
.output_bfd
, bfd_object
))
3322 einfo (_("%F%P: %s: can not make object file: %E\n"), name
);
3323 if (!bfd_set_arch_mach (link_info
.output_bfd
,
3324 ldfile_output_architecture
,
3325 ldfile_output_machine
))
3326 einfo (_("%F%P: %s: can not set architecture: %E\n"), name
);
3328 link_info
.hash
= bfd_link_hash_table_create (link_info
.output_bfd
);
3329 if (link_info
.hash
== NULL
)
3330 einfo (_("%F%P: can not create hash table: %E\n"));
3332 bfd_set_gp_size (link_info
.output_bfd
, g_switch_value
);
3336 ldlang_open_output (lang_statement_union_type
*statement
)
3338 switch (statement
->header
.type
)
3340 case lang_output_statement_enum
:
3341 ASSERT (link_info
.output_bfd
== NULL
);
3342 open_output (statement
->output_statement
.name
);
3343 ldemul_set_output_arch ();
3344 if (config
.magic_demand_paged
3345 && !bfd_link_relocatable (&link_info
))
3346 link_info
.output_bfd
->flags
|= D_PAGED
;
3348 link_info
.output_bfd
->flags
&= ~D_PAGED
;
3349 if (config
.text_read_only
)
3350 link_info
.output_bfd
->flags
|= WP_TEXT
;
3352 link_info
.output_bfd
->flags
&= ~WP_TEXT
;
3353 if (link_info
.traditional_format
)
3354 link_info
.output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
3356 link_info
.output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
3359 case lang_target_statement_enum
:
3360 current_target
= statement
->target_statement
.target
;
3368 init_opb (asection
*s
)
3373 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
3375 && (s
->flags
& SEC_ELF_OCTETS
) != 0)
3378 x
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3379 ldfile_output_machine
);
3381 while ((x
& 1) == 0)
3389 /* Open all the input files. */
3393 OPEN_BFD_NORMAL
= 0,
3397 #if BFD_SUPPORTS_PLUGINS
3398 static lang_input_statement_type
*plugin_insert
= NULL
;
3399 static struct bfd_link_hash_entry
*plugin_undefs
= NULL
;
3403 open_input_bfds (lang_statement_union_type
*s
, enum open_bfd_mode mode
)
3405 for (; s
!= NULL
; s
= s
->header
.next
)
3407 switch (s
->header
.type
)
3409 case lang_constructors_statement_enum
:
3410 open_input_bfds (constructor_list
.head
, mode
);
3412 case lang_output_section_statement_enum
:
3413 open_input_bfds (s
->output_section_statement
.children
.head
, mode
);
3415 case lang_wild_statement_enum
:
3416 /* Maybe we should load the file's symbols. */
3417 if ((mode
& OPEN_BFD_RESCAN
) == 0
3418 && s
->wild_statement
.filename
3419 && !wildcardp (s
->wild_statement
.filename
)
3420 && !archive_path (s
->wild_statement
.filename
))
3421 lookup_name (s
->wild_statement
.filename
);
3422 open_input_bfds (s
->wild_statement
.children
.head
, mode
);
3424 case lang_group_statement_enum
:
3426 struct bfd_link_hash_entry
*undefs
;
3427 #if BFD_SUPPORTS_PLUGINS
3428 lang_input_statement_type
*plugin_insert_save
;
3431 /* We must continually search the entries in the group
3432 until no new symbols are added to the list of undefined
3437 #if BFD_SUPPORTS_PLUGINS
3438 plugin_insert_save
= plugin_insert
;
3440 undefs
= link_info
.hash
->undefs_tail
;
3441 open_input_bfds (s
->group_statement
.children
.head
,
3442 mode
| OPEN_BFD_FORCE
);
3444 while (undefs
!= link_info
.hash
->undefs_tail
3445 #if BFD_SUPPORTS_PLUGINS
3446 /* Objects inserted by a plugin, which are loaded
3447 before we hit this loop, may have added new
3449 || (plugin_insert
!= plugin_insert_save
&& plugin_undefs
)
3454 case lang_target_statement_enum
:
3455 current_target
= s
->target_statement
.target
;
3457 case lang_input_statement_enum
:
3458 if (s
->input_statement
.flags
.real
)
3460 lang_statement_union_type
**os_tail
;
3461 lang_statement_list_type add
;
3464 s
->input_statement
.target
= current_target
;
3466 /* If we are being called from within a group, and this
3467 is an archive which has already been searched, then
3468 force it to be researched unless the whole archive
3469 has been loaded already. Do the same for a rescan.
3470 Likewise reload --as-needed shared libs. */
3471 if (mode
!= OPEN_BFD_NORMAL
3472 #if BFD_SUPPORTS_PLUGINS
3473 && ((mode
& OPEN_BFD_RESCAN
) == 0
3474 || plugin_insert
== NULL
)
3476 && s
->input_statement
.flags
.loaded
3477 && (abfd
= s
->input_statement
.the_bfd
) != NULL
3478 && ((bfd_get_format (abfd
) == bfd_archive
3479 && !s
->input_statement
.flags
.whole_archive
)
3480 || (bfd_get_format (abfd
) == bfd_object
3481 && ((abfd
->flags
) & DYNAMIC
) != 0
3482 && s
->input_statement
.flags
.add_DT_NEEDED_for_regular
3483 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
3484 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)))
3486 s
->input_statement
.flags
.loaded
= false;
3487 s
->input_statement
.flags
.reload
= true;
3490 os_tail
= lang_os_list
.tail
;
3491 lang_list_init (&add
);
3493 if (!load_symbols (&s
->input_statement
, &add
))
3494 config
.make_executable
= false;
3496 if (add
.head
!= NULL
)
3498 /* If this was a script with output sections then
3499 tack any added statements on to the end of the
3500 list. This avoids having to reorder the output
3501 section statement list. Very likely the user
3502 forgot -T, and whatever we do here will not meet
3503 naive user expectations. */
3504 if (os_tail
!= lang_os_list
.tail
)
3506 einfo (_("%P: warning: %s contains output sections;"
3507 " did you forget -T?\n"),
3508 s
->input_statement
.filename
);
3509 *stat_ptr
->tail
= add
.head
;
3510 stat_ptr
->tail
= add
.tail
;
3514 *add
.tail
= s
->header
.next
;
3515 s
->header
.next
= add
.head
;
3519 #if BFD_SUPPORTS_PLUGINS
3520 /* If we have found the point at which a plugin added new
3521 files, clear plugin_insert to enable archive rescan. */
3522 if (&s
->input_statement
== plugin_insert
)
3523 plugin_insert
= NULL
;
3526 case lang_assignment_statement_enum
:
3527 if (s
->assignment_statement
.exp
->type
.node_class
!= etree_assert
)
3528 exp_fold_tree_no_dot (s
->assignment_statement
.exp
);
3535 /* Exit if any of the files were missing. */
3536 if (input_flags
.missing_file
)
3540 #ifdef ENABLE_LIBCTF
3541 /* Emit CTF errors and warnings. fp can be NULL to report errors/warnings
3542 that happened specifically at CTF open time. */
3544 lang_ctf_errs_warnings (ctf_dict_t
*fp
)
3546 ctf_next_t
*i
= NULL
;
3551 while ((text
= ctf_errwarning_next (fp
, &i
, &is_warning
, &err
)) != NULL
)
3553 einfo (_("%s: %s\n"), is_warning
? _("CTF warning"): _("CTF error"),
3557 if (err
!= ECTF_NEXT_END
)
3559 einfo (_("CTF error: cannot get CTF errors: `%s'\n"),
3563 /* `err' returns errors from the error/warning iterator in particular.
3564 These never assert. But if we have an fp, that could have recorded
3565 an assertion failure: assert if it has done so. */
3566 ASSERT (!fp
|| ctf_errno (fp
) != ECTF_INTERNAL
);
3569 /* Open the CTF sections in the input files with libctf: if any were opened,
3570 create a fake input file that we'll write the merged CTF data to later
3574 ldlang_open_ctf (void)
3579 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3583 /* Incoming files from the compiler have a single ctf_dict_t in them
3584 (which is presented to us by the libctf API in a ctf_archive_t
3585 wrapper): files derived from a previous relocatable link have a CTF
3586 archive containing possibly many CTF files. */
3588 if ((file
->the_ctf
= ctf_bfdopen (file
->the_bfd
, &err
)) == NULL
)
3590 if (err
!= ECTF_NOCTFDATA
)
3592 lang_ctf_errs_warnings (NULL
);
3593 einfo (_("%P: warning: CTF section in %pB not loaded; "
3594 "its types will be discarded: %s\n"), file
->the_bfd
,
3600 /* Prevent the contents of this section from being written, while
3601 requiring the section itself to be duplicated in the output, but only
3603 /* This section must exist if ctf_bfdopen() succeeded. */
3604 sect
= bfd_get_section_by_name (file
->the_bfd
, ".ctf");
3606 sect
->flags
|= SEC_NEVER_LOAD
| SEC_HAS_CONTENTS
| SEC_LINKER_CREATED
;
3609 sect
->flags
|= SEC_EXCLUDE
;
3619 if ((ctf_output
= ctf_create (&err
)) != NULL
)
3622 einfo (_("%P: warning: CTF output not created: `%s'\n"),
3625 LANG_FOR_EACH_INPUT_STATEMENT (errfile
)
3626 ctf_close (errfile
->the_ctf
);
3629 /* Merge together CTF sections. After this, only the symtab-dependent
3630 function and data object sections need adjustment. */
3633 lang_merge_ctf (void)
3635 asection
*output_sect
;
3641 output_sect
= bfd_get_section_by_name (link_info
.output_bfd
, ".ctf");
3643 /* If the section was discarded, don't waste time merging. */
3644 if (output_sect
== NULL
)
3646 ctf_dict_close (ctf_output
);
3649 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3651 ctf_close (file
->the_ctf
);
3652 file
->the_ctf
= NULL
;
3657 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3662 /* Takes ownership of file->the_ctf. */
3663 if (ctf_link_add_ctf (ctf_output
, file
->the_ctf
, file
->filename
) < 0)
3665 einfo (_("%P: warning: CTF section in %pB cannot be linked: `%s'\n"),
3666 file
->the_bfd
, ctf_errmsg (ctf_errno (ctf_output
)));
3667 ctf_close (file
->the_ctf
);
3668 file
->the_ctf
= NULL
;
3673 if (!config
.ctf_share_duplicated
)
3674 flags
= CTF_LINK_SHARE_UNCONFLICTED
;
3676 flags
= CTF_LINK_SHARE_DUPLICATED
;
3677 if (!config
.ctf_variables
)
3678 flags
|= CTF_LINK_OMIT_VARIABLES_SECTION
;
3679 if (bfd_link_relocatable (&link_info
))
3680 flags
|= CTF_LINK_NO_FILTER_REPORTED_SYMS
;
3682 if (ctf_link (ctf_output
, flags
) < 0)
3684 lang_ctf_errs_warnings (ctf_output
);
3685 einfo (_("%P: warning: CTF linking failed; "
3686 "output will have no CTF section: %s\n"),
3687 ctf_errmsg (ctf_errno (ctf_output
)));
3690 output_sect
->size
= 0;
3691 output_sect
->flags
|= SEC_EXCLUDE
;
3694 /* Output any lingering errors that didn't come from ctf_link. */
3695 lang_ctf_errs_warnings (ctf_output
);
3698 /* Let the emulation acquire strings from the dynamic strtab to help it optimize
3699 the CTF, if supported. */
3702 ldlang_ctf_acquire_strings (struct elf_strtab_hash
*dynstrtab
)
3704 ldemul_acquire_strings_for_ctf (ctf_output
, dynstrtab
);
3707 /* Inform the emulation about the addition of a new dynamic symbol, in BFD
3709 void ldlang_ctf_new_dynsym (int symidx
, struct elf_internal_sym
*sym
)
3711 ldemul_new_dynsym_for_ctf (ctf_output
, symidx
, sym
);
3714 /* Write out the CTF section. Called early, if the emulation isn't going to
3715 need to dedup against the strtab and symtab, then possibly called from the
3716 target linker code if the dedup has happened. */
3718 lang_write_ctf (int late
)
3721 asection
*output_sect
;
3728 /* Emit CTF late if this emulation says it can do so. */
3729 if (ldemul_emit_ctf_early ())
3734 if (!ldemul_emit_ctf_early ())
3738 /* Inform the emulation that all the symbols that will be received have
3741 ldemul_new_dynsym_for_ctf (ctf_output
, 0, NULL
);
3745 output_sect
= bfd_get_section_by_name (link_info
.output_bfd
, ".ctf");
3748 output_sect
->contents
= ctf_link_write (ctf_output
, &output_size
,
3749 CTF_COMPRESSION_THRESHOLD
);
3750 output_sect
->size
= output_size
;
3751 output_sect
->flags
|= SEC_IN_MEMORY
| SEC_KEEP
;
3753 lang_ctf_errs_warnings (ctf_output
);
3754 if (!output_sect
->contents
)
3756 einfo (_("%P: warning: CTF section emission failed; "
3757 "output will have no CTF section: %s\n"),
3758 ctf_errmsg (ctf_errno (ctf_output
)));
3759 output_sect
->size
= 0;
3760 output_sect
->flags
|= SEC_EXCLUDE
;
3764 /* This also closes every CTF input file used in the link. */
3765 ctf_dict_close (ctf_output
);
3768 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3769 file
->the_ctf
= NULL
;
3772 /* Write out the CTF section late, if the emulation needs that. */
3775 ldlang_write_ctf_late (void)
3777 /* Trigger a "late call", if the emulation needs one. */
3783 ldlang_open_ctf (void)
3785 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3789 /* If built without CTF, warn and delete all CTF sections from the output.
3790 (The alternative would be to simply concatenate them, which does not
3791 yield a valid CTF section.) */
3793 if ((sect
= bfd_get_section_by_name (file
->the_bfd
, ".ctf")) != NULL
)
3795 einfo (_("%P: warning: CTF section in %pB not linkable: "
3796 "%P was built without support for CTF\n"), file
->the_bfd
);
3798 sect
->flags
|= SEC_EXCLUDE
;
3803 static void lang_merge_ctf (void) {}
3805 ldlang_ctf_acquire_strings (struct elf_strtab_hash
*dynstrtab
3806 ATTRIBUTE_UNUSED
) {}
3808 ldlang_ctf_new_dynsym (int symidx ATTRIBUTE_UNUSED
,
3809 struct elf_internal_sym
*sym ATTRIBUTE_UNUSED
) {}
3810 static void lang_write_ctf (int late ATTRIBUTE_UNUSED
) {}
3811 void ldlang_write_ctf_late (void) {}
3814 /* Add the supplied name to the symbol table as an undefined reference.
3815 This is a two step process as the symbol table doesn't even exist at
3816 the time the ld command line is processed. First we put the name
3817 on a list, then, once the output file has been opened, transfer the
3818 name to the symbol table. */
3820 typedef struct bfd_sym_chain ldlang_undef_chain_list_type
;
3822 #define ldlang_undef_chain_list_head entry_symbol.next
3825 ldlang_add_undef (const char *const name
, bool cmdline ATTRIBUTE_UNUSED
)
3827 ldlang_undef_chain_list_type
*new_undef
;
3829 new_undef
= stat_alloc (sizeof (*new_undef
));
3830 new_undef
->next
= ldlang_undef_chain_list_head
;
3831 ldlang_undef_chain_list_head
= new_undef
;
3833 new_undef
->name
= xstrdup (name
);
3835 if (link_info
.output_bfd
!= NULL
)
3836 insert_undefined (new_undef
->name
);
3839 /* Insert NAME as undefined in the symbol table. */
3842 insert_undefined (const char *name
)
3844 struct bfd_link_hash_entry
*h
;
3846 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, false, true);
3848 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
3849 if (h
->type
== bfd_link_hash_new
)
3851 h
->type
= bfd_link_hash_undefined
;
3852 h
->u
.undef
.abfd
= NULL
;
3853 h
->non_ir_ref_regular
= true;
3854 bfd_link_add_undef (link_info
.hash
, h
);
3858 /* Run through the list of undefineds created above and place them
3859 into the linker hash table as undefined symbols belonging to the
3863 lang_place_undefineds (void)
3865 ldlang_undef_chain_list_type
*ptr
;
3867 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
3868 insert_undefined (ptr
->name
);
3871 /* Mark -u symbols against garbage collection. */
3874 lang_mark_undefineds (void)
3876 ldlang_undef_chain_list_type
*ptr
;
3878 if (is_elf_hash_table (link_info
.hash
))
3879 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
3881 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*)
3882 bfd_link_hash_lookup (link_info
.hash
, ptr
->name
, false, false, true);
3888 /* Structure used to build the list of symbols that the user has required
3891 struct require_defined_symbol
3894 struct require_defined_symbol
*next
;
3897 /* The list of symbols that the user has required be defined. */
3899 static struct require_defined_symbol
*require_defined_symbol_list
;
3901 /* Add a new symbol NAME to the list of symbols that are required to be
3905 ldlang_add_require_defined (const char *const name
)
3907 struct require_defined_symbol
*ptr
;
3909 ldlang_add_undef (name
, true);
3910 ptr
= stat_alloc (sizeof (*ptr
));
3911 ptr
->next
= require_defined_symbol_list
;
3912 ptr
->name
= strdup (name
);
3913 require_defined_symbol_list
= ptr
;
3916 /* Check that all symbols the user required to be defined, are defined,
3917 raise an error if we find a symbol that is not defined. */
3920 ldlang_check_require_defined_symbols (void)
3922 struct require_defined_symbol
*ptr
;
3924 for (ptr
= require_defined_symbol_list
; ptr
!= NULL
; ptr
= ptr
->next
)
3926 struct bfd_link_hash_entry
*h
;
3928 h
= bfd_link_hash_lookup (link_info
.hash
, ptr
->name
,
3929 false, false, true);
3931 || (h
->type
!= bfd_link_hash_defined
3932 && h
->type
!= bfd_link_hash_defweak
))
3933 einfo(_("%X%P: required symbol `%s' not defined\n"), ptr
->name
);
3937 /* Check for all readonly or some readwrite sections. */
3940 check_input_sections
3941 (lang_statement_union_type
*s
,
3942 lang_output_section_statement_type
*output_section_statement
)
3944 for (; s
!= NULL
; s
= s
->header
.next
)
3946 switch (s
->header
.type
)
3948 case lang_wild_statement_enum
:
3949 walk_wild (&s
->wild_statement
, check_section_callback
,
3950 output_section_statement
);
3951 if (!output_section_statement
->all_input_readonly
)
3954 case lang_constructors_statement_enum
:
3955 check_input_sections (constructor_list
.head
,
3956 output_section_statement
);
3957 if (!output_section_statement
->all_input_readonly
)
3960 case lang_group_statement_enum
:
3961 check_input_sections (s
->group_statement
.children
.head
,
3962 output_section_statement
);
3963 if (!output_section_statement
->all_input_readonly
)
3972 /* Update wildcard statements if needed. */
3975 update_wild_statements (lang_statement_union_type
*s
)
3977 struct wildcard_list
*sec
;
3979 switch (sort_section
)
3989 for (; s
!= NULL
; s
= s
->header
.next
)
3991 switch (s
->header
.type
)
3996 case lang_wild_statement_enum
:
3997 for (sec
= s
->wild_statement
.section_list
; sec
!= NULL
;
3999 /* Don't sort .init/.fini sections. */
4000 if (strcmp (sec
->spec
.name
, ".init") != 0
4001 && strcmp (sec
->spec
.name
, ".fini") != 0)
4003 switch (sec
->spec
.sorted
)
4006 sec
->spec
.sorted
= sort_section
;
4009 if (sort_section
== by_alignment
)
4010 sec
->spec
.sorted
= by_name_alignment
;
4013 if (sort_section
== by_name
)
4014 sec
->spec
.sorted
= by_alignment_name
;
4019 s
->wild_statement
.any_specs_sorted
= true;
4023 case lang_constructors_statement_enum
:
4024 update_wild_statements (constructor_list
.head
);
4027 case lang_output_section_statement_enum
:
4028 update_wild_statements
4029 (s
->output_section_statement
.children
.head
);
4032 case lang_group_statement_enum
:
4033 update_wild_statements (s
->group_statement
.children
.head
);
4041 /* Open input files and attach to output sections. */
4044 map_input_to_output_sections
4045 (lang_statement_union_type
*s
, const char *target
,
4046 lang_output_section_statement_type
*os
)
4048 for (; s
!= NULL
; s
= s
->header
.next
)
4050 lang_output_section_statement_type
*tos
;
4052 unsigned int type
= 0;
4054 switch (s
->header
.type
)
4056 case lang_wild_statement_enum
:
4057 wild (&s
->wild_statement
, target
, os
);
4059 case lang_constructors_statement_enum
:
4060 map_input_to_output_sections (constructor_list
.head
,
4064 case lang_output_section_statement_enum
:
4065 tos
= &s
->output_section_statement
;
4066 if (tos
->constraint
== ONLY_IF_RW
4067 || tos
->constraint
== ONLY_IF_RO
)
4069 tos
->all_input_readonly
= true;
4070 check_input_sections (tos
->children
.head
, tos
);
4071 if (tos
->all_input_readonly
!= (tos
->constraint
== ONLY_IF_RO
))
4072 tos
->constraint
= -1;
4074 if (tos
->constraint
>= 0)
4075 map_input_to_output_sections (tos
->children
.head
,
4079 case lang_output_statement_enum
:
4081 case lang_target_statement_enum
:
4082 target
= s
->target_statement
.target
;
4084 case lang_group_statement_enum
:
4085 map_input_to_output_sections (s
->group_statement
.children
.head
,
4089 case lang_data_statement_enum
:
4090 /* Make sure that any sections mentioned in the expression
4092 exp_init_os (s
->data_statement
.exp
);
4093 /* The output section gets CONTENTS, ALLOC and LOAD, but
4094 these may be overridden by the script. */
4095 flags
= SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
;
4096 switch (os
->sectype
)
4098 case normal_section
:
4099 case overlay_section
:
4100 case first_overlay_section
:
4102 case noalloc_section
:
4103 flags
= SEC_HAS_CONTENTS
;
4105 case readonly_section
:
4106 flags
|= SEC_READONLY
;
4108 case typed_readonly_section
:
4109 flags
|= SEC_READONLY
;
4112 if (os
->sectype_value
->type
.node_class
== etree_name
4113 && os
->sectype_value
->type
.node_code
== NAME
)
4115 const char *name
= os
->sectype_value
->name
.name
;
4116 if (strcmp (name
, "SHT_PROGBITS") == 0)
4117 type
= SHT_PROGBITS
;
4118 else if (strcmp (name
, "SHT_STRTAB") == 0)
4120 else if (strcmp (name
, "SHT_NOTE") == 0)
4122 else if (strcmp (name
, "SHT_NOBITS") == 0)
4124 else if (strcmp (name
, "SHT_INIT_ARRAY") == 0)
4125 type
= SHT_INIT_ARRAY
;
4126 else if (strcmp (name
, "SHT_FINI_ARRAY") == 0)
4127 type
= SHT_FINI_ARRAY
;
4128 else if (strcmp (name
, "SHT_PREINIT_ARRAY") == 0)
4129 type
= SHT_PREINIT_ARRAY
;
4131 einfo (_ ("%F%P: invalid type for output section `%s'\n"),
4136 exp_fold_tree_no_dot (os
->sectype_value
);
4137 if (expld
.result
.valid_p
)
4138 type
= expld
.result
.value
;
4140 einfo (_ ("%F%P: invalid type for output section `%s'\n"),
4144 case noload_section
:
4145 if (bfd_get_flavour (link_info
.output_bfd
)
4146 == bfd_target_elf_flavour
)
4147 flags
= SEC_NEVER_LOAD
| SEC_ALLOC
;
4149 flags
= SEC_NEVER_LOAD
| SEC_HAS_CONTENTS
;
4152 if (os
->bfd_section
== NULL
)
4153 init_os (os
, flags
| SEC_READONLY
);
4155 os
->bfd_section
->flags
|= flags
;
4156 os
->bfd_section
->type
= type
;
4158 case lang_input_section_enum
:
4160 case lang_fill_statement_enum
:
4161 case lang_object_symbols_statement_enum
:
4162 case lang_reloc_statement_enum
:
4163 case lang_padding_statement_enum
:
4164 case lang_input_statement_enum
:
4165 if (os
!= NULL
&& os
->bfd_section
== NULL
)
4168 case lang_assignment_statement_enum
:
4169 if (os
!= NULL
&& os
->bfd_section
== NULL
)
4172 /* Make sure that any sections mentioned in the assignment
4174 exp_init_os (s
->assignment_statement
.exp
);
4176 case lang_address_statement_enum
:
4177 /* Mark the specified section with the supplied address.
4178 If this section was actually a segment marker, then the
4179 directive is ignored if the linker script explicitly
4180 processed the segment marker. Originally, the linker
4181 treated segment directives (like -Ttext on the
4182 command-line) as section directives. We honor the
4183 section directive semantics for backwards compatibility;
4184 linker scripts that do not specifically check for
4185 SEGMENT_START automatically get the old semantics. */
4186 if (!s
->address_statement
.segment
4187 || !s
->address_statement
.segment
->used
)
4189 const char *name
= s
->address_statement
.section_name
;
4191 /* Create the output section statement here so that
4192 orphans with a set address will be placed after other
4193 script sections. If we let the orphan placement code
4194 place them in amongst other sections then the address
4195 will affect following script sections, which is
4196 likely to surprise naive users. */
4197 tos
= lang_output_section_statement_lookup (name
, 0, 1);
4198 tos
->addr_tree
= s
->address_statement
.address
;
4199 if (tos
->bfd_section
== NULL
)
4203 case lang_insert_statement_enum
:
4205 case lang_input_matcher_enum
:
4211 /* An insert statement snips out all the linker statements from the
4212 start of the list and places them after the output section
4213 statement specified by the insert. This operation is complicated
4214 by the fact that we keep a doubly linked list of output section
4215 statements as well as the singly linked list of all statements.
4216 FIXME someday: Twiddling with the list not only moves statements
4217 from the user's script but also input and group statements that are
4218 built from command line object files and --start-group. We only
4219 get away with this because the list pointers used by file_chain
4220 and input_file_chain are not reordered, and processing via
4221 statement_list after this point mostly ignores input statements.
4222 One exception is the map file, where LOAD and START GROUP/END GROUP
4223 can end up looking odd. */
4226 process_insert_statements (lang_statement_union_type
**start
)
4228 lang_statement_union_type
**s
;
4229 lang_output_section_statement_type
*first_os
= NULL
;
4230 lang_output_section_statement_type
*last_os
= NULL
;
4231 lang_output_section_statement_type
*os
;
4236 if ((*s
)->header
.type
== lang_output_section_statement_enum
)
4238 /* Keep pointers to the first and last output section
4239 statement in the sequence we may be about to move. */
4240 os
= &(*s
)->output_section_statement
;
4242 ASSERT (last_os
== NULL
|| last_os
->next
== os
);
4245 /* Set constraint negative so that lang_output_section_find
4246 won't match this output section statement. At this
4247 stage in linking constraint has values in the range
4248 [-1, ONLY_IN_RW]. */
4249 last_os
->constraint
= -2 - last_os
->constraint
;
4250 if (first_os
== NULL
)
4253 else if ((*s
)->header
.type
== lang_group_statement_enum
)
4255 /* A user might put -T between --start-group and
4256 --end-group. One way this odd construct might arise is
4257 from a wrapper around ld to change library search
4258 behaviour. For example:
4260 exec real_ld --start-group "$@" --end-group
4261 This isn't completely unreasonable so go looking inside a
4262 group statement for insert statements. */
4263 process_insert_statements (&(*s
)->group_statement
.children
.head
);
4265 else if ((*s
)->header
.type
== lang_insert_statement_enum
)
4267 lang_insert_statement_type
*i
= &(*s
)->insert_statement
;
4268 lang_output_section_statement_type
*where
;
4269 lang_statement_union_type
**ptr
;
4270 lang_statement_union_type
*first
;
4272 if (link_info
.non_contiguous_regions
)
4274 einfo (_("warning: INSERT statement in linker script is "
4275 "incompatible with --enable-non-contiguous-regions.\n"));
4278 where
= lang_output_section_find (i
->where
);
4279 if (where
!= NULL
&& i
->is_before
)
4282 where
= where
->prev
;
4283 while (where
!= NULL
&& where
->constraint
< 0);
4287 einfo (_("%F%P: %s not found for insert\n"), i
->where
);
4291 /* Deal with reordering the output section statement list. */
4292 if (last_os
!= NULL
)
4294 asection
*first_sec
, *last_sec
;
4295 struct lang_output_section_statement_struct
**next
;
4297 /* Snip out the output sections we are moving. */
4298 first_os
->prev
->next
= last_os
->next
;
4299 if (last_os
->next
== NULL
)
4301 next
= &first_os
->prev
->next
;
4302 lang_os_list
.tail
= (lang_statement_union_type
**) next
;
4305 last_os
->next
->prev
= first_os
->prev
;
4306 /* Add them in at the new position. */
4307 last_os
->next
= where
->next
;
4308 if (where
->next
== NULL
)
4310 next
= &last_os
->next
;
4311 lang_os_list
.tail
= (lang_statement_union_type
**) next
;
4314 where
->next
->prev
= last_os
;
4315 first_os
->prev
= where
;
4316 where
->next
= first_os
;
4318 /* Move the bfd sections in the same way. */
4321 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
4323 os
->constraint
= -2 - os
->constraint
;
4324 if (os
->bfd_section
!= NULL
4325 && os
->bfd_section
->owner
!= NULL
)
4327 last_sec
= os
->bfd_section
;
4328 if (first_sec
== NULL
)
4329 first_sec
= last_sec
;
4334 if (last_sec
!= NULL
)
4336 asection
*sec
= where
->bfd_section
;
4338 sec
= output_prev_sec_find (where
);
4340 /* The place we want to insert must come after the
4341 sections we are moving. So if we find no
4342 section or if the section is the same as our
4343 last section, then no move is needed. */
4344 if (sec
!= NULL
&& sec
!= last_sec
)
4346 /* Trim them off. */
4347 if (first_sec
->prev
!= NULL
)
4348 first_sec
->prev
->next
= last_sec
->next
;
4350 link_info
.output_bfd
->sections
= last_sec
->next
;
4351 if (last_sec
->next
!= NULL
)
4352 last_sec
->next
->prev
= first_sec
->prev
;
4354 link_info
.output_bfd
->section_last
= first_sec
->prev
;
4356 last_sec
->next
= sec
->next
;
4357 if (sec
->next
!= NULL
)
4358 sec
->next
->prev
= last_sec
;
4360 link_info
.output_bfd
->section_last
= last_sec
;
4361 first_sec
->prev
= sec
;
4362 sec
->next
= first_sec
;
4370 ptr
= insert_os_after (where
);
4371 /* Snip everything from the start of the list, up to and
4372 including the insert statement we are currently processing. */
4374 *start
= (*s
)->header
.next
;
4375 /* Add them back where they belong, minus the insert. */
4378 statement_list
.tail
= s
;
4383 s
= &(*s
)->header
.next
;
4386 /* Undo constraint twiddling. */
4387 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
4389 os
->constraint
= -2 - os
->constraint
;
4395 /* An output section might have been removed after its statement was
4396 added. For example, ldemul_before_allocation can remove dynamic
4397 sections if they turn out to be not needed. Clean them up here. */
4400 strip_excluded_output_sections (void)
4402 lang_output_section_statement_type
*os
;
4404 /* Run lang_size_sections (if not already done). */
4405 if (expld
.phase
!= lang_mark_phase_enum
)
4407 expld
.phase
= lang_mark_phase_enum
;
4408 expld
.dataseg
.phase
= exp_seg_none
;
4409 one_lang_size_sections_pass (NULL
, false);
4410 lang_reset_memory_regions ();
4413 for (os
= (void *) lang_os_list
.head
;
4417 asection
*output_section
;
4420 if (os
->constraint
< 0)
4423 output_section
= os
->bfd_section
;
4424 if (output_section
== NULL
)
4427 exclude
= (output_section
->rawsize
== 0
4428 && (output_section
->flags
& SEC_KEEP
) == 0
4429 && !bfd_section_removed_from_list (link_info
.output_bfd
,
4432 /* Some sections have not yet been sized, notably .gnu.version,
4433 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
4434 input sections, so don't drop output sections that have such
4435 input sections unless they are also marked SEC_EXCLUDE. */
4436 if (exclude
&& output_section
->map_head
.s
!= NULL
)
4440 for (s
= output_section
->map_head
.s
; s
!= NULL
; s
= s
->map_head
.s
)
4441 if ((s
->flags
& SEC_EXCLUDE
) == 0
4442 && ((s
->flags
& SEC_LINKER_CREATED
) != 0
4443 || link_info
.emitrelocations
))
4452 /* We don't set bfd_section to NULL since bfd_section of the
4453 removed output section statement may still be used. */
4454 if (!os
->update_dot
)
4456 output_section
->flags
|= SEC_EXCLUDE
;
4457 bfd_section_list_remove (link_info
.output_bfd
, output_section
);
4458 link_info
.output_bfd
->section_count
--;
4463 /* Called from ldwrite to clear out asection.map_head and
4464 asection.map_tail for use as link_orders in ldwrite. */
4467 lang_clear_os_map (void)
4469 lang_output_section_statement_type
*os
;
4471 if (map_head_is_link_order
)
4474 for (os
= (void *) lang_os_list
.head
;
4478 asection
*output_section
;
4480 if (os
->constraint
< 0)
4483 output_section
= os
->bfd_section
;
4484 if (output_section
== NULL
)
4487 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
4488 output_section
->map_head
.link_order
= NULL
;
4489 output_section
->map_tail
.link_order
= NULL
;
4492 /* Stop future calls to lang_add_section from messing with map_head
4493 and map_tail link_order fields. */
4494 map_head_is_link_order
= true;
4498 print_output_section_statement
4499 (lang_output_section_statement_type
*output_section_statement
)
4501 asection
*section
= output_section_statement
->bfd_section
;
4504 if (output_section_statement
!= abs_output_section
)
4506 minfo ("\n%s", output_section_statement
->name
);
4508 if (section
!= NULL
)
4510 print_dot
= section
->vma
;
4512 len
= strlen (output_section_statement
->name
);
4513 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
4518 print_spaces (SECTION_NAME_MAP_LENGTH
- len
);
4520 minfo ("0x%V %W", section
->vma
, TO_ADDR (section
->size
));
4522 if (section
->vma
!= section
->lma
)
4523 minfo (_(" load address 0x%V"), section
->lma
);
4525 if (output_section_statement
->update_dot_tree
!= NULL
)
4526 exp_fold_tree (output_section_statement
->update_dot_tree
,
4527 bfd_abs_section_ptr
, &print_dot
);
4533 print_statement_list (output_section_statement
->children
.head
,
4534 output_section_statement
);
4538 print_assignment (lang_assignment_statement_type
*assignment
,
4539 lang_output_section_statement_type
*output_section
)
4545 print_spaces (SECTION_NAME_MAP_LENGTH
);
4547 if (assignment
->exp
->type
.node_class
== etree_assert
)
4550 tree
= assignment
->exp
->assert_s
.child
;
4554 const char *dst
= assignment
->exp
->assign
.dst
;
4556 is_dot
= (dst
[0] == '.' && dst
[1] == 0);
4557 tree
= assignment
->exp
;
4560 osec
= output_section
->bfd_section
;
4562 osec
= bfd_abs_section_ptr
;
4564 if (assignment
->exp
->type
.node_class
!= etree_provide
)
4565 exp_fold_tree (tree
, osec
, &print_dot
);
4567 expld
.result
.valid_p
= false;
4570 const char *str
= buf
;
4571 if (expld
.result
.valid_p
)
4575 if (assignment
->exp
->type
.node_class
== etree_assert
4577 || expld
.assign_name
!= NULL
)
4579 value
= expld
.result
.value
;
4581 if (expld
.result
.section
!= NULL
)
4582 value
+= expld
.result
.section
->vma
;
4586 bfd_sprintf_vma (link_info
.output_bfd
, buf
+ 2, value
);
4592 struct bfd_link_hash_entry
*h
;
4594 h
= bfd_link_hash_lookup (link_info
.hash
, assignment
->exp
->assign
.dst
,
4595 false, false, true);
4597 && (h
->type
== bfd_link_hash_defined
4598 || h
->type
== bfd_link_hash_defweak
))
4600 value
= h
->u
.def
.value
;
4601 value
+= h
->u
.def
.section
->output_section
->vma
;
4602 value
+= h
->u
.def
.section
->output_offset
;
4607 bfd_sprintf_vma (link_info
.output_bfd
, buf
+ 3, value
);
4611 str
= "[unresolved]";
4616 if (assignment
->exp
->type
.node_class
== etree_provide
)
4621 expld
.assign_name
= NULL
;
4623 fprintf (config
.map_file
, "%-34s", str
);
4624 exp_print_tree (assignment
->exp
);
4629 print_input_statement (lang_input_statement_type
*statm
)
4631 if (statm
->filename
!= NULL
)
4632 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
4635 /* Print all symbols defined in a particular section. This is called
4636 via bfd_link_hash_traverse, or by print_all_symbols. */
4639 print_one_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
)
4641 asection
*sec
= (asection
*) ptr
;
4643 if ((hash_entry
->type
== bfd_link_hash_defined
4644 || hash_entry
->type
== bfd_link_hash_defweak
)
4645 && sec
== hash_entry
->u
.def
.section
)
4647 print_spaces (SECTION_NAME_MAP_LENGTH
);
4649 (hash_entry
->u
.def
.value
4650 + hash_entry
->u
.def
.section
->output_offset
4651 + hash_entry
->u
.def
.section
->output_section
->vma
));
4653 minfo (" %pT\n", hash_entry
->root
.string
);
4660 hash_entry_addr_cmp (const void *a
, const void *b
)
4662 const struct bfd_link_hash_entry
*l
= *(const struct bfd_link_hash_entry
**)a
;
4663 const struct bfd_link_hash_entry
*r
= *(const struct bfd_link_hash_entry
**)b
;
4665 if (l
->u
.def
.value
< r
->u
.def
.value
)
4667 else if (l
->u
.def
.value
> r
->u
.def
.value
)
4674 print_all_symbols (asection
*sec
)
4676 input_section_userdata_type
*ud
= bfd_section_userdata (sec
);
4677 struct map_symbol_def
*def
;
4678 struct bfd_link_hash_entry
**entries
;
4684 *ud
->map_symbol_def_tail
= 0;
4686 /* Sort the symbols by address. */
4687 entries
= (struct bfd_link_hash_entry
**)
4688 obstack_alloc (&map_obstack
,
4689 ud
->map_symbol_def_count
* sizeof (*entries
));
4691 for (i
= 0, def
= ud
->map_symbol_def_head
; def
; def
= def
->next
, i
++)
4692 entries
[i
] = def
->entry
;
4694 qsort (entries
, ud
->map_symbol_def_count
, sizeof (*entries
),
4695 hash_entry_addr_cmp
);
4697 /* Print the symbols. */
4698 for (i
= 0; i
< ud
->map_symbol_def_count
; i
++)
4699 ldemul_print_symbol (entries
[i
], sec
);
4701 obstack_free (&map_obstack
, entries
);
4704 /* Print information about an input section to the map file. */
4707 print_input_section (asection
*i
, bool is_discarded
)
4709 bfd_size_type size
= i
->size
;
4715 minfo (" %s", i
->name
);
4717 len
= 1 + strlen (i
->name
);
4718 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
4723 print_spaces (SECTION_NAME_MAP_LENGTH
- len
);
4725 if (i
->output_section
!= NULL
4726 && i
->output_section
->owner
== link_info
.output_bfd
)
4727 addr
= i
->output_section
->vma
+ i
->output_offset
;
4736 bfd_sprintf_vma (link_info
.output_bfd
, buf
, addr
);
4737 minfo ("0x%s %W %pB\n", buf
, TO_ADDR (size
), i
->owner
);
4739 if (size
!= i
->rawsize
&& i
->rawsize
!= 0)
4741 len
= SECTION_NAME_MAP_LENGTH
+ 3 + strlen (buf
);
4743 minfo (_("%W (size before relaxing)\n"), TO_ADDR (i
->rawsize
));
4746 if (i
->output_section
!= NULL
4747 && i
->output_section
->owner
== link_info
.output_bfd
)
4749 if (link_info
.reduce_memory_overheads
)
4750 bfd_link_hash_traverse (link_info
.hash
, ldemul_print_symbol
, i
);
4752 print_all_symbols (i
);
4754 /* Update print_dot, but make sure that we do not move it
4755 backwards - this could happen if we have overlays and a
4756 later overlay is shorter than an earier one. */
4757 if (addr
+ TO_ADDR (size
) > print_dot
)
4758 print_dot
= addr
+ TO_ADDR (size
);
4763 print_fill_statement (lang_fill_statement_type
*fill
)
4767 fputs (" FILL mask 0x", config
.map_file
);
4768 for (p
= fill
->fill
->data
, size
= fill
->fill
->size
; size
!= 0; p
++, size
--)
4769 fprintf (config
.map_file
, "%02x", *p
);
4770 fputs ("\n", config
.map_file
);
4774 print_data_statement (lang_data_statement_type
*data
)
4780 init_opb (data
->output_section
);
4781 print_spaces (SECTION_NAME_MAP_LENGTH
);
4783 addr
= data
->output_offset
;
4784 if (data
->output_section
!= NULL
)
4785 addr
+= data
->output_section
->vma
;
4813 if (size
< TO_SIZE ((unsigned) 1))
4814 size
= TO_SIZE ((unsigned) 1);
4815 minfo ("0x%V %W %s 0x%v", addr
, TO_ADDR (size
), name
, data
->value
);
4817 if (data
->exp
->type
.node_class
!= etree_value
)
4820 exp_print_tree (data
->exp
);
4825 print_dot
= addr
+ TO_ADDR (size
);
4828 /* Print an address statement. These are generated by options like
4832 print_address_statement (lang_address_statement_type
*address
)
4834 minfo (_("Address of section %s set to "), address
->section_name
);
4835 exp_print_tree (address
->address
);
4839 /* Print a reloc statement. */
4842 print_reloc_statement (lang_reloc_statement_type
*reloc
)
4847 init_opb (reloc
->output_section
);
4848 print_spaces (SECTION_NAME_MAP_LENGTH
);
4850 addr
= reloc
->output_offset
;
4851 if (reloc
->output_section
!= NULL
)
4852 addr
+= reloc
->output_section
->vma
;
4854 size
= bfd_get_reloc_size (reloc
->howto
);
4856 minfo ("0x%V %W RELOC %s ", addr
, TO_ADDR (size
), reloc
->howto
->name
);
4858 if (reloc
->name
!= NULL
)
4859 minfo ("%s+", reloc
->name
);
4861 minfo ("%s+", reloc
->section
->name
);
4863 exp_print_tree (reloc
->addend_exp
);
4867 print_dot
= addr
+ TO_ADDR (size
);
4871 print_padding_statement (lang_padding_statement_type
*s
)
4876 init_opb (s
->output_section
);
4879 len
= sizeof " *fill*" - 1;
4880 print_spaces (SECTION_NAME_MAP_LENGTH
- len
);
4882 addr
= s
->output_offset
;
4883 if (s
->output_section
!= NULL
)
4884 addr
+= s
->output_section
->vma
;
4885 minfo ("0x%V %W ", addr
, TO_ADDR (s
->size
));
4887 if (s
->fill
->size
!= 0)
4891 for (p
= s
->fill
->data
, size
= s
->fill
->size
; size
!= 0; p
++, size
--)
4892 fprintf (config
.map_file
, "%02x", *p
);
4897 print_dot
= addr
+ TO_ADDR (s
->size
);
4901 print_wild_statement (lang_wild_statement_type
*w
,
4902 lang_output_section_statement_type
*os
)
4904 struct wildcard_list
*sec
;
4908 if (w
->exclude_name_list
)
4911 minfo ("EXCLUDE_FILE(%s", w
->exclude_name_list
->name
);
4912 for (tmp
= w
->exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
4913 minfo (" %s", tmp
->name
);
4917 if (w
->filenames_sorted
)
4918 minfo ("SORT_BY_NAME(");
4919 if (w
->filename
!= NULL
)
4920 minfo ("%s", w
->filename
);
4923 if (w
->filenames_sorted
)
4927 for (sec
= w
->section_list
; sec
; sec
= sec
->next
)
4929 int closing_paren
= 0;
4931 switch (sec
->spec
.sorted
)
4937 minfo ("SORT_BY_NAME(");
4942 minfo ("SORT_BY_ALIGNMENT(");
4946 case by_name_alignment
:
4947 minfo ("SORT_BY_NAME(SORT_BY_ALIGNMENT(");
4951 case by_alignment_name
:
4952 minfo ("SORT_BY_ALIGNMENT(SORT_BY_NAME(");
4957 minfo ("SORT_NONE(");
4961 case by_init_priority
:
4962 minfo ("SORT_BY_INIT_PRIORITY(");
4967 if (sec
->spec
.exclude_name_list
!= NULL
)
4970 minfo ("EXCLUDE_FILE(%s", sec
->spec
.exclude_name_list
->name
);
4971 for (tmp
= sec
->spec
.exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
4972 minfo (" %s", tmp
->name
);
4975 if (sec
->spec
.name
!= NULL
)
4976 minfo ("%s", sec
->spec
.name
);
4979 for (;closing_paren
> 0; closing_paren
--)
4988 print_statement_list (w
->children
.head
, os
);
4991 /* Print a group statement. */
4994 print_group (lang_group_statement_type
*s
,
4995 lang_output_section_statement_type
*os
)
4997 fprintf (config
.map_file
, "START GROUP\n");
4998 print_statement_list (s
->children
.head
, os
);
4999 fprintf (config
.map_file
, "END GROUP\n");
5002 /* Print the list of statements in S.
5003 This can be called for any statement type. */
5006 print_statement_list (lang_statement_union_type
*s
,
5007 lang_output_section_statement_type
*os
)
5011 print_statement (s
, os
);
5016 /* Print the first statement in statement list S.
5017 This can be called for any statement type. */
5020 print_statement (lang_statement_union_type
*s
,
5021 lang_output_section_statement_type
*os
)
5023 switch (s
->header
.type
)
5026 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
5029 case lang_constructors_statement_enum
:
5030 if (constructor_list
.head
!= NULL
)
5032 if (constructors_sorted
)
5033 minfo (" SORT (CONSTRUCTORS)\n");
5035 minfo (" CONSTRUCTORS\n");
5036 print_statement_list (constructor_list
.head
, os
);
5039 case lang_wild_statement_enum
:
5040 print_wild_statement (&s
->wild_statement
, os
);
5042 case lang_address_statement_enum
:
5043 print_address_statement (&s
->address_statement
);
5045 case lang_object_symbols_statement_enum
:
5046 minfo (" CREATE_OBJECT_SYMBOLS\n");
5048 case lang_fill_statement_enum
:
5049 print_fill_statement (&s
->fill_statement
);
5051 case lang_data_statement_enum
:
5052 print_data_statement (&s
->data_statement
);
5054 case lang_reloc_statement_enum
:
5055 print_reloc_statement (&s
->reloc_statement
);
5057 case lang_input_section_enum
:
5058 print_input_section (s
->input_section
.section
, false);
5060 case lang_padding_statement_enum
:
5061 print_padding_statement (&s
->padding_statement
);
5063 case lang_output_section_statement_enum
:
5064 print_output_section_statement (&s
->output_section_statement
);
5066 case lang_assignment_statement_enum
:
5067 print_assignment (&s
->assignment_statement
, os
);
5069 case lang_target_statement_enum
:
5070 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
5072 case lang_output_statement_enum
:
5073 minfo ("OUTPUT(%s", s
->output_statement
.name
);
5074 if (output_target
!= NULL
)
5075 minfo (" %s", output_target
);
5078 case lang_input_statement_enum
:
5079 print_input_statement (&s
->input_statement
);
5081 case lang_group_statement_enum
:
5082 print_group (&s
->group_statement
, os
);
5084 case lang_insert_statement_enum
:
5085 minfo ("INSERT %s %s\n",
5086 s
->insert_statement
.is_before
? "BEFORE" : "AFTER",
5087 s
->insert_statement
.where
);
5093 print_statements (void)
5095 print_statement_list (statement_list
.head
, abs_output_section
);
5098 /* Print the first N statements in statement list S to STDERR.
5099 If N == 0, nothing is printed.
5100 If N < 0, the entire list is printed.
5101 Intended to be called from GDB. */
5104 dprint_statement (lang_statement_union_type
*s
, int n
)
5106 FILE *map_save
= config
.map_file
;
5108 config
.map_file
= stderr
;
5111 print_statement_list (s
, abs_output_section
);
5114 while (s
&& --n
>= 0)
5116 print_statement (s
, abs_output_section
);
5121 config
.map_file
= map_save
;
5125 insert_pad (lang_statement_union_type
**ptr
,
5127 bfd_size_type alignment_needed
,
5128 asection
*output_section
,
5131 static fill_type zero_fill
;
5132 lang_statement_union_type
*pad
= NULL
;
5134 if (ptr
!= &statement_list
.head
)
5135 pad
= ((lang_statement_union_type
*)
5136 ((char *) ptr
- offsetof (lang_statement_union_type
, header
.next
)));
5138 && pad
->header
.type
== lang_padding_statement_enum
5139 && pad
->padding_statement
.output_section
== output_section
)
5141 /* Use the existing pad statement. */
5143 else if ((pad
= *ptr
) != NULL
5144 && pad
->header
.type
== lang_padding_statement_enum
5145 && pad
->padding_statement
.output_section
== output_section
)
5147 /* Use the existing pad statement. */
5151 /* Make a new padding statement, linked into existing chain. */
5152 pad
= stat_alloc (sizeof (lang_padding_statement_type
));
5153 pad
->header
.next
= *ptr
;
5155 pad
->header
.type
= lang_padding_statement_enum
;
5156 pad
->padding_statement
.output_section
= output_section
;
5159 pad
->padding_statement
.fill
= fill
;
5161 pad
->padding_statement
.output_offset
= dot
- output_section
->vma
;
5162 pad
->padding_statement
.size
= alignment_needed
;
5163 if (!(output_section
->flags
& SEC_FIXED_SIZE
))
5164 output_section
->size
= TO_SIZE (dot
+ TO_ADDR (alignment_needed
)
5165 - output_section
->vma
);
5168 /* Work out how much this section will move the dot point. */
5172 (lang_statement_union_type
**this_ptr
,
5173 lang_output_section_statement_type
*output_section_statement
,
5178 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
5179 asection
*i
= is
->section
;
5180 asection
*o
= output_section_statement
->bfd_section
;
5183 if (link_info
.non_contiguous_regions
)
5185 /* If the input section I has already been successfully assigned
5186 to an output section other than O, don't bother with it and
5187 let the caller remove it from the list. Keep processing in
5188 case we have already handled O, because the repeated passes
5189 have reinitialized its size. */
5190 if (i
->already_assigned
&& i
->already_assigned
!= o
)
5197 if (i
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
5198 i
->output_offset
= i
->vma
- o
->vma
;
5199 else if (((i
->flags
& SEC_EXCLUDE
) != 0)
5200 || output_section_statement
->ignored
)
5201 i
->output_offset
= dot
- o
->vma
;
5204 bfd_size_type alignment_needed
;
5206 /* Align this section first to the input sections requirement,
5207 then to the output section's requirement. If this alignment
5208 is greater than any seen before, then record it too. Perform
5209 the alignment by inserting a magic 'padding' statement. */
5211 if (output_section_statement
->subsection_alignment
!= NULL
)
5213 = exp_get_power (output_section_statement
->subsection_alignment
,
5214 "subsection alignment");
5216 if (o
->alignment_power
< i
->alignment_power
)
5217 o
->alignment_power
= i
->alignment_power
;
5219 alignment_needed
= align_power (dot
, i
->alignment_power
) - dot
;
5221 if (alignment_needed
!= 0)
5223 insert_pad (this_ptr
, fill
, TO_SIZE (alignment_needed
), o
, dot
);
5224 dot
+= alignment_needed
;
5227 if (link_info
.non_contiguous_regions
)
5229 /* If I would overflow O, let the caller remove I from the
5231 if (output_section_statement
->region
)
5233 bfd_vma end
= output_section_statement
->region
->origin
5234 + output_section_statement
->region
->length
;
5236 if (dot
+ TO_ADDR (i
->size
) > end
)
5238 if (i
->flags
& SEC_LINKER_CREATED
)
5239 einfo (_("%F%P: Output section '%s' not large enough for the "
5240 "linker-created stubs section '%s'.\n"),
5241 i
->output_section
->name
, i
->name
);
5243 if (i
->rawsize
&& i
->rawsize
!= i
->size
)
5244 einfo (_("%F%P: Relaxation not supported with "
5245 "--enable-non-contiguous-regions (section '%s' "
5246 "would overflow '%s' after it changed size).\n"),
5247 i
->name
, i
->output_section
->name
);
5251 i
->output_section
= NULL
;
5257 /* Remember where in the output section this input section goes. */
5258 i
->output_offset
= dot
- o
->vma
;
5260 /* Mark how big the output section must be to contain this now. */
5261 dot
+= TO_ADDR (i
->size
);
5262 if (!(o
->flags
& SEC_FIXED_SIZE
))
5263 o
->size
= TO_SIZE (dot
- o
->vma
);
5265 if (link_info
.non_contiguous_regions
)
5267 /* Record that I was successfully assigned to O, and update
5268 its actual output section too. */
5269 i
->already_assigned
= o
;
5270 i
->output_section
= o
;
5284 sort_sections_by_lma (const void *arg1
, const void *arg2
)
5286 const asection
*sec1
= ((const struct check_sec
*) arg1
)->sec
;
5287 const asection
*sec2
= ((const struct check_sec
*) arg2
)->sec
;
5289 if (sec1
->lma
< sec2
->lma
)
5291 else if (sec1
->lma
> sec2
->lma
)
5293 else if (sec1
->id
< sec2
->id
)
5295 else if (sec1
->id
> sec2
->id
)
5302 sort_sections_by_vma (const void *arg1
, const void *arg2
)
5304 const asection
*sec1
= ((const struct check_sec
*) arg1
)->sec
;
5305 const asection
*sec2
= ((const struct check_sec
*) arg2
)->sec
;
5307 if (sec1
->vma
< sec2
->vma
)
5309 else if (sec1
->vma
> sec2
->vma
)
5311 else if (sec1
->id
< sec2
->id
)
5313 else if (sec1
->id
> sec2
->id
)
5319 #define IS_TBSS(s) \
5320 ((s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == SEC_THREAD_LOCAL)
5322 #define IGNORE_SECTION(s) \
5323 ((s->flags & SEC_ALLOC) == 0 || IS_TBSS (s))
5325 /* Check to see if any allocated sections overlap with other allocated
5326 sections. This can happen if a linker script specifies the output
5327 section addresses of the two sections. Also check whether any memory
5328 region has overflowed. */
5331 lang_check_section_addresses (void)
5334 struct check_sec
*sections
;
5339 bfd_vma p_start
= 0;
5341 lang_memory_region_type
*m
;
5344 /* Detect address space overflow on allocated sections. */
5345 addr_mask
= ((bfd_vma
) 1 <<
5346 (bfd_arch_bits_per_address (link_info
.output_bfd
) - 1)) - 1;
5347 addr_mask
= (addr_mask
<< 1) + 1;
5348 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5349 if ((s
->flags
& SEC_ALLOC
) != 0)
5351 s_end
= (s
->vma
+ s
->size
) & addr_mask
;
5352 if (s_end
!= 0 && s_end
< (s
->vma
& addr_mask
))
5353 einfo (_("%X%P: section %s VMA wraps around address space\n"),
5357 s_end
= (s
->lma
+ s
->size
) & addr_mask
;
5358 if (s_end
!= 0 && s_end
< (s
->lma
& addr_mask
))
5359 einfo (_("%X%P: section %s LMA wraps around address space\n"),
5364 if (bfd_count_sections (link_info
.output_bfd
) <= 1)
5367 count
= bfd_count_sections (link_info
.output_bfd
);
5368 sections
= XNEWVEC (struct check_sec
, count
);
5370 /* Scan all sections in the output list. */
5372 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5374 if (IGNORE_SECTION (s
)
5378 sections
[count
].sec
= s
;
5379 sections
[count
].warned
= false;
5389 qsort (sections
, count
, sizeof (*sections
), sort_sections_by_lma
);
5391 /* First check section LMAs. There should be no overlap of LMAs on
5392 loadable sections, even with overlays. */
5393 for (p
= NULL
, i
= 0; i
< count
; i
++)
5395 s
= sections
[i
].sec
;
5397 if ((s
->flags
& SEC_LOAD
) != 0)
5400 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
5402 /* Look for an overlap. We have sorted sections by lma, so
5403 we know that s_start >= p_start. Besides the obvious
5404 case of overlap when the current section starts before
5405 the previous one ends, we also must have overlap if the
5406 previous section wraps around the address space. */
5408 && (s_start
<= p_end
5409 || p_end
< p_start
))
5411 einfo (_("%X%P: section %s LMA [%V,%V]"
5412 " overlaps section %s LMA [%V,%V]\n"),
5413 s
->name
, s_start
, s_end
, p
->name
, p_start
, p_end
);
5414 sections
[i
].warned
= true;
5422 /* If any non-zero size allocated section (excluding tbss) starts at
5423 exactly the same VMA as another such section, then we have
5424 overlays. Overlays generated by the OVERLAY keyword will have
5425 this property. It is possible to intentionally generate overlays
5426 that fail this test, but it would be unusual. */
5427 qsort (sections
, count
, sizeof (*sections
), sort_sections_by_vma
);
5429 p_start
= sections
[0].sec
->vma
;
5430 for (i
= 1; i
< count
; i
++)
5432 s_start
= sections
[i
].sec
->vma
;
5433 if (p_start
== s_start
)
5441 /* Now check section VMAs if no overlays were detected. */
5444 for (p
= NULL
, i
= 0; i
< count
; i
++)
5446 s
= sections
[i
].sec
;
5449 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
5452 && !sections
[i
].warned
5453 && (s_start
<= p_end
5454 || p_end
< p_start
))
5455 einfo (_("%X%P: section %s VMA [%V,%V]"
5456 " overlaps section %s VMA [%V,%V]\n"),
5457 s
->name
, s_start
, s_end
, p
->name
, p_start
, p_end
);
5466 /* If any memory region has overflowed, report by how much.
5467 We do not issue this diagnostic for regions that had sections
5468 explicitly placed outside their bounds; os_region_check's
5469 diagnostics are adequate for that case.
5471 FIXME: It is conceivable that m->current - (m->origin + m->length)
5472 might overflow a 32-bit integer. There is, alas, no way to print
5473 a bfd_vma quantity in decimal. */
5474 for (m
= lang_memory_region_list
; m
; m
= m
->next
)
5475 if (m
->had_full_message
)
5477 unsigned long over
= m
->current
- (m
->origin
+ m
->length
);
5478 einfo (ngettext ("%X%P: region `%s' overflowed by %lu byte\n",
5479 "%X%P: region `%s' overflowed by %lu bytes\n",
5481 m
->name_list
.name
, over
);
5485 /* Make sure the new address is within the region. We explicitly permit the
5486 current address to be at the exact end of the region when the address is
5487 non-zero, in case the region is at the end of addressable memory and the
5488 calculation wraps around. */
5491 os_region_check (lang_output_section_statement_type
*os
,
5492 lang_memory_region_type
*region
,
5496 if ((region
->current
< region
->origin
5497 || (region
->current
- region
->origin
> region
->length
))
5498 && ((region
->current
!= region
->origin
+ region
->length
)
5503 einfo (_("%X%P: address 0x%v of %pB section `%s'"
5504 " is not within region `%s'\n"),
5506 os
->bfd_section
->owner
,
5507 os
->bfd_section
->name
,
5508 region
->name_list
.name
);
5510 else if (!region
->had_full_message
)
5512 region
->had_full_message
= true;
5514 einfo (_("%X%P: %pB section `%s' will not fit in region `%s'\n"),
5515 os
->bfd_section
->owner
,
5516 os
->bfd_section
->name
,
5517 region
->name_list
.name
);
5523 ldlang_check_relro_region (lang_statement_union_type
*s
)
5525 seg_align_type
*seg
= &expld
.dataseg
;
5527 if (seg
->relro
== exp_seg_relro_start
)
5529 if (!seg
->relro_start_stat
)
5530 seg
->relro_start_stat
= s
;
5533 ASSERT (seg
->relro_start_stat
== s
);
5536 else if (seg
->relro
== exp_seg_relro_end
)
5538 if (!seg
->relro_end_stat
)
5539 seg
->relro_end_stat
= s
;
5542 ASSERT (seg
->relro_end_stat
== s
);
5547 /* Set the sizes for all the output sections. */
5550 lang_size_sections_1
5551 (lang_statement_union_type
**prev
,
5552 lang_output_section_statement_type
*output_section_statement
,
5558 lang_statement_union_type
*s
;
5559 lang_statement_union_type
*prev_s
= NULL
;
5560 bool removed_prev_s
= false;
5562 /* Size up the sections from their constituent parts. */
5563 for (s
= *prev
; s
!= NULL
; prev_s
= s
, s
= s
->header
.next
)
5565 bool removed
= false;
5567 switch (s
->header
.type
)
5569 case lang_output_section_statement_enum
:
5571 bfd_vma newdot
, after
, dotdelta
;
5572 lang_output_section_statement_type
*os
;
5573 lang_memory_region_type
*r
;
5574 int section_alignment
= 0;
5576 os
= &s
->output_section_statement
;
5577 init_opb (os
->bfd_section
);
5578 if (os
->constraint
== -1)
5581 /* FIXME: We shouldn't need to zero section vmas for ld -r
5582 here, in lang_insert_orphan, or in the default linker scripts.
5583 This is covering for coff backend linker bugs. See PR6945. */
5584 if (os
->addr_tree
== NULL
5585 && bfd_link_relocatable (&link_info
)
5586 && (bfd_get_flavour (link_info
.output_bfd
)
5587 == bfd_target_coff_flavour
))
5588 os
->addr_tree
= exp_intop (0);
5589 if (os
->addr_tree
!= NULL
)
5591 exp_fold_tree (os
->addr_tree
, bfd_abs_section_ptr
, &dot
);
5593 if (expld
.result
.valid_p
)
5595 dot
= expld
.result
.value
;
5596 if (expld
.result
.section
!= NULL
)
5597 dot
+= expld
.result
.section
->vma
;
5599 else if (expld
.phase
!= lang_mark_phase_enum
)
5600 einfo (_("%F%P:%pS: non constant or forward reference"
5601 " address expression for section %s\n"),
5602 os
->addr_tree
, os
->name
);
5605 if (os
->bfd_section
== NULL
)
5606 /* This section was removed or never actually created. */
5609 /* If this is a COFF shared library section, use the size and
5610 address from the input section. FIXME: This is COFF
5611 specific; it would be cleaner if there were some other way
5612 to do this, but nothing simple comes to mind. */
5613 if (((bfd_get_flavour (link_info
.output_bfd
)
5614 == bfd_target_ecoff_flavour
)
5615 || (bfd_get_flavour (link_info
.output_bfd
)
5616 == bfd_target_coff_flavour
))
5617 && (os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
5621 if (os
->children
.head
== NULL
5622 || os
->children
.head
->header
.next
!= NULL
5623 || (os
->children
.head
->header
.type
5624 != lang_input_section_enum
))
5625 einfo (_("%X%P: internal error on COFF shared library"
5626 " section %s\n"), os
->name
);
5628 input
= os
->children
.head
->input_section
.section
;
5629 bfd_set_section_vma (os
->bfd_section
,
5630 bfd_section_vma (input
));
5631 if (!(os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
5632 os
->bfd_section
->size
= input
->size
;
5638 if (bfd_is_abs_section (os
->bfd_section
))
5640 /* No matter what happens, an abs section starts at zero. */
5641 ASSERT (os
->bfd_section
->vma
== 0);
5645 if (os
->addr_tree
== NULL
)
5647 /* No address specified for this section, get one
5648 from the region specification. */
5649 if (os
->region
== NULL
5650 || ((os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))
5651 && os
->region
->name_list
.name
[0] == '*'
5652 && strcmp (os
->region
->name_list
.name
,
5653 DEFAULT_MEMORY_REGION
) == 0))
5655 os
->region
= lang_memory_default (os
->bfd_section
);
5658 /* If a loadable section is using the default memory
5659 region, and some non default memory regions were
5660 defined, issue an error message. */
5662 && !IGNORE_SECTION (os
->bfd_section
)
5663 && !bfd_link_relocatable (&link_info
)
5665 && strcmp (os
->region
->name_list
.name
,
5666 DEFAULT_MEMORY_REGION
) == 0
5667 && lang_memory_region_list
!= NULL
5668 && (strcmp (lang_memory_region_list
->name_list
.name
,
5669 DEFAULT_MEMORY_REGION
) != 0
5670 || lang_memory_region_list
->next
!= NULL
)
5671 && lang_sizing_iteration
== 1)
5673 /* By default this is an error rather than just a
5674 warning because if we allocate the section to the
5675 default memory region we can end up creating an
5676 excessively large binary, or even seg faulting when
5677 attempting to perform a negative seek. See
5678 sources.redhat.com/ml/binutils/2003-04/msg00423.html
5679 for an example of this. This behaviour can be
5680 overridden by the using the --no-check-sections
5682 if (command_line
.check_section_addresses
)
5683 einfo (_("%F%P: error: no memory region specified"
5684 " for loadable section `%s'\n"),
5685 bfd_section_name (os
->bfd_section
));
5687 einfo (_("%P: warning: no memory region specified"
5688 " for loadable section `%s'\n"),
5689 bfd_section_name (os
->bfd_section
));
5692 newdot
= os
->region
->current
;
5693 section_alignment
= os
->bfd_section
->alignment_power
;
5696 section_alignment
= exp_get_power (os
->section_alignment
,
5697 "section alignment");
5699 /* Align to what the section needs. */
5700 if (section_alignment
> 0)
5702 bfd_vma savedot
= newdot
;
5705 newdot
= align_power (newdot
, section_alignment
);
5706 dotdelta
= newdot
- savedot
;
5708 if (lang_sizing_iteration
== 1)
5710 else if (lang_sizing_iteration
> 1)
5712 /* Only report adjustments that would change
5713 alignment from what we have already reported. */
5714 diff
= newdot
- os
->bfd_section
->vma
;
5715 if (!(diff
& (((bfd_vma
) 1 << section_alignment
) - 1)))
5719 && (config
.warn_section_align
5720 || os
->addr_tree
!= NULL
))
5721 einfo (_("%P: warning: "
5722 "start of section %s changed by %ld\n"),
5723 os
->name
, (long) diff
);
5726 bfd_set_section_vma (os
->bfd_section
, newdot
);
5728 os
->bfd_section
->output_offset
= 0;
5731 lang_size_sections_1 (&os
->children
.head
, os
,
5732 os
->fill
, newdot
, relax
, check_regions
);
5734 os
->processed_vma
= true;
5736 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
5737 /* Except for some special linker created sections,
5738 no output section should change from zero size
5739 after strip_excluded_output_sections. A non-zero
5740 size on an ignored section indicates that some
5741 input section was not sized early enough. */
5742 ASSERT (os
->bfd_section
->size
== 0);
5745 dot
= os
->bfd_section
->vma
;
5747 /* Put the section within the requested block size, or
5748 align at the block boundary. */
5750 + TO_ADDR (os
->bfd_section
->size
)
5751 + os
->block_value
- 1)
5752 & - (bfd_vma
) os
->block_value
);
5754 if (!(os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
5755 os
->bfd_section
->size
= TO_SIZE (after
5756 - os
->bfd_section
->vma
);
5759 /* Set section lma. */
5762 r
= lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, false);
5766 bfd_vma lma
= exp_get_abs_int (os
->load_base
, 0, "load base");
5767 os
->bfd_section
->lma
= lma
;
5769 else if (os
->lma_region
!= NULL
)
5771 bfd_vma lma
= os
->lma_region
->current
;
5773 if (os
->align_lma_with_input
)
5777 /* When LMA_REGION is the same as REGION, align the LMA
5778 as we did for the VMA, possibly including alignment
5779 from the bfd section. If a different region, then
5780 only align according to the value in the output
5782 if (os
->lma_region
!= os
->region
)
5783 section_alignment
= exp_get_power (os
->section_alignment
,
5784 "section alignment");
5785 if (section_alignment
> 0)
5786 lma
= align_power (lma
, section_alignment
);
5788 os
->bfd_section
->lma
= lma
;
5790 else if (r
->last_os
!= NULL
5791 && (os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
5796 last
= r
->last_os
->output_section_statement
.bfd_section
;
5798 /* A backwards move of dot should be accompanied by
5799 an explicit assignment to the section LMA (ie.
5800 os->load_base set) because backwards moves can
5801 create overlapping LMAs. */
5803 && os
->bfd_section
->size
!= 0
5804 && dot
+ TO_ADDR (os
->bfd_section
->size
) <= last
->vma
)
5806 /* If dot moved backwards then leave lma equal to
5807 vma. This is the old default lma, which might
5808 just happen to work when the backwards move is
5809 sufficiently large. Nag if this changes anything,
5810 so people can fix their linker scripts. */
5812 if (last
->vma
!= last
->lma
)
5813 einfo (_("%P: warning: dot moved backwards "
5814 "before `%s'\n"), os
->name
);
5818 /* If this is an overlay, set the current lma to that
5819 at the end of the previous section. */
5820 if (os
->sectype
== overlay_section
)
5821 lma
= last
->lma
+ TO_ADDR (last
->size
);
5823 /* Otherwise, keep the same lma to vma relationship
5824 as the previous section. */
5826 lma
= os
->bfd_section
->vma
+ last
->lma
- last
->vma
;
5828 if (section_alignment
> 0)
5829 lma
= align_power (lma
, section_alignment
);
5830 os
->bfd_section
->lma
= lma
;
5833 os
->processed_lma
= true;
5835 /* Keep track of normal sections using the default
5836 lma region. We use this to set the lma for
5837 following sections. Overlays or other linker
5838 script assignment to lma might mean that the
5839 default lma == vma is incorrect.
5840 To avoid warnings about dot moving backwards when using
5841 -Ttext, don't start tracking sections until we find one
5842 of non-zero size or with lma set differently to vma.
5843 Do this tracking before we short-cut the loop so that we
5844 track changes for the case where the section size is zero,
5845 but the lma is set differently to the vma. This is
5846 important, if an orphan section is placed after an
5847 otherwise empty output section that has an explicit lma
5848 set, we want that lma reflected in the orphans lma. */
5849 if (((!IGNORE_SECTION (os
->bfd_section
)
5850 && (os
->bfd_section
->size
!= 0
5851 || (r
->last_os
== NULL
5852 && os
->bfd_section
->vma
!= os
->bfd_section
->lma
)
5853 || (r
->last_os
!= NULL
5854 && dot
>= (r
->last_os
->output_section_statement
5855 .bfd_section
->vma
))))
5856 || os
->sectype
== first_overlay_section
)
5857 && os
->lma_region
== NULL
5858 && !bfd_link_relocatable (&link_info
))
5861 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
5864 /* .tbss sections effectively have zero size. */
5865 if (!IS_TBSS (os
->bfd_section
)
5866 || bfd_link_relocatable (&link_info
))
5867 dotdelta
= TO_ADDR (os
->bfd_section
->size
);
5872 if (os
->update_dot_tree
!= 0)
5873 exp_fold_tree (os
->update_dot_tree
, bfd_abs_section_ptr
, &dot
);
5875 /* Update dot in the region ?
5876 We only do this if the section is going to be allocated,
5877 since unallocated sections do not contribute to the region's
5878 overall size in memory. */
5879 if (os
->region
!= NULL
5880 && (os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
)))
5882 os
->region
->current
= dot
;
5885 /* Make sure the new address is within the region. */
5886 os_region_check (os
, os
->region
, os
->addr_tree
,
5887 os
->bfd_section
->vma
);
5889 if (os
->lma_region
!= NULL
&& os
->lma_region
!= os
->region
5890 && ((os
->bfd_section
->flags
& SEC_LOAD
)
5891 || os
->align_lma_with_input
))
5893 os
->lma_region
->current
= os
->bfd_section
->lma
+ dotdelta
;
5896 os_region_check (os
, os
->lma_region
, NULL
,
5897 os
->bfd_section
->lma
);
5903 case lang_constructors_statement_enum
:
5904 dot
= lang_size_sections_1 (&constructor_list
.head
,
5905 output_section_statement
,
5906 fill
, dot
, relax
, check_regions
);
5909 case lang_data_statement_enum
:
5911 unsigned int size
= 0;
5913 s
->data_statement
.output_offset
=
5914 dot
- output_section_statement
->bfd_section
->vma
;
5915 s
->data_statement
.output_section
=
5916 output_section_statement
->bfd_section
;
5918 /* We might refer to provided symbols in the expression, and
5919 need to mark them as needed. */
5920 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
5922 switch (s
->data_statement
.type
)
5940 if (size
< TO_SIZE ((unsigned) 1))
5941 size
= TO_SIZE ((unsigned) 1);
5942 dot
+= TO_ADDR (size
);
5943 if (!(output_section_statement
->bfd_section
->flags
5945 output_section_statement
->bfd_section
->size
5946 = TO_SIZE (dot
- output_section_statement
->bfd_section
->vma
);
5951 case lang_reloc_statement_enum
:
5955 s
->reloc_statement
.output_offset
=
5956 dot
- output_section_statement
->bfd_section
->vma
;
5957 s
->reloc_statement
.output_section
=
5958 output_section_statement
->bfd_section
;
5959 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
5960 dot
+= TO_ADDR (size
);
5961 if (!(output_section_statement
->bfd_section
->flags
5963 output_section_statement
->bfd_section
->size
5964 = TO_SIZE (dot
- output_section_statement
->bfd_section
->vma
);
5968 case lang_wild_statement_enum
:
5969 dot
= lang_size_sections_1 (&s
->wild_statement
.children
.head
,
5970 output_section_statement
,
5971 fill
, dot
, relax
, check_regions
);
5974 case lang_object_symbols_statement_enum
:
5975 link_info
.create_object_symbols_section
5976 = output_section_statement
->bfd_section
;
5977 output_section_statement
->bfd_section
->flags
|= SEC_KEEP
;
5980 case lang_output_statement_enum
:
5981 case lang_target_statement_enum
:
5984 case lang_input_section_enum
:
5988 i
= s
->input_section
.section
;
5993 if (!bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
5994 einfo (_("%F%P: can't relax section: %E\n"));
5998 dot
= size_input_section (prev
, output_section_statement
,
5999 fill
, &removed
, dot
);
6003 case lang_input_statement_enum
:
6006 case lang_fill_statement_enum
:
6007 s
->fill_statement
.output_section
=
6008 output_section_statement
->bfd_section
;
6010 fill
= s
->fill_statement
.fill
;
6013 case lang_assignment_statement_enum
:
6015 bfd_vma newdot
= dot
;
6016 etree_type
*tree
= s
->assignment_statement
.exp
;
6018 expld
.dataseg
.relro
= exp_seg_relro_none
;
6020 exp_fold_tree (tree
,
6021 output_section_statement
->bfd_section
,
6024 ldlang_check_relro_region (s
);
6026 expld
.dataseg
.relro
= exp_seg_relro_none
;
6028 /* This symbol may be relative to this section. */
6029 if ((tree
->type
.node_class
== etree_provided
6030 || tree
->type
.node_class
== etree_assign
)
6031 && (tree
->assign
.dst
[0] != '.'
6032 || tree
->assign
.dst
[1] != '\0'))
6033 output_section_statement
->update_dot
= 1;
6035 if (!output_section_statement
->ignored
)
6037 if (output_section_statement
== abs_output_section
)
6039 /* If we don't have an output section, then just adjust
6040 the default memory address. */
6041 lang_memory_region_lookup (DEFAULT_MEMORY_REGION
,
6042 false)->current
= newdot
;
6044 else if (newdot
!= dot
)
6046 /* Insert a pad after this statement. We can't
6047 put the pad before when relaxing, in case the
6048 assignment references dot. */
6049 insert_pad (&s
->header
.next
, fill
, TO_SIZE (newdot
- dot
),
6050 output_section_statement
->bfd_section
, dot
);
6052 /* Don't neuter the pad below when relaxing. */
6055 /* If dot is advanced, this implies that the section
6056 should have space allocated to it, unless the
6057 user has explicitly stated that the section
6058 should not be allocated. */
6059 if (output_section_statement
->sectype
!= noalloc_section
6060 && (output_section_statement
->sectype
!= noload_section
6061 || (bfd_get_flavour (link_info
.output_bfd
)
6062 == bfd_target_elf_flavour
)))
6063 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
;
6070 case lang_padding_statement_enum
:
6071 /* If this is the first time lang_size_sections is called,
6072 we won't have any padding statements. If this is the
6073 second or later passes when relaxing, we should allow
6074 padding to shrink. If padding is needed on this pass, it
6075 will be added back in. */
6076 s
->padding_statement
.size
= 0;
6078 /* Make sure output_offset is valid. If relaxation shrinks
6079 the section and this pad isn't needed, it's possible to
6080 have output_offset larger than the final size of the
6081 section. bfd_set_section_contents will complain even for
6082 a pad size of zero. */
6083 s
->padding_statement
.output_offset
6084 = dot
- output_section_statement
->bfd_section
->vma
;
6087 case lang_group_statement_enum
:
6088 dot
= lang_size_sections_1 (&s
->group_statement
.children
.head
,
6089 output_section_statement
,
6090 fill
, dot
, relax
, check_regions
);
6093 case lang_insert_statement_enum
:
6096 /* We can only get here when relaxing is turned on. */
6097 case lang_address_statement_enum
:
6105 /* If an input section doesn't fit in the current output
6106 section, remove it from the list. Handle the case where we
6107 have to remove an input_section statement here: there is a
6108 special case to remove the first element of the list. */
6109 if (link_info
.non_contiguous_regions
&& removed
)
6111 /* If we removed the first element during the previous
6112 iteration, override the loop assignment of prev_s. */
6118 /* If there was a real previous input section, just skip
6120 prev_s
->header
.next
=s
->header
.next
;
6122 removed_prev_s
= false;
6126 /* Remove the first input section of the list. */
6127 *prev
= s
->header
.next
;
6128 removed_prev_s
= true;
6131 /* Move to next element, unless we removed the head of the
6133 if (!removed_prev_s
)
6134 prev
= &s
->header
.next
;
6138 prev
= &s
->header
.next
;
6139 removed_prev_s
= false;
6145 /* Callback routine that is used in _bfd_elf_map_sections_to_segments.
6146 The BFD library has set NEW_SEGMENT to TRUE iff it thinks that
6147 CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different
6148 segments. We are allowed an opportunity to override this decision. */
6151 ldlang_override_segment_assignment (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6152 bfd
*abfd ATTRIBUTE_UNUSED
,
6153 asection
*current_section
,
6154 asection
*previous_section
,
6157 lang_output_section_statement_type
*cur
;
6158 lang_output_section_statement_type
*prev
;
6160 /* The checks below are only necessary when the BFD library has decided
6161 that the two sections ought to be placed into the same segment. */
6165 /* Paranoia checks. */
6166 if (current_section
== NULL
|| previous_section
== NULL
)
6169 /* If this flag is set, the target never wants code and non-code
6170 sections comingled in the same segment. */
6171 if (config
.separate_code
6172 && ((current_section
->flags
^ previous_section
->flags
) & SEC_CODE
))
6175 /* Find the memory regions associated with the two sections.
6176 We call lang_output_section_find() here rather than scanning the list
6177 of output sections looking for a matching section pointer because if
6178 we have a large number of sections then a hash lookup is faster. */
6179 cur
= lang_output_section_find (current_section
->name
);
6180 prev
= lang_output_section_find (previous_section
->name
);
6182 /* More paranoia. */
6183 if (cur
== NULL
|| prev
== NULL
)
6186 /* If the regions are different then force the sections to live in
6187 different segments. See the email thread starting at the following
6188 URL for the reasons why this is necessary:
6189 http://sourceware.org/ml/binutils/2007-02/msg00216.html */
6190 return cur
->region
!= prev
->region
;
6194 one_lang_size_sections_pass (bool *relax
, bool check_regions
)
6196 lang_statement_iteration
++;
6197 if (expld
.phase
!= lang_mark_phase_enum
)
6198 lang_sizing_iteration
++;
6199 lang_size_sections_1 (&statement_list
.head
, abs_output_section
,
6200 0, 0, relax
, check_regions
);
6204 lang_size_segment (void)
6206 /* If XXX_SEGMENT_ALIGN XXX_SEGMENT_END pair was seen, check whether
6207 a page could be saved in the data segment. */
6208 seg_align_type
*seg
= &expld
.dataseg
;
6209 bfd_vma first
, last
;
6211 first
= -seg
->base
& (seg
->commonpagesize
- 1);
6212 last
= seg
->end
& (seg
->commonpagesize
- 1);
6214 && ((seg
->base
& ~(seg
->commonpagesize
- 1))
6215 != (seg
->end
& ~(seg
->commonpagesize
- 1)))
6216 && first
+ last
<= seg
->commonpagesize
)
6218 seg
->phase
= exp_seg_adjust
;
6222 seg
->phase
= exp_seg_done
;
6227 lang_size_relro_segment_1 (void)
6229 seg_align_type
*seg
= &expld
.dataseg
;
6230 bfd_vma relro_end
, desired_end
;
6233 /* Compute the expected PT_GNU_RELRO/PT_LOAD segment end. */
6234 relro_end
= (seg
->relro_end
+ seg
->relropagesize
- 1) & -seg
->relropagesize
;
6236 /* Adjust by the offset arg of XXX_SEGMENT_RELRO_END. */
6237 desired_end
= relro_end
- seg
->relro_offset
;
6239 /* For sections in the relro segment.. */
6240 for (sec
= link_info
.output_bfd
->section_last
; sec
; sec
= sec
->prev
)
6241 if ((sec
->flags
& SEC_ALLOC
) != 0
6242 && sec
->vma
>= seg
->base
6243 && sec
->vma
< seg
->relro_end
- seg
->relro_offset
)
6245 /* Where do we want to put this section so that it ends as
6247 bfd_vma start
, end
, bump
;
6249 end
= start
= sec
->vma
;
6251 end
+= TO_ADDR (sec
->size
);
6252 bump
= desired_end
- end
;
6253 /* We'd like to increase START by BUMP, but we must heed
6254 alignment so the increase might be less than optimum. */
6256 start
&= ~(((bfd_vma
) 1 << sec
->alignment_power
) - 1);
6257 /* This is now the desired end for the previous section. */
6258 desired_end
= start
;
6261 seg
->phase
= exp_seg_relro_adjust
;
6262 ASSERT (desired_end
>= seg
->base
);
6263 seg
->base
= desired_end
;
6268 lang_size_relro_segment (bool *relax
, bool check_regions
)
6270 bool do_reset
= false;
6272 if (link_info
.relro
&& expld
.dataseg
.relro_end
)
6274 bfd_vma data_initial_base
= expld
.dataseg
.base
;
6275 bfd_vma data_relro_end
= lang_size_relro_segment_1 ();
6277 lang_reset_memory_regions ();
6278 one_lang_size_sections_pass (relax
, check_regions
);
6280 /* Assignments to dot, or to output section address in a user
6281 script have increased padding over the original. Revert. */
6282 if (expld
.dataseg
.relro_end
> data_relro_end
)
6284 expld
.dataseg
.base
= data_initial_base
;
6288 else if (lang_size_segment ())
6295 lang_size_sections (bool *relax
, bool check_regions
)
6297 expld
.phase
= lang_allocating_phase_enum
;
6298 expld
.dataseg
.phase
= exp_seg_none
;
6300 one_lang_size_sections_pass (relax
, check_regions
);
6302 if (expld
.dataseg
.phase
!= exp_seg_end_seen
)
6303 expld
.dataseg
.phase
= exp_seg_done
;
6305 if (expld
.dataseg
.phase
== exp_seg_end_seen
)
6308 = lang_size_relro_segment (relax
, check_regions
);
6312 lang_reset_memory_regions ();
6313 one_lang_size_sections_pass (relax
, check_regions
);
6316 if (link_info
.relro
&& expld
.dataseg
.relro_end
)
6318 link_info
.relro_start
= expld
.dataseg
.base
;
6319 link_info
.relro_end
= expld
.dataseg
.relro_end
;
6324 static lang_output_section_statement_type
*current_section
;
6325 static lang_assignment_statement_type
*current_assign
;
6326 static bool prefer_next_section
;
6328 /* Worker function for lang_do_assignments. Recursiveness goes here. */
6331 lang_do_assignments_1 (lang_statement_union_type
*s
,
6332 lang_output_section_statement_type
*current_os
,
6337 for (; s
!= NULL
; s
= s
->header
.next
)
6339 switch (s
->header
.type
)
6341 case lang_constructors_statement_enum
:
6342 dot
= lang_do_assignments_1 (constructor_list
.head
,
6343 current_os
, fill
, dot
, found_end
);
6346 case lang_output_section_statement_enum
:
6348 lang_output_section_statement_type
*os
;
6351 os
= &(s
->output_section_statement
);
6352 os
->after_end
= *found_end
;
6353 init_opb (os
->bfd_section
);
6355 if (os
->bfd_section
!= NULL
)
6357 if (!os
->ignored
&& (os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
6359 current_section
= os
;
6360 prefer_next_section
= false;
6362 newdot
= os
->bfd_section
->vma
;
6364 newdot
= lang_do_assignments_1 (os
->children
.head
,
6365 os
, os
->fill
, newdot
, found_end
);
6368 if (os
->bfd_section
!= NULL
)
6370 newdot
= os
->bfd_section
->vma
;
6372 /* .tbss sections effectively have zero size. */
6373 if (!IS_TBSS (os
->bfd_section
)
6374 || bfd_link_relocatable (&link_info
))
6375 newdot
+= TO_ADDR (os
->bfd_section
->size
);
6377 if (os
->update_dot_tree
!= NULL
)
6378 exp_fold_tree (os
->update_dot_tree
,
6379 bfd_abs_section_ptr
, &newdot
);
6386 case lang_wild_statement_enum
:
6388 dot
= lang_do_assignments_1 (s
->wild_statement
.children
.head
,
6389 current_os
, fill
, dot
, found_end
);
6392 case lang_object_symbols_statement_enum
:
6393 case lang_output_statement_enum
:
6394 case lang_target_statement_enum
:
6397 case lang_data_statement_enum
:
6398 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
6399 if (expld
.result
.valid_p
)
6401 s
->data_statement
.value
= expld
.result
.value
;
6402 if (expld
.result
.section
!= NULL
)
6403 s
->data_statement
.value
+= expld
.result
.section
->vma
;
6405 else if (expld
.phase
== lang_final_phase_enum
)
6406 einfo (_("%F%P: invalid data statement\n"));
6409 switch (s
->data_statement
.type
)
6427 if (size
< TO_SIZE ((unsigned) 1))
6428 size
= TO_SIZE ((unsigned) 1);
6429 dot
+= TO_ADDR (size
);
6433 case lang_reloc_statement_enum
:
6434 exp_fold_tree (s
->reloc_statement
.addend_exp
,
6435 bfd_abs_section_ptr
, &dot
);
6436 if (expld
.result
.valid_p
)
6437 s
->reloc_statement
.addend_value
= expld
.result
.value
;
6438 else if (expld
.phase
== lang_final_phase_enum
)
6439 einfo (_("%F%P: invalid reloc statement\n"));
6440 dot
+= TO_ADDR (bfd_get_reloc_size (s
->reloc_statement
.howto
));
6443 case lang_input_section_enum
:
6445 asection
*in
= s
->input_section
.section
;
6447 if ((in
->flags
& SEC_EXCLUDE
) == 0)
6448 dot
+= TO_ADDR (in
->size
);
6452 case lang_input_statement_enum
:
6455 case lang_fill_statement_enum
:
6456 fill
= s
->fill_statement
.fill
;
6459 case lang_assignment_statement_enum
:
6460 current_assign
= &s
->assignment_statement
;
6461 if (current_assign
->exp
->type
.node_class
!= etree_assert
)
6463 const char *p
= current_assign
->exp
->assign
.dst
;
6465 if (current_os
== abs_output_section
&& p
[0] == '.' && p
[1] == 0)
6466 prefer_next_section
= true;
6470 if (strcmp (p
, "end") == 0)
6473 exp_fold_tree (s
->assignment_statement
.exp
,
6474 (current_os
->bfd_section
!= NULL
6475 ? current_os
->bfd_section
: bfd_und_section_ptr
),
6479 case lang_padding_statement_enum
:
6480 dot
+= TO_ADDR (s
->padding_statement
.size
);
6483 case lang_group_statement_enum
:
6484 dot
= lang_do_assignments_1 (s
->group_statement
.children
.head
,
6485 current_os
, fill
, dot
, found_end
);
6488 case lang_insert_statement_enum
:
6491 case lang_address_statement_enum
:
6503 lang_do_assignments (lang_phase_type phase
)
6505 bool found_end
= false;
6507 current_section
= NULL
;
6508 prefer_next_section
= false;
6509 expld
.phase
= phase
;
6510 lang_statement_iteration
++;
6511 lang_do_assignments_1 (statement_list
.head
,
6512 abs_output_section
, NULL
, 0, &found_end
);
6515 /* For an assignment statement outside of an output section statement,
6516 choose the best of neighbouring output sections to use for values
6520 section_for_dot (void)
6524 /* Assignments belong to the previous output section, unless there
6525 has been an assignment to "dot", in which case following
6526 assignments belong to the next output section. (The assumption
6527 is that an assignment to "dot" is setting up the address for the
6528 next output section.) Except that past the assignment to "_end"
6529 we always associate with the previous section. This exception is
6530 for targets like SH that define an alloc .stack or other
6531 weirdness after non-alloc sections. */
6532 if (current_section
== NULL
|| prefer_next_section
)
6534 lang_statement_union_type
*stmt
;
6535 lang_output_section_statement_type
*os
;
6537 for (stmt
= (lang_statement_union_type
*) current_assign
;
6539 stmt
= stmt
->header
.next
)
6540 if (stmt
->header
.type
== lang_output_section_statement_enum
)
6543 os
= stmt
? &stmt
->output_section_statement
: NULL
;
6546 && (os
->bfd_section
== NULL
6547 || (os
->bfd_section
->flags
& SEC_EXCLUDE
) != 0
6548 || bfd_section_removed_from_list (link_info
.output_bfd
,
6552 if (current_section
== NULL
|| os
== NULL
|| !os
->after_end
)
6555 s
= os
->bfd_section
;
6557 s
= link_info
.output_bfd
->section_last
;
6559 && ((s
->flags
& SEC_ALLOC
) == 0
6560 || (s
->flags
& SEC_THREAD_LOCAL
) != 0))
6565 return bfd_abs_section_ptr
;
6569 s
= current_section
->bfd_section
;
6571 /* The section may have been stripped. */
6573 && ((s
->flags
& SEC_EXCLUDE
) != 0
6574 || (s
->flags
& SEC_ALLOC
) == 0
6575 || (s
->flags
& SEC_THREAD_LOCAL
) != 0
6576 || bfd_section_removed_from_list (link_info
.output_bfd
, s
)))
6579 s
= link_info
.output_bfd
->sections
;
6581 && ((s
->flags
& SEC_ALLOC
) == 0
6582 || (s
->flags
& SEC_THREAD_LOCAL
) != 0))
6587 return bfd_abs_section_ptr
;
6590 /* Array of __start/__stop/.startof./.sizeof/ symbols. */
6592 static struct bfd_link_hash_entry
**start_stop_syms
;
6593 static size_t start_stop_count
= 0;
6594 static size_t start_stop_alloc
= 0;
6596 /* Give start/stop SYMBOL for SEC a preliminary definition, and add it
6597 to start_stop_syms. */
6600 lang_define_start_stop (const char *symbol
, asection
*sec
)
6602 struct bfd_link_hash_entry
*h
;
6604 h
= bfd_define_start_stop (link_info
.output_bfd
, &link_info
, symbol
, sec
);
6607 if (start_stop_count
== start_stop_alloc
)
6609 start_stop_alloc
= 2 * start_stop_alloc
+ 10;
6611 = xrealloc (start_stop_syms
,
6612 start_stop_alloc
* sizeof (*start_stop_syms
));
6614 start_stop_syms
[start_stop_count
++] = h
;
6618 /* Check for input sections whose names match references to
6619 __start_SECNAME or __stop_SECNAME symbols. Give the symbols
6620 preliminary definitions. */
6623 lang_init_start_stop (void)
6627 char leading_char
= bfd_get_symbol_leading_char (link_info
.output_bfd
);
6629 for (abfd
= link_info
.input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
6630 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
6633 const char *secname
= s
->name
;
6635 for (ps
= secname
; *ps
!= '\0'; ps
++)
6636 if (!ISALNUM ((unsigned char) *ps
) && *ps
!= '_')
6640 char *symbol
= (char *) xmalloc (10 + strlen (secname
));
6642 symbol
[0] = leading_char
;
6643 sprintf (symbol
+ (leading_char
!= 0), "__start_%s", secname
);
6644 lang_define_start_stop (symbol
, s
);
6646 symbol
[1] = leading_char
;
6647 memcpy (symbol
+ 1 + (leading_char
!= 0), "__stop", 6);
6648 lang_define_start_stop (symbol
+ 1, s
);
6655 /* Iterate over start_stop_syms. */
6658 foreach_start_stop (void (*func
) (struct bfd_link_hash_entry
*))
6662 for (i
= 0; i
< start_stop_count
; ++i
)
6663 func (start_stop_syms
[i
]);
6666 /* __start and __stop symbols are only supposed to be defined by the
6667 linker for orphan sections, but we now extend that to sections that
6668 map to an output section of the same name. The symbols were
6669 defined early for --gc-sections, before we mapped input to output
6670 sections, so undo those that don't satisfy this rule. */
6673 undef_start_stop (struct bfd_link_hash_entry
*h
)
6675 if (h
->ldscript_def
)
6678 if (h
->u
.def
.section
->output_section
== NULL
6679 || h
->u
.def
.section
->output_section
->owner
!= link_info
.output_bfd
6680 || strcmp (h
->u
.def
.section
->name
,
6681 h
->u
.def
.section
->output_section
->name
) != 0)
6683 asection
*sec
= bfd_get_section_by_name (link_info
.output_bfd
,
6684 h
->u
.def
.section
->name
);
6687 /* When there are more than one input sections with the same
6688 section name, SECNAME, linker picks the first one to define
6689 __start_SECNAME and __stop_SECNAME symbols. When the first
6690 input section is removed by comdat group, we need to check
6691 if there is still an output section with section name
6694 for (i
= sec
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
6695 if (strcmp (h
->u
.def
.section
->name
, i
->name
) == 0)
6697 h
->u
.def
.section
= i
;
6701 h
->type
= bfd_link_hash_undefined
;
6702 h
->u
.undef
.abfd
= NULL
;
6703 if (is_elf_hash_table (link_info
.hash
))
6705 const struct elf_backend_data
*bed
;
6706 struct elf_link_hash_entry
*eh
= (struct elf_link_hash_entry
*) h
;
6707 unsigned int was_forced
= eh
->forced_local
;
6709 bed
= get_elf_backend_data (link_info
.output_bfd
);
6710 (*bed
->elf_backend_hide_symbol
) (&link_info
, eh
, true);
6711 if (!eh
->ref_regular_nonweak
)
6712 h
->type
= bfd_link_hash_undefweak
;
6713 eh
->def_regular
= 0;
6714 eh
->forced_local
= was_forced
;
6720 lang_undef_start_stop (void)
6722 foreach_start_stop (undef_start_stop
);
6725 /* Check for output sections whose names match references to
6726 .startof.SECNAME or .sizeof.SECNAME symbols. Give the symbols
6727 preliminary definitions. */
6730 lang_init_startof_sizeof (void)
6734 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6736 const char *secname
= s
->name
;
6737 char *symbol
= (char *) xmalloc (10 + strlen (secname
));
6739 sprintf (symbol
, ".startof.%s", secname
);
6740 lang_define_start_stop (symbol
, s
);
6742 memcpy (symbol
+ 1, ".size", 5);
6743 lang_define_start_stop (symbol
+ 1, s
);
6748 /* Set .startof., .sizeof., __start and __stop symbols final values. */
6751 set_start_stop (struct bfd_link_hash_entry
*h
)
6754 || h
->type
!= bfd_link_hash_defined
)
6757 if (h
->root
.string
[0] == '.')
6759 /* .startof. or .sizeof. symbol.
6760 .startof. already has final value. */
6761 if (h
->root
.string
[2] == 'i')
6764 h
->u
.def
.value
= TO_ADDR (h
->u
.def
.section
->size
);
6765 h
->u
.def
.section
= bfd_abs_section_ptr
;
6770 /* __start or __stop symbol. */
6771 int has_lead
= bfd_get_symbol_leading_char (link_info
.output_bfd
) != 0;
6773 h
->u
.def
.section
= h
->u
.def
.section
->output_section
;
6774 if (h
->root
.string
[4 + has_lead
] == 'o')
6777 h
->u
.def
.value
= TO_ADDR (h
->u
.def
.section
->size
);
6783 lang_finalize_start_stop (void)
6785 foreach_start_stop (set_start_stop
);
6789 lang_symbol_tweaks (void)
6791 /* Give initial values for __start and __stop symbols, so that ELF
6792 gc_sections will keep sections referenced by these symbols. Must
6793 be done before lang_do_assignments. */
6794 if (config
.build_constructors
)
6795 lang_init_start_stop ();
6797 /* Make __ehdr_start hidden, and set def_regular even though it is
6798 likely undefined at this stage. For lang_check_relocs. */
6799 if (is_elf_hash_table (link_info
.hash
)
6800 && !bfd_link_relocatable (&link_info
))
6802 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*)
6803 bfd_link_hash_lookup (link_info
.hash
, "__ehdr_start",
6804 false, false, true);
6806 /* Only adjust the export class if the symbol was referenced
6807 and not defined, otherwise leave it alone. */
6809 && (h
->root
.type
== bfd_link_hash_new
6810 || h
->root
.type
== bfd_link_hash_undefined
6811 || h
->root
.type
== bfd_link_hash_undefweak
6812 || h
->root
.type
== bfd_link_hash_common
))
6814 const struct elf_backend_data
*bed
;
6815 bed
= get_elf_backend_data (link_info
.output_bfd
);
6816 (*bed
->elf_backend_hide_symbol
) (&link_info
, h
, true);
6817 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
6818 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
6820 h
->root
.linker_def
= 1;
6821 h
->root
.rel_from_abs
= 1;
6829 struct bfd_link_hash_entry
*h
;
6832 if ((bfd_link_relocatable (&link_info
) && !link_info
.gc_sections
)
6833 || bfd_link_dll (&link_info
))
6834 warn
= entry_from_cmdline
;
6838 /* Force the user to specify a root when generating a relocatable with
6839 --gc-sections, unless --gc-keep-exported was also given. */
6840 if (bfd_link_relocatable (&link_info
)
6841 && link_info
.gc_sections
6842 && !link_info
.gc_keep_exported
)
6844 struct bfd_sym_chain
*sym
;
6846 for (sym
= link_info
.gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
6848 h
= bfd_link_hash_lookup (link_info
.hash
, sym
->name
,
6849 false, false, false);
6851 && (h
->type
== bfd_link_hash_defined
6852 || h
->type
== bfd_link_hash_defweak
)
6853 && !bfd_is_const_section (h
->u
.def
.section
))
6857 einfo (_("%F%P: --gc-sections requires a defined symbol root "
6858 "specified by -e or -u\n"));
6861 if (entry_symbol
.name
== NULL
)
6863 /* No entry has been specified. Look for the default entry, but
6864 don't warn if we don't find it. */
6865 entry_symbol
.name
= entry_symbol_default
;
6869 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
.name
,
6870 false, false, true);
6872 && (h
->type
== bfd_link_hash_defined
6873 || h
->type
== bfd_link_hash_defweak
)
6874 && h
->u
.def
.section
->output_section
!= NULL
)
6878 val
= (h
->u
.def
.value
6879 + bfd_section_vma (h
->u
.def
.section
->output_section
)
6880 + h
->u
.def
.section
->output_offset
);
6881 if (!bfd_set_start_address (link_info
.output_bfd
, val
))
6882 einfo (_("%F%P: %s: can't set start address\n"), entry_symbol
.name
);
6889 /* We couldn't find the entry symbol. Try parsing it as a
6891 val
= bfd_scan_vma (entry_symbol
.name
, &send
, 0);
6894 if (!bfd_set_start_address (link_info
.output_bfd
, val
))
6895 einfo (_("%F%P: can't set start address\n"));
6897 /* BZ 2004952: Only use the start of the entry section for executables. */
6898 else if bfd_link_executable (&link_info
)
6902 /* Can't find the entry symbol, and it's not a number. Use
6903 the first address in the text section. */
6904 ts
= bfd_get_section_by_name (link_info
.output_bfd
, entry_section
);
6908 einfo (_("%P: warning: cannot find entry symbol %s;"
6909 " defaulting to %V\n"),
6911 bfd_section_vma (ts
));
6912 if (!bfd_set_start_address (link_info
.output_bfd
,
6913 bfd_section_vma (ts
)))
6914 einfo (_("%F%P: can't set start address\n"));
6919 einfo (_("%P: warning: cannot find entry symbol %s;"
6920 " not setting start address\n"),
6927 einfo (_("%P: warning: cannot find entry symbol %s;"
6928 " not setting start address\n"),
6934 /* This is a small function used when we want to ignore errors from
6938 ignore_bfd_errors (const char *fmt ATTRIBUTE_UNUSED
,
6939 va_list ap ATTRIBUTE_UNUSED
)
6941 /* Don't do anything. */
6944 /* Check that the architecture of all the input files is compatible
6945 with the output file. Also call the backend to let it do any
6946 other checking that is needed. */
6951 lang_input_statement_type
*file
;
6953 const bfd_arch_info_type
*compatible
;
6955 for (file
= (void *) file_chain
.head
;
6959 #if BFD_SUPPORTS_PLUGINS
6960 /* Don't check format of files claimed by plugin. */
6961 if (file
->flags
.claimed
)
6963 #endif /* BFD_SUPPORTS_PLUGINS */
6964 input_bfd
= file
->the_bfd
;
6966 = bfd_arch_get_compatible (input_bfd
, link_info
.output_bfd
,
6967 command_line
.accept_unknown_input_arch
);
6969 /* In general it is not possible to perform a relocatable
6970 link between differing object formats when the input
6971 file has relocations, because the relocations in the
6972 input format may not have equivalent representations in
6973 the output format (and besides BFD does not translate
6974 relocs for other link purposes than a final link). */
6975 if (!file
->flags
.just_syms
6976 && (bfd_link_relocatable (&link_info
)
6977 || link_info
.emitrelocations
)
6978 && (compatible
== NULL
6979 || (bfd_get_flavour (input_bfd
)
6980 != bfd_get_flavour (link_info
.output_bfd
)))
6981 && (bfd_get_file_flags (input_bfd
) & HAS_RELOC
) != 0)
6983 einfo (_("%F%P: relocatable linking with relocations from"
6984 " format %s (%pB) to format %s (%pB) is not supported\n"),
6985 bfd_get_target (input_bfd
), input_bfd
,
6986 bfd_get_target (link_info
.output_bfd
), link_info
.output_bfd
);
6987 /* einfo with %F exits. */
6990 if (compatible
== NULL
)
6992 if (command_line
.warn_mismatch
)
6993 einfo (_("%X%P: %s architecture of input file `%pB'"
6994 " is incompatible with %s output\n"),
6995 bfd_printable_name (input_bfd
), input_bfd
,
6996 bfd_printable_name (link_info
.output_bfd
));
6999 /* If the input bfd has no contents, it shouldn't set the
7000 private data of the output bfd. */
7001 else if (!file
->flags
.just_syms
7002 && ((input_bfd
->flags
& DYNAMIC
) != 0
7003 || bfd_count_sections (input_bfd
) != 0))
7005 bfd_error_handler_type pfn
= NULL
;
7007 /* If we aren't supposed to warn about mismatched input
7008 files, temporarily set the BFD error handler to a
7009 function which will do nothing. We still want to call
7010 bfd_merge_private_bfd_data, since it may set up
7011 information which is needed in the output file. */
7012 if (!command_line
.warn_mismatch
)
7013 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
7014 if (!bfd_merge_private_bfd_data (input_bfd
, &link_info
))
7016 if (command_line
.warn_mismatch
)
7017 einfo (_("%X%P: failed to merge target specific data"
7018 " of file %pB\n"), input_bfd
);
7020 if (!command_line
.warn_mismatch
)
7021 bfd_set_error_handler (pfn
);
7026 /* Look through all the global common symbols and attach them to the
7027 correct section. The -sort-common command line switch may be used
7028 to roughly sort the entries by alignment. */
7033 if (link_info
.inhibit_common_definition
)
7035 if (bfd_link_relocatable (&link_info
)
7036 && !command_line
.force_common_definition
)
7039 if (!config
.sort_common
)
7040 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, NULL
);
7045 if (config
.sort_common
== sort_descending
)
7047 for (power
= 4; power
> 0; power
--)
7048 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7051 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7055 for (power
= 0; power
<= 4; power
++)
7056 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7058 power
= (unsigned int) -1;
7059 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7064 /* Place one common symbol in the correct section. */
7067 lang_one_common (struct bfd_link_hash_entry
*h
, void *info
)
7069 unsigned int power_of_two
;
7073 if (h
->type
!= bfd_link_hash_common
)
7077 power_of_two
= h
->u
.c
.p
->alignment_power
;
7079 if (config
.sort_common
== sort_descending
7080 && power_of_two
< *(unsigned int *) info
)
7082 else if (config
.sort_common
== sort_ascending
7083 && power_of_two
> *(unsigned int *) info
)
7086 section
= h
->u
.c
.p
->section
;
7087 if (!bfd_define_common_symbol (link_info
.output_bfd
, &link_info
, h
))
7088 einfo (_("%F%P: could not define common symbol `%pT': %E\n"),
7091 if (config
.map_file
!= NULL
)
7093 static bool header_printed
;
7098 if (!header_printed
)
7100 minfo (_("\nAllocating common symbols\n"));
7101 minfo (_("Common symbol size file\n\n"));
7102 header_printed
= true;
7105 name
= bfd_demangle (link_info
.output_bfd
, h
->root
.string
,
7106 DMGL_ANSI
| DMGL_PARAMS
);
7109 minfo ("%s", h
->root
.string
);
7110 len
= strlen (h
->root
.string
);
7115 len
= strlen (name
);
7125 sprintf (buf
, "%" PRIx64
, (uint64_t) size
);
7126 fprintf (config
.map_file
, "%*s0x%-16s", 20 - len
, "", buf
);
7128 minfo ("%pB\n", section
->owner
);
7134 /* Handle a single orphan section S, placing the orphan into an appropriate
7135 output section. The effects of the --orphan-handling command line
7136 option are handled here. */
7139 ldlang_place_orphan (asection
*s
)
7141 if (config
.orphan_handling
== orphan_handling_discard
)
7143 lang_output_section_statement_type
*os
;
7144 os
= lang_output_section_statement_lookup (DISCARD_SECTION_NAME
, 0, 1);
7145 if (os
->addr_tree
== NULL
7146 && (bfd_link_relocatable (&link_info
)
7147 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0))
7148 os
->addr_tree
= exp_intop (0);
7149 lang_add_section (&os
->children
, s
, NULL
, NULL
, os
);
7153 lang_output_section_statement_type
*os
;
7154 const char *name
= s
->name
;
7157 if (config
.orphan_handling
== orphan_handling_error
)
7158 einfo (_("%X%P: error: unplaced orphan section `%pA' from `%pB'\n"),
7161 if (config
.unique_orphan_sections
|| unique_section_p (s
, NULL
))
7162 constraint
= SPECIAL
;
7164 os
= ldemul_place_orphan (s
, name
, constraint
);
7167 os
= lang_output_section_statement_lookup (name
, constraint
, 1);
7168 if (os
->addr_tree
== NULL
7169 && (bfd_link_relocatable (&link_info
)
7170 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0))
7171 os
->addr_tree
= exp_intop (0);
7172 lang_add_section (&os
->children
, s
, NULL
, NULL
, os
);
7175 if (config
.orphan_handling
== orphan_handling_warn
)
7176 einfo (_("%P: warning: orphan section `%pA' from `%pB' being "
7177 "placed in section `%s'\n"),
7178 s
, s
->owner
, os
->name
);
7182 /* Run through the input files and ensure that every input section has
7183 somewhere to go. If one is found without a destination then create
7184 an input request and place it into the statement tree. */
7187 lang_place_orphans (void)
7189 LANG_FOR_EACH_INPUT_STATEMENT (file
)
7193 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7195 if (s
->output_section
== NULL
)
7197 /* This section of the file is not attached, root
7198 around for a sensible place for it to go. */
7200 if (file
->flags
.just_syms
)
7201 bfd_link_just_syms (file
->the_bfd
, s
, &link_info
);
7202 else if (lang_discard_section_p (s
))
7203 s
->output_section
= bfd_abs_section_ptr
;
7204 else if (strcmp (s
->name
, "COMMON") == 0)
7206 /* This is a lonely common section which must have
7207 come from an archive. We attach to the section
7208 with the wildcard. */
7209 if (!bfd_link_relocatable (&link_info
)
7210 || command_line
.force_common_definition
)
7212 if (default_common_section
== NULL
)
7213 default_common_section
7214 = lang_output_section_statement_lookup (".bss", 0, 1);
7215 lang_add_section (&default_common_section
->children
, s
,
7216 NULL
, NULL
, default_common_section
);
7220 ldlang_place_orphan (s
);
7227 lang_set_flags (lang_memory_region_type
*ptr
, const char *flags
, int invert
)
7229 flagword
*ptr_flags
;
7231 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
7237 /* PR 17900: An exclamation mark in the attributes reverses
7238 the sense of any of the attributes that follow. */
7241 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
7245 *ptr_flags
|= SEC_ALLOC
;
7249 *ptr_flags
|= SEC_READONLY
;
7253 *ptr_flags
|= SEC_DATA
;
7257 *ptr_flags
|= SEC_CODE
;
7262 *ptr_flags
|= SEC_LOAD
;
7266 einfo (_("%F%P: invalid character %c (%d) in flags\n"),
7274 /* Call a function on each real input file. This function will be
7275 called on an archive, but not on the elements. */
7278 lang_for_each_input_file (void (*func
) (lang_input_statement_type
*))
7280 lang_input_statement_type
*f
;
7282 for (f
= (void *) input_file_chain
.head
;
7284 f
= f
->next_real_file
)
7289 /* Call a function on each real file. The function will be called on
7290 all the elements of an archive which are included in the link, but
7291 will not be called on the archive file itself. */
7294 lang_for_each_file (void (*func
) (lang_input_statement_type
*))
7296 LANG_FOR_EACH_INPUT_STATEMENT (f
)
7304 ldlang_add_file (lang_input_statement_type
*entry
)
7306 lang_statement_append (&file_chain
, entry
, &entry
->next
);
7308 /* The BFD linker needs to have a list of all input BFDs involved in
7310 ASSERT (link_info
.input_bfds_tail
!= &entry
->the_bfd
->link
.next
7311 && entry
->the_bfd
->link
.next
== NULL
);
7312 ASSERT (entry
->the_bfd
!= link_info
.output_bfd
);
7314 *link_info
.input_bfds_tail
= entry
->the_bfd
;
7315 link_info
.input_bfds_tail
= &entry
->the_bfd
->link
.next
;
7316 bfd_set_usrdata (entry
->the_bfd
, entry
);
7317 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
7319 /* Look through the sections and check for any which should not be
7320 included in the link. We need to do this now, so that we can
7321 notice when the backend linker tries to report multiple
7322 definition errors for symbols which are in sections we aren't
7323 going to link. FIXME: It might be better to entirely ignore
7324 symbols which are defined in sections which are going to be
7325 discarded. This would require modifying the backend linker for
7326 each backend which might set the SEC_LINK_ONCE flag. If we do
7327 this, we should probably handle SEC_EXCLUDE in the same way. */
7329 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, entry
);
7333 lang_add_output (const char *name
, int from_script
)
7335 /* Make -o on command line override OUTPUT in script. */
7336 if (!had_output_filename
|| !from_script
)
7338 output_filename
= name
;
7339 had_output_filename
= true;
7343 lang_output_section_statement_type
*
7344 lang_enter_output_section_statement (const char *output_section_statement_name
,
7345 etree_type
*address_exp
,
7346 enum section_type sectype
,
7347 etree_type
*sectype_value
,
7349 etree_type
*subalign
,
7352 int align_with_input
)
7354 lang_output_section_statement_type
*os
;
7356 os
= lang_output_section_statement_lookup (output_section_statement_name
,
7358 current_section
= os
;
7360 if (os
->addr_tree
== NULL
)
7362 os
->addr_tree
= address_exp
;
7364 os
->sectype
= sectype
;
7365 if (sectype
== type_section
|| sectype
== typed_readonly_section
)
7366 os
->sectype_value
= sectype_value
;
7367 else if (sectype
== noload_section
)
7368 os
->flags
= SEC_NEVER_LOAD
;
7370 os
->flags
= SEC_NO_FLAGS
;
7371 os
->block_value
= 1;
7373 /* Make next things chain into subchain of this. */
7374 push_stat_ptr (&os
->children
);
7376 os
->align_lma_with_input
= align_with_input
== ALIGN_WITH_INPUT
;
7377 if (os
->align_lma_with_input
&& align
!= NULL
)
7378 einfo (_("%F%P:%pS: error: align with input and explicit align specified\n"),
7381 os
->subsection_alignment
= subalign
;
7382 os
->section_alignment
= align
;
7384 os
->load_base
= ebase
;
7391 lang_output_statement_type
*new_stmt
;
7393 new_stmt
= new_stat (lang_output_statement
, stat_ptr
);
7394 new_stmt
->name
= output_filename
;
7397 /* Reset the current counters in the regions. */
7400 lang_reset_memory_regions (void)
7402 lang_memory_region_type
*p
= lang_memory_region_list
;
7404 lang_output_section_statement_type
*os
;
7406 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
7408 p
->current
= p
->origin
;
7412 for (os
= (void *) lang_os_list
.head
;
7416 os
->processed_vma
= false;
7417 os
->processed_lma
= false;
7420 for (o
= link_info
.output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
7422 /* Save the last size for possible use by bfd_relax_section. */
7423 o
->rawsize
= o
->size
;
7424 if (!(o
->flags
& SEC_FIXED_SIZE
))
7429 /* Worker for lang_gc_sections_1. */
7432 gc_section_callback (lang_wild_statement_type
*ptr
,
7433 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
7435 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
7436 void *data ATTRIBUTE_UNUSED
)
7438 /* If the wild pattern was marked KEEP, the member sections
7439 should be as well. */
7440 if (ptr
->keep_sections
)
7441 section
->flags
|= SEC_KEEP
;
7444 /* Iterate over sections marking them against GC. */
7447 lang_gc_sections_1 (lang_statement_union_type
*s
)
7449 for (; s
!= NULL
; s
= s
->header
.next
)
7451 switch (s
->header
.type
)
7453 case lang_wild_statement_enum
:
7454 walk_wild (&s
->wild_statement
, gc_section_callback
, NULL
);
7456 case lang_constructors_statement_enum
:
7457 lang_gc_sections_1 (constructor_list
.head
);
7459 case lang_output_section_statement_enum
:
7460 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
7462 case lang_group_statement_enum
:
7463 lang_gc_sections_1 (s
->group_statement
.children
.head
);
7472 lang_gc_sections (void)
7474 /* Keep all sections so marked in the link script. */
7475 lang_gc_sections_1 (statement_list
.head
);
7477 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
7478 the special case of .stabstr debug info. (See bfd/stabs.c)
7479 Twiddle the flag here, to simplify later linker code. */
7480 if (bfd_link_relocatable (&link_info
))
7482 LANG_FOR_EACH_INPUT_STATEMENT (f
)
7485 #if BFD_SUPPORTS_PLUGINS
7486 if (f
->flags
.claimed
)
7489 for (sec
= f
->the_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7490 if ((sec
->flags
& SEC_DEBUGGING
) == 0
7491 || strcmp (sec
->name
, ".stabstr") != 0)
7492 sec
->flags
&= ~SEC_EXCLUDE
;
7496 if (link_info
.gc_sections
)
7497 bfd_gc_sections (link_info
.output_bfd
, &link_info
);
7500 /* Worker for lang_find_relro_sections_1. */
7503 find_relro_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
7504 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
7506 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
7509 /* Discarded, excluded and ignored sections effectively have zero
7511 if (section
->output_section
!= NULL
7512 && section
->output_section
->owner
== link_info
.output_bfd
7513 && (section
->output_section
->flags
& SEC_EXCLUDE
) == 0
7514 && !IGNORE_SECTION (section
)
7515 && section
->size
!= 0)
7517 bool *has_relro_section
= (bool *) data
;
7518 *has_relro_section
= true;
7522 /* Iterate over sections for relro sections. */
7525 lang_find_relro_sections_1 (lang_statement_union_type
*s
,
7526 bool *has_relro_section
)
7528 if (*has_relro_section
)
7531 for (; s
!= NULL
; s
= s
->header
.next
)
7533 if (s
== expld
.dataseg
.relro_end_stat
)
7536 switch (s
->header
.type
)
7538 case lang_wild_statement_enum
:
7539 walk_wild (&s
->wild_statement
,
7540 find_relro_section_callback
,
7543 case lang_constructors_statement_enum
:
7544 lang_find_relro_sections_1 (constructor_list
.head
,
7547 case lang_output_section_statement_enum
:
7548 lang_find_relro_sections_1 (s
->output_section_statement
.children
.head
,
7551 case lang_group_statement_enum
:
7552 lang_find_relro_sections_1 (s
->group_statement
.children
.head
,
7562 lang_find_relro_sections (void)
7564 bool has_relro_section
= false;
7566 /* Check all sections in the link script. */
7568 lang_find_relro_sections_1 (expld
.dataseg
.relro_start_stat
,
7569 &has_relro_section
);
7571 if (!has_relro_section
)
7572 link_info
.relro
= false;
7575 /* Relax all sections until bfd_relax_section gives up. */
7578 lang_relax_sections (bool need_layout
)
7580 /* NB: Also enable relaxation to layout sections for DT_RELR. */
7581 if (RELAXATION_ENABLED
|| link_info
.enable_dt_relr
)
7583 /* We may need more than one relaxation pass. */
7584 int i
= link_info
.relax_pass
;
7586 /* The backend can use it to determine the current pass. */
7587 link_info
.relax_pass
= 0;
7591 /* Keep relaxing until bfd_relax_section gives up. */
7594 link_info
.relax_trip
= -1;
7597 link_info
.relax_trip
++;
7599 /* Note: pe-dll.c does something like this also. If you find
7600 you need to change this code, you probably need to change
7601 pe-dll.c also. DJ */
7603 /* Do all the assignments with our current guesses as to
7605 lang_do_assignments (lang_assigning_phase_enum
);
7607 /* We must do this after lang_do_assignments, because it uses
7609 lang_reset_memory_regions ();
7611 /* Perform another relax pass - this time we know where the
7612 globals are, so can make a better guess. */
7613 relax_again
= false;
7614 lang_size_sections (&relax_again
, false);
7616 while (relax_again
);
7618 link_info
.relax_pass
++;
7625 /* Final extra sizing to report errors. */
7626 lang_do_assignments (lang_assigning_phase_enum
);
7627 lang_reset_memory_regions ();
7628 lang_size_sections (NULL
, true);
7632 #if BFD_SUPPORTS_PLUGINS
7633 /* Find the insert point for the plugin's replacement files. We
7634 place them after the first claimed real object file, or if the
7635 first claimed object is an archive member, after the last real
7636 object file immediately preceding the archive. In the event
7637 no objects have been claimed at all, we return the first dummy
7638 object file on the list as the insert point; that works, but
7639 the callee must be careful when relinking the file_chain as it
7640 is not actually on that chain, only the statement_list and the
7641 input_file list; in that case, the replacement files must be
7642 inserted at the head of the file_chain. */
7644 static lang_input_statement_type
*
7645 find_replacements_insert_point (bool *before
)
7647 lang_input_statement_type
*claim1
, *lastobject
;
7648 lastobject
= (void *) input_file_chain
.head
;
7649 for (claim1
= (void *) file_chain
.head
;
7651 claim1
= claim1
->next
)
7653 if (claim1
->flags
.claimed
)
7655 *before
= claim1
->flags
.claim_archive
;
7656 return claim1
->flags
.claim_archive
? lastobject
: claim1
;
7658 /* Update lastobject if this is a real object file. */
7659 if (claim1
->the_bfd
!= NULL
&& claim1
->the_bfd
->my_archive
== NULL
)
7660 lastobject
= claim1
;
7662 /* No files were claimed by the plugin. Choose the last object
7663 file found on the list (maybe the first, dummy entry) as the
7669 /* Find where to insert ADD, an archive element or shared library
7670 added during a rescan. */
7672 static lang_input_statement_type
**
7673 find_rescan_insertion (lang_input_statement_type
*add
)
7675 bfd
*add_bfd
= add
->the_bfd
;
7676 lang_input_statement_type
*f
;
7677 lang_input_statement_type
*last_loaded
= NULL
;
7678 lang_input_statement_type
*before
= NULL
;
7679 lang_input_statement_type
**iter
= NULL
;
7681 if (add_bfd
->my_archive
!= NULL
)
7682 add_bfd
= add_bfd
->my_archive
;
7684 /* First look through the input file chain, to find an object file
7685 before the one we've rescanned. Normal object files always
7686 appear on both the input file chain and the file chain, so this
7687 lets us get quickly to somewhere near the correct place on the
7688 file chain if it is full of archive elements. Archives don't
7689 appear on the file chain, but if an element has been extracted
7690 then their input_statement->next points at it. */
7691 for (f
= (void *) input_file_chain
.head
;
7693 f
= f
->next_real_file
)
7695 if (f
->the_bfd
== add_bfd
)
7697 before
= last_loaded
;
7698 if (f
->next
!= NULL
)
7699 return &f
->next
->next
;
7701 if (f
->the_bfd
!= NULL
&& f
->next
!= NULL
)
7705 for (iter
= before
? &before
->next
: &file_chain
.head
->input_statement
.next
;
7707 iter
= &(*iter
)->next
)
7708 if (!(*iter
)->flags
.claim_archive
7709 && (*iter
)->the_bfd
->my_archive
== NULL
)
7715 /* Insert SRCLIST into DESTLIST after given element by chaining
7716 on FIELD as the next-pointer. (Counterintuitively does not need
7717 a pointer to the actual after-node itself, just its chain field.) */
7720 lang_list_insert_after (lang_statement_list_type
*destlist
,
7721 lang_statement_list_type
*srclist
,
7722 lang_statement_union_type
**field
)
7724 *(srclist
->tail
) = *field
;
7725 *field
= srclist
->head
;
7726 if (destlist
->tail
== field
)
7727 destlist
->tail
= srclist
->tail
;
7730 /* Detach new nodes added to DESTLIST since the time ORIGLIST
7731 was taken as a copy of it and leave them in ORIGLIST. */
7734 lang_list_remove_tail (lang_statement_list_type
*destlist
,
7735 lang_statement_list_type
*origlist
)
7737 union lang_statement_union
**savetail
;
7738 /* Check that ORIGLIST really is an earlier state of DESTLIST. */
7739 ASSERT (origlist
->head
== destlist
->head
);
7740 savetail
= origlist
->tail
;
7741 origlist
->head
= *(savetail
);
7742 origlist
->tail
= destlist
->tail
;
7743 destlist
->tail
= savetail
;
7747 static lang_statement_union_type
**
7748 find_next_input_statement (lang_statement_union_type
**s
)
7750 for ( ; *s
; s
= &(*s
)->header
.next
)
7752 lang_statement_union_type
**t
;
7753 switch ((*s
)->header
.type
)
7755 case lang_input_statement_enum
:
7757 case lang_wild_statement_enum
:
7758 t
= &(*s
)->wild_statement
.children
.head
;
7760 case lang_group_statement_enum
:
7761 t
= &(*s
)->group_statement
.children
.head
;
7763 case lang_output_section_statement_enum
:
7764 t
= &(*s
)->output_section_statement
.children
.head
;
7769 t
= find_next_input_statement (t
);
7775 #endif /* BFD_SUPPORTS_PLUGINS */
7777 /* Add NAME to the list of garbage collection entry points. */
7780 lang_add_gc_name (const char *name
)
7782 struct bfd_sym_chain
*sym
;
7787 sym
= stat_alloc (sizeof (*sym
));
7789 sym
->next
= link_info
.gc_sym_list
;
7791 link_info
.gc_sym_list
= sym
;
7794 /* Check relocations. */
7797 lang_check_relocs (void)
7799 if (link_info
.check_relocs_after_open_input
)
7803 for (abfd
= link_info
.input_bfds
;
7804 abfd
!= (bfd
*) NULL
; abfd
= abfd
->link
.next
)
7805 if (!bfd_link_check_relocs (abfd
, &link_info
))
7807 /* No object output, fail return. */
7808 config
.make_executable
= false;
7809 /* Note: we do not abort the loop, but rather
7810 continue the scan in case there are other
7811 bad relocations to report. */
7816 /* Look through all output sections looking for places where we can
7817 propagate forward the lma region. */
7820 lang_propagate_lma_regions (void)
7822 lang_output_section_statement_type
*os
;
7824 for (os
= (void *) lang_os_list
.head
;
7828 if (os
->prev
!= NULL
7829 && os
->lma_region
== NULL
7830 && os
->load_base
== NULL
7831 && os
->addr_tree
== NULL
7832 && os
->region
== os
->prev
->region
)
7833 os
->lma_region
= os
->prev
->lma_region
;
7838 reset_one_wild (lang_statement_union_type
*statement
)
7840 if (statement
->header
.type
== lang_wild_statement_enum
)
7842 lang_wild_statement_type
*stmt
= &statement
->wild_statement
;
7843 lang_list_init (&stmt
->matching_sections
);
7848 reset_resolved_wilds (void)
7850 lang_for_each_statement (reset_one_wild
);
7856 /* Finalize dynamic list. */
7857 if (link_info
.dynamic_list
)
7858 lang_finalize_version_expr_head (&link_info
.dynamic_list
->head
);
7860 current_target
= default_target
;
7862 /* Open the output file. */
7863 lang_for_each_statement (ldlang_open_output
);
7866 ldemul_create_output_section_statements ();
7868 /* Add to the hash table all undefineds on the command line. */
7869 lang_place_undefineds ();
7871 if (!bfd_section_already_linked_table_init ())
7872 einfo (_("%F%P: can not create hash table: %E\n"));
7874 /* A first pass through the memory regions ensures that if any region
7875 references a symbol for its origin or length then this symbol will be
7876 added to the symbol table. Having these symbols in the symbol table
7877 means that when we call open_input_bfds PROVIDE statements will
7878 trigger to provide any needed symbols. The regions origins and
7879 lengths are not assigned as a result of this call. */
7880 lang_do_memory_regions (false);
7882 /* Create a bfd for each input file. */
7883 current_target
= default_target
;
7884 lang_statement_iteration
++;
7885 open_input_bfds (statement_list
.head
, OPEN_BFD_NORMAL
);
7887 /* Now that open_input_bfds has processed assignments and provide
7888 statements we can give values to symbolic origin/length now. */
7889 lang_do_memory_regions (true);
7891 #if BFD_SUPPORTS_PLUGINS
7892 if (link_info
.lto_plugin_active
)
7894 lang_statement_list_type added
;
7895 lang_statement_list_type files
, inputfiles
;
7897 ldemul_before_plugin_all_symbols_read ();
7899 /* Now all files are read, let the plugin(s) decide if there
7900 are any more to be added to the link before we call the
7901 emulation's after_open hook. We create a private list of
7902 input statements for this purpose, which we will eventually
7903 insert into the global statement list after the first claimed
7906 /* We need to manipulate all three chains in synchrony. */
7908 inputfiles
= input_file_chain
;
7909 if (plugin_call_all_symbols_read ())
7910 einfo (_("%F%P: %s: plugin reported error after all symbols read\n"),
7911 plugin_error_plugin ());
7912 link_info
.lto_all_symbols_read
= true;
7913 /* Open any newly added files, updating the file chains. */
7914 plugin_undefs
= link_info
.hash
->undefs_tail
;
7915 open_input_bfds (*added
.tail
, OPEN_BFD_NORMAL
);
7916 if (plugin_undefs
== link_info
.hash
->undefs_tail
)
7917 plugin_undefs
= NULL
;
7918 /* Restore the global list pointer now they have all been added. */
7919 lang_list_remove_tail (stat_ptr
, &added
);
7920 /* And detach the fresh ends of the file lists. */
7921 lang_list_remove_tail (&file_chain
, &files
);
7922 lang_list_remove_tail (&input_file_chain
, &inputfiles
);
7923 /* Were any new files added? */
7924 if (added
.head
!= NULL
)
7926 /* If so, we will insert them into the statement list immediately
7927 after the first input file that was claimed by the plugin,
7928 unless that file was an archive in which case it is inserted
7929 immediately before. */
7931 lang_statement_union_type
**prev
;
7932 plugin_insert
= find_replacements_insert_point (&before
);
7933 /* If a plugin adds input files without having claimed any, we
7934 don't really have a good idea where to place them. Just putting
7935 them at the start or end of the list is liable to leave them
7936 outside the crtbegin...crtend range. */
7937 ASSERT (plugin_insert
!= NULL
);
7938 /* Splice the new statement list into the old one. */
7939 prev
= &plugin_insert
->header
.next
;
7942 prev
= find_next_input_statement (prev
);
7943 if (*prev
!= (void *) plugin_insert
->next_real_file
)
7945 /* We didn't find the expected input statement.
7946 Fall back to adding after plugin_insert. */
7947 prev
= &plugin_insert
->header
.next
;
7950 lang_list_insert_after (stat_ptr
, &added
, prev
);
7951 /* Likewise for the file chains. */
7952 lang_list_insert_after (&input_file_chain
, &inputfiles
,
7953 (void *) &plugin_insert
->next_real_file
);
7954 /* We must be careful when relinking file_chain; we may need to
7955 insert the new files at the head of the list if the insert
7956 point chosen is the dummy first input file. */
7957 if (plugin_insert
->filename
)
7958 lang_list_insert_after (&file_chain
, &files
,
7959 (void *) &plugin_insert
->next
);
7961 lang_list_insert_after (&file_chain
, &files
, &file_chain
.head
);
7963 /* Rescan archives in case new undefined symbols have appeared. */
7965 lang_statement_iteration
++;
7966 open_input_bfds (statement_list
.head
, OPEN_BFD_RESCAN
);
7967 lang_list_remove_tail (&file_chain
, &files
);
7968 while (files
.head
!= NULL
)
7970 lang_input_statement_type
**insert
;
7971 lang_input_statement_type
**iter
, *temp
;
7974 insert
= find_rescan_insertion (&files
.head
->input_statement
);
7975 /* All elements from an archive can be added at once. */
7976 iter
= &files
.head
->input_statement
.next
;
7977 my_arch
= files
.head
->input_statement
.the_bfd
->my_archive
;
7978 if (my_arch
!= NULL
)
7979 for (; *iter
!= NULL
; iter
= &(*iter
)->next
)
7980 if ((*iter
)->the_bfd
->my_archive
!= my_arch
)
7983 *insert
= &files
.head
->input_statement
;
7984 files
.head
= (lang_statement_union_type
*) *iter
;
7986 if (my_arch
!= NULL
)
7988 lang_input_statement_type
*parent
= bfd_usrdata (my_arch
);
7990 parent
->next
= (lang_input_statement_type
*)
7992 - offsetof (lang_input_statement_type
, next
));
7997 #endif /* BFD_SUPPORTS_PLUGINS */
7999 /* Make sure that nobody has tried to add a symbol to this list
8001 ASSERT (link_info
.gc_sym_list
== NULL
);
8003 link_info
.gc_sym_list
= &entry_symbol
;
8005 if (entry_symbol
.name
== NULL
)
8007 link_info
.gc_sym_list
= ldlang_undef_chain_list_head
;
8009 /* entry_symbol is normally initialied by a ENTRY definition in the
8010 linker script or the -e command line option. But if neither of
8011 these have been used, the target specific backend may still have
8012 provided an entry symbol via a call to lang_default_entry().
8013 Unfortunately this value will not be processed until lang_end()
8014 is called, long after this function has finished. So detect this
8015 case here and add the target's entry symbol to the list of starting
8016 points for garbage collection resolution. */
8017 lang_add_gc_name (entry_symbol_default
);
8020 lang_add_gc_name (link_info
.init_function
);
8021 lang_add_gc_name (link_info
.fini_function
);
8023 ldemul_after_open ();
8024 if (config
.map_file
!= NULL
)
8025 lang_print_asneeded ();
8029 bfd_section_already_linked_table_free ();
8031 /* Make sure that we're not mixing architectures. We call this
8032 after all the input files have been opened, but before we do any
8033 other processing, so that any operations merge_private_bfd_data
8034 does on the output file will be known during the rest of the
8038 /* Handle .exports instead of a version script if we're told to do so. */
8039 if (command_line
.version_exports_section
)
8040 lang_do_version_exports_section ();
8042 /* Build all sets based on the information gathered from the input
8044 ldctor_build_sets ();
8046 lang_symbol_tweaks ();
8048 /* PR 13683: We must rerun the assignments prior to running garbage
8049 collection in order to make sure that all symbol aliases are resolved. */
8050 lang_do_assignments (lang_mark_phase_enum
);
8051 expld
.phase
= lang_first_phase_enum
;
8053 /* Size up the common data. */
8057 debug_prefix_tree ();
8061 /* Remove unreferenced sections if asked to. */
8062 lang_gc_sections ();
8064 lang_mark_undefineds ();
8066 /* Check relocations. */
8067 lang_check_relocs ();
8069 ldemul_after_check_relocs ();
8071 /* There might have been new sections created (e.g. as result of
8072 checking relocs to need a .got, or suchlike), so to properly order
8073 them into our lists of matching sections reset them here. */
8074 reset_resolved_wilds ();
8077 /* Update wild statements in case the user gave --sort-section.
8078 Note how the option might have come after the linker script and
8079 so couldn't have been set when the wild statements were created. */
8080 update_wild_statements (statement_list
.head
);
8082 /* Run through the contours of the script and attach input sections
8083 to the correct output sections. */
8084 lang_statement_iteration
++;
8085 map_input_to_output_sections (statement_list
.head
, NULL
, NULL
);
8087 /* Start at the statement immediately after the special abs_section
8088 output statement, so that it isn't reordered. */
8089 process_insert_statements (&lang_os_list
.head
->header
.next
);
8091 ldemul_before_place_orphans ();
8093 /* Find any sections not attached explicitly and handle them. */
8094 lang_place_orphans ();
8096 if (!bfd_link_relocatable (&link_info
))
8100 /* Merge SEC_MERGE sections. This has to be done after GC of
8101 sections, so that GCed sections are not merged, but before
8102 assigning dynamic symbols, since removing whole input sections
8104 bfd_merge_sections (link_info
.output_bfd
, &link_info
);
8106 /* Look for a text section and set the readonly attribute in it. */
8107 found
= bfd_get_section_by_name (link_info
.output_bfd
, ".text");
8111 if (config
.text_read_only
)
8112 found
->flags
|= SEC_READONLY
;
8114 found
->flags
&= ~SEC_READONLY
;
8118 /* Merge together CTF sections. After this, only the symtab-dependent
8119 function and data object sections need adjustment. */
8122 /* Emit the CTF, iff the emulation doesn't need to do late emission after
8123 examining things laid out late, like the strtab. */
8126 /* Copy forward lma regions for output sections in same lma region. */
8127 lang_propagate_lma_regions ();
8129 /* Defining __start/__stop symbols early for --gc-sections to work
8130 around a glibc build problem can result in these symbols being
8131 defined when they should not be. Fix them now. */
8132 if (config
.build_constructors
)
8133 lang_undef_start_stop ();
8135 /* Define .startof./.sizeof. symbols with preliminary values before
8136 dynamic symbols are created. */
8137 if (!bfd_link_relocatable (&link_info
))
8138 lang_init_startof_sizeof ();
8140 /* Do anything special before sizing sections. This is where ELF
8141 and other back-ends size dynamic sections. */
8142 ldemul_before_allocation ();
8144 /* We must record the program headers before we try to fix the
8145 section positions, since they will affect SIZEOF_HEADERS. */
8146 lang_record_phdrs ();
8148 /* Check relro sections. */
8149 if (link_info
.relro
&& !bfd_link_relocatable (&link_info
))
8150 lang_find_relro_sections ();
8152 /* Size up the sections. */
8153 lang_size_sections (NULL
, !RELAXATION_ENABLED
);
8155 /* See if anything special should be done now we know how big
8156 everything is. This is where relaxation is done. */
8157 ldemul_after_allocation ();
8159 /* Fix any __start, __stop, .startof. or .sizeof. symbols. */
8160 lang_finalize_start_stop ();
8162 /* Do all the assignments again, to report errors. Assignment
8163 statements are processed multiple times, updating symbols; In
8164 open_input_bfds, lang_do_assignments, and lang_size_sections.
8165 Since lang_relax_sections calls lang_do_assignments, symbols are
8166 also updated in ldemul_after_allocation. */
8167 lang_do_assignments (lang_final_phase_enum
);
8171 /* Convert absolute symbols to section relative. */
8172 ldexp_finalize_syms ();
8174 /* Make sure that the section addresses make sense. */
8175 if (command_line
.check_section_addresses
)
8176 lang_check_section_addresses ();
8178 /* Check any required symbols are known. */
8179 ldlang_check_require_defined_symbols ();
8184 /* EXPORTED TO YACC */
8187 lang_add_wild (struct wildcard_spec
*filespec
,
8188 struct wildcard_list
*section_list
,
8191 struct wildcard_list
*curr
, *next
;
8192 lang_wild_statement_type
*new_stmt
;
8193 bool any_specs_sorted
= false;
8195 /* Reverse the list as the parser puts it back to front. */
8196 for (curr
= section_list
, section_list
= NULL
;
8198 section_list
= curr
, curr
= next
)
8200 if (curr
->spec
.sorted
!= none
&& curr
->spec
.sorted
!= by_none
)
8201 any_specs_sorted
= true;
8203 curr
->next
= section_list
;
8206 if (filespec
!= NULL
&& filespec
->name
!= NULL
)
8208 if (strcmp (filespec
->name
, "*") == 0)
8209 filespec
->name
= NULL
;
8210 else if (!wildcardp (filespec
->name
))
8211 lang_has_input_file
= true;
8214 new_stmt
= new_stat (lang_wild_statement
, stat_ptr
);
8215 new_stmt
->filename
= NULL
;
8216 new_stmt
->filenames_sorted
= false;
8217 new_stmt
->any_specs_sorted
= any_specs_sorted
;
8218 new_stmt
->section_flag_list
= NULL
;
8219 new_stmt
->exclude_name_list
= NULL
;
8220 if (filespec
!= NULL
)
8222 new_stmt
->filename
= filespec
->name
;
8223 new_stmt
->filenames_sorted
= filespec
->sorted
== by_name
;
8224 new_stmt
->section_flag_list
= filespec
->section_flag_list
;
8225 new_stmt
->exclude_name_list
= filespec
->exclude_name_list
;
8227 new_stmt
->section_list
= section_list
;
8228 new_stmt
->keep_sections
= keep_sections
;
8229 lang_list_init (&new_stmt
->children
);
8230 lang_list_init (&new_stmt
->matching_sections
);
8231 analyze_walk_wild_section_handler (new_stmt
);
8234 printf ("wild %s(", new_stmt
->filename
? new_stmt
->filename
: "*");
8235 for (curr
= new_stmt
->section_list
; curr
; curr
= curr
->next
)
8236 printf ("%s ", curr
->spec
.name
? curr
->spec
.name
: "*");
8242 lang_section_start (const char *name
, etree_type
*address
,
8243 const segment_type
*segment
)
8245 lang_address_statement_type
*ad
;
8247 ad
= new_stat (lang_address_statement
, stat_ptr
);
8248 ad
->section_name
= name
;
8249 ad
->address
= address
;
8250 ad
->segment
= segment
;
8253 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
8254 because of a -e argument on the command line, or zero if this is
8255 called by ENTRY in a linker script. Command line arguments take
8259 lang_add_entry (const char *name
, bool cmdline
)
8261 if (entry_symbol
.name
== NULL
8263 || !entry_from_cmdline
)
8265 entry_symbol
.name
= name
;
8266 entry_from_cmdline
= cmdline
;
8270 /* Set the default start symbol to NAME. .em files should use this,
8271 not lang_add_entry, to override the use of "start" if neither the
8272 linker script nor the command line specifies an entry point. NAME
8273 must be permanently allocated. */
8275 lang_default_entry (const char *name
)
8277 entry_symbol_default
= name
;
8281 lang_add_target (const char *name
)
8283 lang_target_statement_type
*new_stmt
;
8285 new_stmt
= new_stat (lang_target_statement
, stat_ptr
);
8286 new_stmt
->target
= name
;
8290 lang_add_map (const char *name
)
8297 map_option_f
= true;
8305 lang_add_fill (fill_type
*fill
)
8307 lang_fill_statement_type
*new_stmt
;
8309 new_stmt
= new_stat (lang_fill_statement
, stat_ptr
);
8310 new_stmt
->fill
= fill
;
8314 lang_add_data (int type
, union etree_union
*exp
)
8316 lang_data_statement_type
*new_stmt
;
8318 new_stmt
= new_stat (lang_data_statement
, stat_ptr
);
8319 new_stmt
->exp
= exp
;
8320 new_stmt
->type
= type
;
8323 /* Create a new reloc statement. RELOC is the BFD relocation type to
8324 generate. HOWTO is the corresponding howto structure (we could
8325 look this up, but the caller has already done so). SECTION is the
8326 section to generate a reloc against, or NAME is the name of the
8327 symbol to generate a reloc against. Exactly one of SECTION and
8328 NAME must be NULL. ADDEND is an expression for the addend. */
8331 lang_add_reloc (bfd_reloc_code_real_type reloc
,
8332 reloc_howto_type
*howto
,
8335 union etree_union
*addend
)
8337 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
8341 p
->section
= section
;
8343 p
->addend_exp
= addend
;
8345 p
->addend_value
= 0;
8346 p
->output_section
= NULL
;
8347 p
->output_offset
= 0;
8350 lang_assignment_statement_type
*
8351 lang_add_assignment (etree_type
*exp
)
8353 lang_assignment_statement_type
*new_stmt
;
8355 new_stmt
= new_stat (lang_assignment_statement
, stat_ptr
);
8356 new_stmt
->exp
= exp
;
8361 lang_add_attribute (enum statement_enum attribute
)
8363 new_statement (attribute
, sizeof (lang_statement_header_type
), stat_ptr
);
8367 lang_startup (const char *name
)
8369 if (first_file
->filename
!= NULL
)
8371 einfo (_("%F%P: multiple STARTUP files\n"));
8373 first_file
->filename
= name
;
8374 first_file
->local_sym_name
= name
;
8375 first_file
->flags
.real
= true;
8379 lang_float (bool maybe
)
8381 lang_float_flag
= maybe
;
8385 /* Work out the load- and run-time regions from a script statement, and
8386 store them in *LMA_REGION and *REGION respectively.
8388 MEMSPEC is the name of the run-time region, or the value of
8389 DEFAULT_MEMORY_REGION if the statement didn't specify one.
8390 LMA_MEMSPEC is the name of the load-time region, or null if the
8391 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
8392 had an explicit load address.
8394 It is an error to specify both a load region and a load address. */
8397 lang_get_regions (lang_memory_region_type
**region
,
8398 lang_memory_region_type
**lma_region
,
8399 const char *memspec
,
8400 const char *lma_memspec
,
8404 *lma_region
= lang_memory_region_lookup (lma_memspec
, false);
8406 /* If no runtime region or VMA has been specified, but the load region
8407 has been specified, then use the load region for the runtime region
8409 if (lma_memspec
!= NULL
8411 && strcmp (memspec
, DEFAULT_MEMORY_REGION
) == 0)
8412 *region
= *lma_region
;
8414 *region
= lang_memory_region_lookup (memspec
, false);
8416 if (have_lma
&& lma_memspec
!= 0)
8417 einfo (_("%X%P:%pS: section has both a load address and a load region\n"),
8422 lang_leave_output_section_statement (fill_type
*fill
, const char *memspec
,
8423 lang_output_section_phdr_list
*phdrs
,
8424 const char *lma_memspec
)
8426 lang_get_regions (¤t_section
->region
,
8427 ¤t_section
->lma_region
,
8428 memspec
, lma_memspec
,
8429 current_section
->load_base
!= NULL
,
8430 current_section
->addr_tree
!= NULL
);
8432 current_section
->fill
= fill
;
8433 current_section
->phdrs
= phdrs
;
8437 /* Set the output format type. -oformat overrides scripts. */
8440 lang_add_output_format (const char *format
,
8445 if (output_target
== NULL
|| !from_script
)
8447 if (command_line
.endian
== ENDIAN_BIG
8450 else if (command_line
.endian
== ENDIAN_LITTLE
8454 output_target
= format
;
8459 lang_add_insert (const char *where
, int is_before
)
8461 lang_insert_statement_type
*new_stmt
;
8463 new_stmt
= new_stat (lang_insert_statement
, stat_ptr
);
8464 new_stmt
->where
= where
;
8465 new_stmt
->is_before
= is_before
;
8466 saved_script_handle
= previous_script_handle
;
8469 /* Enter a group. This creates a new lang_group_statement, and sets
8470 stat_ptr to build new statements within the group. */
8473 lang_enter_group (void)
8475 lang_group_statement_type
*g
;
8477 g
= new_stat (lang_group_statement
, stat_ptr
);
8478 lang_list_init (&g
->children
);
8479 push_stat_ptr (&g
->children
);
8482 /* Leave a group. This just resets stat_ptr to start writing to the
8483 regular list of statements again. Note that this will not work if
8484 groups can occur inside anything else which can adjust stat_ptr,
8485 but currently they can't. */
8488 lang_leave_group (void)
8493 /* Add a new program header. This is called for each entry in a PHDRS
8494 command in a linker script. */
8497 lang_new_phdr (const char *name
,
8504 struct lang_phdr
*n
, **pp
;
8507 n
= stat_alloc (sizeof (struct lang_phdr
));
8510 n
->type
= exp_get_vma (type
, 0, "program header type");
8511 n
->filehdr
= filehdr
;
8516 hdrs
= n
->type
== 1 && (phdrs
|| filehdr
);
8518 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
8521 && !((*pp
)->filehdr
|| (*pp
)->phdrs
))
8523 einfo (_("%X%P:%pS: PHDRS and FILEHDR are not supported"
8524 " when prior PT_LOAD headers lack them\n"), NULL
);
8531 /* Record the program header information in the output BFD. FIXME: We
8532 should not be calling an ELF specific function here. */
8535 lang_record_phdrs (void)
8539 lang_output_section_phdr_list
*last
;
8540 struct lang_phdr
*l
;
8541 lang_output_section_statement_type
*os
;
8544 secs
= (asection
**) xmalloc (alc
* sizeof (asection
*));
8547 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
8554 for (os
= (void *) lang_os_list
.head
;
8558 lang_output_section_phdr_list
*pl
;
8560 if (os
->constraint
< 0)
8568 if (os
->sectype
== noload_section
8569 || os
->bfd_section
== NULL
8570 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
8573 /* Don't add orphans to PT_INTERP header. */
8579 lang_output_section_statement_type
*tmp_os
;
8581 /* If we have not run across a section with a program
8582 header assigned to it yet, then scan forwards to find
8583 one. This prevents inconsistencies in the linker's
8584 behaviour when a script has specified just a single
8585 header and there are sections in that script which are
8586 not assigned to it, and which occur before the first
8587 use of that header. See here for more details:
8588 http://sourceware.org/ml/binutils/2007-02/msg00291.html */
8589 for (tmp_os
= os
; tmp_os
; tmp_os
= tmp_os
->next
)
8592 last
= tmp_os
->phdrs
;
8596 einfo (_("%F%P: no sections assigned to phdrs\n"));
8601 if (os
->bfd_section
== NULL
)
8604 for (; pl
!= NULL
; pl
= pl
->next
)
8606 if (strcmp (pl
->name
, l
->name
) == 0)
8611 secs
= (asection
**) xrealloc (secs
,
8612 alc
* sizeof (asection
*));
8614 secs
[c
] = os
->bfd_section
;
8621 if (l
->flags
== NULL
)
8624 flags
= exp_get_vma (l
->flags
, 0, "phdr flags");
8629 at
= exp_get_vma (l
->at
, 0, "phdr load address");
8631 if (!bfd_record_phdr (link_info
.output_bfd
, l
->type
,
8632 l
->flags
!= NULL
, flags
, l
->at
!= NULL
,
8633 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
8634 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
8639 /* Make sure all the phdr assignments succeeded. */
8640 for (os
= (void *) lang_os_list
.head
;
8644 lang_output_section_phdr_list
*pl
;
8646 if (os
->constraint
< 0
8647 || os
->bfd_section
== NULL
)
8650 for (pl
= os
->phdrs
;
8653 if (!pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
8654 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
8655 os
->name
, pl
->name
);
8659 /* Record a list of sections which may not be cross referenced. */
8662 lang_add_nocrossref (lang_nocrossref_type
*l
)
8664 struct lang_nocrossrefs
*n
;
8666 n
= (struct lang_nocrossrefs
*) xmalloc (sizeof *n
);
8667 n
->next
= nocrossref_list
;
8669 n
->onlyfirst
= false;
8670 nocrossref_list
= n
;
8672 /* Set notice_all so that we get informed about all symbols. */
8673 link_info
.notice_all
= true;
8676 /* Record a section that cannot be referenced from a list of sections. */
8679 lang_add_nocrossref_to (lang_nocrossref_type
*l
)
8681 lang_add_nocrossref (l
);
8682 nocrossref_list
->onlyfirst
= true;
8685 /* Overlay handling. We handle overlays with some static variables. */
8687 /* The overlay virtual address. */
8688 static etree_type
*overlay_vma
;
8689 /* And subsection alignment. */
8690 static etree_type
*overlay_subalign
;
8692 /* An expression for the maximum section size seen so far. */
8693 static etree_type
*overlay_max
;
8695 /* A list of all the sections in this overlay. */
8697 struct overlay_list
{
8698 struct overlay_list
*next
;
8699 lang_output_section_statement_type
*os
;
8702 static struct overlay_list
*overlay_list
;
8704 /* Start handling an overlay. */
8707 lang_enter_overlay (etree_type
*vma_expr
, etree_type
*subalign
)
8709 /* The grammar should prevent nested overlays from occurring. */
8710 ASSERT (overlay_vma
== NULL
8711 && overlay_subalign
== NULL
8712 && overlay_max
== NULL
);
8714 overlay_vma
= vma_expr
;
8715 overlay_subalign
= subalign
;
8718 /* Start a section in an overlay. We handle this by calling
8719 lang_enter_output_section_statement with the correct VMA.
8720 lang_leave_overlay sets up the LMA and memory regions. */
8723 lang_enter_overlay_section (const char *name
)
8725 struct overlay_list
*n
;
8728 lang_enter_output_section_statement (name
, overlay_vma
, overlay_section
,
8729 0, 0, overlay_subalign
, 0, 0, 0);
8731 /* If this is the first section, then base the VMA of future
8732 sections on this one. This will work correctly even if `.' is
8733 used in the addresses. */
8734 if (overlay_list
== NULL
)
8735 overlay_vma
= exp_nameop (ADDR
, name
);
8737 /* Remember the section. */
8738 n
= (struct overlay_list
*) xmalloc (sizeof *n
);
8739 n
->os
= current_section
;
8740 n
->next
= overlay_list
;
8743 size
= exp_nameop (SIZEOF
, name
);
8745 /* Arrange to work out the maximum section end address. */
8746 if (overlay_max
== NULL
)
8749 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
8752 /* Finish a section in an overlay. There isn't any special to do
8756 lang_leave_overlay_section (fill_type
*fill
,
8757 lang_output_section_phdr_list
*phdrs
)
8764 name
= current_section
->name
;
8766 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
8767 region and that no load-time region has been specified. It doesn't
8768 really matter what we say here, since lang_leave_overlay will
8770 lang_leave_output_section_statement (fill
, DEFAULT_MEMORY_REGION
, phdrs
, 0);
8772 /* Define the magic symbols. */
8774 clean
= (char *) xmalloc (strlen (name
) + 1);
8776 for (s1
= name
; *s1
!= '\0'; s1
++)
8777 if (ISALNUM (*s1
) || *s1
== '_')
8781 buf
= (char *) xmalloc (strlen (clean
) + sizeof "__load_start_");
8782 sprintf (buf
, "__load_start_%s", clean
);
8783 lang_add_assignment (exp_provide (buf
,
8784 exp_nameop (LOADADDR
, name
),
8787 buf
= (char *) xmalloc (strlen (clean
) + sizeof "__load_stop_");
8788 sprintf (buf
, "__load_stop_%s", clean
);
8789 lang_add_assignment (exp_provide (buf
,
8791 exp_nameop (LOADADDR
, name
),
8792 exp_nameop (SIZEOF
, name
)),
8798 /* Finish an overlay. If there are any overlay wide settings, this
8799 looks through all the sections in the overlay and sets them. */
8802 lang_leave_overlay (etree_type
*lma_expr
,
8805 const char *memspec
,
8806 lang_output_section_phdr_list
*phdrs
,
8807 const char *lma_memspec
)
8809 lang_memory_region_type
*region
;
8810 lang_memory_region_type
*lma_region
;
8811 struct overlay_list
*l
;
8812 lang_nocrossref_type
*nocrossref
;
8814 lang_get_regions (®ion
, &lma_region
,
8815 memspec
, lma_memspec
,
8816 lma_expr
!= NULL
, false);
8820 /* After setting the size of the last section, set '.' to end of the
8822 if (overlay_list
!= NULL
)
8824 overlay_list
->os
->update_dot
= 1;
8825 overlay_list
->os
->update_dot_tree
8826 = exp_assign (".", exp_binop ('+', overlay_vma
, overlay_max
), false);
8832 struct overlay_list
*next
;
8834 if (fill
!= NULL
&& l
->os
->fill
== NULL
)
8837 l
->os
->region
= region
;
8838 l
->os
->lma_region
= lma_region
;
8840 /* The first section has the load address specified in the
8841 OVERLAY statement. The rest are worked out from that.
8842 The base address is not needed (and should be null) if
8843 an LMA region was specified. */
8846 l
->os
->load_base
= lma_expr
;
8847 l
->os
->sectype
= first_overlay_section
;
8849 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
8850 l
->os
->phdrs
= phdrs
;
8854 lang_nocrossref_type
*nc
;
8856 nc
= (lang_nocrossref_type
*) xmalloc (sizeof *nc
);
8857 nc
->name
= l
->os
->name
;
8858 nc
->next
= nocrossref
;
8867 if (nocrossref
!= NULL
)
8868 lang_add_nocrossref (nocrossref
);
8871 overlay_list
= NULL
;
8873 overlay_subalign
= NULL
;
8876 /* Version handling. This is only useful for ELF. */
8878 /* If PREV is NULL, return first version pattern matching particular symbol.
8879 If PREV is non-NULL, return first version pattern matching particular
8880 symbol after PREV (previously returned by lang_vers_match). */
8882 static struct bfd_elf_version_expr
*
8883 lang_vers_match (struct bfd_elf_version_expr_head
*head
,
8884 struct bfd_elf_version_expr
*prev
,
8888 const char *cxx_sym
= sym
;
8889 const char *java_sym
= sym
;
8890 struct bfd_elf_version_expr
*expr
= NULL
;
8891 enum demangling_styles curr_style
;
8893 curr_style
= CURRENT_DEMANGLING_STYLE
;
8894 cplus_demangle_set_style (no_demangling
);
8895 c_sym
= bfd_demangle (link_info
.output_bfd
, sym
, DMGL_NO_OPTS
);
8898 cplus_demangle_set_style (curr_style
);
8900 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
8902 cxx_sym
= bfd_demangle (link_info
.output_bfd
, sym
,
8903 DMGL_PARAMS
| DMGL_ANSI
);
8907 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
8909 java_sym
= bfd_demangle (link_info
.output_bfd
, sym
, DMGL_JAVA
);
8914 if (head
->htab
&& (prev
== NULL
|| prev
->literal
))
8916 struct bfd_elf_version_expr e
;
8918 switch (prev
? prev
->mask
: 0)
8921 if (head
->mask
& BFD_ELF_VERSION_C_TYPE
)
8924 expr
= (struct bfd_elf_version_expr
*)
8925 htab_find ((htab_t
) head
->htab
, &e
);
8926 while (expr
&& strcmp (expr
->pattern
, c_sym
) == 0)
8927 if (expr
->mask
== BFD_ELF_VERSION_C_TYPE
)
8933 case BFD_ELF_VERSION_C_TYPE
:
8934 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
8936 e
.pattern
= cxx_sym
;
8937 expr
= (struct bfd_elf_version_expr
*)
8938 htab_find ((htab_t
) head
->htab
, &e
);
8939 while (expr
&& strcmp (expr
->pattern
, cxx_sym
) == 0)
8940 if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
8946 case BFD_ELF_VERSION_CXX_TYPE
:
8947 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
8949 e
.pattern
= java_sym
;
8950 expr
= (struct bfd_elf_version_expr
*)
8951 htab_find ((htab_t
) head
->htab
, &e
);
8952 while (expr
&& strcmp (expr
->pattern
, java_sym
) == 0)
8953 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
8964 /* Finally, try the wildcards. */
8965 if (prev
== NULL
|| prev
->literal
)
8966 expr
= head
->remaining
;
8969 for (; expr
; expr
= expr
->next
)
8976 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
8979 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
8981 else if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
8985 if (fnmatch (expr
->pattern
, s
, 0) == 0)
8991 free ((char *) c_sym
);
8993 free ((char *) cxx_sym
);
8994 if (java_sym
!= sym
)
8995 free ((char *) java_sym
);
8999 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
9000 return a pointer to the symbol name with any backslash quotes removed. */
9003 realsymbol (const char *pattern
)
9006 bool changed
= false, backslash
= false;
9007 char *s
, *symbol
= (char *) xmalloc (strlen (pattern
) + 1);
9009 for (p
= pattern
, s
= symbol
; *p
!= '\0'; ++p
)
9011 /* It is a glob pattern only if there is no preceding
9015 /* Remove the preceding backslash. */
9022 if (*p
== '?' || *p
== '*' || *p
== '[')
9029 backslash
= *p
== '\\';
9045 /* This is called for each variable name or match expression. NEW_NAME is
9046 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
9047 pattern to be matched against symbol names. */
9049 struct bfd_elf_version_expr
*
9050 lang_new_vers_pattern (struct bfd_elf_version_expr
*orig
,
9051 const char *new_name
,
9055 struct bfd_elf_version_expr
*ret
;
9057 ret
= (struct bfd_elf_version_expr
*) xmalloc (sizeof *ret
);
9061 ret
->literal
= true;
9062 ret
->pattern
= literal_p
? new_name
: realsymbol (new_name
);
9063 if (ret
->pattern
== NULL
)
9065 ret
->pattern
= new_name
;
9066 ret
->literal
= false;
9069 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
9070 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
9071 else if (strcasecmp (lang
, "C++") == 0)
9072 ret
->mask
= BFD_ELF_VERSION_CXX_TYPE
;
9073 else if (strcasecmp (lang
, "Java") == 0)
9074 ret
->mask
= BFD_ELF_VERSION_JAVA_TYPE
;
9077 einfo (_("%X%P: unknown language `%s' in version information\n"),
9079 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
9082 return ldemul_new_vers_pattern (ret
);
9085 /* This is called for each set of variable names and match
9088 struct bfd_elf_version_tree
*
9089 lang_new_vers_node (struct bfd_elf_version_expr
*globals
,
9090 struct bfd_elf_version_expr
*locals
)
9092 struct bfd_elf_version_tree
*ret
;
9094 ret
= (struct bfd_elf_version_tree
*) xcalloc (1, sizeof *ret
);
9095 ret
->globals
.list
= globals
;
9096 ret
->locals
.list
= locals
;
9097 ret
->match
= lang_vers_match
;
9098 ret
->name_indx
= (unsigned int) -1;
9102 /* This static variable keeps track of version indices. */
9104 static int version_index
;
9107 version_expr_head_hash (const void *p
)
9109 const struct bfd_elf_version_expr
*e
=
9110 (const struct bfd_elf_version_expr
*) p
;
9112 return htab_hash_string (e
->pattern
);
9116 version_expr_head_eq (const void *p1
, const void *p2
)
9118 const struct bfd_elf_version_expr
*e1
=
9119 (const struct bfd_elf_version_expr
*) p1
;
9120 const struct bfd_elf_version_expr
*e2
=
9121 (const struct bfd_elf_version_expr
*) p2
;
9123 return strcmp (e1
->pattern
, e2
->pattern
) == 0;
9127 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head
*head
)
9130 struct bfd_elf_version_expr
*e
, *next
;
9131 struct bfd_elf_version_expr
**list_loc
, **remaining_loc
;
9133 for (e
= head
->list
; e
; e
= e
->next
)
9137 head
->mask
|= e
->mask
;
9142 head
->htab
= htab_create (count
* 2, version_expr_head_hash
,
9143 version_expr_head_eq
, NULL
);
9144 list_loc
= &head
->list
;
9145 remaining_loc
= &head
->remaining
;
9146 for (e
= head
->list
; e
; e
= next
)
9152 remaining_loc
= &e
->next
;
9156 void **loc
= htab_find_slot ((htab_t
) head
->htab
, e
, INSERT
);
9160 struct bfd_elf_version_expr
*e1
, *last
;
9162 e1
= (struct bfd_elf_version_expr
*) *loc
;
9166 if (e1
->mask
== e
->mask
)
9174 while (e1
&& strcmp (e1
->pattern
, e
->pattern
) == 0);
9178 /* This is a duplicate. */
9179 /* FIXME: Memory leak. Sometimes pattern is not
9180 xmalloced alone, but in larger chunk of memory. */
9181 /* free (e->pattern); */
9186 e
->next
= last
->next
;
9194 list_loc
= &e
->next
;
9198 *remaining_loc
= NULL
;
9199 *list_loc
= head
->remaining
;
9202 head
->remaining
= head
->list
;
9205 /* This is called when we know the name and dependencies of the
9209 lang_register_vers_node (const char *name
,
9210 struct bfd_elf_version_tree
*version
,
9211 struct bfd_elf_version_deps
*deps
)
9213 struct bfd_elf_version_tree
*t
, **pp
;
9214 struct bfd_elf_version_expr
*e1
;
9219 if (link_info
.version_info
!= NULL
9220 && (name
[0] == '\0' || link_info
.version_info
->name
[0] == '\0'))
9222 einfo (_("%X%P: anonymous version tag cannot be combined"
9223 " with other version tags\n"));
9228 /* Make sure this node has a unique name. */
9229 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9230 if (strcmp (t
->name
, name
) == 0)
9231 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
9233 lang_finalize_version_expr_head (&version
->globals
);
9234 lang_finalize_version_expr_head (&version
->locals
);
9236 /* Check the global and local match names, and make sure there
9237 aren't any duplicates. */
9239 for (e1
= version
->globals
.list
; e1
!= NULL
; e1
= e1
->next
)
9241 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9243 struct bfd_elf_version_expr
*e2
;
9245 if (t
->locals
.htab
&& e1
->literal
)
9247 e2
= (struct bfd_elf_version_expr
*)
9248 htab_find ((htab_t
) t
->locals
.htab
, e1
);
9249 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
9251 if (e1
->mask
== e2
->mask
)
9252 einfo (_("%X%P: duplicate expression `%s'"
9253 " in version information\n"), e1
->pattern
);
9257 else if (!e1
->literal
)
9258 for (e2
= t
->locals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
9259 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
9260 && e1
->mask
== e2
->mask
)
9261 einfo (_("%X%P: duplicate expression `%s'"
9262 " in version information\n"), e1
->pattern
);
9266 for (e1
= version
->locals
.list
; e1
!= NULL
; e1
= e1
->next
)
9268 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9270 struct bfd_elf_version_expr
*e2
;
9272 if (t
->globals
.htab
&& e1
->literal
)
9274 e2
= (struct bfd_elf_version_expr
*)
9275 htab_find ((htab_t
) t
->globals
.htab
, e1
);
9276 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
9278 if (e1
->mask
== e2
->mask
)
9279 einfo (_("%X%P: duplicate expression `%s'"
9280 " in version information\n"),
9285 else if (!e1
->literal
)
9286 for (e2
= t
->globals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
9287 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
9288 && e1
->mask
== e2
->mask
)
9289 einfo (_("%X%P: duplicate expression `%s'"
9290 " in version information\n"), e1
->pattern
);
9294 version
->deps
= deps
;
9295 version
->name
= name
;
9296 if (name
[0] != '\0')
9299 version
->vernum
= version_index
;
9302 version
->vernum
= 0;
9304 for (pp
= &link_info
.version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
9309 /* This is called when we see a version dependency. */
9311 struct bfd_elf_version_deps
*
9312 lang_add_vers_depend (struct bfd_elf_version_deps
*list
, const char *name
)
9314 struct bfd_elf_version_deps
*ret
;
9315 struct bfd_elf_version_tree
*t
;
9317 ret
= (struct bfd_elf_version_deps
*) xmalloc (sizeof *ret
);
9320 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9322 if (strcmp (t
->name
, name
) == 0)
9324 ret
->version_needed
= t
;
9329 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
9331 ret
->version_needed
= NULL
;
9336 lang_do_version_exports_section (void)
9338 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
9340 LANG_FOR_EACH_INPUT_STATEMENT (is
)
9342 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
9350 contents
= (char *) xmalloc (len
);
9351 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
9352 einfo (_("%X%P: unable to read .exports section contents\n"), sec
);
9355 while (p
< contents
+ len
)
9357 greg
= lang_new_vers_pattern (greg
, p
, NULL
, false);
9358 p
= strchr (p
, '\0') + 1;
9361 /* Do not free the contents, as we used them creating the regex. */
9363 /* Do not include this section in the link. */
9364 sec
->flags
|= SEC_EXCLUDE
| SEC_KEEP
;
9367 lreg
= lang_new_vers_pattern (NULL
, "*", NULL
, false);
9368 lang_register_vers_node (command_line
.version_exports_section
,
9369 lang_new_vers_node (greg
, lreg
), NULL
);
9372 /* Evaluate LENGTH and ORIGIN parts of MEMORY spec. This is initially
9373 called with UPDATE_REGIONS_P set to FALSE, in this case no errors are
9374 thrown, however, references to symbols in the origin and length fields
9375 will be pushed into the symbol table, this allows PROVIDE statements to
9376 then provide these symbols. This function is called a second time with
9377 UPDATE_REGIONS_P set to TRUE, this time the we update the actual region
9378 data structures, and throw errors if missing symbols are encountered. */
9381 lang_do_memory_regions (bool update_regions_p
)
9383 lang_memory_region_type
*r
= lang_memory_region_list
;
9385 for (; r
!= NULL
; r
= r
->next
)
9389 exp_fold_tree_no_dot (r
->origin_exp
);
9390 if (update_regions_p
)
9392 if (expld
.result
.valid_p
)
9394 r
->origin
= expld
.result
.value
;
9395 r
->current
= r
->origin
;
9398 einfo (_("%P: invalid origin for memory region %s\n"),
9404 exp_fold_tree_no_dot (r
->length_exp
);
9405 if (update_regions_p
)
9407 if (expld
.result
.valid_p
)
9408 r
->length
= expld
.result
.value
;
9410 einfo (_("%P: invalid length for memory region %s\n"),
9418 lang_add_unique (const char *name
)
9420 struct unique_sections
*ent
;
9422 for (ent
= unique_section_list
; ent
; ent
= ent
->next
)
9423 if (strcmp (ent
->name
, name
) == 0)
9426 ent
= (struct unique_sections
*) xmalloc (sizeof *ent
);
9427 ent
->name
= xstrdup (name
);
9428 ent
->next
= unique_section_list
;
9429 unique_section_list
= ent
;
9432 /* Append the list of dynamic symbols to the existing one. */
9435 lang_append_dynamic_list (struct bfd_elf_dynamic_list
**list_p
,
9436 struct bfd_elf_version_expr
*dynamic
)
9440 struct bfd_elf_version_expr
*tail
;
9441 for (tail
= dynamic
; tail
->next
!= NULL
; tail
= tail
->next
)
9443 tail
->next
= (*list_p
)->head
.list
;
9444 (*list_p
)->head
.list
= dynamic
;
9448 struct bfd_elf_dynamic_list
*d
;
9450 d
= (struct bfd_elf_dynamic_list
*) xcalloc (1, sizeof *d
);
9451 d
->head
.list
= dynamic
;
9452 d
->match
= lang_vers_match
;
9457 /* Append the list of C++ typeinfo dynamic symbols to the existing
9461 lang_append_dynamic_list_cpp_typeinfo (void)
9463 const char *symbols
[] =
9465 "typeinfo name for*",
9468 struct bfd_elf_version_expr
*dynamic
= NULL
;
9471 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
9472 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
9475 lang_append_dynamic_list (&link_info
.dynamic_list
, dynamic
);
9478 /* Append the list of C++ operator new and delete dynamic symbols to the
9482 lang_append_dynamic_list_cpp_new (void)
9484 const char *symbols
[] =
9489 struct bfd_elf_version_expr
*dynamic
= NULL
;
9492 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
9493 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
9496 lang_append_dynamic_list (&link_info
.dynamic_list
, dynamic
);
9499 /* Scan a space and/or comma separated string of features. */
9502 lang_ld_feature (char *str
)
9510 while (*p
== ',' || ISSPACE (*p
))
9515 while (*q
&& *q
!= ',' && !ISSPACE (*q
))
9519 if (strcasecmp (p
, "SANE_EXPR") == 0)
9520 config
.sane_expr
= true;
9522 einfo (_("%X%P: unknown feature `%s'\n"), p
);
9528 /* Pretty print memory amount. */
9531 lang_print_memory_size (uint64_t sz
)
9533 if ((sz
& 0x3fffffff) == 0)
9534 printf ("%10" PRIu64
" GB", sz
>> 30);
9535 else if ((sz
& 0xfffff) == 0)
9536 printf ("%10" PRIu64
" MB", sz
>> 20);
9537 else if ((sz
& 0x3ff) == 0)
9538 printf ("%10" PRIu64
" KB", sz
>> 10);
9540 printf (" %10" PRIu64
" B", sz
);
9543 /* Implement --print-memory-usage: disply per region memory usage. */
9546 lang_print_memory_usage (void)
9548 lang_memory_region_type
*r
;
9550 printf ("Memory region Used Size Region Size %%age Used\n");
9551 for (r
= lang_memory_region_list
; r
->next
!= NULL
; r
= r
->next
)
9553 bfd_vma used_length
= r
->current
- r
->origin
;
9555 printf ("%16s: ",r
->name_list
.name
);
9556 lang_print_memory_size (used_length
);
9557 lang_print_memory_size (r
->length
);
9561 double percent
= used_length
* 100.0 / r
->length
;
9562 printf (" %6.2f%%", percent
);