1 /* Linker command language support.
2 Copyright (C) 1991-2025 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"
47 #if BFD_SUPPORTS_PLUGINS
51 /* FIXME: Put it here to avoid NAME conflict from ldgram.h. */
55 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
58 /* Convert between addresses in bytes and sizes in octets.
59 For currently supported targets, octets_per_byte is always a power
60 of two, so we can use shifts. */
61 #define TO_ADDR(X) ((X) >> opb_shift)
62 #define TO_SIZE(X) ((X) << opb_shift)
64 /* Local variables. */
65 static struct obstack stat_obstack
;
66 static struct obstack map_obstack
;
67 static struct obstack pt_obstack
;
69 #define obstack_chunk_alloc xmalloc
70 #define obstack_chunk_free free
71 static const char *entry_symbol_default
= "start";
72 static bool map_head_is_link_order
= false;
73 static lang_output_section_statement_type
*default_common_section
;
74 static bool map_option_f
;
75 static bfd_vma print_dot
;
76 static lang_input_statement_type
*first_file
;
77 static const char *current_target
;
78 static lang_statement_list_type
*stat_save
[10];
79 static lang_statement_list_type
**stat_save_ptr
= &stat_save
[0];
80 static struct unique_sections
*unique_section_list
;
81 static struct asneeded_minfo
*asneeded_list_head
;
82 static unsigned int opb_shift
= 0;
83 static cmdline_list_type cmdline_object_only_file_list
;
84 static cmdline_list_type cmdline_object_only_archive_list
;
85 static cmdline_list_type cmdline_temp_object_only_list
;
87 /* Forward declarations. */
88 static void exp_init_os (etree_type
*);
89 static lang_input_statement_type
*lookup_name (const char *);
90 static bool wont_add_section_p (asection
*,
91 lang_output_section_statement_type
*);
92 static void insert_undefined (const char *);
93 static bool sort_def_symbol (struct bfd_link_hash_entry
*, void *);
94 static lang_statement_union_type
*new_statement (enum statement_enum type
,
96 lang_statement_list_type
*list
);
97 static void print_statement (lang_statement_union_type
*,
98 lang_output_section_statement_type
*);
99 static void print_statement_list (lang_statement_union_type
*,
100 lang_output_section_statement_type
*);
101 static void print_statements (void);
102 static void print_input_section (asection
*, bool);
103 static bool lang_one_common (struct bfd_link_hash_entry
*, void *);
104 static void lang_record_phdrs (void);
105 static void lang_do_version_exports_section (void);
106 static void lang_finalize_version_expr_head
107 (struct bfd_elf_version_expr_head
*);
108 static void lang_do_memory_regions (bool);
109 static void cmdline_lists_init (void);
110 static void cmdline_get_object_only_input_files (void);
111 static void print_cmdline_list (cmdline_union_type
*);
112 static bool cmdline_on_object_only_archive_list_p (bfd
*);
114 /* Exported variables. */
115 const char *output_target
;
116 lang_output_section_statement_type
*abs_output_section
;
117 /* Header for list of statements corresponding to any files involved in the
118 link, either specified from the command-line or added implicitely (eg.
119 archive member used to resolved undefined symbol, wildcard statement from
120 linker script, etc.). Next pointer is in next field of a
121 lang_statement_header_type (reached via header field in a
122 lang_statement_union). */
123 lang_statement_list_type statement_list
;
124 lang_statement_list_type lang_os_list
;
125 lang_statement_list_type
*stat_ptr
= &statement_list
;
126 /* Header for list of statements corresponding to files used in the final
127 executable. This can be either object file specified on the command-line
128 or library member resolving an undefined reference. Next pointer is in next
129 field of a lang_input_statement_type (reached via input_statement field in a
130 lang_statement_union). */
131 lang_statement_list_type file_chain
= { NULL
, NULL
};
132 /* Header for list of statements corresponding to files specified on the
133 command-line for linking. It thus contains real object files and archive
134 but not archive members. Next pointer is in next_real_file field of a
135 lang_input_statement_type statement (reached via input_statement field in a
136 lang_statement_union). */
137 lang_statement_list_type input_file_chain
;
138 static const char *current_input_file
;
139 struct bfd_elf_dynamic_list
**current_dynamic_list_p
;
140 struct bfd_sym_chain entry_symbol
= { NULL
, NULL
};
141 const char *entry_section
= ".text";
142 struct lang_input_statement_flags input_flags
;
143 bool entry_from_cmdline
;
144 bool lang_has_input_file
= false;
145 bool had_output_filename
= false;
146 bool lang_float_flag
= false;
147 bool delete_output_file_on_failure
= false;
148 bool enable_linker_version
= false;
149 struct lang_phdr
*lang_phdr_list
;
150 struct lang_nocrossrefs
*nocrossref_list
;
151 struct asneeded_minfo
**asneeded_list_tail
;
153 static ctf_dict_t
*ctf_output
;
156 /* Functions that traverse the linker script and might evaluate
157 DEFINED() need to increment this at the start of the traversal. */
158 int lang_statement_iteration
= 0;
160 /* Count times through one_lang_size_sections_pass after mark phase. */
161 static int lang_sizing_iteration
= 0;
163 /* Return TRUE if the PATTERN argument is a wildcard pattern.
164 Although backslashes are treated specially if a pattern contains
165 wildcards, we do not consider the mere presence of a backslash to
166 be enough to cause the pattern to be treated as a wildcard.
167 That lets us handle DOS filenames more naturally. */
168 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
170 #define new_stat(x, y) \
171 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
173 #define outside_section_address(q) \
174 ((q)->output_offset + (q)->output_section->vma)
176 #define outside_symbol_address(q) \
177 ((q)->value + outside_section_address (q->section))
179 /* CTF sections smaller than this are not compressed: compression of
180 dictionaries this small doesn't gain much, and this lets consumers mmap the
181 sections directly out of the ELF file and use them with no decompression
182 overhead if they want to. */
183 #define CTF_COMPRESSION_THRESHOLD 4096
186 stat_alloc (size_t size
)
188 return obstack_alloc (&stat_obstack
, size
);
192 stat_free (void *str
)
194 obstack_free (&stat_obstack
, str
);
198 stat_memdup (const void *src
, size_t copy_size
, size_t alloc_size
)
200 void *ret
= obstack_alloc (&stat_obstack
, alloc_size
);
201 memcpy (ret
, src
, copy_size
);
202 if (alloc_size
> copy_size
)
203 memset ((char *) ret
+ copy_size
, 0, alloc_size
- copy_size
);
208 stat_strdup (const char *str
)
210 size_t len
= strlen (str
) + 1;
211 return stat_memdup (str
, len
, len
);
215 stat_concat (const char *first
, ...)
218 va_start (args
, first
);
221 for (const char *arg
= first
; arg
; arg
= va_arg (args
, const char *))
222 length
+= strlen (arg
);
224 char *new_str
= stat_alloc (length
+ 1);
226 va_start (args
, first
);
228 for (const char *arg
= first
; arg
; arg
= va_arg (args
, const char *))
230 length
= strlen (arg
);
231 memcpy (end
, arg
, length
);
239 /* Code for handling simple wildcards without going through fnmatch,
240 which can be expensive because of charset translations etc. */
242 /* A simple wild is a literal string followed by a single '*',
243 where the literal part is at least 4 characters long. */
246 is_simple_wild (const char *name
)
248 size_t len
= strcspn (name
, "*?[");
249 return len
>= 4 && name
[len
] == '*' && name
[len
+ 1] == '\0';
253 match_simple_wild (const char *pattern
, const char *name
)
255 /* The first four characters of the pattern are guaranteed valid
256 non-wildcard characters. So we can go faster. */
257 if (pattern
[0] != name
[0] || pattern
[1] != name
[1]
258 || pattern
[2] != name
[2] || pattern
[3] != name
[3])
263 while (*pattern
!= '*')
264 if (*name
++ != *pattern
++)
271 name_match (const char *pattern
, const char *name
)
273 if (is_simple_wild (pattern
))
274 return !match_simple_wild (pattern
, name
);
275 if (wildcardp (pattern
))
276 return fnmatch (pattern
, name
, 0);
277 return strcmp (pattern
, name
);
280 /* Given an analyzed wildcard_spec SPEC, match it against NAME,
281 returns zero on a match, non-zero if there's no match. */
284 spec_match (const struct wildcard_spec
*spec
, const char *name
)
286 size_t nl
= spec
->namelen
;
287 size_t pl
= spec
->prefixlen
;
288 size_t sl
= spec
->suffixlen
;
289 size_t inputlen
= strlen (name
);
297 r
= memcmp (spec
->name
, name
, pl
);
307 r
= memcmp (spec
->name
+ nl
- sl
, name
+ inputlen
- sl
, sl
);
312 if (nl
== pl
+ sl
+ 1 && spec
->name
[pl
] == '*')
316 return fnmatch (spec
->name
+ pl
, name
+ pl
, 0);
325 stat_ldirname (const char *name
)
327 const char *base
= lbasename (name
);
329 while (base
> name
&& IS_DIR_SEPARATOR (base
[-1]))
331 size_t len
= base
- name
;
334 return stat_memdup (name
, len
, len
+ 1);
337 /* If PATTERN is of the form archive:file, return a pointer to the
338 separator. If not, return NULL. */
341 archive_path (const char *pattern
)
345 if (link_info
.path_separator
== 0)
348 p
= strchr (pattern
, link_info
.path_separator
);
349 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
350 if (p
== NULL
|| link_info
.path_separator
!= ':')
353 /* Assume a match on the second char is part of drive specifier,
354 as in "c:\silly.dos". */
355 if (p
== pattern
+ 1 && ISALPHA (*pattern
))
356 p
= strchr (p
+ 1, link_info
.path_separator
);
361 /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path,
362 return whether F matches FILE_SPEC. */
365 input_statement_is_archive_path (const char *file_spec
, char *sep
,
366 lang_input_statement_type
*f
)
371 || name_match (sep
+ 1, f
->filename
) == 0)
372 && ((sep
!= file_spec
)
373 == (f
->the_bfd
!= NULL
&& f
->the_bfd
->my_archive
!= NULL
)))
377 if (sep
!= file_spec
)
379 const char *aname
= bfd_get_filename (f
->the_bfd
->my_archive
);
381 match
= name_match (file_spec
, aname
) == 0;
382 *sep
= link_info
.path_separator
;
389 unique_section_p (const asection
*sec
,
390 const lang_output_section_statement_type
*os
)
392 struct unique_sections
*unam
;
395 if (!link_info
.resolve_section_groups
396 && sec
->owner
!= NULL
397 && bfd_is_group_section (sec
->owner
, sec
))
399 && strcmp (os
->name
, DISCARD_SECTION_NAME
) == 0);
402 for (unam
= unique_section_list
; unam
; unam
= unam
->next
)
403 if (name_match (unam
->name
, secnam
) == 0)
409 /* Generic traversal routines for finding matching sections. */
411 /* Return true if FILE matches a pattern in EXCLUDE_LIST, otherwise return
415 walk_wild_file_in_exclude_list (struct name_list
*exclude_list
,
416 lang_input_statement_type
*file
)
418 struct name_list
*list_tmp
;
420 for (list_tmp
= exclude_list
;
422 list_tmp
= list_tmp
->next
)
424 char *p
= archive_path (list_tmp
->name
);
428 if (input_statement_is_archive_path (list_tmp
->name
, p
, file
))
432 else if (name_match (list_tmp
->name
, file
->filename
) == 0)
435 /* FIXME: Perhaps remove the following at some stage? Matching
436 unadorned archives like this was never documented and has
437 been superceded by the archive:path syntax. */
438 else if (file
->the_bfd
!= NULL
439 && file
->the_bfd
->my_archive
!= NULL
440 && name_match (list_tmp
->name
,
441 bfd_get_filename (file
->the_bfd
->my_archive
)) == 0)
448 /* Add SECTION (from input FILE) to the list of matching sections
449 within PTR (the matching wildcard is SEC). */
452 add_matching_section (lang_wild_statement_type
*ptr
,
453 struct wildcard_list
*sec
,
455 lang_input_statement_type
*file
)
457 lang_input_matcher_type
*new_section
;
458 /* Add a section reference to the list. */
459 new_section
= new_stat (lang_input_matcher
, &ptr
->matching_sections
);
460 new_section
->section
= section
;
461 new_section
->pattern
= sec
;
462 new_section
->input_stmt
= file
;
465 /* Process section S (from input file FILE) in relation to wildcard
466 statement PTR. We already know that a prefix of the name of S matches
467 some wildcard in PTR's wildcard list. Here we check if the filename
468 matches as well (if it's specified) and if any of the wildcards in fact
472 walk_wild_section_match (lang_wild_statement_type
*ptr
,
473 lang_input_statement_type
*file
,
476 struct wildcard_list
*sec
;
477 const char *file_spec
= ptr
->filename
;
480 /* Check if filenames match. */
481 if (file_spec
== NULL
)
483 else if ((p
= archive_path (file_spec
)) != NULL
)
485 if (!input_statement_is_archive_path (file_spec
, p
, file
))
488 else if (wildcardp (file_spec
))
490 if (fnmatch (file_spec
, file
->filename
, 0) != 0)
495 /* XXX Matching against non-wildcard filename in wild statements
496 was done by going through lookup_name, which uses
497 ->local_sym_name to compare against, not ->filename. We retain
498 this behaviour even though the above code paths use filename.
499 It would be more logical to use it here as well, in which
500 case the above wildcard() arm could be folded into this by using
501 name_match. This would also solve the worry of what to do
502 about unset local_sym_name (in which case lookup_name simply adds
503 the input file again). */
504 const char *filename
= file
->local_sym_name
;
505 lang_input_statement_type
*arch_is
;
506 if (filename
&& filename_cmp (filename
, file_spec
) == 0)
508 /* FIXME: see also walk_wild_file_in_exclude_list for why we
509 also check parents BFD (local_sym_)name to match input statements
510 with unadorned archive names. */
511 else if (file
->the_bfd
512 && file
->the_bfd
->my_archive
513 && (arch_is
= bfd_usrdata (file
->the_bfd
->my_archive
))
514 && arch_is
->local_sym_name
515 && filename_cmp (arch_is
->local_sym_name
, file_spec
) == 0)
521 /* If filename is excluded we're done. */
522 if (walk_wild_file_in_exclude_list (ptr
->exclude_name_list
, file
))
525 /* Check section name against each wildcard spec. If there's no
526 wildcard all sections match. */
527 sec
= ptr
->section_list
;
529 add_matching_section (ptr
, sec
, s
, file
);
532 const char *sname
= bfd_section_name (s
);
533 for (; sec
!= NULL
; sec
= sec
->next
)
535 if (sec
->spec
.name
!= NULL
536 && spec_match (&sec
->spec
, sname
) != 0)
539 /* Don't process sections from files which were excluded. */
540 if (!walk_wild_file_in_exclude_list (sec
->spec
.exclude_name_list
,
542 add_matching_section (ptr
, sec
, s
, file
);
547 /* Return the numerical value of the init_priority attribute from
548 section name NAME. */
551 get_init_priority (const asection
*sec
)
553 const char *name
= bfd_section_name (sec
);
556 /* GCC uses the following section names for the init_priority
557 attribute with numerical values 101 to 65535 inclusive. A
558 lower value means a higher priority.
560 1: .init_array.NNNNN/.fini_array.NNNNN: Where NNNNN is the
561 decimal numerical value of the init_priority attribute.
562 The order of execution in .init_array is forward and
563 .fini_array is backward.
564 2: .ctors.NNNNN/.dtors.NNNNN: Where NNNNN is 65535 minus the
565 decimal numerical value of the init_priority attribute.
566 The order of execution in .ctors is backward and .dtors
569 .init_array.NNNNN sections would normally be placed in an output
570 .init_array section, .fini_array.NNNNN in .fini_array,
571 .ctors.NNNNN in .ctors, and .dtors.NNNNN in .dtors. This means
572 we should sort by increasing number (and could just use
573 SORT_BY_NAME in scripts). However if .ctors.NNNNN sections are
574 being placed in .init_array (which may also contain
575 .init_array.NNNNN sections) or .dtors.NNNNN sections are being
576 placed in .fini_array then we need to extract the init_priority
577 attribute and sort on that. */
578 dot
= strrchr (name
, '.');
579 if (dot
!= NULL
&& ISDIGIT (dot
[1]))
582 unsigned long init_priority
= strtoul (dot
+ 1, &end
, 10);
586 && (strncmp (name
, ".ctors", 6) == 0
587 || strncmp (name
, ".dtors", 6) == 0))
588 init_priority
= 65535 - init_priority
;
589 if (init_priority
<= INT_MAX
)
590 return init_priority
;
596 /* Compare sections ASEC and BSEC according to SORT. */
599 compare_section (sort_type sort
, asection
*asec
, asection
*bsec
, bool reversed
)
602 int a_priority
, b_priority
;
609 case by_init_priority
:
610 a_priority
= get_init_priority (asec
);
611 b_priority
= get_init_priority (bsec
);
612 if (a_priority
< 0 || b_priority
< 0)
615 ret
= b_priority
- a_priority
;
617 ret
= a_priority
- b_priority
;
623 case by_alignment_name
:
624 ret
= bfd_section_alignment (bsec
) - bfd_section_alignment (asec
);
632 ret
= strcmp (bfd_section_name (bsec
), bfd_section_name (asec
));
634 ret
= strcmp (bfd_section_name (asec
), bfd_section_name (bsec
));
637 case by_name_alignment
:
639 ret
= strcmp (bfd_section_name (bsec
), bfd_section_name (asec
));
641 ret
= strcmp (bfd_section_name (asec
), bfd_section_name (bsec
));
647 ret
= bfd_section_alignment (bsec
) - bfd_section_alignment (asec
);
654 /* PE puts the sort key in the input statement. */
657 sort_filename (bfd
*abfd
)
659 lang_input_statement_type
*is
= bfd_usrdata (abfd
);
662 return bfd_get_filename (abfd
);
665 /* Handle wildcard sorting. This returns the place in a binary search tree
666 where this FILE:SECTION should be inserted for wild statement WILD where
667 the spec SEC was the matching one. The tree is later linearized. */
669 static lang_section_bst_type
**
670 wild_sort (lang_wild_statement_type
*wild
,
671 struct wildcard_list
*sec
,
672 lang_input_statement_type
*file
,
675 lang_section_bst_type
**tree
;
677 if (!wild
->filenames_sorted
678 && (sec
== NULL
|| sec
->spec
.sorted
== none
679 || sec
->spec
.sorted
== by_none
))
681 /* We might be called even if _this_ spec doesn't need sorting,
682 in which case we simply append at the right end of tree. */
683 return wild
->rightmost
;
689 /* Sorting by filename takes precedence over sorting by section
692 if (wild
->filenames_sorted
)
697 asection
*lsec
= (*tree
)->section
;
699 /* The PE support for the .idata section as generated by
700 dlltool assumes that files will be sorted by the name of
701 the archive and then the name of the file within the
704 fa
= file
->the_bfd
->my_archive
!= NULL
;
706 fn
= sort_filename (file
->the_bfd
->my_archive
);
708 fn
= sort_filename (file
->the_bfd
);
710 la
= lsec
->owner
->my_archive
!= NULL
;
712 ln
= sort_filename (lsec
->owner
->my_archive
);
714 ln
= sort_filename (lsec
->owner
);
716 if (wild
->filenames_reversed
)
717 i
= filename_cmp (ln
, fn
);
719 i
= filename_cmp (fn
, ln
);
722 { tree
= &((*tree
)->right
); continue; }
724 { tree
= &((*tree
)->left
); continue; }
729 fn
= sort_filename (file
->the_bfd
);
731 ln
= sort_filename (lsec
->owner
);
733 if (wild
->filenames_reversed
)
734 i
= filename_cmp (ln
, fn
);
736 i
= filename_cmp (fn
, ln
);
739 { tree
= &((*tree
)->right
); continue; }
741 { tree
= &((*tree
)->left
); continue; }
745 /* Here either the files are not sorted by name, or we are
746 looking at the sections for this file. */
748 /* Find the correct node to append this section. */
749 if (sec
&& sec
->spec
.sorted
!= none
&& sec
->spec
.sorted
!= by_none
750 && compare_section (sec
->spec
.sorted
, section
, (*tree
)->section
, sec
->spec
.reversed
) < 0)
751 tree
= &((*tree
)->left
);
753 tree
= &((*tree
)->right
);
759 /* Use wild_sort to build a BST to sort sections. */
762 output_section_callback_sort (lang_wild_statement_type
*ptr
,
763 struct wildcard_list
*sec
,
765 lang_input_statement_type
*file
,
768 lang_section_bst_type
*node
;
769 lang_section_bst_type
**tree
;
770 lang_output_section_statement_type
*os
;
772 os
= (lang_output_section_statement_type
*) output
;
774 if (unique_section_p (section
, os
))
777 /* Don't add sections to the tree when we already know that
778 lang_add_section won't do anything with it. */
779 if (wont_add_section_p (section
, os
))
782 node
= stat_alloc (sizeof (*node
));
785 node
->section
= section
;
786 node
->pattern
= ptr
->section_list
;
788 tree
= wild_sort (ptr
, sec
, file
, section
);
792 if (tree
== ptr
->rightmost
)
793 ptr
->rightmost
= &node
->right
;
797 /* Convert a sorted sections' BST back to list form. */
800 output_section_callback_tree_to_list (lang_wild_statement_type
*ptr
,
801 lang_section_bst_type
*tree
,
805 output_section_callback_tree_to_list (ptr
, tree
->left
, output
);
807 lang_add_section (&ptr
->children
, tree
->section
, tree
->pattern
,
808 ptr
->section_flag_list
,
809 (lang_output_section_statement_type
*) output
);
812 output_section_callback_tree_to_list (ptr
, tree
->right
, output
);
816 /* Sections are matched against wildcard statements via a prefix tree.
817 The prefix tree holds prefixes of all matching patterns (up to the first
818 wildcard character), and the wild statement from which those patterns
819 came. When matching a section name against the tree we're walking through
820 the tree character by character. Each statement we hit is one that
821 potentially matches. This is checked by actually going through the
822 (glob) matching routines.
824 When the section name turns out to actually match we record that section
825 in the wild statements list of matching sections. */
827 /* A prefix can be matched by multiple statement, so we need a list of them. */
828 struct wild_stmt_list
830 lang_wild_statement_type
*stmt
;
831 struct wild_stmt_list
*next
;
834 /* The prefix tree itself. */
837 /* The list of all children (linked via .next). */
838 struct prefixtree
*child
;
839 struct prefixtree
*next
;
840 /* This tree node is responsible for the prefix of parent plus 'c'. */
842 /* The statements that potentially can match this prefix. */
843 struct wild_stmt_list
*stmt
;
846 /* We always have a root node in the prefix tree. It corresponds to the
847 empty prefix. E.g. a glob like "*" would sit in this root. */
848 static struct prefixtree the_root
, *ptroot
= &the_root
;
850 /* Given a prefix tree in *TREE, corresponding to prefix P, find or
851 INSERT the tree node corresponding to prefix P+C. */
853 static struct prefixtree
*
854 get_prefix_tree (struct prefixtree
**tree
, char c
, bool insert
)
856 struct prefixtree
*t
;
857 for (t
= *tree
; t
; t
= t
->next
)
862 t
= (struct prefixtree
*) obstack_alloc (&pt_obstack
, sizeof *t
);
871 /* Add STMT to the set of statements that can be matched by the prefix
872 corresponding to prefix tree T. */
875 pt_add_stmt (struct prefixtree
*t
, lang_wild_statement_type
*stmt
)
877 struct wild_stmt_list
*sl
, **psl
;
878 sl
= (struct wild_stmt_list
*) obstack_alloc (&pt_obstack
, sizeof *sl
);
887 /* Insert STMT into the global prefix tree. */
890 insert_prefix_tree (lang_wild_statement_type
*stmt
)
892 struct wildcard_list
*sec
;
893 struct prefixtree
*t
;
895 if (!stmt
->section_list
)
897 /* If we have no section_list (no wildcards in the wild STMT),
898 then every section name will match, so add this to the root. */
899 pt_add_stmt (ptroot
, stmt
);
903 for (sec
= stmt
->section_list
; sec
; sec
= sec
->next
)
905 const char *name
= sec
->spec
.name
? sec
->spec
.name
: "*";
908 for (; (c
= *name
); name
++)
910 if (c
== '*' || c
== '[' || c
== '?')
912 t
= get_prefix_tree (&t
->child
, c
, true);
914 /* If we hit a glob character, the matching prefix is what we saw
915 until now. If we hit the end of pattern (hence it's no glob) then
916 we can do better: we only need to record a match when a section name
917 completely matches, not merely a prefix, so record the trailing 0
920 t
= get_prefix_tree (&t
->child
, 0, true);
921 pt_add_stmt (t
, stmt
);
925 /* Dump T indented by INDENT spaces. */
928 debug_prefix_tree_rec (struct prefixtree
*t
, int indent
)
930 for (; t
; t
= t
->next
)
932 struct wild_stmt_list
*sl
;
933 printf ("%*s %c", indent
, "", t
->c
);
934 for (sl
= t
->stmt
; sl
; sl
= sl
->next
)
936 struct wildcard_list
*curr
;
937 printf (" %p ", sl
->stmt
);
938 for (curr
= sl
->stmt
->section_list
; curr
; curr
= curr
->next
)
939 printf ("%s ", curr
->spec
.name
? curr
->spec
.name
: "*");
942 debug_prefix_tree_rec (t
->child
, indent
+ 2);
946 /* Dump the global prefix tree. */
949 debug_prefix_tree (void)
951 debug_prefix_tree_rec (ptroot
, 2);
954 /* Like strcspn() but start to look from the end to beginning of
955 S. Returns the length of the suffix of S consisting entirely
956 of characters not in REJECT. */
959 rstrcspn (const char *s
, const char *reject
)
961 size_t len
= strlen (s
), sufflen
= 0;
965 if (strchr (reject
, c
) != 0)
972 /* Analyze the wildcards in wild statement PTR to setup various
973 things for quick matching. */
976 analyze_walk_wild_section_handler (lang_wild_statement_type
*ptr
)
978 struct wildcard_list
*sec
;
981 ptr
->rightmost
= &ptr
->tree
;
983 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
987 sec
->spec
.namelen
= strlen (sec
->spec
.name
);
988 sec
->spec
.prefixlen
= strcspn (sec
->spec
.name
, "?*[");
989 sec
->spec
.suffixlen
= rstrcspn (sec
->spec
.name
+ sec
->spec
.prefixlen
,
993 sec
->spec
.namelen
= sec
->spec
.prefixlen
= sec
->spec
.suffixlen
= 0;
996 insert_prefix_tree (ptr
);
999 /* Match all sections from FILE against the global prefix tree,
1000 and record them into each wild statement that has a match. */
1003 resolve_wild_sections (lang_input_statement_type
*file
)
1007 if (file
->flags
.just_syms
)
1010 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
1012 const char *sname
= bfd_section_name (s
);
1014 struct prefixtree
*t
= ptroot
;
1015 //printf (" YYY consider %s of %s\n", sname, file->the_bfd->filename);
1020 struct wild_stmt_list
*sl
;
1021 for (sl
= t
->stmt
; sl
; sl
= sl
->next
)
1023 walk_wild_section_match (sl
->stmt
, file
, s
);
1024 //printf (" ZZZ maybe place into %p\n", sl->stmt);
1030 t
= get_prefix_tree (&t
->child
, c
, false);
1036 /* Match all sections from all input files against the global prefix tree. */
1039 resolve_wilds (void)
1041 LANG_FOR_EACH_INPUT_STATEMENT (f
)
1043 //printf("XXX %s\n", f->filename);
1044 if (f
->the_bfd
== NULL
1045 || !bfd_check_format (f
->the_bfd
, bfd_archive
))
1046 resolve_wild_sections (f
);
1051 /* This is an archive file. We must map each member of the
1052 archive separately. */
1053 member
= bfd_openr_next_archived_file (f
->the_bfd
, NULL
);
1054 while (member
!= NULL
)
1056 /* When lookup_name is called, it will call the add_symbols
1057 entry point for the archive. For each element of the
1058 archive which is included, BFD will call ldlang_add_file,
1059 which will set the usrdata field of the member to the
1060 lang_input_statement. */
1061 if (bfd_usrdata (member
) != NULL
)
1062 resolve_wild_sections (bfd_usrdata (member
));
1064 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
1070 /* For each input section that matches wild statement S calls
1071 CALLBACK with DATA. */
1074 walk_wild (lang_wild_statement_type
*s
, callback_t callback
, void *data
)
1076 lang_statement_union_type
*l
;
1078 for (l
= s
->matching_sections
.head
; l
; l
= l
->header
.next
)
1080 (*callback
) (s
, l
->input_matcher
.pattern
, l
->input_matcher
.section
,
1081 l
->input_matcher
.input_stmt
, data
);
1085 /* lang_for_each_statement walks the parse tree and calls the provided
1086 function for each node, except those inside output section statements
1087 with constraint set to -1. */
1090 lang_for_each_statement_worker (void (*func
) (lang_statement_union_type
*),
1091 lang_statement_union_type
*s
)
1093 for (; s
!= NULL
; s
= s
->header
.next
)
1097 switch (s
->header
.type
)
1099 case lang_constructors_statement_enum
:
1100 lang_for_each_statement_worker (func
, constructor_list
.head
);
1102 case lang_output_section_statement_enum
:
1103 if (s
->output_section_statement
.constraint
!= -1)
1104 lang_for_each_statement_worker
1105 (func
, s
->output_section_statement
.children
.head
);
1107 case lang_wild_statement_enum
:
1108 lang_for_each_statement_worker (func
,
1109 s
->wild_statement
.children
.head
);
1111 case lang_group_statement_enum
:
1112 lang_for_each_statement_worker (func
,
1113 s
->group_statement
.children
.head
);
1115 case lang_data_statement_enum
:
1116 case lang_reloc_statement_enum
:
1117 case lang_object_symbols_statement_enum
:
1118 case lang_output_statement_enum
:
1119 case lang_target_statement_enum
:
1120 case lang_input_section_enum
:
1121 case lang_input_statement_enum
:
1122 case lang_assignment_statement_enum
:
1123 case lang_padding_statement_enum
:
1124 case lang_address_statement_enum
:
1125 case lang_fill_statement_enum
:
1126 case lang_insert_statement_enum
:
1136 lang_for_each_statement (void (*func
) (lang_statement_union_type
*))
1138 lang_for_each_statement_worker (func
, statement_list
.head
);
1141 /*----------------------------------------------------------------------*/
1144 lang_list_init (lang_statement_list_type
*list
)
1147 list
->tail
= &list
->head
;
1151 lang_statement_append (lang_statement_list_type
*list
,
1155 *(list
->tail
) = element
;
1160 push_stat_ptr (lang_statement_list_type
*new_ptr
)
1162 if (stat_save_ptr
>= stat_save
+ sizeof (stat_save
) / sizeof (stat_save
[0]))
1164 *stat_save_ptr
++ = stat_ptr
;
1171 if (stat_save_ptr
<= stat_save
)
1173 stat_ptr
= *--stat_save_ptr
;
1176 /* Build a new statement node for the parse tree. */
1178 static lang_statement_union_type
*
1179 new_statement (enum statement_enum type
,
1181 lang_statement_list_type
*list
)
1183 lang_statement_union_type
*new_stmt
;
1185 new_stmt
= stat_alloc (size
);
1186 new_stmt
->header
.type
= type
;
1187 new_stmt
->header
.next
= NULL
;
1188 lang_statement_append (list
, new_stmt
, &new_stmt
->header
.next
);
1192 /* Build a new input file node for the language. There are several
1193 ways in which we treat an input file, eg, we only look at symbols,
1194 or prefix it with a -l etc.
1196 We can be supplied with requests for input files more than once;
1197 they may, for example be split over several lines like foo.o(.text)
1198 foo.o(.data) etc, so when asked for a file we check that we haven't
1199 got it already so we don't duplicate the bfd. */
1201 static lang_input_statement_type
*
1202 new_afile (const char *name
,
1203 lang_input_file_enum_type file_type
,
1205 const char *from_filename
)
1207 lang_input_statement_type
*p
;
1209 lang_has_input_file
= true;
1211 /* PR 30632: It is OK for name to be NULL. For example
1212 see the initialization of first_file in lang_init(). */
1215 name
= ldfile_possibly_remap_input (name
);
1216 /* But if a name is remapped to NULL, it should be ignored. */
1221 p
= new_stat (lang_input_statement
, stat_ptr
);
1222 memset (&p
->the_bfd
, 0,
1223 sizeof (*p
) - offsetof (lang_input_statement_type
, the_bfd
));
1224 p
->extra_search_path
= NULL
;
1226 p
->flags
.dynamic
= input_flags
.dynamic
;
1227 p
->flags
.add_DT_NEEDED_for_dynamic
= input_flags
.add_DT_NEEDED_for_dynamic
;
1228 p
->flags
.add_DT_NEEDED_for_regular
= input_flags
.add_DT_NEEDED_for_regular
;
1229 p
->flags
.whole_archive
= input_flags
.whole_archive
;
1230 p
->flags
.sysrooted
= input_flags
.sysrooted
;
1235 case lang_input_file_is_symbols_only_enum
:
1237 p
->local_sym_name
= name
;
1238 p
->flags
.real
= true;
1239 p
->flags
.just_syms
= true;
1241 case lang_input_file_is_fake_enum
:
1243 p
->local_sym_name
= name
;
1245 case lang_input_file_is_l_enum
:
1246 if (name
[0] == ':' && name
[1] != '\0')
1248 p
->filename
= name
+ 1;
1249 p
->flags
.full_name_provided
= true;
1253 p
->local_sym_name
= concat ("-l", name
, (const char *) NULL
);
1254 p
->flags
.maybe_archive
= true;
1255 p
->flags
.real
= true;
1256 p
->flags
.search_dirs
= true;
1258 case lang_input_file_is_marker_enum
:
1260 p
->local_sym_name
= name
;
1261 p
->flags
.search_dirs
= true;
1263 case lang_input_file_is_search_file_enum
:
1265 p
->local_sym_name
= name
;
1266 /* If name is a relative path, search the directory of the current linker
1268 if (from_filename
&& !IS_ABSOLUTE_PATH (name
))
1269 p
->extra_search_path
= stat_ldirname (from_filename
);
1270 p
->flags
.real
= true;
1271 p
->flags
.search_dirs
= true;
1273 case lang_input_file_is_file_enum
:
1275 p
->local_sym_name
= name
;
1276 p
->flags
.real
= true;
1282 lang_statement_append (&input_file_chain
, p
, &p
->next_real_file
);
1286 lang_input_statement_type
*
1287 lang_add_input_file (const char *name
,
1288 lang_input_file_enum_type file_type
,
1292 && (*name
== '=' || startswith (name
, "$SYSROOT")))
1294 lang_input_statement_type
*ret
;
1295 char *sysrooted_name
1296 = concat (ld_sysroot
,
1297 name
+ (*name
== '=' ? 1 : strlen ("$SYSROOT")),
1298 (const char *) NULL
);
1300 /* We've now forcibly prepended the sysroot, making the input
1301 file independent of the context. Therefore, temporarily
1302 force a non-sysrooted context for this statement, so it won't
1303 get the sysroot prepended again when opened. (N.B. if it's a
1304 script, any child nodes with input files starting with "/"
1305 will be handled as "sysrooted" as they'll be found to be
1306 within the sysroot subdirectory.) */
1307 unsigned int outer_sysrooted
= input_flags
.sysrooted
;
1308 input_flags
.sysrooted
= 0;
1309 ret
= new_afile (sysrooted_name
, file_type
, target
, NULL
);
1310 input_flags
.sysrooted
= outer_sysrooted
;
1314 return new_afile (name
, file_type
, target
, current_input_file
);
1317 struct out_section_hash_entry
1319 struct bfd_hash_entry root
;
1320 lang_statement_union_type s
;
1321 struct out_section_hash_entry
*tail
;
1324 /* The hash table. */
1326 static struct bfd_hash_table output_section_statement_table
;
1328 /* Support routines for the hash table used by lang_output_section_find,
1329 initialize the table, fill in an entry and remove the table. */
1331 static struct out_section_hash_entry
*
1332 output_section_statement_newfunc_1 (struct bfd_hash_entry
*entry
,
1333 struct bfd_hash_table
*table
,
1336 lang_output_section_statement_type
**nextp
;
1337 struct out_section_hash_entry
*ret
;
1341 entry
= (struct bfd_hash_entry
*) bfd_hash_allocate (table
,
1347 entry
= bfd_hash_newfunc (entry
, table
, string
);
1351 ret
= (struct out_section_hash_entry
*) entry
;
1352 memset (&ret
->s
, 0, sizeof (ret
->s
));
1353 ret
->s
.header
.type
= lang_output_section_statement_enum
;
1354 ret
->s
.output_section_statement
.subsection_alignment
= NULL
;
1355 ret
->s
.output_section_statement
.section_alignment
= NULL
;
1356 ret
->s
.output_section_statement
.block_value
= 1;
1357 lang_list_init (&ret
->s
.output_section_statement
.children
);
1358 lang_list_init (&ret
->s
.output_section_statement
.sort_children
);
1359 lang_statement_append (stat_ptr
, &ret
->s
, &ret
->s
.header
.next
);
1361 /* For every output section statement added to the list, except the
1362 first one, lang_os_list.tail points to the "next"
1363 field of the last element of the list. */
1364 if (lang_os_list
.head
!= NULL
)
1365 ret
->s
.output_section_statement
.prev
1366 = ((lang_output_section_statement_type
*)
1367 ((char *) lang_os_list
.tail
1368 - offsetof (lang_output_section_statement_type
, next
)));
1370 /* GCC's strict aliasing rules prevent us from just casting the
1371 address, so we store the pointer in a variable and cast that
1373 nextp
= &ret
->s
.output_section_statement
.next
;
1374 lang_statement_append (&lang_os_list
, &ret
->s
, nextp
);
1378 static struct bfd_hash_entry
*
1379 output_section_statement_newfunc (struct bfd_hash_entry
*entry
,
1380 struct bfd_hash_table
*table
,
1383 struct out_section_hash_entry
*ret
;
1385 ret
= output_section_statement_newfunc_1 (entry
, table
, string
);
1393 output_section_statement_table_init (void)
1395 if (!bfd_hash_table_init_n (&output_section_statement_table
,
1396 output_section_statement_newfunc
,
1397 sizeof (struct out_section_hash_entry
),
1399 fatal (_("%P: can not create hash table: %E\n"));
1403 output_section_statement_table_free (void)
1405 bfd_hash_table_free (&output_section_statement_table
);
1408 /* Build enough state so that the parser can build its tree. */
1411 lang_init (bool object_only
)
1415 obstack_begin (&stat_obstack
, 1000);
1416 obstack_init (&pt_obstack
);
1419 stat_ptr
= &statement_list
;
1421 output_section_statement_table_init ();
1423 cmdline_lists_init ();
1425 lang_list_init (stat_ptr
);
1427 lang_list_init (&input_file_chain
);
1428 lang_list_init (&lang_os_list
);
1429 lang_list_init (&file_chain
);
1430 first_file
= lang_add_input_file (NULL
, lang_input_file_is_marker_enum
,
1432 abs_output_section
=
1433 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
, 0, 1);
1435 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
1437 asneeded_list_head
= NULL
;
1438 asneeded_list_tail
= &asneeded_list_head
;
1444 output_section_statement_table_free ();
1448 /*----------------------------------------------------------------------
1449 A region is an area of memory declared with the
1450 MEMORY { name:org=exp, len=exp ... }
1453 We maintain a list of all the regions here.
1455 If no regions are specified in the script, then the default is used
1456 which is created when looked up to be the entire data space.
1458 If create is true we are creating a region inside a MEMORY block.
1459 In this case it is probably an error to create a region that has
1460 already been created. If we are not inside a MEMORY block it is
1461 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
1462 and so we issue a warning.
1464 Each region has at least one name. The first name is either
1465 DEFAULT_MEMORY_REGION or the name given in the MEMORY block. You can add
1466 alias names to an existing region within a script with
1467 REGION_ALIAS (alias, region_name). Each name corresponds to at most one
1470 static lang_memory_region_type
*lang_memory_region_list
;
1471 static lang_memory_region_type
**lang_memory_region_list_tail
1472 = &lang_memory_region_list
;
1474 lang_memory_region_type
*
1475 lang_memory_region_lookup (const char *const name
, bool create
)
1477 lang_memory_region_name
*n
;
1478 lang_memory_region_type
*r
;
1479 lang_memory_region_type
*new_region
;
1481 /* NAME is NULL for LMA memspecs if no region was specified. */
1485 for (r
= lang_memory_region_list
; r
!= NULL
; r
= r
->next
)
1486 for (n
= &r
->name_list
; n
!= NULL
; n
= n
->next
)
1487 if (strcmp (n
->name
, name
) == 0)
1490 einfo (_("%P:%pS: warning: redeclaration of memory region `%s'\n"),
1495 if (!create
&& strcmp (name
, DEFAULT_MEMORY_REGION
))
1496 einfo (_("%P:%pS: warning: memory region `%s' not declared\n"),
1499 new_region
= stat_alloc (sizeof (lang_memory_region_type
));
1501 new_region
->name_list
.name
= stat_strdup (name
);
1502 new_region
->name_list
.next
= NULL
;
1503 new_region
->next
= NULL
;
1504 new_region
->origin_exp
= NULL
;
1505 new_region
->origin
= 0;
1506 new_region
->length_exp
= NULL
;
1507 new_region
->length
= ~(bfd_size_type
) 0;
1508 new_region
->current
= 0;
1509 new_region
->last_os
= NULL
;
1510 new_region
->flags
= 0;
1511 new_region
->not_flags
= 0;
1512 new_region
->had_full_message
= false;
1514 *lang_memory_region_list_tail
= new_region
;
1515 lang_memory_region_list_tail
= &new_region
->next
;
1521 lang_memory_region_alias (const char *alias
, const char *region_name
)
1523 lang_memory_region_name
*n
;
1524 lang_memory_region_type
*r
;
1525 lang_memory_region_type
*region
;
1527 /* The default region must be unique. This ensures that it is not necessary
1528 to iterate through the name list if someone wants the check if a region is
1529 the default memory region. */
1530 if (strcmp (region_name
, DEFAULT_MEMORY_REGION
) == 0
1531 || strcmp (alias
, DEFAULT_MEMORY_REGION
) == 0)
1532 fatal (_("%P:%pS: error: alias for default memory region\n"), NULL
);
1534 /* Look for the target region and check if the alias is not already
1537 for (r
= lang_memory_region_list
; r
!= NULL
; r
= r
->next
)
1538 for (n
= &r
->name_list
; n
!= NULL
; n
= n
->next
)
1540 if (region
== NULL
&& strcmp (n
->name
, region_name
) == 0)
1542 if (strcmp (n
->name
, alias
) == 0)
1543 fatal (_("%P:%pS: error: redefinition of memory region "
1548 /* Check if the target region exists. */
1550 fatal (_("%P:%pS: error: memory region `%s' "
1551 "for alias `%s' does not exist\n"),
1552 NULL
, region_name
, alias
);
1554 /* Add alias to region name list. */
1555 n
= stat_alloc (sizeof (lang_memory_region_name
));
1556 n
->name
= stat_strdup (alias
);
1557 n
->next
= region
->name_list
.next
;
1558 region
->name_list
.next
= n
;
1561 static lang_memory_region_type
*
1562 lang_memory_default (asection
*section
)
1564 lang_memory_region_type
*p
;
1566 flagword sec_flags
= section
->flags
;
1568 /* Override SEC_DATA to mean a writable section. */
1569 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
1570 sec_flags
|= SEC_DATA
;
1572 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
1574 if ((p
->flags
& sec_flags
) != 0
1575 && (p
->not_flags
& sec_flags
) == 0)
1580 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, false);
1583 /* Get the output section statement directly from the userdata. */
1585 lang_output_section_statement_type
*
1586 lang_output_section_get (const asection
*output_section
)
1588 return bfd_section_userdata (output_section
);
1591 /* Find or create an output_section_statement with the given NAME.
1592 If CONSTRAINT is non-zero match one with that constraint, otherwise
1593 match any non-negative constraint. If CREATE is 0 return NULL when
1594 no match exists. If CREATE is 1, create an output_section_statement
1595 when no match exists or if CONSTRAINT is SPECIAL. If CREATE is 2,
1596 always make a new output_section_statement. */
1598 lang_output_section_statement_type
*
1599 lang_output_section_statement_lookup (const char *name
,
1603 struct out_section_hash_entry
*entry
;
1605 entry
= ((struct out_section_hash_entry
*)
1606 bfd_hash_lookup (&output_section_statement_table
, name
,
1607 create
!= 0, false));
1611 fatal (_("%P: failed creating section `%s': %E\n"), name
);
1615 if (entry
->s
.output_section_statement
.name
!= NULL
)
1617 /* We have a section of this name, but it might not have the correct
1619 struct out_section_hash_entry
*first_ent
= entry
;
1620 struct out_section_hash_entry
*last_ent
;
1622 name
= entry
->s
.output_section_statement
.name
;
1624 && !(create
&& constraint
== SPECIAL
))
1628 if (constraint
== entry
->s
.output_section_statement
.constraint
1630 && entry
->s
.output_section_statement
.constraint
>= 0))
1631 return &entry
->s
.output_section_statement
;
1633 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1635 while (entry
!= NULL
1636 && name
== entry
->s
.output_section_statement
.name
);
1639 last_ent
= first_ent
->tail
;
1644 /* Only the first entry needs the tail pointer. */
1645 entry
= bfd_hash_allocate (&output_section_statement_table
,
1646 offsetof (struct out_section_hash_entry
, tail
));
1649 = output_section_statement_newfunc_1 (&entry
->root
,
1650 &output_section_statement_table
,
1654 fatal (_("%P: failed creating section `%s': %E\n"), name
);
1657 entry
->root
= last_ent
->root
;
1658 last_ent
->root
.next
= &entry
->root
;
1659 first_ent
->tail
= entry
;
1662 entry
->s
.output_section_statement
.name
= name
;
1663 entry
->s
.output_section_statement
.constraint
= constraint
;
1664 entry
->s
.output_section_statement
.dup_output
= (create
== 2
1665 || constraint
== SPECIAL
);
1666 return &entry
->s
.output_section_statement
;
1669 /* Find the next output_section_statement with the same name as OS.
1670 If CONSTRAINT is non-zero, find one with that constraint otherwise
1671 match any non-negative constraint. */
1673 lang_output_section_statement_type
*
1674 next_matching_output_section_statement (lang_output_section_statement_type
*os
,
1677 /* All output_section_statements are actually part of a
1678 struct out_section_hash_entry. */
1679 struct out_section_hash_entry
*entry
= (struct out_section_hash_entry
*)
1681 - offsetof (struct out_section_hash_entry
, s
.output_section_statement
));
1682 const char *name
= os
->name
;
1684 ASSERT (name
== entry
->root
.string
);
1687 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1689 || name
!= entry
->s
.output_section_statement
.name
)
1692 while (constraint
!= entry
->s
.output_section_statement
.constraint
1694 || entry
->s
.output_section_statement
.constraint
< 0));
1696 return &entry
->s
.output_section_statement
;
1699 /* A variant of lang_output_section_find used by place_orphan.
1700 Returns the output statement that should precede a new output
1701 statement for SEC. If an exact match is found on certain flags,
1704 lang_output_section_statement_type
*
1705 lang_output_section_find_by_flags (const asection
*sec
,
1707 lang_output_section_statement_type
**exact
,
1708 lang_match_sec_type_func match_type
)
1710 lang_output_section_statement_type
*first
, *look
, *found
;
1711 flagword look_flags
, differ
;
1713 /* We know the first statement on this list is *ABS*. May as well
1715 first
= (void *) lang_os_list
.head
;
1716 first
= first
->next
;
1718 /* First try for an exact match. */
1720 for (look
= first
; look
; look
= look
->next
)
1722 look_flags
= look
->flags
;
1723 if (look
->bfd_section
!= NULL
)
1725 look_flags
= look
->bfd_section
->flags
;
1726 if (match_type
&& !match_type (link_info
.output_bfd
,
1731 differ
= look_flags
^ sec_flags
;
1732 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
1733 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1743 if ((sec_flags
& SEC_CODE
) != 0
1744 && (sec_flags
& SEC_ALLOC
) != 0)
1746 /* Try for a rw code section. */
1747 for (look
= first
; look
; look
= look
->next
)
1749 look_flags
= look
->flags
;
1750 if (look
->bfd_section
!= NULL
)
1752 look_flags
= look
->bfd_section
->flags
;
1753 if (match_type
&& !match_type (link_info
.output_bfd
,
1758 differ
= look_flags
^ sec_flags
;
1759 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1760 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1764 else if ((sec_flags
& SEC_READONLY
) != 0
1765 && (sec_flags
& SEC_ALLOC
) != 0)
1767 /* .rodata can go after .text, .sdata2 after .rodata. */
1768 for (look
= first
; look
; look
= look
->next
)
1770 look_flags
= look
->flags
;
1771 if (look
->bfd_section
!= NULL
)
1773 look_flags
= look
->bfd_section
->flags
;
1774 if (match_type
&& !match_type (link_info
.output_bfd
,
1779 differ
= look_flags
^ sec_flags
;
1780 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1781 | SEC_READONLY
| SEC_SMALL_DATA
))
1782 || (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1784 && !(look_flags
& SEC_SMALL_DATA
)))
1788 else if ((sec_flags
& SEC_THREAD_LOCAL
) != 0
1789 && (sec_flags
& SEC_ALLOC
) != 0)
1791 /* .tdata can go after .data, .tbss after .tdata. Treat .tbss
1792 as if it were a loaded section, and don't use match_type. */
1793 bool seen_thread_local
= false;
1796 for (look
= first
; look
; look
= look
->next
)
1798 look_flags
= look
->flags
;
1799 if (look
->bfd_section
!= NULL
)
1800 look_flags
= look
->bfd_section
->flags
;
1802 differ
= look_flags
^ (sec_flags
| SEC_LOAD
| SEC_HAS_CONTENTS
);
1803 if (!(differ
& (SEC_THREAD_LOCAL
| SEC_ALLOC
)))
1805 /* .tdata and .tbss must be adjacent and in that order. */
1806 if (!(look_flags
& SEC_LOAD
)
1807 && (sec_flags
& SEC_LOAD
))
1808 /* ..so if we're at a .tbss section and we're placing
1809 a .tdata section stop looking and return the
1810 previous section. */
1813 seen_thread_local
= true;
1815 else if (seen_thread_local
)
1817 else if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
)))
1821 else if ((sec_flags
& SEC_SMALL_DATA
) != 0
1822 && (sec_flags
& SEC_ALLOC
) != 0)
1824 /* .sdata goes after .data, .sbss after .sdata. */
1825 for (look
= first
; look
; look
= look
->next
)
1827 look_flags
= look
->flags
;
1828 if (look
->bfd_section
!= NULL
)
1830 look_flags
= look
->bfd_section
->flags
;
1831 if (match_type
&& !match_type (link_info
.output_bfd
,
1836 differ
= look_flags
^ sec_flags
;
1837 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1838 | SEC_THREAD_LOCAL
))
1839 || ((look_flags
& SEC_SMALL_DATA
)
1840 && !(sec_flags
& SEC_HAS_CONTENTS
)))
1844 else if ((sec_flags
& SEC_HAS_CONTENTS
) != 0
1845 && (sec_flags
& SEC_ALLOC
) != 0)
1847 /* .data goes after .rodata. */
1848 for (look
= first
; look
; look
= look
->next
)
1850 look_flags
= look
->flags
;
1851 if (look
->bfd_section
!= NULL
)
1853 look_flags
= look
->bfd_section
->flags
;
1854 if (match_type
&& !match_type (link_info
.output_bfd
,
1859 differ
= look_flags
^ sec_flags
;
1860 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1861 | SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1865 else if ((sec_flags
& SEC_ALLOC
) != 0)
1867 /* .bss goes after any other alloc section. */
1868 for (look
= first
; look
; look
= look
->next
)
1870 look_flags
= look
->flags
;
1871 if (look
->bfd_section
!= NULL
)
1873 look_flags
= look
->bfd_section
->flags
;
1874 if (match_type
&& !match_type (link_info
.output_bfd
,
1879 differ
= look_flags
^ sec_flags
;
1880 if (!(differ
& SEC_ALLOC
))
1886 /* non-alloc go last. */
1887 for (look
= first
; look
; look
= look
->next
)
1889 look_flags
= look
->flags
;
1890 if (look
->bfd_section
!= NULL
)
1891 look_flags
= look
->bfd_section
->flags
;
1892 differ
= look_flags
^ sec_flags
;
1893 if (!(differ
& SEC_DEBUGGING
))
1899 if (found
|| !match_type
)
1902 return lang_output_section_find_by_flags (sec
, sec_flags
, NULL
, NULL
);
1905 /* Find the last output section before given output statement.
1906 Used by place_orphan. */
1909 output_prev_sec_find (lang_output_section_statement_type
*os
)
1911 lang_output_section_statement_type
*lookup
;
1913 for (lookup
= os
->prev
; lookup
!= NULL
; lookup
= lookup
->prev
)
1915 if (lookup
->constraint
< 0)
1918 if (lookup
->bfd_section
!= NULL
)
1919 return lookup
->bfd_section
;
1925 /* Look for a suitable place for a new output section statement. The
1926 idea is to skip over anything that might be inside a SECTIONS {}
1927 statement in a script, before we find another output section
1928 statement. Assignments to "dot" before an output section statement
1929 are assumed to belong to it, except in two cases; The first
1930 assignment to dot, and assignments before non-alloc sections.
1931 Otherwise we might put an orphan before . = . + SIZEOF_HEADERS or
1932 similar assignments that set the initial address, or we might
1933 insert non-alloc note sections among assignments setting end of
1936 static lang_statement_union_type
**
1937 insert_os_after (lang_statement_union_type
*after
)
1939 lang_statement_union_type
**where
;
1940 lang_statement_union_type
**assign
= NULL
;
1943 ignore_first
= after
== lang_os_list
.head
;
1945 for (where
= &after
->header
.next
;
1947 where
= &(*where
)->header
.next
)
1949 switch ((*where
)->header
.type
)
1951 case lang_assignment_statement_enum
:
1954 lang_assignment_statement_type
*ass
;
1956 ass
= &(*where
)->assignment_statement
;
1957 if (ass
->exp
->type
.node_class
!= etree_assert
1958 && ass
->exp
->assign
.dst
[0] == '.'
1959 && ass
->exp
->assign
.dst
[1] == 0)
1963 ignore_first
= false;
1967 case lang_wild_statement_enum
:
1968 case lang_input_section_enum
:
1969 case lang_object_symbols_statement_enum
:
1970 case lang_fill_statement_enum
:
1971 case lang_data_statement_enum
:
1972 case lang_reloc_statement_enum
:
1973 case lang_padding_statement_enum
:
1974 case lang_constructors_statement_enum
:
1976 ignore_first
= false;
1978 case lang_output_section_statement_enum
:
1981 asection
*s
= (*where
)->output_section_statement
.bfd_section
;
1984 || s
->map_head
.s
== NULL
1985 || (s
->flags
& SEC_ALLOC
) != 0)
1989 case lang_input_statement_enum
:
1990 case lang_address_statement_enum
:
1991 case lang_target_statement_enum
:
1992 case lang_output_statement_enum
:
1993 case lang_group_statement_enum
:
1994 case lang_insert_statement_enum
:
1996 case lang_input_matcher_enum
:
2005 lang_output_section_statement_type
*
2006 lang_insert_orphan (asection
*s
,
2007 const char *secname
,
2009 lang_output_section_statement_type
*after
,
2010 struct orphan_save
*place
,
2011 etree_type
*address
,
2012 lang_statement_list_type
*add_child
)
2014 lang_statement_list_type add
;
2015 lang_output_section_statement_type
*os
;
2016 lang_output_section_statement_type
**os_tail
;
2018 /* If we have found an appropriate place for the output section
2019 statements for this orphan, add them to our own private list,
2020 inserting them later into the global statement list. */
2023 lang_list_init (&add
);
2024 push_stat_ptr (&add
);
2027 if (bfd_link_relocatable (&link_info
)
2028 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0)
2029 address
= exp_intop (0);
2031 os_tail
= (lang_output_section_statement_type
**) lang_os_list
.tail
;
2032 os
= lang_enter_output_section_statement (
2033 secname
, address
, normal_section
, 0, NULL
, NULL
, NULL
, constraint
, 0);
2035 if (add_child
== NULL
)
2036 add_child
= &os
->children
;
2037 lang_add_section (add_child
, s
, NULL
, NULL
, os
);
2039 if (after
&& (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) != 0)
2041 const char *region
= (after
->region
2042 ? after
->region
->name_list
.name
2043 : DEFAULT_MEMORY_REGION
);
2044 const char *lma_region
= (after
->lma_region
2045 ? after
->lma_region
->name_list
.name
2047 lang_leave_output_section_statement (NULL
, region
, after
->phdrs
,
2051 lang_leave_output_section_statement (NULL
, DEFAULT_MEMORY_REGION
, NULL
,
2054 /* Restore the global list pointer. */
2058 if (after
!= NULL
&& os
->bfd_section
!= NULL
)
2060 asection
*snew
, *as
;
2061 bool place_after
= place
->stmt
== NULL
;
2062 bool insert_after
= true;
2064 snew
= os
->bfd_section
;
2066 /* Shuffle the bfd section list to make the output file look
2067 neater. This is really only cosmetic. */
2068 if (place
->section
== NULL
2069 && after
!= (void *) lang_os_list
.head
)
2071 asection
*bfd_section
= after
->bfd_section
;
2073 /* If the output statement hasn't been used to place any input
2074 sections (and thus doesn't have an output bfd_section),
2075 look for the closest prior output statement having an
2077 if (bfd_section
== NULL
)
2078 bfd_section
= output_prev_sec_find (after
);
2080 if (bfd_section
!= NULL
2081 && bfd_section
->owner
!= NULL
2082 && bfd_section
!= snew
)
2083 place
->section
= &bfd_section
->next
;
2086 if (place
->section
== NULL
)
2087 place
->section
= &link_info
.output_bfd
->sections
;
2089 as
= *place
->section
;
2093 /* Put the section at the end of the list. */
2095 /* Unlink the section. */
2096 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2098 /* Now tack it back on in the right place. */
2099 bfd_section_list_append (link_info
.output_bfd
, snew
);
2101 else if ((bfd_get_flavour (link_info
.output_bfd
)
2102 == bfd_target_elf_flavour
)
2103 && (bfd_get_flavour (s
->owner
)
2104 == bfd_target_elf_flavour
)
2105 && ((elf_section_type (s
) == SHT_NOTE
2106 && (s
->flags
& SEC_LOAD
) != 0)
2107 || (elf_section_type (as
) == SHT_NOTE
2108 && (as
->flags
& SEC_LOAD
) != 0)))
2110 /* Make sure that output note sections are grouped and sorted
2111 by alignments when inserting a note section or insert a
2112 section after a note section, */
2114 /* A specific section after which the output note section
2115 should be placed. */
2116 asection
*after_sec
;
2117 /* True if we need to insert the orphan section after a
2118 specific section to maintain output note section order. */
2119 bool after_sec_note
= false;
2121 static asection
*first_orphan_note
= NULL
;
2123 /* Group and sort output note section by alignments in
2126 if (elf_section_type (s
) == SHT_NOTE
2127 && (s
->flags
& SEC_LOAD
) != 0)
2129 /* Search from the beginning for the last output note
2130 section with equal or larger alignments. NB: Don't
2131 place orphan note section after non-note sections. */
2133 first_orphan_note
= NULL
;
2135 /* NB: When --rosegment is used, the .note.gnu.build-id
2136 section is placed before text sections. Ignore the
2137 .note.gnu.build-id section if -z separate-code and
2138 --rosegment are used together to avoid putting any
2139 note sections between the .note.gnu.build-id section
2140 and text sections in the same PT_LOAD segment. */
2141 bool ignore_build_id
= (link_info
.separate_code
2142 && link_info
.one_rosegment
);
2144 for (sec
= link_info
.output_bfd
->sections
;
2146 && !bfd_is_abs_section (sec
));
2149 && elf_section_type (sec
) == SHT_NOTE
2150 && (sec
->flags
& SEC_LOAD
) != 0
2151 && (!ignore_build_id
2152 || strcmp (sec
->name
, ".note.gnu.build-id") != 0))
2154 if (!first_orphan_note
)
2155 first_orphan_note
= sec
;
2156 if (sec
->alignment_power
>= s
->alignment_power
)
2159 else if (first_orphan_note
)
2161 /* Stop if there is non-note section after the first
2162 orphan note section. */
2166 /* If this will be the first orphan note section, it can
2167 be placed at the default location. */
2168 after_sec_note
= first_orphan_note
!= NULL
;
2169 if (after_sec
== NULL
&& after_sec_note
)
2171 /* If all output note sections have smaller
2172 alignments, place the section before all
2173 output orphan note sections. */
2174 after_sec
= first_orphan_note
;
2175 insert_after
= false;
2178 else if (first_orphan_note
)
2180 /* Don't place non-note sections in the middle of orphan
2182 after_sec_note
= true;
2184 for (sec
= as
->next
;
2186 && !bfd_is_abs_section (sec
));
2188 if (elf_section_type (sec
) == SHT_NOTE
2189 && (sec
->flags
& SEC_LOAD
) != 0)
2197 /* Search forward to insert OS after AFTER_SEC output
2199 lang_output_section_statement_type
*stmt
, *next
;
2201 for (stmt
= after
; stmt
!= NULL
; stmt
= next
)
2206 if (stmt
->bfd_section
== after_sec
)
2216 /* If INSERT_AFTER is FALSE, place OS before
2217 AFTER_SEC output statement. */
2218 if (next
&& next
->bfd_section
== after_sec
)
2228 /* Search backward to insert OS after AFTER_SEC output
2231 for (stmt
= after
; stmt
!= NULL
; stmt
= stmt
->prev
)
2235 if (stmt
->bfd_section
== after_sec
)
2244 /* If INSERT_AFTER is FALSE, place OS before
2245 AFTER_SEC output statement. */
2246 if (stmt
->next
->bfd_section
== after_sec
)
2256 if (after_sec
== NULL
2257 || (insert_after
&& after_sec
->next
!= snew
)
2258 || (!insert_after
&& after_sec
->prev
!= snew
))
2260 /* Unlink the section. */
2261 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2263 /* Place SNEW after AFTER_SEC. If AFTER_SEC is NULL,
2268 bfd_section_list_insert_after (link_info
.output_bfd
,
2271 bfd_section_list_insert_before (link_info
.output_bfd
,
2275 bfd_section_list_prepend (link_info
.output_bfd
, snew
);
2278 else if (as
!= snew
&& as
->prev
!= snew
)
2280 /* Unlink the section. */
2281 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2283 /* Now tack it back on in the right place. */
2284 bfd_section_list_insert_before (link_info
.output_bfd
,
2288 else if (as
!= snew
&& as
->prev
!= snew
)
2290 /* Unlink the section. */
2291 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2293 /* Now tack it back on in the right place. */
2294 bfd_section_list_insert_before (link_info
.output_bfd
, as
, snew
);
2297 /* Save the end of this list. Further ophans of this type will
2298 follow the one we've just added. */
2299 place
->section
= &snew
->next
;
2301 /* The following is non-cosmetic. We try to put the output
2302 statements in some sort of reasonable order here, because they
2303 determine the final load addresses of the orphan sections.
2304 In addition, placing output statements in the wrong order may
2305 require extra segments. For instance, given a typical
2306 situation of all read-only sections placed in one segment and
2307 following that a segment containing all the read-write
2308 sections, we wouldn't want to place an orphan read/write
2309 section before or amongst the read-only ones. */
2310 if (add
.head
!= NULL
)
2312 lang_output_section_statement_type
*newly_added_os
;
2314 /* Place OS after AFTER if AFTER_NOTE is TRUE. */
2317 lang_statement_union_type
**where
;
2319 where
= insert_os_after ((lang_statement_union_type
*) after
);
2323 place
->os_tail
= &after
->next
;
2327 /* Put it after the last orphan statement we added. */
2328 *add
.tail
= *place
->stmt
;
2329 *place
->stmt
= add
.head
;
2332 /* Fix the global list pointer if we happened to tack our
2333 new list at the tail. */
2334 if (*stat_ptr
->tail
== add
.head
)
2335 stat_ptr
->tail
= add
.tail
;
2337 /* Save the end of this list. */
2338 place
->stmt
= add
.tail
;
2340 /* Do the same for the list of output section statements. */
2341 newly_added_os
= *os_tail
;
2343 newly_added_os
->prev
= (lang_output_section_statement_type
*)
2344 ((char *) place
->os_tail
2345 - offsetof (lang_output_section_statement_type
, next
));
2346 newly_added_os
->next
= *place
->os_tail
;
2347 if (newly_added_os
->next
!= NULL
)
2348 newly_added_os
->next
->prev
= newly_added_os
;
2349 *place
->os_tail
= newly_added_os
;
2350 place
->os_tail
= &newly_added_os
->next
;
2352 /* Fixing the global list pointer here is a little different.
2353 We added to the list in lang_enter_output_section_statement,
2354 trimmed off the new output_section_statment above when
2355 assigning *os_tail = NULL, but possibly added it back in
2356 the same place when assigning *place->os_tail. */
2357 if (*os_tail
== NULL
)
2358 lang_os_list
.tail
= (lang_statement_union_type
**) os_tail
;
2365 lang_print_asneeded (void)
2367 struct asneeded_minfo
*m
;
2369 if (asneeded_list_head
== NULL
)
2372 minfo (_("\nAs-needed library included to satisfy reference by file (symbol)\n\n"));
2374 for (m
= asneeded_list_head
; m
!= NULL
; m
= m
->next
)
2378 minfo ("%s", m
->soname
);
2379 len
= strlen (m
->soname
);
2386 print_spaces (30 - len
);
2389 minfo ("%pB ", m
->ref
);
2390 minfo ("(%pT)\n", m
->name
);
2395 lang_map_flags (flagword flag
)
2397 if (flag
& SEC_ALLOC
)
2400 if (flag
& SEC_CODE
)
2403 if (flag
& SEC_READONLY
)
2406 if (flag
& SEC_DATA
)
2409 if (flag
& SEC_LOAD
)
2416 lang_memory_region_type
*m
;
2417 bool dis_header_printed
= false;
2419 ldfile_print_input_remaps ();
2421 LANG_FOR_EACH_INPUT_STATEMENT (file
)
2425 if ((file
->the_bfd
->flags
& (BFD_LINKER_CREATED
| DYNAMIC
)) != 0
2426 || file
->flags
.just_syms
)
2429 if (config
.print_map_discarded
)
2430 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
2431 if ((s
->output_section
== NULL
2432 || s
->output_section
->owner
!= link_info
.output_bfd
)
2433 && (s
->flags
& (SEC_LINKER_CREATED
| SEC_KEEP
)) == 0)
2435 if (! dis_header_printed
)
2437 minfo (_("\nDiscarded input sections\n\n"));
2438 dis_header_printed
= true;
2441 print_input_section (s
, true);
2444 if (config
.print_map_discarded
&& ! dis_header_printed
)
2445 minfo (_("\nThere are no discarded input sections\n"));
2447 minfo (_("\nMemory Configuration\n\n"));
2448 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
2449 _("Name"), _("Origin"), _("Length"), _("Attributes"));
2451 for (m
= lang_memory_region_list
; m
!= NULL
; m
= m
->next
)
2453 fprintf (config
.map_file
, "%-16s", m
->name_list
.name
);
2456 bfd_sprintf_vma (link_info
.output_bfd
, buf
, m
->origin
);
2457 fprintf (config
.map_file
, " 0x%-16s", buf
);
2458 bfd_sprintf_vma (link_info
.output_bfd
, buf
, m
->length
);
2459 fprintf (config
.map_file
,
2460 " 0x%*s", m
->flags
|| m
->not_flags
? -17 : 0, buf
);
2462 lang_map_flags (m
->flags
);
2467 lang_map_flags (m
->not_flags
);
2473 minfo (_("\nLinker script and memory map\n\n"));
2475 if (!link_info
.reduce_memory_overheads
)
2477 obstack_begin (&map_obstack
, 1000);
2478 bfd_link_hash_traverse (link_info
.hash
, sort_def_symbol
, 0);
2480 expld
.phase
= lang_fixed_phase_enum
;
2481 lang_statement_iteration
++;
2482 print_statements ();
2484 ldemul_extra_map_file_text (link_info
.output_bfd
, &link_info
,
2489 is_defined (struct bfd_link_hash_entry
*h
)
2492 && (h
->type
== bfd_link_hash_defined
2493 || h
->type
== bfd_link_hash_defweak
);
2497 sort_def_symbol (struct bfd_link_hash_entry
*hash_entry
,
2498 void *info ATTRIBUTE_UNUSED
)
2500 if (is_defined (hash_entry
)
2501 && hash_entry
->u
.def
.section
->owner
!= link_info
.output_bfd
2502 && hash_entry
->u
.def
.section
->owner
!= NULL
)
2504 input_section_userdata_type
*ud
;
2505 struct map_symbol_def
*def
;
2507 ud
= bfd_section_userdata (hash_entry
->u
.def
.section
);
2510 ud
= stat_alloc (sizeof (*ud
));
2511 bfd_set_section_userdata (hash_entry
->u
.def
.section
, ud
);
2512 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
2513 ud
->map_symbol_def_count
= 0;
2515 else if (!ud
->map_symbol_def_tail
)
2516 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
2518 def
= (struct map_symbol_def
*) obstack_alloc (&map_obstack
, sizeof *def
);
2519 def
->entry
= hash_entry
;
2520 *(ud
->map_symbol_def_tail
) = def
;
2521 ud
->map_symbol_def_tail
= &def
->next
;
2522 ud
->map_symbol_def_count
++;
2527 /* Initialize an output section. */
2530 init_os (lang_output_section_statement_type
*s
, flagword flags
)
2532 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
2533 fatal (_("%P: illegal use of `%s' section\n"), DISCARD_SECTION_NAME
);
2536 s
->bfd_section
= bfd_get_section_by_name (link_info
.output_bfd
, s
->name
);
2537 if (s
->bfd_section
== NULL
)
2538 s
->bfd_section
= bfd_make_section_anyway_with_flags (link_info
.output_bfd
,
2540 if (s
->bfd_section
== NULL
)
2542 fatal (_("%P: output format %s cannot represent section"
2543 " called %s: %E\n"),
2544 link_info
.output_bfd
->xvec
->name
, s
->name
);
2546 s
->bfd_section
->output_section
= s
->bfd_section
;
2547 s
->bfd_section
->output_offset
= 0;
2549 /* Set the userdata of the output section to the output section
2550 statement to avoid lookup. */
2551 bfd_set_section_userdata (s
->bfd_section
, s
);
2553 /* If there is a base address, make sure that any sections it might
2554 mention are initialized. */
2555 if (s
->addr_tree
!= NULL
)
2556 exp_init_os (s
->addr_tree
);
2558 if (s
->load_base
!= NULL
)
2559 exp_init_os (s
->load_base
);
2561 /* If supplied an alignment, set it. */
2562 if (s
->section_alignment
!= NULL
)
2563 s
->bfd_section
->alignment_power
= exp_get_power (s
->section_alignment
, s
,
2564 "section alignment");
2568 get_os_init_flag (lang_output_section_statement_type
* os
)
2571 switch (os
->sectype
)
2573 case readonly_section
: return SEC_READONLY
;
2574 case noload_section
: return SEC_NEVER_LOAD
;
2581 /* Make sure that all output sections mentioned in an expression are
2585 exp_init_os (etree_type
*exp
)
2587 switch (exp
->type
.node_class
)
2591 case etree_provided
:
2592 exp_init_os (exp
->assign
.src
);
2596 exp_init_os (exp
->binary
.lhs
);
2597 exp_init_os (exp
->binary
.rhs
);
2601 exp_init_os (exp
->trinary
.cond
);
2602 exp_init_os (exp
->trinary
.lhs
);
2603 exp_init_os (exp
->trinary
.rhs
);
2607 exp_init_os (exp
->assert_s
.child
);
2611 exp_init_os (exp
->unary
.child
);
2615 switch (exp
->type
.node_code
)
2620 lang_output_section_statement_type
*os
;
2622 os
= lang_output_section_find (exp
->name
.name
);
2623 if (os
!= NULL
&& os
->bfd_section
== NULL
)
2624 init_os (os
, get_os_init_flag (os
));
2635 section_already_linked (bfd
*abfd
, asection
*sec
, void *data
)
2637 lang_input_statement_type
*entry
= (lang_input_statement_type
*) data
;
2639 /* If we are only reading symbols from this object, then we want to
2640 discard all sections. */
2641 if (entry
->flags
.just_syms
)
2643 bfd_link_just_syms (abfd
, sec
, &link_info
);
2647 /* Deal with SHF_EXCLUDE ELF sections. */
2648 if (!bfd_link_relocatable (&link_info
)
2649 && (abfd
->flags
& BFD_PLUGIN
) == 0
2650 && (sec
->flags
& (SEC_GROUP
| SEC_KEEP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
2651 sec
->output_section
= bfd_abs_section_ptr
;
2653 if (!(abfd
->flags
& DYNAMIC
))
2654 bfd_section_already_linked (abfd
, sec
, &link_info
);
2658 /* Returns true if SECTION is one we know will be discarded based on its
2659 section flags, otherwise returns false. */
2662 lang_discard_section_p (asection
*section
)
2665 flagword flags
= section
->flags
;
2667 /* Discard sections marked with SEC_EXCLUDE. */
2668 discard
= (flags
& SEC_EXCLUDE
) != 0;
2670 /* Discard the group descriptor sections when we're finally placing the
2671 sections from within the group. */
2672 if ((flags
& SEC_GROUP
) != 0
2673 && link_info
.resolve_section_groups
)
2676 /* Discard debugging sections if we are stripping debugging
2678 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
2679 && (flags
& SEC_DEBUGGING
) != 0)
2682 /* Discard non-alloc sections if we are stripping section headers. */
2683 else if (config
.no_section_header
&& (flags
& SEC_ALLOC
) == 0)
2689 /* Return TRUE if SECTION is never going to be added to output statement
2690 OUTPUT. lang_add_section() definitely won't do anything with SECTION
2691 if this returns TRUE. It may do something (or not) if this returns FALSE.
2693 Can be used as early-out to filter matches. This may set
2694 output_section of SECTION, if it was unset, to the abs section in case
2695 we discover SECTION to be always discarded. This may also give
2696 warning messages. */
2699 wont_add_section_p (asection
*section
,
2700 lang_output_section_statement_type
*output
)
2704 /* Is this section one we know should be discarded? */
2705 discard
= lang_discard_section_p (section
);
2707 /* Discard input sections which are assigned to a section named
2708 DISCARD_SECTION_NAME. */
2709 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
2714 if (section
->output_section
== NULL
)
2716 /* This prevents future calls from assigning this section or
2717 warning about it again. */
2718 section
->output_section
= bfd_abs_section_ptr
;
2720 else if (bfd_is_abs_section (section
->output_section
))
2722 else if (link_info
.non_contiguous_regions_warnings
)
2723 einfo (_("%P:%pS: warning: --enable-non-contiguous-regions makes "
2724 "section `%pA' from `%pB' match /DISCARD/ clause.\n"),
2725 NULL
, section
, section
->owner
);
2730 if (section
->output_section
!= NULL
)
2732 if (!link_info
.non_contiguous_regions
)
2735 /* SECTION has already been handled in a special way
2736 (eg. LINK_ONCE): skip it. */
2737 if (bfd_is_abs_section (section
->output_section
))
2740 /* Already assigned to the same output section, do not process
2741 it again, to avoid creating loops between duplicate sections
2743 if (section
->output_section
== output
->bfd_section
)
2746 if (link_info
.non_contiguous_regions_warnings
&& output
->bfd_section
)
2747 einfo (_("%P:%pS: warning: --enable-non-contiguous-regions may "
2748 "change behaviour for section `%pA' from `%pB' (assigned to "
2749 "%pA, but additional match: %pA)\n"),
2750 NULL
, section
, section
->owner
, section
->output_section
,
2751 output
->bfd_section
);
2753 /* SECTION has already been assigned to an output section, but
2754 the user allows it to be mapped to another one in case it
2755 overflows. We'll later update the actual output section in
2756 size_input_section as appropriate. */
2762 /* The wild routines.
2764 These expand statements like *(.text) and foo.o to a list of
2765 explicit actions, like foo.o(.text), bar.o(.text) and
2766 foo.o(.text, .data). */
2768 /* Add SECTION to the output section OUTPUT. Do this by creating a
2769 lang_input_section statement which is placed at PTR. */
2772 lang_add_section (lang_statement_list_type
*ptr
,
2774 struct wildcard_list
*pattern
,
2775 struct flag_info
*sflag_info
,
2776 lang_output_section_statement_type
*output
)
2778 flagword flags
= section
->flags
;
2780 lang_input_section_type
*new_section
;
2781 bfd
*abfd
= link_info
.output_bfd
;
2783 if (wont_add_section_p (section
, output
))
2790 keep
= bfd_lookup_section_flags (&link_info
, sflag_info
, section
);
2795 /* We don't copy the SEC_NEVER_LOAD flag from an input section
2796 to an output section, because we want to be able to include a
2797 SEC_NEVER_LOAD section in the middle of an otherwise loaded
2798 section (I don't know why we want to do this, but we do).
2799 build_link_order in ldwrite.c handles this case by turning
2800 the embedded SEC_NEVER_LOAD section into a fill. */
2801 flags
&= ~ SEC_NEVER_LOAD
;
2803 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
2804 already been processed. One reason to do this is that on pe
2805 format targets, .text$foo sections go into .text and it's odd
2806 to see .text with SEC_LINK_ONCE set. */
2807 if ((flags
& (SEC_LINK_ONCE
| SEC_GROUP
)) == (SEC_LINK_ONCE
| SEC_GROUP
))
2809 if (link_info
.resolve_section_groups
)
2810 flags
&= ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
);
2812 flags
&= ~(SEC_LINK_DUPLICATES
| SEC_RELOC
);
2814 else if (!bfd_link_relocatable (&link_info
))
2815 flags
&= ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
);
2817 switch (output
->sectype
)
2819 case normal_section
:
2820 case overlay_section
:
2821 case first_overlay_section
:
2824 case noalloc_section
:
2825 flags
&= ~SEC_ALLOC
;
2827 case typed_readonly_section
:
2828 case readonly_section
:
2829 flags
|= SEC_READONLY
;
2831 case noload_section
:
2833 flags
|= SEC_NEVER_LOAD
;
2834 /* Unfortunately GNU ld has managed to evolve two different
2835 meanings to NOLOAD in scripts. ELF gets a .bss style noload,
2836 alloc, no contents section. All others get a noload, noalloc
2837 section. Unlike a .bss style section, if a note section is
2838 marked as NOLOAD, also clear SEC_ALLOC. */
2839 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
2840 && elf_section_type (section
) != SHT_NOTE
)
2841 flags
&= ~SEC_HAS_CONTENTS
;
2843 flags
&= ~SEC_ALLOC
;
2847 if (output
->bfd_section
== NULL
)
2848 init_os (output
, flags
);
2850 /* If SEC_READONLY is not set in the input section, then clear
2851 it from the output section. */
2852 output
->bfd_section
->flags
&= flags
| ~SEC_READONLY
;
2854 if (output
->bfd_section
->linker_has_input
)
2856 /* Only set SEC_READONLY flag on the first input section. */
2857 flags
&= ~ SEC_READONLY
;
2859 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
2860 if ((output
->bfd_section
->flags
& (SEC_MERGE
| SEC_STRINGS
))
2861 != (flags
& (SEC_MERGE
| SEC_STRINGS
))
2862 || ((flags
& SEC_MERGE
) != 0
2863 && output
->bfd_section
->entsize
!= section
->entsize
))
2865 output
->bfd_section
->flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2866 flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2869 output
->bfd_section
->flags
|= flags
;
2871 if (!output
->bfd_section
->linker_has_input
)
2873 output
->bfd_section
->linker_has_input
= 1;
2874 /* This must happen after flags have been updated. The output
2875 section may have been created before we saw its first input
2876 section, eg. for a data statement. */
2877 bfd_init_private_section_data (section
->owner
, section
,
2878 link_info
.output_bfd
,
2879 output
->bfd_section
,
2881 if ((flags
& SEC_MERGE
) != 0)
2882 output
->bfd_section
->entsize
= section
->entsize
;
2885 if ((flags
& SEC_TIC54X_BLOCK
) != 0
2886 && bfd_get_arch (section
->owner
) == bfd_arch_tic54x
)
2888 /* FIXME: This value should really be obtained from the bfd... */
2889 output
->block_value
= 128;
2892 /* When a .ctors section is placed in .init_array it must be copied
2893 in reverse order. Similarly for .dtors. Set that up. */
2894 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
2895 && ((startswith (section
->name
, ".ctors")
2896 && strcmp (output
->bfd_section
->name
, ".init_array") == 0)
2897 || (startswith (section
->name
, ".dtors")
2898 && strcmp (output
->bfd_section
->name
, ".fini_array") == 0))
2899 && (section
->name
[6] == 0 || section
->name
[6] == '.'))
2900 section
->flags
|= SEC_ELF_REVERSE_COPY
;
2902 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
2903 output
->bfd_section
->alignment_power
= section
->alignment_power
;
2905 section
->output_section
= output
->bfd_section
;
2907 if (!map_head_is_link_order
)
2909 asection
*s
= output
->bfd_section
->map_tail
.s
;
2910 output
->bfd_section
->map_tail
.s
= section
;
2911 section
->map_head
.s
= NULL
;
2912 section
->map_tail
.s
= s
;
2914 s
->map_head
.s
= section
;
2916 output
->bfd_section
->map_head
.s
= section
;
2919 /* Add a section reference to the list. */
2920 new_section
= new_stat (lang_input_section
, ptr
);
2921 new_section
->section
= section
;
2922 new_section
->pattern
= pattern
;
2925 /* Expand a wild statement for a particular FILE. SECTION may be
2926 NULL, in which case it is a wild card. This assumes that the
2927 wild statement doesn't need any sorting (of filenames or sections). */
2930 output_section_callback_nosort (lang_wild_statement_type
*ptr
,
2931 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
2933 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
2936 lang_output_section_statement_type
*os
;
2938 os
= (lang_output_section_statement_type
*) output
;
2940 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2941 if (unique_section_p (section
, os
))
2944 lang_add_section (&ptr
->children
, section
, ptr
->section_list
,
2945 ptr
->section_flag_list
, os
);
2948 /* Check if all sections in a wild statement for a particular FILE
2952 check_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
2953 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
2955 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
2958 lang_output_section_statement_type
*os
;
2960 os
= (lang_output_section_statement_type
*) output
;
2962 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2963 if (unique_section_p (section
, os
))
2966 if (section
->output_section
== NULL
&& (section
->flags
& SEC_READONLY
) == 0)
2967 os
->all_input_readonly
= false;
2970 /* This is passed a file name which must have been seen already and
2971 added to the statement tree. We will see if it has been opened
2972 already and had its symbols read. If not then we'll read it. */
2974 static lang_input_statement_type
*
2975 lookup_name (const char *name
)
2977 lang_input_statement_type
*search
;
2979 for (search
= (void *) input_file_chain
.head
;
2981 search
= search
->next_real_file
)
2983 /* Use the local_sym_name as the name of the file that has
2984 already been loaded as filename might have been transformed
2985 via the search directory lookup mechanism. */
2986 const char *filename
= search
->local_sym_name
;
2988 if (filename
!= NULL
2989 && filename_cmp (filename
, name
) == 0)
2995 /* Arrange to splice the input statement added by new_afile into
2996 statement_list after the current input_file_chain tail.
2997 We know input_file_chain is not an empty list, and that
2998 lookup_name was called via open_input_bfds. Later calls to
2999 lookup_name should always match an existing input_statement. */
3000 lang_statement_union_type
**tail
= stat_ptr
->tail
;
3001 lang_statement_union_type
**after
3002 = (void *) ((char *) input_file_chain
.tail
3003 - offsetof (lang_input_statement_type
, next_real_file
)
3004 + offsetof (lang_input_statement_type
, header
.next
));
3005 lang_statement_union_type
*rest
= *after
;
3006 stat_ptr
->tail
= after
;
3007 search
= new_afile (name
, lang_input_file_is_search_file_enum
,
3008 default_target
, NULL
);
3009 *stat_ptr
->tail
= rest
;
3011 stat_ptr
->tail
= tail
;
3014 /* If we have already added this file, or this file is not real
3015 don't add this file. */
3016 if (search
->flags
.loaded
|| !search
->flags
.real
)
3019 if (!load_symbols (search
, NULL
))
3025 /* Save LIST as a list of libraries whose symbols should not be exported. */
3030 struct excluded_lib
*next
;
3032 static struct excluded_lib
*excluded_libs
;
3035 add_excluded_libs (const char *list
)
3037 const char *p
= list
, *end
;
3041 struct excluded_lib
*entry
;
3042 end
= strpbrk (p
, ",:");
3044 end
= p
+ strlen (p
);
3045 entry
= stat_alloc (sizeof (*entry
));
3046 entry
->next
= excluded_libs
;
3047 entry
->name
= stat_memdup (p
, end
- p
, end
- p
+ 1);
3048 excluded_libs
= entry
;
3056 check_excluded_libs (bfd
*abfd
)
3058 struct excluded_lib
*lib
= excluded_libs
;
3062 int len
= strlen (lib
->name
);
3063 const char *filename
= lbasename (bfd_get_filename (abfd
));
3065 if (strcmp (lib
->name
, "ALL") == 0)
3067 abfd
->no_export
= true;
3071 if (filename_ncmp (lib
->name
, filename
, len
) == 0
3072 && (filename
[len
] == '\0'
3073 || (filename
[len
] == '.' && filename
[len
+ 1] == 'a'
3074 && filename
[len
+ 2] == '\0')))
3076 abfd
->no_export
= true;
3084 /* Get the symbols for an input file. */
3087 load_symbols (lang_input_statement_type
*entry
,
3088 lang_statement_list_type
*place
)
3092 if (entry
->flags
.loaded
)
3095 ldfile_open_file (entry
);
3097 /* Do not process further if the file was missing. */
3098 if (entry
->flags
.missing_file
)
3101 if (trace_files
|| verbose
)
3102 info_msg ("%pI\n", entry
);
3104 if (!bfd_check_format (entry
->the_bfd
, bfd_archive
)
3105 && !bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
3108 struct lang_input_statement_flags save_flags
;
3111 err
= bfd_get_error ();
3113 /* See if the emulation has some special knowledge. */
3114 if (ldemul_unrecognized_file (entry
))
3116 if (err
== bfd_error_file_ambiguously_recognized
)
3121 if (err
== bfd_error_file_ambiguously_recognized
)
3125 einfo (_("%P: %pB: file not recognized: %E;"
3126 " matching formats:"), entry
->the_bfd
);
3127 for (p
= matching
; *p
!= NULL
; p
++)
3132 else if (err
!= bfd_error_file_not_recognized
3134 fatal (_("%P: %pB: file not recognized: %E\n"), entry
->the_bfd
);
3136 bfd_close (entry
->the_bfd
);
3137 entry
->the_bfd
= NULL
;
3139 /* Try to interpret the file as a linker script. */
3140 save_flags
= input_flags
;
3141 ldfile_open_command_file (entry
->filename
);
3143 push_stat_ptr (place
);
3144 input_flags
.add_DT_NEEDED_for_regular
3145 = entry
->flags
.add_DT_NEEDED_for_regular
;
3146 input_flags
.add_DT_NEEDED_for_dynamic
3147 = entry
->flags
.add_DT_NEEDED_for_dynamic
;
3148 input_flags
.whole_archive
= entry
->flags
.whole_archive
;
3149 input_flags
.dynamic
= entry
->flags
.dynamic
;
3151 ldfile_assumed_script
= true;
3152 parser_input
= input_script
;
3153 current_input_file
= entry
->filename
;
3155 current_input_file
= NULL
;
3156 ldfile_assumed_script
= false;
3158 /* missing_file is sticky. sysrooted will already have been
3159 restored when seeing EOF in yyparse, but no harm to restore
3161 save_flags
.missing_file
|= input_flags
.missing_file
;
3162 input_flags
= save_flags
;
3166 entry
->flags
.loaded
= true;
3171 if (ldemul_recognized_file (entry
))
3174 /* We don't call ldlang_add_file for an archive. Instead, the
3175 add_symbols entry point will call ldlang_add_file, via the
3176 add_archive_element callback, for each element of the archive
3178 switch (bfd_get_format (entry
->the_bfd
))
3184 if (!entry
->flags
.reload
)
3185 ldlang_add_file (entry
);
3189 check_excluded_libs (entry
->the_bfd
);
3191 bfd_set_usrdata (entry
->the_bfd
, entry
);
3192 if (entry
->flags
.whole_archive
)
3200 member
= bfd_openr_next_archived_file (entry
->the_bfd
, member
);
3205 if (!bfd_check_format (member
, bfd_object
))
3207 fatal (_("%P: %pB: member %pB in archive is not an object\n"),
3208 entry
->the_bfd
, member
);
3212 if (config
.emitting_gnu_object_only
)
3214 if (!cmdline_on_object_only_archive_list_p (member
))
3219 if (!(*link_info
.callbacks
3220 ->add_archive_element
) (&link_info
, member
,
3221 "--whole-archive", &subsbfd
))
3224 /* Potentially, the add_archive_element hook may have set a
3225 substitute BFD for us. */
3226 if (!bfd_link_add_symbols (subsbfd
, &link_info
))
3228 fatal (_("%P: %pB: error adding symbols: %E\n"), member
);
3233 entry
->flags
.loaded
= loaded
;
3239 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
3240 entry
->flags
.loaded
= true;
3242 fatal (_("%P: %pB: error adding symbols: %E\n"), entry
->the_bfd
);
3244 return entry
->flags
.loaded
;
3247 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
3248 may be NULL, indicating that it is a wildcard. Separate
3249 lang_input_section statements are created for each part of the
3250 expansion; they are added after the wild statement S. OUTPUT is
3251 the output section. */
3254 wild (lang_wild_statement_type
*s
,
3255 const char *target ATTRIBUTE_UNUSED
,
3256 lang_output_section_statement_type
*output
)
3258 struct wildcard_list
*sec
;
3260 if (s
->filenames_sorted
|| s
->any_specs_sorted
)
3262 lang_section_bst_type
*tree
;
3264 walk_wild (s
, output_section_callback_sort
, output
);
3269 output_section_callback_tree_to_list (s
, tree
, output
);
3271 s
->rightmost
= &s
->tree
;
3275 walk_wild (s
, output_section_callback_nosort
, output
);
3277 if (default_common_section
== NULL
)
3278 for (sec
= s
->section_list
; sec
!= NULL
; sec
= sec
->next
)
3279 if (sec
->spec
.name
!= NULL
&& strcmp (sec
->spec
.name
, "COMMON") == 0)
3281 /* Remember the section that common is going to in case we
3282 later get something which doesn't know where to put it. */
3283 default_common_section
= output
;
3288 /* Return TRUE iff target is the sought target. */
3291 get_target (const bfd_target
*target
, void *data
)
3293 const char *sought
= (const char *) data
;
3295 return strcmp (target
->name
, sought
) == 0;
3298 /* Like strcpy() but convert to lower case as well. */
3301 stricpy (char *dest
, const char *src
)
3305 while ((c
= *src
++) != 0)
3306 *dest
++ = TOLOWER (c
);
3311 /* Remove the first occurrence of needle (if any) in haystack
3315 strcut (char *haystack
, const char *needle
)
3317 haystack
= strstr (haystack
, needle
);
3323 for (src
= haystack
+ strlen (needle
); *src
;)
3324 *haystack
++ = *src
++;
3330 /* Compare two target format name strings.
3331 Return a value indicating how "similar" they are. */
3334 name_compare (const char *first
, const char *second
)
3340 copy1
= (char *) xmalloc (strlen (first
) + 1);
3341 copy2
= (char *) xmalloc (strlen (second
) + 1);
3343 /* Convert the names to lower case. */
3344 stricpy (copy1
, first
);
3345 stricpy (copy2
, second
);
3347 /* Remove size and endian strings from the name. */
3348 strcut (copy1
, "big");
3349 strcut (copy1
, "little");
3350 strcut (copy2
, "big");
3351 strcut (copy2
, "little");
3353 /* Return a value based on how many characters match,
3354 starting from the beginning. If both strings are
3355 the same then return 10 * their length. */
3356 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
3357 if (copy1
[result
] == 0)
3369 /* Set by closest_target_match() below. */
3370 static const bfd_target
*winner
;
3372 /* Scan all the valid bfd targets looking for one that has the endianness
3373 requirement that was specified on the command line, and is the nearest
3374 match to the original output target. */
3377 closest_target_match (const bfd_target
*target
, void *data
)
3379 const bfd_target
*original
= (const bfd_target
*) data
;
3381 if (command_line
.endian
== ENDIAN_BIG
3382 && target
->byteorder
!= BFD_ENDIAN_BIG
)
3385 if (command_line
.endian
== ENDIAN_LITTLE
3386 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
3389 /* Must be the same flavour. */
3390 if (target
->flavour
!= original
->flavour
)
3393 /* Ignore generic big and little endian elf vectors. */
3394 if (strcmp (target
->name
, "elf32-big") == 0
3395 || strcmp (target
->name
, "elf64-big") == 0
3396 || strcmp (target
->name
, "elf32-little") == 0
3397 || strcmp (target
->name
, "elf64-little") == 0)
3400 /* If we have not found a potential winner yet, then record this one. */
3407 /* Oh dear, we now have two potential candidates for a successful match.
3408 Compare their names and choose the better one. */
3409 if (name_compare (target
->name
, original
->name
)
3410 > name_compare (winner
->name
, original
->name
))
3413 /* Keep on searching until wqe have checked them all. */
3417 /* Return the BFD target format of the first input file. */
3420 get_first_input_target (void)
3422 const char *target
= NULL
;
3424 LANG_FOR_EACH_INPUT_STATEMENT (s
)
3426 if (s
->header
.type
== lang_input_statement_enum
3429 ldfile_open_file (s
);
3431 if (s
->the_bfd
!= NULL
3432 && bfd_check_format (s
->the_bfd
, bfd_object
))
3434 target
= bfd_get_target (s
->the_bfd
);
3446 lang_get_output_target (void)
3450 /* Has the user told us which output format to use? */
3451 if (output_target
!= NULL
)
3452 return output_target
;
3454 /* No - has the current target been set to something other than
3456 if (current_target
!= default_target
&& current_target
!= NULL
)
3457 return current_target
;
3459 /* No - can we determine the format of the first input file? */
3460 target
= get_first_input_target ();
3464 /* Failed - use the default output target. */
3465 return default_target
;
3468 /* Open the output file. */
3471 open_output (const char *name
)
3473 lang_input_statement_type
*f
;
3474 char *out
= lrealpath (name
);
3476 for (f
= (void *) input_file_chain
.head
;
3478 f
= f
->next_real_file
)
3481 char *in
= lrealpath (f
->local_sym_name
);
3482 if (filename_cmp (in
, out
) == 0)
3483 fatal (_("%P: input file '%s' is the same as output file\n"),
3489 output_target
= lang_get_output_target ();
3491 /* Has the user requested a particular endianness on the command
3493 if (command_line
.endian
!= ENDIAN_UNSET
)
3495 /* Get the chosen target. */
3496 const bfd_target
*target
3497 = bfd_iterate_over_targets (get_target
, (void *) output_target
);
3499 /* If the target is not supported, we cannot do anything. */
3502 enum bfd_endian desired_endian
;
3504 if (command_line
.endian
== ENDIAN_BIG
)
3505 desired_endian
= BFD_ENDIAN_BIG
;
3507 desired_endian
= BFD_ENDIAN_LITTLE
;
3509 /* See if the target has the wrong endianness. This should
3510 not happen if the linker script has provided big and
3511 little endian alternatives, but some scrips don't do
3513 if (target
->byteorder
!= desired_endian
)
3515 /* If it does, then see if the target provides
3516 an alternative with the correct endianness. */
3517 if (target
->alternative_target
!= NULL
3518 && (target
->alternative_target
->byteorder
== desired_endian
))
3519 output_target
= target
->alternative_target
->name
;
3522 /* Try to find a target as similar as possible to
3523 the default target, but which has the desired
3524 endian characteristic. */
3525 bfd_iterate_over_targets (closest_target_match
,
3528 /* Oh dear - we could not find any targets that
3529 satisfy our requirements. */
3531 einfo (_("%P: warning: could not find any targets"
3532 " that match endianness requirement\n"));
3534 output_target
= winner
->name
;
3540 link_info
.output_bfd
= bfd_openw (name
, output_target
);
3542 if (link_info
.output_bfd
== NULL
)
3544 if (bfd_get_error () == bfd_error_invalid_target
)
3545 fatal (_("%P: target %s not found\n"), output_target
);
3547 fatal (_("%P: cannot open output file %s: %E\n"), name
);
3550 delete_output_file_on_failure
= true;
3552 if (!bfd_set_format (link_info
.output_bfd
, bfd_object
))
3553 fatal (_("%P: %s: can not make object file: %E\n"), name
);
3554 if (!bfd_set_arch_mach (link_info
.output_bfd
,
3555 ldfile_output_architecture
,
3556 ldfile_output_machine
))
3557 fatal (_("%P: %s: can not set architecture: %E\n"), name
);
3559 link_info
.hash
= bfd_link_hash_table_create (link_info
.output_bfd
);
3560 if (link_info
.hash
== NULL
)
3561 fatal (_("%P: can not create hash table: %E\n"));
3563 bfd_set_gp_size (link_info
.output_bfd
, g_switch_value
);
3567 ldlang_open_output (lang_statement_union_type
*statement
)
3569 switch (statement
->header
.type
)
3571 case lang_output_statement_enum
:
3572 ASSERT (link_info
.output_bfd
== NULL
);
3573 open_output (statement
->output_statement
.name
);
3574 ldemul_set_output_arch ();
3575 if (config
.magic_demand_paged
3576 && !bfd_link_relocatable (&link_info
))
3577 link_info
.output_bfd
->flags
|= D_PAGED
;
3579 link_info
.output_bfd
->flags
&= ~D_PAGED
;
3580 if (config
.text_read_only
)
3581 link_info
.output_bfd
->flags
|= WP_TEXT
;
3583 link_info
.output_bfd
->flags
&= ~WP_TEXT
;
3584 if (link_info
.traditional_format
)
3585 link_info
.output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
3587 link_info
.output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
3588 if (config
.no_section_header
)
3589 link_info
.output_bfd
->flags
|= BFD_NO_SECTION_HEADER
;
3591 link_info
.output_bfd
->flags
&= ~BFD_NO_SECTION_HEADER
;
3594 case lang_target_statement_enum
:
3595 current_target
= statement
->target_statement
.target
;
3603 init_opb (asection
*s
)
3608 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
3610 && (s
->flags
& SEC_ELF_OCTETS
) != 0)
3613 x
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3614 ldfile_output_machine
);
3616 while ((x
& 1) == 0)
3624 /* Open all the input files. */
3628 OPEN_BFD_NORMAL
= 0,
3632 #if BFD_SUPPORTS_PLUGINS
3633 static lang_input_statement_type
*plugin_insert
= NULL
;
3634 static struct bfd_link_hash_entry
*plugin_undefs
= NULL
;
3638 open_input_bfds (lang_statement_union_type
*s
,
3639 lang_output_section_statement_type
*os
,
3640 enum open_bfd_mode mode
)
3642 for (; s
!= NULL
; s
= s
->header
.next
)
3644 switch (s
->header
.type
)
3646 case lang_constructors_statement_enum
:
3647 open_input_bfds (constructor_list
.head
, os
, mode
);
3649 case lang_output_section_statement_enum
:
3650 os
= &s
->output_section_statement
;
3651 open_input_bfds (os
->children
.head
, os
, mode
);
3653 case lang_wild_statement_enum
:
3654 /* Maybe we should load the file's symbols. */
3655 if ((mode
& OPEN_BFD_RESCAN
) == 0
3656 && s
->wild_statement
.filename
3657 && !wildcardp (s
->wild_statement
.filename
)
3658 && !archive_path (s
->wild_statement
.filename
))
3659 lookup_name (s
->wild_statement
.filename
);
3660 open_input_bfds (s
->wild_statement
.children
.head
, os
, mode
);
3662 case lang_group_statement_enum
:
3664 struct bfd_link_hash_entry
*undefs
;
3665 #if BFD_SUPPORTS_PLUGINS
3666 lang_input_statement_type
*plugin_insert_save
;
3669 /* We must continually search the entries in the group
3670 until no new symbols are added to the list of undefined
3675 #if BFD_SUPPORTS_PLUGINS
3676 plugin_insert_save
= plugin_insert
;
3678 undefs
= link_info
.hash
->undefs_tail
;
3679 open_input_bfds (s
->group_statement
.children
.head
, os
,
3680 mode
| OPEN_BFD_FORCE
);
3682 while (undefs
!= link_info
.hash
->undefs_tail
3683 #if BFD_SUPPORTS_PLUGINS
3684 /* Objects inserted by a plugin, which are loaded
3685 before we hit this loop, may have added new
3687 || (plugin_insert
!= plugin_insert_save
&& plugin_undefs
)
3692 case lang_target_statement_enum
:
3693 current_target
= s
->target_statement
.target
;
3695 case lang_input_statement_enum
:
3696 if (s
->input_statement
.flags
.real
)
3698 lang_statement_union_type
**os_tail
;
3699 lang_statement_list_type add
;
3702 s
->input_statement
.target
= current_target
;
3704 /* If we are being called from within a group, and this
3705 is an archive which has already been searched, then
3706 force it to be researched unless the whole archive
3707 has been loaded already. Do the same for a rescan.
3708 Likewise reload --as-needed shared libs. */
3709 if (mode
!= OPEN_BFD_NORMAL
3710 #if BFD_SUPPORTS_PLUGINS
3711 && ((mode
& OPEN_BFD_RESCAN
) == 0
3712 || plugin_insert
== NULL
)
3714 && s
->input_statement
.flags
.loaded
3715 && (abfd
= s
->input_statement
.the_bfd
) != NULL
3716 && ((bfd_get_format (abfd
) == bfd_archive
3717 && !s
->input_statement
.flags
.whole_archive
)
3718 || (bfd_get_format (abfd
) == bfd_object
3719 && ((abfd
->flags
) & DYNAMIC
) != 0
3720 && s
->input_statement
.flags
.add_DT_NEEDED_for_regular
3721 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
3722 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)))
3724 s
->input_statement
.flags
.loaded
= false;
3725 s
->input_statement
.flags
.reload
= true;
3728 os_tail
= lang_os_list
.tail
;
3729 lang_list_init (&add
);
3731 if (!load_symbols (&s
->input_statement
, &add
))
3732 config
.make_executable
= false;
3734 if (add
.head
!= NULL
)
3736 /* If this was a script with output sections then
3737 tack any added statements on to the end of the
3738 list. This avoids having to reorder the output
3739 section statement list. Very likely the user
3740 forgot -T, and whatever we do here will not meet
3741 naive user expectations. */
3742 if (os_tail
!= lang_os_list
.tail
)
3744 einfo (_("%P: warning: %s contains output sections;"
3745 " did you forget -T?\n"),
3746 s
->input_statement
.filename
);
3747 *stat_ptr
->tail
= add
.head
;
3748 stat_ptr
->tail
= add
.tail
;
3752 *add
.tail
= s
->header
.next
;
3753 s
->header
.next
= add
.head
;
3757 #if BFD_SUPPORTS_PLUGINS
3758 /* If we have found the point at which a plugin added new
3759 files, clear plugin_insert to enable archive rescan. */
3760 if (&s
->input_statement
== plugin_insert
)
3761 plugin_insert
= NULL
;
3764 case lang_assignment_statement_enum
:
3765 if (s
->assignment_statement
.exp
->type
.node_class
!= etree_assert
)
3766 exp_fold_tree_no_dot (s
->assignment_statement
.exp
, os
);
3773 /* Exit if any of the files were missing. */
3774 if (input_flags
.missing_file
)
3778 #ifdef ENABLE_LIBCTF
3779 /* Emit CTF errors and warnings. fp can be NULL to report errors/warnings
3780 that happened specifically at CTF open time. */
3782 lang_ctf_errs_warnings (ctf_dict_t
*fp
)
3784 ctf_next_t
*i
= NULL
;
3789 while ((text
= ctf_errwarning_next (fp
, &i
, &is_warning
, &err
)) != NULL
)
3791 einfo (_("%s: %s\n"), is_warning
? _("CTF warning"): _("CTF error"),
3795 if (err
!= ECTF_NEXT_END
)
3797 einfo (_("CTF error: cannot get CTF errors: `%s'\n"),
3801 /* `err' returns errors from the error/warning iterator in particular.
3802 These never assert. But if we have an fp, that could have recorded
3803 an assertion failure: assert if it has done so. */
3804 ASSERT (!fp
|| ctf_errno (fp
) != ECTF_INTERNAL
);
3807 /* Open the CTF sections in the input files with libctf: if any were opened,
3808 create a fake input file that we'll write the merged CTF data to later
3812 ldlang_open_ctf (void)
3817 ld_start_phase (PHASE_CTF
);
3819 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3823 /* Incoming files from the compiler have a single ctf_dict_t in them
3824 (which is presented to us by the libctf API in a ctf_archive_t
3825 wrapper): files derived from a previous relocatable link have a CTF
3826 archive containing possibly many CTF files. */
3828 if ((file
->the_ctf
= ctf_bfdopen (file
->the_bfd
, &err
)) == NULL
)
3830 if (err
!= ECTF_NOCTFDATA
)
3832 lang_ctf_errs_warnings (NULL
);
3833 einfo (_("%P: warning: CTF section in %pB not loaded; "
3834 "its types will be discarded: %s\n"), file
->the_bfd
,
3840 /* Prevent the contents of this section from being written, while
3841 requiring the section itself to be duplicated in the output, but only
3843 /* This section must exist if ctf_bfdopen() succeeded. */
3844 sect
= bfd_get_section_by_name (file
->the_bfd
, ".ctf");
3846 sect
->flags
|= SEC_NEVER_LOAD
| SEC_HAS_CONTENTS
| SEC_LINKER_CREATED
;
3849 sect
->flags
|= SEC_EXCLUDE
;
3856 ld_stop_phase (PHASE_CTF
);
3860 if ((ctf_output
= ctf_create (&err
)) != NULL
)
3862 ld_stop_phase (PHASE_CTF
);
3866 einfo (_("%P: warning: CTF output not created: `%s'\n"),
3869 LANG_FOR_EACH_INPUT_STATEMENT (errfile
)
3870 ctf_close (errfile
->the_ctf
);
3872 ld_stop_phase (PHASE_CTF
);
3875 /* Merge together CTF sections. After this, only the symtab-dependent
3876 function and data object sections need adjustment. */
3879 lang_merge_ctf (void)
3881 asection
*output_sect
;
3887 ld_start_phase (PHASE_CTF
);
3889 output_sect
= bfd_get_section_by_name (link_info
.output_bfd
, ".ctf");
3891 /* If the section was discarded, don't waste time merging. */
3892 if (output_sect
== NULL
)
3894 ctf_dict_close (ctf_output
);
3897 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3899 ctf_close (file
->the_ctf
);
3900 file
->the_ctf
= NULL
;
3903 ld_stop_phase (PHASE_CTF
);
3907 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3912 /* Takes ownership of file->the_ctf. */
3913 if (ctf_link_add_ctf (ctf_output
, file
->the_ctf
, file
->filename
) < 0)
3915 einfo (_("%P: warning: CTF section in %pB cannot be linked: `%s'\n"),
3916 file
->the_bfd
, ctf_errmsg (ctf_errno (ctf_output
)));
3917 ctf_close (file
->the_ctf
);
3918 file
->the_ctf
= NULL
;
3923 if (!config
.ctf_share_duplicated
)
3924 flags
= CTF_LINK_SHARE_UNCONFLICTED
;
3926 flags
= CTF_LINK_SHARE_DUPLICATED
;
3927 if (!config
.ctf_variables
)
3928 flags
|= CTF_LINK_OMIT_VARIABLES_SECTION
;
3929 if (bfd_link_relocatable (&link_info
))
3930 flags
|= CTF_LINK_NO_FILTER_REPORTED_SYMS
;
3932 if (ctf_link (ctf_output
, flags
) < 0)
3934 lang_ctf_errs_warnings (ctf_output
);
3935 einfo (_("%P: warning: CTF linking failed; "
3936 "output will have no CTF section: %s\n"),
3937 ctf_errmsg (ctf_errno (ctf_output
)));
3940 output_sect
->size
= 0;
3941 output_sect
->flags
|= SEC_EXCLUDE
;
3944 /* Output any lingering errors that didn't come from ctf_link. */
3945 lang_ctf_errs_warnings (ctf_output
);
3947 ld_stop_phase (PHASE_CTF
);
3950 /* Let the emulation acquire strings from the dynamic strtab to help it optimize
3951 the CTF, if supported. */
3954 ldlang_ctf_acquire_strings (struct elf_strtab_hash
*dynstrtab
)
3956 ld_start_phase (PHASE_CTF
);
3957 ldemul_acquire_strings_for_ctf (ctf_output
, dynstrtab
);
3958 ld_stop_phase (PHASE_CTF
);
3961 /* Inform the emulation about the addition of a new dynamic symbol, in BFD
3963 void ldlang_ctf_new_dynsym (int symidx
, struct elf_internal_sym
*sym
)
3965 ldemul_new_dynsym_for_ctf (ctf_output
, symidx
, sym
);
3968 /* Write out the CTF section. Called early, if the emulation isn't going to
3969 need to dedup against the strtab and symtab, then possibly called from the
3970 target linker code if the dedup has happened. */
3972 lang_write_ctf (int late
)
3975 asection
*output_sect
;
3980 ld_start_phase (PHASE_CTF
);
3984 /* Emit CTF late if this emulation says it can do so. */
3985 if (ldemul_emit_ctf_early ())
3987 ld_stop_phase (PHASE_CTF
);
3993 if (!ldemul_emit_ctf_early ())
3995 ld_stop_phase (PHASE_CTF
);
4000 /* Inform the emulation that all the symbols that will be received have
4003 ldemul_new_dynsym_for_ctf (ctf_output
, 0, NULL
);
4007 output_sect
= bfd_get_section_by_name (link_info
.output_bfd
, ".ctf");
4010 output_sect
->contents
= ctf_link_write (ctf_output
, &output_size
,
4011 CTF_COMPRESSION_THRESHOLD
);
4012 output_sect
->size
= output_size
;
4013 output_sect
->flags
|= SEC_IN_MEMORY
| SEC_KEEP
;
4015 lang_ctf_errs_warnings (ctf_output
);
4016 if (!output_sect
->contents
)
4018 einfo (_("%P: warning: CTF section emission failed; "
4019 "output will have no CTF section: %s\n"),
4020 ctf_errmsg (ctf_errno (ctf_output
)));
4021 output_sect
->size
= 0;
4022 output_sect
->flags
|= SEC_EXCLUDE
;
4026 /* This also closes every CTF input file used in the link. */
4027 ctf_dict_close (ctf_output
);
4030 LANG_FOR_EACH_INPUT_STATEMENT (file
)
4031 file
->the_ctf
= NULL
;
4033 ld_stop_phase (PHASE_CTF
);
4036 /* Write out the CTF section late, if the emulation needs that. */
4039 ldlang_write_ctf_late (void)
4041 /* Trigger a "late call", if the emulation needs one. */
4047 ldlang_open_ctf (void)
4049 LANG_FOR_EACH_INPUT_STATEMENT (file
)
4053 /* If built without CTF, warn and delete all CTF sections from the output.
4054 (The alternative would be to simply concatenate them, which does not
4055 yield a valid CTF section.) */
4057 if ((sect
= bfd_get_section_by_name (file
->the_bfd
, ".ctf")) != NULL
)
4059 einfo (_("%P: warning: CTF section in %pB not linkable: "
4060 "%P was built without support for CTF\n"), file
->the_bfd
);
4062 sect
->flags
|= SEC_EXCLUDE
;
4067 static void lang_merge_ctf (void) {}
4069 ldlang_ctf_acquire_strings (struct elf_strtab_hash
*dynstrtab
4070 ATTRIBUTE_UNUSED
) {}
4072 ldlang_ctf_new_dynsym (int symidx ATTRIBUTE_UNUSED
,
4073 struct elf_internal_sym
*sym ATTRIBUTE_UNUSED
) {}
4074 static void lang_write_ctf (int late ATTRIBUTE_UNUSED
) {}
4075 void ldlang_write_ctf_late (void) {}
4078 /* Add the supplied name to the symbol table as an undefined reference.
4079 This is a two step process as the symbol table doesn't even exist at
4080 the time the ld command line is processed. First we put the name
4081 on a list, then, once the output file has been opened, transfer the
4082 name to the symbol table. */
4084 typedef struct bfd_sym_chain ldlang_undef_chain_list_type
;
4086 #define ldlang_undef_chain_list_head entry_symbol.next
4089 ldlang_add_undef (const char *const name
, bool cmdline ATTRIBUTE_UNUSED
)
4091 ldlang_undef_chain_list_type
*new_undef
;
4093 new_undef
= stat_alloc (sizeof (*new_undef
));
4094 new_undef
->next
= ldlang_undef_chain_list_head
;
4095 ldlang_undef_chain_list_head
= new_undef
;
4097 new_undef
->name
= stat_strdup (name
);
4099 if (link_info
.output_bfd
!= NULL
)
4100 insert_undefined (new_undef
->name
);
4103 /* Insert NAME as undefined in the symbol table. */
4106 insert_undefined (const char *name
)
4108 struct bfd_link_hash_entry
*h
;
4110 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, false, true);
4112 fatal (_("%P: bfd_link_hash_lookup failed: %E\n"));
4113 if (h
->type
== bfd_link_hash_new
)
4115 h
->type
= bfd_link_hash_undefined
;
4116 h
->u
.undef
.abfd
= NULL
;
4117 h
->non_ir_ref_regular
= true;
4118 bfd_link_add_undef (link_info
.hash
, h
);
4122 /* Run through the list of undefineds created above and place them
4123 into the linker hash table as undefined symbols belonging to the
4127 lang_place_undefineds (void)
4129 ldlang_undef_chain_list_type
*ptr
;
4131 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
4132 insert_undefined (ptr
->name
);
4135 /* Mark -u symbols against garbage collection. */
4138 lang_mark_undefineds (void)
4140 ldlang_undef_chain_list_type
*ptr
;
4142 if (is_elf_hash_table (link_info
.hash
))
4143 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
4145 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*)
4146 bfd_link_hash_lookup (link_info
.hash
, ptr
->name
, false, false, true);
4152 /* Structure used to build the list of symbols that the user has required
4155 struct require_defined_symbol
4158 struct require_defined_symbol
*next
;
4161 /* The list of symbols that the user has required be defined. */
4163 static struct require_defined_symbol
*require_defined_symbol_list
;
4165 /* Add a new symbol NAME to the list of symbols that are required to be
4169 ldlang_add_require_defined (const char *const name
)
4171 struct require_defined_symbol
*ptr
;
4173 ldlang_add_undef (name
, true);
4174 ptr
= stat_alloc (sizeof (*ptr
));
4175 ptr
->next
= require_defined_symbol_list
;
4176 ptr
->name
= stat_strdup (name
);
4177 require_defined_symbol_list
= ptr
;
4180 /* Check that all symbols the user required to be defined, are defined,
4181 raise an error if we find a symbol that is not defined. */
4184 ldlang_check_require_defined_symbols (void)
4186 struct require_defined_symbol
*ptr
;
4188 for (ptr
= require_defined_symbol_list
; ptr
!= NULL
; ptr
= ptr
->next
)
4190 struct bfd_link_hash_entry
*h
;
4192 h
= bfd_link_hash_lookup (link_info
.hash
, ptr
->name
,
4193 false, false, true);
4194 if (! is_defined (h
))
4195 einfo(_("%X%P: required symbol `%s' not defined\n"), ptr
->name
);
4199 /* Check for all readonly or some readwrite sections. */
4202 check_input_sections
4203 (lang_statement_union_type
*s
,
4204 lang_output_section_statement_type
*output_section_statement
)
4206 for (; s
!= NULL
; s
= s
->header
.next
)
4208 switch (s
->header
.type
)
4210 case lang_wild_statement_enum
:
4211 walk_wild (&s
->wild_statement
, check_section_callback
,
4212 output_section_statement
);
4213 if (!output_section_statement
->all_input_readonly
)
4216 case lang_constructors_statement_enum
:
4217 check_input_sections (constructor_list
.head
,
4218 output_section_statement
);
4219 if (!output_section_statement
->all_input_readonly
)
4222 case lang_group_statement_enum
:
4223 check_input_sections (s
->group_statement
.children
.head
,
4224 output_section_statement
);
4225 if (!output_section_statement
->all_input_readonly
)
4234 /* Update wildcard statements if needed. */
4237 update_wild_statements (lang_statement_union_type
*s
)
4239 struct wildcard_list
*sec
;
4241 switch (sort_section
)
4251 for (; s
!= NULL
; s
= s
->header
.next
)
4253 switch (s
->header
.type
)
4258 case lang_wild_statement_enum
:
4259 for (sec
= s
->wild_statement
.section_list
; sec
!= NULL
;
4261 /* Don't sort .init/.fini sections. */
4262 if (strcmp (sec
->spec
.name
, ".init") != 0
4263 && strcmp (sec
->spec
.name
, ".fini") != 0)
4265 switch (sec
->spec
.sorted
)
4268 sec
->spec
.sorted
= sort_section
;
4271 if (sort_section
== by_alignment
)
4272 sec
->spec
.sorted
= by_name_alignment
;
4275 if (sort_section
== by_name
)
4276 sec
->spec
.sorted
= by_alignment_name
;
4281 s
->wild_statement
.any_specs_sorted
= true;
4285 case lang_constructors_statement_enum
:
4286 update_wild_statements (constructor_list
.head
);
4289 case lang_output_section_statement_enum
:
4290 update_wild_statements
4291 (s
->output_section_statement
.children
.head
);
4294 case lang_group_statement_enum
:
4295 update_wild_statements (s
->group_statement
.children
.head
);
4303 /* Open input files and attach to output sections. */
4306 map_input_to_output_sections
4307 (lang_statement_union_type
*s
, const char *target
,
4308 lang_output_section_statement_type
*os
)
4310 for (; s
!= NULL
; s
= s
->header
.next
)
4312 lang_output_section_statement_type
*tos
;
4314 unsigned int type
= 0;
4316 switch (s
->header
.type
)
4318 case lang_wild_statement_enum
:
4319 wild (&s
->wild_statement
, target
, os
);
4321 case lang_constructors_statement_enum
:
4322 map_input_to_output_sections (constructor_list
.head
,
4326 case lang_output_section_statement_enum
:
4327 tos
= &s
->output_section_statement
;
4328 if (tos
->constraint
== ONLY_IF_RW
4329 || tos
->constraint
== ONLY_IF_RO
)
4331 tos
->all_input_readonly
= true;
4332 check_input_sections (tos
->children
.head
, tos
);
4333 if (tos
->all_input_readonly
!= (tos
->constraint
== ONLY_IF_RO
))
4334 tos
->constraint
= -1;
4336 if (tos
->constraint
>= 0)
4337 map_input_to_output_sections (tos
->children
.head
,
4341 case lang_output_statement_enum
:
4343 case lang_target_statement_enum
:
4344 target
= s
->target_statement
.target
;
4346 case lang_group_statement_enum
:
4347 map_input_to_output_sections (s
->group_statement
.children
.head
,
4351 case lang_data_statement_enum
:
4353 /* This should never happen. */
4355 /* Make sure that any sections mentioned in the expression
4357 exp_init_os (s
->data_statement
.exp
);
4358 /* The output section gets CONTENTS, ALLOC and LOAD, but
4359 these may be overridden by the script. */
4360 flags
= SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
;
4361 switch (os
->sectype
)
4363 case normal_section
:
4364 case overlay_section
:
4365 case first_overlay_section
:
4367 case noalloc_section
:
4368 flags
= SEC_HAS_CONTENTS
;
4370 case readonly_section
:
4371 flags
|= SEC_READONLY
;
4373 case typed_readonly_section
:
4374 flags
|= SEC_READONLY
;
4377 if (os
->sectype_value
->type
.node_class
== etree_name
4378 && os
->sectype_value
->type
.node_code
== NAME
)
4380 const char *name
= os
->sectype_value
->name
.name
;
4381 if (strcmp (name
, "SHT_PROGBITS") == 0)
4382 type
= SHT_PROGBITS
;
4383 else if (strcmp (name
, "SHT_STRTAB") == 0)
4385 else if (strcmp (name
, "SHT_NOTE") == 0)
4387 else if (strcmp (name
, "SHT_NOBITS") == 0)
4389 else if (strcmp (name
, "SHT_INIT_ARRAY") == 0)
4390 type
= SHT_INIT_ARRAY
;
4391 else if (strcmp (name
, "SHT_FINI_ARRAY") == 0)
4392 type
= SHT_FINI_ARRAY
;
4393 else if (strcmp (name
, "SHT_PREINIT_ARRAY") == 0)
4394 type
= SHT_PREINIT_ARRAY
;
4396 fatal (_ ("%P: invalid type for output section `%s'\n"),
4401 exp_fold_tree_no_dot (os
->sectype_value
, os
);
4402 if (expld
.result
.valid_p
)
4403 type
= expld
.result
.value
;
4405 fatal (_ ("%P: invalid type for output section `%s'\n"),
4409 case noload_section
:
4410 if (bfd_get_flavour (link_info
.output_bfd
)
4411 == bfd_target_elf_flavour
)
4412 flags
= SEC_NEVER_LOAD
| SEC_ALLOC
;
4414 flags
= SEC_NEVER_LOAD
| SEC_HAS_CONTENTS
;
4417 if (os
->bfd_section
== NULL
)
4418 init_os (os
, flags
| SEC_READONLY
);
4420 os
->bfd_section
->flags
|= flags
;
4421 os
->bfd_section
->type
= type
;
4423 case lang_input_section_enum
:
4425 case lang_fill_statement_enum
:
4426 case lang_object_symbols_statement_enum
:
4427 case lang_reloc_statement_enum
:
4428 case lang_padding_statement_enum
:
4429 case lang_input_statement_enum
:
4430 if (os
!= NULL
&& os
->bfd_section
== NULL
)
4434 case lang_assignment_statement_enum
:
4435 if (os
!= NULL
&& os
->bfd_section
== NULL
)
4436 init_os (os
, get_os_init_flag (os
));
4438 /* Make sure that any sections mentioned in the assignment
4440 exp_init_os (s
->assignment_statement
.exp
);
4443 case lang_address_statement_enum
:
4444 /* Mark the specified section with the supplied address.
4445 If this section was actually a segment marker, then the
4446 directive is ignored if the linker script explicitly
4447 processed the segment marker. Originally, the linker
4448 treated segment directives (like -Ttext on the
4449 command-line) as section directives. We honor the
4450 section directive semantics for backwards compatibility;
4451 linker scripts that do not specifically check for
4452 SEGMENT_START automatically get the old semantics. */
4453 if (!s
->address_statement
.segment
4454 || !s
->address_statement
.segment
->used
)
4456 const char *name
= s
->address_statement
.section_name
;
4458 /* Create the output section statement here so that
4459 orphans with a set address will be placed after other
4460 script sections. If we let the orphan placement code
4461 place them in amongst other sections then the address
4462 will affect following script sections, which is
4463 likely to surprise naive users. */
4464 tos
= lang_output_section_statement_lookup (name
, 0, 1);
4465 tos
->addr_tree
= s
->address_statement
.address
;
4466 if (tos
->bfd_section
== NULL
)
4470 case lang_insert_statement_enum
:
4472 case lang_input_matcher_enum
:
4478 /* An insert statement snips out all the linker statements from the
4479 start of the list and places them after the output section
4480 statement specified by the insert. This operation is complicated
4481 by the fact that we keep a doubly linked list of output section
4482 statements as well as the singly linked list of all statements.
4483 FIXME someday: Twiddling with the list not only moves statements
4484 from the user's script but also input and group statements that are
4485 built from command line object files and --start-group. We only
4486 get away with this because the list pointers used by file_chain
4487 and input_file_chain are not reordered, and processing via
4488 statement_list after this point mostly ignores input statements.
4489 One exception is the map file, where LOAD and START GROUP/END GROUP
4490 can end up looking odd. */
4493 process_insert_statements (lang_statement_union_type
**start
)
4495 lang_statement_union_type
**s
;
4496 lang_output_section_statement_type
*first_os
= NULL
;
4497 lang_output_section_statement_type
*last_os
= NULL
;
4498 lang_output_section_statement_type
*os
;
4503 if ((*s
)->header
.type
== lang_output_section_statement_enum
)
4505 /* Keep pointers to the first and last output section
4506 statement in the sequence we may be about to move. */
4507 os
= &(*s
)->output_section_statement
;
4509 ASSERT (last_os
== NULL
|| last_os
->next
== os
);
4512 /* Set constraint negative so that lang_output_section_find
4513 won't match this output section statement. At this
4514 stage in linking constraint has values in the range
4515 [-1, ONLY_IN_RW]. */
4516 last_os
->constraint
= -2 - last_os
->constraint
;
4517 if (first_os
== NULL
)
4520 else if ((*s
)->header
.type
== lang_group_statement_enum
)
4522 /* A user might put -T between --start-group and
4523 --end-group. One way this odd construct might arise is
4524 from a wrapper around ld to change library search
4525 behaviour. For example:
4527 exec real_ld --start-group "$@" --end-group
4528 This isn't completely unreasonable so go looking inside a
4529 group statement for insert statements. */
4530 process_insert_statements (&(*s
)->group_statement
.children
.head
);
4532 else if ((*s
)->header
.type
== lang_insert_statement_enum
)
4534 lang_insert_statement_type
*i
= &(*s
)->insert_statement
;
4535 lang_output_section_statement_type
*where
;
4536 lang_statement_union_type
**ptr
;
4537 lang_statement_union_type
*first
;
4539 if (link_info
.non_contiguous_regions
)
4541 einfo (_("warning: INSERT statement in linker script is "
4542 "incompatible with --enable-non-contiguous-regions.\n"));
4545 where
= lang_output_section_find (i
->where
);
4546 if (where
!= NULL
&& i
->is_before
)
4549 where
= where
->prev
;
4550 while (where
!= NULL
&& where
->constraint
< 0);
4554 fatal (_("%P: %s not found for insert\n"), i
->where
);
4558 /* Deal with reordering the output section statement list. */
4559 if (last_os
!= NULL
)
4561 asection
*first_sec
, *last_sec
;
4562 struct lang_output_section_statement_struct
**next
;
4564 /* Snip out the output sections we are moving. */
4565 first_os
->prev
->next
= last_os
->next
;
4566 if (last_os
->next
== NULL
)
4568 next
= &first_os
->prev
->next
;
4569 lang_os_list
.tail
= (lang_statement_union_type
**) next
;
4572 last_os
->next
->prev
= first_os
->prev
;
4573 /* Add them in at the new position. */
4574 last_os
->next
= where
->next
;
4575 if (where
->next
== NULL
)
4577 next
= &last_os
->next
;
4578 lang_os_list
.tail
= (lang_statement_union_type
**) next
;
4581 where
->next
->prev
= last_os
;
4582 first_os
->prev
= where
;
4583 where
->next
= first_os
;
4585 /* Move the bfd sections in the same way. */
4588 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
4590 os
->constraint
= -2 - os
->constraint
;
4591 if (os
->bfd_section
!= NULL
4592 && os
->bfd_section
->owner
!= NULL
)
4594 last_sec
= os
->bfd_section
;
4595 if (first_sec
== NULL
)
4596 first_sec
= last_sec
;
4601 if (last_sec
!= NULL
)
4603 asection
*sec
= where
->bfd_section
;
4605 sec
= output_prev_sec_find (where
);
4607 /* The place we want to insert must come after the
4608 sections we are moving. So if we find no
4609 section or if the section is the same as our
4610 last section, then no move is needed. */
4611 if (sec
!= NULL
&& sec
!= last_sec
)
4613 /* Trim them off. */
4614 if (first_sec
->prev
!= NULL
)
4615 first_sec
->prev
->next
= last_sec
->next
;
4617 link_info
.output_bfd
->sections
= last_sec
->next
;
4618 if (last_sec
->next
!= NULL
)
4619 last_sec
->next
->prev
= first_sec
->prev
;
4621 link_info
.output_bfd
->section_last
= first_sec
->prev
;
4623 if (sec
->owner
== NULL
)
4624 /* SEC is the absolute section, from the
4625 first dummy output section statement. Add
4626 back the sections we trimmed off to the
4627 start of the bfd sections. */
4630 last_sec
->next
= sec
->next
;
4632 last_sec
->next
= link_info
.output_bfd
->sections
;
4633 if (last_sec
->next
!= NULL
)
4634 last_sec
->next
->prev
= last_sec
;
4636 link_info
.output_bfd
->section_last
= last_sec
;
4637 first_sec
->prev
= sec
;
4638 if (first_sec
->prev
!= NULL
)
4639 first_sec
->prev
->next
= first_sec
;
4641 link_info
.output_bfd
->sections
= first_sec
;
4646 lang_statement_union_type
*after
= (void *) where
;
4647 if (where
== &lang_os_list
.head
->output_section_statement
4648 && where
->next
== first_os
)
4650 /* PR30155. Handle a corner case where the statement
4651 list is something like the following:
4653 . .data 0x0000000000000000 0x0
4654 . [0x0000000000000000] b = .
4656 . .data 0x0000000000000000 0x0 t.o
4657 . 0x0000000000000000 0x4 LONG 0x0
4658 . INSERT BEFORE .text.start
4659 . [0x0000000000000004] a = .
4660 . .text.start 0x0000000000000000 0x0
4661 . [0x0000000000000000] c = .
4662 . OUTPUT(a.out elf64-x86-64)
4663 Here we do not want to allow insert_os_after to
4664 choose a point inside the list we are moving.
4665 That would lose the list. Instead, let
4666 insert_os_after work from the INSERT, which in this
4667 particular example will result in inserting after
4668 the assignment "a = .". */
4671 ptr
= insert_os_after (after
);
4672 /* Snip everything from the start of the list, up to and
4673 including the insert statement we are currently processing. */
4675 *start
= (*s
)->header
.next
;
4676 /* Add them back where they belong, minus the insert. */
4679 statement_list
.tail
= s
;
4686 s
= &(*s
)->header
.next
;
4689 /* Undo constraint twiddling. */
4690 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
4692 os
->constraint
= -2 - os
->constraint
;
4698 /* An output section might have been removed after its statement was
4699 added. For example, ldemul_before_allocation can remove dynamic
4700 sections if they turn out to be not needed. Clean them up here. */
4703 strip_excluded_output_sections (void)
4705 lang_output_section_statement_type
*os
;
4707 /* Run lang_size_sections (if not already done). */
4708 if (expld
.phase
!= lang_mark_phase_enum
)
4710 expld
.phase
= lang_mark_phase_enum
;
4711 expld
.dataseg
.phase
= exp_seg_none
;
4712 one_lang_size_sections_pass (NULL
, false);
4713 lang_reset_memory_regions ();
4716 for (os
= (void *) lang_os_list
.head
;
4720 asection
*output_section
;
4723 if (os
->constraint
< 0)
4726 output_section
= os
->bfd_section
;
4727 if (output_section
== NULL
)
4730 exclude
= (output_section
->rawsize
== 0
4731 && (output_section
->flags
& SEC_KEEP
) == 0
4732 && !bfd_section_removed_from_list (link_info
.output_bfd
,
4735 /* Some sections have not yet been sized, notably .gnu.version,
4736 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
4737 input sections, so don't drop output sections that have such
4738 input sections unless they are also marked SEC_EXCLUDE. */
4739 if (exclude
&& output_section
->map_head
.s
!= NULL
)
4743 for (s
= output_section
->map_head
.s
; s
!= NULL
; s
= s
->map_head
.s
)
4744 if ((s
->flags
& SEC_EXCLUDE
) == 0
4745 && ((s
->flags
& SEC_LINKER_CREATED
) != 0
4746 || link_info
.emitrelocations
))
4755 /* We don't set bfd_section to NULL since bfd_section of the
4756 removed output section statement may still be used. */
4757 if (!os
->update_dot
)
4759 output_section
->flags
|= SEC_EXCLUDE
;
4760 bfd_section_list_remove (link_info
.output_bfd
, output_section
);
4761 link_info
.output_bfd
->section_count
--;
4766 /* Called from ldwrite to clear out asection.map_head and
4767 asection.map_tail for use as link_orders in ldwrite. */
4770 lang_clear_os_map (void)
4772 lang_output_section_statement_type
*os
;
4774 if (map_head_is_link_order
)
4777 for (os
= (void *) lang_os_list
.head
;
4781 asection
*output_section
;
4783 if (os
->constraint
< 0)
4786 output_section
= os
->bfd_section
;
4787 if (output_section
== NULL
)
4790 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
4791 output_section
->map_head
.link_order
= NULL
;
4792 output_section
->map_tail
.link_order
= NULL
;
4795 /* Stop future calls to lang_add_section from messing with map_head
4796 and map_tail link_order fields. */
4797 map_head_is_link_order
= true;
4801 print_output_section_statement
4802 (lang_output_section_statement_type
*output_section_statement
)
4804 asection
*section
= output_section_statement
->bfd_section
;
4807 if (output_section_statement
!= abs_output_section
)
4809 minfo ("\n%s", output_section_statement
->name
);
4811 if (section
!= NULL
)
4813 print_dot
= section
->vma
;
4815 len
= strlen (output_section_statement
->name
);
4816 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
4821 print_spaces (SECTION_NAME_MAP_LENGTH
- len
);
4823 minfo ("0x%V %W", section
->vma
, TO_ADDR (section
->size
));
4825 if (section
->vma
!= section
->lma
)
4826 minfo (_(" load address 0x%V"), section
->lma
);
4828 if (output_section_statement
->update_dot_tree
!= NULL
)
4829 exp_fold_tree (output_section_statement
->update_dot_tree
,
4830 output_section_statement
,
4831 bfd_abs_section_ptr
, &print_dot
);
4837 print_statement_list (output_section_statement
->children
.head
,
4838 output_section_statement
);
4842 print_assignment (lang_assignment_statement_type
*assignment
,
4843 lang_output_section_statement_type
*output_section
)
4849 print_spaces (SECTION_NAME_MAP_LENGTH
);
4851 if (assignment
->exp
->type
.node_class
== etree_assert
)
4854 tree
= assignment
->exp
->assert_s
.child
;
4858 const char *dst
= assignment
->exp
->assign
.dst
;
4860 is_dot
= (dst
[0] == '.' && dst
[1] == 0);
4861 tree
= assignment
->exp
;
4864 osec
= output_section
->bfd_section
;
4866 osec
= bfd_abs_section_ptr
;
4868 if (assignment
->exp
->type
.node_class
!= etree_provide
)
4869 exp_fold_tree (tree
, output_section
, osec
, &print_dot
);
4871 expld
.result
.valid_p
= false;
4874 const char *str
= buf
;
4875 if (expld
.result
.valid_p
)
4879 if (assignment
->exp
->type
.node_class
== etree_assert
4881 || expld
.assign_name
!= NULL
)
4883 value
= expld
.result
.value
;
4885 if (expld
.result
.section
!= NULL
)
4886 value
+= expld
.result
.section
->vma
;
4890 bfd_sprintf_vma (link_info
.output_bfd
, buf
+ 2, value
);
4896 struct bfd_link_hash_entry
*h
;
4898 h
= bfd_link_hash_lookup (link_info
.hash
, assignment
->exp
->assign
.dst
,
4899 false, false, true);
4902 value
= h
->u
.def
.value
;
4903 value
+= h
->u
.def
.section
->output_section
->vma
;
4904 value
+= h
->u
.def
.section
->output_offset
;
4909 bfd_sprintf_vma (link_info
.output_bfd
, buf
+ 3, value
);
4913 str
= "[unresolved]";
4918 if (assignment
->exp
->type
.node_class
== etree_provide
)
4923 expld
.assign_name
= NULL
;
4925 fprintf (config
.map_file
, "%-34s", str
);
4926 exp_print_tree (assignment
->exp
);
4931 print_input_statement (lang_input_statement_type
*statm
)
4933 if (statm
->filename
!= NULL
)
4934 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
4937 /* Print all symbols defined in a particular section. This is called
4938 via bfd_link_hash_traverse, or by print_all_symbols. */
4941 print_one_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
)
4943 asection
*sec
= (asection
*) ptr
;
4945 if (is_defined (hash_entry
)
4946 && sec
== hash_entry
->u
.def
.section
)
4948 print_spaces (SECTION_NAME_MAP_LENGTH
);
4950 (hash_entry
->u
.def
.value
4951 + hash_entry
->u
.def
.section
->output_offset
4952 + hash_entry
->u
.def
.section
->output_section
->vma
));
4954 minfo (" %pT\n", hash_entry
->root
.string
);
4961 hash_entry_addr_cmp (const void *a
, const void *b
)
4963 const struct bfd_link_hash_entry
*l
= *(const struct bfd_link_hash_entry
**)a
;
4964 const struct bfd_link_hash_entry
*r
= *(const struct bfd_link_hash_entry
**)b
;
4966 if (l
->u
.def
.value
< r
->u
.def
.value
)
4968 else if (l
->u
.def
.value
> r
->u
.def
.value
)
4975 print_all_symbols (asection
*sec
)
4977 input_section_userdata_type
*ud
= bfd_section_userdata (sec
);
4978 struct map_symbol_def
*def
;
4979 struct bfd_link_hash_entry
**entries
;
4985 *ud
->map_symbol_def_tail
= 0;
4987 /* Sort the symbols by address. */
4988 entries
= (struct bfd_link_hash_entry
**)
4989 obstack_alloc (&map_obstack
,
4990 ud
->map_symbol_def_count
* sizeof (*entries
));
4992 for (i
= 0, def
= ud
->map_symbol_def_head
; def
; def
= def
->next
, i
++)
4993 entries
[i
] = def
->entry
;
4995 qsort (entries
, ud
->map_symbol_def_count
, sizeof (*entries
),
4996 hash_entry_addr_cmp
);
4998 /* Print the symbols. */
4999 for (i
= 0; i
< ud
->map_symbol_def_count
; i
++)
5000 ldemul_print_symbol (entries
[i
], sec
);
5002 obstack_free (&map_obstack
, entries
);
5005 /* Returns TRUE if SYM is a symbol suitable for printing
5006 in a linker map as a local symbol. */
5009 ld_is_local_symbol (asymbol
* sym
)
5011 const char * name
= bfd_asymbol_name (sym
);
5013 if (name
== NULL
|| *name
== 0)
5016 /* Skip .Lxxx and such like. */
5017 if (bfd_is_local_label (link_info
.output_bfd
, sym
))
5020 /* FIXME: This is intended to skip ARM mapping symbols,
5021 which for some reason are not excluded by bfd_is_local_label,
5022 but maybe it is wrong for other architectures.
5023 It would be better to fix bfd_is_local_label. */
5027 /* Some local symbols, eg _GLOBAL_OFFSET_TABLE_, are present
5028 in the hash table, so do not print duplicates here. */
5029 struct bfd_link_hash_entry
* h
;
5030 h
= bfd_link_hash_lookup (link_info
.hash
, name
, false /* create */,
5031 false /* copy */, true /* follow */);
5035 /* Symbols from the plugin owned BFD will not get their own
5036 iteration of this function, but can be on the link_info
5037 list. So include them here. */
5038 if (h
->u
.def
.section
->owner
!= NULL
5039 && ((bfd_get_file_flags (h
->u
.def
.section
->owner
) & (BFD_LINKER_CREATED
| BFD_PLUGIN
))
5040 == (BFD_LINKER_CREATED
| BFD_PLUGIN
)))
5046 /* Print information about an input section to the map file. */
5049 print_input_section (asection
*i
, bool is_discarded
)
5051 bfd_size_type size
= i
->size
;
5057 minfo (" %s", i
->name
);
5059 len
= 1 + strlen (i
->name
);
5060 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
5065 print_spaces (SECTION_NAME_MAP_LENGTH
- len
);
5067 if ((i
->flags
& SEC_EXCLUDE
) == 0
5068 && i
->output_section
!= NULL
5069 && i
->output_section
->owner
== link_info
.output_bfd
)
5070 addr
= i
->output_section
->vma
+ i
->output_offset
;
5079 bfd_sprintf_vma (link_info
.output_bfd
, buf
, addr
);
5080 minfo ("0x%s %W %pB\n", buf
, TO_ADDR (size
), i
->owner
);
5082 if (size
!= i
->rawsize
&& i
->rawsize
!= 0)
5084 len
= SECTION_NAME_MAP_LENGTH
+ 3 + strlen (buf
);
5086 minfo (_("%W (size before relaxing)\n"), TO_ADDR (i
->rawsize
));
5089 if (i
->output_section
!= NULL
5090 && i
->output_section
->owner
== link_info
.output_bfd
)
5092 if (link_info
.reduce_memory_overheads
)
5093 bfd_link_hash_traverse (link_info
.hash
, ldemul_print_symbol
, i
);
5095 print_all_symbols (i
);
5097 /* Update print_dot, but make sure that we do not move it
5098 backwards - this could happen if we have overlays and a
5099 later overlay is shorter than an earier one. */
5100 if (addr
+ TO_ADDR (size
) > print_dot
)
5101 print_dot
= addr
+ TO_ADDR (size
);
5103 if (config
.print_map_locals
)
5105 long storage_needed
;
5107 /* FIXME: It would be better to cache this table, rather
5108 than recreating it for each output section. */
5109 /* FIXME: This call is not working for non-ELF based targets.
5111 storage_needed
= bfd_get_symtab_upper_bound (link_info
.output_bfd
);
5112 if (storage_needed
> 0)
5114 asymbol
** symbol_table
;
5115 long number_of_symbols
;
5118 symbol_table
= xmalloc (storage_needed
);
5119 number_of_symbols
= bfd_canonicalize_symtab (link_info
.output_bfd
, symbol_table
);
5121 for (j
= 0; j
< number_of_symbols
; j
++)
5123 asymbol
* sym
= symbol_table
[j
];
5124 bfd_vma sym_addr
= sym
->value
+ i
->output_section
->vma
;
5126 if (sym
->section
== i
->output_section
5127 && (sym
->flags
& BSF_LOCAL
) != 0
5129 && sym_addr
< print_dot
5130 && ld_is_local_symbol (sym
))
5132 print_spaces (SECTION_NAME_MAP_LENGTH
);
5133 minfo ("0x%V (local) %s\n", sym_addr
, bfd_asymbol_name (sym
));
5137 free (symbol_table
);
5144 print_fill_statement (lang_fill_statement_type
*fill
)
5148 fputs (" FILL mask 0x", config
.map_file
);
5149 for (p
= fill
->fill
->data
, size
= fill
->fill
->size
; size
!= 0; p
++, size
--)
5150 fprintf (config
.map_file
, "%02x", *p
);
5151 fputs ("\n", config
.map_file
);
5155 print_data_statement (lang_data_statement_type
*data
)
5161 init_opb (data
->output_section
);
5162 print_spaces (SECTION_NAME_MAP_LENGTH
);
5164 addr
= data
->output_offset
;
5165 if (data
->output_section
!= NULL
)
5166 addr
+= data
->output_section
->vma
;
5194 if (size
< TO_SIZE ((unsigned) 1))
5195 size
= TO_SIZE ((unsigned) 1);
5196 minfo ("0x%V %W %s 0x%v", addr
, TO_ADDR (size
), name
, data
->value
);
5198 if (data
->exp
->type
.node_class
!= etree_value
)
5201 exp_print_tree (data
->exp
);
5206 print_dot
= addr
+ TO_ADDR (size
);
5209 /* Print an address statement. These are generated by options like
5213 print_address_statement (lang_address_statement_type
*address
)
5215 minfo (_("Address of section %s set to "), address
->section_name
);
5216 exp_print_tree (address
->address
);
5220 /* Print a reloc statement. */
5223 print_reloc_statement (lang_reloc_statement_type
*reloc
)
5228 init_opb (reloc
->output_section
);
5229 print_spaces (SECTION_NAME_MAP_LENGTH
);
5231 addr
= reloc
->output_offset
;
5232 if (reloc
->output_section
!= NULL
)
5233 addr
+= reloc
->output_section
->vma
;
5235 size
= bfd_get_reloc_size (reloc
->howto
);
5237 minfo ("0x%V %W RELOC %s ", addr
, TO_ADDR (size
), reloc
->howto
->name
);
5239 if (reloc
->name
!= NULL
)
5240 minfo ("%s+", reloc
->name
);
5242 minfo ("%s+", reloc
->section
->name
);
5244 exp_print_tree (reloc
->addend_exp
);
5248 print_dot
= addr
+ TO_ADDR (size
);
5252 print_padding_statement (lang_padding_statement_type
*s
)
5257 init_opb (s
->output_section
);
5260 len
= sizeof " *fill*" - 1;
5261 print_spaces (SECTION_NAME_MAP_LENGTH
- len
);
5263 addr
= s
->output_offset
;
5264 if (s
->output_section
!= NULL
)
5265 addr
+= s
->output_section
->vma
;
5266 minfo ("0x%V %W ", addr
, TO_ADDR (s
->size
));
5268 if (s
->fill
->size
!= 0)
5272 for (p
= s
->fill
->data
, size
= s
->fill
->size
; size
!= 0; p
++, size
--)
5273 fprintf (config
.map_file
, "%02x", *p
);
5278 print_dot
= addr
+ TO_ADDR (s
->size
);
5282 print_wild_statement (lang_wild_statement_type
*w
,
5283 lang_output_section_statement_type
*os
)
5285 struct wildcard_list
*sec
;
5289 if (w
->exclude_name_list
)
5292 minfo ("EXCLUDE_FILE(%s", w
->exclude_name_list
->name
);
5293 for (tmp
= w
->exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
5294 minfo (" %s", tmp
->name
);
5298 if (w
->filenames_sorted
)
5299 minfo ("SORT_BY_NAME(");
5300 if (w
->filenames_reversed
)
5302 if (w
->filename
!= NULL
)
5303 minfo ("%s", w
->filename
);
5306 if (w
->filenames_reversed
)
5308 if (w
->filenames_sorted
)
5312 for (sec
= w
->section_list
; sec
; sec
= sec
->next
)
5314 int closing_paren
= 0;
5316 switch (sec
->spec
.sorted
)
5322 minfo ("SORT_BY_NAME(");
5327 minfo ("SORT_BY_ALIGNMENT(");
5331 case by_name_alignment
:
5332 minfo ("SORT_BY_NAME(SORT_BY_ALIGNMENT(");
5336 case by_alignment_name
:
5337 minfo ("SORT_BY_ALIGNMENT(SORT_BY_NAME(");
5342 minfo ("SORT_NONE(");
5346 case by_init_priority
:
5347 minfo ("SORT_BY_INIT_PRIORITY(");
5352 if (sec
->spec
.reversed
)
5358 if (sec
->spec
.exclude_name_list
!= NULL
)
5361 minfo ("EXCLUDE_FILE(%s", sec
->spec
.exclude_name_list
->name
);
5362 for (tmp
= sec
->spec
.exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
5363 minfo (" %s", tmp
->name
);
5366 if (sec
->spec
.name
!= NULL
)
5367 minfo ("%s", sec
->spec
.name
);
5370 for (;closing_paren
> 0; closing_paren
--)
5379 print_statement_list (w
->children
.head
, os
);
5382 /* Print a group statement. */
5385 print_group (lang_group_statement_type
*s
,
5386 lang_output_section_statement_type
*os
)
5388 fprintf (config
.map_file
, "START GROUP\n");
5389 print_statement_list (s
->children
.head
, os
);
5390 fprintf (config
.map_file
, "END GROUP\n");
5393 /* Print the list of statements in S.
5394 This can be called for any statement type. */
5397 print_statement_list (lang_statement_union_type
*s
,
5398 lang_output_section_statement_type
*os
)
5402 print_statement (s
, os
);
5407 /* Print the first statement in statement list S.
5408 This can be called for any statement type. */
5411 print_statement (lang_statement_union_type
*s
,
5412 lang_output_section_statement_type
*os
)
5414 switch (s
->header
.type
)
5417 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
5420 case lang_constructors_statement_enum
:
5421 if (constructor_list
.head
!= NULL
)
5423 if (constructors_sorted
)
5424 minfo (" SORT (CONSTRUCTORS)\n");
5426 minfo (" CONSTRUCTORS\n");
5427 print_statement_list (constructor_list
.head
, os
);
5430 case lang_wild_statement_enum
:
5431 print_wild_statement (&s
->wild_statement
, os
);
5433 case lang_address_statement_enum
:
5434 print_address_statement (&s
->address_statement
);
5436 case lang_object_symbols_statement_enum
:
5437 minfo (" CREATE_OBJECT_SYMBOLS\n");
5439 case lang_fill_statement_enum
:
5440 print_fill_statement (&s
->fill_statement
);
5442 case lang_data_statement_enum
:
5443 print_data_statement (&s
->data_statement
);
5445 case lang_reloc_statement_enum
:
5446 print_reloc_statement (&s
->reloc_statement
);
5448 case lang_input_section_enum
:
5449 print_input_section (s
->input_section
.section
, false);
5451 case lang_padding_statement_enum
:
5452 print_padding_statement (&s
->padding_statement
);
5454 case lang_output_section_statement_enum
:
5455 print_output_section_statement (&s
->output_section_statement
);
5457 case lang_assignment_statement_enum
:
5458 print_assignment (&s
->assignment_statement
, os
);
5460 case lang_target_statement_enum
:
5461 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
5463 case lang_output_statement_enum
:
5464 minfo ("OUTPUT(%s", s
->output_statement
.name
);
5465 if (output_target
!= NULL
)
5466 minfo (" %s", output_target
);
5469 case lang_input_statement_enum
:
5470 print_input_statement (&s
->input_statement
);
5472 case lang_group_statement_enum
:
5473 print_group (&s
->group_statement
, os
);
5475 case lang_insert_statement_enum
:
5476 minfo ("INSERT %s %s\n",
5477 s
->insert_statement
.is_before
? "BEFORE" : "AFTER",
5478 s
->insert_statement
.where
);
5484 print_statements (void)
5486 print_statement_list (statement_list
.head
, abs_output_section
);
5489 /* Print the first N statements in statement list S to STDERR.
5490 If N == 0, nothing is printed.
5491 If N < 0, the entire list is printed.
5492 Intended to be called from GDB. */
5495 dprint_statement (lang_statement_union_type
*s
, int n
)
5497 FILE *map_save
= config
.map_file
;
5499 config
.map_file
= stderr
;
5502 print_statement_list (s
, abs_output_section
);
5505 while (s
&& --n
>= 0)
5507 print_statement (s
, abs_output_section
);
5512 config
.map_file
= map_save
;
5516 insert_pad (lang_statement_union_type
**ptr
,
5518 bfd_size_type alignment_needed
,
5519 asection
*output_section
,
5522 static fill_type zero_fill
;
5523 lang_statement_union_type
*pad
= NULL
;
5525 if (ptr
!= &statement_list
.head
)
5526 pad
= ((lang_statement_union_type
*)
5527 ((char *) ptr
- offsetof (lang_statement_union_type
, header
.next
)));
5529 && pad
->header
.type
== lang_padding_statement_enum
5530 && pad
->padding_statement
.output_section
== output_section
)
5532 /* Use the existing pad statement. */
5534 else if ((pad
= *ptr
) != NULL
5535 && pad
->header
.type
== lang_padding_statement_enum
5536 && pad
->padding_statement
.output_section
== output_section
)
5538 /* Use the existing pad statement. */
5542 /* Make a new padding statement, linked into existing chain. */
5543 pad
= stat_alloc (sizeof (lang_padding_statement_type
));
5544 pad
->header
.next
= *ptr
;
5546 pad
->header
.type
= lang_padding_statement_enum
;
5547 pad
->padding_statement
.output_section
= output_section
;
5550 pad
->padding_statement
.fill
= fill
;
5552 pad
->padding_statement
.output_offset
= dot
- output_section
->vma
;
5553 pad
->padding_statement
.size
= alignment_needed
;
5554 if (!(output_section
->flags
& SEC_FIXED_SIZE
))
5555 output_section
->size
= TO_SIZE (dot
+ TO_ADDR (alignment_needed
)
5556 - output_section
->vma
);
5559 /* Work out how much this section will move the dot point. */
5563 (lang_statement_union_type
**this_ptr
,
5564 lang_output_section_statement_type
*output_section_statement
,
5569 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
5570 asection
*i
= is
->section
;
5571 asection
*o
= output_section_statement
->bfd_section
;
5574 if (link_info
.non_contiguous_regions
)
5576 /* If the input section I has already been successfully assigned
5577 to an output section other than O, don't bother with it and
5578 let the caller remove it from the list. Keep processing in
5579 case we have already handled O, because the repeated passes
5580 have reinitialized its size. */
5581 if (i
->already_assigned
&& i
->already_assigned
!= o
)
5588 if (i
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
5589 i
->output_offset
= i
->vma
- o
->vma
;
5590 else if (((i
->flags
& SEC_EXCLUDE
) != 0)
5591 || output_section_statement
->ignored
)
5592 i
->output_offset
= dot
- o
->vma
;
5595 bfd_size_type alignment_needed
;
5597 /* Align this section first to the input sections requirement,
5598 then to the output section's requirement. If this alignment
5599 is greater than any seen before, then record it too. Perform
5600 the alignment by inserting a magic 'padding' statement.
5601 We can force input section alignment within an output section
5602 by using SUBALIGN. The value specified overrides any alignment
5603 given by input sections, whether larger or smaller. */
5605 if (output_section_statement
->subsection_alignment
!= NULL
)
5606 o
->alignment_power
= i
->alignment_power
=
5607 exp_get_power (output_section_statement
->subsection_alignment
,
5608 output_section_statement
,
5609 "subsection alignment");
5611 if (o
->alignment_power
< i
->alignment_power
)
5612 o
->alignment_power
= i
->alignment_power
;
5614 alignment_needed
= align_power (dot
, i
->alignment_power
) - dot
;
5616 if (alignment_needed
!= 0)
5618 insert_pad (this_ptr
, fill
, TO_SIZE (alignment_needed
), o
, dot
);
5619 dot
+= alignment_needed
;
5622 if (link_info
.non_contiguous_regions
)
5624 /* If I would overflow O, let the caller remove I from the
5626 if (output_section_statement
->region
)
5628 bfd_vma end
= output_section_statement
->region
->origin
5629 + output_section_statement
->region
->length
;
5631 if (dot
+ TO_ADDR (i
->size
) > end
)
5633 if (i
->flags
& SEC_LINKER_CREATED
)
5634 fatal (_("%P: Output section `%pA' not large enough for "
5635 "the linker-created stubs section `%pA'.\n"),
5636 i
->output_section
, i
);
5638 if (i
->rawsize
&& i
->rawsize
!= i
->size
)
5639 fatal (_("%P: Relaxation not supported with "
5640 "--enable-non-contiguous-regions (section `%pA' "
5641 "would overflow `%pA' after it changed size).\n"),
5642 i
, i
->output_section
);
5646 i
->output_section
= NULL
;
5652 /* Remember where in the output section this input section goes. */
5653 i
->output_offset
= dot
- o
->vma
;
5655 /* Mark how big the output section must be to contain this now. */
5656 dot
+= TO_ADDR (i
->size
);
5657 if (!(o
->flags
& SEC_FIXED_SIZE
))
5658 o
->size
= TO_SIZE (dot
- o
->vma
);
5660 if (link_info
.non_contiguous_regions
)
5662 /* Record that I was successfully assigned to O, and update
5663 its actual output section too. */
5664 i
->already_assigned
= o
;
5665 i
->output_section
= o
;
5679 sort_sections_by_lma (const void *arg1
, const void *arg2
)
5681 const asection
*sec1
= ((const struct check_sec
*) arg1
)->sec
;
5682 const asection
*sec2
= ((const struct check_sec
*) arg2
)->sec
;
5684 if (sec1
->lma
< sec2
->lma
)
5686 else if (sec1
->lma
> sec2
->lma
)
5688 else if (sec1
->id
< sec2
->id
)
5690 else if (sec1
->id
> sec2
->id
)
5697 sort_sections_by_vma (const void *arg1
, const void *arg2
)
5699 const asection
*sec1
= ((const struct check_sec
*) arg1
)->sec
;
5700 const asection
*sec2
= ((const struct check_sec
*) arg2
)->sec
;
5702 if (sec1
->vma
< sec2
->vma
)
5704 else if (sec1
->vma
> sec2
->vma
)
5706 else if (sec1
->id
< sec2
->id
)
5708 else if (sec1
->id
> sec2
->id
)
5714 #define IS_TBSS(s) \
5715 ((s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == SEC_THREAD_LOCAL)
5717 #define IGNORE_SECTION(s) \
5718 ((s->flags & SEC_ALLOC) == 0 || IS_TBSS (s))
5720 /* Check to see if any allocated sections overlap with other allocated
5721 sections. This can happen if a linker script specifies the output
5722 section addresses of the two sections. Also check whether any memory
5723 region has overflowed. */
5726 lang_check_section_addresses (void)
5729 struct check_sec
*sections
;
5734 bfd_vma p_start
= 0;
5736 lang_memory_region_type
*m
;
5739 /* Detect address space overflow on allocated sections. */
5740 addr_mask
= ((bfd_vma
) 1 <<
5741 (bfd_arch_bits_per_address (link_info
.output_bfd
) - 1)) - 1;
5742 addr_mask
= (addr_mask
<< 1) + 1;
5743 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5744 if ((s
->flags
& SEC_ALLOC
) != 0)
5746 s_end
= (s
->vma
+ s
->size
) & addr_mask
;
5747 if (s_end
!= 0 && s_end
< (s
->vma
& addr_mask
))
5748 einfo (_("%X%P: section %s VMA wraps around address space\n"),
5752 s_end
= (s
->lma
+ s
->size
) & addr_mask
;
5753 if (s_end
!= 0 && s_end
< (s
->lma
& addr_mask
))
5754 einfo (_("%X%P: section %s LMA wraps around address space\n"),
5759 if (bfd_count_sections (link_info
.output_bfd
) <= 1)
5762 count
= bfd_count_sections (link_info
.output_bfd
);
5763 sections
= XNEWVEC (struct check_sec
, count
);
5765 /* Scan all sections in the output list. */
5767 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5769 if (IGNORE_SECTION (s
)
5773 sections
[count
].sec
= s
;
5774 sections
[count
].warned
= false;
5784 qsort (sections
, count
, sizeof (*sections
), sort_sections_by_lma
);
5786 /* First check section LMAs. There should be no overlap of LMAs on
5787 loadable sections, even with overlays. */
5788 for (p
= NULL
, i
= 0; i
< count
; i
++)
5790 s
= sections
[i
].sec
;
5792 if ((s
->flags
& SEC_LOAD
) != 0)
5795 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
5797 /* Look for an overlap. We have sorted sections by lma, so
5798 we know that s_start >= p_start. Besides the obvious
5799 case of overlap when the current section starts before
5800 the previous one ends, we also must have overlap if the
5801 previous section wraps around the address space. */
5803 && (s_start
<= p_end
5804 || p_end
< p_start
))
5806 einfo (_("%X%P: section %s LMA [%V,%V]"
5807 " overlaps section %s LMA [%V,%V]\n"),
5808 s
->name
, s_start
, s_end
, p
->name
, p_start
, p_end
);
5809 sections
[i
].warned
= true;
5817 /* If any non-zero size allocated section (excluding tbss) starts at
5818 exactly the same VMA as another such section, then we have
5819 overlays. Overlays generated by the OVERLAY keyword will have
5820 this property. It is possible to intentionally generate overlays
5821 that fail this test, but it would be unusual. */
5822 qsort (sections
, count
, sizeof (*sections
), sort_sections_by_vma
);
5824 p_start
= sections
[0].sec
->vma
;
5825 for (i
= 1; i
< count
; i
++)
5827 s_start
= sections
[i
].sec
->vma
;
5828 if (p_start
== s_start
)
5836 /* Now check section VMAs if no overlays were detected. */
5839 for (p
= NULL
, i
= 0; i
< count
; i
++)
5841 s
= sections
[i
].sec
;
5844 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
5847 && !sections
[i
].warned
5848 && (s_start
<= p_end
5849 || p_end
< p_start
))
5850 einfo (_("%X%P: section %s VMA [%V,%V]"
5851 " overlaps section %s VMA [%V,%V]\n"),
5852 s
->name
, s_start
, s_end
, p
->name
, p_start
, p_end
);
5861 /* If any memory region has overflowed, report by how much.
5862 We do not issue this diagnostic for regions that had sections
5863 explicitly placed outside their bounds; os_region_check's
5864 diagnostics are adequate for that case.
5866 FIXME: It is conceivable that m->current - (m->origin + m->length)
5867 might overflow a 32-bit integer. There is, alas, no way to print
5868 a bfd_vma quantity in decimal. */
5869 for (m
= lang_memory_region_list
; m
; m
= m
->next
)
5870 if (m
->had_full_message
)
5872 unsigned long over
= m
->current
- (m
->origin
+ m
->length
);
5873 einfo (ngettext ("%X%P: region `%s' overflowed by %lu byte\n",
5874 "%X%P: region `%s' overflowed by %lu bytes\n",
5876 m
->name_list
.name
, over
);
5880 /* Make sure the new address is within the region. We explicitly permit the
5881 current address to be at the exact end of the region when the address is
5882 non-zero, in case the region is at the end of addressable memory and the
5883 calculation wraps around. */
5886 os_region_check (lang_output_section_statement_type
*os
,
5887 lang_memory_region_type
*region
,
5891 if ((region
->current
< region
->origin
5892 || (region
->current
- region
->origin
> region
->length
))
5893 && ((region
->current
!= region
->origin
+ region
->length
)
5898 einfo (_("%X%P: address 0x%v of %pB section `%s'"
5899 " is not within region `%s'\n"),
5901 os
->bfd_section
->owner
,
5902 os
->bfd_section
->name
,
5903 region
->name_list
.name
);
5905 else if (!region
->had_full_message
)
5907 region
->had_full_message
= true;
5909 einfo (_("%X%P: %pB section `%s' will not fit in region `%s'\n"),
5910 os
->bfd_section
->owner
,
5911 os
->bfd_section
->name
,
5912 region
->name_list
.name
);
5918 ldlang_check_relro_region (lang_statement_union_type
*s
)
5920 seg_align_type
*seg
= &expld
.dataseg
;
5922 if (seg
->relro
== exp_seg_relro_start
)
5924 if (!seg
->relro_start_stat
)
5925 seg
->relro_start_stat
= s
;
5928 ASSERT (seg
->relro_start_stat
== s
);
5931 else if (seg
->relro
== exp_seg_relro_end
)
5933 if (!seg
->relro_end_stat
)
5934 seg
->relro_end_stat
= s
;
5937 ASSERT (seg
->relro_end_stat
== s
);
5942 /* Set the sizes for all the output sections. */
5945 lang_size_sections_1
5946 (lang_statement_union_type
**prev
,
5947 lang_output_section_statement_type
*current_os
,
5953 lang_statement_union_type
*s
;
5954 lang_statement_union_type
*prev_s
= NULL
;
5955 bool removed_prev_s
= false;
5956 lang_output_section_statement_type
*os
= current_os
;
5958 /* Size up the sections from their constituent parts. */
5959 for (s
= *prev
; s
!= NULL
; prev_s
= s
, s
= s
->header
.next
)
5961 bool removed
= false;
5963 switch (s
->header
.type
)
5965 case lang_output_section_statement_enum
:
5967 bfd_vma newdot
, after
, dotdelta
;
5968 lang_memory_region_type
*r
;
5969 int section_alignment
= 0;
5971 os
= &s
->output_section_statement
;
5972 init_opb (os
->bfd_section
);
5973 if (os
->constraint
== -1)
5976 /* FIXME: We shouldn't need to zero section vmas for ld -r
5977 here, in lang_insert_orphan, or in the default linker scripts.
5978 This is covering for coff backend linker bugs. See PR6945. */
5979 if (os
->addr_tree
== NULL
5980 && bfd_link_relocatable (&link_info
)
5981 && (bfd_get_flavour (link_info
.output_bfd
)
5982 == bfd_target_coff_flavour
))
5983 os
->addr_tree
= exp_intop (0);
5984 if (os
->addr_tree
!= NULL
)
5986 exp_fold_tree (os
->addr_tree
, os
, bfd_abs_section_ptr
, &dot
);
5988 if (expld
.result
.valid_p
)
5990 dot
= expld
.result
.value
;
5991 if (expld
.result
.section
!= NULL
)
5992 dot
+= expld
.result
.section
->vma
;
5994 else if (expld
.phase
!= lang_mark_phase_enum
)
5995 fatal (_("%P:%pS: non constant or forward reference"
5996 " address expression for section %s\n"),
5997 os
->addr_tree
, os
->name
);
6000 if (os
->bfd_section
== NULL
)
6001 /* This section was removed or never actually created. */
6004 /* If this is a COFF shared library section, use the size and
6005 address from the input section. FIXME: This is COFF
6006 specific; it would be cleaner if there were some other way
6007 to do this, but nothing simple comes to mind. */
6008 if (((bfd_get_flavour (link_info
.output_bfd
)
6009 == bfd_target_ecoff_flavour
)
6010 || (bfd_get_flavour (link_info
.output_bfd
)
6011 == bfd_target_coff_flavour
))
6012 && (os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
6016 if (os
->children
.head
== NULL
6017 || os
->children
.head
->header
.next
!= NULL
6018 || (os
->children
.head
->header
.type
6019 != lang_input_section_enum
))
6020 einfo (_("%X%P: internal error on COFF shared library"
6021 " section %s\n"), os
->name
);
6023 input
= os
->children
.head
->input_section
.section
;
6024 bfd_set_section_vma (os
->bfd_section
,
6025 bfd_section_vma (input
));
6026 if (!(os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
6027 os
->bfd_section
->size
= input
->size
;
6033 if (bfd_is_abs_section (os
->bfd_section
))
6035 /* No matter what happens, an abs section starts at zero. */
6036 ASSERT (os
->bfd_section
->vma
== 0);
6040 if (os
->addr_tree
== NULL
)
6042 /* No address specified for this section, get one
6043 from the region specification. */
6044 if (os
->region
== NULL
6045 || ((os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))
6046 && os
->region
->name_list
.name
[0] == '*'
6047 && strcmp (os
->region
->name_list
.name
,
6048 DEFAULT_MEMORY_REGION
) == 0))
6050 os
->region
= lang_memory_default (os
->bfd_section
);
6053 /* If a loadable section is using the default memory
6054 region, and some non default memory regions were
6055 defined, issue an error message. */
6057 && !IGNORE_SECTION (os
->bfd_section
)
6058 && !bfd_link_relocatable (&link_info
)
6060 && strcmp (os
->region
->name_list
.name
,
6061 DEFAULT_MEMORY_REGION
) == 0
6062 && lang_memory_region_list
!= NULL
6063 && (strcmp (lang_memory_region_list
->name_list
.name
,
6064 DEFAULT_MEMORY_REGION
) != 0
6065 || lang_memory_region_list
->next
!= NULL
)
6066 && lang_sizing_iteration
== 1)
6068 /* By default this is an error rather than just a
6069 warning because if we allocate the section to the
6070 default memory region we can end up creating an
6071 excessively large binary, or even seg faulting when
6072 attempting to perform a negative seek. See
6073 sources.redhat.com/ml/binutils/2003-04/msg00423.html
6074 for an example of this. This behaviour can be
6075 overridden by the using the --no-check-sections
6077 if (command_line
.check_section_addresses
)
6078 fatal (_("%P: error: no memory region specified"
6079 " for loadable section `%s'\n"),
6080 bfd_section_name (os
->bfd_section
));
6082 einfo (_("%P: warning: no memory region specified"
6083 " for loadable section `%s'\n"),
6084 bfd_section_name (os
->bfd_section
));
6087 newdot
= os
->region
->current
;
6088 section_alignment
= os
->bfd_section
->alignment_power
;
6091 section_alignment
= exp_get_power (os
->section_alignment
, os
,
6092 "section alignment");
6094 /* Align to what the section needs. */
6095 if (section_alignment
> 0)
6097 bfd_vma savedot
= newdot
;
6100 newdot
= align_power (newdot
, section_alignment
);
6101 dotdelta
= newdot
- savedot
;
6103 if (lang_sizing_iteration
== 1)
6105 else if (lang_sizing_iteration
> 1)
6107 /* Only report adjustments that would change
6108 alignment from what we have already reported. */
6109 diff
= newdot
- os
->bfd_section
->vma
;
6110 if (!(diff
& (((bfd_vma
) 1 << section_alignment
) - 1)))
6114 && (config
.warn_section_align
6115 || os
->addr_tree
!= NULL
))
6116 einfo (_("%P: warning: "
6117 "start of section %s changed by %ld\n"),
6118 os
->name
, (long) diff
);
6121 bfd_set_section_vma (os
->bfd_section
, newdot
);
6123 os
->bfd_section
->output_offset
= 0;
6126 lang_size_sections_1 (&os
->children
.head
, os
,
6127 os
->fill
, newdot
, relax
, check_regions
);
6129 os
->processed_vma
= true;
6131 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
6132 /* Except for some special linker created sections,
6133 no output section should change from zero size
6134 after strip_excluded_output_sections. A non-zero
6135 size on an ignored section indicates that some
6136 input section was not sized early enough. */
6137 ASSERT (os
->bfd_section
->size
== 0);
6140 dot
= os
->bfd_section
->vma
;
6142 /* Put the section within the requested block size, or
6143 align at the block boundary. */
6145 + TO_ADDR (os
->bfd_section
->size
)
6146 + os
->block_value
- 1)
6147 & - (bfd_vma
) os
->block_value
);
6149 if (!(os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
6150 os
->bfd_section
->size
= TO_SIZE (after
6151 - os
->bfd_section
->vma
);
6154 /* Set section lma. */
6157 r
= lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, false);
6161 bfd_vma lma
= exp_get_abs_int (os
->load_base
, 0, "load base");
6162 os
->bfd_section
->lma
= lma
;
6164 else if (os
->lma_region
!= NULL
)
6166 bfd_vma lma
= os
->lma_region
->current
;
6168 if (os
->align_lma_with_input
)
6172 /* When LMA_REGION is the same as REGION, align the LMA
6173 as we did for the VMA, possibly including alignment
6174 from the bfd section. If a different region, then
6175 only align according to the value in the output
6177 if (os
->lma_region
!= os
->region
)
6178 section_alignment
= exp_get_power (os
->section_alignment
,
6180 "section alignment");
6181 if (section_alignment
> 0)
6182 lma
= align_power (lma
, section_alignment
);
6184 os
->bfd_section
->lma
= lma
;
6186 else if (r
->last_os
!= NULL
6187 && (os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
6192 last
= r
->last_os
->output_section_statement
.bfd_section
;
6194 /* A backwards move of dot should be accompanied by
6195 an explicit assignment to the section LMA (ie.
6196 os->load_base set) because backwards moves can
6197 create overlapping LMAs. */
6199 && os
->bfd_section
->size
!= 0
6200 && dot
+ TO_ADDR (os
->bfd_section
->size
) <= last
->vma
)
6202 /* If dot moved backwards then leave lma equal to
6203 vma. This is the old default lma, which might
6204 just happen to work when the backwards move is
6205 sufficiently large. Nag if this changes anything,
6206 so people can fix their linker scripts. */
6208 if (last
->vma
!= last
->lma
)
6209 einfo (_("%P: warning: dot moved backwards "
6210 "before `%s'\n"), os
->name
);
6214 /* If this is an overlay, set the current lma to that
6215 at the end of the previous section. */
6216 if (os
->sectype
== overlay_section
)
6217 lma
= last
->lma
+ TO_ADDR (last
->size
);
6219 /* Otherwise, keep the same lma to vma relationship
6220 as the previous section. */
6222 lma
= os
->bfd_section
->vma
+ last
->lma
- last
->vma
;
6224 if (section_alignment
> 0)
6225 lma
= align_power (lma
, section_alignment
);
6226 os
->bfd_section
->lma
= lma
;
6229 os
->processed_lma
= true;
6231 /* Keep track of normal sections using the default
6232 lma region. We use this to set the lma for
6233 following sections. Overlays or other linker
6234 script assignment to lma might mean that the
6235 default lma == vma is incorrect.
6236 To avoid warnings about dot moving backwards when using
6237 -Ttext, don't start tracking sections until we find one
6238 of non-zero size or with lma set differently to vma.
6239 Do this tracking before we short-cut the loop so that we
6240 track changes for the case where the section size is zero,
6241 but the lma is set differently to the vma. This is
6242 important, if an orphan section is placed after an
6243 otherwise empty output section that has an explicit lma
6244 set, we want that lma reflected in the orphans lma. */
6245 if (((!IGNORE_SECTION (os
->bfd_section
)
6246 && (os
->bfd_section
->size
!= 0
6247 || (r
->last_os
== NULL
6248 && os
->bfd_section
->vma
!= os
->bfd_section
->lma
)
6249 || (r
->last_os
!= NULL
6250 && dot
>= (r
->last_os
->output_section_statement
6251 .bfd_section
->vma
))))
6252 || os
->sectype
== first_overlay_section
)
6253 && os
->lma_region
== NULL
6254 && !bfd_link_relocatable (&link_info
))
6257 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
6260 /* .tbss sections effectively have zero size. */
6261 if (!IS_TBSS (os
->bfd_section
)
6262 || bfd_link_relocatable (&link_info
))
6263 dotdelta
= TO_ADDR (os
->bfd_section
->size
);
6268 if (os
->update_dot_tree
!= 0)
6269 exp_fold_tree (os
->update_dot_tree
, os
, bfd_abs_section_ptr
, &dot
);
6271 /* Update dot in the region ?
6272 We only do this if the section is going to be allocated,
6273 since unallocated sections do not contribute to the region's
6274 overall size in memory. */
6275 if (os
->region
!= NULL
6276 && (os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
)))
6278 os
->region
->current
= dot
;
6281 /* Make sure the new address is within the region. */
6282 os_region_check (os
, os
->region
, os
->addr_tree
,
6283 os
->bfd_section
->vma
);
6285 if (os
->lma_region
!= NULL
&& os
->lma_region
!= os
->region
6286 && ((os
->bfd_section
->flags
& SEC_LOAD
)
6287 || os
->align_lma_with_input
))
6289 os
->lma_region
->current
= os
->bfd_section
->lma
+ dotdelta
;
6292 os_region_check (os
, os
->lma_region
, NULL
,
6293 os
->bfd_section
->lma
);
6299 case lang_constructors_statement_enum
:
6300 dot
= lang_size_sections_1 (&constructor_list
.head
, current_os
,
6301 fill
, dot
, relax
, check_regions
);
6304 case lang_data_statement_enum
:
6306 unsigned int size
= 0;
6308 s
->data_statement
.output_offset
= dot
- current_os
->bfd_section
->vma
;
6309 s
->data_statement
.output_section
= current_os
->bfd_section
;
6311 /* We might refer to provided symbols in the expression, and
6312 need to mark them as needed. */
6313 exp_fold_tree (s
->data_statement
.exp
, os
,
6314 bfd_abs_section_ptr
, &dot
);
6316 switch (s
->data_statement
.type
)
6334 if (size
< TO_SIZE ((unsigned) 1))
6335 size
= TO_SIZE ((unsigned) 1);
6336 dot
+= TO_ADDR (size
);
6337 if (!(current_os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
6338 current_os
->bfd_section
->size
6339 = TO_SIZE (dot
- current_os
->bfd_section
->vma
);
6344 case lang_reloc_statement_enum
:
6348 s
->reloc_statement
.output_offset
6349 = dot
- current_os
->bfd_section
->vma
;
6350 s
->reloc_statement
.output_section
6351 = current_os
->bfd_section
;
6352 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
6353 dot
+= TO_ADDR (size
);
6354 if (!(current_os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
6355 current_os
->bfd_section
->size
6356 = TO_SIZE (dot
- current_os
->bfd_section
->vma
);
6360 case lang_wild_statement_enum
:
6361 dot
= lang_size_sections_1 (&s
->wild_statement
.children
.head
,
6362 current_os
, fill
, dot
, relax
,
6366 case lang_object_symbols_statement_enum
:
6367 link_info
.create_object_symbols_section
= current_os
->bfd_section
;
6368 current_os
->bfd_section
->flags
|= SEC_KEEP
;
6371 case lang_output_statement_enum
:
6372 case lang_target_statement_enum
:
6375 case lang_input_section_enum
:
6379 i
= s
->input_section
.section
;
6384 if (!bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
6385 fatal (_("%P: can't relax section: %E\n"));
6389 dot
= size_input_section (prev
, current_os
, fill
, &removed
, dot
);
6393 case lang_input_statement_enum
:
6396 case lang_fill_statement_enum
:
6397 s
->fill_statement
.output_section
= current_os
->bfd_section
;
6399 fill
= s
->fill_statement
.fill
;
6402 case lang_assignment_statement_enum
:
6404 bfd_vma newdot
= dot
;
6405 etree_type
*tree
= s
->assignment_statement
.exp
;
6407 expld
.dataseg
.relro
= exp_seg_relro_none
;
6409 exp_fold_tree (tree
, os
, current_os
->bfd_section
, &newdot
);
6411 ldlang_check_relro_region (s
);
6413 expld
.dataseg
.relro
= exp_seg_relro_none
;
6415 /* This symbol may be relative to this section. */
6416 if ((tree
->type
.node_class
== etree_provided
6417 || tree
->type
.node_class
== etree_assign
)
6418 && (tree
->assign
.dst
[0] != '.'
6419 || tree
->assign
.dst
[1] != '\0'))
6420 current_os
->update_dot
= 1;
6422 if (!current_os
->ignored
)
6424 if (current_os
== abs_output_section
)
6426 /* If we don't have an output section, then just adjust
6427 the default memory address. */
6428 lang_memory_region_lookup (DEFAULT_MEMORY_REGION
,
6429 false)->current
= newdot
;
6431 else if (newdot
!= dot
)
6433 /* Insert a pad after this statement. We can't
6434 put the pad before when relaxing, in case the
6435 assignment references dot. */
6436 insert_pad (&s
->header
.next
, fill
, TO_SIZE (newdot
- dot
),
6437 current_os
->bfd_section
, dot
);
6439 /* Don't neuter the pad below when relaxing. */
6442 /* If dot is advanced, this implies that the section
6443 should have space allocated to it, unless the
6444 user has explicitly stated that the section
6445 should not be allocated. */
6446 if (current_os
->sectype
!= noalloc_section
6447 && (current_os
->sectype
!= noload_section
6448 || (bfd_get_flavour (link_info
.output_bfd
)
6449 == bfd_target_elf_flavour
)))
6450 current_os
->bfd_section
->flags
|= SEC_ALLOC
;
6457 case lang_padding_statement_enum
:
6458 /* If this is the first time lang_size_sections is called,
6459 we won't have any padding statements. If this is the
6460 second or later passes when relaxing, we should allow
6461 padding to shrink. If padding is needed on this pass, it
6462 will be added back in. */
6463 s
->padding_statement
.size
= 0;
6465 /* Make sure output_offset is valid. If relaxation shrinks
6466 the section and this pad isn't needed, it's possible to
6467 have output_offset larger than the final size of the
6468 section. bfd_set_section_contents will complain even for
6469 a pad size of zero. */
6470 s
->padding_statement
.output_offset
6471 = dot
- current_os
->bfd_section
->vma
;
6474 case lang_group_statement_enum
:
6475 dot
= lang_size_sections_1 (&s
->group_statement
.children
.head
,
6476 current_os
, fill
, dot
, relax
,
6480 case lang_insert_statement_enum
:
6483 /* We can only get here when relaxing is turned on. */
6484 case lang_address_statement_enum
:
6492 /* If an input section doesn't fit in the current output
6493 section, remove it from the list. Handle the case where we
6494 have to remove an input_section statement here: there is a
6495 special case to remove the first element of the list. */
6496 if (link_info
.non_contiguous_regions
&& removed
)
6498 /* If we removed the first element during the previous
6499 iteration, override the loop assignment of prev_s. */
6505 /* If there was a real previous input section, just skip
6507 prev_s
->header
.next
=s
->header
.next
;
6509 removed_prev_s
= false;
6513 /* Remove the first input section of the list. */
6514 *prev
= s
->header
.next
;
6515 removed_prev_s
= true;
6518 /* Move to next element, unless we removed the head of the
6520 if (!removed_prev_s
)
6521 prev
= &s
->header
.next
;
6525 prev
= &s
->header
.next
;
6526 removed_prev_s
= false;
6532 /* Callback routine that is used in _bfd_elf_map_sections_to_segments.
6533 The BFD library has set NEW_SEGMENT to TRUE iff it thinks that
6534 CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different
6535 segments. We are allowed an opportunity to override this decision. */
6538 ldlang_override_segment_assignment (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6539 bfd
*abfd ATTRIBUTE_UNUSED
,
6540 asection
*current_section
,
6541 asection
*previous_section
,
6544 lang_output_section_statement_type
*cur
;
6545 lang_output_section_statement_type
*prev
;
6547 /* The checks below are only necessary when the BFD library has decided
6548 that the two sections ought to be placed into the same segment. */
6552 /* Paranoia checks. */
6553 if (current_section
== NULL
|| previous_section
== NULL
)
6556 /* If this flag is set, the target never wants code and non-code
6557 sections comingled in the same segment. */
6558 if (config
.separate_code
6559 && ((current_section
->flags
^ previous_section
->flags
) & SEC_CODE
))
6562 /* Find the memory regions associated with the two sections.
6563 We call lang_output_section_find() here rather than scanning the list
6564 of output sections looking for a matching section pointer because if
6565 we have a large number of sections then a hash lookup is faster. */
6566 cur
= lang_output_section_find (current_section
->name
);
6567 prev
= lang_output_section_find (previous_section
->name
);
6569 /* More paranoia. */
6570 if (cur
== NULL
|| prev
== NULL
)
6573 /* If the regions are different then force the sections to live in
6574 different segments. See the email thread starting at the following
6575 URL for the reasons why this is necessary:
6576 http://sourceware.org/ml/binutils/2007-02/msg00216.html */
6577 return cur
->region
!= prev
->region
;
6581 one_lang_size_sections_pass (bool *relax
, bool check_regions
)
6583 lang_statement_iteration
++;
6584 if (expld
.phase
!= lang_mark_phase_enum
)
6585 lang_sizing_iteration
++;
6586 lang_size_sections_1 (&statement_list
.head
, abs_output_section
,
6587 0, 0, relax
, check_regions
);
6591 lang_size_segment (void)
6593 /* If XXX_SEGMENT_ALIGN XXX_SEGMENT_END pair was seen, check whether
6594 a page could be saved in the data segment. */
6595 seg_align_type
*seg
= &expld
.dataseg
;
6596 bfd_vma first
, last
;
6598 first
= -seg
->base
& (seg
->commonpagesize
- 1);
6599 last
= seg
->end
& (seg
->commonpagesize
- 1);
6601 && ((seg
->base
& ~(seg
->commonpagesize
- 1))
6602 != (seg
->end
& ~(seg
->commonpagesize
- 1)))
6603 && first
+ last
<= seg
->commonpagesize
)
6605 seg
->phase
= exp_seg_adjust
;
6609 seg
->phase
= exp_seg_done
;
6614 lang_size_relro_segment_1 (void)
6616 seg_align_type
*seg
= &expld
.dataseg
;
6617 bfd_vma relro_end
, desired_end
;
6620 /* Compute the expected PT_GNU_RELRO/PT_LOAD segment end. */
6621 relro_end
= (seg
->relro_end
+ seg
->relropagesize
- 1) & -seg
->relropagesize
;
6623 /* Adjust by the offset arg of XXX_SEGMENT_RELRO_END. */
6624 desired_end
= relro_end
- seg
->relro_offset
;
6626 /* For sections in the relro segment.. */
6627 for (sec
= link_info
.output_bfd
->section_last
; sec
; sec
= sec
->prev
)
6628 if ((sec
->flags
& SEC_ALLOC
) != 0
6629 && sec
->vma
>= seg
->base
6630 && sec
->vma
< seg
->relro_end
- seg
->relro_offset
)
6632 /* Where do we want to put this section so that it ends as
6634 bfd_vma start
, end
, bump
;
6636 end
= start
= sec
->vma
;
6638 end
+= TO_ADDR (sec
->size
);
6639 bump
= desired_end
- end
;
6640 /* We'd like to increase START by BUMP, but we must heed
6641 alignment so the increase might be less than optimum. */
6643 start
&= ~(((bfd_vma
) 1 << sec
->alignment_power
) - 1);
6644 /* This is now the desired end for the previous section. */
6645 desired_end
= start
;
6648 seg
->phase
= exp_seg_relro_adjust
;
6649 ASSERT (desired_end
>= seg
->base
);
6650 seg
->base
= desired_end
;
6655 lang_size_relro_segment (bool *relax
, bool check_regions
)
6657 bool do_reset
= false;
6659 if (link_info
.relro
&& expld
.dataseg
.relro_end
)
6661 bfd_vma data_initial_base
= expld
.dataseg
.base
;
6662 bfd_vma data_relro_end
= lang_size_relro_segment_1 ();
6664 lang_reset_memory_regions ();
6665 one_lang_size_sections_pass (relax
, check_regions
);
6667 /* Assignments to dot, or to output section address in a user
6668 script have increased padding over the original. Revert. */
6669 if (expld
.dataseg
.relro_end
> data_relro_end
)
6671 expld
.dataseg
.base
= data_initial_base
;
6675 else if (lang_size_segment ())
6682 lang_size_sections (bool *relax
, bool check_regions
)
6684 expld
.phase
= lang_allocating_phase_enum
;
6685 expld
.dataseg
.phase
= exp_seg_none
;
6687 one_lang_size_sections_pass (relax
, check_regions
);
6689 if (expld
.dataseg
.phase
!= exp_seg_end_seen
)
6690 expld
.dataseg
.phase
= exp_seg_done
;
6692 if (expld
.dataseg
.phase
== exp_seg_end_seen
)
6695 = lang_size_relro_segment (relax
, check_regions
);
6699 lang_reset_memory_regions ();
6700 one_lang_size_sections_pass (relax
, check_regions
);
6703 if (link_info
.relro
&& expld
.dataseg
.relro_end
)
6705 link_info
.relro_start
= expld
.dataseg
.base
;
6706 link_info
.relro_end
= expld
.dataseg
.relro_end
;
6711 static lang_output_section_statement_type
*current_section
;
6712 static lang_assignment_statement_type
*current_assign
;
6713 static bool prefer_next_section
;
6715 /* Worker function for lang_do_assignments. Recursiveness goes here. */
6718 lang_do_assignments_1 (lang_statement_union_type
*s
,
6719 lang_output_section_statement_type
*current_os
,
6724 lang_output_section_statement_type
*os
= current_os
;
6726 for (; s
!= NULL
; s
= s
->header
.next
)
6728 switch (s
->header
.type
)
6730 case lang_constructors_statement_enum
:
6731 dot
= lang_do_assignments_1 (constructor_list
.head
,
6732 current_os
, fill
, dot
, found_end
);
6735 case lang_output_section_statement_enum
:
6739 os
= &s
->output_section_statement
;
6740 os
->after_end
= *found_end
;
6741 init_opb (os
->bfd_section
);
6743 if (os
->bfd_section
!= NULL
)
6745 if (!os
->ignored
&& (os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
6747 current_section
= os
;
6748 prefer_next_section
= false;
6750 newdot
= os
->bfd_section
->vma
;
6752 newdot
= lang_do_assignments_1 (os
->children
.head
,
6753 os
, os
->fill
, newdot
, found_end
);
6756 if (os
->bfd_section
!= NULL
)
6758 newdot
= os
->bfd_section
->vma
;
6760 /* .tbss sections effectively have zero size. */
6761 if (!IS_TBSS (os
->bfd_section
)
6762 || bfd_link_relocatable (&link_info
))
6763 newdot
+= TO_ADDR (os
->bfd_section
->size
);
6765 if (os
->update_dot_tree
!= NULL
)
6766 exp_fold_tree (os
->update_dot_tree
, os
,
6767 bfd_abs_section_ptr
, &newdot
);
6774 case lang_wild_statement_enum
:
6776 dot
= lang_do_assignments_1 (s
->wild_statement
.children
.head
,
6777 current_os
, fill
, dot
, found_end
);
6780 case lang_object_symbols_statement_enum
:
6781 case lang_output_statement_enum
:
6782 case lang_target_statement_enum
:
6785 case lang_data_statement_enum
:
6786 exp_fold_tree (s
->data_statement
.exp
, os
, bfd_abs_section_ptr
, &dot
);
6787 if (expld
.result
.valid_p
)
6789 s
->data_statement
.value
= expld
.result
.value
;
6790 if (expld
.result
.section
!= NULL
)
6791 s
->data_statement
.value
+= expld
.result
.section
->vma
;
6793 else if (expld
.phase
== lang_final_phase_enum
)
6794 fatal (_("%P: invalid data statement\n"));
6797 switch (s
->data_statement
.type
)
6815 if (size
< TO_SIZE ((unsigned) 1))
6816 size
= TO_SIZE ((unsigned) 1);
6817 dot
+= TO_ADDR (size
);
6821 case lang_reloc_statement_enum
:
6822 exp_fold_tree (s
->reloc_statement
.addend_exp
, os
,
6823 bfd_abs_section_ptr
, &dot
);
6824 if (expld
.result
.valid_p
)
6825 s
->reloc_statement
.addend_value
= expld
.result
.value
;
6826 else if (expld
.phase
== lang_final_phase_enum
)
6827 fatal (_("%P: invalid reloc statement\n"));
6828 dot
+= TO_ADDR (bfd_get_reloc_size (s
->reloc_statement
.howto
));
6831 case lang_input_section_enum
:
6833 asection
*in
= s
->input_section
.section
;
6835 if ((in
->flags
& SEC_EXCLUDE
) == 0)
6836 dot
+= TO_ADDR (in
->size
);
6840 case lang_input_statement_enum
:
6843 case lang_fill_statement_enum
:
6844 fill
= s
->fill_statement
.fill
;
6847 case lang_assignment_statement_enum
:
6848 current_assign
= &s
->assignment_statement
;
6849 if (current_assign
->exp
->type
.node_class
!= etree_assert
)
6851 const char *p
= current_assign
->exp
->assign
.dst
;
6853 if (current_os
== abs_output_section
&& p
[0] == '.' && p
[1] == 0)
6854 prefer_next_section
= true;
6858 if (strcmp (p
, "end") == 0)
6861 exp_fold_tree (s
->assignment_statement
.exp
, os
,
6862 (current_os
->bfd_section
!= NULL
6863 ? current_os
->bfd_section
: bfd_und_section_ptr
),
6867 case lang_padding_statement_enum
:
6868 dot
+= TO_ADDR (s
->padding_statement
.size
);
6871 case lang_group_statement_enum
:
6872 dot
= lang_do_assignments_1 (s
->group_statement
.children
.head
,
6873 current_os
, fill
, dot
, found_end
);
6876 case lang_insert_statement_enum
:
6879 case lang_address_statement_enum
:
6891 lang_do_assignments (lang_phase_type phase
)
6893 bool found_end
= false;
6895 current_section
= NULL
;
6896 prefer_next_section
= false;
6897 expld
.phase
= phase
;
6898 lang_statement_iteration
++;
6899 lang_do_assignments_1 (statement_list
.head
,
6900 abs_output_section
, NULL
, 0, &found_end
);
6903 /* For an assignment statement outside of an output section statement,
6904 choose the best of neighbouring output sections to use for values
6908 section_for_dot (void)
6912 /* Assignments belong to the previous output section, unless there
6913 has been an assignment to "dot", in which case following
6914 assignments belong to the next output section. (The assumption
6915 is that an assignment to "dot" is setting up the address for the
6916 next output section.) Except that past the assignment to "_end"
6917 we always associate with the previous section. This exception is
6918 for targets like SH that define an alloc .stack or other
6919 weirdness after non-alloc sections. */
6920 if (current_section
== NULL
|| prefer_next_section
)
6922 lang_statement_union_type
*stmt
;
6923 lang_output_section_statement_type
*os
;
6925 for (stmt
= (lang_statement_union_type
*) current_assign
;
6927 stmt
= stmt
->header
.next
)
6928 if (stmt
->header
.type
== lang_output_section_statement_enum
)
6931 os
= stmt
? &stmt
->output_section_statement
: NULL
;
6934 && (os
->bfd_section
== NULL
6935 || (os
->bfd_section
->flags
& SEC_EXCLUDE
) != 0
6936 || bfd_section_removed_from_list (link_info
.output_bfd
,
6940 if (current_section
== NULL
|| os
== NULL
|| !os
->after_end
)
6943 s
= os
->bfd_section
;
6945 s
= link_info
.output_bfd
->section_last
;
6947 && ((s
->flags
& SEC_ALLOC
) == 0
6948 || (s
->flags
& SEC_THREAD_LOCAL
) != 0))
6953 return bfd_abs_section_ptr
;
6957 s
= current_section
->bfd_section
;
6959 /* The section may have been stripped. */
6961 && ((s
->flags
& SEC_EXCLUDE
) != 0
6962 || (s
->flags
& SEC_ALLOC
) == 0
6963 || (s
->flags
& SEC_THREAD_LOCAL
) != 0
6964 || bfd_section_removed_from_list (link_info
.output_bfd
, s
)))
6967 s
= link_info
.output_bfd
->sections
;
6969 && ((s
->flags
& SEC_ALLOC
) == 0
6970 || (s
->flags
& SEC_THREAD_LOCAL
) != 0))
6975 return bfd_abs_section_ptr
;
6978 /* Array of __start/__stop/.startof./.sizeof/ symbols. */
6980 static struct bfd_link_hash_entry
**start_stop_syms
;
6981 static size_t start_stop_count
= 0;
6982 static size_t start_stop_alloc
= 0;
6984 /* Give start/stop SYMBOL for SEC a preliminary definition, and add it
6985 to start_stop_syms. */
6988 lang_define_start_stop (const char *symbol
, asection
*sec
)
6990 struct bfd_link_hash_entry
*h
;
6992 h
= bfd_define_start_stop (link_info
.output_bfd
, &link_info
, symbol
, sec
);
6995 if (start_stop_count
== start_stop_alloc
)
6997 start_stop_alloc
= 2 * start_stop_alloc
+ 10;
6999 = xrealloc (start_stop_syms
,
7000 start_stop_alloc
* sizeof (*start_stop_syms
));
7002 start_stop_syms
[start_stop_count
++] = h
;
7006 /* Check for input sections whose names match references to
7007 __start_SECNAME or __stop_SECNAME symbols. Give the symbols
7008 preliminary definitions. */
7011 lang_init_start_stop (void)
7015 char leading_char
= bfd_get_symbol_leading_char (link_info
.output_bfd
);
7017 for (abfd
= link_info
.input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
7018 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7021 const char *secname
= s
->name
;
7023 for (ps
= secname
; *ps
!= '\0'; ps
++)
7024 if (!ISALNUM ((unsigned char) *ps
) && *ps
!= '_')
7028 char *symbol
= (char *) xmalloc (10 + strlen (secname
));
7030 symbol
[0] = leading_char
;
7031 sprintf (symbol
+ (leading_char
!= 0), "__start_%s", secname
);
7032 lang_define_start_stop (symbol
, s
);
7034 symbol
[1] = leading_char
;
7035 memcpy (symbol
+ 1 + (leading_char
!= 0), "__stop", 6);
7036 lang_define_start_stop (symbol
+ 1, s
);
7043 /* Iterate over start_stop_syms. */
7046 foreach_start_stop (void (*func
) (struct bfd_link_hash_entry
*))
7050 for (i
= 0; i
< start_stop_count
; ++i
)
7051 func (start_stop_syms
[i
]);
7054 /* __start and __stop symbols are only supposed to be defined by the
7055 linker for orphan sections, but we now extend that to sections that
7056 map to an output section of the same name. The symbols were
7057 defined early for --gc-sections, before we mapped input to output
7058 sections, so undo those that don't satisfy this rule. */
7061 undef_start_stop (struct bfd_link_hash_entry
*h
)
7063 if (h
->ldscript_def
)
7066 if (h
->u
.def
.section
->output_section
== NULL
7067 || h
->u
.def
.section
->output_section
->owner
!= link_info
.output_bfd
7068 || strcmp (h
->u
.def
.section
->name
,
7069 h
->u
.def
.section
->output_section
->name
) != 0)
7071 asection
*sec
= bfd_get_section_by_name (link_info
.output_bfd
,
7072 h
->u
.def
.section
->name
);
7075 /* When there are more than one input sections with the same
7076 section name, SECNAME, linker picks the first one to define
7077 __start_SECNAME and __stop_SECNAME symbols. When the first
7078 input section is removed by comdat group, we need to check
7079 if there is still an output section with section name
7082 for (i
= sec
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
7083 if (strcmp (h
->u
.def
.section
->name
, i
->name
) == 0)
7085 h
->u
.def
.section
= i
;
7089 h
->type
= bfd_link_hash_undefined
;
7090 h
->u
.undef
.abfd
= NULL
;
7091 if (is_elf_hash_table (link_info
.hash
))
7093 const struct elf_backend_data
*bed
;
7094 struct elf_link_hash_entry
*eh
= (struct elf_link_hash_entry
*) h
;
7095 unsigned int was_forced
= eh
->forced_local
;
7097 bed
= get_elf_backend_data (link_info
.output_bfd
);
7098 (*bed
->elf_backend_hide_symbol
) (&link_info
, eh
, true);
7099 if (!eh
->ref_regular_nonweak
)
7100 h
->type
= bfd_link_hash_undefweak
;
7101 eh
->def_regular
= 0;
7102 eh
->forced_local
= was_forced
;
7108 lang_undef_start_stop (void)
7110 foreach_start_stop (undef_start_stop
);
7113 /* Check for output sections whose names match references to
7114 .startof.SECNAME or .sizeof.SECNAME symbols. Give the symbols
7115 preliminary definitions. */
7118 lang_init_startof_sizeof (void)
7122 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7124 const char *secname
= s
->name
;
7125 char *symbol
= (char *) xmalloc (10 + strlen (secname
));
7127 sprintf (symbol
, ".startof.%s", secname
);
7128 lang_define_start_stop (symbol
, s
);
7130 memcpy (symbol
+ 1, ".size", 5);
7131 lang_define_start_stop (symbol
+ 1, s
);
7136 /* Set .startof., .sizeof., __start and __stop symbols final values. */
7139 set_start_stop (struct bfd_link_hash_entry
*h
)
7142 || h
->type
!= bfd_link_hash_defined
)
7145 if (h
->root
.string
[0] == '.')
7147 /* .startof. or .sizeof. symbol.
7148 .startof. already has final value. */
7149 if (h
->root
.string
[2] == 'i')
7152 h
->u
.def
.value
= TO_ADDR (h
->u
.def
.section
->size
);
7153 h
->u
.def
.section
= bfd_abs_section_ptr
;
7158 /* __start or __stop symbol. */
7159 int has_lead
= bfd_get_symbol_leading_char (link_info
.output_bfd
) != 0;
7161 h
->u
.def
.section
= h
->u
.def
.section
->output_section
;
7162 if (h
->root
.string
[4 + has_lead
] == 'o')
7165 h
->u
.def
.value
= TO_ADDR (h
->u
.def
.section
->size
);
7171 lang_finalize_start_stop (void)
7173 foreach_start_stop (set_start_stop
);
7177 lang_symbol_tweaks (void)
7179 /* Give initial values for __start and __stop symbols, so that ELF
7180 gc_sections will keep sections referenced by these symbols. Must
7181 be done before lang_do_assignments. */
7182 if (config
.build_constructors
)
7183 lang_init_start_stop ();
7185 /* Make __ehdr_start hidden, and set def_regular even though it is
7186 likely undefined at this stage. For lang_check_relocs. */
7187 if (is_elf_hash_table (link_info
.hash
)
7188 && !bfd_link_relocatable (&link_info
))
7190 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*)
7191 bfd_link_hash_lookup (link_info
.hash
, "__ehdr_start",
7192 false, false, true);
7194 /* Only adjust the export class if the symbol was referenced
7195 and not defined, otherwise leave it alone. */
7197 && (h
->root
.type
== bfd_link_hash_new
7198 || h
->root
.type
== bfd_link_hash_undefined
7199 || h
->root
.type
== bfd_link_hash_undefweak
7200 || h
->root
.type
== bfd_link_hash_common
))
7202 const struct elf_backend_data
*bed
;
7203 bed
= get_elf_backend_data (link_info
.output_bfd
);
7204 (*bed
->elf_backend_hide_symbol
) (&link_info
, h
, true);
7205 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
7206 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
7208 h
->root
.linker_def
= 1;
7209 h
->root
.rel_from_abs
= 1;
7210 elf_hash_table (&link_info
)->hehdr_start
= h
;
7218 struct bfd_link_hash_entry
*h
;
7221 if ((bfd_link_relocatable (&link_info
) && !link_info
.gc_sections
)
7222 || bfd_link_dll (&link_info
))
7223 warn
= entry_from_cmdline
;
7227 /* Force the user to specify a root when generating a relocatable with
7228 --gc-sections, unless --gc-keep-exported was also given. */
7229 if (bfd_link_relocatable (&link_info
)
7230 && link_info
.gc_sections
7231 && !link_info
.gc_keep_exported
)
7233 struct bfd_sym_chain
*sym
;
7235 for (sym
= link_info
.gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
7237 h
= bfd_link_hash_lookup (link_info
.hash
, sym
->name
,
7238 false, false, false);
7240 && !bfd_is_const_section (h
->u
.def
.section
))
7244 fatal (_("%P: --gc-sections requires a defined symbol root "
7245 "specified by -e or -u\n"));
7248 if (entry_symbol
.name
== NULL
)
7250 /* No entry has been specified. Look for the default entry, but
7251 don't warn if we don't find it. */
7252 entry_symbol
.name
= entry_symbol_default
;
7256 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
.name
,
7257 false, false, true);
7259 if (! is_defined (h
) || h
->u
.def
.section
->output_section
== NULL
)
7260 h
= ldemul_find_alt_start_symbol (&entry_symbol
);
7263 && h
->u
.def
.section
->output_section
!= NULL
)
7267 val
= (h
->u
.def
.value
7268 + bfd_section_vma (h
->u
.def
.section
->output_section
)
7269 + h
->u
.def
.section
->output_offset
);
7270 if (!bfd_set_start_address (link_info
.output_bfd
, val
))
7271 fatal (_("%P: %s: can't set start address\n"), entry_symbol
.name
);
7278 /* We couldn't find the entry symbol. Try parsing it as a
7280 val
= bfd_scan_vma (entry_symbol
.name
, &send
, 0);
7283 if (!bfd_set_start_address (link_info
.output_bfd
, val
))
7284 fatal (_("%P: can't set start address\n"));
7286 /* BZ 2004952: Only use the start of the entry section for executables. */
7287 else if bfd_link_executable (&link_info
)
7291 /* Can't find the entry symbol, and it's not a number. Use
7292 the first address in the text section. */
7293 ts
= bfd_get_section_by_name (link_info
.output_bfd
, entry_section
);
7297 einfo (_("%P: warning: cannot find entry symbol %s;"
7298 " defaulting to %V\n"),
7300 bfd_section_vma (ts
));
7301 if (!bfd_set_start_address (link_info
.output_bfd
,
7302 bfd_section_vma (ts
)))
7303 fatal (_("%P: can't set start address\n"));
7308 einfo (_("%P: warning: cannot find entry symbol %s;"
7309 " not setting start address\n"),
7316 einfo (_("%P: warning: cannot find entry symbol %s;"
7317 " not setting start address\n"),
7323 /* This is a small function used when we want to ignore errors from
7327 ignore_bfd_errors (const char *fmt ATTRIBUTE_UNUSED
,
7328 va_list ap ATTRIBUTE_UNUSED
)
7330 /* Don't do anything. */
7333 /* Check that the architecture of all the input files is compatible
7334 with the output file. Also call the backend to let it do any
7335 other checking that is needed. */
7340 lang_input_statement_type
*file
;
7342 const bfd_arch_info_type
*compatible
;
7344 for (file
= (void *) file_chain
.head
;
7348 #if BFD_SUPPORTS_PLUGINS
7349 /* Don't check format of files claimed by plugin. */
7350 if (file
->flags
.claimed
)
7352 #endif /* BFD_SUPPORTS_PLUGINS */
7353 input_bfd
= file
->the_bfd
;
7355 = bfd_arch_get_compatible (input_bfd
, link_info
.output_bfd
,
7356 command_line
.accept_unknown_input_arch
);
7358 /* In general it is not possible to perform a relocatable
7359 link between differing object formats when the input
7360 file has relocations, because the relocations in the
7361 input format may not have equivalent representations in
7362 the output format (and besides BFD does not translate
7363 relocs for other link purposes than a final link). */
7364 if (!file
->flags
.just_syms
7365 && (bfd_link_relocatable (&link_info
)
7366 || link_info
.emitrelocations
)
7367 && (compatible
== NULL
7368 || (bfd_get_flavour (input_bfd
)
7369 != bfd_get_flavour (link_info
.output_bfd
)))
7370 && (bfd_get_file_flags (input_bfd
) & HAS_RELOC
) != 0)
7372 fatal (_("%P: relocatable linking with relocations from"
7373 " format %s (%pB) to format %s (%pB) is not supported\n"),
7374 bfd_get_target (input_bfd
), input_bfd
,
7375 bfd_get_target (link_info
.output_bfd
), link_info
.output_bfd
);
7378 if (compatible
== NULL
)
7380 if (command_line
.warn_mismatch
)
7381 einfo (_("%X%P: %s architecture of input file `%pB'"
7382 " is incompatible with %s output\n"),
7383 bfd_printable_name (input_bfd
), input_bfd
,
7384 bfd_printable_name (link_info
.output_bfd
));
7387 /* If the input bfd has no contents, it shouldn't set the
7388 private data of the output bfd. */
7389 else if (!file
->flags
.just_syms
7390 && ((input_bfd
->flags
& DYNAMIC
) != 0
7391 || bfd_count_sections (input_bfd
) != 0))
7393 bfd_error_handler_type pfn
= NULL
;
7395 /* If we aren't supposed to warn about mismatched input
7396 files, temporarily set the BFD error handler to a
7397 function which will do nothing. We still want to call
7398 bfd_merge_private_bfd_data, since it may set up
7399 information which is needed in the output file. */
7400 if (!command_line
.warn_mismatch
)
7401 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
7402 if (!bfd_merge_private_bfd_data (input_bfd
, &link_info
))
7404 if (command_line
.warn_mismatch
)
7405 einfo (_("%X%P: failed to merge target specific data"
7406 " of file %pB\n"), input_bfd
);
7408 if (!command_line
.warn_mismatch
)
7409 bfd_set_error_handler (pfn
);
7414 /* Look through all the global common symbols and attach them to the
7415 correct section. The -sort-common command line switch may be used
7416 to roughly sort the entries by alignment. */
7421 if (link_info
.inhibit_common_definition
)
7423 if (bfd_link_relocatable (&link_info
)
7424 && !command_line
.force_common_definition
)
7427 if (!config
.sort_common
)
7428 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, NULL
);
7433 if (config
.sort_common
== sort_descending
)
7435 for (power
= 4; power
> 0; power
--)
7436 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7439 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7443 for (power
= 0; power
<= 4; power
++)
7444 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7446 power
= (unsigned int) -1;
7447 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7452 /* Place one common symbol in the correct section. */
7455 lang_one_common (struct bfd_link_hash_entry
*h
, void *info
)
7457 unsigned int power_of_two
;
7461 if (h
->type
!= bfd_link_hash_common
)
7465 power_of_two
= h
->u
.c
.p
->alignment_power
;
7467 if (config
.sort_common
== sort_descending
7468 && power_of_two
< *(unsigned int *) info
)
7470 else if (config
.sort_common
== sort_ascending
7471 && power_of_two
> *(unsigned int *) info
)
7474 section
= h
->u
.c
.p
->section
;
7475 if (!bfd_define_common_symbol (link_info
.output_bfd
, &link_info
, h
))
7476 fatal (_("%P: could not define common symbol `%pT': %E\n"),
7479 if (config
.map_file
!= NULL
)
7481 static bool header_printed
;
7486 if (!header_printed
)
7488 minfo (_("\nAllocating common symbols\n"));
7489 minfo (_("Common symbol size file\n\n"));
7490 header_printed
= true;
7493 name
= bfd_demangle (link_info
.output_bfd
, h
->root
.string
,
7494 DMGL_ANSI
| DMGL_PARAMS
);
7497 minfo ("%s", h
->root
.string
);
7498 len
= strlen (h
->root
.string
);
7503 len
= strlen (name
);
7513 sprintf (buf
, "%" PRIx64
, (uint64_t) size
);
7514 fprintf (config
.map_file
, "%*s0x%-16s", 20 - len
, "", buf
);
7516 minfo ("%pB\n", section
->owner
);
7522 /* Handle a single orphan section S, placing the orphan into an appropriate
7523 output section. The effects of the --orphan-handling command line
7524 option are handled here. */
7527 ldlang_place_orphan (asection
*s
)
7529 if (config
.orphan_handling
== orphan_handling_discard
)
7531 lang_output_section_statement_type
*os
;
7532 os
= lang_output_section_statement_lookup (DISCARD_SECTION_NAME
, 0, 1);
7533 if (os
->addr_tree
== NULL
7534 && (bfd_link_relocatable (&link_info
)
7535 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0))
7536 os
->addr_tree
= exp_intop (0);
7537 lang_add_section (&os
->children
, s
, NULL
, NULL
, os
);
7541 lang_output_section_statement_type
*os
;
7542 const char *name
= s
->name
;
7545 if (config
.orphan_handling
== orphan_handling_error
)
7546 einfo (_("%X%P: error: unplaced orphan section `%pA' from `%pB'\n"),
7549 if (config
.unique_orphan_sections
|| unique_section_p (s
, NULL
))
7550 constraint
= SPECIAL
;
7552 os
= ldemul_place_orphan (s
, name
, constraint
);
7555 os
= lang_output_section_statement_lookup (name
, constraint
, 1);
7556 if (os
->addr_tree
== NULL
7557 && (bfd_link_relocatable (&link_info
)
7558 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0))
7559 os
->addr_tree
= exp_intop (0);
7560 lang_add_section (&os
->children
, s
, NULL
, NULL
, os
);
7563 if (config
.orphan_handling
== orphan_handling_warn
)
7564 einfo (_("%P: warning: orphan section `%pA' from `%pB' being "
7565 "placed in section `%s'\n"),
7566 s
, s
->owner
, os
->name
);
7570 /* Run through the input files and ensure that every input section has
7571 somewhere to go. If one is found without a destination then create
7572 an input request and place it into the statement tree. */
7575 lang_place_orphans (void)
7577 LANG_FOR_EACH_INPUT_STATEMENT (file
)
7581 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7583 if (s
->output_section
== NULL
)
7585 /* This section of the file is not attached, root
7586 around for a sensible place for it to go. */
7588 if (file
->flags
.just_syms
)
7589 bfd_link_just_syms (file
->the_bfd
, s
, &link_info
);
7590 else if (lang_discard_section_p (s
))
7591 s
->output_section
= bfd_abs_section_ptr
;
7592 else if (strcmp (s
->name
, "COMMON") == 0)
7594 /* This is a lonely common section which must have
7595 come from an archive. We attach to the section
7596 with the wildcard. */
7597 if (!bfd_link_relocatable (&link_info
)
7598 || command_line
.force_common_definition
)
7600 if (default_common_section
== NULL
)
7601 default_common_section
7602 = lang_output_section_statement_lookup (".bss", 0, 1);
7603 lang_add_section (&default_common_section
->children
, s
,
7604 NULL
, NULL
, default_common_section
);
7608 ldlang_place_orphan (s
);
7615 lang_set_flags (lang_memory_region_type
*ptr
, const char *flags
, int invert
)
7617 flagword
*ptr_flags
;
7619 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
7625 /* PR 17900: An exclamation mark in the attributes reverses
7626 the sense of any of the attributes that follow. */
7629 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
7633 *ptr_flags
|= SEC_ALLOC
;
7637 *ptr_flags
|= SEC_READONLY
;
7641 *ptr_flags
|= SEC_DATA
;
7645 *ptr_flags
|= SEC_CODE
;
7650 *ptr_flags
|= SEC_LOAD
;
7654 fatal (_("%P: invalid character %c (%d) in flags\n"),
7662 /* Call a function on each real input file. This function will be
7663 called on an archive, but not on the elements. */
7666 lang_for_each_input_file (void (*func
) (lang_input_statement_type
*))
7668 lang_input_statement_type
*f
;
7670 for (f
= (void *) input_file_chain
.head
;
7672 f
= f
->next_real_file
)
7677 /* Call a function on each real file. The function will be called on
7678 all the elements of an archive which are included in the link, but
7679 will not be called on the archive file itself. */
7682 lang_for_each_file (void (*func
) (lang_input_statement_type
*))
7684 LANG_FOR_EACH_INPUT_STATEMENT (f
)
7692 ldlang_add_file (lang_input_statement_type
*entry
)
7694 lang_statement_append (&file_chain
, entry
, &entry
->next
);
7696 /* The BFD linker needs to have a list of all input BFDs involved in
7698 ASSERT (link_info
.input_bfds_tail
!= &entry
->the_bfd
->link
.next
7699 && entry
->the_bfd
->link
.next
== NULL
);
7700 ASSERT (entry
->the_bfd
!= link_info
.output_bfd
);
7702 *link_info
.input_bfds_tail
= entry
->the_bfd
;
7703 link_info
.input_bfds_tail
= &entry
->the_bfd
->link
.next
;
7704 bfd_set_usrdata (entry
->the_bfd
, entry
);
7705 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
7707 /* Look through the sections and check for any which should not be
7708 included in the link. We need to do this now, so that we can
7709 notice when the backend linker tries to report multiple
7710 definition errors for symbols which are in sections we aren't
7711 going to link. FIXME: It might be better to entirely ignore
7712 symbols which are defined in sections which are going to be
7713 discarded. This would require modifying the backend linker for
7714 each backend which might set the SEC_LINK_ONCE flag. If we do
7715 this, we should probably handle SEC_EXCLUDE in the same way. */
7717 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, entry
);
7721 lang_add_output (const char *name
, int from_script
)
7723 /* Make -o on command line override OUTPUT in script. */
7724 if (!had_output_filename
|| !from_script
)
7726 output_filename
= name
;
7727 had_output_filename
= true;
7731 lang_output_section_statement_type
*
7732 lang_enter_output_section_statement (const char *output_section_statement_name
,
7733 etree_type
*address_exp
,
7734 enum section_type sectype
,
7735 etree_type
*sectype_value
,
7737 etree_type
*subalign
,
7740 int align_with_input
)
7742 lang_output_section_statement_type
*os
;
7744 os
= lang_output_section_statement_lookup (output_section_statement_name
,
7746 in_section_ordering
? 0 : 2);
7747 if (os
== NULL
) /* && in_section_ordering */
7748 fatal (_("%P:%pS: error: output section '%s' must already exist\n"),
7749 NULL
, output_section_statement_name
);
7750 current_section
= os
;
7752 /* Make next things chain into subchain of this. */
7753 push_stat_ptr (in_section_ordering
? &os
->sort_children
: &os
->children
);
7755 if (in_section_ordering
)
7758 if (os
->addr_tree
== NULL
)
7759 os
->addr_tree
= address_exp
;
7761 os
->sectype
= sectype
;
7762 if (sectype
== type_section
|| sectype
== typed_readonly_section
)
7763 os
->sectype_value
= sectype_value
;
7764 else if (sectype
== noload_section
)
7765 os
->flags
= SEC_NEVER_LOAD
;
7767 os
->flags
= SEC_NO_FLAGS
;
7768 os
->block_value
= 1;
7770 os
->align_lma_with_input
= align_with_input
== ALIGN_WITH_INPUT
;
7771 if (os
->align_lma_with_input
&& align
!= NULL
)
7772 fatal (_("%P:%pS: error: align with input and explicit align specified\n"),
7775 os
->subsection_alignment
= subalign
;
7776 os
->section_alignment
= align
;
7778 os
->load_base
= ebase
;
7785 lang_output_statement_type
*new_stmt
;
7787 new_stmt
= new_stat (lang_output_statement
, stat_ptr
);
7788 new_stmt
->name
= output_filename
;
7791 /* Reset the current counters in the regions. */
7794 lang_reset_memory_regions (void)
7796 lang_memory_region_type
*p
= lang_memory_region_list
;
7798 lang_output_section_statement_type
*os
;
7800 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
7802 p
->current
= p
->origin
;
7806 for (os
= (void *) lang_os_list
.head
;
7810 os
->processed_vma
= false;
7811 os
->processed_lma
= false;
7814 for (o
= link_info
.output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
7816 /* Save the last size for possible use by bfd_relax_section. */
7817 o
->rawsize
= o
->size
;
7818 if (!(o
->flags
& SEC_FIXED_SIZE
))
7823 /* Worker for lang_gc_sections_1. */
7826 gc_section_callback (lang_wild_statement_type
*ptr
,
7827 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
7829 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
7830 void *data ATTRIBUTE_UNUSED
)
7832 /* If the wild pattern was marked KEEP, the member sections
7833 should be as well. */
7834 if (ptr
->keep_sections
)
7835 section
->flags
|= SEC_KEEP
;
7838 /* Iterate over sections marking them against GC. */
7841 lang_gc_sections_1 (lang_statement_union_type
*s
)
7843 for (; s
!= NULL
; s
= s
->header
.next
)
7845 switch (s
->header
.type
)
7847 case lang_wild_statement_enum
:
7848 walk_wild (&s
->wild_statement
, gc_section_callback
, NULL
);
7850 case lang_constructors_statement_enum
:
7851 lang_gc_sections_1 (constructor_list
.head
);
7853 case lang_output_section_statement_enum
:
7854 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
7856 case lang_group_statement_enum
:
7857 lang_gc_sections_1 (s
->group_statement
.children
.head
);
7866 lang_gc_sections (void)
7868 /* Keep all sections so marked in the link script. */
7869 lang_gc_sections_1 (statement_list
.head
);
7871 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
7872 the special case of .stabstr debug info. (See bfd/stabs.c)
7873 Twiddle the flag here, to simplify later linker code. */
7874 if (bfd_link_relocatable (&link_info
))
7876 LANG_FOR_EACH_INPUT_STATEMENT (f
)
7879 #if BFD_SUPPORTS_PLUGINS
7880 if (f
->flags
.claimed
)
7883 for (sec
= f
->the_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7884 if ((sec
->flags
& SEC_DEBUGGING
) == 0
7885 || strcmp (sec
->name
, ".stabstr") != 0)
7886 sec
->flags
&= ~SEC_EXCLUDE
;
7890 if (link_info
.gc_sections
)
7891 bfd_gc_sections (link_info
.output_bfd
, &link_info
);
7894 /* Worker for lang_find_relro_sections_1. */
7897 find_relro_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
7898 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
7900 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
7903 /* Discarded, excluded and ignored sections effectively have zero
7905 if (section
->output_section
!= NULL
7906 && section
->output_section
->owner
== link_info
.output_bfd
7907 && (section
->output_section
->flags
& SEC_EXCLUDE
) == 0
7908 && !IGNORE_SECTION (section
)
7909 && section
->size
!= 0)
7911 bool *has_relro_section
= (bool *) data
;
7912 *has_relro_section
= true;
7916 /* Iterate over sections for relro sections. */
7919 lang_find_relro_sections_1 (lang_statement_union_type
*s
,
7920 bool *has_relro_section
)
7922 if (*has_relro_section
)
7925 for (; s
!= NULL
; s
= s
->header
.next
)
7927 if (s
== expld
.dataseg
.relro_end_stat
)
7930 switch (s
->header
.type
)
7932 case lang_wild_statement_enum
:
7933 walk_wild (&s
->wild_statement
,
7934 find_relro_section_callback
,
7937 case lang_constructors_statement_enum
:
7938 lang_find_relro_sections_1 (constructor_list
.head
,
7941 case lang_output_section_statement_enum
:
7942 lang_find_relro_sections_1 (s
->output_section_statement
.children
.head
,
7945 case lang_group_statement_enum
:
7946 lang_find_relro_sections_1 (s
->group_statement
.children
.head
,
7956 lang_find_relro_sections (void)
7958 bool has_relro_section
= false;
7960 /* Check all sections in the link script. */
7962 lang_find_relro_sections_1 (expld
.dataseg
.relro_start_stat
,
7963 &has_relro_section
);
7965 if (!has_relro_section
)
7966 link_info
.relro
= false;
7969 /* Relax all sections until bfd_relax_section gives up. */
7972 lang_relax_sections (bool need_layout
)
7974 /* NB: Also enable relaxation to layout sections for DT_RELR. */
7975 if (RELAXATION_ENABLED
|| link_info
.enable_dt_relr
)
7977 /* We may need more than one relaxation pass. */
7978 int i
= link_info
.relax_pass
;
7980 /* The backend can use it to determine the current pass. */
7981 link_info
.relax_pass
= 0;
7985 /* Keep relaxing until bfd_relax_section gives up. */
7988 link_info
.relax_trip
= -1;
7991 link_info
.relax_trip
++;
7993 /* Note: pe-dll.c does something like this also. If you find
7994 you need to change this code, you probably need to change
7995 pe-dll.c also. DJ */
7997 /* Do all the assignments with our current guesses as to
7999 lang_do_assignments (lang_assigning_phase_enum
);
8001 /* We must do this after lang_do_assignments, because it uses
8003 lang_reset_memory_regions ();
8005 /* Perform another relax pass - this time we know where the
8006 globals are, so can make a better guess. */
8007 relax_again
= false;
8008 lang_size_sections (&relax_again
, false);
8010 while (relax_again
);
8012 link_info
.relax_pass
++;
8019 /* Final extra sizing to report errors. */
8020 lang_do_assignments (lang_assigning_phase_enum
);
8021 lang_reset_memory_regions ();
8022 lang_size_sections (NULL
, true);
8026 #if BFD_SUPPORTS_PLUGINS
8027 /* Find the insert point for the plugin's replacement files. We
8028 place them after the first claimed real object file, or if the
8029 first claimed object is an archive member, after the last real
8030 object file immediately preceding the archive. In the event
8031 no objects have been claimed at all, we return the first dummy
8032 object file on the list as the insert point; that works, but
8033 the callee must be careful when relinking the file_chain as it
8034 is not actually on that chain, only the statement_list and the
8035 input_file list; in that case, the replacement files must be
8036 inserted at the head of the file_chain. */
8038 static lang_input_statement_type
*
8039 find_replacements_insert_point (bool *before
)
8041 lang_input_statement_type
*claim1
, *lastobject
;
8042 lastobject
= (void *) input_file_chain
.head
;
8043 for (claim1
= (void *) file_chain
.head
;
8045 claim1
= claim1
->next
)
8047 if (claim1
->flags
.claimed
)
8049 *before
= claim1
->flags
.claim_archive
;
8050 return claim1
->flags
.claim_archive
? lastobject
: claim1
;
8052 /* Update lastobject if this is a real object file. */
8053 if (claim1
->the_bfd
!= NULL
&& claim1
->the_bfd
->my_archive
== NULL
)
8054 lastobject
= claim1
;
8056 /* No files were claimed by the plugin. Choose the last object
8057 file found on the list (maybe the first, dummy entry) as the
8063 /* Find where to insert ADD, an archive element or shared library
8064 added during a rescan. */
8066 static lang_input_statement_type
**
8067 find_rescan_insertion (lang_input_statement_type
*add
)
8069 bfd
*add_bfd
= add
->the_bfd
;
8070 lang_input_statement_type
*f
;
8071 lang_input_statement_type
*last_loaded
= NULL
;
8072 lang_input_statement_type
*before
= NULL
;
8073 lang_input_statement_type
**iter
= NULL
;
8075 if (add_bfd
->my_archive
!= NULL
)
8076 add_bfd
= add_bfd
->my_archive
;
8078 /* First look through the input file chain, to find an object file
8079 before the one we've rescanned. Normal object files always
8080 appear on both the input file chain and the file chain, so this
8081 lets us get quickly to somewhere near the correct place on the
8082 file chain if it is full of archive elements. Archives don't
8083 appear on the file chain, but if an element has been extracted
8084 then their input_statement->next points at it. */
8085 for (f
= (void *) input_file_chain
.head
;
8087 f
= f
->next_real_file
)
8089 if (f
->the_bfd
== add_bfd
)
8091 before
= last_loaded
;
8092 if (f
->next
!= NULL
)
8093 return &f
->next
->next
;
8095 if (f
->the_bfd
!= NULL
&& f
->next
!= NULL
)
8099 for (iter
= before
? &before
->next
: &file_chain
.head
->input_statement
.next
;
8101 iter
= &(*iter
)->next
)
8102 if (!(*iter
)->flags
.claim_archive
8103 && (*iter
)->the_bfd
->my_archive
== NULL
)
8109 /* Detach new nodes added to DESTLIST since the time ORIGLIST
8110 was taken as a copy of it and leave them in ORIGLIST. */
8113 lang_list_remove_tail (lang_statement_list_type
*destlist
,
8114 lang_statement_list_type
*origlist
)
8116 union lang_statement_union
**savetail
;
8117 /* Check that ORIGLIST really is an earlier state of DESTLIST. */
8118 ASSERT (origlist
->head
== destlist
->head
);
8119 savetail
= origlist
->tail
;
8120 origlist
->head
= *(savetail
);
8121 origlist
->tail
= destlist
->tail
;
8122 destlist
->tail
= savetail
;
8126 static lang_statement_union_type
**
8127 find_next_input_statement (lang_statement_union_type
**s
)
8129 for ( ; *s
; s
= &(*s
)->header
.next
)
8131 lang_statement_union_type
**t
;
8132 switch ((*s
)->header
.type
)
8134 case lang_input_statement_enum
:
8136 case lang_wild_statement_enum
:
8137 t
= &(*s
)->wild_statement
.children
.head
;
8139 case lang_group_statement_enum
:
8140 t
= &(*s
)->group_statement
.children
.head
;
8142 case lang_output_section_statement_enum
:
8143 t
= &(*s
)->output_section_statement
.children
.head
;
8148 t
= find_next_input_statement (t
);
8154 #endif /* BFD_SUPPORTS_PLUGINS */
8156 /* Insert SRCLIST into DESTLIST after given element by chaining
8157 on FIELD as the next-pointer. (Counterintuitively does not need
8158 a pointer to the actual after-node itself, just its chain field.) */
8161 lang_list_insert_after (lang_statement_list_type
*destlist
,
8162 lang_statement_list_type
*srclist
,
8163 lang_statement_union_type
**field
)
8165 *(srclist
->tail
) = *field
;
8166 *field
= srclist
->head
;
8167 if (destlist
->tail
== field
)
8168 destlist
->tail
= srclist
->tail
;
8171 /* Add NAME to the list of garbage collection entry points. */
8174 lang_add_gc_name (const char *name
)
8176 struct bfd_sym_chain
*sym
;
8181 sym
= stat_alloc (sizeof (*sym
));
8183 sym
->next
= link_info
.gc_sym_list
;
8185 link_info
.gc_sym_list
= sym
;
8188 /* Check relocations. */
8191 lang_check_relocs (void)
8193 if (link_info
.check_relocs_after_open_input
)
8197 for (abfd
= link_info
.input_bfds
;
8198 abfd
!= (bfd
*) NULL
; abfd
= abfd
->link
.next
)
8199 if (!bfd_link_check_relocs (abfd
, &link_info
))
8201 /* No object output, fail return. */
8202 config
.make_executable
= false;
8203 /* Note: we do not abort the loop, but rather
8204 continue the scan in case there are other
8205 bad relocations to report. */
8210 /* Look through all output sections looking for places where we can
8211 propagate forward the lma region. */
8214 lang_propagate_lma_regions (void)
8216 lang_output_section_statement_type
*os
;
8218 for (os
= (void *) lang_os_list
.head
;
8222 if (os
->prev
!= NULL
8223 && os
->lma_region
== NULL
8224 && os
->load_base
== NULL
8225 && os
->addr_tree
== NULL
8226 && os
->region
== os
->prev
->region
)
8227 os
->lma_region
= os
->prev
->lma_region
;
8232 warn_non_contiguous_discards (void)
8234 LANG_FOR_EACH_INPUT_STATEMENT (file
)
8236 if ((file
->the_bfd
->flags
& (BFD_LINKER_CREATED
| DYNAMIC
)) != 0
8237 || file
->flags
.just_syms
)
8240 for (asection
*s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
8241 if (s
->output_section
== NULL
8242 && (s
->flags
& SEC_LINKER_CREATED
) == 0)
8243 einfo (_("%P: warning: --enable-non-contiguous-regions "
8244 "discards section `%pA' from `%pB'\n"),
8250 reset_one_wild (lang_statement_union_type
*statement
)
8252 if (statement
->header
.type
== lang_wild_statement_enum
)
8254 lang_wild_statement_type
*stmt
= &statement
->wild_statement
;
8255 lang_list_init (&stmt
->matching_sections
);
8260 reset_resolved_wilds (void)
8262 lang_for_each_statement (reset_one_wild
);
8265 /* For each output section statement, splice any entries on the
8266 sort_children list before the first wild statement on the children
8270 lang_os_merge_sort_children (void)
8272 lang_output_section_statement_type
*os
;
8273 for (os
= (void *) lang_os_list
.head
; os
!= NULL
; os
= os
->next
)
8275 if (os
->sort_children
.head
!= NULL
)
8277 lang_statement_union_type
**where
;
8278 for (where
= &os
->children
.head
;
8280 where
= &(*where
)->header
.next
)
8281 if ((*where
)->header
.type
== lang_wild_statement_enum
)
8283 lang_list_insert_after (&os
->children
, &os
->sort_children
, where
);
8291 lang_os_merge_sort_children ();
8293 /* Finalize dynamic list. */
8294 if (link_info
.dynamic_list
)
8295 lang_finalize_version_expr_head (&link_info
.dynamic_list
->head
);
8297 current_target
= default_target
;
8299 /* Open the output file. */
8300 lang_for_each_statement (ldlang_open_output
);
8303 ldemul_create_output_section_statements ();
8305 /* Add to the hash table all undefineds on the command line. */
8306 lang_place_undefineds ();
8308 if (!bfd_section_already_linked_table_init ())
8309 fatal (_("%P: can not create hash table: %E\n"));
8311 /* A first pass through the memory regions ensures that if any region
8312 references a symbol for its origin or length then this symbol will be
8313 added to the symbol table. Having these symbols in the symbol table
8314 means that when we call open_input_bfds PROVIDE statements will
8315 trigger to provide any needed symbols. The regions origins and
8316 lengths are not assigned as a result of this call. */
8317 lang_do_memory_regions (false);
8319 /* Create a bfd for each input file. */
8320 current_target
= default_target
;
8321 lang_statement_iteration
++;
8322 open_input_bfds (statement_list
.head
, NULL
, OPEN_BFD_NORMAL
);
8324 /* Now that open_input_bfds has processed assignments and provide
8325 statements we can give values to symbolic origin/length now. */
8326 lang_do_memory_regions (true);
8328 ldemul_before_plugin_all_symbols_read ();
8330 #if BFD_SUPPORTS_PLUGINS
8331 if (link_info
.lto_plugin_active
)
8333 lang_statement_list_type added
;
8334 lang_statement_list_type files
, inputfiles
;
8336 /* Now all files are read, let the plugin(s) decide if there
8337 are any more to be added to the link before we call the
8338 emulation's after_open hook. We create a private list of
8339 input statements for this purpose, which we will eventually
8340 insert into the global statement list after the first claimed
8343 /* We need to manipulate all three chains in synchrony. */
8345 inputfiles
= input_file_chain
;
8346 if (plugin_call_all_symbols_read ())
8347 fatal (_("%P: %s: plugin reported error after all symbols read\n"),
8348 plugin_error_plugin ());
8349 link_info
.lto_all_symbols_read
= true;
8350 /* Open any newly added files, updating the file chains. */
8351 plugin_undefs
= link_info
.hash
->undefs_tail
;
8352 lang_output_section_statement_type
*last_os
= NULL
;
8353 if (lang_os_list
.head
!= NULL
)
8354 last_os
= ((lang_output_section_statement_type
*)
8355 ((char *) lang_os_list
.tail
8356 - offsetof (lang_output_section_statement_type
, next
)));
8357 open_input_bfds (*added
.tail
, last_os
, OPEN_BFD_NORMAL
);
8358 if (plugin_undefs
== link_info
.hash
->undefs_tail
)
8359 plugin_undefs
= NULL
;
8360 /* Restore the global list pointer now they have all been added. */
8361 lang_list_remove_tail (stat_ptr
, &added
);
8362 /* And detach the fresh ends of the file lists. */
8363 lang_list_remove_tail (&file_chain
, &files
);
8364 lang_list_remove_tail (&input_file_chain
, &inputfiles
);
8365 /* Were any new files added? */
8366 if (added
.head
!= NULL
)
8368 /* If so, we will insert them into the statement list immediately
8369 after the first input file that was claimed by the plugin,
8370 unless that file was an archive in which case it is inserted
8371 immediately before. */
8373 lang_statement_union_type
**prev
;
8374 plugin_insert
= find_replacements_insert_point (&before
);
8375 /* If a plugin adds input files without having claimed any, we
8376 don't really have a good idea where to place them. Just putting
8377 them at the start or end of the list is liable to leave them
8378 outside the crtbegin...crtend range. */
8379 ASSERT (plugin_insert
!= NULL
);
8380 /* Splice the new statement list into the old one. */
8381 prev
= &plugin_insert
->header
.next
;
8384 prev
= find_next_input_statement (prev
);
8385 if (*prev
!= (void *) plugin_insert
->next_real_file
)
8387 /* We didn't find the expected input statement.
8388 Fall back to adding after plugin_insert. */
8389 prev
= &plugin_insert
->header
.next
;
8392 lang_list_insert_after (stat_ptr
, &added
, prev
);
8393 /* Likewise for the file chains. */
8394 lang_list_insert_after (&input_file_chain
, &inputfiles
,
8395 (void *) &plugin_insert
->next_real_file
);
8396 /* We must be careful when relinking file_chain; we may need to
8397 insert the new files at the head of the list if the insert
8398 point chosen is the dummy first input file. */
8399 if (plugin_insert
->filename
)
8400 lang_list_insert_after (&file_chain
, &files
,
8401 (void *) &plugin_insert
->next
);
8403 lang_list_insert_after (&file_chain
, &files
, &file_chain
.head
);
8405 /* Rescan archives in case new undefined symbols have appeared. */
8407 lang_statement_iteration
++;
8408 open_input_bfds (statement_list
.head
, NULL
, OPEN_BFD_RESCAN
);
8409 lang_list_remove_tail (&file_chain
, &files
);
8410 while (files
.head
!= NULL
)
8412 lang_input_statement_type
**insert
;
8413 lang_input_statement_type
**iter
, *temp
;
8416 insert
= find_rescan_insertion (&files
.head
->input_statement
);
8417 /* All elements from an archive can be added at once. */
8418 iter
= &files
.head
->input_statement
.next
;
8419 my_arch
= files
.head
->input_statement
.the_bfd
->my_archive
;
8420 if (my_arch
!= NULL
)
8421 for (; *iter
!= NULL
; iter
= &(*iter
)->next
)
8422 if ((*iter
)->the_bfd
->my_archive
!= my_arch
)
8425 *insert
= &files
.head
->input_statement
;
8426 files
.head
= (lang_statement_union_type
*) *iter
;
8428 if (file_chain
.tail
== (lang_statement_union_type
**) insert
)
8429 file_chain
.tail
= (lang_statement_union_type
**) iter
;
8430 if (my_arch
!= NULL
)
8432 lang_input_statement_type
*parent
= bfd_usrdata (my_arch
);
8434 parent
->next
= (lang_input_statement_type
*)
8436 - offsetof (lang_input_statement_type
, next
));
8442 #endif /* BFD_SUPPORTS_PLUGINS */
8443 if (bfd_link_relocatable (&link_info
))
8445 /* Check if .gnu_object_only section should be created. */
8450 for (p
= link_info
.input_bfds
; p
!= (bfd
*) NULL
; p
= p
->link
.next
)
8452 enum bfd_lto_object_type lto_type
= bfd_get_lto_type (p
);
8453 /* NB: Treat fat IR object as IR object here. */
8454 if (lto_type
== lto_fat_ir_object
)
8455 lto_type
= lto_slim_ir_object
;
8456 object_type
|= 1 << lto_type
;
8457 if ((object_type
& (1 << lto_mixed_object
)) != 0
8459 & (1 << lto_non_ir_object
8460 | 1 << lto_slim_ir_object
))
8461 == (1 << lto_non_ir_object
| 1 << lto_slim_ir_object
)))
8463 config
.emit_gnu_object_only
= true;
8469 && (cmdline_object_only_file_list
.head
8470 || cmdline_object_only_archive_list
.head
))
8472 info_msg (_("Object-only input files:\n "));
8473 print_cmdline_list (cmdline_object_only_file_list
.head
);
8474 print_cmdline_list (cmdline_object_only_archive_list
.head
);
8478 struct bfd_sym_chain
**sym
= &link_info
.gc_sym_list
;
8480 sym
= &(*sym
)->next
;
8482 *sym
= &entry_symbol
;
8484 if (entry_symbol
.name
== NULL
)
8486 *sym
= ldlang_undef_chain_list_head
;
8488 /* entry_symbol is normally initialised by an ENTRY definition in the
8489 linker script or the -e command line option. But if neither of
8490 these have been used, the target specific backend may still have
8491 provided an entry symbol via a call to lang_default_entry().
8492 Unfortunately this value will not be processed until lang_end()
8493 is called, long after this function has finished. So detect this
8494 case here and add the target's entry symbol to the list of starting
8495 points for garbage collection resolution. */
8496 lang_add_gc_name (entry_symbol_default
);
8499 lang_add_gc_name (link_info
.init_function
);
8500 lang_add_gc_name (link_info
.fini_function
);
8502 ldemul_after_open ();
8503 if (config
.map_file
!= NULL
)
8504 lang_print_asneeded ();
8508 bfd_section_already_linked_table_free ();
8510 /* Make sure that we're not mixing architectures. We call this
8511 after all the input files have been opened, but before we do any
8512 other processing, so that any operations merge_private_bfd_data
8513 does on the output file will be known during the rest of the
8517 /* Handle .exports instead of a version script if we're told to do so. */
8518 if (command_line
.version_exports_section
)
8519 lang_do_version_exports_section ();
8521 /* Build all sets based on the information gathered from the input
8523 ldctor_build_sets ();
8525 lang_symbol_tweaks ();
8527 /* PR 13683: We must rerun the assignments prior to running garbage
8528 collection in order to make sure that all symbol aliases are resolved. */
8529 lang_do_assignments (lang_mark_phase_enum
);
8530 expld
.phase
= lang_first_phase_enum
;
8532 /* Size up the common data. */
8536 debug_prefix_tree ();
8540 /* Remove unreferenced sections if asked to. */
8541 lang_gc_sections ();
8543 lang_mark_undefineds ();
8545 /* Check relocations. */
8546 lang_check_relocs ();
8548 ldemul_after_check_relocs ();
8550 /* There might have been new sections created (e.g. as result of
8551 checking relocs to need a .got, or suchlike), so to properly order
8552 them into our lists of matching sections reset them here. */
8553 reset_resolved_wilds ();
8556 /* Update wild statements in case the user gave --sort-section.
8557 Note how the option might have come after the linker script and
8558 so couldn't have been set when the wild statements were created. */
8559 update_wild_statements (statement_list
.head
);
8561 /* Run through the contours of the script and attach input sections
8562 to the correct output sections. */
8563 lang_statement_iteration
++;
8564 map_input_to_output_sections (statement_list
.head
, NULL
, NULL
);
8566 /* Start at the statement immediately after the special abs_section
8567 output statement, so that it isn't reordered. */
8568 process_insert_statements (&lang_os_list
.head
->header
.next
);
8570 ldemul_before_place_orphans ();
8572 /* Find any sections not attached explicitly and handle them. */
8573 lang_place_orphans ();
8575 if (!bfd_link_relocatable (&link_info
))
8579 ld_start_phase (PHASE_MERGE
);
8581 /* Merge SEC_MERGE sections. This has to be done after GC of
8582 sections, so that GCed sections are not merged, but before
8583 assigning dynamic symbols, since removing whole input sections
8585 if (!bfd_merge_sections (link_info
.output_bfd
, &link_info
))
8586 fatal (_("%P: bfd_merge_sections failed: %E\n"));
8588 ld_stop_phase (PHASE_MERGE
);
8590 /* Look for a text section and set the readonly attribute in it. */
8591 found
= bfd_get_section_by_name (link_info
.output_bfd
, ".text");
8595 if (config
.text_read_only
)
8596 found
->flags
|= SEC_READONLY
;
8598 found
->flags
&= ~SEC_READONLY
;
8602 /* Merge together CTF sections. After this, only the symtab-dependent
8603 function and data object sections need adjustment. */
8606 /* Emit the CTF, iff the emulation doesn't need to do late emission after
8607 examining things laid out late, like the strtab. */
8610 /* Copy forward lma regions for output sections in same lma region. */
8611 lang_propagate_lma_regions ();
8613 /* Defining __start/__stop symbols early for --gc-sections to work
8614 around a glibc build problem can result in these symbols being
8615 defined when they should not be. Fix them now. */
8616 if (config
.build_constructors
)
8617 lang_undef_start_stop ();
8619 /* Define .startof./.sizeof. symbols with preliminary values before
8620 dynamic symbols are created. */
8621 if (!bfd_link_relocatable (&link_info
))
8622 lang_init_startof_sizeof ();
8624 /* Do anything special before sizing sections. This is where ELF
8625 and other back-ends size dynamic sections. */
8626 ldemul_before_allocation ();
8628 /* We must record the program headers before we try to fix the
8629 section positions, since they will affect SIZEOF_HEADERS. */
8630 lang_record_phdrs ();
8632 /* Check relro sections. */
8633 if (link_info
.relro
&& !bfd_link_relocatable (&link_info
))
8634 lang_find_relro_sections ();
8636 /* Size up the sections. */
8637 lang_size_sections (NULL
, !RELAXATION_ENABLED
);
8639 /* See if anything special should be done now we know how big
8640 everything is. This is where relaxation is done. */
8641 ldemul_after_allocation ();
8643 /* Fix any __start, __stop, .startof. or .sizeof. symbols. */
8644 lang_finalize_start_stop ();
8646 /* Do all the assignments again, to report errors. Assignment
8647 statements are processed multiple times, updating symbols; In
8648 open_input_bfds, lang_do_assignments, and lang_size_sections.
8649 Since lang_relax_sections calls lang_do_assignments, symbols are
8650 also updated in ldemul_after_allocation. */
8651 lang_do_assignments (lang_final_phase_enum
);
8655 /* Convert absolute symbols to section relative. */
8656 ldexp_finalize_syms ();
8658 /* Make sure that the section addresses make sense. */
8659 if (command_line
.check_section_addresses
)
8660 lang_check_section_addresses ();
8662 if (link_info
.non_contiguous_regions
8663 && link_info
.non_contiguous_regions_warnings
)
8664 warn_non_contiguous_discards ();
8666 /* Check any required symbols are known. */
8667 ldlang_check_require_defined_symbols ();
8673 lang_add_version_string (void)
8675 if (! enable_linker_version
)
8678 const char * str
= "GNU ld ";
8679 int len
= strlen (str
);
8682 for (i
= 0 ; i
< len
; i
++)
8683 lang_add_data (BYTE
, exp_intop (str
[i
]));
8685 str
= BFD_VERSION_STRING
;
8688 for (i
= 0 ; i
< len
; i
++)
8689 lang_add_data (BYTE
, exp_intop (str
[i
]));
8691 lang_add_data (BYTE
, exp_intop ('\0'));
8694 /* EXPORTED TO YACC */
8697 lang_add_wild (struct wildcard_spec
*filespec
,
8698 struct wildcard_list
*section_list
,
8701 struct wildcard_list
*curr
, *next
;
8702 lang_wild_statement_type
*new_stmt
;
8703 bool any_specs_sorted
= false;
8705 /* Reverse the list as the parser puts it back to front. */
8706 for (curr
= section_list
, section_list
= NULL
;
8708 section_list
= curr
, curr
= next
)
8710 if (curr
->spec
.sorted
!= none
&& curr
->spec
.sorted
!= by_none
)
8711 any_specs_sorted
= true;
8713 curr
->next
= section_list
;
8716 if (filespec
!= NULL
&& filespec
->name
!= NULL
)
8718 if (strcmp (filespec
->name
, "*") == 0)
8719 filespec
->name
= NULL
;
8720 else if (!wildcardp (filespec
->name
))
8721 lang_has_input_file
= true;
8724 new_stmt
= new_stat (lang_wild_statement
, stat_ptr
);
8725 new_stmt
->filename
= NULL
;
8726 new_stmt
->filenames_sorted
= false;
8727 new_stmt
->filenames_reversed
= false;
8728 new_stmt
->any_specs_sorted
= any_specs_sorted
;
8729 new_stmt
->section_flag_list
= NULL
;
8730 new_stmt
->exclude_name_list
= NULL
;
8731 if (filespec
!= NULL
)
8733 new_stmt
->filename
= filespec
->name
;
8734 new_stmt
->filenames_sorted
= (filespec
->sorted
== by_name
|| filespec
->reversed
);
8735 new_stmt
->filenames_reversed
= filespec
->reversed
;
8736 new_stmt
->section_flag_list
= filespec
->section_flag_list
;
8737 new_stmt
->exclude_name_list
= filespec
->exclude_name_list
;
8739 new_stmt
->section_list
= section_list
;
8740 new_stmt
->keep_sections
= keep_sections
;
8741 lang_list_init (&new_stmt
->children
);
8742 lang_list_init (&new_stmt
->matching_sections
);
8743 analyze_walk_wild_section_handler (new_stmt
);
8746 printf ("wild %s(", new_stmt
->filename
? new_stmt
->filename
: "*");
8747 for (curr
= new_stmt
->section_list
; curr
; curr
= curr
->next
)
8748 printf ("%s ", curr
->spec
.name
? curr
->spec
.name
: "*");
8754 lang_section_start (const char *name
, etree_type
*address
,
8755 const segment_type
*segment
)
8757 lang_address_statement_type
*ad
;
8759 ad
= new_stat (lang_address_statement
, stat_ptr
);
8760 ad
->section_name
= name
;
8761 ad
->address
= address
;
8762 ad
->segment
= segment
;
8765 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
8766 because of a -e argument on the command line, or zero if this is
8767 called by ENTRY in a linker script. Command line arguments take
8771 lang_add_entry (const char *name
, bool cmdline
)
8773 if (entry_symbol
.name
== NULL
8775 || !entry_from_cmdline
)
8777 entry_symbol
.name
= name
;
8778 entry_from_cmdline
= cmdline
;
8782 /* Set the default start symbol to NAME. .em files should use this,
8783 not lang_add_entry, to override the use of "start" if neither the
8784 linker script nor the command line specifies an entry point. NAME
8785 must be permanently allocated. */
8787 lang_default_entry (const char *name
)
8789 entry_symbol_default
= name
;
8793 lang_add_target (const char *name
)
8795 lang_target_statement_type
*new_stmt
;
8797 new_stmt
= new_stat (lang_target_statement
, stat_ptr
);
8798 new_stmt
->target
= name
;
8802 lang_add_map (const char *name
)
8809 map_option_f
= true;
8817 lang_add_fill (fill_type
*fill
)
8819 lang_fill_statement_type
*new_stmt
;
8821 new_stmt
= new_stat (lang_fill_statement
, stat_ptr
);
8822 new_stmt
->fill
= fill
;
8826 lang_add_data (int type
, union etree_union
*exp
)
8828 lang_data_statement_type
*new_stmt
;
8830 new_stmt
= new_stat (lang_data_statement
, stat_ptr
);
8831 new_stmt
->exp
= exp
;
8832 new_stmt
->type
= type
;
8836 lang_add_string (const char *s
)
8838 bfd_vma len
= strlen (s
);
8840 bool escape
= false;
8842 /* Add byte expressions until end of string. */
8843 for (i
= 0 ; i
< len
; i
++)
8852 /* Ignore the escape. */
8855 case 'n': c
= '\n'; break;
8856 case 'r': c
= '\r'; break;
8857 case 't': c
= '\t'; break;
8867 /* We have an octal number. */
8869 unsigned int value
= c
- '0';
8872 if ((c
>= '0') && (c
<= '7'))
8880 if ((c
>= '0') && (c
<= '7'))
8891 /* octal: \777 is treated as '\077' + '7' */
8902 lang_add_data (BYTE
, exp_intop (c
));
8910 lang_add_data (BYTE
, exp_intop (c
));
8914 /* Remeber to terminate the string. */
8915 lang_add_data (BYTE
, exp_intop (0));
8918 /* Create a new reloc statement. RELOC is the BFD relocation type to
8919 generate. HOWTO is the corresponding howto structure (we could
8920 look this up, but the caller has already done so). SECTION is the
8921 section to generate a reloc against, or NAME is the name of the
8922 symbol to generate a reloc against. Exactly one of SECTION and
8923 NAME must be NULL. ADDEND is an expression for the addend. */
8926 lang_add_reloc (bfd_reloc_code_real_type reloc
,
8927 reloc_howto_type
*howto
,
8930 union etree_union
*addend
)
8932 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
8936 p
->section
= section
;
8938 p
->addend_exp
= addend
;
8940 p
->addend_value
= 0;
8941 p
->output_section
= NULL
;
8942 p
->output_offset
= 0;
8945 lang_assignment_statement_type
*
8946 lang_add_assignment (etree_type
*exp
)
8948 lang_assignment_statement_type
*new_stmt
;
8950 new_stmt
= new_stat (lang_assignment_statement
, stat_ptr
);
8951 new_stmt
->exp
= exp
;
8956 lang_add_attribute (enum statement_enum attribute
)
8958 new_statement (attribute
, sizeof (lang_statement_header_type
), stat_ptr
);
8962 lang_startup (const char *name
)
8964 if (first_file
->filename
!= NULL
)
8965 fatal (_("%P: multiple STARTUP files\n"));
8966 first_file
->filename
= name
;
8967 first_file
->local_sym_name
= name
;
8968 first_file
->flags
.real
= true;
8972 lang_float (bool maybe
)
8974 lang_float_flag
= maybe
;
8978 /* Work out the load- and run-time regions from a script statement, and
8979 store them in *LMA_REGION and *REGION respectively.
8981 MEMSPEC is the name of the run-time region, or the value of
8982 DEFAULT_MEMORY_REGION if the statement didn't specify one.
8983 LMA_MEMSPEC is the name of the load-time region, or null if the
8984 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
8985 had an explicit load address.
8987 It is an error to specify both a load region and a load address. */
8990 lang_get_regions (lang_memory_region_type
**region
,
8991 lang_memory_region_type
**lma_region
,
8992 const char *memspec
,
8993 const char *lma_memspec
,
8997 *lma_region
= lang_memory_region_lookup (lma_memspec
, false);
8999 /* If no runtime region or VMA has been specified, but the load region
9000 has been specified, then use the load region for the runtime region
9002 if (lma_memspec
!= NULL
9004 && strcmp (memspec
, DEFAULT_MEMORY_REGION
) == 0)
9005 *region
= *lma_region
;
9007 *region
= lang_memory_region_lookup (memspec
, false);
9009 if (have_lma
&& lma_memspec
!= 0)
9010 einfo (_("%X%P:%pS: section has both a load address and a load region\n"),
9015 lang_leave_output_section_statement (fill_type
*fill
, const char *memspec
,
9016 lang_output_section_phdr_list
*phdrs
,
9017 const char *lma_memspec
)
9020 if (in_section_ordering
)
9023 lang_get_regions (¤t_section
->region
,
9024 ¤t_section
->lma_region
,
9025 memspec
, lma_memspec
,
9026 current_section
->load_base
!= NULL
,
9027 current_section
->addr_tree
!= NULL
);
9029 current_section
->fill
= fill
;
9030 current_section
->phdrs
= phdrs
;
9033 /* Set the output format type. -oformat overrides scripts. */
9036 lang_add_output_format (const char *format
,
9041 if (output_target
== NULL
|| !from_script
)
9043 if (command_line
.endian
== ENDIAN_BIG
9046 else if (command_line
.endian
== ENDIAN_LITTLE
9050 output_target
= format
;
9055 lang_add_insert (const char *where
, int is_before
)
9057 lang_insert_statement_type
*new_stmt
;
9059 new_stmt
= new_stat (lang_insert_statement
, stat_ptr
);
9060 new_stmt
->where
= where
;
9061 new_stmt
->is_before
= is_before
;
9062 saved_script_handle
= previous_script_handle
;
9065 /* Enter a group. This creates a new lang_group_statement, and sets
9066 stat_ptr to build new statements within the group. */
9069 lang_enter_group (void)
9071 lang_group_statement_type
*g
;
9073 g
= new_stat (lang_group_statement
, stat_ptr
);
9074 lang_list_init (&g
->children
);
9075 push_stat_ptr (&g
->children
);
9078 /* Leave a group. This just resets stat_ptr to start writing to the
9079 regular list of statements again. Note that this will not work if
9080 groups can occur inside anything else which can adjust stat_ptr,
9081 but currently they can't. */
9084 lang_leave_group (void)
9089 /* Add a new program header. This is called for each entry in a PHDRS
9090 command in a linker script. */
9093 lang_new_phdr (const char *name
,
9100 struct lang_phdr
*n
, **pp
;
9103 n
= stat_alloc (sizeof (struct lang_phdr
));
9106 n
->type
= exp_get_vma (type
, NULL
, 0, "program header type");
9107 n
->filehdr
= filehdr
;
9112 hdrs
= n
->type
== 1 && (phdrs
|| filehdr
);
9114 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
9117 && !((*pp
)->filehdr
|| (*pp
)->phdrs
))
9119 einfo (_("%X%P:%pS: PHDRS and FILEHDR are not supported"
9120 " when prior PT_LOAD headers lack them\n"), NULL
);
9127 /* Record the program header information in the output BFD. FIXME: We
9128 should not be calling an ELF specific function here. */
9131 lang_record_phdrs (void)
9135 lang_output_section_phdr_list
*last
;
9136 struct lang_phdr
*l
;
9137 lang_output_section_statement_type
*os
;
9140 secs
= (asection
**) xmalloc (alc
* sizeof (asection
*));
9143 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
9150 for (os
= (void *) lang_os_list
.head
;
9154 lang_output_section_phdr_list
*pl
;
9156 if (os
->constraint
< 0)
9164 if (os
->sectype
== noload_section
9165 || os
->bfd_section
== NULL
9166 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
9169 /* Don't add orphans to PT_INTERP header. */
9170 if (l
->type
== PT_INTERP
)
9175 lang_output_section_statement_type
*tmp_os
;
9177 /* If we have not run across a section with a program
9178 header assigned to it yet, then scan forwards to find
9179 one. This prevents inconsistencies in the linker's
9180 behaviour when a script has specified just a single
9181 header and there are sections in that script which are
9182 not assigned to it, and which occur before the first
9183 use of that header. See here for more details:
9184 http://sourceware.org/ml/binutils/2007-02/msg00291.html */
9185 for (tmp_os
= os
; tmp_os
; tmp_os
= tmp_os
->next
)
9188 last
= tmp_os
->phdrs
;
9192 fatal (_("%P: no sections assigned to phdrs\n"));
9197 if (os
->bfd_section
== NULL
)
9200 for (; pl
!= NULL
; pl
= pl
->next
)
9202 if (strcmp (pl
->name
, l
->name
) == 0)
9207 secs
= (asection
**) xrealloc (secs
,
9208 alc
* sizeof (asection
*));
9210 secs
[c
] = os
->bfd_section
;
9217 if (l
->flags
== NULL
)
9220 flags
= exp_get_vma (l
->flags
, NULL
, 0, "phdr flags");
9225 at
= exp_get_vma (l
->at
, NULL
, 0, "phdr load address");
9227 if (!bfd_record_phdr (link_info
.output_bfd
, l
->type
,
9228 l
->flags
!= NULL
, flags
, l
->at
!= NULL
,
9229 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
9230 fatal (_("%P: bfd_record_phdr failed: %E\n"));
9235 /* Make sure all the phdr assignments succeeded. */
9236 for (os
= (void *) lang_os_list
.head
;
9240 lang_output_section_phdr_list
*pl
;
9242 if (os
->constraint
< 0
9243 || os
->bfd_section
== NULL
)
9246 for (pl
= os
->phdrs
;
9249 if (!pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
9250 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
9251 os
->name
, pl
->name
);
9255 /* Record a list of sections which may not be cross referenced. */
9258 lang_add_nocrossref (lang_nocrossref_type
*l
)
9260 struct lang_nocrossrefs
*n
;
9262 n
= stat_alloc (sizeof *n
);
9263 n
->next
= nocrossref_list
;
9265 n
->onlyfirst
= false;
9266 nocrossref_list
= n
;
9268 /* Set notice_all so that we get informed about all symbols. */
9269 link_info
.notice_all
= true;
9272 /* Record a section that cannot be referenced from a list of sections. */
9275 lang_add_nocrossref_to (lang_nocrossref_type
*l
)
9277 lang_add_nocrossref (l
);
9278 nocrossref_list
->onlyfirst
= true;
9281 /* Overlay handling. We handle overlays with some static variables. */
9283 /* The overlay virtual address. */
9284 static etree_type
*overlay_vma
;
9285 /* And subsection alignment. */
9286 static etree_type
*overlay_subalign
;
9288 /* An expression for the maximum section size seen so far. */
9289 static etree_type
*overlay_max
;
9291 /* A list of all the sections in this overlay. */
9293 struct overlay_list
{
9294 struct overlay_list
*next
;
9295 lang_output_section_statement_type
*os
;
9298 static struct overlay_list
*overlay_list
;
9300 /* Start handling an overlay. */
9303 lang_enter_overlay (etree_type
*vma_expr
, etree_type
*subalign
)
9305 /* The grammar should prevent nested overlays from occurring. */
9306 ASSERT (overlay_vma
== NULL
9307 && overlay_subalign
== NULL
9308 && overlay_max
== NULL
);
9310 overlay_vma
= vma_expr
;
9311 overlay_subalign
= subalign
;
9314 /* Start a section in an overlay. We handle this by calling
9315 lang_enter_output_section_statement with the correct VMA.
9316 lang_leave_overlay sets up the LMA and memory regions. */
9319 lang_enter_overlay_section (const char *name
)
9321 struct overlay_list
*n
;
9324 lang_enter_output_section_statement (name
, overlay_vma
, overlay_section
,
9325 0, 0, overlay_subalign
, 0, 0, 0);
9327 /* If this is the first section, then base the VMA of future
9328 sections on this one. This will work correctly even if `.' is
9329 used in the addresses. */
9330 if (overlay_list
== NULL
)
9331 overlay_vma
= exp_nameop (ADDR
, name
);
9333 /* Remember the section. */
9334 n
= (struct overlay_list
*) xmalloc (sizeof *n
);
9335 n
->os
= current_section
;
9336 n
->next
= overlay_list
;
9339 size
= exp_nameop (SIZEOF
, name
);
9341 /* Arrange to work out the maximum section end address. */
9342 if (overlay_max
== NULL
)
9345 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
9348 /* Finish a section in an overlay. There isn't any special to do
9352 lang_leave_overlay_section (fill_type
*fill
,
9353 lang_output_section_phdr_list
*phdrs
)
9355 const char *name
= current_section
->name
;;
9357 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
9358 region and that no load-time region has been specified. It doesn't
9359 really matter what we say here, since lang_leave_overlay will
9361 lang_leave_output_section_statement (fill
, DEFAULT_MEMORY_REGION
, phdrs
, 0);
9363 /* Define the magic symbols. */
9365 char *clean
= xmalloc (strlen (name
) + 1);
9367 for (const char *s1
= name
; *s1
!= '\0'; s1
++)
9368 if (ISALNUM (*s1
) || *s1
== '_')
9372 char *buf
= xasprintf ("__load_start_%s", clean
);
9373 lang_add_assignment (exp_provide (buf
,
9374 exp_nameop (LOADADDR
, name
),
9377 buf
= xasprintf ("__load_stop_%s", clean
);
9378 lang_add_assignment (exp_provide (buf
,
9380 exp_nameop (LOADADDR
, name
),
9381 exp_nameop (SIZEOF
, name
)),
9387 /* Finish an overlay. If there are any overlay wide settings, this
9388 looks through all the sections in the overlay and sets them. */
9391 lang_leave_overlay (etree_type
*lma_expr
,
9394 const char *memspec
,
9395 lang_output_section_phdr_list
*phdrs
,
9396 const char *lma_memspec
)
9398 lang_memory_region_type
*region
;
9399 lang_memory_region_type
*lma_region
;
9400 struct overlay_list
*l
;
9401 lang_nocrossref_type
*nocrossref
;
9403 lang_get_regions (®ion
, &lma_region
,
9404 memspec
, lma_memspec
,
9405 lma_expr
!= NULL
, false);
9409 /* After setting the size of the last section, set '.' to end of the
9411 if (overlay_list
!= NULL
)
9413 overlay_list
->os
->update_dot
= 1;
9414 overlay_list
->os
->update_dot_tree
9415 = exp_assign (".", exp_binop ('+', overlay_vma
, overlay_max
), false);
9421 struct overlay_list
*next
;
9423 if (fill
!= NULL
&& l
->os
->fill
== NULL
)
9426 l
->os
->region
= region
;
9427 l
->os
->lma_region
= lma_region
;
9429 /* The first section has the load address specified in the
9430 OVERLAY statement. The rest are worked out from that.
9431 The base address is not needed (and should be null) if
9432 an LMA region was specified. */
9435 l
->os
->load_base
= lma_expr
;
9436 l
->os
->sectype
= first_overlay_section
;
9438 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
9439 l
->os
->phdrs
= phdrs
;
9443 lang_nocrossref_type
*nc
;
9445 nc
= stat_alloc (sizeof *nc
);
9446 nc
->name
= l
->os
->name
;
9447 nc
->next
= nocrossref
;
9456 if (nocrossref
!= NULL
)
9457 lang_add_nocrossref (nocrossref
);
9460 overlay_list
= NULL
;
9462 overlay_subalign
= NULL
;
9465 /* Version handling. This is only useful for ELF. */
9467 /* If PREV is NULL, return first version pattern matching particular symbol.
9468 If PREV is non-NULL, return first version pattern matching particular
9469 symbol after PREV (previously returned by lang_vers_match). */
9471 static struct bfd_elf_version_expr
*
9472 lang_vers_match (struct bfd_elf_version_expr_head
*head
,
9473 struct bfd_elf_version_expr
*prev
,
9477 const char *cxx_sym
= sym
;
9478 const char *java_sym
= sym
;
9479 struct bfd_elf_version_expr
*expr
= NULL
;
9480 enum demangling_styles curr_style
;
9482 curr_style
= CURRENT_DEMANGLING_STYLE
;
9483 cplus_demangle_set_style (no_demangling
);
9484 c_sym
= bfd_demangle (link_info
.output_bfd
, sym
, DMGL_NO_OPTS
);
9487 cplus_demangle_set_style (curr_style
);
9489 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
9491 cxx_sym
= bfd_demangle (link_info
.output_bfd
, sym
,
9492 DMGL_PARAMS
| DMGL_ANSI
);
9496 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
9498 java_sym
= bfd_demangle (link_info
.output_bfd
, sym
, DMGL_JAVA
);
9503 if (head
->htab
&& (prev
== NULL
|| prev
->literal
))
9505 struct bfd_elf_version_expr e
;
9507 switch (prev
? prev
->mask
: 0)
9510 if (head
->mask
& BFD_ELF_VERSION_C_TYPE
)
9513 expr
= (struct bfd_elf_version_expr
*)
9514 htab_find ((htab_t
) head
->htab
, &e
);
9515 while (expr
&& strcmp (expr
->pattern
, c_sym
) == 0)
9516 if (expr
->mask
== BFD_ELF_VERSION_C_TYPE
)
9522 case BFD_ELF_VERSION_C_TYPE
:
9523 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
9525 e
.pattern
= cxx_sym
;
9526 expr
= (struct bfd_elf_version_expr
*)
9527 htab_find ((htab_t
) head
->htab
, &e
);
9528 while (expr
&& strcmp (expr
->pattern
, cxx_sym
) == 0)
9529 if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
9535 case BFD_ELF_VERSION_CXX_TYPE
:
9536 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
9538 e
.pattern
= java_sym
;
9539 expr
= (struct bfd_elf_version_expr
*)
9540 htab_find ((htab_t
) head
->htab
, &e
);
9541 while (expr
&& strcmp (expr
->pattern
, java_sym
) == 0)
9542 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
9553 /* Finally, try the wildcards. */
9554 if (prev
== NULL
|| prev
->literal
)
9555 expr
= head
->remaining
;
9558 for (; expr
; expr
= expr
->next
)
9565 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
9568 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
9570 else if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
9574 if (fnmatch (expr
->pattern
, s
, 0) == 0)
9580 free ((char *) c_sym
);
9582 free ((char *) cxx_sym
);
9583 if (java_sym
!= sym
)
9584 free ((char *) java_sym
);
9588 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
9589 return a pointer to the symbol name with any backslash quotes removed. */
9592 realsymbol (const char *pattern
)
9595 bool changed
= false, backslash
= false;
9596 char *s
, *symbol
= (char *) xmalloc (strlen (pattern
) + 1);
9598 for (p
= pattern
, s
= symbol
; *p
!= '\0'; ++p
)
9600 /* It is a glob pattern only if there is no preceding
9604 /* Remove the preceding backslash. */
9611 if (*p
== '?' || *p
== '*' || *p
== '[')
9618 backslash
= *p
== '\\';
9625 pattern
= stat_strdup (symbol
);
9631 /* This is called for each variable name or match expression. NEW_NAME is
9632 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
9633 pattern to be matched against symbol names. */
9635 struct bfd_elf_version_expr
*
9636 lang_new_vers_pattern (struct bfd_elf_version_expr
*orig
,
9637 const char *new_name
,
9641 struct bfd_elf_version_expr
*ret
;
9643 ret
= stat_alloc (sizeof *ret
);
9647 ret
->literal
= true;
9648 ret
->pattern
= literal_p
? new_name
: realsymbol (new_name
);
9649 if (ret
->pattern
== NULL
)
9651 ret
->pattern
= new_name
;
9652 ret
->literal
= false;
9655 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
9656 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
9657 else if (strcasecmp (lang
, "C++") == 0)
9658 ret
->mask
= BFD_ELF_VERSION_CXX_TYPE
;
9659 else if (strcasecmp (lang
, "Java") == 0)
9660 ret
->mask
= BFD_ELF_VERSION_JAVA_TYPE
;
9663 einfo (_("%X%P: unknown language `%s' in version information\n"),
9665 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
9668 return ldemul_new_vers_pattern (ret
);
9671 /* This is called for each set of variable names and match
9674 struct bfd_elf_version_tree
*
9675 lang_new_vers_node (struct bfd_elf_version_expr
*globals
,
9676 struct bfd_elf_version_expr
*locals
)
9678 struct bfd_elf_version_tree
*ret
;
9680 ret
= stat_alloc (sizeof (*ret
));
9681 memset (ret
, 0, sizeof (*ret
));
9682 ret
->globals
.list
= globals
;
9683 ret
->locals
.list
= locals
;
9684 ret
->match
= lang_vers_match
;
9685 ret
->name_indx
= (unsigned int) -1;
9689 /* This static variable keeps track of version indices. */
9691 static int version_index
;
9694 version_expr_head_hash (const void *p
)
9696 const struct bfd_elf_version_expr
*e
=
9697 (const struct bfd_elf_version_expr
*) p
;
9699 return htab_hash_string (e
->pattern
);
9703 version_expr_head_eq (const void *p1
, const void *p2
)
9705 const struct bfd_elf_version_expr
*e1
=
9706 (const struct bfd_elf_version_expr
*) p1
;
9707 const struct bfd_elf_version_expr
*e2
=
9708 (const struct bfd_elf_version_expr
*) p2
;
9710 return strcmp (e1
->pattern
, e2
->pattern
) == 0;
9714 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head
*head
)
9717 struct bfd_elf_version_expr
*e
, *next
;
9718 struct bfd_elf_version_expr
**list_loc
, **remaining_loc
;
9720 for (e
= head
->list
; e
; e
= e
->next
)
9724 head
->mask
|= e
->mask
;
9729 head
->htab
= htab_create (count
* 2, version_expr_head_hash
,
9730 version_expr_head_eq
, NULL
);
9731 list_loc
= &head
->list
;
9732 remaining_loc
= &head
->remaining
;
9733 for (e
= head
->list
; e
; e
= next
)
9739 remaining_loc
= &e
->next
;
9743 void **loc
= htab_find_slot ((htab_t
) head
->htab
, e
, INSERT
);
9747 struct bfd_elf_version_expr
*e1
, *last
;
9749 e1
= (struct bfd_elf_version_expr
*) *loc
;
9753 if (e1
->mask
== e
->mask
)
9761 while (e1
&& strcmp (e1
->pattern
, e
->pattern
) == 0);
9765 e
->next
= last
->next
;
9773 list_loc
= &e
->next
;
9777 *remaining_loc
= NULL
;
9778 *list_loc
= head
->remaining
;
9781 head
->remaining
= head
->list
;
9784 /* This is called when we know the name and dependencies of the
9788 lang_register_vers_node (const char *name
,
9789 struct bfd_elf_version_tree
*version
,
9790 struct bfd_elf_version_deps
*deps
)
9792 struct bfd_elf_version_tree
*t
, **pp
;
9793 struct bfd_elf_version_expr
*e1
;
9798 if (link_info
.version_info
!= NULL
9799 && (name
[0] == '\0' || link_info
.version_info
->name
[0] == '\0'))
9801 einfo (_("%X%P: anonymous version tag cannot be combined"
9802 " with other version tags\n"));
9806 /* Make sure this node has a unique name. */
9807 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9808 if (strcmp (t
->name
, name
) == 0)
9809 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
9811 lang_finalize_version_expr_head (&version
->globals
);
9812 lang_finalize_version_expr_head (&version
->locals
);
9814 /* Check the global and local match names, and make sure there
9815 aren't any duplicates. */
9817 for (e1
= version
->globals
.list
; e1
!= NULL
; e1
= e1
->next
)
9819 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9821 struct bfd_elf_version_expr
*e2
;
9823 if (t
->locals
.htab
&& e1
->literal
)
9825 e2
= (struct bfd_elf_version_expr
*)
9826 htab_find ((htab_t
) t
->locals
.htab
, e1
);
9827 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
9829 if (e1
->mask
== e2
->mask
)
9830 einfo (_("%X%P: duplicate expression `%s'"
9831 " in version information\n"), e1
->pattern
);
9835 else if (!e1
->literal
)
9836 for (e2
= t
->locals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
9837 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
9838 && e1
->mask
== e2
->mask
)
9839 einfo (_("%X%P: duplicate expression `%s'"
9840 " in version information\n"), e1
->pattern
);
9844 for (e1
= version
->locals
.list
; e1
!= NULL
; e1
= e1
->next
)
9846 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9848 struct bfd_elf_version_expr
*e2
;
9850 if (t
->globals
.htab
&& e1
->literal
)
9852 e2
= (struct bfd_elf_version_expr
*)
9853 htab_find ((htab_t
) t
->globals
.htab
, e1
);
9854 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
9856 if (e1
->mask
== e2
->mask
)
9857 einfo (_("%X%P: duplicate expression `%s'"
9858 " in version information\n"),
9863 else if (!e1
->literal
)
9864 for (e2
= t
->globals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
9865 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
9866 && e1
->mask
== e2
->mask
)
9867 einfo (_("%X%P: duplicate expression `%s'"
9868 " in version information\n"), e1
->pattern
);
9872 version
->deps
= deps
;
9873 version
->name
= name
;
9874 if (name
[0] != '\0')
9877 version
->vernum
= version_index
;
9880 version
->vernum
= 0;
9882 for (pp
= &link_info
.version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
9887 /* This is called when we see a version dependency. */
9889 struct bfd_elf_version_deps
*
9890 lang_add_vers_depend (struct bfd_elf_version_deps
*list
, const char *name
)
9892 struct bfd_elf_version_deps
*ret
;
9893 struct bfd_elf_version_tree
*t
;
9895 ret
= stat_alloc (sizeof *ret
);
9898 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9900 if (strcmp (t
->name
, name
) == 0)
9902 ret
->version_needed
= t
;
9907 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
9909 ret
->version_needed
= NULL
;
9914 lang_do_version_exports_section (void)
9916 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
9918 LANG_FOR_EACH_INPUT_STATEMENT (is
)
9920 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
9928 contents
= stat_alloc (len
);
9929 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
9930 einfo (_("%X%P: unable to read .exports section contents\n"), sec
);
9933 while (p
< contents
+ len
)
9935 greg
= lang_new_vers_pattern (greg
, p
, NULL
, false);
9936 p
= strchr (p
, '\0') + 1;
9939 /* Do not include this section in the link. */
9940 sec
->flags
|= SEC_EXCLUDE
| SEC_KEEP
;
9943 lreg
= lang_new_vers_pattern (NULL
, "*", NULL
, false);
9944 lang_register_vers_node (command_line
.version_exports_section
,
9945 lang_new_vers_node (greg
, lreg
), NULL
);
9948 /* Evaluate LENGTH and ORIGIN parts of MEMORY spec. This is initially
9949 called with UPDATE_REGIONS_P set to FALSE, in this case no errors are
9950 thrown, however, references to symbols in the origin and length fields
9951 will be pushed into the symbol table, this allows PROVIDE statements to
9952 then provide these symbols. This function is called a second time with
9953 UPDATE_REGIONS_P set to TRUE, this time the we update the actual region
9954 data structures, and throw errors if missing symbols are encountered. */
9957 lang_do_memory_regions (bool update_regions_p
)
9959 lang_memory_region_type
*r
= lang_memory_region_list
;
9961 for (; r
!= NULL
; r
= r
->next
)
9965 exp_fold_tree_no_dot (r
->origin_exp
, NULL
);
9966 if (update_regions_p
)
9968 if (expld
.result
.valid_p
)
9970 r
->origin
= expld
.result
.value
;
9971 r
->current
= r
->origin
;
9974 einfo (_("%P: invalid origin for memory region %s\n"),
9980 exp_fold_tree_no_dot (r
->length_exp
, NULL
);
9981 if (update_regions_p
)
9983 if (expld
.result
.valid_p
)
9984 r
->length
= expld
.result
.value
;
9986 einfo (_("%P: invalid length for memory region %s\n"),
9994 lang_add_unique (const char *name
)
9996 struct unique_sections
*ent
;
9998 for (ent
= unique_section_list
; ent
; ent
= ent
->next
)
9999 if (strcmp (ent
->name
, name
) == 0)
10002 ent
= stat_alloc (sizeof *ent
);
10003 ent
->name
= stat_strdup (name
);
10004 ent
->next
= unique_section_list
;
10005 unique_section_list
= ent
;
10008 /* Append the list of dynamic symbols to the existing one. */
10011 lang_append_dynamic_list (struct bfd_elf_dynamic_list
**list_p
,
10012 struct bfd_elf_version_expr
*dynamic
)
10016 struct bfd_elf_version_expr
*tail
;
10017 for (tail
= dynamic
; tail
->next
!= NULL
; tail
= tail
->next
)
10019 tail
->next
= (*list_p
)->head
.list
;
10020 (*list_p
)->head
.list
= dynamic
;
10024 struct bfd_elf_dynamic_list
*d
;
10026 d
= stat_alloc (sizeof (*d
));
10027 memset (d
, 0, sizeof (*d
));
10028 d
->head
.list
= dynamic
;
10029 d
->match
= lang_vers_match
;
10034 /* Append the list of C++ typeinfo dynamic symbols to the existing
10038 lang_append_dynamic_list_cpp_typeinfo (void)
10040 const char *symbols
[] =
10042 "typeinfo name for*",
10045 struct bfd_elf_version_expr
*dynamic
= NULL
;
10048 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
10049 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
10052 lang_append_dynamic_list (&link_info
.dynamic_list
, dynamic
);
10055 /* Append the list of C++ operator new and delete dynamic symbols to the
10059 lang_append_dynamic_list_cpp_new (void)
10061 const char *symbols
[] =
10066 struct bfd_elf_version_expr
*dynamic
= NULL
;
10069 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
10070 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
10073 lang_append_dynamic_list (&link_info
.dynamic_list
, dynamic
);
10076 /* Scan a space and/or comma separated string of features. */
10079 lang_ld_feature (char *str
)
10087 while (*p
== ',' || ISSPACE (*p
))
10092 while (*q
&& *q
!= ',' && !ISSPACE (*q
))
10096 if (strcasecmp (p
, "SANE_EXPR") == 0)
10097 config
.sane_expr
= true;
10099 einfo (_("%X%P: unknown feature `%s'\n"), p
);
10105 /* Pretty print memory amount. */
10108 lang_print_memory_size (uint64_t sz
)
10111 printf (" %10" PRIu64
" B", sz
);
10112 else if ((sz
& 0x3fffffff) == 0)
10113 printf ("%10" PRIu64
" GB", sz
>> 30);
10114 else if ((sz
& 0xfffff) == 0)
10115 printf ("%10" PRIu64
" MB", sz
>> 20);
10116 else if ((sz
& 0x3ff) == 0)
10117 printf ("%10" PRIu64
" KB", sz
>> 10);
10119 printf (" %10" PRIu64
" B", sz
);
10122 /* Implement --print-memory-usage: disply per region memory usage. */
10125 lang_print_memory_usage (void)
10127 lang_memory_region_type
*r
;
10129 printf ("Memory region Used Size Region Size %%age Used\n");
10130 for (r
= lang_memory_region_list
; r
->next
!= NULL
; r
= r
->next
)
10132 bfd_vma used_length
= r
->current
- r
->origin
;
10134 printf ("%16s: ",r
->name_list
.name
);
10135 lang_print_memory_size (used_length
);
10136 lang_print_memory_size (r
->length
);
10138 if (r
->length
!= 0)
10140 double percent
= used_length
* 100.0 / r
->length
;
10141 printf (" %6.2f%%", percent
);
10148 cmdline_lists_init (void)
10150 cmdline_object_only_file_list
.tail
10151 = &cmdline_object_only_file_list
.head
;
10152 cmdline_object_only_archive_list
.tail
10153 = &cmdline_object_only_archive_list
.head
;
10154 cmdline_temp_object_only_list
.tail
10155 = &cmdline_temp_object_only_list
.head
;
10158 /* Allocate an item with TYPE and DATA. */
10160 static cmdline_union_type
*
10161 cmdline_list_new (cmdline_enum_type type
, void *data
)
10163 cmdline_union_type
*new_opt
;
10165 new_opt
= (cmdline_union_type
*) stat_alloc (sizeof (*new_opt
));
10166 new_opt
->header
.type
= type
;
10171 case cmdline_is_file_enum
:
10172 new_opt
->file
.filename
= (const char *) data
;
10174 case cmdline_is_bfd_enum
:
10175 new_opt
->abfd
.abfd
= (bfd
*) data
;
10181 /* Append an item with TYPE and DATA to LIST. */
10184 cmdline_list_append (cmdline_list_type
*list
, cmdline_enum_type type
,
10187 cmdline_union_type
*new_opt
= cmdline_list_new (type
, data
);
10188 new_opt
->header
.next
= NULL
;
10189 *list
->tail
= new_opt
;
10190 list
->tail
= &new_opt
->header
.next
;
10194 print_cmdline_list (cmdline_union_type
*c
)
10196 for (; c
!= NULL
; c
= c
->header
.next
)
10197 switch (c
->header
.type
)
10201 case cmdline_is_file_enum
:
10202 info_msg (" %s", c
->file
.filename
);
10204 case cmdline_is_bfd_enum
:
10205 info_msg (" [%B]", c
->abfd
.abfd
);
10212 /* Return TRUE if ABFD is on cmdline_object_only_archive_list. */
10215 cmdline_on_object_only_archive_list_p (bfd
*abfd
)
10217 cmdline_union_type
*c
, *next
;
10218 bfd
*archive
, *obfd
, *oarchive
;
10219 ufile_ptr origin
= abfd
->origin
;
10221 archive
= abfd
->my_archive
;
10222 for (c
= cmdline_object_only_archive_list
.head
; c
!= NULL
; c
= next
)
10224 if (c
->header
.type
!= cmdline_is_bfd_enum
)
10227 next
= c
->header
.next
;
10228 obfd
= c
->abfd
.abfd
;
10229 oarchive
= obfd
->my_archive
;
10231 /* The list is grouped by archive file name and sorted by member
10233 if (strcmp (archive
->filename
, oarchive
->filename
) != 0)
10236 if (origin
== obfd
->origin
)
10238 else if (origin
< obfd
->origin
)
10245 /* Append an item with TYPE and DATA to cmdline_object_only_file_list
10246 or cmdline_object_only_archive_list if needed. */
10249 cmdline_object_only_list_append (cmdline_enum_type type
, void *data
)
10251 cmdline_union_type
*c
;
10252 cmdline_union_type
*new_opt
, *next
, **prev
;
10253 bfd
*abfd
, *archive
;
10254 bfd
*obfd
, *oarchive
;
10255 bfd
*nbfd
, *narchive
;
10256 ufile_ptr origin
, norigin
;
10258 /* Put it on cmdline_object_only_file_list if it isn't an archive
10264 case cmdline_is_bfd_enum
:
10265 abfd
= (bfd
*) data
;
10266 archive
= abfd
->my_archive
;
10270 case cmdline_is_file_enum
:
10271 cmdline_list_append (&cmdline_object_only_file_list
, type
, data
);
10275 /* Put archive member on cmdline_object_only_archive_list and sort
10276 the list by archive name and archive member origin. */
10277 new_opt
= (cmdline_union_type
*) stat_alloc (sizeof (*new_opt
));
10278 new_opt
->header
.type
= cmdline_is_bfd_enum
;
10279 new_opt
->header
.next
= NULL
;
10280 new_opt
->abfd
.abfd
= (bfd
*) data
;
10282 c
= cmdline_object_only_archive_list
.head
;
10285 cmdline_object_only_archive_list
.head
= new_opt
;
10286 cmdline_object_only_archive_list
.tail
= &new_opt
->header
.next
;
10291 origin
= abfd
->origin
;
10292 for (; c
!= NULL
; c
= next
)
10294 if (c
->header
.type
!= cmdline_is_bfd_enum
)
10297 next
= c
->header
.next
;
10299 obfd
= c
->abfd
.abfd
;
10300 oarchive
= obfd
->my_archive
;
10302 if (strcmp (archive
->filename
, oarchive
->filename
) == 0)
10306 if (origin
< obfd
->origin
)
10308 /* Insert it before the current. */
10309 new_opt
->header
.next
= c
;
10313 cmdline_object_only_archive_list
.head
= new_opt
;
10319 /* Check origin. */
10322 if (next
->header
.type
!= cmdline_is_bfd_enum
)
10325 nbfd
= next
->abfd
.abfd
;
10326 norigin
= nbfd
->origin
;
10327 if (origin
> norigin
)
10329 /* Insert it after NEXT. */
10333 narchive
= nbfd
->my_archive
;
10334 if (strcmp (archive
->filename
, narchive
->filename
) != 0)
10336 /* Insert it befor NEXT. */
10342 next
= next
->header
.next
;
10348 next
= next
->header
.next
;
10351 if (*cmdline_object_only_archive_list
.tail
== c
->header
.next
)
10352 cmdline_object_only_archive_list
.tail
10353 = &new_opt
->header
.next
;
10355 prev
= &c
->header
.next
;
10356 new_opt
->header
.next
= next
;
10361 prev
= &c
->header
.next
;
10364 *cmdline_object_only_archive_list
.tail
= new_opt
;
10365 cmdline_object_only_archive_list
.tail
= &new_opt
->header
.next
;
10368 /* Get object-only input files. */
10371 cmdline_get_object_only_input_files (void)
10373 cmdline_union_type
*c
, *next
;
10374 bfd
*abfd
, *archive
;
10375 bfd
*nbfd
, *narchive
;
10377 /* Add files first. */
10378 for (c
= cmdline_object_only_file_list
.head
;
10379 c
!= NULL
; c
= c
->header
.next
)
10380 switch (c
->header
.type
)
10384 case cmdline_is_file_enum
:
10385 lang_add_input_file (c
->file
.filename
,
10386 lang_input_file_is_file_enum
, NULL
);
10388 case cmdline_is_bfd_enum
:
10389 abfd
= c
->abfd
.abfd
;
10390 if (abfd
->my_archive
)
10392 lang_add_input_file (abfd
->filename
,
10393 lang_input_file_is_file_enum
, NULL
);
10397 /* Add archive members next. */
10398 for (c
= cmdline_object_only_archive_list
.head
; c
!= NULL
; c
= next
)
10400 if (c
->header
.type
!= cmdline_is_bfd_enum
)
10403 next
= c
->header
.next
;
10405 abfd
= c
->abfd
.abfd
;
10406 archive
= abfd
->my_archive
;
10408 /* Add the first archive of the archive member group. */
10409 lang_add_input_file (archive
->filename
,
10410 lang_input_file_is_file_enum
, NULL
);
10412 /* Skip the rest members in the archive member group. */
10418 if (next
->header
.type
!= cmdline_is_bfd_enum
)
10421 next
= next
->header
.next
;
10424 nbfd
= next
->abfd
.abfd
;
10425 narchive
= nbfd
->my_archive
;
10427 while (strcmp (archive
->filename
, narchive
->filename
) == 0);
10438 /* Create a section in OBFD with the same
10439 name and attributes as ISECTION in IBFD. */
10442 setup_section (bfd
*ibfd
, sec_ptr isection
, void *p
)
10444 struct cmdline_arg
*arg
= (struct cmdline_arg
*) p
;
10445 bfd
*obfd
= arg
->obfd
;
10446 asymbol
**isympp
= arg
->isympp
;
10447 const char *name
= isection
->name
;
10451 /* Skip the object-only section. */
10452 if (ibfd
->object_only_section
== isection
)
10455 /* If we have already failed earlier on, do not keep on generating
10460 osection
= bfd_make_section_anyway_with_flags (obfd
, name
,
10463 if (osection
== NULL
)
10465 err
= _("failed to create output section");
10469 osection
->size
= isection
->size
;
10470 osection
->vma
= isection
->vma
;
10471 osection
->lma
= isection
->lma
;
10472 osection
->alignment_power
= isection
->alignment_power
;
10474 /* Copy merge entity size. */
10475 osection
->entsize
= isection
->entsize
;
10477 /* This used to be mangle_section; we do here to avoid using
10478 bfd_get_section_by_name since some formats allow multiple
10479 sections with the same name. */
10480 isection
->output_section
= osection
;
10481 isection
->output_offset
= 0;
10483 if ((isection
->flags
& SEC_GROUP
) != 0)
10485 asymbol
*gsym
= bfd_group_signature (isection
, isympp
);
10489 gsym
->flags
|= BSF_KEEP
;
10490 if (ibfd
->xvec
->flavour
== bfd_target_elf_flavour
)
10491 elf_group_id (isection
) = gsym
;
10495 /* Allow the BFD backend to copy any private data it understands
10496 from the input section to the output section. */
10497 if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
10499 err
= _("failed to copy private data");
10503 /* All went well. */
10508 fatal (_("%P: setup_section: %s: %s\n"), err
, name
);
10511 /* Copy the data of input section ISECTION of IBFD
10512 to an output section with the same name in OBFD.
10513 If stripping then don't copy any relocation info. */
10516 copy_section (bfd
*ibfd
, sec_ptr isection
, void *p
)
10518 struct cmdline_arg
*arg
= (struct cmdline_arg
*) p
;
10519 bfd
*obfd
= arg
->obfd
;
10520 asymbol
**isympp
= arg
->isympp
;
10524 bfd_size_type size
;
10529 /* Skip the object-only section. */
10530 if (ibfd
->object_only_section
== isection
)
10533 /* If we have already failed earlier on, do not keep on generating
10538 flags
= bfd_section_flags (isection
);
10539 if ((flags
& SEC_GROUP
) != 0)
10542 osection
= isection
->output_section
;
10543 size
= bfd_section_size (isection
);
10545 if (size
== 0 || osection
== 0)
10548 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
10552 /* Do not complain if the target does not support relocations. */
10554 && bfd_get_error () == bfd_error_invalid_operation
)
10558 err
= bfd_errmsg (bfd_get_error ());
10564 bfd_set_reloc (obfd
, osection
, NULL
, 0);
10567 relpp
= (arelent
**) xmalloc (relsize
);
10568 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
10571 err
= _("relocation count is negative");
10575 bfd_set_reloc (obfd
, osection
,
10576 relcount
== 0 ? NULL
: relpp
, relcount
);
10581 if (bfd_section_flags (isection
) & SEC_HAS_CONTENTS
)
10583 bfd_byte
*memhunk
= NULL
;
10585 if (!bfd_get_full_section_contents (ibfd
, isection
, &memhunk
))
10587 err
= bfd_errmsg (bfd_get_error ());
10591 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
10593 err
= bfd_errmsg (bfd_get_error ());
10599 /* All went well. */
10603 fatal (_("%P: copy_section: %s: %s\n"), err
, isection
->name
);
10605 /* Open the temporary bfd created in the same directory as PATH. */
10608 cmdline_fopen_temp (const char *path
, const char *target
,
10611 #define template "ldXXXXXX"
10612 const char *slash
= strrchr (path
, '/');
10617 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
10619 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
10620 char *bslash
= strrchr (path
, '\\');
10622 if (slash
== NULL
|| (bslash
!= NULL
&& bslash
> slash
))
10624 if (slash
== NULL
&& path
[0] != '\0' && path
[1] == ':')
10629 if (slash
!= (char *) NULL
)
10631 len
= slash
- path
;
10632 tmpname
= (char *) xmalloc (len
+ sizeof (template) + 2);
10633 memcpy (tmpname
, path
, len
);
10635 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
10636 /* If tmpname is "X:", appending a slash will make it a root
10637 directory on drive X, which is NOT the same as the current
10638 directory on drive X. */
10639 if (len
== 2 && tmpname
[1] == ':')
10640 tmpname
[len
++] = '.';
10642 tmpname
[len
++] = '/';
10646 tmpname
= (char *) xmalloc (sizeof (template));
10650 memcpy (tmpname
+ len
, template, sizeof (template));
10653 #ifdef HAVE_MKSTEMP
10654 fd
= mkstemp (tmpname
);
10656 tmpname
= mktemp (tmpname
);
10657 if (tmpname
== NULL
)
10659 fd
= open (tmpname
, O_RDWR
| O_CREAT
| O_EXCL
, 0600);
10663 return bfd_fopen (tmpname
, target
, mode
, fd
);
10666 /* Add the object-only section. */
10669 cmdline_add_object_only_section (bfd_byte
*contents
, size_t size
)
10673 enum bfd_architecture iarch
;
10674 unsigned int imach
;
10677 asymbol
**isympp
= NULL
;
10678 asymbol
**osympp
= NULL
;
10679 bfd
*obfd
= NULL
, *ibfd
;
10688 char *ofilename
= NULL
;
10691 ibfd
= bfd_openr (output_filename
, output_target
);
10694 err
= bfd_errmsg (bfd_get_error ());
10698 if (!bfd_check_format_matches (ibfd
, bfd_object
, &matching
))
10700 err
= bfd_errmsg (bfd_get_error ());
10704 obfd
= cmdline_fopen_temp (output_filename
, output_target
, FOPEN_WB
);
10707 err
= bfd_errmsg (bfd_get_error ());
10710 /* To be used after bfd_close (). */
10711 ofilename
= xstrdup (bfd_get_filename (obfd
));
10713 if (!bfd_set_format (obfd
, bfd_object
))
10715 err
= bfd_errmsg (bfd_get_error ());
10719 /* Copy the start address, flags and architecture of input file to
10721 flags
= bfd_get_file_flags (ibfd
);
10722 start
= bfd_get_start_address (ibfd
);
10723 iarch
= bfd_get_arch (ibfd
);
10724 imach
= bfd_get_mach (ibfd
);
10725 if (!bfd_set_start_address (obfd
, start
)
10726 || !bfd_set_file_flags (obfd
, flags
)
10727 || !bfd_set_arch_mach (obfd
, iarch
, imach
))
10729 err
= bfd_errmsg (bfd_get_error ());
10733 symsize
= bfd_get_symtab_upper_bound (ibfd
);
10736 err
= bfd_errmsg (bfd_get_error ());
10740 isympp
= (asymbol
**) xmalloc (symsize
);
10741 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
10744 err
= bfd_errmsg (bfd_get_error ());
10749 arg
.isympp
= isympp
;
10752 /* BFD mandates that all output sections be created and sizes set before
10753 any output is done. Thus, we traverse all sections multiple times. */
10754 bfd_map_over_sections (ibfd
, setup_section
, &arg
);
10758 err
= _("error setting up sections");
10762 /* Allow the BFD backend to copy any private data it understands
10763 from the input section to the output section. */
10764 if (! bfd_copy_private_header_data (ibfd
, obfd
))
10766 err
= _("error copying private header data");
10770 /* Create the object-only section. */
10771 sec
= bfd_make_section_with_flags (obfd
,
10772 GNU_OBJECT_ONLY_SECTION_NAME
,
10776 | SEC_LINKER_CREATED
));
10779 err
= _("can't create object-only section");
10783 if (! bfd_set_section_size (sec
, size
))
10785 err
= _("can't set object-only section size");
10789 if (ibfd
->object_only_section
)
10791 /* Filter out the object-only section symbol. */
10792 long src_count
= 0, dst_count
= 0;
10793 asymbol
**from
, **to
;
10795 osympp
= xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
10798 for (; src_count
< symcount
; src_count
++)
10800 asymbol
*sym
= from
[src_count
];
10801 if (bfd_asymbol_section (sym
) != ibfd
->object_only_section
)
10802 to
[dst_count
++] = sym
;
10804 to
[dst_count
] = NULL
;
10805 symcount
= dst_count
;
10806 bfd_set_symtab (obfd
, osympp
, symcount
);
10809 bfd_set_symtab (obfd
, isympp
, symcount
);
10811 /* This has to happen after the symbol table has been set. */
10812 bfd_map_over_sections (ibfd
, copy_section
, &arg
);
10816 err
= _("error copying sections");
10820 /* Copy the object-only section to the output. */
10821 if (! bfd_set_section_contents (obfd
, sec
, contents
, 0, size
))
10823 err
= _("error adding object-only section");
10827 /* Allow the BFD backend to copy any private data it understands
10828 from the input BFD to the output BFD. This is done last to
10829 permit the routine to look at the filtered symbol table, which is
10830 important for the ECOFF code at least. */
10831 if (! bfd_copy_private_bfd_data (ibfd
, obfd
))
10833 err
= _("error copying private BFD data");
10837 if (!bfd_close (obfd
))
10839 unlink (ofilename
);
10840 fatal (_("%P: failed to finish output with object-only section\n"));
10843 /* Must be freed after bfd_close (). */
10847 if (rename (ofilename
, output_filename
))
10849 unlink (ofilename
);
10850 fatal (_("%P: failed to rename output with object-only section\n"));
10863 unlink (ofilename
);
10866 fatal (_("%P: failed to add object-only section: %s\n"), err
);
10869 /* Emit the final output with object-only section. */
10872 cmdline_emit_object_only_section (void)
10874 const char *saved_output_filename
= output_filename
;
10877 bfd_byte
*contents
;
10880 /* Get a temporary object-only file. */
10881 output_filename
= make_temp_file (".obj-only.o");
10883 had_output_filename
= false;
10884 link_info
.input_bfds
= NULL
;
10885 link_info
.input_bfds_tail
= &link_info
.input_bfds
;
10890 /* Set up the object-only output. */
10893 /* Open the object-only file for output. */
10894 lang_for_each_statement (ldlang_open_output
);
10896 ldemul_create_output_section_statements ();
10898 if (!bfd_section_already_linked_table_init ())
10899 fatal (_("%P: Failed to create hash table\n"));
10901 /* Call cmdline_on_object_only_archive_list_p to check which member
10902 should be loaded. */
10903 input_flags
.whole_archive
= true;
10905 /* Set it to avoid adding more to cmdline lists. */
10906 config
.emitting_gnu_object_only
= true;
10908 /* Get object-only input files. */
10909 cmdline_get_object_only_input_files ();
10911 /* Open object-only input files. */
10912 open_input_bfds (statement_list
.head
, NULL
, OPEN_BFD_NORMAL
);
10914 ldemul_after_open ();
10916 bfd_section_already_linked_table_free ();
10918 /* Make sure that we're not mixing architectures. We call this
10919 after all the input files have been opened, but before we do any
10920 other processing, so that any operations merge_private_bfd_data
10921 does on the output file will be known during the rest of the
10925 /* Size up the common data. */
10928 /* Update wild statements. */
10929 update_wild_statements (statement_list
.head
);
10931 /* Run through the contours of the script and attach input sections
10932 to the correct output sections. */
10933 map_input_to_output_sections (statement_list
.head
, NULL
, NULL
);
10935 /* Find any sections not attached explicitly and handle them. */
10936 lang_place_orphans ();
10938 /* Do anything special before sizing sections. This is where ELF
10939 and other back-ends size dynamic sections. */
10940 ldemul_before_allocation ();
10942 /* Size up the sections. */
10943 lang_size_sections (NULL
, ! RELAXATION_ENABLED
);
10945 /* See if anything special should be done now we know how big
10946 everything is. This is where relaxation is done. */
10947 ldemul_after_allocation ();
10951 /* Make sure that the section addresses make sense. */
10952 if (command_line
.check_section_addresses
)
10953 lang_check_section_addresses ();
10959 ldexp_finish (true);
10962 if (! bfd_close (link_info
.output_bfd
))
10963 fatal (_("%P:%s: final close failed on object-only output: %E\n"),
10966 link_info
.output_bfd
= NULL
;
10968 /* Read in the object-only file. */
10969 fd
= open (output_filename
, O_RDONLY
| O_BINARY
);
10972 bfd_set_error (bfd_error_system_call
);
10973 fatal (_("%P:%s: cannot open object-only output: %E\n"),
10977 /* Get the object-only file size. */
10978 if (fstat (fd
, &st
) != 0)
10980 bfd_set_error (bfd_error_system_call
);
10981 fatal (_("%P:%s: cannot stat object-only output: %E\n"),
10987 contents
= (bfd_byte
*) xmalloc (size
);
10988 while (off
!= size
)
10992 got
= read (fd
, contents
+ off
, size
- off
);
10995 bfd_set_error (bfd_error_system_call
);
10996 fatal (_("%P:%s: read failed on object-only output: %E\n"),
11005 /* Remove the temporary object-only file. */
11006 unlink (output_filename
);
11008 output_filename
= saved_output_filename
;
11010 cmdline_add_object_only_section (contents
, size
);
11015 /* Extract the object-only section. */
11017 static const char *
11018 cmdline_extract_object_only_section (bfd
*abfd
)
11020 const char *name
= bfd_extract_object_only_section (abfd
);
11023 fatal (_("%P: cannot extract object-only section from %B: %E\n"), abfd
);
11025 /* It should be removed after it is done. */
11026 cmdline_list_append (&cmdline_temp_object_only_list
,
11027 cmdline_is_file_enum
, (void *) name
);
11032 /* Load the object-only section. */
11035 cmdline_load_object_only_section (const char *name
)
11037 lang_input_statement_type
*entry
11038 = new_afile (name
, lang_input_file_is_file_enum
, NULL
, NULL
);
11043 ldfile_open_file (entry
);
11045 if (trace_files
|| verbose
)
11046 info_msg ("%pI\n", entry
);
11048 if (entry
->flags
.missing_file
11049 || bfd_get_format (entry
->the_bfd
) != bfd_object
)
11052 ldlang_add_file (entry
);
11054 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
11055 entry
->flags
.loaded
= true;
11057 fatal (_("%P: %pB: error adding symbols: %E\n"), entry
->the_bfd
);
11060 /* Check and handle the object-only section. */
11063 cmdline_check_object_only_section (bfd
*abfd
, bool lto
)
11065 const char *filename
;
11067 if (config
.emitting_gnu_object_only
|| abfd
->format
!= bfd_object
)
11072 /* For LTO link, we only need to extract object-only section
11073 from the mixed object, add it to input, and put it on LTO
11075 switch (bfd_get_lto_type (abfd
))
11079 case lto_mixed_object
:
11080 filename
= cmdline_extract_object_only_section (abfd
);
11081 cmdline_load_object_only_section (filename
);
11083 case lto_non_ir_object
:
11084 case lto_slim_ir_object
:
11085 case lto_fat_ir_object
:
11089 else if (bfd_link_relocatable (&link_info
))
11091 /* For non-LTO relocatable link, we need to append non-IR object
11092 file and the object file in object-only section to the object
11094 switch (bfd_get_lto_type (abfd
))
11098 case lto_mixed_object
:
11099 filename
= cmdline_extract_object_only_section (abfd
);
11100 cmdline_object_only_list_append (cmdline_is_file_enum
,
11101 (void *) filename
);
11103 case lto_non_ir_object
:
11104 cmdline_object_only_list_append (cmdline_is_bfd_enum
, abfd
);
11106 case lto_slim_ir_object
:
11107 case lto_fat_ir_object
:
11113 /* Remove temporary object-only files. */
11116 cmdline_remove_object_only_files (void)
11118 cmdline_union_type
*c
;
11120 if (config
.plugin_save_temps
)
11123 c
= cmdline_temp_object_only_list
.head
;
11124 for (; c
!= NULL
; c
= c
->header
.next
)
11125 switch (c
->header
.type
)
11129 case cmdline_is_file_enum
:
11130 unlink (c
->file
.filename
);