1 /* Linker command language support.
2 Copyright (C) 1991-2024 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"
46 #if BFD_SUPPORTS_PLUGINS
51 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
54 /* Convert between addresses in bytes and sizes in octets.
55 For currently supported targets, octets_per_byte is always a power
56 of two, so we can use shifts. */
57 #define TO_ADDR(X) ((X) >> opb_shift)
58 #define TO_SIZE(X) ((X) << opb_shift)
60 /* Local variables. */
61 static struct obstack stat_obstack
;
62 static struct obstack map_obstack
;
63 static struct obstack pt_obstack
;
65 #define obstack_chunk_alloc xmalloc
66 #define obstack_chunk_free free
67 static const char *entry_symbol_default
= "start";
68 static bool map_head_is_link_order
= false;
69 static lang_output_section_statement_type
*default_common_section
;
70 static bool map_option_f
;
71 static bfd_vma print_dot
;
72 static lang_input_statement_type
*first_file
;
73 static const char *current_target
;
74 static lang_statement_list_type
*stat_save
[10];
75 static lang_statement_list_type
**stat_save_ptr
= &stat_save
[0];
76 static struct unique_sections
*unique_section_list
;
77 static struct asneeded_minfo
*asneeded_list_head
;
78 static unsigned int opb_shift
= 0;
80 /* Forward declarations. */
81 static void exp_init_os (etree_type
*);
82 static lang_input_statement_type
*lookup_name (const char *);
83 static bool wont_add_section_p (asection
*,
84 lang_output_section_statement_type
*);
85 static void insert_undefined (const char *);
86 static bool sort_def_symbol (struct bfd_link_hash_entry
*, void *);
87 static lang_statement_union_type
*new_statement (enum statement_enum type
,
89 lang_statement_list_type
*list
);
90 static void print_statement (lang_statement_union_type
*,
91 lang_output_section_statement_type
*);
92 static void print_statement_list (lang_statement_union_type
*,
93 lang_output_section_statement_type
*);
94 static void print_statements (void);
95 static void print_input_section (asection
*, bool);
96 static bool lang_one_common (struct bfd_link_hash_entry
*, void *);
97 static void lang_record_phdrs (void);
98 static void lang_do_version_exports_section (void);
99 static void lang_finalize_version_expr_head
100 (struct bfd_elf_version_expr_head
*);
101 static void lang_do_memory_regions (bool);
103 /* Exported variables. */
104 const char *output_target
;
105 lang_output_section_statement_type
*abs_output_section
;
106 /* Header for list of statements corresponding to any files involved in the
107 link, either specified from the command-line or added implicitely (eg.
108 archive member used to resolved undefined symbol, wildcard statement from
109 linker script, etc.). Next pointer is in next field of a
110 lang_statement_header_type (reached via header field in a
111 lang_statement_union). */
112 lang_statement_list_type statement_list
;
113 lang_statement_list_type lang_os_list
;
114 lang_statement_list_type
*stat_ptr
= &statement_list
;
115 /* Header for list of statements corresponding to files used in the final
116 executable. This can be either object file specified on the command-line
117 or library member resolving an undefined reference. Next pointer is in next
118 field of a lang_input_statement_type (reached via input_statement field in a
119 lang_statement_union). */
120 lang_statement_list_type file_chain
= { NULL
, NULL
};
121 /* Header for list of statements corresponding to files specified on the
122 command-line for linking. It thus contains real object files and archive
123 but not archive members. Next pointer is in next_real_file field of a
124 lang_input_statement_type statement (reached via input_statement field in a
125 lang_statement_union). */
126 lang_statement_list_type input_file_chain
;
127 static const char *current_input_file
;
128 struct bfd_elf_dynamic_list
**current_dynamic_list_p
;
129 struct bfd_sym_chain entry_symbol
= { NULL
, NULL
};
130 const char *entry_section
= ".text";
131 struct lang_input_statement_flags input_flags
;
132 bool entry_from_cmdline
;
133 bool lang_has_input_file
= false;
134 bool had_output_filename
= false;
135 bool lang_float_flag
= false;
136 bool delete_output_file_on_failure
= false;
137 bool enable_linker_version
= false;
138 struct lang_phdr
*lang_phdr_list
;
139 struct lang_nocrossrefs
*nocrossref_list
;
140 struct asneeded_minfo
**asneeded_list_tail
;
142 static ctf_dict_t
*ctf_output
;
145 /* Functions that traverse the linker script and might evaluate
146 DEFINED() need to increment this at the start of the traversal. */
147 int lang_statement_iteration
= 0;
149 /* Count times through one_lang_size_sections_pass after mark phase. */
150 static int lang_sizing_iteration
= 0;
152 /* Return TRUE if the PATTERN argument is a wildcard pattern.
153 Although backslashes are treated specially if a pattern contains
154 wildcards, we do not consider the mere presence of a backslash to
155 be enough to cause the pattern to be treated as a wildcard.
156 That lets us handle DOS filenames more naturally. */
157 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
159 #define new_stat(x, y) \
160 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
162 #define outside_section_address(q) \
163 ((q)->output_offset + (q)->output_section->vma)
165 #define outside_symbol_address(q) \
166 ((q)->value + outside_section_address (q->section))
168 /* CTF sections smaller than this are not compressed: compression of
169 dictionaries this small doesn't gain much, and this lets consumers mmap the
170 sections directly out of the ELF file and use them with no decompression
171 overhead if they want to. */
172 #define CTF_COMPRESSION_THRESHOLD 4096
175 stat_alloc (size_t size
)
177 return obstack_alloc (&stat_obstack
, size
);
180 /* Code for handling simple wildcards without going through fnmatch,
181 which can be expensive because of charset translations etc. */
183 /* A simple wild is a literal string followed by a single '*',
184 where the literal part is at least 4 characters long. */
187 is_simple_wild (const char *name
)
189 size_t len
= strcspn (name
, "*?[");
190 return len
>= 4 && name
[len
] == '*' && name
[len
+ 1] == '\0';
194 match_simple_wild (const char *pattern
, const char *name
)
196 /* The first four characters of the pattern are guaranteed valid
197 non-wildcard characters. So we can go faster. */
198 if (pattern
[0] != name
[0] || pattern
[1] != name
[1]
199 || pattern
[2] != name
[2] || pattern
[3] != name
[3])
204 while (*pattern
!= '*')
205 if (*name
++ != *pattern
++)
212 name_match (const char *pattern
, const char *name
)
214 if (is_simple_wild (pattern
))
215 return !match_simple_wild (pattern
, name
);
216 if (wildcardp (pattern
))
217 return fnmatch (pattern
, name
, 0);
218 return strcmp (pattern
, name
);
221 /* Given an analyzed wildcard_spec SPEC, match it against NAME,
222 returns zero on a match, non-zero if there's no match. */
225 spec_match (const struct wildcard_spec
*spec
, const char *name
)
227 size_t nl
= spec
->namelen
;
228 size_t pl
= spec
->prefixlen
;
229 size_t sl
= spec
->suffixlen
;
230 size_t inputlen
= strlen (name
);
238 r
= memcmp (spec
->name
, name
, pl
);
248 r
= memcmp (spec
->name
+ nl
- sl
, name
+ inputlen
- sl
, sl
);
253 if (nl
== pl
+ sl
+ 1 && spec
->name
[pl
] == '*')
257 return fnmatch (spec
->name
+ pl
, name
+ pl
, 0);
266 ldirname (const char *name
)
268 const char *base
= lbasename (name
);
271 while (base
> name
&& IS_DIR_SEPARATOR (base
[-1]))
275 dirname
= strdup (name
);
276 dirname
[base
- name
] = '\0';
280 /* If PATTERN is of the form archive:file, return a pointer to the
281 separator. If not, return NULL. */
284 archive_path (const char *pattern
)
288 if (link_info
.path_separator
== 0)
291 p
= strchr (pattern
, link_info
.path_separator
);
292 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
293 if (p
== NULL
|| link_info
.path_separator
!= ':')
296 /* Assume a match on the second char is part of drive specifier,
297 as in "c:\silly.dos". */
298 if (p
== pattern
+ 1 && ISALPHA (*pattern
))
299 p
= strchr (p
+ 1, link_info
.path_separator
);
304 /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path,
305 return whether F matches FILE_SPEC. */
308 input_statement_is_archive_path (const char *file_spec
, char *sep
,
309 lang_input_statement_type
*f
)
314 || name_match (sep
+ 1, f
->filename
) == 0)
315 && ((sep
!= file_spec
)
316 == (f
->the_bfd
!= NULL
&& f
->the_bfd
->my_archive
!= NULL
)))
320 if (sep
!= file_spec
)
322 const char *aname
= bfd_get_filename (f
->the_bfd
->my_archive
);
324 match
= name_match (file_spec
, aname
) == 0;
325 *sep
= link_info
.path_separator
;
332 unique_section_p (const asection
*sec
,
333 const lang_output_section_statement_type
*os
)
335 struct unique_sections
*unam
;
338 if (!link_info
.resolve_section_groups
339 && sec
->owner
!= NULL
340 && bfd_is_group_section (sec
->owner
, sec
))
342 && strcmp (os
->name
, DISCARD_SECTION_NAME
) == 0);
345 for (unam
= unique_section_list
; unam
; unam
= unam
->next
)
346 if (name_match (unam
->name
, secnam
) == 0)
352 /* Generic traversal routines for finding matching sections. */
354 /* Return true if FILE matches a pattern in EXCLUDE_LIST, otherwise return
358 walk_wild_file_in_exclude_list (struct name_list
*exclude_list
,
359 lang_input_statement_type
*file
)
361 struct name_list
*list_tmp
;
363 for (list_tmp
= exclude_list
;
365 list_tmp
= list_tmp
->next
)
367 char *p
= archive_path (list_tmp
->name
);
371 if (input_statement_is_archive_path (list_tmp
->name
, p
, file
))
375 else if (name_match (list_tmp
->name
, file
->filename
) == 0)
378 /* FIXME: Perhaps remove the following at some stage? Matching
379 unadorned archives like this was never documented and has
380 been superceded by the archive:path syntax. */
381 else if (file
->the_bfd
!= NULL
382 && file
->the_bfd
->my_archive
!= NULL
383 && name_match (list_tmp
->name
,
384 bfd_get_filename (file
->the_bfd
->my_archive
)) == 0)
391 /* Add SECTION (from input FILE) to the list of matching sections
392 within PTR (the matching wildcard is SEC). */
395 add_matching_section (lang_wild_statement_type
*ptr
,
396 struct wildcard_list
*sec
,
398 lang_input_statement_type
*file
)
400 lang_input_matcher_type
*new_section
;
401 /* Add a section reference to the list. */
402 new_section
= new_stat (lang_input_matcher
, &ptr
->matching_sections
);
403 new_section
->section
= section
;
404 new_section
->pattern
= sec
;
405 new_section
->input_stmt
= file
;
408 /* Process section S (from input file FILE) in relation to wildcard
409 statement PTR. We already know that a prefix of the name of S matches
410 some wildcard in PTR's wildcard list. Here we check if the filename
411 matches as well (if it's specified) and if any of the wildcards in fact
415 walk_wild_section_match (lang_wild_statement_type
*ptr
,
416 lang_input_statement_type
*file
,
419 struct wildcard_list
*sec
;
420 const char *file_spec
= ptr
->filename
;
423 /* Check if filenames match. */
424 if (file_spec
== NULL
)
426 else if ((p
= archive_path (file_spec
)) != NULL
)
428 if (!input_statement_is_archive_path (file_spec
, p
, file
))
431 else if (wildcardp (file_spec
))
433 if (fnmatch (file_spec
, file
->filename
, 0) != 0)
438 /* XXX Matching against non-wildcard filename in wild statements
439 was done by going through lookup_name, which uses
440 ->local_sym_name to compare against, not ->filename. We retain
441 this behaviour even though the above code paths use filename.
442 It would be more logical to use it here as well, in which
443 case the above wildcard() arm could be folded into this by using
444 name_match. This would also solve the worry of what to do
445 about unset local_sym_name (in which case lookup_name simply adds
446 the input file again). */
447 const char *filename
= file
->local_sym_name
;
448 lang_input_statement_type
*arch_is
;
449 if (filename
&& filename_cmp (filename
, file_spec
) == 0)
451 /* FIXME: see also walk_wild_file_in_exclude_list for why we
452 also check parents BFD (local_sym_)name to match input statements
453 with unadorned archive names. */
454 else if (file
->the_bfd
455 && file
->the_bfd
->my_archive
456 && (arch_is
= bfd_usrdata (file
->the_bfd
->my_archive
))
457 && arch_is
->local_sym_name
458 && filename_cmp (arch_is
->local_sym_name
, file_spec
) == 0)
464 /* If filename is excluded we're done. */
465 if (walk_wild_file_in_exclude_list (ptr
->exclude_name_list
, file
))
468 /* Check section name against each wildcard spec. If there's no
469 wildcard all sections match. */
470 sec
= ptr
->section_list
;
472 add_matching_section (ptr
, sec
, s
, file
);
475 const char *sname
= bfd_section_name (s
);
476 for (; sec
!= NULL
; sec
= sec
->next
)
478 if (sec
->spec
.name
!= NULL
479 && spec_match (&sec
->spec
, sname
) != 0)
482 /* Don't process sections from files which were excluded. */
483 if (!walk_wild_file_in_exclude_list (sec
->spec
.exclude_name_list
,
485 add_matching_section (ptr
, sec
, s
, file
);
490 /* Return the numerical value of the init_priority attribute from
491 section name NAME. */
494 get_init_priority (const asection
*sec
)
496 const char *name
= bfd_section_name (sec
);
499 /* GCC uses the following section names for the init_priority
500 attribute with numerical values 101 to 65535 inclusive. A
501 lower value means a higher priority.
503 1: .init_array.NNNNN/.fini_array.NNNNN: Where NNNNN is the
504 decimal numerical value of the init_priority attribute.
505 The order of execution in .init_array is forward and
506 .fini_array is backward.
507 2: .ctors.NNNNN/.dtors.NNNNN: Where NNNNN is 65535 minus the
508 decimal numerical value of the init_priority attribute.
509 The order of execution in .ctors is backward and .dtors
512 .init_array.NNNNN sections would normally be placed in an output
513 .init_array section, .fini_array.NNNNN in .fini_array,
514 .ctors.NNNNN in .ctors, and .dtors.NNNNN in .dtors. This means
515 we should sort by increasing number (and could just use
516 SORT_BY_NAME in scripts). However if .ctors.NNNNN sections are
517 being placed in .init_array (which may also contain
518 .init_array.NNNNN sections) or .dtors.NNNNN sections are being
519 placed in .fini_array then we need to extract the init_priority
520 attribute and sort on that. */
521 dot
= strrchr (name
, '.');
522 if (dot
!= NULL
&& ISDIGIT (dot
[1]))
525 unsigned long init_priority
= strtoul (dot
+ 1, &end
, 10);
529 && (strncmp (name
, ".ctors", 6) == 0
530 || strncmp (name
, ".dtors", 6) == 0))
531 init_priority
= 65535 - init_priority
;
532 if (init_priority
<= INT_MAX
)
533 return init_priority
;
539 /* Compare sections ASEC and BSEC according to SORT. */
542 compare_section (sort_type sort
, asection
*asec
, asection
*bsec
, bool reversed
)
545 int a_priority
, b_priority
;
552 case by_init_priority
:
553 a_priority
= get_init_priority (asec
);
554 b_priority
= get_init_priority (bsec
);
555 if (a_priority
< 0 || b_priority
< 0)
558 ret
= b_priority
- a_priority
;
560 ret
= a_priority
- b_priority
;
566 case by_alignment_name
:
567 ret
= bfd_section_alignment (bsec
) - bfd_section_alignment (asec
);
575 ret
= strcmp (bfd_section_name (bsec
), bfd_section_name (asec
));
577 ret
= strcmp (bfd_section_name (asec
), bfd_section_name (bsec
));
580 case by_name_alignment
:
582 ret
= strcmp (bfd_section_name (bsec
), bfd_section_name (asec
));
584 ret
= strcmp (bfd_section_name (asec
), bfd_section_name (bsec
));
590 ret
= bfd_section_alignment (bsec
) - bfd_section_alignment (asec
);
597 /* PE puts the sort key in the input statement. */
600 sort_filename (bfd
*abfd
)
602 lang_input_statement_type
*is
= bfd_usrdata (abfd
);
605 return bfd_get_filename (abfd
);
608 /* Handle wildcard sorting. This returns the place in a binary search tree
609 where this FILE:SECTION should be inserted for wild statement WILD where
610 the spec SEC was the matching one. The tree is later linearized. */
612 static lang_section_bst_type
**
613 wild_sort (lang_wild_statement_type
*wild
,
614 struct wildcard_list
*sec
,
615 lang_input_statement_type
*file
,
618 lang_section_bst_type
**tree
;
620 if (!wild
->filenames_sorted
621 && (sec
== NULL
|| sec
->spec
.sorted
== none
622 || sec
->spec
.sorted
== by_none
))
624 /* We might be called even if _this_ spec doesn't need sorting,
625 in which case we simply append at the right end of tree. */
626 return wild
->rightmost
;
632 /* Sorting by filename takes precedence over sorting by section
635 if (wild
->filenames_sorted
)
640 asection
*lsec
= (*tree
)->section
;
642 /* The PE support for the .idata section as generated by
643 dlltool assumes that files will be sorted by the name of
644 the archive and then the name of the file within the
647 fa
= file
->the_bfd
->my_archive
!= NULL
;
649 fn
= sort_filename (file
->the_bfd
->my_archive
);
651 fn
= sort_filename (file
->the_bfd
);
653 la
= lsec
->owner
->my_archive
!= NULL
;
655 ln
= sort_filename (lsec
->owner
->my_archive
);
657 ln
= sort_filename (lsec
->owner
);
659 if (wild
->filenames_reversed
)
660 i
= filename_cmp (ln
, fn
);
662 i
= filename_cmp (fn
, ln
);
665 { tree
= &((*tree
)->right
); continue; }
667 { tree
= &((*tree
)->left
); continue; }
672 fn
= sort_filename (file
->the_bfd
);
674 ln
= sort_filename (lsec
->owner
);
676 if (wild
->filenames_reversed
)
677 i
= filename_cmp (ln
, fn
);
679 i
= filename_cmp (fn
, ln
);
682 { tree
= &((*tree
)->right
); continue; }
684 { tree
= &((*tree
)->left
); continue; }
688 /* Here either the files are not sorted by name, or we are
689 looking at the sections for this file. */
691 /* Find the correct node to append this section. */
692 if (sec
&& sec
->spec
.sorted
!= none
&& sec
->spec
.sorted
!= by_none
693 && compare_section (sec
->spec
.sorted
, section
, (*tree
)->section
, sec
->spec
.reversed
) < 0)
694 tree
= &((*tree
)->left
);
696 tree
= &((*tree
)->right
);
702 /* Use wild_sort to build a BST to sort sections. */
705 output_section_callback_sort (lang_wild_statement_type
*ptr
,
706 struct wildcard_list
*sec
,
708 lang_input_statement_type
*file
,
711 lang_section_bst_type
*node
;
712 lang_section_bst_type
**tree
;
713 lang_output_section_statement_type
*os
;
715 os
= (lang_output_section_statement_type
*) output
;
717 if (unique_section_p (section
, os
))
720 /* Don't add sections to the tree when we already know that
721 lang_add_section won't do anything with it. */
722 if (wont_add_section_p (section
, os
))
725 node
= (lang_section_bst_type
*) xmalloc (sizeof (lang_section_bst_type
));
728 node
->section
= section
;
729 node
->pattern
= ptr
->section_list
;
731 tree
= wild_sort (ptr
, sec
, file
, section
);
735 if (tree
== ptr
->rightmost
)
736 ptr
->rightmost
= &node
->right
;
740 /* Convert a sorted sections' BST back to list form. */
743 output_section_callback_tree_to_list (lang_wild_statement_type
*ptr
,
744 lang_section_bst_type
*tree
,
748 output_section_callback_tree_to_list (ptr
, tree
->left
, output
);
750 lang_add_section (&ptr
->children
, tree
->section
, tree
->pattern
,
751 ptr
->section_flag_list
,
752 (lang_output_section_statement_type
*) output
);
755 output_section_callback_tree_to_list (ptr
, tree
->right
, output
);
761 /* Sections are matched against wildcard statements via a prefix tree.
762 The prefix tree holds prefixes of all matching patterns (up to the first
763 wildcard character), and the wild statement from which those patterns
764 came. When matching a section name against the tree we're walking through
765 the tree character by character. Each statement we hit is one that
766 potentially matches. This is checked by actually going through the
767 (glob) matching routines.
769 When the section name turns out to actually match we record that section
770 in the wild statements list of matching sections. */
772 /* A prefix can be matched by multiple statement, so we need a list of them. */
773 struct wild_stmt_list
775 lang_wild_statement_type
*stmt
;
776 struct wild_stmt_list
*next
;
779 /* The prefix tree itself. */
782 /* The list of all children (linked via .next). */
783 struct prefixtree
*child
;
784 struct prefixtree
*next
;
785 /* This tree node is responsible for the prefix of parent plus 'c'. */
787 /* The statements that potentially can match this prefix. */
788 struct wild_stmt_list
*stmt
;
791 /* We always have a root node in the prefix tree. It corresponds to the
792 empty prefix. E.g. a glob like "*" would sit in this root. */
793 static struct prefixtree the_root
, *ptroot
= &the_root
;
795 /* Given a prefix tree in *TREE, corresponding to prefix P, find or
796 INSERT the tree node corresponding to prefix P+C. */
798 static struct prefixtree
*
799 get_prefix_tree (struct prefixtree
**tree
, char c
, bool insert
)
801 struct prefixtree
*t
;
802 for (t
= *tree
; t
; t
= t
->next
)
807 t
= (struct prefixtree
*) obstack_alloc (&pt_obstack
, sizeof *t
);
816 /* Add STMT to the set of statements that can be matched by the prefix
817 corresponding to prefix tree T. */
820 pt_add_stmt (struct prefixtree
*t
, lang_wild_statement_type
*stmt
)
822 struct wild_stmt_list
*sl
, **psl
;
823 sl
= (struct wild_stmt_list
*) obstack_alloc (&pt_obstack
, sizeof *sl
);
832 /* Insert STMT into the global prefix tree. */
835 insert_prefix_tree (lang_wild_statement_type
*stmt
)
837 struct wildcard_list
*sec
;
838 struct prefixtree
*t
;
840 if (!stmt
->section_list
)
842 /* If we have no section_list (no wildcards in the wild STMT),
843 then every section name will match, so add this to the root. */
844 pt_add_stmt (ptroot
, stmt
);
848 for (sec
= stmt
->section_list
; sec
; sec
= sec
->next
)
850 const char *name
= sec
->spec
.name
? sec
->spec
.name
: "*";
853 for (; (c
= *name
); name
++)
855 if (c
== '*' || c
== '[' || c
== '?')
857 t
= get_prefix_tree (&t
->child
, c
, true);
859 /* If we hit a glob character, the matching prefix is what we saw
860 until now. If we hit the end of pattern (hence it's no glob) then
861 we can do better: we only need to record a match when a section name
862 completely matches, not merely a prefix, so record the trailing 0
865 t
= get_prefix_tree (&t
->child
, 0, true);
866 pt_add_stmt (t
, stmt
);
870 /* Dump T indented by INDENT spaces. */
873 debug_prefix_tree_rec (struct prefixtree
*t
, int indent
)
875 for (; t
; t
= t
->next
)
877 struct wild_stmt_list
*sl
;
878 printf ("%*s %c", indent
, "", t
->c
);
879 for (sl
= t
->stmt
; sl
; sl
= sl
->next
)
881 struct wildcard_list
*curr
;
882 printf (" %p ", sl
->stmt
);
883 for (curr
= sl
->stmt
->section_list
; curr
; curr
= curr
->next
)
884 printf ("%s ", curr
->spec
.name
? curr
->spec
.name
: "*");
887 debug_prefix_tree_rec (t
->child
, indent
+ 2);
891 /* Dump the global prefix tree. */
894 debug_prefix_tree (void)
896 debug_prefix_tree_rec (ptroot
, 2);
899 /* Like strcspn() but start to look from the end to beginning of
900 S. Returns the length of the suffix of S consisting entirely
901 of characters not in REJECT. */
904 rstrcspn (const char *s
, const char *reject
)
906 size_t len
= strlen (s
), sufflen
= 0;
910 if (strchr (reject
, c
) != 0)
917 /* Analyze the wildcards in wild statement PTR to setup various
918 things for quick matching. */
921 analyze_walk_wild_section_handler (lang_wild_statement_type
*ptr
)
923 struct wildcard_list
*sec
;
926 ptr
->rightmost
= &ptr
->tree
;
928 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
932 sec
->spec
.namelen
= strlen (sec
->spec
.name
);
933 sec
->spec
.prefixlen
= strcspn (sec
->spec
.name
, "?*[");
934 sec
->spec
.suffixlen
= rstrcspn (sec
->spec
.name
+ sec
->spec
.prefixlen
,
938 sec
->spec
.namelen
= sec
->spec
.prefixlen
= sec
->spec
.suffixlen
= 0;
941 insert_prefix_tree (ptr
);
944 /* Match all sections from FILE against the global prefix tree,
945 and record them into each wild statement that has a match. */
948 resolve_wild_sections (lang_input_statement_type
*file
)
952 if (file
->flags
.just_syms
)
955 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
957 const char *sname
= bfd_section_name (s
);
959 struct prefixtree
*t
= ptroot
;
960 //printf (" YYY consider %s of %s\n", sname, file->the_bfd->filename);
965 struct wild_stmt_list
*sl
;
966 for (sl
= t
->stmt
; sl
; sl
= sl
->next
)
968 walk_wild_section_match (sl
->stmt
, file
, s
);
969 //printf (" ZZZ maybe place into %p\n", sl->stmt);
975 t
= get_prefix_tree (&t
->child
, c
, false);
981 /* Match all sections from all input files against the global prefix tree. */
986 LANG_FOR_EACH_INPUT_STATEMENT (f
)
988 //printf("XXX %s\n", f->filename);
989 if (f
->the_bfd
== NULL
990 || !bfd_check_format (f
->the_bfd
, bfd_archive
))
991 resolve_wild_sections (f
);
996 /* This is an archive file. We must map each member of the
997 archive separately. */
998 member
= bfd_openr_next_archived_file (f
->the_bfd
, NULL
);
999 while (member
!= NULL
)
1001 /* When lookup_name is called, it will call the add_symbols
1002 entry point for the archive. For each element of the
1003 archive which is included, BFD will call ldlang_add_file,
1004 which will set the usrdata field of the member to the
1005 lang_input_statement. */
1006 if (bfd_usrdata (member
) != NULL
)
1007 resolve_wild_sections (bfd_usrdata (member
));
1009 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
1015 /* For each input section that matches wild statement S calls
1016 CALLBACK with DATA. */
1019 walk_wild (lang_wild_statement_type
*s
, callback_t callback
, void *data
)
1021 lang_statement_union_type
*l
;
1023 for (l
= s
->matching_sections
.head
; l
; l
= l
->header
.next
)
1025 (*callback
) (s
, l
->input_matcher
.pattern
, l
->input_matcher
.section
,
1026 l
->input_matcher
.input_stmt
, data
);
1030 /* lang_for_each_statement walks the parse tree and calls the provided
1031 function for each node, except those inside output section statements
1032 with constraint set to -1. */
1035 lang_for_each_statement_worker (void (*func
) (lang_statement_union_type
*),
1036 lang_statement_union_type
*s
)
1038 for (; s
!= NULL
; s
= s
->header
.next
)
1042 switch (s
->header
.type
)
1044 case lang_constructors_statement_enum
:
1045 lang_for_each_statement_worker (func
, constructor_list
.head
);
1047 case lang_output_section_statement_enum
:
1048 if (s
->output_section_statement
.constraint
!= -1)
1049 lang_for_each_statement_worker
1050 (func
, s
->output_section_statement
.children
.head
);
1052 case lang_wild_statement_enum
:
1053 lang_for_each_statement_worker (func
,
1054 s
->wild_statement
.children
.head
);
1056 case lang_group_statement_enum
:
1057 lang_for_each_statement_worker (func
,
1058 s
->group_statement
.children
.head
);
1060 case lang_data_statement_enum
:
1061 case lang_reloc_statement_enum
:
1062 case lang_object_symbols_statement_enum
:
1063 case lang_output_statement_enum
:
1064 case lang_target_statement_enum
:
1065 case lang_input_section_enum
:
1066 case lang_input_statement_enum
:
1067 case lang_assignment_statement_enum
:
1068 case lang_padding_statement_enum
:
1069 case lang_address_statement_enum
:
1070 case lang_fill_statement_enum
:
1071 case lang_insert_statement_enum
:
1081 lang_for_each_statement (void (*func
) (lang_statement_union_type
*))
1083 lang_for_each_statement_worker (func
, statement_list
.head
);
1086 /*----------------------------------------------------------------------*/
1089 lang_list_init (lang_statement_list_type
*list
)
1092 list
->tail
= &list
->head
;
1096 lang_statement_append (lang_statement_list_type
*list
,
1100 *(list
->tail
) = element
;
1105 push_stat_ptr (lang_statement_list_type
*new_ptr
)
1107 if (stat_save_ptr
>= stat_save
+ sizeof (stat_save
) / sizeof (stat_save
[0]))
1109 *stat_save_ptr
++ = stat_ptr
;
1116 if (stat_save_ptr
<= stat_save
)
1118 stat_ptr
= *--stat_save_ptr
;
1121 /* Build a new statement node for the parse tree. */
1123 static lang_statement_union_type
*
1124 new_statement (enum statement_enum type
,
1126 lang_statement_list_type
*list
)
1128 lang_statement_union_type
*new_stmt
;
1130 new_stmt
= stat_alloc (size
);
1131 new_stmt
->header
.type
= type
;
1132 new_stmt
->header
.next
= NULL
;
1133 lang_statement_append (list
, new_stmt
, &new_stmt
->header
.next
);
1137 /* Build a new input file node for the language. There are several
1138 ways in which we treat an input file, eg, we only look at symbols,
1139 or prefix it with a -l etc.
1141 We can be supplied with requests for input files more than once;
1142 they may, for example be split over several lines like foo.o(.text)
1143 foo.o(.data) etc, so when asked for a file we check that we haven't
1144 got it already so we don't duplicate the bfd. */
1146 static lang_input_statement_type
*
1147 new_afile (const char *name
,
1148 lang_input_file_enum_type file_type
,
1150 const char *from_filename
)
1152 lang_input_statement_type
*p
;
1154 lang_has_input_file
= true;
1156 /* PR 30632: It is OK for name to be NULL. For example
1157 see the initialization of first_file in lang_init(). */
1160 name
= ldfile_possibly_remap_input (name
);
1161 /* But if a name is remapped to NULL, it should be ignored. */
1166 p
= new_stat (lang_input_statement
, stat_ptr
);
1167 memset (&p
->the_bfd
, 0,
1168 sizeof (*p
) - offsetof (lang_input_statement_type
, the_bfd
));
1169 p
->extra_search_path
= NULL
;
1171 p
->flags
.dynamic
= input_flags
.dynamic
;
1172 p
->flags
.add_DT_NEEDED_for_dynamic
= input_flags
.add_DT_NEEDED_for_dynamic
;
1173 p
->flags
.add_DT_NEEDED_for_regular
= input_flags
.add_DT_NEEDED_for_regular
;
1174 p
->flags
.whole_archive
= input_flags
.whole_archive
;
1175 p
->flags
.sysrooted
= input_flags
.sysrooted
;
1180 case lang_input_file_is_symbols_only_enum
:
1182 p
->local_sym_name
= name
;
1183 p
->flags
.real
= true;
1184 p
->flags
.just_syms
= true;
1186 case lang_input_file_is_fake_enum
:
1188 p
->local_sym_name
= name
;
1190 case lang_input_file_is_l_enum
:
1191 if (name
[0] == ':' && name
[1] != '\0')
1193 p
->filename
= name
+ 1;
1194 p
->flags
.full_name_provided
= true;
1198 p
->local_sym_name
= concat ("-l", name
, (const char *) NULL
);
1199 p
->flags
.maybe_archive
= true;
1200 p
->flags
.real
= true;
1201 p
->flags
.search_dirs
= true;
1203 case lang_input_file_is_marker_enum
:
1205 p
->local_sym_name
= name
;
1206 p
->flags
.search_dirs
= true;
1208 case lang_input_file_is_search_file_enum
:
1210 p
->local_sym_name
= name
;
1211 /* If name is a relative path, search the directory of the current linker
1213 if (from_filename
&& !IS_ABSOLUTE_PATH (name
))
1214 p
->extra_search_path
= ldirname (from_filename
);
1215 p
->flags
.real
= true;
1216 p
->flags
.search_dirs
= true;
1218 case lang_input_file_is_file_enum
:
1220 p
->local_sym_name
= name
;
1221 p
->flags
.real
= true;
1227 lang_statement_append (&input_file_chain
, p
, &p
->next_real_file
);
1231 lang_input_statement_type
*
1232 lang_add_input_file (const char *name
,
1233 lang_input_file_enum_type file_type
,
1237 && (*name
== '=' || startswith (name
, "$SYSROOT")))
1239 lang_input_statement_type
*ret
;
1240 char *sysrooted_name
1241 = concat (ld_sysroot
,
1242 name
+ (*name
== '=' ? 1 : strlen ("$SYSROOT")),
1243 (const char *) NULL
);
1245 /* We've now forcibly prepended the sysroot, making the input
1246 file independent of the context. Therefore, temporarily
1247 force a non-sysrooted context for this statement, so it won't
1248 get the sysroot prepended again when opened. (N.B. if it's a
1249 script, any child nodes with input files starting with "/"
1250 will be handled as "sysrooted" as they'll be found to be
1251 within the sysroot subdirectory.) */
1252 unsigned int outer_sysrooted
= input_flags
.sysrooted
;
1253 input_flags
.sysrooted
= 0;
1254 ret
= new_afile (sysrooted_name
, file_type
, target
, NULL
);
1255 input_flags
.sysrooted
= outer_sysrooted
;
1259 return new_afile (name
, file_type
, target
, current_input_file
);
1262 struct out_section_hash_entry
1264 struct bfd_hash_entry root
;
1265 lang_statement_union_type s
;
1268 /* The hash table. */
1270 static struct bfd_hash_table output_section_statement_table
;
1272 /* Support routines for the hash table used by lang_output_section_find,
1273 initialize the table, fill in an entry and remove the table. */
1275 static struct bfd_hash_entry
*
1276 output_section_statement_newfunc (struct bfd_hash_entry
*entry
,
1277 struct bfd_hash_table
*table
,
1280 lang_output_section_statement_type
**nextp
;
1281 struct out_section_hash_entry
*ret
;
1285 entry
= (struct bfd_hash_entry
*) bfd_hash_allocate (table
,
1291 entry
= bfd_hash_newfunc (entry
, table
, string
);
1295 ret
= (struct out_section_hash_entry
*) entry
;
1296 memset (&ret
->s
, 0, sizeof (ret
->s
));
1297 ret
->s
.header
.type
= lang_output_section_statement_enum
;
1298 ret
->s
.output_section_statement
.subsection_alignment
= NULL
;
1299 ret
->s
.output_section_statement
.section_alignment
= NULL
;
1300 ret
->s
.output_section_statement
.block_value
= 1;
1301 lang_list_init (&ret
->s
.output_section_statement
.children
);
1302 lang_statement_append (stat_ptr
, &ret
->s
, &ret
->s
.header
.next
);
1304 /* For every output section statement added to the list, except the
1305 first one, lang_os_list.tail points to the "next"
1306 field of the last element of the list. */
1307 if (lang_os_list
.head
!= NULL
)
1308 ret
->s
.output_section_statement
.prev
1309 = ((lang_output_section_statement_type
*)
1310 ((char *) lang_os_list
.tail
1311 - offsetof (lang_output_section_statement_type
, next
)));
1313 /* GCC's strict aliasing rules prevent us from just casting the
1314 address, so we store the pointer in a variable and cast that
1316 nextp
= &ret
->s
.output_section_statement
.next
;
1317 lang_statement_append (&lang_os_list
, &ret
->s
, nextp
);
1322 output_section_statement_table_init (void)
1324 if (!bfd_hash_table_init_n (&output_section_statement_table
,
1325 output_section_statement_newfunc
,
1326 sizeof (struct out_section_hash_entry
),
1328 einfo (_("%F%P: can not create hash table: %E\n"));
1332 output_section_statement_table_free (void)
1334 bfd_hash_table_free (&output_section_statement_table
);
1337 /* Build enough state so that the parser can build its tree. */
1342 obstack_begin (&stat_obstack
, 1000);
1343 obstack_init (&pt_obstack
);
1345 stat_ptr
= &statement_list
;
1347 output_section_statement_table_init ();
1349 lang_list_init (stat_ptr
);
1351 lang_list_init (&input_file_chain
);
1352 lang_list_init (&lang_os_list
);
1353 lang_list_init (&file_chain
);
1354 first_file
= lang_add_input_file (NULL
, lang_input_file_is_marker_enum
,
1356 abs_output_section
=
1357 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
, 0, 1);
1359 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
1361 asneeded_list_head
= NULL
;
1362 asneeded_list_tail
= &asneeded_list_head
;
1368 output_section_statement_table_free ();
1369 ldfile_remap_input_free ();
1372 /*----------------------------------------------------------------------
1373 A region is an area of memory declared with the
1374 MEMORY { name:org=exp, len=exp ... }
1377 We maintain a list of all the regions here.
1379 If no regions are specified in the script, then the default is used
1380 which is created when looked up to be the entire data space.
1382 If create is true we are creating a region inside a MEMORY block.
1383 In this case it is probably an error to create a region that has
1384 already been created. If we are not inside a MEMORY block it is
1385 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
1386 and so we issue a warning.
1388 Each region has at least one name. The first name is either
1389 DEFAULT_MEMORY_REGION or the name given in the MEMORY block. You can add
1390 alias names to an existing region within a script with
1391 REGION_ALIAS (alias, region_name). Each name corresponds to at most one
1394 static lang_memory_region_type
*lang_memory_region_list
;
1395 static lang_memory_region_type
**lang_memory_region_list_tail
1396 = &lang_memory_region_list
;
1398 lang_memory_region_type
*
1399 lang_memory_region_lookup (const char *const name
, bool create
)
1401 lang_memory_region_name
*n
;
1402 lang_memory_region_type
*r
;
1403 lang_memory_region_type
*new_region
;
1405 /* NAME is NULL for LMA memspecs if no region was specified. */
1409 for (r
= lang_memory_region_list
; r
!= NULL
; r
= r
->next
)
1410 for (n
= &r
->name_list
; n
!= NULL
; n
= n
->next
)
1411 if (strcmp (n
->name
, name
) == 0)
1414 einfo (_("%P:%pS: warning: redeclaration of memory region `%s'\n"),
1419 if (!create
&& strcmp (name
, DEFAULT_MEMORY_REGION
))
1420 einfo (_("%P:%pS: warning: memory region `%s' not declared\n"),
1423 new_region
= stat_alloc (sizeof (lang_memory_region_type
));
1425 new_region
->name_list
.name
= xstrdup (name
);
1426 new_region
->name_list
.next
= NULL
;
1427 new_region
->next
= NULL
;
1428 new_region
->origin_exp
= NULL
;
1429 new_region
->origin
= 0;
1430 new_region
->length_exp
= NULL
;
1431 new_region
->length
= ~(bfd_size_type
) 0;
1432 new_region
->current
= 0;
1433 new_region
->last_os
= NULL
;
1434 new_region
->flags
= 0;
1435 new_region
->not_flags
= 0;
1436 new_region
->had_full_message
= false;
1438 *lang_memory_region_list_tail
= new_region
;
1439 lang_memory_region_list_tail
= &new_region
->next
;
1445 lang_memory_region_alias (const char *alias
, const char *region_name
)
1447 lang_memory_region_name
*n
;
1448 lang_memory_region_type
*r
;
1449 lang_memory_region_type
*region
;
1451 /* The default region must be unique. This ensures that it is not necessary
1452 to iterate through the name list if someone wants the check if a region is
1453 the default memory region. */
1454 if (strcmp (region_name
, DEFAULT_MEMORY_REGION
) == 0
1455 || strcmp (alias
, DEFAULT_MEMORY_REGION
) == 0)
1456 einfo (_("%F%P:%pS: error: alias for default memory region\n"), NULL
);
1458 /* Look for the target region and check if the alias is not already
1461 for (r
= lang_memory_region_list
; r
!= NULL
; r
= r
->next
)
1462 for (n
= &r
->name_list
; n
!= NULL
; n
= n
->next
)
1464 if (region
== NULL
&& strcmp (n
->name
, region_name
) == 0)
1466 if (strcmp (n
->name
, alias
) == 0)
1467 einfo (_("%F%P:%pS: error: redefinition of memory region "
1472 /* Check if the target region exists. */
1474 einfo (_("%F%P:%pS: error: memory region `%s' "
1475 "for alias `%s' does not exist\n"),
1476 NULL
, region_name
, alias
);
1478 /* Add alias to region name list. */
1479 n
= stat_alloc (sizeof (lang_memory_region_name
));
1480 n
->name
= xstrdup (alias
);
1481 n
->next
= region
->name_list
.next
;
1482 region
->name_list
.next
= n
;
1485 static lang_memory_region_type
*
1486 lang_memory_default (asection
*section
)
1488 lang_memory_region_type
*p
;
1490 flagword sec_flags
= section
->flags
;
1492 /* Override SEC_DATA to mean a writable section. */
1493 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
1494 sec_flags
|= SEC_DATA
;
1496 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
1498 if ((p
->flags
& sec_flags
) != 0
1499 && (p
->not_flags
& sec_flags
) == 0)
1504 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, false);
1507 /* Get the output section statement directly from the userdata. */
1509 lang_output_section_statement_type
*
1510 lang_output_section_get (const asection
*output_section
)
1512 return bfd_section_userdata (output_section
);
1515 /* Find or create an output_section_statement with the given NAME.
1516 If CONSTRAINT is non-zero match one with that constraint, otherwise
1517 match any non-negative constraint. If CREATE is 0 return NULL when
1518 no match exists. If CREATE is 1, create an output_section_statement
1519 when no match exists or if CONSTRAINT is SPECIAL. If CREATE is 2,
1520 always make a new output_section_statement. */
1522 lang_output_section_statement_type
*
1523 lang_output_section_statement_lookup (const char *name
,
1527 struct out_section_hash_entry
*entry
;
1529 entry
= ((struct out_section_hash_entry
*)
1530 bfd_hash_lookup (&output_section_statement_table
, name
,
1531 create
!= 0, false));
1535 einfo (_("%F%P: failed creating section `%s': %E\n"), name
);
1539 if (entry
->s
.output_section_statement
.name
!= NULL
)
1541 /* We have a section of this name, but it might not have the correct
1543 struct out_section_hash_entry
*last_ent
;
1545 name
= entry
->s
.output_section_statement
.name
;
1549 && !(create
&& constraint
== SPECIAL
)
1550 && (constraint
== entry
->s
.output_section_statement
.constraint
1552 && entry
->s
.output_section_statement
.constraint
>= 0)))
1553 return &entry
->s
.output_section_statement
;
1555 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1557 while (entry
!= NULL
1558 && name
== entry
->s
.output_section_statement
.name
);
1564 = ((struct out_section_hash_entry
*)
1565 output_section_statement_newfunc (NULL
,
1566 &output_section_statement_table
,
1570 einfo (_("%F%P: failed creating section `%s': %E\n"), name
);
1573 entry
->root
= last_ent
->root
;
1574 last_ent
->root
.next
= &entry
->root
;
1577 entry
->s
.output_section_statement
.name
= name
;
1578 entry
->s
.output_section_statement
.constraint
= constraint
;
1579 entry
->s
.output_section_statement
.dup_output
= (create
== 2
1580 || constraint
== SPECIAL
);
1581 return &entry
->s
.output_section_statement
;
1584 /* Find the next output_section_statement with the same name as OS.
1585 If CONSTRAINT is non-zero, find one with that constraint otherwise
1586 match any non-negative constraint. */
1588 lang_output_section_statement_type
*
1589 next_matching_output_section_statement (lang_output_section_statement_type
*os
,
1592 /* All output_section_statements are actually part of a
1593 struct out_section_hash_entry. */
1594 struct out_section_hash_entry
*entry
= (struct out_section_hash_entry
*)
1596 - offsetof (struct out_section_hash_entry
, s
.output_section_statement
));
1597 const char *name
= os
->name
;
1599 ASSERT (name
== entry
->root
.string
);
1602 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1604 || name
!= entry
->s
.output_section_statement
.name
)
1607 while (constraint
!= entry
->s
.output_section_statement
.constraint
1609 || entry
->s
.output_section_statement
.constraint
< 0));
1611 return &entry
->s
.output_section_statement
;
1614 /* A variant of lang_output_section_find used by place_orphan.
1615 Returns the output statement that should precede a new output
1616 statement for SEC. If an exact match is found on certain flags,
1619 lang_output_section_statement_type
*
1620 lang_output_section_find_by_flags (const asection
*sec
,
1622 lang_output_section_statement_type
**exact
,
1623 lang_match_sec_type_func match_type
)
1625 lang_output_section_statement_type
*first
, *look
, *found
;
1626 flagword look_flags
, differ
;
1628 /* We know the first statement on this list is *ABS*. May as well
1630 first
= (void *) lang_os_list
.head
;
1631 first
= first
->next
;
1633 /* First try for an exact match. */
1635 for (look
= first
; look
; look
= look
->next
)
1637 look_flags
= look
->flags
;
1638 if (look
->bfd_section
!= NULL
)
1640 look_flags
= look
->bfd_section
->flags
;
1641 if (match_type
&& !match_type (link_info
.output_bfd
,
1646 differ
= look_flags
^ sec_flags
;
1647 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
1648 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1658 if ((sec_flags
& SEC_CODE
) != 0
1659 && (sec_flags
& SEC_ALLOC
) != 0)
1661 /* Try for a rw code section. */
1662 for (look
= first
; look
; look
= look
->next
)
1664 look_flags
= look
->flags
;
1665 if (look
->bfd_section
!= NULL
)
1667 look_flags
= look
->bfd_section
->flags
;
1668 if (match_type
&& !match_type (link_info
.output_bfd
,
1673 differ
= look_flags
^ sec_flags
;
1674 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1675 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1679 else if ((sec_flags
& SEC_READONLY
) != 0
1680 && (sec_flags
& SEC_ALLOC
) != 0)
1682 /* .rodata can go after .text, .sdata2 after .rodata. */
1683 for (look
= first
; look
; look
= look
->next
)
1685 look_flags
= look
->flags
;
1686 if (look
->bfd_section
!= NULL
)
1688 look_flags
= look
->bfd_section
->flags
;
1689 if (match_type
&& !match_type (link_info
.output_bfd
,
1694 differ
= look_flags
^ sec_flags
;
1695 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1696 | SEC_READONLY
| SEC_SMALL_DATA
))
1697 || (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1699 && !(look_flags
& SEC_SMALL_DATA
)))
1703 else if ((sec_flags
& SEC_THREAD_LOCAL
) != 0
1704 && (sec_flags
& SEC_ALLOC
) != 0)
1706 /* .tdata can go after .data, .tbss after .tdata. Treat .tbss
1707 as if it were a loaded section, and don't use match_type. */
1708 bool seen_thread_local
= false;
1711 for (look
= first
; look
; look
= look
->next
)
1713 look_flags
= look
->flags
;
1714 if (look
->bfd_section
!= NULL
)
1715 look_flags
= look
->bfd_section
->flags
;
1717 differ
= look_flags
^ (sec_flags
| SEC_LOAD
| SEC_HAS_CONTENTS
);
1718 if (!(differ
& (SEC_THREAD_LOCAL
| SEC_ALLOC
)))
1720 /* .tdata and .tbss must be adjacent and in that order. */
1721 if (!(look_flags
& SEC_LOAD
)
1722 && (sec_flags
& SEC_LOAD
))
1723 /* ..so if we're at a .tbss section and we're placing
1724 a .tdata section stop looking and return the
1725 previous section. */
1728 seen_thread_local
= true;
1730 else if (seen_thread_local
)
1732 else if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
)))
1736 else if ((sec_flags
& SEC_SMALL_DATA
) != 0
1737 && (sec_flags
& SEC_ALLOC
) != 0)
1739 /* .sdata goes after .data, .sbss after .sdata. */
1740 for (look
= first
; look
; look
= look
->next
)
1742 look_flags
= look
->flags
;
1743 if (look
->bfd_section
!= NULL
)
1745 look_flags
= look
->bfd_section
->flags
;
1746 if (match_type
&& !match_type (link_info
.output_bfd
,
1751 differ
= look_flags
^ sec_flags
;
1752 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1753 | SEC_THREAD_LOCAL
))
1754 || ((look_flags
& SEC_SMALL_DATA
)
1755 && !(sec_flags
& SEC_HAS_CONTENTS
)))
1759 else if ((sec_flags
& SEC_HAS_CONTENTS
) != 0
1760 && (sec_flags
& SEC_ALLOC
) != 0)
1762 /* .data goes after .rodata. */
1763 for (look
= first
; look
; look
= look
->next
)
1765 look_flags
= look
->flags
;
1766 if (look
->bfd_section
!= NULL
)
1768 look_flags
= look
->bfd_section
->flags
;
1769 if (match_type
&& !match_type (link_info
.output_bfd
,
1774 differ
= look_flags
^ sec_flags
;
1775 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1776 | SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1780 else if ((sec_flags
& SEC_ALLOC
) != 0)
1782 /* .bss goes after any other alloc section. */
1783 for (look
= first
; look
; look
= look
->next
)
1785 look_flags
= look
->flags
;
1786 if (look
->bfd_section
!= NULL
)
1788 look_flags
= look
->bfd_section
->flags
;
1789 if (match_type
&& !match_type (link_info
.output_bfd
,
1794 differ
= look_flags
^ sec_flags
;
1795 if (!(differ
& SEC_ALLOC
))
1801 /* non-alloc go last. */
1802 for (look
= first
; look
; look
= look
->next
)
1804 look_flags
= look
->flags
;
1805 if (look
->bfd_section
!= NULL
)
1806 look_flags
= look
->bfd_section
->flags
;
1807 differ
= look_flags
^ sec_flags
;
1808 if (!(differ
& SEC_DEBUGGING
))
1814 if (found
|| !match_type
)
1817 return lang_output_section_find_by_flags (sec
, sec_flags
, NULL
, NULL
);
1820 /* Find the last output section before given output statement.
1821 Used by place_orphan. */
1824 output_prev_sec_find (lang_output_section_statement_type
*os
)
1826 lang_output_section_statement_type
*lookup
;
1828 for (lookup
= os
->prev
; lookup
!= NULL
; lookup
= lookup
->prev
)
1830 if (lookup
->constraint
< 0)
1833 if (lookup
->bfd_section
!= NULL
)
1834 return lookup
->bfd_section
;
1840 /* Look for a suitable place for a new output section statement. The
1841 idea is to skip over anything that might be inside a SECTIONS {}
1842 statement in a script, before we find another output section
1843 statement. Assignments to "dot" before an output section statement
1844 are assumed to belong to it, except in two cases; The first
1845 assignment to dot, and assignments before non-alloc sections.
1846 Otherwise we might put an orphan before . = . + SIZEOF_HEADERS or
1847 similar assignments that set the initial address, or we might
1848 insert non-alloc note sections among assignments setting end of
1851 static lang_statement_union_type
**
1852 insert_os_after (lang_statement_union_type
*after
)
1854 lang_statement_union_type
**where
;
1855 lang_statement_union_type
**assign
= NULL
;
1858 ignore_first
= after
== lang_os_list
.head
;
1860 for (where
= &after
->header
.next
;
1862 where
= &(*where
)->header
.next
)
1864 switch ((*where
)->header
.type
)
1866 case lang_assignment_statement_enum
:
1869 lang_assignment_statement_type
*ass
;
1871 ass
= &(*where
)->assignment_statement
;
1872 if (ass
->exp
->type
.node_class
!= etree_assert
1873 && ass
->exp
->assign
.dst
[0] == '.'
1874 && ass
->exp
->assign
.dst
[1] == 0)
1878 ignore_first
= false;
1882 case lang_wild_statement_enum
:
1883 case lang_input_section_enum
:
1884 case lang_object_symbols_statement_enum
:
1885 case lang_fill_statement_enum
:
1886 case lang_data_statement_enum
:
1887 case lang_reloc_statement_enum
:
1888 case lang_padding_statement_enum
:
1889 case lang_constructors_statement_enum
:
1891 ignore_first
= false;
1893 case lang_output_section_statement_enum
:
1896 asection
*s
= (*where
)->output_section_statement
.bfd_section
;
1899 || s
->map_head
.s
== NULL
1900 || (s
->flags
& SEC_ALLOC
) != 0)
1904 case lang_input_statement_enum
:
1905 case lang_address_statement_enum
:
1906 case lang_target_statement_enum
:
1907 case lang_output_statement_enum
:
1908 case lang_group_statement_enum
:
1909 case lang_insert_statement_enum
:
1911 case lang_input_matcher_enum
:
1920 lang_output_section_statement_type
*
1921 lang_insert_orphan (asection
*s
,
1922 const char *secname
,
1924 lang_output_section_statement_type
*after
,
1925 struct orphan_save
*place
,
1926 etree_type
*address
,
1927 lang_statement_list_type
*add_child
)
1929 lang_statement_list_type add
;
1930 lang_output_section_statement_type
*os
;
1931 lang_output_section_statement_type
**os_tail
;
1933 /* If we have found an appropriate place for the output section
1934 statements for this orphan, add them to our own private list,
1935 inserting them later into the global statement list. */
1938 lang_list_init (&add
);
1939 push_stat_ptr (&add
);
1942 if (bfd_link_relocatable (&link_info
)
1943 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0)
1944 address
= exp_intop (0);
1946 os_tail
= (lang_output_section_statement_type
**) lang_os_list
.tail
;
1947 os
= lang_enter_output_section_statement (
1948 secname
, address
, normal_section
, 0, NULL
, NULL
, NULL
, constraint
, 0);
1950 if (add_child
== NULL
)
1951 add_child
= &os
->children
;
1952 lang_add_section (add_child
, s
, NULL
, NULL
, os
);
1954 if (after
&& (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) != 0)
1956 const char *region
= (after
->region
1957 ? after
->region
->name_list
.name
1958 : DEFAULT_MEMORY_REGION
);
1959 const char *lma_region
= (after
->lma_region
1960 ? after
->lma_region
->name_list
.name
1962 lang_leave_output_section_statement (NULL
, region
, after
->phdrs
,
1966 lang_leave_output_section_statement (NULL
, DEFAULT_MEMORY_REGION
, NULL
,
1969 /* Restore the global list pointer. */
1973 if (after
!= NULL
&& os
->bfd_section
!= NULL
)
1975 asection
*snew
, *as
;
1976 bool place_after
= place
->stmt
== NULL
;
1977 bool insert_after
= true;
1979 snew
= os
->bfd_section
;
1981 /* Shuffle the bfd section list to make the output file look
1982 neater. This is really only cosmetic. */
1983 if (place
->section
== NULL
1984 && after
!= (void *) lang_os_list
.head
)
1986 asection
*bfd_section
= after
->bfd_section
;
1988 /* If the output statement hasn't been used to place any input
1989 sections (and thus doesn't have an output bfd_section),
1990 look for the closest prior output statement having an
1992 if (bfd_section
== NULL
)
1993 bfd_section
= output_prev_sec_find (after
);
1995 if (bfd_section
!= NULL
1996 && bfd_section
->owner
!= NULL
1997 && bfd_section
!= snew
)
1998 place
->section
= &bfd_section
->next
;
2001 if (place
->section
== NULL
)
2002 place
->section
= &link_info
.output_bfd
->sections
;
2004 as
= *place
->section
;
2008 /* Put the section at the end of the list. */
2010 /* Unlink the section. */
2011 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2013 /* Now tack it back on in the right place. */
2014 bfd_section_list_append (link_info
.output_bfd
, snew
);
2016 else if ((bfd_get_flavour (link_info
.output_bfd
)
2017 == bfd_target_elf_flavour
)
2018 && (bfd_get_flavour (s
->owner
)
2019 == bfd_target_elf_flavour
)
2020 && ((elf_section_type (s
) == SHT_NOTE
2021 && (s
->flags
& SEC_LOAD
) != 0)
2022 || (elf_section_type (as
) == SHT_NOTE
2023 && (as
->flags
& SEC_LOAD
) != 0)))
2025 /* Make sure that output note sections are grouped and sorted
2026 by alignments when inserting a note section or insert a
2027 section after a note section, */
2029 /* A specific section after which the output note section
2030 should be placed. */
2031 asection
*after_sec
;
2032 /* True if we need to insert the orphan section after a
2033 specific section to maintain output note section order. */
2034 bool after_sec_note
= false;
2036 static asection
*first_orphan_note
= NULL
;
2038 /* Group and sort output note section by alignments in
2041 if (elf_section_type (s
) == SHT_NOTE
2042 && (s
->flags
& SEC_LOAD
) != 0)
2044 /* Search from the beginning for the last output note
2045 section with equal or larger alignments. NB: Don't
2046 place orphan note section after non-note sections. */
2048 first_orphan_note
= NULL
;
2049 for (sec
= link_info
.output_bfd
->sections
;
2051 && !bfd_is_abs_section (sec
));
2054 && elf_section_type (sec
) == SHT_NOTE
2055 && (sec
->flags
& SEC_LOAD
) != 0)
2057 if (!first_orphan_note
)
2058 first_orphan_note
= sec
;
2059 if (sec
->alignment_power
>= s
->alignment_power
)
2062 else if (first_orphan_note
)
2064 /* Stop if there is non-note section after the first
2065 orphan note section. */
2069 /* If this will be the first orphan note section, it can
2070 be placed at the default location. */
2071 after_sec_note
= first_orphan_note
!= NULL
;
2072 if (after_sec
== NULL
&& after_sec_note
)
2074 /* If all output note sections have smaller
2075 alignments, place the section before all
2076 output orphan note sections. */
2077 after_sec
= first_orphan_note
;
2078 insert_after
= false;
2081 else if (first_orphan_note
)
2083 /* Don't place non-note sections in the middle of orphan
2085 after_sec_note
= true;
2087 for (sec
= as
->next
;
2089 && !bfd_is_abs_section (sec
));
2091 if (elf_section_type (sec
) == SHT_NOTE
2092 && (sec
->flags
& SEC_LOAD
) != 0)
2100 /* Search forward to insert OS after AFTER_SEC output
2102 lang_output_section_statement_type
*stmt
, *next
;
2104 for (stmt
= after
; stmt
!= NULL
; stmt
= next
)
2109 if (stmt
->bfd_section
== after_sec
)
2119 /* If INSERT_AFTER is FALSE, place OS before
2120 AFTER_SEC output statement. */
2121 if (next
&& next
->bfd_section
== after_sec
)
2131 /* Search backward to insert OS after AFTER_SEC output
2134 for (stmt
= after
; stmt
!= NULL
; stmt
= stmt
->prev
)
2138 if (stmt
->bfd_section
== after_sec
)
2147 /* If INSERT_AFTER is FALSE, place OS before
2148 AFTER_SEC output statement. */
2149 if (stmt
->next
->bfd_section
== after_sec
)
2159 if (after_sec
== NULL
2160 || (insert_after
&& after_sec
->next
!= snew
)
2161 || (!insert_after
&& after_sec
->prev
!= snew
))
2163 /* Unlink the section. */
2164 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2166 /* Place SNEW after AFTER_SEC. If AFTER_SEC is NULL,
2171 bfd_section_list_insert_after (link_info
.output_bfd
,
2174 bfd_section_list_insert_before (link_info
.output_bfd
,
2178 bfd_section_list_prepend (link_info
.output_bfd
, snew
);
2181 else if (as
!= snew
&& as
->prev
!= snew
)
2183 /* Unlink the section. */
2184 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2186 /* Now tack it back on in the right place. */
2187 bfd_section_list_insert_before (link_info
.output_bfd
,
2191 else if (as
!= snew
&& as
->prev
!= snew
)
2193 /* Unlink the section. */
2194 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2196 /* Now tack it back on in the right place. */
2197 bfd_section_list_insert_before (link_info
.output_bfd
, as
, snew
);
2200 /* Save the end of this list. Further ophans of this type will
2201 follow the one we've just added. */
2202 place
->section
= &snew
->next
;
2204 /* The following is non-cosmetic. We try to put the output
2205 statements in some sort of reasonable order here, because they
2206 determine the final load addresses of the orphan sections.
2207 In addition, placing output statements in the wrong order may
2208 require extra segments. For instance, given a typical
2209 situation of all read-only sections placed in one segment and
2210 following that a segment containing all the read-write
2211 sections, we wouldn't want to place an orphan read/write
2212 section before or amongst the read-only ones. */
2213 if (add
.head
!= NULL
)
2215 lang_output_section_statement_type
*newly_added_os
;
2217 /* Place OS after AFTER if AFTER_NOTE is TRUE. */
2220 lang_statement_union_type
**where
;
2222 where
= insert_os_after ((lang_statement_union_type
*) after
);
2226 place
->os_tail
= &after
->next
;
2230 /* Put it after the last orphan statement we added. */
2231 *add
.tail
= *place
->stmt
;
2232 *place
->stmt
= add
.head
;
2235 /* Fix the global list pointer if we happened to tack our
2236 new list at the tail. */
2237 if (*stat_ptr
->tail
== add
.head
)
2238 stat_ptr
->tail
= add
.tail
;
2240 /* Save the end of this list. */
2241 place
->stmt
= add
.tail
;
2243 /* Do the same for the list of output section statements. */
2244 newly_added_os
= *os_tail
;
2246 newly_added_os
->prev
= (lang_output_section_statement_type
*)
2247 ((char *) place
->os_tail
2248 - offsetof (lang_output_section_statement_type
, next
));
2249 newly_added_os
->next
= *place
->os_tail
;
2250 if (newly_added_os
->next
!= NULL
)
2251 newly_added_os
->next
->prev
= newly_added_os
;
2252 *place
->os_tail
= newly_added_os
;
2253 place
->os_tail
= &newly_added_os
->next
;
2255 /* Fixing the global list pointer here is a little different.
2256 We added to the list in lang_enter_output_section_statement,
2257 trimmed off the new output_section_statment above when
2258 assigning *os_tail = NULL, but possibly added it back in
2259 the same place when assigning *place->os_tail. */
2260 if (*os_tail
== NULL
)
2261 lang_os_list
.tail
= (lang_statement_union_type
**) os_tail
;
2268 lang_print_asneeded (void)
2270 struct asneeded_minfo
*m
;
2272 if (asneeded_list_head
== NULL
)
2275 minfo (_("\nAs-needed library included to satisfy reference by file (symbol)\n\n"));
2277 for (m
= asneeded_list_head
; m
!= NULL
; m
= m
->next
)
2281 minfo ("%s", m
->soname
);
2282 len
= strlen (m
->soname
);
2289 print_spaces (30 - len
);
2292 minfo ("%pB ", m
->ref
);
2293 minfo ("(%pT)\n", m
->name
);
2298 lang_map_flags (flagword flag
)
2300 if (flag
& SEC_ALLOC
)
2303 if (flag
& SEC_CODE
)
2306 if (flag
& SEC_READONLY
)
2309 if (flag
& SEC_DATA
)
2312 if (flag
& SEC_LOAD
)
2319 lang_memory_region_type
*m
;
2320 bool dis_header_printed
= false;
2322 ldfile_print_input_remaps ();
2324 LANG_FOR_EACH_INPUT_STATEMENT (file
)
2328 if ((file
->the_bfd
->flags
& (BFD_LINKER_CREATED
| DYNAMIC
)) != 0
2329 || file
->flags
.just_syms
)
2332 if (config
.print_map_discarded
)
2333 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
2334 if ((s
->output_section
== NULL
2335 || s
->output_section
->owner
!= link_info
.output_bfd
)
2336 && (s
->flags
& (SEC_LINKER_CREATED
| SEC_KEEP
)) == 0)
2338 if (! dis_header_printed
)
2340 minfo (_("\nDiscarded input sections\n\n"));
2341 dis_header_printed
= true;
2344 print_input_section (s
, true);
2347 if (config
.print_map_discarded
&& ! dis_header_printed
)
2348 minfo (_("\nThere are no discarded input sections\n"));
2350 minfo (_("\nMemory Configuration\n\n"));
2351 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
2352 _("Name"), _("Origin"), _("Length"), _("Attributes"));
2354 for (m
= lang_memory_region_list
; m
!= NULL
; m
= m
->next
)
2356 fprintf (config
.map_file
, "%-16s", m
->name_list
.name
);
2359 bfd_sprintf_vma (link_info
.output_bfd
, buf
, m
->origin
);
2360 fprintf (config
.map_file
, " 0x%-16s", buf
);
2361 bfd_sprintf_vma (link_info
.output_bfd
, buf
, m
->length
);
2362 fprintf (config
.map_file
,
2363 " 0x%*s", m
->flags
|| m
->not_flags
? -17 : 0, buf
);
2365 lang_map_flags (m
->flags
);
2370 lang_map_flags (m
->not_flags
);
2376 minfo (_("\nLinker script and memory map\n\n"));
2378 if (!link_info
.reduce_memory_overheads
)
2380 obstack_begin (&map_obstack
, 1000);
2381 bfd_link_hash_traverse (link_info
.hash
, sort_def_symbol
, 0);
2383 expld
.phase
= lang_fixed_phase_enum
;
2384 lang_statement_iteration
++;
2385 print_statements ();
2387 ldemul_extra_map_file_text (link_info
.output_bfd
, &link_info
,
2392 sort_def_symbol (struct bfd_link_hash_entry
*hash_entry
,
2393 void *info ATTRIBUTE_UNUSED
)
2395 if ((hash_entry
->type
== bfd_link_hash_defined
2396 || hash_entry
->type
== bfd_link_hash_defweak
)
2397 && hash_entry
->u
.def
.section
->owner
!= link_info
.output_bfd
2398 && hash_entry
->u
.def
.section
->owner
!= NULL
)
2400 input_section_userdata_type
*ud
;
2401 struct map_symbol_def
*def
;
2403 ud
= bfd_section_userdata (hash_entry
->u
.def
.section
);
2406 ud
= stat_alloc (sizeof (*ud
));
2407 bfd_set_section_userdata (hash_entry
->u
.def
.section
, ud
);
2408 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
2409 ud
->map_symbol_def_count
= 0;
2411 else if (!ud
->map_symbol_def_tail
)
2412 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
2414 def
= (struct map_symbol_def
*) obstack_alloc (&map_obstack
, sizeof *def
);
2415 def
->entry
= hash_entry
;
2416 *(ud
->map_symbol_def_tail
) = def
;
2417 ud
->map_symbol_def_tail
= &def
->next
;
2418 ud
->map_symbol_def_count
++;
2423 /* Initialize an output section. */
2426 init_os (lang_output_section_statement_type
*s
, flagword flags
)
2428 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
2429 einfo (_("%F%P: illegal use of `%s' section\n"), DISCARD_SECTION_NAME
);
2432 s
->bfd_section
= bfd_get_section_by_name (link_info
.output_bfd
, s
->name
);
2433 if (s
->bfd_section
== NULL
)
2434 s
->bfd_section
= bfd_make_section_anyway_with_flags (link_info
.output_bfd
,
2436 if (s
->bfd_section
== NULL
)
2438 einfo (_("%F%P: output format %s cannot represent section"
2439 " called %s: %E\n"),
2440 link_info
.output_bfd
->xvec
->name
, s
->name
);
2442 s
->bfd_section
->output_section
= s
->bfd_section
;
2443 s
->bfd_section
->output_offset
= 0;
2445 /* Set the userdata of the output section to the output section
2446 statement to avoid lookup. */
2447 bfd_set_section_userdata (s
->bfd_section
, s
);
2449 /* If there is a base address, make sure that any sections it might
2450 mention are initialized. */
2451 if (s
->addr_tree
!= NULL
)
2452 exp_init_os (s
->addr_tree
);
2454 if (s
->load_base
!= NULL
)
2455 exp_init_os (s
->load_base
);
2457 /* If supplied an alignment, set it. */
2458 if (s
->section_alignment
!= NULL
)
2459 s
->bfd_section
->alignment_power
= exp_get_power (s
->section_alignment
, s
,
2460 "section alignment");
2464 get_os_init_flag (lang_output_section_statement_type
* os
)
2467 switch (os
->sectype
)
2469 case readonly_section
: return SEC_READONLY
;
2470 case noload_section
: return SEC_NEVER_LOAD
;
2477 /* Make sure that all output sections mentioned in an expression are
2481 exp_init_os (etree_type
*exp
)
2483 switch (exp
->type
.node_class
)
2487 case etree_provided
:
2488 exp_init_os (exp
->assign
.src
);
2492 exp_init_os (exp
->binary
.lhs
);
2493 exp_init_os (exp
->binary
.rhs
);
2497 exp_init_os (exp
->trinary
.cond
);
2498 exp_init_os (exp
->trinary
.lhs
);
2499 exp_init_os (exp
->trinary
.rhs
);
2503 exp_init_os (exp
->assert_s
.child
);
2507 exp_init_os (exp
->unary
.child
);
2511 switch (exp
->type
.node_code
)
2516 lang_output_section_statement_type
*os
;
2518 os
= lang_output_section_find (exp
->name
.name
);
2519 if (os
!= NULL
&& os
->bfd_section
== NULL
)
2520 init_os (os
, get_os_init_flag (os
));
2531 section_already_linked (bfd
*abfd
, asection
*sec
, void *data
)
2533 lang_input_statement_type
*entry
= (lang_input_statement_type
*) data
;
2535 /* If we are only reading symbols from this object, then we want to
2536 discard all sections. */
2537 if (entry
->flags
.just_syms
)
2539 bfd_link_just_syms (abfd
, sec
, &link_info
);
2543 /* Deal with SHF_EXCLUDE ELF sections. */
2544 if (!bfd_link_relocatable (&link_info
)
2545 && (abfd
->flags
& BFD_PLUGIN
) == 0
2546 && (sec
->flags
& (SEC_GROUP
| SEC_KEEP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
2547 sec
->output_section
= bfd_abs_section_ptr
;
2549 if (!(abfd
->flags
& DYNAMIC
))
2550 bfd_section_already_linked (abfd
, sec
, &link_info
);
2554 /* Returns true if SECTION is one we know will be discarded based on its
2555 section flags, otherwise returns false. */
2558 lang_discard_section_p (asection
*section
)
2561 flagword flags
= section
->flags
;
2563 /* Discard sections marked with SEC_EXCLUDE. */
2564 discard
= (flags
& SEC_EXCLUDE
) != 0;
2566 /* Discard the group descriptor sections when we're finally placing the
2567 sections from within the group. */
2568 if ((flags
& SEC_GROUP
) != 0
2569 && link_info
.resolve_section_groups
)
2572 /* Discard debugging sections if we are stripping debugging
2574 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
2575 && (flags
& SEC_DEBUGGING
) != 0)
2578 /* Discard non-alloc sections if we are stripping section headers. */
2579 else if (config
.no_section_header
&& (flags
& SEC_ALLOC
) == 0)
2585 /* Return TRUE if SECTION is never going to be added to output statement
2586 OUTPUT. lang_add_section() definitely won't do anything with SECTION
2587 if this returns TRUE. It may do something (or not) if this returns FALSE.
2589 Can be used as early-out to filter matches. This may set
2590 output_section of SECTION, if it was unset, to the abs section in case
2591 we discover SECTION to be always discarded. This may also give
2592 warning messages. */
2595 wont_add_section_p (asection
*section
,
2596 lang_output_section_statement_type
*output
)
2600 /* Is this section one we know should be discarded? */
2601 discard
= lang_discard_section_p (section
);
2603 /* Discard input sections which are assigned to a section named
2604 DISCARD_SECTION_NAME. */
2605 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
2610 if (section
->output_section
== NULL
)
2612 /* This prevents future calls from assigning this section or
2613 warning about it again. */
2614 section
->output_section
= bfd_abs_section_ptr
;
2616 else if (bfd_is_abs_section (section
->output_section
))
2618 else if (link_info
.non_contiguous_regions_warnings
)
2619 einfo (_("%P:%pS: warning: --enable-non-contiguous-regions makes "
2620 "section `%pA' from `%pB' match /DISCARD/ clause.\n"),
2621 NULL
, section
, section
->owner
);
2626 if (section
->output_section
!= NULL
)
2628 if (!link_info
.non_contiguous_regions
)
2631 /* SECTION has already been handled in a special way
2632 (eg. LINK_ONCE): skip it. */
2633 if (bfd_is_abs_section (section
->output_section
))
2636 /* Already assigned to the same output section, do not process
2637 it again, to avoid creating loops between duplicate sections
2639 if (section
->output_section
== output
->bfd_section
)
2642 if (link_info
.non_contiguous_regions_warnings
&& output
->bfd_section
)
2643 einfo (_("%P:%pS: warning: --enable-non-contiguous-regions may "
2644 "change behaviour for section `%pA' from `%pB' (assigned to "
2645 "%pA, but additional match: %pA)\n"),
2646 NULL
, section
, section
->owner
, section
->output_section
,
2647 output
->bfd_section
);
2649 /* SECTION has already been assigned to an output section, but
2650 the user allows it to be mapped to another one in case it
2651 overflows. We'll later update the actual output section in
2652 size_input_section as appropriate. */
2658 /* The wild routines.
2660 These expand statements like *(.text) and foo.o to a list of
2661 explicit actions, like foo.o(.text), bar.o(.text) and
2662 foo.o(.text, .data). */
2664 /* Add SECTION to the output section OUTPUT. Do this by creating a
2665 lang_input_section statement which is placed at PTR. */
2668 lang_add_section (lang_statement_list_type
*ptr
,
2670 struct wildcard_list
*pattern
,
2671 struct flag_info
*sflag_info
,
2672 lang_output_section_statement_type
*output
)
2674 flagword flags
= section
->flags
;
2676 lang_input_section_type
*new_section
;
2677 bfd
*abfd
= link_info
.output_bfd
;
2679 if (wont_add_section_p (section
, output
))
2686 keep
= bfd_lookup_section_flags (&link_info
, sflag_info
, section
);
2691 /* We don't copy the SEC_NEVER_LOAD flag from an input section
2692 to an output section, because we want to be able to include a
2693 SEC_NEVER_LOAD section in the middle of an otherwise loaded
2694 section (I don't know why we want to do this, but we do).
2695 build_link_order in ldwrite.c handles this case by turning
2696 the embedded SEC_NEVER_LOAD section into a fill. */
2697 flags
&= ~ SEC_NEVER_LOAD
;
2699 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
2700 already been processed. One reason to do this is that on pe
2701 format targets, .text$foo sections go into .text and it's odd
2702 to see .text with SEC_LINK_ONCE set. */
2703 if ((flags
& (SEC_LINK_ONCE
| SEC_GROUP
)) == (SEC_LINK_ONCE
| SEC_GROUP
))
2705 if (link_info
.resolve_section_groups
)
2706 flags
&= ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
);
2708 flags
&= ~(SEC_LINK_DUPLICATES
| SEC_RELOC
);
2710 else if (!bfd_link_relocatable (&link_info
))
2711 flags
&= ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
);
2713 switch (output
->sectype
)
2715 case normal_section
:
2716 case overlay_section
:
2717 case first_overlay_section
:
2720 case noalloc_section
:
2721 flags
&= ~SEC_ALLOC
;
2723 case typed_readonly_section
:
2724 case readonly_section
:
2725 flags
|= SEC_READONLY
;
2727 case noload_section
:
2729 flags
|= SEC_NEVER_LOAD
;
2730 /* Unfortunately GNU ld has managed to evolve two different
2731 meanings to NOLOAD in scripts. ELF gets a .bss style noload,
2732 alloc, no contents section. All others get a noload, noalloc
2734 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
)
2735 flags
&= ~SEC_HAS_CONTENTS
;
2737 flags
&= ~SEC_ALLOC
;
2741 if (output
->bfd_section
== NULL
)
2742 init_os (output
, flags
);
2744 /* If SEC_READONLY is not set in the input section, then clear
2745 it from the output section. */
2746 output
->bfd_section
->flags
&= flags
| ~SEC_READONLY
;
2748 if (output
->bfd_section
->linker_has_input
)
2750 /* Only set SEC_READONLY flag on the first input section. */
2751 flags
&= ~ SEC_READONLY
;
2753 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
2754 if ((output
->bfd_section
->flags
& (SEC_MERGE
| SEC_STRINGS
))
2755 != (flags
& (SEC_MERGE
| SEC_STRINGS
))
2756 || ((flags
& SEC_MERGE
) != 0
2757 && output
->bfd_section
->entsize
!= section
->entsize
))
2759 output
->bfd_section
->flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2760 flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2763 output
->bfd_section
->flags
|= flags
;
2765 if (!output
->bfd_section
->linker_has_input
)
2767 output
->bfd_section
->linker_has_input
= 1;
2768 /* This must happen after flags have been updated. The output
2769 section may have been created before we saw its first input
2770 section, eg. for a data statement. */
2771 bfd_init_private_section_data (section
->owner
, section
,
2772 link_info
.output_bfd
,
2773 output
->bfd_section
,
2775 if ((flags
& SEC_MERGE
) != 0)
2776 output
->bfd_section
->entsize
= section
->entsize
;
2779 if ((flags
& SEC_TIC54X_BLOCK
) != 0
2780 && bfd_get_arch (section
->owner
) == bfd_arch_tic54x
)
2782 /* FIXME: This value should really be obtained from the bfd... */
2783 output
->block_value
= 128;
2786 /* When a .ctors section is placed in .init_array it must be copied
2787 in reverse order. Similarly for .dtors. Set that up. */
2788 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
2789 && ((startswith (section
->name
, ".ctors")
2790 && strcmp (output
->bfd_section
->name
, ".init_array") == 0)
2791 || (startswith (section
->name
, ".dtors")
2792 && strcmp (output
->bfd_section
->name
, ".fini_array") == 0))
2793 && (section
->name
[6] == 0 || section
->name
[6] == '.'))
2794 section
->flags
|= SEC_ELF_REVERSE_COPY
;
2796 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
2797 output
->bfd_section
->alignment_power
= section
->alignment_power
;
2799 section
->output_section
= output
->bfd_section
;
2801 if (!map_head_is_link_order
)
2803 asection
*s
= output
->bfd_section
->map_tail
.s
;
2804 output
->bfd_section
->map_tail
.s
= section
;
2805 section
->map_head
.s
= NULL
;
2806 section
->map_tail
.s
= s
;
2808 s
->map_head
.s
= section
;
2810 output
->bfd_section
->map_head
.s
= section
;
2813 /* Add a section reference to the list. */
2814 new_section
= new_stat (lang_input_section
, ptr
);
2815 new_section
->section
= section
;
2816 new_section
->pattern
= pattern
;
2819 /* Expand a wild statement for a particular FILE. SECTION may be
2820 NULL, in which case it is a wild card. This assumes that the
2821 wild statement doesn't need any sorting (of filenames or sections). */
2824 output_section_callback_nosort (lang_wild_statement_type
*ptr
,
2825 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
2827 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
2830 lang_output_section_statement_type
*os
;
2832 os
= (lang_output_section_statement_type
*) output
;
2834 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2835 if (unique_section_p (section
, os
))
2838 lang_add_section (&ptr
->children
, section
, ptr
->section_list
,
2839 ptr
->section_flag_list
, os
);
2842 /* Check if all sections in a wild statement for a particular FILE
2846 check_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
2847 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
2849 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
2852 lang_output_section_statement_type
*os
;
2854 os
= (lang_output_section_statement_type
*) output
;
2856 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2857 if (unique_section_p (section
, os
))
2860 if (section
->output_section
== NULL
&& (section
->flags
& SEC_READONLY
) == 0)
2861 os
->all_input_readonly
= false;
2864 /* This is passed a file name which must have been seen already and
2865 added to the statement tree. We will see if it has been opened
2866 already and had its symbols read. If not then we'll read it. */
2868 static lang_input_statement_type
*
2869 lookup_name (const char *name
)
2871 lang_input_statement_type
*search
;
2873 for (search
= (void *) input_file_chain
.head
;
2875 search
= search
->next_real_file
)
2877 /* Use the local_sym_name as the name of the file that has
2878 already been loaded as filename might have been transformed
2879 via the search directory lookup mechanism. */
2880 const char *filename
= search
->local_sym_name
;
2882 if (filename
!= NULL
2883 && filename_cmp (filename
, name
) == 0)
2889 /* Arrange to splice the input statement added by new_afile into
2890 statement_list after the current input_file_chain tail.
2891 We know input_file_chain is not an empty list, and that
2892 lookup_name was called via open_input_bfds. Later calls to
2893 lookup_name should always match an existing input_statement. */
2894 lang_statement_union_type
**tail
= stat_ptr
->tail
;
2895 lang_statement_union_type
**after
2896 = (void *) ((char *) input_file_chain
.tail
2897 - offsetof (lang_input_statement_type
, next_real_file
)
2898 + offsetof (lang_input_statement_type
, header
.next
));
2899 lang_statement_union_type
*rest
= *after
;
2900 stat_ptr
->tail
= after
;
2901 search
= new_afile (name
, lang_input_file_is_search_file_enum
,
2902 default_target
, NULL
);
2903 *stat_ptr
->tail
= rest
;
2905 stat_ptr
->tail
= tail
;
2908 /* If we have already added this file, or this file is not real
2909 don't add this file. */
2910 if (search
->flags
.loaded
|| !search
->flags
.real
)
2913 if (!load_symbols (search
, NULL
))
2919 /* Save LIST as a list of libraries whose symbols should not be exported. */
2924 struct excluded_lib
*next
;
2926 static struct excluded_lib
*excluded_libs
;
2929 add_excluded_libs (const char *list
)
2931 const char *p
= list
, *end
;
2935 struct excluded_lib
*entry
;
2936 end
= strpbrk (p
, ",:");
2938 end
= p
+ strlen (p
);
2939 entry
= (struct excluded_lib
*) xmalloc (sizeof (*entry
));
2940 entry
->next
= excluded_libs
;
2941 entry
->name
= (char *) xmalloc (end
- p
+ 1);
2942 memcpy (entry
->name
, p
, end
- p
);
2943 entry
->name
[end
- p
] = '\0';
2944 excluded_libs
= entry
;
2952 check_excluded_libs (bfd
*abfd
)
2954 struct excluded_lib
*lib
= excluded_libs
;
2958 int len
= strlen (lib
->name
);
2959 const char *filename
= lbasename (bfd_get_filename (abfd
));
2961 if (strcmp (lib
->name
, "ALL") == 0)
2963 abfd
->no_export
= true;
2967 if (filename_ncmp (lib
->name
, filename
, len
) == 0
2968 && (filename
[len
] == '\0'
2969 || (filename
[len
] == '.' && filename
[len
+ 1] == 'a'
2970 && filename
[len
+ 2] == '\0')))
2972 abfd
->no_export
= true;
2980 /* Get the symbols for an input file. */
2983 load_symbols (lang_input_statement_type
*entry
,
2984 lang_statement_list_type
*place
)
2988 if (entry
->flags
.loaded
)
2991 ldfile_open_file (entry
);
2993 /* Do not process further if the file was missing. */
2994 if (entry
->flags
.missing_file
)
2997 if (trace_files
|| verbose
)
2998 info_msg ("%pI\n", entry
);
3000 if (!bfd_check_format (entry
->the_bfd
, bfd_archive
)
3001 && !bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
3004 struct lang_input_statement_flags save_flags
;
3007 err
= bfd_get_error ();
3009 /* See if the emulation has some special knowledge. */
3010 if (ldemul_unrecognized_file (entry
))
3012 if (err
== bfd_error_file_ambiguously_recognized
)
3017 if (err
== bfd_error_file_ambiguously_recognized
)
3021 einfo (_("%P: %pB: file not recognized: %E;"
3022 " matching formats:"), entry
->the_bfd
);
3023 for (p
= matching
; *p
!= NULL
; p
++)
3028 else if (err
!= bfd_error_file_not_recognized
3030 einfo (_("%F%P: %pB: file not recognized: %E\n"), entry
->the_bfd
);
3032 bfd_close (entry
->the_bfd
);
3033 entry
->the_bfd
= NULL
;
3035 /* Try to interpret the file as a linker script. */
3036 save_flags
= input_flags
;
3037 ldfile_open_command_file (entry
->filename
);
3039 push_stat_ptr (place
);
3040 input_flags
.add_DT_NEEDED_for_regular
3041 = entry
->flags
.add_DT_NEEDED_for_regular
;
3042 input_flags
.add_DT_NEEDED_for_dynamic
3043 = entry
->flags
.add_DT_NEEDED_for_dynamic
;
3044 input_flags
.whole_archive
= entry
->flags
.whole_archive
;
3045 input_flags
.dynamic
= entry
->flags
.dynamic
;
3047 ldfile_assumed_script
= true;
3048 parser_input
= input_script
;
3049 current_input_file
= entry
->filename
;
3051 current_input_file
= NULL
;
3052 ldfile_assumed_script
= false;
3054 /* missing_file is sticky. sysrooted will already have been
3055 restored when seeing EOF in yyparse, but no harm to restore
3057 save_flags
.missing_file
|= input_flags
.missing_file
;
3058 input_flags
= save_flags
;
3062 entry
->flags
.loaded
= true;
3067 if (ldemul_recognized_file (entry
))
3070 /* We don't call ldlang_add_file for an archive. Instead, the
3071 add_symbols entry point will call ldlang_add_file, via the
3072 add_archive_element callback, for each element of the archive
3074 switch (bfd_get_format (entry
->the_bfd
))
3080 if (!entry
->flags
.reload
)
3081 ldlang_add_file (entry
);
3085 check_excluded_libs (entry
->the_bfd
);
3087 bfd_set_usrdata (entry
->the_bfd
, entry
);
3088 if (entry
->flags
.whole_archive
)
3096 member
= bfd_openr_next_archived_file (entry
->the_bfd
, member
);
3101 if (!bfd_check_format (member
, bfd_object
))
3103 einfo (_("%F%P: %pB: member %pB in archive is not an object\n"),
3104 entry
->the_bfd
, member
);
3109 if (!(*link_info
.callbacks
3110 ->add_archive_element
) (&link_info
, member
,
3111 "--whole-archive", &subsbfd
))
3114 /* Potentially, the add_archive_element hook may have set a
3115 substitute BFD for us. */
3116 if (!bfd_link_add_symbols (subsbfd
, &link_info
))
3118 einfo (_("%F%P: %pB: error adding symbols: %E\n"), member
);
3123 entry
->flags
.loaded
= loaded
;
3129 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
3130 entry
->flags
.loaded
= true;
3132 einfo (_("%F%P: %pB: error adding symbols: %E\n"), entry
->the_bfd
);
3134 return entry
->flags
.loaded
;
3137 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
3138 may be NULL, indicating that it is a wildcard. Separate
3139 lang_input_section statements are created for each part of the
3140 expansion; they are added after the wild statement S. OUTPUT is
3141 the output section. */
3144 wild (lang_wild_statement_type
*s
,
3145 const char *target ATTRIBUTE_UNUSED
,
3146 lang_output_section_statement_type
*output
)
3148 struct wildcard_list
*sec
;
3150 if (s
->filenames_sorted
|| s
->any_specs_sorted
)
3152 lang_section_bst_type
*tree
;
3154 walk_wild (s
, output_section_callback_sort
, output
);
3159 output_section_callback_tree_to_list (s
, tree
, output
);
3161 s
->rightmost
= &s
->tree
;
3165 walk_wild (s
, output_section_callback_nosort
, output
);
3167 if (default_common_section
== NULL
)
3168 for (sec
= s
->section_list
; sec
!= NULL
; sec
= sec
->next
)
3169 if (sec
->spec
.name
!= NULL
&& strcmp (sec
->spec
.name
, "COMMON") == 0)
3171 /* Remember the section that common is going to in case we
3172 later get something which doesn't know where to put it. */
3173 default_common_section
= output
;
3178 /* Return TRUE iff target is the sought target. */
3181 get_target (const bfd_target
*target
, void *data
)
3183 const char *sought
= (const char *) data
;
3185 return strcmp (target
->name
, sought
) == 0;
3188 /* Like strcpy() but convert to lower case as well. */
3191 stricpy (char *dest
, const char *src
)
3195 while ((c
= *src
++) != 0)
3196 *dest
++ = TOLOWER (c
);
3201 /* Remove the first occurrence of needle (if any) in haystack
3205 strcut (char *haystack
, const char *needle
)
3207 haystack
= strstr (haystack
, needle
);
3213 for (src
= haystack
+ strlen (needle
); *src
;)
3214 *haystack
++ = *src
++;
3220 /* Compare two target format name strings.
3221 Return a value indicating how "similar" they are. */
3224 name_compare (const char *first
, const char *second
)
3230 copy1
= (char *) xmalloc (strlen (first
) + 1);
3231 copy2
= (char *) xmalloc (strlen (second
) + 1);
3233 /* Convert the names to lower case. */
3234 stricpy (copy1
, first
);
3235 stricpy (copy2
, second
);
3237 /* Remove size and endian strings from the name. */
3238 strcut (copy1
, "big");
3239 strcut (copy1
, "little");
3240 strcut (copy2
, "big");
3241 strcut (copy2
, "little");
3243 /* Return a value based on how many characters match,
3244 starting from the beginning. If both strings are
3245 the same then return 10 * their length. */
3246 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
3247 if (copy1
[result
] == 0)
3259 /* Set by closest_target_match() below. */
3260 static const bfd_target
*winner
;
3262 /* Scan all the valid bfd targets looking for one that has the endianness
3263 requirement that was specified on the command line, and is the nearest
3264 match to the original output target. */
3267 closest_target_match (const bfd_target
*target
, void *data
)
3269 const bfd_target
*original
= (const bfd_target
*) data
;
3271 if (command_line
.endian
== ENDIAN_BIG
3272 && target
->byteorder
!= BFD_ENDIAN_BIG
)
3275 if (command_line
.endian
== ENDIAN_LITTLE
3276 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
3279 /* Must be the same flavour. */
3280 if (target
->flavour
!= original
->flavour
)
3283 /* Ignore generic big and little endian elf vectors. */
3284 if (strcmp (target
->name
, "elf32-big") == 0
3285 || strcmp (target
->name
, "elf64-big") == 0
3286 || strcmp (target
->name
, "elf32-little") == 0
3287 || strcmp (target
->name
, "elf64-little") == 0)
3290 /* If we have not found a potential winner yet, then record this one. */
3297 /* Oh dear, we now have two potential candidates for a successful match.
3298 Compare their names and choose the better one. */
3299 if (name_compare (target
->name
, original
->name
)
3300 > name_compare (winner
->name
, original
->name
))
3303 /* Keep on searching until wqe have checked them all. */
3307 /* Return the BFD target format of the first input file. */
3310 get_first_input_target (void)
3312 const char *target
= NULL
;
3314 LANG_FOR_EACH_INPUT_STATEMENT (s
)
3316 if (s
->header
.type
== lang_input_statement_enum
3319 ldfile_open_file (s
);
3321 if (s
->the_bfd
!= NULL
3322 && bfd_check_format (s
->the_bfd
, bfd_object
))
3324 target
= bfd_get_target (s
->the_bfd
);
3336 lang_get_output_target (void)
3340 /* Has the user told us which output format to use? */
3341 if (output_target
!= NULL
)
3342 return output_target
;
3344 /* No - has the current target been set to something other than
3346 if (current_target
!= default_target
&& current_target
!= NULL
)
3347 return current_target
;
3349 /* No - can we determine the format of the first input file? */
3350 target
= get_first_input_target ();
3354 /* Failed - use the default output target. */
3355 return default_target
;
3358 /* Open the output file. */
3361 open_output (const char *name
)
3363 lang_input_statement_type
*f
;
3364 char *out
= lrealpath (name
);
3366 for (f
= (void *) input_file_chain
.head
;
3368 f
= f
->next_real_file
)
3371 char *in
= lrealpath (f
->local_sym_name
);
3372 if (filename_cmp (in
, out
) == 0)
3373 einfo (_("%F%P: input file '%s' is the same as output file\n"),
3379 output_target
= lang_get_output_target ();
3381 /* Has the user requested a particular endianness on the command
3383 if (command_line
.endian
!= ENDIAN_UNSET
)
3385 /* Get the chosen target. */
3386 const bfd_target
*target
3387 = bfd_iterate_over_targets (get_target
, (void *) output_target
);
3389 /* If the target is not supported, we cannot do anything. */
3392 enum bfd_endian desired_endian
;
3394 if (command_line
.endian
== ENDIAN_BIG
)
3395 desired_endian
= BFD_ENDIAN_BIG
;
3397 desired_endian
= BFD_ENDIAN_LITTLE
;
3399 /* See if the target has the wrong endianness. This should
3400 not happen if the linker script has provided big and
3401 little endian alternatives, but some scrips don't do
3403 if (target
->byteorder
!= desired_endian
)
3405 /* If it does, then see if the target provides
3406 an alternative with the correct endianness. */
3407 if (target
->alternative_target
!= NULL
3408 && (target
->alternative_target
->byteorder
== desired_endian
))
3409 output_target
= target
->alternative_target
->name
;
3412 /* Try to find a target as similar as possible to
3413 the default target, but which has the desired
3414 endian characteristic. */
3415 bfd_iterate_over_targets (closest_target_match
,
3418 /* Oh dear - we could not find any targets that
3419 satisfy our requirements. */
3421 einfo (_("%P: warning: could not find any targets"
3422 " that match endianness requirement\n"));
3424 output_target
= winner
->name
;
3430 link_info
.output_bfd
= bfd_openw (name
, output_target
);
3432 if (link_info
.output_bfd
== NULL
)
3434 if (bfd_get_error () == bfd_error_invalid_target
)
3435 einfo (_("%F%P: target %s not found\n"), output_target
);
3437 einfo (_("%F%P: cannot open output file %s: %E\n"), name
);
3440 delete_output_file_on_failure
= true;
3442 if (!bfd_set_format (link_info
.output_bfd
, bfd_object
))
3443 einfo (_("%F%P: %s: can not make object file: %E\n"), name
);
3444 if (!bfd_set_arch_mach (link_info
.output_bfd
,
3445 ldfile_output_architecture
,
3446 ldfile_output_machine
))
3447 einfo (_("%F%P: %s: can not set architecture: %E\n"), name
);
3449 link_info
.hash
= bfd_link_hash_table_create (link_info
.output_bfd
);
3450 if (link_info
.hash
== NULL
)
3451 einfo (_("%F%P: can not create hash table: %E\n"));
3453 bfd_set_gp_size (link_info
.output_bfd
, g_switch_value
);
3457 ldlang_open_output (lang_statement_union_type
*statement
)
3459 switch (statement
->header
.type
)
3461 case lang_output_statement_enum
:
3462 ASSERT (link_info
.output_bfd
== NULL
);
3463 open_output (statement
->output_statement
.name
);
3464 ldemul_set_output_arch ();
3465 if (config
.magic_demand_paged
3466 && !bfd_link_relocatable (&link_info
))
3467 link_info
.output_bfd
->flags
|= D_PAGED
;
3469 link_info
.output_bfd
->flags
&= ~D_PAGED
;
3470 if (config
.text_read_only
)
3471 link_info
.output_bfd
->flags
|= WP_TEXT
;
3473 link_info
.output_bfd
->flags
&= ~WP_TEXT
;
3474 if (link_info
.traditional_format
)
3475 link_info
.output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
3477 link_info
.output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
3478 if (config
.no_section_header
)
3479 link_info
.output_bfd
->flags
|= BFD_NO_SECTION_HEADER
;
3481 link_info
.output_bfd
->flags
&= ~BFD_NO_SECTION_HEADER
;
3484 case lang_target_statement_enum
:
3485 current_target
= statement
->target_statement
.target
;
3493 init_opb (asection
*s
)
3498 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
3500 && (s
->flags
& SEC_ELF_OCTETS
) != 0)
3503 x
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3504 ldfile_output_machine
);
3506 while ((x
& 1) == 0)
3514 /* Open all the input files. */
3518 OPEN_BFD_NORMAL
= 0,
3522 #if BFD_SUPPORTS_PLUGINS
3523 static lang_input_statement_type
*plugin_insert
= NULL
;
3524 static struct bfd_link_hash_entry
*plugin_undefs
= NULL
;
3528 open_input_bfds (lang_statement_union_type
*s
,
3529 lang_output_section_statement_type
*os
,
3530 enum open_bfd_mode mode
)
3532 for (; s
!= NULL
; s
= s
->header
.next
)
3534 switch (s
->header
.type
)
3536 case lang_constructors_statement_enum
:
3537 open_input_bfds (constructor_list
.head
, os
, mode
);
3539 case lang_output_section_statement_enum
:
3540 os
= &s
->output_section_statement
;
3541 open_input_bfds (os
->children
.head
, os
, mode
);
3543 case lang_wild_statement_enum
:
3544 /* Maybe we should load the file's symbols. */
3545 if ((mode
& OPEN_BFD_RESCAN
) == 0
3546 && s
->wild_statement
.filename
3547 && !wildcardp (s
->wild_statement
.filename
)
3548 && !archive_path (s
->wild_statement
.filename
))
3549 lookup_name (s
->wild_statement
.filename
);
3550 open_input_bfds (s
->wild_statement
.children
.head
, os
, mode
);
3552 case lang_group_statement_enum
:
3554 struct bfd_link_hash_entry
*undefs
;
3555 #if BFD_SUPPORTS_PLUGINS
3556 lang_input_statement_type
*plugin_insert_save
;
3559 /* We must continually search the entries in the group
3560 until no new symbols are added to the list of undefined
3565 #if BFD_SUPPORTS_PLUGINS
3566 plugin_insert_save
= plugin_insert
;
3568 undefs
= link_info
.hash
->undefs_tail
;
3569 open_input_bfds (s
->group_statement
.children
.head
, os
,
3570 mode
| OPEN_BFD_FORCE
);
3572 while (undefs
!= link_info
.hash
->undefs_tail
3573 #if BFD_SUPPORTS_PLUGINS
3574 /* Objects inserted by a plugin, which are loaded
3575 before we hit this loop, may have added new
3577 || (plugin_insert
!= plugin_insert_save
&& plugin_undefs
)
3582 case lang_target_statement_enum
:
3583 current_target
= s
->target_statement
.target
;
3585 case lang_input_statement_enum
:
3586 if (s
->input_statement
.flags
.real
)
3588 lang_statement_union_type
**os_tail
;
3589 lang_statement_list_type add
;
3592 s
->input_statement
.target
= current_target
;
3594 /* If we are being called from within a group, and this
3595 is an archive which has already been searched, then
3596 force it to be researched unless the whole archive
3597 has been loaded already. Do the same for a rescan.
3598 Likewise reload --as-needed shared libs. */
3599 if (mode
!= OPEN_BFD_NORMAL
3600 #if BFD_SUPPORTS_PLUGINS
3601 && ((mode
& OPEN_BFD_RESCAN
) == 0
3602 || plugin_insert
== NULL
)
3604 && s
->input_statement
.flags
.loaded
3605 && (abfd
= s
->input_statement
.the_bfd
) != NULL
3606 && ((bfd_get_format (abfd
) == bfd_archive
3607 && !s
->input_statement
.flags
.whole_archive
)
3608 || (bfd_get_format (abfd
) == bfd_object
3609 && ((abfd
->flags
) & DYNAMIC
) != 0
3610 && s
->input_statement
.flags
.add_DT_NEEDED_for_regular
3611 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
3612 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)))
3614 s
->input_statement
.flags
.loaded
= false;
3615 s
->input_statement
.flags
.reload
= true;
3618 os_tail
= lang_os_list
.tail
;
3619 lang_list_init (&add
);
3621 if (!load_symbols (&s
->input_statement
, &add
))
3622 config
.make_executable
= false;
3624 if (add
.head
!= NULL
)
3626 /* If this was a script with output sections then
3627 tack any added statements on to the end of the
3628 list. This avoids having to reorder the output
3629 section statement list. Very likely the user
3630 forgot -T, and whatever we do here will not meet
3631 naive user expectations. */
3632 if (os_tail
!= lang_os_list
.tail
)
3634 einfo (_("%P: warning: %s contains output sections;"
3635 " did you forget -T?\n"),
3636 s
->input_statement
.filename
);
3637 *stat_ptr
->tail
= add
.head
;
3638 stat_ptr
->tail
= add
.tail
;
3642 *add
.tail
= s
->header
.next
;
3643 s
->header
.next
= add
.head
;
3647 #if BFD_SUPPORTS_PLUGINS
3648 /* If we have found the point at which a plugin added new
3649 files, clear plugin_insert to enable archive rescan. */
3650 if (&s
->input_statement
== plugin_insert
)
3651 plugin_insert
= NULL
;
3654 case lang_assignment_statement_enum
:
3655 if (s
->assignment_statement
.exp
->type
.node_class
!= etree_assert
)
3656 exp_fold_tree_no_dot (s
->assignment_statement
.exp
, os
);
3663 /* Exit if any of the files were missing. */
3664 if (input_flags
.missing_file
)
3668 #ifdef ENABLE_LIBCTF
3669 /* Emit CTF errors and warnings. fp can be NULL to report errors/warnings
3670 that happened specifically at CTF open time. */
3672 lang_ctf_errs_warnings (ctf_dict_t
*fp
)
3674 ctf_next_t
*i
= NULL
;
3679 while ((text
= ctf_errwarning_next (fp
, &i
, &is_warning
, &err
)) != NULL
)
3681 einfo (_("%s: %s\n"), is_warning
? _("CTF warning"): _("CTF error"),
3685 if (err
!= ECTF_NEXT_END
)
3687 einfo (_("CTF error: cannot get CTF errors: `%s'\n"),
3691 /* `err' returns errors from the error/warning iterator in particular.
3692 These never assert. But if we have an fp, that could have recorded
3693 an assertion failure: assert if it has done so. */
3694 ASSERT (!fp
|| ctf_errno (fp
) != ECTF_INTERNAL
);
3697 /* Open the CTF sections in the input files with libctf: if any were opened,
3698 create a fake input file that we'll write the merged CTF data to later
3702 ldlang_open_ctf (void)
3707 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3711 /* Incoming files from the compiler have a single ctf_dict_t in them
3712 (which is presented to us by the libctf API in a ctf_archive_t
3713 wrapper): files derived from a previous relocatable link have a CTF
3714 archive containing possibly many CTF files. */
3716 if ((file
->the_ctf
= ctf_bfdopen (file
->the_bfd
, &err
)) == NULL
)
3718 if (err
!= ECTF_NOCTFDATA
)
3720 lang_ctf_errs_warnings (NULL
);
3721 einfo (_("%P: warning: CTF section in %pB not loaded; "
3722 "its types will be discarded: %s\n"), file
->the_bfd
,
3728 /* Prevent the contents of this section from being written, while
3729 requiring the section itself to be duplicated in the output, but only
3731 /* This section must exist if ctf_bfdopen() succeeded. */
3732 sect
= bfd_get_section_by_name (file
->the_bfd
, ".ctf");
3734 sect
->flags
|= SEC_NEVER_LOAD
| SEC_HAS_CONTENTS
| SEC_LINKER_CREATED
;
3737 sect
->flags
|= SEC_EXCLUDE
;
3747 if ((ctf_output
= ctf_create (&err
)) != NULL
)
3750 einfo (_("%P: warning: CTF output not created: `%s'\n"),
3753 LANG_FOR_EACH_INPUT_STATEMENT (errfile
)
3754 ctf_close (errfile
->the_ctf
);
3757 /* Merge together CTF sections. After this, only the symtab-dependent
3758 function and data object sections need adjustment. */
3761 lang_merge_ctf (void)
3763 asection
*output_sect
;
3769 output_sect
= bfd_get_section_by_name (link_info
.output_bfd
, ".ctf");
3771 /* If the section was discarded, don't waste time merging. */
3772 if (output_sect
== NULL
)
3774 ctf_dict_close (ctf_output
);
3777 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3779 ctf_close (file
->the_ctf
);
3780 file
->the_ctf
= NULL
;
3785 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3790 /* Takes ownership of file->the_ctf. */
3791 if (ctf_link_add_ctf (ctf_output
, file
->the_ctf
, file
->filename
) < 0)
3793 einfo (_("%P: warning: CTF section in %pB cannot be linked: `%s'\n"),
3794 file
->the_bfd
, ctf_errmsg (ctf_errno (ctf_output
)));
3795 ctf_close (file
->the_ctf
);
3796 file
->the_ctf
= NULL
;
3801 if (!config
.ctf_share_duplicated
)
3802 flags
= CTF_LINK_SHARE_UNCONFLICTED
;
3804 flags
= CTF_LINK_SHARE_DUPLICATED
;
3805 if (!config
.ctf_variables
)
3806 flags
|= CTF_LINK_OMIT_VARIABLES_SECTION
;
3807 if (bfd_link_relocatable (&link_info
))
3808 flags
|= CTF_LINK_NO_FILTER_REPORTED_SYMS
;
3810 if (ctf_link (ctf_output
, flags
) < 0)
3812 lang_ctf_errs_warnings (ctf_output
);
3813 einfo (_("%P: warning: CTF linking failed; "
3814 "output will have no CTF section: %s\n"),
3815 ctf_errmsg (ctf_errno (ctf_output
)));
3818 output_sect
->size
= 0;
3819 output_sect
->flags
|= SEC_EXCLUDE
;
3822 /* Output any lingering errors that didn't come from ctf_link. */
3823 lang_ctf_errs_warnings (ctf_output
);
3826 /* Let the emulation acquire strings from the dynamic strtab to help it optimize
3827 the CTF, if supported. */
3830 ldlang_ctf_acquire_strings (struct elf_strtab_hash
*dynstrtab
)
3832 ldemul_acquire_strings_for_ctf (ctf_output
, dynstrtab
);
3835 /* Inform the emulation about the addition of a new dynamic symbol, in BFD
3837 void ldlang_ctf_new_dynsym (int symidx
, struct elf_internal_sym
*sym
)
3839 ldemul_new_dynsym_for_ctf (ctf_output
, symidx
, sym
);
3842 /* Write out the CTF section. Called early, if the emulation isn't going to
3843 need to dedup against the strtab and symtab, then possibly called from the
3844 target linker code if the dedup has happened. */
3846 lang_write_ctf (int late
)
3849 asection
*output_sect
;
3856 /* Emit CTF late if this emulation says it can do so. */
3857 if (ldemul_emit_ctf_early ())
3862 if (!ldemul_emit_ctf_early ())
3866 /* Inform the emulation that all the symbols that will be received have
3869 ldemul_new_dynsym_for_ctf (ctf_output
, 0, NULL
);
3873 output_sect
= bfd_get_section_by_name (link_info
.output_bfd
, ".ctf");
3876 output_sect
->contents
= ctf_link_write (ctf_output
, &output_size
,
3877 CTF_COMPRESSION_THRESHOLD
);
3878 output_sect
->size
= output_size
;
3879 output_sect
->flags
|= SEC_IN_MEMORY
| SEC_KEEP
;
3881 lang_ctf_errs_warnings (ctf_output
);
3882 if (!output_sect
->contents
)
3884 einfo (_("%P: warning: CTF section emission failed; "
3885 "output will have no CTF section: %s\n"),
3886 ctf_errmsg (ctf_errno (ctf_output
)));
3887 output_sect
->size
= 0;
3888 output_sect
->flags
|= SEC_EXCLUDE
;
3892 /* This also closes every CTF input file used in the link. */
3893 ctf_dict_close (ctf_output
);
3896 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3897 file
->the_ctf
= NULL
;
3900 /* Write out the CTF section late, if the emulation needs that. */
3903 ldlang_write_ctf_late (void)
3905 /* Trigger a "late call", if the emulation needs one. */
3911 ldlang_open_ctf (void)
3913 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3917 /* If built without CTF, warn and delete all CTF sections from the output.
3918 (The alternative would be to simply concatenate them, which does not
3919 yield a valid CTF section.) */
3921 if ((sect
= bfd_get_section_by_name (file
->the_bfd
, ".ctf")) != NULL
)
3923 einfo (_("%P: warning: CTF section in %pB not linkable: "
3924 "%P was built without support for CTF\n"), file
->the_bfd
);
3926 sect
->flags
|= SEC_EXCLUDE
;
3931 static void lang_merge_ctf (void) {}
3933 ldlang_ctf_acquire_strings (struct elf_strtab_hash
*dynstrtab
3934 ATTRIBUTE_UNUSED
) {}
3936 ldlang_ctf_new_dynsym (int symidx ATTRIBUTE_UNUSED
,
3937 struct elf_internal_sym
*sym ATTRIBUTE_UNUSED
) {}
3938 static void lang_write_ctf (int late ATTRIBUTE_UNUSED
) {}
3939 void ldlang_write_ctf_late (void) {}
3942 /* Add the supplied name to the symbol table as an undefined reference.
3943 This is a two step process as the symbol table doesn't even exist at
3944 the time the ld command line is processed. First we put the name
3945 on a list, then, once the output file has been opened, transfer the
3946 name to the symbol table. */
3948 typedef struct bfd_sym_chain ldlang_undef_chain_list_type
;
3950 #define ldlang_undef_chain_list_head entry_symbol.next
3953 ldlang_add_undef (const char *const name
, bool cmdline ATTRIBUTE_UNUSED
)
3955 ldlang_undef_chain_list_type
*new_undef
;
3957 new_undef
= stat_alloc (sizeof (*new_undef
));
3958 new_undef
->next
= ldlang_undef_chain_list_head
;
3959 ldlang_undef_chain_list_head
= new_undef
;
3961 new_undef
->name
= xstrdup (name
);
3963 if (link_info
.output_bfd
!= NULL
)
3964 insert_undefined (new_undef
->name
);
3967 /* Insert NAME as undefined in the symbol table. */
3970 insert_undefined (const char *name
)
3972 struct bfd_link_hash_entry
*h
;
3974 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, false, true);
3976 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
3977 if (h
->type
== bfd_link_hash_new
)
3979 h
->type
= bfd_link_hash_undefined
;
3980 h
->u
.undef
.abfd
= NULL
;
3981 h
->non_ir_ref_regular
= true;
3982 bfd_link_add_undef (link_info
.hash
, h
);
3986 /* Run through the list of undefineds created above and place them
3987 into the linker hash table as undefined symbols belonging to the
3991 lang_place_undefineds (void)
3993 ldlang_undef_chain_list_type
*ptr
;
3995 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
3996 insert_undefined (ptr
->name
);
3999 /* Mark -u symbols against garbage collection. */
4002 lang_mark_undefineds (void)
4004 ldlang_undef_chain_list_type
*ptr
;
4006 if (is_elf_hash_table (link_info
.hash
))
4007 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
4009 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*)
4010 bfd_link_hash_lookup (link_info
.hash
, ptr
->name
, false, false, true);
4016 /* Structure used to build the list of symbols that the user has required
4019 struct require_defined_symbol
4022 struct require_defined_symbol
*next
;
4025 /* The list of symbols that the user has required be defined. */
4027 static struct require_defined_symbol
*require_defined_symbol_list
;
4029 /* Add a new symbol NAME to the list of symbols that are required to be
4033 ldlang_add_require_defined (const char *const name
)
4035 struct require_defined_symbol
*ptr
;
4037 ldlang_add_undef (name
, true);
4038 ptr
= stat_alloc (sizeof (*ptr
));
4039 ptr
->next
= require_defined_symbol_list
;
4040 ptr
->name
= strdup (name
);
4041 require_defined_symbol_list
= ptr
;
4044 /* Check that all symbols the user required to be defined, are defined,
4045 raise an error if we find a symbol that is not defined. */
4048 ldlang_check_require_defined_symbols (void)
4050 struct require_defined_symbol
*ptr
;
4052 for (ptr
= require_defined_symbol_list
; ptr
!= NULL
; ptr
= ptr
->next
)
4054 struct bfd_link_hash_entry
*h
;
4056 h
= bfd_link_hash_lookup (link_info
.hash
, ptr
->name
,
4057 false, false, true);
4059 || (h
->type
!= bfd_link_hash_defined
4060 && h
->type
!= bfd_link_hash_defweak
))
4061 einfo(_("%X%P: required symbol `%s' not defined\n"), ptr
->name
);
4065 /* Check for all readonly or some readwrite sections. */
4068 check_input_sections
4069 (lang_statement_union_type
*s
,
4070 lang_output_section_statement_type
*output_section_statement
)
4072 for (; s
!= NULL
; s
= s
->header
.next
)
4074 switch (s
->header
.type
)
4076 case lang_wild_statement_enum
:
4077 walk_wild (&s
->wild_statement
, check_section_callback
,
4078 output_section_statement
);
4079 if (!output_section_statement
->all_input_readonly
)
4082 case lang_constructors_statement_enum
:
4083 check_input_sections (constructor_list
.head
,
4084 output_section_statement
);
4085 if (!output_section_statement
->all_input_readonly
)
4088 case lang_group_statement_enum
:
4089 check_input_sections (s
->group_statement
.children
.head
,
4090 output_section_statement
);
4091 if (!output_section_statement
->all_input_readonly
)
4100 /* Update wildcard statements if needed. */
4103 update_wild_statements (lang_statement_union_type
*s
)
4105 struct wildcard_list
*sec
;
4107 switch (sort_section
)
4117 for (; s
!= NULL
; s
= s
->header
.next
)
4119 switch (s
->header
.type
)
4124 case lang_wild_statement_enum
:
4125 for (sec
= s
->wild_statement
.section_list
; sec
!= NULL
;
4127 /* Don't sort .init/.fini sections. */
4128 if (strcmp (sec
->spec
.name
, ".init") != 0
4129 && strcmp (sec
->spec
.name
, ".fini") != 0)
4131 switch (sec
->spec
.sorted
)
4134 sec
->spec
.sorted
= sort_section
;
4137 if (sort_section
== by_alignment
)
4138 sec
->spec
.sorted
= by_name_alignment
;
4141 if (sort_section
== by_name
)
4142 sec
->spec
.sorted
= by_alignment_name
;
4147 s
->wild_statement
.any_specs_sorted
= true;
4151 case lang_constructors_statement_enum
:
4152 update_wild_statements (constructor_list
.head
);
4155 case lang_output_section_statement_enum
:
4156 update_wild_statements
4157 (s
->output_section_statement
.children
.head
);
4160 case lang_group_statement_enum
:
4161 update_wild_statements (s
->group_statement
.children
.head
);
4169 /* Open input files and attach to output sections. */
4172 map_input_to_output_sections
4173 (lang_statement_union_type
*s
, const char *target
,
4174 lang_output_section_statement_type
*os
)
4176 for (; s
!= NULL
; s
= s
->header
.next
)
4178 lang_output_section_statement_type
*tos
;
4180 unsigned int type
= 0;
4182 switch (s
->header
.type
)
4184 case lang_wild_statement_enum
:
4185 wild (&s
->wild_statement
, target
, os
);
4187 case lang_constructors_statement_enum
:
4188 map_input_to_output_sections (constructor_list
.head
,
4192 case lang_output_section_statement_enum
:
4193 tos
= &s
->output_section_statement
;
4194 if (tos
->constraint
== ONLY_IF_RW
4195 || tos
->constraint
== ONLY_IF_RO
)
4197 tos
->all_input_readonly
= true;
4198 check_input_sections (tos
->children
.head
, tos
);
4199 if (tos
->all_input_readonly
!= (tos
->constraint
== ONLY_IF_RO
))
4200 tos
->constraint
= -1;
4202 if (tos
->constraint
>= 0)
4203 map_input_to_output_sections (tos
->children
.head
,
4207 case lang_output_statement_enum
:
4209 case lang_target_statement_enum
:
4210 target
= s
->target_statement
.target
;
4212 case lang_group_statement_enum
:
4213 map_input_to_output_sections (s
->group_statement
.children
.head
,
4217 case lang_data_statement_enum
:
4219 /* This should never happen. */
4221 /* Make sure that any sections mentioned in the expression
4223 exp_init_os (s
->data_statement
.exp
);
4224 /* The output section gets CONTENTS, ALLOC and LOAD, but
4225 these may be overridden by the script. */
4226 flags
= SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
;
4227 switch (os
->sectype
)
4229 case normal_section
:
4230 case overlay_section
:
4231 case first_overlay_section
:
4233 case noalloc_section
:
4234 flags
= SEC_HAS_CONTENTS
;
4236 case readonly_section
:
4237 flags
|= SEC_READONLY
;
4239 case typed_readonly_section
:
4240 flags
|= SEC_READONLY
;
4243 if (os
->sectype_value
->type
.node_class
== etree_name
4244 && os
->sectype_value
->type
.node_code
== NAME
)
4246 const char *name
= os
->sectype_value
->name
.name
;
4247 if (strcmp (name
, "SHT_PROGBITS") == 0)
4248 type
= SHT_PROGBITS
;
4249 else if (strcmp (name
, "SHT_STRTAB") == 0)
4251 else if (strcmp (name
, "SHT_NOTE") == 0)
4253 else if (strcmp (name
, "SHT_NOBITS") == 0)
4255 else if (strcmp (name
, "SHT_INIT_ARRAY") == 0)
4256 type
= SHT_INIT_ARRAY
;
4257 else if (strcmp (name
, "SHT_FINI_ARRAY") == 0)
4258 type
= SHT_FINI_ARRAY
;
4259 else if (strcmp (name
, "SHT_PREINIT_ARRAY") == 0)
4260 type
= SHT_PREINIT_ARRAY
;
4262 einfo (_ ("%F%P: invalid type for output section `%s'\n"),
4267 exp_fold_tree_no_dot (os
->sectype_value
, os
);
4268 if (expld
.result
.valid_p
)
4269 type
= expld
.result
.value
;
4271 einfo (_ ("%F%P: invalid type for output section `%s'\n"),
4275 case noload_section
:
4276 if (bfd_get_flavour (link_info
.output_bfd
)
4277 == bfd_target_elf_flavour
)
4278 flags
= SEC_NEVER_LOAD
| SEC_ALLOC
;
4280 flags
= SEC_NEVER_LOAD
| SEC_HAS_CONTENTS
;
4283 if (os
->bfd_section
== NULL
)
4284 init_os (os
, flags
| SEC_READONLY
);
4286 os
->bfd_section
->flags
|= flags
;
4287 os
->bfd_section
->type
= type
;
4289 case lang_input_section_enum
:
4291 case lang_fill_statement_enum
:
4292 case lang_object_symbols_statement_enum
:
4293 case lang_reloc_statement_enum
:
4294 case lang_padding_statement_enum
:
4295 case lang_input_statement_enum
:
4296 if (os
!= NULL
&& os
->bfd_section
== NULL
)
4300 case lang_assignment_statement_enum
:
4301 if (os
!= NULL
&& os
->bfd_section
== NULL
)
4302 init_os (os
, get_os_init_flag (os
));
4304 /* Make sure that any sections mentioned in the assignment
4306 exp_init_os (s
->assignment_statement
.exp
);
4309 case lang_address_statement_enum
:
4310 /* Mark the specified section with the supplied address.
4311 If this section was actually a segment marker, then the
4312 directive is ignored if the linker script explicitly
4313 processed the segment marker. Originally, the linker
4314 treated segment directives (like -Ttext on the
4315 command-line) as section directives. We honor the
4316 section directive semantics for backwards compatibility;
4317 linker scripts that do not specifically check for
4318 SEGMENT_START automatically get the old semantics. */
4319 if (!s
->address_statement
.segment
4320 || !s
->address_statement
.segment
->used
)
4322 const char *name
= s
->address_statement
.section_name
;
4324 /* Create the output section statement here so that
4325 orphans with a set address will be placed after other
4326 script sections. If we let the orphan placement code
4327 place them in amongst other sections then the address
4328 will affect following script sections, which is
4329 likely to surprise naive users. */
4330 tos
= lang_output_section_statement_lookup (name
, 0, 1);
4331 tos
->addr_tree
= s
->address_statement
.address
;
4332 if (tos
->bfd_section
== NULL
)
4336 case lang_insert_statement_enum
:
4338 case lang_input_matcher_enum
:
4344 /* An insert statement snips out all the linker statements from the
4345 start of the list and places them after the output section
4346 statement specified by the insert. This operation is complicated
4347 by the fact that we keep a doubly linked list of output section
4348 statements as well as the singly linked list of all statements.
4349 FIXME someday: Twiddling with the list not only moves statements
4350 from the user's script but also input and group statements that are
4351 built from command line object files and --start-group. We only
4352 get away with this because the list pointers used by file_chain
4353 and input_file_chain are not reordered, and processing via
4354 statement_list after this point mostly ignores input statements.
4355 One exception is the map file, where LOAD and START GROUP/END GROUP
4356 can end up looking odd. */
4359 process_insert_statements (lang_statement_union_type
**start
)
4361 lang_statement_union_type
**s
;
4362 lang_output_section_statement_type
*first_os
= NULL
;
4363 lang_output_section_statement_type
*last_os
= NULL
;
4364 lang_output_section_statement_type
*os
;
4369 if ((*s
)->header
.type
== lang_output_section_statement_enum
)
4371 /* Keep pointers to the first and last output section
4372 statement in the sequence we may be about to move. */
4373 os
= &(*s
)->output_section_statement
;
4375 ASSERT (last_os
== NULL
|| last_os
->next
== os
);
4378 /* Set constraint negative so that lang_output_section_find
4379 won't match this output section statement. At this
4380 stage in linking constraint has values in the range
4381 [-1, ONLY_IN_RW]. */
4382 last_os
->constraint
= -2 - last_os
->constraint
;
4383 if (first_os
== NULL
)
4386 else if ((*s
)->header
.type
== lang_group_statement_enum
)
4388 /* A user might put -T between --start-group and
4389 --end-group. One way this odd construct might arise is
4390 from a wrapper around ld to change library search
4391 behaviour. For example:
4393 exec real_ld --start-group "$@" --end-group
4394 This isn't completely unreasonable so go looking inside a
4395 group statement for insert statements. */
4396 process_insert_statements (&(*s
)->group_statement
.children
.head
);
4398 else if ((*s
)->header
.type
== lang_insert_statement_enum
)
4400 lang_insert_statement_type
*i
= &(*s
)->insert_statement
;
4401 lang_output_section_statement_type
*where
;
4402 lang_statement_union_type
**ptr
;
4403 lang_statement_union_type
*first
;
4405 if (link_info
.non_contiguous_regions
)
4407 einfo (_("warning: INSERT statement in linker script is "
4408 "incompatible with --enable-non-contiguous-regions.\n"));
4411 where
= lang_output_section_find (i
->where
);
4412 if (where
!= NULL
&& i
->is_before
)
4415 where
= where
->prev
;
4416 while (where
!= NULL
&& where
->constraint
< 0);
4420 einfo (_("%F%P: %s not found for insert\n"), i
->where
);
4424 /* Deal with reordering the output section statement list. */
4425 if (last_os
!= NULL
)
4427 asection
*first_sec
, *last_sec
;
4428 struct lang_output_section_statement_struct
**next
;
4430 /* Snip out the output sections we are moving. */
4431 first_os
->prev
->next
= last_os
->next
;
4432 if (last_os
->next
== NULL
)
4434 next
= &first_os
->prev
->next
;
4435 lang_os_list
.tail
= (lang_statement_union_type
**) next
;
4438 last_os
->next
->prev
= first_os
->prev
;
4439 /* Add them in at the new position. */
4440 last_os
->next
= where
->next
;
4441 if (where
->next
== NULL
)
4443 next
= &last_os
->next
;
4444 lang_os_list
.tail
= (lang_statement_union_type
**) next
;
4447 where
->next
->prev
= last_os
;
4448 first_os
->prev
= where
;
4449 where
->next
= first_os
;
4451 /* Move the bfd sections in the same way. */
4454 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
4456 os
->constraint
= -2 - os
->constraint
;
4457 if (os
->bfd_section
!= NULL
4458 && os
->bfd_section
->owner
!= NULL
)
4460 last_sec
= os
->bfd_section
;
4461 if (first_sec
== NULL
)
4462 first_sec
= last_sec
;
4467 if (last_sec
!= NULL
)
4469 asection
*sec
= where
->bfd_section
;
4471 sec
= output_prev_sec_find (where
);
4473 /* The place we want to insert must come after the
4474 sections we are moving. So if we find no
4475 section or if the section is the same as our
4476 last section, then no move is needed. */
4477 if (sec
!= NULL
&& sec
!= last_sec
)
4479 /* Trim them off. */
4480 if (first_sec
->prev
!= NULL
)
4481 first_sec
->prev
->next
= last_sec
->next
;
4483 link_info
.output_bfd
->sections
= last_sec
->next
;
4484 if (last_sec
->next
!= NULL
)
4485 last_sec
->next
->prev
= first_sec
->prev
;
4487 link_info
.output_bfd
->section_last
= first_sec
->prev
;
4489 if (sec
->owner
== NULL
)
4490 /* SEC is the absolute section, from the
4491 first dummy output section statement. Add
4492 back the sections we trimmed off to the
4493 start of the bfd sections. */
4496 last_sec
->next
= sec
->next
;
4498 last_sec
->next
= link_info
.output_bfd
->sections
;
4499 if (last_sec
->next
!= NULL
)
4500 last_sec
->next
->prev
= last_sec
;
4502 link_info
.output_bfd
->section_last
= last_sec
;
4503 first_sec
->prev
= sec
;
4504 if (first_sec
->prev
!= NULL
)
4505 first_sec
->prev
->next
= first_sec
;
4507 link_info
.output_bfd
->sections
= first_sec
;
4512 lang_statement_union_type
*after
= (void *) where
;
4513 if (where
== &lang_os_list
.head
->output_section_statement
4514 && where
->next
== first_os
)
4516 /* PR30155. Handle a corner case where the statement
4517 list is something like the following:
4519 . .data 0x0000000000000000 0x0
4520 . [0x0000000000000000] b = .
4522 . .data 0x0000000000000000 0x0 t.o
4523 . 0x0000000000000000 0x4 LONG 0x0
4524 . INSERT BEFORE .text.start
4525 . [0x0000000000000004] a = .
4526 . .text.start 0x0000000000000000 0x0
4527 . [0x0000000000000000] c = .
4528 . OUTPUT(a.out elf64-x86-64)
4529 Here we do not want to allow insert_os_after to
4530 choose a point inside the list we are moving.
4531 That would lose the list. Instead, let
4532 insert_os_after work from the INSERT, which in this
4533 particular example will result in inserting after
4534 the assignment "a = .". */
4537 ptr
= insert_os_after (after
);
4538 /* Snip everything from the start of the list, up to and
4539 including the insert statement we are currently processing. */
4541 *start
= (*s
)->header
.next
;
4542 /* Add them back where they belong, minus the insert. */
4545 statement_list
.tail
= s
;
4552 s
= &(*s
)->header
.next
;
4555 /* Undo constraint twiddling. */
4556 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
4558 os
->constraint
= -2 - os
->constraint
;
4564 /* An output section might have been removed after its statement was
4565 added. For example, ldemul_before_allocation can remove dynamic
4566 sections if they turn out to be not needed. Clean them up here. */
4569 strip_excluded_output_sections (void)
4571 lang_output_section_statement_type
*os
;
4573 /* Run lang_size_sections (if not already done). */
4574 if (expld
.phase
!= lang_mark_phase_enum
)
4576 expld
.phase
= lang_mark_phase_enum
;
4577 expld
.dataseg
.phase
= exp_seg_none
;
4578 one_lang_size_sections_pass (NULL
, false);
4579 lang_reset_memory_regions ();
4582 for (os
= (void *) lang_os_list
.head
;
4586 asection
*output_section
;
4589 if (os
->constraint
< 0)
4592 output_section
= os
->bfd_section
;
4593 if (output_section
== NULL
)
4596 exclude
= (output_section
->rawsize
== 0
4597 && (output_section
->flags
& SEC_KEEP
) == 0
4598 && !bfd_section_removed_from_list (link_info
.output_bfd
,
4601 /* Some sections have not yet been sized, notably .gnu.version,
4602 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
4603 input sections, so don't drop output sections that have such
4604 input sections unless they are also marked SEC_EXCLUDE. */
4605 if (exclude
&& output_section
->map_head
.s
!= NULL
)
4609 for (s
= output_section
->map_head
.s
; s
!= NULL
; s
= s
->map_head
.s
)
4610 if ((s
->flags
& SEC_EXCLUDE
) == 0
4611 && ((s
->flags
& SEC_LINKER_CREATED
) != 0
4612 || link_info
.emitrelocations
))
4621 /* We don't set bfd_section to NULL since bfd_section of the
4622 removed output section statement may still be used. */
4623 if (!os
->update_dot
)
4625 output_section
->flags
|= SEC_EXCLUDE
;
4626 bfd_section_list_remove (link_info
.output_bfd
, output_section
);
4627 link_info
.output_bfd
->section_count
--;
4632 /* Called from ldwrite to clear out asection.map_head and
4633 asection.map_tail for use as link_orders in ldwrite. */
4636 lang_clear_os_map (void)
4638 lang_output_section_statement_type
*os
;
4640 if (map_head_is_link_order
)
4643 for (os
= (void *) lang_os_list
.head
;
4647 asection
*output_section
;
4649 if (os
->constraint
< 0)
4652 output_section
= os
->bfd_section
;
4653 if (output_section
== NULL
)
4656 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
4657 output_section
->map_head
.link_order
= NULL
;
4658 output_section
->map_tail
.link_order
= NULL
;
4661 /* Stop future calls to lang_add_section from messing with map_head
4662 and map_tail link_order fields. */
4663 map_head_is_link_order
= true;
4667 print_output_section_statement
4668 (lang_output_section_statement_type
*output_section_statement
)
4670 asection
*section
= output_section_statement
->bfd_section
;
4673 if (output_section_statement
!= abs_output_section
)
4675 minfo ("\n%s", output_section_statement
->name
);
4677 if (section
!= NULL
)
4679 print_dot
= section
->vma
;
4681 len
= strlen (output_section_statement
->name
);
4682 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
4687 print_spaces (SECTION_NAME_MAP_LENGTH
- len
);
4689 minfo ("0x%V %W", section
->vma
, TO_ADDR (section
->size
));
4691 if (section
->vma
!= section
->lma
)
4692 minfo (_(" load address 0x%V"), section
->lma
);
4694 if (output_section_statement
->update_dot_tree
!= NULL
)
4695 exp_fold_tree (output_section_statement
->update_dot_tree
,
4696 output_section_statement
,
4697 bfd_abs_section_ptr
, &print_dot
);
4703 print_statement_list (output_section_statement
->children
.head
,
4704 output_section_statement
);
4708 print_assignment (lang_assignment_statement_type
*assignment
,
4709 lang_output_section_statement_type
*output_section
)
4715 print_spaces (SECTION_NAME_MAP_LENGTH
);
4717 if (assignment
->exp
->type
.node_class
== etree_assert
)
4720 tree
= assignment
->exp
->assert_s
.child
;
4724 const char *dst
= assignment
->exp
->assign
.dst
;
4726 is_dot
= (dst
[0] == '.' && dst
[1] == 0);
4727 tree
= assignment
->exp
;
4730 osec
= output_section
->bfd_section
;
4732 osec
= bfd_abs_section_ptr
;
4734 if (assignment
->exp
->type
.node_class
!= etree_provide
)
4735 exp_fold_tree (tree
, output_section
, osec
, &print_dot
);
4737 expld
.result
.valid_p
= false;
4740 const char *str
= buf
;
4741 if (expld
.result
.valid_p
)
4745 if (assignment
->exp
->type
.node_class
== etree_assert
4747 || expld
.assign_name
!= NULL
)
4749 value
= expld
.result
.value
;
4751 if (expld
.result
.section
!= NULL
)
4752 value
+= expld
.result
.section
->vma
;
4756 bfd_sprintf_vma (link_info
.output_bfd
, buf
+ 2, value
);
4762 struct bfd_link_hash_entry
*h
;
4764 h
= bfd_link_hash_lookup (link_info
.hash
, assignment
->exp
->assign
.dst
,
4765 false, false, true);
4767 && (h
->type
== bfd_link_hash_defined
4768 || h
->type
== bfd_link_hash_defweak
))
4770 value
= h
->u
.def
.value
;
4771 value
+= h
->u
.def
.section
->output_section
->vma
;
4772 value
+= h
->u
.def
.section
->output_offset
;
4777 bfd_sprintf_vma (link_info
.output_bfd
, buf
+ 3, value
);
4781 str
= "[unresolved]";
4786 if (assignment
->exp
->type
.node_class
== etree_provide
)
4791 expld
.assign_name
= NULL
;
4793 fprintf (config
.map_file
, "%-34s", str
);
4794 exp_print_tree (assignment
->exp
);
4799 print_input_statement (lang_input_statement_type
*statm
)
4801 if (statm
->filename
!= NULL
)
4802 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
4805 /* Print all symbols defined in a particular section. This is called
4806 via bfd_link_hash_traverse, or by print_all_symbols. */
4809 print_one_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
)
4811 asection
*sec
= (asection
*) ptr
;
4813 if ((hash_entry
->type
== bfd_link_hash_defined
4814 || hash_entry
->type
== bfd_link_hash_defweak
)
4815 && sec
== hash_entry
->u
.def
.section
)
4817 print_spaces (SECTION_NAME_MAP_LENGTH
);
4819 (hash_entry
->u
.def
.value
4820 + hash_entry
->u
.def
.section
->output_offset
4821 + hash_entry
->u
.def
.section
->output_section
->vma
));
4823 minfo (" %pT\n", hash_entry
->root
.string
);
4830 hash_entry_addr_cmp (const void *a
, const void *b
)
4832 const struct bfd_link_hash_entry
*l
= *(const struct bfd_link_hash_entry
**)a
;
4833 const struct bfd_link_hash_entry
*r
= *(const struct bfd_link_hash_entry
**)b
;
4835 if (l
->u
.def
.value
< r
->u
.def
.value
)
4837 else if (l
->u
.def
.value
> r
->u
.def
.value
)
4844 print_all_symbols (asection
*sec
)
4846 input_section_userdata_type
*ud
= bfd_section_userdata (sec
);
4847 struct map_symbol_def
*def
;
4848 struct bfd_link_hash_entry
**entries
;
4854 *ud
->map_symbol_def_tail
= 0;
4856 /* Sort the symbols by address. */
4857 entries
= (struct bfd_link_hash_entry
**)
4858 obstack_alloc (&map_obstack
,
4859 ud
->map_symbol_def_count
* sizeof (*entries
));
4861 for (i
= 0, def
= ud
->map_symbol_def_head
; def
; def
= def
->next
, i
++)
4862 entries
[i
] = def
->entry
;
4864 qsort (entries
, ud
->map_symbol_def_count
, sizeof (*entries
),
4865 hash_entry_addr_cmp
);
4867 /* Print the symbols. */
4868 for (i
= 0; i
< ud
->map_symbol_def_count
; i
++)
4869 ldemul_print_symbol (entries
[i
], sec
);
4871 obstack_free (&map_obstack
, entries
);
4874 /* Returns TRUE if SYM is a symbol suitable for printing
4875 in a linker map as a local symbol. */
4878 ld_is_local_symbol (asymbol
* sym
)
4880 const char * name
= bfd_asymbol_name (sym
);
4882 if (name
== NULL
|| *name
== 0)
4885 if (strcmp (name
, "(null)") == 0)
4888 /* Skip .Lxxx and such like. */
4889 if (bfd_is_local_label (link_info
.output_bfd
, sym
))
4892 /* FIXME: This is intended to skip ARM mapping symbols,
4893 which for some reason are not excluded by bfd_is_local_label,
4894 but maybe it is wrong for other architectures.
4895 It would be better to fix bfd_is_local_label. */
4899 /* Some local symbols, eg _GLOBAL_OFFSET_TABLE_, are present
4900 in the hash table, so do not print duplicates here. */
4901 struct bfd_link_hash_entry
* h
;
4902 h
= bfd_link_hash_lookup (link_info
.hash
, name
, false /* create */,
4903 false /* copy */, true /* follow */);
4907 /* Symbols from the plugin owned BFD will not get their own
4908 iteration of this function, but can be on the link_info
4909 list. So include them here. */
4910 if (h
->u
.def
.section
->owner
!= NULL
4911 && ((bfd_get_file_flags (h
->u
.def
.section
->owner
) & (BFD_LINKER_CREATED
| BFD_PLUGIN
))
4912 == (BFD_LINKER_CREATED
| BFD_PLUGIN
)))
4918 /* Print information about an input section to the map file. */
4921 print_input_section (asection
*i
, bool is_discarded
)
4923 bfd_size_type size
= i
->size
;
4929 minfo (" %s", i
->name
);
4931 len
= 1 + strlen (i
->name
);
4932 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
4937 print_spaces (SECTION_NAME_MAP_LENGTH
- len
);
4939 if (i
->output_section
!= NULL
4940 && i
->output_section
->owner
== link_info
.output_bfd
)
4941 addr
= i
->output_section
->vma
+ i
->output_offset
;
4950 bfd_sprintf_vma (link_info
.output_bfd
, buf
, addr
);
4951 minfo ("0x%s %W %pB\n", buf
, TO_ADDR (size
), i
->owner
);
4953 if (size
!= i
->rawsize
&& i
->rawsize
!= 0)
4955 len
= SECTION_NAME_MAP_LENGTH
+ 3 + strlen (buf
);
4957 minfo (_("%W (size before relaxing)\n"), TO_ADDR (i
->rawsize
));
4960 if (i
->output_section
!= NULL
4961 && i
->output_section
->owner
== link_info
.output_bfd
)
4963 if (link_info
.reduce_memory_overheads
)
4964 bfd_link_hash_traverse (link_info
.hash
, ldemul_print_symbol
, i
);
4966 print_all_symbols (i
);
4968 /* Update print_dot, but make sure that we do not move it
4969 backwards - this could happen if we have overlays and a
4970 later overlay is shorter than an earier one. */
4971 if (addr
+ TO_ADDR (size
) > print_dot
)
4972 print_dot
= addr
+ TO_ADDR (size
);
4974 if (config
.print_map_locals
)
4976 long storage_needed
;
4978 /* FIXME: It would be better to cache this table, rather
4979 than recreating it for each output section. */
4980 /* FIXME: This call is not working for non-ELF based targets.
4982 storage_needed
= bfd_get_symtab_upper_bound (link_info
.output_bfd
);
4983 if (storage_needed
> 0)
4985 asymbol
** symbol_table
;
4986 long number_of_symbols
;
4989 symbol_table
= xmalloc (storage_needed
);
4990 number_of_symbols
= bfd_canonicalize_symtab (link_info
.output_bfd
, symbol_table
);
4992 for (j
= 0; j
< number_of_symbols
; j
++)
4994 asymbol
* sym
= symbol_table
[j
];
4995 bfd_vma sym_addr
= sym
->value
+ i
->output_section
->vma
;
4997 if (sym
->section
== i
->output_section
4998 && (sym
->flags
& BSF_LOCAL
) != 0
5000 && sym_addr
< print_dot
5001 && ld_is_local_symbol (sym
))
5003 print_spaces (SECTION_NAME_MAP_LENGTH
);
5004 minfo ("0x%V (local) %s\n", sym_addr
, bfd_asymbol_name (sym
));
5008 free (symbol_table
);
5015 print_fill_statement (lang_fill_statement_type
*fill
)
5019 fputs (" FILL mask 0x", config
.map_file
);
5020 for (p
= fill
->fill
->data
, size
= fill
->fill
->size
; size
!= 0; p
++, size
--)
5021 fprintf (config
.map_file
, "%02x", *p
);
5022 fputs ("\n", config
.map_file
);
5026 print_data_statement (lang_data_statement_type
*data
)
5032 init_opb (data
->output_section
);
5033 print_spaces (SECTION_NAME_MAP_LENGTH
);
5035 addr
= data
->output_offset
;
5036 if (data
->output_section
!= NULL
)
5037 addr
+= data
->output_section
->vma
;
5065 if (size
< TO_SIZE ((unsigned) 1))
5066 size
= TO_SIZE ((unsigned) 1);
5067 minfo ("0x%V %W %s 0x%v", addr
, TO_ADDR (size
), name
, data
->value
);
5069 if (data
->exp
->type
.node_class
!= etree_value
)
5072 exp_print_tree (data
->exp
);
5077 print_dot
= addr
+ TO_ADDR (size
);
5080 /* Print an address statement. These are generated by options like
5084 print_address_statement (lang_address_statement_type
*address
)
5086 minfo (_("Address of section %s set to "), address
->section_name
);
5087 exp_print_tree (address
->address
);
5091 /* Print a reloc statement. */
5094 print_reloc_statement (lang_reloc_statement_type
*reloc
)
5099 init_opb (reloc
->output_section
);
5100 print_spaces (SECTION_NAME_MAP_LENGTH
);
5102 addr
= reloc
->output_offset
;
5103 if (reloc
->output_section
!= NULL
)
5104 addr
+= reloc
->output_section
->vma
;
5106 size
= bfd_get_reloc_size (reloc
->howto
);
5108 minfo ("0x%V %W RELOC %s ", addr
, TO_ADDR (size
), reloc
->howto
->name
);
5110 if (reloc
->name
!= NULL
)
5111 minfo ("%s+", reloc
->name
);
5113 minfo ("%s+", reloc
->section
->name
);
5115 exp_print_tree (reloc
->addend_exp
);
5119 print_dot
= addr
+ TO_ADDR (size
);
5123 print_padding_statement (lang_padding_statement_type
*s
)
5128 init_opb (s
->output_section
);
5131 len
= sizeof " *fill*" - 1;
5132 print_spaces (SECTION_NAME_MAP_LENGTH
- len
);
5134 addr
= s
->output_offset
;
5135 if (s
->output_section
!= NULL
)
5136 addr
+= s
->output_section
->vma
;
5137 minfo ("0x%V %W ", addr
, TO_ADDR (s
->size
));
5139 if (s
->fill
->size
!= 0)
5143 for (p
= s
->fill
->data
, size
= s
->fill
->size
; size
!= 0; p
++, size
--)
5144 fprintf (config
.map_file
, "%02x", *p
);
5149 print_dot
= addr
+ TO_ADDR (s
->size
);
5153 print_wild_statement (lang_wild_statement_type
*w
,
5154 lang_output_section_statement_type
*os
)
5156 struct wildcard_list
*sec
;
5160 if (w
->exclude_name_list
)
5163 minfo ("EXCLUDE_FILE(%s", w
->exclude_name_list
->name
);
5164 for (tmp
= w
->exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
5165 minfo (" %s", tmp
->name
);
5169 if (w
->filenames_sorted
)
5170 minfo ("SORT_BY_NAME(");
5171 if (w
->filenames_reversed
)
5173 if (w
->filename
!= NULL
)
5174 minfo ("%s", w
->filename
);
5177 if (w
->filenames_reversed
)
5179 if (w
->filenames_sorted
)
5183 for (sec
= w
->section_list
; sec
; sec
= sec
->next
)
5185 int closing_paren
= 0;
5187 switch (sec
->spec
.sorted
)
5193 minfo ("SORT_BY_NAME(");
5198 minfo ("SORT_BY_ALIGNMENT(");
5202 case by_name_alignment
:
5203 minfo ("SORT_BY_NAME(SORT_BY_ALIGNMENT(");
5207 case by_alignment_name
:
5208 minfo ("SORT_BY_ALIGNMENT(SORT_BY_NAME(");
5213 minfo ("SORT_NONE(");
5217 case by_init_priority
:
5218 minfo ("SORT_BY_INIT_PRIORITY(");
5223 if (sec
->spec
.reversed
)
5229 if (sec
->spec
.exclude_name_list
!= NULL
)
5232 minfo ("EXCLUDE_FILE(%s", sec
->spec
.exclude_name_list
->name
);
5233 for (tmp
= sec
->spec
.exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
5234 minfo (" %s", tmp
->name
);
5237 if (sec
->spec
.name
!= NULL
)
5238 minfo ("%s", sec
->spec
.name
);
5241 for (;closing_paren
> 0; closing_paren
--)
5250 print_statement_list (w
->children
.head
, os
);
5253 /* Print a group statement. */
5256 print_group (lang_group_statement_type
*s
,
5257 lang_output_section_statement_type
*os
)
5259 fprintf (config
.map_file
, "START GROUP\n");
5260 print_statement_list (s
->children
.head
, os
);
5261 fprintf (config
.map_file
, "END GROUP\n");
5264 /* Print the list of statements in S.
5265 This can be called for any statement type. */
5268 print_statement_list (lang_statement_union_type
*s
,
5269 lang_output_section_statement_type
*os
)
5273 print_statement (s
, os
);
5278 /* Print the first statement in statement list S.
5279 This can be called for any statement type. */
5282 print_statement (lang_statement_union_type
*s
,
5283 lang_output_section_statement_type
*os
)
5285 switch (s
->header
.type
)
5288 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
5291 case lang_constructors_statement_enum
:
5292 if (constructor_list
.head
!= NULL
)
5294 if (constructors_sorted
)
5295 minfo (" SORT (CONSTRUCTORS)\n");
5297 minfo (" CONSTRUCTORS\n");
5298 print_statement_list (constructor_list
.head
, os
);
5301 case lang_wild_statement_enum
:
5302 print_wild_statement (&s
->wild_statement
, os
);
5304 case lang_address_statement_enum
:
5305 print_address_statement (&s
->address_statement
);
5307 case lang_object_symbols_statement_enum
:
5308 minfo (" CREATE_OBJECT_SYMBOLS\n");
5310 case lang_fill_statement_enum
:
5311 print_fill_statement (&s
->fill_statement
);
5313 case lang_data_statement_enum
:
5314 print_data_statement (&s
->data_statement
);
5316 case lang_reloc_statement_enum
:
5317 print_reloc_statement (&s
->reloc_statement
);
5319 case lang_input_section_enum
:
5320 print_input_section (s
->input_section
.section
, false);
5322 case lang_padding_statement_enum
:
5323 print_padding_statement (&s
->padding_statement
);
5325 case lang_output_section_statement_enum
:
5326 print_output_section_statement (&s
->output_section_statement
);
5328 case lang_assignment_statement_enum
:
5329 print_assignment (&s
->assignment_statement
, os
);
5331 case lang_target_statement_enum
:
5332 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
5334 case lang_output_statement_enum
:
5335 minfo ("OUTPUT(%s", s
->output_statement
.name
);
5336 if (output_target
!= NULL
)
5337 minfo (" %s", output_target
);
5340 case lang_input_statement_enum
:
5341 print_input_statement (&s
->input_statement
);
5343 case lang_group_statement_enum
:
5344 print_group (&s
->group_statement
, os
);
5346 case lang_insert_statement_enum
:
5347 minfo ("INSERT %s %s\n",
5348 s
->insert_statement
.is_before
? "BEFORE" : "AFTER",
5349 s
->insert_statement
.where
);
5355 print_statements (void)
5357 print_statement_list (statement_list
.head
, abs_output_section
);
5360 /* Print the first N statements in statement list S to STDERR.
5361 If N == 0, nothing is printed.
5362 If N < 0, the entire list is printed.
5363 Intended to be called from GDB. */
5366 dprint_statement (lang_statement_union_type
*s
, int n
)
5368 FILE *map_save
= config
.map_file
;
5370 config
.map_file
= stderr
;
5373 print_statement_list (s
, abs_output_section
);
5376 while (s
&& --n
>= 0)
5378 print_statement (s
, abs_output_section
);
5383 config
.map_file
= map_save
;
5387 insert_pad (lang_statement_union_type
**ptr
,
5389 bfd_size_type alignment_needed
,
5390 asection
*output_section
,
5393 static fill_type zero_fill
;
5394 lang_statement_union_type
*pad
= NULL
;
5396 if (ptr
!= &statement_list
.head
)
5397 pad
= ((lang_statement_union_type
*)
5398 ((char *) ptr
- offsetof (lang_statement_union_type
, header
.next
)));
5400 && pad
->header
.type
== lang_padding_statement_enum
5401 && pad
->padding_statement
.output_section
== output_section
)
5403 /* Use the existing pad statement. */
5405 else if ((pad
= *ptr
) != NULL
5406 && pad
->header
.type
== lang_padding_statement_enum
5407 && pad
->padding_statement
.output_section
== output_section
)
5409 /* Use the existing pad statement. */
5413 /* Make a new padding statement, linked into existing chain. */
5414 pad
= stat_alloc (sizeof (lang_padding_statement_type
));
5415 pad
->header
.next
= *ptr
;
5417 pad
->header
.type
= lang_padding_statement_enum
;
5418 pad
->padding_statement
.output_section
= output_section
;
5421 pad
->padding_statement
.fill
= fill
;
5423 pad
->padding_statement
.output_offset
= dot
- output_section
->vma
;
5424 pad
->padding_statement
.size
= alignment_needed
;
5425 if (!(output_section
->flags
& SEC_FIXED_SIZE
))
5426 output_section
->size
= TO_SIZE (dot
+ TO_ADDR (alignment_needed
)
5427 - output_section
->vma
);
5430 /* Work out how much this section will move the dot point. */
5434 (lang_statement_union_type
**this_ptr
,
5435 lang_output_section_statement_type
*output_section_statement
,
5440 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
5441 asection
*i
= is
->section
;
5442 asection
*o
= output_section_statement
->bfd_section
;
5445 if (link_info
.non_contiguous_regions
)
5447 /* If the input section I has already been successfully assigned
5448 to an output section other than O, don't bother with it and
5449 let the caller remove it from the list. Keep processing in
5450 case we have already handled O, because the repeated passes
5451 have reinitialized its size. */
5452 if (i
->already_assigned
&& i
->already_assigned
!= o
)
5459 if (i
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
5460 i
->output_offset
= i
->vma
- o
->vma
;
5461 else if (((i
->flags
& SEC_EXCLUDE
) != 0)
5462 || output_section_statement
->ignored
)
5463 i
->output_offset
= dot
- o
->vma
;
5466 bfd_size_type alignment_needed
;
5468 /* Align this section first to the input sections requirement,
5469 then to the output section's requirement. If this alignment
5470 is greater than any seen before, then record it too. Perform
5471 the alignment by inserting a magic 'padding' statement.
5472 We can force input section alignment within an output section
5473 by using SUBALIGN. The value specified overrides any alignment
5474 given by input sections, whether larger or smaller. */
5476 if (output_section_statement
->subsection_alignment
!= NULL
)
5477 o
->alignment_power
= i
->alignment_power
=
5478 exp_get_power (output_section_statement
->subsection_alignment
,
5479 output_section_statement
,
5480 "subsection alignment");
5482 if (o
->alignment_power
< i
->alignment_power
)
5483 o
->alignment_power
= i
->alignment_power
;
5485 alignment_needed
= align_power (dot
, i
->alignment_power
) - dot
;
5487 if (alignment_needed
!= 0)
5489 insert_pad (this_ptr
, fill
, TO_SIZE (alignment_needed
), o
, dot
);
5490 dot
+= alignment_needed
;
5493 if (link_info
.non_contiguous_regions
)
5495 /* If I would overflow O, let the caller remove I from the
5497 if (output_section_statement
->region
)
5499 bfd_vma end
= output_section_statement
->region
->origin
5500 + output_section_statement
->region
->length
;
5502 if (dot
+ TO_ADDR (i
->size
) > end
)
5504 if (i
->flags
& SEC_LINKER_CREATED
)
5505 einfo (_("%F%P: Output section `%pA' not large enough for "
5506 "the linker-created stubs section `%pA'.\n"),
5507 i
->output_section
, i
);
5509 if (i
->rawsize
&& i
->rawsize
!= i
->size
)
5510 einfo (_("%F%P: Relaxation not supported with "
5511 "--enable-non-contiguous-regions (section `%pA' "
5512 "would overflow `%pA' after it changed size).\n"),
5513 i
, i
->output_section
);
5517 i
->output_section
= NULL
;
5523 /* Remember where in the output section this input section goes. */
5524 i
->output_offset
= dot
- o
->vma
;
5526 /* Mark how big the output section must be to contain this now. */
5527 dot
+= TO_ADDR (i
->size
);
5528 if (!(o
->flags
& SEC_FIXED_SIZE
))
5529 o
->size
= TO_SIZE (dot
- o
->vma
);
5531 if (link_info
.non_contiguous_regions
)
5533 /* Record that I was successfully assigned to O, and update
5534 its actual output section too. */
5535 i
->already_assigned
= o
;
5536 i
->output_section
= o
;
5550 sort_sections_by_lma (const void *arg1
, const void *arg2
)
5552 const asection
*sec1
= ((const struct check_sec
*) arg1
)->sec
;
5553 const asection
*sec2
= ((const struct check_sec
*) arg2
)->sec
;
5555 if (sec1
->lma
< sec2
->lma
)
5557 else if (sec1
->lma
> sec2
->lma
)
5559 else if (sec1
->id
< sec2
->id
)
5561 else if (sec1
->id
> sec2
->id
)
5568 sort_sections_by_vma (const void *arg1
, const void *arg2
)
5570 const asection
*sec1
= ((const struct check_sec
*) arg1
)->sec
;
5571 const asection
*sec2
= ((const struct check_sec
*) arg2
)->sec
;
5573 if (sec1
->vma
< sec2
->vma
)
5575 else if (sec1
->vma
> sec2
->vma
)
5577 else if (sec1
->id
< sec2
->id
)
5579 else if (sec1
->id
> sec2
->id
)
5585 #define IS_TBSS(s) \
5586 ((s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == SEC_THREAD_LOCAL)
5588 #define IGNORE_SECTION(s) \
5589 ((s->flags & SEC_ALLOC) == 0 || IS_TBSS (s))
5591 /* Check to see if any allocated sections overlap with other allocated
5592 sections. This can happen if a linker script specifies the output
5593 section addresses of the two sections. Also check whether any memory
5594 region has overflowed. */
5597 lang_check_section_addresses (void)
5600 struct check_sec
*sections
;
5605 bfd_vma p_start
= 0;
5607 lang_memory_region_type
*m
;
5610 /* Detect address space overflow on allocated sections. */
5611 addr_mask
= ((bfd_vma
) 1 <<
5612 (bfd_arch_bits_per_address (link_info
.output_bfd
) - 1)) - 1;
5613 addr_mask
= (addr_mask
<< 1) + 1;
5614 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5615 if ((s
->flags
& SEC_ALLOC
) != 0)
5617 s_end
= (s
->vma
+ s
->size
) & addr_mask
;
5618 if (s_end
!= 0 && s_end
< (s
->vma
& addr_mask
))
5619 einfo (_("%X%P: section %s VMA wraps around address space\n"),
5623 s_end
= (s
->lma
+ s
->size
) & addr_mask
;
5624 if (s_end
!= 0 && s_end
< (s
->lma
& addr_mask
))
5625 einfo (_("%X%P: section %s LMA wraps around address space\n"),
5630 if (bfd_count_sections (link_info
.output_bfd
) <= 1)
5633 count
= bfd_count_sections (link_info
.output_bfd
);
5634 sections
= XNEWVEC (struct check_sec
, count
);
5636 /* Scan all sections in the output list. */
5638 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5640 if (IGNORE_SECTION (s
)
5644 sections
[count
].sec
= s
;
5645 sections
[count
].warned
= false;
5655 qsort (sections
, count
, sizeof (*sections
), sort_sections_by_lma
);
5657 /* First check section LMAs. There should be no overlap of LMAs on
5658 loadable sections, even with overlays. */
5659 for (p
= NULL
, i
= 0; i
< count
; i
++)
5661 s
= sections
[i
].sec
;
5663 if ((s
->flags
& SEC_LOAD
) != 0)
5666 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
5668 /* Look for an overlap. We have sorted sections by lma, so
5669 we know that s_start >= p_start. Besides the obvious
5670 case of overlap when the current section starts before
5671 the previous one ends, we also must have overlap if the
5672 previous section wraps around the address space. */
5674 && (s_start
<= p_end
5675 || p_end
< p_start
))
5677 einfo (_("%X%P: section %s LMA [%V,%V]"
5678 " overlaps section %s LMA [%V,%V]\n"),
5679 s
->name
, s_start
, s_end
, p
->name
, p_start
, p_end
);
5680 sections
[i
].warned
= true;
5688 /* If any non-zero size allocated section (excluding tbss) starts at
5689 exactly the same VMA as another such section, then we have
5690 overlays. Overlays generated by the OVERLAY keyword will have
5691 this property. It is possible to intentionally generate overlays
5692 that fail this test, but it would be unusual. */
5693 qsort (sections
, count
, sizeof (*sections
), sort_sections_by_vma
);
5695 p_start
= sections
[0].sec
->vma
;
5696 for (i
= 1; i
< count
; i
++)
5698 s_start
= sections
[i
].sec
->vma
;
5699 if (p_start
== s_start
)
5707 /* Now check section VMAs if no overlays were detected. */
5710 for (p
= NULL
, i
= 0; i
< count
; i
++)
5712 s
= sections
[i
].sec
;
5715 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
5718 && !sections
[i
].warned
5719 && (s_start
<= p_end
5720 || p_end
< p_start
))
5721 einfo (_("%X%P: section %s VMA [%V,%V]"
5722 " overlaps section %s VMA [%V,%V]\n"),
5723 s
->name
, s_start
, s_end
, p
->name
, p_start
, p_end
);
5732 /* If any memory region has overflowed, report by how much.
5733 We do not issue this diagnostic for regions that had sections
5734 explicitly placed outside their bounds; os_region_check's
5735 diagnostics are adequate for that case.
5737 FIXME: It is conceivable that m->current - (m->origin + m->length)
5738 might overflow a 32-bit integer. There is, alas, no way to print
5739 a bfd_vma quantity in decimal. */
5740 for (m
= lang_memory_region_list
; m
; m
= m
->next
)
5741 if (m
->had_full_message
)
5743 unsigned long over
= m
->current
- (m
->origin
+ m
->length
);
5744 einfo (ngettext ("%X%P: region `%s' overflowed by %lu byte\n",
5745 "%X%P: region `%s' overflowed by %lu bytes\n",
5747 m
->name_list
.name
, over
);
5751 /* Make sure the new address is within the region. We explicitly permit the
5752 current address to be at the exact end of the region when the address is
5753 non-zero, in case the region is at the end of addressable memory and the
5754 calculation wraps around. */
5757 os_region_check (lang_output_section_statement_type
*os
,
5758 lang_memory_region_type
*region
,
5762 if ((region
->current
< region
->origin
5763 || (region
->current
- region
->origin
> region
->length
))
5764 && ((region
->current
!= region
->origin
+ region
->length
)
5769 einfo (_("%X%P: address 0x%v of %pB section `%s'"
5770 " is not within region `%s'\n"),
5772 os
->bfd_section
->owner
,
5773 os
->bfd_section
->name
,
5774 region
->name_list
.name
);
5776 else if (!region
->had_full_message
)
5778 region
->had_full_message
= true;
5780 einfo (_("%X%P: %pB section `%s' will not fit in region `%s'\n"),
5781 os
->bfd_section
->owner
,
5782 os
->bfd_section
->name
,
5783 region
->name_list
.name
);
5789 ldlang_check_relro_region (lang_statement_union_type
*s
)
5791 seg_align_type
*seg
= &expld
.dataseg
;
5793 if (seg
->relro
== exp_seg_relro_start
)
5795 if (!seg
->relro_start_stat
)
5796 seg
->relro_start_stat
= s
;
5799 ASSERT (seg
->relro_start_stat
== s
);
5802 else if (seg
->relro
== exp_seg_relro_end
)
5804 if (!seg
->relro_end_stat
)
5805 seg
->relro_end_stat
= s
;
5808 ASSERT (seg
->relro_end_stat
== s
);
5813 /* Set the sizes for all the output sections. */
5816 lang_size_sections_1
5817 (lang_statement_union_type
**prev
,
5818 lang_output_section_statement_type
*current_os
,
5824 lang_statement_union_type
*s
;
5825 lang_statement_union_type
*prev_s
= NULL
;
5826 bool removed_prev_s
= false;
5827 lang_output_section_statement_type
*os
= current_os
;
5829 /* Size up the sections from their constituent parts. */
5830 for (s
= *prev
; s
!= NULL
; prev_s
= s
, s
= s
->header
.next
)
5832 bool removed
= false;
5834 switch (s
->header
.type
)
5836 case lang_output_section_statement_enum
:
5838 bfd_vma newdot
, after
, dotdelta
;
5839 lang_memory_region_type
*r
;
5840 int section_alignment
= 0;
5842 os
= &s
->output_section_statement
;
5843 init_opb (os
->bfd_section
);
5844 if (os
->constraint
== -1)
5847 /* FIXME: We shouldn't need to zero section vmas for ld -r
5848 here, in lang_insert_orphan, or in the default linker scripts.
5849 This is covering for coff backend linker bugs. See PR6945. */
5850 if (os
->addr_tree
== NULL
5851 && bfd_link_relocatable (&link_info
)
5852 && (bfd_get_flavour (link_info
.output_bfd
)
5853 == bfd_target_coff_flavour
))
5854 os
->addr_tree
= exp_intop (0);
5855 if (os
->addr_tree
!= NULL
)
5857 exp_fold_tree (os
->addr_tree
, os
, bfd_abs_section_ptr
, &dot
);
5859 if (expld
.result
.valid_p
)
5861 dot
= expld
.result
.value
;
5862 if (expld
.result
.section
!= NULL
)
5863 dot
+= expld
.result
.section
->vma
;
5865 else if (expld
.phase
!= lang_mark_phase_enum
)
5866 einfo (_("%F%P:%pS: non constant or forward reference"
5867 " address expression for section %s\n"),
5868 os
->addr_tree
, os
->name
);
5871 if (os
->bfd_section
== NULL
)
5872 /* This section was removed or never actually created. */
5875 /* If this is a COFF shared library section, use the size and
5876 address from the input section. FIXME: This is COFF
5877 specific; it would be cleaner if there were some other way
5878 to do this, but nothing simple comes to mind. */
5879 if (((bfd_get_flavour (link_info
.output_bfd
)
5880 == bfd_target_ecoff_flavour
)
5881 || (bfd_get_flavour (link_info
.output_bfd
)
5882 == bfd_target_coff_flavour
))
5883 && (os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
5887 if (os
->children
.head
== NULL
5888 || os
->children
.head
->header
.next
!= NULL
5889 || (os
->children
.head
->header
.type
5890 != lang_input_section_enum
))
5891 einfo (_("%X%P: internal error on COFF shared library"
5892 " section %s\n"), os
->name
);
5894 input
= os
->children
.head
->input_section
.section
;
5895 bfd_set_section_vma (os
->bfd_section
,
5896 bfd_section_vma (input
));
5897 if (!(os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
5898 os
->bfd_section
->size
= input
->size
;
5904 if (bfd_is_abs_section (os
->bfd_section
))
5906 /* No matter what happens, an abs section starts at zero. */
5907 ASSERT (os
->bfd_section
->vma
== 0);
5911 if (os
->addr_tree
== NULL
)
5913 /* No address specified for this section, get one
5914 from the region specification. */
5915 if (os
->region
== NULL
5916 || ((os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))
5917 && os
->region
->name_list
.name
[0] == '*'
5918 && strcmp (os
->region
->name_list
.name
,
5919 DEFAULT_MEMORY_REGION
) == 0))
5921 os
->region
= lang_memory_default (os
->bfd_section
);
5924 /* If a loadable section is using the default memory
5925 region, and some non default memory regions were
5926 defined, issue an error message. */
5928 && !IGNORE_SECTION (os
->bfd_section
)
5929 && !bfd_link_relocatable (&link_info
)
5931 && strcmp (os
->region
->name_list
.name
,
5932 DEFAULT_MEMORY_REGION
) == 0
5933 && lang_memory_region_list
!= NULL
5934 && (strcmp (lang_memory_region_list
->name_list
.name
,
5935 DEFAULT_MEMORY_REGION
) != 0
5936 || lang_memory_region_list
->next
!= NULL
)
5937 && lang_sizing_iteration
== 1)
5939 /* By default this is an error rather than just a
5940 warning because if we allocate the section to the
5941 default memory region we can end up creating an
5942 excessively large binary, or even seg faulting when
5943 attempting to perform a negative seek. See
5944 sources.redhat.com/ml/binutils/2003-04/msg00423.html
5945 for an example of this. This behaviour can be
5946 overridden by the using the --no-check-sections
5948 if (command_line
.check_section_addresses
)
5949 einfo (_("%F%P: error: no memory region specified"
5950 " for loadable section `%s'\n"),
5951 bfd_section_name (os
->bfd_section
));
5953 einfo (_("%P: warning: no memory region specified"
5954 " for loadable section `%s'\n"),
5955 bfd_section_name (os
->bfd_section
));
5958 newdot
= os
->region
->current
;
5959 section_alignment
= os
->bfd_section
->alignment_power
;
5962 section_alignment
= exp_get_power (os
->section_alignment
, os
,
5963 "section alignment");
5965 /* Align to what the section needs. */
5966 if (section_alignment
> 0)
5968 bfd_vma savedot
= newdot
;
5971 newdot
= align_power (newdot
, section_alignment
);
5972 dotdelta
= newdot
- savedot
;
5974 if (lang_sizing_iteration
== 1)
5976 else if (lang_sizing_iteration
> 1)
5978 /* Only report adjustments that would change
5979 alignment from what we have already reported. */
5980 diff
= newdot
- os
->bfd_section
->vma
;
5981 if (!(diff
& (((bfd_vma
) 1 << section_alignment
) - 1)))
5985 && (config
.warn_section_align
5986 || os
->addr_tree
!= NULL
))
5987 einfo (_("%P: warning: "
5988 "start of section %s changed by %ld\n"),
5989 os
->name
, (long) diff
);
5992 bfd_set_section_vma (os
->bfd_section
, newdot
);
5994 os
->bfd_section
->output_offset
= 0;
5997 lang_size_sections_1 (&os
->children
.head
, os
,
5998 os
->fill
, newdot
, relax
, check_regions
);
6000 os
->processed_vma
= true;
6002 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
6003 /* Except for some special linker created sections,
6004 no output section should change from zero size
6005 after strip_excluded_output_sections. A non-zero
6006 size on an ignored section indicates that some
6007 input section was not sized early enough. */
6008 ASSERT (os
->bfd_section
->size
== 0);
6011 dot
= os
->bfd_section
->vma
;
6013 /* Put the section within the requested block size, or
6014 align at the block boundary. */
6016 + TO_ADDR (os
->bfd_section
->size
)
6017 + os
->block_value
- 1)
6018 & - (bfd_vma
) os
->block_value
);
6020 if (!(os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
6021 os
->bfd_section
->size
= TO_SIZE (after
6022 - os
->bfd_section
->vma
);
6025 /* Set section lma. */
6028 r
= lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, false);
6032 bfd_vma lma
= exp_get_abs_int (os
->load_base
, 0, "load base");
6033 os
->bfd_section
->lma
= lma
;
6035 else if (os
->lma_region
!= NULL
)
6037 bfd_vma lma
= os
->lma_region
->current
;
6039 if (os
->align_lma_with_input
)
6043 /* When LMA_REGION is the same as REGION, align the LMA
6044 as we did for the VMA, possibly including alignment
6045 from the bfd section. If a different region, then
6046 only align according to the value in the output
6048 if (os
->lma_region
!= os
->region
)
6049 section_alignment
= exp_get_power (os
->section_alignment
,
6051 "section alignment");
6052 if (section_alignment
> 0)
6053 lma
= align_power (lma
, section_alignment
);
6055 os
->bfd_section
->lma
= lma
;
6057 else if (r
->last_os
!= NULL
6058 && (os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
6063 last
= r
->last_os
->output_section_statement
.bfd_section
;
6065 /* A backwards move of dot should be accompanied by
6066 an explicit assignment to the section LMA (ie.
6067 os->load_base set) because backwards moves can
6068 create overlapping LMAs. */
6070 && os
->bfd_section
->size
!= 0
6071 && dot
+ TO_ADDR (os
->bfd_section
->size
) <= last
->vma
)
6073 /* If dot moved backwards then leave lma equal to
6074 vma. This is the old default lma, which might
6075 just happen to work when the backwards move is
6076 sufficiently large. Nag if this changes anything,
6077 so people can fix their linker scripts. */
6079 if (last
->vma
!= last
->lma
)
6080 einfo (_("%P: warning: dot moved backwards "
6081 "before `%s'\n"), os
->name
);
6085 /* If this is an overlay, set the current lma to that
6086 at the end of the previous section. */
6087 if (os
->sectype
== overlay_section
)
6088 lma
= last
->lma
+ TO_ADDR (last
->size
);
6090 /* Otherwise, keep the same lma to vma relationship
6091 as the previous section. */
6093 lma
= os
->bfd_section
->vma
+ last
->lma
- last
->vma
;
6095 if (section_alignment
> 0)
6096 lma
= align_power (lma
, section_alignment
);
6097 os
->bfd_section
->lma
= lma
;
6100 os
->processed_lma
= true;
6102 /* Keep track of normal sections using the default
6103 lma region. We use this to set the lma for
6104 following sections. Overlays or other linker
6105 script assignment to lma might mean that the
6106 default lma == vma is incorrect.
6107 To avoid warnings about dot moving backwards when using
6108 -Ttext, don't start tracking sections until we find one
6109 of non-zero size or with lma set differently to vma.
6110 Do this tracking before we short-cut the loop so that we
6111 track changes for the case where the section size is zero,
6112 but the lma is set differently to the vma. This is
6113 important, if an orphan section is placed after an
6114 otherwise empty output section that has an explicit lma
6115 set, we want that lma reflected in the orphans lma. */
6116 if (((!IGNORE_SECTION (os
->bfd_section
)
6117 && (os
->bfd_section
->size
!= 0
6118 || (r
->last_os
== NULL
6119 && os
->bfd_section
->vma
!= os
->bfd_section
->lma
)
6120 || (r
->last_os
!= NULL
6121 && dot
>= (r
->last_os
->output_section_statement
6122 .bfd_section
->vma
))))
6123 || os
->sectype
== first_overlay_section
)
6124 && os
->lma_region
== NULL
6125 && !bfd_link_relocatable (&link_info
))
6128 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
6131 /* .tbss sections effectively have zero size. */
6132 if (!IS_TBSS (os
->bfd_section
)
6133 || bfd_link_relocatable (&link_info
))
6134 dotdelta
= TO_ADDR (os
->bfd_section
->size
);
6139 if (os
->update_dot_tree
!= 0)
6140 exp_fold_tree (os
->update_dot_tree
, os
, bfd_abs_section_ptr
, &dot
);
6142 /* Update dot in the region ?
6143 We only do this if the section is going to be allocated,
6144 since unallocated sections do not contribute to the region's
6145 overall size in memory. */
6146 if (os
->region
!= NULL
6147 && (os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
)))
6149 os
->region
->current
= dot
;
6152 /* Make sure the new address is within the region. */
6153 os_region_check (os
, os
->region
, os
->addr_tree
,
6154 os
->bfd_section
->vma
);
6156 if (os
->lma_region
!= NULL
&& os
->lma_region
!= os
->region
6157 && ((os
->bfd_section
->flags
& SEC_LOAD
)
6158 || os
->align_lma_with_input
))
6160 os
->lma_region
->current
= os
->bfd_section
->lma
+ dotdelta
;
6163 os_region_check (os
, os
->lma_region
, NULL
,
6164 os
->bfd_section
->lma
);
6170 case lang_constructors_statement_enum
:
6171 dot
= lang_size_sections_1 (&constructor_list
.head
, current_os
,
6172 fill
, dot
, relax
, check_regions
);
6175 case lang_data_statement_enum
:
6177 unsigned int size
= 0;
6179 s
->data_statement
.output_offset
= dot
- current_os
->bfd_section
->vma
;
6180 s
->data_statement
.output_section
= current_os
->bfd_section
;
6182 /* We might refer to provided symbols in the expression, and
6183 need to mark them as needed. */
6184 exp_fold_tree (s
->data_statement
.exp
, os
,
6185 bfd_abs_section_ptr
, &dot
);
6187 switch (s
->data_statement
.type
)
6205 if (size
< TO_SIZE ((unsigned) 1))
6206 size
= TO_SIZE ((unsigned) 1);
6207 dot
+= TO_ADDR (size
);
6208 if (!(current_os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
6209 current_os
->bfd_section
->size
6210 = TO_SIZE (dot
- current_os
->bfd_section
->vma
);
6215 case lang_reloc_statement_enum
:
6219 s
->reloc_statement
.output_offset
6220 = dot
- current_os
->bfd_section
->vma
;
6221 s
->reloc_statement
.output_section
6222 = current_os
->bfd_section
;
6223 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
6224 dot
+= TO_ADDR (size
);
6225 if (!(current_os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
6226 current_os
->bfd_section
->size
6227 = TO_SIZE (dot
- current_os
->bfd_section
->vma
);
6231 case lang_wild_statement_enum
:
6232 dot
= lang_size_sections_1 (&s
->wild_statement
.children
.head
,
6233 current_os
, fill
, dot
, relax
,
6237 case lang_object_symbols_statement_enum
:
6238 link_info
.create_object_symbols_section
= current_os
->bfd_section
;
6239 current_os
->bfd_section
->flags
|= SEC_KEEP
;
6242 case lang_output_statement_enum
:
6243 case lang_target_statement_enum
:
6246 case lang_input_section_enum
:
6250 i
= s
->input_section
.section
;
6255 if (!bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
6256 einfo (_("%F%P: can't relax section: %E\n"));
6260 dot
= size_input_section (prev
, current_os
, fill
, &removed
, dot
);
6264 case lang_input_statement_enum
:
6267 case lang_fill_statement_enum
:
6268 s
->fill_statement
.output_section
= current_os
->bfd_section
;
6270 fill
= s
->fill_statement
.fill
;
6273 case lang_assignment_statement_enum
:
6275 bfd_vma newdot
= dot
;
6276 etree_type
*tree
= s
->assignment_statement
.exp
;
6278 expld
.dataseg
.relro
= exp_seg_relro_none
;
6280 exp_fold_tree (tree
, os
, current_os
->bfd_section
, &newdot
);
6282 ldlang_check_relro_region (s
);
6284 expld
.dataseg
.relro
= exp_seg_relro_none
;
6286 /* This symbol may be relative to this section. */
6287 if ((tree
->type
.node_class
== etree_provided
6288 || tree
->type
.node_class
== etree_assign
)
6289 && (tree
->assign
.dst
[0] != '.'
6290 || tree
->assign
.dst
[1] != '\0'))
6291 current_os
->update_dot
= 1;
6293 if (!current_os
->ignored
)
6295 if (current_os
== abs_output_section
)
6297 /* If we don't have an output section, then just adjust
6298 the default memory address. */
6299 lang_memory_region_lookup (DEFAULT_MEMORY_REGION
,
6300 false)->current
= newdot
;
6302 else if (newdot
!= dot
)
6304 /* Insert a pad after this statement. We can't
6305 put the pad before when relaxing, in case the
6306 assignment references dot. */
6307 insert_pad (&s
->header
.next
, fill
, TO_SIZE (newdot
- dot
),
6308 current_os
->bfd_section
, dot
);
6310 /* Don't neuter the pad below when relaxing. */
6313 /* If dot is advanced, this implies that the section
6314 should have space allocated to it, unless the
6315 user has explicitly stated that the section
6316 should not be allocated. */
6317 if (current_os
->sectype
!= noalloc_section
6318 && (current_os
->sectype
!= noload_section
6319 || (bfd_get_flavour (link_info
.output_bfd
)
6320 == bfd_target_elf_flavour
)))
6321 current_os
->bfd_section
->flags
|= SEC_ALLOC
;
6328 case lang_padding_statement_enum
:
6329 /* If this is the first time lang_size_sections is called,
6330 we won't have any padding statements. If this is the
6331 second or later passes when relaxing, we should allow
6332 padding to shrink. If padding is needed on this pass, it
6333 will be added back in. */
6334 s
->padding_statement
.size
= 0;
6336 /* Make sure output_offset is valid. If relaxation shrinks
6337 the section and this pad isn't needed, it's possible to
6338 have output_offset larger than the final size of the
6339 section. bfd_set_section_contents will complain even for
6340 a pad size of zero. */
6341 s
->padding_statement
.output_offset
6342 = dot
- current_os
->bfd_section
->vma
;
6345 case lang_group_statement_enum
:
6346 dot
= lang_size_sections_1 (&s
->group_statement
.children
.head
,
6347 current_os
, fill
, dot
, relax
,
6351 case lang_insert_statement_enum
:
6354 /* We can only get here when relaxing is turned on. */
6355 case lang_address_statement_enum
:
6363 /* If an input section doesn't fit in the current output
6364 section, remove it from the list. Handle the case where we
6365 have to remove an input_section statement here: there is a
6366 special case to remove the first element of the list. */
6367 if (link_info
.non_contiguous_regions
&& removed
)
6369 /* If we removed the first element during the previous
6370 iteration, override the loop assignment of prev_s. */
6376 /* If there was a real previous input section, just skip
6378 prev_s
->header
.next
=s
->header
.next
;
6380 removed_prev_s
= false;
6384 /* Remove the first input section of the list. */
6385 *prev
= s
->header
.next
;
6386 removed_prev_s
= true;
6389 /* Move to next element, unless we removed the head of the
6391 if (!removed_prev_s
)
6392 prev
= &s
->header
.next
;
6396 prev
= &s
->header
.next
;
6397 removed_prev_s
= false;
6403 /* Callback routine that is used in _bfd_elf_map_sections_to_segments.
6404 The BFD library has set NEW_SEGMENT to TRUE iff it thinks that
6405 CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different
6406 segments. We are allowed an opportunity to override this decision. */
6409 ldlang_override_segment_assignment (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6410 bfd
*abfd ATTRIBUTE_UNUSED
,
6411 asection
*current_section
,
6412 asection
*previous_section
,
6415 lang_output_section_statement_type
*cur
;
6416 lang_output_section_statement_type
*prev
;
6418 /* The checks below are only necessary when the BFD library has decided
6419 that the two sections ought to be placed into the same segment. */
6423 /* Paranoia checks. */
6424 if (current_section
== NULL
|| previous_section
== NULL
)
6427 /* If this flag is set, the target never wants code and non-code
6428 sections comingled in the same segment. */
6429 if (config
.separate_code
6430 && ((current_section
->flags
^ previous_section
->flags
) & SEC_CODE
))
6433 /* Find the memory regions associated with the two sections.
6434 We call lang_output_section_find() here rather than scanning the list
6435 of output sections looking for a matching section pointer because if
6436 we have a large number of sections then a hash lookup is faster. */
6437 cur
= lang_output_section_find (current_section
->name
);
6438 prev
= lang_output_section_find (previous_section
->name
);
6440 /* More paranoia. */
6441 if (cur
== NULL
|| prev
== NULL
)
6444 /* If the regions are different then force the sections to live in
6445 different segments. See the email thread starting at the following
6446 URL for the reasons why this is necessary:
6447 http://sourceware.org/ml/binutils/2007-02/msg00216.html */
6448 return cur
->region
!= prev
->region
;
6452 one_lang_size_sections_pass (bool *relax
, bool check_regions
)
6454 lang_statement_iteration
++;
6455 if (expld
.phase
!= lang_mark_phase_enum
)
6456 lang_sizing_iteration
++;
6457 lang_size_sections_1 (&statement_list
.head
, abs_output_section
,
6458 0, 0, relax
, check_regions
);
6462 lang_size_segment (void)
6464 /* If XXX_SEGMENT_ALIGN XXX_SEGMENT_END pair was seen, check whether
6465 a page could be saved in the data segment. */
6466 seg_align_type
*seg
= &expld
.dataseg
;
6467 bfd_vma first
, last
;
6469 first
= -seg
->base
& (seg
->commonpagesize
- 1);
6470 last
= seg
->end
& (seg
->commonpagesize
- 1);
6472 && ((seg
->base
& ~(seg
->commonpagesize
- 1))
6473 != (seg
->end
& ~(seg
->commonpagesize
- 1)))
6474 && first
+ last
<= seg
->commonpagesize
)
6476 seg
->phase
= exp_seg_adjust
;
6480 seg
->phase
= exp_seg_done
;
6485 lang_size_relro_segment_1 (void)
6487 seg_align_type
*seg
= &expld
.dataseg
;
6488 bfd_vma relro_end
, desired_end
;
6491 /* Compute the expected PT_GNU_RELRO/PT_LOAD segment end. */
6492 relro_end
= (seg
->relro_end
+ seg
->relropagesize
- 1) & -seg
->relropagesize
;
6494 /* Adjust by the offset arg of XXX_SEGMENT_RELRO_END. */
6495 desired_end
= relro_end
- seg
->relro_offset
;
6497 /* For sections in the relro segment.. */
6498 for (sec
= link_info
.output_bfd
->section_last
; sec
; sec
= sec
->prev
)
6499 if ((sec
->flags
& SEC_ALLOC
) != 0
6500 && sec
->vma
>= seg
->base
6501 && sec
->vma
< seg
->relro_end
- seg
->relro_offset
)
6503 /* Where do we want to put this section so that it ends as
6505 bfd_vma start
, end
, bump
;
6507 end
= start
= sec
->vma
;
6509 end
+= TO_ADDR (sec
->size
);
6510 bump
= desired_end
- end
;
6511 /* We'd like to increase START by BUMP, but we must heed
6512 alignment so the increase might be less than optimum. */
6514 start
&= ~(((bfd_vma
) 1 << sec
->alignment_power
) - 1);
6515 /* This is now the desired end for the previous section. */
6516 desired_end
= start
;
6519 seg
->phase
= exp_seg_relro_adjust
;
6520 ASSERT (desired_end
>= seg
->base
);
6521 seg
->base
= desired_end
;
6526 lang_size_relro_segment (bool *relax
, bool check_regions
)
6528 bool do_reset
= false;
6530 if (link_info
.relro
&& expld
.dataseg
.relro_end
)
6532 bfd_vma data_initial_base
= expld
.dataseg
.base
;
6533 bfd_vma data_relro_end
= lang_size_relro_segment_1 ();
6535 lang_reset_memory_regions ();
6536 one_lang_size_sections_pass (relax
, check_regions
);
6538 /* Assignments to dot, or to output section address in a user
6539 script have increased padding over the original. Revert. */
6540 if (expld
.dataseg
.relro_end
> data_relro_end
)
6542 expld
.dataseg
.base
= data_initial_base
;
6546 else if (lang_size_segment ())
6553 lang_size_sections (bool *relax
, bool check_regions
)
6555 expld
.phase
= lang_allocating_phase_enum
;
6556 expld
.dataseg
.phase
= exp_seg_none
;
6558 one_lang_size_sections_pass (relax
, check_regions
);
6560 if (expld
.dataseg
.phase
!= exp_seg_end_seen
)
6561 expld
.dataseg
.phase
= exp_seg_done
;
6563 if (expld
.dataseg
.phase
== exp_seg_end_seen
)
6566 = lang_size_relro_segment (relax
, check_regions
);
6570 lang_reset_memory_regions ();
6571 one_lang_size_sections_pass (relax
, check_regions
);
6574 if (link_info
.relro
&& expld
.dataseg
.relro_end
)
6576 link_info
.relro_start
= expld
.dataseg
.base
;
6577 link_info
.relro_end
= expld
.dataseg
.relro_end
;
6582 static lang_output_section_statement_type
*current_section
;
6583 static lang_assignment_statement_type
*current_assign
;
6584 static bool prefer_next_section
;
6586 /* Worker function for lang_do_assignments. Recursiveness goes here. */
6589 lang_do_assignments_1 (lang_statement_union_type
*s
,
6590 lang_output_section_statement_type
*current_os
,
6595 lang_output_section_statement_type
*os
= current_os
;
6597 for (; s
!= NULL
; s
= s
->header
.next
)
6599 switch (s
->header
.type
)
6601 case lang_constructors_statement_enum
:
6602 dot
= lang_do_assignments_1 (constructor_list
.head
,
6603 current_os
, fill
, dot
, found_end
);
6606 case lang_output_section_statement_enum
:
6610 os
= &s
->output_section_statement
;
6611 os
->after_end
= *found_end
;
6612 init_opb (os
->bfd_section
);
6614 if (os
->bfd_section
!= NULL
)
6616 if (!os
->ignored
&& (os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
6618 current_section
= os
;
6619 prefer_next_section
= false;
6621 newdot
= os
->bfd_section
->vma
;
6623 newdot
= lang_do_assignments_1 (os
->children
.head
,
6624 os
, os
->fill
, newdot
, found_end
);
6627 if (os
->bfd_section
!= NULL
)
6629 newdot
= os
->bfd_section
->vma
;
6631 /* .tbss sections effectively have zero size. */
6632 if (!IS_TBSS (os
->bfd_section
)
6633 || bfd_link_relocatable (&link_info
))
6634 newdot
+= TO_ADDR (os
->bfd_section
->size
);
6636 if (os
->update_dot_tree
!= NULL
)
6637 exp_fold_tree (os
->update_dot_tree
, os
,
6638 bfd_abs_section_ptr
, &newdot
);
6645 case lang_wild_statement_enum
:
6647 dot
= lang_do_assignments_1 (s
->wild_statement
.children
.head
,
6648 current_os
, fill
, dot
, found_end
);
6651 case lang_object_symbols_statement_enum
:
6652 case lang_output_statement_enum
:
6653 case lang_target_statement_enum
:
6656 case lang_data_statement_enum
:
6657 exp_fold_tree (s
->data_statement
.exp
, os
, bfd_abs_section_ptr
, &dot
);
6658 if (expld
.result
.valid_p
)
6660 s
->data_statement
.value
= expld
.result
.value
;
6661 if (expld
.result
.section
!= NULL
)
6662 s
->data_statement
.value
+= expld
.result
.section
->vma
;
6664 else if (expld
.phase
== lang_final_phase_enum
)
6665 einfo (_("%F%P: invalid data statement\n"));
6668 switch (s
->data_statement
.type
)
6686 if (size
< TO_SIZE ((unsigned) 1))
6687 size
= TO_SIZE ((unsigned) 1);
6688 dot
+= TO_ADDR (size
);
6692 case lang_reloc_statement_enum
:
6693 exp_fold_tree (s
->reloc_statement
.addend_exp
, os
,
6694 bfd_abs_section_ptr
, &dot
);
6695 if (expld
.result
.valid_p
)
6696 s
->reloc_statement
.addend_value
= expld
.result
.value
;
6697 else if (expld
.phase
== lang_final_phase_enum
)
6698 einfo (_("%F%P: invalid reloc statement\n"));
6699 dot
+= TO_ADDR (bfd_get_reloc_size (s
->reloc_statement
.howto
));
6702 case lang_input_section_enum
:
6704 asection
*in
= s
->input_section
.section
;
6706 if ((in
->flags
& SEC_EXCLUDE
) == 0)
6707 dot
+= TO_ADDR (in
->size
);
6711 case lang_input_statement_enum
:
6714 case lang_fill_statement_enum
:
6715 fill
= s
->fill_statement
.fill
;
6718 case lang_assignment_statement_enum
:
6719 current_assign
= &s
->assignment_statement
;
6720 if (current_assign
->exp
->type
.node_class
!= etree_assert
)
6722 const char *p
= current_assign
->exp
->assign
.dst
;
6724 if (current_os
== abs_output_section
&& p
[0] == '.' && p
[1] == 0)
6725 prefer_next_section
= true;
6729 if (strcmp (p
, "end") == 0)
6732 exp_fold_tree (s
->assignment_statement
.exp
, os
,
6733 (current_os
->bfd_section
!= NULL
6734 ? current_os
->bfd_section
: bfd_und_section_ptr
),
6738 case lang_padding_statement_enum
:
6739 dot
+= TO_ADDR (s
->padding_statement
.size
);
6742 case lang_group_statement_enum
:
6743 dot
= lang_do_assignments_1 (s
->group_statement
.children
.head
,
6744 current_os
, fill
, dot
, found_end
);
6747 case lang_insert_statement_enum
:
6750 case lang_address_statement_enum
:
6762 lang_do_assignments (lang_phase_type phase
)
6764 bool found_end
= false;
6766 current_section
= NULL
;
6767 prefer_next_section
= false;
6768 expld
.phase
= phase
;
6769 lang_statement_iteration
++;
6770 lang_do_assignments_1 (statement_list
.head
,
6771 abs_output_section
, NULL
, 0, &found_end
);
6774 /* For an assignment statement outside of an output section statement,
6775 choose the best of neighbouring output sections to use for values
6779 section_for_dot (void)
6783 /* Assignments belong to the previous output section, unless there
6784 has been an assignment to "dot", in which case following
6785 assignments belong to the next output section. (The assumption
6786 is that an assignment to "dot" is setting up the address for the
6787 next output section.) Except that past the assignment to "_end"
6788 we always associate with the previous section. This exception is
6789 for targets like SH that define an alloc .stack or other
6790 weirdness after non-alloc sections. */
6791 if (current_section
== NULL
|| prefer_next_section
)
6793 lang_statement_union_type
*stmt
;
6794 lang_output_section_statement_type
*os
;
6796 for (stmt
= (lang_statement_union_type
*) current_assign
;
6798 stmt
= stmt
->header
.next
)
6799 if (stmt
->header
.type
== lang_output_section_statement_enum
)
6802 os
= stmt
? &stmt
->output_section_statement
: NULL
;
6805 && (os
->bfd_section
== NULL
6806 || (os
->bfd_section
->flags
& SEC_EXCLUDE
) != 0
6807 || bfd_section_removed_from_list (link_info
.output_bfd
,
6811 if (current_section
== NULL
|| os
== NULL
|| !os
->after_end
)
6814 s
= os
->bfd_section
;
6816 s
= link_info
.output_bfd
->section_last
;
6818 && ((s
->flags
& SEC_ALLOC
) == 0
6819 || (s
->flags
& SEC_THREAD_LOCAL
) != 0))
6824 return bfd_abs_section_ptr
;
6828 s
= current_section
->bfd_section
;
6830 /* The section may have been stripped. */
6832 && ((s
->flags
& SEC_EXCLUDE
) != 0
6833 || (s
->flags
& SEC_ALLOC
) == 0
6834 || (s
->flags
& SEC_THREAD_LOCAL
) != 0
6835 || bfd_section_removed_from_list (link_info
.output_bfd
, s
)))
6838 s
= link_info
.output_bfd
->sections
;
6840 && ((s
->flags
& SEC_ALLOC
) == 0
6841 || (s
->flags
& SEC_THREAD_LOCAL
) != 0))
6846 return bfd_abs_section_ptr
;
6849 /* Array of __start/__stop/.startof./.sizeof/ symbols. */
6851 static struct bfd_link_hash_entry
**start_stop_syms
;
6852 static size_t start_stop_count
= 0;
6853 static size_t start_stop_alloc
= 0;
6855 /* Give start/stop SYMBOL for SEC a preliminary definition, and add it
6856 to start_stop_syms. */
6859 lang_define_start_stop (const char *symbol
, asection
*sec
)
6861 struct bfd_link_hash_entry
*h
;
6863 h
= bfd_define_start_stop (link_info
.output_bfd
, &link_info
, symbol
, sec
);
6866 if (start_stop_count
== start_stop_alloc
)
6868 start_stop_alloc
= 2 * start_stop_alloc
+ 10;
6870 = xrealloc (start_stop_syms
,
6871 start_stop_alloc
* sizeof (*start_stop_syms
));
6873 start_stop_syms
[start_stop_count
++] = h
;
6877 /* Check for input sections whose names match references to
6878 __start_SECNAME or __stop_SECNAME symbols. Give the symbols
6879 preliminary definitions. */
6882 lang_init_start_stop (void)
6886 char leading_char
= bfd_get_symbol_leading_char (link_info
.output_bfd
);
6888 for (abfd
= link_info
.input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
6889 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
6892 const char *secname
= s
->name
;
6894 for (ps
= secname
; *ps
!= '\0'; ps
++)
6895 if (!ISALNUM ((unsigned char) *ps
) && *ps
!= '_')
6899 char *symbol
= (char *) xmalloc (10 + strlen (secname
));
6901 symbol
[0] = leading_char
;
6902 sprintf (symbol
+ (leading_char
!= 0), "__start_%s", secname
);
6903 lang_define_start_stop (symbol
, s
);
6905 symbol
[1] = leading_char
;
6906 memcpy (symbol
+ 1 + (leading_char
!= 0), "__stop", 6);
6907 lang_define_start_stop (symbol
+ 1, s
);
6914 /* Iterate over start_stop_syms. */
6917 foreach_start_stop (void (*func
) (struct bfd_link_hash_entry
*))
6921 for (i
= 0; i
< start_stop_count
; ++i
)
6922 func (start_stop_syms
[i
]);
6925 /* __start and __stop symbols are only supposed to be defined by the
6926 linker for orphan sections, but we now extend that to sections that
6927 map to an output section of the same name. The symbols were
6928 defined early for --gc-sections, before we mapped input to output
6929 sections, so undo those that don't satisfy this rule. */
6932 undef_start_stop (struct bfd_link_hash_entry
*h
)
6934 if (h
->ldscript_def
)
6937 if (h
->u
.def
.section
->output_section
== NULL
6938 || h
->u
.def
.section
->output_section
->owner
!= link_info
.output_bfd
6939 || strcmp (h
->u
.def
.section
->name
,
6940 h
->u
.def
.section
->output_section
->name
) != 0)
6942 asection
*sec
= bfd_get_section_by_name (link_info
.output_bfd
,
6943 h
->u
.def
.section
->name
);
6946 /* When there are more than one input sections with the same
6947 section name, SECNAME, linker picks the first one to define
6948 __start_SECNAME and __stop_SECNAME symbols. When the first
6949 input section is removed by comdat group, we need to check
6950 if there is still an output section with section name
6953 for (i
= sec
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
6954 if (strcmp (h
->u
.def
.section
->name
, i
->name
) == 0)
6956 h
->u
.def
.section
= i
;
6960 h
->type
= bfd_link_hash_undefined
;
6961 h
->u
.undef
.abfd
= NULL
;
6962 if (is_elf_hash_table (link_info
.hash
))
6964 const struct elf_backend_data
*bed
;
6965 struct elf_link_hash_entry
*eh
= (struct elf_link_hash_entry
*) h
;
6966 unsigned int was_forced
= eh
->forced_local
;
6968 bed
= get_elf_backend_data (link_info
.output_bfd
);
6969 (*bed
->elf_backend_hide_symbol
) (&link_info
, eh
, true);
6970 if (!eh
->ref_regular_nonweak
)
6971 h
->type
= bfd_link_hash_undefweak
;
6972 eh
->def_regular
= 0;
6973 eh
->forced_local
= was_forced
;
6979 lang_undef_start_stop (void)
6981 foreach_start_stop (undef_start_stop
);
6984 /* Check for output sections whose names match references to
6985 .startof.SECNAME or .sizeof.SECNAME symbols. Give the symbols
6986 preliminary definitions. */
6989 lang_init_startof_sizeof (void)
6993 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6995 const char *secname
= s
->name
;
6996 char *symbol
= (char *) xmalloc (10 + strlen (secname
));
6998 sprintf (symbol
, ".startof.%s", secname
);
6999 lang_define_start_stop (symbol
, s
);
7001 memcpy (symbol
+ 1, ".size", 5);
7002 lang_define_start_stop (symbol
+ 1, s
);
7007 /* Set .startof., .sizeof., __start and __stop symbols final values. */
7010 set_start_stop (struct bfd_link_hash_entry
*h
)
7013 || h
->type
!= bfd_link_hash_defined
)
7016 if (h
->root
.string
[0] == '.')
7018 /* .startof. or .sizeof. symbol.
7019 .startof. already has final value. */
7020 if (h
->root
.string
[2] == 'i')
7023 h
->u
.def
.value
= TO_ADDR (h
->u
.def
.section
->size
);
7024 h
->u
.def
.section
= bfd_abs_section_ptr
;
7029 /* __start or __stop symbol. */
7030 int has_lead
= bfd_get_symbol_leading_char (link_info
.output_bfd
) != 0;
7032 h
->u
.def
.section
= h
->u
.def
.section
->output_section
;
7033 if (h
->root
.string
[4 + has_lead
] == 'o')
7036 h
->u
.def
.value
= TO_ADDR (h
->u
.def
.section
->size
);
7042 lang_finalize_start_stop (void)
7044 foreach_start_stop (set_start_stop
);
7048 lang_symbol_tweaks (void)
7050 /* Give initial values for __start and __stop symbols, so that ELF
7051 gc_sections will keep sections referenced by these symbols. Must
7052 be done before lang_do_assignments. */
7053 if (config
.build_constructors
)
7054 lang_init_start_stop ();
7056 /* Make __ehdr_start hidden, and set def_regular even though it is
7057 likely undefined at this stage. For lang_check_relocs. */
7058 if (is_elf_hash_table (link_info
.hash
)
7059 && !bfd_link_relocatable (&link_info
))
7061 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*)
7062 bfd_link_hash_lookup (link_info
.hash
, "__ehdr_start",
7063 false, false, true);
7065 /* Only adjust the export class if the symbol was referenced
7066 and not defined, otherwise leave it alone. */
7068 && (h
->root
.type
== bfd_link_hash_new
7069 || h
->root
.type
== bfd_link_hash_undefined
7070 || h
->root
.type
== bfd_link_hash_undefweak
7071 || h
->root
.type
== bfd_link_hash_common
))
7073 const struct elf_backend_data
*bed
;
7074 bed
= get_elf_backend_data (link_info
.output_bfd
);
7075 (*bed
->elf_backend_hide_symbol
) (&link_info
, h
, true);
7076 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
7077 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
7079 h
->root
.linker_def
= 1;
7080 h
->root
.rel_from_abs
= 1;
7088 struct bfd_link_hash_entry
*h
;
7091 if ((bfd_link_relocatable (&link_info
) && !link_info
.gc_sections
)
7092 || bfd_link_dll (&link_info
))
7093 warn
= entry_from_cmdline
;
7097 /* Force the user to specify a root when generating a relocatable with
7098 --gc-sections, unless --gc-keep-exported was also given. */
7099 if (bfd_link_relocatable (&link_info
)
7100 && link_info
.gc_sections
7101 && !link_info
.gc_keep_exported
)
7103 struct bfd_sym_chain
*sym
;
7105 for (sym
= link_info
.gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
7107 h
= bfd_link_hash_lookup (link_info
.hash
, sym
->name
,
7108 false, false, false);
7110 && (h
->type
== bfd_link_hash_defined
7111 || h
->type
== bfd_link_hash_defweak
)
7112 && !bfd_is_const_section (h
->u
.def
.section
))
7116 einfo (_("%F%P: --gc-sections requires a defined symbol root "
7117 "specified by -e or -u\n"));
7120 if (entry_symbol
.name
== NULL
)
7122 /* No entry has been specified. Look for the default entry, but
7123 don't warn if we don't find it. */
7124 entry_symbol
.name
= entry_symbol_default
;
7128 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
.name
,
7129 false, false, true);
7131 && (h
->type
== bfd_link_hash_defined
7132 || h
->type
== bfd_link_hash_defweak
)
7133 && h
->u
.def
.section
->output_section
!= NULL
)
7137 val
= (h
->u
.def
.value
7138 + bfd_section_vma (h
->u
.def
.section
->output_section
)
7139 + h
->u
.def
.section
->output_offset
);
7140 if (!bfd_set_start_address (link_info
.output_bfd
, val
))
7141 einfo (_("%F%P: %s: can't set start address\n"), entry_symbol
.name
);
7148 /* We couldn't find the entry symbol. Try parsing it as a
7150 val
= bfd_scan_vma (entry_symbol
.name
, &send
, 0);
7153 if (!bfd_set_start_address (link_info
.output_bfd
, val
))
7154 einfo (_("%F%P: can't set start address\n"));
7156 /* BZ 2004952: Only use the start of the entry section for executables. */
7157 else if bfd_link_executable (&link_info
)
7161 /* Can't find the entry symbol, and it's not a number. Use
7162 the first address in the text section. */
7163 ts
= bfd_get_section_by_name (link_info
.output_bfd
, entry_section
);
7167 einfo (_("%P: warning: cannot find entry symbol %s;"
7168 " defaulting to %V\n"),
7170 bfd_section_vma (ts
));
7171 if (!bfd_set_start_address (link_info
.output_bfd
,
7172 bfd_section_vma (ts
)))
7173 einfo (_("%F%P: can't set start address\n"));
7178 einfo (_("%P: warning: cannot find entry symbol %s;"
7179 " not setting start address\n"),
7186 einfo (_("%P: warning: cannot find entry symbol %s;"
7187 " not setting start address\n"),
7193 /* This is a small function used when we want to ignore errors from
7197 ignore_bfd_errors (const char *fmt ATTRIBUTE_UNUSED
,
7198 va_list ap ATTRIBUTE_UNUSED
)
7200 /* Don't do anything. */
7203 /* Check that the architecture of all the input files is compatible
7204 with the output file. Also call the backend to let it do any
7205 other checking that is needed. */
7210 lang_input_statement_type
*file
;
7212 const bfd_arch_info_type
*compatible
;
7214 for (file
= (void *) file_chain
.head
;
7218 #if BFD_SUPPORTS_PLUGINS
7219 /* Don't check format of files claimed by plugin. */
7220 if (file
->flags
.claimed
)
7222 #endif /* BFD_SUPPORTS_PLUGINS */
7223 input_bfd
= file
->the_bfd
;
7225 = bfd_arch_get_compatible (input_bfd
, link_info
.output_bfd
,
7226 command_line
.accept_unknown_input_arch
);
7228 /* In general it is not possible to perform a relocatable
7229 link between differing object formats when the input
7230 file has relocations, because the relocations in the
7231 input format may not have equivalent representations in
7232 the output format (and besides BFD does not translate
7233 relocs for other link purposes than a final link). */
7234 if (!file
->flags
.just_syms
7235 && (bfd_link_relocatable (&link_info
)
7236 || link_info
.emitrelocations
)
7237 && (compatible
== NULL
7238 || (bfd_get_flavour (input_bfd
)
7239 != bfd_get_flavour (link_info
.output_bfd
)))
7240 && (bfd_get_file_flags (input_bfd
) & HAS_RELOC
) != 0)
7242 einfo (_("%F%P: relocatable linking with relocations from"
7243 " format %s (%pB) to format %s (%pB) is not supported\n"),
7244 bfd_get_target (input_bfd
), input_bfd
,
7245 bfd_get_target (link_info
.output_bfd
), link_info
.output_bfd
);
7246 /* einfo with %F exits. */
7249 if (compatible
== NULL
)
7251 if (command_line
.warn_mismatch
)
7252 einfo (_("%X%P: %s architecture of input file `%pB'"
7253 " is incompatible with %s output\n"),
7254 bfd_printable_name (input_bfd
), input_bfd
,
7255 bfd_printable_name (link_info
.output_bfd
));
7258 /* If the input bfd has no contents, it shouldn't set the
7259 private data of the output bfd. */
7260 else if (!file
->flags
.just_syms
7261 && ((input_bfd
->flags
& DYNAMIC
) != 0
7262 || bfd_count_sections (input_bfd
) != 0))
7264 bfd_error_handler_type pfn
= NULL
;
7266 /* If we aren't supposed to warn about mismatched input
7267 files, temporarily set the BFD error handler to a
7268 function which will do nothing. We still want to call
7269 bfd_merge_private_bfd_data, since it may set up
7270 information which is needed in the output file. */
7271 if (!command_line
.warn_mismatch
)
7272 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
7273 if (!bfd_merge_private_bfd_data (input_bfd
, &link_info
))
7275 if (command_line
.warn_mismatch
)
7276 einfo (_("%X%P: failed to merge target specific data"
7277 " of file %pB\n"), input_bfd
);
7279 if (!command_line
.warn_mismatch
)
7280 bfd_set_error_handler (pfn
);
7285 /* Look through all the global common symbols and attach them to the
7286 correct section. The -sort-common command line switch may be used
7287 to roughly sort the entries by alignment. */
7292 if (link_info
.inhibit_common_definition
)
7294 if (bfd_link_relocatable (&link_info
)
7295 && !command_line
.force_common_definition
)
7298 if (!config
.sort_common
)
7299 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, NULL
);
7304 if (config
.sort_common
== sort_descending
)
7306 for (power
= 4; power
> 0; power
--)
7307 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7310 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7314 for (power
= 0; power
<= 4; power
++)
7315 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7317 power
= (unsigned int) -1;
7318 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7323 /* Place one common symbol in the correct section. */
7326 lang_one_common (struct bfd_link_hash_entry
*h
, void *info
)
7328 unsigned int power_of_two
;
7332 if (h
->type
!= bfd_link_hash_common
)
7336 power_of_two
= h
->u
.c
.p
->alignment_power
;
7338 if (config
.sort_common
== sort_descending
7339 && power_of_two
< *(unsigned int *) info
)
7341 else if (config
.sort_common
== sort_ascending
7342 && power_of_two
> *(unsigned int *) info
)
7345 section
= h
->u
.c
.p
->section
;
7346 if (!bfd_define_common_symbol (link_info
.output_bfd
, &link_info
, h
))
7347 einfo (_("%F%P: could not define common symbol `%pT': %E\n"),
7350 if (config
.map_file
!= NULL
)
7352 static bool header_printed
;
7357 if (!header_printed
)
7359 minfo (_("\nAllocating common symbols\n"));
7360 minfo (_("Common symbol size file\n\n"));
7361 header_printed
= true;
7364 name
= bfd_demangle (link_info
.output_bfd
, h
->root
.string
,
7365 DMGL_ANSI
| DMGL_PARAMS
);
7368 minfo ("%s", h
->root
.string
);
7369 len
= strlen (h
->root
.string
);
7374 len
= strlen (name
);
7384 sprintf (buf
, "%" PRIx64
, (uint64_t) size
);
7385 fprintf (config
.map_file
, "%*s0x%-16s", 20 - len
, "", buf
);
7387 minfo ("%pB\n", section
->owner
);
7393 /* Handle a single orphan section S, placing the orphan into an appropriate
7394 output section. The effects of the --orphan-handling command line
7395 option are handled here. */
7398 ldlang_place_orphan (asection
*s
)
7400 if (config
.orphan_handling
== orphan_handling_discard
)
7402 lang_output_section_statement_type
*os
;
7403 os
= lang_output_section_statement_lookup (DISCARD_SECTION_NAME
, 0, 1);
7404 if (os
->addr_tree
== NULL
7405 && (bfd_link_relocatable (&link_info
)
7406 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0))
7407 os
->addr_tree
= exp_intop (0);
7408 lang_add_section (&os
->children
, s
, NULL
, NULL
, os
);
7412 lang_output_section_statement_type
*os
;
7413 const char *name
= s
->name
;
7416 if (config
.orphan_handling
== orphan_handling_error
)
7417 einfo (_("%X%P: error: unplaced orphan section `%pA' from `%pB'\n"),
7420 if (config
.unique_orphan_sections
|| unique_section_p (s
, NULL
))
7421 constraint
= SPECIAL
;
7423 os
= ldemul_place_orphan (s
, name
, constraint
);
7426 os
= lang_output_section_statement_lookup (name
, constraint
, 1);
7427 if (os
->addr_tree
== NULL
7428 && (bfd_link_relocatable (&link_info
)
7429 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0))
7430 os
->addr_tree
= exp_intop (0);
7431 lang_add_section (&os
->children
, s
, NULL
, NULL
, os
);
7434 if (config
.orphan_handling
== orphan_handling_warn
)
7435 einfo (_("%P: warning: orphan section `%pA' from `%pB' being "
7436 "placed in section `%s'\n"),
7437 s
, s
->owner
, os
->name
);
7441 /* Run through the input files and ensure that every input section has
7442 somewhere to go. If one is found without a destination then create
7443 an input request and place it into the statement tree. */
7446 lang_place_orphans (void)
7448 LANG_FOR_EACH_INPUT_STATEMENT (file
)
7452 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7454 if (s
->output_section
== NULL
)
7456 /* This section of the file is not attached, root
7457 around for a sensible place for it to go. */
7459 if (file
->flags
.just_syms
)
7460 bfd_link_just_syms (file
->the_bfd
, s
, &link_info
);
7461 else if (lang_discard_section_p (s
))
7462 s
->output_section
= bfd_abs_section_ptr
;
7463 else if (strcmp (s
->name
, "COMMON") == 0)
7465 /* This is a lonely common section which must have
7466 come from an archive. We attach to the section
7467 with the wildcard. */
7468 if (!bfd_link_relocatable (&link_info
)
7469 || command_line
.force_common_definition
)
7471 if (default_common_section
== NULL
)
7472 default_common_section
7473 = lang_output_section_statement_lookup (".bss", 0, 1);
7474 lang_add_section (&default_common_section
->children
, s
,
7475 NULL
, NULL
, default_common_section
);
7479 ldlang_place_orphan (s
);
7486 lang_set_flags (lang_memory_region_type
*ptr
, const char *flags
, int invert
)
7488 flagword
*ptr_flags
;
7490 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
7496 /* PR 17900: An exclamation mark in the attributes reverses
7497 the sense of any of the attributes that follow. */
7500 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
7504 *ptr_flags
|= SEC_ALLOC
;
7508 *ptr_flags
|= SEC_READONLY
;
7512 *ptr_flags
|= SEC_DATA
;
7516 *ptr_flags
|= SEC_CODE
;
7521 *ptr_flags
|= SEC_LOAD
;
7525 einfo (_("%F%P: invalid character %c (%d) in flags\n"),
7533 /* Call a function on each real input file. This function will be
7534 called on an archive, but not on the elements. */
7537 lang_for_each_input_file (void (*func
) (lang_input_statement_type
*))
7539 lang_input_statement_type
*f
;
7541 for (f
= (void *) input_file_chain
.head
;
7543 f
= f
->next_real_file
)
7548 /* Call a function on each real file. The function will be called on
7549 all the elements of an archive which are included in the link, but
7550 will not be called on the archive file itself. */
7553 lang_for_each_file (void (*func
) (lang_input_statement_type
*))
7555 LANG_FOR_EACH_INPUT_STATEMENT (f
)
7563 ldlang_add_file (lang_input_statement_type
*entry
)
7565 lang_statement_append (&file_chain
, entry
, &entry
->next
);
7567 /* The BFD linker needs to have a list of all input BFDs involved in
7569 ASSERT (link_info
.input_bfds_tail
!= &entry
->the_bfd
->link
.next
7570 && entry
->the_bfd
->link
.next
== NULL
);
7571 ASSERT (entry
->the_bfd
!= link_info
.output_bfd
);
7573 *link_info
.input_bfds_tail
= entry
->the_bfd
;
7574 link_info
.input_bfds_tail
= &entry
->the_bfd
->link
.next
;
7575 bfd_set_usrdata (entry
->the_bfd
, entry
);
7576 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
7578 /* Look through the sections and check for any which should not be
7579 included in the link. We need to do this now, so that we can
7580 notice when the backend linker tries to report multiple
7581 definition errors for symbols which are in sections we aren't
7582 going to link. FIXME: It might be better to entirely ignore
7583 symbols which are defined in sections which are going to be
7584 discarded. This would require modifying the backend linker for
7585 each backend which might set the SEC_LINK_ONCE flag. If we do
7586 this, we should probably handle SEC_EXCLUDE in the same way. */
7588 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, entry
);
7592 lang_add_output (const char *name
, int from_script
)
7594 /* Make -o on command line override OUTPUT in script. */
7595 if (!had_output_filename
|| !from_script
)
7597 output_filename
= name
;
7598 had_output_filename
= true;
7602 lang_output_section_statement_type
*
7603 lang_enter_output_section_statement (const char *output_section_statement_name
,
7604 etree_type
*address_exp
,
7605 enum section_type sectype
,
7606 etree_type
*sectype_value
,
7608 etree_type
*subalign
,
7611 int align_with_input
)
7613 lang_output_section_statement_type
*os
;
7615 os
= lang_output_section_statement_lookup (output_section_statement_name
,
7617 current_section
= os
;
7619 if (os
->addr_tree
== NULL
)
7621 os
->addr_tree
= address_exp
;
7623 os
->sectype
= sectype
;
7624 if (sectype
== type_section
|| sectype
== typed_readonly_section
)
7625 os
->sectype_value
= sectype_value
;
7626 else if (sectype
== noload_section
)
7627 os
->flags
= SEC_NEVER_LOAD
;
7629 os
->flags
= SEC_NO_FLAGS
;
7630 os
->block_value
= 1;
7632 /* Make next things chain into subchain of this. */
7633 push_stat_ptr (&os
->children
);
7635 os
->align_lma_with_input
= align_with_input
== ALIGN_WITH_INPUT
;
7636 if (os
->align_lma_with_input
&& align
!= NULL
)
7637 einfo (_("%F%P:%pS: error: align with input and explicit align specified\n"),
7640 os
->subsection_alignment
= subalign
;
7641 os
->section_alignment
= align
;
7643 os
->load_base
= ebase
;
7650 lang_output_statement_type
*new_stmt
;
7652 new_stmt
= new_stat (lang_output_statement
, stat_ptr
);
7653 new_stmt
->name
= output_filename
;
7656 /* Reset the current counters in the regions. */
7659 lang_reset_memory_regions (void)
7661 lang_memory_region_type
*p
= lang_memory_region_list
;
7663 lang_output_section_statement_type
*os
;
7665 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
7667 p
->current
= p
->origin
;
7671 for (os
= (void *) lang_os_list
.head
;
7675 os
->processed_vma
= false;
7676 os
->processed_lma
= false;
7679 for (o
= link_info
.output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
7681 /* Save the last size for possible use by bfd_relax_section. */
7682 o
->rawsize
= o
->size
;
7683 if (!(o
->flags
& SEC_FIXED_SIZE
))
7688 /* Worker for lang_gc_sections_1. */
7691 gc_section_callback (lang_wild_statement_type
*ptr
,
7692 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
7694 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
7695 void *data ATTRIBUTE_UNUSED
)
7697 /* If the wild pattern was marked KEEP, the member sections
7698 should be as well. */
7699 if (ptr
->keep_sections
)
7700 section
->flags
|= SEC_KEEP
;
7703 /* Iterate over sections marking them against GC. */
7706 lang_gc_sections_1 (lang_statement_union_type
*s
)
7708 for (; s
!= NULL
; s
= s
->header
.next
)
7710 switch (s
->header
.type
)
7712 case lang_wild_statement_enum
:
7713 walk_wild (&s
->wild_statement
, gc_section_callback
, NULL
);
7715 case lang_constructors_statement_enum
:
7716 lang_gc_sections_1 (constructor_list
.head
);
7718 case lang_output_section_statement_enum
:
7719 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
7721 case lang_group_statement_enum
:
7722 lang_gc_sections_1 (s
->group_statement
.children
.head
);
7731 lang_gc_sections (void)
7733 /* Keep all sections so marked in the link script. */
7734 lang_gc_sections_1 (statement_list
.head
);
7736 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
7737 the special case of .stabstr debug info. (See bfd/stabs.c)
7738 Twiddle the flag here, to simplify later linker code. */
7739 if (bfd_link_relocatable (&link_info
))
7741 LANG_FOR_EACH_INPUT_STATEMENT (f
)
7744 #if BFD_SUPPORTS_PLUGINS
7745 if (f
->flags
.claimed
)
7748 for (sec
= f
->the_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7749 if ((sec
->flags
& SEC_DEBUGGING
) == 0
7750 || strcmp (sec
->name
, ".stabstr") != 0)
7751 sec
->flags
&= ~SEC_EXCLUDE
;
7755 if (link_info
.gc_sections
)
7756 bfd_gc_sections (link_info
.output_bfd
, &link_info
);
7759 /* Worker for lang_find_relro_sections_1. */
7762 find_relro_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
7763 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
7765 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
7768 /* Discarded, excluded and ignored sections effectively have zero
7770 if (section
->output_section
!= NULL
7771 && section
->output_section
->owner
== link_info
.output_bfd
7772 && (section
->output_section
->flags
& SEC_EXCLUDE
) == 0
7773 && !IGNORE_SECTION (section
)
7774 && section
->size
!= 0)
7776 bool *has_relro_section
= (bool *) data
;
7777 *has_relro_section
= true;
7781 /* Iterate over sections for relro sections. */
7784 lang_find_relro_sections_1 (lang_statement_union_type
*s
,
7785 bool *has_relro_section
)
7787 if (*has_relro_section
)
7790 for (; s
!= NULL
; s
= s
->header
.next
)
7792 if (s
== expld
.dataseg
.relro_end_stat
)
7795 switch (s
->header
.type
)
7797 case lang_wild_statement_enum
:
7798 walk_wild (&s
->wild_statement
,
7799 find_relro_section_callback
,
7802 case lang_constructors_statement_enum
:
7803 lang_find_relro_sections_1 (constructor_list
.head
,
7806 case lang_output_section_statement_enum
:
7807 lang_find_relro_sections_1 (s
->output_section_statement
.children
.head
,
7810 case lang_group_statement_enum
:
7811 lang_find_relro_sections_1 (s
->group_statement
.children
.head
,
7821 lang_find_relro_sections (void)
7823 bool has_relro_section
= false;
7825 /* Check all sections in the link script. */
7827 lang_find_relro_sections_1 (expld
.dataseg
.relro_start_stat
,
7828 &has_relro_section
);
7830 if (!has_relro_section
)
7831 link_info
.relro
= false;
7834 /* Relax all sections until bfd_relax_section gives up. */
7837 lang_relax_sections (bool need_layout
)
7839 /* NB: Also enable relaxation to layout sections for DT_RELR. */
7840 if (RELAXATION_ENABLED
|| link_info
.enable_dt_relr
)
7842 /* We may need more than one relaxation pass. */
7843 int i
= link_info
.relax_pass
;
7845 /* The backend can use it to determine the current pass. */
7846 link_info
.relax_pass
= 0;
7850 /* Keep relaxing until bfd_relax_section gives up. */
7853 link_info
.relax_trip
= -1;
7856 link_info
.relax_trip
++;
7858 /* Note: pe-dll.c does something like this also. If you find
7859 you need to change this code, you probably need to change
7860 pe-dll.c also. DJ */
7862 /* Do all the assignments with our current guesses as to
7864 lang_do_assignments (lang_assigning_phase_enum
);
7866 /* We must do this after lang_do_assignments, because it uses
7868 lang_reset_memory_regions ();
7870 /* Perform another relax pass - this time we know where the
7871 globals are, so can make a better guess. */
7872 relax_again
= false;
7873 lang_size_sections (&relax_again
, false);
7875 while (relax_again
);
7877 link_info
.relax_pass
++;
7884 /* Final extra sizing to report errors. */
7885 lang_do_assignments (lang_assigning_phase_enum
);
7886 lang_reset_memory_regions ();
7887 lang_size_sections (NULL
, true);
7891 #if BFD_SUPPORTS_PLUGINS
7892 /* Find the insert point for the plugin's replacement files. We
7893 place them after the first claimed real object file, or if the
7894 first claimed object is an archive member, after the last real
7895 object file immediately preceding the archive. In the event
7896 no objects have been claimed at all, we return the first dummy
7897 object file on the list as the insert point; that works, but
7898 the callee must be careful when relinking the file_chain as it
7899 is not actually on that chain, only the statement_list and the
7900 input_file list; in that case, the replacement files must be
7901 inserted at the head of the file_chain. */
7903 static lang_input_statement_type
*
7904 find_replacements_insert_point (bool *before
)
7906 lang_input_statement_type
*claim1
, *lastobject
;
7907 lastobject
= (void *) input_file_chain
.head
;
7908 for (claim1
= (void *) file_chain
.head
;
7910 claim1
= claim1
->next
)
7912 if (claim1
->flags
.claimed
)
7914 *before
= claim1
->flags
.claim_archive
;
7915 return claim1
->flags
.claim_archive
? lastobject
: claim1
;
7917 /* Update lastobject if this is a real object file. */
7918 if (claim1
->the_bfd
!= NULL
&& claim1
->the_bfd
->my_archive
== NULL
)
7919 lastobject
= claim1
;
7921 /* No files were claimed by the plugin. Choose the last object
7922 file found on the list (maybe the first, dummy entry) as the
7928 /* Find where to insert ADD, an archive element or shared library
7929 added during a rescan. */
7931 static lang_input_statement_type
**
7932 find_rescan_insertion (lang_input_statement_type
*add
)
7934 bfd
*add_bfd
= add
->the_bfd
;
7935 lang_input_statement_type
*f
;
7936 lang_input_statement_type
*last_loaded
= NULL
;
7937 lang_input_statement_type
*before
= NULL
;
7938 lang_input_statement_type
**iter
= NULL
;
7940 if (add_bfd
->my_archive
!= NULL
)
7941 add_bfd
= add_bfd
->my_archive
;
7943 /* First look through the input file chain, to find an object file
7944 before the one we've rescanned. Normal object files always
7945 appear on both the input file chain and the file chain, so this
7946 lets us get quickly to somewhere near the correct place on the
7947 file chain if it is full of archive elements. Archives don't
7948 appear on the file chain, but if an element has been extracted
7949 then their input_statement->next points at it. */
7950 for (f
= (void *) input_file_chain
.head
;
7952 f
= f
->next_real_file
)
7954 if (f
->the_bfd
== add_bfd
)
7956 before
= last_loaded
;
7957 if (f
->next
!= NULL
)
7958 return &f
->next
->next
;
7960 if (f
->the_bfd
!= NULL
&& f
->next
!= NULL
)
7964 for (iter
= before
? &before
->next
: &file_chain
.head
->input_statement
.next
;
7966 iter
= &(*iter
)->next
)
7967 if (!(*iter
)->flags
.claim_archive
7968 && (*iter
)->the_bfd
->my_archive
== NULL
)
7974 /* Insert SRCLIST into DESTLIST after given element by chaining
7975 on FIELD as the next-pointer. (Counterintuitively does not need
7976 a pointer to the actual after-node itself, just its chain field.) */
7979 lang_list_insert_after (lang_statement_list_type
*destlist
,
7980 lang_statement_list_type
*srclist
,
7981 lang_statement_union_type
**field
)
7983 *(srclist
->tail
) = *field
;
7984 *field
= srclist
->head
;
7985 if (destlist
->tail
== field
)
7986 destlist
->tail
= srclist
->tail
;
7989 /* Detach new nodes added to DESTLIST since the time ORIGLIST
7990 was taken as a copy of it and leave them in ORIGLIST. */
7993 lang_list_remove_tail (lang_statement_list_type
*destlist
,
7994 lang_statement_list_type
*origlist
)
7996 union lang_statement_union
**savetail
;
7997 /* Check that ORIGLIST really is an earlier state of DESTLIST. */
7998 ASSERT (origlist
->head
== destlist
->head
);
7999 savetail
= origlist
->tail
;
8000 origlist
->head
= *(savetail
);
8001 origlist
->tail
= destlist
->tail
;
8002 destlist
->tail
= savetail
;
8006 static lang_statement_union_type
**
8007 find_next_input_statement (lang_statement_union_type
**s
)
8009 for ( ; *s
; s
= &(*s
)->header
.next
)
8011 lang_statement_union_type
**t
;
8012 switch ((*s
)->header
.type
)
8014 case lang_input_statement_enum
:
8016 case lang_wild_statement_enum
:
8017 t
= &(*s
)->wild_statement
.children
.head
;
8019 case lang_group_statement_enum
:
8020 t
= &(*s
)->group_statement
.children
.head
;
8022 case lang_output_section_statement_enum
:
8023 t
= &(*s
)->output_section_statement
.children
.head
;
8028 t
= find_next_input_statement (t
);
8034 #endif /* BFD_SUPPORTS_PLUGINS */
8036 /* Add NAME to the list of garbage collection entry points. */
8039 lang_add_gc_name (const char *name
)
8041 struct bfd_sym_chain
*sym
;
8046 sym
= stat_alloc (sizeof (*sym
));
8048 sym
->next
= link_info
.gc_sym_list
;
8050 link_info
.gc_sym_list
= sym
;
8053 /* Check relocations. */
8056 lang_check_relocs (void)
8058 if (link_info
.check_relocs_after_open_input
)
8062 for (abfd
= link_info
.input_bfds
;
8063 abfd
!= (bfd
*) NULL
; abfd
= abfd
->link
.next
)
8064 if (!bfd_link_check_relocs (abfd
, &link_info
))
8066 /* No object output, fail return. */
8067 config
.make_executable
= false;
8068 /* Note: we do not abort the loop, but rather
8069 continue the scan in case there are other
8070 bad relocations to report. */
8075 /* Look through all output sections looking for places where we can
8076 propagate forward the lma region. */
8079 lang_propagate_lma_regions (void)
8081 lang_output_section_statement_type
*os
;
8083 for (os
= (void *) lang_os_list
.head
;
8087 if (os
->prev
!= NULL
8088 && os
->lma_region
== NULL
8089 && os
->load_base
== NULL
8090 && os
->addr_tree
== NULL
8091 && os
->region
== os
->prev
->region
)
8092 os
->lma_region
= os
->prev
->lma_region
;
8097 warn_non_contiguous_discards (void)
8099 LANG_FOR_EACH_INPUT_STATEMENT (file
)
8101 if ((file
->the_bfd
->flags
& (BFD_LINKER_CREATED
| DYNAMIC
)) != 0
8102 || file
->flags
.just_syms
)
8105 for (asection
*s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
8106 if (s
->output_section
== NULL
8107 && (s
->flags
& SEC_LINKER_CREATED
) == 0)
8108 einfo (_("%P: warning: --enable-non-contiguous-regions "
8109 "discards section `%pA' from `%pB'\n"),
8115 reset_one_wild (lang_statement_union_type
*statement
)
8117 if (statement
->header
.type
== lang_wild_statement_enum
)
8119 lang_wild_statement_type
*stmt
= &statement
->wild_statement
;
8120 lang_list_init (&stmt
->matching_sections
);
8125 reset_resolved_wilds (void)
8127 lang_for_each_statement (reset_one_wild
);
8133 /* Finalize dynamic list. */
8134 if (link_info
.dynamic_list
)
8135 lang_finalize_version_expr_head (&link_info
.dynamic_list
->head
);
8137 current_target
= default_target
;
8139 /* Open the output file. */
8140 lang_for_each_statement (ldlang_open_output
);
8143 ldemul_create_output_section_statements ();
8145 /* Add to the hash table all undefineds on the command line. */
8146 lang_place_undefineds ();
8148 if (!bfd_section_already_linked_table_init ())
8149 einfo (_("%F%P: can not create hash table: %E\n"));
8151 /* A first pass through the memory regions ensures that if any region
8152 references a symbol for its origin or length then this symbol will be
8153 added to the symbol table. Having these symbols in the symbol table
8154 means that when we call open_input_bfds PROVIDE statements will
8155 trigger to provide any needed symbols. The regions origins and
8156 lengths are not assigned as a result of this call. */
8157 lang_do_memory_regions (false);
8159 /* Create a bfd for each input file. */
8160 current_target
= default_target
;
8161 lang_statement_iteration
++;
8162 open_input_bfds (statement_list
.head
, NULL
, OPEN_BFD_NORMAL
);
8164 /* Now that open_input_bfds has processed assignments and provide
8165 statements we can give values to symbolic origin/length now. */
8166 lang_do_memory_regions (true);
8168 ldemul_before_plugin_all_symbols_read ();
8170 #if BFD_SUPPORTS_PLUGINS
8171 if (link_info
.lto_plugin_active
)
8173 lang_statement_list_type added
;
8174 lang_statement_list_type files
, inputfiles
;
8176 /* Now all files are read, let the plugin(s) decide if there
8177 are any more to be added to the link before we call the
8178 emulation's after_open hook. We create a private list of
8179 input statements for this purpose, which we will eventually
8180 insert into the global statement list after the first claimed
8183 /* We need to manipulate all three chains in synchrony. */
8185 inputfiles
= input_file_chain
;
8186 if (plugin_call_all_symbols_read ())
8187 einfo (_("%F%P: %s: plugin reported error after all symbols read\n"),
8188 plugin_error_plugin ());
8189 link_info
.lto_all_symbols_read
= true;
8190 /* Open any newly added files, updating the file chains. */
8191 plugin_undefs
= link_info
.hash
->undefs_tail
;
8192 lang_output_section_statement_type
*last_os
= NULL
;
8193 if (lang_os_list
.head
!= NULL
)
8194 last_os
= ((lang_output_section_statement_type
*)
8195 ((char *) lang_os_list
.tail
8196 - offsetof (lang_output_section_statement_type
, next
)));
8197 open_input_bfds (*added
.tail
, last_os
, OPEN_BFD_NORMAL
);
8198 if (plugin_undefs
== link_info
.hash
->undefs_tail
)
8199 plugin_undefs
= NULL
;
8200 /* Restore the global list pointer now they have all been added. */
8201 lang_list_remove_tail (stat_ptr
, &added
);
8202 /* And detach the fresh ends of the file lists. */
8203 lang_list_remove_tail (&file_chain
, &files
);
8204 lang_list_remove_tail (&input_file_chain
, &inputfiles
);
8205 /* Were any new files added? */
8206 if (added
.head
!= NULL
)
8208 /* If so, we will insert them into the statement list immediately
8209 after the first input file that was claimed by the plugin,
8210 unless that file was an archive in which case it is inserted
8211 immediately before. */
8213 lang_statement_union_type
**prev
;
8214 plugin_insert
= find_replacements_insert_point (&before
);
8215 /* If a plugin adds input files without having claimed any, we
8216 don't really have a good idea where to place them. Just putting
8217 them at the start or end of the list is liable to leave them
8218 outside the crtbegin...crtend range. */
8219 ASSERT (plugin_insert
!= NULL
);
8220 /* Splice the new statement list into the old one. */
8221 prev
= &plugin_insert
->header
.next
;
8224 prev
= find_next_input_statement (prev
);
8225 if (*prev
!= (void *) plugin_insert
->next_real_file
)
8227 /* We didn't find the expected input statement.
8228 Fall back to adding after plugin_insert. */
8229 prev
= &plugin_insert
->header
.next
;
8232 lang_list_insert_after (stat_ptr
, &added
, prev
);
8233 /* Likewise for the file chains. */
8234 lang_list_insert_after (&input_file_chain
, &inputfiles
,
8235 (void *) &plugin_insert
->next_real_file
);
8236 /* We must be careful when relinking file_chain; we may need to
8237 insert the new files at the head of the list if the insert
8238 point chosen is the dummy first input file. */
8239 if (plugin_insert
->filename
)
8240 lang_list_insert_after (&file_chain
, &files
,
8241 (void *) &plugin_insert
->next
);
8243 lang_list_insert_after (&file_chain
, &files
, &file_chain
.head
);
8245 /* Rescan archives in case new undefined symbols have appeared. */
8247 lang_statement_iteration
++;
8248 open_input_bfds (statement_list
.head
, NULL
, OPEN_BFD_RESCAN
);
8249 lang_list_remove_tail (&file_chain
, &files
);
8250 while (files
.head
!= NULL
)
8252 lang_input_statement_type
**insert
;
8253 lang_input_statement_type
**iter
, *temp
;
8256 insert
= find_rescan_insertion (&files
.head
->input_statement
);
8257 /* All elements from an archive can be added at once. */
8258 iter
= &files
.head
->input_statement
.next
;
8259 my_arch
= files
.head
->input_statement
.the_bfd
->my_archive
;
8260 if (my_arch
!= NULL
)
8261 for (; *iter
!= NULL
; iter
= &(*iter
)->next
)
8262 if ((*iter
)->the_bfd
->my_archive
!= my_arch
)
8265 *insert
= &files
.head
->input_statement
;
8266 files
.head
= (lang_statement_union_type
*) *iter
;
8268 if (file_chain
.tail
== (lang_statement_union_type
**) insert
)
8269 file_chain
.tail
= (lang_statement_union_type
**) iter
;
8270 if (my_arch
!= NULL
)
8272 lang_input_statement_type
*parent
= bfd_usrdata (my_arch
);
8274 parent
->next
= (lang_input_statement_type
*)
8276 - offsetof (lang_input_statement_type
, next
));
8281 #endif /* BFD_SUPPORTS_PLUGINS */
8283 struct bfd_sym_chain
**sym
= &link_info
.gc_sym_list
;
8285 sym
= &(*sym
)->next
;
8287 *sym
= &entry_symbol
;
8289 if (entry_symbol
.name
== NULL
)
8291 *sym
= ldlang_undef_chain_list_head
;
8293 /* entry_symbol is normally initialised by an ENTRY definition in the
8294 linker script or the -e command line option. But if neither of
8295 these have been used, the target specific backend may still have
8296 provided an entry symbol via a call to lang_default_entry().
8297 Unfortunately this value will not be processed until lang_end()
8298 is called, long after this function has finished. So detect this
8299 case here and add the target's entry symbol to the list of starting
8300 points for garbage collection resolution. */
8301 lang_add_gc_name (entry_symbol_default
);
8304 lang_add_gc_name (link_info
.init_function
);
8305 lang_add_gc_name (link_info
.fini_function
);
8307 ldemul_after_open ();
8308 if (config
.map_file
!= NULL
)
8309 lang_print_asneeded ();
8313 bfd_section_already_linked_table_free ();
8315 /* Make sure that we're not mixing architectures. We call this
8316 after all the input files have been opened, but before we do any
8317 other processing, so that any operations merge_private_bfd_data
8318 does on the output file will be known during the rest of the
8322 /* Handle .exports instead of a version script if we're told to do so. */
8323 if (command_line
.version_exports_section
)
8324 lang_do_version_exports_section ();
8326 /* Build all sets based on the information gathered from the input
8328 ldctor_build_sets ();
8330 lang_symbol_tweaks ();
8332 /* PR 13683: We must rerun the assignments prior to running garbage
8333 collection in order to make sure that all symbol aliases are resolved. */
8334 lang_do_assignments (lang_mark_phase_enum
);
8335 expld
.phase
= lang_first_phase_enum
;
8337 /* Size up the common data. */
8341 debug_prefix_tree ();
8345 /* Remove unreferenced sections if asked to. */
8346 lang_gc_sections ();
8348 lang_mark_undefineds ();
8350 /* Check relocations. */
8351 lang_check_relocs ();
8353 ldemul_after_check_relocs ();
8355 /* There might have been new sections created (e.g. as result of
8356 checking relocs to need a .got, or suchlike), so to properly order
8357 them into our lists of matching sections reset them here. */
8358 reset_resolved_wilds ();
8361 /* Update wild statements in case the user gave --sort-section.
8362 Note how the option might have come after the linker script and
8363 so couldn't have been set when the wild statements were created. */
8364 update_wild_statements (statement_list
.head
);
8366 /* Run through the contours of the script and attach input sections
8367 to the correct output sections. */
8368 lang_statement_iteration
++;
8369 map_input_to_output_sections (statement_list
.head
, NULL
, NULL
);
8371 /* Start at the statement immediately after the special abs_section
8372 output statement, so that it isn't reordered. */
8373 process_insert_statements (&lang_os_list
.head
->header
.next
);
8375 ldemul_before_place_orphans ();
8377 /* Find any sections not attached explicitly and handle them. */
8378 lang_place_orphans ();
8380 if (!bfd_link_relocatable (&link_info
))
8384 /* Merge SEC_MERGE sections. This has to be done after GC of
8385 sections, so that GCed sections are not merged, but before
8386 assigning dynamic symbols, since removing whole input sections
8388 bfd_merge_sections (link_info
.output_bfd
, &link_info
);
8390 /* Look for a text section and set the readonly attribute in it. */
8391 found
= bfd_get_section_by_name (link_info
.output_bfd
, ".text");
8395 if (config
.text_read_only
)
8396 found
->flags
|= SEC_READONLY
;
8398 found
->flags
&= ~SEC_READONLY
;
8402 /* Merge together CTF sections. After this, only the symtab-dependent
8403 function and data object sections need adjustment. */
8406 /* Emit the CTF, iff the emulation doesn't need to do late emission after
8407 examining things laid out late, like the strtab. */
8410 /* Copy forward lma regions for output sections in same lma region. */
8411 lang_propagate_lma_regions ();
8413 /* Defining __start/__stop symbols early for --gc-sections to work
8414 around a glibc build problem can result in these symbols being
8415 defined when they should not be. Fix them now. */
8416 if (config
.build_constructors
)
8417 lang_undef_start_stop ();
8419 /* Define .startof./.sizeof. symbols with preliminary values before
8420 dynamic symbols are created. */
8421 if (!bfd_link_relocatable (&link_info
))
8422 lang_init_startof_sizeof ();
8424 /* Do anything special before sizing sections. This is where ELF
8425 and other back-ends size dynamic sections. */
8426 ldemul_before_allocation ();
8428 /* We must record the program headers before we try to fix the
8429 section positions, since they will affect SIZEOF_HEADERS. */
8430 lang_record_phdrs ();
8432 /* Check relro sections. */
8433 if (link_info
.relro
&& !bfd_link_relocatable (&link_info
))
8434 lang_find_relro_sections ();
8436 /* Size up the sections. */
8437 lang_size_sections (NULL
, !RELAXATION_ENABLED
);
8439 /* See if anything special should be done now we know how big
8440 everything is. This is where relaxation is done. */
8441 ldemul_after_allocation ();
8443 /* Fix any __start, __stop, .startof. or .sizeof. symbols. */
8444 lang_finalize_start_stop ();
8446 /* Do all the assignments again, to report errors. Assignment
8447 statements are processed multiple times, updating symbols; In
8448 open_input_bfds, lang_do_assignments, and lang_size_sections.
8449 Since lang_relax_sections calls lang_do_assignments, symbols are
8450 also updated in ldemul_after_allocation. */
8451 lang_do_assignments (lang_final_phase_enum
);
8455 /* Convert absolute symbols to section relative. */
8456 ldexp_finalize_syms ();
8458 /* Make sure that the section addresses make sense. */
8459 if (command_line
.check_section_addresses
)
8460 lang_check_section_addresses ();
8462 if (link_info
.non_contiguous_regions
8463 && link_info
.non_contiguous_regions_warnings
)
8464 warn_non_contiguous_discards ();
8466 /* Check any required symbols are known. */
8467 ldlang_check_require_defined_symbols ();
8473 lang_add_version_string (void)
8475 if (! enable_linker_version
)
8478 const char * str
= "GNU ld ";
8479 int len
= strlen (str
);
8482 for (i
= 0 ; i
< len
; i
++)
8483 lang_add_data (BYTE
, exp_intop (str
[i
]));
8485 str
= BFD_VERSION_STRING
;
8488 for (i
= 0 ; i
< len
; i
++)
8489 lang_add_data (BYTE
, exp_intop (str
[i
]));
8491 lang_add_data (BYTE
, exp_intop ('\0'));
8494 /* EXPORTED TO YACC */
8497 lang_add_wild (struct wildcard_spec
*filespec
,
8498 struct wildcard_list
*section_list
,
8501 struct wildcard_list
*curr
, *next
;
8502 lang_wild_statement_type
*new_stmt
;
8503 bool any_specs_sorted
= false;
8505 /* Reverse the list as the parser puts it back to front. */
8506 for (curr
= section_list
, section_list
= NULL
;
8508 section_list
= curr
, curr
= next
)
8510 if (curr
->spec
.sorted
!= none
&& curr
->spec
.sorted
!= by_none
)
8511 any_specs_sorted
= true;
8513 curr
->next
= section_list
;
8516 if (filespec
!= NULL
&& filespec
->name
!= NULL
)
8518 if (strcmp (filespec
->name
, "*") == 0)
8519 filespec
->name
= NULL
;
8520 else if (!wildcardp (filespec
->name
))
8521 lang_has_input_file
= true;
8524 new_stmt
= new_stat (lang_wild_statement
, stat_ptr
);
8525 new_stmt
->filename
= NULL
;
8526 new_stmt
->filenames_sorted
= false;
8527 new_stmt
->any_specs_sorted
= any_specs_sorted
;
8528 new_stmt
->section_flag_list
= NULL
;
8529 new_stmt
->exclude_name_list
= NULL
;
8530 if (filespec
!= NULL
)
8532 new_stmt
->filename
= filespec
->name
;
8533 new_stmt
->filenames_sorted
= (filespec
->sorted
== by_name
|| filespec
->reversed
);
8534 new_stmt
->section_flag_list
= filespec
->section_flag_list
;
8535 new_stmt
->exclude_name_list
= filespec
->exclude_name_list
;
8536 new_stmt
->filenames_reversed
= filespec
->reversed
;
8538 new_stmt
->section_list
= section_list
;
8539 new_stmt
->keep_sections
= keep_sections
;
8540 lang_list_init (&new_stmt
->children
);
8541 lang_list_init (&new_stmt
->matching_sections
);
8542 analyze_walk_wild_section_handler (new_stmt
);
8545 printf ("wild %s(", new_stmt
->filename
? new_stmt
->filename
: "*");
8546 for (curr
= new_stmt
->section_list
; curr
; curr
= curr
->next
)
8547 printf ("%s ", curr
->spec
.name
? curr
->spec
.name
: "*");
8553 lang_section_start (const char *name
, etree_type
*address
,
8554 const segment_type
*segment
)
8556 lang_address_statement_type
*ad
;
8558 ad
= new_stat (lang_address_statement
, stat_ptr
);
8559 ad
->section_name
= name
;
8560 ad
->address
= address
;
8561 ad
->segment
= segment
;
8564 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
8565 because of a -e argument on the command line, or zero if this is
8566 called by ENTRY in a linker script. Command line arguments take
8570 lang_add_entry (const char *name
, bool cmdline
)
8572 if (entry_symbol
.name
== NULL
8574 || !entry_from_cmdline
)
8576 entry_symbol
.name
= name
;
8577 entry_from_cmdline
= cmdline
;
8581 /* Set the default start symbol to NAME. .em files should use this,
8582 not lang_add_entry, to override the use of "start" if neither the
8583 linker script nor the command line specifies an entry point. NAME
8584 must be permanently allocated. */
8586 lang_default_entry (const char *name
)
8588 entry_symbol_default
= name
;
8592 lang_add_target (const char *name
)
8594 lang_target_statement_type
*new_stmt
;
8596 new_stmt
= new_stat (lang_target_statement
, stat_ptr
);
8597 new_stmt
->target
= name
;
8601 lang_add_map (const char *name
)
8608 map_option_f
= true;
8616 lang_add_fill (fill_type
*fill
)
8618 lang_fill_statement_type
*new_stmt
;
8620 new_stmt
= new_stat (lang_fill_statement
, stat_ptr
);
8621 new_stmt
->fill
= fill
;
8625 lang_add_data (int type
, union etree_union
*exp
)
8627 lang_data_statement_type
*new_stmt
;
8629 new_stmt
= new_stat (lang_data_statement
, stat_ptr
);
8630 new_stmt
->exp
= exp
;
8631 new_stmt
->type
= type
;
8635 lang_add_string (const char *s
)
8637 bfd_vma len
= strlen (s
);
8639 bool escape
= false;
8641 /* Add byte expressions until end of string. */
8642 for (i
= 0 ; i
< len
; i
++)
8651 /* Ignore the escape. */
8654 case 'n': c
= '\n'; break;
8655 case 'r': c
= '\r'; break;
8656 case 't': c
= '\t'; break;
8666 /* We have an octal number. */
8668 unsigned int value
= c
- '0';
8671 if ((c
>= '0') && (c
<= '7'))
8679 if ((c
>= '0') && (c
<= '7'))
8690 /* octal: \777 is treated as '\077' + '7' */
8701 lang_add_data (BYTE
, exp_intop (c
));
8709 lang_add_data (BYTE
, exp_intop (c
));
8713 /* Remeber to terminate the string. */
8714 lang_add_data (BYTE
, exp_intop (0));
8717 /* Create a new reloc statement. RELOC is the BFD relocation type to
8718 generate. HOWTO is the corresponding howto structure (we could
8719 look this up, but the caller has already done so). SECTION is the
8720 section to generate a reloc against, or NAME is the name of the
8721 symbol to generate a reloc against. Exactly one of SECTION and
8722 NAME must be NULL. ADDEND is an expression for the addend. */
8725 lang_add_reloc (bfd_reloc_code_real_type reloc
,
8726 reloc_howto_type
*howto
,
8729 union etree_union
*addend
)
8731 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
8735 p
->section
= section
;
8737 p
->addend_exp
= addend
;
8739 p
->addend_value
= 0;
8740 p
->output_section
= NULL
;
8741 p
->output_offset
= 0;
8744 lang_assignment_statement_type
*
8745 lang_add_assignment (etree_type
*exp
)
8747 lang_assignment_statement_type
*new_stmt
;
8749 new_stmt
= new_stat (lang_assignment_statement
, stat_ptr
);
8750 new_stmt
->exp
= exp
;
8755 lang_add_attribute (enum statement_enum attribute
)
8757 new_statement (attribute
, sizeof (lang_statement_header_type
), stat_ptr
);
8761 lang_startup (const char *name
)
8763 if (first_file
->filename
!= NULL
)
8765 einfo (_("%F%P: multiple STARTUP files\n"));
8767 first_file
->filename
= name
;
8768 first_file
->local_sym_name
= name
;
8769 first_file
->flags
.real
= true;
8773 lang_float (bool maybe
)
8775 lang_float_flag
= maybe
;
8779 /* Work out the load- and run-time regions from a script statement, and
8780 store them in *LMA_REGION and *REGION respectively.
8782 MEMSPEC is the name of the run-time region, or the value of
8783 DEFAULT_MEMORY_REGION if the statement didn't specify one.
8784 LMA_MEMSPEC is the name of the load-time region, or null if the
8785 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
8786 had an explicit load address.
8788 It is an error to specify both a load region and a load address. */
8791 lang_get_regions (lang_memory_region_type
**region
,
8792 lang_memory_region_type
**lma_region
,
8793 const char *memspec
,
8794 const char *lma_memspec
,
8798 *lma_region
= lang_memory_region_lookup (lma_memspec
, false);
8800 /* If no runtime region or VMA has been specified, but the load region
8801 has been specified, then use the load region for the runtime region
8803 if (lma_memspec
!= NULL
8805 && strcmp (memspec
, DEFAULT_MEMORY_REGION
) == 0)
8806 *region
= *lma_region
;
8808 *region
= lang_memory_region_lookup (memspec
, false);
8810 if (have_lma
&& lma_memspec
!= 0)
8811 einfo (_("%X%P:%pS: section has both a load address and a load region\n"),
8816 lang_leave_output_section_statement (fill_type
*fill
, const char *memspec
,
8817 lang_output_section_phdr_list
*phdrs
,
8818 const char *lma_memspec
)
8820 lang_get_regions (¤t_section
->region
,
8821 ¤t_section
->lma_region
,
8822 memspec
, lma_memspec
,
8823 current_section
->load_base
!= NULL
,
8824 current_section
->addr_tree
!= NULL
);
8826 current_section
->fill
= fill
;
8827 current_section
->phdrs
= phdrs
;
8831 /* Set the output format type. -oformat overrides scripts. */
8834 lang_add_output_format (const char *format
,
8839 if (output_target
== NULL
|| !from_script
)
8841 if (command_line
.endian
== ENDIAN_BIG
8844 else if (command_line
.endian
== ENDIAN_LITTLE
8848 output_target
= format
;
8853 lang_add_insert (const char *where
, int is_before
)
8855 lang_insert_statement_type
*new_stmt
;
8857 new_stmt
= new_stat (lang_insert_statement
, stat_ptr
);
8858 new_stmt
->where
= where
;
8859 new_stmt
->is_before
= is_before
;
8860 saved_script_handle
= previous_script_handle
;
8863 /* Enter a group. This creates a new lang_group_statement, and sets
8864 stat_ptr to build new statements within the group. */
8867 lang_enter_group (void)
8869 lang_group_statement_type
*g
;
8871 g
= new_stat (lang_group_statement
, stat_ptr
);
8872 lang_list_init (&g
->children
);
8873 push_stat_ptr (&g
->children
);
8876 /* Leave a group. This just resets stat_ptr to start writing to the
8877 regular list of statements again. Note that this will not work if
8878 groups can occur inside anything else which can adjust stat_ptr,
8879 but currently they can't. */
8882 lang_leave_group (void)
8887 /* Add a new program header. This is called for each entry in a PHDRS
8888 command in a linker script. */
8891 lang_new_phdr (const char *name
,
8898 struct lang_phdr
*n
, **pp
;
8901 n
= stat_alloc (sizeof (struct lang_phdr
));
8904 n
->type
= exp_get_vma (type
, NULL
, 0, "program header type");
8905 n
->filehdr
= filehdr
;
8910 hdrs
= n
->type
== 1 && (phdrs
|| filehdr
);
8912 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
8915 && !((*pp
)->filehdr
|| (*pp
)->phdrs
))
8917 einfo (_("%X%P:%pS: PHDRS and FILEHDR are not supported"
8918 " when prior PT_LOAD headers lack them\n"), NULL
);
8925 /* Record the program header information in the output BFD. FIXME: We
8926 should not be calling an ELF specific function here. */
8929 lang_record_phdrs (void)
8933 lang_output_section_phdr_list
*last
;
8934 struct lang_phdr
*l
;
8935 lang_output_section_statement_type
*os
;
8938 secs
= (asection
**) xmalloc (alc
* sizeof (asection
*));
8941 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
8948 for (os
= (void *) lang_os_list
.head
;
8952 lang_output_section_phdr_list
*pl
;
8954 if (os
->constraint
< 0)
8962 if (os
->sectype
== noload_section
8963 || os
->bfd_section
== NULL
8964 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
8967 /* Don't add orphans to PT_INTERP header. */
8973 lang_output_section_statement_type
*tmp_os
;
8975 /* If we have not run across a section with a program
8976 header assigned to it yet, then scan forwards to find
8977 one. This prevents inconsistencies in the linker's
8978 behaviour when a script has specified just a single
8979 header and there are sections in that script which are
8980 not assigned to it, and which occur before the first
8981 use of that header. See here for more details:
8982 http://sourceware.org/ml/binutils/2007-02/msg00291.html */
8983 for (tmp_os
= os
; tmp_os
; tmp_os
= tmp_os
->next
)
8986 last
= tmp_os
->phdrs
;
8990 einfo (_("%F%P: no sections assigned to phdrs\n"));
8995 if (os
->bfd_section
== NULL
)
8998 for (; pl
!= NULL
; pl
= pl
->next
)
9000 if (strcmp (pl
->name
, l
->name
) == 0)
9005 secs
= (asection
**) xrealloc (secs
,
9006 alc
* sizeof (asection
*));
9008 secs
[c
] = os
->bfd_section
;
9015 if (l
->flags
== NULL
)
9018 flags
= exp_get_vma (l
->flags
, NULL
, 0, "phdr flags");
9023 at
= exp_get_vma (l
->at
, NULL
, 0, "phdr load address");
9025 if (!bfd_record_phdr (link_info
.output_bfd
, l
->type
,
9026 l
->flags
!= NULL
, flags
, l
->at
!= NULL
,
9027 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
9028 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
9033 /* Make sure all the phdr assignments succeeded. */
9034 for (os
= (void *) lang_os_list
.head
;
9038 lang_output_section_phdr_list
*pl
;
9040 if (os
->constraint
< 0
9041 || os
->bfd_section
== NULL
)
9044 for (pl
= os
->phdrs
;
9047 if (!pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
9048 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
9049 os
->name
, pl
->name
);
9053 /* Record a list of sections which may not be cross referenced. */
9056 lang_add_nocrossref (lang_nocrossref_type
*l
)
9058 struct lang_nocrossrefs
*n
;
9060 n
= (struct lang_nocrossrefs
*) xmalloc (sizeof *n
);
9061 n
->next
= nocrossref_list
;
9063 n
->onlyfirst
= false;
9064 nocrossref_list
= n
;
9066 /* Set notice_all so that we get informed about all symbols. */
9067 link_info
.notice_all
= true;
9070 /* Record a section that cannot be referenced from a list of sections. */
9073 lang_add_nocrossref_to (lang_nocrossref_type
*l
)
9075 lang_add_nocrossref (l
);
9076 nocrossref_list
->onlyfirst
= true;
9079 /* Overlay handling. We handle overlays with some static variables. */
9081 /* The overlay virtual address. */
9082 static etree_type
*overlay_vma
;
9083 /* And subsection alignment. */
9084 static etree_type
*overlay_subalign
;
9086 /* An expression for the maximum section size seen so far. */
9087 static etree_type
*overlay_max
;
9089 /* A list of all the sections in this overlay. */
9091 struct overlay_list
{
9092 struct overlay_list
*next
;
9093 lang_output_section_statement_type
*os
;
9096 static struct overlay_list
*overlay_list
;
9098 /* Start handling an overlay. */
9101 lang_enter_overlay (etree_type
*vma_expr
, etree_type
*subalign
)
9103 /* The grammar should prevent nested overlays from occurring. */
9104 ASSERT (overlay_vma
== NULL
9105 && overlay_subalign
== NULL
9106 && overlay_max
== NULL
);
9108 overlay_vma
= vma_expr
;
9109 overlay_subalign
= subalign
;
9112 /* Start a section in an overlay. We handle this by calling
9113 lang_enter_output_section_statement with the correct VMA.
9114 lang_leave_overlay sets up the LMA and memory regions. */
9117 lang_enter_overlay_section (const char *name
)
9119 struct overlay_list
*n
;
9122 lang_enter_output_section_statement (name
, overlay_vma
, overlay_section
,
9123 0, 0, overlay_subalign
, 0, 0, 0);
9125 /* If this is the first section, then base the VMA of future
9126 sections on this one. This will work correctly even if `.' is
9127 used in the addresses. */
9128 if (overlay_list
== NULL
)
9129 overlay_vma
= exp_nameop (ADDR
, name
);
9131 /* Remember the section. */
9132 n
= (struct overlay_list
*) xmalloc (sizeof *n
);
9133 n
->os
= current_section
;
9134 n
->next
= overlay_list
;
9137 size
= exp_nameop (SIZEOF
, name
);
9139 /* Arrange to work out the maximum section end address. */
9140 if (overlay_max
== NULL
)
9143 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
9146 /* Finish a section in an overlay. There isn't any special to do
9150 lang_leave_overlay_section (fill_type
*fill
,
9151 lang_output_section_phdr_list
*phdrs
)
9158 name
= current_section
->name
;
9160 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
9161 region and that no load-time region has been specified. It doesn't
9162 really matter what we say here, since lang_leave_overlay will
9164 lang_leave_output_section_statement (fill
, DEFAULT_MEMORY_REGION
, phdrs
, 0);
9166 /* Define the magic symbols. */
9168 clean
= (char *) xmalloc (strlen (name
) + 1);
9170 for (s1
= name
; *s1
!= '\0'; s1
++)
9171 if (ISALNUM (*s1
) || *s1
== '_')
9175 buf
= (char *) xmalloc (strlen (clean
) + sizeof "__load_start_");
9176 sprintf (buf
, "__load_start_%s", clean
);
9177 lang_add_assignment (exp_provide (buf
,
9178 exp_nameop (LOADADDR
, name
),
9181 buf
= (char *) xmalloc (strlen (clean
) + sizeof "__load_stop_");
9182 sprintf (buf
, "__load_stop_%s", clean
);
9183 lang_add_assignment (exp_provide (buf
,
9185 exp_nameop (LOADADDR
, name
),
9186 exp_nameop (SIZEOF
, name
)),
9192 /* Finish an overlay. If there are any overlay wide settings, this
9193 looks through all the sections in the overlay and sets them. */
9196 lang_leave_overlay (etree_type
*lma_expr
,
9199 const char *memspec
,
9200 lang_output_section_phdr_list
*phdrs
,
9201 const char *lma_memspec
)
9203 lang_memory_region_type
*region
;
9204 lang_memory_region_type
*lma_region
;
9205 struct overlay_list
*l
;
9206 lang_nocrossref_type
*nocrossref
;
9208 lang_get_regions (®ion
, &lma_region
,
9209 memspec
, lma_memspec
,
9210 lma_expr
!= NULL
, false);
9214 /* After setting the size of the last section, set '.' to end of the
9216 if (overlay_list
!= NULL
)
9218 overlay_list
->os
->update_dot
= 1;
9219 overlay_list
->os
->update_dot_tree
9220 = exp_assign (".", exp_binop ('+', overlay_vma
, overlay_max
), false);
9226 struct overlay_list
*next
;
9228 if (fill
!= NULL
&& l
->os
->fill
== NULL
)
9231 l
->os
->region
= region
;
9232 l
->os
->lma_region
= lma_region
;
9234 /* The first section has the load address specified in the
9235 OVERLAY statement. The rest are worked out from that.
9236 The base address is not needed (and should be null) if
9237 an LMA region was specified. */
9240 l
->os
->load_base
= lma_expr
;
9241 l
->os
->sectype
= first_overlay_section
;
9243 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
9244 l
->os
->phdrs
= phdrs
;
9248 lang_nocrossref_type
*nc
;
9250 nc
= (lang_nocrossref_type
*) xmalloc (sizeof *nc
);
9251 nc
->name
= l
->os
->name
;
9252 nc
->next
= nocrossref
;
9261 if (nocrossref
!= NULL
)
9262 lang_add_nocrossref (nocrossref
);
9265 overlay_list
= NULL
;
9267 overlay_subalign
= NULL
;
9270 /* Version handling. This is only useful for ELF. */
9272 /* If PREV is NULL, return first version pattern matching particular symbol.
9273 If PREV is non-NULL, return first version pattern matching particular
9274 symbol after PREV (previously returned by lang_vers_match). */
9276 static struct bfd_elf_version_expr
*
9277 lang_vers_match (struct bfd_elf_version_expr_head
*head
,
9278 struct bfd_elf_version_expr
*prev
,
9282 const char *cxx_sym
= sym
;
9283 const char *java_sym
= sym
;
9284 struct bfd_elf_version_expr
*expr
= NULL
;
9285 enum demangling_styles curr_style
;
9287 curr_style
= CURRENT_DEMANGLING_STYLE
;
9288 cplus_demangle_set_style (no_demangling
);
9289 c_sym
= bfd_demangle (link_info
.output_bfd
, sym
, DMGL_NO_OPTS
);
9292 cplus_demangle_set_style (curr_style
);
9294 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
9296 cxx_sym
= bfd_demangle (link_info
.output_bfd
, sym
,
9297 DMGL_PARAMS
| DMGL_ANSI
);
9301 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
9303 java_sym
= bfd_demangle (link_info
.output_bfd
, sym
, DMGL_JAVA
);
9308 if (head
->htab
&& (prev
== NULL
|| prev
->literal
))
9310 struct bfd_elf_version_expr e
;
9312 switch (prev
? prev
->mask
: 0)
9315 if (head
->mask
& BFD_ELF_VERSION_C_TYPE
)
9318 expr
= (struct bfd_elf_version_expr
*)
9319 htab_find ((htab_t
) head
->htab
, &e
);
9320 while (expr
&& strcmp (expr
->pattern
, c_sym
) == 0)
9321 if (expr
->mask
== BFD_ELF_VERSION_C_TYPE
)
9327 case BFD_ELF_VERSION_C_TYPE
:
9328 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
9330 e
.pattern
= cxx_sym
;
9331 expr
= (struct bfd_elf_version_expr
*)
9332 htab_find ((htab_t
) head
->htab
, &e
);
9333 while (expr
&& strcmp (expr
->pattern
, cxx_sym
) == 0)
9334 if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
9340 case BFD_ELF_VERSION_CXX_TYPE
:
9341 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
9343 e
.pattern
= java_sym
;
9344 expr
= (struct bfd_elf_version_expr
*)
9345 htab_find ((htab_t
) head
->htab
, &e
);
9346 while (expr
&& strcmp (expr
->pattern
, java_sym
) == 0)
9347 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
9358 /* Finally, try the wildcards. */
9359 if (prev
== NULL
|| prev
->literal
)
9360 expr
= head
->remaining
;
9363 for (; expr
; expr
= expr
->next
)
9370 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
9373 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
9375 else if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
9379 if (fnmatch (expr
->pattern
, s
, 0) == 0)
9385 free ((char *) c_sym
);
9387 free ((char *) cxx_sym
);
9388 if (java_sym
!= sym
)
9389 free ((char *) java_sym
);
9393 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
9394 return a pointer to the symbol name with any backslash quotes removed. */
9397 realsymbol (const char *pattern
)
9400 bool changed
= false, backslash
= false;
9401 char *s
, *symbol
= (char *) xmalloc (strlen (pattern
) + 1);
9403 for (p
= pattern
, s
= symbol
; *p
!= '\0'; ++p
)
9405 /* It is a glob pattern only if there is no preceding
9409 /* Remove the preceding backslash. */
9416 if (*p
== '?' || *p
== '*' || *p
== '[')
9423 backslash
= *p
== '\\';
9439 /* This is called for each variable name or match expression. NEW_NAME is
9440 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
9441 pattern to be matched against symbol names. */
9443 struct bfd_elf_version_expr
*
9444 lang_new_vers_pattern (struct bfd_elf_version_expr
*orig
,
9445 const char *new_name
,
9449 struct bfd_elf_version_expr
*ret
;
9451 ret
= (struct bfd_elf_version_expr
*) xmalloc (sizeof *ret
);
9455 ret
->literal
= true;
9456 ret
->pattern
= literal_p
? new_name
: realsymbol (new_name
);
9457 if (ret
->pattern
== NULL
)
9459 ret
->pattern
= new_name
;
9460 ret
->literal
= false;
9463 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
9464 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
9465 else if (strcasecmp (lang
, "C++") == 0)
9466 ret
->mask
= BFD_ELF_VERSION_CXX_TYPE
;
9467 else if (strcasecmp (lang
, "Java") == 0)
9468 ret
->mask
= BFD_ELF_VERSION_JAVA_TYPE
;
9471 einfo (_("%X%P: unknown language `%s' in version information\n"),
9473 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
9476 return ldemul_new_vers_pattern (ret
);
9479 /* This is called for each set of variable names and match
9482 struct bfd_elf_version_tree
*
9483 lang_new_vers_node (struct bfd_elf_version_expr
*globals
,
9484 struct bfd_elf_version_expr
*locals
)
9486 struct bfd_elf_version_tree
*ret
;
9488 ret
= (struct bfd_elf_version_tree
*) xcalloc (1, sizeof *ret
);
9489 ret
->globals
.list
= globals
;
9490 ret
->locals
.list
= locals
;
9491 ret
->match
= lang_vers_match
;
9492 ret
->name_indx
= (unsigned int) -1;
9496 /* This static variable keeps track of version indices. */
9498 static int version_index
;
9501 version_expr_head_hash (const void *p
)
9503 const struct bfd_elf_version_expr
*e
=
9504 (const struct bfd_elf_version_expr
*) p
;
9506 return htab_hash_string (e
->pattern
);
9510 version_expr_head_eq (const void *p1
, const void *p2
)
9512 const struct bfd_elf_version_expr
*e1
=
9513 (const struct bfd_elf_version_expr
*) p1
;
9514 const struct bfd_elf_version_expr
*e2
=
9515 (const struct bfd_elf_version_expr
*) p2
;
9517 return strcmp (e1
->pattern
, e2
->pattern
) == 0;
9521 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head
*head
)
9524 struct bfd_elf_version_expr
*e
, *next
;
9525 struct bfd_elf_version_expr
**list_loc
, **remaining_loc
;
9527 for (e
= head
->list
; e
; e
= e
->next
)
9531 head
->mask
|= e
->mask
;
9536 head
->htab
= htab_create (count
* 2, version_expr_head_hash
,
9537 version_expr_head_eq
, NULL
);
9538 list_loc
= &head
->list
;
9539 remaining_loc
= &head
->remaining
;
9540 for (e
= head
->list
; e
; e
= next
)
9546 remaining_loc
= &e
->next
;
9550 void **loc
= htab_find_slot ((htab_t
) head
->htab
, e
, INSERT
);
9554 struct bfd_elf_version_expr
*e1
, *last
;
9556 e1
= (struct bfd_elf_version_expr
*) *loc
;
9560 if (e1
->mask
== e
->mask
)
9568 while (e1
&& strcmp (e1
->pattern
, e
->pattern
) == 0);
9572 /* This is a duplicate. */
9573 /* FIXME: Memory leak. Sometimes pattern is not
9574 xmalloced alone, but in larger chunk of memory. */
9575 /* free (e->pattern); */
9580 e
->next
= last
->next
;
9588 list_loc
= &e
->next
;
9592 *remaining_loc
= NULL
;
9593 *list_loc
= head
->remaining
;
9596 head
->remaining
= head
->list
;
9599 /* This is called when we know the name and dependencies of the
9603 lang_register_vers_node (const char *name
,
9604 struct bfd_elf_version_tree
*version
,
9605 struct bfd_elf_version_deps
*deps
)
9607 struct bfd_elf_version_tree
*t
, **pp
;
9608 struct bfd_elf_version_expr
*e1
;
9613 if (link_info
.version_info
!= NULL
9614 && (name
[0] == '\0' || link_info
.version_info
->name
[0] == '\0'))
9616 einfo (_("%X%P: anonymous version tag cannot be combined"
9617 " with other version tags\n"));
9622 /* Make sure this node has a unique name. */
9623 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9624 if (strcmp (t
->name
, name
) == 0)
9625 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
9627 lang_finalize_version_expr_head (&version
->globals
);
9628 lang_finalize_version_expr_head (&version
->locals
);
9630 /* Check the global and local match names, and make sure there
9631 aren't any duplicates. */
9633 for (e1
= version
->globals
.list
; e1
!= NULL
; e1
= e1
->next
)
9635 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9637 struct bfd_elf_version_expr
*e2
;
9639 if (t
->locals
.htab
&& e1
->literal
)
9641 e2
= (struct bfd_elf_version_expr
*)
9642 htab_find ((htab_t
) t
->locals
.htab
, e1
);
9643 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
9645 if (e1
->mask
== e2
->mask
)
9646 einfo (_("%X%P: duplicate expression `%s'"
9647 " in version information\n"), e1
->pattern
);
9651 else if (!e1
->literal
)
9652 for (e2
= t
->locals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
9653 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
9654 && e1
->mask
== e2
->mask
)
9655 einfo (_("%X%P: duplicate expression `%s'"
9656 " in version information\n"), e1
->pattern
);
9660 for (e1
= version
->locals
.list
; e1
!= NULL
; e1
= e1
->next
)
9662 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9664 struct bfd_elf_version_expr
*e2
;
9666 if (t
->globals
.htab
&& e1
->literal
)
9668 e2
= (struct bfd_elf_version_expr
*)
9669 htab_find ((htab_t
) t
->globals
.htab
, e1
);
9670 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
9672 if (e1
->mask
== e2
->mask
)
9673 einfo (_("%X%P: duplicate expression `%s'"
9674 " in version information\n"),
9679 else if (!e1
->literal
)
9680 for (e2
= t
->globals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
9681 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
9682 && e1
->mask
== e2
->mask
)
9683 einfo (_("%X%P: duplicate expression `%s'"
9684 " in version information\n"), e1
->pattern
);
9688 version
->deps
= deps
;
9689 version
->name
= name
;
9690 if (name
[0] != '\0')
9693 version
->vernum
= version_index
;
9696 version
->vernum
= 0;
9698 for (pp
= &link_info
.version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
9703 /* This is called when we see a version dependency. */
9705 struct bfd_elf_version_deps
*
9706 lang_add_vers_depend (struct bfd_elf_version_deps
*list
, const char *name
)
9708 struct bfd_elf_version_deps
*ret
;
9709 struct bfd_elf_version_tree
*t
;
9711 ret
= (struct bfd_elf_version_deps
*) xmalloc (sizeof *ret
);
9714 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9716 if (strcmp (t
->name
, name
) == 0)
9718 ret
->version_needed
= t
;
9723 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
9725 ret
->version_needed
= NULL
;
9730 lang_do_version_exports_section (void)
9732 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
9734 LANG_FOR_EACH_INPUT_STATEMENT (is
)
9736 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
9744 contents
= (char *) xmalloc (len
);
9745 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
9746 einfo (_("%X%P: unable to read .exports section contents\n"), sec
);
9749 while (p
< contents
+ len
)
9751 greg
= lang_new_vers_pattern (greg
, p
, NULL
, false);
9752 p
= strchr (p
, '\0') + 1;
9755 /* Do not free the contents, as we used them creating the regex. */
9757 /* Do not include this section in the link. */
9758 sec
->flags
|= SEC_EXCLUDE
| SEC_KEEP
;
9761 lreg
= lang_new_vers_pattern (NULL
, "*", NULL
, false);
9762 lang_register_vers_node (command_line
.version_exports_section
,
9763 lang_new_vers_node (greg
, lreg
), NULL
);
9766 /* Evaluate LENGTH and ORIGIN parts of MEMORY spec. This is initially
9767 called with UPDATE_REGIONS_P set to FALSE, in this case no errors are
9768 thrown, however, references to symbols in the origin and length fields
9769 will be pushed into the symbol table, this allows PROVIDE statements to
9770 then provide these symbols. This function is called a second time with
9771 UPDATE_REGIONS_P set to TRUE, this time the we update the actual region
9772 data structures, and throw errors if missing symbols are encountered. */
9775 lang_do_memory_regions (bool update_regions_p
)
9777 lang_memory_region_type
*r
= lang_memory_region_list
;
9779 for (; r
!= NULL
; r
= r
->next
)
9783 exp_fold_tree_no_dot (r
->origin_exp
, NULL
);
9784 if (update_regions_p
)
9786 if (expld
.result
.valid_p
)
9788 r
->origin
= expld
.result
.value
;
9789 r
->current
= r
->origin
;
9792 einfo (_("%P: invalid origin for memory region %s\n"),
9798 exp_fold_tree_no_dot (r
->length_exp
, NULL
);
9799 if (update_regions_p
)
9801 if (expld
.result
.valid_p
)
9802 r
->length
= expld
.result
.value
;
9804 einfo (_("%P: invalid length for memory region %s\n"),
9812 lang_add_unique (const char *name
)
9814 struct unique_sections
*ent
;
9816 for (ent
= unique_section_list
; ent
; ent
= ent
->next
)
9817 if (strcmp (ent
->name
, name
) == 0)
9820 ent
= (struct unique_sections
*) xmalloc (sizeof *ent
);
9821 ent
->name
= xstrdup (name
);
9822 ent
->next
= unique_section_list
;
9823 unique_section_list
= ent
;
9826 /* Append the list of dynamic symbols to the existing one. */
9829 lang_append_dynamic_list (struct bfd_elf_dynamic_list
**list_p
,
9830 struct bfd_elf_version_expr
*dynamic
)
9834 struct bfd_elf_version_expr
*tail
;
9835 for (tail
= dynamic
; tail
->next
!= NULL
; tail
= tail
->next
)
9837 tail
->next
= (*list_p
)->head
.list
;
9838 (*list_p
)->head
.list
= dynamic
;
9842 struct bfd_elf_dynamic_list
*d
;
9844 d
= (struct bfd_elf_dynamic_list
*) xcalloc (1, sizeof *d
);
9845 d
->head
.list
= dynamic
;
9846 d
->match
= lang_vers_match
;
9851 /* Append the list of C++ typeinfo dynamic symbols to the existing
9855 lang_append_dynamic_list_cpp_typeinfo (void)
9857 const char *symbols
[] =
9859 "typeinfo name for*",
9862 struct bfd_elf_version_expr
*dynamic
= NULL
;
9865 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
9866 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
9869 lang_append_dynamic_list (&link_info
.dynamic_list
, dynamic
);
9872 /* Append the list of C++ operator new and delete dynamic symbols to the
9876 lang_append_dynamic_list_cpp_new (void)
9878 const char *symbols
[] =
9883 struct bfd_elf_version_expr
*dynamic
= NULL
;
9886 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
9887 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
9890 lang_append_dynamic_list (&link_info
.dynamic_list
, dynamic
);
9893 /* Scan a space and/or comma separated string of features. */
9896 lang_ld_feature (char *str
)
9904 while (*p
== ',' || ISSPACE (*p
))
9909 while (*q
&& *q
!= ',' && !ISSPACE (*q
))
9913 if (strcasecmp (p
, "SANE_EXPR") == 0)
9914 config
.sane_expr
= true;
9916 einfo (_("%X%P: unknown feature `%s'\n"), p
);
9922 /* Pretty print memory amount. */
9925 lang_print_memory_size (uint64_t sz
)
9928 printf (" %10" PRIu64
" B", sz
);
9929 else if ((sz
& 0x3fffffff) == 0)
9930 printf ("%10" PRIu64
" GB", sz
>> 30);
9931 else if ((sz
& 0xfffff) == 0)
9932 printf ("%10" PRIu64
" MB", sz
>> 20);
9933 else if ((sz
& 0x3ff) == 0)
9934 printf ("%10" PRIu64
" KB", sz
>> 10);
9936 printf (" %10" PRIu64
" B", sz
);
9939 /* Implement --print-memory-usage: disply per region memory usage. */
9942 lang_print_memory_usage (void)
9944 lang_memory_region_type
*r
;
9946 printf ("Memory region Used Size Region Size %%age Used\n");
9947 for (r
= lang_memory_region_list
; r
->next
!= NULL
; r
= r
->next
)
9949 bfd_vma used_length
= r
->current
- r
->origin
;
9951 printf ("%16s: ",r
->name_list
.name
);
9952 lang_print_memory_size (used_length
);
9953 lang_print_memory_size (r
->length
);
9957 double percent
= used_length
* 100.0 / r
->length
;
9958 printf (" %6.2f%%", percent
);