1 /* Linker command language support.
2 Copyright (C) 1991-2023 Free Software Foundation, Inc.
4 This file is part of the GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
24 #include "libiberty.h"
25 #include "filenames.h"
26 #include "safe-ctype.h"
45 #if BFD_SUPPORTS_PLUGINS
47 #endif /* BFD_SUPPORTS_PLUGINS */
50 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
53 /* Convert between addresses in bytes and sizes in octets.
54 For currently supported targets, octets_per_byte is always a power
55 of two, so we can use shifts. */
56 #define TO_ADDR(X) ((X) >> opb_shift)
57 #define TO_SIZE(X) ((X) << opb_shift)
59 /* Local variables. */
60 static struct obstack stat_obstack
;
61 static struct obstack map_obstack
;
62 static struct obstack pt_obstack
;
64 #define obstack_chunk_alloc xmalloc
65 #define obstack_chunk_free free
66 static const char *entry_symbol_default
= "start";
67 static bool map_head_is_link_order
= false;
68 static lang_output_section_statement_type
*default_common_section
;
69 static bool map_option_f
;
70 static bfd_vma print_dot
;
71 static lang_input_statement_type
*first_file
;
72 static const char *current_target
;
73 static lang_statement_list_type
*stat_save
[10];
74 static lang_statement_list_type
**stat_save_ptr
= &stat_save
[0];
75 static struct unique_sections
*unique_section_list
;
76 static struct asneeded_minfo
*asneeded_list_head
;
77 static unsigned int opb_shift
= 0;
79 /* Forward declarations. */
80 static void exp_init_os (etree_type
*);
81 static lang_input_statement_type
*lookup_name (const char *);
82 static void insert_undefined (const char *);
83 static bool sort_def_symbol (struct bfd_link_hash_entry
*, void *);
84 static lang_statement_union_type
*new_statement (enum statement_enum type
,
86 lang_statement_list_type
*list
);
87 static void print_statement (lang_statement_union_type
*,
88 lang_output_section_statement_type
*);
89 static void print_statement_list (lang_statement_union_type
*,
90 lang_output_section_statement_type
*);
91 static void print_statements (void);
92 static void print_input_section (asection
*, bool);
93 static bool lang_one_common (struct bfd_link_hash_entry
*, void *);
94 static void lang_record_phdrs (void);
95 static void lang_do_version_exports_section (void);
96 static void lang_finalize_version_expr_head
97 (struct bfd_elf_version_expr_head
*);
98 static void lang_do_memory_regions (bool);
100 /* Exported variables. */
101 const char *output_target
;
102 lang_output_section_statement_type
*abs_output_section
;
103 /* Header for list of statements corresponding to any files involved in the
104 link, either specified from the command-line or added implicitely (eg.
105 archive member used to resolved undefined symbol, wildcard statement from
106 linker script, etc.). Next pointer is in next field of a
107 lang_statement_header_type (reached via header field in a
108 lang_statement_union). */
109 lang_statement_list_type statement_list
;
110 lang_statement_list_type lang_os_list
;
111 lang_statement_list_type
*stat_ptr
= &statement_list
;
112 /* Header for list of statements corresponding to files used in the final
113 executable. This can be either object file specified on the command-line
114 or library member resolving an undefined reference. Next pointer is in next
115 field of a lang_input_statement_type (reached via input_statement field in a
116 lang_statement_union). */
117 lang_statement_list_type file_chain
= { NULL
, NULL
};
118 /* Header for list of statements corresponding to files specified on the
119 command-line for linking. It thus contains real object files and archive
120 but not archive members. Next pointer is in next_real_file field of a
121 lang_input_statement_type statement (reached via input_statement field in a
122 lang_statement_union). */
123 lang_statement_list_type input_file_chain
;
124 static const char *current_input_file
;
125 struct bfd_elf_dynamic_list
**current_dynamic_list_p
;
126 struct bfd_sym_chain entry_symbol
= { NULL
, NULL
};
127 const char *entry_section
= ".text";
128 struct lang_input_statement_flags input_flags
;
129 bool entry_from_cmdline
;
130 bool lang_has_input_file
= false;
131 bool had_output_filename
= false;
132 bool lang_float_flag
= false;
133 bool delete_output_file_on_failure
= false;
134 struct lang_phdr
*lang_phdr_list
;
135 struct lang_nocrossrefs
*nocrossref_list
;
136 struct asneeded_minfo
**asneeded_list_tail
;
138 static ctf_dict_t
*ctf_output
;
141 /* Functions that traverse the linker script and might evaluate
142 DEFINED() need to increment this at the start of the traversal. */
143 int lang_statement_iteration
= 0;
145 /* Count times through one_lang_size_sections_pass after mark phase. */
146 static int lang_sizing_iteration
= 0;
148 /* Return TRUE if the PATTERN argument is a wildcard pattern.
149 Although backslashes are treated specially if a pattern contains
150 wildcards, we do not consider the mere presence of a backslash to
151 be enough to cause the pattern to be treated as a wildcard.
152 That lets us handle DOS filenames more naturally. */
153 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
155 #define new_stat(x, y) \
156 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
158 #define outside_section_address(q) \
159 ((q)->output_offset + (q)->output_section->vma)
161 #define outside_symbol_address(q) \
162 ((q)->value + outside_section_address (q->section))
164 /* CTF sections smaller than this are not compressed: compression of
165 dictionaries this small doesn't gain much, and this lets consumers mmap the
166 sections directly out of the ELF file and use them with no decompression
167 overhead if they want to. */
168 #define CTF_COMPRESSION_THRESHOLD 4096
171 stat_alloc (size_t size
)
173 return obstack_alloc (&stat_obstack
, size
);
176 /* Code for handling simple wildcards without going through fnmatch,
177 which can be expensive because of charset translations etc. */
179 /* A simple wild is a literal string followed by a single '*',
180 where the literal part is at least 4 characters long. */
183 is_simple_wild (const char *name
)
185 size_t len
= strcspn (name
, "*?[");
186 return len
>= 4 && name
[len
] == '*' && name
[len
+ 1] == '\0';
190 match_simple_wild (const char *pattern
, const char *name
)
192 /* The first four characters of the pattern are guaranteed valid
193 non-wildcard characters. So we can go faster. */
194 if (pattern
[0] != name
[0] || pattern
[1] != name
[1]
195 || pattern
[2] != name
[2] || pattern
[3] != name
[3])
200 while (*pattern
!= '*')
201 if (*name
++ != *pattern
++)
208 name_match (const char *pattern
, const char *name
)
210 if (is_simple_wild (pattern
))
211 return !match_simple_wild (pattern
, name
);
212 if (wildcardp (pattern
))
213 return fnmatch (pattern
, name
, 0);
214 return strcmp (pattern
, name
);
217 /* Given an analyzed wildcard_spec SPEC, match it against NAME,
218 returns zero on a match, non-zero if there's no match. */
221 spec_match (const struct wildcard_spec
*spec
, const char *name
)
223 size_t nl
= spec
->namelen
;
224 size_t pl
= spec
->prefixlen
;
225 size_t sl
= spec
->suffixlen
;
226 size_t inputlen
= strlen (name
);
234 r
= memcmp (spec
->name
, name
, pl
);
244 r
= memcmp (spec
->name
+ nl
- sl
, name
+ inputlen
- sl
, sl
);
249 if (nl
== pl
+ sl
+ 1 && spec
->name
[pl
] == '*')
253 return fnmatch (spec
->name
+ pl
, name
+ pl
, 0);
262 ldirname (const char *name
)
264 const char *base
= lbasename (name
);
267 while (base
> name
&& IS_DIR_SEPARATOR (base
[-1]))
271 dirname
= strdup (name
);
272 dirname
[base
- name
] = '\0';
276 /* If PATTERN is of the form archive:file, return a pointer to the
277 separator. If not, return NULL. */
280 archive_path (const char *pattern
)
284 if (link_info
.path_separator
== 0)
287 p
= strchr (pattern
, link_info
.path_separator
);
288 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
289 if (p
== NULL
|| link_info
.path_separator
!= ':')
292 /* Assume a match on the second char is part of drive specifier,
293 as in "c:\silly.dos". */
294 if (p
== pattern
+ 1 && ISALPHA (*pattern
))
295 p
= strchr (p
+ 1, link_info
.path_separator
);
300 /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path,
301 return whether F matches FILE_SPEC. */
304 input_statement_is_archive_path (const char *file_spec
, char *sep
,
305 lang_input_statement_type
*f
)
310 || name_match (sep
+ 1, f
->filename
) == 0)
311 && ((sep
!= file_spec
)
312 == (f
->the_bfd
!= NULL
&& f
->the_bfd
->my_archive
!= NULL
)))
316 if (sep
!= file_spec
)
318 const char *aname
= bfd_get_filename (f
->the_bfd
->my_archive
);
320 match
= name_match (file_spec
, aname
) == 0;
321 *sep
= link_info
.path_separator
;
328 unique_section_p (const asection
*sec
,
329 const lang_output_section_statement_type
*os
)
331 struct unique_sections
*unam
;
334 if (!link_info
.resolve_section_groups
335 && sec
->owner
!= NULL
336 && bfd_is_group_section (sec
->owner
, sec
))
338 && strcmp (os
->name
, DISCARD_SECTION_NAME
) == 0);
341 for (unam
= unique_section_list
; unam
; unam
= unam
->next
)
342 if (name_match (unam
->name
, secnam
) == 0)
348 /* Generic traversal routines for finding matching sections. */
350 /* Return true if FILE matches a pattern in EXCLUDE_LIST, otherwise return
354 walk_wild_file_in_exclude_list (struct name_list
*exclude_list
,
355 lang_input_statement_type
*file
)
357 struct name_list
*list_tmp
;
359 for (list_tmp
= exclude_list
;
361 list_tmp
= list_tmp
->next
)
363 char *p
= archive_path (list_tmp
->name
);
367 if (input_statement_is_archive_path (list_tmp
->name
, p
, file
))
371 else if (name_match (list_tmp
->name
, file
->filename
) == 0)
374 /* FIXME: Perhaps remove the following at some stage? Matching
375 unadorned archives like this was never documented and has
376 been superceded by the archive:path syntax. */
377 else if (file
->the_bfd
!= NULL
378 && file
->the_bfd
->my_archive
!= NULL
379 && name_match (list_tmp
->name
,
380 bfd_get_filename (file
->the_bfd
->my_archive
)) == 0)
387 /* Add SECTION (from input FILE) to the list of matching sections
388 within PTR (the matching wildcard is SEC). */
391 add_matching_section (lang_wild_statement_type
*ptr
,
392 struct wildcard_list
*sec
,
394 lang_input_statement_type
*file
)
396 lang_input_matcher_type
*new_section
;
397 /* Add a section reference to the list. */
398 new_section
= new_stat (lang_input_matcher
, &ptr
->matching_sections
);
399 new_section
->section
= section
;
400 new_section
->pattern
= sec
;
401 new_section
->input_stmt
= file
;
404 /* Process section S (from input file FILE) in relation to wildcard
405 statement PTR. We already know that a prefix of the name of S matches
406 some wildcard in PTR's wildcard list. Here we check if the filename
407 matches as well (if it's specified) and if any of the wildcards in fact
411 walk_wild_section_match (lang_wild_statement_type
*ptr
,
412 lang_input_statement_type
*file
,
415 struct wildcard_list
*sec
;
416 const char *file_spec
= ptr
->filename
;
419 /* Check if filenames match. */
420 if (file_spec
== NULL
)
422 else if ((p
= archive_path (file_spec
)) != NULL
)
424 if (!input_statement_is_archive_path (file_spec
, p
, file
))
427 else if (wildcardp (file_spec
))
429 if (fnmatch (file_spec
, file
->filename
, 0) != 0)
434 lang_input_statement_type
*f
;
435 /* Perform the iteration over a single file. */
436 f
= lookup_name (file_spec
);
441 /* If filename is excluded we're done. */
442 if (walk_wild_file_in_exclude_list (ptr
->exclude_name_list
, file
))
445 /* Check section name against each wildcard spec. If there's no
446 wildcard all sections match. */
447 sec
= ptr
->section_list
;
449 add_matching_section (ptr
, sec
, s
, file
);
452 const char *sname
= bfd_section_name (s
);
453 for (; sec
!= NULL
; sec
= sec
->next
)
455 if (sec
->spec
.name
!= NULL
456 && spec_match (&sec
->spec
, sname
) != 0)
459 /* Don't process sections from files which were excluded. */
460 if (!walk_wild_file_in_exclude_list (sec
->spec
.exclude_name_list
,
462 add_matching_section (ptr
, sec
, s
, file
);
467 /* Return the numerical value of the init_priority attribute from
468 section name NAME. */
471 get_init_priority (const asection
*sec
)
473 const char *name
= bfd_section_name (sec
);
476 /* GCC uses the following section names for the init_priority
477 attribute with numerical values 101 to 65535 inclusive. A
478 lower value means a higher priority.
480 1: .init_array.NNNNN/.fini_array.NNNNN: Where NNNNN is the
481 decimal numerical value of the init_priority attribute.
482 The order of execution in .init_array is forward and
483 .fini_array is backward.
484 2: .ctors.NNNNN/.dtors.NNNNN: Where NNNNN is 65535 minus the
485 decimal numerical value of the init_priority attribute.
486 The order of execution in .ctors is backward and .dtors
489 .init_array.NNNNN sections would normally be placed in an output
490 .init_array section, .fini_array.NNNNN in .fini_array,
491 .ctors.NNNNN in .ctors, and .dtors.NNNNN in .dtors. This means
492 we should sort by increasing number (and could just use
493 SORT_BY_NAME in scripts). However if .ctors.NNNNN sections are
494 being placed in .init_array (which may also contain
495 .init_array.NNNNN sections) or .dtors.NNNNN sections are being
496 placed in .fini_array then we need to extract the init_priority
497 attribute and sort on that. */
498 dot
= strrchr (name
, '.');
499 if (dot
!= NULL
&& ISDIGIT (dot
[1]))
502 unsigned long init_priority
= strtoul (dot
+ 1, &end
, 10);
506 && (strncmp (name
, ".ctors", 6) == 0
507 || strncmp (name
, ".dtors", 6) == 0))
508 init_priority
= 65535 - init_priority
;
509 if (init_priority
<= INT_MAX
)
510 return init_priority
;
516 /* Compare sections ASEC and BSEC according to SORT. */
519 compare_section (sort_type sort
, asection
*asec
, asection
*bsec
)
522 int a_priority
, b_priority
;
529 case by_init_priority
:
530 a_priority
= get_init_priority (asec
);
531 b_priority
= get_init_priority (bsec
);
532 if (a_priority
< 0 || b_priority
< 0)
534 ret
= a_priority
- b_priority
;
540 case by_alignment_name
:
541 ret
= bfd_section_alignment (bsec
) - bfd_section_alignment (asec
);
548 ret
= strcmp (bfd_section_name (asec
), bfd_section_name (bsec
));
551 case by_name_alignment
:
552 ret
= strcmp (bfd_section_name (asec
), bfd_section_name (bsec
));
558 ret
= bfd_section_alignment (bsec
) - bfd_section_alignment (asec
);
565 /* PE puts the sort key in the input statement. */
568 sort_filename (bfd
*abfd
)
570 lang_input_statement_type
*is
= bfd_usrdata (abfd
);
573 return bfd_get_filename (abfd
);
576 /* Handle wildcard sorting. This returns the place in a binary search tree
577 where this FILE:SECTION should be inserted for wild statement WILD where
578 the spec SEC was the matching one. The tree is later linearized. */
580 static lang_section_bst_type
**
581 wild_sort (lang_wild_statement_type
*wild
,
582 struct wildcard_list
*sec
,
583 lang_input_statement_type
*file
,
586 lang_section_bst_type
**tree
;
588 if (!wild
->filenames_sorted
589 && (sec
== NULL
|| sec
->spec
.sorted
== none
590 || sec
->spec
.sorted
== by_none
))
592 /* We might be called even if _this_ spec doesn't need sorting,
593 in which case we simply append at the right end of tree. */
594 return wild
->rightmost
;
600 /* Sorting by filename takes precedence over sorting by section
603 if (wild
->filenames_sorted
)
608 asection
*lsec
= (*tree
)->section
;
610 /* The PE support for the .idata section as generated by
611 dlltool assumes that files will be sorted by the name of
612 the archive and then the name of the file within the
615 fa
= file
->the_bfd
->my_archive
!= NULL
;
617 fn
= sort_filename (file
->the_bfd
->my_archive
);
619 fn
= sort_filename (file
->the_bfd
);
621 la
= lsec
->owner
->my_archive
!= NULL
;
623 ln
= sort_filename (lsec
->owner
->my_archive
);
625 ln
= sort_filename (lsec
->owner
);
627 i
= filename_cmp (fn
, ln
);
629 { tree
= &((*tree
)->right
); continue; }
631 { tree
= &((*tree
)->left
); continue; }
636 fn
= sort_filename (file
->the_bfd
);
638 ln
= sort_filename (lsec
->owner
);
640 i
= filename_cmp (fn
, ln
);
642 { tree
= &((*tree
)->right
); continue; }
644 { tree
= &((*tree
)->left
); continue; }
648 /* Here either the files are not sorted by name, or we are
649 looking at the sections for this file. */
651 /* Find the correct node to append this section. */
652 if (sec
&& sec
->spec
.sorted
!= none
&& sec
->spec
.sorted
!= by_none
653 && compare_section (sec
->spec
.sorted
, section
, (*tree
)->section
) < 0)
654 tree
= &((*tree
)->left
);
656 tree
= &((*tree
)->right
);
662 /* Use wild_sort to build a BST to sort sections. */
665 output_section_callback_sort (lang_wild_statement_type
*ptr
,
666 struct wildcard_list
*sec
,
668 lang_input_statement_type
*file
,
671 lang_section_bst_type
*node
;
672 lang_section_bst_type
**tree
;
673 lang_output_section_statement_type
*os
;
675 os
= (lang_output_section_statement_type
*) output
;
677 if (unique_section_p (section
, os
))
680 node
= (lang_section_bst_type
*) xmalloc (sizeof (lang_section_bst_type
));
683 node
->section
= section
;
684 node
->pattern
= ptr
->section_list
;
686 tree
= wild_sort (ptr
, sec
, file
, section
);
690 if (tree
== ptr
->rightmost
)
691 ptr
->rightmost
= &node
->right
;
695 /* Convert a sorted sections' BST back to list form. */
698 output_section_callback_tree_to_list (lang_wild_statement_type
*ptr
,
699 lang_section_bst_type
*tree
,
703 output_section_callback_tree_to_list (ptr
, tree
->left
, output
);
705 lang_add_section (&ptr
->children
, tree
->section
, tree
->pattern
,
706 ptr
->section_flag_list
,
707 (lang_output_section_statement_type
*) output
);
710 output_section_callback_tree_to_list (ptr
, tree
->right
, output
);
716 /* Sections are matched against wildcard statements via a prefix tree.
717 The prefix tree holds prefixes of all matching patterns (up to the first
718 wildcard character), and the wild statement from which those patterns
719 came. When matching a section name against the tree we're walking through
720 the tree character by character. Each statement we hit is one that
721 potentially matches. This is checked by actually going through the
722 (glob) matching routines.
724 When the section name turns out to actually match we record that section
725 in the wild statements list of matching sections. */
727 /* A prefix can be matched by multiple statement, so we need a list of them. */
728 struct wild_stmt_list
730 lang_wild_statement_type
*stmt
;
731 struct wild_stmt_list
*next
;
734 /* The prefix tree itself. */
737 /* The list of all children (linked via .next). */
738 struct prefixtree
*child
;
739 struct prefixtree
*next
;
740 /* This tree node is responsible for the prefix of parent plus 'c'. */
742 /* The statements that potentially can match this prefix. */
743 struct wild_stmt_list
*stmt
;
746 /* We always have a root node in the prefix tree. It corresponds to the
747 empty prefix. E.g. a glob like "*" would sit in this root. */
748 static struct prefixtree the_root
, *ptroot
= &the_root
;
750 /* Given a prefix tree in *TREE, corresponding to prefix P, find or
751 INSERT the tree node corresponding to prefix P+C. */
753 static struct prefixtree
*
754 get_prefix_tree (struct prefixtree
**tree
, char c
, bool insert
)
756 struct prefixtree
*t
;
757 for (t
= *tree
; t
; t
= t
->next
)
762 t
= (struct prefixtree
*) obstack_alloc (&pt_obstack
, sizeof *t
);
771 /* Add STMT to the set of statements that can be matched by the prefix
772 corresponding to prefix tree T. */
775 pt_add_stmt (struct prefixtree
*t
, lang_wild_statement_type
*stmt
)
777 struct wild_stmt_list
*sl
, **psl
;
778 sl
= (struct wild_stmt_list
*) obstack_alloc (&pt_obstack
, sizeof *sl
);
787 /* Insert STMT into the global prefix tree. */
790 insert_prefix_tree (lang_wild_statement_type
*stmt
)
792 struct wildcard_list
*sec
;
793 struct prefixtree
*t
;
795 if (!stmt
->section_list
)
797 /* If we have no section_list (no wildcards in the wild STMT),
798 then every section name will match, so add this to the root. */
799 pt_add_stmt (ptroot
, stmt
);
803 for (sec
= stmt
->section_list
; sec
; sec
= sec
->next
)
805 const char *name
= sec
->spec
.name
? sec
->spec
.name
: "*";
808 for (; (c
= *name
); name
++)
810 if (c
== '*' || c
== '[' || c
== '?')
812 t
= get_prefix_tree (&t
->child
, c
, true);
814 /* If we hit a glob character, the matching prefix is what we saw
815 until now. If we hit the end of pattern (hence it's no glob) then
816 we can do better: we only need to record a match when a section name
817 completely matches, not merely a prefix, so record the trailing 0
820 t
= get_prefix_tree (&t
->child
, 0, true);
821 pt_add_stmt (t
, stmt
);
825 /* Dump T indented by INDENT spaces. */
828 debug_prefix_tree_rec (struct prefixtree
*t
, int indent
)
830 for (; t
; t
= t
->next
)
832 struct wild_stmt_list
*sl
;
833 printf ("%*s %c", indent
, "", t
->c
);
834 for (sl
= t
->stmt
; sl
; sl
= sl
->next
)
836 struct wildcard_list
*curr
;
837 printf (" %p ", sl
->stmt
);
838 for (curr
= sl
->stmt
->section_list
; curr
; curr
= curr
->next
)
839 printf ("%s ", curr
->spec
.name
? curr
->spec
.name
: "*");
842 debug_prefix_tree_rec (t
->child
, indent
+ 2);
846 /* Dump the global prefix tree. */
849 debug_prefix_tree (void)
851 debug_prefix_tree_rec (ptroot
, 2);
854 /* Like strcspn() but start to look from the end to beginning of
855 S. Returns the length of the suffix of S consisting entirely
856 of characters not in REJECT. */
859 rstrcspn (const char *s
, const char *reject
)
861 size_t len
= strlen (s
), sufflen
= 0;
865 if (strchr (reject
, c
) != 0)
872 /* Analyze the wildcards in wild statement PTR to setup various
873 things for quick matching. */
876 analyze_walk_wild_section_handler (lang_wild_statement_type
*ptr
)
878 struct wildcard_list
*sec
;
881 ptr
->rightmost
= &ptr
->tree
;
883 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
887 sec
->spec
.namelen
= strlen (sec
->spec
.name
);
888 sec
->spec
.prefixlen
= strcspn (sec
->spec
.name
, "?*[");
889 sec
->spec
.suffixlen
= rstrcspn (sec
->spec
.name
+ sec
->spec
.prefixlen
,
893 sec
->spec
.namelen
= sec
->spec
.prefixlen
= sec
->spec
.suffixlen
= 0;
896 insert_prefix_tree (ptr
);
899 /* Match all sections from FILE against the global prefix tree,
900 and record them into each wild statement that has a match. */
903 resolve_wild_sections (lang_input_statement_type
*file
)
907 if (file
->flags
.just_syms
)
910 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
912 const char *sname
= bfd_section_name (s
);
914 struct prefixtree
*t
= ptroot
;
915 //printf (" YYY consider %s of %s\n", sname, file->the_bfd->filename);
920 struct wild_stmt_list
*sl
;
921 for (sl
= t
->stmt
; sl
; sl
= sl
->next
)
923 walk_wild_section_match (sl
->stmt
, file
, s
);
924 //printf (" ZZZ maybe place into %p\n", sl->stmt);
930 t
= get_prefix_tree (&t
->child
, c
, false);
936 /* Match all sections from all input files against the global prefix tree. */
941 LANG_FOR_EACH_INPUT_STATEMENT (f
)
943 //printf("XXX %s\n", f->filename);
944 if (f
->the_bfd
== NULL
945 || !bfd_check_format (f
->the_bfd
, bfd_archive
))
946 resolve_wild_sections (f
);
951 /* This is an archive file. We must map each member of the
952 archive separately. */
953 member
= bfd_openr_next_archived_file (f
->the_bfd
, NULL
);
954 while (member
!= NULL
)
956 /* When lookup_name is called, it will call the add_symbols
957 entry point for the archive. For each element of the
958 archive which is included, BFD will call ldlang_add_file,
959 which will set the usrdata field of the member to the
960 lang_input_statement. */
961 if (bfd_usrdata (member
) != NULL
)
962 resolve_wild_sections (bfd_usrdata (member
));
964 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
970 /* For each input section that matches wild statement S calls
971 CALLBACK with DATA. */
974 walk_wild (lang_wild_statement_type
*s
, callback_t callback
, void *data
)
976 lang_statement_union_type
*l
;
978 for (l
= s
->matching_sections
.head
; l
; l
= l
->header
.next
)
980 (*callback
) (s
, l
->input_matcher
.pattern
, l
->input_matcher
.section
,
981 l
->input_matcher
.input_stmt
, data
);
985 /* lang_for_each_statement walks the parse tree and calls the provided
986 function for each node, except those inside output section statements
987 with constraint set to -1. */
990 lang_for_each_statement_worker (void (*func
) (lang_statement_union_type
*),
991 lang_statement_union_type
*s
)
993 for (; s
!= NULL
; s
= s
->header
.next
)
997 switch (s
->header
.type
)
999 case lang_constructors_statement_enum
:
1000 lang_for_each_statement_worker (func
, constructor_list
.head
);
1002 case lang_output_section_statement_enum
:
1003 if (s
->output_section_statement
.constraint
!= -1)
1004 lang_for_each_statement_worker
1005 (func
, s
->output_section_statement
.children
.head
);
1007 case lang_wild_statement_enum
:
1008 lang_for_each_statement_worker (func
,
1009 s
->wild_statement
.children
.head
);
1011 case lang_group_statement_enum
:
1012 lang_for_each_statement_worker (func
,
1013 s
->group_statement
.children
.head
);
1015 case lang_data_statement_enum
:
1016 case lang_reloc_statement_enum
:
1017 case lang_object_symbols_statement_enum
:
1018 case lang_output_statement_enum
:
1019 case lang_target_statement_enum
:
1020 case lang_input_section_enum
:
1021 case lang_input_statement_enum
:
1022 case lang_assignment_statement_enum
:
1023 case lang_padding_statement_enum
:
1024 case lang_address_statement_enum
:
1025 case lang_fill_statement_enum
:
1026 case lang_insert_statement_enum
:
1036 lang_for_each_statement (void (*func
) (lang_statement_union_type
*))
1038 lang_for_each_statement_worker (func
, statement_list
.head
);
1041 /*----------------------------------------------------------------------*/
1044 lang_list_init (lang_statement_list_type
*list
)
1047 list
->tail
= &list
->head
;
1051 lang_statement_append (lang_statement_list_type
*list
,
1055 *(list
->tail
) = element
;
1060 push_stat_ptr (lang_statement_list_type
*new_ptr
)
1062 if (stat_save_ptr
>= stat_save
+ sizeof (stat_save
) / sizeof (stat_save
[0]))
1064 *stat_save_ptr
++ = stat_ptr
;
1071 if (stat_save_ptr
<= stat_save
)
1073 stat_ptr
= *--stat_save_ptr
;
1076 /* Build a new statement node for the parse tree. */
1078 static lang_statement_union_type
*
1079 new_statement (enum statement_enum type
,
1081 lang_statement_list_type
*list
)
1083 lang_statement_union_type
*new_stmt
;
1085 new_stmt
= stat_alloc (size
);
1086 new_stmt
->header
.type
= type
;
1087 new_stmt
->header
.next
= NULL
;
1088 lang_statement_append (list
, new_stmt
, &new_stmt
->header
.next
);
1092 /* Build a new input file node for the language. There are several
1093 ways in which we treat an input file, eg, we only look at symbols,
1094 or prefix it with a -l etc.
1096 We can be supplied with requests for input files more than once;
1097 they may, for example be split over several lines like foo.o(.text)
1098 foo.o(.data) etc, so when asked for a file we check that we haven't
1099 got it already so we don't duplicate the bfd. */
1101 static lang_input_statement_type
*
1102 new_afile (const char *name
,
1103 lang_input_file_enum_type file_type
,
1105 const char *from_filename
)
1107 lang_input_statement_type
*p
;
1109 lang_has_input_file
= true;
1111 p
= new_stat (lang_input_statement
, stat_ptr
);
1112 memset (&p
->the_bfd
, 0,
1113 sizeof (*p
) - offsetof (lang_input_statement_type
, the_bfd
));
1114 p
->extra_search_path
= NULL
;
1116 p
->flags
.dynamic
= input_flags
.dynamic
;
1117 p
->flags
.add_DT_NEEDED_for_dynamic
= input_flags
.add_DT_NEEDED_for_dynamic
;
1118 p
->flags
.add_DT_NEEDED_for_regular
= input_flags
.add_DT_NEEDED_for_regular
;
1119 p
->flags
.whole_archive
= input_flags
.whole_archive
;
1120 p
->flags
.sysrooted
= input_flags
.sysrooted
;
1125 case lang_input_file_is_symbols_only_enum
:
1127 p
->local_sym_name
= name
;
1128 p
->flags
.real
= true;
1129 p
->flags
.just_syms
= true;
1131 case lang_input_file_is_fake_enum
:
1133 p
->local_sym_name
= name
;
1135 case lang_input_file_is_l_enum
:
1136 if (name
[0] == ':' && name
[1] != '\0')
1138 p
->filename
= name
+ 1;
1139 p
->flags
.full_name_provided
= true;
1143 p
->local_sym_name
= concat ("-l", name
, (const char *) NULL
);
1144 p
->flags
.maybe_archive
= true;
1145 p
->flags
.real
= true;
1146 p
->flags
.search_dirs
= true;
1148 case lang_input_file_is_marker_enum
:
1150 p
->local_sym_name
= name
;
1151 p
->flags
.search_dirs
= true;
1153 case lang_input_file_is_search_file_enum
:
1155 p
->local_sym_name
= name
;
1156 /* If name is a relative path, search the directory of the current linker
1158 if (from_filename
&& !IS_ABSOLUTE_PATH (name
))
1159 p
->extra_search_path
= ldirname (from_filename
);
1160 p
->flags
.real
= true;
1161 p
->flags
.search_dirs
= true;
1163 case lang_input_file_is_file_enum
:
1165 p
->local_sym_name
= name
;
1166 p
->flags
.real
= true;
1172 lang_statement_append (&input_file_chain
, p
, &p
->next_real_file
);
1176 lang_input_statement_type
*
1177 lang_add_input_file (const char *name
,
1178 lang_input_file_enum_type file_type
,
1182 && (*name
== '=' || startswith (name
, "$SYSROOT")))
1184 lang_input_statement_type
*ret
;
1185 char *sysrooted_name
1186 = concat (ld_sysroot
,
1187 name
+ (*name
== '=' ? 1 : strlen ("$SYSROOT")),
1188 (const char *) NULL
);
1190 /* We've now forcibly prepended the sysroot, making the input
1191 file independent of the context. Therefore, temporarily
1192 force a non-sysrooted context for this statement, so it won't
1193 get the sysroot prepended again when opened. (N.B. if it's a
1194 script, any child nodes with input files starting with "/"
1195 will be handled as "sysrooted" as they'll be found to be
1196 within the sysroot subdirectory.) */
1197 unsigned int outer_sysrooted
= input_flags
.sysrooted
;
1198 input_flags
.sysrooted
= 0;
1199 ret
= new_afile (sysrooted_name
, file_type
, target
, NULL
);
1200 input_flags
.sysrooted
= outer_sysrooted
;
1204 return new_afile (name
, file_type
, target
, current_input_file
);
1207 struct out_section_hash_entry
1209 struct bfd_hash_entry root
;
1210 lang_statement_union_type s
;
1213 /* The hash table. */
1215 static struct bfd_hash_table output_section_statement_table
;
1217 /* Support routines for the hash table used by lang_output_section_find,
1218 initialize the table, fill in an entry and remove the table. */
1220 static struct bfd_hash_entry
*
1221 output_section_statement_newfunc (struct bfd_hash_entry
*entry
,
1222 struct bfd_hash_table
*table
,
1225 lang_output_section_statement_type
**nextp
;
1226 struct out_section_hash_entry
*ret
;
1230 entry
= (struct bfd_hash_entry
*) bfd_hash_allocate (table
,
1236 entry
= bfd_hash_newfunc (entry
, table
, string
);
1240 ret
= (struct out_section_hash_entry
*) entry
;
1241 memset (&ret
->s
, 0, sizeof (ret
->s
));
1242 ret
->s
.header
.type
= lang_output_section_statement_enum
;
1243 ret
->s
.output_section_statement
.subsection_alignment
= NULL
;
1244 ret
->s
.output_section_statement
.section_alignment
= NULL
;
1245 ret
->s
.output_section_statement
.block_value
= 1;
1246 lang_list_init (&ret
->s
.output_section_statement
.children
);
1247 lang_statement_append (stat_ptr
, &ret
->s
, &ret
->s
.header
.next
);
1249 /* For every output section statement added to the list, except the
1250 first one, lang_os_list.tail points to the "next"
1251 field of the last element of the list. */
1252 if (lang_os_list
.head
!= NULL
)
1253 ret
->s
.output_section_statement
.prev
1254 = ((lang_output_section_statement_type
*)
1255 ((char *) lang_os_list
.tail
1256 - offsetof (lang_output_section_statement_type
, next
)));
1258 /* GCC's strict aliasing rules prevent us from just casting the
1259 address, so we store the pointer in a variable and cast that
1261 nextp
= &ret
->s
.output_section_statement
.next
;
1262 lang_statement_append (&lang_os_list
, &ret
->s
, nextp
);
1267 output_section_statement_table_init (void)
1269 if (!bfd_hash_table_init_n (&output_section_statement_table
,
1270 output_section_statement_newfunc
,
1271 sizeof (struct out_section_hash_entry
),
1273 einfo (_("%F%P: can not create hash table: %E\n"));
1277 output_section_statement_table_free (void)
1279 bfd_hash_table_free (&output_section_statement_table
);
1282 /* Build enough state so that the parser can build its tree. */
1287 obstack_begin (&stat_obstack
, 1000);
1288 obstack_init (&pt_obstack
);
1290 stat_ptr
= &statement_list
;
1292 output_section_statement_table_init ();
1294 lang_list_init (stat_ptr
);
1296 lang_list_init (&input_file_chain
);
1297 lang_list_init (&lang_os_list
);
1298 lang_list_init (&file_chain
);
1299 first_file
= lang_add_input_file (NULL
, lang_input_file_is_marker_enum
,
1301 abs_output_section
=
1302 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
, 0, 1);
1304 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
1306 asneeded_list_head
= NULL
;
1307 asneeded_list_tail
= &asneeded_list_head
;
1313 output_section_statement_table_free ();
1316 /*----------------------------------------------------------------------
1317 A region is an area of memory declared with the
1318 MEMORY { name:org=exp, len=exp ... }
1321 We maintain a list of all the regions here.
1323 If no regions are specified in the script, then the default is used
1324 which is created when looked up to be the entire data space.
1326 If create is true we are creating a region inside a MEMORY block.
1327 In this case it is probably an error to create a region that has
1328 already been created. If we are not inside a MEMORY block it is
1329 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
1330 and so we issue a warning.
1332 Each region has at least one name. The first name is either
1333 DEFAULT_MEMORY_REGION or the name given in the MEMORY block. You can add
1334 alias names to an existing region within a script with
1335 REGION_ALIAS (alias, region_name). Each name corresponds to at most one
1338 static lang_memory_region_type
*lang_memory_region_list
;
1339 static lang_memory_region_type
**lang_memory_region_list_tail
1340 = &lang_memory_region_list
;
1342 lang_memory_region_type
*
1343 lang_memory_region_lookup (const char *const name
, bool create
)
1345 lang_memory_region_name
*n
;
1346 lang_memory_region_type
*r
;
1347 lang_memory_region_type
*new_region
;
1349 /* NAME is NULL for LMA memspecs if no region was specified. */
1353 for (r
= lang_memory_region_list
; r
!= NULL
; r
= r
->next
)
1354 for (n
= &r
->name_list
; n
!= NULL
; n
= n
->next
)
1355 if (strcmp (n
->name
, name
) == 0)
1358 einfo (_("%P:%pS: warning: redeclaration of memory region `%s'\n"),
1363 if (!create
&& strcmp (name
, DEFAULT_MEMORY_REGION
))
1364 einfo (_("%P:%pS: warning: memory region `%s' not declared\n"),
1367 new_region
= stat_alloc (sizeof (lang_memory_region_type
));
1369 new_region
->name_list
.name
= xstrdup (name
);
1370 new_region
->name_list
.next
= NULL
;
1371 new_region
->next
= NULL
;
1372 new_region
->origin_exp
= NULL
;
1373 new_region
->origin
= 0;
1374 new_region
->length_exp
= NULL
;
1375 new_region
->length
= ~(bfd_size_type
) 0;
1376 new_region
->current
= 0;
1377 new_region
->last_os
= NULL
;
1378 new_region
->flags
= 0;
1379 new_region
->not_flags
= 0;
1380 new_region
->had_full_message
= false;
1382 *lang_memory_region_list_tail
= new_region
;
1383 lang_memory_region_list_tail
= &new_region
->next
;
1389 lang_memory_region_alias (const char *alias
, const char *region_name
)
1391 lang_memory_region_name
*n
;
1392 lang_memory_region_type
*r
;
1393 lang_memory_region_type
*region
;
1395 /* The default region must be unique. This ensures that it is not necessary
1396 to iterate through the name list if someone wants the check if a region is
1397 the default memory region. */
1398 if (strcmp (region_name
, DEFAULT_MEMORY_REGION
) == 0
1399 || strcmp (alias
, DEFAULT_MEMORY_REGION
) == 0)
1400 einfo (_("%F%P:%pS: error: alias for default memory region\n"), NULL
);
1402 /* Look for the target region and check if the alias is not already
1405 for (r
= lang_memory_region_list
; r
!= NULL
; r
= r
->next
)
1406 for (n
= &r
->name_list
; n
!= NULL
; n
= n
->next
)
1408 if (region
== NULL
&& strcmp (n
->name
, region_name
) == 0)
1410 if (strcmp (n
->name
, alias
) == 0)
1411 einfo (_("%F%P:%pS: error: redefinition of memory region "
1416 /* Check if the target region exists. */
1418 einfo (_("%F%P:%pS: error: memory region `%s' "
1419 "for alias `%s' does not exist\n"),
1420 NULL
, region_name
, alias
);
1422 /* Add alias to region name list. */
1423 n
= stat_alloc (sizeof (lang_memory_region_name
));
1424 n
->name
= xstrdup (alias
);
1425 n
->next
= region
->name_list
.next
;
1426 region
->name_list
.next
= n
;
1429 static lang_memory_region_type
*
1430 lang_memory_default (asection
*section
)
1432 lang_memory_region_type
*p
;
1434 flagword sec_flags
= section
->flags
;
1436 /* Override SEC_DATA to mean a writable section. */
1437 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
1438 sec_flags
|= SEC_DATA
;
1440 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
1442 if ((p
->flags
& sec_flags
) != 0
1443 && (p
->not_flags
& sec_flags
) == 0)
1448 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, false);
1451 /* Get the output section statement directly from the userdata. */
1453 lang_output_section_statement_type
*
1454 lang_output_section_get (const asection
*output_section
)
1456 return bfd_section_userdata (output_section
);
1459 /* Find or create an output_section_statement with the given NAME.
1460 If CONSTRAINT is non-zero match one with that constraint, otherwise
1461 match any non-negative constraint. If CREATE is 0 return NULL when
1462 no match exists. If CREATE is 1, create an output_section_statement
1463 when no match exists or if CONSTRAINT is SPECIAL. If CREATE is 2,
1464 always make a new output_section_statement. */
1466 lang_output_section_statement_type
*
1467 lang_output_section_statement_lookup (const char *name
,
1471 struct out_section_hash_entry
*entry
;
1473 entry
= ((struct out_section_hash_entry
*)
1474 bfd_hash_lookup (&output_section_statement_table
, name
,
1475 create
!= 0, false));
1479 einfo (_("%F%P: failed creating section `%s': %E\n"), name
);
1483 if (entry
->s
.output_section_statement
.name
!= NULL
)
1485 /* We have a section of this name, but it might not have the correct
1487 struct out_section_hash_entry
*last_ent
;
1489 name
= entry
->s
.output_section_statement
.name
;
1493 && !(create
&& constraint
== SPECIAL
)
1494 && (constraint
== entry
->s
.output_section_statement
.constraint
1496 && entry
->s
.output_section_statement
.constraint
>= 0)))
1497 return &entry
->s
.output_section_statement
;
1499 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1501 while (entry
!= NULL
1502 && name
== entry
->s
.output_section_statement
.name
);
1508 = ((struct out_section_hash_entry
*)
1509 output_section_statement_newfunc (NULL
,
1510 &output_section_statement_table
,
1514 einfo (_("%F%P: failed creating section `%s': %E\n"), name
);
1517 entry
->root
= last_ent
->root
;
1518 last_ent
->root
.next
= &entry
->root
;
1521 entry
->s
.output_section_statement
.name
= name
;
1522 entry
->s
.output_section_statement
.constraint
= constraint
;
1523 entry
->s
.output_section_statement
.dup_output
= (create
== 2
1524 || constraint
== SPECIAL
);
1525 return &entry
->s
.output_section_statement
;
1528 /* Find the next output_section_statement with the same name as OS.
1529 If CONSTRAINT is non-zero, find one with that constraint otherwise
1530 match any non-negative constraint. */
1532 lang_output_section_statement_type
*
1533 next_matching_output_section_statement (lang_output_section_statement_type
*os
,
1536 /* All output_section_statements are actually part of a
1537 struct out_section_hash_entry. */
1538 struct out_section_hash_entry
*entry
= (struct out_section_hash_entry
*)
1540 - offsetof (struct out_section_hash_entry
, s
.output_section_statement
));
1541 const char *name
= os
->name
;
1543 ASSERT (name
== entry
->root
.string
);
1546 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1548 || name
!= entry
->s
.output_section_statement
.name
)
1551 while (constraint
!= entry
->s
.output_section_statement
.constraint
1553 || entry
->s
.output_section_statement
.constraint
< 0));
1555 return &entry
->s
.output_section_statement
;
1558 /* A variant of lang_output_section_find used by place_orphan.
1559 Returns the output statement that should precede a new output
1560 statement for SEC. If an exact match is found on certain flags,
1563 lang_output_section_statement_type
*
1564 lang_output_section_find_by_flags (const asection
*sec
,
1566 lang_output_section_statement_type
**exact
,
1567 lang_match_sec_type_func match_type
)
1569 lang_output_section_statement_type
*first
, *look
, *found
;
1570 flagword look_flags
, differ
;
1572 /* We know the first statement on this list is *ABS*. May as well
1574 first
= (void *) lang_os_list
.head
;
1575 first
= first
->next
;
1577 /* First try for an exact match. */
1579 for (look
= first
; look
; look
= look
->next
)
1581 look_flags
= look
->flags
;
1582 if (look
->bfd_section
!= NULL
)
1584 look_flags
= look
->bfd_section
->flags
;
1585 if (match_type
&& !match_type (link_info
.output_bfd
,
1590 differ
= look_flags
^ sec_flags
;
1591 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
1592 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1602 if ((sec_flags
& SEC_CODE
) != 0
1603 && (sec_flags
& SEC_ALLOC
) != 0)
1605 /* Try for a rw code section. */
1606 for (look
= first
; look
; look
= look
->next
)
1608 look_flags
= look
->flags
;
1609 if (look
->bfd_section
!= NULL
)
1611 look_flags
= look
->bfd_section
->flags
;
1612 if (match_type
&& !match_type (link_info
.output_bfd
,
1617 differ
= look_flags
^ sec_flags
;
1618 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1619 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1623 else if ((sec_flags
& SEC_READONLY
) != 0
1624 && (sec_flags
& SEC_ALLOC
) != 0)
1626 /* .rodata can go after .text, .sdata2 after .rodata. */
1627 for (look
= first
; look
; look
= look
->next
)
1629 look_flags
= look
->flags
;
1630 if (look
->bfd_section
!= NULL
)
1632 look_flags
= look
->bfd_section
->flags
;
1633 if (match_type
&& !match_type (link_info
.output_bfd
,
1638 differ
= look_flags
^ sec_flags
;
1639 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1640 | SEC_READONLY
| SEC_SMALL_DATA
))
1641 || (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1643 && !(look_flags
& SEC_SMALL_DATA
)))
1647 else if ((sec_flags
& SEC_THREAD_LOCAL
) != 0
1648 && (sec_flags
& SEC_ALLOC
) != 0)
1650 /* .tdata can go after .data, .tbss after .tdata. Treat .tbss
1651 as if it were a loaded section, and don't use match_type. */
1652 bool seen_thread_local
= false;
1655 for (look
= first
; look
; look
= look
->next
)
1657 look_flags
= look
->flags
;
1658 if (look
->bfd_section
!= NULL
)
1659 look_flags
= look
->bfd_section
->flags
;
1661 differ
= look_flags
^ (sec_flags
| SEC_LOAD
| SEC_HAS_CONTENTS
);
1662 if (!(differ
& (SEC_THREAD_LOCAL
| SEC_ALLOC
)))
1664 /* .tdata and .tbss must be adjacent and in that order. */
1665 if (!(look_flags
& SEC_LOAD
)
1666 && (sec_flags
& SEC_LOAD
))
1667 /* ..so if we're at a .tbss section and we're placing
1668 a .tdata section stop looking and return the
1669 previous section. */
1672 seen_thread_local
= true;
1674 else if (seen_thread_local
)
1676 else if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
)))
1680 else if ((sec_flags
& SEC_SMALL_DATA
) != 0
1681 && (sec_flags
& SEC_ALLOC
) != 0)
1683 /* .sdata goes after .data, .sbss after .sdata. */
1684 for (look
= first
; look
; look
= look
->next
)
1686 look_flags
= look
->flags
;
1687 if (look
->bfd_section
!= NULL
)
1689 look_flags
= look
->bfd_section
->flags
;
1690 if (match_type
&& !match_type (link_info
.output_bfd
,
1695 differ
= look_flags
^ sec_flags
;
1696 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1697 | SEC_THREAD_LOCAL
))
1698 || ((look_flags
& SEC_SMALL_DATA
)
1699 && !(sec_flags
& SEC_HAS_CONTENTS
)))
1703 else if ((sec_flags
& SEC_HAS_CONTENTS
) != 0
1704 && (sec_flags
& SEC_ALLOC
) != 0)
1706 /* .data goes after .rodata. */
1707 for (look
= first
; look
; look
= look
->next
)
1709 look_flags
= look
->flags
;
1710 if (look
->bfd_section
!= NULL
)
1712 look_flags
= look
->bfd_section
->flags
;
1713 if (match_type
&& !match_type (link_info
.output_bfd
,
1718 differ
= look_flags
^ sec_flags
;
1719 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1720 | SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1724 else if ((sec_flags
& SEC_ALLOC
) != 0)
1726 /* .bss goes after any other alloc section. */
1727 for (look
= first
; look
; look
= look
->next
)
1729 look_flags
= look
->flags
;
1730 if (look
->bfd_section
!= NULL
)
1732 look_flags
= look
->bfd_section
->flags
;
1733 if (match_type
&& !match_type (link_info
.output_bfd
,
1738 differ
= look_flags
^ sec_flags
;
1739 if (!(differ
& SEC_ALLOC
))
1745 /* non-alloc go last. */
1746 for (look
= first
; look
; look
= look
->next
)
1748 look_flags
= look
->flags
;
1749 if (look
->bfd_section
!= NULL
)
1750 look_flags
= look
->bfd_section
->flags
;
1751 differ
= look_flags
^ sec_flags
;
1752 if (!(differ
& SEC_DEBUGGING
))
1758 if (found
|| !match_type
)
1761 return lang_output_section_find_by_flags (sec
, sec_flags
, NULL
, NULL
);
1764 /* Find the last output section before given output statement.
1765 Used by place_orphan. */
1768 output_prev_sec_find (lang_output_section_statement_type
*os
)
1770 lang_output_section_statement_type
*lookup
;
1772 for (lookup
= os
->prev
; lookup
!= NULL
; lookup
= lookup
->prev
)
1774 if (lookup
->constraint
< 0)
1777 if (lookup
->bfd_section
!= NULL
&& lookup
->bfd_section
->owner
!= NULL
)
1778 return lookup
->bfd_section
;
1784 /* Look for a suitable place for a new output section statement. The
1785 idea is to skip over anything that might be inside a SECTIONS {}
1786 statement in a script, before we find another output section
1787 statement. Assignments to "dot" before an output section statement
1788 are assumed to belong to it, except in two cases; The first
1789 assignment to dot, and assignments before non-alloc sections.
1790 Otherwise we might put an orphan before . = . + SIZEOF_HEADERS or
1791 similar assignments that set the initial address, or we might
1792 insert non-alloc note sections among assignments setting end of
1795 static lang_statement_union_type
**
1796 insert_os_after (lang_output_section_statement_type
*after
)
1798 lang_statement_union_type
**where
;
1799 lang_statement_union_type
**assign
= NULL
;
1802 ignore_first
= after
== (void *) lang_os_list
.head
;
1804 for (where
= &after
->header
.next
;
1806 where
= &(*where
)->header
.next
)
1808 switch ((*where
)->header
.type
)
1810 case lang_assignment_statement_enum
:
1813 lang_assignment_statement_type
*ass
;
1815 ass
= &(*where
)->assignment_statement
;
1816 if (ass
->exp
->type
.node_class
!= etree_assert
1817 && ass
->exp
->assign
.dst
[0] == '.'
1818 && ass
->exp
->assign
.dst
[1] == 0)
1822 ignore_first
= false;
1826 case lang_wild_statement_enum
:
1827 case lang_input_section_enum
:
1828 case lang_object_symbols_statement_enum
:
1829 case lang_fill_statement_enum
:
1830 case lang_data_statement_enum
:
1831 case lang_reloc_statement_enum
:
1832 case lang_padding_statement_enum
:
1833 case lang_constructors_statement_enum
:
1835 ignore_first
= false;
1837 case lang_output_section_statement_enum
:
1840 asection
*s
= (*where
)->output_section_statement
.bfd_section
;
1843 || s
->map_head
.s
== NULL
1844 || (s
->flags
& SEC_ALLOC
) != 0)
1848 case lang_input_statement_enum
:
1849 case lang_address_statement_enum
:
1850 case lang_target_statement_enum
:
1851 case lang_output_statement_enum
:
1852 case lang_group_statement_enum
:
1853 case lang_insert_statement_enum
:
1855 case lang_input_matcher_enum
:
1864 lang_output_section_statement_type
*
1865 lang_insert_orphan (asection
*s
,
1866 const char *secname
,
1868 lang_output_section_statement_type
*after
,
1869 struct orphan_save
*place
,
1870 etree_type
*address
,
1871 lang_statement_list_type
*add_child
)
1873 lang_statement_list_type add
;
1874 lang_output_section_statement_type
*os
;
1875 lang_output_section_statement_type
**os_tail
;
1877 /* If we have found an appropriate place for the output section
1878 statements for this orphan, add them to our own private list,
1879 inserting them later into the global statement list. */
1882 lang_list_init (&add
);
1883 push_stat_ptr (&add
);
1886 if (bfd_link_relocatable (&link_info
)
1887 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0)
1888 address
= exp_intop (0);
1890 os_tail
= (lang_output_section_statement_type
**) lang_os_list
.tail
;
1891 os
= lang_enter_output_section_statement (
1892 secname
, address
, normal_section
, 0, NULL
, NULL
, NULL
, constraint
, 0);
1894 if (add_child
== NULL
)
1895 add_child
= &os
->children
;
1896 lang_add_section (add_child
, s
, NULL
, NULL
, os
);
1898 if (after
&& (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) != 0)
1900 const char *region
= (after
->region
1901 ? after
->region
->name_list
.name
1902 : DEFAULT_MEMORY_REGION
);
1903 const char *lma_region
= (after
->lma_region
1904 ? after
->lma_region
->name_list
.name
1906 lang_leave_output_section_statement (NULL
, region
, after
->phdrs
,
1910 lang_leave_output_section_statement (NULL
, DEFAULT_MEMORY_REGION
, NULL
,
1913 /* Restore the global list pointer. */
1917 if (after
!= NULL
&& os
->bfd_section
!= NULL
)
1919 asection
*snew
, *as
;
1920 bool place_after
= place
->stmt
== NULL
;
1921 bool insert_after
= true;
1923 snew
= os
->bfd_section
;
1925 /* Shuffle the bfd section list to make the output file look
1926 neater. This is really only cosmetic. */
1927 if (place
->section
== NULL
1928 && after
!= (void *) lang_os_list
.head
)
1930 asection
*bfd_section
= after
->bfd_section
;
1932 /* If the output statement hasn't been used to place any input
1933 sections (and thus doesn't have an output bfd_section),
1934 look for the closest prior output statement having an
1936 if (bfd_section
== NULL
)
1937 bfd_section
= output_prev_sec_find (after
);
1939 if (bfd_section
!= NULL
&& bfd_section
!= snew
)
1940 place
->section
= &bfd_section
->next
;
1943 if (place
->section
== NULL
)
1944 place
->section
= &link_info
.output_bfd
->sections
;
1946 as
= *place
->section
;
1950 /* Put the section at the end of the list. */
1952 /* Unlink the section. */
1953 bfd_section_list_remove (link_info
.output_bfd
, snew
);
1955 /* Now tack it back on in the right place. */
1956 bfd_section_list_append (link_info
.output_bfd
, snew
);
1958 else if ((bfd_get_flavour (link_info
.output_bfd
)
1959 == bfd_target_elf_flavour
)
1960 && (bfd_get_flavour (s
->owner
)
1961 == bfd_target_elf_flavour
)
1962 && ((elf_section_type (s
) == SHT_NOTE
1963 && (s
->flags
& SEC_LOAD
) != 0)
1964 || (elf_section_type (as
) == SHT_NOTE
1965 && (as
->flags
& SEC_LOAD
) != 0)))
1967 /* Make sure that output note sections are grouped and sorted
1968 by alignments when inserting a note section or insert a
1969 section after a note section, */
1971 /* A specific section after which the output note section
1972 should be placed. */
1973 asection
*after_sec
;
1974 /* True if we need to insert the orphan section after a
1975 specific section to maintain output note section order. */
1976 bool after_sec_note
= false;
1978 static asection
*first_orphan_note
= NULL
;
1980 /* Group and sort output note section by alignments in
1983 if (elf_section_type (s
) == SHT_NOTE
1984 && (s
->flags
& SEC_LOAD
) != 0)
1986 /* Search from the beginning for the last output note
1987 section with equal or larger alignments. NB: Don't
1988 place orphan note section after non-note sections. */
1990 first_orphan_note
= NULL
;
1991 for (sec
= link_info
.output_bfd
->sections
;
1993 && !bfd_is_abs_section (sec
));
1996 && elf_section_type (sec
) == SHT_NOTE
1997 && (sec
->flags
& SEC_LOAD
) != 0)
1999 if (!first_orphan_note
)
2000 first_orphan_note
= sec
;
2001 if (sec
->alignment_power
>= s
->alignment_power
)
2004 else if (first_orphan_note
)
2006 /* Stop if there is non-note section after the first
2007 orphan note section. */
2011 /* If this will be the first orphan note section, it can
2012 be placed at the default location. */
2013 after_sec_note
= first_orphan_note
!= NULL
;
2014 if (after_sec
== NULL
&& after_sec_note
)
2016 /* If all output note sections have smaller
2017 alignments, place the section before all
2018 output orphan note sections. */
2019 after_sec
= first_orphan_note
;
2020 insert_after
= false;
2023 else if (first_orphan_note
)
2025 /* Don't place non-note sections in the middle of orphan
2027 after_sec_note
= true;
2029 for (sec
= as
->next
;
2031 && !bfd_is_abs_section (sec
));
2033 if (elf_section_type (sec
) == SHT_NOTE
2034 && (sec
->flags
& SEC_LOAD
) != 0)
2042 /* Search forward to insert OS after AFTER_SEC output
2044 lang_output_section_statement_type
*stmt
, *next
;
2046 for (stmt
= after
; stmt
!= NULL
; stmt
= next
)
2051 if (stmt
->bfd_section
== after_sec
)
2061 /* If INSERT_AFTER is FALSE, place OS before
2062 AFTER_SEC output statement. */
2063 if (next
&& next
->bfd_section
== after_sec
)
2073 /* Search backward to insert OS after AFTER_SEC output
2076 for (stmt
= after
; stmt
!= NULL
; stmt
= stmt
->prev
)
2080 if (stmt
->bfd_section
== after_sec
)
2089 /* If INSERT_AFTER is FALSE, place OS before
2090 AFTER_SEC output statement. */
2091 if (stmt
->next
->bfd_section
== after_sec
)
2101 if (after_sec
== NULL
2102 || (insert_after
&& after_sec
->next
!= snew
)
2103 || (!insert_after
&& after_sec
->prev
!= snew
))
2105 /* Unlink the section. */
2106 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2108 /* Place SNEW after AFTER_SEC. If AFTER_SEC is NULL,
2113 bfd_section_list_insert_after (link_info
.output_bfd
,
2116 bfd_section_list_insert_before (link_info
.output_bfd
,
2120 bfd_section_list_prepend (link_info
.output_bfd
, snew
);
2123 else if (as
!= snew
&& as
->prev
!= snew
)
2125 /* Unlink the section. */
2126 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2128 /* Now tack it back on in the right place. */
2129 bfd_section_list_insert_before (link_info
.output_bfd
,
2133 else if (as
!= snew
&& as
->prev
!= snew
)
2135 /* Unlink the section. */
2136 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2138 /* Now tack it back on in the right place. */
2139 bfd_section_list_insert_before (link_info
.output_bfd
, as
, snew
);
2142 /* Save the end of this list. Further ophans of this type will
2143 follow the one we've just added. */
2144 place
->section
= &snew
->next
;
2146 /* The following is non-cosmetic. We try to put the output
2147 statements in some sort of reasonable order here, because they
2148 determine the final load addresses of the orphan sections.
2149 In addition, placing output statements in the wrong order may
2150 require extra segments. For instance, given a typical
2151 situation of all read-only sections placed in one segment and
2152 following that a segment containing all the read-write
2153 sections, we wouldn't want to place an orphan read/write
2154 section before or amongst the read-only ones. */
2155 if (add
.head
!= NULL
)
2157 lang_output_section_statement_type
*newly_added_os
;
2159 /* Place OS after AFTER if AFTER_NOTE is TRUE. */
2162 lang_statement_union_type
**where
= insert_os_after (after
);
2167 place
->os_tail
= &after
->next
;
2171 /* Put it after the last orphan statement we added. */
2172 *add
.tail
= *place
->stmt
;
2173 *place
->stmt
= add
.head
;
2176 /* Fix the global list pointer if we happened to tack our
2177 new list at the tail. */
2178 if (*stat_ptr
->tail
== add
.head
)
2179 stat_ptr
->tail
= add
.tail
;
2181 /* Save the end of this list. */
2182 place
->stmt
= add
.tail
;
2184 /* Do the same for the list of output section statements. */
2185 newly_added_os
= *os_tail
;
2187 newly_added_os
->prev
= (lang_output_section_statement_type
*)
2188 ((char *) place
->os_tail
2189 - offsetof (lang_output_section_statement_type
, next
));
2190 newly_added_os
->next
= *place
->os_tail
;
2191 if (newly_added_os
->next
!= NULL
)
2192 newly_added_os
->next
->prev
= newly_added_os
;
2193 *place
->os_tail
= newly_added_os
;
2194 place
->os_tail
= &newly_added_os
->next
;
2196 /* Fixing the global list pointer here is a little different.
2197 We added to the list in lang_enter_output_section_statement,
2198 trimmed off the new output_section_statment above when
2199 assigning *os_tail = NULL, but possibly added it back in
2200 the same place when assigning *place->os_tail. */
2201 if (*os_tail
== NULL
)
2202 lang_os_list
.tail
= (lang_statement_union_type
**) os_tail
;
2209 lang_print_asneeded (void)
2211 struct asneeded_minfo
*m
;
2213 if (asneeded_list_head
== NULL
)
2216 minfo (_("\nAs-needed library included to satisfy reference by file (symbol)\n\n"));
2218 for (m
= asneeded_list_head
; m
!= NULL
; m
= m
->next
)
2222 minfo ("%s", m
->soname
);
2223 len
= strlen (m
->soname
);
2230 print_spaces (30 - len
);
2233 minfo ("%pB ", m
->ref
);
2234 minfo ("(%pT)\n", m
->name
);
2239 lang_map_flags (flagword flag
)
2241 if (flag
& SEC_ALLOC
)
2244 if (flag
& SEC_CODE
)
2247 if (flag
& SEC_READONLY
)
2250 if (flag
& SEC_DATA
)
2253 if (flag
& SEC_LOAD
)
2260 lang_memory_region_type
*m
;
2261 bool dis_header_printed
= false;
2263 LANG_FOR_EACH_INPUT_STATEMENT (file
)
2267 if ((file
->the_bfd
->flags
& (BFD_LINKER_CREATED
| DYNAMIC
)) != 0
2268 || file
->flags
.just_syms
)
2271 if (config
.print_map_discarded
)
2272 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
2273 if ((s
->output_section
== NULL
2274 || s
->output_section
->owner
!= link_info
.output_bfd
)
2275 && (s
->flags
& (SEC_LINKER_CREATED
| SEC_KEEP
)) == 0)
2277 if (! dis_header_printed
)
2279 fprintf (config
.map_file
, _("\nDiscarded input sections\n\n"));
2280 dis_header_printed
= true;
2283 print_input_section (s
, true);
2287 minfo (_("\nMemory Configuration\n\n"));
2288 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
2289 _("Name"), _("Origin"), _("Length"), _("Attributes"));
2291 for (m
= lang_memory_region_list
; m
!= NULL
; m
= m
->next
)
2293 fprintf (config
.map_file
, "%-16s", m
->name_list
.name
);
2296 bfd_sprintf_vma (link_info
.output_bfd
, buf
, m
->origin
);
2297 fprintf (config
.map_file
, " 0x%-16s", buf
);
2298 bfd_sprintf_vma (link_info
.output_bfd
, buf
, m
->length
);
2299 fprintf (config
.map_file
,
2300 " 0x%*s", m
->flags
|| m
->not_flags
? -17 : 0, buf
);
2302 lang_map_flags (m
->flags
);
2307 lang_map_flags (m
->not_flags
);
2313 fprintf (config
.map_file
, _("\nLinker script and memory map\n\n"));
2315 if (!link_info
.reduce_memory_overheads
)
2317 obstack_begin (&map_obstack
, 1000);
2318 bfd_link_hash_traverse (link_info
.hash
, sort_def_symbol
, 0);
2320 expld
.phase
= lang_fixed_phase_enum
;
2321 lang_statement_iteration
++;
2322 print_statements ();
2324 ldemul_extra_map_file_text (link_info
.output_bfd
, &link_info
,
2329 sort_def_symbol (struct bfd_link_hash_entry
*hash_entry
,
2330 void *info ATTRIBUTE_UNUSED
)
2332 if ((hash_entry
->type
== bfd_link_hash_defined
2333 || hash_entry
->type
== bfd_link_hash_defweak
)
2334 && hash_entry
->u
.def
.section
->owner
!= link_info
.output_bfd
2335 && hash_entry
->u
.def
.section
->owner
!= NULL
)
2337 input_section_userdata_type
*ud
;
2338 struct map_symbol_def
*def
;
2340 ud
= bfd_section_userdata (hash_entry
->u
.def
.section
);
2343 ud
= stat_alloc (sizeof (*ud
));
2344 bfd_set_section_userdata (hash_entry
->u
.def
.section
, ud
);
2345 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
2346 ud
->map_symbol_def_count
= 0;
2348 else if (!ud
->map_symbol_def_tail
)
2349 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
2351 def
= (struct map_symbol_def
*) obstack_alloc (&map_obstack
, sizeof *def
);
2352 def
->entry
= hash_entry
;
2353 *(ud
->map_symbol_def_tail
) = def
;
2354 ud
->map_symbol_def_tail
= &def
->next
;
2355 ud
->map_symbol_def_count
++;
2360 /* Initialize an output section. */
2363 init_os (lang_output_section_statement_type
*s
, flagword flags
)
2365 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
2366 einfo (_("%F%P: illegal use of `%s' section\n"), DISCARD_SECTION_NAME
);
2369 s
->bfd_section
= bfd_get_section_by_name (link_info
.output_bfd
, s
->name
);
2370 if (s
->bfd_section
== NULL
)
2371 s
->bfd_section
= bfd_make_section_anyway_with_flags (link_info
.output_bfd
,
2373 if (s
->bfd_section
== NULL
)
2375 einfo (_("%F%P: output format %s cannot represent section"
2376 " called %s: %E\n"),
2377 link_info
.output_bfd
->xvec
->name
, s
->name
);
2379 s
->bfd_section
->output_section
= s
->bfd_section
;
2380 s
->bfd_section
->output_offset
= 0;
2382 /* Set the userdata of the output section to the output section
2383 statement to avoid lookup. */
2384 bfd_set_section_userdata (s
->bfd_section
, s
);
2386 /* If there is a base address, make sure that any sections it might
2387 mention are initialized. */
2388 if (s
->addr_tree
!= NULL
)
2389 exp_init_os (s
->addr_tree
);
2391 if (s
->load_base
!= NULL
)
2392 exp_init_os (s
->load_base
);
2394 /* If supplied an alignment, set it. */
2395 if (s
->section_alignment
!= NULL
)
2396 s
->bfd_section
->alignment_power
= exp_get_power (s
->section_alignment
,
2397 "section alignment");
2400 /* Make sure that all output sections mentioned in an expression are
2404 exp_init_os (etree_type
*exp
)
2406 switch (exp
->type
.node_class
)
2410 case etree_provided
:
2411 exp_init_os (exp
->assign
.src
);
2415 exp_init_os (exp
->binary
.lhs
);
2416 exp_init_os (exp
->binary
.rhs
);
2420 exp_init_os (exp
->trinary
.cond
);
2421 exp_init_os (exp
->trinary
.lhs
);
2422 exp_init_os (exp
->trinary
.rhs
);
2426 exp_init_os (exp
->assert_s
.child
);
2430 exp_init_os (exp
->unary
.child
);
2434 switch (exp
->type
.node_code
)
2439 lang_output_section_statement_type
*os
;
2441 os
= lang_output_section_find (exp
->name
.name
);
2442 if (os
!= NULL
&& os
->bfd_section
== NULL
)
2454 section_already_linked (bfd
*abfd
, asection
*sec
, void *data
)
2456 lang_input_statement_type
*entry
= (lang_input_statement_type
*) data
;
2458 /* If we are only reading symbols from this object, then we want to
2459 discard all sections. */
2460 if (entry
->flags
.just_syms
)
2462 bfd_link_just_syms (abfd
, sec
, &link_info
);
2466 /* Deal with SHF_EXCLUDE ELF sections. */
2467 if (!bfd_link_relocatable (&link_info
)
2468 && (abfd
->flags
& BFD_PLUGIN
) == 0
2469 && (sec
->flags
& (SEC_GROUP
| SEC_KEEP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
2470 sec
->output_section
= bfd_abs_section_ptr
;
2472 if (!(abfd
->flags
& DYNAMIC
))
2473 bfd_section_already_linked (abfd
, sec
, &link_info
);
2477 /* Returns true if SECTION is one we know will be discarded based on its
2478 section flags, otherwise returns false. */
2481 lang_discard_section_p (asection
*section
)
2484 flagword flags
= section
->flags
;
2486 /* Discard sections marked with SEC_EXCLUDE. */
2487 discard
= (flags
& SEC_EXCLUDE
) != 0;
2489 /* Discard the group descriptor sections when we're finally placing the
2490 sections from within the group. */
2491 if ((flags
& SEC_GROUP
) != 0
2492 && link_info
.resolve_section_groups
)
2495 /* Discard debugging sections if we are stripping debugging
2497 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
2498 && (flags
& SEC_DEBUGGING
) != 0)
2504 /* The wild routines.
2506 These expand statements like *(.text) and foo.o to a list of
2507 explicit actions, like foo.o(.text), bar.o(.text) and
2508 foo.o(.text, .data). */
2510 /* Add SECTION to the output section OUTPUT. Do this by creating a
2511 lang_input_section statement which is placed at PTR. */
2514 lang_add_section (lang_statement_list_type
*ptr
,
2516 struct wildcard_list
*pattern
,
2517 struct flag_info
*sflag_info
,
2518 lang_output_section_statement_type
*output
)
2520 flagword flags
= section
->flags
;
2523 lang_input_section_type
*new_section
;
2524 bfd
*abfd
= link_info
.output_bfd
;
2526 /* Is this section one we know should be discarded? */
2527 discard
= lang_discard_section_p (section
);
2529 /* Discard input sections which are assigned to a section named
2530 DISCARD_SECTION_NAME. */
2531 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
2536 if (section
->output_section
== NULL
)
2538 /* This prevents future calls from assigning this section. */
2539 section
->output_section
= bfd_abs_section_ptr
;
2541 else if (link_info
.non_contiguous_regions_warnings
)
2542 einfo (_("%P:%pS: warning: --enable-non-contiguous-regions makes "
2543 "section `%pA' from `%pB' match /DISCARD/ clause.\n"),
2544 NULL
, section
, section
->owner
);
2553 keep
= bfd_lookup_section_flags (&link_info
, sflag_info
, section
);
2558 if (section
->output_section
!= NULL
)
2560 if (!link_info
.non_contiguous_regions
)
2563 /* SECTION has already been handled in a special way
2564 (eg. LINK_ONCE): skip it. */
2565 if (bfd_is_abs_section (section
->output_section
))
2568 /* Already assigned to the same output section, do not process
2569 it again, to avoid creating loops between duplicate sections
2571 if (section
->output_section
== output
->bfd_section
)
2574 if (link_info
.non_contiguous_regions_warnings
&& output
->bfd_section
)
2575 einfo (_("%P:%pS: warning: --enable-non-contiguous-regions may "
2576 "change behaviour for section `%pA' from `%pB' (assigned to "
2577 "%pA, but additional match: %pA)\n"),
2578 NULL
, section
, section
->owner
, section
->output_section
,
2579 output
->bfd_section
);
2581 /* SECTION has already been assigned to an output section, but
2582 the user allows it to be mapped to another one in case it
2583 overflows. We'll later update the actual output section in
2584 size_input_section as appropriate. */
2587 /* We don't copy the SEC_NEVER_LOAD flag from an input section
2588 to an output section, because we want to be able to include a
2589 SEC_NEVER_LOAD section in the middle of an otherwise loaded
2590 section (I don't know why we want to do this, but we do).
2591 build_link_order in ldwrite.c handles this case by turning
2592 the embedded SEC_NEVER_LOAD section into a fill. */
2593 flags
&= ~ SEC_NEVER_LOAD
;
2595 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
2596 already been processed. One reason to do this is that on pe
2597 format targets, .text$foo sections go into .text and it's odd
2598 to see .text with SEC_LINK_ONCE set. */
2599 if ((flags
& (SEC_LINK_ONCE
| SEC_GROUP
)) == (SEC_LINK_ONCE
| SEC_GROUP
))
2601 if (link_info
.resolve_section_groups
)
2602 flags
&= ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
);
2604 flags
&= ~(SEC_LINK_DUPLICATES
| SEC_RELOC
);
2606 else if (!bfd_link_relocatable (&link_info
))
2607 flags
&= ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
);
2609 switch (output
->sectype
)
2611 case normal_section
:
2612 case overlay_section
:
2613 case first_overlay_section
:
2616 case noalloc_section
:
2617 flags
&= ~SEC_ALLOC
;
2619 case typed_readonly_section
:
2620 case readonly_section
:
2621 flags
|= SEC_READONLY
;
2623 case noload_section
:
2625 flags
|= SEC_NEVER_LOAD
;
2626 /* Unfortunately GNU ld has managed to evolve two different
2627 meanings to NOLOAD in scripts. ELF gets a .bss style noload,
2628 alloc, no contents section. All others get a noload, noalloc
2630 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
)
2631 flags
&= ~SEC_HAS_CONTENTS
;
2633 flags
&= ~SEC_ALLOC
;
2637 if (output
->bfd_section
== NULL
)
2638 init_os (output
, flags
);
2640 /* If SEC_READONLY is not set in the input section, then clear
2641 it from the output section. */
2642 output
->bfd_section
->flags
&= flags
| ~SEC_READONLY
;
2644 if (output
->bfd_section
->linker_has_input
)
2646 /* Only set SEC_READONLY flag on the first input section. */
2647 flags
&= ~ SEC_READONLY
;
2649 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
2650 if ((output
->bfd_section
->flags
& (SEC_MERGE
| SEC_STRINGS
))
2651 != (flags
& (SEC_MERGE
| SEC_STRINGS
))
2652 || ((flags
& SEC_MERGE
) != 0
2653 && output
->bfd_section
->entsize
!= section
->entsize
))
2655 output
->bfd_section
->flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2656 flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2659 output
->bfd_section
->flags
|= flags
;
2661 if (!output
->bfd_section
->linker_has_input
)
2663 output
->bfd_section
->linker_has_input
= 1;
2664 /* This must happen after flags have been updated. The output
2665 section may have been created before we saw its first input
2666 section, eg. for a data statement. */
2667 bfd_init_private_section_data (section
->owner
, section
,
2668 link_info
.output_bfd
,
2669 output
->bfd_section
,
2671 if ((flags
& SEC_MERGE
) != 0)
2672 output
->bfd_section
->entsize
= section
->entsize
;
2675 if ((flags
& SEC_TIC54X_BLOCK
) != 0
2676 && bfd_get_arch (section
->owner
) == bfd_arch_tic54x
)
2678 /* FIXME: This value should really be obtained from the bfd... */
2679 output
->block_value
= 128;
2682 /* When a .ctors section is placed in .init_array it must be copied
2683 in reverse order. Similarly for .dtors. Set that up. */
2684 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
2685 && ((startswith (section
->name
, ".ctors")
2686 && strcmp (output
->bfd_section
->name
, ".init_array") == 0)
2687 || (startswith (section
->name
, ".dtors")
2688 && strcmp (output
->bfd_section
->name
, ".fini_array") == 0))
2689 && (section
->name
[6] == 0 || section
->name
[6] == '.'))
2690 section
->flags
|= SEC_ELF_REVERSE_COPY
;
2692 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
2693 output
->bfd_section
->alignment_power
= section
->alignment_power
;
2695 section
->output_section
= output
->bfd_section
;
2697 if (!map_head_is_link_order
)
2699 asection
*s
= output
->bfd_section
->map_tail
.s
;
2700 output
->bfd_section
->map_tail
.s
= section
;
2701 section
->map_head
.s
= NULL
;
2702 section
->map_tail
.s
= s
;
2704 s
->map_head
.s
= section
;
2706 output
->bfd_section
->map_head
.s
= section
;
2709 /* Add a section reference to the list. */
2710 new_section
= new_stat (lang_input_section
, ptr
);
2711 new_section
->section
= section
;
2712 new_section
->pattern
= pattern
;
2715 /* Expand a wild statement for a particular FILE. SECTION may be
2716 NULL, in which case it is a wild card. This assumes that the
2717 wild statement doesn't need any sorting (of filenames or sections). */
2720 output_section_callback_nosort (lang_wild_statement_type
*ptr
,
2721 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
2723 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
2726 lang_output_section_statement_type
*os
;
2728 os
= (lang_output_section_statement_type
*) output
;
2730 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2731 if (unique_section_p (section
, os
))
2734 lang_add_section (&ptr
->children
, section
, ptr
->section_list
,
2735 ptr
->section_flag_list
, os
);
2738 /* Check if all sections in a wild statement for a particular FILE
2742 check_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
2743 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
2745 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
2748 lang_output_section_statement_type
*os
;
2750 os
= (lang_output_section_statement_type
*) output
;
2752 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2753 if (unique_section_p (section
, os
))
2756 if (section
->output_section
== NULL
&& (section
->flags
& SEC_READONLY
) == 0)
2757 os
->all_input_readonly
= false;
2760 /* This is passed a file name which must have been seen already and
2761 added to the statement tree. We will see if it has been opened
2762 already and had its symbols read. If not then we'll read it. */
2764 static lang_input_statement_type
*
2765 lookup_name (const char *name
)
2767 lang_input_statement_type
*search
;
2769 for (search
= (void *) input_file_chain
.head
;
2771 search
= search
->next_real_file
)
2773 /* Use the local_sym_name as the name of the file that has
2774 already been loaded as filename might have been transformed
2775 via the search directory lookup mechanism. */
2776 const char *filename
= search
->local_sym_name
;
2778 if (filename
!= NULL
2779 && filename_cmp (filename
, name
) == 0)
2785 /* Arrange to splice the input statement added by new_afile into
2786 statement_list after the current input_file_chain tail.
2787 We know input_file_chain is not an empty list, and that
2788 lookup_name was called via open_input_bfds. Later calls to
2789 lookup_name should always match an existing input_statement. */
2790 lang_statement_union_type
**tail
= stat_ptr
->tail
;
2791 lang_statement_union_type
**after
2792 = (void *) ((char *) input_file_chain
.tail
2793 - offsetof (lang_input_statement_type
, next_real_file
)
2794 + offsetof (lang_input_statement_type
, header
.next
));
2795 lang_statement_union_type
*rest
= *after
;
2796 stat_ptr
->tail
= after
;
2797 search
= new_afile (name
, lang_input_file_is_search_file_enum
,
2798 default_target
, NULL
);
2799 *stat_ptr
->tail
= rest
;
2801 stat_ptr
->tail
= tail
;
2804 /* If we have already added this file, or this file is not real
2805 don't add this file. */
2806 if (search
->flags
.loaded
|| !search
->flags
.real
)
2809 if (!load_symbols (search
, NULL
))
2815 /* Save LIST as a list of libraries whose symbols should not be exported. */
2820 struct excluded_lib
*next
;
2822 static struct excluded_lib
*excluded_libs
;
2825 add_excluded_libs (const char *list
)
2827 const char *p
= list
, *end
;
2831 struct excluded_lib
*entry
;
2832 end
= strpbrk (p
, ",:");
2834 end
= p
+ strlen (p
);
2835 entry
= (struct excluded_lib
*) xmalloc (sizeof (*entry
));
2836 entry
->next
= excluded_libs
;
2837 entry
->name
= (char *) xmalloc (end
- p
+ 1);
2838 memcpy (entry
->name
, p
, end
- p
);
2839 entry
->name
[end
- p
] = '\0';
2840 excluded_libs
= entry
;
2848 check_excluded_libs (bfd
*abfd
)
2850 struct excluded_lib
*lib
= excluded_libs
;
2854 int len
= strlen (lib
->name
);
2855 const char *filename
= lbasename (bfd_get_filename (abfd
));
2857 if (strcmp (lib
->name
, "ALL") == 0)
2859 abfd
->no_export
= true;
2863 if (filename_ncmp (lib
->name
, filename
, len
) == 0
2864 && (filename
[len
] == '\0'
2865 || (filename
[len
] == '.' && filename
[len
+ 1] == 'a'
2866 && filename
[len
+ 2] == '\0')))
2868 abfd
->no_export
= true;
2876 /* Get the symbols for an input file. */
2879 load_symbols (lang_input_statement_type
*entry
,
2880 lang_statement_list_type
*place
)
2884 if (entry
->flags
.loaded
)
2887 ldfile_open_file (entry
);
2889 /* Do not process further if the file was missing. */
2890 if (entry
->flags
.missing_file
)
2893 if (trace_files
|| verbose
)
2894 info_msg ("%pI\n", entry
);
2896 if (!bfd_check_format (entry
->the_bfd
, bfd_archive
)
2897 && !bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
2900 struct lang_input_statement_flags save_flags
;
2903 err
= bfd_get_error ();
2905 /* See if the emulation has some special knowledge. */
2906 if (ldemul_unrecognized_file (entry
))
2908 if (err
== bfd_error_file_ambiguously_recognized
)
2913 if (err
== bfd_error_file_ambiguously_recognized
)
2917 einfo (_("%P: %pB: file not recognized: %E;"
2918 " matching formats:"), entry
->the_bfd
);
2919 for (p
= matching
; *p
!= NULL
; p
++)
2924 else if (err
!= bfd_error_file_not_recognized
2926 einfo (_("%F%P: %pB: file not recognized: %E\n"), entry
->the_bfd
);
2928 bfd_close (entry
->the_bfd
);
2929 entry
->the_bfd
= NULL
;
2931 /* Try to interpret the file as a linker script. */
2932 save_flags
= input_flags
;
2933 ldfile_open_command_file (entry
->filename
);
2935 push_stat_ptr (place
);
2936 input_flags
.add_DT_NEEDED_for_regular
2937 = entry
->flags
.add_DT_NEEDED_for_regular
;
2938 input_flags
.add_DT_NEEDED_for_dynamic
2939 = entry
->flags
.add_DT_NEEDED_for_dynamic
;
2940 input_flags
.whole_archive
= entry
->flags
.whole_archive
;
2941 input_flags
.dynamic
= entry
->flags
.dynamic
;
2943 ldfile_assumed_script
= true;
2944 parser_input
= input_script
;
2945 current_input_file
= entry
->filename
;
2947 current_input_file
= NULL
;
2948 ldfile_assumed_script
= false;
2950 /* missing_file is sticky. sysrooted will already have been
2951 restored when seeing EOF in yyparse, but no harm to restore
2953 save_flags
.missing_file
|= input_flags
.missing_file
;
2954 input_flags
= save_flags
;
2958 entry
->flags
.loaded
= true;
2963 if (ldemul_recognized_file (entry
))
2966 /* We don't call ldlang_add_file for an archive. Instead, the
2967 add_symbols entry point will call ldlang_add_file, via the
2968 add_archive_element callback, for each element of the archive
2970 switch (bfd_get_format (entry
->the_bfd
))
2976 if (!entry
->flags
.reload
)
2977 ldlang_add_file (entry
);
2981 check_excluded_libs (entry
->the_bfd
);
2983 bfd_set_usrdata (entry
->the_bfd
, entry
);
2984 if (entry
->flags
.whole_archive
)
2992 member
= bfd_openr_next_archived_file (entry
->the_bfd
, member
);
2997 if (!bfd_check_format (member
, bfd_object
))
2999 einfo (_("%F%P: %pB: member %pB in archive is not an object\n"),
3000 entry
->the_bfd
, member
);
3005 if (!(*link_info
.callbacks
3006 ->add_archive_element
) (&link_info
, member
,
3007 "--whole-archive", &subsbfd
))
3010 /* Potentially, the add_archive_element hook may have set a
3011 substitute BFD for us. */
3012 if (!bfd_link_add_symbols (subsbfd
, &link_info
))
3014 einfo (_("%F%P: %pB: error adding symbols: %E\n"), member
);
3019 entry
->flags
.loaded
= loaded
;
3025 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
3026 entry
->flags
.loaded
= true;
3028 einfo (_("%F%P: %pB: error adding symbols: %E\n"), entry
->the_bfd
);
3030 return entry
->flags
.loaded
;
3033 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
3034 may be NULL, indicating that it is a wildcard. Separate
3035 lang_input_section statements are created for each part of the
3036 expansion; they are added after the wild statement S. OUTPUT is
3037 the output section. */
3040 wild (lang_wild_statement_type
*s
,
3041 const char *target ATTRIBUTE_UNUSED
,
3042 lang_output_section_statement_type
*output
)
3044 struct wildcard_list
*sec
;
3046 if (s
->filenames_sorted
|| s
->any_specs_sorted
)
3048 lang_section_bst_type
*tree
;
3050 walk_wild (s
, output_section_callback_sort
, output
);
3055 output_section_callback_tree_to_list (s
, tree
, output
);
3057 s
->rightmost
= &s
->tree
;
3061 walk_wild (s
, output_section_callback_nosort
, output
);
3063 if (default_common_section
== NULL
)
3064 for (sec
= s
->section_list
; sec
!= NULL
; sec
= sec
->next
)
3065 if (sec
->spec
.name
!= NULL
&& strcmp (sec
->spec
.name
, "COMMON") == 0)
3067 /* Remember the section that common is going to in case we
3068 later get something which doesn't know where to put it. */
3069 default_common_section
= output
;
3074 /* Return TRUE iff target is the sought target. */
3077 get_target (const bfd_target
*target
, void *data
)
3079 const char *sought
= (const char *) data
;
3081 return strcmp (target
->name
, sought
) == 0;
3084 /* Like strcpy() but convert to lower case as well. */
3087 stricpy (char *dest
, const char *src
)
3091 while ((c
= *src
++) != 0)
3092 *dest
++ = TOLOWER (c
);
3097 /* Remove the first occurrence of needle (if any) in haystack
3101 strcut (char *haystack
, const char *needle
)
3103 haystack
= strstr (haystack
, needle
);
3109 for (src
= haystack
+ strlen (needle
); *src
;)
3110 *haystack
++ = *src
++;
3116 /* Compare two target format name strings.
3117 Return a value indicating how "similar" they are. */
3120 name_compare (const char *first
, const char *second
)
3126 copy1
= (char *) xmalloc (strlen (first
) + 1);
3127 copy2
= (char *) xmalloc (strlen (second
) + 1);
3129 /* Convert the names to lower case. */
3130 stricpy (copy1
, first
);
3131 stricpy (copy2
, second
);
3133 /* Remove size and endian strings from the name. */
3134 strcut (copy1
, "big");
3135 strcut (copy1
, "little");
3136 strcut (copy2
, "big");
3137 strcut (copy2
, "little");
3139 /* Return a value based on how many characters match,
3140 starting from the beginning. If both strings are
3141 the same then return 10 * their length. */
3142 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
3143 if (copy1
[result
] == 0)
3155 /* Set by closest_target_match() below. */
3156 static const bfd_target
*winner
;
3158 /* Scan all the valid bfd targets looking for one that has the endianness
3159 requirement that was specified on the command line, and is the nearest
3160 match to the original output target. */
3163 closest_target_match (const bfd_target
*target
, void *data
)
3165 const bfd_target
*original
= (const bfd_target
*) data
;
3167 if (command_line
.endian
== ENDIAN_BIG
3168 && target
->byteorder
!= BFD_ENDIAN_BIG
)
3171 if (command_line
.endian
== ENDIAN_LITTLE
3172 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
3175 /* Must be the same flavour. */
3176 if (target
->flavour
!= original
->flavour
)
3179 /* Ignore generic big and little endian elf vectors. */
3180 if (strcmp (target
->name
, "elf32-big") == 0
3181 || strcmp (target
->name
, "elf64-big") == 0
3182 || strcmp (target
->name
, "elf32-little") == 0
3183 || strcmp (target
->name
, "elf64-little") == 0)
3186 /* If we have not found a potential winner yet, then record this one. */
3193 /* Oh dear, we now have two potential candidates for a successful match.
3194 Compare their names and choose the better one. */
3195 if (name_compare (target
->name
, original
->name
)
3196 > name_compare (winner
->name
, original
->name
))
3199 /* Keep on searching until wqe have checked them all. */
3203 /* Return the BFD target format of the first input file. */
3206 get_first_input_target (void)
3208 const char *target
= NULL
;
3210 LANG_FOR_EACH_INPUT_STATEMENT (s
)
3212 if (s
->header
.type
== lang_input_statement_enum
3215 ldfile_open_file (s
);
3217 if (s
->the_bfd
!= NULL
3218 && bfd_check_format (s
->the_bfd
, bfd_object
))
3220 target
= bfd_get_target (s
->the_bfd
);
3232 lang_get_output_target (void)
3236 /* Has the user told us which output format to use? */
3237 if (output_target
!= NULL
)
3238 return output_target
;
3240 /* No - has the current target been set to something other than
3242 if (current_target
!= default_target
&& current_target
!= NULL
)
3243 return current_target
;
3245 /* No - can we determine the format of the first input file? */
3246 target
= get_first_input_target ();
3250 /* Failed - use the default output target. */
3251 return default_target
;
3254 /* Open the output file. */
3257 open_output (const char *name
)
3259 lang_input_statement_type
*f
;
3260 char *out
= lrealpath (name
);
3262 for (f
= (void *) input_file_chain
.head
;
3264 f
= f
->next_real_file
)
3267 char *in
= lrealpath (f
->local_sym_name
);
3268 if (filename_cmp (in
, out
) == 0)
3269 einfo (_("%F%P: input file '%s' is the same as output file\n"),
3275 output_target
= lang_get_output_target ();
3277 /* Has the user requested a particular endianness on the command
3279 if (command_line
.endian
!= ENDIAN_UNSET
)
3281 /* Get the chosen target. */
3282 const bfd_target
*target
3283 = bfd_iterate_over_targets (get_target
, (void *) output_target
);
3285 /* If the target is not supported, we cannot do anything. */
3288 enum bfd_endian desired_endian
;
3290 if (command_line
.endian
== ENDIAN_BIG
)
3291 desired_endian
= BFD_ENDIAN_BIG
;
3293 desired_endian
= BFD_ENDIAN_LITTLE
;
3295 /* See if the target has the wrong endianness. This should
3296 not happen if the linker script has provided big and
3297 little endian alternatives, but some scrips don't do
3299 if (target
->byteorder
!= desired_endian
)
3301 /* If it does, then see if the target provides
3302 an alternative with the correct endianness. */
3303 if (target
->alternative_target
!= NULL
3304 && (target
->alternative_target
->byteorder
== desired_endian
))
3305 output_target
= target
->alternative_target
->name
;
3308 /* Try to find a target as similar as possible to
3309 the default target, but which has the desired
3310 endian characteristic. */
3311 bfd_iterate_over_targets (closest_target_match
,
3314 /* Oh dear - we could not find any targets that
3315 satisfy our requirements. */
3317 einfo (_("%P: warning: could not find any targets"
3318 " that match endianness requirement\n"));
3320 output_target
= winner
->name
;
3326 link_info
.output_bfd
= bfd_openw (name
, output_target
);
3328 if (link_info
.output_bfd
== NULL
)
3330 if (bfd_get_error () == bfd_error_invalid_target
)
3331 einfo (_("%F%P: target %s not found\n"), output_target
);
3333 einfo (_("%F%P: cannot open output file %s: %E\n"), name
);
3336 delete_output_file_on_failure
= true;
3338 if (!bfd_set_format (link_info
.output_bfd
, bfd_object
))
3339 einfo (_("%F%P: %s: can not make object file: %E\n"), name
);
3340 if (!bfd_set_arch_mach (link_info
.output_bfd
,
3341 ldfile_output_architecture
,
3342 ldfile_output_machine
))
3343 einfo (_("%F%P: %s: can not set architecture: %E\n"), name
);
3345 link_info
.hash
= bfd_link_hash_table_create (link_info
.output_bfd
);
3346 if (link_info
.hash
== NULL
)
3347 einfo (_("%F%P: can not create hash table: %E\n"));
3349 bfd_set_gp_size (link_info
.output_bfd
, g_switch_value
);
3353 ldlang_open_output (lang_statement_union_type
*statement
)
3355 switch (statement
->header
.type
)
3357 case lang_output_statement_enum
:
3358 ASSERT (link_info
.output_bfd
== NULL
);
3359 open_output (statement
->output_statement
.name
);
3360 ldemul_set_output_arch ();
3361 if (config
.magic_demand_paged
3362 && !bfd_link_relocatable (&link_info
))
3363 link_info
.output_bfd
->flags
|= D_PAGED
;
3365 link_info
.output_bfd
->flags
&= ~D_PAGED
;
3366 if (config
.text_read_only
)
3367 link_info
.output_bfd
->flags
|= WP_TEXT
;
3369 link_info
.output_bfd
->flags
&= ~WP_TEXT
;
3370 if (link_info
.traditional_format
)
3371 link_info
.output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
3373 link_info
.output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
3376 case lang_target_statement_enum
:
3377 current_target
= statement
->target_statement
.target
;
3385 init_opb (asection
*s
)
3390 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
3392 && (s
->flags
& SEC_ELF_OCTETS
) != 0)
3395 x
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3396 ldfile_output_machine
);
3398 while ((x
& 1) == 0)
3406 /* Open all the input files. */
3410 OPEN_BFD_NORMAL
= 0,
3414 #if BFD_SUPPORTS_PLUGINS
3415 static lang_input_statement_type
*plugin_insert
= NULL
;
3416 static struct bfd_link_hash_entry
*plugin_undefs
= NULL
;
3420 open_input_bfds (lang_statement_union_type
*s
, enum open_bfd_mode mode
)
3422 for (; s
!= NULL
; s
= s
->header
.next
)
3424 switch (s
->header
.type
)
3426 case lang_constructors_statement_enum
:
3427 open_input_bfds (constructor_list
.head
, mode
);
3429 case lang_output_section_statement_enum
:
3430 open_input_bfds (s
->output_section_statement
.children
.head
, mode
);
3432 case lang_wild_statement_enum
:
3433 /* Maybe we should load the file's symbols. */
3434 if ((mode
& OPEN_BFD_RESCAN
) == 0
3435 && s
->wild_statement
.filename
3436 && !wildcardp (s
->wild_statement
.filename
)
3437 && !archive_path (s
->wild_statement
.filename
))
3438 lookup_name (s
->wild_statement
.filename
);
3439 open_input_bfds (s
->wild_statement
.children
.head
, mode
);
3441 case lang_group_statement_enum
:
3443 struct bfd_link_hash_entry
*undefs
;
3444 #if BFD_SUPPORTS_PLUGINS
3445 lang_input_statement_type
*plugin_insert_save
;
3448 /* We must continually search the entries in the group
3449 until no new symbols are added to the list of undefined
3454 #if BFD_SUPPORTS_PLUGINS
3455 plugin_insert_save
= plugin_insert
;
3457 undefs
= link_info
.hash
->undefs_tail
;
3458 open_input_bfds (s
->group_statement
.children
.head
,
3459 mode
| OPEN_BFD_FORCE
);
3461 while (undefs
!= link_info
.hash
->undefs_tail
3462 #if BFD_SUPPORTS_PLUGINS
3463 /* Objects inserted by a plugin, which are loaded
3464 before we hit this loop, may have added new
3466 || (plugin_insert
!= plugin_insert_save
&& plugin_undefs
)
3471 case lang_target_statement_enum
:
3472 current_target
= s
->target_statement
.target
;
3474 case lang_input_statement_enum
:
3475 if (s
->input_statement
.flags
.real
)
3477 lang_statement_union_type
**os_tail
;
3478 lang_statement_list_type add
;
3481 s
->input_statement
.target
= current_target
;
3483 /* If we are being called from within a group, and this
3484 is an archive which has already been searched, then
3485 force it to be researched unless the whole archive
3486 has been loaded already. Do the same for a rescan.
3487 Likewise reload --as-needed shared libs. */
3488 if (mode
!= OPEN_BFD_NORMAL
3489 #if BFD_SUPPORTS_PLUGINS
3490 && ((mode
& OPEN_BFD_RESCAN
) == 0
3491 || plugin_insert
== NULL
)
3493 && s
->input_statement
.flags
.loaded
3494 && (abfd
= s
->input_statement
.the_bfd
) != NULL
3495 && ((bfd_get_format (abfd
) == bfd_archive
3496 && !s
->input_statement
.flags
.whole_archive
)
3497 || (bfd_get_format (abfd
) == bfd_object
3498 && ((abfd
->flags
) & DYNAMIC
) != 0
3499 && s
->input_statement
.flags
.add_DT_NEEDED_for_regular
3500 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
3501 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)))
3503 s
->input_statement
.flags
.loaded
= false;
3504 s
->input_statement
.flags
.reload
= true;
3507 os_tail
= lang_os_list
.tail
;
3508 lang_list_init (&add
);
3510 if (!load_symbols (&s
->input_statement
, &add
))
3511 config
.make_executable
= false;
3513 if (add
.head
!= NULL
)
3515 /* If this was a script with output sections then
3516 tack any added statements on to the end of the
3517 list. This avoids having to reorder the output
3518 section statement list. Very likely the user
3519 forgot -T, and whatever we do here will not meet
3520 naive user expectations. */
3521 if (os_tail
!= lang_os_list
.tail
)
3523 einfo (_("%P: warning: %s contains output sections;"
3524 " did you forget -T?\n"),
3525 s
->input_statement
.filename
);
3526 *stat_ptr
->tail
= add
.head
;
3527 stat_ptr
->tail
= add
.tail
;
3531 *add
.tail
= s
->header
.next
;
3532 s
->header
.next
= add
.head
;
3536 #if BFD_SUPPORTS_PLUGINS
3537 /* If we have found the point at which a plugin added new
3538 files, clear plugin_insert to enable archive rescan. */
3539 if (&s
->input_statement
== plugin_insert
)
3540 plugin_insert
= NULL
;
3543 case lang_assignment_statement_enum
:
3544 if (s
->assignment_statement
.exp
->type
.node_class
!= etree_assert
)
3545 exp_fold_tree_no_dot (s
->assignment_statement
.exp
);
3552 /* Exit if any of the files were missing. */
3553 if (input_flags
.missing_file
)
3557 #ifdef ENABLE_LIBCTF
3558 /* Emit CTF errors and warnings. fp can be NULL to report errors/warnings
3559 that happened specifically at CTF open time. */
3561 lang_ctf_errs_warnings (ctf_dict_t
*fp
)
3563 ctf_next_t
*i
= NULL
;
3568 while ((text
= ctf_errwarning_next (fp
, &i
, &is_warning
, &err
)) != NULL
)
3570 einfo (_("%s: %s\n"), is_warning
? _("CTF warning"): _("CTF error"),
3574 if (err
!= ECTF_NEXT_END
)
3576 einfo (_("CTF error: cannot get CTF errors: `%s'\n"),
3580 /* `err' returns errors from the error/warning iterator in particular.
3581 These never assert. But if we have an fp, that could have recorded
3582 an assertion failure: assert if it has done so. */
3583 ASSERT (!fp
|| ctf_errno (fp
) != ECTF_INTERNAL
);
3586 /* Open the CTF sections in the input files with libctf: if any were opened,
3587 create a fake input file that we'll write the merged CTF data to later
3591 ldlang_open_ctf (void)
3596 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3600 /* Incoming files from the compiler have a single ctf_dict_t in them
3601 (which is presented to us by the libctf API in a ctf_archive_t
3602 wrapper): files derived from a previous relocatable link have a CTF
3603 archive containing possibly many CTF files. */
3605 if ((file
->the_ctf
= ctf_bfdopen (file
->the_bfd
, &err
)) == NULL
)
3607 if (err
!= ECTF_NOCTFDATA
)
3609 lang_ctf_errs_warnings (NULL
);
3610 einfo (_("%P: warning: CTF section in %pB not loaded; "
3611 "its types will be discarded: %s\n"), file
->the_bfd
,
3617 /* Prevent the contents of this section from being written, while
3618 requiring the section itself to be duplicated in the output, but only
3620 /* This section must exist if ctf_bfdopen() succeeded. */
3621 sect
= bfd_get_section_by_name (file
->the_bfd
, ".ctf");
3623 sect
->flags
|= SEC_NEVER_LOAD
| SEC_HAS_CONTENTS
| SEC_LINKER_CREATED
;
3626 sect
->flags
|= SEC_EXCLUDE
;
3636 if ((ctf_output
= ctf_create (&err
)) != NULL
)
3639 einfo (_("%P: warning: CTF output not created: `%s'\n"),
3642 LANG_FOR_EACH_INPUT_STATEMENT (errfile
)
3643 ctf_close (errfile
->the_ctf
);
3646 /* Merge together CTF sections. After this, only the symtab-dependent
3647 function and data object sections need adjustment. */
3650 lang_merge_ctf (void)
3652 asection
*output_sect
;
3658 output_sect
= bfd_get_section_by_name (link_info
.output_bfd
, ".ctf");
3660 /* If the section was discarded, don't waste time merging. */
3661 if (output_sect
== NULL
)
3663 ctf_dict_close (ctf_output
);
3666 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3668 ctf_close (file
->the_ctf
);
3669 file
->the_ctf
= NULL
;
3674 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3679 /* Takes ownership of file->the_ctf. */
3680 if (ctf_link_add_ctf (ctf_output
, file
->the_ctf
, file
->filename
) < 0)
3682 einfo (_("%P: warning: CTF section in %pB cannot be linked: `%s'\n"),
3683 file
->the_bfd
, ctf_errmsg (ctf_errno (ctf_output
)));
3684 ctf_close (file
->the_ctf
);
3685 file
->the_ctf
= NULL
;
3690 if (!config
.ctf_share_duplicated
)
3691 flags
= CTF_LINK_SHARE_UNCONFLICTED
;
3693 flags
= CTF_LINK_SHARE_DUPLICATED
;
3694 if (!config
.ctf_variables
)
3695 flags
|= CTF_LINK_OMIT_VARIABLES_SECTION
;
3696 if (bfd_link_relocatable (&link_info
))
3697 flags
|= CTF_LINK_NO_FILTER_REPORTED_SYMS
;
3699 if (ctf_link (ctf_output
, flags
) < 0)
3701 lang_ctf_errs_warnings (ctf_output
);
3702 einfo (_("%P: warning: CTF linking failed; "
3703 "output will have no CTF section: %s\n"),
3704 ctf_errmsg (ctf_errno (ctf_output
)));
3707 output_sect
->size
= 0;
3708 output_sect
->flags
|= SEC_EXCLUDE
;
3711 /* Output any lingering errors that didn't come from ctf_link. */
3712 lang_ctf_errs_warnings (ctf_output
);
3715 /* Let the emulation acquire strings from the dynamic strtab to help it optimize
3716 the CTF, if supported. */
3719 ldlang_ctf_acquire_strings (struct elf_strtab_hash
*dynstrtab
)
3721 ldemul_acquire_strings_for_ctf (ctf_output
, dynstrtab
);
3724 /* Inform the emulation about the addition of a new dynamic symbol, in BFD
3726 void ldlang_ctf_new_dynsym (int symidx
, struct elf_internal_sym
*sym
)
3728 ldemul_new_dynsym_for_ctf (ctf_output
, symidx
, sym
);
3731 /* Write out the CTF section. Called early, if the emulation isn't going to
3732 need to dedup against the strtab and symtab, then possibly called from the
3733 target linker code if the dedup has happened. */
3735 lang_write_ctf (int late
)
3738 asection
*output_sect
;
3745 /* Emit CTF late if this emulation says it can do so. */
3746 if (ldemul_emit_ctf_early ())
3751 if (!ldemul_emit_ctf_early ())
3755 /* Inform the emulation that all the symbols that will be received have
3758 ldemul_new_dynsym_for_ctf (ctf_output
, 0, NULL
);
3762 output_sect
= bfd_get_section_by_name (link_info
.output_bfd
, ".ctf");
3765 output_sect
->contents
= ctf_link_write (ctf_output
, &output_size
,
3766 CTF_COMPRESSION_THRESHOLD
);
3767 output_sect
->size
= output_size
;
3768 output_sect
->flags
|= SEC_IN_MEMORY
| SEC_KEEP
;
3770 lang_ctf_errs_warnings (ctf_output
);
3771 if (!output_sect
->contents
)
3773 einfo (_("%P: warning: CTF section emission failed; "
3774 "output will have no CTF section: %s\n"),
3775 ctf_errmsg (ctf_errno (ctf_output
)));
3776 output_sect
->size
= 0;
3777 output_sect
->flags
|= SEC_EXCLUDE
;
3781 /* This also closes every CTF input file used in the link. */
3782 ctf_dict_close (ctf_output
);
3785 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3786 file
->the_ctf
= NULL
;
3789 /* Write out the CTF section late, if the emulation needs that. */
3792 ldlang_write_ctf_late (void)
3794 /* Trigger a "late call", if the emulation needs one. */
3800 ldlang_open_ctf (void)
3802 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3806 /* If built without CTF, warn and delete all CTF sections from the output.
3807 (The alternative would be to simply concatenate them, which does not
3808 yield a valid CTF section.) */
3810 if ((sect
= bfd_get_section_by_name (file
->the_bfd
, ".ctf")) != NULL
)
3812 einfo (_("%P: warning: CTF section in %pB not linkable: "
3813 "%P was built without support for CTF\n"), file
->the_bfd
);
3815 sect
->flags
|= SEC_EXCLUDE
;
3820 static void lang_merge_ctf (void) {}
3822 ldlang_ctf_acquire_strings (struct elf_strtab_hash
*dynstrtab
3823 ATTRIBUTE_UNUSED
) {}
3825 ldlang_ctf_new_dynsym (int symidx ATTRIBUTE_UNUSED
,
3826 struct elf_internal_sym
*sym ATTRIBUTE_UNUSED
) {}
3827 static void lang_write_ctf (int late ATTRIBUTE_UNUSED
) {}
3828 void ldlang_write_ctf_late (void) {}
3831 /* Add the supplied name to the symbol table as an undefined reference.
3832 This is a two step process as the symbol table doesn't even exist at
3833 the time the ld command line is processed. First we put the name
3834 on a list, then, once the output file has been opened, transfer the
3835 name to the symbol table. */
3837 typedef struct bfd_sym_chain ldlang_undef_chain_list_type
;
3839 #define ldlang_undef_chain_list_head entry_symbol.next
3842 ldlang_add_undef (const char *const name
, bool cmdline ATTRIBUTE_UNUSED
)
3844 ldlang_undef_chain_list_type
*new_undef
;
3846 new_undef
= stat_alloc (sizeof (*new_undef
));
3847 new_undef
->next
= ldlang_undef_chain_list_head
;
3848 ldlang_undef_chain_list_head
= new_undef
;
3850 new_undef
->name
= xstrdup (name
);
3852 if (link_info
.output_bfd
!= NULL
)
3853 insert_undefined (new_undef
->name
);
3856 /* Insert NAME as undefined in the symbol table. */
3859 insert_undefined (const char *name
)
3861 struct bfd_link_hash_entry
*h
;
3863 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, false, true);
3865 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
3866 if (h
->type
== bfd_link_hash_new
)
3868 h
->type
= bfd_link_hash_undefined
;
3869 h
->u
.undef
.abfd
= NULL
;
3870 h
->non_ir_ref_regular
= true;
3871 bfd_link_add_undef (link_info
.hash
, h
);
3875 /* Run through the list of undefineds created above and place them
3876 into the linker hash table as undefined symbols belonging to the
3880 lang_place_undefineds (void)
3882 ldlang_undef_chain_list_type
*ptr
;
3884 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
3885 insert_undefined (ptr
->name
);
3888 /* Mark -u symbols against garbage collection. */
3891 lang_mark_undefineds (void)
3893 ldlang_undef_chain_list_type
*ptr
;
3895 if (is_elf_hash_table (link_info
.hash
))
3896 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
3898 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*)
3899 bfd_link_hash_lookup (link_info
.hash
, ptr
->name
, false, false, true);
3905 /* Structure used to build the list of symbols that the user has required
3908 struct require_defined_symbol
3911 struct require_defined_symbol
*next
;
3914 /* The list of symbols that the user has required be defined. */
3916 static struct require_defined_symbol
*require_defined_symbol_list
;
3918 /* Add a new symbol NAME to the list of symbols that are required to be
3922 ldlang_add_require_defined (const char *const name
)
3924 struct require_defined_symbol
*ptr
;
3926 ldlang_add_undef (name
, true);
3927 ptr
= stat_alloc (sizeof (*ptr
));
3928 ptr
->next
= require_defined_symbol_list
;
3929 ptr
->name
= strdup (name
);
3930 require_defined_symbol_list
= ptr
;
3933 /* Check that all symbols the user required to be defined, are defined,
3934 raise an error if we find a symbol that is not defined. */
3937 ldlang_check_require_defined_symbols (void)
3939 struct require_defined_symbol
*ptr
;
3941 for (ptr
= require_defined_symbol_list
; ptr
!= NULL
; ptr
= ptr
->next
)
3943 struct bfd_link_hash_entry
*h
;
3945 h
= bfd_link_hash_lookup (link_info
.hash
, ptr
->name
,
3946 false, false, true);
3948 || (h
->type
!= bfd_link_hash_defined
3949 && h
->type
!= bfd_link_hash_defweak
))
3950 einfo(_("%X%P: required symbol `%s' not defined\n"), ptr
->name
);
3954 /* Check for all readonly or some readwrite sections. */
3957 check_input_sections
3958 (lang_statement_union_type
*s
,
3959 lang_output_section_statement_type
*output_section_statement
)
3961 for (; s
!= NULL
; s
= s
->header
.next
)
3963 switch (s
->header
.type
)
3965 case lang_wild_statement_enum
:
3966 walk_wild (&s
->wild_statement
, check_section_callback
,
3967 output_section_statement
);
3968 if (!output_section_statement
->all_input_readonly
)
3971 case lang_constructors_statement_enum
:
3972 check_input_sections (constructor_list
.head
,
3973 output_section_statement
);
3974 if (!output_section_statement
->all_input_readonly
)
3977 case lang_group_statement_enum
:
3978 check_input_sections (s
->group_statement
.children
.head
,
3979 output_section_statement
);
3980 if (!output_section_statement
->all_input_readonly
)
3989 /* Update wildcard statements if needed. */
3992 update_wild_statements (lang_statement_union_type
*s
)
3994 struct wildcard_list
*sec
;
3996 switch (sort_section
)
4006 for (; s
!= NULL
; s
= s
->header
.next
)
4008 switch (s
->header
.type
)
4013 case lang_wild_statement_enum
:
4014 for (sec
= s
->wild_statement
.section_list
; sec
!= NULL
;
4016 /* Don't sort .init/.fini sections. */
4017 if (strcmp (sec
->spec
.name
, ".init") != 0
4018 && strcmp (sec
->spec
.name
, ".fini") != 0)
4020 switch (sec
->spec
.sorted
)
4023 sec
->spec
.sorted
= sort_section
;
4026 if (sort_section
== by_alignment
)
4027 sec
->spec
.sorted
= by_name_alignment
;
4030 if (sort_section
== by_name
)
4031 sec
->spec
.sorted
= by_alignment_name
;
4036 s
->wild_statement
.any_specs_sorted
= true;
4040 case lang_constructors_statement_enum
:
4041 update_wild_statements (constructor_list
.head
);
4044 case lang_output_section_statement_enum
:
4045 update_wild_statements
4046 (s
->output_section_statement
.children
.head
);
4049 case lang_group_statement_enum
:
4050 update_wild_statements (s
->group_statement
.children
.head
);
4058 /* Open input files and attach to output sections. */
4061 map_input_to_output_sections
4062 (lang_statement_union_type
*s
, const char *target
,
4063 lang_output_section_statement_type
*os
)
4065 for (; s
!= NULL
; s
= s
->header
.next
)
4067 lang_output_section_statement_type
*tos
;
4069 unsigned int type
= 0;
4071 switch (s
->header
.type
)
4073 case lang_wild_statement_enum
:
4074 wild (&s
->wild_statement
, target
, os
);
4076 case lang_constructors_statement_enum
:
4077 map_input_to_output_sections (constructor_list
.head
,
4081 case lang_output_section_statement_enum
:
4082 tos
= &s
->output_section_statement
;
4083 if (tos
->constraint
== ONLY_IF_RW
4084 || tos
->constraint
== ONLY_IF_RO
)
4086 tos
->all_input_readonly
= true;
4087 check_input_sections (tos
->children
.head
, tos
);
4088 if (tos
->all_input_readonly
!= (tos
->constraint
== ONLY_IF_RO
))
4089 tos
->constraint
= -1;
4091 if (tos
->constraint
>= 0)
4092 map_input_to_output_sections (tos
->children
.head
,
4096 case lang_output_statement_enum
:
4098 case lang_target_statement_enum
:
4099 target
= s
->target_statement
.target
;
4101 case lang_group_statement_enum
:
4102 map_input_to_output_sections (s
->group_statement
.children
.head
,
4106 case lang_data_statement_enum
:
4107 /* Make sure that any sections mentioned in the expression
4109 exp_init_os (s
->data_statement
.exp
);
4110 /* The output section gets CONTENTS, ALLOC and LOAD, but
4111 these may be overridden by the script. */
4112 flags
= SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
;
4113 switch (os
->sectype
)
4115 case normal_section
:
4116 case overlay_section
:
4117 case first_overlay_section
:
4119 case noalloc_section
:
4120 flags
= SEC_HAS_CONTENTS
;
4122 case readonly_section
:
4123 flags
|= SEC_READONLY
;
4125 case typed_readonly_section
:
4126 flags
|= SEC_READONLY
;
4129 if (os
->sectype_value
->type
.node_class
== etree_name
4130 && os
->sectype_value
->type
.node_code
== NAME
)
4132 const char *name
= os
->sectype_value
->name
.name
;
4133 if (strcmp (name
, "SHT_PROGBITS") == 0)
4134 type
= SHT_PROGBITS
;
4135 else if (strcmp (name
, "SHT_STRTAB") == 0)
4137 else if (strcmp (name
, "SHT_NOTE") == 0)
4139 else if (strcmp (name
, "SHT_NOBITS") == 0)
4141 else if (strcmp (name
, "SHT_INIT_ARRAY") == 0)
4142 type
= SHT_INIT_ARRAY
;
4143 else if (strcmp (name
, "SHT_FINI_ARRAY") == 0)
4144 type
= SHT_FINI_ARRAY
;
4145 else if (strcmp (name
, "SHT_PREINIT_ARRAY") == 0)
4146 type
= SHT_PREINIT_ARRAY
;
4148 einfo (_ ("%F%P: invalid type for output section `%s'\n"),
4153 exp_fold_tree_no_dot (os
->sectype_value
);
4154 if (expld
.result
.valid_p
)
4155 type
= expld
.result
.value
;
4157 einfo (_ ("%F%P: invalid type for output section `%s'\n"),
4161 case noload_section
:
4162 if (bfd_get_flavour (link_info
.output_bfd
)
4163 == bfd_target_elf_flavour
)
4164 flags
= SEC_NEVER_LOAD
| SEC_ALLOC
;
4166 flags
= SEC_NEVER_LOAD
| SEC_HAS_CONTENTS
;
4169 if (os
->bfd_section
== NULL
)
4170 init_os (os
, flags
| SEC_READONLY
);
4172 os
->bfd_section
->flags
|= flags
;
4173 os
->bfd_section
->type
= type
;
4175 case lang_input_section_enum
:
4177 case lang_fill_statement_enum
:
4178 case lang_object_symbols_statement_enum
:
4179 case lang_reloc_statement_enum
:
4180 case lang_padding_statement_enum
:
4181 case lang_input_statement_enum
:
4182 if (os
!= NULL
&& os
->bfd_section
== NULL
)
4185 case lang_assignment_statement_enum
:
4186 if (os
!= NULL
&& os
->bfd_section
== NULL
)
4189 /* Make sure that any sections mentioned in the assignment
4191 exp_init_os (s
->assignment_statement
.exp
);
4193 case lang_address_statement_enum
:
4194 /* Mark the specified section with the supplied address.
4195 If this section was actually a segment marker, then the
4196 directive is ignored if the linker script explicitly
4197 processed the segment marker. Originally, the linker
4198 treated segment directives (like -Ttext on the
4199 command-line) as section directives. We honor the
4200 section directive semantics for backwards compatibility;
4201 linker scripts that do not specifically check for
4202 SEGMENT_START automatically get the old semantics. */
4203 if (!s
->address_statement
.segment
4204 || !s
->address_statement
.segment
->used
)
4206 const char *name
= s
->address_statement
.section_name
;
4208 /* Create the output section statement here so that
4209 orphans with a set address will be placed after other
4210 script sections. If we let the orphan placement code
4211 place them in amongst other sections then the address
4212 will affect following script sections, which is
4213 likely to surprise naive users. */
4214 tos
= lang_output_section_statement_lookup (name
, 0, 1);
4215 tos
->addr_tree
= s
->address_statement
.address
;
4216 if (tos
->bfd_section
== NULL
)
4220 case lang_insert_statement_enum
:
4222 case lang_input_matcher_enum
:
4228 /* An insert statement snips out all the linker statements from the
4229 start of the list and places them after the output section
4230 statement specified by the insert. This operation is complicated
4231 by the fact that we keep a doubly linked list of output section
4232 statements as well as the singly linked list of all statements.
4233 FIXME someday: Twiddling with the list not only moves statements
4234 from the user's script but also input and group statements that are
4235 built from command line object files and --start-group. We only
4236 get away with this because the list pointers used by file_chain
4237 and input_file_chain are not reordered, and processing via
4238 statement_list after this point mostly ignores input statements.
4239 One exception is the map file, where LOAD and START GROUP/END GROUP
4240 can end up looking odd. */
4243 process_insert_statements (lang_statement_union_type
**start
)
4245 lang_statement_union_type
**s
;
4246 lang_output_section_statement_type
*first_os
= NULL
;
4247 lang_output_section_statement_type
*last_os
= NULL
;
4248 lang_output_section_statement_type
*os
;
4253 if ((*s
)->header
.type
== lang_output_section_statement_enum
)
4255 /* Keep pointers to the first and last output section
4256 statement in the sequence we may be about to move. */
4257 os
= &(*s
)->output_section_statement
;
4259 ASSERT (last_os
== NULL
|| last_os
->next
== os
);
4262 /* Set constraint negative so that lang_output_section_find
4263 won't match this output section statement. At this
4264 stage in linking constraint has values in the range
4265 [-1, ONLY_IN_RW]. */
4266 last_os
->constraint
= -2 - last_os
->constraint
;
4267 if (first_os
== NULL
)
4270 else if ((*s
)->header
.type
== lang_group_statement_enum
)
4272 /* A user might put -T between --start-group and
4273 --end-group. One way this odd construct might arise is
4274 from a wrapper around ld to change library search
4275 behaviour. For example:
4277 exec real_ld --start-group "$@" --end-group
4278 This isn't completely unreasonable so go looking inside a
4279 group statement for insert statements. */
4280 process_insert_statements (&(*s
)->group_statement
.children
.head
);
4282 else if ((*s
)->header
.type
== lang_insert_statement_enum
)
4284 lang_insert_statement_type
*i
= &(*s
)->insert_statement
;
4285 lang_output_section_statement_type
*where
;
4286 lang_statement_union_type
**ptr
;
4287 lang_statement_union_type
*first
;
4289 if (link_info
.non_contiguous_regions
)
4291 einfo (_("warning: INSERT statement in linker script is "
4292 "incompatible with --enable-non-contiguous-regions.\n"));
4295 where
= lang_output_section_find (i
->where
);
4296 if (where
!= NULL
&& i
->is_before
)
4299 where
= where
->prev
;
4300 while (where
!= NULL
&& where
->constraint
< 0);
4304 einfo (_("%F%P: %s not found for insert\n"), i
->where
);
4308 /* Deal with reordering the output section statement list. */
4309 if (last_os
!= NULL
)
4311 asection
*first_sec
, *last_sec
;
4312 struct lang_output_section_statement_struct
**next
;
4314 /* Snip out the output sections we are moving. */
4315 first_os
->prev
->next
= last_os
->next
;
4316 if (last_os
->next
== NULL
)
4318 next
= &first_os
->prev
->next
;
4319 lang_os_list
.tail
= (lang_statement_union_type
**) next
;
4322 last_os
->next
->prev
= first_os
->prev
;
4323 /* Add them in at the new position. */
4324 last_os
->next
= where
->next
;
4325 if (where
->next
== NULL
)
4327 next
= &last_os
->next
;
4328 lang_os_list
.tail
= (lang_statement_union_type
**) next
;
4331 where
->next
->prev
= last_os
;
4332 first_os
->prev
= where
;
4333 where
->next
= first_os
;
4335 /* Move the bfd sections in the same way. */
4338 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
4340 os
->constraint
= -2 - os
->constraint
;
4341 if (os
->bfd_section
!= NULL
4342 && os
->bfd_section
->owner
!= NULL
)
4344 last_sec
= os
->bfd_section
;
4345 if (first_sec
== NULL
)
4346 first_sec
= last_sec
;
4351 if (last_sec
!= NULL
)
4353 asection
*sec
= where
->bfd_section
;
4355 sec
= output_prev_sec_find (where
);
4357 /* The place we want to insert must come after the
4358 sections we are moving. So if we find no
4359 section or if the section is the same as our
4360 last section, then no move is needed. */
4361 if (sec
!= NULL
&& sec
!= last_sec
)
4363 /* Trim them off. */
4364 if (first_sec
->prev
!= NULL
)
4365 first_sec
->prev
->next
= last_sec
->next
;
4367 link_info
.output_bfd
->sections
= last_sec
->next
;
4368 if (last_sec
->next
!= NULL
)
4369 last_sec
->next
->prev
= first_sec
->prev
;
4371 link_info
.output_bfd
->section_last
= first_sec
->prev
;
4373 last_sec
->next
= sec
->next
;
4374 if (sec
->next
!= NULL
)
4375 sec
->next
->prev
= last_sec
;
4377 link_info
.output_bfd
->section_last
= last_sec
;
4378 first_sec
->prev
= sec
;
4379 sec
->next
= first_sec
;
4387 ptr
= insert_os_after (where
);
4388 /* Snip everything from the start of the list, up to and
4389 including the insert statement we are currently processing. */
4391 *start
= (*s
)->header
.next
;
4392 /* Add them back where they belong, minus the insert. */
4395 statement_list
.tail
= s
;
4400 s
= &(*s
)->header
.next
;
4403 /* Undo constraint twiddling. */
4404 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
4406 os
->constraint
= -2 - os
->constraint
;
4412 /* An output section might have been removed after its statement was
4413 added. For example, ldemul_before_allocation can remove dynamic
4414 sections if they turn out to be not needed. Clean them up here. */
4417 strip_excluded_output_sections (void)
4419 lang_output_section_statement_type
*os
;
4421 /* Run lang_size_sections (if not already done). */
4422 if (expld
.phase
!= lang_mark_phase_enum
)
4424 expld
.phase
= lang_mark_phase_enum
;
4425 expld
.dataseg
.phase
= exp_seg_none
;
4426 one_lang_size_sections_pass (NULL
, false);
4427 lang_reset_memory_regions ();
4430 for (os
= (void *) lang_os_list
.head
;
4434 asection
*output_section
;
4437 if (os
->constraint
< 0)
4440 output_section
= os
->bfd_section
;
4441 if (output_section
== NULL
)
4444 exclude
= (output_section
->rawsize
== 0
4445 && (output_section
->flags
& SEC_KEEP
) == 0
4446 && !bfd_section_removed_from_list (link_info
.output_bfd
,
4449 /* Some sections have not yet been sized, notably .gnu.version,
4450 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
4451 input sections, so don't drop output sections that have such
4452 input sections unless they are also marked SEC_EXCLUDE. */
4453 if (exclude
&& output_section
->map_head
.s
!= NULL
)
4457 for (s
= output_section
->map_head
.s
; s
!= NULL
; s
= s
->map_head
.s
)
4458 if ((s
->flags
& SEC_EXCLUDE
) == 0
4459 && ((s
->flags
& SEC_LINKER_CREATED
) != 0
4460 || link_info
.emitrelocations
))
4469 /* We don't set bfd_section to NULL since bfd_section of the
4470 removed output section statement may still be used. */
4471 if (!os
->update_dot
)
4473 output_section
->flags
|= SEC_EXCLUDE
;
4474 bfd_section_list_remove (link_info
.output_bfd
, output_section
);
4475 link_info
.output_bfd
->section_count
--;
4480 /* Called from ldwrite to clear out asection.map_head and
4481 asection.map_tail for use as link_orders in ldwrite. */
4484 lang_clear_os_map (void)
4486 lang_output_section_statement_type
*os
;
4488 if (map_head_is_link_order
)
4491 for (os
= (void *) lang_os_list
.head
;
4495 asection
*output_section
;
4497 if (os
->constraint
< 0)
4500 output_section
= os
->bfd_section
;
4501 if (output_section
== NULL
)
4504 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
4505 output_section
->map_head
.link_order
= NULL
;
4506 output_section
->map_tail
.link_order
= NULL
;
4509 /* Stop future calls to lang_add_section from messing with map_head
4510 and map_tail link_order fields. */
4511 map_head_is_link_order
= true;
4515 print_output_section_statement
4516 (lang_output_section_statement_type
*output_section_statement
)
4518 asection
*section
= output_section_statement
->bfd_section
;
4521 if (output_section_statement
!= abs_output_section
)
4523 minfo ("\n%s", output_section_statement
->name
);
4525 if (section
!= NULL
)
4527 print_dot
= section
->vma
;
4529 len
= strlen (output_section_statement
->name
);
4530 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
4535 print_spaces (SECTION_NAME_MAP_LENGTH
- len
);
4537 minfo ("0x%V %W", section
->vma
, TO_ADDR (section
->size
));
4539 if (section
->vma
!= section
->lma
)
4540 minfo (_(" load address 0x%V"), section
->lma
);
4542 if (output_section_statement
->update_dot_tree
!= NULL
)
4543 exp_fold_tree (output_section_statement
->update_dot_tree
,
4544 bfd_abs_section_ptr
, &print_dot
);
4550 print_statement_list (output_section_statement
->children
.head
,
4551 output_section_statement
);
4555 print_assignment (lang_assignment_statement_type
*assignment
,
4556 lang_output_section_statement_type
*output_section
)
4562 print_spaces (SECTION_NAME_MAP_LENGTH
);
4564 if (assignment
->exp
->type
.node_class
== etree_assert
)
4567 tree
= assignment
->exp
->assert_s
.child
;
4571 const char *dst
= assignment
->exp
->assign
.dst
;
4573 is_dot
= (dst
[0] == '.' && dst
[1] == 0);
4574 tree
= assignment
->exp
;
4577 osec
= output_section
->bfd_section
;
4579 osec
= bfd_abs_section_ptr
;
4581 if (assignment
->exp
->type
.node_class
!= etree_provide
)
4582 exp_fold_tree (tree
, osec
, &print_dot
);
4584 expld
.result
.valid_p
= false;
4587 const char *str
= buf
;
4588 if (expld
.result
.valid_p
)
4592 if (assignment
->exp
->type
.node_class
== etree_assert
4594 || expld
.assign_name
!= NULL
)
4596 value
= expld
.result
.value
;
4598 if (expld
.result
.section
!= NULL
)
4599 value
+= expld
.result
.section
->vma
;
4603 bfd_sprintf_vma (link_info
.output_bfd
, buf
+ 2, value
);
4609 struct bfd_link_hash_entry
*h
;
4611 h
= bfd_link_hash_lookup (link_info
.hash
, assignment
->exp
->assign
.dst
,
4612 false, false, true);
4614 && (h
->type
== bfd_link_hash_defined
4615 || h
->type
== bfd_link_hash_defweak
))
4617 value
= h
->u
.def
.value
;
4618 value
+= h
->u
.def
.section
->output_section
->vma
;
4619 value
+= h
->u
.def
.section
->output_offset
;
4624 bfd_sprintf_vma (link_info
.output_bfd
, buf
+ 3, value
);
4628 str
= "[unresolved]";
4633 if (assignment
->exp
->type
.node_class
== etree_provide
)
4638 expld
.assign_name
= NULL
;
4640 fprintf (config
.map_file
, "%-34s", str
);
4641 exp_print_tree (assignment
->exp
);
4646 print_input_statement (lang_input_statement_type
*statm
)
4648 if (statm
->filename
!= NULL
)
4649 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
4652 /* Print all symbols defined in a particular section. This is called
4653 via bfd_link_hash_traverse, or by print_all_symbols. */
4656 print_one_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
)
4658 asection
*sec
= (asection
*) ptr
;
4660 if ((hash_entry
->type
== bfd_link_hash_defined
4661 || hash_entry
->type
== bfd_link_hash_defweak
)
4662 && sec
== hash_entry
->u
.def
.section
)
4664 print_spaces (SECTION_NAME_MAP_LENGTH
);
4666 (hash_entry
->u
.def
.value
4667 + hash_entry
->u
.def
.section
->output_offset
4668 + hash_entry
->u
.def
.section
->output_section
->vma
));
4670 minfo (" %pT\n", hash_entry
->root
.string
);
4677 hash_entry_addr_cmp (const void *a
, const void *b
)
4679 const struct bfd_link_hash_entry
*l
= *(const struct bfd_link_hash_entry
**)a
;
4680 const struct bfd_link_hash_entry
*r
= *(const struct bfd_link_hash_entry
**)b
;
4682 if (l
->u
.def
.value
< r
->u
.def
.value
)
4684 else if (l
->u
.def
.value
> r
->u
.def
.value
)
4691 print_all_symbols (asection
*sec
)
4693 input_section_userdata_type
*ud
= bfd_section_userdata (sec
);
4694 struct map_symbol_def
*def
;
4695 struct bfd_link_hash_entry
**entries
;
4701 *ud
->map_symbol_def_tail
= 0;
4703 /* Sort the symbols by address. */
4704 entries
= (struct bfd_link_hash_entry
**)
4705 obstack_alloc (&map_obstack
,
4706 ud
->map_symbol_def_count
* sizeof (*entries
));
4708 for (i
= 0, def
= ud
->map_symbol_def_head
; def
; def
= def
->next
, i
++)
4709 entries
[i
] = def
->entry
;
4711 qsort (entries
, ud
->map_symbol_def_count
, sizeof (*entries
),
4712 hash_entry_addr_cmp
);
4714 /* Print the symbols. */
4715 for (i
= 0; i
< ud
->map_symbol_def_count
; i
++)
4716 ldemul_print_symbol (entries
[i
], sec
);
4718 obstack_free (&map_obstack
, entries
);
4721 /* Print information about an input section to the map file. */
4724 print_input_section (asection
*i
, bool is_discarded
)
4726 bfd_size_type size
= i
->size
;
4732 minfo (" %s", i
->name
);
4734 len
= 1 + strlen (i
->name
);
4735 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
4740 print_spaces (SECTION_NAME_MAP_LENGTH
- len
);
4742 if (i
->output_section
!= NULL
4743 && i
->output_section
->owner
== link_info
.output_bfd
)
4744 addr
= i
->output_section
->vma
+ i
->output_offset
;
4753 bfd_sprintf_vma (link_info
.output_bfd
, buf
, addr
);
4754 minfo ("0x%s %W %pB\n", buf
, TO_ADDR (size
), i
->owner
);
4756 if (size
!= i
->rawsize
&& i
->rawsize
!= 0)
4758 len
= SECTION_NAME_MAP_LENGTH
+ 3 + strlen (buf
);
4760 minfo (_("%W (size before relaxing)\n"), TO_ADDR (i
->rawsize
));
4763 if (i
->output_section
!= NULL
4764 && i
->output_section
->owner
== link_info
.output_bfd
)
4766 if (link_info
.reduce_memory_overheads
)
4767 bfd_link_hash_traverse (link_info
.hash
, ldemul_print_symbol
, i
);
4769 print_all_symbols (i
);
4771 /* Update print_dot, but make sure that we do not move it
4772 backwards - this could happen if we have overlays and a
4773 later overlay is shorter than an earier one. */
4774 if (addr
+ TO_ADDR (size
) > print_dot
)
4775 print_dot
= addr
+ TO_ADDR (size
);
4780 print_fill_statement (lang_fill_statement_type
*fill
)
4784 fputs (" FILL mask 0x", config
.map_file
);
4785 for (p
= fill
->fill
->data
, size
= fill
->fill
->size
; size
!= 0; p
++, size
--)
4786 fprintf (config
.map_file
, "%02x", *p
);
4787 fputs ("\n", config
.map_file
);
4791 print_data_statement (lang_data_statement_type
*data
)
4797 init_opb (data
->output_section
);
4798 print_spaces (SECTION_NAME_MAP_LENGTH
);
4800 addr
= data
->output_offset
;
4801 if (data
->output_section
!= NULL
)
4802 addr
+= data
->output_section
->vma
;
4830 if (size
< TO_SIZE ((unsigned) 1))
4831 size
= TO_SIZE ((unsigned) 1);
4832 minfo ("0x%V %W %s 0x%v", addr
, TO_ADDR (size
), name
, data
->value
);
4834 if (data
->exp
->type
.node_class
!= etree_value
)
4837 exp_print_tree (data
->exp
);
4842 print_dot
= addr
+ TO_ADDR (size
);
4845 /* Print an address statement. These are generated by options like
4849 print_address_statement (lang_address_statement_type
*address
)
4851 minfo (_("Address of section %s set to "), address
->section_name
);
4852 exp_print_tree (address
->address
);
4856 /* Print a reloc statement. */
4859 print_reloc_statement (lang_reloc_statement_type
*reloc
)
4864 init_opb (reloc
->output_section
);
4865 print_spaces (SECTION_NAME_MAP_LENGTH
);
4867 addr
= reloc
->output_offset
;
4868 if (reloc
->output_section
!= NULL
)
4869 addr
+= reloc
->output_section
->vma
;
4871 size
= bfd_get_reloc_size (reloc
->howto
);
4873 minfo ("0x%V %W RELOC %s ", addr
, TO_ADDR (size
), reloc
->howto
->name
);
4875 if (reloc
->name
!= NULL
)
4876 minfo ("%s+", reloc
->name
);
4878 minfo ("%s+", reloc
->section
->name
);
4880 exp_print_tree (reloc
->addend_exp
);
4884 print_dot
= addr
+ TO_ADDR (size
);
4888 print_padding_statement (lang_padding_statement_type
*s
)
4893 init_opb (s
->output_section
);
4896 len
= sizeof " *fill*" - 1;
4897 print_spaces (SECTION_NAME_MAP_LENGTH
- len
);
4899 addr
= s
->output_offset
;
4900 if (s
->output_section
!= NULL
)
4901 addr
+= s
->output_section
->vma
;
4902 minfo ("0x%V %W ", addr
, TO_ADDR (s
->size
));
4904 if (s
->fill
->size
!= 0)
4908 for (p
= s
->fill
->data
, size
= s
->fill
->size
; size
!= 0; p
++, size
--)
4909 fprintf (config
.map_file
, "%02x", *p
);
4914 print_dot
= addr
+ TO_ADDR (s
->size
);
4918 print_wild_statement (lang_wild_statement_type
*w
,
4919 lang_output_section_statement_type
*os
)
4921 struct wildcard_list
*sec
;
4925 if (w
->exclude_name_list
)
4928 minfo ("EXCLUDE_FILE(%s", w
->exclude_name_list
->name
);
4929 for (tmp
= w
->exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
4930 minfo (" %s", tmp
->name
);
4934 if (w
->filenames_sorted
)
4935 minfo ("SORT_BY_NAME(");
4936 if (w
->filename
!= NULL
)
4937 minfo ("%s", w
->filename
);
4940 if (w
->filenames_sorted
)
4944 for (sec
= w
->section_list
; sec
; sec
= sec
->next
)
4946 int closing_paren
= 0;
4948 switch (sec
->spec
.sorted
)
4954 minfo ("SORT_BY_NAME(");
4959 minfo ("SORT_BY_ALIGNMENT(");
4963 case by_name_alignment
:
4964 minfo ("SORT_BY_NAME(SORT_BY_ALIGNMENT(");
4968 case by_alignment_name
:
4969 minfo ("SORT_BY_ALIGNMENT(SORT_BY_NAME(");
4974 minfo ("SORT_NONE(");
4978 case by_init_priority
:
4979 minfo ("SORT_BY_INIT_PRIORITY(");
4984 if (sec
->spec
.exclude_name_list
!= NULL
)
4987 minfo ("EXCLUDE_FILE(%s", sec
->spec
.exclude_name_list
->name
);
4988 for (tmp
= sec
->spec
.exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
4989 minfo (" %s", tmp
->name
);
4992 if (sec
->spec
.name
!= NULL
)
4993 minfo ("%s", sec
->spec
.name
);
4996 for (;closing_paren
> 0; closing_paren
--)
5005 print_statement_list (w
->children
.head
, os
);
5008 /* Print a group statement. */
5011 print_group (lang_group_statement_type
*s
,
5012 lang_output_section_statement_type
*os
)
5014 fprintf (config
.map_file
, "START GROUP\n");
5015 print_statement_list (s
->children
.head
, os
);
5016 fprintf (config
.map_file
, "END GROUP\n");
5019 /* Print the list of statements in S.
5020 This can be called for any statement type. */
5023 print_statement_list (lang_statement_union_type
*s
,
5024 lang_output_section_statement_type
*os
)
5028 print_statement (s
, os
);
5033 /* Print the first statement in statement list S.
5034 This can be called for any statement type. */
5037 print_statement (lang_statement_union_type
*s
,
5038 lang_output_section_statement_type
*os
)
5040 switch (s
->header
.type
)
5043 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
5046 case lang_constructors_statement_enum
:
5047 if (constructor_list
.head
!= NULL
)
5049 if (constructors_sorted
)
5050 minfo (" SORT (CONSTRUCTORS)\n");
5052 minfo (" CONSTRUCTORS\n");
5053 print_statement_list (constructor_list
.head
, os
);
5056 case lang_wild_statement_enum
:
5057 print_wild_statement (&s
->wild_statement
, os
);
5059 case lang_address_statement_enum
:
5060 print_address_statement (&s
->address_statement
);
5062 case lang_object_symbols_statement_enum
:
5063 minfo (" CREATE_OBJECT_SYMBOLS\n");
5065 case lang_fill_statement_enum
:
5066 print_fill_statement (&s
->fill_statement
);
5068 case lang_data_statement_enum
:
5069 print_data_statement (&s
->data_statement
);
5071 case lang_reloc_statement_enum
:
5072 print_reloc_statement (&s
->reloc_statement
);
5074 case lang_input_section_enum
:
5075 print_input_section (s
->input_section
.section
, false);
5077 case lang_padding_statement_enum
:
5078 print_padding_statement (&s
->padding_statement
);
5080 case lang_output_section_statement_enum
:
5081 print_output_section_statement (&s
->output_section_statement
);
5083 case lang_assignment_statement_enum
:
5084 print_assignment (&s
->assignment_statement
, os
);
5086 case lang_target_statement_enum
:
5087 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
5089 case lang_output_statement_enum
:
5090 minfo ("OUTPUT(%s", s
->output_statement
.name
);
5091 if (output_target
!= NULL
)
5092 minfo (" %s", output_target
);
5095 case lang_input_statement_enum
:
5096 print_input_statement (&s
->input_statement
);
5098 case lang_group_statement_enum
:
5099 print_group (&s
->group_statement
, os
);
5101 case lang_insert_statement_enum
:
5102 minfo ("INSERT %s %s\n",
5103 s
->insert_statement
.is_before
? "BEFORE" : "AFTER",
5104 s
->insert_statement
.where
);
5110 print_statements (void)
5112 print_statement_list (statement_list
.head
, abs_output_section
);
5115 /* Print the first N statements in statement list S to STDERR.
5116 If N == 0, nothing is printed.
5117 If N < 0, the entire list is printed.
5118 Intended to be called from GDB. */
5121 dprint_statement (lang_statement_union_type
*s
, int n
)
5123 FILE *map_save
= config
.map_file
;
5125 config
.map_file
= stderr
;
5128 print_statement_list (s
, abs_output_section
);
5131 while (s
&& --n
>= 0)
5133 print_statement (s
, abs_output_section
);
5138 config
.map_file
= map_save
;
5142 insert_pad (lang_statement_union_type
**ptr
,
5144 bfd_size_type alignment_needed
,
5145 asection
*output_section
,
5148 static fill_type zero_fill
;
5149 lang_statement_union_type
*pad
= NULL
;
5151 if (ptr
!= &statement_list
.head
)
5152 pad
= ((lang_statement_union_type
*)
5153 ((char *) ptr
- offsetof (lang_statement_union_type
, header
.next
)));
5155 && pad
->header
.type
== lang_padding_statement_enum
5156 && pad
->padding_statement
.output_section
== output_section
)
5158 /* Use the existing pad statement. */
5160 else if ((pad
= *ptr
) != NULL
5161 && pad
->header
.type
== lang_padding_statement_enum
5162 && pad
->padding_statement
.output_section
== output_section
)
5164 /* Use the existing pad statement. */
5168 /* Make a new padding statement, linked into existing chain. */
5169 pad
= stat_alloc (sizeof (lang_padding_statement_type
));
5170 pad
->header
.next
= *ptr
;
5172 pad
->header
.type
= lang_padding_statement_enum
;
5173 pad
->padding_statement
.output_section
= output_section
;
5176 pad
->padding_statement
.fill
= fill
;
5178 pad
->padding_statement
.output_offset
= dot
- output_section
->vma
;
5179 pad
->padding_statement
.size
= alignment_needed
;
5180 if (!(output_section
->flags
& SEC_FIXED_SIZE
))
5181 output_section
->size
= TO_SIZE (dot
+ TO_ADDR (alignment_needed
)
5182 - output_section
->vma
);
5185 /* Work out how much this section will move the dot point. */
5189 (lang_statement_union_type
**this_ptr
,
5190 lang_output_section_statement_type
*output_section_statement
,
5195 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
5196 asection
*i
= is
->section
;
5197 asection
*o
= output_section_statement
->bfd_section
;
5200 if (link_info
.non_contiguous_regions
)
5202 /* If the input section I has already been successfully assigned
5203 to an output section other than O, don't bother with it and
5204 let the caller remove it from the list. Keep processing in
5205 case we have already handled O, because the repeated passes
5206 have reinitialized its size. */
5207 if (i
->already_assigned
&& i
->already_assigned
!= o
)
5214 if (i
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
5215 i
->output_offset
= i
->vma
- o
->vma
;
5216 else if (((i
->flags
& SEC_EXCLUDE
) != 0)
5217 || output_section_statement
->ignored
)
5218 i
->output_offset
= dot
- o
->vma
;
5221 bfd_size_type alignment_needed
;
5223 /* Align this section first to the input sections requirement,
5224 then to the output section's requirement. If this alignment
5225 is greater than any seen before, then record it too. Perform
5226 the alignment by inserting a magic 'padding' statement. */
5228 if (output_section_statement
->subsection_alignment
!= NULL
)
5230 = exp_get_power (output_section_statement
->subsection_alignment
,
5231 "subsection alignment");
5233 if (o
->alignment_power
< i
->alignment_power
)
5234 o
->alignment_power
= i
->alignment_power
;
5236 alignment_needed
= align_power (dot
, i
->alignment_power
) - dot
;
5238 if (alignment_needed
!= 0)
5240 insert_pad (this_ptr
, fill
, TO_SIZE (alignment_needed
), o
, dot
);
5241 dot
+= alignment_needed
;
5244 if (link_info
.non_contiguous_regions
)
5246 /* If I would overflow O, let the caller remove I from the
5248 if (output_section_statement
->region
)
5250 bfd_vma end
= output_section_statement
->region
->origin
5251 + output_section_statement
->region
->length
;
5253 if (dot
+ TO_ADDR (i
->size
) > end
)
5255 if (i
->flags
& SEC_LINKER_CREATED
)
5256 einfo (_("%F%P: Output section `%pA' not large enough for "
5257 "the linker-created stubs section `%pA'.\n"),
5258 i
->output_section
, i
);
5260 if (i
->rawsize
&& i
->rawsize
!= i
->size
)
5261 einfo (_("%F%P: Relaxation not supported with "
5262 "--enable-non-contiguous-regions (section `%pA' "
5263 "would overflow `%pA' after it changed size).\n"),
5264 i
, i
->output_section
);
5268 i
->output_section
= NULL
;
5274 /* Remember where in the output section this input section goes. */
5275 i
->output_offset
= dot
- o
->vma
;
5277 /* Mark how big the output section must be to contain this now. */
5278 dot
+= TO_ADDR (i
->size
);
5279 if (!(o
->flags
& SEC_FIXED_SIZE
))
5280 o
->size
= TO_SIZE (dot
- o
->vma
);
5282 if (link_info
.non_contiguous_regions
)
5284 /* Record that I was successfully assigned to O, and update
5285 its actual output section too. */
5286 i
->already_assigned
= o
;
5287 i
->output_section
= o
;
5301 sort_sections_by_lma (const void *arg1
, const void *arg2
)
5303 const asection
*sec1
= ((const struct check_sec
*) arg1
)->sec
;
5304 const asection
*sec2
= ((const struct check_sec
*) arg2
)->sec
;
5306 if (sec1
->lma
< sec2
->lma
)
5308 else if (sec1
->lma
> sec2
->lma
)
5310 else if (sec1
->id
< sec2
->id
)
5312 else if (sec1
->id
> sec2
->id
)
5319 sort_sections_by_vma (const void *arg1
, const void *arg2
)
5321 const asection
*sec1
= ((const struct check_sec
*) arg1
)->sec
;
5322 const asection
*sec2
= ((const struct check_sec
*) arg2
)->sec
;
5324 if (sec1
->vma
< sec2
->vma
)
5326 else if (sec1
->vma
> sec2
->vma
)
5328 else if (sec1
->id
< sec2
->id
)
5330 else if (sec1
->id
> sec2
->id
)
5336 #define IS_TBSS(s) \
5337 ((s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == SEC_THREAD_LOCAL)
5339 #define IGNORE_SECTION(s) \
5340 ((s->flags & SEC_ALLOC) == 0 || IS_TBSS (s))
5342 /* Check to see if any allocated sections overlap with other allocated
5343 sections. This can happen if a linker script specifies the output
5344 section addresses of the two sections. Also check whether any memory
5345 region has overflowed. */
5348 lang_check_section_addresses (void)
5351 struct check_sec
*sections
;
5356 bfd_vma p_start
= 0;
5358 lang_memory_region_type
*m
;
5361 /* Detect address space overflow on allocated sections. */
5362 addr_mask
= ((bfd_vma
) 1 <<
5363 (bfd_arch_bits_per_address (link_info
.output_bfd
) - 1)) - 1;
5364 addr_mask
= (addr_mask
<< 1) + 1;
5365 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5366 if ((s
->flags
& SEC_ALLOC
) != 0)
5368 s_end
= (s
->vma
+ s
->size
) & addr_mask
;
5369 if (s_end
!= 0 && s_end
< (s
->vma
& addr_mask
))
5370 einfo (_("%X%P: section %s VMA wraps around address space\n"),
5374 s_end
= (s
->lma
+ s
->size
) & addr_mask
;
5375 if (s_end
!= 0 && s_end
< (s
->lma
& addr_mask
))
5376 einfo (_("%X%P: section %s LMA wraps around address space\n"),
5381 if (bfd_count_sections (link_info
.output_bfd
) <= 1)
5384 count
= bfd_count_sections (link_info
.output_bfd
);
5385 sections
= XNEWVEC (struct check_sec
, count
);
5387 /* Scan all sections in the output list. */
5389 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5391 if (IGNORE_SECTION (s
)
5395 sections
[count
].sec
= s
;
5396 sections
[count
].warned
= false;
5406 qsort (sections
, count
, sizeof (*sections
), sort_sections_by_lma
);
5408 /* First check section LMAs. There should be no overlap of LMAs on
5409 loadable sections, even with overlays. */
5410 for (p
= NULL
, i
= 0; i
< count
; i
++)
5412 s
= sections
[i
].sec
;
5414 if ((s
->flags
& SEC_LOAD
) != 0)
5417 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
5419 /* Look for an overlap. We have sorted sections by lma, so
5420 we know that s_start >= p_start. Besides the obvious
5421 case of overlap when the current section starts before
5422 the previous one ends, we also must have overlap if the
5423 previous section wraps around the address space. */
5425 && (s_start
<= p_end
5426 || p_end
< p_start
))
5428 einfo (_("%X%P: section %s LMA [%V,%V]"
5429 " overlaps section %s LMA [%V,%V]\n"),
5430 s
->name
, s_start
, s_end
, p
->name
, p_start
, p_end
);
5431 sections
[i
].warned
= true;
5439 /* If any non-zero size allocated section (excluding tbss) starts at
5440 exactly the same VMA as another such section, then we have
5441 overlays. Overlays generated by the OVERLAY keyword will have
5442 this property. It is possible to intentionally generate overlays
5443 that fail this test, but it would be unusual. */
5444 qsort (sections
, count
, sizeof (*sections
), sort_sections_by_vma
);
5446 p_start
= sections
[0].sec
->vma
;
5447 for (i
= 1; i
< count
; i
++)
5449 s_start
= sections
[i
].sec
->vma
;
5450 if (p_start
== s_start
)
5458 /* Now check section VMAs if no overlays were detected. */
5461 for (p
= NULL
, i
= 0; i
< count
; i
++)
5463 s
= sections
[i
].sec
;
5466 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
5469 && !sections
[i
].warned
5470 && (s_start
<= p_end
5471 || p_end
< p_start
))
5472 einfo (_("%X%P: section %s VMA [%V,%V]"
5473 " overlaps section %s VMA [%V,%V]\n"),
5474 s
->name
, s_start
, s_end
, p
->name
, p_start
, p_end
);
5483 /* If any memory region has overflowed, report by how much.
5484 We do not issue this diagnostic for regions that had sections
5485 explicitly placed outside their bounds; os_region_check's
5486 diagnostics are adequate for that case.
5488 FIXME: It is conceivable that m->current - (m->origin + m->length)
5489 might overflow a 32-bit integer. There is, alas, no way to print
5490 a bfd_vma quantity in decimal. */
5491 for (m
= lang_memory_region_list
; m
; m
= m
->next
)
5492 if (m
->had_full_message
)
5494 unsigned long over
= m
->current
- (m
->origin
+ m
->length
);
5495 einfo (ngettext ("%X%P: region `%s' overflowed by %lu byte\n",
5496 "%X%P: region `%s' overflowed by %lu bytes\n",
5498 m
->name_list
.name
, over
);
5502 /* Make sure the new address is within the region. We explicitly permit the
5503 current address to be at the exact end of the region when the address is
5504 non-zero, in case the region is at the end of addressable memory and the
5505 calculation wraps around. */
5508 os_region_check (lang_output_section_statement_type
*os
,
5509 lang_memory_region_type
*region
,
5513 if ((region
->current
< region
->origin
5514 || (region
->current
- region
->origin
> region
->length
))
5515 && ((region
->current
!= region
->origin
+ region
->length
)
5520 einfo (_("%X%P: address 0x%v of %pB section `%s'"
5521 " is not within region `%s'\n"),
5523 os
->bfd_section
->owner
,
5524 os
->bfd_section
->name
,
5525 region
->name_list
.name
);
5527 else if (!region
->had_full_message
)
5529 region
->had_full_message
= true;
5531 einfo (_("%X%P: %pB section `%s' will not fit in region `%s'\n"),
5532 os
->bfd_section
->owner
,
5533 os
->bfd_section
->name
,
5534 region
->name_list
.name
);
5540 ldlang_check_relro_region (lang_statement_union_type
*s
)
5542 seg_align_type
*seg
= &expld
.dataseg
;
5544 if (seg
->relro
== exp_seg_relro_start
)
5546 if (!seg
->relro_start_stat
)
5547 seg
->relro_start_stat
= s
;
5550 ASSERT (seg
->relro_start_stat
== s
);
5553 else if (seg
->relro
== exp_seg_relro_end
)
5555 if (!seg
->relro_end_stat
)
5556 seg
->relro_end_stat
= s
;
5559 ASSERT (seg
->relro_end_stat
== s
);
5564 /* Set the sizes for all the output sections. */
5567 lang_size_sections_1
5568 (lang_statement_union_type
**prev
,
5569 lang_output_section_statement_type
*output_section_statement
,
5575 lang_statement_union_type
*s
;
5576 lang_statement_union_type
*prev_s
= NULL
;
5577 bool removed_prev_s
= false;
5579 /* Size up the sections from their constituent parts. */
5580 for (s
= *prev
; s
!= NULL
; prev_s
= s
, s
= s
->header
.next
)
5582 bool removed
= false;
5584 switch (s
->header
.type
)
5586 case lang_output_section_statement_enum
:
5588 bfd_vma newdot
, after
, dotdelta
;
5589 lang_output_section_statement_type
*os
;
5590 lang_memory_region_type
*r
;
5591 int section_alignment
= 0;
5593 os
= &s
->output_section_statement
;
5594 init_opb (os
->bfd_section
);
5595 if (os
->constraint
== -1)
5598 /* FIXME: We shouldn't need to zero section vmas for ld -r
5599 here, in lang_insert_orphan, or in the default linker scripts.
5600 This is covering for coff backend linker bugs. See PR6945. */
5601 if (os
->addr_tree
== NULL
5602 && bfd_link_relocatable (&link_info
)
5603 && (bfd_get_flavour (link_info
.output_bfd
)
5604 == bfd_target_coff_flavour
))
5605 os
->addr_tree
= exp_intop (0);
5606 if (os
->addr_tree
!= NULL
)
5608 exp_fold_tree (os
->addr_tree
, bfd_abs_section_ptr
, &dot
);
5610 if (expld
.result
.valid_p
)
5612 dot
= expld
.result
.value
;
5613 if (expld
.result
.section
!= NULL
)
5614 dot
+= expld
.result
.section
->vma
;
5616 else if (expld
.phase
!= lang_mark_phase_enum
)
5617 einfo (_("%F%P:%pS: non constant or forward reference"
5618 " address expression for section %s\n"),
5619 os
->addr_tree
, os
->name
);
5622 if (os
->bfd_section
== NULL
)
5623 /* This section was removed or never actually created. */
5626 /* If this is a COFF shared library section, use the size and
5627 address from the input section. FIXME: This is COFF
5628 specific; it would be cleaner if there were some other way
5629 to do this, but nothing simple comes to mind. */
5630 if (((bfd_get_flavour (link_info
.output_bfd
)
5631 == bfd_target_ecoff_flavour
)
5632 || (bfd_get_flavour (link_info
.output_bfd
)
5633 == bfd_target_coff_flavour
))
5634 && (os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
5638 if (os
->children
.head
== NULL
5639 || os
->children
.head
->header
.next
!= NULL
5640 || (os
->children
.head
->header
.type
5641 != lang_input_section_enum
))
5642 einfo (_("%X%P: internal error on COFF shared library"
5643 " section %s\n"), os
->name
);
5645 input
= os
->children
.head
->input_section
.section
;
5646 bfd_set_section_vma (os
->bfd_section
,
5647 bfd_section_vma (input
));
5648 if (!(os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
5649 os
->bfd_section
->size
= input
->size
;
5655 if (bfd_is_abs_section (os
->bfd_section
))
5657 /* No matter what happens, an abs section starts at zero. */
5658 ASSERT (os
->bfd_section
->vma
== 0);
5662 if (os
->addr_tree
== NULL
)
5664 /* No address specified for this section, get one
5665 from the region specification. */
5666 if (os
->region
== NULL
5667 || ((os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))
5668 && os
->region
->name_list
.name
[0] == '*'
5669 && strcmp (os
->region
->name_list
.name
,
5670 DEFAULT_MEMORY_REGION
) == 0))
5672 os
->region
= lang_memory_default (os
->bfd_section
);
5675 /* If a loadable section is using the default memory
5676 region, and some non default memory regions were
5677 defined, issue an error message. */
5679 && !IGNORE_SECTION (os
->bfd_section
)
5680 && !bfd_link_relocatable (&link_info
)
5682 && strcmp (os
->region
->name_list
.name
,
5683 DEFAULT_MEMORY_REGION
) == 0
5684 && lang_memory_region_list
!= NULL
5685 && (strcmp (lang_memory_region_list
->name_list
.name
,
5686 DEFAULT_MEMORY_REGION
) != 0
5687 || lang_memory_region_list
->next
!= NULL
)
5688 && lang_sizing_iteration
== 1)
5690 /* By default this is an error rather than just a
5691 warning because if we allocate the section to the
5692 default memory region we can end up creating an
5693 excessively large binary, or even seg faulting when
5694 attempting to perform a negative seek. See
5695 sources.redhat.com/ml/binutils/2003-04/msg00423.html
5696 for an example of this. This behaviour can be
5697 overridden by the using the --no-check-sections
5699 if (command_line
.check_section_addresses
)
5700 einfo (_("%F%P: error: no memory region specified"
5701 " for loadable section `%s'\n"),
5702 bfd_section_name (os
->bfd_section
));
5704 einfo (_("%P: warning: no memory region specified"
5705 " for loadable section `%s'\n"),
5706 bfd_section_name (os
->bfd_section
));
5709 newdot
= os
->region
->current
;
5710 section_alignment
= os
->bfd_section
->alignment_power
;
5713 section_alignment
= exp_get_power (os
->section_alignment
,
5714 "section alignment");
5716 /* Align to what the section needs. */
5717 if (section_alignment
> 0)
5719 bfd_vma savedot
= newdot
;
5722 newdot
= align_power (newdot
, section_alignment
);
5723 dotdelta
= newdot
- savedot
;
5725 if (lang_sizing_iteration
== 1)
5727 else if (lang_sizing_iteration
> 1)
5729 /* Only report adjustments that would change
5730 alignment from what we have already reported. */
5731 diff
= newdot
- os
->bfd_section
->vma
;
5732 if (!(diff
& (((bfd_vma
) 1 << section_alignment
) - 1)))
5736 && (config
.warn_section_align
5737 || os
->addr_tree
!= NULL
))
5738 einfo (_("%P: warning: "
5739 "start of section %s changed by %ld\n"),
5740 os
->name
, (long) diff
);
5743 bfd_set_section_vma (os
->bfd_section
, newdot
);
5745 os
->bfd_section
->output_offset
= 0;
5748 lang_size_sections_1 (&os
->children
.head
, os
,
5749 os
->fill
, newdot
, relax
, check_regions
);
5751 os
->processed_vma
= true;
5753 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
5754 /* Except for some special linker created sections,
5755 no output section should change from zero size
5756 after strip_excluded_output_sections. A non-zero
5757 size on an ignored section indicates that some
5758 input section was not sized early enough. */
5759 ASSERT (os
->bfd_section
->size
== 0);
5762 dot
= os
->bfd_section
->vma
;
5764 /* Put the section within the requested block size, or
5765 align at the block boundary. */
5767 + TO_ADDR (os
->bfd_section
->size
)
5768 + os
->block_value
- 1)
5769 & - (bfd_vma
) os
->block_value
);
5771 if (!(os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
5772 os
->bfd_section
->size
= TO_SIZE (after
5773 - os
->bfd_section
->vma
);
5776 /* Set section lma. */
5779 r
= lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, false);
5783 bfd_vma lma
= exp_get_abs_int (os
->load_base
, 0, "load base");
5784 os
->bfd_section
->lma
= lma
;
5786 else if (os
->lma_region
!= NULL
)
5788 bfd_vma lma
= os
->lma_region
->current
;
5790 if (os
->align_lma_with_input
)
5794 /* When LMA_REGION is the same as REGION, align the LMA
5795 as we did for the VMA, possibly including alignment
5796 from the bfd section. If a different region, then
5797 only align according to the value in the output
5799 if (os
->lma_region
!= os
->region
)
5800 section_alignment
= exp_get_power (os
->section_alignment
,
5801 "section alignment");
5802 if (section_alignment
> 0)
5803 lma
= align_power (lma
, section_alignment
);
5805 os
->bfd_section
->lma
= lma
;
5807 else if (r
->last_os
!= NULL
5808 && (os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
5813 last
= r
->last_os
->output_section_statement
.bfd_section
;
5815 /* A backwards move of dot should be accompanied by
5816 an explicit assignment to the section LMA (ie.
5817 os->load_base set) because backwards moves can
5818 create overlapping LMAs. */
5820 && os
->bfd_section
->size
!= 0
5821 && dot
+ TO_ADDR (os
->bfd_section
->size
) <= last
->vma
)
5823 /* If dot moved backwards then leave lma equal to
5824 vma. This is the old default lma, which might
5825 just happen to work when the backwards move is
5826 sufficiently large. Nag if this changes anything,
5827 so people can fix their linker scripts. */
5829 if (last
->vma
!= last
->lma
)
5830 einfo (_("%P: warning: dot moved backwards "
5831 "before `%s'\n"), os
->name
);
5835 /* If this is an overlay, set the current lma to that
5836 at the end of the previous section. */
5837 if (os
->sectype
== overlay_section
)
5838 lma
= last
->lma
+ TO_ADDR (last
->size
);
5840 /* Otherwise, keep the same lma to vma relationship
5841 as the previous section. */
5843 lma
= os
->bfd_section
->vma
+ last
->lma
- last
->vma
;
5845 if (section_alignment
> 0)
5846 lma
= align_power (lma
, section_alignment
);
5847 os
->bfd_section
->lma
= lma
;
5850 os
->processed_lma
= true;
5852 /* Keep track of normal sections using the default
5853 lma region. We use this to set the lma for
5854 following sections. Overlays or other linker
5855 script assignment to lma might mean that the
5856 default lma == vma is incorrect.
5857 To avoid warnings about dot moving backwards when using
5858 -Ttext, don't start tracking sections until we find one
5859 of non-zero size or with lma set differently to vma.
5860 Do this tracking before we short-cut the loop so that we
5861 track changes for the case where the section size is zero,
5862 but the lma is set differently to the vma. This is
5863 important, if an orphan section is placed after an
5864 otherwise empty output section that has an explicit lma
5865 set, we want that lma reflected in the orphans lma. */
5866 if (((!IGNORE_SECTION (os
->bfd_section
)
5867 && (os
->bfd_section
->size
!= 0
5868 || (r
->last_os
== NULL
5869 && os
->bfd_section
->vma
!= os
->bfd_section
->lma
)
5870 || (r
->last_os
!= NULL
5871 && dot
>= (r
->last_os
->output_section_statement
5872 .bfd_section
->vma
))))
5873 || os
->sectype
== first_overlay_section
)
5874 && os
->lma_region
== NULL
5875 && !bfd_link_relocatable (&link_info
))
5878 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
5881 /* .tbss sections effectively have zero size. */
5882 if (!IS_TBSS (os
->bfd_section
)
5883 || bfd_link_relocatable (&link_info
))
5884 dotdelta
= TO_ADDR (os
->bfd_section
->size
);
5889 if (os
->update_dot_tree
!= 0)
5890 exp_fold_tree (os
->update_dot_tree
, bfd_abs_section_ptr
, &dot
);
5892 /* Update dot in the region ?
5893 We only do this if the section is going to be allocated,
5894 since unallocated sections do not contribute to the region's
5895 overall size in memory. */
5896 if (os
->region
!= NULL
5897 && (os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
)))
5899 os
->region
->current
= dot
;
5902 /* Make sure the new address is within the region. */
5903 os_region_check (os
, os
->region
, os
->addr_tree
,
5904 os
->bfd_section
->vma
);
5906 if (os
->lma_region
!= NULL
&& os
->lma_region
!= os
->region
5907 && ((os
->bfd_section
->flags
& SEC_LOAD
)
5908 || os
->align_lma_with_input
))
5910 os
->lma_region
->current
= os
->bfd_section
->lma
+ dotdelta
;
5913 os_region_check (os
, os
->lma_region
, NULL
,
5914 os
->bfd_section
->lma
);
5920 case lang_constructors_statement_enum
:
5921 dot
= lang_size_sections_1 (&constructor_list
.head
,
5922 output_section_statement
,
5923 fill
, dot
, relax
, check_regions
);
5926 case lang_data_statement_enum
:
5928 unsigned int size
= 0;
5930 s
->data_statement
.output_offset
=
5931 dot
- output_section_statement
->bfd_section
->vma
;
5932 s
->data_statement
.output_section
=
5933 output_section_statement
->bfd_section
;
5935 /* We might refer to provided symbols in the expression, and
5936 need to mark them as needed. */
5937 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
5939 switch (s
->data_statement
.type
)
5957 if (size
< TO_SIZE ((unsigned) 1))
5958 size
= TO_SIZE ((unsigned) 1);
5959 dot
+= TO_ADDR (size
);
5960 if (!(output_section_statement
->bfd_section
->flags
5962 output_section_statement
->bfd_section
->size
5963 = TO_SIZE (dot
- output_section_statement
->bfd_section
->vma
);
5968 case lang_reloc_statement_enum
:
5972 s
->reloc_statement
.output_offset
=
5973 dot
- output_section_statement
->bfd_section
->vma
;
5974 s
->reloc_statement
.output_section
=
5975 output_section_statement
->bfd_section
;
5976 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
5977 dot
+= TO_ADDR (size
);
5978 if (!(output_section_statement
->bfd_section
->flags
5980 output_section_statement
->bfd_section
->size
5981 = TO_SIZE (dot
- output_section_statement
->bfd_section
->vma
);
5985 case lang_wild_statement_enum
:
5986 dot
= lang_size_sections_1 (&s
->wild_statement
.children
.head
,
5987 output_section_statement
,
5988 fill
, dot
, relax
, check_regions
);
5991 case lang_object_symbols_statement_enum
:
5992 link_info
.create_object_symbols_section
5993 = output_section_statement
->bfd_section
;
5994 output_section_statement
->bfd_section
->flags
|= SEC_KEEP
;
5997 case lang_output_statement_enum
:
5998 case lang_target_statement_enum
:
6001 case lang_input_section_enum
:
6005 i
= s
->input_section
.section
;
6010 if (!bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
6011 einfo (_("%F%P: can't relax section: %E\n"));
6015 dot
= size_input_section (prev
, output_section_statement
,
6016 fill
, &removed
, dot
);
6020 case lang_input_statement_enum
:
6023 case lang_fill_statement_enum
:
6024 s
->fill_statement
.output_section
=
6025 output_section_statement
->bfd_section
;
6027 fill
= s
->fill_statement
.fill
;
6030 case lang_assignment_statement_enum
:
6032 bfd_vma newdot
= dot
;
6033 etree_type
*tree
= s
->assignment_statement
.exp
;
6035 expld
.dataseg
.relro
= exp_seg_relro_none
;
6037 exp_fold_tree (tree
,
6038 output_section_statement
->bfd_section
,
6041 ldlang_check_relro_region (s
);
6043 expld
.dataseg
.relro
= exp_seg_relro_none
;
6045 /* This symbol may be relative to this section. */
6046 if ((tree
->type
.node_class
== etree_provided
6047 || tree
->type
.node_class
== etree_assign
)
6048 && (tree
->assign
.dst
[0] != '.'
6049 || tree
->assign
.dst
[1] != '\0'))
6050 output_section_statement
->update_dot
= 1;
6052 if (!output_section_statement
->ignored
)
6054 if (output_section_statement
== abs_output_section
)
6056 /* If we don't have an output section, then just adjust
6057 the default memory address. */
6058 lang_memory_region_lookup (DEFAULT_MEMORY_REGION
,
6059 false)->current
= newdot
;
6061 else if (newdot
!= dot
)
6063 /* Insert a pad after this statement. We can't
6064 put the pad before when relaxing, in case the
6065 assignment references dot. */
6066 insert_pad (&s
->header
.next
, fill
, TO_SIZE (newdot
- dot
),
6067 output_section_statement
->bfd_section
, dot
);
6069 /* Don't neuter the pad below when relaxing. */
6072 /* If dot is advanced, this implies that the section
6073 should have space allocated to it, unless the
6074 user has explicitly stated that the section
6075 should not be allocated. */
6076 if (output_section_statement
->sectype
!= noalloc_section
6077 && (output_section_statement
->sectype
!= noload_section
6078 || (bfd_get_flavour (link_info
.output_bfd
)
6079 == bfd_target_elf_flavour
)))
6080 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
;
6087 case lang_padding_statement_enum
:
6088 /* If this is the first time lang_size_sections is called,
6089 we won't have any padding statements. If this is the
6090 second or later passes when relaxing, we should allow
6091 padding to shrink. If padding is needed on this pass, it
6092 will be added back in. */
6093 s
->padding_statement
.size
= 0;
6095 /* Make sure output_offset is valid. If relaxation shrinks
6096 the section and this pad isn't needed, it's possible to
6097 have output_offset larger than the final size of the
6098 section. bfd_set_section_contents will complain even for
6099 a pad size of zero. */
6100 s
->padding_statement
.output_offset
6101 = dot
- output_section_statement
->bfd_section
->vma
;
6104 case lang_group_statement_enum
:
6105 dot
= lang_size_sections_1 (&s
->group_statement
.children
.head
,
6106 output_section_statement
,
6107 fill
, dot
, relax
, check_regions
);
6110 case lang_insert_statement_enum
:
6113 /* We can only get here when relaxing is turned on. */
6114 case lang_address_statement_enum
:
6122 /* If an input section doesn't fit in the current output
6123 section, remove it from the list. Handle the case where we
6124 have to remove an input_section statement here: there is a
6125 special case to remove the first element of the list. */
6126 if (link_info
.non_contiguous_regions
&& removed
)
6128 /* If we removed the first element during the previous
6129 iteration, override the loop assignment of prev_s. */
6135 /* If there was a real previous input section, just skip
6137 prev_s
->header
.next
=s
->header
.next
;
6139 removed_prev_s
= false;
6143 /* Remove the first input section of the list. */
6144 *prev
= s
->header
.next
;
6145 removed_prev_s
= true;
6148 /* Move to next element, unless we removed the head of the
6150 if (!removed_prev_s
)
6151 prev
= &s
->header
.next
;
6155 prev
= &s
->header
.next
;
6156 removed_prev_s
= false;
6162 /* Callback routine that is used in _bfd_elf_map_sections_to_segments.
6163 The BFD library has set NEW_SEGMENT to TRUE iff it thinks that
6164 CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different
6165 segments. We are allowed an opportunity to override this decision. */
6168 ldlang_override_segment_assignment (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6169 bfd
*abfd ATTRIBUTE_UNUSED
,
6170 asection
*current_section
,
6171 asection
*previous_section
,
6174 lang_output_section_statement_type
*cur
;
6175 lang_output_section_statement_type
*prev
;
6177 /* The checks below are only necessary when the BFD library has decided
6178 that the two sections ought to be placed into the same segment. */
6182 /* Paranoia checks. */
6183 if (current_section
== NULL
|| previous_section
== NULL
)
6186 /* If this flag is set, the target never wants code and non-code
6187 sections comingled in the same segment. */
6188 if (config
.separate_code
6189 && ((current_section
->flags
^ previous_section
->flags
) & SEC_CODE
))
6192 /* Find the memory regions associated with the two sections.
6193 We call lang_output_section_find() here rather than scanning the list
6194 of output sections looking for a matching section pointer because if
6195 we have a large number of sections then a hash lookup is faster. */
6196 cur
= lang_output_section_find (current_section
->name
);
6197 prev
= lang_output_section_find (previous_section
->name
);
6199 /* More paranoia. */
6200 if (cur
== NULL
|| prev
== NULL
)
6203 /* If the regions are different then force the sections to live in
6204 different segments. See the email thread starting at the following
6205 URL for the reasons why this is necessary:
6206 http://sourceware.org/ml/binutils/2007-02/msg00216.html */
6207 return cur
->region
!= prev
->region
;
6211 one_lang_size_sections_pass (bool *relax
, bool check_regions
)
6213 lang_statement_iteration
++;
6214 if (expld
.phase
!= lang_mark_phase_enum
)
6215 lang_sizing_iteration
++;
6216 lang_size_sections_1 (&statement_list
.head
, abs_output_section
,
6217 0, 0, relax
, check_regions
);
6221 lang_size_segment (void)
6223 /* If XXX_SEGMENT_ALIGN XXX_SEGMENT_END pair was seen, check whether
6224 a page could be saved in the data segment. */
6225 seg_align_type
*seg
= &expld
.dataseg
;
6226 bfd_vma first
, last
;
6228 first
= -seg
->base
& (seg
->commonpagesize
- 1);
6229 last
= seg
->end
& (seg
->commonpagesize
- 1);
6231 && ((seg
->base
& ~(seg
->commonpagesize
- 1))
6232 != (seg
->end
& ~(seg
->commonpagesize
- 1)))
6233 && first
+ last
<= seg
->commonpagesize
)
6235 seg
->phase
= exp_seg_adjust
;
6239 seg
->phase
= exp_seg_done
;
6244 lang_size_relro_segment_1 (void)
6246 seg_align_type
*seg
= &expld
.dataseg
;
6247 bfd_vma relro_end
, desired_end
;
6250 /* Compute the expected PT_GNU_RELRO/PT_LOAD segment end. */
6251 relro_end
= (seg
->relro_end
+ seg
->relropagesize
- 1) & -seg
->relropagesize
;
6253 /* Adjust by the offset arg of XXX_SEGMENT_RELRO_END. */
6254 desired_end
= relro_end
- seg
->relro_offset
;
6256 /* For sections in the relro segment.. */
6257 for (sec
= link_info
.output_bfd
->section_last
; sec
; sec
= sec
->prev
)
6258 if ((sec
->flags
& SEC_ALLOC
) != 0
6259 && sec
->vma
>= seg
->base
6260 && sec
->vma
< seg
->relro_end
- seg
->relro_offset
)
6262 /* Where do we want to put this section so that it ends as
6264 bfd_vma start
, end
, bump
;
6266 end
= start
= sec
->vma
;
6268 end
+= TO_ADDR (sec
->size
);
6269 bump
= desired_end
- end
;
6270 /* We'd like to increase START by BUMP, but we must heed
6271 alignment so the increase might be less than optimum. */
6273 start
&= ~(((bfd_vma
) 1 << sec
->alignment_power
) - 1);
6274 /* This is now the desired end for the previous section. */
6275 desired_end
= start
;
6278 seg
->phase
= exp_seg_relro_adjust
;
6279 ASSERT (desired_end
>= seg
->base
);
6280 seg
->base
= desired_end
;
6285 lang_size_relro_segment (bool *relax
, bool check_regions
)
6287 bool do_reset
= false;
6289 if (link_info
.relro
&& expld
.dataseg
.relro_end
)
6291 bfd_vma data_initial_base
= expld
.dataseg
.base
;
6292 bfd_vma data_relro_end
= lang_size_relro_segment_1 ();
6294 lang_reset_memory_regions ();
6295 one_lang_size_sections_pass (relax
, check_regions
);
6297 /* Assignments to dot, or to output section address in a user
6298 script have increased padding over the original. Revert. */
6299 if (expld
.dataseg
.relro_end
> data_relro_end
)
6301 expld
.dataseg
.base
= data_initial_base
;
6305 else if (lang_size_segment ())
6312 lang_size_sections (bool *relax
, bool check_regions
)
6314 expld
.phase
= lang_allocating_phase_enum
;
6315 expld
.dataseg
.phase
= exp_seg_none
;
6317 one_lang_size_sections_pass (relax
, check_regions
);
6319 if (expld
.dataseg
.phase
!= exp_seg_end_seen
)
6320 expld
.dataseg
.phase
= exp_seg_done
;
6322 if (expld
.dataseg
.phase
== exp_seg_end_seen
)
6325 = lang_size_relro_segment (relax
, check_regions
);
6329 lang_reset_memory_regions ();
6330 one_lang_size_sections_pass (relax
, check_regions
);
6333 if (link_info
.relro
&& expld
.dataseg
.relro_end
)
6335 link_info
.relro_start
= expld
.dataseg
.base
;
6336 link_info
.relro_end
= expld
.dataseg
.relro_end
;
6341 static lang_output_section_statement_type
*current_section
;
6342 static lang_assignment_statement_type
*current_assign
;
6343 static bool prefer_next_section
;
6345 /* Worker function for lang_do_assignments. Recursiveness goes here. */
6348 lang_do_assignments_1 (lang_statement_union_type
*s
,
6349 lang_output_section_statement_type
*current_os
,
6354 for (; s
!= NULL
; s
= s
->header
.next
)
6356 switch (s
->header
.type
)
6358 case lang_constructors_statement_enum
:
6359 dot
= lang_do_assignments_1 (constructor_list
.head
,
6360 current_os
, fill
, dot
, found_end
);
6363 case lang_output_section_statement_enum
:
6365 lang_output_section_statement_type
*os
;
6368 os
= &(s
->output_section_statement
);
6369 os
->after_end
= *found_end
;
6370 init_opb (os
->bfd_section
);
6372 if (os
->bfd_section
!= NULL
)
6374 if (!os
->ignored
&& (os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
6376 current_section
= os
;
6377 prefer_next_section
= false;
6379 newdot
= os
->bfd_section
->vma
;
6381 newdot
= lang_do_assignments_1 (os
->children
.head
,
6382 os
, os
->fill
, newdot
, found_end
);
6385 if (os
->bfd_section
!= NULL
)
6387 newdot
= os
->bfd_section
->vma
;
6389 /* .tbss sections effectively have zero size. */
6390 if (!IS_TBSS (os
->bfd_section
)
6391 || bfd_link_relocatable (&link_info
))
6392 newdot
+= TO_ADDR (os
->bfd_section
->size
);
6394 if (os
->update_dot_tree
!= NULL
)
6395 exp_fold_tree (os
->update_dot_tree
,
6396 bfd_abs_section_ptr
, &newdot
);
6403 case lang_wild_statement_enum
:
6405 dot
= lang_do_assignments_1 (s
->wild_statement
.children
.head
,
6406 current_os
, fill
, dot
, found_end
);
6409 case lang_object_symbols_statement_enum
:
6410 case lang_output_statement_enum
:
6411 case lang_target_statement_enum
:
6414 case lang_data_statement_enum
:
6415 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
6416 if (expld
.result
.valid_p
)
6418 s
->data_statement
.value
= expld
.result
.value
;
6419 if (expld
.result
.section
!= NULL
)
6420 s
->data_statement
.value
+= expld
.result
.section
->vma
;
6422 else if (expld
.phase
== lang_final_phase_enum
)
6423 einfo (_("%F%P: invalid data statement\n"));
6426 switch (s
->data_statement
.type
)
6444 if (size
< TO_SIZE ((unsigned) 1))
6445 size
= TO_SIZE ((unsigned) 1);
6446 dot
+= TO_ADDR (size
);
6450 case lang_reloc_statement_enum
:
6451 exp_fold_tree (s
->reloc_statement
.addend_exp
,
6452 bfd_abs_section_ptr
, &dot
);
6453 if (expld
.result
.valid_p
)
6454 s
->reloc_statement
.addend_value
= expld
.result
.value
;
6455 else if (expld
.phase
== lang_final_phase_enum
)
6456 einfo (_("%F%P: invalid reloc statement\n"));
6457 dot
+= TO_ADDR (bfd_get_reloc_size (s
->reloc_statement
.howto
));
6460 case lang_input_section_enum
:
6462 asection
*in
= s
->input_section
.section
;
6464 if ((in
->flags
& SEC_EXCLUDE
) == 0)
6465 dot
+= TO_ADDR (in
->size
);
6469 case lang_input_statement_enum
:
6472 case lang_fill_statement_enum
:
6473 fill
= s
->fill_statement
.fill
;
6476 case lang_assignment_statement_enum
:
6477 current_assign
= &s
->assignment_statement
;
6478 if (current_assign
->exp
->type
.node_class
!= etree_assert
)
6480 const char *p
= current_assign
->exp
->assign
.dst
;
6482 if (current_os
== abs_output_section
&& p
[0] == '.' && p
[1] == 0)
6483 prefer_next_section
= true;
6487 if (strcmp (p
, "end") == 0)
6490 exp_fold_tree (s
->assignment_statement
.exp
,
6491 (current_os
->bfd_section
!= NULL
6492 ? current_os
->bfd_section
: bfd_und_section_ptr
),
6496 case lang_padding_statement_enum
:
6497 dot
+= TO_ADDR (s
->padding_statement
.size
);
6500 case lang_group_statement_enum
:
6501 dot
= lang_do_assignments_1 (s
->group_statement
.children
.head
,
6502 current_os
, fill
, dot
, found_end
);
6505 case lang_insert_statement_enum
:
6508 case lang_address_statement_enum
:
6520 lang_do_assignments (lang_phase_type phase
)
6522 bool found_end
= false;
6524 current_section
= NULL
;
6525 prefer_next_section
= false;
6526 expld
.phase
= phase
;
6527 lang_statement_iteration
++;
6528 lang_do_assignments_1 (statement_list
.head
,
6529 abs_output_section
, NULL
, 0, &found_end
);
6532 /* For an assignment statement outside of an output section statement,
6533 choose the best of neighbouring output sections to use for values
6537 section_for_dot (void)
6541 /* Assignments belong to the previous output section, unless there
6542 has been an assignment to "dot", in which case following
6543 assignments belong to the next output section. (The assumption
6544 is that an assignment to "dot" is setting up the address for the
6545 next output section.) Except that past the assignment to "_end"
6546 we always associate with the previous section. This exception is
6547 for targets like SH that define an alloc .stack or other
6548 weirdness after non-alloc sections. */
6549 if (current_section
== NULL
|| prefer_next_section
)
6551 lang_statement_union_type
*stmt
;
6552 lang_output_section_statement_type
*os
;
6554 for (stmt
= (lang_statement_union_type
*) current_assign
;
6556 stmt
= stmt
->header
.next
)
6557 if (stmt
->header
.type
== lang_output_section_statement_enum
)
6560 os
= stmt
? &stmt
->output_section_statement
: NULL
;
6563 && (os
->bfd_section
== NULL
6564 || (os
->bfd_section
->flags
& SEC_EXCLUDE
) != 0
6565 || bfd_section_removed_from_list (link_info
.output_bfd
,
6569 if (current_section
== NULL
|| os
== NULL
|| !os
->after_end
)
6572 s
= os
->bfd_section
;
6574 s
= link_info
.output_bfd
->section_last
;
6576 && ((s
->flags
& SEC_ALLOC
) == 0
6577 || (s
->flags
& SEC_THREAD_LOCAL
) != 0))
6582 return bfd_abs_section_ptr
;
6586 s
= current_section
->bfd_section
;
6588 /* The section may have been stripped. */
6590 && ((s
->flags
& SEC_EXCLUDE
) != 0
6591 || (s
->flags
& SEC_ALLOC
) == 0
6592 || (s
->flags
& SEC_THREAD_LOCAL
) != 0
6593 || bfd_section_removed_from_list (link_info
.output_bfd
, s
)))
6596 s
= link_info
.output_bfd
->sections
;
6598 && ((s
->flags
& SEC_ALLOC
) == 0
6599 || (s
->flags
& SEC_THREAD_LOCAL
) != 0))
6604 return bfd_abs_section_ptr
;
6607 /* Array of __start/__stop/.startof./.sizeof/ symbols. */
6609 static struct bfd_link_hash_entry
**start_stop_syms
;
6610 static size_t start_stop_count
= 0;
6611 static size_t start_stop_alloc
= 0;
6613 /* Give start/stop SYMBOL for SEC a preliminary definition, and add it
6614 to start_stop_syms. */
6617 lang_define_start_stop (const char *symbol
, asection
*sec
)
6619 struct bfd_link_hash_entry
*h
;
6621 h
= bfd_define_start_stop (link_info
.output_bfd
, &link_info
, symbol
, sec
);
6624 if (start_stop_count
== start_stop_alloc
)
6626 start_stop_alloc
= 2 * start_stop_alloc
+ 10;
6628 = xrealloc (start_stop_syms
,
6629 start_stop_alloc
* sizeof (*start_stop_syms
));
6631 start_stop_syms
[start_stop_count
++] = h
;
6635 /* Check for input sections whose names match references to
6636 __start_SECNAME or __stop_SECNAME symbols. Give the symbols
6637 preliminary definitions. */
6640 lang_init_start_stop (void)
6644 char leading_char
= bfd_get_symbol_leading_char (link_info
.output_bfd
);
6646 for (abfd
= link_info
.input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
6647 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
6650 const char *secname
= s
->name
;
6652 for (ps
= secname
; *ps
!= '\0'; ps
++)
6653 if (!ISALNUM ((unsigned char) *ps
) && *ps
!= '_')
6657 char *symbol
= (char *) xmalloc (10 + strlen (secname
));
6659 symbol
[0] = leading_char
;
6660 sprintf (symbol
+ (leading_char
!= 0), "__start_%s", secname
);
6661 lang_define_start_stop (symbol
, s
);
6663 symbol
[1] = leading_char
;
6664 memcpy (symbol
+ 1 + (leading_char
!= 0), "__stop", 6);
6665 lang_define_start_stop (symbol
+ 1, s
);
6672 /* Iterate over start_stop_syms. */
6675 foreach_start_stop (void (*func
) (struct bfd_link_hash_entry
*))
6679 for (i
= 0; i
< start_stop_count
; ++i
)
6680 func (start_stop_syms
[i
]);
6683 /* __start and __stop symbols are only supposed to be defined by the
6684 linker for orphan sections, but we now extend that to sections that
6685 map to an output section of the same name. The symbols were
6686 defined early for --gc-sections, before we mapped input to output
6687 sections, so undo those that don't satisfy this rule. */
6690 undef_start_stop (struct bfd_link_hash_entry
*h
)
6692 if (h
->ldscript_def
)
6695 if (h
->u
.def
.section
->output_section
== NULL
6696 || h
->u
.def
.section
->output_section
->owner
!= link_info
.output_bfd
6697 || strcmp (h
->u
.def
.section
->name
,
6698 h
->u
.def
.section
->output_section
->name
) != 0)
6700 asection
*sec
= bfd_get_section_by_name (link_info
.output_bfd
,
6701 h
->u
.def
.section
->name
);
6704 /* When there are more than one input sections with the same
6705 section name, SECNAME, linker picks the first one to define
6706 __start_SECNAME and __stop_SECNAME symbols. When the first
6707 input section is removed by comdat group, we need to check
6708 if there is still an output section with section name
6711 for (i
= sec
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
6712 if (strcmp (h
->u
.def
.section
->name
, i
->name
) == 0)
6714 h
->u
.def
.section
= i
;
6718 h
->type
= bfd_link_hash_undefined
;
6719 h
->u
.undef
.abfd
= NULL
;
6720 if (is_elf_hash_table (link_info
.hash
))
6722 const struct elf_backend_data
*bed
;
6723 struct elf_link_hash_entry
*eh
= (struct elf_link_hash_entry
*) h
;
6724 unsigned int was_forced
= eh
->forced_local
;
6726 bed
= get_elf_backend_data (link_info
.output_bfd
);
6727 (*bed
->elf_backend_hide_symbol
) (&link_info
, eh
, true);
6728 if (!eh
->ref_regular_nonweak
)
6729 h
->type
= bfd_link_hash_undefweak
;
6730 eh
->def_regular
= 0;
6731 eh
->forced_local
= was_forced
;
6737 lang_undef_start_stop (void)
6739 foreach_start_stop (undef_start_stop
);
6742 /* Check for output sections whose names match references to
6743 .startof.SECNAME or .sizeof.SECNAME symbols. Give the symbols
6744 preliminary definitions. */
6747 lang_init_startof_sizeof (void)
6751 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6753 const char *secname
= s
->name
;
6754 char *symbol
= (char *) xmalloc (10 + strlen (secname
));
6756 sprintf (symbol
, ".startof.%s", secname
);
6757 lang_define_start_stop (symbol
, s
);
6759 memcpy (symbol
+ 1, ".size", 5);
6760 lang_define_start_stop (symbol
+ 1, s
);
6765 /* Set .startof., .sizeof., __start and __stop symbols final values. */
6768 set_start_stop (struct bfd_link_hash_entry
*h
)
6771 || h
->type
!= bfd_link_hash_defined
)
6774 if (h
->root
.string
[0] == '.')
6776 /* .startof. or .sizeof. symbol.
6777 .startof. already has final value. */
6778 if (h
->root
.string
[2] == 'i')
6781 h
->u
.def
.value
= TO_ADDR (h
->u
.def
.section
->size
);
6782 h
->u
.def
.section
= bfd_abs_section_ptr
;
6787 /* __start or __stop symbol. */
6788 int has_lead
= bfd_get_symbol_leading_char (link_info
.output_bfd
) != 0;
6790 h
->u
.def
.section
= h
->u
.def
.section
->output_section
;
6791 if (h
->root
.string
[4 + has_lead
] == 'o')
6794 h
->u
.def
.value
= TO_ADDR (h
->u
.def
.section
->size
);
6800 lang_finalize_start_stop (void)
6802 foreach_start_stop (set_start_stop
);
6806 lang_symbol_tweaks (void)
6808 /* Give initial values for __start and __stop symbols, so that ELF
6809 gc_sections will keep sections referenced by these symbols. Must
6810 be done before lang_do_assignments. */
6811 if (config
.build_constructors
)
6812 lang_init_start_stop ();
6814 /* Make __ehdr_start hidden, and set def_regular even though it is
6815 likely undefined at this stage. For lang_check_relocs. */
6816 if (is_elf_hash_table (link_info
.hash
)
6817 && !bfd_link_relocatable (&link_info
))
6819 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*)
6820 bfd_link_hash_lookup (link_info
.hash
, "__ehdr_start",
6821 false, false, true);
6823 /* Only adjust the export class if the symbol was referenced
6824 and not defined, otherwise leave it alone. */
6826 && (h
->root
.type
== bfd_link_hash_new
6827 || h
->root
.type
== bfd_link_hash_undefined
6828 || h
->root
.type
== bfd_link_hash_undefweak
6829 || h
->root
.type
== bfd_link_hash_common
))
6831 const struct elf_backend_data
*bed
;
6832 bed
= get_elf_backend_data (link_info
.output_bfd
);
6833 (*bed
->elf_backend_hide_symbol
) (&link_info
, h
, true);
6834 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
6835 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
6837 h
->root
.linker_def
= 1;
6838 h
->root
.rel_from_abs
= 1;
6846 struct bfd_link_hash_entry
*h
;
6849 if ((bfd_link_relocatable (&link_info
) && !link_info
.gc_sections
)
6850 || bfd_link_dll (&link_info
))
6851 warn
= entry_from_cmdline
;
6855 /* Force the user to specify a root when generating a relocatable with
6856 --gc-sections, unless --gc-keep-exported was also given. */
6857 if (bfd_link_relocatable (&link_info
)
6858 && link_info
.gc_sections
6859 && !link_info
.gc_keep_exported
)
6861 struct bfd_sym_chain
*sym
;
6863 for (sym
= link_info
.gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
6865 h
= bfd_link_hash_lookup (link_info
.hash
, sym
->name
,
6866 false, false, false);
6868 && (h
->type
== bfd_link_hash_defined
6869 || h
->type
== bfd_link_hash_defweak
)
6870 && !bfd_is_const_section (h
->u
.def
.section
))
6874 einfo (_("%F%P: --gc-sections requires a defined symbol root "
6875 "specified by -e or -u\n"));
6878 if (entry_symbol
.name
== NULL
)
6880 /* No entry has been specified. Look for the default entry, but
6881 don't warn if we don't find it. */
6882 entry_symbol
.name
= entry_symbol_default
;
6886 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
.name
,
6887 false, false, true);
6889 && (h
->type
== bfd_link_hash_defined
6890 || h
->type
== bfd_link_hash_defweak
)
6891 && h
->u
.def
.section
->output_section
!= NULL
)
6895 val
= (h
->u
.def
.value
6896 + bfd_section_vma (h
->u
.def
.section
->output_section
)
6897 + h
->u
.def
.section
->output_offset
);
6898 if (!bfd_set_start_address (link_info
.output_bfd
, val
))
6899 einfo (_("%F%P: %s: can't set start address\n"), entry_symbol
.name
);
6906 /* We couldn't find the entry symbol. Try parsing it as a
6908 val
= bfd_scan_vma (entry_symbol
.name
, &send
, 0);
6911 if (!bfd_set_start_address (link_info
.output_bfd
, val
))
6912 einfo (_("%F%P: can't set start address\n"));
6914 /* BZ 2004952: Only use the start of the entry section for executables. */
6915 else if bfd_link_executable (&link_info
)
6919 /* Can't find the entry symbol, and it's not a number. Use
6920 the first address in the text section. */
6921 ts
= bfd_get_section_by_name (link_info
.output_bfd
, entry_section
);
6925 einfo (_("%P: warning: cannot find entry symbol %s;"
6926 " defaulting to %V\n"),
6928 bfd_section_vma (ts
));
6929 if (!bfd_set_start_address (link_info
.output_bfd
,
6930 bfd_section_vma (ts
)))
6931 einfo (_("%F%P: can't set start address\n"));
6936 einfo (_("%P: warning: cannot find entry symbol %s;"
6937 " not setting start address\n"),
6944 einfo (_("%P: warning: cannot find entry symbol %s;"
6945 " not setting start address\n"),
6951 /* This is a small function used when we want to ignore errors from
6955 ignore_bfd_errors (const char *fmt ATTRIBUTE_UNUSED
,
6956 va_list ap ATTRIBUTE_UNUSED
)
6958 /* Don't do anything. */
6961 /* Check that the architecture of all the input files is compatible
6962 with the output file. Also call the backend to let it do any
6963 other checking that is needed. */
6968 lang_input_statement_type
*file
;
6970 const bfd_arch_info_type
*compatible
;
6972 for (file
= (void *) file_chain
.head
;
6976 #if BFD_SUPPORTS_PLUGINS
6977 /* Don't check format of files claimed by plugin. */
6978 if (file
->flags
.claimed
)
6980 #endif /* BFD_SUPPORTS_PLUGINS */
6981 input_bfd
= file
->the_bfd
;
6983 = bfd_arch_get_compatible (input_bfd
, link_info
.output_bfd
,
6984 command_line
.accept_unknown_input_arch
);
6986 /* In general it is not possible to perform a relocatable
6987 link between differing object formats when the input
6988 file has relocations, because the relocations in the
6989 input format may not have equivalent representations in
6990 the output format (and besides BFD does not translate
6991 relocs for other link purposes than a final link). */
6992 if (!file
->flags
.just_syms
6993 && (bfd_link_relocatable (&link_info
)
6994 || link_info
.emitrelocations
)
6995 && (compatible
== NULL
6996 || (bfd_get_flavour (input_bfd
)
6997 != bfd_get_flavour (link_info
.output_bfd
)))
6998 && (bfd_get_file_flags (input_bfd
) & HAS_RELOC
) != 0)
7000 einfo (_("%F%P: relocatable linking with relocations from"
7001 " format %s (%pB) to format %s (%pB) is not supported\n"),
7002 bfd_get_target (input_bfd
), input_bfd
,
7003 bfd_get_target (link_info
.output_bfd
), link_info
.output_bfd
);
7004 /* einfo with %F exits. */
7007 if (compatible
== NULL
)
7009 if (command_line
.warn_mismatch
)
7010 einfo (_("%X%P: %s architecture of input file `%pB'"
7011 " is incompatible with %s output\n"),
7012 bfd_printable_name (input_bfd
), input_bfd
,
7013 bfd_printable_name (link_info
.output_bfd
));
7016 /* If the input bfd has no contents, it shouldn't set the
7017 private data of the output bfd. */
7018 else if (!file
->flags
.just_syms
7019 && ((input_bfd
->flags
& DYNAMIC
) != 0
7020 || bfd_count_sections (input_bfd
) != 0))
7022 bfd_error_handler_type pfn
= NULL
;
7024 /* If we aren't supposed to warn about mismatched input
7025 files, temporarily set the BFD error handler to a
7026 function which will do nothing. We still want to call
7027 bfd_merge_private_bfd_data, since it may set up
7028 information which is needed in the output file. */
7029 if (!command_line
.warn_mismatch
)
7030 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
7031 if (!bfd_merge_private_bfd_data (input_bfd
, &link_info
))
7033 if (command_line
.warn_mismatch
)
7034 einfo (_("%X%P: failed to merge target specific data"
7035 " of file %pB\n"), input_bfd
);
7037 if (!command_line
.warn_mismatch
)
7038 bfd_set_error_handler (pfn
);
7043 /* Look through all the global common symbols and attach them to the
7044 correct section. The -sort-common command line switch may be used
7045 to roughly sort the entries by alignment. */
7050 if (link_info
.inhibit_common_definition
)
7052 if (bfd_link_relocatable (&link_info
)
7053 && !command_line
.force_common_definition
)
7056 if (!config
.sort_common
)
7057 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, NULL
);
7062 if (config
.sort_common
== sort_descending
)
7064 for (power
= 4; power
> 0; power
--)
7065 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7068 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7072 for (power
= 0; power
<= 4; power
++)
7073 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7075 power
= (unsigned int) -1;
7076 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7081 /* Place one common symbol in the correct section. */
7084 lang_one_common (struct bfd_link_hash_entry
*h
, void *info
)
7086 unsigned int power_of_two
;
7090 if (h
->type
!= bfd_link_hash_common
)
7094 power_of_two
= h
->u
.c
.p
->alignment_power
;
7096 if (config
.sort_common
== sort_descending
7097 && power_of_two
< *(unsigned int *) info
)
7099 else if (config
.sort_common
== sort_ascending
7100 && power_of_two
> *(unsigned int *) info
)
7103 section
= h
->u
.c
.p
->section
;
7104 if (!bfd_define_common_symbol (link_info
.output_bfd
, &link_info
, h
))
7105 einfo (_("%F%P: could not define common symbol `%pT': %E\n"),
7108 if (config
.map_file
!= NULL
)
7110 static bool header_printed
;
7115 if (!header_printed
)
7117 minfo (_("\nAllocating common symbols\n"));
7118 minfo (_("Common symbol size file\n\n"));
7119 header_printed
= true;
7122 name
= bfd_demangle (link_info
.output_bfd
, h
->root
.string
,
7123 DMGL_ANSI
| DMGL_PARAMS
);
7126 minfo ("%s", h
->root
.string
);
7127 len
= strlen (h
->root
.string
);
7132 len
= strlen (name
);
7142 sprintf (buf
, "%" PRIx64
, (uint64_t) size
);
7143 fprintf (config
.map_file
, "%*s0x%-16s", 20 - len
, "", buf
);
7145 minfo ("%pB\n", section
->owner
);
7151 /* Handle a single orphan section S, placing the orphan into an appropriate
7152 output section. The effects of the --orphan-handling command line
7153 option are handled here. */
7156 ldlang_place_orphan (asection
*s
)
7158 if (config
.orphan_handling
== orphan_handling_discard
)
7160 lang_output_section_statement_type
*os
;
7161 os
= lang_output_section_statement_lookup (DISCARD_SECTION_NAME
, 0, 1);
7162 if (os
->addr_tree
== NULL
7163 && (bfd_link_relocatable (&link_info
)
7164 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0))
7165 os
->addr_tree
= exp_intop (0);
7166 lang_add_section (&os
->children
, s
, NULL
, NULL
, os
);
7170 lang_output_section_statement_type
*os
;
7171 const char *name
= s
->name
;
7174 if (config
.orphan_handling
== orphan_handling_error
)
7175 einfo (_("%X%P: error: unplaced orphan section `%pA' from `%pB'\n"),
7178 if (config
.unique_orphan_sections
|| unique_section_p (s
, NULL
))
7179 constraint
= SPECIAL
;
7181 os
= ldemul_place_orphan (s
, name
, constraint
);
7184 os
= lang_output_section_statement_lookup (name
, constraint
, 1);
7185 if (os
->addr_tree
== NULL
7186 && (bfd_link_relocatable (&link_info
)
7187 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0))
7188 os
->addr_tree
= exp_intop (0);
7189 lang_add_section (&os
->children
, s
, NULL
, NULL
, os
);
7192 if (config
.orphan_handling
== orphan_handling_warn
)
7193 einfo (_("%P: warning: orphan section `%pA' from `%pB' being "
7194 "placed in section `%s'\n"),
7195 s
, s
->owner
, os
->name
);
7199 /* Run through the input files and ensure that every input section has
7200 somewhere to go. If one is found without a destination then create
7201 an input request and place it into the statement tree. */
7204 lang_place_orphans (void)
7206 LANG_FOR_EACH_INPUT_STATEMENT (file
)
7210 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7212 if (s
->output_section
== NULL
)
7214 /* This section of the file is not attached, root
7215 around for a sensible place for it to go. */
7217 if (file
->flags
.just_syms
)
7218 bfd_link_just_syms (file
->the_bfd
, s
, &link_info
);
7219 else if (lang_discard_section_p (s
))
7220 s
->output_section
= bfd_abs_section_ptr
;
7221 else if (strcmp (s
->name
, "COMMON") == 0)
7223 /* This is a lonely common section which must have
7224 come from an archive. We attach to the section
7225 with the wildcard. */
7226 if (!bfd_link_relocatable (&link_info
)
7227 || command_line
.force_common_definition
)
7229 if (default_common_section
== NULL
)
7230 default_common_section
7231 = lang_output_section_statement_lookup (".bss", 0, 1);
7232 lang_add_section (&default_common_section
->children
, s
,
7233 NULL
, NULL
, default_common_section
);
7237 ldlang_place_orphan (s
);
7244 lang_set_flags (lang_memory_region_type
*ptr
, const char *flags
, int invert
)
7246 flagword
*ptr_flags
;
7248 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
7254 /* PR 17900: An exclamation mark in the attributes reverses
7255 the sense of any of the attributes that follow. */
7258 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
7262 *ptr_flags
|= SEC_ALLOC
;
7266 *ptr_flags
|= SEC_READONLY
;
7270 *ptr_flags
|= SEC_DATA
;
7274 *ptr_flags
|= SEC_CODE
;
7279 *ptr_flags
|= SEC_LOAD
;
7283 einfo (_("%F%P: invalid character %c (%d) in flags\n"),
7291 /* Call a function on each real input file. This function will be
7292 called on an archive, but not on the elements. */
7295 lang_for_each_input_file (void (*func
) (lang_input_statement_type
*))
7297 lang_input_statement_type
*f
;
7299 for (f
= (void *) input_file_chain
.head
;
7301 f
= f
->next_real_file
)
7306 /* Call a function on each real file. The function will be called on
7307 all the elements of an archive which are included in the link, but
7308 will not be called on the archive file itself. */
7311 lang_for_each_file (void (*func
) (lang_input_statement_type
*))
7313 LANG_FOR_EACH_INPUT_STATEMENT (f
)
7321 ldlang_add_file (lang_input_statement_type
*entry
)
7323 lang_statement_append (&file_chain
, entry
, &entry
->next
);
7325 /* The BFD linker needs to have a list of all input BFDs involved in
7327 ASSERT (link_info
.input_bfds_tail
!= &entry
->the_bfd
->link
.next
7328 && entry
->the_bfd
->link
.next
== NULL
);
7329 ASSERT (entry
->the_bfd
!= link_info
.output_bfd
);
7331 *link_info
.input_bfds_tail
= entry
->the_bfd
;
7332 link_info
.input_bfds_tail
= &entry
->the_bfd
->link
.next
;
7333 bfd_set_usrdata (entry
->the_bfd
, entry
);
7334 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
7336 /* Look through the sections and check for any which should not be
7337 included in the link. We need to do this now, so that we can
7338 notice when the backend linker tries to report multiple
7339 definition errors for symbols which are in sections we aren't
7340 going to link. FIXME: It might be better to entirely ignore
7341 symbols which are defined in sections which are going to be
7342 discarded. This would require modifying the backend linker for
7343 each backend which might set the SEC_LINK_ONCE flag. If we do
7344 this, we should probably handle SEC_EXCLUDE in the same way. */
7346 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, entry
);
7350 lang_add_output (const char *name
, int from_script
)
7352 /* Make -o on command line override OUTPUT in script. */
7353 if (!had_output_filename
|| !from_script
)
7355 output_filename
= name
;
7356 had_output_filename
= true;
7360 lang_output_section_statement_type
*
7361 lang_enter_output_section_statement (const char *output_section_statement_name
,
7362 etree_type
*address_exp
,
7363 enum section_type sectype
,
7364 etree_type
*sectype_value
,
7366 etree_type
*subalign
,
7369 int align_with_input
)
7371 lang_output_section_statement_type
*os
;
7373 os
= lang_output_section_statement_lookup (output_section_statement_name
,
7375 current_section
= os
;
7377 if (os
->addr_tree
== NULL
)
7379 os
->addr_tree
= address_exp
;
7381 os
->sectype
= sectype
;
7382 if (sectype
== type_section
|| sectype
== typed_readonly_section
)
7383 os
->sectype_value
= sectype_value
;
7384 else if (sectype
== noload_section
)
7385 os
->flags
= SEC_NEVER_LOAD
;
7387 os
->flags
= SEC_NO_FLAGS
;
7388 os
->block_value
= 1;
7390 /* Make next things chain into subchain of this. */
7391 push_stat_ptr (&os
->children
);
7393 os
->align_lma_with_input
= align_with_input
== ALIGN_WITH_INPUT
;
7394 if (os
->align_lma_with_input
&& align
!= NULL
)
7395 einfo (_("%F%P:%pS: error: align with input and explicit align specified\n"),
7398 os
->subsection_alignment
= subalign
;
7399 os
->section_alignment
= align
;
7401 os
->load_base
= ebase
;
7408 lang_output_statement_type
*new_stmt
;
7410 new_stmt
= new_stat (lang_output_statement
, stat_ptr
);
7411 new_stmt
->name
= output_filename
;
7414 /* Reset the current counters in the regions. */
7417 lang_reset_memory_regions (void)
7419 lang_memory_region_type
*p
= lang_memory_region_list
;
7421 lang_output_section_statement_type
*os
;
7423 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
7425 p
->current
= p
->origin
;
7429 for (os
= (void *) lang_os_list
.head
;
7433 os
->processed_vma
= false;
7434 os
->processed_lma
= false;
7437 for (o
= link_info
.output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
7439 /* Save the last size for possible use by bfd_relax_section. */
7440 o
->rawsize
= o
->size
;
7441 if (!(o
->flags
& SEC_FIXED_SIZE
))
7446 /* Worker for lang_gc_sections_1. */
7449 gc_section_callback (lang_wild_statement_type
*ptr
,
7450 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
7452 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
7453 void *data ATTRIBUTE_UNUSED
)
7455 /* If the wild pattern was marked KEEP, the member sections
7456 should be as well. */
7457 if (ptr
->keep_sections
)
7458 section
->flags
|= SEC_KEEP
;
7461 /* Iterate over sections marking them against GC. */
7464 lang_gc_sections_1 (lang_statement_union_type
*s
)
7466 for (; s
!= NULL
; s
= s
->header
.next
)
7468 switch (s
->header
.type
)
7470 case lang_wild_statement_enum
:
7471 walk_wild (&s
->wild_statement
, gc_section_callback
, NULL
);
7473 case lang_constructors_statement_enum
:
7474 lang_gc_sections_1 (constructor_list
.head
);
7476 case lang_output_section_statement_enum
:
7477 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
7479 case lang_group_statement_enum
:
7480 lang_gc_sections_1 (s
->group_statement
.children
.head
);
7489 lang_gc_sections (void)
7491 /* Keep all sections so marked in the link script. */
7492 lang_gc_sections_1 (statement_list
.head
);
7494 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
7495 the special case of .stabstr debug info. (See bfd/stabs.c)
7496 Twiddle the flag here, to simplify later linker code. */
7497 if (bfd_link_relocatable (&link_info
))
7499 LANG_FOR_EACH_INPUT_STATEMENT (f
)
7502 #if BFD_SUPPORTS_PLUGINS
7503 if (f
->flags
.claimed
)
7506 for (sec
= f
->the_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7507 if ((sec
->flags
& SEC_DEBUGGING
) == 0
7508 || strcmp (sec
->name
, ".stabstr") != 0)
7509 sec
->flags
&= ~SEC_EXCLUDE
;
7513 if (link_info
.gc_sections
)
7514 bfd_gc_sections (link_info
.output_bfd
, &link_info
);
7517 /* Worker for lang_find_relro_sections_1. */
7520 find_relro_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
7521 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
7523 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
7526 /* Discarded, excluded and ignored sections effectively have zero
7528 if (section
->output_section
!= NULL
7529 && section
->output_section
->owner
== link_info
.output_bfd
7530 && (section
->output_section
->flags
& SEC_EXCLUDE
) == 0
7531 && !IGNORE_SECTION (section
)
7532 && section
->size
!= 0)
7534 bool *has_relro_section
= (bool *) data
;
7535 *has_relro_section
= true;
7539 /* Iterate over sections for relro sections. */
7542 lang_find_relro_sections_1 (lang_statement_union_type
*s
,
7543 bool *has_relro_section
)
7545 if (*has_relro_section
)
7548 for (; s
!= NULL
; s
= s
->header
.next
)
7550 if (s
== expld
.dataseg
.relro_end_stat
)
7553 switch (s
->header
.type
)
7555 case lang_wild_statement_enum
:
7556 walk_wild (&s
->wild_statement
,
7557 find_relro_section_callback
,
7560 case lang_constructors_statement_enum
:
7561 lang_find_relro_sections_1 (constructor_list
.head
,
7564 case lang_output_section_statement_enum
:
7565 lang_find_relro_sections_1 (s
->output_section_statement
.children
.head
,
7568 case lang_group_statement_enum
:
7569 lang_find_relro_sections_1 (s
->group_statement
.children
.head
,
7579 lang_find_relro_sections (void)
7581 bool has_relro_section
= false;
7583 /* Check all sections in the link script. */
7585 lang_find_relro_sections_1 (expld
.dataseg
.relro_start_stat
,
7586 &has_relro_section
);
7588 if (!has_relro_section
)
7589 link_info
.relro
= false;
7592 /* Relax all sections until bfd_relax_section gives up. */
7595 lang_relax_sections (bool need_layout
)
7597 /* NB: Also enable relaxation to layout sections for DT_RELR. */
7598 if (RELAXATION_ENABLED
|| link_info
.enable_dt_relr
)
7600 /* We may need more than one relaxation pass. */
7601 int i
= link_info
.relax_pass
;
7603 /* The backend can use it to determine the current pass. */
7604 link_info
.relax_pass
= 0;
7608 /* Keep relaxing until bfd_relax_section gives up. */
7611 link_info
.relax_trip
= -1;
7614 link_info
.relax_trip
++;
7616 /* Note: pe-dll.c does something like this also. If you find
7617 you need to change this code, you probably need to change
7618 pe-dll.c also. DJ */
7620 /* Do all the assignments with our current guesses as to
7622 lang_do_assignments (lang_assigning_phase_enum
);
7624 /* We must do this after lang_do_assignments, because it uses
7626 lang_reset_memory_regions ();
7628 /* Perform another relax pass - this time we know where the
7629 globals are, so can make a better guess. */
7630 relax_again
= false;
7631 lang_size_sections (&relax_again
, false);
7633 while (relax_again
);
7635 link_info
.relax_pass
++;
7642 /* Final extra sizing to report errors. */
7643 lang_do_assignments (lang_assigning_phase_enum
);
7644 lang_reset_memory_regions ();
7645 lang_size_sections (NULL
, true);
7649 #if BFD_SUPPORTS_PLUGINS
7650 /* Find the insert point for the plugin's replacement files. We
7651 place them after the first claimed real object file, or if the
7652 first claimed object is an archive member, after the last real
7653 object file immediately preceding the archive. In the event
7654 no objects have been claimed at all, we return the first dummy
7655 object file on the list as the insert point; that works, but
7656 the callee must be careful when relinking the file_chain as it
7657 is not actually on that chain, only the statement_list and the
7658 input_file list; in that case, the replacement files must be
7659 inserted at the head of the file_chain. */
7661 static lang_input_statement_type
*
7662 find_replacements_insert_point (bool *before
)
7664 lang_input_statement_type
*claim1
, *lastobject
;
7665 lastobject
= (void *) input_file_chain
.head
;
7666 for (claim1
= (void *) file_chain
.head
;
7668 claim1
= claim1
->next
)
7670 if (claim1
->flags
.claimed
)
7672 *before
= claim1
->flags
.claim_archive
;
7673 return claim1
->flags
.claim_archive
? lastobject
: claim1
;
7675 /* Update lastobject if this is a real object file. */
7676 if (claim1
->the_bfd
!= NULL
&& claim1
->the_bfd
->my_archive
== NULL
)
7677 lastobject
= claim1
;
7679 /* No files were claimed by the plugin. Choose the last object
7680 file found on the list (maybe the first, dummy entry) as the
7686 /* Find where to insert ADD, an archive element or shared library
7687 added during a rescan. */
7689 static lang_input_statement_type
**
7690 find_rescan_insertion (lang_input_statement_type
*add
)
7692 bfd
*add_bfd
= add
->the_bfd
;
7693 lang_input_statement_type
*f
;
7694 lang_input_statement_type
*last_loaded
= NULL
;
7695 lang_input_statement_type
*before
= NULL
;
7696 lang_input_statement_type
**iter
= NULL
;
7698 if (add_bfd
->my_archive
!= NULL
)
7699 add_bfd
= add_bfd
->my_archive
;
7701 /* First look through the input file chain, to find an object file
7702 before the one we've rescanned. Normal object files always
7703 appear on both the input file chain and the file chain, so this
7704 lets us get quickly to somewhere near the correct place on the
7705 file chain if it is full of archive elements. Archives don't
7706 appear on the file chain, but if an element has been extracted
7707 then their input_statement->next points at it. */
7708 for (f
= (void *) input_file_chain
.head
;
7710 f
= f
->next_real_file
)
7712 if (f
->the_bfd
== add_bfd
)
7714 before
= last_loaded
;
7715 if (f
->next
!= NULL
)
7716 return &f
->next
->next
;
7718 if (f
->the_bfd
!= NULL
&& f
->next
!= NULL
)
7722 for (iter
= before
? &before
->next
: &file_chain
.head
->input_statement
.next
;
7724 iter
= &(*iter
)->next
)
7725 if (!(*iter
)->flags
.claim_archive
7726 && (*iter
)->the_bfd
->my_archive
== NULL
)
7732 /* Insert SRCLIST into DESTLIST after given element by chaining
7733 on FIELD as the next-pointer. (Counterintuitively does not need
7734 a pointer to the actual after-node itself, just its chain field.) */
7737 lang_list_insert_after (lang_statement_list_type
*destlist
,
7738 lang_statement_list_type
*srclist
,
7739 lang_statement_union_type
**field
)
7741 *(srclist
->tail
) = *field
;
7742 *field
= srclist
->head
;
7743 if (destlist
->tail
== field
)
7744 destlist
->tail
= srclist
->tail
;
7747 /* Detach new nodes added to DESTLIST since the time ORIGLIST
7748 was taken as a copy of it and leave them in ORIGLIST. */
7751 lang_list_remove_tail (lang_statement_list_type
*destlist
,
7752 lang_statement_list_type
*origlist
)
7754 union lang_statement_union
**savetail
;
7755 /* Check that ORIGLIST really is an earlier state of DESTLIST. */
7756 ASSERT (origlist
->head
== destlist
->head
);
7757 savetail
= origlist
->tail
;
7758 origlist
->head
= *(savetail
);
7759 origlist
->tail
= destlist
->tail
;
7760 destlist
->tail
= savetail
;
7764 static lang_statement_union_type
**
7765 find_next_input_statement (lang_statement_union_type
**s
)
7767 for ( ; *s
; s
= &(*s
)->header
.next
)
7769 lang_statement_union_type
**t
;
7770 switch ((*s
)->header
.type
)
7772 case lang_input_statement_enum
:
7774 case lang_wild_statement_enum
:
7775 t
= &(*s
)->wild_statement
.children
.head
;
7777 case lang_group_statement_enum
:
7778 t
= &(*s
)->group_statement
.children
.head
;
7780 case lang_output_section_statement_enum
:
7781 t
= &(*s
)->output_section_statement
.children
.head
;
7786 t
= find_next_input_statement (t
);
7792 #endif /* BFD_SUPPORTS_PLUGINS */
7794 /* Add NAME to the list of garbage collection entry points. */
7797 lang_add_gc_name (const char *name
)
7799 struct bfd_sym_chain
*sym
;
7804 sym
= stat_alloc (sizeof (*sym
));
7806 sym
->next
= link_info
.gc_sym_list
;
7808 link_info
.gc_sym_list
= sym
;
7811 /* Check relocations. */
7814 lang_check_relocs (void)
7816 if (link_info
.check_relocs_after_open_input
)
7820 for (abfd
= link_info
.input_bfds
;
7821 abfd
!= (bfd
*) NULL
; abfd
= abfd
->link
.next
)
7822 if (!bfd_link_check_relocs (abfd
, &link_info
))
7824 /* No object output, fail return. */
7825 config
.make_executable
= false;
7826 /* Note: we do not abort the loop, but rather
7827 continue the scan in case there are other
7828 bad relocations to report. */
7833 /* Look through all output sections looking for places where we can
7834 propagate forward the lma region. */
7837 lang_propagate_lma_regions (void)
7839 lang_output_section_statement_type
*os
;
7841 for (os
= (void *) lang_os_list
.head
;
7845 if (os
->prev
!= NULL
7846 && os
->lma_region
== NULL
7847 && os
->load_base
== NULL
7848 && os
->addr_tree
== NULL
7849 && os
->region
== os
->prev
->region
)
7850 os
->lma_region
= os
->prev
->lma_region
;
7855 warn_non_contiguous_discards (void)
7857 LANG_FOR_EACH_INPUT_STATEMENT (file
)
7859 if ((file
->the_bfd
->flags
& (BFD_LINKER_CREATED
| DYNAMIC
)) != 0
7860 || file
->flags
.just_syms
)
7863 for (asection
*s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7864 if (s
->output_section
== NULL
7865 && (s
->flags
& SEC_LINKER_CREATED
) == 0)
7866 einfo (_("%P: warning: --enable-non-contiguous-regions "
7867 "discards section `%pA' from `%pB'\n"),
7873 reset_one_wild (lang_statement_union_type
*statement
)
7875 if (statement
->header
.type
== lang_wild_statement_enum
)
7877 lang_wild_statement_type
*stmt
= &statement
->wild_statement
;
7878 lang_list_init (&stmt
->matching_sections
);
7883 reset_resolved_wilds (void)
7885 lang_for_each_statement (reset_one_wild
);
7891 /* Finalize dynamic list. */
7892 if (link_info
.dynamic_list
)
7893 lang_finalize_version_expr_head (&link_info
.dynamic_list
->head
);
7895 current_target
= default_target
;
7897 /* Open the output file. */
7898 lang_for_each_statement (ldlang_open_output
);
7901 ldemul_create_output_section_statements ();
7903 /* Add to the hash table all undefineds on the command line. */
7904 lang_place_undefineds ();
7906 if (!bfd_section_already_linked_table_init ())
7907 einfo (_("%F%P: can not create hash table: %E\n"));
7909 /* A first pass through the memory regions ensures that if any region
7910 references a symbol for its origin or length then this symbol will be
7911 added to the symbol table. Having these symbols in the symbol table
7912 means that when we call open_input_bfds PROVIDE statements will
7913 trigger to provide any needed symbols. The regions origins and
7914 lengths are not assigned as a result of this call. */
7915 lang_do_memory_regions (false);
7917 /* Create a bfd for each input file. */
7918 current_target
= default_target
;
7919 lang_statement_iteration
++;
7920 open_input_bfds (statement_list
.head
, OPEN_BFD_NORMAL
);
7922 /* Now that open_input_bfds has processed assignments and provide
7923 statements we can give values to symbolic origin/length now. */
7924 lang_do_memory_regions (true);
7926 #if BFD_SUPPORTS_PLUGINS
7927 if (link_info
.lto_plugin_active
)
7929 lang_statement_list_type added
;
7930 lang_statement_list_type files
, inputfiles
;
7932 ldemul_before_plugin_all_symbols_read ();
7934 /* Now all files are read, let the plugin(s) decide if there
7935 are any more to be added to the link before we call the
7936 emulation's after_open hook. We create a private list of
7937 input statements for this purpose, which we will eventually
7938 insert into the global statement list after the first claimed
7941 /* We need to manipulate all three chains in synchrony. */
7943 inputfiles
= input_file_chain
;
7944 if (plugin_call_all_symbols_read ())
7945 einfo (_("%F%P: %s: plugin reported error after all symbols read\n"),
7946 plugin_error_plugin ());
7947 link_info
.lto_all_symbols_read
= true;
7948 /* Open any newly added files, updating the file chains. */
7949 plugin_undefs
= link_info
.hash
->undefs_tail
;
7950 open_input_bfds (*added
.tail
, OPEN_BFD_NORMAL
);
7951 if (plugin_undefs
== link_info
.hash
->undefs_tail
)
7952 plugin_undefs
= NULL
;
7953 /* Restore the global list pointer now they have all been added. */
7954 lang_list_remove_tail (stat_ptr
, &added
);
7955 /* And detach the fresh ends of the file lists. */
7956 lang_list_remove_tail (&file_chain
, &files
);
7957 lang_list_remove_tail (&input_file_chain
, &inputfiles
);
7958 /* Were any new files added? */
7959 if (added
.head
!= NULL
)
7961 /* If so, we will insert them into the statement list immediately
7962 after the first input file that was claimed by the plugin,
7963 unless that file was an archive in which case it is inserted
7964 immediately before. */
7966 lang_statement_union_type
**prev
;
7967 plugin_insert
= find_replacements_insert_point (&before
);
7968 /* If a plugin adds input files without having claimed any, we
7969 don't really have a good idea where to place them. Just putting
7970 them at the start or end of the list is liable to leave them
7971 outside the crtbegin...crtend range. */
7972 ASSERT (plugin_insert
!= NULL
);
7973 /* Splice the new statement list into the old one. */
7974 prev
= &plugin_insert
->header
.next
;
7977 prev
= find_next_input_statement (prev
);
7978 if (*prev
!= (void *) plugin_insert
->next_real_file
)
7980 /* We didn't find the expected input statement.
7981 Fall back to adding after plugin_insert. */
7982 prev
= &plugin_insert
->header
.next
;
7985 lang_list_insert_after (stat_ptr
, &added
, prev
);
7986 /* Likewise for the file chains. */
7987 lang_list_insert_after (&input_file_chain
, &inputfiles
,
7988 (void *) &plugin_insert
->next_real_file
);
7989 /* We must be careful when relinking file_chain; we may need to
7990 insert the new files at the head of the list if the insert
7991 point chosen is the dummy first input file. */
7992 if (plugin_insert
->filename
)
7993 lang_list_insert_after (&file_chain
, &files
,
7994 (void *) &plugin_insert
->next
);
7996 lang_list_insert_after (&file_chain
, &files
, &file_chain
.head
);
7998 /* Rescan archives in case new undefined symbols have appeared. */
8000 lang_statement_iteration
++;
8001 open_input_bfds (statement_list
.head
, OPEN_BFD_RESCAN
);
8002 lang_list_remove_tail (&file_chain
, &files
);
8003 while (files
.head
!= NULL
)
8005 lang_input_statement_type
**insert
;
8006 lang_input_statement_type
**iter
, *temp
;
8009 insert
= find_rescan_insertion (&files
.head
->input_statement
);
8010 /* All elements from an archive can be added at once. */
8011 iter
= &files
.head
->input_statement
.next
;
8012 my_arch
= files
.head
->input_statement
.the_bfd
->my_archive
;
8013 if (my_arch
!= NULL
)
8014 for (; *iter
!= NULL
; iter
= &(*iter
)->next
)
8015 if ((*iter
)->the_bfd
->my_archive
!= my_arch
)
8018 *insert
= &files
.head
->input_statement
;
8019 files
.head
= (lang_statement_union_type
*) *iter
;
8021 if (file_chain
.tail
== (lang_statement_union_type
**) insert
)
8022 file_chain
.tail
= (lang_statement_union_type
**) iter
;
8023 if (my_arch
!= NULL
)
8025 lang_input_statement_type
*parent
= bfd_usrdata (my_arch
);
8027 parent
->next
= (lang_input_statement_type
*)
8029 - offsetof (lang_input_statement_type
, next
));
8034 #endif /* BFD_SUPPORTS_PLUGINS */
8036 /* Make sure that nobody has tried to add a symbol to this list
8038 ASSERT (link_info
.gc_sym_list
== NULL
);
8040 link_info
.gc_sym_list
= &entry_symbol
;
8042 if (entry_symbol
.name
== NULL
)
8044 link_info
.gc_sym_list
= ldlang_undef_chain_list_head
;
8046 /* entry_symbol is normally initialied by a ENTRY definition in the
8047 linker script or the -e command line option. But if neither of
8048 these have been used, the target specific backend may still have
8049 provided an entry symbol via a call to lang_default_entry().
8050 Unfortunately this value will not be processed until lang_end()
8051 is called, long after this function has finished. So detect this
8052 case here and add the target's entry symbol to the list of starting
8053 points for garbage collection resolution. */
8054 lang_add_gc_name (entry_symbol_default
);
8057 lang_add_gc_name (link_info
.init_function
);
8058 lang_add_gc_name (link_info
.fini_function
);
8060 ldemul_after_open ();
8061 if (config
.map_file
!= NULL
)
8062 lang_print_asneeded ();
8066 bfd_section_already_linked_table_free ();
8068 /* Make sure that we're not mixing architectures. We call this
8069 after all the input files have been opened, but before we do any
8070 other processing, so that any operations merge_private_bfd_data
8071 does on the output file will be known during the rest of the
8075 /* Handle .exports instead of a version script if we're told to do so. */
8076 if (command_line
.version_exports_section
)
8077 lang_do_version_exports_section ();
8079 /* Build all sets based on the information gathered from the input
8081 ldctor_build_sets ();
8083 lang_symbol_tweaks ();
8085 /* PR 13683: We must rerun the assignments prior to running garbage
8086 collection in order to make sure that all symbol aliases are resolved. */
8087 lang_do_assignments (lang_mark_phase_enum
);
8088 expld
.phase
= lang_first_phase_enum
;
8090 /* Size up the common data. */
8094 debug_prefix_tree ();
8098 /* Remove unreferenced sections if asked to. */
8099 lang_gc_sections ();
8101 lang_mark_undefineds ();
8103 /* Check relocations. */
8104 lang_check_relocs ();
8106 ldemul_after_check_relocs ();
8108 /* There might have been new sections created (e.g. as result of
8109 checking relocs to need a .got, or suchlike), so to properly order
8110 them into our lists of matching sections reset them here. */
8111 reset_resolved_wilds ();
8114 /* Update wild statements in case the user gave --sort-section.
8115 Note how the option might have come after the linker script and
8116 so couldn't have been set when the wild statements were created. */
8117 update_wild_statements (statement_list
.head
);
8119 /* Run through the contours of the script and attach input sections
8120 to the correct output sections. */
8121 lang_statement_iteration
++;
8122 map_input_to_output_sections (statement_list
.head
, NULL
, NULL
);
8124 /* Start at the statement immediately after the special abs_section
8125 output statement, so that it isn't reordered. */
8126 process_insert_statements (&lang_os_list
.head
->header
.next
);
8128 ldemul_before_place_orphans ();
8130 /* Find any sections not attached explicitly and handle them. */
8131 lang_place_orphans ();
8133 if (!bfd_link_relocatable (&link_info
))
8137 /* Merge SEC_MERGE sections. This has to be done after GC of
8138 sections, so that GCed sections are not merged, but before
8139 assigning dynamic symbols, since removing whole input sections
8141 bfd_merge_sections (link_info
.output_bfd
, &link_info
);
8143 /* Look for a text section and set the readonly attribute in it. */
8144 found
= bfd_get_section_by_name (link_info
.output_bfd
, ".text");
8148 if (config
.text_read_only
)
8149 found
->flags
|= SEC_READONLY
;
8151 found
->flags
&= ~SEC_READONLY
;
8155 /* Merge together CTF sections. After this, only the symtab-dependent
8156 function and data object sections need adjustment. */
8159 /* Emit the CTF, iff the emulation doesn't need to do late emission after
8160 examining things laid out late, like the strtab. */
8163 /* Copy forward lma regions for output sections in same lma region. */
8164 lang_propagate_lma_regions ();
8166 /* Defining __start/__stop symbols early for --gc-sections to work
8167 around a glibc build problem can result in these symbols being
8168 defined when they should not be. Fix them now. */
8169 if (config
.build_constructors
)
8170 lang_undef_start_stop ();
8172 /* Define .startof./.sizeof. symbols with preliminary values before
8173 dynamic symbols are created. */
8174 if (!bfd_link_relocatable (&link_info
))
8175 lang_init_startof_sizeof ();
8177 /* Do anything special before sizing sections. This is where ELF
8178 and other back-ends size dynamic sections. */
8179 ldemul_before_allocation ();
8181 /* We must record the program headers before we try to fix the
8182 section positions, since they will affect SIZEOF_HEADERS. */
8183 lang_record_phdrs ();
8185 /* Check relro sections. */
8186 if (link_info
.relro
&& !bfd_link_relocatable (&link_info
))
8187 lang_find_relro_sections ();
8189 /* Size up the sections. */
8190 lang_size_sections (NULL
, !RELAXATION_ENABLED
);
8192 /* See if anything special should be done now we know how big
8193 everything is. This is where relaxation is done. */
8194 ldemul_after_allocation ();
8196 /* Fix any __start, __stop, .startof. or .sizeof. symbols. */
8197 lang_finalize_start_stop ();
8199 /* Do all the assignments again, to report errors. Assignment
8200 statements are processed multiple times, updating symbols; In
8201 open_input_bfds, lang_do_assignments, and lang_size_sections.
8202 Since lang_relax_sections calls lang_do_assignments, symbols are
8203 also updated in ldemul_after_allocation. */
8204 lang_do_assignments (lang_final_phase_enum
);
8208 /* Convert absolute symbols to section relative. */
8209 ldexp_finalize_syms ();
8211 /* Make sure that the section addresses make sense. */
8212 if (command_line
.check_section_addresses
)
8213 lang_check_section_addresses ();
8215 if (link_info
.non_contiguous_regions
8216 && link_info
.non_contiguous_regions_warnings
)
8217 warn_non_contiguous_discards ();
8219 /* Check any required symbols are known. */
8220 ldlang_check_require_defined_symbols ();
8225 /* EXPORTED TO YACC */
8228 lang_add_wild (struct wildcard_spec
*filespec
,
8229 struct wildcard_list
*section_list
,
8232 struct wildcard_list
*curr
, *next
;
8233 lang_wild_statement_type
*new_stmt
;
8234 bool any_specs_sorted
= false;
8236 /* Reverse the list as the parser puts it back to front. */
8237 for (curr
= section_list
, section_list
= NULL
;
8239 section_list
= curr
, curr
= next
)
8241 if (curr
->spec
.sorted
!= none
&& curr
->spec
.sorted
!= by_none
)
8242 any_specs_sorted
= true;
8244 curr
->next
= section_list
;
8247 if (filespec
!= NULL
&& filespec
->name
!= NULL
)
8249 if (strcmp (filespec
->name
, "*") == 0)
8250 filespec
->name
= NULL
;
8251 else if (!wildcardp (filespec
->name
))
8252 lang_has_input_file
= true;
8255 new_stmt
= new_stat (lang_wild_statement
, stat_ptr
);
8256 new_stmt
->filename
= NULL
;
8257 new_stmt
->filenames_sorted
= false;
8258 new_stmt
->any_specs_sorted
= any_specs_sorted
;
8259 new_stmt
->section_flag_list
= NULL
;
8260 new_stmt
->exclude_name_list
= NULL
;
8261 if (filespec
!= NULL
)
8263 new_stmt
->filename
= filespec
->name
;
8264 new_stmt
->filenames_sorted
= filespec
->sorted
== by_name
;
8265 new_stmt
->section_flag_list
= filespec
->section_flag_list
;
8266 new_stmt
->exclude_name_list
= filespec
->exclude_name_list
;
8268 new_stmt
->section_list
= section_list
;
8269 new_stmt
->keep_sections
= keep_sections
;
8270 lang_list_init (&new_stmt
->children
);
8271 lang_list_init (&new_stmt
->matching_sections
);
8272 analyze_walk_wild_section_handler (new_stmt
);
8275 printf ("wild %s(", new_stmt
->filename
? new_stmt
->filename
: "*");
8276 for (curr
= new_stmt
->section_list
; curr
; curr
= curr
->next
)
8277 printf ("%s ", curr
->spec
.name
? curr
->spec
.name
: "*");
8283 lang_section_start (const char *name
, etree_type
*address
,
8284 const segment_type
*segment
)
8286 lang_address_statement_type
*ad
;
8288 ad
= new_stat (lang_address_statement
, stat_ptr
);
8289 ad
->section_name
= name
;
8290 ad
->address
= address
;
8291 ad
->segment
= segment
;
8294 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
8295 because of a -e argument on the command line, or zero if this is
8296 called by ENTRY in a linker script. Command line arguments take
8300 lang_add_entry (const char *name
, bool cmdline
)
8302 if (entry_symbol
.name
== NULL
8304 || !entry_from_cmdline
)
8306 entry_symbol
.name
= name
;
8307 entry_from_cmdline
= cmdline
;
8311 /* Set the default start symbol to NAME. .em files should use this,
8312 not lang_add_entry, to override the use of "start" if neither the
8313 linker script nor the command line specifies an entry point. NAME
8314 must be permanently allocated. */
8316 lang_default_entry (const char *name
)
8318 entry_symbol_default
= name
;
8322 lang_add_target (const char *name
)
8324 lang_target_statement_type
*new_stmt
;
8326 new_stmt
= new_stat (lang_target_statement
, stat_ptr
);
8327 new_stmt
->target
= name
;
8331 lang_add_map (const char *name
)
8338 map_option_f
= true;
8346 lang_add_fill (fill_type
*fill
)
8348 lang_fill_statement_type
*new_stmt
;
8350 new_stmt
= new_stat (lang_fill_statement
, stat_ptr
);
8351 new_stmt
->fill
= fill
;
8355 lang_add_data (int type
, union etree_union
*exp
)
8357 lang_data_statement_type
*new_stmt
;
8359 new_stmt
= new_stat (lang_data_statement
, stat_ptr
);
8360 new_stmt
->exp
= exp
;
8361 new_stmt
->type
= type
;
8364 /* Convert escape codes in S.
8365 Supports \n, \r, \t and \NNN octals.
8366 Returns a copy of S in a malloc'ed buffer. */
8369 convert_string (const char * s
)
8371 size_t len
= strlen (s
);
8373 bool escape
= false;
8374 char * buffer
= malloc (len
+ 1);
8377 for (i
= 0, b
= buffer
; i
< len
; i
++)
8386 /* Ignore the escape. */
8389 case 'n': c
= '\n'; break;
8390 case 'r': c
= '\r'; break;
8391 case 't': c
= '\t'; break;
8401 /* We have an octal number. */
8403 unsigned int value
= c
- '0';
8406 if ((c
>= '0') && (c
<= '7'))
8414 if ((c
>= '0') && (c
<= '7'))
8425 /* octal: \777 is treated as '\077' + '7' */
8454 lang_add_string (size_t size
, const char *s
)
8460 string
= convert_string (s
);
8461 len
= strlen (string
);
8463 /* Check if it is ASCIZ command (len == 0) */
8465 /* Make sure that we include the terminating nul byte. */
8467 else if (len
>= size
)
8471 einfo (_("%P:%pS: warning: ASCII string does not fit in allocated space,"
8472 " truncated\n"), NULL
);
8475 for (i
= 0 ; i
< len
; i
++)
8476 lang_add_data (BYTE
, exp_intop (string
[i
]));
8479 lang_add_data (BYTE
, exp_intop ('\0'));
8484 /* Create a new reloc statement. RELOC is the BFD relocation type to
8485 generate. HOWTO is the corresponding howto structure (we could
8486 look this up, but the caller has already done so). SECTION is the
8487 section to generate a reloc against, or NAME is the name of the
8488 symbol to generate a reloc against. Exactly one of SECTION and
8489 NAME must be NULL. ADDEND is an expression for the addend. */
8492 lang_add_reloc (bfd_reloc_code_real_type reloc
,
8493 reloc_howto_type
*howto
,
8496 union etree_union
*addend
)
8498 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
8502 p
->section
= section
;
8504 p
->addend_exp
= addend
;
8506 p
->addend_value
= 0;
8507 p
->output_section
= NULL
;
8508 p
->output_offset
= 0;
8511 lang_assignment_statement_type
*
8512 lang_add_assignment (etree_type
*exp
)
8514 lang_assignment_statement_type
*new_stmt
;
8516 new_stmt
= new_stat (lang_assignment_statement
, stat_ptr
);
8517 new_stmt
->exp
= exp
;
8522 lang_add_attribute (enum statement_enum attribute
)
8524 new_statement (attribute
, sizeof (lang_statement_header_type
), stat_ptr
);
8528 lang_startup (const char *name
)
8530 if (first_file
->filename
!= NULL
)
8532 einfo (_("%F%P: multiple STARTUP files\n"));
8534 first_file
->filename
= name
;
8535 first_file
->local_sym_name
= name
;
8536 first_file
->flags
.real
= true;
8540 lang_float (bool maybe
)
8542 lang_float_flag
= maybe
;
8546 /* Work out the load- and run-time regions from a script statement, and
8547 store them in *LMA_REGION and *REGION respectively.
8549 MEMSPEC is the name of the run-time region, or the value of
8550 DEFAULT_MEMORY_REGION if the statement didn't specify one.
8551 LMA_MEMSPEC is the name of the load-time region, or null if the
8552 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
8553 had an explicit load address.
8555 It is an error to specify both a load region and a load address. */
8558 lang_get_regions (lang_memory_region_type
**region
,
8559 lang_memory_region_type
**lma_region
,
8560 const char *memspec
,
8561 const char *lma_memspec
,
8565 *lma_region
= lang_memory_region_lookup (lma_memspec
, false);
8567 /* If no runtime region or VMA has been specified, but the load region
8568 has been specified, then use the load region for the runtime region
8570 if (lma_memspec
!= NULL
8572 && strcmp (memspec
, DEFAULT_MEMORY_REGION
) == 0)
8573 *region
= *lma_region
;
8575 *region
= lang_memory_region_lookup (memspec
, false);
8577 if (have_lma
&& lma_memspec
!= 0)
8578 einfo (_("%X%P:%pS: section has both a load address and a load region\n"),
8583 lang_leave_output_section_statement (fill_type
*fill
, const char *memspec
,
8584 lang_output_section_phdr_list
*phdrs
,
8585 const char *lma_memspec
)
8587 lang_get_regions (¤t_section
->region
,
8588 ¤t_section
->lma_region
,
8589 memspec
, lma_memspec
,
8590 current_section
->load_base
!= NULL
,
8591 current_section
->addr_tree
!= NULL
);
8593 current_section
->fill
= fill
;
8594 current_section
->phdrs
= phdrs
;
8598 /* Set the output format type. -oformat overrides scripts. */
8601 lang_add_output_format (const char *format
,
8606 if (output_target
== NULL
|| !from_script
)
8608 if (command_line
.endian
== ENDIAN_BIG
8611 else if (command_line
.endian
== ENDIAN_LITTLE
8615 output_target
= format
;
8620 lang_add_insert (const char *where
, int is_before
)
8622 lang_insert_statement_type
*new_stmt
;
8624 new_stmt
= new_stat (lang_insert_statement
, stat_ptr
);
8625 new_stmt
->where
= where
;
8626 new_stmt
->is_before
= is_before
;
8627 saved_script_handle
= previous_script_handle
;
8630 /* Enter a group. This creates a new lang_group_statement, and sets
8631 stat_ptr to build new statements within the group. */
8634 lang_enter_group (void)
8636 lang_group_statement_type
*g
;
8638 g
= new_stat (lang_group_statement
, stat_ptr
);
8639 lang_list_init (&g
->children
);
8640 push_stat_ptr (&g
->children
);
8643 /* Leave a group. This just resets stat_ptr to start writing to the
8644 regular list of statements again. Note that this will not work if
8645 groups can occur inside anything else which can adjust stat_ptr,
8646 but currently they can't. */
8649 lang_leave_group (void)
8654 /* Add a new program header. This is called for each entry in a PHDRS
8655 command in a linker script. */
8658 lang_new_phdr (const char *name
,
8665 struct lang_phdr
*n
, **pp
;
8668 n
= stat_alloc (sizeof (struct lang_phdr
));
8671 n
->type
= exp_get_vma (type
, 0, "program header type");
8672 n
->filehdr
= filehdr
;
8677 hdrs
= n
->type
== 1 && (phdrs
|| filehdr
);
8679 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
8682 && !((*pp
)->filehdr
|| (*pp
)->phdrs
))
8684 einfo (_("%X%P:%pS: PHDRS and FILEHDR are not supported"
8685 " when prior PT_LOAD headers lack them\n"), NULL
);
8692 /* Record the program header information in the output BFD. FIXME: We
8693 should not be calling an ELF specific function here. */
8696 lang_record_phdrs (void)
8700 lang_output_section_phdr_list
*last
;
8701 struct lang_phdr
*l
;
8702 lang_output_section_statement_type
*os
;
8705 secs
= (asection
**) xmalloc (alc
* sizeof (asection
*));
8708 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
8715 for (os
= (void *) lang_os_list
.head
;
8719 lang_output_section_phdr_list
*pl
;
8721 if (os
->constraint
< 0)
8729 if (os
->sectype
== noload_section
8730 || os
->bfd_section
== NULL
8731 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
8734 /* Don't add orphans to PT_INTERP header. */
8740 lang_output_section_statement_type
*tmp_os
;
8742 /* If we have not run across a section with a program
8743 header assigned to it yet, then scan forwards to find
8744 one. This prevents inconsistencies in the linker's
8745 behaviour when a script has specified just a single
8746 header and there are sections in that script which are
8747 not assigned to it, and which occur before the first
8748 use of that header. See here for more details:
8749 http://sourceware.org/ml/binutils/2007-02/msg00291.html */
8750 for (tmp_os
= os
; tmp_os
; tmp_os
= tmp_os
->next
)
8753 last
= tmp_os
->phdrs
;
8757 einfo (_("%F%P: no sections assigned to phdrs\n"));
8762 if (os
->bfd_section
== NULL
)
8765 for (; pl
!= NULL
; pl
= pl
->next
)
8767 if (strcmp (pl
->name
, l
->name
) == 0)
8772 secs
= (asection
**) xrealloc (secs
,
8773 alc
* sizeof (asection
*));
8775 secs
[c
] = os
->bfd_section
;
8782 if (l
->flags
== NULL
)
8785 flags
= exp_get_vma (l
->flags
, 0, "phdr flags");
8790 at
= exp_get_vma (l
->at
, 0, "phdr load address");
8792 if (!bfd_record_phdr (link_info
.output_bfd
, l
->type
,
8793 l
->flags
!= NULL
, flags
, l
->at
!= NULL
,
8794 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
8795 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
8800 /* Make sure all the phdr assignments succeeded. */
8801 for (os
= (void *) lang_os_list
.head
;
8805 lang_output_section_phdr_list
*pl
;
8807 if (os
->constraint
< 0
8808 || os
->bfd_section
== NULL
)
8811 for (pl
= os
->phdrs
;
8814 if (!pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
8815 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
8816 os
->name
, pl
->name
);
8820 /* Record a list of sections which may not be cross referenced. */
8823 lang_add_nocrossref (lang_nocrossref_type
*l
)
8825 struct lang_nocrossrefs
*n
;
8827 n
= (struct lang_nocrossrefs
*) xmalloc (sizeof *n
);
8828 n
->next
= nocrossref_list
;
8830 n
->onlyfirst
= false;
8831 nocrossref_list
= n
;
8833 /* Set notice_all so that we get informed about all symbols. */
8834 link_info
.notice_all
= true;
8837 /* Record a section that cannot be referenced from a list of sections. */
8840 lang_add_nocrossref_to (lang_nocrossref_type
*l
)
8842 lang_add_nocrossref (l
);
8843 nocrossref_list
->onlyfirst
= true;
8846 /* Overlay handling. We handle overlays with some static variables. */
8848 /* The overlay virtual address. */
8849 static etree_type
*overlay_vma
;
8850 /* And subsection alignment. */
8851 static etree_type
*overlay_subalign
;
8853 /* An expression for the maximum section size seen so far. */
8854 static etree_type
*overlay_max
;
8856 /* A list of all the sections in this overlay. */
8858 struct overlay_list
{
8859 struct overlay_list
*next
;
8860 lang_output_section_statement_type
*os
;
8863 static struct overlay_list
*overlay_list
;
8865 /* Start handling an overlay. */
8868 lang_enter_overlay (etree_type
*vma_expr
, etree_type
*subalign
)
8870 /* The grammar should prevent nested overlays from occurring. */
8871 ASSERT (overlay_vma
== NULL
8872 && overlay_subalign
== NULL
8873 && overlay_max
== NULL
);
8875 overlay_vma
= vma_expr
;
8876 overlay_subalign
= subalign
;
8879 /* Start a section in an overlay. We handle this by calling
8880 lang_enter_output_section_statement with the correct VMA.
8881 lang_leave_overlay sets up the LMA and memory regions. */
8884 lang_enter_overlay_section (const char *name
)
8886 struct overlay_list
*n
;
8889 lang_enter_output_section_statement (name
, overlay_vma
, overlay_section
,
8890 0, 0, overlay_subalign
, 0, 0, 0);
8892 /* If this is the first section, then base the VMA of future
8893 sections on this one. This will work correctly even if `.' is
8894 used in the addresses. */
8895 if (overlay_list
== NULL
)
8896 overlay_vma
= exp_nameop (ADDR
, name
);
8898 /* Remember the section. */
8899 n
= (struct overlay_list
*) xmalloc (sizeof *n
);
8900 n
->os
= current_section
;
8901 n
->next
= overlay_list
;
8904 size
= exp_nameop (SIZEOF
, name
);
8906 /* Arrange to work out the maximum section end address. */
8907 if (overlay_max
== NULL
)
8910 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
8913 /* Finish a section in an overlay. There isn't any special to do
8917 lang_leave_overlay_section (fill_type
*fill
,
8918 lang_output_section_phdr_list
*phdrs
)
8925 name
= current_section
->name
;
8927 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
8928 region and that no load-time region has been specified. It doesn't
8929 really matter what we say here, since lang_leave_overlay will
8931 lang_leave_output_section_statement (fill
, DEFAULT_MEMORY_REGION
, phdrs
, 0);
8933 /* Define the magic symbols. */
8935 clean
= (char *) xmalloc (strlen (name
) + 1);
8937 for (s1
= name
; *s1
!= '\0'; s1
++)
8938 if (ISALNUM (*s1
) || *s1
== '_')
8942 buf
= (char *) xmalloc (strlen (clean
) + sizeof "__load_start_");
8943 sprintf (buf
, "__load_start_%s", clean
);
8944 lang_add_assignment (exp_provide (buf
,
8945 exp_nameop (LOADADDR
, name
),
8948 buf
= (char *) xmalloc (strlen (clean
) + sizeof "__load_stop_");
8949 sprintf (buf
, "__load_stop_%s", clean
);
8950 lang_add_assignment (exp_provide (buf
,
8952 exp_nameop (LOADADDR
, name
),
8953 exp_nameop (SIZEOF
, name
)),
8959 /* Finish an overlay. If there are any overlay wide settings, this
8960 looks through all the sections in the overlay and sets them. */
8963 lang_leave_overlay (etree_type
*lma_expr
,
8966 const char *memspec
,
8967 lang_output_section_phdr_list
*phdrs
,
8968 const char *lma_memspec
)
8970 lang_memory_region_type
*region
;
8971 lang_memory_region_type
*lma_region
;
8972 struct overlay_list
*l
;
8973 lang_nocrossref_type
*nocrossref
;
8975 lang_get_regions (®ion
, &lma_region
,
8976 memspec
, lma_memspec
,
8977 lma_expr
!= NULL
, false);
8981 /* After setting the size of the last section, set '.' to end of the
8983 if (overlay_list
!= NULL
)
8985 overlay_list
->os
->update_dot
= 1;
8986 overlay_list
->os
->update_dot_tree
8987 = exp_assign (".", exp_binop ('+', overlay_vma
, overlay_max
), false);
8993 struct overlay_list
*next
;
8995 if (fill
!= NULL
&& l
->os
->fill
== NULL
)
8998 l
->os
->region
= region
;
8999 l
->os
->lma_region
= lma_region
;
9001 /* The first section has the load address specified in the
9002 OVERLAY statement. The rest are worked out from that.
9003 The base address is not needed (and should be null) if
9004 an LMA region was specified. */
9007 l
->os
->load_base
= lma_expr
;
9008 l
->os
->sectype
= first_overlay_section
;
9010 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
9011 l
->os
->phdrs
= phdrs
;
9015 lang_nocrossref_type
*nc
;
9017 nc
= (lang_nocrossref_type
*) xmalloc (sizeof *nc
);
9018 nc
->name
= l
->os
->name
;
9019 nc
->next
= nocrossref
;
9028 if (nocrossref
!= NULL
)
9029 lang_add_nocrossref (nocrossref
);
9032 overlay_list
= NULL
;
9034 overlay_subalign
= NULL
;
9037 /* Version handling. This is only useful for ELF. */
9039 /* If PREV is NULL, return first version pattern matching particular symbol.
9040 If PREV is non-NULL, return first version pattern matching particular
9041 symbol after PREV (previously returned by lang_vers_match). */
9043 static struct bfd_elf_version_expr
*
9044 lang_vers_match (struct bfd_elf_version_expr_head
*head
,
9045 struct bfd_elf_version_expr
*prev
,
9049 const char *cxx_sym
= sym
;
9050 const char *java_sym
= sym
;
9051 struct bfd_elf_version_expr
*expr
= NULL
;
9052 enum demangling_styles curr_style
;
9054 curr_style
= CURRENT_DEMANGLING_STYLE
;
9055 cplus_demangle_set_style (no_demangling
);
9056 c_sym
= bfd_demangle (link_info
.output_bfd
, sym
, DMGL_NO_OPTS
);
9059 cplus_demangle_set_style (curr_style
);
9061 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
9063 cxx_sym
= bfd_demangle (link_info
.output_bfd
, sym
,
9064 DMGL_PARAMS
| DMGL_ANSI
);
9068 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
9070 java_sym
= bfd_demangle (link_info
.output_bfd
, sym
, DMGL_JAVA
);
9075 if (head
->htab
&& (prev
== NULL
|| prev
->literal
))
9077 struct bfd_elf_version_expr e
;
9079 switch (prev
? prev
->mask
: 0)
9082 if (head
->mask
& BFD_ELF_VERSION_C_TYPE
)
9085 expr
= (struct bfd_elf_version_expr
*)
9086 htab_find ((htab_t
) head
->htab
, &e
);
9087 while (expr
&& strcmp (expr
->pattern
, c_sym
) == 0)
9088 if (expr
->mask
== BFD_ELF_VERSION_C_TYPE
)
9094 case BFD_ELF_VERSION_C_TYPE
:
9095 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
9097 e
.pattern
= cxx_sym
;
9098 expr
= (struct bfd_elf_version_expr
*)
9099 htab_find ((htab_t
) head
->htab
, &e
);
9100 while (expr
&& strcmp (expr
->pattern
, cxx_sym
) == 0)
9101 if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
9107 case BFD_ELF_VERSION_CXX_TYPE
:
9108 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
9110 e
.pattern
= java_sym
;
9111 expr
= (struct bfd_elf_version_expr
*)
9112 htab_find ((htab_t
) head
->htab
, &e
);
9113 while (expr
&& strcmp (expr
->pattern
, java_sym
) == 0)
9114 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
9125 /* Finally, try the wildcards. */
9126 if (prev
== NULL
|| prev
->literal
)
9127 expr
= head
->remaining
;
9130 for (; expr
; expr
= expr
->next
)
9137 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
9140 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
9142 else if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
9146 if (fnmatch (expr
->pattern
, s
, 0) == 0)
9152 free ((char *) c_sym
);
9154 free ((char *) cxx_sym
);
9155 if (java_sym
!= sym
)
9156 free ((char *) java_sym
);
9160 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
9161 return a pointer to the symbol name with any backslash quotes removed. */
9164 realsymbol (const char *pattern
)
9167 bool changed
= false, backslash
= false;
9168 char *s
, *symbol
= (char *) xmalloc (strlen (pattern
) + 1);
9170 for (p
= pattern
, s
= symbol
; *p
!= '\0'; ++p
)
9172 /* It is a glob pattern only if there is no preceding
9176 /* Remove the preceding backslash. */
9183 if (*p
== '?' || *p
== '*' || *p
== '[')
9190 backslash
= *p
== '\\';
9206 /* This is called for each variable name or match expression. NEW_NAME is
9207 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
9208 pattern to be matched against symbol names. */
9210 struct bfd_elf_version_expr
*
9211 lang_new_vers_pattern (struct bfd_elf_version_expr
*orig
,
9212 const char *new_name
,
9216 struct bfd_elf_version_expr
*ret
;
9218 ret
= (struct bfd_elf_version_expr
*) xmalloc (sizeof *ret
);
9222 ret
->literal
= true;
9223 ret
->pattern
= literal_p
? new_name
: realsymbol (new_name
);
9224 if (ret
->pattern
== NULL
)
9226 ret
->pattern
= new_name
;
9227 ret
->literal
= false;
9230 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
9231 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
9232 else if (strcasecmp (lang
, "C++") == 0)
9233 ret
->mask
= BFD_ELF_VERSION_CXX_TYPE
;
9234 else if (strcasecmp (lang
, "Java") == 0)
9235 ret
->mask
= BFD_ELF_VERSION_JAVA_TYPE
;
9238 einfo (_("%X%P: unknown language `%s' in version information\n"),
9240 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
9243 return ldemul_new_vers_pattern (ret
);
9246 /* This is called for each set of variable names and match
9249 struct bfd_elf_version_tree
*
9250 lang_new_vers_node (struct bfd_elf_version_expr
*globals
,
9251 struct bfd_elf_version_expr
*locals
)
9253 struct bfd_elf_version_tree
*ret
;
9255 ret
= (struct bfd_elf_version_tree
*) xcalloc (1, sizeof *ret
);
9256 ret
->globals
.list
= globals
;
9257 ret
->locals
.list
= locals
;
9258 ret
->match
= lang_vers_match
;
9259 ret
->name_indx
= (unsigned int) -1;
9263 /* This static variable keeps track of version indices. */
9265 static int version_index
;
9268 version_expr_head_hash (const void *p
)
9270 const struct bfd_elf_version_expr
*e
=
9271 (const struct bfd_elf_version_expr
*) p
;
9273 return htab_hash_string (e
->pattern
);
9277 version_expr_head_eq (const void *p1
, const void *p2
)
9279 const struct bfd_elf_version_expr
*e1
=
9280 (const struct bfd_elf_version_expr
*) p1
;
9281 const struct bfd_elf_version_expr
*e2
=
9282 (const struct bfd_elf_version_expr
*) p2
;
9284 return strcmp (e1
->pattern
, e2
->pattern
) == 0;
9288 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head
*head
)
9291 struct bfd_elf_version_expr
*e
, *next
;
9292 struct bfd_elf_version_expr
**list_loc
, **remaining_loc
;
9294 for (e
= head
->list
; e
; e
= e
->next
)
9298 head
->mask
|= e
->mask
;
9303 head
->htab
= htab_create (count
* 2, version_expr_head_hash
,
9304 version_expr_head_eq
, NULL
);
9305 list_loc
= &head
->list
;
9306 remaining_loc
= &head
->remaining
;
9307 for (e
= head
->list
; e
; e
= next
)
9313 remaining_loc
= &e
->next
;
9317 void **loc
= htab_find_slot ((htab_t
) head
->htab
, e
, INSERT
);
9321 struct bfd_elf_version_expr
*e1
, *last
;
9323 e1
= (struct bfd_elf_version_expr
*) *loc
;
9327 if (e1
->mask
== e
->mask
)
9335 while (e1
&& strcmp (e1
->pattern
, e
->pattern
) == 0);
9339 /* This is a duplicate. */
9340 /* FIXME: Memory leak. Sometimes pattern is not
9341 xmalloced alone, but in larger chunk of memory. */
9342 /* free (e->pattern); */
9347 e
->next
= last
->next
;
9355 list_loc
= &e
->next
;
9359 *remaining_loc
= NULL
;
9360 *list_loc
= head
->remaining
;
9363 head
->remaining
= head
->list
;
9366 /* This is called when we know the name and dependencies of the
9370 lang_register_vers_node (const char *name
,
9371 struct bfd_elf_version_tree
*version
,
9372 struct bfd_elf_version_deps
*deps
)
9374 struct bfd_elf_version_tree
*t
, **pp
;
9375 struct bfd_elf_version_expr
*e1
;
9380 if (link_info
.version_info
!= NULL
9381 && (name
[0] == '\0' || link_info
.version_info
->name
[0] == '\0'))
9383 einfo (_("%X%P: anonymous version tag cannot be combined"
9384 " with other version tags\n"));
9389 /* Make sure this node has a unique name. */
9390 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9391 if (strcmp (t
->name
, name
) == 0)
9392 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
9394 lang_finalize_version_expr_head (&version
->globals
);
9395 lang_finalize_version_expr_head (&version
->locals
);
9397 /* Check the global and local match names, and make sure there
9398 aren't any duplicates. */
9400 for (e1
= version
->globals
.list
; e1
!= NULL
; e1
= e1
->next
)
9402 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9404 struct bfd_elf_version_expr
*e2
;
9406 if (t
->locals
.htab
&& e1
->literal
)
9408 e2
= (struct bfd_elf_version_expr
*)
9409 htab_find ((htab_t
) t
->locals
.htab
, e1
);
9410 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
9412 if (e1
->mask
== e2
->mask
)
9413 einfo (_("%X%P: duplicate expression `%s'"
9414 " in version information\n"), e1
->pattern
);
9418 else if (!e1
->literal
)
9419 for (e2
= t
->locals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
9420 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
9421 && e1
->mask
== e2
->mask
)
9422 einfo (_("%X%P: duplicate expression `%s'"
9423 " in version information\n"), e1
->pattern
);
9427 for (e1
= version
->locals
.list
; e1
!= NULL
; e1
= e1
->next
)
9429 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9431 struct bfd_elf_version_expr
*e2
;
9433 if (t
->globals
.htab
&& e1
->literal
)
9435 e2
= (struct bfd_elf_version_expr
*)
9436 htab_find ((htab_t
) t
->globals
.htab
, e1
);
9437 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
9439 if (e1
->mask
== e2
->mask
)
9440 einfo (_("%X%P: duplicate expression `%s'"
9441 " in version information\n"),
9446 else if (!e1
->literal
)
9447 for (e2
= t
->globals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
9448 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
9449 && e1
->mask
== e2
->mask
)
9450 einfo (_("%X%P: duplicate expression `%s'"
9451 " in version information\n"), e1
->pattern
);
9455 version
->deps
= deps
;
9456 version
->name
= name
;
9457 if (name
[0] != '\0')
9460 version
->vernum
= version_index
;
9463 version
->vernum
= 0;
9465 for (pp
= &link_info
.version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
9470 /* This is called when we see a version dependency. */
9472 struct bfd_elf_version_deps
*
9473 lang_add_vers_depend (struct bfd_elf_version_deps
*list
, const char *name
)
9475 struct bfd_elf_version_deps
*ret
;
9476 struct bfd_elf_version_tree
*t
;
9478 ret
= (struct bfd_elf_version_deps
*) xmalloc (sizeof *ret
);
9481 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9483 if (strcmp (t
->name
, name
) == 0)
9485 ret
->version_needed
= t
;
9490 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
9492 ret
->version_needed
= NULL
;
9497 lang_do_version_exports_section (void)
9499 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
9501 LANG_FOR_EACH_INPUT_STATEMENT (is
)
9503 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
9511 contents
= (char *) xmalloc (len
);
9512 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
9513 einfo (_("%X%P: unable to read .exports section contents\n"), sec
);
9516 while (p
< contents
+ len
)
9518 greg
= lang_new_vers_pattern (greg
, p
, NULL
, false);
9519 p
= strchr (p
, '\0') + 1;
9522 /* Do not free the contents, as we used them creating the regex. */
9524 /* Do not include this section in the link. */
9525 sec
->flags
|= SEC_EXCLUDE
| SEC_KEEP
;
9528 lreg
= lang_new_vers_pattern (NULL
, "*", NULL
, false);
9529 lang_register_vers_node (command_line
.version_exports_section
,
9530 lang_new_vers_node (greg
, lreg
), NULL
);
9533 /* Evaluate LENGTH and ORIGIN parts of MEMORY spec. This is initially
9534 called with UPDATE_REGIONS_P set to FALSE, in this case no errors are
9535 thrown, however, references to symbols in the origin and length fields
9536 will be pushed into the symbol table, this allows PROVIDE statements to
9537 then provide these symbols. This function is called a second time with
9538 UPDATE_REGIONS_P set to TRUE, this time the we update the actual region
9539 data structures, and throw errors if missing symbols are encountered. */
9542 lang_do_memory_regions (bool update_regions_p
)
9544 lang_memory_region_type
*r
= lang_memory_region_list
;
9546 for (; r
!= NULL
; r
= r
->next
)
9550 exp_fold_tree_no_dot (r
->origin_exp
);
9551 if (update_regions_p
)
9553 if (expld
.result
.valid_p
)
9555 r
->origin
= expld
.result
.value
;
9556 r
->current
= r
->origin
;
9559 einfo (_("%P: invalid origin for memory region %s\n"),
9565 exp_fold_tree_no_dot (r
->length_exp
);
9566 if (update_regions_p
)
9568 if (expld
.result
.valid_p
)
9569 r
->length
= expld
.result
.value
;
9571 einfo (_("%P: invalid length for memory region %s\n"),
9579 lang_add_unique (const char *name
)
9581 struct unique_sections
*ent
;
9583 for (ent
= unique_section_list
; ent
; ent
= ent
->next
)
9584 if (strcmp (ent
->name
, name
) == 0)
9587 ent
= (struct unique_sections
*) xmalloc (sizeof *ent
);
9588 ent
->name
= xstrdup (name
);
9589 ent
->next
= unique_section_list
;
9590 unique_section_list
= ent
;
9593 /* Append the list of dynamic symbols to the existing one. */
9596 lang_append_dynamic_list (struct bfd_elf_dynamic_list
**list_p
,
9597 struct bfd_elf_version_expr
*dynamic
)
9601 struct bfd_elf_version_expr
*tail
;
9602 for (tail
= dynamic
; tail
->next
!= NULL
; tail
= tail
->next
)
9604 tail
->next
= (*list_p
)->head
.list
;
9605 (*list_p
)->head
.list
= dynamic
;
9609 struct bfd_elf_dynamic_list
*d
;
9611 d
= (struct bfd_elf_dynamic_list
*) xcalloc (1, sizeof *d
);
9612 d
->head
.list
= dynamic
;
9613 d
->match
= lang_vers_match
;
9618 /* Append the list of C++ typeinfo dynamic symbols to the existing
9622 lang_append_dynamic_list_cpp_typeinfo (void)
9624 const char *symbols
[] =
9626 "typeinfo name for*",
9629 struct bfd_elf_version_expr
*dynamic
= NULL
;
9632 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
9633 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
9636 lang_append_dynamic_list (&link_info
.dynamic_list
, dynamic
);
9639 /* Append the list of C++ operator new and delete dynamic symbols to the
9643 lang_append_dynamic_list_cpp_new (void)
9645 const char *symbols
[] =
9650 struct bfd_elf_version_expr
*dynamic
= NULL
;
9653 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
9654 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
9657 lang_append_dynamic_list (&link_info
.dynamic_list
, dynamic
);
9660 /* Scan a space and/or comma separated string of features. */
9663 lang_ld_feature (char *str
)
9671 while (*p
== ',' || ISSPACE (*p
))
9676 while (*q
&& *q
!= ',' && !ISSPACE (*q
))
9680 if (strcasecmp (p
, "SANE_EXPR") == 0)
9681 config
.sane_expr
= true;
9683 einfo (_("%X%P: unknown feature `%s'\n"), p
);
9689 /* Pretty print memory amount. */
9692 lang_print_memory_size (uint64_t sz
)
9694 if ((sz
& 0x3fffffff) == 0)
9695 printf ("%10" PRIu64
" GB", sz
>> 30);
9696 else if ((sz
& 0xfffff) == 0)
9697 printf ("%10" PRIu64
" MB", sz
>> 20);
9698 else if ((sz
& 0x3ff) == 0)
9699 printf ("%10" PRIu64
" KB", sz
>> 10);
9701 printf (" %10" PRIu64
" B", sz
);
9704 /* Implement --print-memory-usage: disply per region memory usage. */
9707 lang_print_memory_usage (void)
9709 lang_memory_region_type
*r
;
9711 printf ("Memory region Used Size Region Size %%age Used\n");
9712 for (r
= lang_memory_region_list
; r
->next
!= NULL
; r
= r
->next
)
9714 bfd_vma used_length
= r
->current
- r
->origin
;
9716 printf ("%16s: ",r
->name_list
.name
);
9717 lang_print_memory_size (used_length
);
9718 lang_print_memory_size (r
->length
);
9722 double percent
= used_length
* 100.0 / r
->length
;
9723 printf (" %6.2f%%", percent
);