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 (compare_section (sec
->spec
.sorted
, section
, (*tree
)->section
) < 0)
653 tree
= &((*tree
)->left
);
655 tree
= &((*tree
)->right
);
661 /* Use wild_sort to build a BST to sort sections. */
664 output_section_callback_sort (lang_wild_statement_type
*ptr
,
665 struct wildcard_list
*sec
,
667 lang_input_statement_type
*file
,
670 lang_section_bst_type
*node
;
671 lang_section_bst_type
**tree
;
672 lang_output_section_statement_type
*os
;
674 os
= (lang_output_section_statement_type
*) output
;
676 if (unique_section_p (section
, os
))
679 node
= (lang_section_bst_type
*) xmalloc (sizeof (lang_section_bst_type
));
682 node
->section
= section
;
683 node
->pattern
= ptr
->section_list
;
685 tree
= wild_sort (ptr
, sec
, file
, section
);
689 if (tree
== ptr
->rightmost
)
690 ptr
->rightmost
= &node
->right
;
694 /* Convert a sorted sections' BST back to list form. */
697 output_section_callback_tree_to_list (lang_wild_statement_type
*ptr
,
698 lang_section_bst_type
*tree
,
702 output_section_callback_tree_to_list (ptr
, tree
->left
, output
);
704 lang_add_section (&ptr
->children
, tree
->section
, tree
->pattern
,
705 ptr
->section_flag_list
,
706 (lang_output_section_statement_type
*) output
);
709 output_section_callback_tree_to_list (ptr
, tree
->right
, output
);
715 /* Sections are matched against wildcard statements via a prefix tree.
716 The prefix tree holds prefixes of all matching patterns (up to the first
717 wildcard character), and the wild statement from which those patterns
718 came. When matching a section name against the tree we're walking through
719 the tree character by character. Each statement we hit is one that
720 potentially matches. This is checked by actually going through the
721 (glob) matching routines.
723 When the section name turns out to actually match we record that section
724 in the wild statements list of matching sections. */
726 /* A prefix can be matched by multiple statement, so we need a list of them. */
727 struct wild_stmt_list
729 lang_wild_statement_type
*stmt
;
730 struct wild_stmt_list
*next
;
733 /* The prefix tree itself. */
736 /* The list of all children (linked via .next). */
737 struct prefixtree
*child
;
738 struct prefixtree
*next
;
739 /* This tree node is responsible for the prefix of parent plus 'c'. */
741 /* The statements that potentially can match this prefix. */
742 struct wild_stmt_list
*stmt
;
745 /* We always have a root node in the prefix tree. It corresponds to the
746 empty prefix. E.g. a glob like "*" would sit in this root. */
747 static struct prefixtree the_root
, *ptroot
= &the_root
;
749 /* Given a prefix tree in *TREE, corresponding to prefix P, find or
750 INSERT the tree node corresponding to prefix P+C. */
752 static struct prefixtree
*
753 get_prefix_tree (struct prefixtree
**tree
, char c
, bool insert
)
755 struct prefixtree
*t
;
756 for (t
= *tree
; t
; t
= t
->next
)
761 t
= (struct prefixtree
*) obstack_alloc (&pt_obstack
, sizeof *t
);
770 /* Add STMT to the set of statements that can be matched by the prefix
771 corresponding to prefix tree T. */
774 pt_add_stmt (struct prefixtree
*t
, lang_wild_statement_type
*stmt
)
776 struct wild_stmt_list
*sl
, **psl
;
777 sl
= (struct wild_stmt_list
*) obstack_alloc (&pt_obstack
, sizeof *sl
);
786 /* Insert STMT into the global prefix tree. */
789 insert_prefix_tree (lang_wild_statement_type
*stmt
)
791 struct wildcard_list
*sec
;
792 struct prefixtree
*t
;
794 if (!stmt
->section_list
)
796 /* If we have no section_list (no wildcards in the wild STMT),
797 then every section name will match, so add this to the root. */
798 pt_add_stmt (ptroot
, stmt
);
802 for (sec
= stmt
->section_list
; sec
; sec
= sec
->next
)
804 const char *name
= sec
->spec
.name
? sec
->spec
.name
: "*";
807 for (; (c
= *name
); name
++)
809 if (c
== '*' || c
== '[' || c
== '?')
811 t
= get_prefix_tree (&t
->child
, c
, true);
813 /* If we hit a glob character, the matching prefix is what we saw
814 until now. If we hit the end of pattern (hence it's no glob) then
815 we can do better: we only need to record a match when a section name
816 completely matches, not merely a prefix, so record the trailing 0
819 t
= get_prefix_tree (&t
->child
, 0, true);
820 pt_add_stmt (t
, stmt
);
824 /* Dump T indented by INDENT spaces. */
827 debug_prefix_tree_rec (struct prefixtree
*t
, int indent
)
829 for (; t
; t
= t
->next
)
831 struct wild_stmt_list
*sl
;
832 printf ("%*s %c", indent
, "", t
->c
);
833 for (sl
= t
->stmt
; sl
; sl
= sl
->next
)
835 struct wildcard_list
*curr
;
836 printf (" %p ", sl
->stmt
);
837 for (curr
= sl
->stmt
->section_list
; curr
; curr
= curr
->next
)
838 printf ("%s ", curr
->spec
.name
? curr
->spec
.name
: "*");
841 debug_prefix_tree_rec (t
->child
, indent
+ 2);
845 /* Dump the global prefix tree. */
848 debug_prefix_tree (void)
850 debug_prefix_tree_rec (ptroot
, 2);
853 /* Like strcspn() but start to look from the end to beginning of
854 S. Returns the length of the suffix of S consisting entirely
855 of characters not in REJECT. */
858 rstrcspn (const char *s
, const char *reject
)
860 size_t len
= strlen (s
), sufflen
= 0;
864 if (strchr (reject
, c
) != 0)
871 /* Analyze the wildcards in wild statement PTR to setup various
872 things for quick matching. */
875 analyze_walk_wild_section_handler (lang_wild_statement_type
*ptr
)
877 struct wildcard_list
*sec
;
880 ptr
->rightmost
= &ptr
->tree
;
882 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
886 sec
->spec
.namelen
= strlen (sec
->spec
.name
);
887 sec
->spec
.prefixlen
= strcspn (sec
->spec
.name
, "?*[");
888 sec
->spec
.suffixlen
= rstrcspn (sec
->spec
.name
+ sec
->spec
.prefixlen
,
892 sec
->spec
.namelen
= sec
->spec
.prefixlen
= sec
->spec
.suffixlen
= 0;
895 insert_prefix_tree (ptr
);
898 /* Match all sections from FILE against the global prefix tree,
899 and record them into each wild statement that has a match. */
902 resolve_wild_sections (lang_input_statement_type
*file
)
906 if (file
->flags
.just_syms
)
909 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
911 const char *sname
= bfd_section_name (s
);
913 struct prefixtree
*t
= ptroot
;
914 //printf (" YYY consider %s of %s\n", sname, file->the_bfd->filename);
919 struct wild_stmt_list
*sl
;
920 for (sl
= t
->stmt
; sl
; sl
= sl
->next
)
922 walk_wild_section_match (sl
->stmt
, file
, s
);
923 //printf (" ZZZ maybe place into %p\n", sl->stmt);
929 t
= get_prefix_tree (&t
->child
, c
, false);
935 /* Match all sections from all input files against the global prefix tree. */
940 LANG_FOR_EACH_INPUT_STATEMENT (f
)
942 //printf("XXX %s\n", f->filename);
943 if (f
->the_bfd
== NULL
944 || !bfd_check_format (f
->the_bfd
, bfd_archive
))
945 resolve_wild_sections (f
);
950 /* This is an archive file. We must map each member of the
951 archive separately. */
952 member
= bfd_openr_next_archived_file (f
->the_bfd
, NULL
);
953 while (member
!= NULL
)
955 /* When lookup_name is called, it will call the add_symbols
956 entry point for the archive. For each element of the
957 archive which is included, BFD will call ldlang_add_file,
958 which will set the usrdata field of the member to the
959 lang_input_statement. */
960 if (bfd_usrdata (member
) != NULL
)
961 resolve_wild_sections (bfd_usrdata (member
));
963 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
969 /* For each input section that matches wild statement S calls
970 CALLBACK with DATA. */
973 walk_wild (lang_wild_statement_type
*s
, callback_t callback
, void *data
)
975 lang_statement_union_type
*l
;
977 for (l
= s
->matching_sections
.head
; l
; l
= l
->header
.next
)
979 (*callback
) (s
, l
->input_matcher
.pattern
, l
->input_matcher
.section
,
980 l
->input_matcher
.input_stmt
, data
);
984 /* lang_for_each_statement walks the parse tree and calls the provided
985 function for each node, except those inside output section statements
986 with constraint set to -1. */
989 lang_for_each_statement_worker (void (*func
) (lang_statement_union_type
*),
990 lang_statement_union_type
*s
)
992 for (; s
!= NULL
; s
= s
->header
.next
)
996 switch (s
->header
.type
)
998 case lang_constructors_statement_enum
:
999 lang_for_each_statement_worker (func
, constructor_list
.head
);
1001 case lang_output_section_statement_enum
:
1002 if (s
->output_section_statement
.constraint
!= -1)
1003 lang_for_each_statement_worker
1004 (func
, s
->output_section_statement
.children
.head
);
1006 case lang_wild_statement_enum
:
1007 lang_for_each_statement_worker (func
,
1008 s
->wild_statement
.children
.head
);
1010 case lang_group_statement_enum
:
1011 lang_for_each_statement_worker (func
,
1012 s
->group_statement
.children
.head
);
1014 case lang_data_statement_enum
:
1015 case lang_reloc_statement_enum
:
1016 case lang_object_symbols_statement_enum
:
1017 case lang_output_statement_enum
:
1018 case lang_target_statement_enum
:
1019 case lang_input_section_enum
:
1020 case lang_input_statement_enum
:
1021 case lang_assignment_statement_enum
:
1022 case lang_padding_statement_enum
:
1023 case lang_address_statement_enum
:
1024 case lang_fill_statement_enum
:
1025 case lang_insert_statement_enum
:
1035 lang_for_each_statement (void (*func
) (lang_statement_union_type
*))
1037 lang_for_each_statement_worker (func
, statement_list
.head
);
1040 /*----------------------------------------------------------------------*/
1043 lang_list_init (lang_statement_list_type
*list
)
1046 list
->tail
= &list
->head
;
1050 lang_statement_append (lang_statement_list_type
*list
,
1054 *(list
->tail
) = element
;
1059 push_stat_ptr (lang_statement_list_type
*new_ptr
)
1061 if (stat_save_ptr
>= stat_save
+ sizeof (stat_save
) / sizeof (stat_save
[0]))
1063 *stat_save_ptr
++ = stat_ptr
;
1070 if (stat_save_ptr
<= stat_save
)
1072 stat_ptr
= *--stat_save_ptr
;
1075 /* Build a new statement node for the parse tree. */
1077 static lang_statement_union_type
*
1078 new_statement (enum statement_enum type
,
1080 lang_statement_list_type
*list
)
1082 lang_statement_union_type
*new_stmt
;
1084 new_stmt
= stat_alloc (size
);
1085 new_stmt
->header
.type
= type
;
1086 new_stmt
->header
.next
= NULL
;
1087 lang_statement_append (list
, new_stmt
, &new_stmt
->header
.next
);
1091 /* Build a new input file node for the language. There are several
1092 ways in which we treat an input file, eg, we only look at symbols,
1093 or prefix it with a -l etc.
1095 We can be supplied with requests for input files more than once;
1096 they may, for example be split over several lines like foo.o(.text)
1097 foo.o(.data) etc, so when asked for a file we check that we haven't
1098 got it already so we don't duplicate the bfd. */
1100 static lang_input_statement_type
*
1101 new_afile (const char *name
,
1102 lang_input_file_enum_type file_type
,
1104 const char *from_filename
)
1106 lang_input_statement_type
*p
;
1108 lang_has_input_file
= true;
1110 p
= new_stat (lang_input_statement
, stat_ptr
);
1111 memset (&p
->the_bfd
, 0,
1112 sizeof (*p
) - offsetof (lang_input_statement_type
, the_bfd
));
1113 p
->extra_search_path
= NULL
;
1115 p
->flags
.dynamic
= input_flags
.dynamic
;
1116 p
->flags
.add_DT_NEEDED_for_dynamic
= input_flags
.add_DT_NEEDED_for_dynamic
;
1117 p
->flags
.add_DT_NEEDED_for_regular
= input_flags
.add_DT_NEEDED_for_regular
;
1118 p
->flags
.whole_archive
= input_flags
.whole_archive
;
1119 p
->flags
.sysrooted
= input_flags
.sysrooted
;
1124 case lang_input_file_is_symbols_only_enum
:
1126 p
->local_sym_name
= name
;
1127 p
->flags
.real
= true;
1128 p
->flags
.just_syms
= true;
1130 case lang_input_file_is_fake_enum
:
1132 p
->local_sym_name
= name
;
1134 case lang_input_file_is_l_enum
:
1135 if (name
[0] == ':' && name
[1] != '\0')
1137 p
->filename
= name
+ 1;
1138 p
->flags
.full_name_provided
= true;
1142 p
->local_sym_name
= concat ("-l", name
, (const char *) NULL
);
1143 p
->flags
.maybe_archive
= true;
1144 p
->flags
.real
= true;
1145 p
->flags
.search_dirs
= true;
1147 case lang_input_file_is_marker_enum
:
1149 p
->local_sym_name
= name
;
1150 p
->flags
.search_dirs
= true;
1152 case lang_input_file_is_search_file_enum
:
1154 p
->local_sym_name
= name
;
1155 /* If name is a relative path, search the directory of the current linker
1157 if (from_filename
&& !IS_ABSOLUTE_PATH (name
))
1158 p
->extra_search_path
= ldirname (from_filename
);
1159 p
->flags
.real
= true;
1160 p
->flags
.search_dirs
= true;
1162 case lang_input_file_is_file_enum
:
1164 p
->local_sym_name
= name
;
1165 p
->flags
.real
= true;
1171 lang_statement_append (&input_file_chain
, p
, &p
->next_real_file
);
1175 lang_input_statement_type
*
1176 lang_add_input_file (const char *name
,
1177 lang_input_file_enum_type file_type
,
1181 && (*name
== '=' || startswith (name
, "$SYSROOT")))
1183 lang_input_statement_type
*ret
;
1184 char *sysrooted_name
1185 = concat (ld_sysroot
,
1186 name
+ (*name
== '=' ? 1 : strlen ("$SYSROOT")),
1187 (const char *) NULL
);
1189 /* We've now forcibly prepended the sysroot, making the input
1190 file independent of the context. Therefore, temporarily
1191 force a non-sysrooted context for this statement, so it won't
1192 get the sysroot prepended again when opened. (N.B. if it's a
1193 script, any child nodes with input files starting with "/"
1194 will be handled as "sysrooted" as they'll be found to be
1195 within the sysroot subdirectory.) */
1196 unsigned int outer_sysrooted
= input_flags
.sysrooted
;
1197 input_flags
.sysrooted
= 0;
1198 ret
= new_afile (sysrooted_name
, file_type
, target
, NULL
);
1199 input_flags
.sysrooted
= outer_sysrooted
;
1203 return new_afile (name
, file_type
, target
, current_input_file
);
1206 struct out_section_hash_entry
1208 struct bfd_hash_entry root
;
1209 lang_statement_union_type s
;
1212 /* The hash table. */
1214 static struct bfd_hash_table output_section_statement_table
;
1216 /* Support routines for the hash table used by lang_output_section_find,
1217 initialize the table, fill in an entry and remove the table. */
1219 static struct bfd_hash_entry
*
1220 output_section_statement_newfunc (struct bfd_hash_entry
*entry
,
1221 struct bfd_hash_table
*table
,
1224 lang_output_section_statement_type
**nextp
;
1225 struct out_section_hash_entry
*ret
;
1229 entry
= (struct bfd_hash_entry
*) bfd_hash_allocate (table
,
1235 entry
= bfd_hash_newfunc (entry
, table
, string
);
1239 ret
= (struct out_section_hash_entry
*) entry
;
1240 memset (&ret
->s
, 0, sizeof (ret
->s
));
1241 ret
->s
.header
.type
= lang_output_section_statement_enum
;
1242 ret
->s
.output_section_statement
.subsection_alignment
= NULL
;
1243 ret
->s
.output_section_statement
.section_alignment
= NULL
;
1244 ret
->s
.output_section_statement
.block_value
= 1;
1245 lang_list_init (&ret
->s
.output_section_statement
.children
);
1246 lang_statement_append (stat_ptr
, &ret
->s
, &ret
->s
.header
.next
);
1248 /* For every output section statement added to the list, except the
1249 first one, lang_os_list.tail points to the "next"
1250 field of the last element of the list. */
1251 if (lang_os_list
.head
!= NULL
)
1252 ret
->s
.output_section_statement
.prev
1253 = ((lang_output_section_statement_type
*)
1254 ((char *) lang_os_list
.tail
1255 - offsetof (lang_output_section_statement_type
, next
)));
1257 /* GCC's strict aliasing rules prevent us from just casting the
1258 address, so we store the pointer in a variable and cast that
1260 nextp
= &ret
->s
.output_section_statement
.next
;
1261 lang_statement_append (&lang_os_list
, &ret
->s
, nextp
);
1266 output_section_statement_table_init (void)
1268 if (!bfd_hash_table_init_n (&output_section_statement_table
,
1269 output_section_statement_newfunc
,
1270 sizeof (struct out_section_hash_entry
),
1272 einfo (_("%F%P: can not create hash table: %E\n"));
1276 output_section_statement_table_free (void)
1278 bfd_hash_table_free (&output_section_statement_table
);
1281 /* Build enough state so that the parser can build its tree. */
1286 obstack_begin (&stat_obstack
, 1000);
1287 obstack_init (&pt_obstack
);
1289 stat_ptr
= &statement_list
;
1291 output_section_statement_table_init ();
1293 lang_list_init (stat_ptr
);
1295 lang_list_init (&input_file_chain
);
1296 lang_list_init (&lang_os_list
);
1297 lang_list_init (&file_chain
);
1298 first_file
= lang_add_input_file (NULL
, lang_input_file_is_marker_enum
,
1300 abs_output_section
=
1301 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
, 0, 1);
1303 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
1305 asneeded_list_head
= NULL
;
1306 asneeded_list_tail
= &asneeded_list_head
;
1312 output_section_statement_table_free ();
1315 /*----------------------------------------------------------------------
1316 A region is an area of memory declared with the
1317 MEMORY { name:org=exp, len=exp ... }
1320 We maintain a list of all the regions here.
1322 If no regions are specified in the script, then the default is used
1323 which is created when looked up to be the entire data space.
1325 If create is true we are creating a region inside a MEMORY block.
1326 In this case it is probably an error to create a region that has
1327 already been created. If we are not inside a MEMORY block it is
1328 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
1329 and so we issue a warning.
1331 Each region has at least one name. The first name is either
1332 DEFAULT_MEMORY_REGION or the name given in the MEMORY block. You can add
1333 alias names to an existing region within a script with
1334 REGION_ALIAS (alias, region_name). Each name corresponds to at most one
1337 static lang_memory_region_type
*lang_memory_region_list
;
1338 static lang_memory_region_type
**lang_memory_region_list_tail
1339 = &lang_memory_region_list
;
1341 lang_memory_region_type
*
1342 lang_memory_region_lookup (const char *const name
, bool create
)
1344 lang_memory_region_name
*n
;
1345 lang_memory_region_type
*r
;
1346 lang_memory_region_type
*new_region
;
1348 /* NAME is NULL for LMA memspecs if no region was specified. */
1352 for (r
= lang_memory_region_list
; r
!= NULL
; r
= r
->next
)
1353 for (n
= &r
->name_list
; n
!= NULL
; n
= n
->next
)
1354 if (strcmp (n
->name
, name
) == 0)
1357 einfo (_("%P:%pS: warning: redeclaration of memory region `%s'\n"),
1362 if (!create
&& strcmp (name
, DEFAULT_MEMORY_REGION
))
1363 einfo (_("%P:%pS: warning: memory region `%s' not declared\n"),
1366 new_region
= stat_alloc (sizeof (lang_memory_region_type
));
1368 new_region
->name_list
.name
= xstrdup (name
);
1369 new_region
->name_list
.next
= NULL
;
1370 new_region
->next
= NULL
;
1371 new_region
->origin_exp
= NULL
;
1372 new_region
->origin
= 0;
1373 new_region
->length_exp
= NULL
;
1374 new_region
->length
= ~(bfd_size_type
) 0;
1375 new_region
->current
= 0;
1376 new_region
->last_os
= NULL
;
1377 new_region
->flags
= 0;
1378 new_region
->not_flags
= 0;
1379 new_region
->had_full_message
= false;
1381 *lang_memory_region_list_tail
= new_region
;
1382 lang_memory_region_list_tail
= &new_region
->next
;
1388 lang_memory_region_alias (const char *alias
, const char *region_name
)
1390 lang_memory_region_name
*n
;
1391 lang_memory_region_type
*r
;
1392 lang_memory_region_type
*region
;
1394 /* The default region must be unique. This ensures that it is not necessary
1395 to iterate through the name list if someone wants the check if a region is
1396 the default memory region. */
1397 if (strcmp (region_name
, DEFAULT_MEMORY_REGION
) == 0
1398 || strcmp (alias
, DEFAULT_MEMORY_REGION
) == 0)
1399 einfo (_("%F%P:%pS: error: alias for default memory region\n"), NULL
);
1401 /* Look for the target region and check if the alias is not already
1404 for (r
= lang_memory_region_list
; r
!= NULL
; r
= r
->next
)
1405 for (n
= &r
->name_list
; n
!= NULL
; n
= n
->next
)
1407 if (region
== NULL
&& strcmp (n
->name
, region_name
) == 0)
1409 if (strcmp (n
->name
, alias
) == 0)
1410 einfo (_("%F%P:%pS: error: redefinition of memory region "
1415 /* Check if the target region exists. */
1417 einfo (_("%F%P:%pS: error: memory region `%s' "
1418 "for alias `%s' does not exist\n"),
1419 NULL
, region_name
, alias
);
1421 /* Add alias to region name list. */
1422 n
= stat_alloc (sizeof (lang_memory_region_name
));
1423 n
->name
= xstrdup (alias
);
1424 n
->next
= region
->name_list
.next
;
1425 region
->name_list
.next
= n
;
1428 static lang_memory_region_type
*
1429 lang_memory_default (asection
*section
)
1431 lang_memory_region_type
*p
;
1433 flagword sec_flags
= section
->flags
;
1435 /* Override SEC_DATA to mean a writable section. */
1436 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
1437 sec_flags
|= SEC_DATA
;
1439 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
1441 if ((p
->flags
& sec_flags
) != 0
1442 && (p
->not_flags
& sec_flags
) == 0)
1447 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, false);
1450 /* Get the output section statement directly from the userdata. */
1452 lang_output_section_statement_type
*
1453 lang_output_section_get (const asection
*output_section
)
1455 return bfd_section_userdata (output_section
);
1458 /* Find or create an output_section_statement with the given NAME.
1459 If CONSTRAINT is non-zero match one with that constraint, otherwise
1460 match any non-negative constraint. If CREATE is 0 return NULL when
1461 no match exists. If CREATE is 1, create an output_section_statement
1462 when no match exists or if CONSTRAINT is SPECIAL. If CREATE is 2,
1463 always make a new output_section_statement. */
1465 lang_output_section_statement_type
*
1466 lang_output_section_statement_lookup (const char *name
,
1470 struct out_section_hash_entry
*entry
;
1472 entry
= ((struct out_section_hash_entry
*)
1473 bfd_hash_lookup (&output_section_statement_table
, name
,
1474 create
!= 0, false));
1478 einfo (_("%F%P: failed creating section `%s': %E\n"), name
);
1482 if (entry
->s
.output_section_statement
.name
!= NULL
)
1484 /* We have a section of this name, but it might not have the correct
1486 struct out_section_hash_entry
*last_ent
;
1488 name
= entry
->s
.output_section_statement
.name
;
1492 && !(create
&& constraint
== SPECIAL
)
1493 && (constraint
== entry
->s
.output_section_statement
.constraint
1495 && entry
->s
.output_section_statement
.constraint
>= 0)))
1496 return &entry
->s
.output_section_statement
;
1498 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1500 while (entry
!= NULL
1501 && name
== entry
->s
.output_section_statement
.name
);
1507 = ((struct out_section_hash_entry
*)
1508 output_section_statement_newfunc (NULL
,
1509 &output_section_statement_table
,
1513 einfo (_("%F%P: failed creating section `%s': %E\n"), name
);
1516 entry
->root
= last_ent
->root
;
1517 last_ent
->root
.next
= &entry
->root
;
1520 entry
->s
.output_section_statement
.name
= name
;
1521 entry
->s
.output_section_statement
.constraint
= constraint
;
1522 entry
->s
.output_section_statement
.dup_output
= (create
== 2
1523 || constraint
== SPECIAL
);
1524 return &entry
->s
.output_section_statement
;
1527 /* Find the next output_section_statement with the same name as OS.
1528 If CONSTRAINT is non-zero, find one with that constraint otherwise
1529 match any non-negative constraint. */
1531 lang_output_section_statement_type
*
1532 next_matching_output_section_statement (lang_output_section_statement_type
*os
,
1535 /* All output_section_statements are actually part of a
1536 struct out_section_hash_entry. */
1537 struct out_section_hash_entry
*entry
= (struct out_section_hash_entry
*)
1539 - offsetof (struct out_section_hash_entry
, s
.output_section_statement
));
1540 const char *name
= os
->name
;
1542 ASSERT (name
== entry
->root
.string
);
1545 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1547 || name
!= entry
->s
.output_section_statement
.name
)
1550 while (constraint
!= entry
->s
.output_section_statement
.constraint
1552 || entry
->s
.output_section_statement
.constraint
< 0));
1554 return &entry
->s
.output_section_statement
;
1557 /* A variant of lang_output_section_find used by place_orphan.
1558 Returns the output statement that should precede a new output
1559 statement for SEC. If an exact match is found on certain flags,
1562 lang_output_section_statement_type
*
1563 lang_output_section_find_by_flags (const asection
*sec
,
1565 lang_output_section_statement_type
**exact
,
1566 lang_match_sec_type_func match_type
)
1568 lang_output_section_statement_type
*first
, *look
, *found
;
1569 flagword look_flags
, differ
;
1571 /* We know the first statement on this list is *ABS*. May as well
1573 first
= (void *) lang_os_list
.head
;
1574 first
= first
->next
;
1576 /* First try for an exact match. */
1578 for (look
= first
; look
; look
= look
->next
)
1580 look_flags
= look
->flags
;
1581 if (look
->bfd_section
!= NULL
)
1583 look_flags
= look
->bfd_section
->flags
;
1584 if (match_type
&& !match_type (link_info
.output_bfd
,
1589 differ
= look_flags
^ sec_flags
;
1590 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
1591 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1601 if ((sec_flags
& SEC_CODE
) != 0
1602 && (sec_flags
& SEC_ALLOC
) != 0)
1604 /* Try for a rw code section. */
1605 for (look
= first
; look
; look
= look
->next
)
1607 look_flags
= look
->flags
;
1608 if (look
->bfd_section
!= NULL
)
1610 look_flags
= look
->bfd_section
->flags
;
1611 if (match_type
&& !match_type (link_info
.output_bfd
,
1616 differ
= look_flags
^ sec_flags
;
1617 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1618 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1622 else if ((sec_flags
& SEC_READONLY
) != 0
1623 && (sec_flags
& SEC_ALLOC
) != 0)
1625 /* .rodata can go after .text, .sdata2 after .rodata. */
1626 for (look
= first
; look
; look
= look
->next
)
1628 look_flags
= look
->flags
;
1629 if (look
->bfd_section
!= NULL
)
1631 look_flags
= look
->bfd_section
->flags
;
1632 if (match_type
&& !match_type (link_info
.output_bfd
,
1637 differ
= look_flags
^ sec_flags
;
1638 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1639 | SEC_READONLY
| SEC_SMALL_DATA
))
1640 || (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1642 && !(look_flags
& SEC_SMALL_DATA
)))
1646 else if ((sec_flags
& SEC_THREAD_LOCAL
) != 0
1647 && (sec_flags
& SEC_ALLOC
) != 0)
1649 /* .tdata can go after .data, .tbss after .tdata. Treat .tbss
1650 as if it were a loaded section, and don't use match_type. */
1651 bool seen_thread_local
= false;
1654 for (look
= first
; look
; look
= look
->next
)
1656 look_flags
= look
->flags
;
1657 if (look
->bfd_section
!= NULL
)
1658 look_flags
= look
->bfd_section
->flags
;
1660 differ
= look_flags
^ (sec_flags
| SEC_LOAD
| SEC_HAS_CONTENTS
);
1661 if (!(differ
& (SEC_THREAD_LOCAL
| SEC_ALLOC
)))
1663 /* .tdata and .tbss must be adjacent and in that order. */
1664 if (!(look_flags
& SEC_LOAD
)
1665 && (sec_flags
& SEC_LOAD
))
1666 /* ..so if we're at a .tbss section and we're placing
1667 a .tdata section stop looking and return the
1668 previous section. */
1671 seen_thread_local
= true;
1673 else if (seen_thread_local
)
1675 else if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
)))
1679 else if ((sec_flags
& SEC_SMALL_DATA
) != 0
1680 && (sec_flags
& SEC_ALLOC
) != 0)
1682 /* .sdata goes after .data, .sbss after .sdata. */
1683 for (look
= first
; look
; look
= look
->next
)
1685 look_flags
= look
->flags
;
1686 if (look
->bfd_section
!= NULL
)
1688 look_flags
= look
->bfd_section
->flags
;
1689 if (match_type
&& !match_type (link_info
.output_bfd
,
1694 differ
= look_flags
^ sec_flags
;
1695 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1696 | SEC_THREAD_LOCAL
))
1697 || ((look_flags
& SEC_SMALL_DATA
)
1698 && !(sec_flags
& SEC_HAS_CONTENTS
)))
1702 else if ((sec_flags
& SEC_HAS_CONTENTS
) != 0
1703 && (sec_flags
& SEC_ALLOC
) != 0)
1705 /* .data goes after .rodata. */
1706 for (look
= first
; look
; look
= look
->next
)
1708 look_flags
= look
->flags
;
1709 if (look
->bfd_section
!= NULL
)
1711 look_flags
= look
->bfd_section
->flags
;
1712 if (match_type
&& !match_type (link_info
.output_bfd
,
1717 differ
= look_flags
^ sec_flags
;
1718 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1719 | SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1723 else if ((sec_flags
& SEC_ALLOC
) != 0)
1725 /* .bss goes after any other alloc section. */
1726 for (look
= first
; look
; look
= look
->next
)
1728 look_flags
= look
->flags
;
1729 if (look
->bfd_section
!= NULL
)
1731 look_flags
= look
->bfd_section
->flags
;
1732 if (match_type
&& !match_type (link_info
.output_bfd
,
1737 differ
= look_flags
^ sec_flags
;
1738 if (!(differ
& SEC_ALLOC
))
1744 /* non-alloc go last. */
1745 for (look
= first
; look
; look
= look
->next
)
1747 look_flags
= look
->flags
;
1748 if (look
->bfd_section
!= NULL
)
1749 look_flags
= look
->bfd_section
->flags
;
1750 differ
= look_flags
^ sec_flags
;
1751 if (!(differ
& SEC_DEBUGGING
))
1757 if (found
|| !match_type
)
1760 return lang_output_section_find_by_flags (sec
, sec_flags
, NULL
, NULL
);
1763 /* Find the last output section before given output statement.
1764 Used by place_orphan. */
1767 output_prev_sec_find (lang_output_section_statement_type
*os
)
1769 lang_output_section_statement_type
*lookup
;
1771 for (lookup
= os
->prev
; lookup
!= NULL
; lookup
= lookup
->prev
)
1773 if (lookup
->constraint
< 0)
1776 if (lookup
->bfd_section
!= NULL
&& lookup
->bfd_section
->owner
!= NULL
)
1777 return lookup
->bfd_section
;
1783 /* Look for a suitable place for a new output section statement. The
1784 idea is to skip over anything that might be inside a SECTIONS {}
1785 statement in a script, before we find another output section
1786 statement. Assignments to "dot" before an output section statement
1787 are assumed to belong to it, except in two cases; The first
1788 assignment to dot, and assignments before non-alloc sections.
1789 Otherwise we might put an orphan before . = . + SIZEOF_HEADERS or
1790 similar assignments that set the initial address, or we might
1791 insert non-alloc note sections among assignments setting end of
1794 static lang_statement_union_type
**
1795 insert_os_after (lang_output_section_statement_type
*after
)
1797 lang_statement_union_type
**where
;
1798 lang_statement_union_type
**assign
= NULL
;
1801 ignore_first
= after
== (void *) lang_os_list
.head
;
1803 for (where
= &after
->header
.next
;
1805 where
= &(*where
)->header
.next
)
1807 switch ((*where
)->header
.type
)
1809 case lang_assignment_statement_enum
:
1812 lang_assignment_statement_type
*ass
;
1814 ass
= &(*where
)->assignment_statement
;
1815 if (ass
->exp
->type
.node_class
!= etree_assert
1816 && ass
->exp
->assign
.dst
[0] == '.'
1817 && ass
->exp
->assign
.dst
[1] == 0)
1821 ignore_first
= false;
1825 case lang_wild_statement_enum
:
1826 case lang_input_section_enum
:
1827 case lang_object_symbols_statement_enum
:
1828 case lang_fill_statement_enum
:
1829 case lang_data_statement_enum
:
1830 case lang_reloc_statement_enum
:
1831 case lang_padding_statement_enum
:
1832 case lang_constructors_statement_enum
:
1834 ignore_first
= false;
1836 case lang_output_section_statement_enum
:
1839 asection
*s
= (*where
)->output_section_statement
.bfd_section
;
1842 || s
->map_head
.s
== NULL
1843 || (s
->flags
& SEC_ALLOC
) != 0)
1847 case lang_input_statement_enum
:
1848 case lang_address_statement_enum
:
1849 case lang_target_statement_enum
:
1850 case lang_output_statement_enum
:
1851 case lang_group_statement_enum
:
1852 case lang_insert_statement_enum
:
1854 case lang_input_matcher_enum
:
1863 lang_output_section_statement_type
*
1864 lang_insert_orphan (asection
*s
,
1865 const char *secname
,
1867 lang_output_section_statement_type
*after
,
1868 struct orphan_save
*place
,
1869 etree_type
*address
,
1870 lang_statement_list_type
*add_child
)
1872 lang_statement_list_type add
;
1873 lang_output_section_statement_type
*os
;
1874 lang_output_section_statement_type
**os_tail
;
1876 /* If we have found an appropriate place for the output section
1877 statements for this orphan, add them to our own private list,
1878 inserting them later into the global statement list. */
1881 lang_list_init (&add
);
1882 push_stat_ptr (&add
);
1885 if (bfd_link_relocatable (&link_info
)
1886 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0)
1887 address
= exp_intop (0);
1889 os_tail
= (lang_output_section_statement_type
**) lang_os_list
.tail
;
1890 os
= lang_enter_output_section_statement (
1891 secname
, address
, normal_section
, 0, NULL
, NULL
, NULL
, constraint
, 0);
1893 if (add_child
== NULL
)
1894 add_child
= &os
->children
;
1895 lang_add_section (add_child
, s
, NULL
, NULL
, os
);
1897 if (after
&& (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) != 0)
1899 const char *region
= (after
->region
1900 ? after
->region
->name_list
.name
1901 : DEFAULT_MEMORY_REGION
);
1902 const char *lma_region
= (after
->lma_region
1903 ? after
->lma_region
->name_list
.name
1905 lang_leave_output_section_statement (NULL
, region
, after
->phdrs
,
1909 lang_leave_output_section_statement (NULL
, DEFAULT_MEMORY_REGION
, NULL
,
1912 /* Restore the global list pointer. */
1916 if (after
!= NULL
&& os
->bfd_section
!= NULL
)
1918 asection
*snew
, *as
;
1919 bool place_after
= place
->stmt
== NULL
;
1920 bool insert_after
= true;
1922 snew
= os
->bfd_section
;
1924 /* Shuffle the bfd section list to make the output file look
1925 neater. This is really only cosmetic. */
1926 if (place
->section
== NULL
1927 && after
!= (void *) lang_os_list
.head
)
1929 asection
*bfd_section
= after
->bfd_section
;
1931 /* If the output statement hasn't been used to place any input
1932 sections (and thus doesn't have an output bfd_section),
1933 look for the closest prior output statement having an
1935 if (bfd_section
== NULL
)
1936 bfd_section
= output_prev_sec_find (after
);
1938 if (bfd_section
!= NULL
&& bfd_section
!= snew
)
1939 place
->section
= &bfd_section
->next
;
1942 if (place
->section
== NULL
)
1943 place
->section
= &link_info
.output_bfd
->sections
;
1945 as
= *place
->section
;
1949 /* Put the section at the end of the list. */
1951 /* Unlink the section. */
1952 bfd_section_list_remove (link_info
.output_bfd
, snew
);
1954 /* Now tack it back on in the right place. */
1955 bfd_section_list_append (link_info
.output_bfd
, snew
);
1957 else if ((bfd_get_flavour (link_info
.output_bfd
)
1958 == bfd_target_elf_flavour
)
1959 && (bfd_get_flavour (s
->owner
)
1960 == bfd_target_elf_flavour
)
1961 && ((elf_section_type (s
) == SHT_NOTE
1962 && (s
->flags
& SEC_LOAD
) != 0)
1963 || (elf_section_type (as
) == SHT_NOTE
1964 && (as
->flags
& SEC_LOAD
) != 0)))
1966 /* Make sure that output note sections are grouped and sorted
1967 by alignments when inserting a note section or insert a
1968 section after a note section, */
1970 /* A specific section after which the output note section
1971 should be placed. */
1972 asection
*after_sec
;
1973 /* True if we need to insert the orphan section after a
1974 specific section to maintain output note section order. */
1975 bool after_sec_note
= false;
1977 static asection
*first_orphan_note
= NULL
;
1979 /* Group and sort output note section by alignments in
1982 if (elf_section_type (s
) == SHT_NOTE
1983 && (s
->flags
& SEC_LOAD
) != 0)
1985 /* Search from the beginning for the last output note
1986 section with equal or larger alignments. NB: Don't
1987 place orphan note section after non-note sections. */
1989 first_orphan_note
= NULL
;
1990 for (sec
= link_info
.output_bfd
->sections
;
1992 && !bfd_is_abs_section (sec
));
1995 && elf_section_type (sec
) == SHT_NOTE
1996 && (sec
->flags
& SEC_LOAD
) != 0)
1998 if (!first_orphan_note
)
1999 first_orphan_note
= sec
;
2000 if (sec
->alignment_power
>= s
->alignment_power
)
2003 else if (first_orphan_note
)
2005 /* Stop if there is non-note section after the first
2006 orphan note section. */
2010 /* If this will be the first orphan note section, it can
2011 be placed at the default location. */
2012 after_sec_note
= first_orphan_note
!= NULL
;
2013 if (after_sec
== NULL
&& after_sec_note
)
2015 /* If all output note sections have smaller
2016 alignments, place the section before all
2017 output orphan note sections. */
2018 after_sec
= first_orphan_note
;
2019 insert_after
= false;
2022 else if (first_orphan_note
)
2024 /* Don't place non-note sections in the middle of orphan
2026 after_sec_note
= true;
2028 for (sec
= as
->next
;
2030 && !bfd_is_abs_section (sec
));
2032 if (elf_section_type (sec
) == SHT_NOTE
2033 && (sec
->flags
& SEC_LOAD
) != 0)
2041 /* Search forward to insert OS after AFTER_SEC output
2043 lang_output_section_statement_type
*stmt
, *next
;
2045 for (stmt
= after
; stmt
!= NULL
; stmt
= next
)
2050 if (stmt
->bfd_section
== after_sec
)
2060 /* If INSERT_AFTER is FALSE, place OS before
2061 AFTER_SEC output statement. */
2062 if (next
&& next
->bfd_section
== after_sec
)
2072 /* Search backward to insert OS after AFTER_SEC output
2075 for (stmt
= after
; stmt
!= NULL
; stmt
= stmt
->prev
)
2079 if (stmt
->bfd_section
== after_sec
)
2088 /* If INSERT_AFTER is FALSE, place OS before
2089 AFTER_SEC output statement. */
2090 if (stmt
->next
->bfd_section
== after_sec
)
2100 if (after_sec
== NULL
2101 || (insert_after
&& after_sec
->next
!= snew
)
2102 || (!insert_after
&& after_sec
->prev
!= snew
))
2104 /* Unlink the section. */
2105 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2107 /* Place SNEW after AFTER_SEC. If AFTER_SEC is NULL,
2112 bfd_section_list_insert_after (link_info
.output_bfd
,
2115 bfd_section_list_insert_before (link_info
.output_bfd
,
2119 bfd_section_list_prepend (link_info
.output_bfd
, snew
);
2122 else if (as
!= snew
&& as
->prev
!= snew
)
2124 /* Unlink the section. */
2125 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2127 /* Now tack it back on in the right place. */
2128 bfd_section_list_insert_before (link_info
.output_bfd
,
2132 else if (as
!= snew
&& as
->prev
!= snew
)
2134 /* Unlink the section. */
2135 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2137 /* Now tack it back on in the right place. */
2138 bfd_section_list_insert_before (link_info
.output_bfd
, as
, snew
);
2141 /* Save the end of this list. Further ophans of this type will
2142 follow the one we've just added. */
2143 place
->section
= &snew
->next
;
2145 /* The following is non-cosmetic. We try to put the output
2146 statements in some sort of reasonable order here, because they
2147 determine the final load addresses of the orphan sections.
2148 In addition, placing output statements in the wrong order may
2149 require extra segments. For instance, given a typical
2150 situation of all read-only sections placed in one segment and
2151 following that a segment containing all the read-write
2152 sections, we wouldn't want to place an orphan read/write
2153 section before or amongst the read-only ones. */
2154 if (add
.head
!= NULL
)
2156 lang_output_section_statement_type
*newly_added_os
;
2158 /* Place OS after AFTER if AFTER_NOTE is TRUE. */
2161 lang_statement_union_type
**where
= insert_os_after (after
);
2166 place
->os_tail
= &after
->next
;
2170 /* Put it after the last orphan statement we added. */
2171 *add
.tail
= *place
->stmt
;
2172 *place
->stmt
= add
.head
;
2175 /* Fix the global list pointer if we happened to tack our
2176 new list at the tail. */
2177 if (*stat_ptr
->tail
== add
.head
)
2178 stat_ptr
->tail
= add
.tail
;
2180 /* Save the end of this list. */
2181 place
->stmt
= add
.tail
;
2183 /* Do the same for the list of output section statements. */
2184 newly_added_os
= *os_tail
;
2186 newly_added_os
->prev
= (lang_output_section_statement_type
*)
2187 ((char *) place
->os_tail
2188 - offsetof (lang_output_section_statement_type
, next
));
2189 newly_added_os
->next
= *place
->os_tail
;
2190 if (newly_added_os
->next
!= NULL
)
2191 newly_added_os
->next
->prev
= newly_added_os
;
2192 *place
->os_tail
= newly_added_os
;
2193 place
->os_tail
= &newly_added_os
->next
;
2195 /* Fixing the global list pointer here is a little different.
2196 We added to the list in lang_enter_output_section_statement,
2197 trimmed off the new output_section_statment above when
2198 assigning *os_tail = NULL, but possibly added it back in
2199 the same place when assigning *place->os_tail. */
2200 if (*os_tail
== NULL
)
2201 lang_os_list
.tail
= (lang_statement_union_type
**) os_tail
;
2208 lang_print_asneeded (void)
2210 struct asneeded_minfo
*m
;
2212 if (asneeded_list_head
== NULL
)
2215 minfo (_("\nAs-needed library included to satisfy reference by file (symbol)\n\n"));
2217 for (m
= asneeded_list_head
; m
!= NULL
; m
= m
->next
)
2221 minfo ("%s", m
->soname
);
2222 len
= strlen (m
->soname
);
2229 print_spaces (30 - len
);
2232 minfo ("%pB ", m
->ref
);
2233 minfo ("(%pT)\n", m
->name
);
2238 lang_map_flags (flagword flag
)
2240 if (flag
& SEC_ALLOC
)
2243 if (flag
& SEC_CODE
)
2246 if (flag
& SEC_READONLY
)
2249 if (flag
& SEC_DATA
)
2252 if (flag
& SEC_LOAD
)
2259 lang_memory_region_type
*m
;
2260 bool dis_header_printed
= false;
2262 LANG_FOR_EACH_INPUT_STATEMENT (file
)
2266 if ((file
->the_bfd
->flags
& (BFD_LINKER_CREATED
| DYNAMIC
)) != 0
2267 || file
->flags
.just_syms
)
2270 if (config
.print_map_discarded
)
2271 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
2272 if ((s
->output_section
== NULL
2273 || s
->output_section
->owner
!= link_info
.output_bfd
)
2274 && (s
->flags
& (SEC_LINKER_CREATED
| SEC_KEEP
)) == 0)
2276 if (! dis_header_printed
)
2278 fprintf (config
.map_file
, _("\nDiscarded input sections\n\n"));
2279 dis_header_printed
= true;
2282 print_input_section (s
, true);
2286 minfo (_("\nMemory Configuration\n\n"));
2287 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
2288 _("Name"), _("Origin"), _("Length"), _("Attributes"));
2290 for (m
= lang_memory_region_list
; m
!= NULL
; m
= m
->next
)
2292 fprintf (config
.map_file
, "%-16s", m
->name_list
.name
);
2295 bfd_sprintf_vma (link_info
.output_bfd
, buf
, m
->origin
);
2296 fprintf (config
.map_file
, " 0x%-16s", buf
);
2297 bfd_sprintf_vma (link_info
.output_bfd
, buf
, m
->length
);
2298 fprintf (config
.map_file
,
2299 " 0x%*s", m
->flags
|| m
->not_flags
? -17 : 0, buf
);
2301 lang_map_flags (m
->flags
);
2306 lang_map_flags (m
->not_flags
);
2312 fprintf (config
.map_file
, _("\nLinker script and memory map\n\n"));
2314 if (!link_info
.reduce_memory_overheads
)
2316 obstack_begin (&map_obstack
, 1000);
2317 bfd_link_hash_traverse (link_info
.hash
, sort_def_symbol
, 0);
2319 expld
.phase
= lang_fixed_phase_enum
;
2320 lang_statement_iteration
++;
2321 print_statements ();
2323 ldemul_extra_map_file_text (link_info
.output_bfd
, &link_info
,
2328 sort_def_symbol (struct bfd_link_hash_entry
*hash_entry
,
2329 void *info ATTRIBUTE_UNUSED
)
2331 if ((hash_entry
->type
== bfd_link_hash_defined
2332 || hash_entry
->type
== bfd_link_hash_defweak
)
2333 && hash_entry
->u
.def
.section
->owner
!= link_info
.output_bfd
2334 && hash_entry
->u
.def
.section
->owner
!= NULL
)
2336 input_section_userdata_type
*ud
;
2337 struct map_symbol_def
*def
;
2339 ud
= bfd_section_userdata (hash_entry
->u
.def
.section
);
2342 ud
= stat_alloc (sizeof (*ud
));
2343 bfd_set_section_userdata (hash_entry
->u
.def
.section
, ud
);
2344 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
2345 ud
->map_symbol_def_count
= 0;
2347 else if (!ud
->map_symbol_def_tail
)
2348 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
2350 def
= (struct map_symbol_def
*) obstack_alloc (&map_obstack
, sizeof *def
);
2351 def
->entry
= hash_entry
;
2352 *(ud
->map_symbol_def_tail
) = def
;
2353 ud
->map_symbol_def_tail
= &def
->next
;
2354 ud
->map_symbol_def_count
++;
2359 /* Initialize an output section. */
2362 init_os (lang_output_section_statement_type
*s
, flagword flags
)
2364 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
2365 einfo (_("%F%P: illegal use of `%s' section\n"), DISCARD_SECTION_NAME
);
2368 s
->bfd_section
= bfd_get_section_by_name (link_info
.output_bfd
, s
->name
);
2369 if (s
->bfd_section
== NULL
)
2370 s
->bfd_section
= bfd_make_section_anyway_with_flags (link_info
.output_bfd
,
2372 if (s
->bfd_section
== NULL
)
2374 einfo (_("%F%P: output format %s cannot represent section"
2375 " called %s: %E\n"),
2376 link_info
.output_bfd
->xvec
->name
, s
->name
);
2378 s
->bfd_section
->output_section
= s
->bfd_section
;
2379 s
->bfd_section
->output_offset
= 0;
2381 /* Set the userdata of the output section to the output section
2382 statement to avoid lookup. */
2383 bfd_set_section_userdata (s
->bfd_section
, s
);
2385 /* If there is a base address, make sure that any sections it might
2386 mention are initialized. */
2387 if (s
->addr_tree
!= NULL
)
2388 exp_init_os (s
->addr_tree
);
2390 if (s
->load_base
!= NULL
)
2391 exp_init_os (s
->load_base
);
2393 /* If supplied an alignment, set it. */
2394 if (s
->section_alignment
!= NULL
)
2395 s
->bfd_section
->alignment_power
= exp_get_power (s
->section_alignment
,
2396 "section alignment");
2399 /* Make sure that all output sections mentioned in an expression are
2403 exp_init_os (etree_type
*exp
)
2405 switch (exp
->type
.node_class
)
2409 case etree_provided
:
2410 exp_init_os (exp
->assign
.src
);
2414 exp_init_os (exp
->binary
.lhs
);
2415 exp_init_os (exp
->binary
.rhs
);
2419 exp_init_os (exp
->trinary
.cond
);
2420 exp_init_os (exp
->trinary
.lhs
);
2421 exp_init_os (exp
->trinary
.rhs
);
2425 exp_init_os (exp
->assert_s
.child
);
2429 exp_init_os (exp
->unary
.child
);
2433 switch (exp
->type
.node_code
)
2438 lang_output_section_statement_type
*os
;
2440 os
= lang_output_section_find (exp
->name
.name
);
2441 if (os
!= NULL
&& os
->bfd_section
== NULL
)
2453 section_already_linked (bfd
*abfd
, asection
*sec
, void *data
)
2455 lang_input_statement_type
*entry
= (lang_input_statement_type
*) data
;
2457 /* If we are only reading symbols from this object, then we want to
2458 discard all sections. */
2459 if (entry
->flags
.just_syms
)
2461 bfd_link_just_syms (abfd
, sec
, &link_info
);
2465 /* Deal with SHF_EXCLUDE ELF sections. */
2466 if (!bfd_link_relocatable (&link_info
)
2467 && (abfd
->flags
& BFD_PLUGIN
) == 0
2468 && (sec
->flags
& (SEC_GROUP
| SEC_KEEP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
2469 sec
->output_section
= bfd_abs_section_ptr
;
2471 if (!(abfd
->flags
& DYNAMIC
))
2472 bfd_section_already_linked (abfd
, sec
, &link_info
);
2476 /* Returns true if SECTION is one we know will be discarded based on its
2477 section flags, otherwise returns false. */
2480 lang_discard_section_p (asection
*section
)
2483 flagword flags
= section
->flags
;
2485 /* Discard sections marked with SEC_EXCLUDE. */
2486 discard
= (flags
& SEC_EXCLUDE
) != 0;
2488 /* Discard the group descriptor sections when we're finally placing the
2489 sections from within the group. */
2490 if ((flags
& SEC_GROUP
) != 0
2491 && link_info
.resolve_section_groups
)
2494 /* Discard debugging sections if we are stripping debugging
2496 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
2497 && (flags
& SEC_DEBUGGING
) != 0)
2503 /* The wild routines.
2505 These expand statements like *(.text) and foo.o to a list of
2506 explicit actions, like foo.o(.text), bar.o(.text) and
2507 foo.o(.text, .data). */
2509 /* Add SECTION to the output section OUTPUT. Do this by creating a
2510 lang_input_section statement which is placed at PTR. */
2513 lang_add_section (lang_statement_list_type
*ptr
,
2515 struct wildcard_list
*pattern
,
2516 struct flag_info
*sflag_info
,
2517 lang_output_section_statement_type
*output
)
2519 flagword flags
= section
->flags
;
2522 lang_input_section_type
*new_section
;
2523 bfd
*abfd
= link_info
.output_bfd
;
2525 /* Is this section one we know should be discarded? */
2526 discard
= lang_discard_section_p (section
);
2528 /* Discard input sections which are assigned to a section named
2529 DISCARD_SECTION_NAME. */
2530 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
2535 if (section
->output_section
== NULL
)
2537 /* This prevents future calls from assigning this section. */
2538 section
->output_section
= bfd_abs_section_ptr
;
2540 else if (link_info
.non_contiguous_regions_warnings
)
2541 einfo (_("%P:%pS: warning: --enable-non-contiguous-regions makes "
2542 "section `%pA' from `%pB' match /DISCARD/ clause.\n"),
2543 NULL
, section
, section
->owner
);
2552 keep
= bfd_lookup_section_flags (&link_info
, sflag_info
, section
);
2557 if (section
->output_section
!= NULL
)
2559 if (!link_info
.non_contiguous_regions
)
2562 /* SECTION has already been handled in a special way
2563 (eg. LINK_ONCE): skip it. */
2564 if (bfd_is_abs_section (section
->output_section
))
2567 /* Already assigned to the same output section, do not process
2568 it again, to avoid creating loops between duplicate sections
2570 if (section
->output_section
== output
->bfd_section
)
2573 if (link_info
.non_contiguous_regions_warnings
&& output
->bfd_section
)
2574 einfo (_("%P:%pS: warning: --enable-non-contiguous-regions may "
2575 "change behaviour for section `%pA' from `%pB' (assigned to "
2576 "%pA, but additional match: %pA)\n"),
2577 NULL
, section
, section
->owner
, section
->output_section
,
2578 output
->bfd_section
);
2580 /* SECTION has already been assigned to an output section, but
2581 the user allows it to be mapped to another one in case it
2582 overflows. We'll later update the actual output section in
2583 size_input_section as appropriate. */
2586 /* We don't copy the SEC_NEVER_LOAD flag from an input section
2587 to an output section, because we want to be able to include a
2588 SEC_NEVER_LOAD section in the middle of an otherwise loaded
2589 section (I don't know why we want to do this, but we do).
2590 build_link_order in ldwrite.c handles this case by turning
2591 the embedded SEC_NEVER_LOAD section into a fill. */
2592 flags
&= ~ SEC_NEVER_LOAD
;
2594 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
2595 already been processed. One reason to do this is that on pe
2596 format targets, .text$foo sections go into .text and it's odd
2597 to see .text with SEC_LINK_ONCE set. */
2598 if ((flags
& (SEC_LINK_ONCE
| SEC_GROUP
)) == (SEC_LINK_ONCE
| SEC_GROUP
))
2600 if (link_info
.resolve_section_groups
)
2601 flags
&= ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
);
2603 flags
&= ~(SEC_LINK_DUPLICATES
| SEC_RELOC
);
2605 else if (!bfd_link_relocatable (&link_info
))
2606 flags
&= ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
);
2608 switch (output
->sectype
)
2610 case normal_section
:
2611 case overlay_section
:
2612 case first_overlay_section
:
2615 case noalloc_section
:
2616 flags
&= ~SEC_ALLOC
;
2618 case typed_readonly_section
:
2619 case readonly_section
:
2620 flags
|= SEC_READONLY
;
2622 case noload_section
:
2624 flags
|= SEC_NEVER_LOAD
;
2625 /* Unfortunately GNU ld has managed to evolve two different
2626 meanings to NOLOAD in scripts. ELF gets a .bss style noload,
2627 alloc, no contents section. All others get a noload, noalloc
2629 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
)
2630 flags
&= ~SEC_HAS_CONTENTS
;
2632 flags
&= ~SEC_ALLOC
;
2636 if (output
->bfd_section
== NULL
)
2637 init_os (output
, flags
);
2639 /* If SEC_READONLY is not set in the input section, then clear
2640 it from the output section. */
2641 output
->bfd_section
->flags
&= flags
| ~SEC_READONLY
;
2643 if (output
->bfd_section
->linker_has_input
)
2645 /* Only set SEC_READONLY flag on the first input section. */
2646 flags
&= ~ SEC_READONLY
;
2648 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
2649 if ((output
->bfd_section
->flags
& (SEC_MERGE
| SEC_STRINGS
))
2650 != (flags
& (SEC_MERGE
| SEC_STRINGS
))
2651 || ((flags
& SEC_MERGE
) != 0
2652 && output
->bfd_section
->entsize
!= section
->entsize
))
2654 output
->bfd_section
->flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2655 flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2658 output
->bfd_section
->flags
|= flags
;
2660 if (!output
->bfd_section
->linker_has_input
)
2662 output
->bfd_section
->linker_has_input
= 1;
2663 /* This must happen after flags have been updated. The output
2664 section may have been created before we saw its first input
2665 section, eg. for a data statement. */
2666 bfd_init_private_section_data (section
->owner
, section
,
2667 link_info
.output_bfd
,
2668 output
->bfd_section
,
2670 if ((flags
& SEC_MERGE
) != 0)
2671 output
->bfd_section
->entsize
= section
->entsize
;
2674 if ((flags
& SEC_TIC54X_BLOCK
) != 0
2675 && bfd_get_arch (section
->owner
) == bfd_arch_tic54x
)
2677 /* FIXME: This value should really be obtained from the bfd... */
2678 output
->block_value
= 128;
2681 /* When a .ctors section is placed in .init_array it must be copied
2682 in reverse order. Similarly for .dtors. Set that up. */
2683 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
2684 && ((startswith (section
->name
, ".ctors")
2685 && strcmp (output
->bfd_section
->name
, ".init_array") == 0)
2686 || (startswith (section
->name
, ".dtors")
2687 && strcmp (output
->bfd_section
->name
, ".fini_array") == 0))
2688 && (section
->name
[6] == 0 || section
->name
[6] == '.'))
2689 section
->flags
|= SEC_ELF_REVERSE_COPY
;
2691 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
2692 output
->bfd_section
->alignment_power
= section
->alignment_power
;
2694 section
->output_section
= output
->bfd_section
;
2696 if (!map_head_is_link_order
)
2698 asection
*s
= output
->bfd_section
->map_tail
.s
;
2699 output
->bfd_section
->map_tail
.s
= section
;
2700 section
->map_head
.s
= NULL
;
2701 section
->map_tail
.s
= s
;
2703 s
->map_head
.s
= section
;
2705 output
->bfd_section
->map_head
.s
= section
;
2708 /* Add a section reference to the list. */
2709 new_section
= new_stat (lang_input_section
, ptr
);
2710 new_section
->section
= section
;
2711 new_section
->pattern
= pattern
;
2714 /* Expand a wild statement for a particular FILE. SECTION may be
2715 NULL, in which case it is a wild card. This assumes that the
2716 wild statement doesn't need any sorting (of filenames or sections). */
2719 output_section_callback_nosort (lang_wild_statement_type
*ptr
,
2720 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
2722 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
2725 lang_output_section_statement_type
*os
;
2727 os
= (lang_output_section_statement_type
*) output
;
2729 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2730 if (unique_section_p (section
, os
))
2733 lang_add_section (&ptr
->children
, section
, ptr
->section_list
,
2734 ptr
->section_flag_list
, os
);
2737 /* Check if all sections in a wild statement for a particular FILE
2741 check_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
2742 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
2744 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
2747 lang_output_section_statement_type
*os
;
2749 os
= (lang_output_section_statement_type
*) output
;
2751 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2752 if (unique_section_p (section
, os
))
2755 if (section
->output_section
== NULL
&& (section
->flags
& SEC_READONLY
) == 0)
2756 os
->all_input_readonly
= false;
2759 /* This is passed a file name which must have been seen already and
2760 added to the statement tree. We will see if it has been opened
2761 already and had its symbols read. If not then we'll read it. */
2763 static lang_input_statement_type
*
2764 lookup_name (const char *name
)
2766 lang_input_statement_type
*search
;
2768 for (search
= (void *) input_file_chain
.head
;
2770 search
= search
->next_real_file
)
2772 /* Use the local_sym_name as the name of the file that has
2773 already been loaded as filename might have been transformed
2774 via the search directory lookup mechanism. */
2775 const char *filename
= search
->local_sym_name
;
2777 if (filename
!= NULL
2778 && filename_cmp (filename
, name
) == 0)
2784 /* Arrange to splice the input statement added by new_afile into
2785 statement_list after the current input_file_chain tail.
2786 We know input_file_chain is not an empty list, and that
2787 lookup_name was called via open_input_bfds. Later calls to
2788 lookup_name should always match an existing input_statement. */
2789 lang_statement_union_type
**tail
= stat_ptr
->tail
;
2790 lang_statement_union_type
**after
2791 = (void *) ((char *) input_file_chain
.tail
2792 - offsetof (lang_input_statement_type
, next_real_file
)
2793 + offsetof (lang_input_statement_type
, header
.next
));
2794 lang_statement_union_type
*rest
= *after
;
2795 stat_ptr
->tail
= after
;
2796 search
= new_afile (name
, lang_input_file_is_search_file_enum
,
2797 default_target
, NULL
);
2798 *stat_ptr
->tail
= rest
;
2800 stat_ptr
->tail
= tail
;
2803 /* If we have already added this file, or this file is not real
2804 don't add this file. */
2805 if (search
->flags
.loaded
|| !search
->flags
.real
)
2808 if (!load_symbols (search
, NULL
))
2814 /* Save LIST as a list of libraries whose symbols should not be exported. */
2819 struct excluded_lib
*next
;
2821 static struct excluded_lib
*excluded_libs
;
2824 add_excluded_libs (const char *list
)
2826 const char *p
= list
, *end
;
2830 struct excluded_lib
*entry
;
2831 end
= strpbrk (p
, ",:");
2833 end
= p
+ strlen (p
);
2834 entry
= (struct excluded_lib
*) xmalloc (sizeof (*entry
));
2835 entry
->next
= excluded_libs
;
2836 entry
->name
= (char *) xmalloc (end
- p
+ 1);
2837 memcpy (entry
->name
, p
, end
- p
);
2838 entry
->name
[end
- p
] = '\0';
2839 excluded_libs
= entry
;
2847 check_excluded_libs (bfd
*abfd
)
2849 struct excluded_lib
*lib
= excluded_libs
;
2853 int len
= strlen (lib
->name
);
2854 const char *filename
= lbasename (bfd_get_filename (abfd
));
2856 if (strcmp (lib
->name
, "ALL") == 0)
2858 abfd
->no_export
= true;
2862 if (filename_ncmp (lib
->name
, filename
, len
) == 0
2863 && (filename
[len
] == '\0'
2864 || (filename
[len
] == '.' && filename
[len
+ 1] == 'a'
2865 && filename
[len
+ 2] == '\0')))
2867 abfd
->no_export
= true;
2875 /* Get the symbols for an input file. */
2878 load_symbols (lang_input_statement_type
*entry
,
2879 lang_statement_list_type
*place
)
2883 if (entry
->flags
.loaded
)
2886 ldfile_open_file (entry
);
2888 /* Do not process further if the file was missing. */
2889 if (entry
->flags
.missing_file
)
2892 if (trace_files
|| verbose
)
2893 info_msg ("%pI\n", entry
);
2895 if (!bfd_check_format (entry
->the_bfd
, bfd_archive
)
2896 && !bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
2899 struct lang_input_statement_flags save_flags
;
2902 err
= bfd_get_error ();
2904 /* See if the emulation has some special knowledge. */
2905 if (ldemul_unrecognized_file (entry
))
2907 if (err
== bfd_error_file_ambiguously_recognized
)
2912 if (err
== bfd_error_file_ambiguously_recognized
)
2916 einfo (_("%P: %pB: file not recognized: %E;"
2917 " matching formats:"), entry
->the_bfd
);
2918 for (p
= matching
; *p
!= NULL
; p
++)
2923 else if (err
!= bfd_error_file_not_recognized
2925 einfo (_("%F%P: %pB: file not recognized: %E\n"), entry
->the_bfd
);
2927 bfd_close (entry
->the_bfd
);
2928 entry
->the_bfd
= NULL
;
2930 /* Try to interpret the file as a linker script. */
2931 save_flags
= input_flags
;
2932 ldfile_open_command_file (entry
->filename
);
2934 push_stat_ptr (place
);
2935 input_flags
.add_DT_NEEDED_for_regular
2936 = entry
->flags
.add_DT_NEEDED_for_regular
;
2937 input_flags
.add_DT_NEEDED_for_dynamic
2938 = entry
->flags
.add_DT_NEEDED_for_dynamic
;
2939 input_flags
.whole_archive
= entry
->flags
.whole_archive
;
2940 input_flags
.dynamic
= entry
->flags
.dynamic
;
2942 ldfile_assumed_script
= true;
2943 parser_input
= input_script
;
2944 current_input_file
= entry
->filename
;
2946 current_input_file
= NULL
;
2947 ldfile_assumed_script
= false;
2949 /* missing_file is sticky. sysrooted will already have been
2950 restored when seeing EOF in yyparse, but no harm to restore
2952 save_flags
.missing_file
|= input_flags
.missing_file
;
2953 input_flags
= save_flags
;
2957 entry
->flags
.loaded
= true;
2962 if (ldemul_recognized_file (entry
))
2965 /* We don't call ldlang_add_file for an archive. Instead, the
2966 add_symbols entry point will call ldlang_add_file, via the
2967 add_archive_element callback, for each element of the archive
2969 switch (bfd_get_format (entry
->the_bfd
))
2975 if (!entry
->flags
.reload
)
2976 ldlang_add_file (entry
);
2980 check_excluded_libs (entry
->the_bfd
);
2982 bfd_set_usrdata (entry
->the_bfd
, entry
);
2983 if (entry
->flags
.whole_archive
)
2991 member
= bfd_openr_next_archived_file (entry
->the_bfd
, member
);
2996 if (!bfd_check_format (member
, bfd_object
))
2998 einfo (_("%F%P: %pB: member %pB in archive is not an object\n"),
2999 entry
->the_bfd
, member
);
3004 if (!(*link_info
.callbacks
3005 ->add_archive_element
) (&link_info
, member
,
3006 "--whole-archive", &subsbfd
))
3009 /* Potentially, the add_archive_element hook may have set a
3010 substitute BFD for us. */
3011 if (!bfd_link_add_symbols (subsbfd
, &link_info
))
3013 einfo (_("%F%P: %pB: error adding symbols: %E\n"), member
);
3018 entry
->flags
.loaded
= loaded
;
3024 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
3025 entry
->flags
.loaded
= true;
3027 einfo (_("%F%P: %pB: error adding symbols: %E\n"), entry
->the_bfd
);
3029 return entry
->flags
.loaded
;
3032 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
3033 may be NULL, indicating that it is a wildcard. Separate
3034 lang_input_section statements are created for each part of the
3035 expansion; they are added after the wild statement S. OUTPUT is
3036 the output section. */
3039 wild (lang_wild_statement_type
*s
,
3040 const char *target ATTRIBUTE_UNUSED
,
3041 lang_output_section_statement_type
*output
)
3043 struct wildcard_list
*sec
;
3045 if (s
->filenames_sorted
|| s
->any_specs_sorted
)
3047 lang_section_bst_type
*tree
;
3049 walk_wild (s
, output_section_callback_sort
, output
);
3054 output_section_callback_tree_to_list (s
, tree
, output
);
3056 s
->rightmost
= &s
->tree
;
3060 walk_wild (s
, output_section_callback_nosort
, output
);
3062 if (default_common_section
== NULL
)
3063 for (sec
= s
->section_list
; sec
!= NULL
; sec
= sec
->next
)
3064 if (sec
->spec
.name
!= NULL
&& strcmp (sec
->spec
.name
, "COMMON") == 0)
3066 /* Remember the section that common is going to in case we
3067 later get something which doesn't know where to put it. */
3068 default_common_section
= output
;
3073 /* Return TRUE iff target is the sought target. */
3076 get_target (const bfd_target
*target
, void *data
)
3078 const char *sought
= (const char *) data
;
3080 return strcmp (target
->name
, sought
) == 0;
3083 /* Like strcpy() but convert to lower case as well. */
3086 stricpy (char *dest
, const char *src
)
3090 while ((c
= *src
++) != 0)
3091 *dest
++ = TOLOWER (c
);
3096 /* Remove the first occurrence of needle (if any) in haystack
3100 strcut (char *haystack
, const char *needle
)
3102 haystack
= strstr (haystack
, needle
);
3108 for (src
= haystack
+ strlen (needle
); *src
;)
3109 *haystack
++ = *src
++;
3115 /* Compare two target format name strings.
3116 Return a value indicating how "similar" they are. */
3119 name_compare (const char *first
, const char *second
)
3125 copy1
= (char *) xmalloc (strlen (first
) + 1);
3126 copy2
= (char *) xmalloc (strlen (second
) + 1);
3128 /* Convert the names to lower case. */
3129 stricpy (copy1
, first
);
3130 stricpy (copy2
, second
);
3132 /* Remove size and endian strings from the name. */
3133 strcut (copy1
, "big");
3134 strcut (copy1
, "little");
3135 strcut (copy2
, "big");
3136 strcut (copy2
, "little");
3138 /* Return a value based on how many characters match,
3139 starting from the beginning. If both strings are
3140 the same then return 10 * their length. */
3141 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
3142 if (copy1
[result
] == 0)
3154 /* Set by closest_target_match() below. */
3155 static const bfd_target
*winner
;
3157 /* Scan all the valid bfd targets looking for one that has the endianness
3158 requirement that was specified on the command line, and is the nearest
3159 match to the original output target. */
3162 closest_target_match (const bfd_target
*target
, void *data
)
3164 const bfd_target
*original
= (const bfd_target
*) data
;
3166 if (command_line
.endian
== ENDIAN_BIG
3167 && target
->byteorder
!= BFD_ENDIAN_BIG
)
3170 if (command_line
.endian
== ENDIAN_LITTLE
3171 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
3174 /* Must be the same flavour. */
3175 if (target
->flavour
!= original
->flavour
)
3178 /* Ignore generic big and little endian elf vectors. */
3179 if (strcmp (target
->name
, "elf32-big") == 0
3180 || strcmp (target
->name
, "elf64-big") == 0
3181 || strcmp (target
->name
, "elf32-little") == 0
3182 || strcmp (target
->name
, "elf64-little") == 0)
3185 /* If we have not found a potential winner yet, then record this one. */
3192 /* Oh dear, we now have two potential candidates for a successful match.
3193 Compare their names and choose the better one. */
3194 if (name_compare (target
->name
, original
->name
)
3195 > name_compare (winner
->name
, original
->name
))
3198 /* Keep on searching until wqe have checked them all. */
3202 /* Return the BFD target format of the first input file. */
3205 get_first_input_target (void)
3207 const char *target
= NULL
;
3209 LANG_FOR_EACH_INPUT_STATEMENT (s
)
3211 if (s
->header
.type
== lang_input_statement_enum
3214 ldfile_open_file (s
);
3216 if (s
->the_bfd
!= NULL
3217 && bfd_check_format (s
->the_bfd
, bfd_object
))
3219 target
= bfd_get_target (s
->the_bfd
);
3231 lang_get_output_target (void)
3235 /* Has the user told us which output format to use? */
3236 if (output_target
!= NULL
)
3237 return output_target
;
3239 /* No - has the current target been set to something other than
3241 if (current_target
!= default_target
&& current_target
!= NULL
)
3242 return current_target
;
3244 /* No - can we determine the format of the first input file? */
3245 target
= get_first_input_target ();
3249 /* Failed - use the default output target. */
3250 return default_target
;
3253 /* Open the output file. */
3256 open_output (const char *name
)
3258 lang_input_statement_type
*f
;
3259 char *out
= lrealpath (name
);
3261 for (f
= (void *) input_file_chain
.head
;
3263 f
= f
->next_real_file
)
3266 char *in
= lrealpath (f
->local_sym_name
);
3267 if (filename_cmp (in
, out
) == 0)
3268 einfo (_("%F%P: input file '%s' is the same as output file\n"),
3274 output_target
= lang_get_output_target ();
3276 /* Has the user requested a particular endianness on the command
3278 if (command_line
.endian
!= ENDIAN_UNSET
)
3280 /* Get the chosen target. */
3281 const bfd_target
*target
3282 = bfd_iterate_over_targets (get_target
, (void *) output_target
);
3284 /* If the target is not supported, we cannot do anything. */
3287 enum bfd_endian desired_endian
;
3289 if (command_line
.endian
== ENDIAN_BIG
)
3290 desired_endian
= BFD_ENDIAN_BIG
;
3292 desired_endian
= BFD_ENDIAN_LITTLE
;
3294 /* See if the target has the wrong endianness. This should
3295 not happen if the linker script has provided big and
3296 little endian alternatives, but some scrips don't do
3298 if (target
->byteorder
!= desired_endian
)
3300 /* If it does, then see if the target provides
3301 an alternative with the correct endianness. */
3302 if (target
->alternative_target
!= NULL
3303 && (target
->alternative_target
->byteorder
== desired_endian
))
3304 output_target
= target
->alternative_target
->name
;
3307 /* Try to find a target as similar as possible to
3308 the default target, but which has the desired
3309 endian characteristic. */
3310 bfd_iterate_over_targets (closest_target_match
,
3313 /* Oh dear - we could not find any targets that
3314 satisfy our requirements. */
3316 einfo (_("%P: warning: could not find any targets"
3317 " that match endianness requirement\n"));
3319 output_target
= winner
->name
;
3325 link_info
.output_bfd
= bfd_openw (name
, output_target
);
3327 if (link_info
.output_bfd
== NULL
)
3329 if (bfd_get_error () == bfd_error_invalid_target
)
3330 einfo (_("%F%P: target %s not found\n"), output_target
);
3332 einfo (_("%F%P: cannot open output file %s: %E\n"), name
);
3335 delete_output_file_on_failure
= true;
3337 if (!bfd_set_format (link_info
.output_bfd
, bfd_object
))
3338 einfo (_("%F%P: %s: can not make object file: %E\n"), name
);
3339 if (!bfd_set_arch_mach (link_info
.output_bfd
,
3340 ldfile_output_architecture
,
3341 ldfile_output_machine
))
3342 einfo (_("%F%P: %s: can not set architecture: %E\n"), name
);
3344 link_info
.hash
= bfd_link_hash_table_create (link_info
.output_bfd
);
3345 if (link_info
.hash
== NULL
)
3346 einfo (_("%F%P: can not create hash table: %E\n"));
3348 bfd_set_gp_size (link_info
.output_bfd
, g_switch_value
);
3352 ldlang_open_output (lang_statement_union_type
*statement
)
3354 switch (statement
->header
.type
)
3356 case lang_output_statement_enum
:
3357 ASSERT (link_info
.output_bfd
== NULL
);
3358 open_output (statement
->output_statement
.name
);
3359 ldemul_set_output_arch ();
3360 if (config
.magic_demand_paged
3361 && !bfd_link_relocatable (&link_info
))
3362 link_info
.output_bfd
->flags
|= D_PAGED
;
3364 link_info
.output_bfd
->flags
&= ~D_PAGED
;
3365 if (config
.text_read_only
)
3366 link_info
.output_bfd
->flags
|= WP_TEXT
;
3368 link_info
.output_bfd
->flags
&= ~WP_TEXT
;
3369 if (link_info
.traditional_format
)
3370 link_info
.output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
3372 link_info
.output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
3375 case lang_target_statement_enum
:
3376 current_target
= statement
->target_statement
.target
;
3384 init_opb (asection
*s
)
3389 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
3391 && (s
->flags
& SEC_ELF_OCTETS
) != 0)
3394 x
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3395 ldfile_output_machine
);
3397 while ((x
& 1) == 0)
3405 /* Open all the input files. */
3409 OPEN_BFD_NORMAL
= 0,
3413 #if BFD_SUPPORTS_PLUGINS
3414 static lang_input_statement_type
*plugin_insert
= NULL
;
3415 static struct bfd_link_hash_entry
*plugin_undefs
= NULL
;
3419 open_input_bfds (lang_statement_union_type
*s
, enum open_bfd_mode mode
)
3421 for (; s
!= NULL
; s
= s
->header
.next
)
3423 switch (s
->header
.type
)
3425 case lang_constructors_statement_enum
:
3426 open_input_bfds (constructor_list
.head
, mode
);
3428 case lang_output_section_statement_enum
:
3429 open_input_bfds (s
->output_section_statement
.children
.head
, mode
);
3431 case lang_wild_statement_enum
:
3432 /* Maybe we should load the file's symbols. */
3433 if ((mode
& OPEN_BFD_RESCAN
) == 0
3434 && s
->wild_statement
.filename
3435 && !wildcardp (s
->wild_statement
.filename
)
3436 && !archive_path (s
->wild_statement
.filename
))
3437 lookup_name (s
->wild_statement
.filename
);
3438 open_input_bfds (s
->wild_statement
.children
.head
, mode
);
3440 case lang_group_statement_enum
:
3442 struct bfd_link_hash_entry
*undefs
;
3443 #if BFD_SUPPORTS_PLUGINS
3444 lang_input_statement_type
*plugin_insert_save
;
3447 /* We must continually search the entries in the group
3448 until no new symbols are added to the list of undefined
3453 #if BFD_SUPPORTS_PLUGINS
3454 plugin_insert_save
= plugin_insert
;
3456 undefs
= link_info
.hash
->undefs_tail
;
3457 open_input_bfds (s
->group_statement
.children
.head
,
3458 mode
| OPEN_BFD_FORCE
);
3460 while (undefs
!= link_info
.hash
->undefs_tail
3461 #if BFD_SUPPORTS_PLUGINS
3462 /* Objects inserted by a plugin, which are loaded
3463 before we hit this loop, may have added new
3465 || (plugin_insert
!= plugin_insert_save
&& plugin_undefs
)
3470 case lang_target_statement_enum
:
3471 current_target
= s
->target_statement
.target
;
3473 case lang_input_statement_enum
:
3474 if (s
->input_statement
.flags
.real
)
3476 lang_statement_union_type
**os_tail
;
3477 lang_statement_list_type add
;
3480 s
->input_statement
.target
= current_target
;
3482 /* If we are being called from within a group, and this
3483 is an archive which has already been searched, then
3484 force it to be researched unless the whole archive
3485 has been loaded already. Do the same for a rescan.
3486 Likewise reload --as-needed shared libs. */
3487 if (mode
!= OPEN_BFD_NORMAL
3488 #if BFD_SUPPORTS_PLUGINS
3489 && ((mode
& OPEN_BFD_RESCAN
) == 0
3490 || plugin_insert
== NULL
)
3492 && s
->input_statement
.flags
.loaded
3493 && (abfd
= s
->input_statement
.the_bfd
) != NULL
3494 && ((bfd_get_format (abfd
) == bfd_archive
3495 && !s
->input_statement
.flags
.whole_archive
)
3496 || (bfd_get_format (abfd
) == bfd_object
3497 && ((abfd
->flags
) & DYNAMIC
) != 0
3498 && s
->input_statement
.flags
.add_DT_NEEDED_for_regular
3499 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
3500 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)))
3502 s
->input_statement
.flags
.loaded
= false;
3503 s
->input_statement
.flags
.reload
= true;
3506 os_tail
= lang_os_list
.tail
;
3507 lang_list_init (&add
);
3509 if (!load_symbols (&s
->input_statement
, &add
))
3510 config
.make_executable
= false;
3512 if (add
.head
!= NULL
)
3514 /* If this was a script with output sections then
3515 tack any added statements on to the end of the
3516 list. This avoids having to reorder the output
3517 section statement list. Very likely the user
3518 forgot -T, and whatever we do here will not meet
3519 naive user expectations. */
3520 if (os_tail
!= lang_os_list
.tail
)
3522 einfo (_("%P: warning: %s contains output sections;"
3523 " did you forget -T?\n"),
3524 s
->input_statement
.filename
);
3525 *stat_ptr
->tail
= add
.head
;
3526 stat_ptr
->tail
= add
.tail
;
3530 *add
.tail
= s
->header
.next
;
3531 s
->header
.next
= add
.head
;
3535 #if BFD_SUPPORTS_PLUGINS
3536 /* If we have found the point at which a plugin added new
3537 files, clear plugin_insert to enable archive rescan. */
3538 if (&s
->input_statement
== plugin_insert
)
3539 plugin_insert
= NULL
;
3542 case lang_assignment_statement_enum
:
3543 if (s
->assignment_statement
.exp
->type
.node_class
!= etree_assert
)
3544 exp_fold_tree_no_dot (s
->assignment_statement
.exp
);
3551 /* Exit if any of the files were missing. */
3552 if (input_flags
.missing_file
)
3556 #ifdef ENABLE_LIBCTF
3557 /* Emit CTF errors and warnings. fp can be NULL to report errors/warnings
3558 that happened specifically at CTF open time. */
3560 lang_ctf_errs_warnings (ctf_dict_t
*fp
)
3562 ctf_next_t
*i
= NULL
;
3567 while ((text
= ctf_errwarning_next (fp
, &i
, &is_warning
, &err
)) != NULL
)
3569 einfo (_("%s: %s\n"), is_warning
? _("CTF warning"): _("CTF error"),
3573 if (err
!= ECTF_NEXT_END
)
3575 einfo (_("CTF error: cannot get CTF errors: `%s'\n"),
3579 /* `err' returns errors from the error/warning iterator in particular.
3580 These never assert. But if we have an fp, that could have recorded
3581 an assertion failure: assert if it has done so. */
3582 ASSERT (!fp
|| ctf_errno (fp
) != ECTF_INTERNAL
);
3585 /* Open the CTF sections in the input files with libctf: if any were opened,
3586 create a fake input file that we'll write the merged CTF data to later
3590 ldlang_open_ctf (void)
3595 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3599 /* Incoming files from the compiler have a single ctf_dict_t in them
3600 (which is presented to us by the libctf API in a ctf_archive_t
3601 wrapper): files derived from a previous relocatable link have a CTF
3602 archive containing possibly many CTF files. */
3604 if ((file
->the_ctf
= ctf_bfdopen (file
->the_bfd
, &err
)) == NULL
)
3606 if (err
!= ECTF_NOCTFDATA
)
3608 lang_ctf_errs_warnings (NULL
);
3609 einfo (_("%P: warning: CTF section in %pB not loaded; "
3610 "its types will be discarded: %s\n"), file
->the_bfd
,
3616 /* Prevent the contents of this section from being written, while
3617 requiring the section itself to be duplicated in the output, but only
3619 /* This section must exist if ctf_bfdopen() succeeded. */
3620 sect
= bfd_get_section_by_name (file
->the_bfd
, ".ctf");
3622 sect
->flags
|= SEC_NEVER_LOAD
| SEC_HAS_CONTENTS
| SEC_LINKER_CREATED
;
3625 sect
->flags
|= SEC_EXCLUDE
;
3635 if ((ctf_output
= ctf_create (&err
)) != NULL
)
3638 einfo (_("%P: warning: CTF output not created: `%s'\n"),
3641 LANG_FOR_EACH_INPUT_STATEMENT (errfile
)
3642 ctf_close (errfile
->the_ctf
);
3645 /* Merge together CTF sections. After this, only the symtab-dependent
3646 function and data object sections need adjustment. */
3649 lang_merge_ctf (void)
3651 asection
*output_sect
;
3657 output_sect
= bfd_get_section_by_name (link_info
.output_bfd
, ".ctf");
3659 /* If the section was discarded, don't waste time merging. */
3660 if (output_sect
== NULL
)
3662 ctf_dict_close (ctf_output
);
3665 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3667 ctf_close (file
->the_ctf
);
3668 file
->the_ctf
= NULL
;
3673 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3678 /* Takes ownership of file->the_ctf. */
3679 if (ctf_link_add_ctf (ctf_output
, file
->the_ctf
, file
->filename
) < 0)
3681 einfo (_("%P: warning: CTF section in %pB cannot be linked: `%s'\n"),
3682 file
->the_bfd
, ctf_errmsg (ctf_errno (ctf_output
)));
3683 ctf_close (file
->the_ctf
);
3684 file
->the_ctf
= NULL
;
3689 if (!config
.ctf_share_duplicated
)
3690 flags
= CTF_LINK_SHARE_UNCONFLICTED
;
3692 flags
= CTF_LINK_SHARE_DUPLICATED
;
3693 if (!config
.ctf_variables
)
3694 flags
|= CTF_LINK_OMIT_VARIABLES_SECTION
;
3695 if (bfd_link_relocatable (&link_info
))
3696 flags
|= CTF_LINK_NO_FILTER_REPORTED_SYMS
;
3698 if (ctf_link (ctf_output
, flags
) < 0)
3700 lang_ctf_errs_warnings (ctf_output
);
3701 einfo (_("%P: warning: CTF linking failed; "
3702 "output will have no CTF section: %s\n"),
3703 ctf_errmsg (ctf_errno (ctf_output
)));
3706 output_sect
->size
= 0;
3707 output_sect
->flags
|= SEC_EXCLUDE
;
3710 /* Output any lingering errors that didn't come from ctf_link. */
3711 lang_ctf_errs_warnings (ctf_output
);
3714 /* Let the emulation acquire strings from the dynamic strtab to help it optimize
3715 the CTF, if supported. */
3718 ldlang_ctf_acquire_strings (struct elf_strtab_hash
*dynstrtab
)
3720 ldemul_acquire_strings_for_ctf (ctf_output
, dynstrtab
);
3723 /* Inform the emulation about the addition of a new dynamic symbol, in BFD
3725 void ldlang_ctf_new_dynsym (int symidx
, struct elf_internal_sym
*sym
)
3727 ldemul_new_dynsym_for_ctf (ctf_output
, symidx
, sym
);
3730 /* Write out the CTF section. Called early, if the emulation isn't going to
3731 need to dedup against the strtab and symtab, then possibly called from the
3732 target linker code if the dedup has happened. */
3734 lang_write_ctf (int late
)
3737 asection
*output_sect
;
3744 /* Emit CTF late if this emulation says it can do so. */
3745 if (ldemul_emit_ctf_early ())
3750 if (!ldemul_emit_ctf_early ())
3754 /* Inform the emulation that all the symbols that will be received have
3757 ldemul_new_dynsym_for_ctf (ctf_output
, 0, NULL
);
3761 output_sect
= bfd_get_section_by_name (link_info
.output_bfd
, ".ctf");
3764 output_sect
->contents
= ctf_link_write (ctf_output
, &output_size
,
3765 CTF_COMPRESSION_THRESHOLD
);
3766 output_sect
->size
= output_size
;
3767 output_sect
->flags
|= SEC_IN_MEMORY
| SEC_KEEP
;
3769 lang_ctf_errs_warnings (ctf_output
);
3770 if (!output_sect
->contents
)
3772 einfo (_("%P: warning: CTF section emission failed; "
3773 "output will have no CTF section: %s\n"),
3774 ctf_errmsg (ctf_errno (ctf_output
)));
3775 output_sect
->size
= 0;
3776 output_sect
->flags
|= SEC_EXCLUDE
;
3780 /* This also closes every CTF input file used in the link. */
3781 ctf_dict_close (ctf_output
);
3784 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3785 file
->the_ctf
= NULL
;
3788 /* Write out the CTF section late, if the emulation needs that. */
3791 ldlang_write_ctf_late (void)
3793 /* Trigger a "late call", if the emulation needs one. */
3799 ldlang_open_ctf (void)
3801 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3805 /* If built without CTF, warn and delete all CTF sections from the output.
3806 (The alternative would be to simply concatenate them, which does not
3807 yield a valid CTF section.) */
3809 if ((sect
= bfd_get_section_by_name (file
->the_bfd
, ".ctf")) != NULL
)
3811 einfo (_("%P: warning: CTF section in %pB not linkable: "
3812 "%P was built without support for CTF\n"), file
->the_bfd
);
3814 sect
->flags
|= SEC_EXCLUDE
;
3819 static void lang_merge_ctf (void) {}
3821 ldlang_ctf_acquire_strings (struct elf_strtab_hash
*dynstrtab
3822 ATTRIBUTE_UNUSED
) {}
3824 ldlang_ctf_new_dynsym (int symidx ATTRIBUTE_UNUSED
,
3825 struct elf_internal_sym
*sym ATTRIBUTE_UNUSED
) {}
3826 static void lang_write_ctf (int late ATTRIBUTE_UNUSED
) {}
3827 void ldlang_write_ctf_late (void) {}
3830 /* Add the supplied name to the symbol table as an undefined reference.
3831 This is a two step process as the symbol table doesn't even exist at
3832 the time the ld command line is processed. First we put the name
3833 on a list, then, once the output file has been opened, transfer the
3834 name to the symbol table. */
3836 typedef struct bfd_sym_chain ldlang_undef_chain_list_type
;
3838 #define ldlang_undef_chain_list_head entry_symbol.next
3841 ldlang_add_undef (const char *const name
, bool cmdline ATTRIBUTE_UNUSED
)
3843 ldlang_undef_chain_list_type
*new_undef
;
3845 new_undef
= stat_alloc (sizeof (*new_undef
));
3846 new_undef
->next
= ldlang_undef_chain_list_head
;
3847 ldlang_undef_chain_list_head
= new_undef
;
3849 new_undef
->name
= xstrdup (name
);
3851 if (link_info
.output_bfd
!= NULL
)
3852 insert_undefined (new_undef
->name
);
3855 /* Insert NAME as undefined in the symbol table. */
3858 insert_undefined (const char *name
)
3860 struct bfd_link_hash_entry
*h
;
3862 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, false, true);
3864 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
3865 if (h
->type
== bfd_link_hash_new
)
3867 h
->type
= bfd_link_hash_undefined
;
3868 h
->u
.undef
.abfd
= NULL
;
3869 h
->non_ir_ref_regular
= true;
3870 bfd_link_add_undef (link_info
.hash
, h
);
3874 /* Run through the list of undefineds created above and place them
3875 into the linker hash table as undefined symbols belonging to the
3879 lang_place_undefineds (void)
3881 ldlang_undef_chain_list_type
*ptr
;
3883 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
3884 insert_undefined (ptr
->name
);
3887 /* Mark -u symbols against garbage collection. */
3890 lang_mark_undefineds (void)
3892 ldlang_undef_chain_list_type
*ptr
;
3894 if (is_elf_hash_table (link_info
.hash
))
3895 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
3897 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*)
3898 bfd_link_hash_lookup (link_info
.hash
, ptr
->name
, false, false, true);
3904 /* Structure used to build the list of symbols that the user has required
3907 struct require_defined_symbol
3910 struct require_defined_symbol
*next
;
3913 /* The list of symbols that the user has required be defined. */
3915 static struct require_defined_symbol
*require_defined_symbol_list
;
3917 /* Add a new symbol NAME to the list of symbols that are required to be
3921 ldlang_add_require_defined (const char *const name
)
3923 struct require_defined_symbol
*ptr
;
3925 ldlang_add_undef (name
, true);
3926 ptr
= stat_alloc (sizeof (*ptr
));
3927 ptr
->next
= require_defined_symbol_list
;
3928 ptr
->name
= strdup (name
);
3929 require_defined_symbol_list
= ptr
;
3932 /* Check that all symbols the user required to be defined, are defined,
3933 raise an error if we find a symbol that is not defined. */
3936 ldlang_check_require_defined_symbols (void)
3938 struct require_defined_symbol
*ptr
;
3940 for (ptr
= require_defined_symbol_list
; ptr
!= NULL
; ptr
= ptr
->next
)
3942 struct bfd_link_hash_entry
*h
;
3944 h
= bfd_link_hash_lookup (link_info
.hash
, ptr
->name
,
3945 false, false, true);
3947 || (h
->type
!= bfd_link_hash_defined
3948 && h
->type
!= bfd_link_hash_defweak
))
3949 einfo(_("%X%P: required symbol `%s' not defined\n"), ptr
->name
);
3953 /* Check for all readonly or some readwrite sections. */
3956 check_input_sections
3957 (lang_statement_union_type
*s
,
3958 lang_output_section_statement_type
*output_section_statement
)
3960 for (; s
!= NULL
; s
= s
->header
.next
)
3962 switch (s
->header
.type
)
3964 case lang_wild_statement_enum
:
3965 walk_wild (&s
->wild_statement
, check_section_callback
,
3966 output_section_statement
);
3967 if (!output_section_statement
->all_input_readonly
)
3970 case lang_constructors_statement_enum
:
3971 check_input_sections (constructor_list
.head
,
3972 output_section_statement
);
3973 if (!output_section_statement
->all_input_readonly
)
3976 case lang_group_statement_enum
:
3977 check_input_sections (s
->group_statement
.children
.head
,
3978 output_section_statement
);
3979 if (!output_section_statement
->all_input_readonly
)
3988 /* Update wildcard statements if needed. */
3991 update_wild_statements (lang_statement_union_type
*s
)
3993 struct wildcard_list
*sec
;
3995 switch (sort_section
)
4005 for (; s
!= NULL
; s
= s
->header
.next
)
4007 switch (s
->header
.type
)
4012 case lang_wild_statement_enum
:
4013 for (sec
= s
->wild_statement
.section_list
; sec
!= NULL
;
4015 /* Don't sort .init/.fini sections. */
4016 if (strcmp (sec
->spec
.name
, ".init") != 0
4017 && strcmp (sec
->spec
.name
, ".fini") != 0)
4019 switch (sec
->spec
.sorted
)
4022 sec
->spec
.sorted
= sort_section
;
4025 if (sort_section
== by_alignment
)
4026 sec
->spec
.sorted
= by_name_alignment
;
4029 if (sort_section
== by_name
)
4030 sec
->spec
.sorted
= by_alignment_name
;
4035 s
->wild_statement
.any_specs_sorted
= true;
4039 case lang_constructors_statement_enum
:
4040 update_wild_statements (constructor_list
.head
);
4043 case lang_output_section_statement_enum
:
4044 update_wild_statements
4045 (s
->output_section_statement
.children
.head
);
4048 case lang_group_statement_enum
:
4049 update_wild_statements (s
->group_statement
.children
.head
);
4057 /* Open input files and attach to output sections. */
4060 map_input_to_output_sections
4061 (lang_statement_union_type
*s
, const char *target
,
4062 lang_output_section_statement_type
*os
)
4064 for (; s
!= NULL
; s
= s
->header
.next
)
4066 lang_output_section_statement_type
*tos
;
4068 unsigned int type
= 0;
4070 switch (s
->header
.type
)
4072 case lang_wild_statement_enum
:
4073 wild (&s
->wild_statement
, target
, os
);
4075 case lang_constructors_statement_enum
:
4076 map_input_to_output_sections (constructor_list
.head
,
4080 case lang_output_section_statement_enum
:
4081 tos
= &s
->output_section_statement
;
4082 if (tos
->constraint
== ONLY_IF_RW
4083 || tos
->constraint
== ONLY_IF_RO
)
4085 tos
->all_input_readonly
= true;
4086 check_input_sections (tos
->children
.head
, tos
);
4087 if (tos
->all_input_readonly
!= (tos
->constraint
== ONLY_IF_RO
))
4088 tos
->constraint
= -1;
4090 if (tos
->constraint
>= 0)
4091 map_input_to_output_sections (tos
->children
.head
,
4095 case lang_output_statement_enum
:
4097 case lang_target_statement_enum
:
4098 target
= s
->target_statement
.target
;
4100 case lang_group_statement_enum
:
4101 map_input_to_output_sections (s
->group_statement
.children
.head
,
4105 case lang_data_statement_enum
:
4106 /* Make sure that any sections mentioned in the expression
4108 exp_init_os (s
->data_statement
.exp
);
4109 /* The output section gets CONTENTS, ALLOC and LOAD, but
4110 these may be overridden by the script. */
4111 flags
= SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
;
4112 switch (os
->sectype
)
4114 case normal_section
:
4115 case overlay_section
:
4116 case first_overlay_section
:
4118 case noalloc_section
:
4119 flags
= SEC_HAS_CONTENTS
;
4121 case readonly_section
:
4122 flags
|= SEC_READONLY
;
4124 case typed_readonly_section
:
4125 flags
|= SEC_READONLY
;
4128 if (os
->sectype_value
->type
.node_class
== etree_name
4129 && os
->sectype_value
->type
.node_code
== NAME
)
4131 const char *name
= os
->sectype_value
->name
.name
;
4132 if (strcmp (name
, "SHT_PROGBITS") == 0)
4133 type
= SHT_PROGBITS
;
4134 else if (strcmp (name
, "SHT_STRTAB") == 0)
4136 else if (strcmp (name
, "SHT_NOTE") == 0)
4138 else if (strcmp (name
, "SHT_NOBITS") == 0)
4140 else if (strcmp (name
, "SHT_INIT_ARRAY") == 0)
4141 type
= SHT_INIT_ARRAY
;
4142 else if (strcmp (name
, "SHT_FINI_ARRAY") == 0)
4143 type
= SHT_FINI_ARRAY
;
4144 else if (strcmp (name
, "SHT_PREINIT_ARRAY") == 0)
4145 type
= SHT_PREINIT_ARRAY
;
4147 einfo (_ ("%F%P: invalid type for output section `%s'\n"),
4152 exp_fold_tree_no_dot (os
->sectype_value
);
4153 if (expld
.result
.valid_p
)
4154 type
= expld
.result
.value
;
4156 einfo (_ ("%F%P: invalid type for output section `%s'\n"),
4160 case noload_section
:
4161 if (bfd_get_flavour (link_info
.output_bfd
)
4162 == bfd_target_elf_flavour
)
4163 flags
= SEC_NEVER_LOAD
| SEC_ALLOC
;
4165 flags
= SEC_NEVER_LOAD
| SEC_HAS_CONTENTS
;
4168 if (os
->bfd_section
== NULL
)
4169 init_os (os
, flags
| SEC_READONLY
);
4171 os
->bfd_section
->flags
|= flags
;
4172 os
->bfd_section
->type
= type
;
4174 case lang_input_section_enum
:
4176 case lang_fill_statement_enum
:
4177 case lang_object_symbols_statement_enum
:
4178 case lang_reloc_statement_enum
:
4179 case lang_padding_statement_enum
:
4180 case lang_input_statement_enum
:
4181 if (os
!= NULL
&& os
->bfd_section
== NULL
)
4184 case lang_assignment_statement_enum
:
4185 if (os
!= NULL
&& os
->bfd_section
== NULL
)
4188 /* Make sure that any sections mentioned in the assignment
4190 exp_init_os (s
->assignment_statement
.exp
);
4192 case lang_address_statement_enum
:
4193 /* Mark the specified section with the supplied address.
4194 If this section was actually a segment marker, then the
4195 directive is ignored if the linker script explicitly
4196 processed the segment marker. Originally, the linker
4197 treated segment directives (like -Ttext on the
4198 command-line) as section directives. We honor the
4199 section directive semantics for backwards compatibility;
4200 linker scripts that do not specifically check for
4201 SEGMENT_START automatically get the old semantics. */
4202 if (!s
->address_statement
.segment
4203 || !s
->address_statement
.segment
->used
)
4205 const char *name
= s
->address_statement
.section_name
;
4207 /* Create the output section statement here so that
4208 orphans with a set address will be placed after other
4209 script sections. If we let the orphan placement code
4210 place them in amongst other sections then the address
4211 will affect following script sections, which is
4212 likely to surprise naive users. */
4213 tos
= lang_output_section_statement_lookup (name
, 0, 1);
4214 tos
->addr_tree
= s
->address_statement
.address
;
4215 if (tos
->bfd_section
== NULL
)
4219 case lang_insert_statement_enum
:
4221 case lang_input_matcher_enum
:
4227 /* An insert statement snips out all the linker statements from the
4228 start of the list and places them after the output section
4229 statement specified by the insert. This operation is complicated
4230 by the fact that we keep a doubly linked list of output section
4231 statements as well as the singly linked list of all statements.
4232 FIXME someday: Twiddling with the list not only moves statements
4233 from the user's script but also input and group statements that are
4234 built from command line object files and --start-group. We only
4235 get away with this because the list pointers used by file_chain
4236 and input_file_chain are not reordered, and processing via
4237 statement_list after this point mostly ignores input statements.
4238 One exception is the map file, where LOAD and START GROUP/END GROUP
4239 can end up looking odd. */
4242 process_insert_statements (lang_statement_union_type
**start
)
4244 lang_statement_union_type
**s
;
4245 lang_output_section_statement_type
*first_os
= NULL
;
4246 lang_output_section_statement_type
*last_os
= NULL
;
4247 lang_output_section_statement_type
*os
;
4252 if ((*s
)->header
.type
== lang_output_section_statement_enum
)
4254 /* Keep pointers to the first and last output section
4255 statement in the sequence we may be about to move. */
4256 os
= &(*s
)->output_section_statement
;
4258 ASSERT (last_os
== NULL
|| last_os
->next
== os
);
4261 /* Set constraint negative so that lang_output_section_find
4262 won't match this output section statement. At this
4263 stage in linking constraint has values in the range
4264 [-1, ONLY_IN_RW]. */
4265 last_os
->constraint
= -2 - last_os
->constraint
;
4266 if (first_os
== NULL
)
4269 else if ((*s
)->header
.type
== lang_group_statement_enum
)
4271 /* A user might put -T between --start-group and
4272 --end-group. One way this odd construct might arise is
4273 from a wrapper around ld to change library search
4274 behaviour. For example:
4276 exec real_ld --start-group "$@" --end-group
4277 This isn't completely unreasonable so go looking inside a
4278 group statement for insert statements. */
4279 process_insert_statements (&(*s
)->group_statement
.children
.head
);
4281 else if ((*s
)->header
.type
== lang_insert_statement_enum
)
4283 lang_insert_statement_type
*i
= &(*s
)->insert_statement
;
4284 lang_output_section_statement_type
*where
;
4285 lang_statement_union_type
**ptr
;
4286 lang_statement_union_type
*first
;
4288 if (link_info
.non_contiguous_regions
)
4290 einfo (_("warning: INSERT statement in linker script is "
4291 "incompatible with --enable-non-contiguous-regions.\n"));
4294 where
= lang_output_section_find (i
->where
);
4295 if (where
!= NULL
&& i
->is_before
)
4298 where
= where
->prev
;
4299 while (where
!= NULL
&& where
->constraint
< 0);
4303 einfo (_("%F%P: %s not found for insert\n"), i
->where
);
4307 /* Deal with reordering the output section statement list. */
4308 if (last_os
!= NULL
)
4310 asection
*first_sec
, *last_sec
;
4311 struct lang_output_section_statement_struct
**next
;
4313 /* Snip out the output sections we are moving. */
4314 first_os
->prev
->next
= last_os
->next
;
4315 if (last_os
->next
== NULL
)
4317 next
= &first_os
->prev
->next
;
4318 lang_os_list
.tail
= (lang_statement_union_type
**) next
;
4321 last_os
->next
->prev
= first_os
->prev
;
4322 /* Add them in at the new position. */
4323 last_os
->next
= where
->next
;
4324 if (where
->next
== NULL
)
4326 next
= &last_os
->next
;
4327 lang_os_list
.tail
= (lang_statement_union_type
**) next
;
4330 where
->next
->prev
= last_os
;
4331 first_os
->prev
= where
;
4332 where
->next
= first_os
;
4334 /* Move the bfd sections in the same way. */
4337 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
4339 os
->constraint
= -2 - os
->constraint
;
4340 if (os
->bfd_section
!= NULL
4341 && os
->bfd_section
->owner
!= NULL
)
4343 last_sec
= os
->bfd_section
;
4344 if (first_sec
== NULL
)
4345 first_sec
= last_sec
;
4350 if (last_sec
!= NULL
)
4352 asection
*sec
= where
->bfd_section
;
4354 sec
= output_prev_sec_find (where
);
4356 /* The place we want to insert must come after the
4357 sections we are moving. So if we find no
4358 section or if the section is the same as our
4359 last section, then no move is needed. */
4360 if (sec
!= NULL
&& sec
!= last_sec
)
4362 /* Trim them off. */
4363 if (first_sec
->prev
!= NULL
)
4364 first_sec
->prev
->next
= last_sec
->next
;
4366 link_info
.output_bfd
->sections
= last_sec
->next
;
4367 if (last_sec
->next
!= NULL
)
4368 last_sec
->next
->prev
= first_sec
->prev
;
4370 link_info
.output_bfd
->section_last
= first_sec
->prev
;
4372 last_sec
->next
= sec
->next
;
4373 if (sec
->next
!= NULL
)
4374 sec
->next
->prev
= last_sec
;
4376 link_info
.output_bfd
->section_last
= last_sec
;
4377 first_sec
->prev
= sec
;
4378 sec
->next
= first_sec
;
4386 ptr
= insert_os_after (where
);
4387 /* Snip everything from the start of the list, up to and
4388 including the insert statement we are currently processing. */
4390 *start
= (*s
)->header
.next
;
4391 /* Add them back where they belong, minus the insert. */
4394 statement_list
.tail
= s
;
4399 s
= &(*s
)->header
.next
;
4402 /* Undo constraint twiddling. */
4403 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
4405 os
->constraint
= -2 - os
->constraint
;
4411 /* An output section might have been removed after its statement was
4412 added. For example, ldemul_before_allocation can remove dynamic
4413 sections if they turn out to be not needed. Clean them up here. */
4416 strip_excluded_output_sections (void)
4418 lang_output_section_statement_type
*os
;
4420 /* Run lang_size_sections (if not already done). */
4421 if (expld
.phase
!= lang_mark_phase_enum
)
4423 expld
.phase
= lang_mark_phase_enum
;
4424 expld
.dataseg
.phase
= exp_seg_none
;
4425 one_lang_size_sections_pass (NULL
, false);
4426 lang_reset_memory_regions ();
4429 for (os
= (void *) lang_os_list
.head
;
4433 asection
*output_section
;
4436 if (os
->constraint
< 0)
4439 output_section
= os
->bfd_section
;
4440 if (output_section
== NULL
)
4443 exclude
= (output_section
->rawsize
== 0
4444 && (output_section
->flags
& SEC_KEEP
) == 0
4445 && !bfd_section_removed_from_list (link_info
.output_bfd
,
4448 /* Some sections have not yet been sized, notably .gnu.version,
4449 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
4450 input sections, so don't drop output sections that have such
4451 input sections unless they are also marked SEC_EXCLUDE. */
4452 if (exclude
&& output_section
->map_head
.s
!= NULL
)
4456 for (s
= output_section
->map_head
.s
; s
!= NULL
; s
= s
->map_head
.s
)
4457 if ((s
->flags
& SEC_EXCLUDE
) == 0
4458 && ((s
->flags
& SEC_LINKER_CREATED
) != 0
4459 || link_info
.emitrelocations
))
4468 /* We don't set bfd_section to NULL since bfd_section of the
4469 removed output section statement may still be used. */
4470 if (!os
->update_dot
)
4472 output_section
->flags
|= SEC_EXCLUDE
;
4473 bfd_section_list_remove (link_info
.output_bfd
, output_section
);
4474 link_info
.output_bfd
->section_count
--;
4479 /* Called from ldwrite to clear out asection.map_head and
4480 asection.map_tail for use as link_orders in ldwrite. */
4483 lang_clear_os_map (void)
4485 lang_output_section_statement_type
*os
;
4487 if (map_head_is_link_order
)
4490 for (os
= (void *) lang_os_list
.head
;
4494 asection
*output_section
;
4496 if (os
->constraint
< 0)
4499 output_section
= os
->bfd_section
;
4500 if (output_section
== NULL
)
4503 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
4504 output_section
->map_head
.link_order
= NULL
;
4505 output_section
->map_tail
.link_order
= NULL
;
4508 /* Stop future calls to lang_add_section from messing with map_head
4509 and map_tail link_order fields. */
4510 map_head_is_link_order
= true;
4514 print_output_section_statement
4515 (lang_output_section_statement_type
*output_section_statement
)
4517 asection
*section
= output_section_statement
->bfd_section
;
4520 if (output_section_statement
!= abs_output_section
)
4522 minfo ("\n%s", output_section_statement
->name
);
4524 if (section
!= NULL
)
4526 print_dot
= section
->vma
;
4528 len
= strlen (output_section_statement
->name
);
4529 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
4534 print_spaces (SECTION_NAME_MAP_LENGTH
- len
);
4536 minfo ("0x%V %W", section
->vma
, TO_ADDR (section
->size
));
4538 if (section
->vma
!= section
->lma
)
4539 minfo (_(" load address 0x%V"), section
->lma
);
4541 if (output_section_statement
->update_dot_tree
!= NULL
)
4542 exp_fold_tree (output_section_statement
->update_dot_tree
,
4543 bfd_abs_section_ptr
, &print_dot
);
4549 print_statement_list (output_section_statement
->children
.head
,
4550 output_section_statement
);
4554 print_assignment (lang_assignment_statement_type
*assignment
,
4555 lang_output_section_statement_type
*output_section
)
4561 print_spaces (SECTION_NAME_MAP_LENGTH
);
4563 if (assignment
->exp
->type
.node_class
== etree_assert
)
4566 tree
= assignment
->exp
->assert_s
.child
;
4570 const char *dst
= assignment
->exp
->assign
.dst
;
4572 is_dot
= (dst
[0] == '.' && dst
[1] == 0);
4573 tree
= assignment
->exp
;
4576 osec
= output_section
->bfd_section
;
4578 osec
= bfd_abs_section_ptr
;
4580 if (assignment
->exp
->type
.node_class
!= etree_provide
)
4581 exp_fold_tree (tree
, osec
, &print_dot
);
4583 expld
.result
.valid_p
= false;
4586 const char *str
= buf
;
4587 if (expld
.result
.valid_p
)
4591 if (assignment
->exp
->type
.node_class
== etree_assert
4593 || expld
.assign_name
!= NULL
)
4595 value
= expld
.result
.value
;
4597 if (expld
.result
.section
!= NULL
)
4598 value
+= expld
.result
.section
->vma
;
4602 bfd_sprintf_vma (link_info
.output_bfd
, buf
+ 2, value
);
4608 struct bfd_link_hash_entry
*h
;
4610 h
= bfd_link_hash_lookup (link_info
.hash
, assignment
->exp
->assign
.dst
,
4611 false, false, true);
4613 && (h
->type
== bfd_link_hash_defined
4614 || h
->type
== bfd_link_hash_defweak
))
4616 value
= h
->u
.def
.value
;
4617 value
+= h
->u
.def
.section
->output_section
->vma
;
4618 value
+= h
->u
.def
.section
->output_offset
;
4623 bfd_sprintf_vma (link_info
.output_bfd
, buf
+ 3, value
);
4627 str
= "[unresolved]";
4632 if (assignment
->exp
->type
.node_class
== etree_provide
)
4637 expld
.assign_name
= NULL
;
4639 fprintf (config
.map_file
, "%-34s", str
);
4640 exp_print_tree (assignment
->exp
);
4645 print_input_statement (lang_input_statement_type
*statm
)
4647 if (statm
->filename
!= NULL
)
4648 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
4651 /* Print all symbols defined in a particular section. This is called
4652 via bfd_link_hash_traverse, or by print_all_symbols. */
4655 print_one_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
)
4657 asection
*sec
= (asection
*) ptr
;
4659 if ((hash_entry
->type
== bfd_link_hash_defined
4660 || hash_entry
->type
== bfd_link_hash_defweak
)
4661 && sec
== hash_entry
->u
.def
.section
)
4663 print_spaces (SECTION_NAME_MAP_LENGTH
);
4665 (hash_entry
->u
.def
.value
4666 + hash_entry
->u
.def
.section
->output_offset
4667 + hash_entry
->u
.def
.section
->output_section
->vma
));
4669 minfo (" %pT\n", hash_entry
->root
.string
);
4676 hash_entry_addr_cmp (const void *a
, const void *b
)
4678 const struct bfd_link_hash_entry
*l
= *(const struct bfd_link_hash_entry
**)a
;
4679 const struct bfd_link_hash_entry
*r
= *(const struct bfd_link_hash_entry
**)b
;
4681 if (l
->u
.def
.value
< r
->u
.def
.value
)
4683 else if (l
->u
.def
.value
> r
->u
.def
.value
)
4690 print_all_symbols (asection
*sec
)
4692 input_section_userdata_type
*ud
= bfd_section_userdata (sec
);
4693 struct map_symbol_def
*def
;
4694 struct bfd_link_hash_entry
**entries
;
4700 *ud
->map_symbol_def_tail
= 0;
4702 /* Sort the symbols by address. */
4703 entries
= (struct bfd_link_hash_entry
**)
4704 obstack_alloc (&map_obstack
,
4705 ud
->map_symbol_def_count
* sizeof (*entries
));
4707 for (i
= 0, def
= ud
->map_symbol_def_head
; def
; def
= def
->next
, i
++)
4708 entries
[i
] = def
->entry
;
4710 qsort (entries
, ud
->map_symbol_def_count
, sizeof (*entries
),
4711 hash_entry_addr_cmp
);
4713 /* Print the symbols. */
4714 for (i
= 0; i
< ud
->map_symbol_def_count
; i
++)
4715 ldemul_print_symbol (entries
[i
], sec
);
4717 obstack_free (&map_obstack
, entries
);
4720 /* Print information about an input section to the map file. */
4723 print_input_section (asection
*i
, bool is_discarded
)
4725 bfd_size_type size
= i
->size
;
4731 minfo (" %s", i
->name
);
4733 len
= 1 + strlen (i
->name
);
4734 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
4739 print_spaces (SECTION_NAME_MAP_LENGTH
- len
);
4741 if (i
->output_section
!= NULL
4742 && i
->output_section
->owner
== link_info
.output_bfd
)
4743 addr
= i
->output_section
->vma
+ i
->output_offset
;
4752 bfd_sprintf_vma (link_info
.output_bfd
, buf
, addr
);
4753 minfo ("0x%s %W %pB\n", buf
, TO_ADDR (size
), i
->owner
);
4755 if (size
!= i
->rawsize
&& i
->rawsize
!= 0)
4757 len
= SECTION_NAME_MAP_LENGTH
+ 3 + strlen (buf
);
4759 minfo (_("%W (size before relaxing)\n"), TO_ADDR (i
->rawsize
));
4762 if (i
->output_section
!= NULL
4763 && i
->output_section
->owner
== link_info
.output_bfd
)
4765 if (link_info
.reduce_memory_overheads
)
4766 bfd_link_hash_traverse (link_info
.hash
, ldemul_print_symbol
, i
);
4768 print_all_symbols (i
);
4770 /* Update print_dot, but make sure that we do not move it
4771 backwards - this could happen if we have overlays and a
4772 later overlay is shorter than an earier one. */
4773 if (addr
+ TO_ADDR (size
) > print_dot
)
4774 print_dot
= addr
+ TO_ADDR (size
);
4779 print_fill_statement (lang_fill_statement_type
*fill
)
4783 fputs (" FILL mask 0x", config
.map_file
);
4784 for (p
= fill
->fill
->data
, size
= fill
->fill
->size
; size
!= 0; p
++, size
--)
4785 fprintf (config
.map_file
, "%02x", *p
);
4786 fputs ("\n", config
.map_file
);
4790 print_data_statement (lang_data_statement_type
*data
)
4796 init_opb (data
->output_section
);
4797 print_spaces (SECTION_NAME_MAP_LENGTH
);
4799 addr
= data
->output_offset
;
4800 if (data
->output_section
!= NULL
)
4801 addr
+= data
->output_section
->vma
;
4829 if (size
< TO_SIZE ((unsigned) 1))
4830 size
= TO_SIZE ((unsigned) 1);
4831 minfo ("0x%V %W %s 0x%v", addr
, TO_ADDR (size
), name
, data
->value
);
4833 if (data
->exp
->type
.node_class
!= etree_value
)
4836 exp_print_tree (data
->exp
);
4841 print_dot
= addr
+ TO_ADDR (size
);
4844 /* Print an address statement. These are generated by options like
4848 print_address_statement (lang_address_statement_type
*address
)
4850 minfo (_("Address of section %s set to "), address
->section_name
);
4851 exp_print_tree (address
->address
);
4855 /* Print a reloc statement. */
4858 print_reloc_statement (lang_reloc_statement_type
*reloc
)
4863 init_opb (reloc
->output_section
);
4864 print_spaces (SECTION_NAME_MAP_LENGTH
);
4866 addr
= reloc
->output_offset
;
4867 if (reloc
->output_section
!= NULL
)
4868 addr
+= reloc
->output_section
->vma
;
4870 size
= bfd_get_reloc_size (reloc
->howto
);
4872 minfo ("0x%V %W RELOC %s ", addr
, TO_ADDR (size
), reloc
->howto
->name
);
4874 if (reloc
->name
!= NULL
)
4875 minfo ("%s+", reloc
->name
);
4877 minfo ("%s+", reloc
->section
->name
);
4879 exp_print_tree (reloc
->addend_exp
);
4883 print_dot
= addr
+ TO_ADDR (size
);
4887 print_padding_statement (lang_padding_statement_type
*s
)
4892 init_opb (s
->output_section
);
4895 len
= sizeof " *fill*" - 1;
4896 print_spaces (SECTION_NAME_MAP_LENGTH
- len
);
4898 addr
= s
->output_offset
;
4899 if (s
->output_section
!= NULL
)
4900 addr
+= s
->output_section
->vma
;
4901 minfo ("0x%V %W ", addr
, TO_ADDR (s
->size
));
4903 if (s
->fill
->size
!= 0)
4907 for (p
= s
->fill
->data
, size
= s
->fill
->size
; size
!= 0; p
++, size
--)
4908 fprintf (config
.map_file
, "%02x", *p
);
4913 print_dot
= addr
+ TO_ADDR (s
->size
);
4917 print_wild_statement (lang_wild_statement_type
*w
,
4918 lang_output_section_statement_type
*os
)
4920 struct wildcard_list
*sec
;
4924 if (w
->exclude_name_list
)
4927 minfo ("EXCLUDE_FILE(%s", w
->exclude_name_list
->name
);
4928 for (tmp
= w
->exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
4929 minfo (" %s", tmp
->name
);
4933 if (w
->filenames_sorted
)
4934 minfo ("SORT_BY_NAME(");
4935 if (w
->filename
!= NULL
)
4936 minfo ("%s", w
->filename
);
4939 if (w
->filenames_sorted
)
4943 for (sec
= w
->section_list
; sec
; sec
= sec
->next
)
4945 int closing_paren
= 0;
4947 switch (sec
->spec
.sorted
)
4953 minfo ("SORT_BY_NAME(");
4958 minfo ("SORT_BY_ALIGNMENT(");
4962 case by_name_alignment
:
4963 minfo ("SORT_BY_NAME(SORT_BY_ALIGNMENT(");
4967 case by_alignment_name
:
4968 minfo ("SORT_BY_ALIGNMENT(SORT_BY_NAME(");
4973 minfo ("SORT_NONE(");
4977 case by_init_priority
:
4978 minfo ("SORT_BY_INIT_PRIORITY(");
4983 if (sec
->spec
.exclude_name_list
!= NULL
)
4986 minfo ("EXCLUDE_FILE(%s", sec
->spec
.exclude_name_list
->name
);
4987 for (tmp
= sec
->spec
.exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
4988 minfo (" %s", tmp
->name
);
4991 if (sec
->spec
.name
!= NULL
)
4992 minfo ("%s", sec
->spec
.name
);
4995 for (;closing_paren
> 0; closing_paren
--)
5004 print_statement_list (w
->children
.head
, os
);
5007 /* Print a group statement. */
5010 print_group (lang_group_statement_type
*s
,
5011 lang_output_section_statement_type
*os
)
5013 fprintf (config
.map_file
, "START GROUP\n");
5014 print_statement_list (s
->children
.head
, os
);
5015 fprintf (config
.map_file
, "END GROUP\n");
5018 /* Print the list of statements in S.
5019 This can be called for any statement type. */
5022 print_statement_list (lang_statement_union_type
*s
,
5023 lang_output_section_statement_type
*os
)
5027 print_statement (s
, os
);
5032 /* Print the first statement in statement list S.
5033 This can be called for any statement type. */
5036 print_statement (lang_statement_union_type
*s
,
5037 lang_output_section_statement_type
*os
)
5039 switch (s
->header
.type
)
5042 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
5045 case lang_constructors_statement_enum
:
5046 if (constructor_list
.head
!= NULL
)
5048 if (constructors_sorted
)
5049 minfo (" SORT (CONSTRUCTORS)\n");
5051 minfo (" CONSTRUCTORS\n");
5052 print_statement_list (constructor_list
.head
, os
);
5055 case lang_wild_statement_enum
:
5056 print_wild_statement (&s
->wild_statement
, os
);
5058 case lang_address_statement_enum
:
5059 print_address_statement (&s
->address_statement
);
5061 case lang_object_symbols_statement_enum
:
5062 minfo (" CREATE_OBJECT_SYMBOLS\n");
5064 case lang_fill_statement_enum
:
5065 print_fill_statement (&s
->fill_statement
);
5067 case lang_data_statement_enum
:
5068 print_data_statement (&s
->data_statement
);
5070 case lang_reloc_statement_enum
:
5071 print_reloc_statement (&s
->reloc_statement
);
5073 case lang_input_section_enum
:
5074 print_input_section (s
->input_section
.section
, false);
5076 case lang_padding_statement_enum
:
5077 print_padding_statement (&s
->padding_statement
);
5079 case lang_output_section_statement_enum
:
5080 print_output_section_statement (&s
->output_section_statement
);
5082 case lang_assignment_statement_enum
:
5083 print_assignment (&s
->assignment_statement
, os
);
5085 case lang_target_statement_enum
:
5086 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
5088 case lang_output_statement_enum
:
5089 minfo ("OUTPUT(%s", s
->output_statement
.name
);
5090 if (output_target
!= NULL
)
5091 minfo (" %s", output_target
);
5094 case lang_input_statement_enum
:
5095 print_input_statement (&s
->input_statement
);
5097 case lang_group_statement_enum
:
5098 print_group (&s
->group_statement
, os
);
5100 case lang_insert_statement_enum
:
5101 minfo ("INSERT %s %s\n",
5102 s
->insert_statement
.is_before
? "BEFORE" : "AFTER",
5103 s
->insert_statement
.where
);
5109 print_statements (void)
5111 print_statement_list (statement_list
.head
, abs_output_section
);
5114 /* Print the first N statements in statement list S to STDERR.
5115 If N == 0, nothing is printed.
5116 If N < 0, the entire list is printed.
5117 Intended to be called from GDB. */
5120 dprint_statement (lang_statement_union_type
*s
, int n
)
5122 FILE *map_save
= config
.map_file
;
5124 config
.map_file
= stderr
;
5127 print_statement_list (s
, abs_output_section
);
5130 while (s
&& --n
>= 0)
5132 print_statement (s
, abs_output_section
);
5137 config
.map_file
= map_save
;
5141 insert_pad (lang_statement_union_type
**ptr
,
5143 bfd_size_type alignment_needed
,
5144 asection
*output_section
,
5147 static fill_type zero_fill
;
5148 lang_statement_union_type
*pad
= NULL
;
5150 if (ptr
!= &statement_list
.head
)
5151 pad
= ((lang_statement_union_type
*)
5152 ((char *) ptr
- offsetof (lang_statement_union_type
, header
.next
)));
5154 && pad
->header
.type
== lang_padding_statement_enum
5155 && pad
->padding_statement
.output_section
== output_section
)
5157 /* Use the existing pad statement. */
5159 else if ((pad
= *ptr
) != NULL
5160 && pad
->header
.type
== lang_padding_statement_enum
5161 && pad
->padding_statement
.output_section
== output_section
)
5163 /* Use the existing pad statement. */
5167 /* Make a new padding statement, linked into existing chain. */
5168 pad
= stat_alloc (sizeof (lang_padding_statement_type
));
5169 pad
->header
.next
= *ptr
;
5171 pad
->header
.type
= lang_padding_statement_enum
;
5172 pad
->padding_statement
.output_section
= output_section
;
5175 pad
->padding_statement
.fill
= fill
;
5177 pad
->padding_statement
.output_offset
= dot
- output_section
->vma
;
5178 pad
->padding_statement
.size
= alignment_needed
;
5179 if (!(output_section
->flags
& SEC_FIXED_SIZE
))
5180 output_section
->size
= TO_SIZE (dot
+ TO_ADDR (alignment_needed
)
5181 - output_section
->vma
);
5184 /* Work out how much this section will move the dot point. */
5188 (lang_statement_union_type
**this_ptr
,
5189 lang_output_section_statement_type
*output_section_statement
,
5194 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
5195 asection
*i
= is
->section
;
5196 asection
*o
= output_section_statement
->bfd_section
;
5199 if (link_info
.non_contiguous_regions
)
5201 /* If the input section I has already been successfully assigned
5202 to an output section other than O, don't bother with it and
5203 let the caller remove it from the list. Keep processing in
5204 case we have already handled O, because the repeated passes
5205 have reinitialized its size. */
5206 if (i
->already_assigned
&& i
->already_assigned
!= o
)
5213 if (i
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
5214 i
->output_offset
= i
->vma
- o
->vma
;
5215 else if (((i
->flags
& SEC_EXCLUDE
) != 0)
5216 || output_section_statement
->ignored
)
5217 i
->output_offset
= dot
- o
->vma
;
5220 bfd_size_type alignment_needed
;
5222 /* Align this section first to the input sections requirement,
5223 then to the output section's requirement. If this alignment
5224 is greater than any seen before, then record it too. Perform
5225 the alignment by inserting a magic 'padding' statement. */
5227 if (output_section_statement
->subsection_alignment
!= NULL
)
5229 = exp_get_power (output_section_statement
->subsection_alignment
,
5230 "subsection alignment");
5232 if (o
->alignment_power
< i
->alignment_power
)
5233 o
->alignment_power
= i
->alignment_power
;
5235 alignment_needed
= align_power (dot
, i
->alignment_power
) - dot
;
5237 if (alignment_needed
!= 0)
5239 insert_pad (this_ptr
, fill
, TO_SIZE (alignment_needed
), o
, dot
);
5240 dot
+= alignment_needed
;
5243 if (link_info
.non_contiguous_regions
)
5245 /* If I would overflow O, let the caller remove I from the
5247 if (output_section_statement
->region
)
5249 bfd_vma end
= output_section_statement
->region
->origin
5250 + output_section_statement
->region
->length
;
5252 if (dot
+ TO_ADDR (i
->size
) > end
)
5254 if (i
->flags
& SEC_LINKER_CREATED
)
5255 einfo (_("%F%P: Output section `%pA' not large enough for "
5256 "the linker-created stubs section `%pA'.\n"),
5257 i
->output_section
, i
);
5259 if (i
->rawsize
&& i
->rawsize
!= i
->size
)
5260 einfo (_("%F%P: Relaxation not supported with "
5261 "--enable-non-contiguous-regions (section `%pA' "
5262 "would overflow `%pA' after it changed size).\n"),
5263 i
, i
->output_section
);
5267 i
->output_section
= NULL
;
5273 /* Remember where in the output section this input section goes. */
5274 i
->output_offset
= dot
- o
->vma
;
5276 /* Mark how big the output section must be to contain this now. */
5277 dot
+= TO_ADDR (i
->size
);
5278 if (!(o
->flags
& SEC_FIXED_SIZE
))
5279 o
->size
= TO_SIZE (dot
- o
->vma
);
5281 if (link_info
.non_contiguous_regions
)
5283 /* Record that I was successfully assigned to O, and update
5284 its actual output section too. */
5285 i
->already_assigned
= o
;
5286 i
->output_section
= o
;
5300 sort_sections_by_lma (const void *arg1
, const void *arg2
)
5302 const asection
*sec1
= ((const struct check_sec
*) arg1
)->sec
;
5303 const asection
*sec2
= ((const struct check_sec
*) arg2
)->sec
;
5305 if (sec1
->lma
< sec2
->lma
)
5307 else if (sec1
->lma
> sec2
->lma
)
5309 else if (sec1
->id
< sec2
->id
)
5311 else if (sec1
->id
> sec2
->id
)
5318 sort_sections_by_vma (const void *arg1
, const void *arg2
)
5320 const asection
*sec1
= ((const struct check_sec
*) arg1
)->sec
;
5321 const asection
*sec2
= ((const struct check_sec
*) arg2
)->sec
;
5323 if (sec1
->vma
< sec2
->vma
)
5325 else if (sec1
->vma
> sec2
->vma
)
5327 else if (sec1
->id
< sec2
->id
)
5329 else if (sec1
->id
> sec2
->id
)
5335 #define IS_TBSS(s) \
5336 ((s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == SEC_THREAD_LOCAL)
5338 #define IGNORE_SECTION(s) \
5339 ((s->flags & SEC_ALLOC) == 0 || IS_TBSS (s))
5341 /* Check to see if any allocated sections overlap with other allocated
5342 sections. This can happen if a linker script specifies the output
5343 section addresses of the two sections. Also check whether any memory
5344 region has overflowed. */
5347 lang_check_section_addresses (void)
5350 struct check_sec
*sections
;
5355 bfd_vma p_start
= 0;
5357 lang_memory_region_type
*m
;
5360 /* Detect address space overflow on allocated sections. */
5361 addr_mask
= ((bfd_vma
) 1 <<
5362 (bfd_arch_bits_per_address (link_info
.output_bfd
) - 1)) - 1;
5363 addr_mask
= (addr_mask
<< 1) + 1;
5364 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5365 if ((s
->flags
& SEC_ALLOC
) != 0)
5367 s_end
= (s
->vma
+ s
->size
) & addr_mask
;
5368 if (s_end
!= 0 && s_end
< (s
->vma
& addr_mask
))
5369 einfo (_("%X%P: section %s VMA wraps around address space\n"),
5373 s_end
= (s
->lma
+ s
->size
) & addr_mask
;
5374 if (s_end
!= 0 && s_end
< (s
->lma
& addr_mask
))
5375 einfo (_("%X%P: section %s LMA wraps around address space\n"),
5380 if (bfd_count_sections (link_info
.output_bfd
) <= 1)
5383 count
= bfd_count_sections (link_info
.output_bfd
);
5384 sections
= XNEWVEC (struct check_sec
, count
);
5386 /* Scan all sections in the output list. */
5388 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5390 if (IGNORE_SECTION (s
)
5394 sections
[count
].sec
= s
;
5395 sections
[count
].warned
= false;
5405 qsort (sections
, count
, sizeof (*sections
), sort_sections_by_lma
);
5407 /* First check section LMAs. There should be no overlap of LMAs on
5408 loadable sections, even with overlays. */
5409 for (p
= NULL
, i
= 0; i
< count
; i
++)
5411 s
= sections
[i
].sec
;
5413 if ((s
->flags
& SEC_LOAD
) != 0)
5416 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
5418 /* Look for an overlap. We have sorted sections by lma, so
5419 we know that s_start >= p_start. Besides the obvious
5420 case of overlap when the current section starts before
5421 the previous one ends, we also must have overlap if the
5422 previous section wraps around the address space. */
5424 && (s_start
<= p_end
5425 || p_end
< p_start
))
5427 einfo (_("%X%P: section %s LMA [%V,%V]"
5428 " overlaps section %s LMA [%V,%V]\n"),
5429 s
->name
, s_start
, s_end
, p
->name
, p_start
, p_end
);
5430 sections
[i
].warned
= true;
5438 /* If any non-zero size allocated section (excluding tbss) starts at
5439 exactly the same VMA as another such section, then we have
5440 overlays. Overlays generated by the OVERLAY keyword will have
5441 this property. It is possible to intentionally generate overlays
5442 that fail this test, but it would be unusual. */
5443 qsort (sections
, count
, sizeof (*sections
), sort_sections_by_vma
);
5445 p_start
= sections
[0].sec
->vma
;
5446 for (i
= 1; i
< count
; i
++)
5448 s_start
= sections
[i
].sec
->vma
;
5449 if (p_start
== s_start
)
5457 /* Now check section VMAs if no overlays were detected. */
5460 for (p
= NULL
, i
= 0; i
< count
; i
++)
5462 s
= sections
[i
].sec
;
5465 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
5468 && !sections
[i
].warned
5469 && (s_start
<= p_end
5470 || p_end
< p_start
))
5471 einfo (_("%X%P: section %s VMA [%V,%V]"
5472 " overlaps section %s VMA [%V,%V]\n"),
5473 s
->name
, s_start
, s_end
, p
->name
, p_start
, p_end
);
5482 /* If any memory region has overflowed, report by how much.
5483 We do not issue this diagnostic for regions that had sections
5484 explicitly placed outside their bounds; os_region_check's
5485 diagnostics are adequate for that case.
5487 FIXME: It is conceivable that m->current - (m->origin + m->length)
5488 might overflow a 32-bit integer. There is, alas, no way to print
5489 a bfd_vma quantity in decimal. */
5490 for (m
= lang_memory_region_list
; m
; m
= m
->next
)
5491 if (m
->had_full_message
)
5493 unsigned long over
= m
->current
- (m
->origin
+ m
->length
);
5494 einfo (ngettext ("%X%P: region `%s' overflowed by %lu byte\n",
5495 "%X%P: region `%s' overflowed by %lu bytes\n",
5497 m
->name_list
.name
, over
);
5501 /* Make sure the new address is within the region. We explicitly permit the
5502 current address to be at the exact end of the region when the address is
5503 non-zero, in case the region is at the end of addressable memory and the
5504 calculation wraps around. */
5507 os_region_check (lang_output_section_statement_type
*os
,
5508 lang_memory_region_type
*region
,
5512 if ((region
->current
< region
->origin
5513 || (region
->current
- region
->origin
> region
->length
))
5514 && ((region
->current
!= region
->origin
+ region
->length
)
5519 einfo (_("%X%P: address 0x%v of %pB section `%s'"
5520 " is not within region `%s'\n"),
5522 os
->bfd_section
->owner
,
5523 os
->bfd_section
->name
,
5524 region
->name_list
.name
);
5526 else if (!region
->had_full_message
)
5528 region
->had_full_message
= true;
5530 einfo (_("%X%P: %pB section `%s' will not fit in region `%s'\n"),
5531 os
->bfd_section
->owner
,
5532 os
->bfd_section
->name
,
5533 region
->name_list
.name
);
5539 ldlang_check_relro_region (lang_statement_union_type
*s
)
5541 seg_align_type
*seg
= &expld
.dataseg
;
5543 if (seg
->relro
== exp_seg_relro_start
)
5545 if (!seg
->relro_start_stat
)
5546 seg
->relro_start_stat
= s
;
5549 ASSERT (seg
->relro_start_stat
== s
);
5552 else if (seg
->relro
== exp_seg_relro_end
)
5554 if (!seg
->relro_end_stat
)
5555 seg
->relro_end_stat
= s
;
5558 ASSERT (seg
->relro_end_stat
== s
);
5563 /* Set the sizes for all the output sections. */
5566 lang_size_sections_1
5567 (lang_statement_union_type
**prev
,
5568 lang_output_section_statement_type
*output_section_statement
,
5574 lang_statement_union_type
*s
;
5575 lang_statement_union_type
*prev_s
= NULL
;
5576 bool removed_prev_s
= false;
5578 /* Size up the sections from their constituent parts. */
5579 for (s
= *prev
; s
!= NULL
; prev_s
= s
, s
= s
->header
.next
)
5581 bool removed
= false;
5583 switch (s
->header
.type
)
5585 case lang_output_section_statement_enum
:
5587 bfd_vma newdot
, after
, dotdelta
;
5588 lang_output_section_statement_type
*os
;
5589 lang_memory_region_type
*r
;
5590 int section_alignment
= 0;
5592 os
= &s
->output_section_statement
;
5593 init_opb (os
->bfd_section
);
5594 if (os
->constraint
== -1)
5597 /* FIXME: We shouldn't need to zero section vmas for ld -r
5598 here, in lang_insert_orphan, or in the default linker scripts.
5599 This is covering for coff backend linker bugs. See PR6945. */
5600 if (os
->addr_tree
== NULL
5601 && bfd_link_relocatable (&link_info
)
5602 && (bfd_get_flavour (link_info
.output_bfd
)
5603 == bfd_target_coff_flavour
))
5604 os
->addr_tree
= exp_intop (0);
5605 if (os
->addr_tree
!= NULL
)
5607 exp_fold_tree (os
->addr_tree
, bfd_abs_section_ptr
, &dot
);
5609 if (expld
.result
.valid_p
)
5611 dot
= expld
.result
.value
;
5612 if (expld
.result
.section
!= NULL
)
5613 dot
+= expld
.result
.section
->vma
;
5615 else if (expld
.phase
!= lang_mark_phase_enum
)
5616 einfo (_("%F%P:%pS: non constant or forward reference"
5617 " address expression for section %s\n"),
5618 os
->addr_tree
, os
->name
);
5621 if (os
->bfd_section
== NULL
)
5622 /* This section was removed or never actually created. */
5625 /* If this is a COFF shared library section, use the size and
5626 address from the input section. FIXME: This is COFF
5627 specific; it would be cleaner if there were some other way
5628 to do this, but nothing simple comes to mind. */
5629 if (((bfd_get_flavour (link_info
.output_bfd
)
5630 == bfd_target_ecoff_flavour
)
5631 || (bfd_get_flavour (link_info
.output_bfd
)
5632 == bfd_target_coff_flavour
))
5633 && (os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
5637 if (os
->children
.head
== NULL
5638 || os
->children
.head
->header
.next
!= NULL
5639 || (os
->children
.head
->header
.type
5640 != lang_input_section_enum
))
5641 einfo (_("%X%P: internal error on COFF shared library"
5642 " section %s\n"), os
->name
);
5644 input
= os
->children
.head
->input_section
.section
;
5645 bfd_set_section_vma (os
->bfd_section
,
5646 bfd_section_vma (input
));
5647 if (!(os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
5648 os
->bfd_section
->size
= input
->size
;
5654 if (bfd_is_abs_section (os
->bfd_section
))
5656 /* No matter what happens, an abs section starts at zero. */
5657 ASSERT (os
->bfd_section
->vma
== 0);
5661 if (os
->addr_tree
== NULL
)
5663 /* No address specified for this section, get one
5664 from the region specification. */
5665 if (os
->region
== NULL
5666 || ((os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))
5667 && os
->region
->name_list
.name
[0] == '*'
5668 && strcmp (os
->region
->name_list
.name
,
5669 DEFAULT_MEMORY_REGION
) == 0))
5671 os
->region
= lang_memory_default (os
->bfd_section
);
5674 /* If a loadable section is using the default memory
5675 region, and some non default memory regions were
5676 defined, issue an error message. */
5678 && !IGNORE_SECTION (os
->bfd_section
)
5679 && !bfd_link_relocatable (&link_info
)
5681 && strcmp (os
->region
->name_list
.name
,
5682 DEFAULT_MEMORY_REGION
) == 0
5683 && lang_memory_region_list
!= NULL
5684 && (strcmp (lang_memory_region_list
->name_list
.name
,
5685 DEFAULT_MEMORY_REGION
) != 0
5686 || lang_memory_region_list
->next
!= NULL
)
5687 && lang_sizing_iteration
== 1)
5689 /* By default this is an error rather than just a
5690 warning because if we allocate the section to the
5691 default memory region we can end up creating an
5692 excessively large binary, or even seg faulting when
5693 attempting to perform a negative seek. See
5694 sources.redhat.com/ml/binutils/2003-04/msg00423.html
5695 for an example of this. This behaviour can be
5696 overridden by the using the --no-check-sections
5698 if (command_line
.check_section_addresses
)
5699 einfo (_("%F%P: error: no memory region specified"
5700 " for loadable section `%s'\n"),
5701 bfd_section_name (os
->bfd_section
));
5703 einfo (_("%P: warning: no memory region specified"
5704 " for loadable section `%s'\n"),
5705 bfd_section_name (os
->bfd_section
));
5708 newdot
= os
->region
->current
;
5709 section_alignment
= os
->bfd_section
->alignment_power
;
5712 section_alignment
= exp_get_power (os
->section_alignment
,
5713 "section alignment");
5715 /* Align to what the section needs. */
5716 if (section_alignment
> 0)
5718 bfd_vma savedot
= newdot
;
5721 newdot
= align_power (newdot
, section_alignment
);
5722 dotdelta
= newdot
- savedot
;
5724 if (lang_sizing_iteration
== 1)
5726 else if (lang_sizing_iteration
> 1)
5728 /* Only report adjustments that would change
5729 alignment from what we have already reported. */
5730 diff
= newdot
- os
->bfd_section
->vma
;
5731 if (!(diff
& (((bfd_vma
) 1 << section_alignment
) - 1)))
5735 && (config
.warn_section_align
5736 || os
->addr_tree
!= NULL
))
5737 einfo (_("%P: warning: "
5738 "start of section %s changed by %ld\n"),
5739 os
->name
, (long) diff
);
5742 bfd_set_section_vma (os
->bfd_section
, newdot
);
5744 os
->bfd_section
->output_offset
= 0;
5747 lang_size_sections_1 (&os
->children
.head
, os
,
5748 os
->fill
, newdot
, relax
, check_regions
);
5750 os
->processed_vma
= true;
5752 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
5753 /* Except for some special linker created sections,
5754 no output section should change from zero size
5755 after strip_excluded_output_sections. A non-zero
5756 size on an ignored section indicates that some
5757 input section was not sized early enough. */
5758 ASSERT (os
->bfd_section
->size
== 0);
5761 dot
= os
->bfd_section
->vma
;
5763 /* Put the section within the requested block size, or
5764 align at the block boundary. */
5766 + TO_ADDR (os
->bfd_section
->size
)
5767 + os
->block_value
- 1)
5768 & - (bfd_vma
) os
->block_value
);
5770 if (!(os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
5771 os
->bfd_section
->size
= TO_SIZE (after
5772 - os
->bfd_section
->vma
);
5775 /* Set section lma. */
5778 r
= lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, false);
5782 bfd_vma lma
= exp_get_abs_int (os
->load_base
, 0, "load base");
5783 os
->bfd_section
->lma
= lma
;
5785 else if (os
->lma_region
!= NULL
)
5787 bfd_vma lma
= os
->lma_region
->current
;
5789 if (os
->align_lma_with_input
)
5793 /* When LMA_REGION is the same as REGION, align the LMA
5794 as we did for the VMA, possibly including alignment
5795 from the bfd section. If a different region, then
5796 only align according to the value in the output
5798 if (os
->lma_region
!= os
->region
)
5799 section_alignment
= exp_get_power (os
->section_alignment
,
5800 "section alignment");
5801 if (section_alignment
> 0)
5802 lma
= align_power (lma
, section_alignment
);
5804 os
->bfd_section
->lma
= lma
;
5806 else if (r
->last_os
!= NULL
5807 && (os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
5812 last
= r
->last_os
->output_section_statement
.bfd_section
;
5814 /* A backwards move of dot should be accompanied by
5815 an explicit assignment to the section LMA (ie.
5816 os->load_base set) because backwards moves can
5817 create overlapping LMAs. */
5819 && os
->bfd_section
->size
!= 0
5820 && dot
+ TO_ADDR (os
->bfd_section
->size
) <= last
->vma
)
5822 /* If dot moved backwards then leave lma equal to
5823 vma. This is the old default lma, which might
5824 just happen to work when the backwards move is
5825 sufficiently large. Nag if this changes anything,
5826 so people can fix their linker scripts. */
5828 if (last
->vma
!= last
->lma
)
5829 einfo (_("%P: warning: dot moved backwards "
5830 "before `%s'\n"), os
->name
);
5834 /* If this is an overlay, set the current lma to that
5835 at the end of the previous section. */
5836 if (os
->sectype
== overlay_section
)
5837 lma
= last
->lma
+ TO_ADDR (last
->size
);
5839 /* Otherwise, keep the same lma to vma relationship
5840 as the previous section. */
5842 lma
= os
->bfd_section
->vma
+ last
->lma
- last
->vma
;
5844 if (section_alignment
> 0)
5845 lma
= align_power (lma
, section_alignment
);
5846 os
->bfd_section
->lma
= lma
;
5849 os
->processed_lma
= true;
5851 /* Keep track of normal sections using the default
5852 lma region. We use this to set the lma for
5853 following sections. Overlays or other linker
5854 script assignment to lma might mean that the
5855 default lma == vma is incorrect.
5856 To avoid warnings about dot moving backwards when using
5857 -Ttext, don't start tracking sections until we find one
5858 of non-zero size or with lma set differently to vma.
5859 Do this tracking before we short-cut the loop so that we
5860 track changes for the case where the section size is zero,
5861 but the lma is set differently to the vma. This is
5862 important, if an orphan section is placed after an
5863 otherwise empty output section that has an explicit lma
5864 set, we want that lma reflected in the orphans lma. */
5865 if (((!IGNORE_SECTION (os
->bfd_section
)
5866 && (os
->bfd_section
->size
!= 0
5867 || (r
->last_os
== NULL
5868 && os
->bfd_section
->vma
!= os
->bfd_section
->lma
)
5869 || (r
->last_os
!= NULL
5870 && dot
>= (r
->last_os
->output_section_statement
5871 .bfd_section
->vma
))))
5872 || os
->sectype
== first_overlay_section
)
5873 && os
->lma_region
== NULL
5874 && !bfd_link_relocatable (&link_info
))
5877 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
5880 /* .tbss sections effectively have zero size. */
5881 if (!IS_TBSS (os
->bfd_section
)
5882 || bfd_link_relocatable (&link_info
))
5883 dotdelta
= TO_ADDR (os
->bfd_section
->size
);
5888 if (os
->update_dot_tree
!= 0)
5889 exp_fold_tree (os
->update_dot_tree
, bfd_abs_section_ptr
, &dot
);
5891 /* Update dot in the region ?
5892 We only do this if the section is going to be allocated,
5893 since unallocated sections do not contribute to the region's
5894 overall size in memory. */
5895 if (os
->region
!= NULL
5896 && (os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
)))
5898 os
->region
->current
= dot
;
5901 /* Make sure the new address is within the region. */
5902 os_region_check (os
, os
->region
, os
->addr_tree
,
5903 os
->bfd_section
->vma
);
5905 if (os
->lma_region
!= NULL
&& os
->lma_region
!= os
->region
5906 && ((os
->bfd_section
->flags
& SEC_LOAD
)
5907 || os
->align_lma_with_input
))
5909 os
->lma_region
->current
= os
->bfd_section
->lma
+ dotdelta
;
5912 os_region_check (os
, os
->lma_region
, NULL
,
5913 os
->bfd_section
->lma
);
5919 case lang_constructors_statement_enum
:
5920 dot
= lang_size_sections_1 (&constructor_list
.head
,
5921 output_section_statement
,
5922 fill
, dot
, relax
, check_regions
);
5925 case lang_data_statement_enum
:
5927 unsigned int size
= 0;
5929 s
->data_statement
.output_offset
=
5930 dot
- output_section_statement
->bfd_section
->vma
;
5931 s
->data_statement
.output_section
=
5932 output_section_statement
->bfd_section
;
5934 /* We might refer to provided symbols in the expression, and
5935 need to mark them as needed. */
5936 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
5938 switch (s
->data_statement
.type
)
5956 if (size
< TO_SIZE ((unsigned) 1))
5957 size
= TO_SIZE ((unsigned) 1);
5958 dot
+= TO_ADDR (size
);
5959 if (!(output_section_statement
->bfd_section
->flags
5961 output_section_statement
->bfd_section
->size
5962 = TO_SIZE (dot
- output_section_statement
->bfd_section
->vma
);
5967 case lang_reloc_statement_enum
:
5971 s
->reloc_statement
.output_offset
=
5972 dot
- output_section_statement
->bfd_section
->vma
;
5973 s
->reloc_statement
.output_section
=
5974 output_section_statement
->bfd_section
;
5975 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
5976 dot
+= TO_ADDR (size
);
5977 if (!(output_section_statement
->bfd_section
->flags
5979 output_section_statement
->bfd_section
->size
5980 = TO_SIZE (dot
- output_section_statement
->bfd_section
->vma
);
5984 case lang_wild_statement_enum
:
5985 dot
= lang_size_sections_1 (&s
->wild_statement
.children
.head
,
5986 output_section_statement
,
5987 fill
, dot
, relax
, check_regions
);
5990 case lang_object_symbols_statement_enum
:
5991 link_info
.create_object_symbols_section
5992 = output_section_statement
->bfd_section
;
5993 output_section_statement
->bfd_section
->flags
|= SEC_KEEP
;
5996 case lang_output_statement_enum
:
5997 case lang_target_statement_enum
:
6000 case lang_input_section_enum
:
6004 i
= s
->input_section
.section
;
6009 if (!bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
6010 einfo (_("%F%P: can't relax section: %E\n"));
6014 dot
= size_input_section (prev
, output_section_statement
,
6015 fill
, &removed
, dot
);
6019 case lang_input_statement_enum
:
6022 case lang_fill_statement_enum
:
6023 s
->fill_statement
.output_section
=
6024 output_section_statement
->bfd_section
;
6026 fill
= s
->fill_statement
.fill
;
6029 case lang_assignment_statement_enum
:
6031 bfd_vma newdot
= dot
;
6032 etree_type
*tree
= s
->assignment_statement
.exp
;
6034 expld
.dataseg
.relro
= exp_seg_relro_none
;
6036 exp_fold_tree (tree
,
6037 output_section_statement
->bfd_section
,
6040 ldlang_check_relro_region (s
);
6042 expld
.dataseg
.relro
= exp_seg_relro_none
;
6044 /* This symbol may be relative to this section. */
6045 if ((tree
->type
.node_class
== etree_provided
6046 || tree
->type
.node_class
== etree_assign
)
6047 && (tree
->assign
.dst
[0] != '.'
6048 || tree
->assign
.dst
[1] != '\0'))
6049 output_section_statement
->update_dot
= 1;
6051 if (!output_section_statement
->ignored
)
6053 if (output_section_statement
== abs_output_section
)
6055 /* If we don't have an output section, then just adjust
6056 the default memory address. */
6057 lang_memory_region_lookup (DEFAULT_MEMORY_REGION
,
6058 false)->current
= newdot
;
6060 else if (newdot
!= dot
)
6062 /* Insert a pad after this statement. We can't
6063 put the pad before when relaxing, in case the
6064 assignment references dot. */
6065 insert_pad (&s
->header
.next
, fill
, TO_SIZE (newdot
- dot
),
6066 output_section_statement
->bfd_section
, dot
);
6068 /* Don't neuter the pad below when relaxing. */
6071 /* If dot is advanced, this implies that the section
6072 should have space allocated to it, unless the
6073 user has explicitly stated that the section
6074 should not be allocated. */
6075 if (output_section_statement
->sectype
!= noalloc_section
6076 && (output_section_statement
->sectype
!= noload_section
6077 || (bfd_get_flavour (link_info
.output_bfd
)
6078 == bfd_target_elf_flavour
)))
6079 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
;
6086 case lang_padding_statement_enum
:
6087 /* If this is the first time lang_size_sections is called,
6088 we won't have any padding statements. If this is the
6089 second or later passes when relaxing, we should allow
6090 padding to shrink. If padding is needed on this pass, it
6091 will be added back in. */
6092 s
->padding_statement
.size
= 0;
6094 /* Make sure output_offset is valid. If relaxation shrinks
6095 the section and this pad isn't needed, it's possible to
6096 have output_offset larger than the final size of the
6097 section. bfd_set_section_contents will complain even for
6098 a pad size of zero. */
6099 s
->padding_statement
.output_offset
6100 = dot
- output_section_statement
->bfd_section
->vma
;
6103 case lang_group_statement_enum
:
6104 dot
= lang_size_sections_1 (&s
->group_statement
.children
.head
,
6105 output_section_statement
,
6106 fill
, dot
, relax
, check_regions
);
6109 case lang_insert_statement_enum
:
6112 /* We can only get here when relaxing is turned on. */
6113 case lang_address_statement_enum
:
6121 /* If an input section doesn't fit in the current output
6122 section, remove it from the list. Handle the case where we
6123 have to remove an input_section statement here: there is a
6124 special case to remove the first element of the list. */
6125 if (link_info
.non_contiguous_regions
&& removed
)
6127 /* If we removed the first element during the previous
6128 iteration, override the loop assignment of prev_s. */
6134 /* If there was a real previous input section, just skip
6136 prev_s
->header
.next
=s
->header
.next
;
6138 removed_prev_s
= false;
6142 /* Remove the first input section of the list. */
6143 *prev
= s
->header
.next
;
6144 removed_prev_s
= true;
6147 /* Move to next element, unless we removed the head of the
6149 if (!removed_prev_s
)
6150 prev
= &s
->header
.next
;
6154 prev
= &s
->header
.next
;
6155 removed_prev_s
= false;
6161 /* Callback routine that is used in _bfd_elf_map_sections_to_segments.
6162 The BFD library has set NEW_SEGMENT to TRUE iff it thinks that
6163 CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different
6164 segments. We are allowed an opportunity to override this decision. */
6167 ldlang_override_segment_assignment (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6168 bfd
*abfd ATTRIBUTE_UNUSED
,
6169 asection
*current_section
,
6170 asection
*previous_section
,
6173 lang_output_section_statement_type
*cur
;
6174 lang_output_section_statement_type
*prev
;
6176 /* The checks below are only necessary when the BFD library has decided
6177 that the two sections ought to be placed into the same segment. */
6181 /* Paranoia checks. */
6182 if (current_section
== NULL
|| previous_section
== NULL
)
6185 /* If this flag is set, the target never wants code and non-code
6186 sections comingled in the same segment. */
6187 if (config
.separate_code
6188 && ((current_section
->flags
^ previous_section
->flags
) & SEC_CODE
))
6191 /* Find the memory regions associated with the two sections.
6192 We call lang_output_section_find() here rather than scanning the list
6193 of output sections looking for a matching section pointer because if
6194 we have a large number of sections then a hash lookup is faster. */
6195 cur
= lang_output_section_find (current_section
->name
);
6196 prev
= lang_output_section_find (previous_section
->name
);
6198 /* More paranoia. */
6199 if (cur
== NULL
|| prev
== NULL
)
6202 /* If the regions are different then force the sections to live in
6203 different segments. See the email thread starting at the following
6204 URL for the reasons why this is necessary:
6205 http://sourceware.org/ml/binutils/2007-02/msg00216.html */
6206 return cur
->region
!= prev
->region
;
6210 one_lang_size_sections_pass (bool *relax
, bool check_regions
)
6212 lang_statement_iteration
++;
6213 if (expld
.phase
!= lang_mark_phase_enum
)
6214 lang_sizing_iteration
++;
6215 lang_size_sections_1 (&statement_list
.head
, abs_output_section
,
6216 0, 0, relax
, check_regions
);
6220 lang_size_segment (void)
6222 /* If XXX_SEGMENT_ALIGN XXX_SEGMENT_END pair was seen, check whether
6223 a page could be saved in the data segment. */
6224 seg_align_type
*seg
= &expld
.dataseg
;
6225 bfd_vma first
, last
;
6227 first
= -seg
->base
& (seg
->commonpagesize
- 1);
6228 last
= seg
->end
& (seg
->commonpagesize
- 1);
6230 && ((seg
->base
& ~(seg
->commonpagesize
- 1))
6231 != (seg
->end
& ~(seg
->commonpagesize
- 1)))
6232 && first
+ last
<= seg
->commonpagesize
)
6234 seg
->phase
= exp_seg_adjust
;
6238 seg
->phase
= exp_seg_done
;
6243 lang_size_relro_segment_1 (void)
6245 seg_align_type
*seg
= &expld
.dataseg
;
6246 bfd_vma relro_end
, desired_end
;
6249 /* Compute the expected PT_GNU_RELRO/PT_LOAD segment end. */
6250 relro_end
= (seg
->relro_end
+ seg
->relropagesize
- 1) & -seg
->relropagesize
;
6252 /* Adjust by the offset arg of XXX_SEGMENT_RELRO_END. */
6253 desired_end
= relro_end
- seg
->relro_offset
;
6255 /* For sections in the relro segment.. */
6256 for (sec
= link_info
.output_bfd
->section_last
; sec
; sec
= sec
->prev
)
6257 if ((sec
->flags
& SEC_ALLOC
) != 0
6258 && sec
->vma
>= seg
->base
6259 && sec
->vma
< seg
->relro_end
- seg
->relro_offset
)
6261 /* Where do we want to put this section so that it ends as
6263 bfd_vma start
, end
, bump
;
6265 end
= start
= sec
->vma
;
6267 end
+= TO_ADDR (sec
->size
);
6268 bump
= desired_end
- end
;
6269 /* We'd like to increase START by BUMP, but we must heed
6270 alignment so the increase might be less than optimum. */
6272 start
&= ~(((bfd_vma
) 1 << sec
->alignment_power
) - 1);
6273 /* This is now the desired end for the previous section. */
6274 desired_end
= start
;
6277 seg
->phase
= exp_seg_relro_adjust
;
6278 ASSERT (desired_end
>= seg
->base
);
6279 seg
->base
= desired_end
;
6284 lang_size_relro_segment (bool *relax
, bool check_regions
)
6286 bool do_reset
= false;
6288 if (link_info
.relro
&& expld
.dataseg
.relro_end
)
6290 bfd_vma data_initial_base
= expld
.dataseg
.base
;
6291 bfd_vma data_relro_end
= lang_size_relro_segment_1 ();
6293 lang_reset_memory_regions ();
6294 one_lang_size_sections_pass (relax
, check_regions
);
6296 /* Assignments to dot, or to output section address in a user
6297 script have increased padding over the original. Revert. */
6298 if (expld
.dataseg
.relro_end
> data_relro_end
)
6300 expld
.dataseg
.base
= data_initial_base
;
6304 else if (lang_size_segment ())
6311 lang_size_sections (bool *relax
, bool check_regions
)
6313 expld
.phase
= lang_allocating_phase_enum
;
6314 expld
.dataseg
.phase
= exp_seg_none
;
6316 one_lang_size_sections_pass (relax
, check_regions
);
6318 if (expld
.dataseg
.phase
!= exp_seg_end_seen
)
6319 expld
.dataseg
.phase
= exp_seg_done
;
6321 if (expld
.dataseg
.phase
== exp_seg_end_seen
)
6324 = lang_size_relro_segment (relax
, check_regions
);
6328 lang_reset_memory_regions ();
6329 one_lang_size_sections_pass (relax
, check_regions
);
6332 if (link_info
.relro
&& expld
.dataseg
.relro_end
)
6334 link_info
.relro_start
= expld
.dataseg
.base
;
6335 link_info
.relro_end
= expld
.dataseg
.relro_end
;
6340 static lang_output_section_statement_type
*current_section
;
6341 static lang_assignment_statement_type
*current_assign
;
6342 static bool prefer_next_section
;
6344 /* Worker function for lang_do_assignments. Recursiveness goes here. */
6347 lang_do_assignments_1 (lang_statement_union_type
*s
,
6348 lang_output_section_statement_type
*current_os
,
6353 for (; s
!= NULL
; s
= s
->header
.next
)
6355 switch (s
->header
.type
)
6357 case lang_constructors_statement_enum
:
6358 dot
= lang_do_assignments_1 (constructor_list
.head
,
6359 current_os
, fill
, dot
, found_end
);
6362 case lang_output_section_statement_enum
:
6364 lang_output_section_statement_type
*os
;
6367 os
= &(s
->output_section_statement
);
6368 os
->after_end
= *found_end
;
6369 init_opb (os
->bfd_section
);
6371 if (os
->bfd_section
!= NULL
)
6373 if (!os
->ignored
&& (os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
6375 current_section
= os
;
6376 prefer_next_section
= false;
6378 newdot
= os
->bfd_section
->vma
;
6380 newdot
= lang_do_assignments_1 (os
->children
.head
,
6381 os
, os
->fill
, newdot
, found_end
);
6384 if (os
->bfd_section
!= NULL
)
6386 newdot
= os
->bfd_section
->vma
;
6388 /* .tbss sections effectively have zero size. */
6389 if (!IS_TBSS (os
->bfd_section
)
6390 || bfd_link_relocatable (&link_info
))
6391 newdot
+= TO_ADDR (os
->bfd_section
->size
);
6393 if (os
->update_dot_tree
!= NULL
)
6394 exp_fold_tree (os
->update_dot_tree
,
6395 bfd_abs_section_ptr
, &newdot
);
6402 case lang_wild_statement_enum
:
6404 dot
= lang_do_assignments_1 (s
->wild_statement
.children
.head
,
6405 current_os
, fill
, dot
, found_end
);
6408 case lang_object_symbols_statement_enum
:
6409 case lang_output_statement_enum
:
6410 case lang_target_statement_enum
:
6413 case lang_data_statement_enum
:
6414 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
6415 if (expld
.result
.valid_p
)
6417 s
->data_statement
.value
= expld
.result
.value
;
6418 if (expld
.result
.section
!= NULL
)
6419 s
->data_statement
.value
+= expld
.result
.section
->vma
;
6421 else if (expld
.phase
== lang_final_phase_enum
)
6422 einfo (_("%F%P: invalid data statement\n"));
6425 switch (s
->data_statement
.type
)
6443 if (size
< TO_SIZE ((unsigned) 1))
6444 size
= TO_SIZE ((unsigned) 1);
6445 dot
+= TO_ADDR (size
);
6449 case lang_reloc_statement_enum
:
6450 exp_fold_tree (s
->reloc_statement
.addend_exp
,
6451 bfd_abs_section_ptr
, &dot
);
6452 if (expld
.result
.valid_p
)
6453 s
->reloc_statement
.addend_value
= expld
.result
.value
;
6454 else if (expld
.phase
== lang_final_phase_enum
)
6455 einfo (_("%F%P: invalid reloc statement\n"));
6456 dot
+= TO_ADDR (bfd_get_reloc_size (s
->reloc_statement
.howto
));
6459 case lang_input_section_enum
:
6461 asection
*in
= s
->input_section
.section
;
6463 if ((in
->flags
& SEC_EXCLUDE
) == 0)
6464 dot
+= TO_ADDR (in
->size
);
6468 case lang_input_statement_enum
:
6471 case lang_fill_statement_enum
:
6472 fill
= s
->fill_statement
.fill
;
6475 case lang_assignment_statement_enum
:
6476 current_assign
= &s
->assignment_statement
;
6477 if (current_assign
->exp
->type
.node_class
!= etree_assert
)
6479 const char *p
= current_assign
->exp
->assign
.dst
;
6481 if (current_os
== abs_output_section
&& p
[0] == '.' && p
[1] == 0)
6482 prefer_next_section
= true;
6486 if (strcmp (p
, "end") == 0)
6489 exp_fold_tree (s
->assignment_statement
.exp
,
6490 (current_os
->bfd_section
!= NULL
6491 ? current_os
->bfd_section
: bfd_und_section_ptr
),
6495 case lang_padding_statement_enum
:
6496 dot
+= TO_ADDR (s
->padding_statement
.size
);
6499 case lang_group_statement_enum
:
6500 dot
= lang_do_assignments_1 (s
->group_statement
.children
.head
,
6501 current_os
, fill
, dot
, found_end
);
6504 case lang_insert_statement_enum
:
6507 case lang_address_statement_enum
:
6519 lang_do_assignments (lang_phase_type phase
)
6521 bool found_end
= false;
6523 current_section
= NULL
;
6524 prefer_next_section
= false;
6525 expld
.phase
= phase
;
6526 lang_statement_iteration
++;
6527 lang_do_assignments_1 (statement_list
.head
,
6528 abs_output_section
, NULL
, 0, &found_end
);
6531 /* For an assignment statement outside of an output section statement,
6532 choose the best of neighbouring output sections to use for values
6536 section_for_dot (void)
6540 /* Assignments belong to the previous output section, unless there
6541 has been an assignment to "dot", in which case following
6542 assignments belong to the next output section. (The assumption
6543 is that an assignment to "dot" is setting up the address for the
6544 next output section.) Except that past the assignment to "_end"
6545 we always associate with the previous section. This exception is
6546 for targets like SH that define an alloc .stack or other
6547 weirdness after non-alloc sections. */
6548 if (current_section
== NULL
|| prefer_next_section
)
6550 lang_statement_union_type
*stmt
;
6551 lang_output_section_statement_type
*os
;
6553 for (stmt
= (lang_statement_union_type
*) current_assign
;
6555 stmt
= stmt
->header
.next
)
6556 if (stmt
->header
.type
== lang_output_section_statement_enum
)
6559 os
= stmt
? &stmt
->output_section_statement
: NULL
;
6562 && (os
->bfd_section
== NULL
6563 || (os
->bfd_section
->flags
& SEC_EXCLUDE
) != 0
6564 || bfd_section_removed_from_list (link_info
.output_bfd
,
6568 if (current_section
== NULL
|| os
== NULL
|| !os
->after_end
)
6571 s
= os
->bfd_section
;
6573 s
= link_info
.output_bfd
->section_last
;
6575 && ((s
->flags
& SEC_ALLOC
) == 0
6576 || (s
->flags
& SEC_THREAD_LOCAL
) != 0))
6581 return bfd_abs_section_ptr
;
6585 s
= current_section
->bfd_section
;
6587 /* The section may have been stripped. */
6589 && ((s
->flags
& SEC_EXCLUDE
) != 0
6590 || (s
->flags
& SEC_ALLOC
) == 0
6591 || (s
->flags
& SEC_THREAD_LOCAL
) != 0
6592 || bfd_section_removed_from_list (link_info
.output_bfd
, s
)))
6595 s
= link_info
.output_bfd
->sections
;
6597 && ((s
->flags
& SEC_ALLOC
) == 0
6598 || (s
->flags
& SEC_THREAD_LOCAL
) != 0))
6603 return bfd_abs_section_ptr
;
6606 /* Array of __start/__stop/.startof./.sizeof/ symbols. */
6608 static struct bfd_link_hash_entry
**start_stop_syms
;
6609 static size_t start_stop_count
= 0;
6610 static size_t start_stop_alloc
= 0;
6612 /* Give start/stop SYMBOL for SEC a preliminary definition, and add it
6613 to start_stop_syms. */
6616 lang_define_start_stop (const char *symbol
, asection
*sec
)
6618 struct bfd_link_hash_entry
*h
;
6620 h
= bfd_define_start_stop (link_info
.output_bfd
, &link_info
, symbol
, sec
);
6623 if (start_stop_count
== start_stop_alloc
)
6625 start_stop_alloc
= 2 * start_stop_alloc
+ 10;
6627 = xrealloc (start_stop_syms
,
6628 start_stop_alloc
* sizeof (*start_stop_syms
));
6630 start_stop_syms
[start_stop_count
++] = h
;
6634 /* Check for input sections whose names match references to
6635 __start_SECNAME or __stop_SECNAME symbols. Give the symbols
6636 preliminary definitions. */
6639 lang_init_start_stop (void)
6643 char leading_char
= bfd_get_symbol_leading_char (link_info
.output_bfd
);
6645 for (abfd
= link_info
.input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
6646 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
6649 const char *secname
= s
->name
;
6651 for (ps
= secname
; *ps
!= '\0'; ps
++)
6652 if (!ISALNUM ((unsigned char) *ps
) && *ps
!= '_')
6656 char *symbol
= (char *) xmalloc (10 + strlen (secname
));
6658 symbol
[0] = leading_char
;
6659 sprintf (symbol
+ (leading_char
!= 0), "__start_%s", secname
);
6660 lang_define_start_stop (symbol
, s
);
6662 symbol
[1] = leading_char
;
6663 memcpy (symbol
+ 1 + (leading_char
!= 0), "__stop", 6);
6664 lang_define_start_stop (symbol
+ 1, s
);
6671 /* Iterate over start_stop_syms. */
6674 foreach_start_stop (void (*func
) (struct bfd_link_hash_entry
*))
6678 for (i
= 0; i
< start_stop_count
; ++i
)
6679 func (start_stop_syms
[i
]);
6682 /* __start and __stop symbols are only supposed to be defined by the
6683 linker for orphan sections, but we now extend that to sections that
6684 map to an output section of the same name. The symbols were
6685 defined early for --gc-sections, before we mapped input to output
6686 sections, so undo those that don't satisfy this rule. */
6689 undef_start_stop (struct bfd_link_hash_entry
*h
)
6691 if (h
->ldscript_def
)
6694 if (h
->u
.def
.section
->output_section
== NULL
6695 || h
->u
.def
.section
->output_section
->owner
!= link_info
.output_bfd
6696 || strcmp (h
->u
.def
.section
->name
,
6697 h
->u
.def
.section
->output_section
->name
) != 0)
6699 asection
*sec
= bfd_get_section_by_name (link_info
.output_bfd
,
6700 h
->u
.def
.section
->name
);
6703 /* When there are more than one input sections with the same
6704 section name, SECNAME, linker picks the first one to define
6705 __start_SECNAME and __stop_SECNAME symbols. When the first
6706 input section is removed by comdat group, we need to check
6707 if there is still an output section with section name
6710 for (i
= sec
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
6711 if (strcmp (h
->u
.def
.section
->name
, i
->name
) == 0)
6713 h
->u
.def
.section
= i
;
6717 h
->type
= bfd_link_hash_undefined
;
6718 h
->u
.undef
.abfd
= NULL
;
6719 if (is_elf_hash_table (link_info
.hash
))
6721 const struct elf_backend_data
*bed
;
6722 struct elf_link_hash_entry
*eh
= (struct elf_link_hash_entry
*) h
;
6723 unsigned int was_forced
= eh
->forced_local
;
6725 bed
= get_elf_backend_data (link_info
.output_bfd
);
6726 (*bed
->elf_backend_hide_symbol
) (&link_info
, eh
, true);
6727 if (!eh
->ref_regular_nonweak
)
6728 h
->type
= bfd_link_hash_undefweak
;
6729 eh
->def_regular
= 0;
6730 eh
->forced_local
= was_forced
;
6736 lang_undef_start_stop (void)
6738 foreach_start_stop (undef_start_stop
);
6741 /* Check for output sections whose names match references to
6742 .startof.SECNAME or .sizeof.SECNAME symbols. Give the symbols
6743 preliminary definitions. */
6746 lang_init_startof_sizeof (void)
6750 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6752 const char *secname
= s
->name
;
6753 char *symbol
= (char *) xmalloc (10 + strlen (secname
));
6755 sprintf (symbol
, ".startof.%s", secname
);
6756 lang_define_start_stop (symbol
, s
);
6758 memcpy (symbol
+ 1, ".size", 5);
6759 lang_define_start_stop (symbol
+ 1, s
);
6764 /* Set .startof., .sizeof., __start and __stop symbols final values. */
6767 set_start_stop (struct bfd_link_hash_entry
*h
)
6770 || h
->type
!= bfd_link_hash_defined
)
6773 if (h
->root
.string
[0] == '.')
6775 /* .startof. or .sizeof. symbol.
6776 .startof. already has final value. */
6777 if (h
->root
.string
[2] == 'i')
6780 h
->u
.def
.value
= TO_ADDR (h
->u
.def
.section
->size
);
6781 h
->u
.def
.section
= bfd_abs_section_ptr
;
6786 /* __start or __stop symbol. */
6787 int has_lead
= bfd_get_symbol_leading_char (link_info
.output_bfd
) != 0;
6789 h
->u
.def
.section
= h
->u
.def
.section
->output_section
;
6790 if (h
->root
.string
[4 + has_lead
] == 'o')
6793 h
->u
.def
.value
= TO_ADDR (h
->u
.def
.section
->size
);
6799 lang_finalize_start_stop (void)
6801 foreach_start_stop (set_start_stop
);
6805 lang_symbol_tweaks (void)
6807 /* Give initial values for __start and __stop symbols, so that ELF
6808 gc_sections will keep sections referenced by these symbols. Must
6809 be done before lang_do_assignments. */
6810 if (config
.build_constructors
)
6811 lang_init_start_stop ();
6813 /* Make __ehdr_start hidden, and set def_regular even though it is
6814 likely undefined at this stage. For lang_check_relocs. */
6815 if (is_elf_hash_table (link_info
.hash
)
6816 && !bfd_link_relocatable (&link_info
))
6818 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*)
6819 bfd_link_hash_lookup (link_info
.hash
, "__ehdr_start",
6820 false, false, true);
6822 /* Only adjust the export class if the symbol was referenced
6823 and not defined, otherwise leave it alone. */
6825 && (h
->root
.type
== bfd_link_hash_new
6826 || h
->root
.type
== bfd_link_hash_undefined
6827 || h
->root
.type
== bfd_link_hash_undefweak
6828 || h
->root
.type
== bfd_link_hash_common
))
6830 const struct elf_backend_data
*bed
;
6831 bed
= get_elf_backend_data (link_info
.output_bfd
);
6832 (*bed
->elf_backend_hide_symbol
) (&link_info
, h
, true);
6833 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
6834 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
6836 h
->root
.linker_def
= 1;
6837 h
->root
.rel_from_abs
= 1;
6845 struct bfd_link_hash_entry
*h
;
6848 if ((bfd_link_relocatable (&link_info
) && !link_info
.gc_sections
)
6849 || bfd_link_dll (&link_info
))
6850 warn
= entry_from_cmdline
;
6854 /* Force the user to specify a root when generating a relocatable with
6855 --gc-sections, unless --gc-keep-exported was also given. */
6856 if (bfd_link_relocatable (&link_info
)
6857 && link_info
.gc_sections
6858 && !link_info
.gc_keep_exported
)
6860 struct bfd_sym_chain
*sym
;
6862 for (sym
= link_info
.gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
6864 h
= bfd_link_hash_lookup (link_info
.hash
, sym
->name
,
6865 false, false, false);
6867 && (h
->type
== bfd_link_hash_defined
6868 || h
->type
== bfd_link_hash_defweak
)
6869 && !bfd_is_const_section (h
->u
.def
.section
))
6873 einfo (_("%F%P: --gc-sections requires a defined symbol root "
6874 "specified by -e or -u\n"));
6877 if (entry_symbol
.name
== NULL
)
6879 /* No entry has been specified. Look for the default entry, but
6880 don't warn if we don't find it. */
6881 entry_symbol
.name
= entry_symbol_default
;
6885 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
.name
,
6886 false, false, true);
6888 && (h
->type
== bfd_link_hash_defined
6889 || h
->type
== bfd_link_hash_defweak
)
6890 && h
->u
.def
.section
->output_section
!= NULL
)
6894 val
= (h
->u
.def
.value
6895 + bfd_section_vma (h
->u
.def
.section
->output_section
)
6896 + h
->u
.def
.section
->output_offset
);
6897 if (!bfd_set_start_address (link_info
.output_bfd
, val
))
6898 einfo (_("%F%P: %s: can't set start address\n"), entry_symbol
.name
);
6905 /* We couldn't find the entry symbol. Try parsing it as a
6907 val
= bfd_scan_vma (entry_symbol
.name
, &send
, 0);
6910 if (!bfd_set_start_address (link_info
.output_bfd
, val
))
6911 einfo (_("%F%P: can't set start address\n"));
6913 /* BZ 2004952: Only use the start of the entry section for executables. */
6914 else if bfd_link_executable (&link_info
)
6918 /* Can't find the entry symbol, and it's not a number. Use
6919 the first address in the text section. */
6920 ts
= bfd_get_section_by_name (link_info
.output_bfd
, entry_section
);
6924 einfo (_("%P: warning: cannot find entry symbol %s;"
6925 " defaulting to %V\n"),
6927 bfd_section_vma (ts
));
6928 if (!bfd_set_start_address (link_info
.output_bfd
,
6929 bfd_section_vma (ts
)))
6930 einfo (_("%F%P: can't set start address\n"));
6935 einfo (_("%P: warning: cannot find entry symbol %s;"
6936 " not setting start address\n"),
6943 einfo (_("%P: warning: cannot find entry symbol %s;"
6944 " not setting start address\n"),
6950 /* This is a small function used when we want to ignore errors from
6954 ignore_bfd_errors (const char *fmt ATTRIBUTE_UNUSED
,
6955 va_list ap ATTRIBUTE_UNUSED
)
6957 /* Don't do anything. */
6960 /* Check that the architecture of all the input files is compatible
6961 with the output file. Also call the backend to let it do any
6962 other checking that is needed. */
6967 lang_input_statement_type
*file
;
6969 const bfd_arch_info_type
*compatible
;
6971 for (file
= (void *) file_chain
.head
;
6975 #if BFD_SUPPORTS_PLUGINS
6976 /* Don't check format of files claimed by plugin. */
6977 if (file
->flags
.claimed
)
6979 #endif /* BFD_SUPPORTS_PLUGINS */
6980 input_bfd
= file
->the_bfd
;
6982 = bfd_arch_get_compatible (input_bfd
, link_info
.output_bfd
,
6983 command_line
.accept_unknown_input_arch
);
6985 /* In general it is not possible to perform a relocatable
6986 link between differing object formats when the input
6987 file has relocations, because the relocations in the
6988 input format may not have equivalent representations in
6989 the output format (and besides BFD does not translate
6990 relocs for other link purposes than a final link). */
6991 if (!file
->flags
.just_syms
6992 && (bfd_link_relocatable (&link_info
)
6993 || link_info
.emitrelocations
)
6994 && (compatible
== NULL
6995 || (bfd_get_flavour (input_bfd
)
6996 != bfd_get_flavour (link_info
.output_bfd
)))
6997 && (bfd_get_file_flags (input_bfd
) & HAS_RELOC
) != 0)
6999 einfo (_("%F%P: relocatable linking with relocations from"
7000 " format %s (%pB) to format %s (%pB) is not supported\n"),
7001 bfd_get_target (input_bfd
), input_bfd
,
7002 bfd_get_target (link_info
.output_bfd
), link_info
.output_bfd
);
7003 /* einfo with %F exits. */
7006 if (compatible
== NULL
)
7008 if (command_line
.warn_mismatch
)
7009 einfo (_("%X%P: %s architecture of input file `%pB'"
7010 " is incompatible with %s output\n"),
7011 bfd_printable_name (input_bfd
), input_bfd
,
7012 bfd_printable_name (link_info
.output_bfd
));
7015 /* If the input bfd has no contents, it shouldn't set the
7016 private data of the output bfd. */
7017 else if (!file
->flags
.just_syms
7018 && ((input_bfd
->flags
& DYNAMIC
) != 0
7019 || bfd_count_sections (input_bfd
) != 0))
7021 bfd_error_handler_type pfn
= NULL
;
7023 /* If we aren't supposed to warn about mismatched input
7024 files, temporarily set the BFD error handler to a
7025 function which will do nothing. We still want to call
7026 bfd_merge_private_bfd_data, since it may set up
7027 information which is needed in the output file. */
7028 if (!command_line
.warn_mismatch
)
7029 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
7030 if (!bfd_merge_private_bfd_data (input_bfd
, &link_info
))
7032 if (command_line
.warn_mismatch
)
7033 einfo (_("%X%P: failed to merge target specific data"
7034 " of file %pB\n"), input_bfd
);
7036 if (!command_line
.warn_mismatch
)
7037 bfd_set_error_handler (pfn
);
7042 /* Look through all the global common symbols and attach them to the
7043 correct section. The -sort-common command line switch may be used
7044 to roughly sort the entries by alignment. */
7049 if (link_info
.inhibit_common_definition
)
7051 if (bfd_link_relocatable (&link_info
)
7052 && !command_line
.force_common_definition
)
7055 if (!config
.sort_common
)
7056 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, NULL
);
7061 if (config
.sort_common
== sort_descending
)
7063 for (power
= 4; power
> 0; power
--)
7064 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7067 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7071 for (power
= 0; power
<= 4; power
++)
7072 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7074 power
= (unsigned int) -1;
7075 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7080 /* Place one common symbol in the correct section. */
7083 lang_one_common (struct bfd_link_hash_entry
*h
, void *info
)
7085 unsigned int power_of_two
;
7089 if (h
->type
!= bfd_link_hash_common
)
7093 power_of_two
= h
->u
.c
.p
->alignment_power
;
7095 if (config
.sort_common
== sort_descending
7096 && power_of_two
< *(unsigned int *) info
)
7098 else if (config
.sort_common
== sort_ascending
7099 && power_of_two
> *(unsigned int *) info
)
7102 section
= h
->u
.c
.p
->section
;
7103 if (!bfd_define_common_symbol (link_info
.output_bfd
, &link_info
, h
))
7104 einfo (_("%F%P: could not define common symbol `%pT': %E\n"),
7107 if (config
.map_file
!= NULL
)
7109 static bool header_printed
;
7114 if (!header_printed
)
7116 minfo (_("\nAllocating common symbols\n"));
7117 minfo (_("Common symbol size file\n\n"));
7118 header_printed
= true;
7121 name
= bfd_demangle (link_info
.output_bfd
, h
->root
.string
,
7122 DMGL_ANSI
| DMGL_PARAMS
);
7125 minfo ("%s", h
->root
.string
);
7126 len
= strlen (h
->root
.string
);
7131 len
= strlen (name
);
7141 sprintf (buf
, "%" PRIx64
, (uint64_t) size
);
7142 fprintf (config
.map_file
, "%*s0x%-16s", 20 - len
, "", buf
);
7144 minfo ("%pB\n", section
->owner
);
7150 /* Handle a single orphan section S, placing the orphan into an appropriate
7151 output section. The effects of the --orphan-handling command line
7152 option are handled here. */
7155 ldlang_place_orphan (asection
*s
)
7157 if (config
.orphan_handling
== orphan_handling_discard
)
7159 lang_output_section_statement_type
*os
;
7160 os
= lang_output_section_statement_lookup (DISCARD_SECTION_NAME
, 0, 1);
7161 if (os
->addr_tree
== NULL
7162 && (bfd_link_relocatable (&link_info
)
7163 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0))
7164 os
->addr_tree
= exp_intop (0);
7165 lang_add_section (&os
->children
, s
, NULL
, NULL
, os
);
7169 lang_output_section_statement_type
*os
;
7170 const char *name
= s
->name
;
7173 if (config
.orphan_handling
== orphan_handling_error
)
7174 einfo (_("%X%P: error: unplaced orphan section `%pA' from `%pB'\n"),
7177 if (config
.unique_orphan_sections
|| unique_section_p (s
, NULL
))
7178 constraint
= SPECIAL
;
7180 os
= ldemul_place_orphan (s
, name
, constraint
);
7183 os
= lang_output_section_statement_lookup (name
, constraint
, 1);
7184 if (os
->addr_tree
== NULL
7185 && (bfd_link_relocatable (&link_info
)
7186 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0))
7187 os
->addr_tree
= exp_intop (0);
7188 lang_add_section (&os
->children
, s
, NULL
, NULL
, os
);
7191 if (config
.orphan_handling
== orphan_handling_warn
)
7192 einfo (_("%P: warning: orphan section `%pA' from `%pB' being "
7193 "placed in section `%s'\n"),
7194 s
, s
->owner
, os
->name
);
7198 /* Run through the input files and ensure that every input section has
7199 somewhere to go. If one is found without a destination then create
7200 an input request and place it into the statement tree. */
7203 lang_place_orphans (void)
7205 LANG_FOR_EACH_INPUT_STATEMENT (file
)
7209 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7211 if (s
->output_section
== NULL
)
7213 /* This section of the file is not attached, root
7214 around for a sensible place for it to go. */
7216 if (file
->flags
.just_syms
)
7217 bfd_link_just_syms (file
->the_bfd
, s
, &link_info
);
7218 else if (lang_discard_section_p (s
))
7219 s
->output_section
= bfd_abs_section_ptr
;
7220 else if (strcmp (s
->name
, "COMMON") == 0)
7222 /* This is a lonely common section which must have
7223 come from an archive. We attach to the section
7224 with the wildcard. */
7225 if (!bfd_link_relocatable (&link_info
)
7226 || command_line
.force_common_definition
)
7228 if (default_common_section
== NULL
)
7229 default_common_section
7230 = lang_output_section_statement_lookup (".bss", 0, 1);
7231 lang_add_section (&default_common_section
->children
, s
,
7232 NULL
, NULL
, default_common_section
);
7236 ldlang_place_orphan (s
);
7243 lang_set_flags (lang_memory_region_type
*ptr
, const char *flags
, int invert
)
7245 flagword
*ptr_flags
;
7247 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
7253 /* PR 17900: An exclamation mark in the attributes reverses
7254 the sense of any of the attributes that follow. */
7257 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
7261 *ptr_flags
|= SEC_ALLOC
;
7265 *ptr_flags
|= SEC_READONLY
;
7269 *ptr_flags
|= SEC_DATA
;
7273 *ptr_flags
|= SEC_CODE
;
7278 *ptr_flags
|= SEC_LOAD
;
7282 einfo (_("%F%P: invalid character %c (%d) in flags\n"),
7290 /* Call a function on each real input file. This function will be
7291 called on an archive, but not on the elements. */
7294 lang_for_each_input_file (void (*func
) (lang_input_statement_type
*))
7296 lang_input_statement_type
*f
;
7298 for (f
= (void *) input_file_chain
.head
;
7300 f
= f
->next_real_file
)
7305 /* Call a function on each real file. The function will be called on
7306 all the elements of an archive which are included in the link, but
7307 will not be called on the archive file itself. */
7310 lang_for_each_file (void (*func
) (lang_input_statement_type
*))
7312 LANG_FOR_EACH_INPUT_STATEMENT (f
)
7320 ldlang_add_file (lang_input_statement_type
*entry
)
7322 lang_statement_append (&file_chain
, entry
, &entry
->next
);
7324 /* The BFD linker needs to have a list of all input BFDs involved in
7326 ASSERT (link_info
.input_bfds_tail
!= &entry
->the_bfd
->link
.next
7327 && entry
->the_bfd
->link
.next
== NULL
);
7328 ASSERT (entry
->the_bfd
!= link_info
.output_bfd
);
7330 *link_info
.input_bfds_tail
= entry
->the_bfd
;
7331 link_info
.input_bfds_tail
= &entry
->the_bfd
->link
.next
;
7332 bfd_set_usrdata (entry
->the_bfd
, entry
);
7333 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
7335 /* Look through the sections and check for any which should not be
7336 included in the link. We need to do this now, so that we can
7337 notice when the backend linker tries to report multiple
7338 definition errors for symbols which are in sections we aren't
7339 going to link. FIXME: It might be better to entirely ignore
7340 symbols which are defined in sections which are going to be
7341 discarded. This would require modifying the backend linker for
7342 each backend which might set the SEC_LINK_ONCE flag. If we do
7343 this, we should probably handle SEC_EXCLUDE in the same way. */
7345 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, entry
);
7349 lang_add_output (const char *name
, int from_script
)
7351 /* Make -o on command line override OUTPUT in script. */
7352 if (!had_output_filename
|| !from_script
)
7354 output_filename
= name
;
7355 had_output_filename
= true;
7359 lang_output_section_statement_type
*
7360 lang_enter_output_section_statement (const char *output_section_statement_name
,
7361 etree_type
*address_exp
,
7362 enum section_type sectype
,
7363 etree_type
*sectype_value
,
7365 etree_type
*subalign
,
7368 int align_with_input
)
7370 lang_output_section_statement_type
*os
;
7372 os
= lang_output_section_statement_lookup (output_section_statement_name
,
7374 current_section
= os
;
7376 if (os
->addr_tree
== NULL
)
7378 os
->addr_tree
= address_exp
;
7380 os
->sectype
= sectype
;
7381 if (sectype
== type_section
|| sectype
== typed_readonly_section
)
7382 os
->sectype_value
= sectype_value
;
7383 else if (sectype
== noload_section
)
7384 os
->flags
= SEC_NEVER_LOAD
;
7386 os
->flags
= SEC_NO_FLAGS
;
7387 os
->block_value
= 1;
7389 /* Make next things chain into subchain of this. */
7390 push_stat_ptr (&os
->children
);
7392 os
->align_lma_with_input
= align_with_input
== ALIGN_WITH_INPUT
;
7393 if (os
->align_lma_with_input
&& align
!= NULL
)
7394 einfo (_("%F%P:%pS: error: align with input and explicit align specified\n"),
7397 os
->subsection_alignment
= subalign
;
7398 os
->section_alignment
= align
;
7400 os
->load_base
= ebase
;
7407 lang_output_statement_type
*new_stmt
;
7409 new_stmt
= new_stat (lang_output_statement
, stat_ptr
);
7410 new_stmt
->name
= output_filename
;
7413 /* Reset the current counters in the regions. */
7416 lang_reset_memory_regions (void)
7418 lang_memory_region_type
*p
= lang_memory_region_list
;
7420 lang_output_section_statement_type
*os
;
7422 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
7424 p
->current
= p
->origin
;
7428 for (os
= (void *) lang_os_list
.head
;
7432 os
->processed_vma
= false;
7433 os
->processed_lma
= false;
7436 for (o
= link_info
.output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
7438 /* Save the last size for possible use by bfd_relax_section. */
7439 o
->rawsize
= o
->size
;
7440 if (!(o
->flags
& SEC_FIXED_SIZE
))
7445 /* Worker for lang_gc_sections_1. */
7448 gc_section_callback (lang_wild_statement_type
*ptr
,
7449 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
7451 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
7452 void *data ATTRIBUTE_UNUSED
)
7454 /* If the wild pattern was marked KEEP, the member sections
7455 should be as well. */
7456 if (ptr
->keep_sections
)
7457 section
->flags
|= SEC_KEEP
;
7460 /* Iterate over sections marking them against GC. */
7463 lang_gc_sections_1 (lang_statement_union_type
*s
)
7465 for (; s
!= NULL
; s
= s
->header
.next
)
7467 switch (s
->header
.type
)
7469 case lang_wild_statement_enum
:
7470 walk_wild (&s
->wild_statement
, gc_section_callback
, NULL
);
7472 case lang_constructors_statement_enum
:
7473 lang_gc_sections_1 (constructor_list
.head
);
7475 case lang_output_section_statement_enum
:
7476 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
7478 case lang_group_statement_enum
:
7479 lang_gc_sections_1 (s
->group_statement
.children
.head
);
7488 lang_gc_sections (void)
7490 /* Keep all sections so marked in the link script. */
7491 lang_gc_sections_1 (statement_list
.head
);
7493 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
7494 the special case of .stabstr debug info. (See bfd/stabs.c)
7495 Twiddle the flag here, to simplify later linker code. */
7496 if (bfd_link_relocatable (&link_info
))
7498 LANG_FOR_EACH_INPUT_STATEMENT (f
)
7501 #if BFD_SUPPORTS_PLUGINS
7502 if (f
->flags
.claimed
)
7505 for (sec
= f
->the_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7506 if ((sec
->flags
& SEC_DEBUGGING
) == 0
7507 || strcmp (sec
->name
, ".stabstr") != 0)
7508 sec
->flags
&= ~SEC_EXCLUDE
;
7512 if (link_info
.gc_sections
)
7513 bfd_gc_sections (link_info
.output_bfd
, &link_info
);
7516 /* Worker for lang_find_relro_sections_1. */
7519 find_relro_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
7520 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
7522 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
7525 /* Discarded, excluded and ignored sections effectively have zero
7527 if (section
->output_section
!= NULL
7528 && section
->output_section
->owner
== link_info
.output_bfd
7529 && (section
->output_section
->flags
& SEC_EXCLUDE
) == 0
7530 && !IGNORE_SECTION (section
)
7531 && section
->size
!= 0)
7533 bool *has_relro_section
= (bool *) data
;
7534 *has_relro_section
= true;
7538 /* Iterate over sections for relro sections. */
7541 lang_find_relro_sections_1 (lang_statement_union_type
*s
,
7542 bool *has_relro_section
)
7544 if (*has_relro_section
)
7547 for (; s
!= NULL
; s
= s
->header
.next
)
7549 if (s
== expld
.dataseg
.relro_end_stat
)
7552 switch (s
->header
.type
)
7554 case lang_wild_statement_enum
:
7555 walk_wild (&s
->wild_statement
,
7556 find_relro_section_callback
,
7559 case lang_constructors_statement_enum
:
7560 lang_find_relro_sections_1 (constructor_list
.head
,
7563 case lang_output_section_statement_enum
:
7564 lang_find_relro_sections_1 (s
->output_section_statement
.children
.head
,
7567 case lang_group_statement_enum
:
7568 lang_find_relro_sections_1 (s
->group_statement
.children
.head
,
7578 lang_find_relro_sections (void)
7580 bool has_relro_section
= false;
7582 /* Check all sections in the link script. */
7584 lang_find_relro_sections_1 (expld
.dataseg
.relro_start_stat
,
7585 &has_relro_section
);
7587 if (!has_relro_section
)
7588 link_info
.relro
= false;
7591 /* Relax all sections until bfd_relax_section gives up. */
7594 lang_relax_sections (bool need_layout
)
7596 /* NB: Also enable relaxation to layout sections for DT_RELR. */
7597 if (RELAXATION_ENABLED
|| link_info
.enable_dt_relr
)
7599 /* We may need more than one relaxation pass. */
7600 int i
= link_info
.relax_pass
;
7602 /* The backend can use it to determine the current pass. */
7603 link_info
.relax_pass
= 0;
7607 /* Keep relaxing until bfd_relax_section gives up. */
7610 link_info
.relax_trip
= -1;
7613 link_info
.relax_trip
++;
7615 /* Note: pe-dll.c does something like this also. If you find
7616 you need to change this code, you probably need to change
7617 pe-dll.c also. DJ */
7619 /* Do all the assignments with our current guesses as to
7621 lang_do_assignments (lang_assigning_phase_enum
);
7623 /* We must do this after lang_do_assignments, because it uses
7625 lang_reset_memory_regions ();
7627 /* Perform another relax pass - this time we know where the
7628 globals are, so can make a better guess. */
7629 relax_again
= false;
7630 lang_size_sections (&relax_again
, false);
7632 while (relax_again
);
7634 link_info
.relax_pass
++;
7641 /* Final extra sizing to report errors. */
7642 lang_do_assignments (lang_assigning_phase_enum
);
7643 lang_reset_memory_regions ();
7644 lang_size_sections (NULL
, true);
7648 #if BFD_SUPPORTS_PLUGINS
7649 /* Find the insert point for the plugin's replacement files. We
7650 place them after the first claimed real object file, or if the
7651 first claimed object is an archive member, after the last real
7652 object file immediately preceding the archive. In the event
7653 no objects have been claimed at all, we return the first dummy
7654 object file on the list as the insert point; that works, but
7655 the callee must be careful when relinking the file_chain as it
7656 is not actually on that chain, only the statement_list and the
7657 input_file list; in that case, the replacement files must be
7658 inserted at the head of the file_chain. */
7660 static lang_input_statement_type
*
7661 find_replacements_insert_point (bool *before
)
7663 lang_input_statement_type
*claim1
, *lastobject
;
7664 lastobject
= (void *) input_file_chain
.head
;
7665 for (claim1
= (void *) file_chain
.head
;
7667 claim1
= claim1
->next
)
7669 if (claim1
->flags
.claimed
)
7671 *before
= claim1
->flags
.claim_archive
;
7672 return claim1
->flags
.claim_archive
? lastobject
: claim1
;
7674 /* Update lastobject if this is a real object file. */
7675 if (claim1
->the_bfd
!= NULL
&& claim1
->the_bfd
->my_archive
== NULL
)
7676 lastobject
= claim1
;
7678 /* No files were claimed by the plugin. Choose the last object
7679 file found on the list (maybe the first, dummy entry) as the
7685 /* Find where to insert ADD, an archive element or shared library
7686 added during a rescan. */
7688 static lang_input_statement_type
**
7689 find_rescan_insertion (lang_input_statement_type
*add
)
7691 bfd
*add_bfd
= add
->the_bfd
;
7692 lang_input_statement_type
*f
;
7693 lang_input_statement_type
*last_loaded
= NULL
;
7694 lang_input_statement_type
*before
= NULL
;
7695 lang_input_statement_type
**iter
= NULL
;
7697 if (add_bfd
->my_archive
!= NULL
)
7698 add_bfd
= add_bfd
->my_archive
;
7700 /* First look through the input file chain, to find an object file
7701 before the one we've rescanned. Normal object files always
7702 appear on both the input file chain and the file chain, so this
7703 lets us get quickly to somewhere near the correct place on the
7704 file chain if it is full of archive elements. Archives don't
7705 appear on the file chain, but if an element has been extracted
7706 then their input_statement->next points at it. */
7707 for (f
= (void *) input_file_chain
.head
;
7709 f
= f
->next_real_file
)
7711 if (f
->the_bfd
== add_bfd
)
7713 before
= last_loaded
;
7714 if (f
->next
!= NULL
)
7715 return &f
->next
->next
;
7717 if (f
->the_bfd
!= NULL
&& f
->next
!= NULL
)
7721 for (iter
= before
? &before
->next
: &file_chain
.head
->input_statement
.next
;
7723 iter
= &(*iter
)->next
)
7724 if (!(*iter
)->flags
.claim_archive
7725 && (*iter
)->the_bfd
->my_archive
== NULL
)
7731 /* Insert SRCLIST into DESTLIST after given element by chaining
7732 on FIELD as the next-pointer. (Counterintuitively does not need
7733 a pointer to the actual after-node itself, just its chain field.) */
7736 lang_list_insert_after (lang_statement_list_type
*destlist
,
7737 lang_statement_list_type
*srclist
,
7738 lang_statement_union_type
**field
)
7740 *(srclist
->tail
) = *field
;
7741 *field
= srclist
->head
;
7742 if (destlist
->tail
== field
)
7743 destlist
->tail
= srclist
->tail
;
7746 /* Detach new nodes added to DESTLIST since the time ORIGLIST
7747 was taken as a copy of it and leave them in ORIGLIST. */
7750 lang_list_remove_tail (lang_statement_list_type
*destlist
,
7751 lang_statement_list_type
*origlist
)
7753 union lang_statement_union
**savetail
;
7754 /* Check that ORIGLIST really is an earlier state of DESTLIST. */
7755 ASSERT (origlist
->head
== destlist
->head
);
7756 savetail
= origlist
->tail
;
7757 origlist
->head
= *(savetail
);
7758 origlist
->tail
= destlist
->tail
;
7759 destlist
->tail
= savetail
;
7763 static lang_statement_union_type
**
7764 find_next_input_statement (lang_statement_union_type
**s
)
7766 for ( ; *s
; s
= &(*s
)->header
.next
)
7768 lang_statement_union_type
**t
;
7769 switch ((*s
)->header
.type
)
7771 case lang_input_statement_enum
:
7773 case lang_wild_statement_enum
:
7774 t
= &(*s
)->wild_statement
.children
.head
;
7776 case lang_group_statement_enum
:
7777 t
= &(*s
)->group_statement
.children
.head
;
7779 case lang_output_section_statement_enum
:
7780 t
= &(*s
)->output_section_statement
.children
.head
;
7785 t
= find_next_input_statement (t
);
7791 #endif /* BFD_SUPPORTS_PLUGINS */
7793 /* Add NAME to the list of garbage collection entry points. */
7796 lang_add_gc_name (const char *name
)
7798 struct bfd_sym_chain
*sym
;
7803 sym
= stat_alloc (sizeof (*sym
));
7805 sym
->next
= link_info
.gc_sym_list
;
7807 link_info
.gc_sym_list
= sym
;
7810 /* Check relocations. */
7813 lang_check_relocs (void)
7815 if (link_info
.check_relocs_after_open_input
)
7819 for (abfd
= link_info
.input_bfds
;
7820 abfd
!= (bfd
*) NULL
; abfd
= abfd
->link
.next
)
7821 if (!bfd_link_check_relocs (abfd
, &link_info
))
7823 /* No object output, fail return. */
7824 config
.make_executable
= false;
7825 /* Note: we do not abort the loop, but rather
7826 continue the scan in case there are other
7827 bad relocations to report. */
7832 /* Look through all output sections looking for places where we can
7833 propagate forward the lma region. */
7836 lang_propagate_lma_regions (void)
7838 lang_output_section_statement_type
*os
;
7840 for (os
= (void *) lang_os_list
.head
;
7844 if (os
->prev
!= NULL
7845 && os
->lma_region
== NULL
7846 && os
->load_base
== NULL
7847 && os
->addr_tree
== NULL
7848 && os
->region
== os
->prev
->region
)
7849 os
->lma_region
= os
->prev
->lma_region
;
7854 warn_non_contiguous_discards (void)
7856 LANG_FOR_EACH_INPUT_STATEMENT (file
)
7858 if ((file
->the_bfd
->flags
& (BFD_LINKER_CREATED
| DYNAMIC
)) != 0
7859 || file
->flags
.just_syms
)
7862 for (asection
*s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7863 if (s
->output_section
== NULL
7864 && (s
->flags
& SEC_LINKER_CREATED
) == 0)
7865 einfo (_("%P: warning: --enable-non-contiguous-regions "
7866 "discards section `%pA' from `%pB'\n"),
7872 reset_one_wild (lang_statement_union_type
*statement
)
7874 if (statement
->header
.type
== lang_wild_statement_enum
)
7876 lang_wild_statement_type
*stmt
= &statement
->wild_statement
;
7877 lang_list_init (&stmt
->matching_sections
);
7882 reset_resolved_wilds (void)
7884 lang_for_each_statement (reset_one_wild
);
7890 /* Finalize dynamic list. */
7891 if (link_info
.dynamic_list
)
7892 lang_finalize_version_expr_head (&link_info
.dynamic_list
->head
);
7894 current_target
= default_target
;
7896 /* Open the output file. */
7897 lang_for_each_statement (ldlang_open_output
);
7900 ldemul_create_output_section_statements ();
7902 /* Add to the hash table all undefineds on the command line. */
7903 lang_place_undefineds ();
7905 if (!bfd_section_already_linked_table_init ())
7906 einfo (_("%F%P: can not create hash table: %E\n"));
7908 /* A first pass through the memory regions ensures that if any region
7909 references a symbol for its origin or length then this symbol will be
7910 added to the symbol table. Having these symbols in the symbol table
7911 means that when we call open_input_bfds PROVIDE statements will
7912 trigger to provide any needed symbols. The regions origins and
7913 lengths are not assigned as a result of this call. */
7914 lang_do_memory_regions (false);
7916 /* Create a bfd for each input file. */
7917 current_target
= default_target
;
7918 lang_statement_iteration
++;
7919 open_input_bfds (statement_list
.head
, OPEN_BFD_NORMAL
);
7921 /* Now that open_input_bfds has processed assignments and provide
7922 statements we can give values to symbolic origin/length now. */
7923 lang_do_memory_regions (true);
7925 #if BFD_SUPPORTS_PLUGINS
7926 if (link_info
.lto_plugin_active
)
7928 lang_statement_list_type added
;
7929 lang_statement_list_type files
, inputfiles
;
7931 ldemul_before_plugin_all_symbols_read ();
7933 /* Now all files are read, let the plugin(s) decide if there
7934 are any more to be added to the link before we call the
7935 emulation's after_open hook. We create a private list of
7936 input statements for this purpose, which we will eventually
7937 insert into the global statement list after the first claimed
7940 /* We need to manipulate all three chains in synchrony. */
7942 inputfiles
= input_file_chain
;
7943 if (plugin_call_all_symbols_read ())
7944 einfo (_("%F%P: %s: plugin reported error after all symbols read\n"),
7945 plugin_error_plugin ());
7946 link_info
.lto_all_symbols_read
= true;
7947 /* Open any newly added files, updating the file chains. */
7948 plugin_undefs
= link_info
.hash
->undefs_tail
;
7949 open_input_bfds (*added
.tail
, OPEN_BFD_NORMAL
);
7950 if (plugin_undefs
== link_info
.hash
->undefs_tail
)
7951 plugin_undefs
= NULL
;
7952 /* Restore the global list pointer now they have all been added. */
7953 lang_list_remove_tail (stat_ptr
, &added
);
7954 /* And detach the fresh ends of the file lists. */
7955 lang_list_remove_tail (&file_chain
, &files
);
7956 lang_list_remove_tail (&input_file_chain
, &inputfiles
);
7957 /* Were any new files added? */
7958 if (added
.head
!= NULL
)
7960 /* If so, we will insert them into the statement list immediately
7961 after the first input file that was claimed by the plugin,
7962 unless that file was an archive in which case it is inserted
7963 immediately before. */
7965 lang_statement_union_type
**prev
;
7966 plugin_insert
= find_replacements_insert_point (&before
);
7967 /* If a plugin adds input files without having claimed any, we
7968 don't really have a good idea where to place them. Just putting
7969 them at the start or end of the list is liable to leave them
7970 outside the crtbegin...crtend range. */
7971 ASSERT (plugin_insert
!= NULL
);
7972 /* Splice the new statement list into the old one. */
7973 prev
= &plugin_insert
->header
.next
;
7976 prev
= find_next_input_statement (prev
);
7977 if (*prev
!= (void *) plugin_insert
->next_real_file
)
7979 /* We didn't find the expected input statement.
7980 Fall back to adding after plugin_insert. */
7981 prev
= &plugin_insert
->header
.next
;
7984 lang_list_insert_after (stat_ptr
, &added
, prev
);
7985 /* Likewise for the file chains. */
7986 lang_list_insert_after (&input_file_chain
, &inputfiles
,
7987 (void *) &plugin_insert
->next_real_file
);
7988 /* We must be careful when relinking file_chain; we may need to
7989 insert the new files at the head of the list if the insert
7990 point chosen is the dummy first input file. */
7991 if (plugin_insert
->filename
)
7992 lang_list_insert_after (&file_chain
, &files
,
7993 (void *) &plugin_insert
->next
);
7995 lang_list_insert_after (&file_chain
, &files
, &file_chain
.head
);
7997 /* Rescan archives in case new undefined symbols have appeared. */
7999 lang_statement_iteration
++;
8000 open_input_bfds (statement_list
.head
, OPEN_BFD_RESCAN
);
8001 lang_list_remove_tail (&file_chain
, &files
);
8002 while (files
.head
!= NULL
)
8004 lang_input_statement_type
**insert
;
8005 lang_input_statement_type
**iter
, *temp
;
8008 insert
= find_rescan_insertion (&files
.head
->input_statement
);
8009 /* All elements from an archive can be added at once. */
8010 iter
= &files
.head
->input_statement
.next
;
8011 my_arch
= files
.head
->input_statement
.the_bfd
->my_archive
;
8012 if (my_arch
!= NULL
)
8013 for (; *iter
!= NULL
; iter
= &(*iter
)->next
)
8014 if ((*iter
)->the_bfd
->my_archive
!= my_arch
)
8017 *insert
= &files
.head
->input_statement
;
8018 files
.head
= (lang_statement_union_type
*) *iter
;
8020 if (my_arch
!= NULL
)
8022 lang_input_statement_type
*parent
= bfd_usrdata (my_arch
);
8024 parent
->next
= (lang_input_statement_type
*)
8026 - offsetof (lang_input_statement_type
, next
));
8031 #endif /* BFD_SUPPORTS_PLUGINS */
8033 /* Make sure that nobody has tried to add a symbol to this list
8035 ASSERT (link_info
.gc_sym_list
== NULL
);
8037 link_info
.gc_sym_list
= &entry_symbol
;
8039 if (entry_symbol
.name
== NULL
)
8041 link_info
.gc_sym_list
= ldlang_undef_chain_list_head
;
8043 /* entry_symbol is normally initialied by a ENTRY definition in the
8044 linker script or the -e command line option. But if neither of
8045 these have been used, the target specific backend may still have
8046 provided an entry symbol via a call to lang_default_entry().
8047 Unfortunately this value will not be processed until lang_end()
8048 is called, long after this function has finished. So detect this
8049 case here and add the target's entry symbol to the list of starting
8050 points for garbage collection resolution. */
8051 lang_add_gc_name (entry_symbol_default
);
8054 lang_add_gc_name (link_info
.init_function
);
8055 lang_add_gc_name (link_info
.fini_function
);
8057 ldemul_after_open ();
8058 if (config
.map_file
!= NULL
)
8059 lang_print_asneeded ();
8063 bfd_section_already_linked_table_free ();
8065 /* Make sure that we're not mixing architectures. We call this
8066 after all the input files have been opened, but before we do any
8067 other processing, so that any operations merge_private_bfd_data
8068 does on the output file will be known during the rest of the
8072 /* Handle .exports instead of a version script if we're told to do so. */
8073 if (command_line
.version_exports_section
)
8074 lang_do_version_exports_section ();
8076 /* Build all sets based on the information gathered from the input
8078 ldctor_build_sets ();
8080 lang_symbol_tweaks ();
8082 /* PR 13683: We must rerun the assignments prior to running garbage
8083 collection in order to make sure that all symbol aliases are resolved. */
8084 lang_do_assignments (lang_mark_phase_enum
);
8085 expld
.phase
= lang_first_phase_enum
;
8087 /* Size up the common data. */
8091 debug_prefix_tree ();
8095 /* Remove unreferenced sections if asked to. */
8096 lang_gc_sections ();
8098 lang_mark_undefineds ();
8100 /* Check relocations. */
8101 lang_check_relocs ();
8103 ldemul_after_check_relocs ();
8105 /* There might have been new sections created (e.g. as result of
8106 checking relocs to need a .got, or suchlike), so to properly order
8107 them into our lists of matching sections reset them here. */
8108 reset_resolved_wilds ();
8111 /* Update wild statements in case the user gave --sort-section.
8112 Note how the option might have come after the linker script and
8113 so couldn't have been set when the wild statements were created. */
8114 update_wild_statements (statement_list
.head
);
8116 /* Run through the contours of the script and attach input sections
8117 to the correct output sections. */
8118 lang_statement_iteration
++;
8119 map_input_to_output_sections (statement_list
.head
, NULL
, NULL
);
8121 /* Start at the statement immediately after the special abs_section
8122 output statement, so that it isn't reordered. */
8123 process_insert_statements (&lang_os_list
.head
->header
.next
);
8125 ldemul_before_place_orphans ();
8127 /* Find any sections not attached explicitly and handle them. */
8128 lang_place_orphans ();
8130 if (!bfd_link_relocatable (&link_info
))
8134 /* Merge SEC_MERGE sections. This has to be done after GC of
8135 sections, so that GCed sections are not merged, but before
8136 assigning dynamic symbols, since removing whole input sections
8138 bfd_merge_sections (link_info
.output_bfd
, &link_info
);
8140 /* Look for a text section and set the readonly attribute in it. */
8141 found
= bfd_get_section_by_name (link_info
.output_bfd
, ".text");
8145 if (config
.text_read_only
)
8146 found
->flags
|= SEC_READONLY
;
8148 found
->flags
&= ~SEC_READONLY
;
8152 /* Merge together CTF sections. After this, only the symtab-dependent
8153 function and data object sections need adjustment. */
8156 /* Emit the CTF, iff the emulation doesn't need to do late emission after
8157 examining things laid out late, like the strtab. */
8160 /* Copy forward lma regions for output sections in same lma region. */
8161 lang_propagate_lma_regions ();
8163 /* Defining __start/__stop symbols early for --gc-sections to work
8164 around a glibc build problem can result in these symbols being
8165 defined when they should not be. Fix them now. */
8166 if (config
.build_constructors
)
8167 lang_undef_start_stop ();
8169 /* Define .startof./.sizeof. symbols with preliminary values before
8170 dynamic symbols are created. */
8171 if (!bfd_link_relocatable (&link_info
))
8172 lang_init_startof_sizeof ();
8174 /* Do anything special before sizing sections. This is where ELF
8175 and other back-ends size dynamic sections. */
8176 ldemul_before_allocation ();
8178 /* We must record the program headers before we try to fix the
8179 section positions, since they will affect SIZEOF_HEADERS. */
8180 lang_record_phdrs ();
8182 /* Check relro sections. */
8183 if (link_info
.relro
&& !bfd_link_relocatable (&link_info
))
8184 lang_find_relro_sections ();
8186 /* Size up the sections. */
8187 lang_size_sections (NULL
, !RELAXATION_ENABLED
);
8189 /* See if anything special should be done now we know how big
8190 everything is. This is where relaxation is done. */
8191 ldemul_after_allocation ();
8193 /* Fix any __start, __stop, .startof. or .sizeof. symbols. */
8194 lang_finalize_start_stop ();
8196 /* Do all the assignments again, to report errors. Assignment
8197 statements are processed multiple times, updating symbols; In
8198 open_input_bfds, lang_do_assignments, and lang_size_sections.
8199 Since lang_relax_sections calls lang_do_assignments, symbols are
8200 also updated in ldemul_after_allocation. */
8201 lang_do_assignments (lang_final_phase_enum
);
8205 /* Convert absolute symbols to section relative. */
8206 ldexp_finalize_syms ();
8208 /* Make sure that the section addresses make sense. */
8209 if (command_line
.check_section_addresses
)
8210 lang_check_section_addresses ();
8212 if (link_info
.non_contiguous_regions
8213 && link_info
.non_contiguous_regions_warnings
)
8214 warn_non_contiguous_discards ();
8216 /* Check any required symbols are known. */
8217 ldlang_check_require_defined_symbols ();
8222 /* EXPORTED TO YACC */
8225 lang_add_wild (struct wildcard_spec
*filespec
,
8226 struct wildcard_list
*section_list
,
8229 struct wildcard_list
*curr
, *next
;
8230 lang_wild_statement_type
*new_stmt
;
8231 bool any_specs_sorted
= false;
8233 /* Reverse the list as the parser puts it back to front. */
8234 for (curr
= section_list
, section_list
= NULL
;
8236 section_list
= curr
, curr
= next
)
8238 if (curr
->spec
.sorted
!= none
&& curr
->spec
.sorted
!= by_none
)
8239 any_specs_sorted
= true;
8241 curr
->next
= section_list
;
8244 if (filespec
!= NULL
&& filespec
->name
!= NULL
)
8246 if (strcmp (filespec
->name
, "*") == 0)
8247 filespec
->name
= NULL
;
8248 else if (!wildcardp (filespec
->name
))
8249 lang_has_input_file
= true;
8252 new_stmt
= new_stat (lang_wild_statement
, stat_ptr
);
8253 new_stmt
->filename
= NULL
;
8254 new_stmt
->filenames_sorted
= false;
8255 new_stmt
->any_specs_sorted
= any_specs_sorted
;
8256 new_stmt
->section_flag_list
= NULL
;
8257 new_stmt
->exclude_name_list
= NULL
;
8258 if (filespec
!= NULL
)
8260 new_stmt
->filename
= filespec
->name
;
8261 new_stmt
->filenames_sorted
= filespec
->sorted
== by_name
;
8262 new_stmt
->section_flag_list
= filespec
->section_flag_list
;
8263 new_stmt
->exclude_name_list
= filespec
->exclude_name_list
;
8265 new_stmt
->section_list
= section_list
;
8266 new_stmt
->keep_sections
= keep_sections
;
8267 lang_list_init (&new_stmt
->children
);
8268 lang_list_init (&new_stmt
->matching_sections
);
8269 analyze_walk_wild_section_handler (new_stmt
);
8272 printf ("wild %s(", new_stmt
->filename
? new_stmt
->filename
: "*");
8273 for (curr
= new_stmt
->section_list
; curr
; curr
= curr
->next
)
8274 printf ("%s ", curr
->spec
.name
? curr
->spec
.name
: "*");
8280 lang_section_start (const char *name
, etree_type
*address
,
8281 const segment_type
*segment
)
8283 lang_address_statement_type
*ad
;
8285 ad
= new_stat (lang_address_statement
, stat_ptr
);
8286 ad
->section_name
= name
;
8287 ad
->address
= address
;
8288 ad
->segment
= segment
;
8291 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
8292 because of a -e argument on the command line, or zero if this is
8293 called by ENTRY in a linker script. Command line arguments take
8297 lang_add_entry (const char *name
, bool cmdline
)
8299 if (entry_symbol
.name
== NULL
8301 || !entry_from_cmdline
)
8303 entry_symbol
.name
= name
;
8304 entry_from_cmdline
= cmdline
;
8308 /* Set the default start symbol to NAME. .em files should use this,
8309 not lang_add_entry, to override the use of "start" if neither the
8310 linker script nor the command line specifies an entry point. NAME
8311 must be permanently allocated. */
8313 lang_default_entry (const char *name
)
8315 entry_symbol_default
= name
;
8319 lang_add_target (const char *name
)
8321 lang_target_statement_type
*new_stmt
;
8323 new_stmt
= new_stat (lang_target_statement
, stat_ptr
);
8324 new_stmt
->target
= name
;
8328 lang_add_map (const char *name
)
8335 map_option_f
= true;
8343 lang_add_fill (fill_type
*fill
)
8345 lang_fill_statement_type
*new_stmt
;
8347 new_stmt
= new_stat (lang_fill_statement
, stat_ptr
);
8348 new_stmt
->fill
= fill
;
8352 lang_add_data (int type
, union etree_union
*exp
)
8354 lang_data_statement_type
*new_stmt
;
8356 new_stmt
= new_stat (lang_data_statement
, stat_ptr
);
8357 new_stmt
->exp
= exp
;
8358 new_stmt
->type
= type
;
8361 /* Create a new reloc statement. RELOC is the BFD relocation type to
8362 generate. HOWTO is the corresponding howto structure (we could
8363 look this up, but the caller has already done so). SECTION is the
8364 section to generate a reloc against, or NAME is the name of the
8365 symbol to generate a reloc against. Exactly one of SECTION and
8366 NAME must be NULL. ADDEND is an expression for the addend. */
8369 lang_add_reloc (bfd_reloc_code_real_type reloc
,
8370 reloc_howto_type
*howto
,
8373 union etree_union
*addend
)
8375 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
8379 p
->section
= section
;
8381 p
->addend_exp
= addend
;
8383 p
->addend_value
= 0;
8384 p
->output_section
= NULL
;
8385 p
->output_offset
= 0;
8388 lang_assignment_statement_type
*
8389 lang_add_assignment (etree_type
*exp
)
8391 lang_assignment_statement_type
*new_stmt
;
8393 new_stmt
= new_stat (lang_assignment_statement
, stat_ptr
);
8394 new_stmt
->exp
= exp
;
8399 lang_add_attribute (enum statement_enum attribute
)
8401 new_statement (attribute
, sizeof (lang_statement_header_type
), stat_ptr
);
8405 lang_startup (const char *name
)
8407 if (first_file
->filename
!= NULL
)
8409 einfo (_("%F%P: multiple STARTUP files\n"));
8411 first_file
->filename
= name
;
8412 first_file
->local_sym_name
= name
;
8413 first_file
->flags
.real
= true;
8417 lang_float (bool maybe
)
8419 lang_float_flag
= maybe
;
8423 /* Work out the load- and run-time regions from a script statement, and
8424 store them in *LMA_REGION and *REGION respectively.
8426 MEMSPEC is the name of the run-time region, or the value of
8427 DEFAULT_MEMORY_REGION if the statement didn't specify one.
8428 LMA_MEMSPEC is the name of the load-time region, or null if the
8429 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
8430 had an explicit load address.
8432 It is an error to specify both a load region and a load address. */
8435 lang_get_regions (lang_memory_region_type
**region
,
8436 lang_memory_region_type
**lma_region
,
8437 const char *memspec
,
8438 const char *lma_memspec
,
8442 *lma_region
= lang_memory_region_lookup (lma_memspec
, false);
8444 /* If no runtime region or VMA has been specified, but the load region
8445 has been specified, then use the load region for the runtime region
8447 if (lma_memspec
!= NULL
8449 && strcmp (memspec
, DEFAULT_MEMORY_REGION
) == 0)
8450 *region
= *lma_region
;
8452 *region
= lang_memory_region_lookup (memspec
, false);
8454 if (have_lma
&& lma_memspec
!= 0)
8455 einfo (_("%X%P:%pS: section has both a load address and a load region\n"),
8460 lang_leave_output_section_statement (fill_type
*fill
, const char *memspec
,
8461 lang_output_section_phdr_list
*phdrs
,
8462 const char *lma_memspec
)
8464 lang_get_regions (¤t_section
->region
,
8465 ¤t_section
->lma_region
,
8466 memspec
, lma_memspec
,
8467 current_section
->load_base
!= NULL
,
8468 current_section
->addr_tree
!= NULL
);
8470 current_section
->fill
= fill
;
8471 current_section
->phdrs
= phdrs
;
8475 /* Set the output format type. -oformat overrides scripts. */
8478 lang_add_output_format (const char *format
,
8483 if (output_target
== NULL
|| !from_script
)
8485 if (command_line
.endian
== ENDIAN_BIG
8488 else if (command_line
.endian
== ENDIAN_LITTLE
8492 output_target
= format
;
8497 lang_add_insert (const char *where
, int is_before
)
8499 lang_insert_statement_type
*new_stmt
;
8501 new_stmt
= new_stat (lang_insert_statement
, stat_ptr
);
8502 new_stmt
->where
= where
;
8503 new_stmt
->is_before
= is_before
;
8504 saved_script_handle
= previous_script_handle
;
8507 /* Enter a group. This creates a new lang_group_statement, and sets
8508 stat_ptr to build new statements within the group. */
8511 lang_enter_group (void)
8513 lang_group_statement_type
*g
;
8515 g
= new_stat (lang_group_statement
, stat_ptr
);
8516 lang_list_init (&g
->children
);
8517 push_stat_ptr (&g
->children
);
8520 /* Leave a group. This just resets stat_ptr to start writing to the
8521 regular list of statements again. Note that this will not work if
8522 groups can occur inside anything else which can adjust stat_ptr,
8523 but currently they can't. */
8526 lang_leave_group (void)
8531 /* Add a new program header. This is called for each entry in a PHDRS
8532 command in a linker script. */
8535 lang_new_phdr (const char *name
,
8542 struct lang_phdr
*n
, **pp
;
8545 n
= stat_alloc (sizeof (struct lang_phdr
));
8548 n
->type
= exp_get_vma (type
, 0, "program header type");
8549 n
->filehdr
= filehdr
;
8554 hdrs
= n
->type
== 1 && (phdrs
|| filehdr
);
8556 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
8559 && !((*pp
)->filehdr
|| (*pp
)->phdrs
))
8561 einfo (_("%X%P:%pS: PHDRS and FILEHDR are not supported"
8562 " when prior PT_LOAD headers lack them\n"), NULL
);
8569 /* Record the program header information in the output BFD. FIXME: We
8570 should not be calling an ELF specific function here. */
8573 lang_record_phdrs (void)
8577 lang_output_section_phdr_list
*last
;
8578 struct lang_phdr
*l
;
8579 lang_output_section_statement_type
*os
;
8582 secs
= (asection
**) xmalloc (alc
* sizeof (asection
*));
8585 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
8592 for (os
= (void *) lang_os_list
.head
;
8596 lang_output_section_phdr_list
*pl
;
8598 if (os
->constraint
< 0)
8606 if (os
->sectype
== noload_section
8607 || os
->bfd_section
== NULL
8608 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
8611 /* Don't add orphans to PT_INTERP header. */
8617 lang_output_section_statement_type
*tmp_os
;
8619 /* If we have not run across a section with a program
8620 header assigned to it yet, then scan forwards to find
8621 one. This prevents inconsistencies in the linker's
8622 behaviour when a script has specified just a single
8623 header and there are sections in that script which are
8624 not assigned to it, and which occur before the first
8625 use of that header. See here for more details:
8626 http://sourceware.org/ml/binutils/2007-02/msg00291.html */
8627 for (tmp_os
= os
; tmp_os
; tmp_os
= tmp_os
->next
)
8630 last
= tmp_os
->phdrs
;
8634 einfo (_("%F%P: no sections assigned to phdrs\n"));
8639 if (os
->bfd_section
== NULL
)
8642 for (; pl
!= NULL
; pl
= pl
->next
)
8644 if (strcmp (pl
->name
, l
->name
) == 0)
8649 secs
= (asection
**) xrealloc (secs
,
8650 alc
* sizeof (asection
*));
8652 secs
[c
] = os
->bfd_section
;
8659 if (l
->flags
== NULL
)
8662 flags
= exp_get_vma (l
->flags
, 0, "phdr flags");
8667 at
= exp_get_vma (l
->at
, 0, "phdr load address");
8669 if (!bfd_record_phdr (link_info
.output_bfd
, l
->type
,
8670 l
->flags
!= NULL
, flags
, l
->at
!= NULL
,
8671 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
8672 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
8677 /* Make sure all the phdr assignments succeeded. */
8678 for (os
= (void *) lang_os_list
.head
;
8682 lang_output_section_phdr_list
*pl
;
8684 if (os
->constraint
< 0
8685 || os
->bfd_section
== NULL
)
8688 for (pl
= os
->phdrs
;
8691 if (!pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
8692 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
8693 os
->name
, pl
->name
);
8697 /* Record a list of sections which may not be cross referenced. */
8700 lang_add_nocrossref (lang_nocrossref_type
*l
)
8702 struct lang_nocrossrefs
*n
;
8704 n
= (struct lang_nocrossrefs
*) xmalloc (sizeof *n
);
8705 n
->next
= nocrossref_list
;
8707 n
->onlyfirst
= false;
8708 nocrossref_list
= n
;
8710 /* Set notice_all so that we get informed about all symbols. */
8711 link_info
.notice_all
= true;
8714 /* Record a section that cannot be referenced from a list of sections. */
8717 lang_add_nocrossref_to (lang_nocrossref_type
*l
)
8719 lang_add_nocrossref (l
);
8720 nocrossref_list
->onlyfirst
= true;
8723 /* Overlay handling. We handle overlays with some static variables. */
8725 /* The overlay virtual address. */
8726 static etree_type
*overlay_vma
;
8727 /* And subsection alignment. */
8728 static etree_type
*overlay_subalign
;
8730 /* An expression for the maximum section size seen so far. */
8731 static etree_type
*overlay_max
;
8733 /* A list of all the sections in this overlay. */
8735 struct overlay_list
{
8736 struct overlay_list
*next
;
8737 lang_output_section_statement_type
*os
;
8740 static struct overlay_list
*overlay_list
;
8742 /* Start handling an overlay. */
8745 lang_enter_overlay (etree_type
*vma_expr
, etree_type
*subalign
)
8747 /* The grammar should prevent nested overlays from occurring. */
8748 ASSERT (overlay_vma
== NULL
8749 && overlay_subalign
== NULL
8750 && overlay_max
== NULL
);
8752 overlay_vma
= vma_expr
;
8753 overlay_subalign
= subalign
;
8756 /* Start a section in an overlay. We handle this by calling
8757 lang_enter_output_section_statement with the correct VMA.
8758 lang_leave_overlay sets up the LMA and memory regions. */
8761 lang_enter_overlay_section (const char *name
)
8763 struct overlay_list
*n
;
8766 lang_enter_output_section_statement (name
, overlay_vma
, overlay_section
,
8767 0, 0, overlay_subalign
, 0, 0, 0);
8769 /* If this is the first section, then base the VMA of future
8770 sections on this one. This will work correctly even if `.' is
8771 used in the addresses. */
8772 if (overlay_list
== NULL
)
8773 overlay_vma
= exp_nameop (ADDR
, name
);
8775 /* Remember the section. */
8776 n
= (struct overlay_list
*) xmalloc (sizeof *n
);
8777 n
->os
= current_section
;
8778 n
->next
= overlay_list
;
8781 size
= exp_nameop (SIZEOF
, name
);
8783 /* Arrange to work out the maximum section end address. */
8784 if (overlay_max
== NULL
)
8787 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
8790 /* Finish a section in an overlay. There isn't any special to do
8794 lang_leave_overlay_section (fill_type
*fill
,
8795 lang_output_section_phdr_list
*phdrs
)
8802 name
= current_section
->name
;
8804 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
8805 region and that no load-time region has been specified. It doesn't
8806 really matter what we say here, since lang_leave_overlay will
8808 lang_leave_output_section_statement (fill
, DEFAULT_MEMORY_REGION
, phdrs
, 0);
8810 /* Define the magic symbols. */
8812 clean
= (char *) xmalloc (strlen (name
) + 1);
8814 for (s1
= name
; *s1
!= '\0'; s1
++)
8815 if (ISALNUM (*s1
) || *s1
== '_')
8819 buf
= (char *) xmalloc (strlen (clean
) + sizeof "__load_start_");
8820 sprintf (buf
, "__load_start_%s", clean
);
8821 lang_add_assignment (exp_provide (buf
,
8822 exp_nameop (LOADADDR
, name
),
8825 buf
= (char *) xmalloc (strlen (clean
) + sizeof "__load_stop_");
8826 sprintf (buf
, "__load_stop_%s", clean
);
8827 lang_add_assignment (exp_provide (buf
,
8829 exp_nameop (LOADADDR
, name
),
8830 exp_nameop (SIZEOF
, name
)),
8836 /* Finish an overlay. If there are any overlay wide settings, this
8837 looks through all the sections in the overlay and sets them. */
8840 lang_leave_overlay (etree_type
*lma_expr
,
8843 const char *memspec
,
8844 lang_output_section_phdr_list
*phdrs
,
8845 const char *lma_memspec
)
8847 lang_memory_region_type
*region
;
8848 lang_memory_region_type
*lma_region
;
8849 struct overlay_list
*l
;
8850 lang_nocrossref_type
*nocrossref
;
8852 lang_get_regions (®ion
, &lma_region
,
8853 memspec
, lma_memspec
,
8854 lma_expr
!= NULL
, false);
8858 /* After setting the size of the last section, set '.' to end of the
8860 if (overlay_list
!= NULL
)
8862 overlay_list
->os
->update_dot
= 1;
8863 overlay_list
->os
->update_dot_tree
8864 = exp_assign (".", exp_binop ('+', overlay_vma
, overlay_max
), false);
8870 struct overlay_list
*next
;
8872 if (fill
!= NULL
&& l
->os
->fill
== NULL
)
8875 l
->os
->region
= region
;
8876 l
->os
->lma_region
= lma_region
;
8878 /* The first section has the load address specified in the
8879 OVERLAY statement. The rest are worked out from that.
8880 The base address is not needed (and should be null) if
8881 an LMA region was specified. */
8884 l
->os
->load_base
= lma_expr
;
8885 l
->os
->sectype
= first_overlay_section
;
8887 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
8888 l
->os
->phdrs
= phdrs
;
8892 lang_nocrossref_type
*nc
;
8894 nc
= (lang_nocrossref_type
*) xmalloc (sizeof *nc
);
8895 nc
->name
= l
->os
->name
;
8896 nc
->next
= nocrossref
;
8905 if (nocrossref
!= NULL
)
8906 lang_add_nocrossref (nocrossref
);
8909 overlay_list
= NULL
;
8911 overlay_subalign
= NULL
;
8914 /* Version handling. This is only useful for ELF. */
8916 /* If PREV is NULL, return first version pattern matching particular symbol.
8917 If PREV is non-NULL, return first version pattern matching particular
8918 symbol after PREV (previously returned by lang_vers_match). */
8920 static struct bfd_elf_version_expr
*
8921 lang_vers_match (struct bfd_elf_version_expr_head
*head
,
8922 struct bfd_elf_version_expr
*prev
,
8926 const char *cxx_sym
= sym
;
8927 const char *java_sym
= sym
;
8928 struct bfd_elf_version_expr
*expr
= NULL
;
8929 enum demangling_styles curr_style
;
8931 curr_style
= CURRENT_DEMANGLING_STYLE
;
8932 cplus_demangle_set_style (no_demangling
);
8933 c_sym
= bfd_demangle (link_info
.output_bfd
, sym
, DMGL_NO_OPTS
);
8936 cplus_demangle_set_style (curr_style
);
8938 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
8940 cxx_sym
= bfd_demangle (link_info
.output_bfd
, sym
,
8941 DMGL_PARAMS
| DMGL_ANSI
);
8945 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
8947 java_sym
= bfd_demangle (link_info
.output_bfd
, sym
, DMGL_JAVA
);
8952 if (head
->htab
&& (prev
== NULL
|| prev
->literal
))
8954 struct bfd_elf_version_expr e
;
8956 switch (prev
? prev
->mask
: 0)
8959 if (head
->mask
& BFD_ELF_VERSION_C_TYPE
)
8962 expr
= (struct bfd_elf_version_expr
*)
8963 htab_find ((htab_t
) head
->htab
, &e
);
8964 while (expr
&& strcmp (expr
->pattern
, c_sym
) == 0)
8965 if (expr
->mask
== BFD_ELF_VERSION_C_TYPE
)
8971 case BFD_ELF_VERSION_C_TYPE
:
8972 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
8974 e
.pattern
= cxx_sym
;
8975 expr
= (struct bfd_elf_version_expr
*)
8976 htab_find ((htab_t
) head
->htab
, &e
);
8977 while (expr
&& strcmp (expr
->pattern
, cxx_sym
) == 0)
8978 if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
8984 case BFD_ELF_VERSION_CXX_TYPE
:
8985 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
8987 e
.pattern
= java_sym
;
8988 expr
= (struct bfd_elf_version_expr
*)
8989 htab_find ((htab_t
) head
->htab
, &e
);
8990 while (expr
&& strcmp (expr
->pattern
, java_sym
) == 0)
8991 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
9002 /* Finally, try the wildcards. */
9003 if (prev
== NULL
|| prev
->literal
)
9004 expr
= head
->remaining
;
9007 for (; expr
; expr
= expr
->next
)
9014 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
9017 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
9019 else if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
9023 if (fnmatch (expr
->pattern
, s
, 0) == 0)
9029 free ((char *) c_sym
);
9031 free ((char *) cxx_sym
);
9032 if (java_sym
!= sym
)
9033 free ((char *) java_sym
);
9037 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
9038 return a pointer to the symbol name with any backslash quotes removed. */
9041 realsymbol (const char *pattern
)
9044 bool changed
= false, backslash
= false;
9045 char *s
, *symbol
= (char *) xmalloc (strlen (pattern
) + 1);
9047 for (p
= pattern
, s
= symbol
; *p
!= '\0'; ++p
)
9049 /* It is a glob pattern only if there is no preceding
9053 /* Remove the preceding backslash. */
9060 if (*p
== '?' || *p
== '*' || *p
== '[')
9067 backslash
= *p
== '\\';
9083 /* This is called for each variable name or match expression. NEW_NAME is
9084 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
9085 pattern to be matched against symbol names. */
9087 struct bfd_elf_version_expr
*
9088 lang_new_vers_pattern (struct bfd_elf_version_expr
*orig
,
9089 const char *new_name
,
9093 struct bfd_elf_version_expr
*ret
;
9095 ret
= (struct bfd_elf_version_expr
*) xmalloc (sizeof *ret
);
9099 ret
->literal
= true;
9100 ret
->pattern
= literal_p
? new_name
: realsymbol (new_name
);
9101 if (ret
->pattern
== NULL
)
9103 ret
->pattern
= new_name
;
9104 ret
->literal
= false;
9107 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
9108 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
9109 else if (strcasecmp (lang
, "C++") == 0)
9110 ret
->mask
= BFD_ELF_VERSION_CXX_TYPE
;
9111 else if (strcasecmp (lang
, "Java") == 0)
9112 ret
->mask
= BFD_ELF_VERSION_JAVA_TYPE
;
9115 einfo (_("%X%P: unknown language `%s' in version information\n"),
9117 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
9120 return ldemul_new_vers_pattern (ret
);
9123 /* This is called for each set of variable names and match
9126 struct bfd_elf_version_tree
*
9127 lang_new_vers_node (struct bfd_elf_version_expr
*globals
,
9128 struct bfd_elf_version_expr
*locals
)
9130 struct bfd_elf_version_tree
*ret
;
9132 ret
= (struct bfd_elf_version_tree
*) xcalloc (1, sizeof *ret
);
9133 ret
->globals
.list
= globals
;
9134 ret
->locals
.list
= locals
;
9135 ret
->match
= lang_vers_match
;
9136 ret
->name_indx
= (unsigned int) -1;
9140 /* This static variable keeps track of version indices. */
9142 static int version_index
;
9145 version_expr_head_hash (const void *p
)
9147 const struct bfd_elf_version_expr
*e
=
9148 (const struct bfd_elf_version_expr
*) p
;
9150 return htab_hash_string (e
->pattern
);
9154 version_expr_head_eq (const void *p1
, const void *p2
)
9156 const struct bfd_elf_version_expr
*e1
=
9157 (const struct bfd_elf_version_expr
*) p1
;
9158 const struct bfd_elf_version_expr
*e2
=
9159 (const struct bfd_elf_version_expr
*) p2
;
9161 return strcmp (e1
->pattern
, e2
->pattern
) == 0;
9165 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head
*head
)
9168 struct bfd_elf_version_expr
*e
, *next
;
9169 struct bfd_elf_version_expr
**list_loc
, **remaining_loc
;
9171 for (e
= head
->list
; e
; e
= e
->next
)
9175 head
->mask
|= e
->mask
;
9180 head
->htab
= htab_create (count
* 2, version_expr_head_hash
,
9181 version_expr_head_eq
, NULL
);
9182 list_loc
= &head
->list
;
9183 remaining_loc
= &head
->remaining
;
9184 for (e
= head
->list
; e
; e
= next
)
9190 remaining_loc
= &e
->next
;
9194 void **loc
= htab_find_slot ((htab_t
) head
->htab
, e
, INSERT
);
9198 struct bfd_elf_version_expr
*e1
, *last
;
9200 e1
= (struct bfd_elf_version_expr
*) *loc
;
9204 if (e1
->mask
== e
->mask
)
9212 while (e1
&& strcmp (e1
->pattern
, e
->pattern
) == 0);
9216 /* This is a duplicate. */
9217 /* FIXME: Memory leak. Sometimes pattern is not
9218 xmalloced alone, but in larger chunk of memory. */
9219 /* free (e->pattern); */
9224 e
->next
= last
->next
;
9232 list_loc
= &e
->next
;
9236 *remaining_loc
= NULL
;
9237 *list_loc
= head
->remaining
;
9240 head
->remaining
= head
->list
;
9243 /* This is called when we know the name and dependencies of the
9247 lang_register_vers_node (const char *name
,
9248 struct bfd_elf_version_tree
*version
,
9249 struct bfd_elf_version_deps
*deps
)
9251 struct bfd_elf_version_tree
*t
, **pp
;
9252 struct bfd_elf_version_expr
*e1
;
9257 if (link_info
.version_info
!= NULL
9258 && (name
[0] == '\0' || link_info
.version_info
->name
[0] == '\0'))
9260 einfo (_("%X%P: anonymous version tag cannot be combined"
9261 " with other version tags\n"));
9266 /* Make sure this node has a unique name. */
9267 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9268 if (strcmp (t
->name
, name
) == 0)
9269 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
9271 lang_finalize_version_expr_head (&version
->globals
);
9272 lang_finalize_version_expr_head (&version
->locals
);
9274 /* Check the global and local match names, and make sure there
9275 aren't any duplicates. */
9277 for (e1
= version
->globals
.list
; e1
!= NULL
; e1
= e1
->next
)
9279 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9281 struct bfd_elf_version_expr
*e2
;
9283 if (t
->locals
.htab
&& e1
->literal
)
9285 e2
= (struct bfd_elf_version_expr
*)
9286 htab_find ((htab_t
) t
->locals
.htab
, e1
);
9287 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
9289 if (e1
->mask
== e2
->mask
)
9290 einfo (_("%X%P: duplicate expression `%s'"
9291 " in version information\n"), e1
->pattern
);
9295 else if (!e1
->literal
)
9296 for (e2
= t
->locals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
9297 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
9298 && e1
->mask
== e2
->mask
)
9299 einfo (_("%X%P: duplicate expression `%s'"
9300 " in version information\n"), e1
->pattern
);
9304 for (e1
= version
->locals
.list
; e1
!= NULL
; e1
= e1
->next
)
9306 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9308 struct bfd_elf_version_expr
*e2
;
9310 if (t
->globals
.htab
&& e1
->literal
)
9312 e2
= (struct bfd_elf_version_expr
*)
9313 htab_find ((htab_t
) t
->globals
.htab
, e1
);
9314 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
9316 if (e1
->mask
== e2
->mask
)
9317 einfo (_("%X%P: duplicate expression `%s'"
9318 " in version information\n"),
9323 else if (!e1
->literal
)
9324 for (e2
= t
->globals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
9325 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
9326 && e1
->mask
== e2
->mask
)
9327 einfo (_("%X%P: duplicate expression `%s'"
9328 " in version information\n"), e1
->pattern
);
9332 version
->deps
= deps
;
9333 version
->name
= name
;
9334 if (name
[0] != '\0')
9337 version
->vernum
= version_index
;
9340 version
->vernum
= 0;
9342 for (pp
= &link_info
.version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
9347 /* This is called when we see a version dependency. */
9349 struct bfd_elf_version_deps
*
9350 lang_add_vers_depend (struct bfd_elf_version_deps
*list
, const char *name
)
9352 struct bfd_elf_version_deps
*ret
;
9353 struct bfd_elf_version_tree
*t
;
9355 ret
= (struct bfd_elf_version_deps
*) xmalloc (sizeof *ret
);
9358 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9360 if (strcmp (t
->name
, name
) == 0)
9362 ret
->version_needed
= t
;
9367 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
9369 ret
->version_needed
= NULL
;
9374 lang_do_version_exports_section (void)
9376 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
9378 LANG_FOR_EACH_INPUT_STATEMENT (is
)
9380 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
9388 contents
= (char *) xmalloc (len
);
9389 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
9390 einfo (_("%X%P: unable to read .exports section contents\n"), sec
);
9393 while (p
< contents
+ len
)
9395 greg
= lang_new_vers_pattern (greg
, p
, NULL
, false);
9396 p
= strchr (p
, '\0') + 1;
9399 /* Do not free the contents, as we used them creating the regex. */
9401 /* Do not include this section in the link. */
9402 sec
->flags
|= SEC_EXCLUDE
| SEC_KEEP
;
9405 lreg
= lang_new_vers_pattern (NULL
, "*", NULL
, false);
9406 lang_register_vers_node (command_line
.version_exports_section
,
9407 lang_new_vers_node (greg
, lreg
), NULL
);
9410 /* Evaluate LENGTH and ORIGIN parts of MEMORY spec. This is initially
9411 called with UPDATE_REGIONS_P set to FALSE, in this case no errors are
9412 thrown, however, references to symbols in the origin and length fields
9413 will be pushed into the symbol table, this allows PROVIDE statements to
9414 then provide these symbols. This function is called a second time with
9415 UPDATE_REGIONS_P set to TRUE, this time the we update the actual region
9416 data structures, and throw errors if missing symbols are encountered. */
9419 lang_do_memory_regions (bool update_regions_p
)
9421 lang_memory_region_type
*r
= lang_memory_region_list
;
9423 for (; r
!= NULL
; r
= r
->next
)
9427 exp_fold_tree_no_dot (r
->origin_exp
);
9428 if (update_regions_p
)
9430 if (expld
.result
.valid_p
)
9432 r
->origin
= expld
.result
.value
;
9433 r
->current
= r
->origin
;
9436 einfo (_("%P: invalid origin for memory region %s\n"),
9442 exp_fold_tree_no_dot (r
->length_exp
);
9443 if (update_regions_p
)
9445 if (expld
.result
.valid_p
)
9446 r
->length
= expld
.result
.value
;
9448 einfo (_("%P: invalid length for memory region %s\n"),
9456 lang_add_unique (const char *name
)
9458 struct unique_sections
*ent
;
9460 for (ent
= unique_section_list
; ent
; ent
= ent
->next
)
9461 if (strcmp (ent
->name
, name
) == 0)
9464 ent
= (struct unique_sections
*) xmalloc (sizeof *ent
);
9465 ent
->name
= xstrdup (name
);
9466 ent
->next
= unique_section_list
;
9467 unique_section_list
= ent
;
9470 /* Append the list of dynamic symbols to the existing one. */
9473 lang_append_dynamic_list (struct bfd_elf_dynamic_list
**list_p
,
9474 struct bfd_elf_version_expr
*dynamic
)
9478 struct bfd_elf_version_expr
*tail
;
9479 for (tail
= dynamic
; tail
->next
!= NULL
; tail
= tail
->next
)
9481 tail
->next
= (*list_p
)->head
.list
;
9482 (*list_p
)->head
.list
= dynamic
;
9486 struct bfd_elf_dynamic_list
*d
;
9488 d
= (struct bfd_elf_dynamic_list
*) xcalloc (1, sizeof *d
);
9489 d
->head
.list
= dynamic
;
9490 d
->match
= lang_vers_match
;
9495 /* Append the list of C++ typeinfo dynamic symbols to the existing
9499 lang_append_dynamic_list_cpp_typeinfo (void)
9501 const char *symbols
[] =
9503 "typeinfo name for*",
9506 struct bfd_elf_version_expr
*dynamic
= NULL
;
9509 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
9510 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
9513 lang_append_dynamic_list (&link_info
.dynamic_list
, dynamic
);
9516 /* Append the list of C++ operator new and delete dynamic symbols to the
9520 lang_append_dynamic_list_cpp_new (void)
9522 const char *symbols
[] =
9527 struct bfd_elf_version_expr
*dynamic
= NULL
;
9530 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
9531 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
9534 lang_append_dynamic_list (&link_info
.dynamic_list
, dynamic
);
9537 /* Scan a space and/or comma separated string of features. */
9540 lang_ld_feature (char *str
)
9548 while (*p
== ',' || ISSPACE (*p
))
9553 while (*q
&& *q
!= ',' && !ISSPACE (*q
))
9557 if (strcasecmp (p
, "SANE_EXPR") == 0)
9558 config
.sane_expr
= true;
9560 einfo (_("%X%P: unknown feature `%s'\n"), p
);
9566 /* Pretty print memory amount. */
9569 lang_print_memory_size (uint64_t sz
)
9571 if ((sz
& 0x3fffffff) == 0)
9572 printf ("%10" PRIu64
" GB", sz
>> 30);
9573 else if ((sz
& 0xfffff) == 0)
9574 printf ("%10" PRIu64
" MB", sz
>> 20);
9575 else if ((sz
& 0x3ff) == 0)
9576 printf ("%10" PRIu64
" KB", sz
>> 10);
9578 printf (" %10" PRIu64
" B", sz
);
9581 /* Implement --print-memory-usage: disply per region memory usage. */
9584 lang_print_memory_usage (void)
9586 lang_memory_region_type
*r
;
9588 printf ("Memory region Used Size Region Size %%age Used\n");
9589 for (r
= lang_memory_region_list
; r
->next
!= NULL
; r
= r
->next
)
9591 bfd_vma used_length
= r
->current
- r
->origin
;
9593 printf ("%16s: ",r
->name_list
.name
);
9594 lang_print_memory_size (used_length
);
9595 lang_print_memory_size (r
->length
);
9599 double percent
= used_length
* 100.0 / r
->length
;
9600 printf (" %6.2f%%", percent
);