1 /* Linker command language support.
2 Copyright (C) 1991-2023 Free Software Foundation, Inc.
4 This file is part of the GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
24 #include "libiberty.h"
25 #include "filenames.h"
26 #include "safe-ctype.h"
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. */
5473 if (output_section_statement
->subsection_alignment
!= NULL
)
5475 = exp_get_power (output_section_statement
->subsection_alignment
,
5476 output_section_statement
,
5477 "subsection alignment");
5479 if (o
->alignment_power
< i
->alignment_power
)
5480 o
->alignment_power
= i
->alignment_power
;
5482 alignment_needed
= align_power (dot
, i
->alignment_power
) - dot
;
5484 if (alignment_needed
!= 0)
5486 insert_pad (this_ptr
, fill
, TO_SIZE (alignment_needed
), o
, dot
);
5487 dot
+= alignment_needed
;
5490 if (link_info
.non_contiguous_regions
)
5492 /* If I would overflow O, let the caller remove I from the
5494 if (output_section_statement
->region
)
5496 bfd_vma end
= output_section_statement
->region
->origin
5497 + output_section_statement
->region
->length
;
5499 if (dot
+ TO_ADDR (i
->size
) > end
)
5501 if (i
->flags
& SEC_LINKER_CREATED
)
5502 einfo (_("%F%P: Output section `%pA' not large enough for "
5503 "the linker-created stubs section `%pA'.\n"),
5504 i
->output_section
, i
);
5506 if (i
->rawsize
&& i
->rawsize
!= i
->size
)
5507 einfo (_("%F%P: Relaxation not supported with "
5508 "--enable-non-contiguous-regions (section `%pA' "
5509 "would overflow `%pA' after it changed size).\n"),
5510 i
, i
->output_section
);
5514 i
->output_section
= NULL
;
5520 /* Remember where in the output section this input section goes. */
5521 i
->output_offset
= dot
- o
->vma
;
5523 /* Mark how big the output section must be to contain this now. */
5524 dot
+= TO_ADDR (i
->size
);
5525 if (!(o
->flags
& SEC_FIXED_SIZE
))
5526 o
->size
= TO_SIZE (dot
- o
->vma
);
5528 if (link_info
.non_contiguous_regions
)
5530 /* Record that I was successfully assigned to O, and update
5531 its actual output section too. */
5532 i
->already_assigned
= o
;
5533 i
->output_section
= o
;
5547 sort_sections_by_lma (const void *arg1
, const void *arg2
)
5549 const asection
*sec1
= ((const struct check_sec
*) arg1
)->sec
;
5550 const asection
*sec2
= ((const struct check_sec
*) arg2
)->sec
;
5552 if (sec1
->lma
< sec2
->lma
)
5554 else if (sec1
->lma
> sec2
->lma
)
5556 else if (sec1
->id
< sec2
->id
)
5558 else if (sec1
->id
> sec2
->id
)
5565 sort_sections_by_vma (const void *arg1
, const void *arg2
)
5567 const asection
*sec1
= ((const struct check_sec
*) arg1
)->sec
;
5568 const asection
*sec2
= ((const struct check_sec
*) arg2
)->sec
;
5570 if (sec1
->vma
< sec2
->vma
)
5572 else if (sec1
->vma
> sec2
->vma
)
5574 else if (sec1
->id
< sec2
->id
)
5576 else if (sec1
->id
> sec2
->id
)
5582 #define IS_TBSS(s) \
5583 ((s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == SEC_THREAD_LOCAL)
5585 #define IGNORE_SECTION(s) \
5586 ((s->flags & SEC_ALLOC) == 0 || IS_TBSS (s))
5588 /* Check to see if any allocated sections overlap with other allocated
5589 sections. This can happen if a linker script specifies the output
5590 section addresses of the two sections. Also check whether any memory
5591 region has overflowed. */
5594 lang_check_section_addresses (void)
5597 struct check_sec
*sections
;
5602 bfd_vma p_start
= 0;
5604 lang_memory_region_type
*m
;
5607 /* Detect address space overflow on allocated sections. */
5608 addr_mask
= ((bfd_vma
) 1 <<
5609 (bfd_arch_bits_per_address (link_info
.output_bfd
) - 1)) - 1;
5610 addr_mask
= (addr_mask
<< 1) + 1;
5611 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5612 if ((s
->flags
& SEC_ALLOC
) != 0)
5614 s_end
= (s
->vma
+ s
->size
) & addr_mask
;
5615 if (s_end
!= 0 && s_end
< (s
->vma
& addr_mask
))
5616 einfo (_("%X%P: section %s VMA wraps around address space\n"),
5620 s_end
= (s
->lma
+ s
->size
) & addr_mask
;
5621 if (s_end
!= 0 && s_end
< (s
->lma
& addr_mask
))
5622 einfo (_("%X%P: section %s LMA wraps around address space\n"),
5627 if (bfd_count_sections (link_info
.output_bfd
) <= 1)
5630 count
= bfd_count_sections (link_info
.output_bfd
);
5631 sections
= XNEWVEC (struct check_sec
, count
);
5633 /* Scan all sections in the output list. */
5635 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5637 if (IGNORE_SECTION (s
)
5641 sections
[count
].sec
= s
;
5642 sections
[count
].warned
= false;
5652 qsort (sections
, count
, sizeof (*sections
), sort_sections_by_lma
);
5654 /* First check section LMAs. There should be no overlap of LMAs on
5655 loadable sections, even with overlays. */
5656 for (p
= NULL
, i
= 0; i
< count
; i
++)
5658 s
= sections
[i
].sec
;
5660 if ((s
->flags
& SEC_LOAD
) != 0)
5663 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
5665 /* Look for an overlap. We have sorted sections by lma, so
5666 we know that s_start >= p_start. Besides the obvious
5667 case of overlap when the current section starts before
5668 the previous one ends, we also must have overlap if the
5669 previous section wraps around the address space. */
5671 && (s_start
<= p_end
5672 || p_end
< p_start
))
5674 einfo (_("%X%P: section %s LMA [%V,%V]"
5675 " overlaps section %s LMA [%V,%V]\n"),
5676 s
->name
, s_start
, s_end
, p
->name
, p_start
, p_end
);
5677 sections
[i
].warned
= true;
5685 /* If any non-zero size allocated section (excluding tbss) starts at
5686 exactly the same VMA as another such section, then we have
5687 overlays. Overlays generated by the OVERLAY keyword will have
5688 this property. It is possible to intentionally generate overlays
5689 that fail this test, but it would be unusual. */
5690 qsort (sections
, count
, sizeof (*sections
), sort_sections_by_vma
);
5692 p_start
= sections
[0].sec
->vma
;
5693 for (i
= 1; i
< count
; i
++)
5695 s_start
= sections
[i
].sec
->vma
;
5696 if (p_start
== s_start
)
5704 /* Now check section VMAs if no overlays were detected. */
5707 for (p
= NULL
, i
= 0; i
< count
; i
++)
5709 s
= sections
[i
].sec
;
5712 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
5715 && !sections
[i
].warned
5716 && (s_start
<= p_end
5717 || p_end
< p_start
))
5718 einfo (_("%X%P: section %s VMA [%V,%V]"
5719 " overlaps section %s VMA [%V,%V]\n"),
5720 s
->name
, s_start
, s_end
, p
->name
, p_start
, p_end
);
5729 /* If any memory region has overflowed, report by how much.
5730 We do not issue this diagnostic for regions that had sections
5731 explicitly placed outside their bounds; os_region_check's
5732 diagnostics are adequate for that case.
5734 FIXME: It is conceivable that m->current - (m->origin + m->length)
5735 might overflow a 32-bit integer. There is, alas, no way to print
5736 a bfd_vma quantity in decimal. */
5737 for (m
= lang_memory_region_list
; m
; m
= m
->next
)
5738 if (m
->had_full_message
)
5740 unsigned long over
= m
->current
- (m
->origin
+ m
->length
);
5741 einfo (ngettext ("%X%P: region `%s' overflowed by %lu byte\n",
5742 "%X%P: region `%s' overflowed by %lu bytes\n",
5744 m
->name_list
.name
, over
);
5748 /* Make sure the new address is within the region. We explicitly permit the
5749 current address to be at the exact end of the region when the address is
5750 non-zero, in case the region is at the end of addressable memory and the
5751 calculation wraps around. */
5754 os_region_check (lang_output_section_statement_type
*os
,
5755 lang_memory_region_type
*region
,
5759 if ((region
->current
< region
->origin
5760 || (region
->current
- region
->origin
> region
->length
))
5761 && ((region
->current
!= region
->origin
+ region
->length
)
5766 einfo (_("%X%P: address 0x%v of %pB section `%s'"
5767 " is not within region `%s'\n"),
5769 os
->bfd_section
->owner
,
5770 os
->bfd_section
->name
,
5771 region
->name_list
.name
);
5773 else if (!region
->had_full_message
)
5775 region
->had_full_message
= true;
5777 einfo (_("%X%P: %pB section `%s' will not fit in region `%s'\n"),
5778 os
->bfd_section
->owner
,
5779 os
->bfd_section
->name
,
5780 region
->name_list
.name
);
5786 ldlang_check_relro_region (lang_statement_union_type
*s
)
5788 seg_align_type
*seg
= &expld
.dataseg
;
5790 if (seg
->relro
== exp_seg_relro_start
)
5792 if (!seg
->relro_start_stat
)
5793 seg
->relro_start_stat
= s
;
5796 ASSERT (seg
->relro_start_stat
== s
);
5799 else if (seg
->relro
== exp_seg_relro_end
)
5801 if (!seg
->relro_end_stat
)
5802 seg
->relro_end_stat
= s
;
5805 ASSERT (seg
->relro_end_stat
== s
);
5810 /* Set the sizes for all the output sections. */
5813 lang_size_sections_1
5814 (lang_statement_union_type
**prev
,
5815 lang_output_section_statement_type
*current_os
,
5821 lang_statement_union_type
*s
;
5822 lang_statement_union_type
*prev_s
= NULL
;
5823 bool removed_prev_s
= false;
5824 lang_output_section_statement_type
*os
= current_os
;
5826 /* Size up the sections from their constituent parts. */
5827 for (s
= *prev
; s
!= NULL
; prev_s
= s
, s
= s
->header
.next
)
5829 bool removed
= false;
5831 switch (s
->header
.type
)
5833 case lang_output_section_statement_enum
:
5835 bfd_vma newdot
, after
, dotdelta
;
5836 lang_memory_region_type
*r
;
5837 int section_alignment
= 0;
5839 os
= &s
->output_section_statement
;
5840 init_opb (os
->bfd_section
);
5841 if (os
->constraint
== -1)
5844 /* FIXME: We shouldn't need to zero section vmas for ld -r
5845 here, in lang_insert_orphan, or in the default linker scripts.
5846 This is covering for coff backend linker bugs. See PR6945. */
5847 if (os
->addr_tree
== NULL
5848 && bfd_link_relocatable (&link_info
)
5849 && (bfd_get_flavour (link_info
.output_bfd
)
5850 == bfd_target_coff_flavour
))
5851 os
->addr_tree
= exp_intop (0);
5852 if (os
->addr_tree
!= NULL
)
5854 exp_fold_tree (os
->addr_tree
, os
, bfd_abs_section_ptr
, &dot
);
5856 if (expld
.result
.valid_p
)
5858 dot
= expld
.result
.value
;
5859 if (expld
.result
.section
!= NULL
)
5860 dot
+= expld
.result
.section
->vma
;
5862 else if (expld
.phase
!= lang_mark_phase_enum
)
5863 einfo (_("%F%P:%pS: non constant or forward reference"
5864 " address expression for section %s\n"),
5865 os
->addr_tree
, os
->name
);
5868 if (os
->bfd_section
== NULL
)
5869 /* This section was removed or never actually created. */
5872 /* If this is a COFF shared library section, use the size and
5873 address from the input section. FIXME: This is COFF
5874 specific; it would be cleaner if there were some other way
5875 to do this, but nothing simple comes to mind. */
5876 if (((bfd_get_flavour (link_info
.output_bfd
)
5877 == bfd_target_ecoff_flavour
)
5878 || (bfd_get_flavour (link_info
.output_bfd
)
5879 == bfd_target_coff_flavour
))
5880 && (os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
5884 if (os
->children
.head
== NULL
5885 || os
->children
.head
->header
.next
!= NULL
5886 || (os
->children
.head
->header
.type
5887 != lang_input_section_enum
))
5888 einfo (_("%X%P: internal error on COFF shared library"
5889 " section %s\n"), os
->name
);
5891 input
= os
->children
.head
->input_section
.section
;
5892 bfd_set_section_vma (os
->bfd_section
,
5893 bfd_section_vma (input
));
5894 if (!(os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
5895 os
->bfd_section
->size
= input
->size
;
5901 if (bfd_is_abs_section (os
->bfd_section
))
5903 /* No matter what happens, an abs section starts at zero. */
5904 ASSERT (os
->bfd_section
->vma
== 0);
5908 if (os
->addr_tree
== NULL
)
5910 /* No address specified for this section, get one
5911 from the region specification. */
5912 if (os
->region
== NULL
5913 || ((os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))
5914 && os
->region
->name_list
.name
[0] == '*'
5915 && strcmp (os
->region
->name_list
.name
,
5916 DEFAULT_MEMORY_REGION
) == 0))
5918 os
->region
= lang_memory_default (os
->bfd_section
);
5921 /* If a loadable section is using the default memory
5922 region, and some non default memory regions were
5923 defined, issue an error message. */
5925 && !IGNORE_SECTION (os
->bfd_section
)
5926 && !bfd_link_relocatable (&link_info
)
5928 && strcmp (os
->region
->name_list
.name
,
5929 DEFAULT_MEMORY_REGION
) == 0
5930 && lang_memory_region_list
!= NULL
5931 && (strcmp (lang_memory_region_list
->name_list
.name
,
5932 DEFAULT_MEMORY_REGION
) != 0
5933 || lang_memory_region_list
->next
!= NULL
)
5934 && lang_sizing_iteration
== 1)
5936 /* By default this is an error rather than just a
5937 warning because if we allocate the section to the
5938 default memory region we can end up creating an
5939 excessively large binary, or even seg faulting when
5940 attempting to perform a negative seek. See
5941 sources.redhat.com/ml/binutils/2003-04/msg00423.html
5942 for an example of this. This behaviour can be
5943 overridden by the using the --no-check-sections
5945 if (command_line
.check_section_addresses
)
5946 einfo (_("%F%P: error: no memory region specified"
5947 " for loadable section `%s'\n"),
5948 bfd_section_name (os
->bfd_section
));
5950 einfo (_("%P: warning: no memory region specified"
5951 " for loadable section `%s'\n"),
5952 bfd_section_name (os
->bfd_section
));
5955 newdot
= os
->region
->current
;
5956 section_alignment
= os
->bfd_section
->alignment_power
;
5959 section_alignment
= exp_get_power (os
->section_alignment
, os
,
5960 "section alignment");
5962 /* Align to what the section needs. */
5963 if (section_alignment
> 0)
5965 bfd_vma savedot
= newdot
;
5968 newdot
= align_power (newdot
, section_alignment
);
5969 dotdelta
= newdot
- savedot
;
5971 if (lang_sizing_iteration
== 1)
5973 else if (lang_sizing_iteration
> 1)
5975 /* Only report adjustments that would change
5976 alignment from what we have already reported. */
5977 diff
= newdot
- os
->bfd_section
->vma
;
5978 if (!(diff
& (((bfd_vma
) 1 << section_alignment
) - 1)))
5982 && (config
.warn_section_align
5983 || os
->addr_tree
!= NULL
))
5984 einfo (_("%P: warning: "
5985 "start of section %s changed by %ld\n"),
5986 os
->name
, (long) diff
);
5989 bfd_set_section_vma (os
->bfd_section
, newdot
);
5991 os
->bfd_section
->output_offset
= 0;
5994 lang_size_sections_1 (&os
->children
.head
, os
,
5995 os
->fill
, newdot
, relax
, check_regions
);
5997 os
->processed_vma
= true;
5999 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
6000 /* Except for some special linker created sections,
6001 no output section should change from zero size
6002 after strip_excluded_output_sections. A non-zero
6003 size on an ignored section indicates that some
6004 input section was not sized early enough. */
6005 ASSERT (os
->bfd_section
->size
== 0);
6008 dot
= os
->bfd_section
->vma
;
6010 /* Put the section within the requested block size, or
6011 align at the block boundary. */
6013 + TO_ADDR (os
->bfd_section
->size
)
6014 + os
->block_value
- 1)
6015 & - (bfd_vma
) os
->block_value
);
6017 if (!(os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
6018 os
->bfd_section
->size
= TO_SIZE (after
6019 - os
->bfd_section
->vma
);
6022 /* Set section lma. */
6025 r
= lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, false);
6029 bfd_vma lma
= exp_get_abs_int (os
->load_base
, 0, "load base");
6030 os
->bfd_section
->lma
= lma
;
6032 else if (os
->lma_region
!= NULL
)
6034 bfd_vma lma
= os
->lma_region
->current
;
6036 if (os
->align_lma_with_input
)
6040 /* When LMA_REGION is the same as REGION, align the LMA
6041 as we did for the VMA, possibly including alignment
6042 from the bfd section. If a different region, then
6043 only align according to the value in the output
6045 if (os
->lma_region
!= os
->region
)
6046 section_alignment
= exp_get_power (os
->section_alignment
,
6048 "section alignment");
6049 if (section_alignment
> 0)
6050 lma
= align_power (lma
, section_alignment
);
6052 os
->bfd_section
->lma
= lma
;
6054 else if (r
->last_os
!= NULL
6055 && (os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
6060 last
= r
->last_os
->output_section_statement
.bfd_section
;
6062 /* A backwards move of dot should be accompanied by
6063 an explicit assignment to the section LMA (ie.
6064 os->load_base set) because backwards moves can
6065 create overlapping LMAs. */
6067 && os
->bfd_section
->size
!= 0
6068 && dot
+ TO_ADDR (os
->bfd_section
->size
) <= last
->vma
)
6070 /* If dot moved backwards then leave lma equal to
6071 vma. This is the old default lma, which might
6072 just happen to work when the backwards move is
6073 sufficiently large. Nag if this changes anything,
6074 so people can fix their linker scripts. */
6076 if (last
->vma
!= last
->lma
)
6077 einfo (_("%P: warning: dot moved backwards "
6078 "before `%s'\n"), os
->name
);
6082 /* If this is an overlay, set the current lma to that
6083 at the end of the previous section. */
6084 if (os
->sectype
== overlay_section
)
6085 lma
= last
->lma
+ TO_ADDR (last
->size
);
6087 /* Otherwise, keep the same lma to vma relationship
6088 as the previous section. */
6090 lma
= os
->bfd_section
->vma
+ last
->lma
- last
->vma
;
6092 if (section_alignment
> 0)
6093 lma
= align_power (lma
, section_alignment
);
6094 os
->bfd_section
->lma
= lma
;
6097 os
->processed_lma
= true;
6099 /* Keep track of normal sections using the default
6100 lma region. We use this to set the lma for
6101 following sections. Overlays or other linker
6102 script assignment to lma might mean that the
6103 default lma == vma is incorrect.
6104 To avoid warnings about dot moving backwards when using
6105 -Ttext, don't start tracking sections until we find one
6106 of non-zero size or with lma set differently to vma.
6107 Do this tracking before we short-cut the loop so that we
6108 track changes for the case where the section size is zero,
6109 but the lma is set differently to the vma. This is
6110 important, if an orphan section is placed after an
6111 otherwise empty output section that has an explicit lma
6112 set, we want that lma reflected in the orphans lma. */
6113 if (((!IGNORE_SECTION (os
->bfd_section
)
6114 && (os
->bfd_section
->size
!= 0
6115 || (r
->last_os
== NULL
6116 && os
->bfd_section
->vma
!= os
->bfd_section
->lma
)
6117 || (r
->last_os
!= NULL
6118 && dot
>= (r
->last_os
->output_section_statement
6119 .bfd_section
->vma
))))
6120 || os
->sectype
== first_overlay_section
)
6121 && os
->lma_region
== NULL
6122 && !bfd_link_relocatable (&link_info
))
6125 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
6128 /* .tbss sections effectively have zero size. */
6129 if (!IS_TBSS (os
->bfd_section
)
6130 || bfd_link_relocatable (&link_info
))
6131 dotdelta
= TO_ADDR (os
->bfd_section
->size
);
6136 if (os
->update_dot_tree
!= 0)
6137 exp_fold_tree (os
->update_dot_tree
, os
, bfd_abs_section_ptr
, &dot
);
6139 /* Update dot in the region ?
6140 We only do this if the section is going to be allocated,
6141 since unallocated sections do not contribute to the region's
6142 overall size in memory. */
6143 if (os
->region
!= NULL
6144 && (os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
)))
6146 os
->region
->current
= dot
;
6149 /* Make sure the new address is within the region. */
6150 os_region_check (os
, os
->region
, os
->addr_tree
,
6151 os
->bfd_section
->vma
);
6153 if (os
->lma_region
!= NULL
&& os
->lma_region
!= os
->region
6154 && ((os
->bfd_section
->flags
& SEC_LOAD
)
6155 || os
->align_lma_with_input
))
6157 os
->lma_region
->current
= os
->bfd_section
->lma
+ dotdelta
;
6160 os_region_check (os
, os
->lma_region
, NULL
,
6161 os
->bfd_section
->lma
);
6167 case lang_constructors_statement_enum
:
6168 dot
= lang_size_sections_1 (&constructor_list
.head
, current_os
,
6169 fill
, dot
, relax
, check_regions
);
6172 case lang_data_statement_enum
:
6174 unsigned int size
= 0;
6176 s
->data_statement
.output_offset
= dot
- current_os
->bfd_section
->vma
;
6177 s
->data_statement
.output_section
= current_os
->bfd_section
;
6179 /* We might refer to provided symbols in the expression, and
6180 need to mark them as needed. */
6181 exp_fold_tree (s
->data_statement
.exp
, os
,
6182 bfd_abs_section_ptr
, &dot
);
6184 switch (s
->data_statement
.type
)
6202 if (size
< TO_SIZE ((unsigned) 1))
6203 size
= TO_SIZE ((unsigned) 1);
6204 dot
+= TO_ADDR (size
);
6205 if (!(current_os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
6206 current_os
->bfd_section
->size
6207 = TO_SIZE (dot
- current_os
->bfd_section
->vma
);
6212 case lang_reloc_statement_enum
:
6216 s
->reloc_statement
.output_offset
6217 = dot
- current_os
->bfd_section
->vma
;
6218 s
->reloc_statement
.output_section
6219 = current_os
->bfd_section
;
6220 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
6221 dot
+= TO_ADDR (size
);
6222 if (!(current_os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
6223 current_os
->bfd_section
->size
6224 = TO_SIZE (dot
- current_os
->bfd_section
->vma
);
6228 case lang_wild_statement_enum
:
6229 dot
= lang_size_sections_1 (&s
->wild_statement
.children
.head
,
6230 current_os
, fill
, dot
, relax
,
6234 case lang_object_symbols_statement_enum
:
6235 link_info
.create_object_symbols_section
= current_os
->bfd_section
;
6236 current_os
->bfd_section
->flags
|= SEC_KEEP
;
6239 case lang_output_statement_enum
:
6240 case lang_target_statement_enum
:
6243 case lang_input_section_enum
:
6247 i
= s
->input_section
.section
;
6252 if (!bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
6253 einfo (_("%F%P: can't relax section: %E\n"));
6257 dot
= size_input_section (prev
, current_os
, fill
, &removed
, dot
);
6261 case lang_input_statement_enum
:
6264 case lang_fill_statement_enum
:
6265 s
->fill_statement
.output_section
= current_os
->bfd_section
;
6267 fill
= s
->fill_statement
.fill
;
6270 case lang_assignment_statement_enum
:
6272 bfd_vma newdot
= dot
;
6273 etree_type
*tree
= s
->assignment_statement
.exp
;
6275 expld
.dataseg
.relro
= exp_seg_relro_none
;
6277 exp_fold_tree (tree
, os
, current_os
->bfd_section
, &newdot
);
6279 ldlang_check_relro_region (s
);
6281 expld
.dataseg
.relro
= exp_seg_relro_none
;
6283 /* This symbol may be relative to this section. */
6284 if ((tree
->type
.node_class
== etree_provided
6285 || tree
->type
.node_class
== etree_assign
)
6286 && (tree
->assign
.dst
[0] != '.'
6287 || tree
->assign
.dst
[1] != '\0'))
6288 current_os
->update_dot
= 1;
6290 if (!current_os
->ignored
)
6292 if (current_os
== abs_output_section
)
6294 /* If we don't have an output section, then just adjust
6295 the default memory address. */
6296 lang_memory_region_lookup (DEFAULT_MEMORY_REGION
,
6297 false)->current
= newdot
;
6299 else if (newdot
!= dot
)
6301 /* Insert a pad after this statement. We can't
6302 put the pad before when relaxing, in case the
6303 assignment references dot. */
6304 insert_pad (&s
->header
.next
, fill
, TO_SIZE (newdot
- dot
),
6305 current_os
->bfd_section
, dot
);
6307 /* Don't neuter the pad below when relaxing. */
6310 /* If dot is advanced, this implies that the section
6311 should have space allocated to it, unless the
6312 user has explicitly stated that the section
6313 should not be allocated. */
6314 if (current_os
->sectype
!= noalloc_section
6315 && (current_os
->sectype
!= noload_section
6316 || (bfd_get_flavour (link_info
.output_bfd
)
6317 == bfd_target_elf_flavour
)))
6318 current_os
->bfd_section
->flags
|= SEC_ALLOC
;
6325 case lang_padding_statement_enum
:
6326 /* If this is the first time lang_size_sections is called,
6327 we won't have any padding statements. If this is the
6328 second or later passes when relaxing, we should allow
6329 padding to shrink. If padding is needed on this pass, it
6330 will be added back in. */
6331 s
->padding_statement
.size
= 0;
6333 /* Make sure output_offset is valid. If relaxation shrinks
6334 the section and this pad isn't needed, it's possible to
6335 have output_offset larger than the final size of the
6336 section. bfd_set_section_contents will complain even for
6337 a pad size of zero. */
6338 s
->padding_statement
.output_offset
6339 = dot
- current_os
->bfd_section
->vma
;
6342 case lang_group_statement_enum
:
6343 dot
= lang_size_sections_1 (&s
->group_statement
.children
.head
,
6344 current_os
, fill
, dot
, relax
,
6348 case lang_insert_statement_enum
:
6351 /* We can only get here when relaxing is turned on. */
6352 case lang_address_statement_enum
:
6360 /* If an input section doesn't fit in the current output
6361 section, remove it from the list. Handle the case where we
6362 have to remove an input_section statement here: there is a
6363 special case to remove the first element of the list. */
6364 if (link_info
.non_contiguous_regions
&& removed
)
6366 /* If we removed the first element during the previous
6367 iteration, override the loop assignment of prev_s. */
6373 /* If there was a real previous input section, just skip
6375 prev_s
->header
.next
=s
->header
.next
;
6377 removed_prev_s
= false;
6381 /* Remove the first input section of the list. */
6382 *prev
= s
->header
.next
;
6383 removed_prev_s
= true;
6386 /* Move to next element, unless we removed the head of the
6388 if (!removed_prev_s
)
6389 prev
= &s
->header
.next
;
6393 prev
= &s
->header
.next
;
6394 removed_prev_s
= false;
6400 /* Callback routine that is used in _bfd_elf_map_sections_to_segments.
6401 The BFD library has set NEW_SEGMENT to TRUE iff it thinks that
6402 CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different
6403 segments. We are allowed an opportunity to override this decision. */
6406 ldlang_override_segment_assignment (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6407 bfd
*abfd ATTRIBUTE_UNUSED
,
6408 asection
*current_section
,
6409 asection
*previous_section
,
6412 lang_output_section_statement_type
*cur
;
6413 lang_output_section_statement_type
*prev
;
6415 /* The checks below are only necessary when the BFD library has decided
6416 that the two sections ought to be placed into the same segment. */
6420 /* Paranoia checks. */
6421 if (current_section
== NULL
|| previous_section
== NULL
)
6424 /* If this flag is set, the target never wants code and non-code
6425 sections comingled in the same segment. */
6426 if (config
.separate_code
6427 && ((current_section
->flags
^ previous_section
->flags
) & SEC_CODE
))
6430 /* Find the memory regions associated with the two sections.
6431 We call lang_output_section_find() here rather than scanning the list
6432 of output sections looking for a matching section pointer because if
6433 we have a large number of sections then a hash lookup is faster. */
6434 cur
= lang_output_section_find (current_section
->name
);
6435 prev
= lang_output_section_find (previous_section
->name
);
6437 /* More paranoia. */
6438 if (cur
== NULL
|| prev
== NULL
)
6441 /* If the regions are different then force the sections to live in
6442 different segments. See the email thread starting at the following
6443 URL for the reasons why this is necessary:
6444 http://sourceware.org/ml/binutils/2007-02/msg00216.html */
6445 return cur
->region
!= prev
->region
;
6449 one_lang_size_sections_pass (bool *relax
, bool check_regions
)
6451 lang_statement_iteration
++;
6452 if (expld
.phase
!= lang_mark_phase_enum
)
6453 lang_sizing_iteration
++;
6454 lang_size_sections_1 (&statement_list
.head
, abs_output_section
,
6455 0, 0, relax
, check_regions
);
6459 lang_size_segment (void)
6461 /* If XXX_SEGMENT_ALIGN XXX_SEGMENT_END pair was seen, check whether
6462 a page could be saved in the data segment. */
6463 seg_align_type
*seg
= &expld
.dataseg
;
6464 bfd_vma first
, last
;
6466 first
= -seg
->base
& (seg
->commonpagesize
- 1);
6467 last
= seg
->end
& (seg
->commonpagesize
- 1);
6469 && ((seg
->base
& ~(seg
->commonpagesize
- 1))
6470 != (seg
->end
& ~(seg
->commonpagesize
- 1)))
6471 && first
+ last
<= seg
->commonpagesize
)
6473 seg
->phase
= exp_seg_adjust
;
6477 seg
->phase
= exp_seg_done
;
6482 lang_size_relro_segment_1 (void)
6484 seg_align_type
*seg
= &expld
.dataseg
;
6485 bfd_vma relro_end
, desired_end
;
6488 /* Compute the expected PT_GNU_RELRO/PT_LOAD segment end. */
6489 relro_end
= (seg
->relro_end
+ seg
->relropagesize
- 1) & -seg
->relropagesize
;
6491 /* Adjust by the offset arg of XXX_SEGMENT_RELRO_END. */
6492 desired_end
= relro_end
- seg
->relro_offset
;
6494 /* For sections in the relro segment.. */
6495 for (sec
= link_info
.output_bfd
->section_last
; sec
; sec
= sec
->prev
)
6496 if ((sec
->flags
& SEC_ALLOC
) != 0
6497 && sec
->vma
>= seg
->base
6498 && sec
->vma
< seg
->relro_end
- seg
->relro_offset
)
6500 /* Where do we want to put this section so that it ends as
6502 bfd_vma start
, end
, bump
;
6504 end
= start
= sec
->vma
;
6506 end
+= TO_ADDR (sec
->size
);
6507 bump
= desired_end
- end
;
6508 /* We'd like to increase START by BUMP, but we must heed
6509 alignment so the increase might be less than optimum. */
6511 start
&= ~(((bfd_vma
) 1 << sec
->alignment_power
) - 1);
6512 /* This is now the desired end for the previous section. */
6513 desired_end
= start
;
6516 seg
->phase
= exp_seg_relro_adjust
;
6517 ASSERT (desired_end
>= seg
->base
);
6518 seg
->base
= desired_end
;
6523 lang_size_relro_segment (bool *relax
, bool check_regions
)
6525 bool do_reset
= false;
6527 if (link_info
.relro
&& expld
.dataseg
.relro_end
)
6529 bfd_vma data_initial_base
= expld
.dataseg
.base
;
6530 bfd_vma data_relro_end
= lang_size_relro_segment_1 ();
6532 lang_reset_memory_regions ();
6533 one_lang_size_sections_pass (relax
, check_regions
);
6535 /* Assignments to dot, or to output section address in a user
6536 script have increased padding over the original. Revert. */
6537 if (expld
.dataseg
.relro_end
> data_relro_end
)
6539 expld
.dataseg
.base
= data_initial_base
;
6543 else if (lang_size_segment ())
6550 lang_size_sections (bool *relax
, bool check_regions
)
6552 expld
.phase
= lang_allocating_phase_enum
;
6553 expld
.dataseg
.phase
= exp_seg_none
;
6555 one_lang_size_sections_pass (relax
, check_regions
);
6557 if (expld
.dataseg
.phase
!= exp_seg_end_seen
)
6558 expld
.dataseg
.phase
= exp_seg_done
;
6560 if (expld
.dataseg
.phase
== exp_seg_end_seen
)
6563 = lang_size_relro_segment (relax
, check_regions
);
6567 lang_reset_memory_regions ();
6568 one_lang_size_sections_pass (relax
, check_regions
);
6571 if (link_info
.relro
&& expld
.dataseg
.relro_end
)
6573 link_info
.relro_start
= expld
.dataseg
.base
;
6574 link_info
.relro_end
= expld
.dataseg
.relro_end
;
6579 static lang_output_section_statement_type
*current_section
;
6580 static lang_assignment_statement_type
*current_assign
;
6581 static bool prefer_next_section
;
6583 /* Worker function for lang_do_assignments. Recursiveness goes here. */
6586 lang_do_assignments_1 (lang_statement_union_type
*s
,
6587 lang_output_section_statement_type
*current_os
,
6592 lang_output_section_statement_type
*os
= current_os
;
6594 for (; s
!= NULL
; s
= s
->header
.next
)
6596 switch (s
->header
.type
)
6598 case lang_constructors_statement_enum
:
6599 dot
= lang_do_assignments_1 (constructor_list
.head
,
6600 current_os
, fill
, dot
, found_end
);
6603 case lang_output_section_statement_enum
:
6607 os
= &s
->output_section_statement
;
6608 os
->after_end
= *found_end
;
6609 init_opb (os
->bfd_section
);
6611 if (os
->bfd_section
!= NULL
)
6613 if (!os
->ignored
&& (os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
6615 current_section
= os
;
6616 prefer_next_section
= false;
6618 newdot
= os
->bfd_section
->vma
;
6620 newdot
= lang_do_assignments_1 (os
->children
.head
,
6621 os
, os
->fill
, newdot
, found_end
);
6624 if (os
->bfd_section
!= NULL
)
6626 newdot
= os
->bfd_section
->vma
;
6628 /* .tbss sections effectively have zero size. */
6629 if (!IS_TBSS (os
->bfd_section
)
6630 || bfd_link_relocatable (&link_info
))
6631 newdot
+= TO_ADDR (os
->bfd_section
->size
);
6633 if (os
->update_dot_tree
!= NULL
)
6634 exp_fold_tree (os
->update_dot_tree
, os
,
6635 bfd_abs_section_ptr
, &newdot
);
6642 case lang_wild_statement_enum
:
6644 dot
= lang_do_assignments_1 (s
->wild_statement
.children
.head
,
6645 current_os
, fill
, dot
, found_end
);
6648 case lang_object_symbols_statement_enum
:
6649 case lang_output_statement_enum
:
6650 case lang_target_statement_enum
:
6653 case lang_data_statement_enum
:
6654 exp_fold_tree (s
->data_statement
.exp
, os
, bfd_abs_section_ptr
, &dot
);
6655 if (expld
.result
.valid_p
)
6657 s
->data_statement
.value
= expld
.result
.value
;
6658 if (expld
.result
.section
!= NULL
)
6659 s
->data_statement
.value
+= expld
.result
.section
->vma
;
6661 else if (expld
.phase
== lang_final_phase_enum
)
6662 einfo (_("%F%P: invalid data statement\n"));
6665 switch (s
->data_statement
.type
)
6683 if (size
< TO_SIZE ((unsigned) 1))
6684 size
= TO_SIZE ((unsigned) 1);
6685 dot
+= TO_ADDR (size
);
6689 case lang_reloc_statement_enum
:
6690 exp_fold_tree (s
->reloc_statement
.addend_exp
, os
,
6691 bfd_abs_section_ptr
, &dot
);
6692 if (expld
.result
.valid_p
)
6693 s
->reloc_statement
.addend_value
= expld
.result
.value
;
6694 else if (expld
.phase
== lang_final_phase_enum
)
6695 einfo (_("%F%P: invalid reloc statement\n"));
6696 dot
+= TO_ADDR (bfd_get_reloc_size (s
->reloc_statement
.howto
));
6699 case lang_input_section_enum
:
6701 asection
*in
= s
->input_section
.section
;
6703 if ((in
->flags
& SEC_EXCLUDE
) == 0)
6704 dot
+= TO_ADDR (in
->size
);
6708 case lang_input_statement_enum
:
6711 case lang_fill_statement_enum
:
6712 fill
= s
->fill_statement
.fill
;
6715 case lang_assignment_statement_enum
:
6716 current_assign
= &s
->assignment_statement
;
6717 if (current_assign
->exp
->type
.node_class
!= etree_assert
)
6719 const char *p
= current_assign
->exp
->assign
.dst
;
6721 if (current_os
== abs_output_section
&& p
[0] == '.' && p
[1] == 0)
6722 prefer_next_section
= true;
6726 if (strcmp (p
, "end") == 0)
6729 exp_fold_tree (s
->assignment_statement
.exp
, os
,
6730 (current_os
->bfd_section
!= NULL
6731 ? current_os
->bfd_section
: bfd_und_section_ptr
),
6735 case lang_padding_statement_enum
:
6736 dot
+= TO_ADDR (s
->padding_statement
.size
);
6739 case lang_group_statement_enum
:
6740 dot
= lang_do_assignments_1 (s
->group_statement
.children
.head
,
6741 current_os
, fill
, dot
, found_end
);
6744 case lang_insert_statement_enum
:
6747 case lang_address_statement_enum
:
6759 lang_do_assignments (lang_phase_type phase
)
6761 bool found_end
= false;
6763 current_section
= NULL
;
6764 prefer_next_section
= false;
6765 expld
.phase
= phase
;
6766 lang_statement_iteration
++;
6767 lang_do_assignments_1 (statement_list
.head
,
6768 abs_output_section
, NULL
, 0, &found_end
);
6771 /* For an assignment statement outside of an output section statement,
6772 choose the best of neighbouring output sections to use for values
6776 section_for_dot (void)
6780 /* Assignments belong to the previous output section, unless there
6781 has been an assignment to "dot", in which case following
6782 assignments belong to the next output section. (The assumption
6783 is that an assignment to "dot" is setting up the address for the
6784 next output section.) Except that past the assignment to "_end"
6785 we always associate with the previous section. This exception is
6786 for targets like SH that define an alloc .stack or other
6787 weirdness after non-alloc sections. */
6788 if (current_section
== NULL
|| prefer_next_section
)
6790 lang_statement_union_type
*stmt
;
6791 lang_output_section_statement_type
*os
;
6793 for (stmt
= (lang_statement_union_type
*) current_assign
;
6795 stmt
= stmt
->header
.next
)
6796 if (stmt
->header
.type
== lang_output_section_statement_enum
)
6799 os
= stmt
? &stmt
->output_section_statement
: NULL
;
6802 && (os
->bfd_section
== NULL
6803 || (os
->bfd_section
->flags
& SEC_EXCLUDE
) != 0
6804 || bfd_section_removed_from_list (link_info
.output_bfd
,
6808 if (current_section
== NULL
|| os
== NULL
|| !os
->after_end
)
6811 s
= os
->bfd_section
;
6813 s
= link_info
.output_bfd
->section_last
;
6815 && ((s
->flags
& SEC_ALLOC
) == 0
6816 || (s
->flags
& SEC_THREAD_LOCAL
) != 0))
6821 return bfd_abs_section_ptr
;
6825 s
= current_section
->bfd_section
;
6827 /* The section may have been stripped. */
6829 && ((s
->flags
& SEC_EXCLUDE
) != 0
6830 || (s
->flags
& SEC_ALLOC
) == 0
6831 || (s
->flags
& SEC_THREAD_LOCAL
) != 0
6832 || bfd_section_removed_from_list (link_info
.output_bfd
, s
)))
6835 s
= link_info
.output_bfd
->sections
;
6837 && ((s
->flags
& SEC_ALLOC
) == 0
6838 || (s
->flags
& SEC_THREAD_LOCAL
) != 0))
6843 return bfd_abs_section_ptr
;
6846 /* Array of __start/__stop/.startof./.sizeof/ symbols. */
6848 static struct bfd_link_hash_entry
**start_stop_syms
;
6849 static size_t start_stop_count
= 0;
6850 static size_t start_stop_alloc
= 0;
6852 /* Give start/stop SYMBOL for SEC a preliminary definition, and add it
6853 to start_stop_syms. */
6856 lang_define_start_stop (const char *symbol
, asection
*sec
)
6858 struct bfd_link_hash_entry
*h
;
6860 h
= bfd_define_start_stop (link_info
.output_bfd
, &link_info
, symbol
, sec
);
6863 if (start_stop_count
== start_stop_alloc
)
6865 start_stop_alloc
= 2 * start_stop_alloc
+ 10;
6867 = xrealloc (start_stop_syms
,
6868 start_stop_alloc
* sizeof (*start_stop_syms
));
6870 start_stop_syms
[start_stop_count
++] = h
;
6874 /* Check for input sections whose names match references to
6875 __start_SECNAME or __stop_SECNAME symbols. Give the symbols
6876 preliminary definitions. */
6879 lang_init_start_stop (void)
6883 char leading_char
= bfd_get_symbol_leading_char (link_info
.output_bfd
);
6885 for (abfd
= link_info
.input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
6886 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
6889 const char *secname
= s
->name
;
6891 for (ps
= secname
; *ps
!= '\0'; ps
++)
6892 if (!ISALNUM ((unsigned char) *ps
) && *ps
!= '_')
6896 char *symbol
= (char *) xmalloc (10 + strlen (secname
));
6898 symbol
[0] = leading_char
;
6899 sprintf (symbol
+ (leading_char
!= 0), "__start_%s", secname
);
6900 lang_define_start_stop (symbol
, s
);
6902 symbol
[1] = leading_char
;
6903 memcpy (symbol
+ 1 + (leading_char
!= 0), "__stop", 6);
6904 lang_define_start_stop (symbol
+ 1, s
);
6911 /* Iterate over start_stop_syms. */
6914 foreach_start_stop (void (*func
) (struct bfd_link_hash_entry
*))
6918 for (i
= 0; i
< start_stop_count
; ++i
)
6919 func (start_stop_syms
[i
]);
6922 /* __start and __stop symbols are only supposed to be defined by the
6923 linker for orphan sections, but we now extend that to sections that
6924 map to an output section of the same name. The symbols were
6925 defined early for --gc-sections, before we mapped input to output
6926 sections, so undo those that don't satisfy this rule. */
6929 undef_start_stop (struct bfd_link_hash_entry
*h
)
6931 if (h
->ldscript_def
)
6934 if (h
->u
.def
.section
->output_section
== NULL
6935 || h
->u
.def
.section
->output_section
->owner
!= link_info
.output_bfd
6936 || strcmp (h
->u
.def
.section
->name
,
6937 h
->u
.def
.section
->output_section
->name
) != 0)
6939 asection
*sec
= bfd_get_section_by_name (link_info
.output_bfd
,
6940 h
->u
.def
.section
->name
);
6943 /* When there are more than one input sections with the same
6944 section name, SECNAME, linker picks the first one to define
6945 __start_SECNAME and __stop_SECNAME symbols. When the first
6946 input section is removed by comdat group, we need to check
6947 if there is still an output section with section name
6950 for (i
= sec
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
6951 if (strcmp (h
->u
.def
.section
->name
, i
->name
) == 0)
6953 h
->u
.def
.section
= i
;
6957 h
->type
= bfd_link_hash_undefined
;
6958 h
->u
.undef
.abfd
= NULL
;
6959 if (is_elf_hash_table (link_info
.hash
))
6961 const struct elf_backend_data
*bed
;
6962 struct elf_link_hash_entry
*eh
= (struct elf_link_hash_entry
*) h
;
6963 unsigned int was_forced
= eh
->forced_local
;
6965 bed
= get_elf_backend_data (link_info
.output_bfd
);
6966 (*bed
->elf_backend_hide_symbol
) (&link_info
, eh
, true);
6967 if (!eh
->ref_regular_nonweak
)
6968 h
->type
= bfd_link_hash_undefweak
;
6969 eh
->def_regular
= 0;
6970 eh
->forced_local
= was_forced
;
6976 lang_undef_start_stop (void)
6978 foreach_start_stop (undef_start_stop
);
6981 /* Check for output sections whose names match references to
6982 .startof.SECNAME or .sizeof.SECNAME symbols. Give the symbols
6983 preliminary definitions. */
6986 lang_init_startof_sizeof (void)
6990 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6992 const char *secname
= s
->name
;
6993 char *symbol
= (char *) xmalloc (10 + strlen (secname
));
6995 sprintf (symbol
, ".startof.%s", secname
);
6996 lang_define_start_stop (symbol
, s
);
6998 memcpy (symbol
+ 1, ".size", 5);
6999 lang_define_start_stop (symbol
+ 1, s
);
7004 /* Set .startof., .sizeof., __start and __stop symbols final values. */
7007 set_start_stop (struct bfd_link_hash_entry
*h
)
7010 || h
->type
!= bfd_link_hash_defined
)
7013 if (h
->root
.string
[0] == '.')
7015 /* .startof. or .sizeof. symbol.
7016 .startof. already has final value. */
7017 if (h
->root
.string
[2] == 'i')
7020 h
->u
.def
.value
= TO_ADDR (h
->u
.def
.section
->size
);
7021 h
->u
.def
.section
= bfd_abs_section_ptr
;
7026 /* __start or __stop symbol. */
7027 int has_lead
= bfd_get_symbol_leading_char (link_info
.output_bfd
) != 0;
7029 h
->u
.def
.section
= h
->u
.def
.section
->output_section
;
7030 if (h
->root
.string
[4 + has_lead
] == 'o')
7033 h
->u
.def
.value
= TO_ADDR (h
->u
.def
.section
->size
);
7039 lang_finalize_start_stop (void)
7041 foreach_start_stop (set_start_stop
);
7045 lang_symbol_tweaks (void)
7047 /* Give initial values for __start and __stop symbols, so that ELF
7048 gc_sections will keep sections referenced by these symbols. Must
7049 be done before lang_do_assignments. */
7050 if (config
.build_constructors
)
7051 lang_init_start_stop ();
7053 /* Make __ehdr_start hidden, and set def_regular even though it is
7054 likely undefined at this stage. For lang_check_relocs. */
7055 if (is_elf_hash_table (link_info
.hash
)
7056 && !bfd_link_relocatable (&link_info
))
7058 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*)
7059 bfd_link_hash_lookup (link_info
.hash
, "__ehdr_start",
7060 false, false, true);
7062 /* Only adjust the export class if the symbol was referenced
7063 and not defined, otherwise leave it alone. */
7065 && (h
->root
.type
== bfd_link_hash_new
7066 || h
->root
.type
== bfd_link_hash_undefined
7067 || h
->root
.type
== bfd_link_hash_undefweak
7068 || h
->root
.type
== bfd_link_hash_common
))
7070 const struct elf_backend_data
*bed
;
7071 bed
= get_elf_backend_data (link_info
.output_bfd
);
7072 (*bed
->elf_backend_hide_symbol
) (&link_info
, h
, true);
7073 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
7074 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
7076 h
->root
.linker_def
= 1;
7077 h
->root
.rel_from_abs
= 1;
7085 struct bfd_link_hash_entry
*h
;
7088 if ((bfd_link_relocatable (&link_info
) && !link_info
.gc_sections
)
7089 || bfd_link_dll (&link_info
))
7090 warn
= entry_from_cmdline
;
7094 /* Force the user to specify a root when generating a relocatable with
7095 --gc-sections, unless --gc-keep-exported was also given. */
7096 if (bfd_link_relocatable (&link_info
)
7097 && link_info
.gc_sections
7098 && !link_info
.gc_keep_exported
)
7100 struct bfd_sym_chain
*sym
;
7102 for (sym
= link_info
.gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
7104 h
= bfd_link_hash_lookup (link_info
.hash
, sym
->name
,
7105 false, false, false);
7107 && (h
->type
== bfd_link_hash_defined
7108 || h
->type
== bfd_link_hash_defweak
)
7109 && !bfd_is_const_section (h
->u
.def
.section
))
7113 einfo (_("%F%P: --gc-sections requires a defined symbol root "
7114 "specified by -e or -u\n"));
7117 if (entry_symbol
.name
== NULL
)
7119 /* No entry has been specified. Look for the default entry, but
7120 don't warn if we don't find it. */
7121 entry_symbol
.name
= entry_symbol_default
;
7125 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
.name
,
7126 false, false, true);
7128 && (h
->type
== bfd_link_hash_defined
7129 || h
->type
== bfd_link_hash_defweak
)
7130 && h
->u
.def
.section
->output_section
!= NULL
)
7134 val
= (h
->u
.def
.value
7135 + bfd_section_vma (h
->u
.def
.section
->output_section
)
7136 + h
->u
.def
.section
->output_offset
);
7137 if (!bfd_set_start_address (link_info
.output_bfd
, val
))
7138 einfo (_("%F%P: %s: can't set start address\n"), entry_symbol
.name
);
7145 /* We couldn't find the entry symbol. Try parsing it as a
7147 val
= bfd_scan_vma (entry_symbol
.name
, &send
, 0);
7150 if (!bfd_set_start_address (link_info
.output_bfd
, val
))
7151 einfo (_("%F%P: can't set start address\n"));
7153 /* BZ 2004952: Only use the start of the entry section for executables. */
7154 else if bfd_link_executable (&link_info
)
7158 /* Can't find the entry symbol, and it's not a number. Use
7159 the first address in the text section. */
7160 ts
= bfd_get_section_by_name (link_info
.output_bfd
, entry_section
);
7164 einfo (_("%P: warning: cannot find entry symbol %s;"
7165 " defaulting to %V\n"),
7167 bfd_section_vma (ts
));
7168 if (!bfd_set_start_address (link_info
.output_bfd
,
7169 bfd_section_vma (ts
)))
7170 einfo (_("%F%P: can't set start address\n"));
7175 einfo (_("%P: warning: cannot find entry symbol %s;"
7176 " not setting start address\n"),
7183 einfo (_("%P: warning: cannot find entry symbol %s;"
7184 " not setting start address\n"),
7190 /* This is a small function used when we want to ignore errors from
7194 ignore_bfd_errors (const char *fmt ATTRIBUTE_UNUSED
,
7195 va_list ap ATTRIBUTE_UNUSED
)
7197 /* Don't do anything. */
7200 /* Check that the architecture of all the input files is compatible
7201 with the output file. Also call the backend to let it do any
7202 other checking that is needed. */
7207 lang_input_statement_type
*file
;
7209 const bfd_arch_info_type
*compatible
;
7211 for (file
= (void *) file_chain
.head
;
7215 #if BFD_SUPPORTS_PLUGINS
7216 /* Don't check format of files claimed by plugin. */
7217 if (file
->flags
.claimed
)
7219 #endif /* BFD_SUPPORTS_PLUGINS */
7220 input_bfd
= file
->the_bfd
;
7222 = bfd_arch_get_compatible (input_bfd
, link_info
.output_bfd
,
7223 command_line
.accept_unknown_input_arch
);
7225 /* In general it is not possible to perform a relocatable
7226 link between differing object formats when the input
7227 file has relocations, because the relocations in the
7228 input format may not have equivalent representations in
7229 the output format (and besides BFD does not translate
7230 relocs for other link purposes than a final link). */
7231 if (!file
->flags
.just_syms
7232 && (bfd_link_relocatable (&link_info
)
7233 || link_info
.emitrelocations
)
7234 && (compatible
== NULL
7235 || (bfd_get_flavour (input_bfd
)
7236 != bfd_get_flavour (link_info
.output_bfd
)))
7237 && (bfd_get_file_flags (input_bfd
) & HAS_RELOC
) != 0)
7239 einfo (_("%F%P: relocatable linking with relocations from"
7240 " format %s (%pB) to format %s (%pB) is not supported\n"),
7241 bfd_get_target (input_bfd
), input_bfd
,
7242 bfd_get_target (link_info
.output_bfd
), link_info
.output_bfd
);
7243 /* einfo with %F exits. */
7246 if (compatible
== NULL
)
7248 if (command_line
.warn_mismatch
)
7249 einfo (_("%X%P: %s architecture of input file `%pB'"
7250 " is incompatible with %s output\n"),
7251 bfd_printable_name (input_bfd
), input_bfd
,
7252 bfd_printable_name (link_info
.output_bfd
));
7255 /* If the input bfd has no contents, it shouldn't set the
7256 private data of the output bfd. */
7257 else if (!file
->flags
.just_syms
7258 && ((input_bfd
->flags
& DYNAMIC
) != 0
7259 || bfd_count_sections (input_bfd
) != 0))
7261 bfd_error_handler_type pfn
= NULL
;
7263 /* If we aren't supposed to warn about mismatched input
7264 files, temporarily set the BFD error handler to a
7265 function which will do nothing. We still want to call
7266 bfd_merge_private_bfd_data, since it may set up
7267 information which is needed in the output file. */
7268 if (!command_line
.warn_mismatch
)
7269 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
7270 if (!bfd_merge_private_bfd_data (input_bfd
, &link_info
))
7272 if (command_line
.warn_mismatch
)
7273 einfo (_("%X%P: failed to merge target specific data"
7274 " of file %pB\n"), input_bfd
);
7276 if (!command_line
.warn_mismatch
)
7277 bfd_set_error_handler (pfn
);
7282 /* Look through all the global common symbols and attach them to the
7283 correct section. The -sort-common command line switch may be used
7284 to roughly sort the entries by alignment. */
7289 if (link_info
.inhibit_common_definition
)
7291 if (bfd_link_relocatable (&link_info
)
7292 && !command_line
.force_common_definition
)
7295 if (!config
.sort_common
)
7296 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, NULL
);
7301 if (config
.sort_common
== sort_descending
)
7303 for (power
= 4; power
> 0; power
--)
7304 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7307 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7311 for (power
= 0; power
<= 4; power
++)
7312 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7314 power
= (unsigned int) -1;
7315 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7320 /* Place one common symbol in the correct section. */
7323 lang_one_common (struct bfd_link_hash_entry
*h
, void *info
)
7325 unsigned int power_of_two
;
7329 if (h
->type
!= bfd_link_hash_common
)
7333 power_of_two
= h
->u
.c
.p
->alignment_power
;
7335 if (config
.sort_common
== sort_descending
7336 && power_of_two
< *(unsigned int *) info
)
7338 else if (config
.sort_common
== sort_ascending
7339 && power_of_two
> *(unsigned int *) info
)
7342 section
= h
->u
.c
.p
->section
;
7343 if (!bfd_define_common_symbol (link_info
.output_bfd
, &link_info
, h
))
7344 einfo (_("%F%P: could not define common symbol `%pT': %E\n"),
7347 if (config
.map_file
!= NULL
)
7349 static bool header_printed
;
7354 if (!header_printed
)
7356 minfo (_("\nAllocating common symbols\n"));
7357 minfo (_("Common symbol size file\n\n"));
7358 header_printed
= true;
7361 name
= bfd_demangle (link_info
.output_bfd
, h
->root
.string
,
7362 DMGL_ANSI
| DMGL_PARAMS
);
7365 minfo ("%s", h
->root
.string
);
7366 len
= strlen (h
->root
.string
);
7371 len
= strlen (name
);
7381 sprintf (buf
, "%" PRIx64
, (uint64_t) size
);
7382 fprintf (config
.map_file
, "%*s0x%-16s", 20 - len
, "", buf
);
7384 minfo ("%pB\n", section
->owner
);
7390 /* Handle a single orphan section S, placing the orphan into an appropriate
7391 output section. The effects of the --orphan-handling command line
7392 option are handled here. */
7395 ldlang_place_orphan (asection
*s
)
7397 if (config
.orphan_handling
== orphan_handling_discard
)
7399 lang_output_section_statement_type
*os
;
7400 os
= lang_output_section_statement_lookup (DISCARD_SECTION_NAME
, 0, 1);
7401 if (os
->addr_tree
== NULL
7402 && (bfd_link_relocatable (&link_info
)
7403 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0))
7404 os
->addr_tree
= exp_intop (0);
7405 lang_add_section (&os
->children
, s
, NULL
, NULL
, os
);
7409 lang_output_section_statement_type
*os
;
7410 const char *name
= s
->name
;
7413 if (config
.orphan_handling
== orphan_handling_error
)
7414 einfo (_("%X%P: error: unplaced orphan section `%pA' from `%pB'\n"),
7417 if (config
.unique_orphan_sections
|| unique_section_p (s
, NULL
))
7418 constraint
= SPECIAL
;
7420 os
= ldemul_place_orphan (s
, name
, constraint
);
7423 os
= lang_output_section_statement_lookup (name
, constraint
, 1);
7424 if (os
->addr_tree
== NULL
7425 && (bfd_link_relocatable (&link_info
)
7426 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0))
7427 os
->addr_tree
= exp_intop (0);
7428 lang_add_section (&os
->children
, s
, NULL
, NULL
, os
);
7431 if (config
.orphan_handling
== orphan_handling_warn
)
7432 einfo (_("%P: warning: orphan section `%pA' from `%pB' being "
7433 "placed in section `%s'\n"),
7434 s
, s
->owner
, os
->name
);
7438 /* Run through the input files and ensure that every input section has
7439 somewhere to go. If one is found without a destination then create
7440 an input request and place it into the statement tree. */
7443 lang_place_orphans (void)
7445 LANG_FOR_EACH_INPUT_STATEMENT (file
)
7449 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7451 if (s
->output_section
== NULL
)
7453 /* This section of the file is not attached, root
7454 around for a sensible place for it to go. */
7456 if (file
->flags
.just_syms
)
7457 bfd_link_just_syms (file
->the_bfd
, s
, &link_info
);
7458 else if (lang_discard_section_p (s
))
7459 s
->output_section
= bfd_abs_section_ptr
;
7460 else if (strcmp (s
->name
, "COMMON") == 0)
7462 /* This is a lonely common section which must have
7463 come from an archive. We attach to the section
7464 with the wildcard. */
7465 if (!bfd_link_relocatable (&link_info
)
7466 || command_line
.force_common_definition
)
7468 if (default_common_section
== NULL
)
7469 default_common_section
7470 = lang_output_section_statement_lookup (".bss", 0, 1);
7471 lang_add_section (&default_common_section
->children
, s
,
7472 NULL
, NULL
, default_common_section
);
7476 ldlang_place_orphan (s
);
7483 lang_set_flags (lang_memory_region_type
*ptr
, const char *flags
, int invert
)
7485 flagword
*ptr_flags
;
7487 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
7493 /* PR 17900: An exclamation mark in the attributes reverses
7494 the sense of any of the attributes that follow. */
7497 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
7501 *ptr_flags
|= SEC_ALLOC
;
7505 *ptr_flags
|= SEC_READONLY
;
7509 *ptr_flags
|= SEC_DATA
;
7513 *ptr_flags
|= SEC_CODE
;
7518 *ptr_flags
|= SEC_LOAD
;
7522 einfo (_("%F%P: invalid character %c (%d) in flags\n"),
7530 /* Call a function on each real input file. This function will be
7531 called on an archive, but not on the elements. */
7534 lang_for_each_input_file (void (*func
) (lang_input_statement_type
*))
7536 lang_input_statement_type
*f
;
7538 for (f
= (void *) input_file_chain
.head
;
7540 f
= f
->next_real_file
)
7545 /* Call a function on each real file. The function will be called on
7546 all the elements of an archive which are included in the link, but
7547 will not be called on the archive file itself. */
7550 lang_for_each_file (void (*func
) (lang_input_statement_type
*))
7552 LANG_FOR_EACH_INPUT_STATEMENT (f
)
7560 ldlang_add_file (lang_input_statement_type
*entry
)
7562 lang_statement_append (&file_chain
, entry
, &entry
->next
);
7564 /* The BFD linker needs to have a list of all input BFDs involved in
7566 ASSERT (link_info
.input_bfds_tail
!= &entry
->the_bfd
->link
.next
7567 && entry
->the_bfd
->link
.next
== NULL
);
7568 ASSERT (entry
->the_bfd
!= link_info
.output_bfd
);
7570 *link_info
.input_bfds_tail
= entry
->the_bfd
;
7571 link_info
.input_bfds_tail
= &entry
->the_bfd
->link
.next
;
7572 bfd_set_usrdata (entry
->the_bfd
, entry
);
7573 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
7575 /* Look through the sections and check for any which should not be
7576 included in the link. We need to do this now, so that we can
7577 notice when the backend linker tries to report multiple
7578 definition errors for symbols which are in sections we aren't
7579 going to link. FIXME: It might be better to entirely ignore
7580 symbols which are defined in sections which are going to be
7581 discarded. This would require modifying the backend linker for
7582 each backend which might set the SEC_LINK_ONCE flag. If we do
7583 this, we should probably handle SEC_EXCLUDE in the same way. */
7585 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, entry
);
7589 lang_add_output (const char *name
, int from_script
)
7591 /* Make -o on command line override OUTPUT in script. */
7592 if (!had_output_filename
|| !from_script
)
7594 output_filename
= name
;
7595 had_output_filename
= true;
7599 lang_output_section_statement_type
*
7600 lang_enter_output_section_statement (const char *output_section_statement_name
,
7601 etree_type
*address_exp
,
7602 enum section_type sectype
,
7603 etree_type
*sectype_value
,
7605 etree_type
*subalign
,
7608 int align_with_input
)
7610 lang_output_section_statement_type
*os
;
7612 os
= lang_output_section_statement_lookup (output_section_statement_name
,
7614 current_section
= os
;
7616 if (os
->addr_tree
== NULL
)
7618 os
->addr_tree
= address_exp
;
7620 os
->sectype
= sectype
;
7621 if (sectype
== type_section
|| sectype
== typed_readonly_section
)
7622 os
->sectype_value
= sectype_value
;
7623 else if (sectype
== noload_section
)
7624 os
->flags
= SEC_NEVER_LOAD
;
7626 os
->flags
= SEC_NO_FLAGS
;
7627 os
->block_value
= 1;
7629 /* Make next things chain into subchain of this. */
7630 push_stat_ptr (&os
->children
);
7632 os
->align_lma_with_input
= align_with_input
== ALIGN_WITH_INPUT
;
7633 if (os
->align_lma_with_input
&& align
!= NULL
)
7634 einfo (_("%F%P:%pS: error: align with input and explicit align specified\n"),
7637 os
->subsection_alignment
= subalign
;
7638 os
->section_alignment
= align
;
7640 os
->load_base
= ebase
;
7647 lang_output_statement_type
*new_stmt
;
7649 new_stmt
= new_stat (lang_output_statement
, stat_ptr
);
7650 new_stmt
->name
= output_filename
;
7653 /* Reset the current counters in the regions. */
7656 lang_reset_memory_regions (void)
7658 lang_memory_region_type
*p
= lang_memory_region_list
;
7660 lang_output_section_statement_type
*os
;
7662 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
7664 p
->current
= p
->origin
;
7668 for (os
= (void *) lang_os_list
.head
;
7672 os
->processed_vma
= false;
7673 os
->processed_lma
= false;
7676 for (o
= link_info
.output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
7678 /* Save the last size for possible use by bfd_relax_section. */
7679 o
->rawsize
= o
->size
;
7680 if (!(o
->flags
& SEC_FIXED_SIZE
))
7685 /* Worker for lang_gc_sections_1. */
7688 gc_section_callback (lang_wild_statement_type
*ptr
,
7689 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
7691 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
7692 void *data ATTRIBUTE_UNUSED
)
7694 /* If the wild pattern was marked KEEP, the member sections
7695 should be as well. */
7696 if (ptr
->keep_sections
)
7697 section
->flags
|= SEC_KEEP
;
7700 /* Iterate over sections marking them against GC. */
7703 lang_gc_sections_1 (lang_statement_union_type
*s
)
7705 for (; s
!= NULL
; s
= s
->header
.next
)
7707 switch (s
->header
.type
)
7709 case lang_wild_statement_enum
:
7710 walk_wild (&s
->wild_statement
, gc_section_callback
, NULL
);
7712 case lang_constructors_statement_enum
:
7713 lang_gc_sections_1 (constructor_list
.head
);
7715 case lang_output_section_statement_enum
:
7716 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
7718 case lang_group_statement_enum
:
7719 lang_gc_sections_1 (s
->group_statement
.children
.head
);
7728 lang_gc_sections (void)
7730 /* Keep all sections so marked in the link script. */
7731 lang_gc_sections_1 (statement_list
.head
);
7733 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
7734 the special case of .stabstr debug info. (See bfd/stabs.c)
7735 Twiddle the flag here, to simplify later linker code. */
7736 if (bfd_link_relocatable (&link_info
))
7738 LANG_FOR_EACH_INPUT_STATEMENT (f
)
7741 #if BFD_SUPPORTS_PLUGINS
7742 if (f
->flags
.claimed
)
7745 for (sec
= f
->the_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7746 if ((sec
->flags
& SEC_DEBUGGING
) == 0
7747 || strcmp (sec
->name
, ".stabstr") != 0)
7748 sec
->flags
&= ~SEC_EXCLUDE
;
7752 if (link_info
.gc_sections
)
7753 bfd_gc_sections (link_info
.output_bfd
, &link_info
);
7756 /* Worker for lang_find_relro_sections_1. */
7759 find_relro_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
7760 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
7762 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
7765 /* Discarded, excluded and ignored sections effectively have zero
7767 if (section
->output_section
!= NULL
7768 && section
->output_section
->owner
== link_info
.output_bfd
7769 && (section
->output_section
->flags
& SEC_EXCLUDE
) == 0
7770 && !IGNORE_SECTION (section
)
7771 && section
->size
!= 0)
7773 bool *has_relro_section
= (bool *) data
;
7774 *has_relro_section
= true;
7778 /* Iterate over sections for relro sections. */
7781 lang_find_relro_sections_1 (lang_statement_union_type
*s
,
7782 bool *has_relro_section
)
7784 if (*has_relro_section
)
7787 for (; s
!= NULL
; s
= s
->header
.next
)
7789 if (s
== expld
.dataseg
.relro_end_stat
)
7792 switch (s
->header
.type
)
7794 case lang_wild_statement_enum
:
7795 walk_wild (&s
->wild_statement
,
7796 find_relro_section_callback
,
7799 case lang_constructors_statement_enum
:
7800 lang_find_relro_sections_1 (constructor_list
.head
,
7803 case lang_output_section_statement_enum
:
7804 lang_find_relro_sections_1 (s
->output_section_statement
.children
.head
,
7807 case lang_group_statement_enum
:
7808 lang_find_relro_sections_1 (s
->group_statement
.children
.head
,
7818 lang_find_relro_sections (void)
7820 bool has_relro_section
= false;
7822 /* Check all sections in the link script. */
7824 lang_find_relro_sections_1 (expld
.dataseg
.relro_start_stat
,
7825 &has_relro_section
);
7827 if (!has_relro_section
)
7828 link_info
.relro
= false;
7831 /* Relax all sections until bfd_relax_section gives up. */
7834 lang_relax_sections (bool need_layout
)
7836 /* NB: Also enable relaxation to layout sections for DT_RELR. */
7837 if (RELAXATION_ENABLED
|| link_info
.enable_dt_relr
)
7839 /* We may need more than one relaxation pass. */
7840 int i
= link_info
.relax_pass
;
7842 /* The backend can use it to determine the current pass. */
7843 link_info
.relax_pass
= 0;
7847 /* Keep relaxing until bfd_relax_section gives up. */
7850 link_info
.relax_trip
= -1;
7853 link_info
.relax_trip
++;
7855 /* Note: pe-dll.c does something like this also. If you find
7856 you need to change this code, you probably need to change
7857 pe-dll.c also. DJ */
7859 /* Do all the assignments with our current guesses as to
7861 lang_do_assignments (lang_assigning_phase_enum
);
7863 /* We must do this after lang_do_assignments, because it uses
7865 lang_reset_memory_regions ();
7867 /* Perform another relax pass - this time we know where the
7868 globals are, so can make a better guess. */
7869 relax_again
= false;
7870 lang_size_sections (&relax_again
, false);
7872 while (relax_again
);
7874 link_info
.relax_pass
++;
7881 /* Final extra sizing to report errors. */
7882 lang_do_assignments (lang_assigning_phase_enum
);
7883 lang_reset_memory_regions ();
7884 lang_size_sections (NULL
, true);
7888 #if BFD_SUPPORTS_PLUGINS
7889 /* Find the insert point for the plugin's replacement files. We
7890 place them after the first claimed real object file, or if the
7891 first claimed object is an archive member, after the last real
7892 object file immediately preceding the archive. In the event
7893 no objects have been claimed at all, we return the first dummy
7894 object file on the list as the insert point; that works, but
7895 the callee must be careful when relinking the file_chain as it
7896 is not actually on that chain, only the statement_list and the
7897 input_file list; in that case, the replacement files must be
7898 inserted at the head of the file_chain. */
7900 static lang_input_statement_type
*
7901 find_replacements_insert_point (bool *before
)
7903 lang_input_statement_type
*claim1
, *lastobject
;
7904 lastobject
= (void *) input_file_chain
.head
;
7905 for (claim1
= (void *) file_chain
.head
;
7907 claim1
= claim1
->next
)
7909 if (claim1
->flags
.claimed
)
7911 *before
= claim1
->flags
.claim_archive
;
7912 return claim1
->flags
.claim_archive
? lastobject
: claim1
;
7914 /* Update lastobject if this is a real object file. */
7915 if (claim1
->the_bfd
!= NULL
&& claim1
->the_bfd
->my_archive
== NULL
)
7916 lastobject
= claim1
;
7918 /* No files were claimed by the plugin. Choose the last object
7919 file found on the list (maybe the first, dummy entry) as the
7925 /* Find where to insert ADD, an archive element or shared library
7926 added during a rescan. */
7928 static lang_input_statement_type
**
7929 find_rescan_insertion (lang_input_statement_type
*add
)
7931 bfd
*add_bfd
= add
->the_bfd
;
7932 lang_input_statement_type
*f
;
7933 lang_input_statement_type
*last_loaded
= NULL
;
7934 lang_input_statement_type
*before
= NULL
;
7935 lang_input_statement_type
**iter
= NULL
;
7937 if (add_bfd
->my_archive
!= NULL
)
7938 add_bfd
= add_bfd
->my_archive
;
7940 /* First look through the input file chain, to find an object file
7941 before the one we've rescanned. Normal object files always
7942 appear on both the input file chain and the file chain, so this
7943 lets us get quickly to somewhere near the correct place on the
7944 file chain if it is full of archive elements. Archives don't
7945 appear on the file chain, but if an element has been extracted
7946 then their input_statement->next points at it. */
7947 for (f
= (void *) input_file_chain
.head
;
7949 f
= f
->next_real_file
)
7951 if (f
->the_bfd
== add_bfd
)
7953 before
= last_loaded
;
7954 if (f
->next
!= NULL
)
7955 return &f
->next
->next
;
7957 if (f
->the_bfd
!= NULL
&& f
->next
!= NULL
)
7961 for (iter
= before
? &before
->next
: &file_chain
.head
->input_statement
.next
;
7963 iter
= &(*iter
)->next
)
7964 if (!(*iter
)->flags
.claim_archive
7965 && (*iter
)->the_bfd
->my_archive
== NULL
)
7971 /* Insert SRCLIST into DESTLIST after given element by chaining
7972 on FIELD as the next-pointer. (Counterintuitively does not need
7973 a pointer to the actual after-node itself, just its chain field.) */
7976 lang_list_insert_after (lang_statement_list_type
*destlist
,
7977 lang_statement_list_type
*srclist
,
7978 lang_statement_union_type
**field
)
7980 *(srclist
->tail
) = *field
;
7981 *field
= srclist
->head
;
7982 if (destlist
->tail
== field
)
7983 destlist
->tail
= srclist
->tail
;
7986 /* Detach new nodes added to DESTLIST since the time ORIGLIST
7987 was taken as a copy of it and leave them in ORIGLIST. */
7990 lang_list_remove_tail (lang_statement_list_type
*destlist
,
7991 lang_statement_list_type
*origlist
)
7993 union lang_statement_union
**savetail
;
7994 /* Check that ORIGLIST really is an earlier state of DESTLIST. */
7995 ASSERT (origlist
->head
== destlist
->head
);
7996 savetail
= origlist
->tail
;
7997 origlist
->head
= *(savetail
);
7998 origlist
->tail
= destlist
->tail
;
7999 destlist
->tail
= savetail
;
8003 static lang_statement_union_type
**
8004 find_next_input_statement (lang_statement_union_type
**s
)
8006 for ( ; *s
; s
= &(*s
)->header
.next
)
8008 lang_statement_union_type
**t
;
8009 switch ((*s
)->header
.type
)
8011 case lang_input_statement_enum
:
8013 case lang_wild_statement_enum
:
8014 t
= &(*s
)->wild_statement
.children
.head
;
8016 case lang_group_statement_enum
:
8017 t
= &(*s
)->group_statement
.children
.head
;
8019 case lang_output_section_statement_enum
:
8020 t
= &(*s
)->output_section_statement
.children
.head
;
8025 t
= find_next_input_statement (t
);
8031 #endif /* BFD_SUPPORTS_PLUGINS */
8033 /* Add NAME to the list of garbage collection entry points. */
8036 lang_add_gc_name (const char *name
)
8038 struct bfd_sym_chain
*sym
;
8043 sym
= stat_alloc (sizeof (*sym
));
8045 sym
->next
= link_info
.gc_sym_list
;
8047 link_info
.gc_sym_list
= sym
;
8050 /* Check relocations. */
8053 lang_check_relocs (void)
8055 if (link_info
.check_relocs_after_open_input
)
8059 for (abfd
= link_info
.input_bfds
;
8060 abfd
!= (bfd
*) NULL
; abfd
= abfd
->link
.next
)
8061 if (!bfd_link_check_relocs (abfd
, &link_info
))
8063 /* No object output, fail return. */
8064 config
.make_executable
= false;
8065 /* Note: we do not abort the loop, but rather
8066 continue the scan in case there are other
8067 bad relocations to report. */
8072 /* Look through all output sections looking for places where we can
8073 propagate forward the lma region. */
8076 lang_propagate_lma_regions (void)
8078 lang_output_section_statement_type
*os
;
8080 for (os
= (void *) lang_os_list
.head
;
8084 if (os
->prev
!= NULL
8085 && os
->lma_region
== NULL
8086 && os
->load_base
== NULL
8087 && os
->addr_tree
== NULL
8088 && os
->region
== os
->prev
->region
)
8089 os
->lma_region
= os
->prev
->lma_region
;
8094 warn_non_contiguous_discards (void)
8096 LANG_FOR_EACH_INPUT_STATEMENT (file
)
8098 if ((file
->the_bfd
->flags
& (BFD_LINKER_CREATED
| DYNAMIC
)) != 0
8099 || file
->flags
.just_syms
)
8102 for (asection
*s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
8103 if (s
->output_section
== NULL
8104 && (s
->flags
& SEC_LINKER_CREATED
) == 0)
8105 einfo (_("%P: warning: --enable-non-contiguous-regions "
8106 "discards section `%pA' from `%pB'\n"),
8112 reset_one_wild (lang_statement_union_type
*statement
)
8114 if (statement
->header
.type
== lang_wild_statement_enum
)
8116 lang_wild_statement_type
*stmt
= &statement
->wild_statement
;
8117 lang_list_init (&stmt
->matching_sections
);
8122 reset_resolved_wilds (void)
8124 lang_for_each_statement (reset_one_wild
);
8130 /* Finalize dynamic list. */
8131 if (link_info
.dynamic_list
)
8132 lang_finalize_version_expr_head (&link_info
.dynamic_list
->head
);
8134 current_target
= default_target
;
8136 /* Open the output file. */
8137 lang_for_each_statement (ldlang_open_output
);
8140 ldemul_create_output_section_statements ();
8142 /* Add to the hash table all undefineds on the command line. */
8143 lang_place_undefineds ();
8145 if (!bfd_section_already_linked_table_init ())
8146 einfo (_("%F%P: can not create hash table: %E\n"));
8148 /* A first pass through the memory regions ensures that if any region
8149 references a symbol for its origin or length then this symbol will be
8150 added to the symbol table. Having these symbols in the symbol table
8151 means that when we call open_input_bfds PROVIDE statements will
8152 trigger to provide any needed symbols. The regions origins and
8153 lengths are not assigned as a result of this call. */
8154 lang_do_memory_regions (false);
8156 /* Create a bfd for each input file. */
8157 current_target
= default_target
;
8158 lang_statement_iteration
++;
8159 open_input_bfds (statement_list
.head
, NULL
, OPEN_BFD_NORMAL
);
8161 /* Now that open_input_bfds has processed assignments and provide
8162 statements we can give values to symbolic origin/length now. */
8163 lang_do_memory_regions (true);
8165 ldemul_before_plugin_all_symbols_read ();
8167 #if BFD_SUPPORTS_PLUGINS
8168 if (link_info
.lto_plugin_active
)
8170 lang_statement_list_type added
;
8171 lang_statement_list_type files
, inputfiles
;
8173 /* Now all files are read, let the plugin(s) decide if there
8174 are any more to be added to the link before we call the
8175 emulation's after_open hook. We create a private list of
8176 input statements for this purpose, which we will eventually
8177 insert into the global statement list after the first claimed
8180 /* We need to manipulate all three chains in synchrony. */
8182 inputfiles
= input_file_chain
;
8183 if (plugin_call_all_symbols_read ())
8184 einfo (_("%F%P: %s: plugin reported error after all symbols read\n"),
8185 plugin_error_plugin ());
8186 link_info
.lto_all_symbols_read
= true;
8187 /* Open any newly added files, updating the file chains. */
8188 plugin_undefs
= link_info
.hash
->undefs_tail
;
8189 lang_output_section_statement_type
*last_os
= NULL
;
8190 if (lang_os_list
.head
!= NULL
)
8191 last_os
= ((lang_output_section_statement_type
*)
8192 ((char *) lang_os_list
.tail
8193 - offsetof (lang_output_section_statement_type
, next
)));
8194 open_input_bfds (*added
.tail
, last_os
, OPEN_BFD_NORMAL
);
8195 if (plugin_undefs
== link_info
.hash
->undefs_tail
)
8196 plugin_undefs
= NULL
;
8197 /* Restore the global list pointer now they have all been added. */
8198 lang_list_remove_tail (stat_ptr
, &added
);
8199 /* And detach the fresh ends of the file lists. */
8200 lang_list_remove_tail (&file_chain
, &files
);
8201 lang_list_remove_tail (&input_file_chain
, &inputfiles
);
8202 /* Were any new files added? */
8203 if (added
.head
!= NULL
)
8205 /* If so, we will insert them into the statement list immediately
8206 after the first input file that was claimed by the plugin,
8207 unless that file was an archive in which case it is inserted
8208 immediately before. */
8210 lang_statement_union_type
**prev
;
8211 plugin_insert
= find_replacements_insert_point (&before
);
8212 /* If a plugin adds input files without having claimed any, we
8213 don't really have a good idea where to place them. Just putting
8214 them at the start or end of the list is liable to leave them
8215 outside the crtbegin...crtend range. */
8216 ASSERT (plugin_insert
!= NULL
);
8217 /* Splice the new statement list into the old one. */
8218 prev
= &plugin_insert
->header
.next
;
8221 prev
= find_next_input_statement (prev
);
8222 if (*prev
!= (void *) plugin_insert
->next_real_file
)
8224 /* We didn't find the expected input statement.
8225 Fall back to adding after plugin_insert. */
8226 prev
= &plugin_insert
->header
.next
;
8229 lang_list_insert_after (stat_ptr
, &added
, prev
);
8230 /* Likewise for the file chains. */
8231 lang_list_insert_after (&input_file_chain
, &inputfiles
,
8232 (void *) &plugin_insert
->next_real_file
);
8233 /* We must be careful when relinking file_chain; we may need to
8234 insert the new files at the head of the list if the insert
8235 point chosen is the dummy first input file. */
8236 if (plugin_insert
->filename
)
8237 lang_list_insert_after (&file_chain
, &files
,
8238 (void *) &plugin_insert
->next
);
8240 lang_list_insert_after (&file_chain
, &files
, &file_chain
.head
);
8242 /* Rescan archives in case new undefined symbols have appeared. */
8244 lang_statement_iteration
++;
8245 open_input_bfds (statement_list
.head
, NULL
, OPEN_BFD_RESCAN
);
8246 lang_list_remove_tail (&file_chain
, &files
);
8247 while (files
.head
!= NULL
)
8249 lang_input_statement_type
**insert
;
8250 lang_input_statement_type
**iter
, *temp
;
8253 insert
= find_rescan_insertion (&files
.head
->input_statement
);
8254 /* All elements from an archive can be added at once. */
8255 iter
= &files
.head
->input_statement
.next
;
8256 my_arch
= files
.head
->input_statement
.the_bfd
->my_archive
;
8257 if (my_arch
!= NULL
)
8258 for (; *iter
!= NULL
; iter
= &(*iter
)->next
)
8259 if ((*iter
)->the_bfd
->my_archive
!= my_arch
)
8262 *insert
= &files
.head
->input_statement
;
8263 files
.head
= (lang_statement_union_type
*) *iter
;
8265 if (file_chain
.tail
== (lang_statement_union_type
**) insert
)
8266 file_chain
.tail
= (lang_statement_union_type
**) iter
;
8267 if (my_arch
!= NULL
)
8269 lang_input_statement_type
*parent
= bfd_usrdata (my_arch
);
8271 parent
->next
= (lang_input_statement_type
*)
8273 - offsetof (lang_input_statement_type
, next
));
8278 #endif /* BFD_SUPPORTS_PLUGINS */
8280 struct bfd_sym_chain
**sym
= &link_info
.gc_sym_list
;
8282 sym
= &(*sym
)->next
;
8284 *sym
= &entry_symbol
;
8286 if (entry_symbol
.name
== NULL
)
8288 *sym
= ldlang_undef_chain_list_head
;
8290 /* entry_symbol is normally initialised by an ENTRY definition in the
8291 linker script or the -e command line option. But if neither of
8292 these have been used, the target specific backend may still have
8293 provided an entry symbol via a call to lang_default_entry().
8294 Unfortunately this value will not be processed until lang_end()
8295 is called, long after this function has finished. So detect this
8296 case here and add the target's entry symbol to the list of starting
8297 points for garbage collection resolution. */
8298 lang_add_gc_name (entry_symbol_default
);
8301 lang_add_gc_name (link_info
.init_function
);
8302 lang_add_gc_name (link_info
.fini_function
);
8304 ldemul_after_open ();
8305 if (config
.map_file
!= NULL
)
8306 lang_print_asneeded ();
8310 bfd_section_already_linked_table_free ();
8312 /* Make sure that we're not mixing architectures. We call this
8313 after all the input files have been opened, but before we do any
8314 other processing, so that any operations merge_private_bfd_data
8315 does on the output file will be known during the rest of the
8319 /* Handle .exports instead of a version script if we're told to do so. */
8320 if (command_line
.version_exports_section
)
8321 lang_do_version_exports_section ();
8323 /* Build all sets based on the information gathered from the input
8325 ldctor_build_sets ();
8327 lang_symbol_tweaks ();
8329 /* PR 13683: We must rerun the assignments prior to running garbage
8330 collection in order to make sure that all symbol aliases are resolved. */
8331 lang_do_assignments (lang_mark_phase_enum
);
8332 expld
.phase
= lang_first_phase_enum
;
8334 /* Size up the common data. */
8338 debug_prefix_tree ();
8342 /* Remove unreferenced sections if asked to. */
8343 lang_gc_sections ();
8345 lang_mark_undefineds ();
8347 /* Check relocations. */
8348 lang_check_relocs ();
8350 ldemul_after_check_relocs ();
8352 /* There might have been new sections created (e.g. as result of
8353 checking relocs to need a .got, or suchlike), so to properly order
8354 them into our lists of matching sections reset them here. */
8355 reset_resolved_wilds ();
8358 /* Update wild statements in case the user gave --sort-section.
8359 Note how the option might have come after the linker script and
8360 so couldn't have been set when the wild statements were created. */
8361 update_wild_statements (statement_list
.head
);
8363 /* Run through the contours of the script and attach input sections
8364 to the correct output sections. */
8365 lang_statement_iteration
++;
8366 map_input_to_output_sections (statement_list
.head
, NULL
, NULL
);
8368 /* Start at the statement immediately after the special abs_section
8369 output statement, so that it isn't reordered. */
8370 process_insert_statements (&lang_os_list
.head
->header
.next
);
8372 ldemul_before_place_orphans ();
8374 /* Find any sections not attached explicitly and handle them. */
8375 lang_place_orphans ();
8377 if (!bfd_link_relocatable (&link_info
))
8381 /* Merge SEC_MERGE sections. This has to be done after GC of
8382 sections, so that GCed sections are not merged, but before
8383 assigning dynamic symbols, since removing whole input sections
8385 bfd_merge_sections (link_info
.output_bfd
, &link_info
);
8387 /* Look for a text section and set the readonly attribute in it. */
8388 found
= bfd_get_section_by_name (link_info
.output_bfd
, ".text");
8392 if (config
.text_read_only
)
8393 found
->flags
|= SEC_READONLY
;
8395 found
->flags
&= ~SEC_READONLY
;
8399 /* Merge together CTF sections. After this, only the symtab-dependent
8400 function and data object sections need adjustment. */
8403 /* Emit the CTF, iff the emulation doesn't need to do late emission after
8404 examining things laid out late, like the strtab. */
8407 /* Copy forward lma regions for output sections in same lma region. */
8408 lang_propagate_lma_regions ();
8410 /* Defining __start/__stop symbols early for --gc-sections to work
8411 around a glibc build problem can result in these symbols being
8412 defined when they should not be. Fix them now. */
8413 if (config
.build_constructors
)
8414 lang_undef_start_stop ();
8416 /* Define .startof./.sizeof. symbols with preliminary values before
8417 dynamic symbols are created. */
8418 if (!bfd_link_relocatable (&link_info
))
8419 lang_init_startof_sizeof ();
8421 /* Do anything special before sizing sections. This is where ELF
8422 and other back-ends size dynamic sections. */
8423 ldemul_before_allocation ();
8425 /* We must record the program headers before we try to fix the
8426 section positions, since they will affect SIZEOF_HEADERS. */
8427 lang_record_phdrs ();
8429 /* Check relro sections. */
8430 if (link_info
.relro
&& !bfd_link_relocatable (&link_info
))
8431 lang_find_relro_sections ();
8433 /* Size up the sections. */
8434 lang_size_sections (NULL
, !RELAXATION_ENABLED
);
8436 /* See if anything special should be done now we know how big
8437 everything is. This is where relaxation is done. */
8438 ldemul_after_allocation ();
8440 /* Fix any __start, __stop, .startof. or .sizeof. symbols. */
8441 lang_finalize_start_stop ();
8443 /* Do all the assignments again, to report errors. Assignment
8444 statements are processed multiple times, updating symbols; In
8445 open_input_bfds, lang_do_assignments, and lang_size_sections.
8446 Since lang_relax_sections calls lang_do_assignments, symbols are
8447 also updated in ldemul_after_allocation. */
8448 lang_do_assignments (lang_final_phase_enum
);
8452 /* Convert absolute symbols to section relative. */
8453 ldexp_finalize_syms ();
8455 /* Make sure that the section addresses make sense. */
8456 if (command_line
.check_section_addresses
)
8457 lang_check_section_addresses ();
8459 if (link_info
.non_contiguous_regions
8460 && link_info
.non_contiguous_regions_warnings
)
8461 warn_non_contiguous_discards ();
8463 /* Check any required symbols are known. */
8464 ldlang_check_require_defined_symbols ();
8470 lang_add_version_string (void)
8472 if (! enable_linker_version
)
8475 const char * str
= "GNU ld ";
8476 int len
= strlen (str
);
8479 for (i
= 0 ; i
< len
; i
++)
8480 lang_add_data (BYTE
, exp_intop (str
[i
]));
8482 str
= BFD_VERSION_STRING
;
8485 for (i
= 0 ; i
< len
; i
++)
8486 lang_add_data (BYTE
, exp_intop (str
[i
]));
8488 lang_add_data (BYTE
, exp_intop ('\0'));
8491 /* EXPORTED TO YACC */
8494 lang_add_wild (struct wildcard_spec
*filespec
,
8495 struct wildcard_list
*section_list
,
8498 struct wildcard_list
*curr
, *next
;
8499 lang_wild_statement_type
*new_stmt
;
8500 bool any_specs_sorted
= false;
8502 /* Reverse the list as the parser puts it back to front. */
8503 for (curr
= section_list
, section_list
= NULL
;
8505 section_list
= curr
, curr
= next
)
8507 if (curr
->spec
.sorted
!= none
&& curr
->spec
.sorted
!= by_none
)
8508 any_specs_sorted
= true;
8510 curr
->next
= section_list
;
8513 if (filespec
!= NULL
&& filespec
->name
!= NULL
)
8515 if (strcmp (filespec
->name
, "*") == 0)
8516 filespec
->name
= NULL
;
8517 else if (!wildcardp (filespec
->name
))
8518 lang_has_input_file
= true;
8521 new_stmt
= new_stat (lang_wild_statement
, stat_ptr
);
8522 new_stmt
->filename
= NULL
;
8523 new_stmt
->filenames_sorted
= false;
8524 new_stmt
->any_specs_sorted
= any_specs_sorted
;
8525 new_stmt
->section_flag_list
= NULL
;
8526 new_stmt
->exclude_name_list
= NULL
;
8527 if (filespec
!= NULL
)
8529 new_stmt
->filename
= filespec
->name
;
8530 new_stmt
->filenames_sorted
= (filespec
->sorted
== by_name
|| filespec
->reversed
);
8531 new_stmt
->section_flag_list
= filespec
->section_flag_list
;
8532 new_stmt
->exclude_name_list
= filespec
->exclude_name_list
;
8533 new_stmt
->filenames_reversed
= filespec
->reversed
;
8535 new_stmt
->section_list
= section_list
;
8536 new_stmt
->keep_sections
= keep_sections
;
8537 lang_list_init (&new_stmt
->children
);
8538 lang_list_init (&new_stmt
->matching_sections
);
8539 analyze_walk_wild_section_handler (new_stmt
);
8542 printf ("wild %s(", new_stmt
->filename
? new_stmt
->filename
: "*");
8543 for (curr
= new_stmt
->section_list
; curr
; curr
= curr
->next
)
8544 printf ("%s ", curr
->spec
.name
? curr
->spec
.name
: "*");
8550 lang_section_start (const char *name
, etree_type
*address
,
8551 const segment_type
*segment
)
8553 lang_address_statement_type
*ad
;
8555 ad
= new_stat (lang_address_statement
, stat_ptr
);
8556 ad
->section_name
= name
;
8557 ad
->address
= address
;
8558 ad
->segment
= segment
;
8561 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
8562 because of a -e argument on the command line, or zero if this is
8563 called by ENTRY in a linker script. Command line arguments take
8567 lang_add_entry (const char *name
, bool cmdline
)
8569 if (entry_symbol
.name
== NULL
8571 || !entry_from_cmdline
)
8573 entry_symbol
.name
= name
;
8574 entry_from_cmdline
= cmdline
;
8578 /* Set the default start symbol to NAME. .em files should use this,
8579 not lang_add_entry, to override the use of "start" if neither the
8580 linker script nor the command line specifies an entry point. NAME
8581 must be permanently allocated. */
8583 lang_default_entry (const char *name
)
8585 entry_symbol_default
= name
;
8589 lang_add_target (const char *name
)
8591 lang_target_statement_type
*new_stmt
;
8593 new_stmt
= new_stat (lang_target_statement
, stat_ptr
);
8594 new_stmt
->target
= name
;
8598 lang_add_map (const char *name
)
8605 map_option_f
= true;
8613 lang_add_fill (fill_type
*fill
)
8615 lang_fill_statement_type
*new_stmt
;
8617 new_stmt
= new_stat (lang_fill_statement
, stat_ptr
);
8618 new_stmt
->fill
= fill
;
8622 lang_add_data (int type
, union etree_union
*exp
)
8624 lang_data_statement_type
*new_stmt
;
8626 new_stmt
= new_stat (lang_data_statement
, stat_ptr
);
8627 new_stmt
->exp
= exp
;
8628 new_stmt
->type
= type
;
8632 lang_add_string (const char *s
)
8634 bfd_vma len
= strlen (s
);
8636 bool escape
= false;
8638 /* Add byte expressions until end of string. */
8639 for (i
= 0 ; i
< len
; i
++)
8648 /* Ignore the escape. */
8651 case 'n': c
= '\n'; break;
8652 case 'r': c
= '\r'; break;
8653 case 't': c
= '\t'; break;
8663 /* We have an octal number. */
8665 unsigned int value
= c
- '0';
8668 if ((c
>= '0') && (c
<= '7'))
8676 if ((c
>= '0') && (c
<= '7'))
8687 /* octal: \777 is treated as '\077' + '7' */
8698 lang_add_data (BYTE
, exp_intop (c
));
8706 lang_add_data (BYTE
, exp_intop (c
));
8710 /* Remeber to terminate the string. */
8711 lang_add_data (BYTE
, exp_intop (0));
8714 /* Create a new reloc statement. RELOC is the BFD relocation type to
8715 generate. HOWTO is the corresponding howto structure (we could
8716 look this up, but the caller has already done so). SECTION is the
8717 section to generate a reloc against, or NAME is the name of the
8718 symbol to generate a reloc against. Exactly one of SECTION and
8719 NAME must be NULL. ADDEND is an expression for the addend. */
8722 lang_add_reloc (bfd_reloc_code_real_type reloc
,
8723 reloc_howto_type
*howto
,
8726 union etree_union
*addend
)
8728 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
8732 p
->section
= section
;
8734 p
->addend_exp
= addend
;
8736 p
->addend_value
= 0;
8737 p
->output_section
= NULL
;
8738 p
->output_offset
= 0;
8741 lang_assignment_statement_type
*
8742 lang_add_assignment (etree_type
*exp
)
8744 lang_assignment_statement_type
*new_stmt
;
8746 new_stmt
= new_stat (lang_assignment_statement
, stat_ptr
);
8747 new_stmt
->exp
= exp
;
8752 lang_add_attribute (enum statement_enum attribute
)
8754 new_statement (attribute
, sizeof (lang_statement_header_type
), stat_ptr
);
8758 lang_startup (const char *name
)
8760 if (first_file
->filename
!= NULL
)
8762 einfo (_("%F%P: multiple STARTUP files\n"));
8764 first_file
->filename
= name
;
8765 first_file
->local_sym_name
= name
;
8766 first_file
->flags
.real
= true;
8770 lang_float (bool maybe
)
8772 lang_float_flag
= maybe
;
8776 /* Work out the load- and run-time regions from a script statement, and
8777 store them in *LMA_REGION and *REGION respectively.
8779 MEMSPEC is the name of the run-time region, or the value of
8780 DEFAULT_MEMORY_REGION if the statement didn't specify one.
8781 LMA_MEMSPEC is the name of the load-time region, or null if the
8782 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
8783 had an explicit load address.
8785 It is an error to specify both a load region and a load address. */
8788 lang_get_regions (lang_memory_region_type
**region
,
8789 lang_memory_region_type
**lma_region
,
8790 const char *memspec
,
8791 const char *lma_memspec
,
8795 *lma_region
= lang_memory_region_lookup (lma_memspec
, false);
8797 /* If no runtime region or VMA has been specified, but the load region
8798 has been specified, then use the load region for the runtime region
8800 if (lma_memspec
!= NULL
8802 && strcmp (memspec
, DEFAULT_MEMORY_REGION
) == 0)
8803 *region
= *lma_region
;
8805 *region
= lang_memory_region_lookup (memspec
, false);
8807 if (have_lma
&& lma_memspec
!= 0)
8808 einfo (_("%X%P:%pS: section has both a load address and a load region\n"),
8813 lang_leave_output_section_statement (fill_type
*fill
, const char *memspec
,
8814 lang_output_section_phdr_list
*phdrs
,
8815 const char *lma_memspec
)
8817 lang_get_regions (¤t_section
->region
,
8818 ¤t_section
->lma_region
,
8819 memspec
, lma_memspec
,
8820 current_section
->load_base
!= NULL
,
8821 current_section
->addr_tree
!= NULL
);
8823 current_section
->fill
= fill
;
8824 current_section
->phdrs
= phdrs
;
8828 /* Set the output format type. -oformat overrides scripts. */
8831 lang_add_output_format (const char *format
,
8836 if (output_target
== NULL
|| !from_script
)
8838 if (command_line
.endian
== ENDIAN_BIG
8841 else if (command_line
.endian
== ENDIAN_LITTLE
8845 output_target
= format
;
8850 lang_add_insert (const char *where
, int is_before
)
8852 lang_insert_statement_type
*new_stmt
;
8854 new_stmt
= new_stat (lang_insert_statement
, stat_ptr
);
8855 new_stmt
->where
= where
;
8856 new_stmt
->is_before
= is_before
;
8857 saved_script_handle
= previous_script_handle
;
8860 /* Enter a group. This creates a new lang_group_statement, and sets
8861 stat_ptr to build new statements within the group. */
8864 lang_enter_group (void)
8866 lang_group_statement_type
*g
;
8868 g
= new_stat (lang_group_statement
, stat_ptr
);
8869 lang_list_init (&g
->children
);
8870 push_stat_ptr (&g
->children
);
8873 /* Leave a group. This just resets stat_ptr to start writing to the
8874 regular list of statements again. Note that this will not work if
8875 groups can occur inside anything else which can adjust stat_ptr,
8876 but currently they can't. */
8879 lang_leave_group (void)
8884 /* Add a new program header. This is called for each entry in a PHDRS
8885 command in a linker script. */
8888 lang_new_phdr (const char *name
,
8895 struct lang_phdr
*n
, **pp
;
8898 n
= stat_alloc (sizeof (struct lang_phdr
));
8901 n
->type
= exp_get_vma (type
, NULL
, 0, "program header type");
8902 n
->filehdr
= filehdr
;
8907 hdrs
= n
->type
== 1 && (phdrs
|| filehdr
);
8909 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
8912 && !((*pp
)->filehdr
|| (*pp
)->phdrs
))
8914 einfo (_("%X%P:%pS: PHDRS and FILEHDR are not supported"
8915 " when prior PT_LOAD headers lack them\n"), NULL
);
8922 /* Record the program header information in the output BFD. FIXME: We
8923 should not be calling an ELF specific function here. */
8926 lang_record_phdrs (void)
8930 lang_output_section_phdr_list
*last
;
8931 struct lang_phdr
*l
;
8932 lang_output_section_statement_type
*os
;
8935 secs
= (asection
**) xmalloc (alc
* sizeof (asection
*));
8938 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
8945 for (os
= (void *) lang_os_list
.head
;
8949 lang_output_section_phdr_list
*pl
;
8951 if (os
->constraint
< 0)
8959 if (os
->sectype
== noload_section
8960 || os
->bfd_section
== NULL
8961 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
8964 /* Don't add orphans to PT_INTERP header. */
8970 lang_output_section_statement_type
*tmp_os
;
8972 /* If we have not run across a section with a program
8973 header assigned to it yet, then scan forwards to find
8974 one. This prevents inconsistencies in the linker's
8975 behaviour when a script has specified just a single
8976 header and there are sections in that script which are
8977 not assigned to it, and which occur before the first
8978 use of that header. See here for more details:
8979 http://sourceware.org/ml/binutils/2007-02/msg00291.html */
8980 for (tmp_os
= os
; tmp_os
; tmp_os
= tmp_os
->next
)
8983 last
= tmp_os
->phdrs
;
8987 einfo (_("%F%P: no sections assigned to phdrs\n"));
8992 if (os
->bfd_section
== NULL
)
8995 for (; pl
!= NULL
; pl
= pl
->next
)
8997 if (strcmp (pl
->name
, l
->name
) == 0)
9002 secs
= (asection
**) xrealloc (secs
,
9003 alc
* sizeof (asection
*));
9005 secs
[c
] = os
->bfd_section
;
9012 if (l
->flags
== NULL
)
9015 flags
= exp_get_vma (l
->flags
, NULL
, 0, "phdr flags");
9020 at
= exp_get_vma (l
->at
, NULL
, 0, "phdr load address");
9022 if (!bfd_record_phdr (link_info
.output_bfd
, l
->type
,
9023 l
->flags
!= NULL
, flags
, l
->at
!= NULL
,
9024 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
9025 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
9030 /* Make sure all the phdr assignments succeeded. */
9031 for (os
= (void *) lang_os_list
.head
;
9035 lang_output_section_phdr_list
*pl
;
9037 if (os
->constraint
< 0
9038 || os
->bfd_section
== NULL
)
9041 for (pl
= os
->phdrs
;
9044 if (!pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
9045 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
9046 os
->name
, pl
->name
);
9050 /* Record a list of sections which may not be cross referenced. */
9053 lang_add_nocrossref (lang_nocrossref_type
*l
)
9055 struct lang_nocrossrefs
*n
;
9057 n
= (struct lang_nocrossrefs
*) xmalloc (sizeof *n
);
9058 n
->next
= nocrossref_list
;
9060 n
->onlyfirst
= false;
9061 nocrossref_list
= n
;
9063 /* Set notice_all so that we get informed about all symbols. */
9064 link_info
.notice_all
= true;
9067 /* Record a section that cannot be referenced from a list of sections. */
9070 lang_add_nocrossref_to (lang_nocrossref_type
*l
)
9072 lang_add_nocrossref (l
);
9073 nocrossref_list
->onlyfirst
= true;
9076 /* Overlay handling. We handle overlays with some static variables. */
9078 /* The overlay virtual address. */
9079 static etree_type
*overlay_vma
;
9080 /* And subsection alignment. */
9081 static etree_type
*overlay_subalign
;
9083 /* An expression for the maximum section size seen so far. */
9084 static etree_type
*overlay_max
;
9086 /* A list of all the sections in this overlay. */
9088 struct overlay_list
{
9089 struct overlay_list
*next
;
9090 lang_output_section_statement_type
*os
;
9093 static struct overlay_list
*overlay_list
;
9095 /* Start handling an overlay. */
9098 lang_enter_overlay (etree_type
*vma_expr
, etree_type
*subalign
)
9100 /* The grammar should prevent nested overlays from occurring. */
9101 ASSERT (overlay_vma
== NULL
9102 && overlay_subalign
== NULL
9103 && overlay_max
== NULL
);
9105 overlay_vma
= vma_expr
;
9106 overlay_subalign
= subalign
;
9109 /* Start a section in an overlay. We handle this by calling
9110 lang_enter_output_section_statement with the correct VMA.
9111 lang_leave_overlay sets up the LMA and memory regions. */
9114 lang_enter_overlay_section (const char *name
)
9116 struct overlay_list
*n
;
9119 lang_enter_output_section_statement (name
, overlay_vma
, overlay_section
,
9120 0, 0, overlay_subalign
, 0, 0, 0);
9122 /* If this is the first section, then base the VMA of future
9123 sections on this one. This will work correctly even if `.' is
9124 used in the addresses. */
9125 if (overlay_list
== NULL
)
9126 overlay_vma
= exp_nameop (ADDR
, name
);
9128 /* Remember the section. */
9129 n
= (struct overlay_list
*) xmalloc (sizeof *n
);
9130 n
->os
= current_section
;
9131 n
->next
= overlay_list
;
9134 size
= exp_nameop (SIZEOF
, name
);
9136 /* Arrange to work out the maximum section end address. */
9137 if (overlay_max
== NULL
)
9140 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
9143 /* Finish a section in an overlay. There isn't any special to do
9147 lang_leave_overlay_section (fill_type
*fill
,
9148 lang_output_section_phdr_list
*phdrs
)
9155 name
= current_section
->name
;
9157 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
9158 region and that no load-time region has been specified. It doesn't
9159 really matter what we say here, since lang_leave_overlay will
9161 lang_leave_output_section_statement (fill
, DEFAULT_MEMORY_REGION
, phdrs
, 0);
9163 /* Define the magic symbols. */
9165 clean
= (char *) xmalloc (strlen (name
) + 1);
9167 for (s1
= name
; *s1
!= '\0'; s1
++)
9168 if (ISALNUM (*s1
) || *s1
== '_')
9172 buf
= (char *) xmalloc (strlen (clean
) + sizeof "__load_start_");
9173 sprintf (buf
, "__load_start_%s", clean
);
9174 lang_add_assignment (exp_provide (buf
,
9175 exp_nameop (LOADADDR
, name
),
9178 buf
= (char *) xmalloc (strlen (clean
) + sizeof "__load_stop_");
9179 sprintf (buf
, "__load_stop_%s", clean
);
9180 lang_add_assignment (exp_provide (buf
,
9182 exp_nameop (LOADADDR
, name
),
9183 exp_nameop (SIZEOF
, name
)),
9189 /* Finish an overlay. If there are any overlay wide settings, this
9190 looks through all the sections in the overlay and sets them. */
9193 lang_leave_overlay (etree_type
*lma_expr
,
9196 const char *memspec
,
9197 lang_output_section_phdr_list
*phdrs
,
9198 const char *lma_memspec
)
9200 lang_memory_region_type
*region
;
9201 lang_memory_region_type
*lma_region
;
9202 struct overlay_list
*l
;
9203 lang_nocrossref_type
*nocrossref
;
9205 lang_get_regions (®ion
, &lma_region
,
9206 memspec
, lma_memspec
,
9207 lma_expr
!= NULL
, false);
9211 /* After setting the size of the last section, set '.' to end of the
9213 if (overlay_list
!= NULL
)
9215 overlay_list
->os
->update_dot
= 1;
9216 overlay_list
->os
->update_dot_tree
9217 = exp_assign (".", exp_binop ('+', overlay_vma
, overlay_max
), false);
9223 struct overlay_list
*next
;
9225 if (fill
!= NULL
&& l
->os
->fill
== NULL
)
9228 l
->os
->region
= region
;
9229 l
->os
->lma_region
= lma_region
;
9231 /* The first section has the load address specified in the
9232 OVERLAY statement. The rest are worked out from that.
9233 The base address is not needed (and should be null) if
9234 an LMA region was specified. */
9237 l
->os
->load_base
= lma_expr
;
9238 l
->os
->sectype
= first_overlay_section
;
9240 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
9241 l
->os
->phdrs
= phdrs
;
9245 lang_nocrossref_type
*nc
;
9247 nc
= (lang_nocrossref_type
*) xmalloc (sizeof *nc
);
9248 nc
->name
= l
->os
->name
;
9249 nc
->next
= nocrossref
;
9258 if (nocrossref
!= NULL
)
9259 lang_add_nocrossref (nocrossref
);
9262 overlay_list
= NULL
;
9264 overlay_subalign
= NULL
;
9267 /* Version handling. This is only useful for ELF. */
9269 /* If PREV is NULL, return first version pattern matching particular symbol.
9270 If PREV is non-NULL, return first version pattern matching particular
9271 symbol after PREV (previously returned by lang_vers_match). */
9273 static struct bfd_elf_version_expr
*
9274 lang_vers_match (struct bfd_elf_version_expr_head
*head
,
9275 struct bfd_elf_version_expr
*prev
,
9279 const char *cxx_sym
= sym
;
9280 const char *java_sym
= sym
;
9281 struct bfd_elf_version_expr
*expr
= NULL
;
9282 enum demangling_styles curr_style
;
9284 curr_style
= CURRENT_DEMANGLING_STYLE
;
9285 cplus_demangle_set_style (no_demangling
);
9286 c_sym
= bfd_demangle (link_info
.output_bfd
, sym
, DMGL_NO_OPTS
);
9289 cplus_demangle_set_style (curr_style
);
9291 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
9293 cxx_sym
= bfd_demangle (link_info
.output_bfd
, sym
,
9294 DMGL_PARAMS
| DMGL_ANSI
);
9298 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
9300 java_sym
= bfd_demangle (link_info
.output_bfd
, sym
, DMGL_JAVA
);
9305 if (head
->htab
&& (prev
== NULL
|| prev
->literal
))
9307 struct bfd_elf_version_expr e
;
9309 switch (prev
? prev
->mask
: 0)
9312 if (head
->mask
& BFD_ELF_VERSION_C_TYPE
)
9315 expr
= (struct bfd_elf_version_expr
*)
9316 htab_find ((htab_t
) head
->htab
, &e
);
9317 while (expr
&& strcmp (expr
->pattern
, c_sym
) == 0)
9318 if (expr
->mask
== BFD_ELF_VERSION_C_TYPE
)
9324 case BFD_ELF_VERSION_C_TYPE
:
9325 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
9327 e
.pattern
= cxx_sym
;
9328 expr
= (struct bfd_elf_version_expr
*)
9329 htab_find ((htab_t
) head
->htab
, &e
);
9330 while (expr
&& strcmp (expr
->pattern
, cxx_sym
) == 0)
9331 if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
9337 case BFD_ELF_VERSION_CXX_TYPE
:
9338 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
9340 e
.pattern
= java_sym
;
9341 expr
= (struct bfd_elf_version_expr
*)
9342 htab_find ((htab_t
) head
->htab
, &e
);
9343 while (expr
&& strcmp (expr
->pattern
, java_sym
) == 0)
9344 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
9355 /* Finally, try the wildcards. */
9356 if (prev
== NULL
|| prev
->literal
)
9357 expr
= head
->remaining
;
9360 for (; expr
; expr
= expr
->next
)
9367 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
9370 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
9372 else if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
9376 if (fnmatch (expr
->pattern
, s
, 0) == 0)
9382 free ((char *) c_sym
);
9384 free ((char *) cxx_sym
);
9385 if (java_sym
!= sym
)
9386 free ((char *) java_sym
);
9390 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
9391 return a pointer to the symbol name with any backslash quotes removed. */
9394 realsymbol (const char *pattern
)
9397 bool changed
= false, backslash
= false;
9398 char *s
, *symbol
= (char *) xmalloc (strlen (pattern
) + 1);
9400 for (p
= pattern
, s
= symbol
; *p
!= '\0'; ++p
)
9402 /* It is a glob pattern only if there is no preceding
9406 /* Remove the preceding backslash. */
9413 if (*p
== '?' || *p
== '*' || *p
== '[')
9420 backslash
= *p
== '\\';
9436 /* This is called for each variable name or match expression. NEW_NAME is
9437 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
9438 pattern to be matched against symbol names. */
9440 struct bfd_elf_version_expr
*
9441 lang_new_vers_pattern (struct bfd_elf_version_expr
*orig
,
9442 const char *new_name
,
9446 struct bfd_elf_version_expr
*ret
;
9448 ret
= (struct bfd_elf_version_expr
*) xmalloc (sizeof *ret
);
9452 ret
->literal
= true;
9453 ret
->pattern
= literal_p
? new_name
: realsymbol (new_name
);
9454 if (ret
->pattern
== NULL
)
9456 ret
->pattern
= new_name
;
9457 ret
->literal
= false;
9460 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
9461 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
9462 else if (strcasecmp (lang
, "C++") == 0)
9463 ret
->mask
= BFD_ELF_VERSION_CXX_TYPE
;
9464 else if (strcasecmp (lang
, "Java") == 0)
9465 ret
->mask
= BFD_ELF_VERSION_JAVA_TYPE
;
9468 einfo (_("%X%P: unknown language `%s' in version information\n"),
9470 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
9473 return ldemul_new_vers_pattern (ret
);
9476 /* This is called for each set of variable names and match
9479 struct bfd_elf_version_tree
*
9480 lang_new_vers_node (struct bfd_elf_version_expr
*globals
,
9481 struct bfd_elf_version_expr
*locals
)
9483 struct bfd_elf_version_tree
*ret
;
9485 ret
= (struct bfd_elf_version_tree
*) xcalloc (1, sizeof *ret
);
9486 ret
->globals
.list
= globals
;
9487 ret
->locals
.list
= locals
;
9488 ret
->match
= lang_vers_match
;
9489 ret
->name_indx
= (unsigned int) -1;
9493 /* This static variable keeps track of version indices. */
9495 static int version_index
;
9498 version_expr_head_hash (const void *p
)
9500 const struct bfd_elf_version_expr
*e
=
9501 (const struct bfd_elf_version_expr
*) p
;
9503 return htab_hash_string (e
->pattern
);
9507 version_expr_head_eq (const void *p1
, const void *p2
)
9509 const struct bfd_elf_version_expr
*e1
=
9510 (const struct bfd_elf_version_expr
*) p1
;
9511 const struct bfd_elf_version_expr
*e2
=
9512 (const struct bfd_elf_version_expr
*) p2
;
9514 return strcmp (e1
->pattern
, e2
->pattern
) == 0;
9518 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head
*head
)
9521 struct bfd_elf_version_expr
*e
, *next
;
9522 struct bfd_elf_version_expr
**list_loc
, **remaining_loc
;
9524 for (e
= head
->list
; e
; e
= e
->next
)
9528 head
->mask
|= e
->mask
;
9533 head
->htab
= htab_create (count
* 2, version_expr_head_hash
,
9534 version_expr_head_eq
, NULL
);
9535 list_loc
= &head
->list
;
9536 remaining_loc
= &head
->remaining
;
9537 for (e
= head
->list
; e
; e
= next
)
9543 remaining_loc
= &e
->next
;
9547 void **loc
= htab_find_slot ((htab_t
) head
->htab
, e
, INSERT
);
9551 struct bfd_elf_version_expr
*e1
, *last
;
9553 e1
= (struct bfd_elf_version_expr
*) *loc
;
9557 if (e1
->mask
== e
->mask
)
9565 while (e1
&& strcmp (e1
->pattern
, e
->pattern
) == 0);
9569 /* This is a duplicate. */
9570 /* FIXME: Memory leak. Sometimes pattern is not
9571 xmalloced alone, but in larger chunk of memory. */
9572 /* free (e->pattern); */
9577 e
->next
= last
->next
;
9585 list_loc
= &e
->next
;
9589 *remaining_loc
= NULL
;
9590 *list_loc
= head
->remaining
;
9593 head
->remaining
= head
->list
;
9596 /* This is called when we know the name and dependencies of the
9600 lang_register_vers_node (const char *name
,
9601 struct bfd_elf_version_tree
*version
,
9602 struct bfd_elf_version_deps
*deps
)
9604 struct bfd_elf_version_tree
*t
, **pp
;
9605 struct bfd_elf_version_expr
*e1
;
9610 if (link_info
.version_info
!= NULL
9611 && (name
[0] == '\0' || link_info
.version_info
->name
[0] == '\0'))
9613 einfo (_("%X%P: anonymous version tag cannot be combined"
9614 " with other version tags\n"));
9619 /* Make sure this node has a unique name. */
9620 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9621 if (strcmp (t
->name
, name
) == 0)
9622 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
9624 lang_finalize_version_expr_head (&version
->globals
);
9625 lang_finalize_version_expr_head (&version
->locals
);
9627 /* Check the global and local match names, and make sure there
9628 aren't any duplicates. */
9630 for (e1
= version
->globals
.list
; e1
!= NULL
; e1
= e1
->next
)
9632 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9634 struct bfd_elf_version_expr
*e2
;
9636 if (t
->locals
.htab
&& e1
->literal
)
9638 e2
= (struct bfd_elf_version_expr
*)
9639 htab_find ((htab_t
) t
->locals
.htab
, e1
);
9640 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
9642 if (e1
->mask
== e2
->mask
)
9643 einfo (_("%X%P: duplicate expression `%s'"
9644 " in version information\n"), e1
->pattern
);
9648 else if (!e1
->literal
)
9649 for (e2
= t
->locals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
9650 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
9651 && e1
->mask
== e2
->mask
)
9652 einfo (_("%X%P: duplicate expression `%s'"
9653 " in version information\n"), e1
->pattern
);
9657 for (e1
= version
->locals
.list
; e1
!= NULL
; e1
= e1
->next
)
9659 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9661 struct bfd_elf_version_expr
*e2
;
9663 if (t
->globals
.htab
&& e1
->literal
)
9665 e2
= (struct bfd_elf_version_expr
*)
9666 htab_find ((htab_t
) t
->globals
.htab
, e1
);
9667 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
9669 if (e1
->mask
== e2
->mask
)
9670 einfo (_("%X%P: duplicate expression `%s'"
9671 " in version information\n"),
9676 else if (!e1
->literal
)
9677 for (e2
= t
->globals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
9678 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
9679 && e1
->mask
== e2
->mask
)
9680 einfo (_("%X%P: duplicate expression `%s'"
9681 " in version information\n"), e1
->pattern
);
9685 version
->deps
= deps
;
9686 version
->name
= name
;
9687 if (name
[0] != '\0')
9690 version
->vernum
= version_index
;
9693 version
->vernum
= 0;
9695 for (pp
= &link_info
.version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
9700 /* This is called when we see a version dependency. */
9702 struct bfd_elf_version_deps
*
9703 lang_add_vers_depend (struct bfd_elf_version_deps
*list
, const char *name
)
9705 struct bfd_elf_version_deps
*ret
;
9706 struct bfd_elf_version_tree
*t
;
9708 ret
= (struct bfd_elf_version_deps
*) xmalloc (sizeof *ret
);
9711 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9713 if (strcmp (t
->name
, name
) == 0)
9715 ret
->version_needed
= t
;
9720 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
9722 ret
->version_needed
= NULL
;
9727 lang_do_version_exports_section (void)
9729 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
9731 LANG_FOR_EACH_INPUT_STATEMENT (is
)
9733 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
9741 contents
= (char *) xmalloc (len
);
9742 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
9743 einfo (_("%X%P: unable to read .exports section contents\n"), sec
);
9746 while (p
< contents
+ len
)
9748 greg
= lang_new_vers_pattern (greg
, p
, NULL
, false);
9749 p
= strchr (p
, '\0') + 1;
9752 /* Do not free the contents, as we used them creating the regex. */
9754 /* Do not include this section in the link. */
9755 sec
->flags
|= SEC_EXCLUDE
| SEC_KEEP
;
9758 lreg
= lang_new_vers_pattern (NULL
, "*", NULL
, false);
9759 lang_register_vers_node (command_line
.version_exports_section
,
9760 lang_new_vers_node (greg
, lreg
), NULL
);
9763 /* Evaluate LENGTH and ORIGIN parts of MEMORY spec. This is initially
9764 called with UPDATE_REGIONS_P set to FALSE, in this case no errors are
9765 thrown, however, references to symbols in the origin and length fields
9766 will be pushed into the symbol table, this allows PROVIDE statements to
9767 then provide these symbols. This function is called a second time with
9768 UPDATE_REGIONS_P set to TRUE, this time the we update the actual region
9769 data structures, and throw errors if missing symbols are encountered. */
9772 lang_do_memory_regions (bool update_regions_p
)
9774 lang_memory_region_type
*r
= lang_memory_region_list
;
9776 for (; r
!= NULL
; r
= r
->next
)
9780 exp_fold_tree_no_dot (r
->origin_exp
, NULL
);
9781 if (update_regions_p
)
9783 if (expld
.result
.valid_p
)
9785 r
->origin
= expld
.result
.value
;
9786 r
->current
= r
->origin
;
9789 einfo (_("%P: invalid origin for memory region %s\n"),
9795 exp_fold_tree_no_dot (r
->length_exp
, NULL
);
9796 if (update_regions_p
)
9798 if (expld
.result
.valid_p
)
9799 r
->length
= expld
.result
.value
;
9801 einfo (_("%P: invalid length for memory region %s\n"),
9809 lang_add_unique (const char *name
)
9811 struct unique_sections
*ent
;
9813 for (ent
= unique_section_list
; ent
; ent
= ent
->next
)
9814 if (strcmp (ent
->name
, name
) == 0)
9817 ent
= (struct unique_sections
*) xmalloc (sizeof *ent
);
9818 ent
->name
= xstrdup (name
);
9819 ent
->next
= unique_section_list
;
9820 unique_section_list
= ent
;
9823 /* Append the list of dynamic symbols to the existing one. */
9826 lang_append_dynamic_list (struct bfd_elf_dynamic_list
**list_p
,
9827 struct bfd_elf_version_expr
*dynamic
)
9831 struct bfd_elf_version_expr
*tail
;
9832 for (tail
= dynamic
; tail
->next
!= NULL
; tail
= tail
->next
)
9834 tail
->next
= (*list_p
)->head
.list
;
9835 (*list_p
)->head
.list
= dynamic
;
9839 struct bfd_elf_dynamic_list
*d
;
9841 d
= (struct bfd_elf_dynamic_list
*) xcalloc (1, sizeof *d
);
9842 d
->head
.list
= dynamic
;
9843 d
->match
= lang_vers_match
;
9848 /* Append the list of C++ typeinfo dynamic symbols to the existing
9852 lang_append_dynamic_list_cpp_typeinfo (void)
9854 const char *symbols
[] =
9856 "typeinfo name for*",
9859 struct bfd_elf_version_expr
*dynamic
= NULL
;
9862 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
9863 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
9866 lang_append_dynamic_list (&link_info
.dynamic_list
, dynamic
);
9869 /* Append the list of C++ operator new and delete dynamic symbols to the
9873 lang_append_dynamic_list_cpp_new (void)
9875 const char *symbols
[] =
9880 struct bfd_elf_version_expr
*dynamic
= NULL
;
9883 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
9884 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
9887 lang_append_dynamic_list (&link_info
.dynamic_list
, dynamic
);
9890 /* Scan a space and/or comma separated string of features. */
9893 lang_ld_feature (char *str
)
9901 while (*p
== ',' || ISSPACE (*p
))
9906 while (*q
&& *q
!= ',' && !ISSPACE (*q
))
9910 if (strcasecmp (p
, "SANE_EXPR") == 0)
9911 config
.sane_expr
= true;
9913 einfo (_("%X%P: unknown feature `%s'\n"), p
);
9919 /* Pretty print memory amount. */
9922 lang_print_memory_size (uint64_t sz
)
9924 if ((sz
& 0x3fffffff) == 0)
9925 printf ("%10" PRIu64
" GB", sz
>> 30);
9926 else if ((sz
& 0xfffff) == 0)
9927 printf ("%10" PRIu64
" MB", sz
>> 20);
9928 else if ((sz
& 0x3ff) == 0)
9929 printf ("%10" PRIu64
" KB", sz
>> 10);
9931 printf (" %10" PRIu64
" B", sz
);
9934 /* Implement --print-memory-usage: disply per region memory usage. */
9937 lang_print_memory_usage (void)
9939 lang_memory_region_type
*r
;
9941 printf ("Memory region Used Size Region Size %%age Used\n");
9942 for (r
= lang_memory_region_list
; r
->next
!= NULL
; r
= r
->next
)
9944 bfd_vma used_length
= r
->current
- r
->origin
;
9946 printf ("%16s: ",r
->name_list
.name
);
9947 lang_print_memory_size (used_length
);
9948 lang_print_memory_size (r
->length
);
9952 double percent
= used_length
* 100.0 / r
->length
;
9953 printf (" %6.2f%%", percent
);