1 /* Linker command language support.
2 Copyright (C) 1991-2023 Free Software Foundation, Inc.
4 This file is part of the GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
24 #include "libiberty.h"
25 #include "filenames.h"
26 #include "safe-ctype.h"
46 #if BFD_SUPPORTS_PLUGINS
51 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
54 /* Convert between addresses in bytes and sizes in octets.
55 For currently supported targets, octets_per_byte is always a power
56 of two, so we can use shifts. */
57 #define TO_ADDR(X) ((X) >> opb_shift)
58 #define TO_SIZE(X) ((X) << opb_shift)
60 /* Local variables. */
61 static struct obstack stat_obstack
;
62 static struct obstack map_obstack
;
63 static struct obstack pt_obstack
;
65 #define obstack_chunk_alloc xmalloc
66 #define obstack_chunk_free free
67 static const char *entry_symbol_default
= "start";
68 static bool map_head_is_link_order
= false;
69 static lang_output_section_statement_type
*default_common_section
;
70 static bool map_option_f
;
71 static bfd_vma print_dot
;
72 static lang_input_statement_type
*first_file
;
73 static const char *current_target
;
74 static lang_statement_list_type
*stat_save
[10];
75 static lang_statement_list_type
**stat_save_ptr
= &stat_save
[0];
76 static struct unique_sections
*unique_section_list
;
77 static struct asneeded_minfo
*asneeded_list_head
;
78 static unsigned int opb_shift
= 0;
80 /* Forward declarations. */
81 static void exp_init_os (etree_type
*);
82 static lang_input_statement_type
*lookup_name (const char *);
83 static bool wont_add_section_p (asection
*,
84 lang_output_section_statement_type
*);
85 static void insert_undefined (const char *);
86 static bool sort_def_symbol (struct bfd_link_hash_entry
*, void *);
87 static lang_statement_union_type
*new_statement (enum statement_enum type
,
89 lang_statement_list_type
*list
);
90 static void print_statement (lang_statement_union_type
*,
91 lang_output_section_statement_type
*);
92 static void print_statement_list (lang_statement_union_type
*,
93 lang_output_section_statement_type
*);
94 static void print_statements (void);
95 static void print_input_section (asection
*, bool);
96 static bool lang_one_common (struct bfd_link_hash_entry
*, void *);
97 static void lang_record_phdrs (void);
98 static void lang_do_version_exports_section (void);
99 static void lang_finalize_version_expr_head
100 (struct bfd_elf_version_expr_head
*);
101 static void lang_do_memory_regions (bool);
103 /* Exported variables. */
104 const char *output_target
;
105 lang_output_section_statement_type
*abs_output_section
;
106 /* Header for list of statements corresponding to any files involved in the
107 link, either specified from the command-line or added implicitely (eg.
108 archive member used to resolved undefined symbol, wildcard statement from
109 linker script, etc.). Next pointer is in next field of a
110 lang_statement_header_type (reached via header field in a
111 lang_statement_union). */
112 lang_statement_list_type statement_list
;
113 lang_statement_list_type lang_os_list
;
114 lang_statement_list_type
*stat_ptr
= &statement_list
;
115 /* Header for list of statements corresponding to files used in the final
116 executable. This can be either object file specified on the command-line
117 or library member resolving an undefined reference. Next pointer is in next
118 field of a lang_input_statement_type (reached via input_statement field in a
119 lang_statement_union). */
120 lang_statement_list_type file_chain
= { NULL
, NULL
};
121 /* Header for list of statements corresponding to files specified on the
122 command-line for linking. It thus contains real object files and archive
123 but not archive members. Next pointer is in next_real_file field of a
124 lang_input_statement_type statement (reached via input_statement field in a
125 lang_statement_union). */
126 lang_statement_list_type input_file_chain
;
127 static const char *current_input_file
;
128 struct bfd_elf_dynamic_list
**current_dynamic_list_p
;
129 struct bfd_sym_chain entry_symbol
= { NULL
, NULL
};
130 const char *entry_section
= ".text";
131 struct lang_input_statement_flags input_flags
;
132 bool entry_from_cmdline
;
133 bool lang_has_input_file
= false;
134 bool had_output_filename
= false;
135 bool lang_float_flag
= false;
136 bool delete_output_file_on_failure
= false;
137 bool enable_linker_version
= false;
138 struct lang_phdr
*lang_phdr_list
;
139 struct lang_nocrossrefs
*nocrossref_list
;
140 struct asneeded_minfo
**asneeded_list_tail
;
142 static ctf_dict_t
*ctf_output
;
145 /* Functions that traverse the linker script and might evaluate
146 DEFINED() need to increment this at the start of the traversal. */
147 int lang_statement_iteration
= 0;
149 /* Count times through one_lang_size_sections_pass after mark phase. */
150 static int lang_sizing_iteration
= 0;
152 /* Return TRUE if the PATTERN argument is a wildcard pattern.
153 Although backslashes are treated specially if a pattern contains
154 wildcards, we do not consider the mere presence of a backslash to
155 be enough to cause the pattern to be treated as a wildcard.
156 That lets us handle DOS filenames more naturally. */
157 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
159 #define new_stat(x, y) \
160 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
162 #define outside_section_address(q) \
163 ((q)->output_offset + (q)->output_section->vma)
165 #define outside_symbol_address(q) \
166 ((q)->value + outside_section_address (q->section))
168 /* CTF sections smaller than this are not compressed: compression of
169 dictionaries this small doesn't gain much, and this lets consumers mmap the
170 sections directly out of the ELF file and use them with no decompression
171 overhead if they want to. */
172 #define CTF_COMPRESSION_THRESHOLD 4096
175 stat_alloc (size_t size
)
177 return obstack_alloc (&stat_obstack
, size
);
180 /* Code for handling simple wildcards without going through fnmatch,
181 which can be expensive because of charset translations etc. */
183 /* A simple wild is a literal string followed by a single '*',
184 where the literal part is at least 4 characters long. */
187 is_simple_wild (const char *name
)
189 size_t len
= strcspn (name
, "*?[");
190 return len
>= 4 && name
[len
] == '*' && name
[len
+ 1] == '\0';
194 match_simple_wild (const char *pattern
, const char *name
)
196 /* The first four characters of the pattern are guaranteed valid
197 non-wildcard characters. So we can go faster. */
198 if (pattern
[0] != name
[0] || pattern
[1] != name
[1]
199 || pattern
[2] != name
[2] || pattern
[3] != name
[3])
204 while (*pattern
!= '*')
205 if (*name
++ != *pattern
++)
212 name_match (const char *pattern
, const char *name
)
214 if (is_simple_wild (pattern
))
215 return !match_simple_wild (pattern
, name
);
216 if (wildcardp (pattern
))
217 return fnmatch (pattern
, name
, 0);
218 return strcmp (pattern
, name
);
221 /* Given an analyzed wildcard_spec SPEC, match it against NAME,
222 returns zero on a match, non-zero if there's no match. */
225 spec_match (const struct wildcard_spec
*spec
, const char *name
)
227 size_t nl
= spec
->namelen
;
228 size_t pl
= spec
->prefixlen
;
229 size_t sl
= spec
->suffixlen
;
230 size_t inputlen
= strlen (name
);
238 r
= memcmp (spec
->name
, name
, pl
);
248 r
= memcmp (spec
->name
+ nl
- sl
, name
+ inputlen
- sl
, sl
);
253 if (nl
== pl
+ sl
+ 1 && spec
->name
[pl
] == '*')
257 return fnmatch (spec
->name
+ pl
, name
+ pl
, 0);
266 ldirname (const char *name
)
268 const char *base
= lbasename (name
);
271 while (base
> name
&& IS_DIR_SEPARATOR (base
[-1]))
275 dirname
= strdup (name
);
276 dirname
[base
- name
] = '\0';
280 /* If PATTERN is of the form archive:file, return a pointer to the
281 separator. If not, return NULL. */
284 archive_path (const char *pattern
)
288 if (link_info
.path_separator
== 0)
291 p
= strchr (pattern
, link_info
.path_separator
);
292 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
293 if (p
== NULL
|| link_info
.path_separator
!= ':')
296 /* Assume a match on the second char is part of drive specifier,
297 as in "c:\silly.dos". */
298 if (p
== pattern
+ 1 && ISALPHA (*pattern
))
299 p
= strchr (p
+ 1, link_info
.path_separator
);
304 /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path,
305 return whether F matches FILE_SPEC. */
308 input_statement_is_archive_path (const char *file_spec
, char *sep
,
309 lang_input_statement_type
*f
)
314 || name_match (sep
+ 1, f
->filename
) == 0)
315 && ((sep
!= file_spec
)
316 == (f
->the_bfd
!= NULL
&& f
->the_bfd
->my_archive
!= NULL
)))
320 if (sep
!= file_spec
)
322 const char *aname
= bfd_get_filename (f
->the_bfd
->my_archive
);
324 match
= name_match (file_spec
, aname
) == 0;
325 *sep
= link_info
.path_separator
;
332 unique_section_p (const asection
*sec
,
333 const lang_output_section_statement_type
*os
)
335 struct unique_sections
*unam
;
338 if (!link_info
.resolve_section_groups
339 && sec
->owner
!= NULL
340 && bfd_is_group_section (sec
->owner
, sec
))
342 && strcmp (os
->name
, DISCARD_SECTION_NAME
) == 0);
345 for (unam
= unique_section_list
; unam
; unam
= unam
->next
)
346 if (name_match (unam
->name
, secnam
) == 0)
352 /* Generic traversal routines for finding matching sections. */
354 /* Return true if FILE matches a pattern in EXCLUDE_LIST, otherwise return
358 walk_wild_file_in_exclude_list (struct name_list
*exclude_list
,
359 lang_input_statement_type
*file
)
361 struct name_list
*list_tmp
;
363 for (list_tmp
= exclude_list
;
365 list_tmp
= list_tmp
->next
)
367 char *p
= archive_path (list_tmp
->name
);
371 if (input_statement_is_archive_path (list_tmp
->name
, p
, file
))
375 else if (name_match (list_tmp
->name
, file
->filename
) == 0)
378 /* FIXME: Perhaps remove the following at some stage? Matching
379 unadorned archives like this was never documented and has
380 been superceded by the archive:path syntax. */
381 else if (file
->the_bfd
!= NULL
382 && file
->the_bfd
->my_archive
!= NULL
383 && name_match (list_tmp
->name
,
384 bfd_get_filename (file
->the_bfd
->my_archive
)) == 0)
391 /* Add SECTION (from input FILE) to the list of matching sections
392 within PTR (the matching wildcard is SEC). */
395 add_matching_section (lang_wild_statement_type
*ptr
,
396 struct wildcard_list
*sec
,
398 lang_input_statement_type
*file
)
400 lang_input_matcher_type
*new_section
;
401 /* Add a section reference to the list. */
402 new_section
= new_stat (lang_input_matcher
, &ptr
->matching_sections
);
403 new_section
->section
= section
;
404 new_section
->pattern
= sec
;
405 new_section
->input_stmt
= file
;
408 /* Process section S (from input file FILE) in relation to wildcard
409 statement PTR. We already know that a prefix of the name of S matches
410 some wildcard in PTR's wildcard list. Here we check if the filename
411 matches as well (if it's specified) and if any of the wildcards in fact
415 walk_wild_section_match (lang_wild_statement_type
*ptr
,
416 lang_input_statement_type
*file
,
419 struct wildcard_list
*sec
;
420 const char *file_spec
= ptr
->filename
;
423 /* Check if filenames match. */
424 if (file_spec
== NULL
)
426 else if ((p
= archive_path (file_spec
)) != NULL
)
428 if (!input_statement_is_archive_path (file_spec
, p
, file
))
431 else if (wildcardp (file_spec
))
433 if (fnmatch (file_spec
, file
->filename
, 0) != 0)
438 /* XXX Matching against non-wildcard filename in wild statements
439 was done by going through lookup_name, which uses
440 ->local_sym_name to compare against, not ->filename. We retain
441 this behaviour even though the above code paths use filename.
442 It would be more logical to use it here as well, in which
443 case the above wildcard() arm could be folded into this by using
444 name_match. This would also solve the worry of what to do
445 about unset local_sym_name (in which case lookup_name simply adds
446 the input file again). */
447 const char *filename
= file
->local_sym_name
;
449 || filename_cmp (filename
, file_spec
) != 0)
453 /* If filename is excluded we're done. */
454 if (walk_wild_file_in_exclude_list (ptr
->exclude_name_list
, file
))
457 /* Check section name against each wildcard spec. If there's no
458 wildcard all sections match. */
459 sec
= ptr
->section_list
;
461 add_matching_section (ptr
, sec
, s
, file
);
464 const char *sname
= bfd_section_name (s
);
465 for (; sec
!= NULL
; sec
= sec
->next
)
467 if (sec
->spec
.name
!= NULL
468 && spec_match (&sec
->spec
, sname
) != 0)
471 /* Don't process sections from files which were excluded. */
472 if (!walk_wild_file_in_exclude_list (sec
->spec
.exclude_name_list
,
474 add_matching_section (ptr
, sec
, s
, file
);
479 /* Return the numerical value of the init_priority attribute from
480 section name NAME. */
483 get_init_priority (const asection
*sec
)
485 const char *name
= bfd_section_name (sec
);
488 /* GCC uses the following section names for the init_priority
489 attribute with numerical values 101 to 65535 inclusive. A
490 lower value means a higher priority.
492 1: .init_array.NNNNN/.fini_array.NNNNN: Where NNNNN is the
493 decimal numerical value of the init_priority attribute.
494 The order of execution in .init_array is forward and
495 .fini_array is backward.
496 2: .ctors.NNNNN/.dtors.NNNNN: Where NNNNN is 65535 minus the
497 decimal numerical value of the init_priority attribute.
498 The order of execution in .ctors is backward and .dtors
501 .init_array.NNNNN sections would normally be placed in an output
502 .init_array section, .fini_array.NNNNN in .fini_array,
503 .ctors.NNNNN in .ctors, and .dtors.NNNNN in .dtors. This means
504 we should sort by increasing number (and could just use
505 SORT_BY_NAME in scripts). However if .ctors.NNNNN sections are
506 being placed in .init_array (which may also contain
507 .init_array.NNNNN sections) or .dtors.NNNNN sections are being
508 placed in .fini_array then we need to extract the init_priority
509 attribute and sort on that. */
510 dot
= strrchr (name
, '.');
511 if (dot
!= NULL
&& ISDIGIT (dot
[1]))
514 unsigned long init_priority
= strtoul (dot
+ 1, &end
, 10);
518 && (strncmp (name
, ".ctors", 6) == 0
519 || strncmp (name
, ".dtors", 6) == 0))
520 init_priority
= 65535 - init_priority
;
521 if (init_priority
<= INT_MAX
)
522 return init_priority
;
528 /* Compare sections ASEC and BSEC according to SORT. */
531 compare_section (sort_type sort
, asection
*asec
, asection
*bsec
)
534 int a_priority
, b_priority
;
541 case by_init_priority
:
542 a_priority
= get_init_priority (asec
);
543 b_priority
= get_init_priority (bsec
);
544 if (a_priority
< 0 || b_priority
< 0)
546 ret
= a_priority
- b_priority
;
552 case by_alignment_name
:
553 ret
= bfd_section_alignment (bsec
) - bfd_section_alignment (asec
);
560 ret
= strcmp (bfd_section_name (asec
), bfd_section_name (bsec
));
563 case by_name_alignment
:
564 ret
= strcmp (bfd_section_name (asec
), bfd_section_name (bsec
));
570 ret
= bfd_section_alignment (bsec
) - bfd_section_alignment (asec
);
577 /* PE puts the sort key in the input statement. */
580 sort_filename (bfd
*abfd
)
582 lang_input_statement_type
*is
= bfd_usrdata (abfd
);
585 return bfd_get_filename (abfd
);
588 /* Handle wildcard sorting. This returns the place in a binary search tree
589 where this FILE:SECTION should be inserted for wild statement WILD where
590 the spec SEC was the matching one. The tree is later linearized. */
592 static lang_section_bst_type
**
593 wild_sort (lang_wild_statement_type
*wild
,
594 struct wildcard_list
*sec
,
595 lang_input_statement_type
*file
,
598 lang_section_bst_type
**tree
;
600 if (!wild
->filenames_sorted
601 && (sec
== NULL
|| sec
->spec
.sorted
== none
602 || sec
->spec
.sorted
== by_none
))
604 /* We might be called even if _this_ spec doesn't need sorting,
605 in which case we simply append at the right end of tree. */
606 return wild
->rightmost
;
612 /* Sorting by filename takes precedence over sorting by section
615 if (wild
->filenames_sorted
)
620 asection
*lsec
= (*tree
)->section
;
622 /* The PE support for the .idata section as generated by
623 dlltool assumes that files will be sorted by the name of
624 the archive and then the name of the file within the
627 fa
= file
->the_bfd
->my_archive
!= NULL
;
629 fn
= sort_filename (file
->the_bfd
->my_archive
);
631 fn
= sort_filename (file
->the_bfd
);
633 la
= lsec
->owner
->my_archive
!= NULL
;
635 ln
= sort_filename (lsec
->owner
->my_archive
);
637 ln
= sort_filename (lsec
->owner
);
639 i
= filename_cmp (fn
, ln
);
641 { tree
= &((*tree
)->right
); continue; }
643 { tree
= &((*tree
)->left
); continue; }
648 fn
= sort_filename (file
->the_bfd
);
650 ln
= sort_filename (lsec
->owner
);
652 i
= filename_cmp (fn
, ln
);
654 { tree
= &((*tree
)->right
); continue; }
656 { tree
= &((*tree
)->left
); continue; }
660 /* Here either the files are not sorted by name, or we are
661 looking at the sections for this file. */
663 /* Find the correct node to append this section. */
664 if (sec
&& sec
->spec
.sorted
!= none
&& sec
->spec
.sorted
!= by_none
665 && compare_section (sec
->spec
.sorted
, section
, (*tree
)->section
) < 0)
666 tree
= &((*tree
)->left
);
668 tree
= &((*tree
)->right
);
674 /* Use wild_sort to build a BST to sort sections. */
677 output_section_callback_sort (lang_wild_statement_type
*ptr
,
678 struct wildcard_list
*sec
,
680 lang_input_statement_type
*file
,
683 lang_section_bst_type
*node
;
684 lang_section_bst_type
**tree
;
685 lang_output_section_statement_type
*os
;
687 os
= (lang_output_section_statement_type
*) output
;
689 if (unique_section_p (section
, os
))
692 /* Don't add sections to the tree when we already know that
693 lang_add_section won't do anything with it. */
694 if (wont_add_section_p (section
, os
))
697 node
= (lang_section_bst_type
*) xmalloc (sizeof (lang_section_bst_type
));
700 node
->section
= section
;
701 node
->pattern
= ptr
->section_list
;
703 tree
= wild_sort (ptr
, sec
, file
, section
);
707 if (tree
== ptr
->rightmost
)
708 ptr
->rightmost
= &node
->right
;
712 /* Convert a sorted sections' BST back to list form. */
715 output_section_callback_tree_to_list (lang_wild_statement_type
*ptr
,
716 lang_section_bst_type
*tree
,
720 output_section_callback_tree_to_list (ptr
, tree
->left
, output
);
722 lang_add_section (&ptr
->children
, tree
->section
, tree
->pattern
,
723 ptr
->section_flag_list
,
724 (lang_output_section_statement_type
*) output
);
727 output_section_callback_tree_to_list (ptr
, tree
->right
, output
);
733 /* Sections are matched against wildcard statements via a prefix tree.
734 The prefix tree holds prefixes of all matching patterns (up to the first
735 wildcard character), and the wild statement from which those patterns
736 came. When matching a section name against the tree we're walking through
737 the tree character by character. Each statement we hit is one that
738 potentially matches. This is checked by actually going through the
739 (glob) matching routines.
741 When the section name turns out to actually match we record that section
742 in the wild statements list of matching sections. */
744 /* A prefix can be matched by multiple statement, so we need a list of them. */
745 struct wild_stmt_list
747 lang_wild_statement_type
*stmt
;
748 struct wild_stmt_list
*next
;
751 /* The prefix tree itself. */
754 /* The list of all children (linked via .next). */
755 struct prefixtree
*child
;
756 struct prefixtree
*next
;
757 /* This tree node is responsible for the prefix of parent plus 'c'. */
759 /* The statements that potentially can match this prefix. */
760 struct wild_stmt_list
*stmt
;
763 /* We always have a root node in the prefix tree. It corresponds to the
764 empty prefix. E.g. a glob like "*" would sit in this root. */
765 static struct prefixtree the_root
, *ptroot
= &the_root
;
767 /* Given a prefix tree in *TREE, corresponding to prefix P, find or
768 INSERT the tree node corresponding to prefix P+C. */
770 static struct prefixtree
*
771 get_prefix_tree (struct prefixtree
**tree
, char c
, bool insert
)
773 struct prefixtree
*t
;
774 for (t
= *tree
; t
; t
= t
->next
)
779 t
= (struct prefixtree
*) obstack_alloc (&pt_obstack
, sizeof *t
);
788 /* Add STMT to the set of statements that can be matched by the prefix
789 corresponding to prefix tree T. */
792 pt_add_stmt (struct prefixtree
*t
, lang_wild_statement_type
*stmt
)
794 struct wild_stmt_list
*sl
, **psl
;
795 sl
= (struct wild_stmt_list
*) obstack_alloc (&pt_obstack
, sizeof *sl
);
804 /* Insert STMT into the global prefix tree. */
807 insert_prefix_tree (lang_wild_statement_type
*stmt
)
809 struct wildcard_list
*sec
;
810 struct prefixtree
*t
;
812 if (!stmt
->section_list
)
814 /* If we have no section_list (no wildcards in the wild STMT),
815 then every section name will match, so add this to the root. */
816 pt_add_stmt (ptroot
, stmt
);
820 for (sec
= stmt
->section_list
; sec
; sec
= sec
->next
)
822 const char *name
= sec
->spec
.name
? sec
->spec
.name
: "*";
825 for (; (c
= *name
); name
++)
827 if (c
== '*' || c
== '[' || c
== '?')
829 t
= get_prefix_tree (&t
->child
, c
, true);
831 /* If we hit a glob character, the matching prefix is what we saw
832 until now. If we hit the end of pattern (hence it's no glob) then
833 we can do better: we only need to record a match when a section name
834 completely matches, not merely a prefix, so record the trailing 0
837 t
= get_prefix_tree (&t
->child
, 0, true);
838 pt_add_stmt (t
, stmt
);
842 /* Dump T indented by INDENT spaces. */
845 debug_prefix_tree_rec (struct prefixtree
*t
, int indent
)
847 for (; t
; t
= t
->next
)
849 struct wild_stmt_list
*sl
;
850 printf ("%*s %c", indent
, "", t
->c
);
851 for (sl
= t
->stmt
; sl
; sl
= sl
->next
)
853 struct wildcard_list
*curr
;
854 printf (" %p ", sl
->stmt
);
855 for (curr
= sl
->stmt
->section_list
; curr
; curr
= curr
->next
)
856 printf ("%s ", curr
->spec
.name
? curr
->spec
.name
: "*");
859 debug_prefix_tree_rec (t
->child
, indent
+ 2);
863 /* Dump the global prefix tree. */
866 debug_prefix_tree (void)
868 debug_prefix_tree_rec (ptroot
, 2);
871 /* Like strcspn() but start to look from the end to beginning of
872 S. Returns the length of the suffix of S consisting entirely
873 of characters not in REJECT. */
876 rstrcspn (const char *s
, const char *reject
)
878 size_t len
= strlen (s
), sufflen
= 0;
882 if (strchr (reject
, c
) != 0)
889 /* Analyze the wildcards in wild statement PTR to setup various
890 things for quick matching. */
893 analyze_walk_wild_section_handler (lang_wild_statement_type
*ptr
)
895 struct wildcard_list
*sec
;
898 ptr
->rightmost
= &ptr
->tree
;
900 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
904 sec
->spec
.namelen
= strlen (sec
->spec
.name
);
905 sec
->spec
.prefixlen
= strcspn (sec
->spec
.name
, "?*[");
906 sec
->spec
.suffixlen
= rstrcspn (sec
->spec
.name
+ sec
->spec
.prefixlen
,
910 sec
->spec
.namelen
= sec
->spec
.prefixlen
= sec
->spec
.suffixlen
= 0;
913 insert_prefix_tree (ptr
);
916 /* Match all sections from FILE against the global prefix tree,
917 and record them into each wild statement that has a match. */
920 resolve_wild_sections (lang_input_statement_type
*file
)
924 if (file
->flags
.just_syms
)
927 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
929 const char *sname
= bfd_section_name (s
);
931 struct prefixtree
*t
= ptroot
;
932 //printf (" YYY consider %s of %s\n", sname, file->the_bfd->filename);
937 struct wild_stmt_list
*sl
;
938 for (sl
= t
->stmt
; sl
; sl
= sl
->next
)
940 walk_wild_section_match (sl
->stmt
, file
, s
);
941 //printf (" ZZZ maybe place into %p\n", sl->stmt);
947 t
= get_prefix_tree (&t
->child
, c
, false);
953 /* Match all sections from all input files against the global prefix tree. */
958 LANG_FOR_EACH_INPUT_STATEMENT (f
)
960 //printf("XXX %s\n", f->filename);
961 if (f
->the_bfd
== NULL
962 || !bfd_check_format (f
->the_bfd
, bfd_archive
))
963 resolve_wild_sections (f
);
968 /* This is an archive file. We must map each member of the
969 archive separately. */
970 member
= bfd_openr_next_archived_file (f
->the_bfd
, NULL
);
971 while (member
!= NULL
)
973 /* When lookup_name is called, it will call the add_symbols
974 entry point for the archive. For each element of the
975 archive which is included, BFD will call ldlang_add_file,
976 which will set the usrdata field of the member to the
977 lang_input_statement. */
978 if (bfd_usrdata (member
) != NULL
)
979 resolve_wild_sections (bfd_usrdata (member
));
981 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
987 /* For each input section that matches wild statement S calls
988 CALLBACK with DATA. */
991 walk_wild (lang_wild_statement_type
*s
, callback_t callback
, void *data
)
993 lang_statement_union_type
*l
;
995 for (l
= s
->matching_sections
.head
; l
; l
= l
->header
.next
)
997 (*callback
) (s
, l
->input_matcher
.pattern
, l
->input_matcher
.section
,
998 l
->input_matcher
.input_stmt
, data
);
1002 /* lang_for_each_statement walks the parse tree and calls the provided
1003 function for each node, except those inside output section statements
1004 with constraint set to -1. */
1007 lang_for_each_statement_worker (void (*func
) (lang_statement_union_type
*),
1008 lang_statement_union_type
*s
)
1010 for (; s
!= NULL
; s
= s
->header
.next
)
1014 switch (s
->header
.type
)
1016 case lang_constructors_statement_enum
:
1017 lang_for_each_statement_worker (func
, constructor_list
.head
);
1019 case lang_output_section_statement_enum
:
1020 if (s
->output_section_statement
.constraint
!= -1)
1021 lang_for_each_statement_worker
1022 (func
, s
->output_section_statement
.children
.head
);
1024 case lang_wild_statement_enum
:
1025 lang_for_each_statement_worker (func
,
1026 s
->wild_statement
.children
.head
);
1028 case lang_group_statement_enum
:
1029 lang_for_each_statement_worker (func
,
1030 s
->group_statement
.children
.head
);
1032 case lang_data_statement_enum
:
1033 case lang_reloc_statement_enum
:
1034 case lang_object_symbols_statement_enum
:
1035 case lang_output_statement_enum
:
1036 case lang_target_statement_enum
:
1037 case lang_input_section_enum
:
1038 case lang_input_statement_enum
:
1039 case lang_assignment_statement_enum
:
1040 case lang_padding_statement_enum
:
1041 case lang_address_statement_enum
:
1042 case lang_fill_statement_enum
:
1043 case lang_insert_statement_enum
:
1053 lang_for_each_statement (void (*func
) (lang_statement_union_type
*))
1055 lang_for_each_statement_worker (func
, statement_list
.head
);
1058 /*----------------------------------------------------------------------*/
1061 lang_list_init (lang_statement_list_type
*list
)
1064 list
->tail
= &list
->head
;
1068 lang_statement_append (lang_statement_list_type
*list
,
1072 *(list
->tail
) = element
;
1077 push_stat_ptr (lang_statement_list_type
*new_ptr
)
1079 if (stat_save_ptr
>= stat_save
+ sizeof (stat_save
) / sizeof (stat_save
[0]))
1081 *stat_save_ptr
++ = stat_ptr
;
1088 if (stat_save_ptr
<= stat_save
)
1090 stat_ptr
= *--stat_save_ptr
;
1093 /* Build a new statement node for the parse tree. */
1095 static lang_statement_union_type
*
1096 new_statement (enum statement_enum type
,
1098 lang_statement_list_type
*list
)
1100 lang_statement_union_type
*new_stmt
;
1102 new_stmt
= stat_alloc (size
);
1103 new_stmt
->header
.type
= type
;
1104 new_stmt
->header
.next
= NULL
;
1105 lang_statement_append (list
, new_stmt
, &new_stmt
->header
.next
);
1109 /* Build a new input file node for the language. There are several
1110 ways in which we treat an input file, eg, we only look at symbols,
1111 or prefix it with a -l etc.
1113 We can be supplied with requests for input files more than once;
1114 they may, for example be split over several lines like foo.o(.text)
1115 foo.o(.data) etc, so when asked for a file we check that we haven't
1116 got it already so we don't duplicate the bfd. */
1118 static lang_input_statement_type
*
1119 new_afile (const char *name
,
1120 lang_input_file_enum_type file_type
,
1122 const char *from_filename
)
1124 lang_input_statement_type
*p
;
1126 lang_has_input_file
= true;
1128 p
= new_stat (lang_input_statement
, stat_ptr
);
1129 memset (&p
->the_bfd
, 0,
1130 sizeof (*p
) - offsetof (lang_input_statement_type
, the_bfd
));
1131 p
->extra_search_path
= NULL
;
1133 p
->flags
.dynamic
= input_flags
.dynamic
;
1134 p
->flags
.add_DT_NEEDED_for_dynamic
= input_flags
.add_DT_NEEDED_for_dynamic
;
1135 p
->flags
.add_DT_NEEDED_for_regular
= input_flags
.add_DT_NEEDED_for_regular
;
1136 p
->flags
.whole_archive
= input_flags
.whole_archive
;
1137 p
->flags
.sysrooted
= input_flags
.sysrooted
;
1142 case lang_input_file_is_symbols_only_enum
:
1144 p
->local_sym_name
= name
;
1145 p
->flags
.real
= true;
1146 p
->flags
.just_syms
= true;
1148 case lang_input_file_is_fake_enum
:
1150 p
->local_sym_name
= name
;
1152 case lang_input_file_is_l_enum
:
1153 if (name
[0] == ':' && name
[1] != '\0')
1155 p
->filename
= name
+ 1;
1156 p
->flags
.full_name_provided
= true;
1160 p
->local_sym_name
= concat ("-l", name
, (const char *) NULL
);
1161 p
->flags
.maybe_archive
= true;
1162 p
->flags
.real
= true;
1163 p
->flags
.search_dirs
= true;
1165 case lang_input_file_is_marker_enum
:
1167 p
->local_sym_name
= name
;
1168 p
->flags
.search_dirs
= true;
1170 case lang_input_file_is_search_file_enum
:
1172 p
->local_sym_name
= name
;
1173 /* If name is a relative path, search the directory of the current linker
1175 if (from_filename
&& !IS_ABSOLUTE_PATH (name
))
1176 p
->extra_search_path
= ldirname (from_filename
);
1177 p
->flags
.real
= true;
1178 p
->flags
.search_dirs
= true;
1180 case lang_input_file_is_file_enum
:
1182 p
->local_sym_name
= name
;
1183 p
->flags
.real
= true;
1189 lang_statement_append (&input_file_chain
, p
, &p
->next_real_file
);
1193 lang_input_statement_type
*
1194 lang_add_input_file (const char *name
,
1195 lang_input_file_enum_type file_type
,
1199 && (*name
== '=' || startswith (name
, "$SYSROOT")))
1201 lang_input_statement_type
*ret
;
1202 char *sysrooted_name
1203 = concat (ld_sysroot
,
1204 name
+ (*name
== '=' ? 1 : strlen ("$SYSROOT")),
1205 (const char *) NULL
);
1207 /* We've now forcibly prepended the sysroot, making the input
1208 file independent of the context. Therefore, temporarily
1209 force a non-sysrooted context for this statement, so it won't
1210 get the sysroot prepended again when opened. (N.B. if it's a
1211 script, any child nodes with input files starting with "/"
1212 will be handled as "sysrooted" as they'll be found to be
1213 within the sysroot subdirectory.) */
1214 unsigned int outer_sysrooted
= input_flags
.sysrooted
;
1215 input_flags
.sysrooted
= 0;
1216 ret
= new_afile (sysrooted_name
, file_type
, target
, NULL
);
1217 input_flags
.sysrooted
= outer_sysrooted
;
1221 return new_afile (name
, file_type
, target
, current_input_file
);
1224 struct out_section_hash_entry
1226 struct bfd_hash_entry root
;
1227 lang_statement_union_type s
;
1230 /* The hash table. */
1232 static struct bfd_hash_table output_section_statement_table
;
1234 /* Support routines for the hash table used by lang_output_section_find,
1235 initialize the table, fill in an entry and remove the table. */
1237 static struct bfd_hash_entry
*
1238 output_section_statement_newfunc (struct bfd_hash_entry
*entry
,
1239 struct bfd_hash_table
*table
,
1242 lang_output_section_statement_type
**nextp
;
1243 struct out_section_hash_entry
*ret
;
1247 entry
= (struct bfd_hash_entry
*) bfd_hash_allocate (table
,
1253 entry
= bfd_hash_newfunc (entry
, table
, string
);
1257 ret
= (struct out_section_hash_entry
*) entry
;
1258 memset (&ret
->s
, 0, sizeof (ret
->s
));
1259 ret
->s
.header
.type
= lang_output_section_statement_enum
;
1260 ret
->s
.output_section_statement
.subsection_alignment
= NULL
;
1261 ret
->s
.output_section_statement
.section_alignment
= NULL
;
1262 ret
->s
.output_section_statement
.block_value
= 1;
1263 lang_list_init (&ret
->s
.output_section_statement
.children
);
1264 lang_statement_append (stat_ptr
, &ret
->s
, &ret
->s
.header
.next
);
1266 /* For every output section statement added to the list, except the
1267 first one, lang_os_list.tail points to the "next"
1268 field of the last element of the list. */
1269 if (lang_os_list
.head
!= NULL
)
1270 ret
->s
.output_section_statement
.prev
1271 = ((lang_output_section_statement_type
*)
1272 ((char *) lang_os_list
.tail
1273 - offsetof (lang_output_section_statement_type
, next
)));
1275 /* GCC's strict aliasing rules prevent us from just casting the
1276 address, so we store the pointer in a variable and cast that
1278 nextp
= &ret
->s
.output_section_statement
.next
;
1279 lang_statement_append (&lang_os_list
, &ret
->s
, nextp
);
1284 output_section_statement_table_init (void)
1286 if (!bfd_hash_table_init_n (&output_section_statement_table
,
1287 output_section_statement_newfunc
,
1288 sizeof (struct out_section_hash_entry
),
1290 einfo (_("%F%P: can not create hash table: %E\n"));
1294 output_section_statement_table_free (void)
1296 bfd_hash_table_free (&output_section_statement_table
);
1299 /* Build enough state so that the parser can build its tree. */
1304 obstack_begin (&stat_obstack
, 1000);
1305 obstack_init (&pt_obstack
);
1307 stat_ptr
= &statement_list
;
1309 output_section_statement_table_init ();
1311 lang_list_init (stat_ptr
);
1313 lang_list_init (&input_file_chain
);
1314 lang_list_init (&lang_os_list
);
1315 lang_list_init (&file_chain
);
1316 first_file
= lang_add_input_file (NULL
, lang_input_file_is_marker_enum
,
1318 abs_output_section
=
1319 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
, 0, 1);
1321 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
1323 asneeded_list_head
= NULL
;
1324 asneeded_list_tail
= &asneeded_list_head
;
1330 output_section_statement_table_free ();
1333 /*----------------------------------------------------------------------
1334 A region is an area of memory declared with the
1335 MEMORY { name:org=exp, len=exp ... }
1338 We maintain a list of all the regions here.
1340 If no regions are specified in the script, then the default is used
1341 which is created when looked up to be the entire data space.
1343 If create is true we are creating a region inside a MEMORY block.
1344 In this case it is probably an error to create a region that has
1345 already been created. If we are not inside a MEMORY block it is
1346 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
1347 and so we issue a warning.
1349 Each region has at least one name. The first name is either
1350 DEFAULT_MEMORY_REGION or the name given in the MEMORY block. You can add
1351 alias names to an existing region within a script with
1352 REGION_ALIAS (alias, region_name). Each name corresponds to at most one
1355 static lang_memory_region_type
*lang_memory_region_list
;
1356 static lang_memory_region_type
**lang_memory_region_list_tail
1357 = &lang_memory_region_list
;
1359 lang_memory_region_type
*
1360 lang_memory_region_lookup (const char *const name
, bool create
)
1362 lang_memory_region_name
*n
;
1363 lang_memory_region_type
*r
;
1364 lang_memory_region_type
*new_region
;
1366 /* NAME is NULL for LMA memspecs if no region was specified. */
1370 for (r
= lang_memory_region_list
; r
!= NULL
; r
= r
->next
)
1371 for (n
= &r
->name_list
; n
!= NULL
; n
= n
->next
)
1372 if (strcmp (n
->name
, name
) == 0)
1375 einfo (_("%P:%pS: warning: redeclaration of memory region `%s'\n"),
1380 if (!create
&& strcmp (name
, DEFAULT_MEMORY_REGION
))
1381 einfo (_("%P:%pS: warning: memory region `%s' not declared\n"),
1384 new_region
= stat_alloc (sizeof (lang_memory_region_type
));
1386 new_region
->name_list
.name
= xstrdup (name
);
1387 new_region
->name_list
.next
= NULL
;
1388 new_region
->next
= NULL
;
1389 new_region
->origin_exp
= NULL
;
1390 new_region
->origin
= 0;
1391 new_region
->length_exp
= NULL
;
1392 new_region
->length
= ~(bfd_size_type
) 0;
1393 new_region
->current
= 0;
1394 new_region
->last_os
= NULL
;
1395 new_region
->flags
= 0;
1396 new_region
->not_flags
= 0;
1397 new_region
->had_full_message
= false;
1399 *lang_memory_region_list_tail
= new_region
;
1400 lang_memory_region_list_tail
= &new_region
->next
;
1406 lang_memory_region_alias (const char *alias
, const char *region_name
)
1408 lang_memory_region_name
*n
;
1409 lang_memory_region_type
*r
;
1410 lang_memory_region_type
*region
;
1412 /* The default region must be unique. This ensures that it is not necessary
1413 to iterate through the name list if someone wants the check if a region is
1414 the default memory region. */
1415 if (strcmp (region_name
, DEFAULT_MEMORY_REGION
) == 0
1416 || strcmp (alias
, DEFAULT_MEMORY_REGION
) == 0)
1417 einfo (_("%F%P:%pS: error: alias for default memory region\n"), NULL
);
1419 /* Look for the target region and check if the alias is not already
1422 for (r
= lang_memory_region_list
; r
!= NULL
; r
= r
->next
)
1423 for (n
= &r
->name_list
; n
!= NULL
; n
= n
->next
)
1425 if (region
== NULL
&& strcmp (n
->name
, region_name
) == 0)
1427 if (strcmp (n
->name
, alias
) == 0)
1428 einfo (_("%F%P:%pS: error: redefinition of memory region "
1433 /* Check if the target region exists. */
1435 einfo (_("%F%P:%pS: error: memory region `%s' "
1436 "for alias `%s' does not exist\n"),
1437 NULL
, region_name
, alias
);
1439 /* Add alias to region name list. */
1440 n
= stat_alloc (sizeof (lang_memory_region_name
));
1441 n
->name
= xstrdup (alias
);
1442 n
->next
= region
->name_list
.next
;
1443 region
->name_list
.next
= n
;
1446 static lang_memory_region_type
*
1447 lang_memory_default (asection
*section
)
1449 lang_memory_region_type
*p
;
1451 flagword sec_flags
= section
->flags
;
1453 /* Override SEC_DATA to mean a writable section. */
1454 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
1455 sec_flags
|= SEC_DATA
;
1457 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
1459 if ((p
->flags
& sec_flags
) != 0
1460 && (p
->not_flags
& sec_flags
) == 0)
1465 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, false);
1468 /* Get the output section statement directly from the userdata. */
1470 lang_output_section_statement_type
*
1471 lang_output_section_get (const asection
*output_section
)
1473 return bfd_section_userdata (output_section
);
1476 /* Find or create an output_section_statement with the given NAME.
1477 If CONSTRAINT is non-zero match one with that constraint, otherwise
1478 match any non-negative constraint. If CREATE is 0 return NULL when
1479 no match exists. If CREATE is 1, create an output_section_statement
1480 when no match exists or if CONSTRAINT is SPECIAL. If CREATE is 2,
1481 always make a new output_section_statement. */
1483 lang_output_section_statement_type
*
1484 lang_output_section_statement_lookup (const char *name
,
1488 struct out_section_hash_entry
*entry
;
1490 entry
= ((struct out_section_hash_entry
*)
1491 bfd_hash_lookup (&output_section_statement_table
, name
,
1492 create
!= 0, false));
1496 einfo (_("%F%P: failed creating section `%s': %E\n"), name
);
1500 if (entry
->s
.output_section_statement
.name
!= NULL
)
1502 /* We have a section of this name, but it might not have the correct
1504 struct out_section_hash_entry
*last_ent
;
1506 name
= entry
->s
.output_section_statement
.name
;
1510 && !(create
&& constraint
== SPECIAL
)
1511 && (constraint
== entry
->s
.output_section_statement
.constraint
1513 && entry
->s
.output_section_statement
.constraint
>= 0)))
1514 return &entry
->s
.output_section_statement
;
1516 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1518 while (entry
!= NULL
1519 && name
== entry
->s
.output_section_statement
.name
);
1525 = ((struct out_section_hash_entry
*)
1526 output_section_statement_newfunc (NULL
,
1527 &output_section_statement_table
,
1531 einfo (_("%F%P: failed creating section `%s': %E\n"), name
);
1534 entry
->root
= last_ent
->root
;
1535 last_ent
->root
.next
= &entry
->root
;
1538 entry
->s
.output_section_statement
.name
= name
;
1539 entry
->s
.output_section_statement
.constraint
= constraint
;
1540 entry
->s
.output_section_statement
.dup_output
= (create
== 2
1541 || constraint
== SPECIAL
);
1542 return &entry
->s
.output_section_statement
;
1545 /* Find the next output_section_statement with the same name as OS.
1546 If CONSTRAINT is non-zero, find one with that constraint otherwise
1547 match any non-negative constraint. */
1549 lang_output_section_statement_type
*
1550 next_matching_output_section_statement (lang_output_section_statement_type
*os
,
1553 /* All output_section_statements are actually part of a
1554 struct out_section_hash_entry. */
1555 struct out_section_hash_entry
*entry
= (struct out_section_hash_entry
*)
1557 - offsetof (struct out_section_hash_entry
, s
.output_section_statement
));
1558 const char *name
= os
->name
;
1560 ASSERT (name
== entry
->root
.string
);
1563 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1565 || name
!= entry
->s
.output_section_statement
.name
)
1568 while (constraint
!= entry
->s
.output_section_statement
.constraint
1570 || entry
->s
.output_section_statement
.constraint
< 0));
1572 return &entry
->s
.output_section_statement
;
1575 /* A variant of lang_output_section_find used by place_orphan.
1576 Returns the output statement that should precede a new output
1577 statement for SEC. If an exact match is found on certain flags,
1580 lang_output_section_statement_type
*
1581 lang_output_section_find_by_flags (const asection
*sec
,
1583 lang_output_section_statement_type
**exact
,
1584 lang_match_sec_type_func match_type
)
1586 lang_output_section_statement_type
*first
, *look
, *found
;
1587 flagword look_flags
, differ
;
1589 /* We know the first statement on this list is *ABS*. May as well
1591 first
= (void *) lang_os_list
.head
;
1592 first
= first
->next
;
1594 /* First try for an exact match. */
1596 for (look
= first
; look
; look
= look
->next
)
1598 look_flags
= look
->flags
;
1599 if (look
->bfd_section
!= NULL
)
1601 look_flags
= look
->bfd_section
->flags
;
1602 if (match_type
&& !match_type (link_info
.output_bfd
,
1607 differ
= look_flags
^ sec_flags
;
1608 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
1609 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1619 if ((sec_flags
& SEC_CODE
) != 0
1620 && (sec_flags
& SEC_ALLOC
) != 0)
1622 /* Try for a rw code section. */
1623 for (look
= first
; look
; look
= look
->next
)
1625 look_flags
= look
->flags
;
1626 if (look
->bfd_section
!= NULL
)
1628 look_flags
= look
->bfd_section
->flags
;
1629 if (match_type
&& !match_type (link_info
.output_bfd
,
1634 differ
= look_flags
^ sec_flags
;
1635 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1636 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1640 else if ((sec_flags
& SEC_READONLY
) != 0
1641 && (sec_flags
& SEC_ALLOC
) != 0)
1643 /* .rodata can go after .text, .sdata2 after .rodata. */
1644 for (look
= first
; look
; look
= look
->next
)
1646 look_flags
= look
->flags
;
1647 if (look
->bfd_section
!= NULL
)
1649 look_flags
= look
->bfd_section
->flags
;
1650 if (match_type
&& !match_type (link_info
.output_bfd
,
1655 differ
= look_flags
^ sec_flags
;
1656 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1657 | SEC_READONLY
| SEC_SMALL_DATA
))
1658 || (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1660 && !(look_flags
& SEC_SMALL_DATA
)))
1664 else if ((sec_flags
& SEC_THREAD_LOCAL
) != 0
1665 && (sec_flags
& SEC_ALLOC
) != 0)
1667 /* .tdata can go after .data, .tbss after .tdata. Treat .tbss
1668 as if it were a loaded section, and don't use match_type. */
1669 bool seen_thread_local
= false;
1672 for (look
= first
; look
; look
= look
->next
)
1674 look_flags
= look
->flags
;
1675 if (look
->bfd_section
!= NULL
)
1676 look_flags
= look
->bfd_section
->flags
;
1678 differ
= look_flags
^ (sec_flags
| SEC_LOAD
| SEC_HAS_CONTENTS
);
1679 if (!(differ
& (SEC_THREAD_LOCAL
| SEC_ALLOC
)))
1681 /* .tdata and .tbss must be adjacent and in that order. */
1682 if (!(look_flags
& SEC_LOAD
)
1683 && (sec_flags
& SEC_LOAD
))
1684 /* ..so if we're at a .tbss section and we're placing
1685 a .tdata section stop looking and return the
1686 previous section. */
1689 seen_thread_local
= true;
1691 else if (seen_thread_local
)
1693 else if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
)))
1697 else if ((sec_flags
& SEC_SMALL_DATA
) != 0
1698 && (sec_flags
& SEC_ALLOC
) != 0)
1700 /* .sdata goes after .data, .sbss after .sdata. */
1701 for (look
= first
; look
; look
= look
->next
)
1703 look_flags
= look
->flags
;
1704 if (look
->bfd_section
!= NULL
)
1706 look_flags
= look
->bfd_section
->flags
;
1707 if (match_type
&& !match_type (link_info
.output_bfd
,
1712 differ
= look_flags
^ sec_flags
;
1713 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1714 | SEC_THREAD_LOCAL
))
1715 || ((look_flags
& SEC_SMALL_DATA
)
1716 && !(sec_flags
& SEC_HAS_CONTENTS
)))
1720 else if ((sec_flags
& SEC_HAS_CONTENTS
) != 0
1721 && (sec_flags
& SEC_ALLOC
) != 0)
1723 /* .data goes after .rodata. */
1724 for (look
= first
; look
; look
= look
->next
)
1726 look_flags
= look
->flags
;
1727 if (look
->bfd_section
!= NULL
)
1729 look_flags
= look
->bfd_section
->flags
;
1730 if (match_type
&& !match_type (link_info
.output_bfd
,
1735 differ
= look_flags
^ sec_flags
;
1736 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1737 | SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1741 else if ((sec_flags
& SEC_ALLOC
) != 0)
1743 /* .bss goes after any other alloc section. */
1744 for (look
= first
; look
; look
= look
->next
)
1746 look_flags
= look
->flags
;
1747 if (look
->bfd_section
!= NULL
)
1749 look_flags
= look
->bfd_section
->flags
;
1750 if (match_type
&& !match_type (link_info
.output_bfd
,
1755 differ
= look_flags
^ sec_flags
;
1756 if (!(differ
& SEC_ALLOC
))
1762 /* non-alloc go last. */
1763 for (look
= first
; look
; look
= look
->next
)
1765 look_flags
= look
->flags
;
1766 if (look
->bfd_section
!= NULL
)
1767 look_flags
= look
->bfd_section
->flags
;
1768 differ
= look_flags
^ sec_flags
;
1769 if (!(differ
& SEC_DEBUGGING
))
1775 if (found
|| !match_type
)
1778 return lang_output_section_find_by_flags (sec
, sec_flags
, NULL
, NULL
);
1781 /* Find the last output section before given output statement.
1782 Used by place_orphan. */
1785 output_prev_sec_find (lang_output_section_statement_type
*os
)
1787 lang_output_section_statement_type
*lookup
;
1789 for (lookup
= os
->prev
; lookup
!= NULL
; lookup
= lookup
->prev
)
1791 if (lookup
->constraint
< 0)
1794 if (lookup
->bfd_section
!= NULL
)
1795 return lookup
->bfd_section
;
1801 /* Look for a suitable place for a new output section statement. The
1802 idea is to skip over anything that might be inside a SECTIONS {}
1803 statement in a script, before we find another output section
1804 statement. Assignments to "dot" before an output section statement
1805 are assumed to belong to it, except in two cases; The first
1806 assignment to dot, and assignments before non-alloc sections.
1807 Otherwise we might put an orphan before . = . + SIZEOF_HEADERS or
1808 similar assignments that set the initial address, or we might
1809 insert non-alloc note sections among assignments setting end of
1812 static lang_statement_union_type
**
1813 insert_os_after (lang_statement_union_type
*after
)
1815 lang_statement_union_type
**where
;
1816 lang_statement_union_type
**assign
= NULL
;
1819 ignore_first
= after
== lang_os_list
.head
;
1821 for (where
= &after
->header
.next
;
1823 where
= &(*where
)->header
.next
)
1825 switch ((*where
)->header
.type
)
1827 case lang_assignment_statement_enum
:
1830 lang_assignment_statement_type
*ass
;
1832 ass
= &(*where
)->assignment_statement
;
1833 if (ass
->exp
->type
.node_class
!= etree_assert
1834 && ass
->exp
->assign
.dst
[0] == '.'
1835 && ass
->exp
->assign
.dst
[1] == 0)
1839 ignore_first
= false;
1843 case lang_wild_statement_enum
:
1844 case lang_input_section_enum
:
1845 case lang_object_symbols_statement_enum
:
1846 case lang_fill_statement_enum
:
1847 case lang_data_statement_enum
:
1848 case lang_reloc_statement_enum
:
1849 case lang_padding_statement_enum
:
1850 case lang_constructors_statement_enum
:
1852 ignore_first
= false;
1854 case lang_output_section_statement_enum
:
1857 asection
*s
= (*where
)->output_section_statement
.bfd_section
;
1860 || s
->map_head
.s
== NULL
1861 || (s
->flags
& SEC_ALLOC
) != 0)
1865 case lang_input_statement_enum
:
1866 case lang_address_statement_enum
:
1867 case lang_target_statement_enum
:
1868 case lang_output_statement_enum
:
1869 case lang_group_statement_enum
:
1870 case lang_insert_statement_enum
:
1872 case lang_input_matcher_enum
:
1881 lang_output_section_statement_type
*
1882 lang_insert_orphan (asection
*s
,
1883 const char *secname
,
1885 lang_output_section_statement_type
*after
,
1886 struct orphan_save
*place
,
1887 etree_type
*address
,
1888 lang_statement_list_type
*add_child
)
1890 lang_statement_list_type add
;
1891 lang_output_section_statement_type
*os
;
1892 lang_output_section_statement_type
**os_tail
;
1894 /* If we have found an appropriate place for the output section
1895 statements for this orphan, add them to our own private list,
1896 inserting them later into the global statement list. */
1899 lang_list_init (&add
);
1900 push_stat_ptr (&add
);
1903 if (bfd_link_relocatable (&link_info
)
1904 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0)
1905 address
= exp_intop (0);
1907 os_tail
= (lang_output_section_statement_type
**) lang_os_list
.tail
;
1908 os
= lang_enter_output_section_statement (
1909 secname
, address
, normal_section
, 0, NULL
, NULL
, NULL
, constraint
, 0);
1911 if (add_child
== NULL
)
1912 add_child
= &os
->children
;
1913 lang_add_section (add_child
, s
, NULL
, NULL
, os
);
1915 if (after
&& (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) != 0)
1917 const char *region
= (after
->region
1918 ? after
->region
->name_list
.name
1919 : DEFAULT_MEMORY_REGION
);
1920 const char *lma_region
= (after
->lma_region
1921 ? after
->lma_region
->name_list
.name
1923 lang_leave_output_section_statement (NULL
, region
, after
->phdrs
,
1927 lang_leave_output_section_statement (NULL
, DEFAULT_MEMORY_REGION
, NULL
,
1930 /* Restore the global list pointer. */
1934 if (after
!= NULL
&& os
->bfd_section
!= NULL
)
1936 asection
*snew
, *as
;
1937 bool place_after
= place
->stmt
== NULL
;
1938 bool insert_after
= true;
1940 snew
= os
->bfd_section
;
1942 /* Shuffle the bfd section list to make the output file look
1943 neater. This is really only cosmetic. */
1944 if (place
->section
== NULL
1945 && after
!= (void *) lang_os_list
.head
)
1947 asection
*bfd_section
= after
->bfd_section
;
1949 /* If the output statement hasn't been used to place any input
1950 sections (and thus doesn't have an output bfd_section),
1951 look for the closest prior output statement having an
1953 if (bfd_section
== NULL
)
1954 bfd_section
= output_prev_sec_find (after
);
1956 if (bfd_section
!= NULL
1957 && bfd_section
->owner
!= NULL
1958 && bfd_section
!= snew
)
1959 place
->section
= &bfd_section
->next
;
1962 if (place
->section
== NULL
)
1963 place
->section
= &link_info
.output_bfd
->sections
;
1965 as
= *place
->section
;
1969 /* Put the section at the end of the list. */
1971 /* Unlink the section. */
1972 bfd_section_list_remove (link_info
.output_bfd
, snew
);
1974 /* Now tack it back on in the right place. */
1975 bfd_section_list_append (link_info
.output_bfd
, snew
);
1977 else if ((bfd_get_flavour (link_info
.output_bfd
)
1978 == bfd_target_elf_flavour
)
1979 && (bfd_get_flavour (s
->owner
)
1980 == bfd_target_elf_flavour
)
1981 && ((elf_section_type (s
) == SHT_NOTE
1982 && (s
->flags
& SEC_LOAD
) != 0)
1983 || (elf_section_type (as
) == SHT_NOTE
1984 && (as
->flags
& SEC_LOAD
) != 0)))
1986 /* Make sure that output note sections are grouped and sorted
1987 by alignments when inserting a note section or insert a
1988 section after a note section, */
1990 /* A specific section after which the output note section
1991 should be placed. */
1992 asection
*after_sec
;
1993 /* True if we need to insert the orphan section after a
1994 specific section to maintain output note section order. */
1995 bool after_sec_note
= false;
1997 static asection
*first_orphan_note
= NULL
;
1999 /* Group and sort output note section by alignments in
2002 if (elf_section_type (s
) == SHT_NOTE
2003 && (s
->flags
& SEC_LOAD
) != 0)
2005 /* Search from the beginning for the last output note
2006 section with equal or larger alignments. NB: Don't
2007 place orphan note section after non-note sections. */
2009 first_orphan_note
= NULL
;
2010 for (sec
= link_info
.output_bfd
->sections
;
2012 && !bfd_is_abs_section (sec
));
2015 && elf_section_type (sec
) == SHT_NOTE
2016 && (sec
->flags
& SEC_LOAD
) != 0)
2018 if (!first_orphan_note
)
2019 first_orphan_note
= sec
;
2020 if (sec
->alignment_power
>= s
->alignment_power
)
2023 else if (first_orphan_note
)
2025 /* Stop if there is non-note section after the first
2026 orphan note section. */
2030 /* If this will be the first orphan note section, it can
2031 be placed at the default location. */
2032 after_sec_note
= first_orphan_note
!= NULL
;
2033 if (after_sec
== NULL
&& after_sec_note
)
2035 /* If all output note sections have smaller
2036 alignments, place the section before all
2037 output orphan note sections. */
2038 after_sec
= first_orphan_note
;
2039 insert_after
= false;
2042 else if (first_orphan_note
)
2044 /* Don't place non-note sections in the middle of orphan
2046 after_sec_note
= true;
2048 for (sec
= as
->next
;
2050 && !bfd_is_abs_section (sec
));
2052 if (elf_section_type (sec
) == SHT_NOTE
2053 && (sec
->flags
& SEC_LOAD
) != 0)
2061 /* Search forward to insert OS after AFTER_SEC output
2063 lang_output_section_statement_type
*stmt
, *next
;
2065 for (stmt
= after
; stmt
!= NULL
; stmt
= next
)
2070 if (stmt
->bfd_section
== after_sec
)
2080 /* If INSERT_AFTER is FALSE, place OS before
2081 AFTER_SEC output statement. */
2082 if (next
&& next
->bfd_section
== after_sec
)
2092 /* Search backward to insert OS after AFTER_SEC output
2095 for (stmt
= after
; stmt
!= NULL
; stmt
= stmt
->prev
)
2099 if (stmt
->bfd_section
== after_sec
)
2108 /* If INSERT_AFTER is FALSE, place OS before
2109 AFTER_SEC output statement. */
2110 if (stmt
->next
->bfd_section
== after_sec
)
2120 if (after_sec
== NULL
2121 || (insert_after
&& after_sec
->next
!= snew
)
2122 || (!insert_after
&& after_sec
->prev
!= snew
))
2124 /* Unlink the section. */
2125 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2127 /* Place SNEW after AFTER_SEC. If AFTER_SEC is NULL,
2132 bfd_section_list_insert_after (link_info
.output_bfd
,
2135 bfd_section_list_insert_before (link_info
.output_bfd
,
2139 bfd_section_list_prepend (link_info
.output_bfd
, snew
);
2142 else if (as
!= snew
&& as
->prev
!= snew
)
2144 /* Unlink the section. */
2145 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2147 /* Now tack it back on in the right place. */
2148 bfd_section_list_insert_before (link_info
.output_bfd
,
2152 else if (as
!= snew
&& as
->prev
!= snew
)
2154 /* Unlink the section. */
2155 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2157 /* Now tack it back on in the right place. */
2158 bfd_section_list_insert_before (link_info
.output_bfd
, as
, snew
);
2161 /* Save the end of this list. Further ophans of this type will
2162 follow the one we've just added. */
2163 place
->section
= &snew
->next
;
2165 /* The following is non-cosmetic. We try to put the output
2166 statements in some sort of reasonable order here, because they
2167 determine the final load addresses of the orphan sections.
2168 In addition, placing output statements in the wrong order may
2169 require extra segments. For instance, given a typical
2170 situation of all read-only sections placed in one segment and
2171 following that a segment containing all the read-write
2172 sections, we wouldn't want to place an orphan read/write
2173 section before or amongst the read-only ones. */
2174 if (add
.head
!= NULL
)
2176 lang_output_section_statement_type
*newly_added_os
;
2178 /* Place OS after AFTER if AFTER_NOTE is TRUE. */
2181 lang_statement_union_type
**where
;
2183 where
= insert_os_after ((lang_statement_union_type
*) after
);
2187 place
->os_tail
= &after
->next
;
2191 /* Put it after the last orphan statement we added. */
2192 *add
.tail
= *place
->stmt
;
2193 *place
->stmt
= add
.head
;
2196 /* Fix the global list pointer if we happened to tack our
2197 new list at the tail. */
2198 if (*stat_ptr
->tail
== add
.head
)
2199 stat_ptr
->tail
= add
.tail
;
2201 /* Save the end of this list. */
2202 place
->stmt
= add
.tail
;
2204 /* Do the same for the list of output section statements. */
2205 newly_added_os
= *os_tail
;
2207 newly_added_os
->prev
= (lang_output_section_statement_type
*)
2208 ((char *) place
->os_tail
2209 - offsetof (lang_output_section_statement_type
, next
));
2210 newly_added_os
->next
= *place
->os_tail
;
2211 if (newly_added_os
->next
!= NULL
)
2212 newly_added_os
->next
->prev
= newly_added_os
;
2213 *place
->os_tail
= newly_added_os
;
2214 place
->os_tail
= &newly_added_os
->next
;
2216 /* Fixing the global list pointer here is a little different.
2217 We added to the list in lang_enter_output_section_statement,
2218 trimmed off the new output_section_statment above when
2219 assigning *os_tail = NULL, but possibly added it back in
2220 the same place when assigning *place->os_tail. */
2221 if (*os_tail
== NULL
)
2222 lang_os_list
.tail
= (lang_statement_union_type
**) os_tail
;
2229 lang_print_asneeded (void)
2231 struct asneeded_minfo
*m
;
2233 if (asneeded_list_head
== NULL
)
2236 minfo (_("\nAs-needed library included to satisfy reference by file (symbol)\n\n"));
2238 for (m
= asneeded_list_head
; m
!= NULL
; m
= m
->next
)
2242 minfo ("%s", m
->soname
);
2243 len
= strlen (m
->soname
);
2250 print_spaces (30 - len
);
2253 minfo ("%pB ", m
->ref
);
2254 minfo ("(%pT)\n", m
->name
);
2259 lang_map_flags (flagword flag
)
2261 if (flag
& SEC_ALLOC
)
2264 if (flag
& SEC_CODE
)
2267 if (flag
& SEC_READONLY
)
2270 if (flag
& SEC_DATA
)
2273 if (flag
& SEC_LOAD
)
2280 lang_memory_region_type
*m
;
2281 bool dis_header_printed
= false;
2283 LANG_FOR_EACH_INPUT_STATEMENT (file
)
2287 if ((file
->the_bfd
->flags
& (BFD_LINKER_CREATED
| DYNAMIC
)) != 0
2288 || file
->flags
.just_syms
)
2291 if (config
.print_map_discarded
)
2292 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
2293 if ((s
->output_section
== NULL
2294 || s
->output_section
->owner
!= link_info
.output_bfd
)
2295 && (s
->flags
& (SEC_LINKER_CREATED
| SEC_KEEP
)) == 0)
2297 if (! dis_header_printed
)
2299 minfo (_("\nDiscarded input sections\n\n"));
2300 dis_header_printed
= true;
2303 print_input_section (s
, true);
2306 if (config
.print_map_discarded
&& ! dis_header_printed
)
2307 minfo (_("\nThere are no discarded input sections\n"));
2309 minfo (_("\nMemory Configuration\n\n"));
2310 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
2311 _("Name"), _("Origin"), _("Length"), _("Attributes"));
2313 for (m
= lang_memory_region_list
; m
!= NULL
; m
= m
->next
)
2315 fprintf (config
.map_file
, "%-16s", m
->name_list
.name
);
2318 bfd_sprintf_vma (link_info
.output_bfd
, buf
, m
->origin
);
2319 fprintf (config
.map_file
, " 0x%-16s", buf
);
2320 bfd_sprintf_vma (link_info
.output_bfd
, buf
, m
->length
);
2321 fprintf (config
.map_file
,
2322 " 0x%*s", m
->flags
|| m
->not_flags
? -17 : 0, buf
);
2324 lang_map_flags (m
->flags
);
2329 lang_map_flags (m
->not_flags
);
2335 minfo (_("\nLinker script and memory map\n\n"));
2337 if (!link_info
.reduce_memory_overheads
)
2339 obstack_begin (&map_obstack
, 1000);
2340 bfd_link_hash_traverse (link_info
.hash
, sort_def_symbol
, 0);
2342 expld
.phase
= lang_fixed_phase_enum
;
2343 lang_statement_iteration
++;
2344 print_statements ();
2346 ldemul_extra_map_file_text (link_info
.output_bfd
, &link_info
,
2351 sort_def_symbol (struct bfd_link_hash_entry
*hash_entry
,
2352 void *info ATTRIBUTE_UNUSED
)
2354 if ((hash_entry
->type
== bfd_link_hash_defined
2355 || hash_entry
->type
== bfd_link_hash_defweak
)
2356 && hash_entry
->u
.def
.section
->owner
!= link_info
.output_bfd
2357 && hash_entry
->u
.def
.section
->owner
!= NULL
)
2359 input_section_userdata_type
*ud
;
2360 struct map_symbol_def
*def
;
2362 ud
= bfd_section_userdata (hash_entry
->u
.def
.section
);
2365 ud
= stat_alloc (sizeof (*ud
));
2366 bfd_set_section_userdata (hash_entry
->u
.def
.section
, ud
);
2367 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
2368 ud
->map_symbol_def_count
= 0;
2370 else if (!ud
->map_symbol_def_tail
)
2371 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
2373 def
= (struct map_symbol_def
*) obstack_alloc (&map_obstack
, sizeof *def
);
2374 def
->entry
= hash_entry
;
2375 *(ud
->map_symbol_def_tail
) = def
;
2376 ud
->map_symbol_def_tail
= &def
->next
;
2377 ud
->map_symbol_def_count
++;
2382 /* Initialize an output section. */
2385 init_os (lang_output_section_statement_type
*s
, flagword flags
)
2387 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
2388 einfo (_("%F%P: illegal use of `%s' section\n"), DISCARD_SECTION_NAME
);
2391 s
->bfd_section
= bfd_get_section_by_name (link_info
.output_bfd
, s
->name
);
2392 if (s
->bfd_section
== NULL
)
2393 s
->bfd_section
= bfd_make_section_anyway_with_flags (link_info
.output_bfd
,
2395 if (s
->bfd_section
== NULL
)
2397 einfo (_("%F%P: output format %s cannot represent section"
2398 " called %s: %E\n"),
2399 link_info
.output_bfd
->xvec
->name
, s
->name
);
2401 s
->bfd_section
->output_section
= s
->bfd_section
;
2402 s
->bfd_section
->output_offset
= 0;
2404 /* Set the userdata of the output section to the output section
2405 statement to avoid lookup. */
2406 bfd_set_section_userdata (s
->bfd_section
, s
);
2408 /* If there is a base address, make sure that any sections it might
2409 mention are initialized. */
2410 if (s
->addr_tree
!= NULL
)
2411 exp_init_os (s
->addr_tree
);
2413 if (s
->load_base
!= NULL
)
2414 exp_init_os (s
->load_base
);
2416 /* If supplied an alignment, set it. */
2417 if (s
->section_alignment
!= NULL
)
2418 s
->bfd_section
->alignment_power
= exp_get_power (s
->section_alignment
,
2419 "section alignment");
2422 /* Make sure that all output sections mentioned in an expression are
2426 exp_init_os (etree_type
*exp
)
2428 switch (exp
->type
.node_class
)
2432 case etree_provided
:
2433 exp_init_os (exp
->assign
.src
);
2437 exp_init_os (exp
->binary
.lhs
);
2438 exp_init_os (exp
->binary
.rhs
);
2442 exp_init_os (exp
->trinary
.cond
);
2443 exp_init_os (exp
->trinary
.lhs
);
2444 exp_init_os (exp
->trinary
.rhs
);
2448 exp_init_os (exp
->assert_s
.child
);
2452 exp_init_os (exp
->unary
.child
);
2456 switch (exp
->type
.node_code
)
2461 lang_output_section_statement_type
*os
;
2463 os
= lang_output_section_find (exp
->name
.name
);
2464 if (os
!= NULL
&& os
->bfd_section
== NULL
)
2476 section_already_linked (bfd
*abfd
, asection
*sec
, void *data
)
2478 lang_input_statement_type
*entry
= (lang_input_statement_type
*) data
;
2480 /* If we are only reading symbols from this object, then we want to
2481 discard all sections. */
2482 if (entry
->flags
.just_syms
)
2484 bfd_link_just_syms (abfd
, sec
, &link_info
);
2488 /* Deal with SHF_EXCLUDE ELF sections. */
2489 if (!bfd_link_relocatable (&link_info
)
2490 && (abfd
->flags
& BFD_PLUGIN
) == 0
2491 && (sec
->flags
& (SEC_GROUP
| SEC_KEEP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
2492 sec
->output_section
= bfd_abs_section_ptr
;
2494 if (!(abfd
->flags
& DYNAMIC
))
2495 bfd_section_already_linked (abfd
, sec
, &link_info
);
2499 /* Returns true if SECTION is one we know will be discarded based on its
2500 section flags, otherwise returns false. */
2503 lang_discard_section_p (asection
*section
)
2506 flagword flags
= section
->flags
;
2508 /* Discard sections marked with SEC_EXCLUDE. */
2509 discard
= (flags
& SEC_EXCLUDE
) != 0;
2511 /* Discard the group descriptor sections when we're finally placing the
2512 sections from within the group. */
2513 if ((flags
& SEC_GROUP
) != 0
2514 && link_info
.resolve_section_groups
)
2517 /* Discard debugging sections if we are stripping debugging
2519 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
2520 && (flags
& SEC_DEBUGGING
) != 0)
2526 /* Return TRUE if SECTION is never going to be added to output statement
2527 OUTPUT. lang_add_section() definitely won't do anything with SECTION
2528 if this returns TRUE. It may do something (or not) if this returns FALSE.
2530 Can be used as early-out to filter matches. This may set
2531 output_section of SECTION, if it was unset, to the abs section in case
2532 we discover SECTION to be always discarded. This may also give
2533 warning messages. */
2536 wont_add_section_p (asection
*section
,
2537 lang_output_section_statement_type
*output
)
2541 /* Is this section one we know should be discarded? */
2542 discard
= lang_discard_section_p (section
);
2544 /* Discard input sections which are assigned to a section named
2545 DISCARD_SECTION_NAME. */
2546 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
2551 if (section
->output_section
== NULL
)
2553 /* This prevents future calls from assigning this section or
2554 warning about it again. */
2555 section
->output_section
= bfd_abs_section_ptr
;
2557 else if (bfd_is_abs_section (section
->output_section
))
2559 else if (link_info
.non_contiguous_regions_warnings
)
2560 einfo (_("%P:%pS: warning: --enable-non-contiguous-regions makes "
2561 "section `%pA' from `%pB' match /DISCARD/ clause.\n"),
2562 NULL
, section
, section
->owner
);
2567 if (section
->output_section
!= NULL
)
2569 if (!link_info
.non_contiguous_regions
)
2572 /* SECTION has already been handled in a special way
2573 (eg. LINK_ONCE): skip it. */
2574 if (bfd_is_abs_section (section
->output_section
))
2577 /* Already assigned to the same output section, do not process
2578 it again, to avoid creating loops between duplicate sections
2580 if (section
->output_section
== output
->bfd_section
)
2583 if (link_info
.non_contiguous_regions_warnings
&& output
->bfd_section
)
2584 einfo (_("%P:%pS: warning: --enable-non-contiguous-regions may "
2585 "change behaviour for section `%pA' from `%pB' (assigned to "
2586 "%pA, but additional match: %pA)\n"),
2587 NULL
, section
, section
->owner
, section
->output_section
,
2588 output
->bfd_section
);
2590 /* SECTION has already been assigned to an output section, but
2591 the user allows it to be mapped to another one in case it
2592 overflows. We'll later update the actual output section in
2593 size_input_section as appropriate. */
2599 /* The wild routines.
2601 These expand statements like *(.text) and foo.o to a list of
2602 explicit actions, like foo.o(.text), bar.o(.text) and
2603 foo.o(.text, .data). */
2605 /* Add SECTION to the output section OUTPUT. Do this by creating a
2606 lang_input_section statement which is placed at PTR. */
2609 lang_add_section (lang_statement_list_type
*ptr
,
2611 struct wildcard_list
*pattern
,
2612 struct flag_info
*sflag_info
,
2613 lang_output_section_statement_type
*output
)
2615 flagword flags
= section
->flags
;
2617 lang_input_section_type
*new_section
;
2618 bfd
*abfd
= link_info
.output_bfd
;
2620 if (wont_add_section_p (section
, output
))
2627 keep
= bfd_lookup_section_flags (&link_info
, sflag_info
, section
);
2632 /* We don't copy the SEC_NEVER_LOAD flag from an input section
2633 to an output section, because we want to be able to include a
2634 SEC_NEVER_LOAD section in the middle of an otherwise loaded
2635 section (I don't know why we want to do this, but we do).
2636 build_link_order in ldwrite.c handles this case by turning
2637 the embedded SEC_NEVER_LOAD section into a fill. */
2638 flags
&= ~ SEC_NEVER_LOAD
;
2640 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
2641 already been processed. One reason to do this is that on pe
2642 format targets, .text$foo sections go into .text and it's odd
2643 to see .text with SEC_LINK_ONCE set. */
2644 if ((flags
& (SEC_LINK_ONCE
| SEC_GROUP
)) == (SEC_LINK_ONCE
| SEC_GROUP
))
2646 if (link_info
.resolve_section_groups
)
2647 flags
&= ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
);
2649 flags
&= ~(SEC_LINK_DUPLICATES
| SEC_RELOC
);
2651 else if (!bfd_link_relocatable (&link_info
))
2652 flags
&= ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
);
2654 switch (output
->sectype
)
2656 case normal_section
:
2657 case overlay_section
:
2658 case first_overlay_section
:
2661 case noalloc_section
:
2662 flags
&= ~SEC_ALLOC
;
2664 case typed_readonly_section
:
2665 case readonly_section
:
2666 flags
|= SEC_READONLY
;
2668 case noload_section
:
2670 flags
|= SEC_NEVER_LOAD
;
2671 /* Unfortunately GNU ld has managed to evolve two different
2672 meanings to NOLOAD in scripts. ELF gets a .bss style noload,
2673 alloc, no contents section. All others get a noload, noalloc
2675 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
)
2676 flags
&= ~SEC_HAS_CONTENTS
;
2678 flags
&= ~SEC_ALLOC
;
2682 if (output
->bfd_section
== NULL
)
2683 init_os (output
, flags
);
2685 /* If SEC_READONLY is not set in the input section, then clear
2686 it from the output section. */
2687 output
->bfd_section
->flags
&= flags
| ~SEC_READONLY
;
2689 if (output
->bfd_section
->linker_has_input
)
2691 /* Only set SEC_READONLY flag on the first input section. */
2692 flags
&= ~ SEC_READONLY
;
2694 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
2695 if ((output
->bfd_section
->flags
& (SEC_MERGE
| SEC_STRINGS
))
2696 != (flags
& (SEC_MERGE
| SEC_STRINGS
))
2697 || ((flags
& SEC_MERGE
) != 0
2698 && output
->bfd_section
->entsize
!= section
->entsize
))
2700 output
->bfd_section
->flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2701 flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2704 output
->bfd_section
->flags
|= flags
;
2706 if (!output
->bfd_section
->linker_has_input
)
2708 output
->bfd_section
->linker_has_input
= 1;
2709 /* This must happen after flags have been updated. The output
2710 section may have been created before we saw its first input
2711 section, eg. for a data statement. */
2712 bfd_init_private_section_data (section
->owner
, section
,
2713 link_info
.output_bfd
,
2714 output
->bfd_section
,
2716 if ((flags
& SEC_MERGE
) != 0)
2717 output
->bfd_section
->entsize
= section
->entsize
;
2720 if ((flags
& SEC_TIC54X_BLOCK
) != 0
2721 && bfd_get_arch (section
->owner
) == bfd_arch_tic54x
)
2723 /* FIXME: This value should really be obtained from the bfd... */
2724 output
->block_value
= 128;
2727 /* When a .ctors section is placed in .init_array it must be copied
2728 in reverse order. Similarly for .dtors. Set that up. */
2729 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
2730 && ((startswith (section
->name
, ".ctors")
2731 && strcmp (output
->bfd_section
->name
, ".init_array") == 0)
2732 || (startswith (section
->name
, ".dtors")
2733 && strcmp (output
->bfd_section
->name
, ".fini_array") == 0))
2734 && (section
->name
[6] == 0 || section
->name
[6] == '.'))
2735 section
->flags
|= SEC_ELF_REVERSE_COPY
;
2737 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
2738 output
->bfd_section
->alignment_power
= section
->alignment_power
;
2740 section
->output_section
= output
->bfd_section
;
2742 if (!map_head_is_link_order
)
2744 asection
*s
= output
->bfd_section
->map_tail
.s
;
2745 output
->bfd_section
->map_tail
.s
= section
;
2746 section
->map_head
.s
= NULL
;
2747 section
->map_tail
.s
= s
;
2749 s
->map_head
.s
= section
;
2751 output
->bfd_section
->map_head
.s
= section
;
2754 /* Add a section reference to the list. */
2755 new_section
= new_stat (lang_input_section
, ptr
);
2756 new_section
->section
= section
;
2757 new_section
->pattern
= pattern
;
2760 /* Expand a wild statement for a particular FILE. SECTION may be
2761 NULL, in which case it is a wild card. This assumes that the
2762 wild statement doesn't need any sorting (of filenames or sections). */
2765 output_section_callback_nosort (lang_wild_statement_type
*ptr
,
2766 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
2768 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
2771 lang_output_section_statement_type
*os
;
2773 os
= (lang_output_section_statement_type
*) output
;
2775 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2776 if (unique_section_p (section
, os
))
2779 lang_add_section (&ptr
->children
, section
, ptr
->section_list
,
2780 ptr
->section_flag_list
, os
);
2783 /* Check if all sections in a wild statement for a particular FILE
2787 check_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
2788 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
2790 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
2793 lang_output_section_statement_type
*os
;
2795 os
= (lang_output_section_statement_type
*) output
;
2797 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2798 if (unique_section_p (section
, os
))
2801 if (section
->output_section
== NULL
&& (section
->flags
& SEC_READONLY
) == 0)
2802 os
->all_input_readonly
= false;
2805 /* This is passed a file name which must have been seen already and
2806 added to the statement tree. We will see if it has been opened
2807 already and had its symbols read. If not then we'll read it. */
2809 static lang_input_statement_type
*
2810 lookup_name (const char *name
)
2812 lang_input_statement_type
*search
;
2814 for (search
= (void *) input_file_chain
.head
;
2816 search
= search
->next_real_file
)
2818 /* Use the local_sym_name as the name of the file that has
2819 already been loaded as filename might have been transformed
2820 via the search directory lookup mechanism. */
2821 const char *filename
= search
->local_sym_name
;
2823 if (filename
!= NULL
2824 && filename_cmp (filename
, name
) == 0)
2830 /* Arrange to splice the input statement added by new_afile into
2831 statement_list after the current input_file_chain tail.
2832 We know input_file_chain is not an empty list, and that
2833 lookup_name was called via open_input_bfds. Later calls to
2834 lookup_name should always match an existing input_statement. */
2835 lang_statement_union_type
**tail
= stat_ptr
->tail
;
2836 lang_statement_union_type
**after
2837 = (void *) ((char *) input_file_chain
.tail
2838 - offsetof (lang_input_statement_type
, next_real_file
)
2839 + offsetof (lang_input_statement_type
, header
.next
));
2840 lang_statement_union_type
*rest
= *after
;
2841 stat_ptr
->tail
= after
;
2842 search
= new_afile (name
, lang_input_file_is_search_file_enum
,
2843 default_target
, NULL
);
2844 *stat_ptr
->tail
= rest
;
2846 stat_ptr
->tail
= tail
;
2849 /* If we have already added this file, or this file is not real
2850 don't add this file. */
2851 if (search
->flags
.loaded
|| !search
->flags
.real
)
2854 if (!load_symbols (search
, NULL
))
2860 /* Save LIST as a list of libraries whose symbols should not be exported. */
2865 struct excluded_lib
*next
;
2867 static struct excluded_lib
*excluded_libs
;
2870 add_excluded_libs (const char *list
)
2872 const char *p
= list
, *end
;
2876 struct excluded_lib
*entry
;
2877 end
= strpbrk (p
, ",:");
2879 end
= p
+ strlen (p
);
2880 entry
= (struct excluded_lib
*) xmalloc (sizeof (*entry
));
2881 entry
->next
= excluded_libs
;
2882 entry
->name
= (char *) xmalloc (end
- p
+ 1);
2883 memcpy (entry
->name
, p
, end
- p
);
2884 entry
->name
[end
- p
] = '\0';
2885 excluded_libs
= entry
;
2893 check_excluded_libs (bfd
*abfd
)
2895 struct excluded_lib
*lib
= excluded_libs
;
2899 int len
= strlen (lib
->name
);
2900 const char *filename
= lbasename (bfd_get_filename (abfd
));
2902 if (strcmp (lib
->name
, "ALL") == 0)
2904 abfd
->no_export
= true;
2908 if (filename_ncmp (lib
->name
, filename
, len
) == 0
2909 && (filename
[len
] == '\0'
2910 || (filename
[len
] == '.' && filename
[len
+ 1] == 'a'
2911 && filename
[len
+ 2] == '\0')))
2913 abfd
->no_export
= true;
2921 /* Get the symbols for an input file. */
2924 load_symbols (lang_input_statement_type
*entry
,
2925 lang_statement_list_type
*place
)
2929 if (entry
->flags
.loaded
)
2932 ldfile_open_file (entry
);
2934 /* Do not process further if the file was missing. */
2935 if (entry
->flags
.missing_file
)
2938 if (trace_files
|| verbose
)
2939 info_msg ("%pI\n", entry
);
2941 if (!bfd_check_format (entry
->the_bfd
, bfd_archive
)
2942 && !bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
2945 struct lang_input_statement_flags save_flags
;
2948 err
= bfd_get_error ();
2950 /* See if the emulation has some special knowledge. */
2951 if (ldemul_unrecognized_file (entry
))
2953 if (err
== bfd_error_file_ambiguously_recognized
)
2958 if (err
== bfd_error_file_ambiguously_recognized
)
2962 einfo (_("%P: %pB: file not recognized: %E;"
2963 " matching formats:"), entry
->the_bfd
);
2964 for (p
= matching
; *p
!= NULL
; p
++)
2969 else if (err
!= bfd_error_file_not_recognized
2971 einfo (_("%F%P: %pB: file not recognized: %E\n"), entry
->the_bfd
);
2973 bfd_close (entry
->the_bfd
);
2974 entry
->the_bfd
= NULL
;
2976 /* Try to interpret the file as a linker script. */
2977 save_flags
= input_flags
;
2978 ldfile_open_command_file (entry
->filename
);
2980 push_stat_ptr (place
);
2981 input_flags
.add_DT_NEEDED_for_regular
2982 = entry
->flags
.add_DT_NEEDED_for_regular
;
2983 input_flags
.add_DT_NEEDED_for_dynamic
2984 = entry
->flags
.add_DT_NEEDED_for_dynamic
;
2985 input_flags
.whole_archive
= entry
->flags
.whole_archive
;
2986 input_flags
.dynamic
= entry
->flags
.dynamic
;
2988 ldfile_assumed_script
= true;
2989 parser_input
= input_script
;
2990 current_input_file
= entry
->filename
;
2992 current_input_file
= NULL
;
2993 ldfile_assumed_script
= false;
2995 /* missing_file is sticky. sysrooted will already have been
2996 restored when seeing EOF in yyparse, but no harm to restore
2998 save_flags
.missing_file
|= input_flags
.missing_file
;
2999 input_flags
= save_flags
;
3003 entry
->flags
.loaded
= true;
3008 if (ldemul_recognized_file (entry
))
3011 /* We don't call ldlang_add_file for an archive. Instead, the
3012 add_symbols entry point will call ldlang_add_file, via the
3013 add_archive_element callback, for each element of the archive
3015 switch (bfd_get_format (entry
->the_bfd
))
3021 if (!entry
->flags
.reload
)
3022 ldlang_add_file (entry
);
3026 check_excluded_libs (entry
->the_bfd
);
3028 bfd_set_usrdata (entry
->the_bfd
, entry
);
3029 if (entry
->flags
.whole_archive
)
3037 member
= bfd_openr_next_archived_file (entry
->the_bfd
, member
);
3042 if (!bfd_check_format (member
, bfd_object
))
3044 einfo (_("%F%P: %pB: member %pB in archive is not an object\n"),
3045 entry
->the_bfd
, member
);
3050 if (!(*link_info
.callbacks
3051 ->add_archive_element
) (&link_info
, member
,
3052 "--whole-archive", &subsbfd
))
3055 /* Potentially, the add_archive_element hook may have set a
3056 substitute BFD for us. */
3057 if (!bfd_link_add_symbols (subsbfd
, &link_info
))
3059 einfo (_("%F%P: %pB: error adding symbols: %E\n"), member
);
3064 entry
->flags
.loaded
= loaded
;
3070 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
3071 entry
->flags
.loaded
= true;
3073 einfo (_("%F%P: %pB: error adding symbols: %E\n"), entry
->the_bfd
);
3075 return entry
->flags
.loaded
;
3078 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
3079 may be NULL, indicating that it is a wildcard. Separate
3080 lang_input_section statements are created for each part of the
3081 expansion; they are added after the wild statement S. OUTPUT is
3082 the output section. */
3085 wild (lang_wild_statement_type
*s
,
3086 const char *target ATTRIBUTE_UNUSED
,
3087 lang_output_section_statement_type
*output
)
3089 struct wildcard_list
*sec
;
3091 if (s
->filenames_sorted
|| s
->any_specs_sorted
)
3093 lang_section_bst_type
*tree
;
3095 walk_wild (s
, output_section_callback_sort
, output
);
3100 output_section_callback_tree_to_list (s
, tree
, output
);
3102 s
->rightmost
= &s
->tree
;
3106 walk_wild (s
, output_section_callback_nosort
, output
);
3108 if (default_common_section
== NULL
)
3109 for (sec
= s
->section_list
; sec
!= NULL
; sec
= sec
->next
)
3110 if (sec
->spec
.name
!= NULL
&& strcmp (sec
->spec
.name
, "COMMON") == 0)
3112 /* Remember the section that common is going to in case we
3113 later get something which doesn't know where to put it. */
3114 default_common_section
= output
;
3119 /* Return TRUE iff target is the sought target. */
3122 get_target (const bfd_target
*target
, void *data
)
3124 const char *sought
= (const char *) data
;
3126 return strcmp (target
->name
, sought
) == 0;
3129 /* Like strcpy() but convert to lower case as well. */
3132 stricpy (char *dest
, const char *src
)
3136 while ((c
= *src
++) != 0)
3137 *dest
++ = TOLOWER (c
);
3142 /* Remove the first occurrence of needle (if any) in haystack
3146 strcut (char *haystack
, const char *needle
)
3148 haystack
= strstr (haystack
, needle
);
3154 for (src
= haystack
+ strlen (needle
); *src
;)
3155 *haystack
++ = *src
++;
3161 /* Compare two target format name strings.
3162 Return a value indicating how "similar" they are. */
3165 name_compare (const char *first
, const char *second
)
3171 copy1
= (char *) xmalloc (strlen (first
) + 1);
3172 copy2
= (char *) xmalloc (strlen (second
) + 1);
3174 /* Convert the names to lower case. */
3175 stricpy (copy1
, first
);
3176 stricpy (copy2
, second
);
3178 /* Remove size and endian strings from the name. */
3179 strcut (copy1
, "big");
3180 strcut (copy1
, "little");
3181 strcut (copy2
, "big");
3182 strcut (copy2
, "little");
3184 /* Return a value based on how many characters match,
3185 starting from the beginning. If both strings are
3186 the same then return 10 * their length. */
3187 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
3188 if (copy1
[result
] == 0)
3200 /* Set by closest_target_match() below. */
3201 static const bfd_target
*winner
;
3203 /* Scan all the valid bfd targets looking for one that has the endianness
3204 requirement that was specified on the command line, and is the nearest
3205 match to the original output target. */
3208 closest_target_match (const bfd_target
*target
, void *data
)
3210 const bfd_target
*original
= (const bfd_target
*) data
;
3212 if (command_line
.endian
== ENDIAN_BIG
3213 && target
->byteorder
!= BFD_ENDIAN_BIG
)
3216 if (command_line
.endian
== ENDIAN_LITTLE
3217 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
3220 /* Must be the same flavour. */
3221 if (target
->flavour
!= original
->flavour
)
3224 /* Ignore generic big and little endian elf vectors. */
3225 if (strcmp (target
->name
, "elf32-big") == 0
3226 || strcmp (target
->name
, "elf64-big") == 0
3227 || strcmp (target
->name
, "elf32-little") == 0
3228 || strcmp (target
->name
, "elf64-little") == 0)
3231 /* If we have not found a potential winner yet, then record this one. */
3238 /* Oh dear, we now have two potential candidates for a successful match.
3239 Compare their names and choose the better one. */
3240 if (name_compare (target
->name
, original
->name
)
3241 > name_compare (winner
->name
, original
->name
))
3244 /* Keep on searching until wqe have checked them all. */
3248 /* Return the BFD target format of the first input file. */
3251 get_first_input_target (void)
3253 const char *target
= NULL
;
3255 LANG_FOR_EACH_INPUT_STATEMENT (s
)
3257 if (s
->header
.type
== lang_input_statement_enum
3260 ldfile_open_file (s
);
3262 if (s
->the_bfd
!= NULL
3263 && bfd_check_format (s
->the_bfd
, bfd_object
))
3265 target
= bfd_get_target (s
->the_bfd
);
3277 lang_get_output_target (void)
3281 /* Has the user told us which output format to use? */
3282 if (output_target
!= NULL
)
3283 return output_target
;
3285 /* No - has the current target been set to something other than
3287 if (current_target
!= default_target
&& current_target
!= NULL
)
3288 return current_target
;
3290 /* No - can we determine the format of the first input file? */
3291 target
= get_first_input_target ();
3295 /* Failed - use the default output target. */
3296 return default_target
;
3299 /* Open the output file. */
3302 open_output (const char *name
)
3304 lang_input_statement_type
*f
;
3305 char *out
= lrealpath (name
);
3307 for (f
= (void *) input_file_chain
.head
;
3309 f
= f
->next_real_file
)
3312 char *in
= lrealpath (f
->local_sym_name
);
3313 if (filename_cmp (in
, out
) == 0)
3314 einfo (_("%F%P: input file '%s' is the same as output file\n"),
3320 output_target
= lang_get_output_target ();
3322 /* Has the user requested a particular endianness on the command
3324 if (command_line
.endian
!= ENDIAN_UNSET
)
3326 /* Get the chosen target. */
3327 const bfd_target
*target
3328 = bfd_iterate_over_targets (get_target
, (void *) output_target
);
3330 /* If the target is not supported, we cannot do anything. */
3333 enum bfd_endian desired_endian
;
3335 if (command_line
.endian
== ENDIAN_BIG
)
3336 desired_endian
= BFD_ENDIAN_BIG
;
3338 desired_endian
= BFD_ENDIAN_LITTLE
;
3340 /* See if the target has the wrong endianness. This should
3341 not happen if the linker script has provided big and
3342 little endian alternatives, but some scrips don't do
3344 if (target
->byteorder
!= desired_endian
)
3346 /* If it does, then see if the target provides
3347 an alternative with the correct endianness. */
3348 if (target
->alternative_target
!= NULL
3349 && (target
->alternative_target
->byteorder
== desired_endian
))
3350 output_target
= target
->alternative_target
->name
;
3353 /* Try to find a target as similar as possible to
3354 the default target, but which has the desired
3355 endian characteristic. */
3356 bfd_iterate_over_targets (closest_target_match
,
3359 /* Oh dear - we could not find any targets that
3360 satisfy our requirements. */
3362 einfo (_("%P: warning: could not find any targets"
3363 " that match endianness requirement\n"));
3365 output_target
= winner
->name
;
3371 link_info
.output_bfd
= bfd_openw (name
, output_target
);
3373 if (link_info
.output_bfd
== NULL
)
3375 if (bfd_get_error () == bfd_error_invalid_target
)
3376 einfo (_("%F%P: target %s not found\n"), output_target
);
3378 einfo (_("%F%P: cannot open output file %s: %E\n"), name
);
3381 delete_output_file_on_failure
= true;
3383 if (!bfd_set_format (link_info
.output_bfd
, bfd_object
))
3384 einfo (_("%F%P: %s: can not make object file: %E\n"), name
);
3385 if (!bfd_set_arch_mach (link_info
.output_bfd
,
3386 ldfile_output_architecture
,
3387 ldfile_output_machine
))
3388 einfo (_("%F%P: %s: can not set architecture: %E\n"), name
);
3390 link_info
.hash
= bfd_link_hash_table_create (link_info
.output_bfd
);
3391 if (link_info
.hash
== NULL
)
3392 einfo (_("%F%P: can not create hash table: %E\n"));
3394 bfd_set_gp_size (link_info
.output_bfd
, g_switch_value
);
3398 ldlang_open_output (lang_statement_union_type
*statement
)
3400 switch (statement
->header
.type
)
3402 case lang_output_statement_enum
:
3403 ASSERT (link_info
.output_bfd
== NULL
);
3404 open_output (statement
->output_statement
.name
);
3405 ldemul_set_output_arch ();
3406 if (config
.magic_demand_paged
3407 && !bfd_link_relocatable (&link_info
))
3408 link_info
.output_bfd
->flags
|= D_PAGED
;
3410 link_info
.output_bfd
->flags
&= ~D_PAGED
;
3411 if (config
.text_read_only
)
3412 link_info
.output_bfd
->flags
|= WP_TEXT
;
3414 link_info
.output_bfd
->flags
&= ~WP_TEXT
;
3415 if (link_info
.traditional_format
)
3416 link_info
.output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
3418 link_info
.output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
3421 case lang_target_statement_enum
:
3422 current_target
= statement
->target_statement
.target
;
3430 init_opb (asection
*s
)
3435 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
3437 && (s
->flags
& SEC_ELF_OCTETS
) != 0)
3440 x
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3441 ldfile_output_machine
);
3443 while ((x
& 1) == 0)
3451 /* Open all the input files. */
3455 OPEN_BFD_NORMAL
= 0,
3459 #if BFD_SUPPORTS_PLUGINS
3460 static lang_input_statement_type
*plugin_insert
= NULL
;
3461 static struct bfd_link_hash_entry
*plugin_undefs
= NULL
;
3465 open_input_bfds (lang_statement_union_type
*s
, enum open_bfd_mode mode
)
3467 for (; s
!= NULL
; s
= s
->header
.next
)
3469 switch (s
->header
.type
)
3471 case lang_constructors_statement_enum
:
3472 open_input_bfds (constructor_list
.head
, mode
);
3474 case lang_output_section_statement_enum
:
3475 open_input_bfds (s
->output_section_statement
.children
.head
, mode
);
3477 case lang_wild_statement_enum
:
3478 /* Maybe we should load the file's symbols. */
3479 if ((mode
& OPEN_BFD_RESCAN
) == 0
3480 && s
->wild_statement
.filename
3481 && !wildcardp (s
->wild_statement
.filename
)
3482 && !archive_path (s
->wild_statement
.filename
))
3483 lookup_name (s
->wild_statement
.filename
);
3484 open_input_bfds (s
->wild_statement
.children
.head
, mode
);
3486 case lang_group_statement_enum
:
3488 struct bfd_link_hash_entry
*undefs
;
3489 #if BFD_SUPPORTS_PLUGINS
3490 lang_input_statement_type
*plugin_insert_save
;
3493 /* We must continually search the entries in the group
3494 until no new symbols are added to the list of undefined
3499 #if BFD_SUPPORTS_PLUGINS
3500 plugin_insert_save
= plugin_insert
;
3502 undefs
= link_info
.hash
->undefs_tail
;
3503 open_input_bfds (s
->group_statement
.children
.head
,
3504 mode
| OPEN_BFD_FORCE
);
3506 while (undefs
!= link_info
.hash
->undefs_tail
3507 #if BFD_SUPPORTS_PLUGINS
3508 /* Objects inserted by a plugin, which are loaded
3509 before we hit this loop, may have added new
3511 || (plugin_insert
!= plugin_insert_save
&& plugin_undefs
)
3516 case lang_target_statement_enum
:
3517 current_target
= s
->target_statement
.target
;
3519 case lang_input_statement_enum
:
3520 if (s
->input_statement
.flags
.real
)
3522 lang_statement_union_type
**os_tail
;
3523 lang_statement_list_type add
;
3526 s
->input_statement
.target
= current_target
;
3528 /* If we are being called from within a group, and this
3529 is an archive which has already been searched, then
3530 force it to be researched unless the whole archive
3531 has been loaded already. Do the same for a rescan.
3532 Likewise reload --as-needed shared libs. */
3533 if (mode
!= OPEN_BFD_NORMAL
3534 #if BFD_SUPPORTS_PLUGINS
3535 && ((mode
& OPEN_BFD_RESCAN
) == 0
3536 || plugin_insert
== NULL
)
3538 && s
->input_statement
.flags
.loaded
3539 && (abfd
= s
->input_statement
.the_bfd
) != NULL
3540 && ((bfd_get_format (abfd
) == bfd_archive
3541 && !s
->input_statement
.flags
.whole_archive
)
3542 || (bfd_get_format (abfd
) == bfd_object
3543 && ((abfd
->flags
) & DYNAMIC
) != 0
3544 && s
->input_statement
.flags
.add_DT_NEEDED_for_regular
3545 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
3546 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)))
3548 s
->input_statement
.flags
.loaded
= false;
3549 s
->input_statement
.flags
.reload
= true;
3552 os_tail
= lang_os_list
.tail
;
3553 lang_list_init (&add
);
3555 if (!load_symbols (&s
->input_statement
, &add
))
3556 config
.make_executable
= false;
3558 if (add
.head
!= NULL
)
3560 /* If this was a script with output sections then
3561 tack any added statements on to the end of the
3562 list. This avoids having to reorder the output
3563 section statement list. Very likely the user
3564 forgot -T, and whatever we do here will not meet
3565 naive user expectations. */
3566 if (os_tail
!= lang_os_list
.tail
)
3568 einfo (_("%P: warning: %s contains output sections;"
3569 " did you forget -T?\n"),
3570 s
->input_statement
.filename
);
3571 *stat_ptr
->tail
= add
.head
;
3572 stat_ptr
->tail
= add
.tail
;
3576 *add
.tail
= s
->header
.next
;
3577 s
->header
.next
= add
.head
;
3581 #if BFD_SUPPORTS_PLUGINS
3582 /* If we have found the point at which a plugin added new
3583 files, clear plugin_insert to enable archive rescan. */
3584 if (&s
->input_statement
== plugin_insert
)
3585 plugin_insert
= NULL
;
3588 case lang_assignment_statement_enum
:
3589 if (s
->assignment_statement
.exp
->type
.node_class
!= etree_assert
)
3590 exp_fold_tree_no_dot (s
->assignment_statement
.exp
);
3597 /* Exit if any of the files were missing. */
3598 if (input_flags
.missing_file
)
3602 #ifdef ENABLE_LIBCTF
3603 /* Emit CTF errors and warnings. fp can be NULL to report errors/warnings
3604 that happened specifically at CTF open time. */
3606 lang_ctf_errs_warnings (ctf_dict_t
*fp
)
3608 ctf_next_t
*i
= NULL
;
3613 while ((text
= ctf_errwarning_next (fp
, &i
, &is_warning
, &err
)) != NULL
)
3615 einfo (_("%s: %s\n"), is_warning
? _("CTF warning"): _("CTF error"),
3619 if (err
!= ECTF_NEXT_END
)
3621 einfo (_("CTF error: cannot get CTF errors: `%s'\n"),
3625 /* `err' returns errors from the error/warning iterator in particular.
3626 These never assert. But if we have an fp, that could have recorded
3627 an assertion failure: assert if it has done so. */
3628 ASSERT (!fp
|| ctf_errno (fp
) != ECTF_INTERNAL
);
3631 /* Open the CTF sections in the input files with libctf: if any were opened,
3632 create a fake input file that we'll write the merged CTF data to later
3636 ldlang_open_ctf (void)
3641 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3645 /* Incoming files from the compiler have a single ctf_dict_t in them
3646 (which is presented to us by the libctf API in a ctf_archive_t
3647 wrapper): files derived from a previous relocatable link have a CTF
3648 archive containing possibly many CTF files. */
3650 if ((file
->the_ctf
= ctf_bfdopen (file
->the_bfd
, &err
)) == NULL
)
3652 if (err
!= ECTF_NOCTFDATA
)
3654 lang_ctf_errs_warnings (NULL
);
3655 einfo (_("%P: warning: CTF section in %pB not loaded; "
3656 "its types will be discarded: %s\n"), file
->the_bfd
,
3662 /* Prevent the contents of this section from being written, while
3663 requiring the section itself to be duplicated in the output, but only
3665 /* This section must exist if ctf_bfdopen() succeeded. */
3666 sect
= bfd_get_section_by_name (file
->the_bfd
, ".ctf");
3668 sect
->flags
|= SEC_NEVER_LOAD
| SEC_HAS_CONTENTS
| SEC_LINKER_CREATED
;
3671 sect
->flags
|= SEC_EXCLUDE
;
3681 if ((ctf_output
= ctf_create (&err
)) != NULL
)
3684 einfo (_("%P: warning: CTF output not created: `%s'\n"),
3687 LANG_FOR_EACH_INPUT_STATEMENT (errfile
)
3688 ctf_close (errfile
->the_ctf
);
3691 /* Merge together CTF sections. After this, only the symtab-dependent
3692 function and data object sections need adjustment. */
3695 lang_merge_ctf (void)
3697 asection
*output_sect
;
3703 output_sect
= bfd_get_section_by_name (link_info
.output_bfd
, ".ctf");
3705 /* If the section was discarded, don't waste time merging. */
3706 if (output_sect
== NULL
)
3708 ctf_dict_close (ctf_output
);
3711 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3713 ctf_close (file
->the_ctf
);
3714 file
->the_ctf
= NULL
;
3719 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3724 /* Takes ownership of file->the_ctf. */
3725 if (ctf_link_add_ctf (ctf_output
, file
->the_ctf
, file
->filename
) < 0)
3727 einfo (_("%P: warning: CTF section in %pB cannot be linked: `%s'\n"),
3728 file
->the_bfd
, ctf_errmsg (ctf_errno (ctf_output
)));
3729 ctf_close (file
->the_ctf
);
3730 file
->the_ctf
= NULL
;
3735 if (!config
.ctf_share_duplicated
)
3736 flags
= CTF_LINK_SHARE_UNCONFLICTED
;
3738 flags
= CTF_LINK_SHARE_DUPLICATED
;
3739 if (!config
.ctf_variables
)
3740 flags
|= CTF_LINK_OMIT_VARIABLES_SECTION
;
3741 if (bfd_link_relocatable (&link_info
))
3742 flags
|= CTF_LINK_NO_FILTER_REPORTED_SYMS
;
3744 if (ctf_link (ctf_output
, flags
) < 0)
3746 lang_ctf_errs_warnings (ctf_output
);
3747 einfo (_("%P: warning: CTF linking failed; "
3748 "output will have no CTF section: %s\n"),
3749 ctf_errmsg (ctf_errno (ctf_output
)));
3752 output_sect
->size
= 0;
3753 output_sect
->flags
|= SEC_EXCLUDE
;
3756 /* Output any lingering errors that didn't come from ctf_link. */
3757 lang_ctf_errs_warnings (ctf_output
);
3760 /* Let the emulation acquire strings from the dynamic strtab to help it optimize
3761 the CTF, if supported. */
3764 ldlang_ctf_acquire_strings (struct elf_strtab_hash
*dynstrtab
)
3766 ldemul_acquire_strings_for_ctf (ctf_output
, dynstrtab
);
3769 /* Inform the emulation about the addition of a new dynamic symbol, in BFD
3771 void ldlang_ctf_new_dynsym (int symidx
, struct elf_internal_sym
*sym
)
3773 ldemul_new_dynsym_for_ctf (ctf_output
, symidx
, sym
);
3776 /* Write out the CTF section. Called early, if the emulation isn't going to
3777 need to dedup against the strtab and symtab, then possibly called from the
3778 target linker code if the dedup has happened. */
3780 lang_write_ctf (int late
)
3783 asection
*output_sect
;
3790 /* Emit CTF late if this emulation says it can do so. */
3791 if (ldemul_emit_ctf_early ())
3796 if (!ldemul_emit_ctf_early ())
3800 /* Inform the emulation that all the symbols that will be received have
3803 ldemul_new_dynsym_for_ctf (ctf_output
, 0, NULL
);
3807 output_sect
= bfd_get_section_by_name (link_info
.output_bfd
, ".ctf");
3810 output_sect
->contents
= ctf_link_write (ctf_output
, &output_size
,
3811 CTF_COMPRESSION_THRESHOLD
);
3812 output_sect
->size
= output_size
;
3813 output_sect
->flags
|= SEC_IN_MEMORY
| SEC_KEEP
;
3815 lang_ctf_errs_warnings (ctf_output
);
3816 if (!output_sect
->contents
)
3818 einfo (_("%P: warning: CTF section emission failed; "
3819 "output will have no CTF section: %s\n"),
3820 ctf_errmsg (ctf_errno (ctf_output
)));
3821 output_sect
->size
= 0;
3822 output_sect
->flags
|= SEC_EXCLUDE
;
3826 /* This also closes every CTF input file used in the link. */
3827 ctf_dict_close (ctf_output
);
3830 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3831 file
->the_ctf
= NULL
;
3834 /* Write out the CTF section late, if the emulation needs that. */
3837 ldlang_write_ctf_late (void)
3839 /* Trigger a "late call", if the emulation needs one. */
3845 ldlang_open_ctf (void)
3847 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3851 /* If built without CTF, warn and delete all CTF sections from the output.
3852 (The alternative would be to simply concatenate them, which does not
3853 yield a valid CTF section.) */
3855 if ((sect
= bfd_get_section_by_name (file
->the_bfd
, ".ctf")) != NULL
)
3857 einfo (_("%P: warning: CTF section in %pB not linkable: "
3858 "%P was built without support for CTF\n"), file
->the_bfd
);
3860 sect
->flags
|= SEC_EXCLUDE
;
3865 static void lang_merge_ctf (void) {}
3867 ldlang_ctf_acquire_strings (struct elf_strtab_hash
*dynstrtab
3868 ATTRIBUTE_UNUSED
) {}
3870 ldlang_ctf_new_dynsym (int symidx ATTRIBUTE_UNUSED
,
3871 struct elf_internal_sym
*sym ATTRIBUTE_UNUSED
) {}
3872 static void lang_write_ctf (int late ATTRIBUTE_UNUSED
) {}
3873 void ldlang_write_ctf_late (void) {}
3876 /* Add the supplied name to the symbol table as an undefined reference.
3877 This is a two step process as the symbol table doesn't even exist at
3878 the time the ld command line is processed. First we put the name
3879 on a list, then, once the output file has been opened, transfer the
3880 name to the symbol table. */
3882 typedef struct bfd_sym_chain ldlang_undef_chain_list_type
;
3884 #define ldlang_undef_chain_list_head entry_symbol.next
3887 ldlang_add_undef (const char *const name
, bool cmdline ATTRIBUTE_UNUSED
)
3889 ldlang_undef_chain_list_type
*new_undef
;
3891 new_undef
= stat_alloc (sizeof (*new_undef
));
3892 new_undef
->next
= ldlang_undef_chain_list_head
;
3893 ldlang_undef_chain_list_head
= new_undef
;
3895 new_undef
->name
= xstrdup (name
);
3897 if (link_info
.output_bfd
!= NULL
)
3898 insert_undefined (new_undef
->name
);
3901 /* Insert NAME as undefined in the symbol table. */
3904 insert_undefined (const char *name
)
3906 struct bfd_link_hash_entry
*h
;
3908 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, false, true);
3910 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
3911 if (h
->type
== bfd_link_hash_new
)
3913 h
->type
= bfd_link_hash_undefined
;
3914 h
->u
.undef
.abfd
= NULL
;
3915 h
->non_ir_ref_regular
= true;
3916 bfd_link_add_undef (link_info
.hash
, h
);
3920 /* Run through the list of undefineds created above and place them
3921 into the linker hash table as undefined symbols belonging to the
3925 lang_place_undefineds (void)
3927 ldlang_undef_chain_list_type
*ptr
;
3929 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
3930 insert_undefined (ptr
->name
);
3933 /* Mark -u symbols against garbage collection. */
3936 lang_mark_undefineds (void)
3938 ldlang_undef_chain_list_type
*ptr
;
3940 if (is_elf_hash_table (link_info
.hash
))
3941 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
3943 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*)
3944 bfd_link_hash_lookup (link_info
.hash
, ptr
->name
, false, false, true);
3950 /* Structure used to build the list of symbols that the user has required
3953 struct require_defined_symbol
3956 struct require_defined_symbol
*next
;
3959 /* The list of symbols that the user has required be defined. */
3961 static struct require_defined_symbol
*require_defined_symbol_list
;
3963 /* Add a new symbol NAME to the list of symbols that are required to be
3967 ldlang_add_require_defined (const char *const name
)
3969 struct require_defined_symbol
*ptr
;
3971 ldlang_add_undef (name
, true);
3972 ptr
= stat_alloc (sizeof (*ptr
));
3973 ptr
->next
= require_defined_symbol_list
;
3974 ptr
->name
= strdup (name
);
3975 require_defined_symbol_list
= ptr
;
3978 /* Check that all symbols the user required to be defined, are defined,
3979 raise an error if we find a symbol that is not defined. */
3982 ldlang_check_require_defined_symbols (void)
3984 struct require_defined_symbol
*ptr
;
3986 for (ptr
= require_defined_symbol_list
; ptr
!= NULL
; ptr
= ptr
->next
)
3988 struct bfd_link_hash_entry
*h
;
3990 h
= bfd_link_hash_lookup (link_info
.hash
, ptr
->name
,
3991 false, false, true);
3993 || (h
->type
!= bfd_link_hash_defined
3994 && h
->type
!= bfd_link_hash_defweak
))
3995 einfo(_("%X%P: required symbol `%s' not defined\n"), ptr
->name
);
3999 /* Check for all readonly or some readwrite sections. */
4002 check_input_sections
4003 (lang_statement_union_type
*s
,
4004 lang_output_section_statement_type
*output_section_statement
)
4006 for (; s
!= NULL
; s
= s
->header
.next
)
4008 switch (s
->header
.type
)
4010 case lang_wild_statement_enum
:
4011 walk_wild (&s
->wild_statement
, check_section_callback
,
4012 output_section_statement
);
4013 if (!output_section_statement
->all_input_readonly
)
4016 case lang_constructors_statement_enum
:
4017 check_input_sections (constructor_list
.head
,
4018 output_section_statement
);
4019 if (!output_section_statement
->all_input_readonly
)
4022 case lang_group_statement_enum
:
4023 check_input_sections (s
->group_statement
.children
.head
,
4024 output_section_statement
);
4025 if (!output_section_statement
->all_input_readonly
)
4034 /* Update wildcard statements if needed. */
4037 update_wild_statements (lang_statement_union_type
*s
)
4039 struct wildcard_list
*sec
;
4041 switch (sort_section
)
4051 for (; s
!= NULL
; s
= s
->header
.next
)
4053 switch (s
->header
.type
)
4058 case lang_wild_statement_enum
:
4059 for (sec
= s
->wild_statement
.section_list
; sec
!= NULL
;
4061 /* Don't sort .init/.fini sections. */
4062 if (strcmp (sec
->spec
.name
, ".init") != 0
4063 && strcmp (sec
->spec
.name
, ".fini") != 0)
4065 switch (sec
->spec
.sorted
)
4068 sec
->spec
.sorted
= sort_section
;
4071 if (sort_section
== by_alignment
)
4072 sec
->spec
.sorted
= by_name_alignment
;
4075 if (sort_section
== by_name
)
4076 sec
->spec
.sorted
= by_alignment_name
;
4081 s
->wild_statement
.any_specs_sorted
= true;
4085 case lang_constructors_statement_enum
:
4086 update_wild_statements (constructor_list
.head
);
4089 case lang_output_section_statement_enum
:
4090 update_wild_statements
4091 (s
->output_section_statement
.children
.head
);
4094 case lang_group_statement_enum
:
4095 update_wild_statements (s
->group_statement
.children
.head
);
4103 /* Open input files and attach to output sections. */
4106 map_input_to_output_sections
4107 (lang_statement_union_type
*s
, const char *target
,
4108 lang_output_section_statement_type
*os
)
4110 for (; s
!= NULL
; s
= s
->header
.next
)
4112 lang_output_section_statement_type
*tos
;
4114 unsigned int type
= 0;
4116 switch (s
->header
.type
)
4118 case lang_wild_statement_enum
:
4119 wild (&s
->wild_statement
, target
, os
);
4121 case lang_constructors_statement_enum
:
4122 map_input_to_output_sections (constructor_list
.head
,
4126 case lang_output_section_statement_enum
:
4127 tos
= &s
->output_section_statement
;
4128 if (tos
->constraint
== ONLY_IF_RW
4129 || tos
->constraint
== ONLY_IF_RO
)
4131 tos
->all_input_readonly
= true;
4132 check_input_sections (tos
->children
.head
, tos
);
4133 if (tos
->all_input_readonly
!= (tos
->constraint
== ONLY_IF_RO
))
4134 tos
->constraint
= -1;
4136 if (tos
->constraint
>= 0)
4137 map_input_to_output_sections (tos
->children
.head
,
4141 case lang_output_statement_enum
:
4143 case lang_target_statement_enum
:
4144 target
= s
->target_statement
.target
;
4146 case lang_group_statement_enum
:
4147 map_input_to_output_sections (s
->group_statement
.children
.head
,
4151 case lang_data_statement_enum
:
4152 /* Make sure that any sections mentioned in the expression
4154 exp_init_os (s
->data_statement
.exp
);
4155 /* The output section gets CONTENTS, ALLOC and LOAD, but
4156 these may be overridden by the script. */
4157 flags
= SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
;
4158 switch (os
->sectype
)
4160 case normal_section
:
4161 case overlay_section
:
4162 case first_overlay_section
:
4164 case noalloc_section
:
4165 flags
= SEC_HAS_CONTENTS
;
4167 case readonly_section
:
4168 flags
|= SEC_READONLY
;
4170 case typed_readonly_section
:
4171 flags
|= SEC_READONLY
;
4174 if (os
->sectype_value
->type
.node_class
== etree_name
4175 && os
->sectype_value
->type
.node_code
== NAME
)
4177 const char *name
= os
->sectype_value
->name
.name
;
4178 if (strcmp (name
, "SHT_PROGBITS") == 0)
4179 type
= SHT_PROGBITS
;
4180 else if (strcmp (name
, "SHT_STRTAB") == 0)
4182 else if (strcmp (name
, "SHT_NOTE") == 0)
4184 else if (strcmp (name
, "SHT_NOBITS") == 0)
4186 else if (strcmp (name
, "SHT_INIT_ARRAY") == 0)
4187 type
= SHT_INIT_ARRAY
;
4188 else if (strcmp (name
, "SHT_FINI_ARRAY") == 0)
4189 type
= SHT_FINI_ARRAY
;
4190 else if (strcmp (name
, "SHT_PREINIT_ARRAY") == 0)
4191 type
= SHT_PREINIT_ARRAY
;
4193 einfo (_ ("%F%P: invalid type for output section `%s'\n"),
4198 exp_fold_tree_no_dot (os
->sectype_value
);
4199 if (expld
.result
.valid_p
)
4200 type
= expld
.result
.value
;
4202 einfo (_ ("%F%P: invalid type for output section `%s'\n"),
4206 case noload_section
:
4207 if (bfd_get_flavour (link_info
.output_bfd
)
4208 == bfd_target_elf_flavour
)
4209 flags
= SEC_NEVER_LOAD
| SEC_ALLOC
;
4211 flags
= SEC_NEVER_LOAD
| SEC_HAS_CONTENTS
;
4214 if (os
->bfd_section
== NULL
)
4215 init_os (os
, flags
| SEC_READONLY
);
4217 os
->bfd_section
->flags
|= flags
;
4218 os
->bfd_section
->type
= type
;
4220 case lang_input_section_enum
:
4222 case lang_fill_statement_enum
:
4223 case lang_object_symbols_statement_enum
:
4224 case lang_reloc_statement_enum
:
4225 case lang_padding_statement_enum
:
4226 case lang_input_statement_enum
:
4227 if (os
!= NULL
&& os
->bfd_section
== NULL
)
4230 case lang_assignment_statement_enum
:
4231 if (os
!= NULL
&& os
->bfd_section
== NULL
)
4234 /* Make sure that any sections mentioned in the assignment
4236 exp_init_os (s
->assignment_statement
.exp
);
4238 case lang_address_statement_enum
:
4239 /* Mark the specified section with the supplied address.
4240 If this section was actually a segment marker, then the
4241 directive is ignored if the linker script explicitly
4242 processed the segment marker. Originally, the linker
4243 treated segment directives (like -Ttext on the
4244 command-line) as section directives. We honor the
4245 section directive semantics for backwards compatibility;
4246 linker scripts that do not specifically check for
4247 SEGMENT_START automatically get the old semantics. */
4248 if (!s
->address_statement
.segment
4249 || !s
->address_statement
.segment
->used
)
4251 const char *name
= s
->address_statement
.section_name
;
4253 /* Create the output section statement here so that
4254 orphans with a set address will be placed after other
4255 script sections. If we let the orphan placement code
4256 place them in amongst other sections then the address
4257 will affect following script sections, which is
4258 likely to surprise naive users. */
4259 tos
= lang_output_section_statement_lookup (name
, 0, 1);
4260 tos
->addr_tree
= s
->address_statement
.address
;
4261 if (tos
->bfd_section
== NULL
)
4265 case lang_insert_statement_enum
:
4267 case lang_input_matcher_enum
:
4273 /* An insert statement snips out all the linker statements from the
4274 start of the list and places them after the output section
4275 statement specified by the insert. This operation is complicated
4276 by the fact that we keep a doubly linked list of output section
4277 statements as well as the singly linked list of all statements.
4278 FIXME someday: Twiddling with the list not only moves statements
4279 from the user's script but also input and group statements that are
4280 built from command line object files and --start-group. We only
4281 get away with this because the list pointers used by file_chain
4282 and input_file_chain are not reordered, and processing via
4283 statement_list after this point mostly ignores input statements.
4284 One exception is the map file, where LOAD and START GROUP/END GROUP
4285 can end up looking odd. */
4288 process_insert_statements (lang_statement_union_type
**start
)
4290 lang_statement_union_type
**s
;
4291 lang_output_section_statement_type
*first_os
= NULL
;
4292 lang_output_section_statement_type
*last_os
= NULL
;
4293 lang_output_section_statement_type
*os
;
4298 if ((*s
)->header
.type
== lang_output_section_statement_enum
)
4300 /* Keep pointers to the first and last output section
4301 statement in the sequence we may be about to move. */
4302 os
= &(*s
)->output_section_statement
;
4304 ASSERT (last_os
== NULL
|| last_os
->next
== os
);
4307 /* Set constraint negative so that lang_output_section_find
4308 won't match this output section statement. At this
4309 stage in linking constraint has values in the range
4310 [-1, ONLY_IN_RW]. */
4311 last_os
->constraint
= -2 - last_os
->constraint
;
4312 if (first_os
== NULL
)
4315 else if ((*s
)->header
.type
== lang_group_statement_enum
)
4317 /* A user might put -T between --start-group and
4318 --end-group. One way this odd construct might arise is
4319 from a wrapper around ld to change library search
4320 behaviour. For example:
4322 exec real_ld --start-group "$@" --end-group
4323 This isn't completely unreasonable so go looking inside a
4324 group statement for insert statements. */
4325 process_insert_statements (&(*s
)->group_statement
.children
.head
);
4327 else if ((*s
)->header
.type
== lang_insert_statement_enum
)
4329 lang_insert_statement_type
*i
= &(*s
)->insert_statement
;
4330 lang_output_section_statement_type
*where
;
4331 lang_statement_union_type
**ptr
;
4332 lang_statement_union_type
*first
;
4334 if (link_info
.non_contiguous_regions
)
4336 einfo (_("warning: INSERT statement in linker script is "
4337 "incompatible with --enable-non-contiguous-regions.\n"));
4340 where
= lang_output_section_find (i
->where
);
4341 if (where
!= NULL
&& i
->is_before
)
4344 where
= where
->prev
;
4345 while (where
!= NULL
&& where
->constraint
< 0);
4349 einfo (_("%F%P: %s not found for insert\n"), i
->where
);
4353 /* Deal with reordering the output section statement list. */
4354 if (last_os
!= NULL
)
4356 asection
*first_sec
, *last_sec
;
4357 struct lang_output_section_statement_struct
**next
;
4359 /* Snip out the output sections we are moving. */
4360 first_os
->prev
->next
= last_os
->next
;
4361 if (last_os
->next
== NULL
)
4363 next
= &first_os
->prev
->next
;
4364 lang_os_list
.tail
= (lang_statement_union_type
**) next
;
4367 last_os
->next
->prev
= first_os
->prev
;
4368 /* Add them in at the new position. */
4369 last_os
->next
= where
->next
;
4370 if (where
->next
== NULL
)
4372 next
= &last_os
->next
;
4373 lang_os_list
.tail
= (lang_statement_union_type
**) next
;
4376 where
->next
->prev
= last_os
;
4377 first_os
->prev
= where
;
4378 where
->next
= first_os
;
4380 /* Move the bfd sections in the same way. */
4383 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
4385 os
->constraint
= -2 - os
->constraint
;
4386 if (os
->bfd_section
!= NULL
4387 && os
->bfd_section
->owner
!= NULL
)
4389 last_sec
= os
->bfd_section
;
4390 if (first_sec
== NULL
)
4391 first_sec
= last_sec
;
4396 if (last_sec
!= NULL
)
4398 asection
*sec
= where
->bfd_section
;
4400 sec
= output_prev_sec_find (where
);
4402 /* The place we want to insert must come after the
4403 sections we are moving. So if we find no
4404 section or if the section is the same as our
4405 last section, then no move is needed. */
4406 if (sec
!= NULL
&& sec
!= last_sec
)
4408 /* Trim them off. */
4409 if (first_sec
->prev
!= NULL
)
4410 first_sec
->prev
->next
= last_sec
->next
;
4412 link_info
.output_bfd
->sections
= last_sec
->next
;
4413 if (last_sec
->next
!= NULL
)
4414 last_sec
->next
->prev
= first_sec
->prev
;
4416 link_info
.output_bfd
->section_last
= first_sec
->prev
;
4418 if (sec
->owner
== NULL
)
4419 /* SEC is the absolute section, from the
4420 first dummy output section statement. Add
4421 back the sections we trimmed off to the
4422 start of the bfd sections. */
4425 last_sec
->next
= sec
->next
;
4427 last_sec
->next
= link_info
.output_bfd
->sections
;
4428 if (last_sec
->next
!= NULL
)
4429 last_sec
->next
->prev
= last_sec
;
4431 link_info
.output_bfd
->section_last
= last_sec
;
4432 first_sec
->prev
= sec
;
4433 if (first_sec
->prev
!= NULL
)
4434 first_sec
->prev
->next
= first_sec
;
4436 link_info
.output_bfd
->sections
= first_sec
;
4441 lang_statement_union_type
*after
= (void *) where
;
4442 if (where
== &lang_os_list
.head
->output_section_statement
4443 && where
->next
== first_os
)
4445 /* PR30155. Handle a corner case where the statement
4446 list is something like the following:
4448 . .data 0x0000000000000000 0x0
4449 . [0x0000000000000000] b = .
4451 . .data 0x0000000000000000 0x0 t.o
4452 . 0x0000000000000000 0x4 LONG 0x0
4453 . INSERT BEFORE .text.start
4454 . [0x0000000000000004] a = .
4455 . .text.start 0x0000000000000000 0x0
4456 . [0x0000000000000000] c = .
4457 . OUTPUT(a.out elf64-x86-64)
4458 Here we do not want to allow insert_os_after to
4459 choose a point inside the list we are moving.
4460 That would lose the list. Instead, let
4461 insert_os_after work from the INSERT, which in this
4462 particular example will result in inserting after
4463 the assignment "a = .". */
4466 ptr
= insert_os_after (after
);
4467 /* Snip everything from the start of the list, up to and
4468 including the insert statement we are currently processing. */
4470 *start
= (*s
)->header
.next
;
4471 /* Add them back where they belong, minus the insert. */
4474 statement_list
.tail
= s
;
4481 s
= &(*s
)->header
.next
;
4484 /* Undo constraint twiddling. */
4485 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
4487 os
->constraint
= -2 - os
->constraint
;
4493 /* An output section might have been removed after its statement was
4494 added. For example, ldemul_before_allocation can remove dynamic
4495 sections if they turn out to be not needed. Clean them up here. */
4498 strip_excluded_output_sections (void)
4500 lang_output_section_statement_type
*os
;
4502 /* Run lang_size_sections (if not already done). */
4503 if (expld
.phase
!= lang_mark_phase_enum
)
4505 expld
.phase
= lang_mark_phase_enum
;
4506 expld
.dataseg
.phase
= exp_seg_none
;
4507 one_lang_size_sections_pass (NULL
, false);
4508 lang_reset_memory_regions ();
4511 for (os
= (void *) lang_os_list
.head
;
4515 asection
*output_section
;
4518 if (os
->constraint
< 0)
4521 output_section
= os
->bfd_section
;
4522 if (output_section
== NULL
)
4525 exclude
= (output_section
->rawsize
== 0
4526 && (output_section
->flags
& SEC_KEEP
) == 0
4527 && !bfd_section_removed_from_list (link_info
.output_bfd
,
4530 /* Some sections have not yet been sized, notably .gnu.version,
4531 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
4532 input sections, so don't drop output sections that have such
4533 input sections unless they are also marked SEC_EXCLUDE. */
4534 if (exclude
&& output_section
->map_head
.s
!= NULL
)
4538 for (s
= output_section
->map_head
.s
; s
!= NULL
; s
= s
->map_head
.s
)
4539 if ((s
->flags
& SEC_EXCLUDE
) == 0
4540 && ((s
->flags
& SEC_LINKER_CREATED
) != 0
4541 || link_info
.emitrelocations
))
4550 /* We don't set bfd_section to NULL since bfd_section of the
4551 removed output section statement may still be used. */
4552 if (!os
->update_dot
)
4554 output_section
->flags
|= SEC_EXCLUDE
;
4555 bfd_section_list_remove (link_info
.output_bfd
, output_section
);
4556 link_info
.output_bfd
->section_count
--;
4561 /* Called from ldwrite to clear out asection.map_head and
4562 asection.map_tail for use as link_orders in ldwrite. */
4565 lang_clear_os_map (void)
4567 lang_output_section_statement_type
*os
;
4569 if (map_head_is_link_order
)
4572 for (os
= (void *) lang_os_list
.head
;
4576 asection
*output_section
;
4578 if (os
->constraint
< 0)
4581 output_section
= os
->bfd_section
;
4582 if (output_section
== NULL
)
4585 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
4586 output_section
->map_head
.link_order
= NULL
;
4587 output_section
->map_tail
.link_order
= NULL
;
4590 /* Stop future calls to lang_add_section from messing with map_head
4591 and map_tail link_order fields. */
4592 map_head_is_link_order
= true;
4596 print_output_section_statement
4597 (lang_output_section_statement_type
*output_section_statement
)
4599 asection
*section
= output_section_statement
->bfd_section
;
4602 if (output_section_statement
!= abs_output_section
)
4604 minfo ("\n%s", output_section_statement
->name
);
4606 if (section
!= NULL
)
4608 print_dot
= section
->vma
;
4610 len
= strlen (output_section_statement
->name
);
4611 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
4616 print_spaces (SECTION_NAME_MAP_LENGTH
- len
);
4618 minfo ("0x%V %W", section
->vma
, TO_ADDR (section
->size
));
4620 if (section
->vma
!= section
->lma
)
4621 minfo (_(" load address 0x%V"), section
->lma
);
4623 if (output_section_statement
->update_dot_tree
!= NULL
)
4624 exp_fold_tree (output_section_statement
->update_dot_tree
,
4625 bfd_abs_section_ptr
, &print_dot
);
4631 print_statement_list (output_section_statement
->children
.head
,
4632 output_section_statement
);
4636 print_assignment (lang_assignment_statement_type
*assignment
,
4637 lang_output_section_statement_type
*output_section
)
4643 print_spaces (SECTION_NAME_MAP_LENGTH
);
4645 if (assignment
->exp
->type
.node_class
== etree_assert
)
4648 tree
= assignment
->exp
->assert_s
.child
;
4652 const char *dst
= assignment
->exp
->assign
.dst
;
4654 is_dot
= (dst
[0] == '.' && dst
[1] == 0);
4655 tree
= assignment
->exp
;
4658 osec
= output_section
->bfd_section
;
4660 osec
= bfd_abs_section_ptr
;
4662 if (assignment
->exp
->type
.node_class
!= etree_provide
)
4663 exp_fold_tree (tree
, osec
, &print_dot
);
4665 expld
.result
.valid_p
= false;
4668 const char *str
= buf
;
4669 if (expld
.result
.valid_p
)
4673 if (assignment
->exp
->type
.node_class
== etree_assert
4675 || expld
.assign_name
!= NULL
)
4677 value
= expld
.result
.value
;
4679 if (expld
.result
.section
!= NULL
)
4680 value
+= expld
.result
.section
->vma
;
4684 bfd_sprintf_vma (link_info
.output_bfd
, buf
+ 2, value
);
4690 struct bfd_link_hash_entry
*h
;
4692 h
= bfd_link_hash_lookup (link_info
.hash
, assignment
->exp
->assign
.dst
,
4693 false, false, true);
4695 && (h
->type
== bfd_link_hash_defined
4696 || h
->type
== bfd_link_hash_defweak
))
4698 value
= h
->u
.def
.value
;
4699 value
+= h
->u
.def
.section
->output_section
->vma
;
4700 value
+= h
->u
.def
.section
->output_offset
;
4705 bfd_sprintf_vma (link_info
.output_bfd
, buf
+ 3, value
);
4709 str
= "[unresolved]";
4714 if (assignment
->exp
->type
.node_class
== etree_provide
)
4719 expld
.assign_name
= NULL
;
4721 fprintf (config
.map_file
, "%-34s", str
);
4722 exp_print_tree (assignment
->exp
);
4727 print_input_statement (lang_input_statement_type
*statm
)
4729 if (statm
->filename
!= NULL
)
4730 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
4733 /* Print all symbols defined in a particular section. This is called
4734 via bfd_link_hash_traverse, or by print_all_symbols. */
4737 print_one_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
)
4739 asection
*sec
= (asection
*) ptr
;
4741 if ((hash_entry
->type
== bfd_link_hash_defined
4742 || hash_entry
->type
== bfd_link_hash_defweak
)
4743 && sec
== hash_entry
->u
.def
.section
)
4745 print_spaces (SECTION_NAME_MAP_LENGTH
);
4747 (hash_entry
->u
.def
.value
4748 + hash_entry
->u
.def
.section
->output_offset
4749 + hash_entry
->u
.def
.section
->output_section
->vma
));
4751 minfo (" %pT\n", hash_entry
->root
.string
);
4758 hash_entry_addr_cmp (const void *a
, const void *b
)
4760 const struct bfd_link_hash_entry
*l
= *(const struct bfd_link_hash_entry
**)a
;
4761 const struct bfd_link_hash_entry
*r
= *(const struct bfd_link_hash_entry
**)b
;
4763 if (l
->u
.def
.value
< r
->u
.def
.value
)
4765 else if (l
->u
.def
.value
> r
->u
.def
.value
)
4772 print_all_symbols (asection
*sec
)
4774 input_section_userdata_type
*ud
= bfd_section_userdata (sec
);
4775 struct map_symbol_def
*def
;
4776 struct bfd_link_hash_entry
**entries
;
4782 *ud
->map_symbol_def_tail
= 0;
4784 /* Sort the symbols by address. */
4785 entries
= (struct bfd_link_hash_entry
**)
4786 obstack_alloc (&map_obstack
,
4787 ud
->map_symbol_def_count
* sizeof (*entries
));
4789 for (i
= 0, def
= ud
->map_symbol_def_head
; def
; def
= def
->next
, i
++)
4790 entries
[i
] = def
->entry
;
4792 qsort (entries
, ud
->map_symbol_def_count
, sizeof (*entries
),
4793 hash_entry_addr_cmp
);
4795 /* Print the symbols. */
4796 for (i
= 0; i
< ud
->map_symbol_def_count
; i
++)
4797 ldemul_print_symbol (entries
[i
], sec
);
4799 obstack_free (&map_obstack
, entries
);
4802 /* Returns TRUE if SYM is a symbol suitable for printing
4803 in a linker map as a local symbol. */
4806 ld_is_local_symbol (asymbol
* sym
)
4808 const char * name
= bfd_asymbol_name (sym
);
4810 if (name
== NULL
|| *name
== 0)
4813 if (strcmp (name
, "(null)") == 0)
4816 /* Skip .Lxxx and such like. */
4817 if (bfd_is_local_label (link_info
.output_bfd
, sym
))
4820 /* FIXME: This is intended to skip ARM mapping symbols,
4821 which for some reason are not excluded by bfd_is_local_label,
4822 but maybe it is wrong for other architectures.
4823 It would be better to fix bfd_is_local_label. */
4827 /* Some local symbols, eg _GLOBAL_OFFSET_TABLE_, are present
4828 in the hash table, so do not print duplicates here. */
4829 struct bfd_link_hash_entry
* h
;
4830 h
= bfd_link_hash_lookup (link_info
.hash
, name
, false /* create */,
4831 false /* copy */, true /* follow */);
4835 /* Symbols from the plugin owned BFD will not get their own
4836 iteration of this function, but can be on the link_info
4837 list. So include them here. */
4838 if (h
->u
.def
.section
->owner
!= NULL
4839 && ((bfd_get_file_flags (h
->u
.def
.section
->owner
) & (BFD_LINKER_CREATED
| BFD_PLUGIN
))
4840 == (BFD_LINKER_CREATED
| BFD_PLUGIN
)))
4846 /* Print information about an input section to the map file. */
4849 print_input_section (asection
*i
, bool is_discarded
)
4851 bfd_size_type size
= i
->size
;
4857 minfo (" %s", i
->name
);
4859 len
= 1 + strlen (i
->name
);
4860 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
4865 print_spaces (SECTION_NAME_MAP_LENGTH
- len
);
4867 if (i
->output_section
!= NULL
4868 && i
->output_section
->owner
== link_info
.output_bfd
)
4869 addr
= i
->output_section
->vma
+ i
->output_offset
;
4878 bfd_sprintf_vma (link_info
.output_bfd
, buf
, addr
);
4879 minfo ("0x%s %W %pB\n", buf
, TO_ADDR (size
), i
->owner
);
4881 if (size
!= i
->rawsize
&& i
->rawsize
!= 0)
4883 len
= SECTION_NAME_MAP_LENGTH
+ 3 + strlen (buf
);
4885 minfo (_("%W (size before relaxing)\n"), TO_ADDR (i
->rawsize
));
4888 if (i
->output_section
!= NULL
4889 && i
->output_section
->owner
== link_info
.output_bfd
)
4891 if (link_info
.reduce_memory_overheads
)
4892 bfd_link_hash_traverse (link_info
.hash
, ldemul_print_symbol
, i
);
4894 print_all_symbols (i
);
4896 /* Update print_dot, but make sure that we do not move it
4897 backwards - this could happen if we have overlays and a
4898 later overlay is shorter than an earier one. */
4899 if (addr
+ TO_ADDR (size
) > print_dot
)
4900 print_dot
= addr
+ TO_ADDR (size
);
4902 if (config
.print_map_locals
)
4904 long storage_needed
;
4906 /* FIXME: It would be better to cache this table, rather
4907 than recreating it for each output section. */
4908 /* FIXME: This call is not working for non-ELF based targets.
4910 storage_needed
= bfd_get_symtab_upper_bound (link_info
.output_bfd
);
4911 if (storage_needed
> 0)
4913 asymbol
** symbol_table
;
4914 long number_of_symbols
;
4917 symbol_table
= xmalloc (storage_needed
);
4918 number_of_symbols
= bfd_canonicalize_symtab (link_info
.output_bfd
, symbol_table
);
4920 for (j
= 0; j
< number_of_symbols
; j
++)
4922 asymbol
* sym
= symbol_table
[j
];
4923 bfd_vma sym_addr
= sym
->value
+ i
->output_section
->vma
;
4925 if (sym
->section
== i
->output_section
4926 && (sym
->flags
& BSF_LOCAL
) != 0
4928 && sym_addr
< print_dot
4929 && ld_is_local_symbol (sym
))
4931 print_spaces (SECTION_NAME_MAP_LENGTH
);
4932 minfo ("0x%V (local) %s\n", sym_addr
, bfd_asymbol_name (sym
));
4936 free (symbol_table
);
4943 print_fill_statement (lang_fill_statement_type
*fill
)
4947 fputs (" FILL mask 0x", config
.map_file
);
4948 for (p
= fill
->fill
->data
, size
= fill
->fill
->size
; size
!= 0; p
++, size
--)
4949 fprintf (config
.map_file
, "%02x", *p
);
4950 fputs ("\n", config
.map_file
);
4954 print_data_statement (lang_data_statement_type
*data
)
4960 init_opb (data
->output_section
);
4961 print_spaces (SECTION_NAME_MAP_LENGTH
);
4963 addr
= data
->output_offset
;
4964 if (data
->output_section
!= NULL
)
4965 addr
+= data
->output_section
->vma
;
4993 if (size
< TO_SIZE ((unsigned) 1))
4994 size
= TO_SIZE ((unsigned) 1);
4995 minfo ("0x%V %W %s 0x%v", addr
, TO_ADDR (size
), name
, data
->value
);
4997 if (data
->exp
->type
.node_class
!= etree_value
)
5000 exp_print_tree (data
->exp
);
5005 print_dot
= addr
+ TO_ADDR (size
);
5008 /* Print an address statement. These are generated by options like
5012 print_address_statement (lang_address_statement_type
*address
)
5014 minfo (_("Address of section %s set to "), address
->section_name
);
5015 exp_print_tree (address
->address
);
5019 /* Print a reloc statement. */
5022 print_reloc_statement (lang_reloc_statement_type
*reloc
)
5027 init_opb (reloc
->output_section
);
5028 print_spaces (SECTION_NAME_MAP_LENGTH
);
5030 addr
= reloc
->output_offset
;
5031 if (reloc
->output_section
!= NULL
)
5032 addr
+= reloc
->output_section
->vma
;
5034 size
= bfd_get_reloc_size (reloc
->howto
);
5036 minfo ("0x%V %W RELOC %s ", addr
, TO_ADDR (size
), reloc
->howto
->name
);
5038 if (reloc
->name
!= NULL
)
5039 minfo ("%s+", reloc
->name
);
5041 minfo ("%s+", reloc
->section
->name
);
5043 exp_print_tree (reloc
->addend_exp
);
5047 print_dot
= addr
+ TO_ADDR (size
);
5051 print_padding_statement (lang_padding_statement_type
*s
)
5056 init_opb (s
->output_section
);
5059 len
= sizeof " *fill*" - 1;
5060 print_spaces (SECTION_NAME_MAP_LENGTH
- len
);
5062 addr
= s
->output_offset
;
5063 if (s
->output_section
!= NULL
)
5064 addr
+= s
->output_section
->vma
;
5065 minfo ("0x%V %W ", addr
, TO_ADDR (s
->size
));
5067 if (s
->fill
->size
!= 0)
5071 for (p
= s
->fill
->data
, size
= s
->fill
->size
; size
!= 0; p
++, size
--)
5072 fprintf (config
.map_file
, "%02x", *p
);
5077 print_dot
= addr
+ TO_ADDR (s
->size
);
5081 print_wild_statement (lang_wild_statement_type
*w
,
5082 lang_output_section_statement_type
*os
)
5084 struct wildcard_list
*sec
;
5088 if (w
->exclude_name_list
)
5091 minfo ("EXCLUDE_FILE(%s", w
->exclude_name_list
->name
);
5092 for (tmp
= w
->exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
5093 minfo (" %s", tmp
->name
);
5097 if (w
->filenames_sorted
)
5098 minfo ("SORT_BY_NAME(");
5099 if (w
->filename
!= NULL
)
5100 minfo ("%s", w
->filename
);
5103 if (w
->filenames_sorted
)
5107 for (sec
= w
->section_list
; sec
; sec
= sec
->next
)
5109 int closing_paren
= 0;
5111 switch (sec
->spec
.sorted
)
5117 minfo ("SORT_BY_NAME(");
5122 minfo ("SORT_BY_ALIGNMENT(");
5126 case by_name_alignment
:
5127 minfo ("SORT_BY_NAME(SORT_BY_ALIGNMENT(");
5131 case by_alignment_name
:
5132 minfo ("SORT_BY_ALIGNMENT(SORT_BY_NAME(");
5137 minfo ("SORT_NONE(");
5141 case by_init_priority
:
5142 minfo ("SORT_BY_INIT_PRIORITY(");
5147 if (sec
->spec
.exclude_name_list
!= NULL
)
5150 minfo ("EXCLUDE_FILE(%s", sec
->spec
.exclude_name_list
->name
);
5151 for (tmp
= sec
->spec
.exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
5152 minfo (" %s", tmp
->name
);
5155 if (sec
->spec
.name
!= NULL
)
5156 minfo ("%s", sec
->spec
.name
);
5159 for (;closing_paren
> 0; closing_paren
--)
5168 print_statement_list (w
->children
.head
, os
);
5171 /* Print a group statement. */
5174 print_group (lang_group_statement_type
*s
,
5175 lang_output_section_statement_type
*os
)
5177 fprintf (config
.map_file
, "START GROUP\n");
5178 print_statement_list (s
->children
.head
, os
);
5179 fprintf (config
.map_file
, "END GROUP\n");
5182 /* Print the list of statements in S.
5183 This can be called for any statement type. */
5186 print_statement_list (lang_statement_union_type
*s
,
5187 lang_output_section_statement_type
*os
)
5191 print_statement (s
, os
);
5196 /* Print the first statement in statement list S.
5197 This can be called for any statement type. */
5200 print_statement (lang_statement_union_type
*s
,
5201 lang_output_section_statement_type
*os
)
5203 switch (s
->header
.type
)
5206 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
5209 case lang_constructors_statement_enum
:
5210 if (constructor_list
.head
!= NULL
)
5212 if (constructors_sorted
)
5213 minfo (" SORT (CONSTRUCTORS)\n");
5215 minfo (" CONSTRUCTORS\n");
5216 print_statement_list (constructor_list
.head
, os
);
5219 case lang_wild_statement_enum
:
5220 print_wild_statement (&s
->wild_statement
, os
);
5222 case lang_address_statement_enum
:
5223 print_address_statement (&s
->address_statement
);
5225 case lang_object_symbols_statement_enum
:
5226 minfo (" CREATE_OBJECT_SYMBOLS\n");
5228 case lang_fill_statement_enum
:
5229 print_fill_statement (&s
->fill_statement
);
5231 case lang_data_statement_enum
:
5232 print_data_statement (&s
->data_statement
);
5234 case lang_reloc_statement_enum
:
5235 print_reloc_statement (&s
->reloc_statement
);
5237 case lang_input_section_enum
:
5238 print_input_section (s
->input_section
.section
, false);
5240 case lang_padding_statement_enum
:
5241 print_padding_statement (&s
->padding_statement
);
5243 case lang_output_section_statement_enum
:
5244 print_output_section_statement (&s
->output_section_statement
);
5246 case lang_assignment_statement_enum
:
5247 print_assignment (&s
->assignment_statement
, os
);
5249 case lang_target_statement_enum
:
5250 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
5252 case lang_output_statement_enum
:
5253 minfo ("OUTPUT(%s", s
->output_statement
.name
);
5254 if (output_target
!= NULL
)
5255 minfo (" %s", output_target
);
5258 case lang_input_statement_enum
:
5259 print_input_statement (&s
->input_statement
);
5261 case lang_group_statement_enum
:
5262 print_group (&s
->group_statement
, os
);
5264 case lang_insert_statement_enum
:
5265 minfo ("INSERT %s %s\n",
5266 s
->insert_statement
.is_before
? "BEFORE" : "AFTER",
5267 s
->insert_statement
.where
);
5273 print_statements (void)
5275 print_statement_list (statement_list
.head
, abs_output_section
);
5278 /* Print the first N statements in statement list S to STDERR.
5279 If N == 0, nothing is printed.
5280 If N < 0, the entire list is printed.
5281 Intended to be called from GDB. */
5284 dprint_statement (lang_statement_union_type
*s
, int n
)
5286 FILE *map_save
= config
.map_file
;
5288 config
.map_file
= stderr
;
5291 print_statement_list (s
, abs_output_section
);
5294 while (s
&& --n
>= 0)
5296 print_statement (s
, abs_output_section
);
5301 config
.map_file
= map_save
;
5305 insert_pad (lang_statement_union_type
**ptr
,
5307 bfd_size_type alignment_needed
,
5308 asection
*output_section
,
5311 static fill_type zero_fill
;
5312 lang_statement_union_type
*pad
= NULL
;
5314 if (ptr
!= &statement_list
.head
)
5315 pad
= ((lang_statement_union_type
*)
5316 ((char *) ptr
- offsetof (lang_statement_union_type
, header
.next
)));
5318 && pad
->header
.type
== lang_padding_statement_enum
5319 && pad
->padding_statement
.output_section
== output_section
)
5321 /* Use the existing pad statement. */
5323 else if ((pad
= *ptr
) != NULL
5324 && pad
->header
.type
== lang_padding_statement_enum
5325 && pad
->padding_statement
.output_section
== output_section
)
5327 /* Use the existing pad statement. */
5331 /* Make a new padding statement, linked into existing chain. */
5332 pad
= stat_alloc (sizeof (lang_padding_statement_type
));
5333 pad
->header
.next
= *ptr
;
5335 pad
->header
.type
= lang_padding_statement_enum
;
5336 pad
->padding_statement
.output_section
= output_section
;
5339 pad
->padding_statement
.fill
= fill
;
5341 pad
->padding_statement
.output_offset
= dot
- output_section
->vma
;
5342 pad
->padding_statement
.size
= alignment_needed
;
5343 if (!(output_section
->flags
& SEC_FIXED_SIZE
))
5344 output_section
->size
= TO_SIZE (dot
+ TO_ADDR (alignment_needed
)
5345 - output_section
->vma
);
5348 /* Work out how much this section will move the dot point. */
5352 (lang_statement_union_type
**this_ptr
,
5353 lang_output_section_statement_type
*output_section_statement
,
5358 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
5359 asection
*i
= is
->section
;
5360 asection
*o
= output_section_statement
->bfd_section
;
5363 if (link_info
.non_contiguous_regions
)
5365 /* If the input section I has already been successfully assigned
5366 to an output section other than O, don't bother with it and
5367 let the caller remove it from the list. Keep processing in
5368 case we have already handled O, because the repeated passes
5369 have reinitialized its size. */
5370 if (i
->already_assigned
&& i
->already_assigned
!= o
)
5377 if (i
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
5378 i
->output_offset
= i
->vma
- o
->vma
;
5379 else if (((i
->flags
& SEC_EXCLUDE
) != 0)
5380 || output_section_statement
->ignored
)
5381 i
->output_offset
= dot
- o
->vma
;
5384 bfd_size_type alignment_needed
;
5386 /* Align this section first to the input sections requirement,
5387 then to the output section's requirement. If this alignment
5388 is greater than any seen before, then record it too. Perform
5389 the alignment by inserting a magic 'padding' statement. */
5391 if (output_section_statement
->subsection_alignment
!= NULL
)
5393 = exp_get_power (output_section_statement
->subsection_alignment
,
5394 "subsection alignment");
5396 if (o
->alignment_power
< i
->alignment_power
)
5397 o
->alignment_power
= i
->alignment_power
;
5399 alignment_needed
= align_power (dot
, i
->alignment_power
) - dot
;
5401 if (alignment_needed
!= 0)
5403 insert_pad (this_ptr
, fill
, TO_SIZE (alignment_needed
), o
, dot
);
5404 dot
+= alignment_needed
;
5407 if (link_info
.non_contiguous_regions
)
5409 /* If I would overflow O, let the caller remove I from the
5411 if (output_section_statement
->region
)
5413 bfd_vma end
= output_section_statement
->region
->origin
5414 + output_section_statement
->region
->length
;
5416 if (dot
+ TO_ADDR (i
->size
) > end
)
5418 if (i
->flags
& SEC_LINKER_CREATED
)
5419 einfo (_("%F%P: Output section `%pA' not large enough for "
5420 "the linker-created stubs section `%pA'.\n"),
5421 i
->output_section
, i
);
5423 if (i
->rawsize
&& i
->rawsize
!= i
->size
)
5424 einfo (_("%F%P: Relaxation not supported with "
5425 "--enable-non-contiguous-regions (section `%pA' "
5426 "would overflow `%pA' after it changed size).\n"),
5427 i
, i
->output_section
);
5431 i
->output_section
= NULL
;
5437 /* Remember where in the output section this input section goes. */
5438 i
->output_offset
= dot
- o
->vma
;
5440 /* Mark how big the output section must be to contain this now. */
5441 dot
+= TO_ADDR (i
->size
);
5442 if (!(o
->flags
& SEC_FIXED_SIZE
))
5443 o
->size
= TO_SIZE (dot
- o
->vma
);
5445 if (link_info
.non_contiguous_regions
)
5447 /* Record that I was successfully assigned to O, and update
5448 its actual output section too. */
5449 i
->already_assigned
= o
;
5450 i
->output_section
= o
;
5464 sort_sections_by_lma (const void *arg1
, const void *arg2
)
5466 const asection
*sec1
= ((const struct check_sec
*) arg1
)->sec
;
5467 const asection
*sec2
= ((const struct check_sec
*) arg2
)->sec
;
5469 if (sec1
->lma
< sec2
->lma
)
5471 else if (sec1
->lma
> sec2
->lma
)
5473 else if (sec1
->id
< sec2
->id
)
5475 else if (sec1
->id
> sec2
->id
)
5482 sort_sections_by_vma (const void *arg1
, const void *arg2
)
5484 const asection
*sec1
= ((const struct check_sec
*) arg1
)->sec
;
5485 const asection
*sec2
= ((const struct check_sec
*) arg2
)->sec
;
5487 if (sec1
->vma
< sec2
->vma
)
5489 else if (sec1
->vma
> sec2
->vma
)
5491 else if (sec1
->id
< sec2
->id
)
5493 else if (sec1
->id
> sec2
->id
)
5499 #define IS_TBSS(s) \
5500 ((s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == SEC_THREAD_LOCAL)
5502 #define IGNORE_SECTION(s) \
5503 ((s->flags & SEC_ALLOC) == 0 || IS_TBSS (s))
5505 /* Check to see if any allocated sections overlap with other allocated
5506 sections. This can happen if a linker script specifies the output
5507 section addresses of the two sections. Also check whether any memory
5508 region has overflowed. */
5511 lang_check_section_addresses (void)
5514 struct check_sec
*sections
;
5519 bfd_vma p_start
= 0;
5521 lang_memory_region_type
*m
;
5524 /* Detect address space overflow on allocated sections. */
5525 addr_mask
= ((bfd_vma
) 1 <<
5526 (bfd_arch_bits_per_address (link_info
.output_bfd
) - 1)) - 1;
5527 addr_mask
= (addr_mask
<< 1) + 1;
5528 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5529 if ((s
->flags
& SEC_ALLOC
) != 0)
5531 s_end
= (s
->vma
+ s
->size
) & addr_mask
;
5532 if (s_end
!= 0 && s_end
< (s
->vma
& addr_mask
))
5533 einfo (_("%X%P: section %s VMA wraps around address space\n"),
5537 s_end
= (s
->lma
+ s
->size
) & addr_mask
;
5538 if (s_end
!= 0 && s_end
< (s
->lma
& addr_mask
))
5539 einfo (_("%X%P: section %s LMA wraps around address space\n"),
5544 if (bfd_count_sections (link_info
.output_bfd
) <= 1)
5547 count
= bfd_count_sections (link_info
.output_bfd
);
5548 sections
= XNEWVEC (struct check_sec
, count
);
5550 /* Scan all sections in the output list. */
5552 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5554 if (IGNORE_SECTION (s
)
5558 sections
[count
].sec
= s
;
5559 sections
[count
].warned
= false;
5569 qsort (sections
, count
, sizeof (*sections
), sort_sections_by_lma
);
5571 /* First check section LMAs. There should be no overlap of LMAs on
5572 loadable sections, even with overlays. */
5573 for (p
= NULL
, i
= 0; i
< count
; i
++)
5575 s
= sections
[i
].sec
;
5577 if ((s
->flags
& SEC_LOAD
) != 0)
5580 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
5582 /* Look for an overlap. We have sorted sections by lma, so
5583 we know that s_start >= p_start. Besides the obvious
5584 case of overlap when the current section starts before
5585 the previous one ends, we also must have overlap if the
5586 previous section wraps around the address space. */
5588 && (s_start
<= p_end
5589 || p_end
< p_start
))
5591 einfo (_("%X%P: section %s LMA [%V,%V]"
5592 " overlaps section %s LMA [%V,%V]\n"),
5593 s
->name
, s_start
, s_end
, p
->name
, p_start
, p_end
);
5594 sections
[i
].warned
= true;
5602 /* If any non-zero size allocated section (excluding tbss) starts at
5603 exactly the same VMA as another such section, then we have
5604 overlays. Overlays generated by the OVERLAY keyword will have
5605 this property. It is possible to intentionally generate overlays
5606 that fail this test, but it would be unusual. */
5607 qsort (sections
, count
, sizeof (*sections
), sort_sections_by_vma
);
5609 p_start
= sections
[0].sec
->vma
;
5610 for (i
= 1; i
< count
; i
++)
5612 s_start
= sections
[i
].sec
->vma
;
5613 if (p_start
== s_start
)
5621 /* Now check section VMAs if no overlays were detected. */
5624 for (p
= NULL
, i
= 0; i
< count
; i
++)
5626 s
= sections
[i
].sec
;
5629 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
5632 && !sections
[i
].warned
5633 && (s_start
<= p_end
5634 || p_end
< p_start
))
5635 einfo (_("%X%P: section %s VMA [%V,%V]"
5636 " overlaps section %s VMA [%V,%V]\n"),
5637 s
->name
, s_start
, s_end
, p
->name
, p_start
, p_end
);
5646 /* If any memory region has overflowed, report by how much.
5647 We do not issue this diagnostic for regions that had sections
5648 explicitly placed outside their bounds; os_region_check's
5649 diagnostics are adequate for that case.
5651 FIXME: It is conceivable that m->current - (m->origin + m->length)
5652 might overflow a 32-bit integer. There is, alas, no way to print
5653 a bfd_vma quantity in decimal. */
5654 for (m
= lang_memory_region_list
; m
; m
= m
->next
)
5655 if (m
->had_full_message
)
5657 unsigned long over
= m
->current
- (m
->origin
+ m
->length
);
5658 einfo (ngettext ("%X%P: region `%s' overflowed by %lu byte\n",
5659 "%X%P: region `%s' overflowed by %lu bytes\n",
5661 m
->name_list
.name
, over
);
5665 /* Make sure the new address is within the region. We explicitly permit the
5666 current address to be at the exact end of the region when the address is
5667 non-zero, in case the region is at the end of addressable memory and the
5668 calculation wraps around. */
5671 os_region_check (lang_output_section_statement_type
*os
,
5672 lang_memory_region_type
*region
,
5676 if ((region
->current
< region
->origin
5677 || (region
->current
- region
->origin
> region
->length
))
5678 && ((region
->current
!= region
->origin
+ region
->length
)
5683 einfo (_("%X%P: address 0x%v of %pB section `%s'"
5684 " is not within region `%s'\n"),
5686 os
->bfd_section
->owner
,
5687 os
->bfd_section
->name
,
5688 region
->name_list
.name
);
5690 else if (!region
->had_full_message
)
5692 region
->had_full_message
= true;
5694 einfo (_("%X%P: %pB section `%s' will not fit in region `%s'\n"),
5695 os
->bfd_section
->owner
,
5696 os
->bfd_section
->name
,
5697 region
->name_list
.name
);
5703 ldlang_check_relro_region (lang_statement_union_type
*s
)
5705 seg_align_type
*seg
= &expld
.dataseg
;
5707 if (seg
->relro
== exp_seg_relro_start
)
5709 if (!seg
->relro_start_stat
)
5710 seg
->relro_start_stat
= s
;
5713 ASSERT (seg
->relro_start_stat
== s
);
5716 else if (seg
->relro
== exp_seg_relro_end
)
5718 if (!seg
->relro_end_stat
)
5719 seg
->relro_end_stat
= s
;
5722 ASSERT (seg
->relro_end_stat
== s
);
5727 /* Set the sizes for all the output sections. */
5730 lang_size_sections_1
5731 (lang_statement_union_type
**prev
,
5732 lang_output_section_statement_type
*output_section_statement
,
5738 lang_statement_union_type
*s
;
5739 lang_statement_union_type
*prev_s
= NULL
;
5740 bool removed_prev_s
= false;
5742 /* Size up the sections from their constituent parts. */
5743 for (s
= *prev
; s
!= NULL
; prev_s
= s
, s
= s
->header
.next
)
5745 bool removed
= false;
5747 switch (s
->header
.type
)
5749 case lang_output_section_statement_enum
:
5751 bfd_vma newdot
, after
, dotdelta
;
5752 lang_output_section_statement_type
*os
;
5753 lang_memory_region_type
*r
;
5754 int section_alignment
= 0;
5756 os
= &s
->output_section_statement
;
5757 init_opb (os
->bfd_section
);
5758 if (os
->constraint
== -1)
5761 /* FIXME: We shouldn't need to zero section vmas for ld -r
5762 here, in lang_insert_orphan, or in the default linker scripts.
5763 This is covering for coff backend linker bugs. See PR6945. */
5764 if (os
->addr_tree
== NULL
5765 && bfd_link_relocatable (&link_info
)
5766 && (bfd_get_flavour (link_info
.output_bfd
)
5767 == bfd_target_coff_flavour
))
5768 os
->addr_tree
= exp_intop (0);
5769 if (os
->addr_tree
!= NULL
)
5771 exp_fold_tree (os
->addr_tree
, bfd_abs_section_ptr
, &dot
);
5773 if (expld
.result
.valid_p
)
5775 dot
= expld
.result
.value
;
5776 if (expld
.result
.section
!= NULL
)
5777 dot
+= expld
.result
.section
->vma
;
5779 else if (expld
.phase
!= lang_mark_phase_enum
)
5780 einfo (_("%F%P:%pS: non constant or forward reference"
5781 " address expression for section %s\n"),
5782 os
->addr_tree
, os
->name
);
5785 if (os
->bfd_section
== NULL
)
5786 /* This section was removed or never actually created. */
5789 /* If this is a COFF shared library section, use the size and
5790 address from the input section. FIXME: This is COFF
5791 specific; it would be cleaner if there were some other way
5792 to do this, but nothing simple comes to mind. */
5793 if (((bfd_get_flavour (link_info
.output_bfd
)
5794 == bfd_target_ecoff_flavour
)
5795 || (bfd_get_flavour (link_info
.output_bfd
)
5796 == bfd_target_coff_flavour
))
5797 && (os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
5801 if (os
->children
.head
== NULL
5802 || os
->children
.head
->header
.next
!= NULL
5803 || (os
->children
.head
->header
.type
5804 != lang_input_section_enum
))
5805 einfo (_("%X%P: internal error on COFF shared library"
5806 " section %s\n"), os
->name
);
5808 input
= os
->children
.head
->input_section
.section
;
5809 bfd_set_section_vma (os
->bfd_section
,
5810 bfd_section_vma (input
));
5811 if (!(os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
5812 os
->bfd_section
->size
= input
->size
;
5818 if (bfd_is_abs_section (os
->bfd_section
))
5820 /* No matter what happens, an abs section starts at zero. */
5821 ASSERT (os
->bfd_section
->vma
== 0);
5825 if (os
->addr_tree
== NULL
)
5827 /* No address specified for this section, get one
5828 from the region specification. */
5829 if (os
->region
== NULL
5830 || ((os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))
5831 && os
->region
->name_list
.name
[0] == '*'
5832 && strcmp (os
->region
->name_list
.name
,
5833 DEFAULT_MEMORY_REGION
) == 0))
5835 os
->region
= lang_memory_default (os
->bfd_section
);
5838 /* If a loadable section is using the default memory
5839 region, and some non default memory regions were
5840 defined, issue an error message. */
5842 && !IGNORE_SECTION (os
->bfd_section
)
5843 && !bfd_link_relocatable (&link_info
)
5845 && strcmp (os
->region
->name_list
.name
,
5846 DEFAULT_MEMORY_REGION
) == 0
5847 && lang_memory_region_list
!= NULL
5848 && (strcmp (lang_memory_region_list
->name_list
.name
,
5849 DEFAULT_MEMORY_REGION
) != 0
5850 || lang_memory_region_list
->next
!= NULL
)
5851 && lang_sizing_iteration
== 1)
5853 /* By default this is an error rather than just a
5854 warning because if we allocate the section to the
5855 default memory region we can end up creating an
5856 excessively large binary, or even seg faulting when
5857 attempting to perform a negative seek. See
5858 sources.redhat.com/ml/binutils/2003-04/msg00423.html
5859 for an example of this. This behaviour can be
5860 overridden by the using the --no-check-sections
5862 if (command_line
.check_section_addresses
)
5863 einfo (_("%F%P: error: no memory region specified"
5864 " for loadable section `%s'\n"),
5865 bfd_section_name (os
->bfd_section
));
5867 einfo (_("%P: warning: no memory region specified"
5868 " for loadable section `%s'\n"),
5869 bfd_section_name (os
->bfd_section
));
5872 newdot
= os
->region
->current
;
5873 section_alignment
= os
->bfd_section
->alignment_power
;
5876 section_alignment
= exp_get_power (os
->section_alignment
,
5877 "section alignment");
5879 /* Align to what the section needs. */
5880 if (section_alignment
> 0)
5882 bfd_vma savedot
= newdot
;
5885 newdot
= align_power (newdot
, section_alignment
);
5886 dotdelta
= newdot
- savedot
;
5888 if (lang_sizing_iteration
== 1)
5890 else if (lang_sizing_iteration
> 1)
5892 /* Only report adjustments that would change
5893 alignment from what we have already reported. */
5894 diff
= newdot
- os
->bfd_section
->vma
;
5895 if (!(diff
& (((bfd_vma
) 1 << section_alignment
) - 1)))
5899 && (config
.warn_section_align
5900 || os
->addr_tree
!= NULL
))
5901 einfo (_("%P: warning: "
5902 "start of section %s changed by %ld\n"),
5903 os
->name
, (long) diff
);
5906 bfd_set_section_vma (os
->bfd_section
, newdot
);
5908 os
->bfd_section
->output_offset
= 0;
5911 lang_size_sections_1 (&os
->children
.head
, os
,
5912 os
->fill
, newdot
, relax
, check_regions
);
5914 os
->processed_vma
= true;
5916 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
5917 /* Except for some special linker created sections,
5918 no output section should change from zero size
5919 after strip_excluded_output_sections. A non-zero
5920 size on an ignored section indicates that some
5921 input section was not sized early enough. */
5922 ASSERT (os
->bfd_section
->size
== 0);
5925 dot
= os
->bfd_section
->vma
;
5927 /* Put the section within the requested block size, or
5928 align at the block boundary. */
5930 + TO_ADDR (os
->bfd_section
->size
)
5931 + os
->block_value
- 1)
5932 & - (bfd_vma
) os
->block_value
);
5934 if (!(os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
5935 os
->bfd_section
->size
= TO_SIZE (after
5936 - os
->bfd_section
->vma
);
5939 /* Set section lma. */
5942 r
= lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, false);
5946 bfd_vma lma
= exp_get_abs_int (os
->load_base
, 0, "load base");
5947 os
->bfd_section
->lma
= lma
;
5949 else if (os
->lma_region
!= NULL
)
5951 bfd_vma lma
= os
->lma_region
->current
;
5953 if (os
->align_lma_with_input
)
5957 /* When LMA_REGION is the same as REGION, align the LMA
5958 as we did for the VMA, possibly including alignment
5959 from the bfd section. If a different region, then
5960 only align according to the value in the output
5962 if (os
->lma_region
!= os
->region
)
5963 section_alignment
= exp_get_power (os
->section_alignment
,
5964 "section alignment");
5965 if (section_alignment
> 0)
5966 lma
= align_power (lma
, section_alignment
);
5968 os
->bfd_section
->lma
= lma
;
5970 else if (r
->last_os
!= NULL
5971 && (os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
5976 last
= r
->last_os
->output_section_statement
.bfd_section
;
5978 /* A backwards move of dot should be accompanied by
5979 an explicit assignment to the section LMA (ie.
5980 os->load_base set) because backwards moves can
5981 create overlapping LMAs. */
5983 && os
->bfd_section
->size
!= 0
5984 && dot
+ TO_ADDR (os
->bfd_section
->size
) <= last
->vma
)
5986 /* If dot moved backwards then leave lma equal to
5987 vma. This is the old default lma, which might
5988 just happen to work when the backwards move is
5989 sufficiently large. Nag if this changes anything,
5990 so people can fix their linker scripts. */
5992 if (last
->vma
!= last
->lma
)
5993 einfo (_("%P: warning: dot moved backwards "
5994 "before `%s'\n"), os
->name
);
5998 /* If this is an overlay, set the current lma to that
5999 at the end of the previous section. */
6000 if (os
->sectype
== overlay_section
)
6001 lma
= last
->lma
+ TO_ADDR (last
->size
);
6003 /* Otherwise, keep the same lma to vma relationship
6004 as the previous section. */
6006 lma
= os
->bfd_section
->vma
+ last
->lma
- last
->vma
;
6008 if (section_alignment
> 0)
6009 lma
= align_power (lma
, section_alignment
);
6010 os
->bfd_section
->lma
= lma
;
6013 os
->processed_lma
= true;
6015 /* Keep track of normal sections using the default
6016 lma region. We use this to set the lma for
6017 following sections. Overlays or other linker
6018 script assignment to lma might mean that the
6019 default lma == vma is incorrect.
6020 To avoid warnings about dot moving backwards when using
6021 -Ttext, don't start tracking sections until we find one
6022 of non-zero size or with lma set differently to vma.
6023 Do this tracking before we short-cut the loop so that we
6024 track changes for the case where the section size is zero,
6025 but the lma is set differently to the vma. This is
6026 important, if an orphan section is placed after an
6027 otherwise empty output section that has an explicit lma
6028 set, we want that lma reflected in the orphans lma. */
6029 if (((!IGNORE_SECTION (os
->bfd_section
)
6030 && (os
->bfd_section
->size
!= 0
6031 || (r
->last_os
== NULL
6032 && os
->bfd_section
->vma
!= os
->bfd_section
->lma
)
6033 || (r
->last_os
!= NULL
6034 && dot
>= (r
->last_os
->output_section_statement
6035 .bfd_section
->vma
))))
6036 || os
->sectype
== first_overlay_section
)
6037 && os
->lma_region
== NULL
6038 && !bfd_link_relocatable (&link_info
))
6041 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
6044 /* .tbss sections effectively have zero size. */
6045 if (!IS_TBSS (os
->bfd_section
)
6046 || bfd_link_relocatable (&link_info
))
6047 dotdelta
= TO_ADDR (os
->bfd_section
->size
);
6052 if (os
->update_dot_tree
!= 0)
6053 exp_fold_tree (os
->update_dot_tree
, bfd_abs_section_ptr
, &dot
);
6055 /* Update dot in the region ?
6056 We only do this if the section is going to be allocated,
6057 since unallocated sections do not contribute to the region's
6058 overall size in memory. */
6059 if (os
->region
!= NULL
6060 && (os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
)))
6062 os
->region
->current
= dot
;
6065 /* Make sure the new address is within the region. */
6066 os_region_check (os
, os
->region
, os
->addr_tree
,
6067 os
->bfd_section
->vma
);
6069 if (os
->lma_region
!= NULL
&& os
->lma_region
!= os
->region
6070 && ((os
->bfd_section
->flags
& SEC_LOAD
)
6071 || os
->align_lma_with_input
))
6073 os
->lma_region
->current
= os
->bfd_section
->lma
+ dotdelta
;
6076 os_region_check (os
, os
->lma_region
, NULL
,
6077 os
->bfd_section
->lma
);
6083 case lang_constructors_statement_enum
:
6084 dot
= lang_size_sections_1 (&constructor_list
.head
,
6085 output_section_statement
,
6086 fill
, dot
, relax
, check_regions
);
6089 case lang_data_statement_enum
:
6091 unsigned int size
= 0;
6093 s
->data_statement
.output_offset
=
6094 dot
- output_section_statement
->bfd_section
->vma
;
6095 s
->data_statement
.output_section
=
6096 output_section_statement
->bfd_section
;
6098 /* We might refer to provided symbols in the expression, and
6099 need to mark them as needed. */
6100 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
6102 switch (s
->data_statement
.type
)
6120 if (size
< TO_SIZE ((unsigned) 1))
6121 size
= TO_SIZE ((unsigned) 1);
6122 dot
+= TO_ADDR (size
);
6123 if (!(output_section_statement
->bfd_section
->flags
6125 output_section_statement
->bfd_section
->size
6126 = TO_SIZE (dot
- output_section_statement
->bfd_section
->vma
);
6131 case lang_reloc_statement_enum
:
6135 s
->reloc_statement
.output_offset
=
6136 dot
- output_section_statement
->bfd_section
->vma
;
6137 s
->reloc_statement
.output_section
=
6138 output_section_statement
->bfd_section
;
6139 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
6140 dot
+= TO_ADDR (size
);
6141 if (!(output_section_statement
->bfd_section
->flags
6143 output_section_statement
->bfd_section
->size
6144 = TO_SIZE (dot
- output_section_statement
->bfd_section
->vma
);
6148 case lang_wild_statement_enum
:
6149 dot
= lang_size_sections_1 (&s
->wild_statement
.children
.head
,
6150 output_section_statement
,
6151 fill
, dot
, relax
, check_regions
);
6154 case lang_object_symbols_statement_enum
:
6155 link_info
.create_object_symbols_section
6156 = output_section_statement
->bfd_section
;
6157 output_section_statement
->bfd_section
->flags
|= SEC_KEEP
;
6160 case lang_output_statement_enum
:
6161 case lang_target_statement_enum
:
6164 case lang_input_section_enum
:
6168 i
= s
->input_section
.section
;
6173 if (!bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
6174 einfo (_("%F%P: can't relax section: %E\n"));
6178 dot
= size_input_section (prev
, output_section_statement
,
6179 fill
, &removed
, dot
);
6183 case lang_input_statement_enum
:
6186 case lang_fill_statement_enum
:
6187 s
->fill_statement
.output_section
=
6188 output_section_statement
->bfd_section
;
6190 fill
= s
->fill_statement
.fill
;
6193 case lang_assignment_statement_enum
:
6195 bfd_vma newdot
= dot
;
6196 etree_type
*tree
= s
->assignment_statement
.exp
;
6198 expld
.dataseg
.relro
= exp_seg_relro_none
;
6200 exp_fold_tree (tree
,
6201 output_section_statement
->bfd_section
,
6204 ldlang_check_relro_region (s
);
6206 expld
.dataseg
.relro
= exp_seg_relro_none
;
6208 /* This symbol may be relative to this section. */
6209 if ((tree
->type
.node_class
== etree_provided
6210 || tree
->type
.node_class
== etree_assign
)
6211 && (tree
->assign
.dst
[0] != '.'
6212 || tree
->assign
.dst
[1] != '\0'))
6213 output_section_statement
->update_dot
= 1;
6215 if (!output_section_statement
->ignored
)
6217 if (output_section_statement
== abs_output_section
)
6219 /* If we don't have an output section, then just adjust
6220 the default memory address. */
6221 lang_memory_region_lookup (DEFAULT_MEMORY_REGION
,
6222 false)->current
= newdot
;
6224 else if (newdot
!= dot
)
6226 /* Insert a pad after this statement. We can't
6227 put the pad before when relaxing, in case the
6228 assignment references dot. */
6229 insert_pad (&s
->header
.next
, fill
, TO_SIZE (newdot
- dot
),
6230 output_section_statement
->bfd_section
, dot
);
6232 /* Don't neuter the pad below when relaxing. */
6235 /* If dot is advanced, this implies that the section
6236 should have space allocated to it, unless the
6237 user has explicitly stated that the section
6238 should not be allocated. */
6239 if (output_section_statement
->sectype
!= noalloc_section
6240 && (output_section_statement
->sectype
!= noload_section
6241 || (bfd_get_flavour (link_info
.output_bfd
)
6242 == bfd_target_elf_flavour
)))
6243 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
;
6250 case lang_padding_statement_enum
:
6251 /* If this is the first time lang_size_sections is called,
6252 we won't have any padding statements. If this is the
6253 second or later passes when relaxing, we should allow
6254 padding to shrink. If padding is needed on this pass, it
6255 will be added back in. */
6256 s
->padding_statement
.size
= 0;
6258 /* Make sure output_offset is valid. If relaxation shrinks
6259 the section and this pad isn't needed, it's possible to
6260 have output_offset larger than the final size of the
6261 section. bfd_set_section_contents will complain even for
6262 a pad size of zero. */
6263 s
->padding_statement
.output_offset
6264 = dot
- output_section_statement
->bfd_section
->vma
;
6267 case lang_group_statement_enum
:
6268 dot
= lang_size_sections_1 (&s
->group_statement
.children
.head
,
6269 output_section_statement
,
6270 fill
, dot
, relax
, check_regions
);
6273 case lang_insert_statement_enum
:
6276 /* We can only get here when relaxing is turned on. */
6277 case lang_address_statement_enum
:
6285 /* If an input section doesn't fit in the current output
6286 section, remove it from the list. Handle the case where we
6287 have to remove an input_section statement here: there is a
6288 special case to remove the first element of the list. */
6289 if (link_info
.non_contiguous_regions
&& removed
)
6291 /* If we removed the first element during the previous
6292 iteration, override the loop assignment of prev_s. */
6298 /* If there was a real previous input section, just skip
6300 prev_s
->header
.next
=s
->header
.next
;
6302 removed_prev_s
= false;
6306 /* Remove the first input section of the list. */
6307 *prev
= s
->header
.next
;
6308 removed_prev_s
= true;
6311 /* Move to next element, unless we removed the head of the
6313 if (!removed_prev_s
)
6314 prev
= &s
->header
.next
;
6318 prev
= &s
->header
.next
;
6319 removed_prev_s
= false;
6325 /* Callback routine that is used in _bfd_elf_map_sections_to_segments.
6326 The BFD library has set NEW_SEGMENT to TRUE iff it thinks that
6327 CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different
6328 segments. We are allowed an opportunity to override this decision. */
6331 ldlang_override_segment_assignment (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6332 bfd
*abfd ATTRIBUTE_UNUSED
,
6333 asection
*current_section
,
6334 asection
*previous_section
,
6337 lang_output_section_statement_type
*cur
;
6338 lang_output_section_statement_type
*prev
;
6340 /* The checks below are only necessary when the BFD library has decided
6341 that the two sections ought to be placed into the same segment. */
6345 /* Paranoia checks. */
6346 if (current_section
== NULL
|| previous_section
== NULL
)
6349 /* If this flag is set, the target never wants code and non-code
6350 sections comingled in the same segment. */
6351 if (config
.separate_code
6352 && ((current_section
->flags
^ previous_section
->flags
) & SEC_CODE
))
6355 /* Find the memory regions associated with the two sections.
6356 We call lang_output_section_find() here rather than scanning the list
6357 of output sections looking for a matching section pointer because if
6358 we have a large number of sections then a hash lookup is faster. */
6359 cur
= lang_output_section_find (current_section
->name
);
6360 prev
= lang_output_section_find (previous_section
->name
);
6362 /* More paranoia. */
6363 if (cur
== NULL
|| prev
== NULL
)
6366 /* If the regions are different then force the sections to live in
6367 different segments. See the email thread starting at the following
6368 URL for the reasons why this is necessary:
6369 http://sourceware.org/ml/binutils/2007-02/msg00216.html */
6370 return cur
->region
!= prev
->region
;
6374 one_lang_size_sections_pass (bool *relax
, bool check_regions
)
6376 lang_statement_iteration
++;
6377 if (expld
.phase
!= lang_mark_phase_enum
)
6378 lang_sizing_iteration
++;
6379 lang_size_sections_1 (&statement_list
.head
, abs_output_section
,
6380 0, 0, relax
, check_regions
);
6384 lang_size_segment (void)
6386 /* If XXX_SEGMENT_ALIGN XXX_SEGMENT_END pair was seen, check whether
6387 a page could be saved in the data segment. */
6388 seg_align_type
*seg
= &expld
.dataseg
;
6389 bfd_vma first
, last
;
6391 first
= -seg
->base
& (seg
->commonpagesize
- 1);
6392 last
= seg
->end
& (seg
->commonpagesize
- 1);
6394 && ((seg
->base
& ~(seg
->commonpagesize
- 1))
6395 != (seg
->end
& ~(seg
->commonpagesize
- 1)))
6396 && first
+ last
<= seg
->commonpagesize
)
6398 seg
->phase
= exp_seg_adjust
;
6402 seg
->phase
= exp_seg_done
;
6407 lang_size_relro_segment_1 (void)
6409 seg_align_type
*seg
= &expld
.dataseg
;
6410 bfd_vma relro_end
, desired_end
;
6413 /* Compute the expected PT_GNU_RELRO/PT_LOAD segment end. */
6414 relro_end
= (seg
->relro_end
+ seg
->relropagesize
- 1) & -seg
->relropagesize
;
6416 /* Adjust by the offset arg of XXX_SEGMENT_RELRO_END. */
6417 desired_end
= relro_end
- seg
->relro_offset
;
6419 /* For sections in the relro segment.. */
6420 for (sec
= link_info
.output_bfd
->section_last
; sec
; sec
= sec
->prev
)
6421 if ((sec
->flags
& SEC_ALLOC
) != 0
6422 && sec
->vma
>= seg
->base
6423 && sec
->vma
< seg
->relro_end
- seg
->relro_offset
)
6425 /* Where do we want to put this section so that it ends as
6427 bfd_vma start
, end
, bump
;
6429 end
= start
= sec
->vma
;
6431 end
+= TO_ADDR (sec
->size
);
6432 bump
= desired_end
- end
;
6433 /* We'd like to increase START by BUMP, but we must heed
6434 alignment so the increase might be less than optimum. */
6436 start
&= ~(((bfd_vma
) 1 << sec
->alignment_power
) - 1);
6437 /* This is now the desired end for the previous section. */
6438 desired_end
= start
;
6441 seg
->phase
= exp_seg_relro_adjust
;
6442 ASSERT (desired_end
>= seg
->base
);
6443 seg
->base
= desired_end
;
6448 lang_size_relro_segment (bool *relax
, bool check_regions
)
6450 bool do_reset
= false;
6452 if (link_info
.relro
&& expld
.dataseg
.relro_end
)
6454 bfd_vma data_initial_base
= expld
.dataseg
.base
;
6455 bfd_vma data_relro_end
= lang_size_relro_segment_1 ();
6457 lang_reset_memory_regions ();
6458 one_lang_size_sections_pass (relax
, check_regions
);
6460 /* Assignments to dot, or to output section address in a user
6461 script have increased padding over the original. Revert. */
6462 if (expld
.dataseg
.relro_end
> data_relro_end
)
6464 expld
.dataseg
.base
= data_initial_base
;
6468 else if (lang_size_segment ())
6475 lang_size_sections (bool *relax
, bool check_regions
)
6477 expld
.phase
= lang_allocating_phase_enum
;
6478 expld
.dataseg
.phase
= exp_seg_none
;
6480 one_lang_size_sections_pass (relax
, check_regions
);
6482 if (expld
.dataseg
.phase
!= exp_seg_end_seen
)
6483 expld
.dataseg
.phase
= exp_seg_done
;
6485 if (expld
.dataseg
.phase
== exp_seg_end_seen
)
6488 = lang_size_relro_segment (relax
, check_regions
);
6492 lang_reset_memory_regions ();
6493 one_lang_size_sections_pass (relax
, check_regions
);
6496 if (link_info
.relro
&& expld
.dataseg
.relro_end
)
6498 link_info
.relro_start
= expld
.dataseg
.base
;
6499 link_info
.relro_end
= expld
.dataseg
.relro_end
;
6504 static lang_output_section_statement_type
*current_section
;
6505 static lang_assignment_statement_type
*current_assign
;
6506 static bool prefer_next_section
;
6508 /* Worker function for lang_do_assignments. Recursiveness goes here. */
6511 lang_do_assignments_1 (lang_statement_union_type
*s
,
6512 lang_output_section_statement_type
*current_os
,
6517 for (; s
!= NULL
; s
= s
->header
.next
)
6519 switch (s
->header
.type
)
6521 case lang_constructors_statement_enum
:
6522 dot
= lang_do_assignments_1 (constructor_list
.head
,
6523 current_os
, fill
, dot
, found_end
);
6526 case lang_output_section_statement_enum
:
6528 lang_output_section_statement_type
*os
;
6531 os
= &(s
->output_section_statement
);
6532 os
->after_end
= *found_end
;
6533 init_opb (os
->bfd_section
);
6535 if (os
->bfd_section
!= NULL
)
6537 if (!os
->ignored
&& (os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
6539 current_section
= os
;
6540 prefer_next_section
= false;
6542 newdot
= os
->bfd_section
->vma
;
6544 newdot
= lang_do_assignments_1 (os
->children
.head
,
6545 os
, os
->fill
, newdot
, found_end
);
6548 if (os
->bfd_section
!= NULL
)
6550 newdot
= os
->bfd_section
->vma
;
6552 /* .tbss sections effectively have zero size. */
6553 if (!IS_TBSS (os
->bfd_section
)
6554 || bfd_link_relocatable (&link_info
))
6555 newdot
+= TO_ADDR (os
->bfd_section
->size
);
6557 if (os
->update_dot_tree
!= NULL
)
6558 exp_fold_tree (os
->update_dot_tree
,
6559 bfd_abs_section_ptr
, &newdot
);
6566 case lang_wild_statement_enum
:
6568 dot
= lang_do_assignments_1 (s
->wild_statement
.children
.head
,
6569 current_os
, fill
, dot
, found_end
);
6572 case lang_object_symbols_statement_enum
:
6573 case lang_output_statement_enum
:
6574 case lang_target_statement_enum
:
6577 case lang_data_statement_enum
:
6578 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
6579 if (expld
.result
.valid_p
)
6581 s
->data_statement
.value
= expld
.result
.value
;
6582 if (expld
.result
.section
!= NULL
)
6583 s
->data_statement
.value
+= expld
.result
.section
->vma
;
6585 else if (expld
.phase
== lang_final_phase_enum
)
6586 einfo (_("%F%P: invalid data statement\n"));
6589 switch (s
->data_statement
.type
)
6607 if (size
< TO_SIZE ((unsigned) 1))
6608 size
= TO_SIZE ((unsigned) 1);
6609 dot
+= TO_ADDR (size
);
6613 case lang_reloc_statement_enum
:
6614 exp_fold_tree (s
->reloc_statement
.addend_exp
,
6615 bfd_abs_section_ptr
, &dot
);
6616 if (expld
.result
.valid_p
)
6617 s
->reloc_statement
.addend_value
= expld
.result
.value
;
6618 else if (expld
.phase
== lang_final_phase_enum
)
6619 einfo (_("%F%P: invalid reloc statement\n"));
6620 dot
+= TO_ADDR (bfd_get_reloc_size (s
->reloc_statement
.howto
));
6623 case lang_input_section_enum
:
6625 asection
*in
= s
->input_section
.section
;
6627 if ((in
->flags
& SEC_EXCLUDE
) == 0)
6628 dot
+= TO_ADDR (in
->size
);
6632 case lang_input_statement_enum
:
6635 case lang_fill_statement_enum
:
6636 fill
= s
->fill_statement
.fill
;
6639 case lang_assignment_statement_enum
:
6640 current_assign
= &s
->assignment_statement
;
6641 if (current_assign
->exp
->type
.node_class
!= etree_assert
)
6643 const char *p
= current_assign
->exp
->assign
.dst
;
6645 if (current_os
== abs_output_section
&& p
[0] == '.' && p
[1] == 0)
6646 prefer_next_section
= true;
6650 if (strcmp (p
, "end") == 0)
6653 exp_fold_tree (s
->assignment_statement
.exp
,
6654 (current_os
->bfd_section
!= NULL
6655 ? current_os
->bfd_section
: bfd_und_section_ptr
),
6659 case lang_padding_statement_enum
:
6660 dot
+= TO_ADDR (s
->padding_statement
.size
);
6663 case lang_group_statement_enum
:
6664 dot
= lang_do_assignments_1 (s
->group_statement
.children
.head
,
6665 current_os
, fill
, dot
, found_end
);
6668 case lang_insert_statement_enum
:
6671 case lang_address_statement_enum
:
6683 lang_do_assignments (lang_phase_type phase
)
6685 bool found_end
= false;
6687 current_section
= NULL
;
6688 prefer_next_section
= false;
6689 expld
.phase
= phase
;
6690 lang_statement_iteration
++;
6691 lang_do_assignments_1 (statement_list
.head
,
6692 abs_output_section
, NULL
, 0, &found_end
);
6695 /* For an assignment statement outside of an output section statement,
6696 choose the best of neighbouring output sections to use for values
6700 section_for_dot (void)
6704 /* Assignments belong to the previous output section, unless there
6705 has been an assignment to "dot", in which case following
6706 assignments belong to the next output section. (The assumption
6707 is that an assignment to "dot" is setting up the address for the
6708 next output section.) Except that past the assignment to "_end"
6709 we always associate with the previous section. This exception is
6710 for targets like SH that define an alloc .stack or other
6711 weirdness after non-alloc sections. */
6712 if (current_section
== NULL
|| prefer_next_section
)
6714 lang_statement_union_type
*stmt
;
6715 lang_output_section_statement_type
*os
;
6717 for (stmt
= (lang_statement_union_type
*) current_assign
;
6719 stmt
= stmt
->header
.next
)
6720 if (stmt
->header
.type
== lang_output_section_statement_enum
)
6723 os
= stmt
? &stmt
->output_section_statement
: NULL
;
6726 && (os
->bfd_section
== NULL
6727 || (os
->bfd_section
->flags
& SEC_EXCLUDE
) != 0
6728 || bfd_section_removed_from_list (link_info
.output_bfd
,
6732 if (current_section
== NULL
|| os
== NULL
|| !os
->after_end
)
6735 s
= os
->bfd_section
;
6737 s
= link_info
.output_bfd
->section_last
;
6739 && ((s
->flags
& SEC_ALLOC
) == 0
6740 || (s
->flags
& SEC_THREAD_LOCAL
) != 0))
6745 return bfd_abs_section_ptr
;
6749 s
= current_section
->bfd_section
;
6751 /* The section may have been stripped. */
6753 && ((s
->flags
& SEC_EXCLUDE
) != 0
6754 || (s
->flags
& SEC_ALLOC
) == 0
6755 || (s
->flags
& SEC_THREAD_LOCAL
) != 0
6756 || bfd_section_removed_from_list (link_info
.output_bfd
, s
)))
6759 s
= link_info
.output_bfd
->sections
;
6761 && ((s
->flags
& SEC_ALLOC
) == 0
6762 || (s
->flags
& SEC_THREAD_LOCAL
) != 0))
6767 return bfd_abs_section_ptr
;
6770 /* Array of __start/__stop/.startof./.sizeof/ symbols. */
6772 static struct bfd_link_hash_entry
**start_stop_syms
;
6773 static size_t start_stop_count
= 0;
6774 static size_t start_stop_alloc
= 0;
6776 /* Give start/stop SYMBOL for SEC a preliminary definition, and add it
6777 to start_stop_syms. */
6780 lang_define_start_stop (const char *symbol
, asection
*sec
)
6782 struct bfd_link_hash_entry
*h
;
6784 h
= bfd_define_start_stop (link_info
.output_bfd
, &link_info
, symbol
, sec
);
6787 if (start_stop_count
== start_stop_alloc
)
6789 start_stop_alloc
= 2 * start_stop_alloc
+ 10;
6791 = xrealloc (start_stop_syms
,
6792 start_stop_alloc
* sizeof (*start_stop_syms
));
6794 start_stop_syms
[start_stop_count
++] = h
;
6798 /* Check for input sections whose names match references to
6799 __start_SECNAME or __stop_SECNAME symbols. Give the symbols
6800 preliminary definitions. */
6803 lang_init_start_stop (void)
6807 char leading_char
= bfd_get_symbol_leading_char (link_info
.output_bfd
);
6809 for (abfd
= link_info
.input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
6810 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
6813 const char *secname
= s
->name
;
6815 for (ps
= secname
; *ps
!= '\0'; ps
++)
6816 if (!ISALNUM ((unsigned char) *ps
) && *ps
!= '_')
6820 char *symbol
= (char *) xmalloc (10 + strlen (secname
));
6822 symbol
[0] = leading_char
;
6823 sprintf (symbol
+ (leading_char
!= 0), "__start_%s", secname
);
6824 lang_define_start_stop (symbol
, s
);
6826 symbol
[1] = leading_char
;
6827 memcpy (symbol
+ 1 + (leading_char
!= 0), "__stop", 6);
6828 lang_define_start_stop (symbol
+ 1, s
);
6835 /* Iterate over start_stop_syms. */
6838 foreach_start_stop (void (*func
) (struct bfd_link_hash_entry
*))
6842 for (i
= 0; i
< start_stop_count
; ++i
)
6843 func (start_stop_syms
[i
]);
6846 /* __start and __stop symbols are only supposed to be defined by the
6847 linker for orphan sections, but we now extend that to sections that
6848 map to an output section of the same name. The symbols were
6849 defined early for --gc-sections, before we mapped input to output
6850 sections, so undo those that don't satisfy this rule. */
6853 undef_start_stop (struct bfd_link_hash_entry
*h
)
6855 if (h
->ldscript_def
)
6858 if (h
->u
.def
.section
->output_section
== NULL
6859 || h
->u
.def
.section
->output_section
->owner
!= link_info
.output_bfd
6860 || strcmp (h
->u
.def
.section
->name
,
6861 h
->u
.def
.section
->output_section
->name
) != 0)
6863 asection
*sec
= bfd_get_section_by_name (link_info
.output_bfd
,
6864 h
->u
.def
.section
->name
);
6867 /* When there are more than one input sections with the same
6868 section name, SECNAME, linker picks the first one to define
6869 __start_SECNAME and __stop_SECNAME symbols. When the first
6870 input section is removed by comdat group, we need to check
6871 if there is still an output section with section name
6874 for (i
= sec
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
6875 if (strcmp (h
->u
.def
.section
->name
, i
->name
) == 0)
6877 h
->u
.def
.section
= i
;
6881 h
->type
= bfd_link_hash_undefined
;
6882 h
->u
.undef
.abfd
= NULL
;
6883 if (is_elf_hash_table (link_info
.hash
))
6885 const struct elf_backend_data
*bed
;
6886 struct elf_link_hash_entry
*eh
= (struct elf_link_hash_entry
*) h
;
6887 unsigned int was_forced
= eh
->forced_local
;
6889 bed
= get_elf_backend_data (link_info
.output_bfd
);
6890 (*bed
->elf_backend_hide_symbol
) (&link_info
, eh
, true);
6891 if (!eh
->ref_regular_nonweak
)
6892 h
->type
= bfd_link_hash_undefweak
;
6893 eh
->def_regular
= 0;
6894 eh
->forced_local
= was_forced
;
6900 lang_undef_start_stop (void)
6902 foreach_start_stop (undef_start_stop
);
6905 /* Check for output sections whose names match references to
6906 .startof.SECNAME or .sizeof.SECNAME symbols. Give the symbols
6907 preliminary definitions. */
6910 lang_init_startof_sizeof (void)
6914 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6916 const char *secname
= s
->name
;
6917 char *symbol
= (char *) xmalloc (10 + strlen (secname
));
6919 sprintf (symbol
, ".startof.%s", secname
);
6920 lang_define_start_stop (symbol
, s
);
6922 memcpy (symbol
+ 1, ".size", 5);
6923 lang_define_start_stop (symbol
+ 1, s
);
6928 /* Set .startof., .sizeof., __start and __stop symbols final values. */
6931 set_start_stop (struct bfd_link_hash_entry
*h
)
6934 || h
->type
!= bfd_link_hash_defined
)
6937 if (h
->root
.string
[0] == '.')
6939 /* .startof. or .sizeof. symbol.
6940 .startof. already has final value. */
6941 if (h
->root
.string
[2] == 'i')
6944 h
->u
.def
.value
= TO_ADDR (h
->u
.def
.section
->size
);
6945 h
->u
.def
.section
= bfd_abs_section_ptr
;
6950 /* __start or __stop symbol. */
6951 int has_lead
= bfd_get_symbol_leading_char (link_info
.output_bfd
) != 0;
6953 h
->u
.def
.section
= h
->u
.def
.section
->output_section
;
6954 if (h
->root
.string
[4 + has_lead
] == 'o')
6957 h
->u
.def
.value
= TO_ADDR (h
->u
.def
.section
->size
);
6963 lang_finalize_start_stop (void)
6965 foreach_start_stop (set_start_stop
);
6969 lang_symbol_tweaks (void)
6971 /* Give initial values for __start and __stop symbols, so that ELF
6972 gc_sections will keep sections referenced by these symbols. Must
6973 be done before lang_do_assignments. */
6974 if (config
.build_constructors
)
6975 lang_init_start_stop ();
6977 /* Make __ehdr_start hidden, and set def_regular even though it is
6978 likely undefined at this stage. For lang_check_relocs. */
6979 if (is_elf_hash_table (link_info
.hash
)
6980 && !bfd_link_relocatable (&link_info
))
6982 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*)
6983 bfd_link_hash_lookup (link_info
.hash
, "__ehdr_start",
6984 false, false, true);
6986 /* Only adjust the export class if the symbol was referenced
6987 and not defined, otherwise leave it alone. */
6989 && (h
->root
.type
== bfd_link_hash_new
6990 || h
->root
.type
== bfd_link_hash_undefined
6991 || h
->root
.type
== bfd_link_hash_undefweak
6992 || h
->root
.type
== bfd_link_hash_common
))
6994 const struct elf_backend_data
*bed
;
6995 bed
= get_elf_backend_data (link_info
.output_bfd
);
6996 (*bed
->elf_backend_hide_symbol
) (&link_info
, h
, true);
6997 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
6998 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
7000 h
->root
.linker_def
= 1;
7001 h
->root
.rel_from_abs
= 1;
7009 struct bfd_link_hash_entry
*h
;
7012 if ((bfd_link_relocatable (&link_info
) && !link_info
.gc_sections
)
7013 || bfd_link_dll (&link_info
))
7014 warn
= entry_from_cmdline
;
7018 /* Force the user to specify a root when generating a relocatable with
7019 --gc-sections, unless --gc-keep-exported was also given. */
7020 if (bfd_link_relocatable (&link_info
)
7021 && link_info
.gc_sections
7022 && !link_info
.gc_keep_exported
)
7024 struct bfd_sym_chain
*sym
;
7026 for (sym
= link_info
.gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
7028 h
= bfd_link_hash_lookup (link_info
.hash
, sym
->name
,
7029 false, false, false);
7031 && (h
->type
== bfd_link_hash_defined
7032 || h
->type
== bfd_link_hash_defweak
)
7033 && !bfd_is_const_section (h
->u
.def
.section
))
7037 einfo (_("%F%P: --gc-sections requires a defined symbol root "
7038 "specified by -e or -u\n"));
7041 if (entry_symbol
.name
== NULL
)
7043 /* No entry has been specified. Look for the default entry, but
7044 don't warn if we don't find it. */
7045 entry_symbol
.name
= entry_symbol_default
;
7049 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
.name
,
7050 false, false, true);
7052 && (h
->type
== bfd_link_hash_defined
7053 || h
->type
== bfd_link_hash_defweak
)
7054 && h
->u
.def
.section
->output_section
!= NULL
)
7058 val
= (h
->u
.def
.value
7059 + bfd_section_vma (h
->u
.def
.section
->output_section
)
7060 + h
->u
.def
.section
->output_offset
);
7061 if (!bfd_set_start_address (link_info
.output_bfd
, val
))
7062 einfo (_("%F%P: %s: can't set start address\n"), entry_symbol
.name
);
7069 /* We couldn't find the entry symbol. Try parsing it as a
7071 val
= bfd_scan_vma (entry_symbol
.name
, &send
, 0);
7074 if (!bfd_set_start_address (link_info
.output_bfd
, val
))
7075 einfo (_("%F%P: can't set start address\n"));
7077 /* BZ 2004952: Only use the start of the entry section for executables. */
7078 else if bfd_link_executable (&link_info
)
7082 /* Can't find the entry symbol, and it's not a number. Use
7083 the first address in the text section. */
7084 ts
= bfd_get_section_by_name (link_info
.output_bfd
, entry_section
);
7088 einfo (_("%P: warning: cannot find entry symbol %s;"
7089 " defaulting to %V\n"),
7091 bfd_section_vma (ts
));
7092 if (!bfd_set_start_address (link_info
.output_bfd
,
7093 bfd_section_vma (ts
)))
7094 einfo (_("%F%P: can't set start address\n"));
7099 einfo (_("%P: warning: cannot find entry symbol %s;"
7100 " not setting start address\n"),
7107 einfo (_("%P: warning: cannot find entry symbol %s;"
7108 " not setting start address\n"),
7114 /* This is a small function used when we want to ignore errors from
7118 ignore_bfd_errors (const char *fmt ATTRIBUTE_UNUSED
,
7119 va_list ap ATTRIBUTE_UNUSED
)
7121 /* Don't do anything. */
7124 /* Check that the architecture of all the input files is compatible
7125 with the output file. Also call the backend to let it do any
7126 other checking that is needed. */
7131 lang_input_statement_type
*file
;
7133 const bfd_arch_info_type
*compatible
;
7135 for (file
= (void *) file_chain
.head
;
7139 #if BFD_SUPPORTS_PLUGINS
7140 /* Don't check format of files claimed by plugin. */
7141 if (file
->flags
.claimed
)
7143 #endif /* BFD_SUPPORTS_PLUGINS */
7144 input_bfd
= file
->the_bfd
;
7146 = bfd_arch_get_compatible (input_bfd
, link_info
.output_bfd
,
7147 command_line
.accept_unknown_input_arch
);
7149 /* In general it is not possible to perform a relocatable
7150 link between differing object formats when the input
7151 file has relocations, because the relocations in the
7152 input format may not have equivalent representations in
7153 the output format (and besides BFD does not translate
7154 relocs for other link purposes than a final link). */
7155 if (!file
->flags
.just_syms
7156 && (bfd_link_relocatable (&link_info
)
7157 || link_info
.emitrelocations
)
7158 && (compatible
== NULL
7159 || (bfd_get_flavour (input_bfd
)
7160 != bfd_get_flavour (link_info
.output_bfd
)))
7161 && (bfd_get_file_flags (input_bfd
) & HAS_RELOC
) != 0)
7163 einfo (_("%F%P: relocatable linking with relocations from"
7164 " format %s (%pB) to format %s (%pB) is not supported\n"),
7165 bfd_get_target (input_bfd
), input_bfd
,
7166 bfd_get_target (link_info
.output_bfd
), link_info
.output_bfd
);
7167 /* einfo with %F exits. */
7170 if (compatible
== NULL
)
7172 if (command_line
.warn_mismatch
)
7173 einfo (_("%X%P: %s architecture of input file `%pB'"
7174 " is incompatible with %s output\n"),
7175 bfd_printable_name (input_bfd
), input_bfd
,
7176 bfd_printable_name (link_info
.output_bfd
));
7179 /* If the input bfd has no contents, it shouldn't set the
7180 private data of the output bfd. */
7181 else if (!file
->flags
.just_syms
7182 && ((input_bfd
->flags
& DYNAMIC
) != 0
7183 || bfd_count_sections (input_bfd
) != 0))
7185 bfd_error_handler_type pfn
= NULL
;
7187 /* If we aren't supposed to warn about mismatched input
7188 files, temporarily set the BFD error handler to a
7189 function which will do nothing. We still want to call
7190 bfd_merge_private_bfd_data, since it may set up
7191 information which is needed in the output file. */
7192 if (!command_line
.warn_mismatch
)
7193 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
7194 if (!bfd_merge_private_bfd_data (input_bfd
, &link_info
))
7196 if (command_line
.warn_mismatch
)
7197 einfo (_("%X%P: failed to merge target specific data"
7198 " of file %pB\n"), input_bfd
);
7200 if (!command_line
.warn_mismatch
)
7201 bfd_set_error_handler (pfn
);
7206 /* Look through all the global common symbols and attach them to the
7207 correct section. The -sort-common command line switch may be used
7208 to roughly sort the entries by alignment. */
7213 if (link_info
.inhibit_common_definition
)
7215 if (bfd_link_relocatable (&link_info
)
7216 && !command_line
.force_common_definition
)
7219 if (!config
.sort_common
)
7220 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, NULL
);
7225 if (config
.sort_common
== sort_descending
)
7227 for (power
= 4; power
> 0; power
--)
7228 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7231 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7235 for (power
= 0; power
<= 4; power
++)
7236 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7238 power
= (unsigned int) -1;
7239 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7244 /* Place one common symbol in the correct section. */
7247 lang_one_common (struct bfd_link_hash_entry
*h
, void *info
)
7249 unsigned int power_of_two
;
7253 if (h
->type
!= bfd_link_hash_common
)
7257 power_of_two
= h
->u
.c
.p
->alignment_power
;
7259 if (config
.sort_common
== sort_descending
7260 && power_of_two
< *(unsigned int *) info
)
7262 else if (config
.sort_common
== sort_ascending
7263 && power_of_two
> *(unsigned int *) info
)
7266 section
= h
->u
.c
.p
->section
;
7267 if (!bfd_define_common_symbol (link_info
.output_bfd
, &link_info
, h
))
7268 einfo (_("%F%P: could not define common symbol `%pT': %E\n"),
7271 if (config
.map_file
!= NULL
)
7273 static bool header_printed
;
7278 if (!header_printed
)
7280 minfo (_("\nAllocating common symbols\n"));
7281 minfo (_("Common symbol size file\n\n"));
7282 header_printed
= true;
7285 name
= bfd_demangle (link_info
.output_bfd
, h
->root
.string
,
7286 DMGL_ANSI
| DMGL_PARAMS
);
7289 minfo ("%s", h
->root
.string
);
7290 len
= strlen (h
->root
.string
);
7295 len
= strlen (name
);
7305 sprintf (buf
, "%" PRIx64
, (uint64_t) size
);
7306 fprintf (config
.map_file
, "%*s0x%-16s", 20 - len
, "", buf
);
7308 minfo ("%pB\n", section
->owner
);
7314 /* Handle a single orphan section S, placing the orphan into an appropriate
7315 output section. The effects of the --orphan-handling command line
7316 option are handled here. */
7319 ldlang_place_orphan (asection
*s
)
7321 if (config
.orphan_handling
== orphan_handling_discard
)
7323 lang_output_section_statement_type
*os
;
7324 os
= lang_output_section_statement_lookup (DISCARD_SECTION_NAME
, 0, 1);
7325 if (os
->addr_tree
== NULL
7326 && (bfd_link_relocatable (&link_info
)
7327 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0))
7328 os
->addr_tree
= exp_intop (0);
7329 lang_add_section (&os
->children
, s
, NULL
, NULL
, os
);
7333 lang_output_section_statement_type
*os
;
7334 const char *name
= s
->name
;
7337 if (config
.orphan_handling
== orphan_handling_error
)
7338 einfo (_("%X%P: error: unplaced orphan section `%pA' from `%pB'\n"),
7341 if (config
.unique_orphan_sections
|| unique_section_p (s
, NULL
))
7342 constraint
= SPECIAL
;
7344 os
= ldemul_place_orphan (s
, name
, constraint
);
7347 os
= lang_output_section_statement_lookup (name
, constraint
, 1);
7348 if (os
->addr_tree
== NULL
7349 && (bfd_link_relocatable (&link_info
)
7350 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0))
7351 os
->addr_tree
= exp_intop (0);
7352 lang_add_section (&os
->children
, s
, NULL
, NULL
, os
);
7355 if (config
.orphan_handling
== orphan_handling_warn
)
7356 einfo (_("%P: warning: orphan section `%pA' from `%pB' being "
7357 "placed in section `%s'\n"),
7358 s
, s
->owner
, os
->name
);
7362 /* Run through the input files and ensure that every input section has
7363 somewhere to go. If one is found without a destination then create
7364 an input request and place it into the statement tree. */
7367 lang_place_orphans (void)
7369 LANG_FOR_EACH_INPUT_STATEMENT (file
)
7373 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7375 if (s
->output_section
== NULL
)
7377 /* This section of the file is not attached, root
7378 around for a sensible place for it to go. */
7380 if (file
->flags
.just_syms
)
7381 bfd_link_just_syms (file
->the_bfd
, s
, &link_info
);
7382 else if (lang_discard_section_p (s
))
7383 s
->output_section
= bfd_abs_section_ptr
;
7384 else if (strcmp (s
->name
, "COMMON") == 0)
7386 /* This is a lonely common section which must have
7387 come from an archive. We attach to the section
7388 with the wildcard. */
7389 if (!bfd_link_relocatable (&link_info
)
7390 || command_line
.force_common_definition
)
7392 if (default_common_section
== NULL
)
7393 default_common_section
7394 = lang_output_section_statement_lookup (".bss", 0, 1);
7395 lang_add_section (&default_common_section
->children
, s
,
7396 NULL
, NULL
, default_common_section
);
7400 ldlang_place_orphan (s
);
7407 lang_set_flags (lang_memory_region_type
*ptr
, const char *flags
, int invert
)
7409 flagword
*ptr_flags
;
7411 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
7417 /* PR 17900: An exclamation mark in the attributes reverses
7418 the sense of any of the attributes that follow. */
7421 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
7425 *ptr_flags
|= SEC_ALLOC
;
7429 *ptr_flags
|= SEC_READONLY
;
7433 *ptr_flags
|= SEC_DATA
;
7437 *ptr_flags
|= SEC_CODE
;
7442 *ptr_flags
|= SEC_LOAD
;
7446 einfo (_("%F%P: invalid character %c (%d) in flags\n"),
7454 /* Call a function on each real input file. This function will be
7455 called on an archive, but not on the elements. */
7458 lang_for_each_input_file (void (*func
) (lang_input_statement_type
*))
7460 lang_input_statement_type
*f
;
7462 for (f
= (void *) input_file_chain
.head
;
7464 f
= f
->next_real_file
)
7469 /* Call a function on each real file. The function will be called on
7470 all the elements of an archive which are included in the link, but
7471 will not be called on the archive file itself. */
7474 lang_for_each_file (void (*func
) (lang_input_statement_type
*))
7476 LANG_FOR_EACH_INPUT_STATEMENT (f
)
7484 ldlang_add_file (lang_input_statement_type
*entry
)
7486 lang_statement_append (&file_chain
, entry
, &entry
->next
);
7488 /* The BFD linker needs to have a list of all input BFDs involved in
7490 ASSERT (link_info
.input_bfds_tail
!= &entry
->the_bfd
->link
.next
7491 && entry
->the_bfd
->link
.next
== NULL
);
7492 ASSERT (entry
->the_bfd
!= link_info
.output_bfd
);
7494 *link_info
.input_bfds_tail
= entry
->the_bfd
;
7495 link_info
.input_bfds_tail
= &entry
->the_bfd
->link
.next
;
7496 bfd_set_usrdata (entry
->the_bfd
, entry
);
7497 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
7499 /* Look through the sections and check for any which should not be
7500 included in the link. We need to do this now, so that we can
7501 notice when the backend linker tries to report multiple
7502 definition errors for symbols which are in sections we aren't
7503 going to link. FIXME: It might be better to entirely ignore
7504 symbols which are defined in sections which are going to be
7505 discarded. This would require modifying the backend linker for
7506 each backend which might set the SEC_LINK_ONCE flag. If we do
7507 this, we should probably handle SEC_EXCLUDE in the same way. */
7509 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, entry
);
7513 lang_add_output (const char *name
, int from_script
)
7515 /* Make -o on command line override OUTPUT in script. */
7516 if (!had_output_filename
|| !from_script
)
7518 output_filename
= name
;
7519 had_output_filename
= true;
7523 lang_output_section_statement_type
*
7524 lang_enter_output_section_statement (const char *output_section_statement_name
,
7525 etree_type
*address_exp
,
7526 enum section_type sectype
,
7527 etree_type
*sectype_value
,
7529 etree_type
*subalign
,
7532 int align_with_input
)
7534 lang_output_section_statement_type
*os
;
7536 os
= lang_output_section_statement_lookup (output_section_statement_name
,
7538 current_section
= os
;
7540 if (os
->addr_tree
== NULL
)
7542 os
->addr_tree
= address_exp
;
7544 os
->sectype
= sectype
;
7545 if (sectype
== type_section
|| sectype
== typed_readonly_section
)
7546 os
->sectype_value
= sectype_value
;
7547 else if (sectype
== noload_section
)
7548 os
->flags
= SEC_NEVER_LOAD
;
7550 os
->flags
= SEC_NO_FLAGS
;
7551 os
->block_value
= 1;
7553 /* Make next things chain into subchain of this. */
7554 push_stat_ptr (&os
->children
);
7556 os
->align_lma_with_input
= align_with_input
== ALIGN_WITH_INPUT
;
7557 if (os
->align_lma_with_input
&& align
!= NULL
)
7558 einfo (_("%F%P:%pS: error: align with input and explicit align specified\n"),
7561 os
->subsection_alignment
= subalign
;
7562 os
->section_alignment
= align
;
7564 os
->load_base
= ebase
;
7571 lang_output_statement_type
*new_stmt
;
7573 new_stmt
= new_stat (lang_output_statement
, stat_ptr
);
7574 new_stmt
->name
= output_filename
;
7577 /* Reset the current counters in the regions. */
7580 lang_reset_memory_regions (void)
7582 lang_memory_region_type
*p
= lang_memory_region_list
;
7584 lang_output_section_statement_type
*os
;
7586 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
7588 p
->current
= p
->origin
;
7592 for (os
= (void *) lang_os_list
.head
;
7596 os
->processed_vma
= false;
7597 os
->processed_lma
= false;
7600 for (o
= link_info
.output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
7602 /* Save the last size for possible use by bfd_relax_section. */
7603 o
->rawsize
= o
->size
;
7604 if (!(o
->flags
& SEC_FIXED_SIZE
))
7609 /* Worker for lang_gc_sections_1. */
7612 gc_section_callback (lang_wild_statement_type
*ptr
,
7613 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
7615 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
7616 void *data ATTRIBUTE_UNUSED
)
7618 /* If the wild pattern was marked KEEP, the member sections
7619 should be as well. */
7620 if (ptr
->keep_sections
)
7621 section
->flags
|= SEC_KEEP
;
7624 /* Iterate over sections marking them against GC. */
7627 lang_gc_sections_1 (lang_statement_union_type
*s
)
7629 for (; s
!= NULL
; s
= s
->header
.next
)
7631 switch (s
->header
.type
)
7633 case lang_wild_statement_enum
:
7634 walk_wild (&s
->wild_statement
, gc_section_callback
, NULL
);
7636 case lang_constructors_statement_enum
:
7637 lang_gc_sections_1 (constructor_list
.head
);
7639 case lang_output_section_statement_enum
:
7640 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
7642 case lang_group_statement_enum
:
7643 lang_gc_sections_1 (s
->group_statement
.children
.head
);
7652 lang_gc_sections (void)
7654 /* Keep all sections so marked in the link script. */
7655 lang_gc_sections_1 (statement_list
.head
);
7657 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
7658 the special case of .stabstr debug info. (See bfd/stabs.c)
7659 Twiddle the flag here, to simplify later linker code. */
7660 if (bfd_link_relocatable (&link_info
))
7662 LANG_FOR_EACH_INPUT_STATEMENT (f
)
7665 #if BFD_SUPPORTS_PLUGINS
7666 if (f
->flags
.claimed
)
7669 for (sec
= f
->the_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7670 if ((sec
->flags
& SEC_DEBUGGING
) == 0
7671 || strcmp (sec
->name
, ".stabstr") != 0)
7672 sec
->flags
&= ~SEC_EXCLUDE
;
7676 if (link_info
.gc_sections
)
7677 bfd_gc_sections (link_info
.output_bfd
, &link_info
);
7680 /* Worker for lang_find_relro_sections_1. */
7683 find_relro_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
7684 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
7686 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
7689 /* Discarded, excluded and ignored sections effectively have zero
7691 if (section
->output_section
!= NULL
7692 && section
->output_section
->owner
== link_info
.output_bfd
7693 && (section
->output_section
->flags
& SEC_EXCLUDE
) == 0
7694 && !IGNORE_SECTION (section
)
7695 && section
->size
!= 0)
7697 bool *has_relro_section
= (bool *) data
;
7698 *has_relro_section
= true;
7702 /* Iterate over sections for relro sections. */
7705 lang_find_relro_sections_1 (lang_statement_union_type
*s
,
7706 bool *has_relro_section
)
7708 if (*has_relro_section
)
7711 for (; s
!= NULL
; s
= s
->header
.next
)
7713 if (s
== expld
.dataseg
.relro_end_stat
)
7716 switch (s
->header
.type
)
7718 case lang_wild_statement_enum
:
7719 walk_wild (&s
->wild_statement
,
7720 find_relro_section_callback
,
7723 case lang_constructors_statement_enum
:
7724 lang_find_relro_sections_1 (constructor_list
.head
,
7727 case lang_output_section_statement_enum
:
7728 lang_find_relro_sections_1 (s
->output_section_statement
.children
.head
,
7731 case lang_group_statement_enum
:
7732 lang_find_relro_sections_1 (s
->group_statement
.children
.head
,
7742 lang_find_relro_sections (void)
7744 bool has_relro_section
= false;
7746 /* Check all sections in the link script. */
7748 lang_find_relro_sections_1 (expld
.dataseg
.relro_start_stat
,
7749 &has_relro_section
);
7751 if (!has_relro_section
)
7752 link_info
.relro
= false;
7755 /* Relax all sections until bfd_relax_section gives up. */
7758 lang_relax_sections (bool need_layout
)
7760 /* NB: Also enable relaxation to layout sections for DT_RELR. */
7761 if (RELAXATION_ENABLED
|| link_info
.enable_dt_relr
)
7763 /* We may need more than one relaxation pass. */
7764 int i
= link_info
.relax_pass
;
7766 /* The backend can use it to determine the current pass. */
7767 link_info
.relax_pass
= 0;
7771 /* Keep relaxing until bfd_relax_section gives up. */
7774 link_info
.relax_trip
= -1;
7777 link_info
.relax_trip
++;
7779 /* Note: pe-dll.c does something like this also. If you find
7780 you need to change this code, you probably need to change
7781 pe-dll.c also. DJ */
7783 /* Do all the assignments with our current guesses as to
7785 lang_do_assignments (lang_assigning_phase_enum
);
7787 /* We must do this after lang_do_assignments, because it uses
7789 lang_reset_memory_regions ();
7791 /* Perform another relax pass - this time we know where the
7792 globals are, so can make a better guess. */
7793 relax_again
= false;
7794 lang_size_sections (&relax_again
, false);
7796 while (relax_again
);
7798 link_info
.relax_pass
++;
7805 /* Final extra sizing to report errors. */
7806 lang_do_assignments (lang_assigning_phase_enum
);
7807 lang_reset_memory_regions ();
7808 lang_size_sections (NULL
, true);
7812 #if BFD_SUPPORTS_PLUGINS
7813 /* Find the insert point for the plugin's replacement files. We
7814 place them after the first claimed real object file, or if the
7815 first claimed object is an archive member, after the last real
7816 object file immediately preceding the archive. In the event
7817 no objects have been claimed at all, we return the first dummy
7818 object file on the list as the insert point; that works, but
7819 the callee must be careful when relinking the file_chain as it
7820 is not actually on that chain, only the statement_list and the
7821 input_file list; in that case, the replacement files must be
7822 inserted at the head of the file_chain. */
7824 static lang_input_statement_type
*
7825 find_replacements_insert_point (bool *before
)
7827 lang_input_statement_type
*claim1
, *lastobject
;
7828 lastobject
= (void *) input_file_chain
.head
;
7829 for (claim1
= (void *) file_chain
.head
;
7831 claim1
= claim1
->next
)
7833 if (claim1
->flags
.claimed
)
7835 *before
= claim1
->flags
.claim_archive
;
7836 return claim1
->flags
.claim_archive
? lastobject
: claim1
;
7838 /* Update lastobject if this is a real object file. */
7839 if (claim1
->the_bfd
!= NULL
&& claim1
->the_bfd
->my_archive
== NULL
)
7840 lastobject
= claim1
;
7842 /* No files were claimed by the plugin. Choose the last object
7843 file found on the list (maybe the first, dummy entry) as the
7849 /* Find where to insert ADD, an archive element or shared library
7850 added during a rescan. */
7852 static lang_input_statement_type
**
7853 find_rescan_insertion (lang_input_statement_type
*add
)
7855 bfd
*add_bfd
= add
->the_bfd
;
7856 lang_input_statement_type
*f
;
7857 lang_input_statement_type
*last_loaded
= NULL
;
7858 lang_input_statement_type
*before
= NULL
;
7859 lang_input_statement_type
**iter
= NULL
;
7861 if (add_bfd
->my_archive
!= NULL
)
7862 add_bfd
= add_bfd
->my_archive
;
7864 /* First look through the input file chain, to find an object file
7865 before the one we've rescanned. Normal object files always
7866 appear on both the input file chain and the file chain, so this
7867 lets us get quickly to somewhere near the correct place on the
7868 file chain if it is full of archive elements. Archives don't
7869 appear on the file chain, but if an element has been extracted
7870 then their input_statement->next points at it. */
7871 for (f
= (void *) input_file_chain
.head
;
7873 f
= f
->next_real_file
)
7875 if (f
->the_bfd
== add_bfd
)
7877 before
= last_loaded
;
7878 if (f
->next
!= NULL
)
7879 return &f
->next
->next
;
7881 if (f
->the_bfd
!= NULL
&& f
->next
!= NULL
)
7885 for (iter
= before
? &before
->next
: &file_chain
.head
->input_statement
.next
;
7887 iter
= &(*iter
)->next
)
7888 if (!(*iter
)->flags
.claim_archive
7889 && (*iter
)->the_bfd
->my_archive
== NULL
)
7895 /* Insert SRCLIST into DESTLIST after given element by chaining
7896 on FIELD as the next-pointer. (Counterintuitively does not need
7897 a pointer to the actual after-node itself, just its chain field.) */
7900 lang_list_insert_after (lang_statement_list_type
*destlist
,
7901 lang_statement_list_type
*srclist
,
7902 lang_statement_union_type
**field
)
7904 *(srclist
->tail
) = *field
;
7905 *field
= srclist
->head
;
7906 if (destlist
->tail
== field
)
7907 destlist
->tail
= srclist
->tail
;
7910 /* Detach new nodes added to DESTLIST since the time ORIGLIST
7911 was taken as a copy of it and leave them in ORIGLIST. */
7914 lang_list_remove_tail (lang_statement_list_type
*destlist
,
7915 lang_statement_list_type
*origlist
)
7917 union lang_statement_union
**savetail
;
7918 /* Check that ORIGLIST really is an earlier state of DESTLIST. */
7919 ASSERT (origlist
->head
== destlist
->head
);
7920 savetail
= origlist
->tail
;
7921 origlist
->head
= *(savetail
);
7922 origlist
->tail
= destlist
->tail
;
7923 destlist
->tail
= savetail
;
7927 static lang_statement_union_type
**
7928 find_next_input_statement (lang_statement_union_type
**s
)
7930 for ( ; *s
; s
= &(*s
)->header
.next
)
7932 lang_statement_union_type
**t
;
7933 switch ((*s
)->header
.type
)
7935 case lang_input_statement_enum
:
7937 case lang_wild_statement_enum
:
7938 t
= &(*s
)->wild_statement
.children
.head
;
7940 case lang_group_statement_enum
:
7941 t
= &(*s
)->group_statement
.children
.head
;
7943 case lang_output_section_statement_enum
:
7944 t
= &(*s
)->output_section_statement
.children
.head
;
7949 t
= find_next_input_statement (t
);
7955 #endif /* BFD_SUPPORTS_PLUGINS */
7957 /* Add NAME to the list of garbage collection entry points. */
7960 lang_add_gc_name (const char *name
)
7962 struct bfd_sym_chain
*sym
;
7967 sym
= stat_alloc (sizeof (*sym
));
7969 sym
->next
= link_info
.gc_sym_list
;
7971 link_info
.gc_sym_list
= sym
;
7974 /* Check relocations. */
7977 lang_check_relocs (void)
7979 if (link_info
.check_relocs_after_open_input
)
7983 for (abfd
= link_info
.input_bfds
;
7984 abfd
!= (bfd
*) NULL
; abfd
= abfd
->link
.next
)
7985 if (!bfd_link_check_relocs (abfd
, &link_info
))
7987 /* No object output, fail return. */
7988 config
.make_executable
= false;
7989 /* Note: we do not abort the loop, but rather
7990 continue the scan in case there are other
7991 bad relocations to report. */
7996 /* Look through all output sections looking for places where we can
7997 propagate forward the lma region. */
8000 lang_propagate_lma_regions (void)
8002 lang_output_section_statement_type
*os
;
8004 for (os
= (void *) lang_os_list
.head
;
8008 if (os
->prev
!= NULL
8009 && os
->lma_region
== NULL
8010 && os
->load_base
== NULL
8011 && os
->addr_tree
== NULL
8012 && os
->region
== os
->prev
->region
)
8013 os
->lma_region
= os
->prev
->lma_region
;
8018 warn_non_contiguous_discards (void)
8020 LANG_FOR_EACH_INPUT_STATEMENT (file
)
8022 if ((file
->the_bfd
->flags
& (BFD_LINKER_CREATED
| DYNAMIC
)) != 0
8023 || file
->flags
.just_syms
)
8026 for (asection
*s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
8027 if (s
->output_section
== NULL
8028 && (s
->flags
& SEC_LINKER_CREATED
) == 0)
8029 einfo (_("%P: warning: --enable-non-contiguous-regions "
8030 "discards section `%pA' from `%pB'\n"),
8036 reset_one_wild (lang_statement_union_type
*statement
)
8038 if (statement
->header
.type
== lang_wild_statement_enum
)
8040 lang_wild_statement_type
*stmt
= &statement
->wild_statement
;
8041 lang_list_init (&stmt
->matching_sections
);
8046 reset_resolved_wilds (void)
8048 lang_for_each_statement (reset_one_wild
);
8054 /* Finalize dynamic list. */
8055 if (link_info
.dynamic_list
)
8056 lang_finalize_version_expr_head (&link_info
.dynamic_list
->head
);
8058 current_target
= default_target
;
8060 /* Open the output file. */
8061 lang_for_each_statement (ldlang_open_output
);
8064 ldemul_create_output_section_statements ();
8066 /* Add to the hash table all undefineds on the command line. */
8067 lang_place_undefineds ();
8069 if (!bfd_section_already_linked_table_init ())
8070 einfo (_("%F%P: can not create hash table: %E\n"));
8072 /* A first pass through the memory regions ensures that if any region
8073 references a symbol for its origin or length then this symbol will be
8074 added to the symbol table. Having these symbols in the symbol table
8075 means that when we call open_input_bfds PROVIDE statements will
8076 trigger to provide any needed symbols. The regions origins and
8077 lengths are not assigned as a result of this call. */
8078 lang_do_memory_regions (false);
8080 /* Create a bfd for each input file. */
8081 current_target
= default_target
;
8082 lang_statement_iteration
++;
8083 open_input_bfds (statement_list
.head
, OPEN_BFD_NORMAL
);
8085 /* Now that open_input_bfds has processed assignments and provide
8086 statements we can give values to symbolic origin/length now. */
8087 lang_do_memory_regions (true);
8089 ldemul_before_plugin_all_symbols_read ();
8091 #if BFD_SUPPORTS_PLUGINS
8092 if (link_info
.lto_plugin_active
)
8094 lang_statement_list_type added
;
8095 lang_statement_list_type files
, inputfiles
;
8097 /* Now all files are read, let the plugin(s) decide if there
8098 are any more to be added to the link before we call the
8099 emulation's after_open hook. We create a private list of
8100 input statements for this purpose, which we will eventually
8101 insert into the global statement list after the first claimed
8104 /* We need to manipulate all three chains in synchrony. */
8106 inputfiles
= input_file_chain
;
8107 if (plugin_call_all_symbols_read ())
8108 einfo (_("%F%P: %s: plugin reported error after all symbols read\n"),
8109 plugin_error_plugin ());
8110 link_info
.lto_all_symbols_read
= true;
8111 /* Open any newly added files, updating the file chains. */
8112 plugin_undefs
= link_info
.hash
->undefs_tail
;
8113 open_input_bfds (*added
.tail
, OPEN_BFD_NORMAL
);
8114 if (plugin_undefs
== link_info
.hash
->undefs_tail
)
8115 plugin_undefs
= NULL
;
8116 /* Restore the global list pointer now they have all been added. */
8117 lang_list_remove_tail (stat_ptr
, &added
);
8118 /* And detach the fresh ends of the file lists. */
8119 lang_list_remove_tail (&file_chain
, &files
);
8120 lang_list_remove_tail (&input_file_chain
, &inputfiles
);
8121 /* Were any new files added? */
8122 if (added
.head
!= NULL
)
8124 /* If so, we will insert them into the statement list immediately
8125 after the first input file that was claimed by the plugin,
8126 unless that file was an archive in which case it is inserted
8127 immediately before. */
8129 lang_statement_union_type
**prev
;
8130 plugin_insert
= find_replacements_insert_point (&before
);
8131 /* If a plugin adds input files without having claimed any, we
8132 don't really have a good idea where to place them. Just putting
8133 them at the start or end of the list is liable to leave them
8134 outside the crtbegin...crtend range. */
8135 ASSERT (plugin_insert
!= NULL
);
8136 /* Splice the new statement list into the old one. */
8137 prev
= &plugin_insert
->header
.next
;
8140 prev
= find_next_input_statement (prev
);
8141 if (*prev
!= (void *) plugin_insert
->next_real_file
)
8143 /* We didn't find the expected input statement.
8144 Fall back to adding after plugin_insert. */
8145 prev
= &plugin_insert
->header
.next
;
8148 lang_list_insert_after (stat_ptr
, &added
, prev
);
8149 /* Likewise for the file chains. */
8150 lang_list_insert_after (&input_file_chain
, &inputfiles
,
8151 (void *) &plugin_insert
->next_real_file
);
8152 /* We must be careful when relinking file_chain; we may need to
8153 insert the new files at the head of the list if the insert
8154 point chosen is the dummy first input file. */
8155 if (plugin_insert
->filename
)
8156 lang_list_insert_after (&file_chain
, &files
,
8157 (void *) &plugin_insert
->next
);
8159 lang_list_insert_after (&file_chain
, &files
, &file_chain
.head
);
8161 /* Rescan archives in case new undefined symbols have appeared. */
8163 lang_statement_iteration
++;
8164 open_input_bfds (statement_list
.head
, OPEN_BFD_RESCAN
);
8165 lang_list_remove_tail (&file_chain
, &files
);
8166 while (files
.head
!= NULL
)
8168 lang_input_statement_type
**insert
;
8169 lang_input_statement_type
**iter
, *temp
;
8172 insert
= find_rescan_insertion (&files
.head
->input_statement
);
8173 /* All elements from an archive can be added at once. */
8174 iter
= &files
.head
->input_statement
.next
;
8175 my_arch
= files
.head
->input_statement
.the_bfd
->my_archive
;
8176 if (my_arch
!= NULL
)
8177 for (; *iter
!= NULL
; iter
= &(*iter
)->next
)
8178 if ((*iter
)->the_bfd
->my_archive
!= my_arch
)
8181 *insert
= &files
.head
->input_statement
;
8182 files
.head
= (lang_statement_union_type
*) *iter
;
8184 if (file_chain
.tail
== (lang_statement_union_type
**) insert
)
8185 file_chain
.tail
= (lang_statement_union_type
**) iter
;
8186 if (my_arch
!= NULL
)
8188 lang_input_statement_type
*parent
= bfd_usrdata (my_arch
);
8190 parent
->next
= (lang_input_statement_type
*)
8192 - offsetof (lang_input_statement_type
, next
));
8197 #endif /* BFD_SUPPORTS_PLUGINS */
8199 struct bfd_sym_chain
**sym
= &link_info
.gc_sym_list
;
8201 sym
= &(*sym
)->next
;
8203 *sym
= &entry_symbol
;
8205 if (entry_symbol
.name
== NULL
)
8207 *sym
= ldlang_undef_chain_list_head
;
8209 /* entry_symbol is normally initialised by an ENTRY definition in the
8210 linker script or the -e command line option. But if neither of
8211 these have been used, the target specific backend may still have
8212 provided an entry symbol via a call to lang_default_entry().
8213 Unfortunately this value will not be processed until lang_end()
8214 is called, long after this function has finished. So detect this
8215 case here and add the target's entry symbol to the list of starting
8216 points for garbage collection resolution. */
8217 lang_add_gc_name (entry_symbol_default
);
8220 lang_add_gc_name (link_info
.init_function
);
8221 lang_add_gc_name (link_info
.fini_function
);
8223 ldemul_after_open ();
8224 if (config
.map_file
!= NULL
)
8225 lang_print_asneeded ();
8229 bfd_section_already_linked_table_free ();
8231 /* Make sure that we're not mixing architectures. We call this
8232 after all the input files have been opened, but before we do any
8233 other processing, so that any operations merge_private_bfd_data
8234 does on the output file will be known during the rest of the
8238 /* Handle .exports instead of a version script if we're told to do so. */
8239 if (command_line
.version_exports_section
)
8240 lang_do_version_exports_section ();
8242 /* Build all sets based on the information gathered from the input
8244 ldctor_build_sets ();
8246 lang_symbol_tweaks ();
8248 /* PR 13683: We must rerun the assignments prior to running garbage
8249 collection in order to make sure that all symbol aliases are resolved. */
8250 lang_do_assignments (lang_mark_phase_enum
);
8251 expld
.phase
= lang_first_phase_enum
;
8253 /* Size up the common data. */
8257 debug_prefix_tree ();
8261 /* Remove unreferenced sections if asked to. */
8262 lang_gc_sections ();
8264 lang_mark_undefineds ();
8266 /* Check relocations. */
8267 lang_check_relocs ();
8269 ldemul_after_check_relocs ();
8271 /* There might have been new sections created (e.g. as result of
8272 checking relocs to need a .got, or suchlike), so to properly order
8273 them into our lists of matching sections reset them here. */
8274 reset_resolved_wilds ();
8277 /* Update wild statements in case the user gave --sort-section.
8278 Note how the option might have come after the linker script and
8279 so couldn't have been set when the wild statements were created. */
8280 update_wild_statements (statement_list
.head
);
8282 /* Run through the contours of the script and attach input sections
8283 to the correct output sections. */
8284 lang_statement_iteration
++;
8285 map_input_to_output_sections (statement_list
.head
, NULL
, NULL
);
8287 /* Start at the statement immediately after the special abs_section
8288 output statement, so that it isn't reordered. */
8289 process_insert_statements (&lang_os_list
.head
->header
.next
);
8291 ldemul_before_place_orphans ();
8293 /* Find any sections not attached explicitly and handle them. */
8294 lang_place_orphans ();
8296 if (!bfd_link_relocatable (&link_info
))
8300 /* Merge SEC_MERGE sections. This has to be done after GC of
8301 sections, so that GCed sections are not merged, but before
8302 assigning dynamic symbols, since removing whole input sections
8304 bfd_merge_sections (link_info
.output_bfd
, &link_info
);
8306 /* Look for a text section and set the readonly attribute in it. */
8307 found
= bfd_get_section_by_name (link_info
.output_bfd
, ".text");
8311 if (config
.text_read_only
)
8312 found
->flags
|= SEC_READONLY
;
8314 found
->flags
&= ~SEC_READONLY
;
8318 /* Merge together CTF sections. After this, only the symtab-dependent
8319 function and data object sections need adjustment. */
8322 /* Emit the CTF, iff the emulation doesn't need to do late emission after
8323 examining things laid out late, like the strtab. */
8326 /* Copy forward lma regions for output sections in same lma region. */
8327 lang_propagate_lma_regions ();
8329 /* Defining __start/__stop symbols early for --gc-sections to work
8330 around a glibc build problem can result in these symbols being
8331 defined when they should not be. Fix them now. */
8332 if (config
.build_constructors
)
8333 lang_undef_start_stop ();
8335 /* Define .startof./.sizeof. symbols with preliminary values before
8336 dynamic symbols are created. */
8337 if (!bfd_link_relocatable (&link_info
))
8338 lang_init_startof_sizeof ();
8340 /* Do anything special before sizing sections. This is where ELF
8341 and other back-ends size dynamic sections. */
8342 ldemul_before_allocation ();
8344 /* We must record the program headers before we try to fix the
8345 section positions, since they will affect SIZEOF_HEADERS. */
8346 lang_record_phdrs ();
8348 /* Check relro sections. */
8349 if (link_info
.relro
&& !bfd_link_relocatable (&link_info
))
8350 lang_find_relro_sections ();
8352 /* Size up the sections. */
8353 lang_size_sections (NULL
, !RELAXATION_ENABLED
);
8355 /* See if anything special should be done now we know how big
8356 everything is. This is where relaxation is done. */
8357 ldemul_after_allocation ();
8359 /* Fix any __start, __stop, .startof. or .sizeof. symbols. */
8360 lang_finalize_start_stop ();
8362 /* Do all the assignments again, to report errors. Assignment
8363 statements are processed multiple times, updating symbols; In
8364 open_input_bfds, lang_do_assignments, and lang_size_sections.
8365 Since lang_relax_sections calls lang_do_assignments, symbols are
8366 also updated in ldemul_after_allocation. */
8367 lang_do_assignments (lang_final_phase_enum
);
8371 /* Convert absolute symbols to section relative. */
8372 ldexp_finalize_syms ();
8374 /* Make sure that the section addresses make sense. */
8375 if (command_line
.check_section_addresses
)
8376 lang_check_section_addresses ();
8378 if (link_info
.non_contiguous_regions
8379 && link_info
.non_contiguous_regions_warnings
)
8380 warn_non_contiguous_discards ();
8382 /* Check any required symbols are known. */
8383 ldlang_check_require_defined_symbols ();
8389 lang_add_version_string (void)
8391 if (! enable_linker_version
)
8394 const char * str
= "GNU ld ";
8395 int len
= strlen (str
);
8398 for (i
= 0 ; i
< len
; i
++)
8399 lang_add_data (BYTE
, exp_intop (str
[i
]));
8401 str
= BFD_VERSION_STRING
;
8404 for (i
= 0 ; i
< len
; i
++)
8405 lang_add_data (BYTE
, exp_intop (str
[i
]));
8407 lang_add_data (BYTE
, exp_intop ('\0'));
8410 /* EXPORTED TO YACC */
8413 lang_add_wild (struct wildcard_spec
*filespec
,
8414 struct wildcard_list
*section_list
,
8417 struct wildcard_list
*curr
, *next
;
8418 lang_wild_statement_type
*new_stmt
;
8419 bool any_specs_sorted
= false;
8421 /* Reverse the list as the parser puts it back to front. */
8422 for (curr
= section_list
, section_list
= NULL
;
8424 section_list
= curr
, curr
= next
)
8426 if (curr
->spec
.sorted
!= none
&& curr
->spec
.sorted
!= by_none
)
8427 any_specs_sorted
= true;
8429 curr
->next
= section_list
;
8432 if (filespec
!= NULL
&& filespec
->name
!= NULL
)
8434 if (strcmp (filespec
->name
, "*") == 0)
8435 filespec
->name
= NULL
;
8436 else if (!wildcardp (filespec
->name
))
8437 lang_has_input_file
= true;
8440 new_stmt
= new_stat (lang_wild_statement
, stat_ptr
);
8441 new_stmt
->filename
= NULL
;
8442 new_stmt
->filenames_sorted
= false;
8443 new_stmt
->any_specs_sorted
= any_specs_sorted
;
8444 new_stmt
->section_flag_list
= NULL
;
8445 new_stmt
->exclude_name_list
= NULL
;
8446 if (filespec
!= NULL
)
8448 new_stmt
->filename
= filespec
->name
;
8449 new_stmt
->filenames_sorted
= filespec
->sorted
== by_name
;
8450 new_stmt
->section_flag_list
= filespec
->section_flag_list
;
8451 new_stmt
->exclude_name_list
= filespec
->exclude_name_list
;
8453 new_stmt
->section_list
= section_list
;
8454 new_stmt
->keep_sections
= keep_sections
;
8455 lang_list_init (&new_stmt
->children
);
8456 lang_list_init (&new_stmt
->matching_sections
);
8457 analyze_walk_wild_section_handler (new_stmt
);
8460 printf ("wild %s(", new_stmt
->filename
? new_stmt
->filename
: "*");
8461 for (curr
= new_stmt
->section_list
; curr
; curr
= curr
->next
)
8462 printf ("%s ", curr
->spec
.name
? curr
->spec
.name
: "*");
8468 lang_section_start (const char *name
, etree_type
*address
,
8469 const segment_type
*segment
)
8471 lang_address_statement_type
*ad
;
8473 ad
= new_stat (lang_address_statement
, stat_ptr
);
8474 ad
->section_name
= name
;
8475 ad
->address
= address
;
8476 ad
->segment
= segment
;
8479 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
8480 because of a -e argument on the command line, or zero if this is
8481 called by ENTRY in a linker script. Command line arguments take
8485 lang_add_entry (const char *name
, bool cmdline
)
8487 if (entry_symbol
.name
== NULL
8489 || !entry_from_cmdline
)
8491 entry_symbol
.name
= name
;
8492 entry_from_cmdline
= cmdline
;
8496 /* Set the default start symbol to NAME. .em files should use this,
8497 not lang_add_entry, to override the use of "start" if neither the
8498 linker script nor the command line specifies an entry point. NAME
8499 must be permanently allocated. */
8501 lang_default_entry (const char *name
)
8503 entry_symbol_default
= name
;
8507 lang_add_target (const char *name
)
8509 lang_target_statement_type
*new_stmt
;
8511 new_stmt
= new_stat (lang_target_statement
, stat_ptr
);
8512 new_stmt
->target
= name
;
8516 lang_add_map (const char *name
)
8523 map_option_f
= true;
8531 lang_add_fill (fill_type
*fill
)
8533 lang_fill_statement_type
*new_stmt
;
8535 new_stmt
= new_stat (lang_fill_statement
, stat_ptr
);
8536 new_stmt
->fill
= fill
;
8540 lang_add_data (int type
, union etree_union
*exp
)
8542 lang_data_statement_type
*new_stmt
;
8544 new_stmt
= new_stat (lang_data_statement
, stat_ptr
);
8545 new_stmt
->exp
= exp
;
8546 new_stmt
->type
= type
;
8550 lang_add_string (const char *s
)
8552 bfd_vma len
= strlen (s
);
8554 bool escape
= false;
8556 /* Add byte expressions until end of string. */
8557 for (i
= 0 ; i
< len
; i
++)
8566 /* Ignore the escape. */
8569 case 'n': c
= '\n'; break;
8570 case 'r': c
= '\r'; break;
8571 case 't': c
= '\t'; break;
8581 /* We have an octal number. */
8583 unsigned int value
= c
- '0';
8586 if ((c
>= '0') && (c
<= '7'))
8594 if ((c
>= '0') && (c
<= '7'))
8605 /* octal: \777 is treated as '\077' + '7' */
8616 lang_add_data (BYTE
, exp_intop (c
));
8624 lang_add_data (BYTE
, exp_intop (c
));
8628 /* Remeber to terminate the string. */
8629 lang_add_data (BYTE
, exp_intop (0));
8632 /* Create a new reloc statement. RELOC is the BFD relocation type to
8633 generate. HOWTO is the corresponding howto structure (we could
8634 look this up, but the caller has already done so). SECTION is the
8635 section to generate a reloc against, or NAME is the name of the
8636 symbol to generate a reloc against. Exactly one of SECTION and
8637 NAME must be NULL. ADDEND is an expression for the addend. */
8640 lang_add_reloc (bfd_reloc_code_real_type reloc
,
8641 reloc_howto_type
*howto
,
8644 union etree_union
*addend
)
8646 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
8650 p
->section
= section
;
8652 p
->addend_exp
= addend
;
8654 p
->addend_value
= 0;
8655 p
->output_section
= NULL
;
8656 p
->output_offset
= 0;
8659 lang_assignment_statement_type
*
8660 lang_add_assignment (etree_type
*exp
)
8662 lang_assignment_statement_type
*new_stmt
;
8664 new_stmt
= new_stat (lang_assignment_statement
, stat_ptr
);
8665 new_stmt
->exp
= exp
;
8670 lang_add_attribute (enum statement_enum attribute
)
8672 new_statement (attribute
, sizeof (lang_statement_header_type
), stat_ptr
);
8676 lang_startup (const char *name
)
8678 if (first_file
->filename
!= NULL
)
8680 einfo (_("%F%P: multiple STARTUP files\n"));
8682 first_file
->filename
= name
;
8683 first_file
->local_sym_name
= name
;
8684 first_file
->flags
.real
= true;
8688 lang_float (bool maybe
)
8690 lang_float_flag
= maybe
;
8694 /* Work out the load- and run-time regions from a script statement, and
8695 store them in *LMA_REGION and *REGION respectively.
8697 MEMSPEC is the name of the run-time region, or the value of
8698 DEFAULT_MEMORY_REGION if the statement didn't specify one.
8699 LMA_MEMSPEC is the name of the load-time region, or null if the
8700 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
8701 had an explicit load address.
8703 It is an error to specify both a load region and a load address. */
8706 lang_get_regions (lang_memory_region_type
**region
,
8707 lang_memory_region_type
**lma_region
,
8708 const char *memspec
,
8709 const char *lma_memspec
,
8713 *lma_region
= lang_memory_region_lookup (lma_memspec
, false);
8715 /* If no runtime region or VMA has been specified, but the load region
8716 has been specified, then use the load region for the runtime region
8718 if (lma_memspec
!= NULL
8720 && strcmp (memspec
, DEFAULT_MEMORY_REGION
) == 0)
8721 *region
= *lma_region
;
8723 *region
= lang_memory_region_lookup (memspec
, false);
8725 if (have_lma
&& lma_memspec
!= 0)
8726 einfo (_("%X%P:%pS: section has both a load address and a load region\n"),
8731 lang_leave_output_section_statement (fill_type
*fill
, const char *memspec
,
8732 lang_output_section_phdr_list
*phdrs
,
8733 const char *lma_memspec
)
8735 lang_get_regions (¤t_section
->region
,
8736 ¤t_section
->lma_region
,
8737 memspec
, lma_memspec
,
8738 current_section
->load_base
!= NULL
,
8739 current_section
->addr_tree
!= NULL
);
8741 current_section
->fill
= fill
;
8742 current_section
->phdrs
= phdrs
;
8746 /* Set the output format type. -oformat overrides scripts. */
8749 lang_add_output_format (const char *format
,
8754 if (output_target
== NULL
|| !from_script
)
8756 if (command_line
.endian
== ENDIAN_BIG
8759 else if (command_line
.endian
== ENDIAN_LITTLE
8763 output_target
= format
;
8768 lang_add_insert (const char *where
, int is_before
)
8770 lang_insert_statement_type
*new_stmt
;
8772 new_stmt
= new_stat (lang_insert_statement
, stat_ptr
);
8773 new_stmt
->where
= where
;
8774 new_stmt
->is_before
= is_before
;
8775 saved_script_handle
= previous_script_handle
;
8778 /* Enter a group. This creates a new lang_group_statement, and sets
8779 stat_ptr to build new statements within the group. */
8782 lang_enter_group (void)
8784 lang_group_statement_type
*g
;
8786 g
= new_stat (lang_group_statement
, stat_ptr
);
8787 lang_list_init (&g
->children
);
8788 push_stat_ptr (&g
->children
);
8791 /* Leave a group. This just resets stat_ptr to start writing to the
8792 regular list of statements again. Note that this will not work if
8793 groups can occur inside anything else which can adjust stat_ptr,
8794 but currently they can't. */
8797 lang_leave_group (void)
8802 /* Add a new program header. This is called for each entry in a PHDRS
8803 command in a linker script. */
8806 lang_new_phdr (const char *name
,
8813 struct lang_phdr
*n
, **pp
;
8816 n
= stat_alloc (sizeof (struct lang_phdr
));
8819 n
->type
= exp_get_vma (type
, 0, "program header type");
8820 n
->filehdr
= filehdr
;
8825 hdrs
= n
->type
== 1 && (phdrs
|| filehdr
);
8827 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
8830 && !((*pp
)->filehdr
|| (*pp
)->phdrs
))
8832 einfo (_("%X%P:%pS: PHDRS and FILEHDR are not supported"
8833 " when prior PT_LOAD headers lack them\n"), NULL
);
8840 /* Record the program header information in the output BFD. FIXME: We
8841 should not be calling an ELF specific function here. */
8844 lang_record_phdrs (void)
8848 lang_output_section_phdr_list
*last
;
8849 struct lang_phdr
*l
;
8850 lang_output_section_statement_type
*os
;
8853 secs
= (asection
**) xmalloc (alc
* sizeof (asection
*));
8856 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
8863 for (os
= (void *) lang_os_list
.head
;
8867 lang_output_section_phdr_list
*pl
;
8869 if (os
->constraint
< 0)
8877 if (os
->sectype
== noload_section
8878 || os
->bfd_section
== NULL
8879 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
8882 /* Don't add orphans to PT_INTERP header. */
8888 lang_output_section_statement_type
*tmp_os
;
8890 /* If we have not run across a section with a program
8891 header assigned to it yet, then scan forwards to find
8892 one. This prevents inconsistencies in the linker's
8893 behaviour when a script has specified just a single
8894 header and there are sections in that script which are
8895 not assigned to it, and which occur before the first
8896 use of that header. See here for more details:
8897 http://sourceware.org/ml/binutils/2007-02/msg00291.html */
8898 for (tmp_os
= os
; tmp_os
; tmp_os
= tmp_os
->next
)
8901 last
= tmp_os
->phdrs
;
8905 einfo (_("%F%P: no sections assigned to phdrs\n"));
8910 if (os
->bfd_section
== NULL
)
8913 for (; pl
!= NULL
; pl
= pl
->next
)
8915 if (strcmp (pl
->name
, l
->name
) == 0)
8920 secs
= (asection
**) xrealloc (secs
,
8921 alc
* sizeof (asection
*));
8923 secs
[c
] = os
->bfd_section
;
8930 if (l
->flags
== NULL
)
8933 flags
= exp_get_vma (l
->flags
, 0, "phdr flags");
8938 at
= exp_get_vma (l
->at
, 0, "phdr load address");
8940 if (!bfd_record_phdr (link_info
.output_bfd
, l
->type
,
8941 l
->flags
!= NULL
, flags
, l
->at
!= NULL
,
8942 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
8943 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
8948 /* Make sure all the phdr assignments succeeded. */
8949 for (os
= (void *) lang_os_list
.head
;
8953 lang_output_section_phdr_list
*pl
;
8955 if (os
->constraint
< 0
8956 || os
->bfd_section
== NULL
)
8959 for (pl
= os
->phdrs
;
8962 if (!pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
8963 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
8964 os
->name
, pl
->name
);
8968 /* Record a list of sections which may not be cross referenced. */
8971 lang_add_nocrossref (lang_nocrossref_type
*l
)
8973 struct lang_nocrossrefs
*n
;
8975 n
= (struct lang_nocrossrefs
*) xmalloc (sizeof *n
);
8976 n
->next
= nocrossref_list
;
8978 n
->onlyfirst
= false;
8979 nocrossref_list
= n
;
8981 /* Set notice_all so that we get informed about all symbols. */
8982 link_info
.notice_all
= true;
8985 /* Record a section that cannot be referenced from a list of sections. */
8988 lang_add_nocrossref_to (lang_nocrossref_type
*l
)
8990 lang_add_nocrossref (l
);
8991 nocrossref_list
->onlyfirst
= true;
8994 /* Overlay handling. We handle overlays with some static variables. */
8996 /* The overlay virtual address. */
8997 static etree_type
*overlay_vma
;
8998 /* And subsection alignment. */
8999 static etree_type
*overlay_subalign
;
9001 /* An expression for the maximum section size seen so far. */
9002 static etree_type
*overlay_max
;
9004 /* A list of all the sections in this overlay. */
9006 struct overlay_list
{
9007 struct overlay_list
*next
;
9008 lang_output_section_statement_type
*os
;
9011 static struct overlay_list
*overlay_list
;
9013 /* Start handling an overlay. */
9016 lang_enter_overlay (etree_type
*vma_expr
, etree_type
*subalign
)
9018 /* The grammar should prevent nested overlays from occurring. */
9019 ASSERT (overlay_vma
== NULL
9020 && overlay_subalign
== NULL
9021 && overlay_max
== NULL
);
9023 overlay_vma
= vma_expr
;
9024 overlay_subalign
= subalign
;
9027 /* Start a section in an overlay. We handle this by calling
9028 lang_enter_output_section_statement with the correct VMA.
9029 lang_leave_overlay sets up the LMA and memory regions. */
9032 lang_enter_overlay_section (const char *name
)
9034 struct overlay_list
*n
;
9037 lang_enter_output_section_statement (name
, overlay_vma
, overlay_section
,
9038 0, 0, overlay_subalign
, 0, 0, 0);
9040 /* If this is the first section, then base the VMA of future
9041 sections on this one. This will work correctly even if `.' is
9042 used in the addresses. */
9043 if (overlay_list
== NULL
)
9044 overlay_vma
= exp_nameop (ADDR
, name
);
9046 /* Remember the section. */
9047 n
= (struct overlay_list
*) xmalloc (sizeof *n
);
9048 n
->os
= current_section
;
9049 n
->next
= overlay_list
;
9052 size
= exp_nameop (SIZEOF
, name
);
9054 /* Arrange to work out the maximum section end address. */
9055 if (overlay_max
== NULL
)
9058 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
9061 /* Finish a section in an overlay. There isn't any special to do
9065 lang_leave_overlay_section (fill_type
*fill
,
9066 lang_output_section_phdr_list
*phdrs
)
9073 name
= current_section
->name
;
9075 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
9076 region and that no load-time region has been specified. It doesn't
9077 really matter what we say here, since lang_leave_overlay will
9079 lang_leave_output_section_statement (fill
, DEFAULT_MEMORY_REGION
, phdrs
, 0);
9081 /* Define the magic symbols. */
9083 clean
= (char *) xmalloc (strlen (name
) + 1);
9085 for (s1
= name
; *s1
!= '\0'; s1
++)
9086 if (ISALNUM (*s1
) || *s1
== '_')
9090 buf
= (char *) xmalloc (strlen (clean
) + sizeof "__load_start_");
9091 sprintf (buf
, "__load_start_%s", clean
);
9092 lang_add_assignment (exp_provide (buf
,
9093 exp_nameop (LOADADDR
, name
),
9096 buf
= (char *) xmalloc (strlen (clean
) + sizeof "__load_stop_");
9097 sprintf (buf
, "__load_stop_%s", clean
);
9098 lang_add_assignment (exp_provide (buf
,
9100 exp_nameop (LOADADDR
, name
),
9101 exp_nameop (SIZEOF
, name
)),
9107 /* Finish an overlay. If there are any overlay wide settings, this
9108 looks through all the sections in the overlay and sets them. */
9111 lang_leave_overlay (etree_type
*lma_expr
,
9114 const char *memspec
,
9115 lang_output_section_phdr_list
*phdrs
,
9116 const char *lma_memspec
)
9118 lang_memory_region_type
*region
;
9119 lang_memory_region_type
*lma_region
;
9120 struct overlay_list
*l
;
9121 lang_nocrossref_type
*nocrossref
;
9123 lang_get_regions (®ion
, &lma_region
,
9124 memspec
, lma_memspec
,
9125 lma_expr
!= NULL
, false);
9129 /* After setting the size of the last section, set '.' to end of the
9131 if (overlay_list
!= NULL
)
9133 overlay_list
->os
->update_dot
= 1;
9134 overlay_list
->os
->update_dot_tree
9135 = exp_assign (".", exp_binop ('+', overlay_vma
, overlay_max
), false);
9141 struct overlay_list
*next
;
9143 if (fill
!= NULL
&& l
->os
->fill
== NULL
)
9146 l
->os
->region
= region
;
9147 l
->os
->lma_region
= lma_region
;
9149 /* The first section has the load address specified in the
9150 OVERLAY statement. The rest are worked out from that.
9151 The base address is not needed (and should be null) if
9152 an LMA region was specified. */
9155 l
->os
->load_base
= lma_expr
;
9156 l
->os
->sectype
= first_overlay_section
;
9158 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
9159 l
->os
->phdrs
= phdrs
;
9163 lang_nocrossref_type
*nc
;
9165 nc
= (lang_nocrossref_type
*) xmalloc (sizeof *nc
);
9166 nc
->name
= l
->os
->name
;
9167 nc
->next
= nocrossref
;
9176 if (nocrossref
!= NULL
)
9177 lang_add_nocrossref (nocrossref
);
9180 overlay_list
= NULL
;
9182 overlay_subalign
= NULL
;
9185 /* Version handling. This is only useful for ELF. */
9187 /* If PREV is NULL, return first version pattern matching particular symbol.
9188 If PREV is non-NULL, return first version pattern matching particular
9189 symbol after PREV (previously returned by lang_vers_match). */
9191 static struct bfd_elf_version_expr
*
9192 lang_vers_match (struct bfd_elf_version_expr_head
*head
,
9193 struct bfd_elf_version_expr
*prev
,
9197 const char *cxx_sym
= sym
;
9198 const char *java_sym
= sym
;
9199 struct bfd_elf_version_expr
*expr
= NULL
;
9200 enum demangling_styles curr_style
;
9202 curr_style
= CURRENT_DEMANGLING_STYLE
;
9203 cplus_demangle_set_style (no_demangling
);
9204 c_sym
= bfd_demangle (link_info
.output_bfd
, sym
, DMGL_NO_OPTS
);
9207 cplus_demangle_set_style (curr_style
);
9209 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
9211 cxx_sym
= bfd_demangle (link_info
.output_bfd
, sym
,
9212 DMGL_PARAMS
| DMGL_ANSI
);
9216 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
9218 java_sym
= bfd_demangle (link_info
.output_bfd
, sym
, DMGL_JAVA
);
9223 if (head
->htab
&& (prev
== NULL
|| prev
->literal
))
9225 struct bfd_elf_version_expr e
;
9227 switch (prev
? prev
->mask
: 0)
9230 if (head
->mask
& BFD_ELF_VERSION_C_TYPE
)
9233 expr
= (struct bfd_elf_version_expr
*)
9234 htab_find ((htab_t
) head
->htab
, &e
);
9235 while (expr
&& strcmp (expr
->pattern
, c_sym
) == 0)
9236 if (expr
->mask
== BFD_ELF_VERSION_C_TYPE
)
9242 case BFD_ELF_VERSION_C_TYPE
:
9243 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
9245 e
.pattern
= cxx_sym
;
9246 expr
= (struct bfd_elf_version_expr
*)
9247 htab_find ((htab_t
) head
->htab
, &e
);
9248 while (expr
&& strcmp (expr
->pattern
, cxx_sym
) == 0)
9249 if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
9255 case BFD_ELF_VERSION_CXX_TYPE
:
9256 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
9258 e
.pattern
= java_sym
;
9259 expr
= (struct bfd_elf_version_expr
*)
9260 htab_find ((htab_t
) head
->htab
, &e
);
9261 while (expr
&& strcmp (expr
->pattern
, java_sym
) == 0)
9262 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
9273 /* Finally, try the wildcards. */
9274 if (prev
== NULL
|| prev
->literal
)
9275 expr
= head
->remaining
;
9278 for (; expr
; expr
= expr
->next
)
9285 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
9288 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
9290 else if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
9294 if (fnmatch (expr
->pattern
, s
, 0) == 0)
9300 free ((char *) c_sym
);
9302 free ((char *) cxx_sym
);
9303 if (java_sym
!= sym
)
9304 free ((char *) java_sym
);
9308 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
9309 return a pointer to the symbol name with any backslash quotes removed. */
9312 realsymbol (const char *pattern
)
9315 bool changed
= false, backslash
= false;
9316 char *s
, *symbol
= (char *) xmalloc (strlen (pattern
) + 1);
9318 for (p
= pattern
, s
= symbol
; *p
!= '\0'; ++p
)
9320 /* It is a glob pattern only if there is no preceding
9324 /* Remove the preceding backslash. */
9331 if (*p
== '?' || *p
== '*' || *p
== '[')
9338 backslash
= *p
== '\\';
9354 /* This is called for each variable name or match expression. NEW_NAME is
9355 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
9356 pattern to be matched against symbol names. */
9358 struct bfd_elf_version_expr
*
9359 lang_new_vers_pattern (struct bfd_elf_version_expr
*orig
,
9360 const char *new_name
,
9364 struct bfd_elf_version_expr
*ret
;
9366 ret
= (struct bfd_elf_version_expr
*) xmalloc (sizeof *ret
);
9370 ret
->literal
= true;
9371 ret
->pattern
= literal_p
? new_name
: realsymbol (new_name
);
9372 if (ret
->pattern
== NULL
)
9374 ret
->pattern
= new_name
;
9375 ret
->literal
= false;
9378 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
9379 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
9380 else if (strcasecmp (lang
, "C++") == 0)
9381 ret
->mask
= BFD_ELF_VERSION_CXX_TYPE
;
9382 else if (strcasecmp (lang
, "Java") == 0)
9383 ret
->mask
= BFD_ELF_VERSION_JAVA_TYPE
;
9386 einfo (_("%X%P: unknown language `%s' in version information\n"),
9388 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
9391 return ldemul_new_vers_pattern (ret
);
9394 /* This is called for each set of variable names and match
9397 struct bfd_elf_version_tree
*
9398 lang_new_vers_node (struct bfd_elf_version_expr
*globals
,
9399 struct bfd_elf_version_expr
*locals
)
9401 struct bfd_elf_version_tree
*ret
;
9403 ret
= (struct bfd_elf_version_tree
*) xcalloc (1, sizeof *ret
);
9404 ret
->globals
.list
= globals
;
9405 ret
->locals
.list
= locals
;
9406 ret
->match
= lang_vers_match
;
9407 ret
->name_indx
= (unsigned int) -1;
9411 /* This static variable keeps track of version indices. */
9413 static int version_index
;
9416 version_expr_head_hash (const void *p
)
9418 const struct bfd_elf_version_expr
*e
=
9419 (const struct bfd_elf_version_expr
*) p
;
9421 return htab_hash_string (e
->pattern
);
9425 version_expr_head_eq (const void *p1
, const void *p2
)
9427 const struct bfd_elf_version_expr
*e1
=
9428 (const struct bfd_elf_version_expr
*) p1
;
9429 const struct bfd_elf_version_expr
*e2
=
9430 (const struct bfd_elf_version_expr
*) p2
;
9432 return strcmp (e1
->pattern
, e2
->pattern
) == 0;
9436 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head
*head
)
9439 struct bfd_elf_version_expr
*e
, *next
;
9440 struct bfd_elf_version_expr
**list_loc
, **remaining_loc
;
9442 for (e
= head
->list
; e
; e
= e
->next
)
9446 head
->mask
|= e
->mask
;
9451 head
->htab
= htab_create (count
* 2, version_expr_head_hash
,
9452 version_expr_head_eq
, NULL
);
9453 list_loc
= &head
->list
;
9454 remaining_loc
= &head
->remaining
;
9455 for (e
= head
->list
; e
; e
= next
)
9461 remaining_loc
= &e
->next
;
9465 void **loc
= htab_find_slot ((htab_t
) head
->htab
, e
, INSERT
);
9469 struct bfd_elf_version_expr
*e1
, *last
;
9471 e1
= (struct bfd_elf_version_expr
*) *loc
;
9475 if (e1
->mask
== e
->mask
)
9483 while (e1
&& strcmp (e1
->pattern
, e
->pattern
) == 0);
9487 /* This is a duplicate. */
9488 /* FIXME: Memory leak. Sometimes pattern is not
9489 xmalloced alone, but in larger chunk of memory. */
9490 /* free (e->pattern); */
9495 e
->next
= last
->next
;
9503 list_loc
= &e
->next
;
9507 *remaining_loc
= NULL
;
9508 *list_loc
= head
->remaining
;
9511 head
->remaining
= head
->list
;
9514 /* This is called when we know the name and dependencies of the
9518 lang_register_vers_node (const char *name
,
9519 struct bfd_elf_version_tree
*version
,
9520 struct bfd_elf_version_deps
*deps
)
9522 struct bfd_elf_version_tree
*t
, **pp
;
9523 struct bfd_elf_version_expr
*e1
;
9528 if (link_info
.version_info
!= NULL
9529 && (name
[0] == '\0' || link_info
.version_info
->name
[0] == '\0'))
9531 einfo (_("%X%P: anonymous version tag cannot be combined"
9532 " with other version tags\n"));
9537 /* Make sure this node has a unique name. */
9538 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9539 if (strcmp (t
->name
, name
) == 0)
9540 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
9542 lang_finalize_version_expr_head (&version
->globals
);
9543 lang_finalize_version_expr_head (&version
->locals
);
9545 /* Check the global and local match names, and make sure there
9546 aren't any duplicates. */
9548 for (e1
= version
->globals
.list
; e1
!= NULL
; e1
= e1
->next
)
9550 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9552 struct bfd_elf_version_expr
*e2
;
9554 if (t
->locals
.htab
&& e1
->literal
)
9556 e2
= (struct bfd_elf_version_expr
*)
9557 htab_find ((htab_t
) t
->locals
.htab
, e1
);
9558 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
9560 if (e1
->mask
== e2
->mask
)
9561 einfo (_("%X%P: duplicate expression `%s'"
9562 " in version information\n"), e1
->pattern
);
9566 else if (!e1
->literal
)
9567 for (e2
= t
->locals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
9568 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
9569 && e1
->mask
== e2
->mask
)
9570 einfo (_("%X%P: duplicate expression `%s'"
9571 " in version information\n"), e1
->pattern
);
9575 for (e1
= version
->locals
.list
; e1
!= NULL
; e1
= e1
->next
)
9577 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9579 struct bfd_elf_version_expr
*e2
;
9581 if (t
->globals
.htab
&& e1
->literal
)
9583 e2
= (struct bfd_elf_version_expr
*)
9584 htab_find ((htab_t
) t
->globals
.htab
, e1
);
9585 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
9587 if (e1
->mask
== e2
->mask
)
9588 einfo (_("%X%P: duplicate expression `%s'"
9589 " in version information\n"),
9594 else if (!e1
->literal
)
9595 for (e2
= t
->globals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
9596 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
9597 && e1
->mask
== e2
->mask
)
9598 einfo (_("%X%P: duplicate expression `%s'"
9599 " in version information\n"), e1
->pattern
);
9603 version
->deps
= deps
;
9604 version
->name
= name
;
9605 if (name
[0] != '\0')
9608 version
->vernum
= version_index
;
9611 version
->vernum
= 0;
9613 for (pp
= &link_info
.version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
9618 /* This is called when we see a version dependency. */
9620 struct bfd_elf_version_deps
*
9621 lang_add_vers_depend (struct bfd_elf_version_deps
*list
, const char *name
)
9623 struct bfd_elf_version_deps
*ret
;
9624 struct bfd_elf_version_tree
*t
;
9626 ret
= (struct bfd_elf_version_deps
*) xmalloc (sizeof *ret
);
9629 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9631 if (strcmp (t
->name
, name
) == 0)
9633 ret
->version_needed
= t
;
9638 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
9640 ret
->version_needed
= NULL
;
9645 lang_do_version_exports_section (void)
9647 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
9649 LANG_FOR_EACH_INPUT_STATEMENT (is
)
9651 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
9659 contents
= (char *) xmalloc (len
);
9660 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
9661 einfo (_("%X%P: unable to read .exports section contents\n"), sec
);
9664 while (p
< contents
+ len
)
9666 greg
= lang_new_vers_pattern (greg
, p
, NULL
, false);
9667 p
= strchr (p
, '\0') + 1;
9670 /* Do not free the contents, as we used them creating the regex. */
9672 /* Do not include this section in the link. */
9673 sec
->flags
|= SEC_EXCLUDE
| SEC_KEEP
;
9676 lreg
= lang_new_vers_pattern (NULL
, "*", NULL
, false);
9677 lang_register_vers_node (command_line
.version_exports_section
,
9678 lang_new_vers_node (greg
, lreg
), NULL
);
9681 /* Evaluate LENGTH and ORIGIN parts of MEMORY spec. This is initially
9682 called with UPDATE_REGIONS_P set to FALSE, in this case no errors are
9683 thrown, however, references to symbols in the origin and length fields
9684 will be pushed into the symbol table, this allows PROVIDE statements to
9685 then provide these symbols. This function is called a second time with
9686 UPDATE_REGIONS_P set to TRUE, this time the we update the actual region
9687 data structures, and throw errors if missing symbols are encountered. */
9690 lang_do_memory_regions (bool update_regions_p
)
9692 lang_memory_region_type
*r
= lang_memory_region_list
;
9694 for (; r
!= NULL
; r
= r
->next
)
9698 exp_fold_tree_no_dot (r
->origin_exp
);
9699 if (update_regions_p
)
9701 if (expld
.result
.valid_p
)
9703 r
->origin
= expld
.result
.value
;
9704 r
->current
= r
->origin
;
9707 einfo (_("%P: invalid origin for memory region %s\n"),
9713 exp_fold_tree_no_dot (r
->length_exp
);
9714 if (update_regions_p
)
9716 if (expld
.result
.valid_p
)
9717 r
->length
= expld
.result
.value
;
9719 einfo (_("%P: invalid length for memory region %s\n"),
9727 lang_add_unique (const char *name
)
9729 struct unique_sections
*ent
;
9731 for (ent
= unique_section_list
; ent
; ent
= ent
->next
)
9732 if (strcmp (ent
->name
, name
) == 0)
9735 ent
= (struct unique_sections
*) xmalloc (sizeof *ent
);
9736 ent
->name
= xstrdup (name
);
9737 ent
->next
= unique_section_list
;
9738 unique_section_list
= ent
;
9741 /* Append the list of dynamic symbols to the existing one. */
9744 lang_append_dynamic_list (struct bfd_elf_dynamic_list
**list_p
,
9745 struct bfd_elf_version_expr
*dynamic
)
9749 struct bfd_elf_version_expr
*tail
;
9750 for (tail
= dynamic
; tail
->next
!= NULL
; tail
= tail
->next
)
9752 tail
->next
= (*list_p
)->head
.list
;
9753 (*list_p
)->head
.list
= dynamic
;
9757 struct bfd_elf_dynamic_list
*d
;
9759 d
= (struct bfd_elf_dynamic_list
*) xcalloc (1, sizeof *d
);
9760 d
->head
.list
= dynamic
;
9761 d
->match
= lang_vers_match
;
9766 /* Append the list of C++ typeinfo dynamic symbols to the existing
9770 lang_append_dynamic_list_cpp_typeinfo (void)
9772 const char *symbols
[] =
9774 "typeinfo name for*",
9777 struct bfd_elf_version_expr
*dynamic
= NULL
;
9780 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
9781 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
9784 lang_append_dynamic_list (&link_info
.dynamic_list
, dynamic
);
9787 /* Append the list of C++ operator new and delete dynamic symbols to the
9791 lang_append_dynamic_list_cpp_new (void)
9793 const char *symbols
[] =
9798 struct bfd_elf_version_expr
*dynamic
= NULL
;
9801 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
9802 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
9805 lang_append_dynamic_list (&link_info
.dynamic_list
, dynamic
);
9808 /* Scan a space and/or comma separated string of features. */
9811 lang_ld_feature (char *str
)
9819 while (*p
== ',' || ISSPACE (*p
))
9824 while (*q
&& *q
!= ',' && !ISSPACE (*q
))
9828 if (strcasecmp (p
, "SANE_EXPR") == 0)
9829 config
.sane_expr
= true;
9831 einfo (_("%X%P: unknown feature `%s'\n"), p
);
9837 /* Pretty print memory amount. */
9840 lang_print_memory_size (uint64_t sz
)
9842 if ((sz
& 0x3fffffff) == 0)
9843 printf ("%10" PRIu64
" GB", sz
>> 30);
9844 else if ((sz
& 0xfffff) == 0)
9845 printf ("%10" PRIu64
" MB", sz
>> 20);
9846 else if ((sz
& 0x3ff) == 0)
9847 printf ("%10" PRIu64
" KB", sz
>> 10);
9849 printf (" %10" PRIu64
" B", sz
);
9852 /* Implement --print-memory-usage: disply per region memory usage. */
9855 lang_print_memory_usage (void)
9857 lang_memory_region_type
*r
;
9859 printf ("Memory region Used Size Region Size %%age Used\n");
9860 for (r
= lang_memory_region_list
; r
->next
!= NULL
; r
= r
->next
)
9862 bfd_vma used_length
= r
->current
- r
->origin
;
9864 printf ("%16s: ",r
->name_list
.name
);
9865 lang_print_memory_size (used_length
);
9866 lang_print_memory_size (r
->length
);
9870 double percent
= used_length
* 100.0 / r
->length
;
9871 printf (" %6.2f%%", percent
);