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 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
= 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 einfo (_("%F%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 einfo (_("%F%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 einfo (_("%F%P:%pS: error: redefinition of memory region "
1548 /* Check if the target region exists. */
1550 einfo (_("%F%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 einfo (_("%F%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 einfo (_("%F%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 sort_def_symbol (struct bfd_link_hash_entry
*hash_entry
,
2490 void *info ATTRIBUTE_UNUSED
)
2492 if ((hash_entry
->type
== bfd_link_hash_defined
2493 || hash_entry
->type
== bfd_link_hash_defweak
)
2494 && hash_entry
->u
.def
.section
->owner
!= link_info
.output_bfd
2495 && hash_entry
->u
.def
.section
->owner
!= NULL
)
2497 input_section_userdata_type
*ud
;
2498 struct map_symbol_def
*def
;
2500 ud
= bfd_section_userdata (hash_entry
->u
.def
.section
);
2503 ud
= stat_alloc (sizeof (*ud
));
2504 bfd_set_section_userdata (hash_entry
->u
.def
.section
, ud
);
2505 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
2506 ud
->map_symbol_def_count
= 0;
2508 else if (!ud
->map_symbol_def_tail
)
2509 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
2511 def
= (struct map_symbol_def
*) obstack_alloc (&map_obstack
, sizeof *def
);
2512 def
->entry
= hash_entry
;
2513 *(ud
->map_symbol_def_tail
) = def
;
2514 ud
->map_symbol_def_tail
= &def
->next
;
2515 ud
->map_symbol_def_count
++;
2520 /* Initialize an output section. */
2523 init_os (lang_output_section_statement_type
*s
, flagword flags
)
2525 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
2526 einfo (_("%F%P: illegal use of `%s' section\n"), DISCARD_SECTION_NAME
);
2529 s
->bfd_section
= bfd_get_section_by_name (link_info
.output_bfd
, s
->name
);
2530 if (s
->bfd_section
== NULL
)
2531 s
->bfd_section
= bfd_make_section_anyway_with_flags (link_info
.output_bfd
,
2533 if (s
->bfd_section
== NULL
)
2535 einfo (_("%F%P: output format %s cannot represent section"
2536 " called %s: %E\n"),
2537 link_info
.output_bfd
->xvec
->name
, s
->name
);
2539 s
->bfd_section
->output_section
= s
->bfd_section
;
2540 s
->bfd_section
->output_offset
= 0;
2542 /* Set the userdata of the output section to the output section
2543 statement to avoid lookup. */
2544 bfd_set_section_userdata (s
->bfd_section
, s
);
2546 /* If there is a base address, make sure that any sections it might
2547 mention are initialized. */
2548 if (s
->addr_tree
!= NULL
)
2549 exp_init_os (s
->addr_tree
);
2551 if (s
->load_base
!= NULL
)
2552 exp_init_os (s
->load_base
);
2554 /* If supplied an alignment, set it. */
2555 if (s
->section_alignment
!= NULL
)
2556 s
->bfd_section
->alignment_power
= exp_get_power (s
->section_alignment
, s
,
2557 "section alignment");
2561 get_os_init_flag (lang_output_section_statement_type
* os
)
2564 switch (os
->sectype
)
2566 case readonly_section
: return SEC_READONLY
;
2567 case noload_section
: return SEC_NEVER_LOAD
;
2574 /* Make sure that all output sections mentioned in an expression are
2578 exp_init_os (etree_type
*exp
)
2580 switch (exp
->type
.node_class
)
2584 case etree_provided
:
2585 exp_init_os (exp
->assign
.src
);
2589 exp_init_os (exp
->binary
.lhs
);
2590 exp_init_os (exp
->binary
.rhs
);
2594 exp_init_os (exp
->trinary
.cond
);
2595 exp_init_os (exp
->trinary
.lhs
);
2596 exp_init_os (exp
->trinary
.rhs
);
2600 exp_init_os (exp
->assert_s
.child
);
2604 exp_init_os (exp
->unary
.child
);
2608 switch (exp
->type
.node_code
)
2613 lang_output_section_statement_type
*os
;
2615 os
= lang_output_section_find (exp
->name
.name
);
2616 if (os
!= NULL
&& os
->bfd_section
== NULL
)
2617 init_os (os
, get_os_init_flag (os
));
2628 section_already_linked (bfd
*abfd
, asection
*sec
, void *data
)
2630 lang_input_statement_type
*entry
= (lang_input_statement_type
*) data
;
2632 /* If we are only reading symbols from this object, then we want to
2633 discard all sections. */
2634 if (entry
->flags
.just_syms
)
2636 bfd_link_just_syms (abfd
, sec
, &link_info
);
2640 /* Deal with SHF_EXCLUDE ELF sections. */
2641 if (!bfd_link_relocatable (&link_info
)
2642 && (abfd
->flags
& BFD_PLUGIN
) == 0
2643 && (sec
->flags
& (SEC_GROUP
| SEC_KEEP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
2644 sec
->output_section
= bfd_abs_section_ptr
;
2646 if (!(abfd
->flags
& DYNAMIC
))
2647 bfd_section_already_linked (abfd
, sec
, &link_info
);
2651 /* Returns true if SECTION is one we know will be discarded based on its
2652 section flags, otherwise returns false. */
2655 lang_discard_section_p (asection
*section
)
2658 flagword flags
= section
->flags
;
2660 /* Discard sections marked with SEC_EXCLUDE. */
2661 discard
= (flags
& SEC_EXCLUDE
) != 0;
2663 /* Discard the group descriptor sections when we're finally placing the
2664 sections from within the group. */
2665 if ((flags
& SEC_GROUP
) != 0
2666 && link_info
.resolve_section_groups
)
2669 /* Discard debugging sections if we are stripping debugging
2671 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
2672 && (flags
& SEC_DEBUGGING
) != 0)
2675 /* Discard non-alloc sections if we are stripping section headers. */
2676 else if (config
.no_section_header
&& (flags
& SEC_ALLOC
) == 0)
2682 /* Return TRUE if SECTION is never going to be added to output statement
2683 OUTPUT. lang_add_section() definitely won't do anything with SECTION
2684 if this returns TRUE. It may do something (or not) if this returns FALSE.
2686 Can be used as early-out to filter matches. This may set
2687 output_section of SECTION, if it was unset, to the abs section in case
2688 we discover SECTION to be always discarded. This may also give
2689 warning messages. */
2692 wont_add_section_p (asection
*section
,
2693 lang_output_section_statement_type
*output
)
2697 /* Is this section one we know should be discarded? */
2698 discard
= lang_discard_section_p (section
);
2700 /* Discard input sections which are assigned to a section named
2701 DISCARD_SECTION_NAME. */
2702 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
2707 if (section
->output_section
== NULL
)
2709 /* This prevents future calls from assigning this section or
2710 warning about it again. */
2711 section
->output_section
= bfd_abs_section_ptr
;
2713 else if (bfd_is_abs_section (section
->output_section
))
2715 else if (link_info
.non_contiguous_regions_warnings
)
2716 einfo (_("%P:%pS: warning: --enable-non-contiguous-regions makes "
2717 "section `%pA' from `%pB' match /DISCARD/ clause.\n"),
2718 NULL
, section
, section
->owner
);
2723 if (section
->output_section
!= NULL
)
2725 if (!link_info
.non_contiguous_regions
)
2728 /* SECTION has already been handled in a special way
2729 (eg. LINK_ONCE): skip it. */
2730 if (bfd_is_abs_section (section
->output_section
))
2733 /* Already assigned to the same output section, do not process
2734 it again, to avoid creating loops between duplicate sections
2736 if (section
->output_section
== output
->bfd_section
)
2739 if (link_info
.non_contiguous_regions_warnings
&& output
->bfd_section
)
2740 einfo (_("%P:%pS: warning: --enable-non-contiguous-regions may "
2741 "change behaviour for section `%pA' from `%pB' (assigned to "
2742 "%pA, but additional match: %pA)\n"),
2743 NULL
, section
, section
->owner
, section
->output_section
,
2744 output
->bfd_section
);
2746 /* SECTION has already been assigned to an output section, but
2747 the user allows it to be mapped to another one in case it
2748 overflows. We'll later update the actual output section in
2749 size_input_section as appropriate. */
2755 /* The wild routines.
2757 These expand statements like *(.text) and foo.o to a list of
2758 explicit actions, like foo.o(.text), bar.o(.text) and
2759 foo.o(.text, .data). */
2761 /* Add SECTION to the output section OUTPUT. Do this by creating a
2762 lang_input_section statement which is placed at PTR. */
2765 lang_add_section (lang_statement_list_type
*ptr
,
2767 struct wildcard_list
*pattern
,
2768 struct flag_info
*sflag_info
,
2769 lang_output_section_statement_type
*output
)
2771 flagword flags
= section
->flags
;
2773 lang_input_section_type
*new_section
;
2774 bfd
*abfd
= link_info
.output_bfd
;
2776 if (wont_add_section_p (section
, output
))
2783 keep
= bfd_lookup_section_flags (&link_info
, sflag_info
, section
);
2788 /* We don't copy the SEC_NEVER_LOAD flag from an input section
2789 to an output section, because we want to be able to include a
2790 SEC_NEVER_LOAD section in the middle of an otherwise loaded
2791 section (I don't know why we want to do this, but we do).
2792 build_link_order in ldwrite.c handles this case by turning
2793 the embedded SEC_NEVER_LOAD section into a fill. */
2794 flags
&= ~ SEC_NEVER_LOAD
;
2796 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
2797 already been processed. One reason to do this is that on pe
2798 format targets, .text$foo sections go into .text and it's odd
2799 to see .text with SEC_LINK_ONCE set. */
2800 if ((flags
& (SEC_LINK_ONCE
| SEC_GROUP
)) == (SEC_LINK_ONCE
| SEC_GROUP
))
2802 if (link_info
.resolve_section_groups
)
2803 flags
&= ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
);
2805 flags
&= ~(SEC_LINK_DUPLICATES
| SEC_RELOC
);
2807 else if (!bfd_link_relocatable (&link_info
))
2808 flags
&= ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
);
2810 switch (output
->sectype
)
2812 case normal_section
:
2813 case overlay_section
:
2814 case first_overlay_section
:
2817 case noalloc_section
:
2818 flags
&= ~SEC_ALLOC
;
2820 case typed_readonly_section
:
2821 case readonly_section
:
2822 flags
|= SEC_READONLY
;
2824 case noload_section
:
2826 flags
|= SEC_NEVER_LOAD
;
2827 /* Unfortunately GNU ld has managed to evolve two different
2828 meanings to NOLOAD in scripts. ELF gets a .bss style noload,
2829 alloc, no contents section. All others get a noload, noalloc
2831 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
)
2832 flags
&= ~SEC_HAS_CONTENTS
;
2834 flags
&= ~SEC_ALLOC
;
2838 if (output
->bfd_section
== NULL
)
2839 init_os (output
, flags
);
2841 /* If SEC_READONLY is not set in the input section, then clear
2842 it from the output section. */
2843 output
->bfd_section
->flags
&= flags
| ~SEC_READONLY
;
2845 if (output
->bfd_section
->linker_has_input
)
2847 /* Only set SEC_READONLY flag on the first input section. */
2848 flags
&= ~ SEC_READONLY
;
2850 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
2851 if ((output
->bfd_section
->flags
& (SEC_MERGE
| SEC_STRINGS
))
2852 != (flags
& (SEC_MERGE
| SEC_STRINGS
))
2853 || ((flags
& SEC_MERGE
) != 0
2854 && output
->bfd_section
->entsize
!= section
->entsize
))
2856 output
->bfd_section
->flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2857 flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2860 output
->bfd_section
->flags
|= flags
;
2862 if (!output
->bfd_section
->linker_has_input
)
2864 output
->bfd_section
->linker_has_input
= 1;
2865 /* This must happen after flags have been updated. The output
2866 section may have been created before we saw its first input
2867 section, eg. for a data statement. */
2868 bfd_init_private_section_data (section
->owner
, section
,
2869 link_info
.output_bfd
,
2870 output
->bfd_section
,
2872 if ((flags
& SEC_MERGE
) != 0)
2873 output
->bfd_section
->entsize
= section
->entsize
;
2876 if ((flags
& SEC_TIC54X_BLOCK
) != 0
2877 && bfd_get_arch (section
->owner
) == bfd_arch_tic54x
)
2879 /* FIXME: This value should really be obtained from the bfd... */
2880 output
->block_value
= 128;
2883 /* When a .ctors section is placed in .init_array it must be copied
2884 in reverse order. Similarly for .dtors. Set that up. */
2885 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
2886 && ((startswith (section
->name
, ".ctors")
2887 && strcmp (output
->bfd_section
->name
, ".init_array") == 0)
2888 || (startswith (section
->name
, ".dtors")
2889 && strcmp (output
->bfd_section
->name
, ".fini_array") == 0))
2890 && (section
->name
[6] == 0 || section
->name
[6] == '.'))
2891 section
->flags
|= SEC_ELF_REVERSE_COPY
;
2893 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
2894 output
->bfd_section
->alignment_power
= section
->alignment_power
;
2896 section
->output_section
= output
->bfd_section
;
2898 if (!map_head_is_link_order
)
2900 asection
*s
= output
->bfd_section
->map_tail
.s
;
2901 output
->bfd_section
->map_tail
.s
= section
;
2902 section
->map_head
.s
= NULL
;
2903 section
->map_tail
.s
= s
;
2905 s
->map_head
.s
= section
;
2907 output
->bfd_section
->map_head
.s
= section
;
2910 /* Add a section reference to the list. */
2911 new_section
= new_stat (lang_input_section
, ptr
);
2912 new_section
->section
= section
;
2913 new_section
->pattern
= pattern
;
2916 /* Expand a wild statement for a particular FILE. SECTION may be
2917 NULL, in which case it is a wild card. This assumes that the
2918 wild statement doesn't need any sorting (of filenames or sections). */
2921 output_section_callback_nosort (lang_wild_statement_type
*ptr
,
2922 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
2924 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
2927 lang_output_section_statement_type
*os
;
2929 os
= (lang_output_section_statement_type
*) output
;
2931 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2932 if (unique_section_p (section
, os
))
2935 lang_add_section (&ptr
->children
, section
, ptr
->section_list
,
2936 ptr
->section_flag_list
, os
);
2939 /* Check if all sections in a wild statement for a particular FILE
2943 check_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
2944 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
2946 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
2949 lang_output_section_statement_type
*os
;
2951 os
= (lang_output_section_statement_type
*) output
;
2953 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2954 if (unique_section_p (section
, os
))
2957 if (section
->output_section
== NULL
&& (section
->flags
& SEC_READONLY
) == 0)
2958 os
->all_input_readonly
= false;
2961 /* This is passed a file name which must have been seen already and
2962 added to the statement tree. We will see if it has been opened
2963 already and had its symbols read. If not then we'll read it. */
2965 static lang_input_statement_type
*
2966 lookup_name (const char *name
)
2968 lang_input_statement_type
*search
;
2970 for (search
= (void *) input_file_chain
.head
;
2972 search
= search
->next_real_file
)
2974 /* Use the local_sym_name as the name of the file that has
2975 already been loaded as filename might have been transformed
2976 via the search directory lookup mechanism. */
2977 const char *filename
= search
->local_sym_name
;
2979 if (filename
!= NULL
2980 && filename_cmp (filename
, name
) == 0)
2986 /* Arrange to splice the input statement added by new_afile into
2987 statement_list after the current input_file_chain tail.
2988 We know input_file_chain is not an empty list, and that
2989 lookup_name was called via open_input_bfds. Later calls to
2990 lookup_name should always match an existing input_statement. */
2991 lang_statement_union_type
**tail
= stat_ptr
->tail
;
2992 lang_statement_union_type
**after
2993 = (void *) ((char *) input_file_chain
.tail
2994 - offsetof (lang_input_statement_type
, next_real_file
)
2995 + offsetof (lang_input_statement_type
, header
.next
));
2996 lang_statement_union_type
*rest
= *after
;
2997 stat_ptr
->tail
= after
;
2998 search
= new_afile (name
, lang_input_file_is_search_file_enum
,
2999 default_target
, NULL
);
3000 *stat_ptr
->tail
= rest
;
3002 stat_ptr
->tail
= tail
;
3005 /* If we have already added this file, or this file is not real
3006 don't add this file. */
3007 if (search
->flags
.loaded
|| !search
->flags
.real
)
3010 if (!load_symbols (search
, NULL
))
3016 /* Save LIST as a list of libraries whose symbols should not be exported. */
3021 struct excluded_lib
*next
;
3023 static struct excluded_lib
*excluded_libs
;
3026 add_excluded_libs (const char *list
)
3028 const char *p
= list
, *end
;
3032 struct excluded_lib
*entry
;
3033 end
= strpbrk (p
, ",:");
3035 end
= p
+ strlen (p
);
3036 entry
= stat_alloc (sizeof (*entry
));
3037 entry
->next
= excluded_libs
;
3038 entry
->name
= stat_memdup (p
, end
- p
, end
- p
+ 1);
3039 excluded_libs
= entry
;
3047 check_excluded_libs (bfd
*abfd
)
3049 struct excluded_lib
*lib
= excluded_libs
;
3053 int len
= strlen (lib
->name
);
3054 const char *filename
= lbasename (bfd_get_filename (abfd
));
3056 if (strcmp (lib
->name
, "ALL") == 0)
3058 abfd
->no_export
= true;
3062 if (filename_ncmp (lib
->name
, filename
, len
) == 0
3063 && (filename
[len
] == '\0'
3064 || (filename
[len
] == '.' && filename
[len
+ 1] == 'a'
3065 && filename
[len
+ 2] == '\0')))
3067 abfd
->no_export
= true;
3075 /* Get the symbols for an input file. */
3078 load_symbols (lang_input_statement_type
*entry
,
3079 lang_statement_list_type
*place
)
3083 if (entry
->flags
.loaded
)
3086 ldfile_open_file (entry
);
3088 /* Do not process further if the file was missing. */
3089 if (entry
->flags
.missing_file
)
3092 if (trace_files
|| verbose
)
3093 info_msg ("%pI\n", entry
);
3095 if (!bfd_check_format (entry
->the_bfd
, bfd_archive
)
3096 && !bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
3099 struct lang_input_statement_flags save_flags
;
3102 err
= bfd_get_error ();
3104 /* See if the emulation has some special knowledge. */
3105 if (ldemul_unrecognized_file (entry
))
3107 if (err
== bfd_error_file_ambiguously_recognized
)
3112 if (err
== bfd_error_file_ambiguously_recognized
)
3116 einfo (_("%P: %pB: file not recognized: %E;"
3117 " matching formats:"), entry
->the_bfd
);
3118 for (p
= matching
; *p
!= NULL
; p
++)
3123 else if (err
!= bfd_error_file_not_recognized
3125 einfo (_("%F%P: %pB: file not recognized: %E\n"), entry
->the_bfd
);
3127 bfd_close (entry
->the_bfd
);
3128 entry
->the_bfd
= NULL
;
3130 /* Try to interpret the file as a linker script. */
3131 save_flags
= input_flags
;
3132 ldfile_open_command_file (entry
->filename
);
3134 push_stat_ptr (place
);
3135 input_flags
.add_DT_NEEDED_for_regular
3136 = entry
->flags
.add_DT_NEEDED_for_regular
;
3137 input_flags
.add_DT_NEEDED_for_dynamic
3138 = entry
->flags
.add_DT_NEEDED_for_dynamic
;
3139 input_flags
.whole_archive
= entry
->flags
.whole_archive
;
3140 input_flags
.dynamic
= entry
->flags
.dynamic
;
3142 ldfile_assumed_script
= true;
3143 parser_input
= input_script
;
3144 current_input_file
= entry
->filename
;
3146 current_input_file
= NULL
;
3147 ldfile_assumed_script
= false;
3149 /* missing_file is sticky. sysrooted will already have been
3150 restored when seeing EOF in yyparse, but no harm to restore
3152 save_flags
.missing_file
|= input_flags
.missing_file
;
3153 input_flags
= save_flags
;
3157 entry
->flags
.loaded
= true;
3162 if (ldemul_recognized_file (entry
))
3165 /* We don't call ldlang_add_file for an archive. Instead, the
3166 add_symbols entry point will call ldlang_add_file, via the
3167 add_archive_element callback, for each element of the archive
3169 switch (bfd_get_format (entry
->the_bfd
))
3175 if (!entry
->flags
.reload
)
3176 ldlang_add_file (entry
);
3180 check_excluded_libs (entry
->the_bfd
);
3182 bfd_set_usrdata (entry
->the_bfd
, entry
);
3183 if (entry
->flags
.whole_archive
)
3191 member
= bfd_openr_next_archived_file (entry
->the_bfd
, member
);
3196 if (!bfd_check_format (member
, bfd_object
))
3198 einfo (_("%F%P: %pB: member %pB in archive is not an object\n"),
3199 entry
->the_bfd
, member
);
3203 if (config
.emitting_gnu_object_only
)
3205 if (!cmdline_on_object_only_archive_list_p (member
))
3210 if (!(*link_info
.callbacks
3211 ->add_archive_element
) (&link_info
, member
,
3212 "--whole-archive", &subsbfd
))
3215 /* Potentially, the add_archive_element hook may have set a
3216 substitute BFD for us. */
3217 if (!bfd_link_add_symbols (subsbfd
, &link_info
))
3219 einfo (_("%F%P: %pB: error adding symbols: %E\n"), member
);
3224 entry
->flags
.loaded
= loaded
;
3230 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
3231 entry
->flags
.loaded
= true;
3233 einfo (_("%F%P: %pB: error adding symbols: %E\n"), entry
->the_bfd
);
3235 return entry
->flags
.loaded
;
3238 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
3239 may be NULL, indicating that it is a wildcard. Separate
3240 lang_input_section statements are created for each part of the
3241 expansion; they are added after the wild statement S. OUTPUT is
3242 the output section. */
3245 wild (lang_wild_statement_type
*s
,
3246 const char *target ATTRIBUTE_UNUSED
,
3247 lang_output_section_statement_type
*output
)
3249 struct wildcard_list
*sec
;
3251 if (s
->filenames_sorted
|| s
->any_specs_sorted
)
3253 lang_section_bst_type
*tree
;
3255 walk_wild (s
, output_section_callback_sort
, output
);
3260 output_section_callback_tree_to_list (s
, tree
, output
);
3262 s
->rightmost
= &s
->tree
;
3266 walk_wild (s
, output_section_callback_nosort
, output
);
3268 if (default_common_section
== NULL
)
3269 for (sec
= s
->section_list
; sec
!= NULL
; sec
= sec
->next
)
3270 if (sec
->spec
.name
!= NULL
&& strcmp (sec
->spec
.name
, "COMMON") == 0)
3272 /* Remember the section that common is going to in case we
3273 later get something which doesn't know where to put it. */
3274 default_common_section
= output
;
3279 /* Return TRUE iff target is the sought target. */
3282 get_target (const bfd_target
*target
, void *data
)
3284 const char *sought
= (const char *) data
;
3286 return strcmp (target
->name
, sought
) == 0;
3289 /* Like strcpy() but convert to lower case as well. */
3292 stricpy (char *dest
, const char *src
)
3296 while ((c
= *src
++) != 0)
3297 *dest
++ = TOLOWER (c
);
3302 /* Remove the first occurrence of needle (if any) in haystack
3306 strcut (char *haystack
, const char *needle
)
3308 haystack
= strstr (haystack
, needle
);
3314 for (src
= haystack
+ strlen (needle
); *src
;)
3315 *haystack
++ = *src
++;
3321 /* Compare two target format name strings.
3322 Return a value indicating how "similar" they are. */
3325 name_compare (const char *first
, const char *second
)
3331 copy1
= (char *) xmalloc (strlen (first
) + 1);
3332 copy2
= (char *) xmalloc (strlen (second
) + 1);
3334 /* Convert the names to lower case. */
3335 stricpy (copy1
, first
);
3336 stricpy (copy2
, second
);
3338 /* Remove size and endian strings from the name. */
3339 strcut (copy1
, "big");
3340 strcut (copy1
, "little");
3341 strcut (copy2
, "big");
3342 strcut (copy2
, "little");
3344 /* Return a value based on how many characters match,
3345 starting from the beginning. If both strings are
3346 the same then return 10 * their length. */
3347 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
3348 if (copy1
[result
] == 0)
3360 /* Set by closest_target_match() below. */
3361 static const bfd_target
*winner
;
3363 /* Scan all the valid bfd targets looking for one that has the endianness
3364 requirement that was specified on the command line, and is the nearest
3365 match to the original output target. */
3368 closest_target_match (const bfd_target
*target
, void *data
)
3370 const bfd_target
*original
= (const bfd_target
*) data
;
3372 if (command_line
.endian
== ENDIAN_BIG
3373 && target
->byteorder
!= BFD_ENDIAN_BIG
)
3376 if (command_line
.endian
== ENDIAN_LITTLE
3377 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
3380 /* Must be the same flavour. */
3381 if (target
->flavour
!= original
->flavour
)
3384 /* Ignore generic big and little endian elf vectors. */
3385 if (strcmp (target
->name
, "elf32-big") == 0
3386 || strcmp (target
->name
, "elf64-big") == 0
3387 || strcmp (target
->name
, "elf32-little") == 0
3388 || strcmp (target
->name
, "elf64-little") == 0)
3391 /* If we have not found a potential winner yet, then record this one. */
3398 /* Oh dear, we now have two potential candidates for a successful match.
3399 Compare their names and choose the better one. */
3400 if (name_compare (target
->name
, original
->name
)
3401 > name_compare (winner
->name
, original
->name
))
3404 /* Keep on searching until wqe have checked them all. */
3408 /* Return the BFD target format of the first input file. */
3411 get_first_input_target (void)
3413 const char *target
= NULL
;
3415 LANG_FOR_EACH_INPUT_STATEMENT (s
)
3417 if (s
->header
.type
== lang_input_statement_enum
3420 ldfile_open_file (s
);
3422 if (s
->the_bfd
!= NULL
3423 && bfd_check_format (s
->the_bfd
, bfd_object
))
3425 target
= bfd_get_target (s
->the_bfd
);
3437 lang_get_output_target (void)
3441 /* Has the user told us which output format to use? */
3442 if (output_target
!= NULL
)
3443 return output_target
;
3445 /* No - has the current target been set to something other than
3447 if (current_target
!= default_target
&& current_target
!= NULL
)
3448 return current_target
;
3450 /* No - can we determine the format of the first input file? */
3451 target
= get_first_input_target ();
3455 /* Failed - use the default output target. */
3456 return default_target
;
3459 /* Open the output file. */
3462 open_output (const char *name
)
3464 lang_input_statement_type
*f
;
3465 char *out
= lrealpath (name
);
3467 for (f
= (void *) input_file_chain
.head
;
3469 f
= f
->next_real_file
)
3472 char *in
= lrealpath (f
->local_sym_name
);
3473 if (filename_cmp (in
, out
) == 0)
3474 einfo (_("%F%P: input file '%s' is the same as output file\n"),
3480 output_target
= lang_get_output_target ();
3482 /* Has the user requested a particular endianness on the command
3484 if (command_line
.endian
!= ENDIAN_UNSET
)
3486 /* Get the chosen target. */
3487 const bfd_target
*target
3488 = bfd_iterate_over_targets (get_target
, (void *) output_target
);
3490 /* If the target is not supported, we cannot do anything. */
3493 enum bfd_endian desired_endian
;
3495 if (command_line
.endian
== ENDIAN_BIG
)
3496 desired_endian
= BFD_ENDIAN_BIG
;
3498 desired_endian
= BFD_ENDIAN_LITTLE
;
3500 /* See if the target has the wrong endianness. This should
3501 not happen if the linker script has provided big and
3502 little endian alternatives, but some scrips don't do
3504 if (target
->byteorder
!= desired_endian
)
3506 /* If it does, then see if the target provides
3507 an alternative with the correct endianness. */
3508 if (target
->alternative_target
!= NULL
3509 && (target
->alternative_target
->byteorder
== desired_endian
))
3510 output_target
= target
->alternative_target
->name
;
3513 /* Try to find a target as similar as possible to
3514 the default target, but which has the desired
3515 endian characteristic. */
3516 bfd_iterate_over_targets (closest_target_match
,
3519 /* Oh dear - we could not find any targets that
3520 satisfy our requirements. */
3522 einfo (_("%P: warning: could not find any targets"
3523 " that match endianness requirement\n"));
3525 output_target
= winner
->name
;
3531 link_info
.output_bfd
= bfd_openw (name
, output_target
);
3533 if (link_info
.output_bfd
== NULL
)
3535 if (bfd_get_error () == bfd_error_invalid_target
)
3536 einfo (_("%F%P: target %s not found\n"), output_target
);
3538 einfo (_("%F%P: cannot open output file %s: %E\n"), name
);
3541 delete_output_file_on_failure
= true;
3543 if (!bfd_set_format (link_info
.output_bfd
, bfd_object
))
3544 einfo (_("%F%P: %s: can not make object file: %E\n"), name
);
3545 if (!bfd_set_arch_mach (link_info
.output_bfd
,
3546 ldfile_output_architecture
,
3547 ldfile_output_machine
))
3548 einfo (_("%F%P: %s: can not set architecture: %E\n"), name
);
3550 link_info
.hash
= bfd_link_hash_table_create (link_info
.output_bfd
);
3551 if (link_info
.hash
== NULL
)
3552 einfo (_("%F%P: can not create hash table: %E\n"));
3554 bfd_set_gp_size (link_info
.output_bfd
, g_switch_value
);
3558 ldlang_open_output (lang_statement_union_type
*statement
)
3560 switch (statement
->header
.type
)
3562 case lang_output_statement_enum
:
3563 ASSERT (link_info
.output_bfd
== NULL
);
3564 open_output (statement
->output_statement
.name
);
3565 ldemul_set_output_arch ();
3566 if (config
.magic_demand_paged
3567 && !bfd_link_relocatable (&link_info
))
3568 link_info
.output_bfd
->flags
|= D_PAGED
;
3570 link_info
.output_bfd
->flags
&= ~D_PAGED
;
3571 if (config
.text_read_only
)
3572 link_info
.output_bfd
->flags
|= WP_TEXT
;
3574 link_info
.output_bfd
->flags
&= ~WP_TEXT
;
3575 if (link_info
.traditional_format
)
3576 link_info
.output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
3578 link_info
.output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
3579 if (config
.no_section_header
)
3580 link_info
.output_bfd
->flags
|= BFD_NO_SECTION_HEADER
;
3582 link_info
.output_bfd
->flags
&= ~BFD_NO_SECTION_HEADER
;
3585 case lang_target_statement_enum
:
3586 current_target
= statement
->target_statement
.target
;
3594 init_opb (asection
*s
)
3599 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
3601 && (s
->flags
& SEC_ELF_OCTETS
) != 0)
3604 x
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3605 ldfile_output_machine
);
3607 while ((x
& 1) == 0)
3615 /* Open all the input files. */
3619 OPEN_BFD_NORMAL
= 0,
3623 #if BFD_SUPPORTS_PLUGINS
3624 static lang_input_statement_type
*plugin_insert
= NULL
;
3625 static struct bfd_link_hash_entry
*plugin_undefs
= NULL
;
3629 open_input_bfds (lang_statement_union_type
*s
,
3630 lang_output_section_statement_type
*os
,
3631 enum open_bfd_mode mode
)
3633 for (; s
!= NULL
; s
= s
->header
.next
)
3635 switch (s
->header
.type
)
3637 case lang_constructors_statement_enum
:
3638 open_input_bfds (constructor_list
.head
, os
, mode
);
3640 case lang_output_section_statement_enum
:
3641 os
= &s
->output_section_statement
;
3642 open_input_bfds (os
->children
.head
, os
, mode
);
3644 case lang_wild_statement_enum
:
3645 /* Maybe we should load the file's symbols. */
3646 if ((mode
& OPEN_BFD_RESCAN
) == 0
3647 && s
->wild_statement
.filename
3648 && !wildcardp (s
->wild_statement
.filename
)
3649 && !archive_path (s
->wild_statement
.filename
))
3650 lookup_name (s
->wild_statement
.filename
);
3651 open_input_bfds (s
->wild_statement
.children
.head
, os
, mode
);
3653 case lang_group_statement_enum
:
3655 struct bfd_link_hash_entry
*undefs
;
3656 #if BFD_SUPPORTS_PLUGINS
3657 lang_input_statement_type
*plugin_insert_save
;
3660 /* We must continually search the entries in the group
3661 until no new symbols are added to the list of undefined
3666 #if BFD_SUPPORTS_PLUGINS
3667 plugin_insert_save
= plugin_insert
;
3669 undefs
= link_info
.hash
->undefs_tail
;
3670 open_input_bfds (s
->group_statement
.children
.head
, os
,
3671 mode
| OPEN_BFD_FORCE
);
3673 while (undefs
!= link_info
.hash
->undefs_tail
3674 #if BFD_SUPPORTS_PLUGINS
3675 /* Objects inserted by a plugin, which are loaded
3676 before we hit this loop, may have added new
3678 || (plugin_insert
!= plugin_insert_save
&& plugin_undefs
)
3683 case lang_target_statement_enum
:
3684 current_target
= s
->target_statement
.target
;
3686 case lang_input_statement_enum
:
3687 if (s
->input_statement
.flags
.real
)
3689 lang_statement_union_type
**os_tail
;
3690 lang_statement_list_type add
;
3693 s
->input_statement
.target
= current_target
;
3695 /* If we are being called from within a group, and this
3696 is an archive which has already been searched, then
3697 force it to be researched unless the whole archive
3698 has been loaded already. Do the same for a rescan.
3699 Likewise reload --as-needed shared libs. */
3700 if (mode
!= OPEN_BFD_NORMAL
3701 #if BFD_SUPPORTS_PLUGINS
3702 && ((mode
& OPEN_BFD_RESCAN
) == 0
3703 || plugin_insert
== NULL
)
3705 && s
->input_statement
.flags
.loaded
3706 && (abfd
= s
->input_statement
.the_bfd
) != NULL
3707 && ((bfd_get_format (abfd
) == bfd_archive
3708 && !s
->input_statement
.flags
.whole_archive
)
3709 || (bfd_get_format (abfd
) == bfd_object
3710 && ((abfd
->flags
) & DYNAMIC
) != 0
3711 && s
->input_statement
.flags
.add_DT_NEEDED_for_regular
3712 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
3713 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)))
3715 s
->input_statement
.flags
.loaded
= false;
3716 s
->input_statement
.flags
.reload
= true;
3719 os_tail
= lang_os_list
.tail
;
3720 lang_list_init (&add
);
3722 if (!load_symbols (&s
->input_statement
, &add
))
3723 config
.make_executable
= false;
3725 if (add
.head
!= NULL
)
3727 /* If this was a script with output sections then
3728 tack any added statements on to the end of the
3729 list. This avoids having to reorder the output
3730 section statement list. Very likely the user
3731 forgot -T, and whatever we do here will not meet
3732 naive user expectations. */
3733 if (os_tail
!= lang_os_list
.tail
)
3735 einfo (_("%P: warning: %s contains output sections;"
3736 " did you forget -T?\n"),
3737 s
->input_statement
.filename
);
3738 *stat_ptr
->tail
= add
.head
;
3739 stat_ptr
->tail
= add
.tail
;
3743 *add
.tail
= s
->header
.next
;
3744 s
->header
.next
= add
.head
;
3748 #if BFD_SUPPORTS_PLUGINS
3749 /* If we have found the point at which a plugin added new
3750 files, clear plugin_insert to enable archive rescan. */
3751 if (&s
->input_statement
== plugin_insert
)
3752 plugin_insert
= NULL
;
3755 case lang_assignment_statement_enum
:
3756 if (s
->assignment_statement
.exp
->type
.node_class
!= etree_assert
)
3757 exp_fold_tree_no_dot (s
->assignment_statement
.exp
, os
);
3764 /* Exit if any of the files were missing. */
3765 if (input_flags
.missing_file
)
3769 #ifdef ENABLE_LIBCTF
3770 /* Emit CTF errors and warnings. fp can be NULL to report errors/warnings
3771 that happened specifically at CTF open time. */
3773 lang_ctf_errs_warnings (ctf_dict_t
*fp
)
3775 ctf_next_t
*i
= NULL
;
3780 while ((text
= ctf_errwarning_next (fp
, &i
, &is_warning
, &err
)) != NULL
)
3782 einfo (_("%s: %s\n"), is_warning
? _("CTF warning"): _("CTF error"),
3786 if (err
!= ECTF_NEXT_END
)
3788 einfo (_("CTF error: cannot get CTF errors: `%s'\n"),
3792 /* `err' returns errors from the error/warning iterator in particular.
3793 These never assert. But if we have an fp, that could have recorded
3794 an assertion failure: assert if it has done so. */
3795 ASSERT (!fp
|| ctf_errno (fp
) != ECTF_INTERNAL
);
3798 /* Open the CTF sections in the input files with libctf: if any were opened,
3799 create a fake input file that we'll write the merged CTF data to later
3803 ldlang_open_ctf (void)
3808 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3812 /* Incoming files from the compiler have a single ctf_dict_t in them
3813 (which is presented to us by the libctf API in a ctf_archive_t
3814 wrapper): files derived from a previous relocatable link have a CTF
3815 archive containing possibly many CTF files. */
3817 if ((file
->the_ctf
= ctf_bfdopen (file
->the_bfd
, &err
)) == NULL
)
3819 if (err
!= ECTF_NOCTFDATA
)
3821 lang_ctf_errs_warnings (NULL
);
3822 einfo (_("%P: warning: CTF section in %pB not loaded; "
3823 "its types will be discarded: %s\n"), file
->the_bfd
,
3829 /* Prevent the contents of this section from being written, while
3830 requiring the section itself to be duplicated in the output, but only
3832 /* This section must exist if ctf_bfdopen() succeeded. */
3833 sect
= bfd_get_section_by_name (file
->the_bfd
, ".ctf");
3835 sect
->flags
|= SEC_NEVER_LOAD
| SEC_HAS_CONTENTS
| SEC_LINKER_CREATED
;
3838 sect
->flags
|= SEC_EXCLUDE
;
3848 if ((ctf_output
= ctf_create (&err
)) != NULL
)
3851 einfo (_("%P: warning: CTF output not created: `%s'\n"),
3854 LANG_FOR_EACH_INPUT_STATEMENT (errfile
)
3855 ctf_close (errfile
->the_ctf
);
3858 /* Merge together CTF sections. After this, only the symtab-dependent
3859 function and data object sections need adjustment. */
3862 lang_merge_ctf (void)
3864 asection
*output_sect
;
3870 output_sect
= bfd_get_section_by_name (link_info
.output_bfd
, ".ctf");
3872 /* If the section was discarded, don't waste time merging. */
3873 if (output_sect
== NULL
)
3875 ctf_dict_close (ctf_output
);
3878 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3880 ctf_close (file
->the_ctf
);
3881 file
->the_ctf
= NULL
;
3886 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3891 /* Takes ownership of file->the_ctf. */
3892 if (ctf_link_add_ctf (ctf_output
, file
->the_ctf
, file
->filename
) < 0)
3894 einfo (_("%P: warning: CTF section in %pB cannot be linked: `%s'\n"),
3895 file
->the_bfd
, ctf_errmsg (ctf_errno (ctf_output
)));
3896 ctf_close (file
->the_ctf
);
3897 file
->the_ctf
= NULL
;
3902 if (!config
.ctf_share_duplicated
)
3903 flags
= CTF_LINK_SHARE_UNCONFLICTED
;
3905 flags
= CTF_LINK_SHARE_DUPLICATED
;
3906 if (!config
.ctf_variables
)
3907 flags
|= CTF_LINK_OMIT_VARIABLES_SECTION
;
3908 if (bfd_link_relocatable (&link_info
))
3909 flags
|= CTF_LINK_NO_FILTER_REPORTED_SYMS
;
3911 if (ctf_link (ctf_output
, flags
) < 0)
3913 lang_ctf_errs_warnings (ctf_output
);
3914 einfo (_("%P: warning: CTF linking failed; "
3915 "output will have no CTF section: %s\n"),
3916 ctf_errmsg (ctf_errno (ctf_output
)));
3919 output_sect
->size
= 0;
3920 output_sect
->flags
|= SEC_EXCLUDE
;
3923 /* Output any lingering errors that didn't come from ctf_link. */
3924 lang_ctf_errs_warnings (ctf_output
);
3927 /* Let the emulation acquire strings from the dynamic strtab to help it optimize
3928 the CTF, if supported. */
3931 ldlang_ctf_acquire_strings (struct elf_strtab_hash
*dynstrtab
)
3933 ldemul_acquire_strings_for_ctf (ctf_output
, dynstrtab
);
3936 /* Inform the emulation about the addition of a new dynamic symbol, in BFD
3938 void ldlang_ctf_new_dynsym (int symidx
, struct elf_internal_sym
*sym
)
3940 ldemul_new_dynsym_for_ctf (ctf_output
, symidx
, sym
);
3943 /* Write out the CTF section. Called early, if the emulation isn't going to
3944 need to dedup against the strtab and symtab, then possibly called from the
3945 target linker code if the dedup has happened. */
3947 lang_write_ctf (int late
)
3950 asection
*output_sect
;
3957 /* Emit CTF late if this emulation says it can do so. */
3958 if (ldemul_emit_ctf_early ())
3963 if (!ldemul_emit_ctf_early ())
3967 /* Inform the emulation that all the symbols that will be received have
3970 ldemul_new_dynsym_for_ctf (ctf_output
, 0, NULL
);
3974 output_sect
= bfd_get_section_by_name (link_info
.output_bfd
, ".ctf");
3977 output_sect
->contents
= ctf_link_write (ctf_output
, &output_size
,
3978 CTF_COMPRESSION_THRESHOLD
);
3979 output_sect
->size
= output_size
;
3980 output_sect
->flags
|= SEC_IN_MEMORY
| SEC_KEEP
;
3982 lang_ctf_errs_warnings (ctf_output
);
3983 if (!output_sect
->contents
)
3985 einfo (_("%P: warning: CTF section emission failed; "
3986 "output will have no CTF section: %s\n"),
3987 ctf_errmsg (ctf_errno (ctf_output
)));
3988 output_sect
->size
= 0;
3989 output_sect
->flags
|= SEC_EXCLUDE
;
3993 /* This also closes every CTF input file used in the link. */
3994 ctf_dict_close (ctf_output
);
3997 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3998 file
->the_ctf
= NULL
;
4001 /* Write out the CTF section late, if the emulation needs that. */
4004 ldlang_write_ctf_late (void)
4006 /* Trigger a "late call", if the emulation needs one. */
4012 ldlang_open_ctf (void)
4014 LANG_FOR_EACH_INPUT_STATEMENT (file
)
4018 /* If built without CTF, warn and delete all CTF sections from the output.
4019 (The alternative would be to simply concatenate them, which does not
4020 yield a valid CTF section.) */
4022 if ((sect
= bfd_get_section_by_name (file
->the_bfd
, ".ctf")) != NULL
)
4024 einfo (_("%P: warning: CTF section in %pB not linkable: "
4025 "%P was built without support for CTF\n"), file
->the_bfd
);
4027 sect
->flags
|= SEC_EXCLUDE
;
4032 static void lang_merge_ctf (void) {}
4034 ldlang_ctf_acquire_strings (struct elf_strtab_hash
*dynstrtab
4035 ATTRIBUTE_UNUSED
) {}
4037 ldlang_ctf_new_dynsym (int symidx ATTRIBUTE_UNUSED
,
4038 struct elf_internal_sym
*sym ATTRIBUTE_UNUSED
) {}
4039 static void lang_write_ctf (int late ATTRIBUTE_UNUSED
) {}
4040 void ldlang_write_ctf_late (void) {}
4043 /* Add the supplied name to the symbol table as an undefined reference.
4044 This is a two step process as the symbol table doesn't even exist at
4045 the time the ld command line is processed. First we put the name
4046 on a list, then, once the output file has been opened, transfer the
4047 name to the symbol table. */
4049 typedef struct bfd_sym_chain ldlang_undef_chain_list_type
;
4051 #define ldlang_undef_chain_list_head entry_symbol.next
4054 ldlang_add_undef (const char *const name
, bool cmdline ATTRIBUTE_UNUSED
)
4056 ldlang_undef_chain_list_type
*new_undef
;
4058 new_undef
= stat_alloc (sizeof (*new_undef
));
4059 new_undef
->next
= ldlang_undef_chain_list_head
;
4060 ldlang_undef_chain_list_head
= new_undef
;
4062 new_undef
->name
= stat_strdup (name
);
4064 if (link_info
.output_bfd
!= NULL
)
4065 insert_undefined (new_undef
->name
);
4068 /* Insert NAME as undefined in the symbol table. */
4071 insert_undefined (const char *name
)
4073 struct bfd_link_hash_entry
*h
;
4075 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, false, true);
4077 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
4078 if (h
->type
== bfd_link_hash_new
)
4080 h
->type
= bfd_link_hash_undefined
;
4081 h
->u
.undef
.abfd
= NULL
;
4082 h
->non_ir_ref_regular
= true;
4083 bfd_link_add_undef (link_info
.hash
, h
);
4087 /* Run through the list of undefineds created above and place them
4088 into the linker hash table as undefined symbols belonging to the
4092 lang_place_undefineds (void)
4094 ldlang_undef_chain_list_type
*ptr
;
4096 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
4097 insert_undefined (ptr
->name
);
4100 /* Mark -u symbols against garbage collection. */
4103 lang_mark_undefineds (void)
4105 ldlang_undef_chain_list_type
*ptr
;
4107 if (is_elf_hash_table (link_info
.hash
))
4108 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
4110 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*)
4111 bfd_link_hash_lookup (link_info
.hash
, ptr
->name
, false, false, true);
4117 /* Structure used to build the list of symbols that the user has required
4120 struct require_defined_symbol
4123 struct require_defined_symbol
*next
;
4126 /* The list of symbols that the user has required be defined. */
4128 static struct require_defined_symbol
*require_defined_symbol_list
;
4130 /* Add a new symbol NAME to the list of symbols that are required to be
4134 ldlang_add_require_defined (const char *const name
)
4136 struct require_defined_symbol
*ptr
;
4138 ldlang_add_undef (name
, true);
4139 ptr
= stat_alloc (sizeof (*ptr
));
4140 ptr
->next
= require_defined_symbol_list
;
4141 ptr
->name
= stat_strdup (name
);
4142 require_defined_symbol_list
= ptr
;
4145 /* Check that all symbols the user required to be defined, are defined,
4146 raise an error if we find a symbol that is not defined. */
4149 ldlang_check_require_defined_symbols (void)
4151 struct require_defined_symbol
*ptr
;
4153 for (ptr
= require_defined_symbol_list
; ptr
!= NULL
; ptr
= ptr
->next
)
4155 struct bfd_link_hash_entry
*h
;
4157 h
= bfd_link_hash_lookup (link_info
.hash
, ptr
->name
,
4158 false, false, true);
4160 || (h
->type
!= bfd_link_hash_defined
4161 && h
->type
!= bfd_link_hash_defweak
))
4162 einfo(_("%X%P: required symbol `%s' not defined\n"), ptr
->name
);
4166 /* Check for all readonly or some readwrite sections. */
4169 check_input_sections
4170 (lang_statement_union_type
*s
,
4171 lang_output_section_statement_type
*output_section_statement
)
4173 for (; s
!= NULL
; s
= s
->header
.next
)
4175 switch (s
->header
.type
)
4177 case lang_wild_statement_enum
:
4178 walk_wild (&s
->wild_statement
, check_section_callback
,
4179 output_section_statement
);
4180 if (!output_section_statement
->all_input_readonly
)
4183 case lang_constructors_statement_enum
:
4184 check_input_sections (constructor_list
.head
,
4185 output_section_statement
);
4186 if (!output_section_statement
->all_input_readonly
)
4189 case lang_group_statement_enum
:
4190 check_input_sections (s
->group_statement
.children
.head
,
4191 output_section_statement
);
4192 if (!output_section_statement
->all_input_readonly
)
4201 /* Update wildcard statements if needed. */
4204 update_wild_statements (lang_statement_union_type
*s
)
4206 struct wildcard_list
*sec
;
4208 switch (sort_section
)
4218 for (; s
!= NULL
; s
= s
->header
.next
)
4220 switch (s
->header
.type
)
4225 case lang_wild_statement_enum
:
4226 for (sec
= s
->wild_statement
.section_list
; sec
!= NULL
;
4228 /* Don't sort .init/.fini sections. */
4229 if (strcmp (sec
->spec
.name
, ".init") != 0
4230 && strcmp (sec
->spec
.name
, ".fini") != 0)
4232 switch (sec
->spec
.sorted
)
4235 sec
->spec
.sorted
= sort_section
;
4238 if (sort_section
== by_alignment
)
4239 sec
->spec
.sorted
= by_name_alignment
;
4242 if (sort_section
== by_name
)
4243 sec
->spec
.sorted
= by_alignment_name
;
4248 s
->wild_statement
.any_specs_sorted
= true;
4252 case lang_constructors_statement_enum
:
4253 update_wild_statements (constructor_list
.head
);
4256 case lang_output_section_statement_enum
:
4257 update_wild_statements
4258 (s
->output_section_statement
.children
.head
);
4261 case lang_group_statement_enum
:
4262 update_wild_statements (s
->group_statement
.children
.head
);
4270 /* Open input files and attach to output sections. */
4273 map_input_to_output_sections
4274 (lang_statement_union_type
*s
, const char *target
,
4275 lang_output_section_statement_type
*os
)
4277 for (; s
!= NULL
; s
= s
->header
.next
)
4279 lang_output_section_statement_type
*tos
;
4281 unsigned int type
= 0;
4283 switch (s
->header
.type
)
4285 case lang_wild_statement_enum
:
4286 wild (&s
->wild_statement
, target
, os
);
4288 case lang_constructors_statement_enum
:
4289 map_input_to_output_sections (constructor_list
.head
,
4293 case lang_output_section_statement_enum
:
4294 tos
= &s
->output_section_statement
;
4295 if (tos
->constraint
== ONLY_IF_RW
4296 || tos
->constraint
== ONLY_IF_RO
)
4298 tos
->all_input_readonly
= true;
4299 check_input_sections (tos
->children
.head
, tos
);
4300 if (tos
->all_input_readonly
!= (tos
->constraint
== ONLY_IF_RO
))
4301 tos
->constraint
= -1;
4303 if (tos
->constraint
>= 0)
4304 map_input_to_output_sections (tos
->children
.head
,
4308 case lang_output_statement_enum
:
4310 case lang_target_statement_enum
:
4311 target
= s
->target_statement
.target
;
4313 case lang_group_statement_enum
:
4314 map_input_to_output_sections (s
->group_statement
.children
.head
,
4318 case lang_data_statement_enum
:
4320 /* This should never happen. */
4322 /* Make sure that any sections mentioned in the expression
4324 exp_init_os (s
->data_statement
.exp
);
4325 /* The output section gets CONTENTS, ALLOC and LOAD, but
4326 these may be overridden by the script. */
4327 flags
= SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
;
4328 switch (os
->sectype
)
4330 case normal_section
:
4331 case overlay_section
:
4332 case first_overlay_section
:
4334 case noalloc_section
:
4335 flags
= SEC_HAS_CONTENTS
;
4337 case readonly_section
:
4338 flags
|= SEC_READONLY
;
4340 case typed_readonly_section
:
4341 flags
|= SEC_READONLY
;
4344 if (os
->sectype_value
->type
.node_class
== etree_name
4345 && os
->sectype_value
->type
.node_code
== NAME
)
4347 const char *name
= os
->sectype_value
->name
.name
;
4348 if (strcmp (name
, "SHT_PROGBITS") == 0)
4349 type
= SHT_PROGBITS
;
4350 else if (strcmp (name
, "SHT_STRTAB") == 0)
4352 else if (strcmp (name
, "SHT_NOTE") == 0)
4354 else if (strcmp (name
, "SHT_NOBITS") == 0)
4356 else if (strcmp (name
, "SHT_INIT_ARRAY") == 0)
4357 type
= SHT_INIT_ARRAY
;
4358 else if (strcmp (name
, "SHT_FINI_ARRAY") == 0)
4359 type
= SHT_FINI_ARRAY
;
4360 else if (strcmp (name
, "SHT_PREINIT_ARRAY") == 0)
4361 type
= SHT_PREINIT_ARRAY
;
4363 einfo (_ ("%F%P: invalid type for output section `%s'\n"),
4368 exp_fold_tree_no_dot (os
->sectype_value
, os
);
4369 if (expld
.result
.valid_p
)
4370 type
= expld
.result
.value
;
4372 einfo (_ ("%F%P: invalid type for output section `%s'\n"),
4376 case noload_section
:
4377 if (bfd_get_flavour (link_info
.output_bfd
)
4378 == bfd_target_elf_flavour
)
4379 flags
= SEC_NEVER_LOAD
| SEC_ALLOC
;
4381 flags
= SEC_NEVER_LOAD
| SEC_HAS_CONTENTS
;
4384 if (os
->bfd_section
== NULL
)
4385 init_os (os
, flags
| SEC_READONLY
);
4387 os
->bfd_section
->flags
|= flags
;
4388 os
->bfd_section
->type
= type
;
4390 case lang_input_section_enum
:
4392 case lang_fill_statement_enum
:
4393 case lang_object_symbols_statement_enum
:
4394 case lang_reloc_statement_enum
:
4395 case lang_padding_statement_enum
:
4396 case lang_input_statement_enum
:
4397 if (os
!= NULL
&& os
->bfd_section
== NULL
)
4401 case lang_assignment_statement_enum
:
4402 if (os
!= NULL
&& os
->bfd_section
== NULL
)
4403 init_os (os
, get_os_init_flag (os
));
4405 /* Make sure that any sections mentioned in the assignment
4407 exp_init_os (s
->assignment_statement
.exp
);
4410 case lang_address_statement_enum
:
4411 /* Mark the specified section with the supplied address.
4412 If this section was actually a segment marker, then the
4413 directive is ignored if the linker script explicitly
4414 processed the segment marker. Originally, the linker
4415 treated segment directives (like -Ttext on the
4416 command-line) as section directives. We honor the
4417 section directive semantics for backwards compatibility;
4418 linker scripts that do not specifically check for
4419 SEGMENT_START automatically get the old semantics. */
4420 if (!s
->address_statement
.segment
4421 || !s
->address_statement
.segment
->used
)
4423 const char *name
= s
->address_statement
.section_name
;
4425 /* Create the output section statement here so that
4426 orphans with a set address will be placed after other
4427 script sections. If we let the orphan placement code
4428 place them in amongst other sections then the address
4429 will affect following script sections, which is
4430 likely to surprise naive users. */
4431 tos
= lang_output_section_statement_lookup (name
, 0, 1);
4432 tos
->addr_tree
= s
->address_statement
.address
;
4433 if (tos
->bfd_section
== NULL
)
4437 case lang_insert_statement_enum
:
4439 case lang_input_matcher_enum
:
4445 /* An insert statement snips out all the linker statements from the
4446 start of the list and places them after the output section
4447 statement specified by the insert. This operation is complicated
4448 by the fact that we keep a doubly linked list of output section
4449 statements as well as the singly linked list of all statements.
4450 FIXME someday: Twiddling with the list not only moves statements
4451 from the user's script but also input and group statements that are
4452 built from command line object files and --start-group. We only
4453 get away with this because the list pointers used by file_chain
4454 and input_file_chain are not reordered, and processing via
4455 statement_list after this point mostly ignores input statements.
4456 One exception is the map file, where LOAD and START GROUP/END GROUP
4457 can end up looking odd. */
4460 process_insert_statements (lang_statement_union_type
**start
)
4462 lang_statement_union_type
**s
;
4463 lang_output_section_statement_type
*first_os
= NULL
;
4464 lang_output_section_statement_type
*last_os
= NULL
;
4465 lang_output_section_statement_type
*os
;
4470 if ((*s
)->header
.type
== lang_output_section_statement_enum
)
4472 /* Keep pointers to the first and last output section
4473 statement in the sequence we may be about to move. */
4474 os
= &(*s
)->output_section_statement
;
4476 ASSERT (last_os
== NULL
|| last_os
->next
== os
);
4479 /* Set constraint negative so that lang_output_section_find
4480 won't match this output section statement. At this
4481 stage in linking constraint has values in the range
4482 [-1, ONLY_IN_RW]. */
4483 last_os
->constraint
= -2 - last_os
->constraint
;
4484 if (first_os
== NULL
)
4487 else if ((*s
)->header
.type
== lang_group_statement_enum
)
4489 /* A user might put -T between --start-group and
4490 --end-group. One way this odd construct might arise is
4491 from a wrapper around ld to change library search
4492 behaviour. For example:
4494 exec real_ld --start-group "$@" --end-group
4495 This isn't completely unreasonable so go looking inside a
4496 group statement for insert statements. */
4497 process_insert_statements (&(*s
)->group_statement
.children
.head
);
4499 else if ((*s
)->header
.type
== lang_insert_statement_enum
)
4501 lang_insert_statement_type
*i
= &(*s
)->insert_statement
;
4502 lang_output_section_statement_type
*where
;
4503 lang_statement_union_type
**ptr
;
4504 lang_statement_union_type
*first
;
4506 if (link_info
.non_contiguous_regions
)
4508 einfo (_("warning: INSERT statement in linker script is "
4509 "incompatible with --enable-non-contiguous-regions.\n"));
4512 where
= lang_output_section_find (i
->where
);
4513 if (where
!= NULL
&& i
->is_before
)
4516 where
= where
->prev
;
4517 while (where
!= NULL
&& where
->constraint
< 0);
4521 einfo (_("%F%P: %s not found for insert\n"), i
->where
);
4525 /* Deal with reordering the output section statement list. */
4526 if (last_os
!= NULL
)
4528 asection
*first_sec
, *last_sec
;
4529 struct lang_output_section_statement_struct
**next
;
4531 /* Snip out the output sections we are moving. */
4532 first_os
->prev
->next
= last_os
->next
;
4533 if (last_os
->next
== NULL
)
4535 next
= &first_os
->prev
->next
;
4536 lang_os_list
.tail
= (lang_statement_union_type
**) next
;
4539 last_os
->next
->prev
= first_os
->prev
;
4540 /* Add them in at the new position. */
4541 last_os
->next
= where
->next
;
4542 if (where
->next
== NULL
)
4544 next
= &last_os
->next
;
4545 lang_os_list
.tail
= (lang_statement_union_type
**) next
;
4548 where
->next
->prev
= last_os
;
4549 first_os
->prev
= where
;
4550 where
->next
= first_os
;
4552 /* Move the bfd sections in the same way. */
4555 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
4557 os
->constraint
= -2 - os
->constraint
;
4558 if (os
->bfd_section
!= NULL
4559 && os
->bfd_section
->owner
!= NULL
)
4561 last_sec
= os
->bfd_section
;
4562 if (first_sec
== NULL
)
4563 first_sec
= last_sec
;
4568 if (last_sec
!= NULL
)
4570 asection
*sec
= where
->bfd_section
;
4572 sec
= output_prev_sec_find (where
);
4574 /* The place we want to insert must come after the
4575 sections we are moving. So if we find no
4576 section or if the section is the same as our
4577 last section, then no move is needed. */
4578 if (sec
!= NULL
&& sec
!= last_sec
)
4580 /* Trim them off. */
4581 if (first_sec
->prev
!= NULL
)
4582 first_sec
->prev
->next
= last_sec
->next
;
4584 link_info
.output_bfd
->sections
= last_sec
->next
;
4585 if (last_sec
->next
!= NULL
)
4586 last_sec
->next
->prev
= first_sec
->prev
;
4588 link_info
.output_bfd
->section_last
= first_sec
->prev
;
4590 if (sec
->owner
== NULL
)
4591 /* SEC is the absolute section, from the
4592 first dummy output section statement. Add
4593 back the sections we trimmed off to the
4594 start of the bfd sections. */
4597 last_sec
->next
= sec
->next
;
4599 last_sec
->next
= link_info
.output_bfd
->sections
;
4600 if (last_sec
->next
!= NULL
)
4601 last_sec
->next
->prev
= last_sec
;
4603 link_info
.output_bfd
->section_last
= last_sec
;
4604 first_sec
->prev
= sec
;
4605 if (first_sec
->prev
!= NULL
)
4606 first_sec
->prev
->next
= first_sec
;
4608 link_info
.output_bfd
->sections
= first_sec
;
4613 lang_statement_union_type
*after
= (void *) where
;
4614 if (where
== &lang_os_list
.head
->output_section_statement
4615 && where
->next
== first_os
)
4617 /* PR30155. Handle a corner case where the statement
4618 list is something like the following:
4620 . .data 0x0000000000000000 0x0
4621 . [0x0000000000000000] b = .
4623 . .data 0x0000000000000000 0x0 t.o
4624 . 0x0000000000000000 0x4 LONG 0x0
4625 . INSERT BEFORE .text.start
4626 . [0x0000000000000004] a = .
4627 . .text.start 0x0000000000000000 0x0
4628 . [0x0000000000000000] c = .
4629 . OUTPUT(a.out elf64-x86-64)
4630 Here we do not want to allow insert_os_after to
4631 choose a point inside the list we are moving.
4632 That would lose the list. Instead, let
4633 insert_os_after work from the INSERT, which in this
4634 particular example will result in inserting after
4635 the assignment "a = .". */
4638 ptr
= insert_os_after (after
);
4639 /* Snip everything from the start of the list, up to and
4640 including the insert statement we are currently processing. */
4642 *start
= (*s
)->header
.next
;
4643 /* Add them back where they belong, minus the insert. */
4646 statement_list
.tail
= s
;
4653 s
= &(*s
)->header
.next
;
4656 /* Undo constraint twiddling. */
4657 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
4659 os
->constraint
= -2 - os
->constraint
;
4665 /* An output section might have been removed after its statement was
4666 added. For example, ldemul_before_allocation can remove dynamic
4667 sections if they turn out to be not needed. Clean them up here. */
4670 strip_excluded_output_sections (void)
4672 lang_output_section_statement_type
*os
;
4674 /* Run lang_size_sections (if not already done). */
4675 if (expld
.phase
!= lang_mark_phase_enum
)
4677 expld
.phase
= lang_mark_phase_enum
;
4678 expld
.dataseg
.phase
= exp_seg_none
;
4679 one_lang_size_sections_pass (NULL
, false);
4680 lang_reset_memory_regions ();
4683 for (os
= (void *) lang_os_list
.head
;
4687 asection
*output_section
;
4690 if (os
->constraint
< 0)
4693 output_section
= os
->bfd_section
;
4694 if (output_section
== NULL
)
4697 exclude
= (output_section
->rawsize
== 0
4698 && (output_section
->flags
& SEC_KEEP
) == 0
4699 && !bfd_section_removed_from_list (link_info
.output_bfd
,
4702 /* Some sections have not yet been sized, notably .gnu.version,
4703 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
4704 input sections, so don't drop output sections that have such
4705 input sections unless they are also marked SEC_EXCLUDE. */
4706 if (exclude
&& output_section
->map_head
.s
!= NULL
)
4710 for (s
= output_section
->map_head
.s
; s
!= NULL
; s
= s
->map_head
.s
)
4711 if ((s
->flags
& SEC_EXCLUDE
) == 0
4712 && ((s
->flags
& SEC_LINKER_CREATED
) != 0
4713 || link_info
.emitrelocations
))
4722 /* We don't set bfd_section to NULL since bfd_section of the
4723 removed output section statement may still be used. */
4724 if (!os
->update_dot
)
4726 output_section
->flags
|= SEC_EXCLUDE
;
4727 bfd_section_list_remove (link_info
.output_bfd
, output_section
);
4728 link_info
.output_bfd
->section_count
--;
4733 /* Called from ldwrite to clear out asection.map_head and
4734 asection.map_tail for use as link_orders in ldwrite. */
4737 lang_clear_os_map (void)
4739 lang_output_section_statement_type
*os
;
4741 if (map_head_is_link_order
)
4744 for (os
= (void *) lang_os_list
.head
;
4748 asection
*output_section
;
4750 if (os
->constraint
< 0)
4753 output_section
= os
->bfd_section
;
4754 if (output_section
== NULL
)
4757 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
4758 output_section
->map_head
.link_order
= NULL
;
4759 output_section
->map_tail
.link_order
= NULL
;
4762 /* Stop future calls to lang_add_section from messing with map_head
4763 and map_tail link_order fields. */
4764 map_head_is_link_order
= true;
4768 print_output_section_statement
4769 (lang_output_section_statement_type
*output_section_statement
)
4771 asection
*section
= output_section_statement
->bfd_section
;
4774 if (output_section_statement
!= abs_output_section
)
4776 minfo ("\n%s", output_section_statement
->name
);
4778 if (section
!= NULL
)
4780 print_dot
= section
->vma
;
4782 len
= strlen (output_section_statement
->name
);
4783 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
4788 print_spaces (SECTION_NAME_MAP_LENGTH
- len
);
4790 minfo ("0x%V %W", section
->vma
, TO_ADDR (section
->size
));
4792 if (section
->vma
!= section
->lma
)
4793 minfo (_(" load address 0x%V"), section
->lma
);
4795 if (output_section_statement
->update_dot_tree
!= NULL
)
4796 exp_fold_tree (output_section_statement
->update_dot_tree
,
4797 output_section_statement
,
4798 bfd_abs_section_ptr
, &print_dot
);
4804 print_statement_list (output_section_statement
->children
.head
,
4805 output_section_statement
);
4809 print_assignment (lang_assignment_statement_type
*assignment
,
4810 lang_output_section_statement_type
*output_section
)
4816 print_spaces (SECTION_NAME_MAP_LENGTH
);
4818 if (assignment
->exp
->type
.node_class
== etree_assert
)
4821 tree
= assignment
->exp
->assert_s
.child
;
4825 const char *dst
= assignment
->exp
->assign
.dst
;
4827 is_dot
= (dst
[0] == '.' && dst
[1] == 0);
4828 tree
= assignment
->exp
;
4831 osec
= output_section
->bfd_section
;
4833 osec
= bfd_abs_section_ptr
;
4835 if (assignment
->exp
->type
.node_class
!= etree_provide
)
4836 exp_fold_tree (tree
, output_section
, osec
, &print_dot
);
4838 expld
.result
.valid_p
= false;
4841 const char *str
= buf
;
4842 if (expld
.result
.valid_p
)
4846 if (assignment
->exp
->type
.node_class
== etree_assert
4848 || expld
.assign_name
!= NULL
)
4850 value
= expld
.result
.value
;
4852 if (expld
.result
.section
!= NULL
)
4853 value
+= expld
.result
.section
->vma
;
4857 bfd_sprintf_vma (link_info
.output_bfd
, buf
+ 2, value
);
4863 struct bfd_link_hash_entry
*h
;
4865 h
= bfd_link_hash_lookup (link_info
.hash
, assignment
->exp
->assign
.dst
,
4866 false, false, true);
4868 && (h
->type
== bfd_link_hash_defined
4869 || h
->type
== bfd_link_hash_defweak
))
4871 value
= h
->u
.def
.value
;
4872 value
+= h
->u
.def
.section
->output_section
->vma
;
4873 value
+= h
->u
.def
.section
->output_offset
;
4878 bfd_sprintf_vma (link_info
.output_bfd
, buf
+ 3, value
);
4882 str
= "[unresolved]";
4887 if (assignment
->exp
->type
.node_class
== etree_provide
)
4892 expld
.assign_name
= NULL
;
4894 fprintf (config
.map_file
, "%-34s", str
);
4895 exp_print_tree (assignment
->exp
);
4900 print_input_statement (lang_input_statement_type
*statm
)
4902 if (statm
->filename
!= NULL
)
4903 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
4906 /* Print all symbols defined in a particular section. This is called
4907 via bfd_link_hash_traverse, or by print_all_symbols. */
4910 print_one_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
)
4912 asection
*sec
= (asection
*) ptr
;
4914 if ((hash_entry
->type
== bfd_link_hash_defined
4915 || hash_entry
->type
== bfd_link_hash_defweak
)
4916 && sec
== hash_entry
->u
.def
.section
)
4918 print_spaces (SECTION_NAME_MAP_LENGTH
);
4920 (hash_entry
->u
.def
.value
4921 + hash_entry
->u
.def
.section
->output_offset
4922 + hash_entry
->u
.def
.section
->output_section
->vma
));
4924 minfo (" %pT\n", hash_entry
->root
.string
);
4931 hash_entry_addr_cmp (const void *a
, const void *b
)
4933 const struct bfd_link_hash_entry
*l
= *(const struct bfd_link_hash_entry
**)a
;
4934 const struct bfd_link_hash_entry
*r
= *(const struct bfd_link_hash_entry
**)b
;
4936 if (l
->u
.def
.value
< r
->u
.def
.value
)
4938 else if (l
->u
.def
.value
> r
->u
.def
.value
)
4945 print_all_symbols (asection
*sec
)
4947 input_section_userdata_type
*ud
= bfd_section_userdata (sec
);
4948 struct map_symbol_def
*def
;
4949 struct bfd_link_hash_entry
**entries
;
4955 *ud
->map_symbol_def_tail
= 0;
4957 /* Sort the symbols by address. */
4958 entries
= (struct bfd_link_hash_entry
**)
4959 obstack_alloc (&map_obstack
,
4960 ud
->map_symbol_def_count
* sizeof (*entries
));
4962 for (i
= 0, def
= ud
->map_symbol_def_head
; def
; def
= def
->next
, i
++)
4963 entries
[i
] = def
->entry
;
4965 qsort (entries
, ud
->map_symbol_def_count
, sizeof (*entries
),
4966 hash_entry_addr_cmp
);
4968 /* Print the symbols. */
4969 for (i
= 0; i
< ud
->map_symbol_def_count
; i
++)
4970 ldemul_print_symbol (entries
[i
], sec
);
4972 obstack_free (&map_obstack
, entries
);
4975 /* Returns TRUE if SYM is a symbol suitable for printing
4976 in a linker map as a local symbol. */
4979 ld_is_local_symbol (asymbol
* sym
)
4981 const char * name
= bfd_asymbol_name (sym
);
4983 if (name
== NULL
|| *name
== 0)
4986 /* Skip .Lxxx and such like. */
4987 if (bfd_is_local_label (link_info
.output_bfd
, sym
))
4990 /* FIXME: This is intended to skip ARM mapping symbols,
4991 which for some reason are not excluded by bfd_is_local_label,
4992 but maybe it is wrong for other architectures.
4993 It would be better to fix bfd_is_local_label. */
4997 /* Some local symbols, eg _GLOBAL_OFFSET_TABLE_, are present
4998 in the hash table, so do not print duplicates here. */
4999 struct bfd_link_hash_entry
* h
;
5000 h
= bfd_link_hash_lookup (link_info
.hash
, name
, false /* create */,
5001 false /* copy */, true /* follow */);
5005 /* Symbols from the plugin owned BFD will not get their own
5006 iteration of this function, but can be on the link_info
5007 list. So include them here. */
5008 if (h
->u
.def
.section
->owner
!= NULL
5009 && ((bfd_get_file_flags (h
->u
.def
.section
->owner
) & (BFD_LINKER_CREATED
| BFD_PLUGIN
))
5010 == (BFD_LINKER_CREATED
| BFD_PLUGIN
)))
5016 /* Print information about an input section to the map file. */
5019 print_input_section (asection
*i
, bool is_discarded
)
5021 bfd_size_type size
= i
->size
;
5027 minfo (" %s", i
->name
);
5029 len
= 1 + strlen (i
->name
);
5030 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
5035 print_spaces (SECTION_NAME_MAP_LENGTH
- len
);
5037 if (i
->output_section
!= NULL
5038 && i
->output_section
->owner
== link_info
.output_bfd
)
5039 addr
= i
->output_section
->vma
+ i
->output_offset
;
5048 bfd_sprintf_vma (link_info
.output_bfd
, buf
, addr
);
5049 minfo ("0x%s %W %pB\n", buf
, TO_ADDR (size
), i
->owner
);
5051 if (size
!= i
->rawsize
&& i
->rawsize
!= 0)
5053 len
= SECTION_NAME_MAP_LENGTH
+ 3 + strlen (buf
);
5055 minfo (_("%W (size before relaxing)\n"), TO_ADDR (i
->rawsize
));
5058 if (i
->output_section
!= NULL
5059 && i
->output_section
->owner
== link_info
.output_bfd
)
5061 if (link_info
.reduce_memory_overheads
)
5062 bfd_link_hash_traverse (link_info
.hash
, ldemul_print_symbol
, i
);
5064 print_all_symbols (i
);
5066 /* Update print_dot, but make sure that we do not move it
5067 backwards - this could happen if we have overlays and a
5068 later overlay is shorter than an earier one. */
5069 if (addr
+ TO_ADDR (size
) > print_dot
)
5070 print_dot
= addr
+ TO_ADDR (size
);
5072 if (config
.print_map_locals
)
5074 long storage_needed
;
5076 /* FIXME: It would be better to cache this table, rather
5077 than recreating it for each output section. */
5078 /* FIXME: This call is not working for non-ELF based targets.
5080 storage_needed
= bfd_get_symtab_upper_bound (link_info
.output_bfd
);
5081 if (storage_needed
> 0)
5083 asymbol
** symbol_table
;
5084 long number_of_symbols
;
5087 symbol_table
= xmalloc (storage_needed
);
5088 number_of_symbols
= bfd_canonicalize_symtab (link_info
.output_bfd
, symbol_table
);
5090 for (j
= 0; j
< number_of_symbols
; j
++)
5092 asymbol
* sym
= symbol_table
[j
];
5093 bfd_vma sym_addr
= sym
->value
+ i
->output_section
->vma
;
5095 if (sym
->section
== i
->output_section
5096 && (sym
->flags
& BSF_LOCAL
) != 0
5098 && sym_addr
< print_dot
5099 && ld_is_local_symbol (sym
))
5101 print_spaces (SECTION_NAME_MAP_LENGTH
);
5102 minfo ("0x%V (local) %s\n", sym_addr
, bfd_asymbol_name (sym
));
5106 free (symbol_table
);
5113 print_fill_statement (lang_fill_statement_type
*fill
)
5117 fputs (" FILL mask 0x", config
.map_file
);
5118 for (p
= fill
->fill
->data
, size
= fill
->fill
->size
; size
!= 0; p
++, size
--)
5119 fprintf (config
.map_file
, "%02x", *p
);
5120 fputs ("\n", config
.map_file
);
5124 print_data_statement (lang_data_statement_type
*data
)
5130 init_opb (data
->output_section
);
5131 print_spaces (SECTION_NAME_MAP_LENGTH
);
5133 addr
= data
->output_offset
;
5134 if (data
->output_section
!= NULL
)
5135 addr
+= data
->output_section
->vma
;
5163 if (size
< TO_SIZE ((unsigned) 1))
5164 size
= TO_SIZE ((unsigned) 1);
5165 minfo ("0x%V %W %s 0x%v", addr
, TO_ADDR (size
), name
, data
->value
);
5167 if (data
->exp
->type
.node_class
!= etree_value
)
5170 exp_print_tree (data
->exp
);
5175 print_dot
= addr
+ TO_ADDR (size
);
5178 /* Print an address statement. These are generated by options like
5182 print_address_statement (lang_address_statement_type
*address
)
5184 minfo (_("Address of section %s set to "), address
->section_name
);
5185 exp_print_tree (address
->address
);
5189 /* Print a reloc statement. */
5192 print_reloc_statement (lang_reloc_statement_type
*reloc
)
5197 init_opb (reloc
->output_section
);
5198 print_spaces (SECTION_NAME_MAP_LENGTH
);
5200 addr
= reloc
->output_offset
;
5201 if (reloc
->output_section
!= NULL
)
5202 addr
+= reloc
->output_section
->vma
;
5204 size
= bfd_get_reloc_size (reloc
->howto
);
5206 minfo ("0x%V %W RELOC %s ", addr
, TO_ADDR (size
), reloc
->howto
->name
);
5208 if (reloc
->name
!= NULL
)
5209 minfo ("%s+", reloc
->name
);
5211 minfo ("%s+", reloc
->section
->name
);
5213 exp_print_tree (reloc
->addend_exp
);
5217 print_dot
= addr
+ TO_ADDR (size
);
5221 print_padding_statement (lang_padding_statement_type
*s
)
5226 init_opb (s
->output_section
);
5229 len
= sizeof " *fill*" - 1;
5230 print_spaces (SECTION_NAME_MAP_LENGTH
- len
);
5232 addr
= s
->output_offset
;
5233 if (s
->output_section
!= NULL
)
5234 addr
+= s
->output_section
->vma
;
5235 minfo ("0x%V %W ", addr
, TO_ADDR (s
->size
));
5237 if (s
->fill
->size
!= 0)
5241 for (p
= s
->fill
->data
, size
= s
->fill
->size
; size
!= 0; p
++, size
--)
5242 fprintf (config
.map_file
, "%02x", *p
);
5247 print_dot
= addr
+ TO_ADDR (s
->size
);
5251 print_wild_statement (lang_wild_statement_type
*w
,
5252 lang_output_section_statement_type
*os
)
5254 struct wildcard_list
*sec
;
5258 if (w
->exclude_name_list
)
5261 minfo ("EXCLUDE_FILE(%s", w
->exclude_name_list
->name
);
5262 for (tmp
= w
->exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
5263 minfo (" %s", tmp
->name
);
5267 if (w
->filenames_sorted
)
5268 minfo ("SORT_BY_NAME(");
5269 if (w
->filenames_reversed
)
5271 if (w
->filename
!= NULL
)
5272 minfo ("%s", w
->filename
);
5275 if (w
->filenames_reversed
)
5277 if (w
->filenames_sorted
)
5281 for (sec
= w
->section_list
; sec
; sec
= sec
->next
)
5283 int closing_paren
= 0;
5285 switch (sec
->spec
.sorted
)
5291 minfo ("SORT_BY_NAME(");
5296 minfo ("SORT_BY_ALIGNMENT(");
5300 case by_name_alignment
:
5301 minfo ("SORT_BY_NAME(SORT_BY_ALIGNMENT(");
5305 case by_alignment_name
:
5306 minfo ("SORT_BY_ALIGNMENT(SORT_BY_NAME(");
5311 minfo ("SORT_NONE(");
5315 case by_init_priority
:
5316 minfo ("SORT_BY_INIT_PRIORITY(");
5321 if (sec
->spec
.reversed
)
5327 if (sec
->spec
.exclude_name_list
!= NULL
)
5330 minfo ("EXCLUDE_FILE(%s", sec
->spec
.exclude_name_list
->name
);
5331 for (tmp
= sec
->spec
.exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
5332 minfo (" %s", tmp
->name
);
5335 if (sec
->spec
.name
!= NULL
)
5336 minfo ("%s", sec
->spec
.name
);
5339 for (;closing_paren
> 0; closing_paren
--)
5348 print_statement_list (w
->children
.head
, os
);
5351 /* Print a group statement. */
5354 print_group (lang_group_statement_type
*s
,
5355 lang_output_section_statement_type
*os
)
5357 fprintf (config
.map_file
, "START GROUP\n");
5358 print_statement_list (s
->children
.head
, os
);
5359 fprintf (config
.map_file
, "END GROUP\n");
5362 /* Print the list of statements in S.
5363 This can be called for any statement type. */
5366 print_statement_list (lang_statement_union_type
*s
,
5367 lang_output_section_statement_type
*os
)
5371 print_statement (s
, os
);
5376 /* Print the first statement in statement list S.
5377 This can be called for any statement type. */
5380 print_statement (lang_statement_union_type
*s
,
5381 lang_output_section_statement_type
*os
)
5383 switch (s
->header
.type
)
5386 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
5389 case lang_constructors_statement_enum
:
5390 if (constructor_list
.head
!= NULL
)
5392 if (constructors_sorted
)
5393 minfo (" SORT (CONSTRUCTORS)\n");
5395 minfo (" CONSTRUCTORS\n");
5396 print_statement_list (constructor_list
.head
, os
);
5399 case lang_wild_statement_enum
:
5400 print_wild_statement (&s
->wild_statement
, os
);
5402 case lang_address_statement_enum
:
5403 print_address_statement (&s
->address_statement
);
5405 case lang_object_symbols_statement_enum
:
5406 minfo (" CREATE_OBJECT_SYMBOLS\n");
5408 case lang_fill_statement_enum
:
5409 print_fill_statement (&s
->fill_statement
);
5411 case lang_data_statement_enum
:
5412 print_data_statement (&s
->data_statement
);
5414 case lang_reloc_statement_enum
:
5415 print_reloc_statement (&s
->reloc_statement
);
5417 case lang_input_section_enum
:
5418 print_input_section (s
->input_section
.section
, false);
5420 case lang_padding_statement_enum
:
5421 print_padding_statement (&s
->padding_statement
);
5423 case lang_output_section_statement_enum
:
5424 print_output_section_statement (&s
->output_section_statement
);
5426 case lang_assignment_statement_enum
:
5427 print_assignment (&s
->assignment_statement
, os
);
5429 case lang_target_statement_enum
:
5430 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
5432 case lang_output_statement_enum
:
5433 minfo ("OUTPUT(%s", s
->output_statement
.name
);
5434 if (output_target
!= NULL
)
5435 minfo (" %s", output_target
);
5438 case lang_input_statement_enum
:
5439 print_input_statement (&s
->input_statement
);
5441 case lang_group_statement_enum
:
5442 print_group (&s
->group_statement
, os
);
5444 case lang_insert_statement_enum
:
5445 minfo ("INSERT %s %s\n",
5446 s
->insert_statement
.is_before
? "BEFORE" : "AFTER",
5447 s
->insert_statement
.where
);
5453 print_statements (void)
5455 print_statement_list (statement_list
.head
, abs_output_section
);
5458 /* Print the first N statements in statement list S to STDERR.
5459 If N == 0, nothing is printed.
5460 If N < 0, the entire list is printed.
5461 Intended to be called from GDB. */
5464 dprint_statement (lang_statement_union_type
*s
, int n
)
5466 FILE *map_save
= config
.map_file
;
5468 config
.map_file
= stderr
;
5471 print_statement_list (s
, abs_output_section
);
5474 while (s
&& --n
>= 0)
5476 print_statement (s
, abs_output_section
);
5481 config
.map_file
= map_save
;
5485 insert_pad (lang_statement_union_type
**ptr
,
5487 bfd_size_type alignment_needed
,
5488 asection
*output_section
,
5491 static fill_type zero_fill
;
5492 lang_statement_union_type
*pad
= NULL
;
5494 if (ptr
!= &statement_list
.head
)
5495 pad
= ((lang_statement_union_type
*)
5496 ((char *) ptr
- offsetof (lang_statement_union_type
, header
.next
)));
5498 && pad
->header
.type
== lang_padding_statement_enum
5499 && pad
->padding_statement
.output_section
== output_section
)
5501 /* Use the existing pad statement. */
5503 else if ((pad
= *ptr
) != NULL
5504 && pad
->header
.type
== lang_padding_statement_enum
5505 && pad
->padding_statement
.output_section
== output_section
)
5507 /* Use the existing pad statement. */
5511 /* Make a new padding statement, linked into existing chain. */
5512 pad
= stat_alloc (sizeof (lang_padding_statement_type
));
5513 pad
->header
.next
= *ptr
;
5515 pad
->header
.type
= lang_padding_statement_enum
;
5516 pad
->padding_statement
.output_section
= output_section
;
5519 pad
->padding_statement
.fill
= fill
;
5521 pad
->padding_statement
.output_offset
= dot
- output_section
->vma
;
5522 pad
->padding_statement
.size
= alignment_needed
;
5523 if (!(output_section
->flags
& SEC_FIXED_SIZE
))
5524 output_section
->size
= TO_SIZE (dot
+ TO_ADDR (alignment_needed
)
5525 - output_section
->vma
);
5528 /* Work out how much this section will move the dot point. */
5532 (lang_statement_union_type
**this_ptr
,
5533 lang_output_section_statement_type
*output_section_statement
,
5538 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
5539 asection
*i
= is
->section
;
5540 asection
*o
= output_section_statement
->bfd_section
;
5543 if (link_info
.non_contiguous_regions
)
5545 /* If the input section I has already been successfully assigned
5546 to an output section other than O, don't bother with it and
5547 let the caller remove it from the list. Keep processing in
5548 case we have already handled O, because the repeated passes
5549 have reinitialized its size. */
5550 if (i
->already_assigned
&& i
->already_assigned
!= o
)
5557 if (i
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
5558 i
->output_offset
= i
->vma
- o
->vma
;
5559 else if (((i
->flags
& SEC_EXCLUDE
) != 0)
5560 || output_section_statement
->ignored
)
5561 i
->output_offset
= dot
- o
->vma
;
5564 bfd_size_type alignment_needed
;
5566 /* Align this section first to the input sections requirement,
5567 then to the output section's requirement. If this alignment
5568 is greater than any seen before, then record it too. Perform
5569 the alignment by inserting a magic 'padding' statement.
5570 We can force input section alignment within an output section
5571 by using SUBALIGN. The value specified overrides any alignment
5572 given by input sections, whether larger or smaller. */
5574 if (output_section_statement
->subsection_alignment
!= NULL
)
5575 o
->alignment_power
= i
->alignment_power
=
5576 exp_get_power (output_section_statement
->subsection_alignment
,
5577 output_section_statement
,
5578 "subsection alignment");
5580 if (o
->alignment_power
< i
->alignment_power
)
5581 o
->alignment_power
= i
->alignment_power
;
5583 alignment_needed
= align_power (dot
, i
->alignment_power
) - dot
;
5585 if (alignment_needed
!= 0)
5587 insert_pad (this_ptr
, fill
, TO_SIZE (alignment_needed
), o
, dot
);
5588 dot
+= alignment_needed
;
5591 if (link_info
.non_contiguous_regions
)
5593 /* If I would overflow O, let the caller remove I from the
5595 if (output_section_statement
->region
)
5597 bfd_vma end
= output_section_statement
->region
->origin
5598 + output_section_statement
->region
->length
;
5600 if (dot
+ TO_ADDR (i
->size
) > end
)
5602 if (i
->flags
& SEC_LINKER_CREATED
)
5603 einfo (_("%F%P: Output section `%pA' not large enough for "
5604 "the linker-created stubs section `%pA'.\n"),
5605 i
->output_section
, i
);
5607 if (i
->rawsize
&& i
->rawsize
!= i
->size
)
5608 einfo (_("%F%P: Relaxation not supported with "
5609 "--enable-non-contiguous-regions (section `%pA' "
5610 "would overflow `%pA' after it changed size).\n"),
5611 i
, i
->output_section
);
5615 i
->output_section
= NULL
;
5621 /* Remember where in the output section this input section goes. */
5622 i
->output_offset
= dot
- o
->vma
;
5624 /* Mark how big the output section must be to contain this now. */
5625 dot
+= TO_ADDR (i
->size
);
5626 if (!(o
->flags
& SEC_FIXED_SIZE
))
5627 o
->size
= TO_SIZE (dot
- o
->vma
);
5629 if (link_info
.non_contiguous_regions
)
5631 /* Record that I was successfully assigned to O, and update
5632 its actual output section too. */
5633 i
->already_assigned
= o
;
5634 i
->output_section
= o
;
5648 sort_sections_by_lma (const void *arg1
, const void *arg2
)
5650 const asection
*sec1
= ((const struct check_sec
*) arg1
)->sec
;
5651 const asection
*sec2
= ((const struct check_sec
*) arg2
)->sec
;
5653 if (sec1
->lma
< sec2
->lma
)
5655 else if (sec1
->lma
> sec2
->lma
)
5657 else if (sec1
->id
< sec2
->id
)
5659 else if (sec1
->id
> sec2
->id
)
5666 sort_sections_by_vma (const void *arg1
, const void *arg2
)
5668 const asection
*sec1
= ((const struct check_sec
*) arg1
)->sec
;
5669 const asection
*sec2
= ((const struct check_sec
*) arg2
)->sec
;
5671 if (sec1
->vma
< sec2
->vma
)
5673 else if (sec1
->vma
> sec2
->vma
)
5675 else if (sec1
->id
< sec2
->id
)
5677 else if (sec1
->id
> sec2
->id
)
5683 #define IS_TBSS(s) \
5684 ((s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == SEC_THREAD_LOCAL)
5686 #define IGNORE_SECTION(s) \
5687 ((s->flags & SEC_ALLOC) == 0 || IS_TBSS (s))
5689 /* Check to see if any allocated sections overlap with other allocated
5690 sections. This can happen if a linker script specifies the output
5691 section addresses of the two sections. Also check whether any memory
5692 region has overflowed. */
5695 lang_check_section_addresses (void)
5698 struct check_sec
*sections
;
5703 bfd_vma p_start
= 0;
5705 lang_memory_region_type
*m
;
5708 /* Detect address space overflow on allocated sections. */
5709 addr_mask
= ((bfd_vma
) 1 <<
5710 (bfd_arch_bits_per_address (link_info
.output_bfd
) - 1)) - 1;
5711 addr_mask
= (addr_mask
<< 1) + 1;
5712 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5713 if ((s
->flags
& SEC_ALLOC
) != 0)
5715 s_end
= (s
->vma
+ s
->size
) & addr_mask
;
5716 if (s_end
!= 0 && s_end
< (s
->vma
& addr_mask
))
5717 einfo (_("%X%P: section %s VMA wraps around address space\n"),
5721 s_end
= (s
->lma
+ s
->size
) & addr_mask
;
5722 if (s_end
!= 0 && s_end
< (s
->lma
& addr_mask
))
5723 einfo (_("%X%P: section %s LMA wraps around address space\n"),
5728 if (bfd_count_sections (link_info
.output_bfd
) <= 1)
5731 count
= bfd_count_sections (link_info
.output_bfd
);
5732 sections
= XNEWVEC (struct check_sec
, count
);
5734 /* Scan all sections in the output list. */
5736 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5738 if (IGNORE_SECTION (s
)
5742 sections
[count
].sec
= s
;
5743 sections
[count
].warned
= false;
5753 qsort (sections
, count
, sizeof (*sections
), sort_sections_by_lma
);
5755 /* First check section LMAs. There should be no overlap of LMAs on
5756 loadable sections, even with overlays. */
5757 for (p
= NULL
, i
= 0; i
< count
; i
++)
5759 s
= sections
[i
].sec
;
5761 if ((s
->flags
& SEC_LOAD
) != 0)
5764 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
5766 /* Look for an overlap. We have sorted sections by lma, so
5767 we know that s_start >= p_start. Besides the obvious
5768 case of overlap when the current section starts before
5769 the previous one ends, we also must have overlap if the
5770 previous section wraps around the address space. */
5772 && (s_start
<= p_end
5773 || p_end
< p_start
))
5775 einfo (_("%X%P: section %s LMA [%V,%V]"
5776 " overlaps section %s LMA [%V,%V]\n"),
5777 s
->name
, s_start
, s_end
, p
->name
, p_start
, p_end
);
5778 sections
[i
].warned
= true;
5786 /* If any non-zero size allocated section (excluding tbss) starts at
5787 exactly the same VMA as another such section, then we have
5788 overlays. Overlays generated by the OVERLAY keyword will have
5789 this property. It is possible to intentionally generate overlays
5790 that fail this test, but it would be unusual. */
5791 qsort (sections
, count
, sizeof (*sections
), sort_sections_by_vma
);
5793 p_start
= sections
[0].sec
->vma
;
5794 for (i
= 1; i
< count
; i
++)
5796 s_start
= sections
[i
].sec
->vma
;
5797 if (p_start
== s_start
)
5805 /* Now check section VMAs if no overlays were detected. */
5808 for (p
= NULL
, i
= 0; i
< count
; i
++)
5810 s
= sections
[i
].sec
;
5813 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
5816 && !sections
[i
].warned
5817 && (s_start
<= p_end
5818 || p_end
< p_start
))
5819 einfo (_("%X%P: section %s VMA [%V,%V]"
5820 " overlaps section %s VMA [%V,%V]\n"),
5821 s
->name
, s_start
, s_end
, p
->name
, p_start
, p_end
);
5830 /* If any memory region has overflowed, report by how much.
5831 We do not issue this diagnostic for regions that had sections
5832 explicitly placed outside their bounds; os_region_check's
5833 diagnostics are adequate for that case.
5835 FIXME: It is conceivable that m->current - (m->origin + m->length)
5836 might overflow a 32-bit integer. There is, alas, no way to print
5837 a bfd_vma quantity in decimal. */
5838 for (m
= lang_memory_region_list
; m
; m
= m
->next
)
5839 if (m
->had_full_message
)
5841 unsigned long over
= m
->current
- (m
->origin
+ m
->length
);
5842 einfo (ngettext ("%X%P: region `%s' overflowed by %lu byte\n",
5843 "%X%P: region `%s' overflowed by %lu bytes\n",
5845 m
->name_list
.name
, over
);
5849 /* Make sure the new address is within the region. We explicitly permit the
5850 current address to be at the exact end of the region when the address is
5851 non-zero, in case the region is at the end of addressable memory and the
5852 calculation wraps around. */
5855 os_region_check (lang_output_section_statement_type
*os
,
5856 lang_memory_region_type
*region
,
5860 if ((region
->current
< region
->origin
5861 || (region
->current
- region
->origin
> region
->length
))
5862 && ((region
->current
!= region
->origin
+ region
->length
)
5867 einfo (_("%X%P: address 0x%v of %pB section `%s'"
5868 " is not within region `%s'\n"),
5870 os
->bfd_section
->owner
,
5871 os
->bfd_section
->name
,
5872 region
->name_list
.name
);
5874 else if (!region
->had_full_message
)
5876 region
->had_full_message
= true;
5878 einfo (_("%X%P: %pB section `%s' will not fit in region `%s'\n"),
5879 os
->bfd_section
->owner
,
5880 os
->bfd_section
->name
,
5881 region
->name_list
.name
);
5887 ldlang_check_relro_region (lang_statement_union_type
*s
)
5889 seg_align_type
*seg
= &expld
.dataseg
;
5891 if (seg
->relro
== exp_seg_relro_start
)
5893 if (!seg
->relro_start_stat
)
5894 seg
->relro_start_stat
= s
;
5897 ASSERT (seg
->relro_start_stat
== s
);
5900 else if (seg
->relro
== exp_seg_relro_end
)
5902 if (!seg
->relro_end_stat
)
5903 seg
->relro_end_stat
= s
;
5906 ASSERT (seg
->relro_end_stat
== s
);
5911 /* Set the sizes for all the output sections. */
5914 lang_size_sections_1
5915 (lang_statement_union_type
**prev
,
5916 lang_output_section_statement_type
*current_os
,
5922 lang_statement_union_type
*s
;
5923 lang_statement_union_type
*prev_s
= NULL
;
5924 bool removed_prev_s
= false;
5925 lang_output_section_statement_type
*os
= current_os
;
5927 /* Size up the sections from their constituent parts. */
5928 for (s
= *prev
; s
!= NULL
; prev_s
= s
, s
= s
->header
.next
)
5930 bool removed
= false;
5932 switch (s
->header
.type
)
5934 case lang_output_section_statement_enum
:
5936 bfd_vma newdot
, after
, dotdelta
;
5937 lang_memory_region_type
*r
;
5938 int section_alignment
= 0;
5940 os
= &s
->output_section_statement
;
5941 init_opb (os
->bfd_section
);
5942 if (os
->constraint
== -1)
5945 /* FIXME: We shouldn't need to zero section vmas for ld -r
5946 here, in lang_insert_orphan, or in the default linker scripts.
5947 This is covering for coff backend linker bugs. See PR6945. */
5948 if (os
->addr_tree
== NULL
5949 && bfd_link_relocatable (&link_info
)
5950 && (bfd_get_flavour (link_info
.output_bfd
)
5951 == bfd_target_coff_flavour
))
5952 os
->addr_tree
= exp_intop (0);
5953 if (os
->addr_tree
!= NULL
)
5955 exp_fold_tree (os
->addr_tree
, os
, bfd_abs_section_ptr
, &dot
);
5957 if (expld
.result
.valid_p
)
5959 dot
= expld
.result
.value
;
5960 if (expld
.result
.section
!= NULL
)
5961 dot
+= expld
.result
.section
->vma
;
5963 else if (expld
.phase
!= lang_mark_phase_enum
)
5964 einfo (_("%F%P:%pS: non constant or forward reference"
5965 " address expression for section %s\n"),
5966 os
->addr_tree
, os
->name
);
5969 if (os
->bfd_section
== NULL
)
5970 /* This section was removed or never actually created. */
5973 /* If this is a COFF shared library section, use the size and
5974 address from the input section. FIXME: This is COFF
5975 specific; it would be cleaner if there were some other way
5976 to do this, but nothing simple comes to mind. */
5977 if (((bfd_get_flavour (link_info
.output_bfd
)
5978 == bfd_target_ecoff_flavour
)
5979 || (bfd_get_flavour (link_info
.output_bfd
)
5980 == bfd_target_coff_flavour
))
5981 && (os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
5985 if (os
->children
.head
== NULL
5986 || os
->children
.head
->header
.next
!= NULL
5987 || (os
->children
.head
->header
.type
5988 != lang_input_section_enum
))
5989 einfo (_("%X%P: internal error on COFF shared library"
5990 " section %s\n"), os
->name
);
5992 input
= os
->children
.head
->input_section
.section
;
5993 bfd_set_section_vma (os
->bfd_section
,
5994 bfd_section_vma (input
));
5995 if (!(os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
5996 os
->bfd_section
->size
= input
->size
;
6002 if (bfd_is_abs_section (os
->bfd_section
))
6004 /* No matter what happens, an abs section starts at zero. */
6005 ASSERT (os
->bfd_section
->vma
== 0);
6009 if (os
->addr_tree
== NULL
)
6011 /* No address specified for this section, get one
6012 from the region specification. */
6013 if (os
->region
== NULL
6014 || ((os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))
6015 && os
->region
->name_list
.name
[0] == '*'
6016 && strcmp (os
->region
->name_list
.name
,
6017 DEFAULT_MEMORY_REGION
) == 0))
6019 os
->region
= lang_memory_default (os
->bfd_section
);
6022 /* If a loadable section is using the default memory
6023 region, and some non default memory regions were
6024 defined, issue an error message. */
6026 && !IGNORE_SECTION (os
->bfd_section
)
6027 && !bfd_link_relocatable (&link_info
)
6029 && strcmp (os
->region
->name_list
.name
,
6030 DEFAULT_MEMORY_REGION
) == 0
6031 && lang_memory_region_list
!= NULL
6032 && (strcmp (lang_memory_region_list
->name_list
.name
,
6033 DEFAULT_MEMORY_REGION
) != 0
6034 || lang_memory_region_list
->next
!= NULL
)
6035 && lang_sizing_iteration
== 1)
6037 /* By default this is an error rather than just a
6038 warning because if we allocate the section to the
6039 default memory region we can end up creating an
6040 excessively large binary, or even seg faulting when
6041 attempting to perform a negative seek. See
6042 sources.redhat.com/ml/binutils/2003-04/msg00423.html
6043 for an example of this. This behaviour can be
6044 overridden by the using the --no-check-sections
6046 if (command_line
.check_section_addresses
)
6047 einfo (_("%F%P: error: no memory region specified"
6048 " for loadable section `%s'\n"),
6049 bfd_section_name (os
->bfd_section
));
6051 einfo (_("%P: warning: no memory region specified"
6052 " for loadable section `%s'\n"),
6053 bfd_section_name (os
->bfd_section
));
6056 newdot
= os
->region
->current
;
6057 section_alignment
= os
->bfd_section
->alignment_power
;
6060 section_alignment
= exp_get_power (os
->section_alignment
, os
,
6061 "section alignment");
6063 /* Align to what the section needs. */
6064 if (section_alignment
> 0)
6066 bfd_vma savedot
= newdot
;
6069 newdot
= align_power (newdot
, section_alignment
);
6070 dotdelta
= newdot
- savedot
;
6072 if (lang_sizing_iteration
== 1)
6074 else if (lang_sizing_iteration
> 1)
6076 /* Only report adjustments that would change
6077 alignment from what we have already reported. */
6078 diff
= newdot
- os
->bfd_section
->vma
;
6079 if (!(diff
& (((bfd_vma
) 1 << section_alignment
) - 1)))
6083 && (config
.warn_section_align
6084 || os
->addr_tree
!= NULL
))
6085 einfo (_("%P: warning: "
6086 "start of section %s changed by %ld\n"),
6087 os
->name
, (long) diff
);
6090 bfd_set_section_vma (os
->bfd_section
, newdot
);
6092 os
->bfd_section
->output_offset
= 0;
6095 lang_size_sections_1 (&os
->children
.head
, os
,
6096 os
->fill
, newdot
, relax
, check_regions
);
6098 os
->processed_vma
= true;
6100 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
6101 /* Except for some special linker created sections,
6102 no output section should change from zero size
6103 after strip_excluded_output_sections. A non-zero
6104 size on an ignored section indicates that some
6105 input section was not sized early enough. */
6106 ASSERT (os
->bfd_section
->size
== 0);
6109 dot
= os
->bfd_section
->vma
;
6111 /* Put the section within the requested block size, or
6112 align at the block boundary. */
6114 + TO_ADDR (os
->bfd_section
->size
)
6115 + os
->block_value
- 1)
6116 & - (bfd_vma
) os
->block_value
);
6118 if (!(os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
6119 os
->bfd_section
->size
= TO_SIZE (after
6120 - os
->bfd_section
->vma
);
6123 /* Set section lma. */
6126 r
= lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, false);
6130 bfd_vma lma
= exp_get_abs_int (os
->load_base
, 0, "load base");
6131 os
->bfd_section
->lma
= lma
;
6133 else if (os
->lma_region
!= NULL
)
6135 bfd_vma lma
= os
->lma_region
->current
;
6137 if (os
->align_lma_with_input
)
6141 /* When LMA_REGION is the same as REGION, align the LMA
6142 as we did for the VMA, possibly including alignment
6143 from the bfd section. If a different region, then
6144 only align according to the value in the output
6146 if (os
->lma_region
!= os
->region
)
6147 section_alignment
= exp_get_power (os
->section_alignment
,
6149 "section alignment");
6150 if (section_alignment
> 0)
6151 lma
= align_power (lma
, section_alignment
);
6153 os
->bfd_section
->lma
= lma
;
6155 else if (r
->last_os
!= NULL
6156 && (os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
6161 last
= r
->last_os
->output_section_statement
.bfd_section
;
6163 /* A backwards move of dot should be accompanied by
6164 an explicit assignment to the section LMA (ie.
6165 os->load_base set) because backwards moves can
6166 create overlapping LMAs. */
6168 && os
->bfd_section
->size
!= 0
6169 && dot
+ TO_ADDR (os
->bfd_section
->size
) <= last
->vma
)
6171 /* If dot moved backwards then leave lma equal to
6172 vma. This is the old default lma, which might
6173 just happen to work when the backwards move is
6174 sufficiently large. Nag if this changes anything,
6175 so people can fix their linker scripts. */
6177 if (last
->vma
!= last
->lma
)
6178 einfo (_("%P: warning: dot moved backwards "
6179 "before `%s'\n"), os
->name
);
6183 /* If this is an overlay, set the current lma to that
6184 at the end of the previous section. */
6185 if (os
->sectype
== overlay_section
)
6186 lma
= last
->lma
+ TO_ADDR (last
->size
);
6188 /* Otherwise, keep the same lma to vma relationship
6189 as the previous section. */
6191 lma
= os
->bfd_section
->vma
+ last
->lma
- last
->vma
;
6193 if (section_alignment
> 0)
6194 lma
= align_power (lma
, section_alignment
);
6195 os
->bfd_section
->lma
= lma
;
6198 os
->processed_lma
= true;
6200 /* Keep track of normal sections using the default
6201 lma region. We use this to set the lma for
6202 following sections. Overlays or other linker
6203 script assignment to lma might mean that the
6204 default lma == vma is incorrect.
6205 To avoid warnings about dot moving backwards when using
6206 -Ttext, don't start tracking sections until we find one
6207 of non-zero size or with lma set differently to vma.
6208 Do this tracking before we short-cut the loop so that we
6209 track changes for the case where the section size is zero,
6210 but the lma is set differently to the vma. This is
6211 important, if an orphan section is placed after an
6212 otherwise empty output section that has an explicit lma
6213 set, we want that lma reflected in the orphans lma. */
6214 if (((!IGNORE_SECTION (os
->bfd_section
)
6215 && (os
->bfd_section
->size
!= 0
6216 || (r
->last_os
== NULL
6217 && os
->bfd_section
->vma
!= os
->bfd_section
->lma
)
6218 || (r
->last_os
!= NULL
6219 && dot
>= (r
->last_os
->output_section_statement
6220 .bfd_section
->vma
))))
6221 || os
->sectype
== first_overlay_section
)
6222 && os
->lma_region
== NULL
6223 && !bfd_link_relocatable (&link_info
))
6226 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
6229 /* .tbss sections effectively have zero size. */
6230 if (!IS_TBSS (os
->bfd_section
)
6231 || bfd_link_relocatable (&link_info
))
6232 dotdelta
= TO_ADDR (os
->bfd_section
->size
);
6237 if (os
->update_dot_tree
!= 0)
6238 exp_fold_tree (os
->update_dot_tree
, os
, bfd_abs_section_ptr
, &dot
);
6240 /* Update dot in the region ?
6241 We only do this if the section is going to be allocated,
6242 since unallocated sections do not contribute to the region's
6243 overall size in memory. */
6244 if (os
->region
!= NULL
6245 && (os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
)))
6247 os
->region
->current
= dot
;
6250 /* Make sure the new address is within the region. */
6251 os_region_check (os
, os
->region
, os
->addr_tree
,
6252 os
->bfd_section
->vma
);
6254 if (os
->lma_region
!= NULL
&& os
->lma_region
!= os
->region
6255 && ((os
->bfd_section
->flags
& SEC_LOAD
)
6256 || os
->align_lma_with_input
))
6258 os
->lma_region
->current
= os
->bfd_section
->lma
+ dotdelta
;
6261 os_region_check (os
, os
->lma_region
, NULL
,
6262 os
->bfd_section
->lma
);
6268 case lang_constructors_statement_enum
:
6269 dot
= lang_size_sections_1 (&constructor_list
.head
, current_os
,
6270 fill
, dot
, relax
, check_regions
);
6273 case lang_data_statement_enum
:
6275 unsigned int size
= 0;
6277 s
->data_statement
.output_offset
= dot
- current_os
->bfd_section
->vma
;
6278 s
->data_statement
.output_section
= current_os
->bfd_section
;
6280 /* We might refer to provided symbols in the expression, and
6281 need to mark them as needed. */
6282 exp_fold_tree (s
->data_statement
.exp
, os
,
6283 bfd_abs_section_ptr
, &dot
);
6285 switch (s
->data_statement
.type
)
6303 if (size
< TO_SIZE ((unsigned) 1))
6304 size
= TO_SIZE ((unsigned) 1);
6305 dot
+= TO_ADDR (size
);
6306 if (!(current_os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
6307 current_os
->bfd_section
->size
6308 = TO_SIZE (dot
- current_os
->bfd_section
->vma
);
6313 case lang_reloc_statement_enum
:
6317 s
->reloc_statement
.output_offset
6318 = dot
- current_os
->bfd_section
->vma
;
6319 s
->reloc_statement
.output_section
6320 = current_os
->bfd_section
;
6321 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
6322 dot
+= TO_ADDR (size
);
6323 if (!(current_os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
6324 current_os
->bfd_section
->size
6325 = TO_SIZE (dot
- current_os
->bfd_section
->vma
);
6329 case lang_wild_statement_enum
:
6330 dot
= lang_size_sections_1 (&s
->wild_statement
.children
.head
,
6331 current_os
, fill
, dot
, relax
,
6335 case lang_object_symbols_statement_enum
:
6336 link_info
.create_object_symbols_section
= current_os
->bfd_section
;
6337 current_os
->bfd_section
->flags
|= SEC_KEEP
;
6340 case lang_output_statement_enum
:
6341 case lang_target_statement_enum
:
6344 case lang_input_section_enum
:
6348 i
= s
->input_section
.section
;
6353 if (!bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
6354 einfo (_("%F%P: can't relax section: %E\n"));
6358 dot
= size_input_section (prev
, current_os
, fill
, &removed
, dot
);
6362 case lang_input_statement_enum
:
6365 case lang_fill_statement_enum
:
6366 s
->fill_statement
.output_section
= current_os
->bfd_section
;
6368 fill
= s
->fill_statement
.fill
;
6371 case lang_assignment_statement_enum
:
6373 bfd_vma newdot
= dot
;
6374 etree_type
*tree
= s
->assignment_statement
.exp
;
6376 expld
.dataseg
.relro
= exp_seg_relro_none
;
6378 exp_fold_tree (tree
, os
, current_os
->bfd_section
, &newdot
);
6380 ldlang_check_relro_region (s
);
6382 expld
.dataseg
.relro
= exp_seg_relro_none
;
6384 /* This symbol may be relative to this section. */
6385 if ((tree
->type
.node_class
== etree_provided
6386 || tree
->type
.node_class
== etree_assign
)
6387 && (tree
->assign
.dst
[0] != '.'
6388 || tree
->assign
.dst
[1] != '\0'))
6389 current_os
->update_dot
= 1;
6391 if (!current_os
->ignored
)
6393 if (current_os
== abs_output_section
)
6395 /* If we don't have an output section, then just adjust
6396 the default memory address. */
6397 lang_memory_region_lookup (DEFAULT_MEMORY_REGION
,
6398 false)->current
= newdot
;
6400 else if (newdot
!= dot
)
6402 /* Insert a pad after this statement. We can't
6403 put the pad before when relaxing, in case the
6404 assignment references dot. */
6405 insert_pad (&s
->header
.next
, fill
, TO_SIZE (newdot
- dot
),
6406 current_os
->bfd_section
, dot
);
6408 /* Don't neuter the pad below when relaxing. */
6411 /* If dot is advanced, this implies that the section
6412 should have space allocated to it, unless the
6413 user has explicitly stated that the section
6414 should not be allocated. */
6415 if (current_os
->sectype
!= noalloc_section
6416 && (current_os
->sectype
!= noload_section
6417 || (bfd_get_flavour (link_info
.output_bfd
)
6418 == bfd_target_elf_flavour
)))
6419 current_os
->bfd_section
->flags
|= SEC_ALLOC
;
6426 case lang_padding_statement_enum
:
6427 /* If this is the first time lang_size_sections is called,
6428 we won't have any padding statements. If this is the
6429 second or later passes when relaxing, we should allow
6430 padding to shrink. If padding is needed on this pass, it
6431 will be added back in. */
6432 s
->padding_statement
.size
= 0;
6434 /* Make sure output_offset is valid. If relaxation shrinks
6435 the section and this pad isn't needed, it's possible to
6436 have output_offset larger than the final size of the
6437 section. bfd_set_section_contents will complain even for
6438 a pad size of zero. */
6439 s
->padding_statement
.output_offset
6440 = dot
- current_os
->bfd_section
->vma
;
6443 case lang_group_statement_enum
:
6444 dot
= lang_size_sections_1 (&s
->group_statement
.children
.head
,
6445 current_os
, fill
, dot
, relax
,
6449 case lang_insert_statement_enum
:
6452 /* We can only get here when relaxing is turned on. */
6453 case lang_address_statement_enum
:
6461 /* If an input section doesn't fit in the current output
6462 section, remove it from the list. Handle the case where we
6463 have to remove an input_section statement here: there is a
6464 special case to remove the first element of the list. */
6465 if (link_info
.non_contiguous_regions
&& removed
)
6467 /* If we removed the first element during the previous
6468 iteration, override the loop assignment of prev_s. */
6474 /* If there was a real previous input section, just skip
6476 prev_s
->header
.next
=s
->header
.next
;
6478 removed_prev_s
= false;
6482 /* Remove the first input section of the list. */
6483 *prev
= s
->header
.next
;
6484 removed_prev_s
= true;
6487 /* Move to next element, unless we removed the head of the
6489 if (!removed_prev_s
)
6490 prev
= &s
->header
.next
;
6494 prev
= &s
->header
.next
;
6495 removed_prev_s
= false;
6501 /* Callback routine that is used in _bfd_elf_map_sections_to_segments.
6502 The BFD library has set NEW_SEGMENT to TRUE iff it thinks that
6503 CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different
6504 segments. We are allowed an opportunity to override this decision. */
6507 ldlang_override_segment_assignment (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6508 bfd
*abfd ATTRIBUTE_UNUSED
,
6509 asection
*current_section
,
6510 asection
*previous_section
,
6513 lang_output_section_statement_type
*cur
;
6514 lang_output_section_statement_type
*prev
;
6516 /* The checks below are only necessary when the BFD library has decided
6517 that the two sections ought to be placed into the same segment. */
6521 /* Paranoia checks. */
6522 if (current_section
== NULL
|| previous_section
== NULL
)
6525 /* If this flag is set, the target never wants code and non-code
6526 sections comingled in the same segment. */
6527 if (config
.separate_code
6528 && ((current_section
->flags
^ previous_section
->flags
) & SEC_CODE
))
6531 /* Find the memory regions associated with the two sections.
6532 We call lang_output_section_find() here rather than scanning the list
6533 of output sections looking for a matching section pointer because if
6534 we have a large number of sections then a hash lookup is faster. */
6535 cur
= lang_output_section_find (current_section
->name
);
6536 prev
= lang_output_section_find (previous_section
->name
);
6538 /* More paranoia. */
6539 if (cur
== NULL
|| prev
== NULL
)
6542 /* If the regions are different then force the sections to live in
6543 different segments. See the email thread starting at the following
6544 URL for the reasons why this is necessary:
6545 http://sourceware.org/ml/binutils/2007-02/msg00216.html */
6546 return cur
->region
!= prev
->region
;
6550 one_lang_size_sections_pass (bool *relax
, bool check_regions
)
6552 lang_statement_iteration
++;
6553 if (expld
.phase
!= lang_mark_phase_enum
)
6554 lang_sizing_iteration
++;
6555 lang_size_sections_1 (&statement_list
.head
, abs_output_section
,
6556 0, 0, relax
, check_regions
);
6560 lang_size_segment (void)
6562 /* If XXX_SEGMENT_ALIGN XXX_SEGMENT_END pair was seen, check whether
6563 a page could be saved in the data segment. */
6564 seg_align_type
*seg
= &expld
.dataseg
;
6565 bfd_vma first
, last
;
6567 first
= -seg
->base
& (seg
->commonpagesize
- 1);
6568 last
= seg
->end
& (seg
->commonpagesize
- 1);
6570 && ((seg
->base
& ~(seg
->commonpagesize
- 1))
6571 != (seg
->end
& ~(seg
->commonpagesize
- 1)))
6572 && first
+ last
<= seg
->commonpagesize
)
6574 seg
->phase
= exp_seg_adjust
;
6578 seg
->phase
= exp_seg_done
;
6583 lang_size_relro_segment_1 (void)
6585 seg_align_type
*seg
= &expld
.dataseg
;
6586 bfd_vma relro_end
, desired_end
;
6589 /* Compute the expected PT_GNU_RELRO/PT_LOAD segment end. */
6590 relro_end
= (seg
->relro_end
+ seg
->relropagesize
- 1) & -seg
->relropagesize
;
6592 /* Adjust by the offset arg of XXX_SEGMENT_RELRO_END. */
6593 desired_end
= relro_end
- seg
->relro_offset
;
6595 /* For sections in the relro segment.. */
6596 for (sec
= link_info
.output_bfd
->section_last
; sec
; sec
= sec
->prev
)
6597 if ((sec
->flags
& SEC_ALLOC
) != 0
6598 && sec
->vma
>= seg
->base
6599 && sec
->vma
< seg
->relro_end
- seg
->relro_offset
)
6601 /* Where do we want to put this section so that it ends as
6603 bfd_vma start
, end
, bump
;
6605 end
= start
= sec
->vma
;
6607 end
+= TO_ADDR (sec
->size
);
6608 bump
= desired_end
- end
;
6609 /* We'd like to increase START by BUMP, but we must heed
6610 alignment so the increase might be less than optimum. */
6612 start
&= ~(((bfd_vma
) 1 << sec
->alignment_power
) - 1);
6613 /* This is now the desired end for the previous section. */
6614 desired_end
= start
;
6617 seg
->phase
= exp_seg_relro_adjust
;
6618 ASSERT (desired_end
>= seg
->base
);
6619 seg
->base
= desired_end
;
6624 lang_size_relro_segment (bool *relax
, bool check_regions
)
6626 bool do_reset
= false;
6628 if (link_info
.relro
&& expld
.dataseg
.relro_end
)
6630 bfd_vma data_initial_base
= expld
.dataseg
.base
;
6631 bfd_vma data_relro_end
= lang_size_relro_segment_1 ();
6633 lang_reset_memory_regions ();
6634 one_lang_size_sections_pass (relax
, check_regions
);
6636 /* Assignments to dot, or to output section address in a user
6637 script have increased padding over the original. Revert. */
6638 if (expld
.dataseg
.relro_end
> data_relro_end
)
6640 expld
.dataseg
.base
= data_initial_base
;
6644 else if (lang_size_segment ())
6651 lang_size_sections (bool *relax
, bool check_regions
)
6653 expld
.phase
= lang_allocating_phase_enum
;
6654 expld
.dataseg
.phase
= exp_seg_none
;
6656 one_lang_size_sections_pass (relax
, check_regions
);
6658 if (expld
.dataseg
.phase
!= exp_seg_end_seen
)
6659 expld
.dataseg
.phase
= exp_seg_done
;
6661 if (expld
.dataseg
.phase
== exp_seg_end_seen
)
6664 = lang_size_relro_segment (relax
, check_regions
);
6668 lang_reset_memory_regions ();
6669 one_lang_size_sections_pass (relax
, check_regions
);
6672 if (link_info
.relro
&& expld
.dataseg
.relro_end
)
6674 link_info
.relro_start
= expld
.dataseg
.base
;
6675 link_info
.relro_end
= expld
.dataseg
.relro_end
;
6680 static lang_output_section_statement_type
*current_section
;
6681 static lang_assignment_statement_type
*current_assign
;
6682 static bool prefer_next_section
;
6684 /* Worker function for lang_do_assignments. Recursiveness goes here. */
6687 lang_do_assignments_1 (lang_statement_union_type
*s
,
6688 lang_output_section_statement_type
*current_os
,
6693 lang_output_section_statement_type
*os
= current_os
;
6695 for (; s
!= NULL
; s
= s
->header
.next
)
6697 switch (s
->header
.type
)
6699 case lang_constructors_statement_enum
:
6700 dot
= lang_do_assignments_1 (constructor_list
.head
,
6701 current_os
, fill
, dot
, found_end
);
6704 case lang_output_section_statement_enum
:
6708 os
= &s
->output_section_statement
;
6709 os
->after_end
= *found_end
;
6710 init_opb (os
->bfd_section
);
6712 if (os
->bfd_section
!= NULL
)
6714 if (!os
->ignored
&& (os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
6716 current_section
= os
;
6717 prefer_next_section
= false;
6719 newdot
= os
->bfd_section
->vma
;
6721 newdot
= lang_do_assignments_1 (os
->children
.head
,
6722 os
, os
->fill
, newdot
, found_end
);
6725 if (os
->bfd_section
!= NULL
)
6727 newdot
= os
->bfd_section
->vma
;
6729 /* .tbss sections effectively have zero size. */
6730 if (!IS_TBSS (os
->bfd_section
)
6731 || bfd_link_relocatable (&link_info
))
6732 newdot
+= TO_ADDR (os
->bfd_section
->size
);
6734 if (os
->update_dot_tree
!= NULL
)
6735 exp_fold_tree (os
->update_dot_tree
, os
,
6736 bfd_abs_section_ptr
, &newdot
);
6743 case lang_wild_statement_enum
:
6745 dot
= lang_do_assignments_1 (s
->wild_statement
.children
.head
,
6746 current_os
, fill
, dot
, found_end
);
6749 case lang_object_symbols_statement_enum
:
6750 case lang_output_statement_enum
:
6751 case lang_target_statement_enum
:
6754 case lang_data_statement_enum
:
6755 exp_fold_tree (s
->data_statement
.exp
, os
, bfd_abs_section_ptr
, &dot
);
6756 if (expld
.result
.valid_p
)
6758 s
->data_statement
.value
= expld
.result
.value
;
6759 if (expld
.result
.section
!= NULL
)
6760 s
->data_statement
.value
+= expld
.result
.section
->vma
;
6762 else if (expld
.phase
== lang_final_phase_enum
)
6763 einfo (_("%F%P: invalid data statement\n"));
6766 switch (s
->data_statement
.type
)
6784 if (size
< TO_SIZE ((unsigned) 1))
6785 size
= TO_SIZE ((unsigned) 1);
6786 dot
+= TO_ADDR (size
);
6790 case lang_reloc_statement_enum
:
6791 exp_fold_tree (s
->reloc_statement
.addend_exp
, os
,
6792 bfd_abs_section_ptr
, &dot
);
6793 if (expld
.result
.valid_p
)
6794 s
->reloc_statement
.addend_value
= expld
.result
.value
;
6795 else if (expld
.phase
== lang_final_phase_enum
)
6796 einfo (_("%F%P: invalid reloc statement\n"));
6797 dot
+= TO_ADDR (bfd_get_reloc_size (s
->reloc_statement
.howto
));
6800 case lang_input_section_enum
:
6802 asection
*in
= s
->input_section
.section
;
6804 if ((in
->flags
& SEC_EXCLUDE
) == 0)
6805 dot
+= TO_ADDR (in
->size
);
6809 case lang_input_statement_enum
:
6812 case lang_fill_statement_enum
:
6813 fill
= s
->fill_statement
.fill
;
6816 case lang_assignment_statement_enum
:
6817 current_assign
= &s
->assignment_statement
;
6818 if (current_assign
->exp
->type
.node_class
!= etree_assert
)
6820 const char *p
= current_assign
->exp
->assign
.dst
;
6822 if (current_os
== abs_output_section
&& p
[0] == '.' && p
[1] == 0)
6823 prefer_next_section
= true;
6827 if (strcmp (p
, "end") == 0)
6830 exp_fold_tree (s
->assignment_statement
.exp
, os
,
6831 (current_os
->bfd_section
!= NULL
6832 ? current_os
->bfd_section
: bfd_und_section_ptr
),
6836 case lang_padding_statement_enum
:
6837 dot
+= TO_ADDR (s
->padding_statement
.size
);
6840 case lang_group_statement_enum
:
6841 dot
= lang_do_assignments_1 (s
->group_statement
.children
.head
,
6842 current_os
, fill
, dot
, found_end
);
6845 case lang_insert_statement_enum
:
6848 case lang_address_statement_enum
:
6860 lang_do_assignments (lang_phase_type phase
)
6862 bool found_end
= false;
6864 current_section
= NULL
;
6865 prefer_next_section
= false;
6866 expld
.phase
= phase
;
6867 lang_statement_iteration
++;
6868 lang_do_assignments_1 (statement_list
.head
,
6869 abs_output_section
, NULL
, 0, &found_end
);
6872 /* For an assignment statement outside of an output section statement,
6873 choose the best of neighbouring output sections to use for values
6877 section_for_dot (void)
6881 /* Assignments belong to the previous output section, unless there
6882 has been an assignment to "dot", in which case following
6883 assignments belong to the next output section. (The assumption
6884 is that an assignment to "dot" is setting up the address for the
6885 next output section.) Except that past the assignment to "_end"
6886 we always associate with the previous section. This exception is
6887 for targets like SH that define an alloc .stack or other
6888 weirdness after non-alloc sections. */
6889 if (current_section
== NULL
|| prefer_next_section
)
6891 lang_statement_union_type
*stmt
;
6892 lang_output_section_statement_type
*os
;
6894 for (stmt
= (lang_statement_union_type
*) current_assign
;
6896 stmt
= stmt
->header
.next
)
6897 if (stmt
->header
.type
== lang_output_section_statement_enum
)
6900 os
= stmt
? &stmt
->output_section_statement
: NULL
;
6903 && (os
->bfd_section
== NULL
6904 || (os
->bfd_section
->flags
& SEC_EXCLUDE
) != 0
6905 || bfd_section_removed_from_list (link_info
.output_bfd
,
6909 if (current_section
== NULL
|| os
== NULL
|| !os
->after_end
)
6912 s
= os
->bfd_section
;
6914 s
= link_info
.output_bfd
->section_last
;
6916 && ((s
->flags
& SEC_ALLOC
) == 0
6917 || (s
->flags
& SEC_THREAD_LOCAL
) != 0))
6922 return bfd_abs_section_ptr
;
6926 s
= current_section
->bfd_section
;
6928 /* The section may have been stripped. */
6930 && ((s
->flags
& SEC_EXCLUDE
) != 0
6931 || (s
->flags
& SEC_ALLOC
) == 0
6932 || (s
->flags
& SEC_THREAD_LOCAL
) != 0
6933 || bfd_section_removed_from_list (link_info
.output_bfd
, s
)))
6936 s
= link_info
.output_bfd
->sections
;
6938 && ((s
->flags
& SEC_ALLOC
) == 0
6939 || (s
->flags
& SEC_THREAD_LOCAL
) != 0))
6944 return bfd_abs_section_ptr
;
6947 /* Array of __start/__stop/.startof./.sizeof/ symbols. */
6949 static struct bfd_link_hash_entry
**start_stop_syms
;
6950 static size_t start_stop_count
= 0;
6951 static size_t start_stop_alloc
= 0;
6953 /* Give start/stop SYMBOL for SEC a preliminary definition, and add it
6954 to start_stop_syms. */
6957 lang_define_start_stop (const char *symbol
, asection
*sec
)
6959 struct bfd_link_hash_entry
*h
;
6961 h
= bfd_define_start_stop (link_info
.output_bfd
, &link_info
, symbol
, sec
);
6964 if (start_stop_count
== start_stop_alloc
)
6966 start_stop_alloc
= 2 * start_stop_alloc
+ 10;
6968 = xrealloc (start_stop_syms
,
6969 start_stop_alloc
* sizeof (*start_stop_syms
));
6971 start_stop_syms
[start_stop_count
++] = h
;
6975 /* Check for input sections whose names match references to
6976 __start_SECNAME or __stop_SECNAME symbols. Give the symbols
6977 preliminary definitions. */
6980 lang_init_start_stop (void)
6984 char leading_char
= bfd_get_symbol_leading_char (link_info
.output_bfd
);
6986 for (abfd
= link_info
.input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
6987 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
6990 const char *secname
= s
->name
;
6992 for (ps
= secname
; *ps
!= '\0'; ps
++)
6993 if (!ISALNUM ((unsigned char) *ps
) && *ps
!= '_')
6997 char *symbol
= (char *) xmalloc (10 + strlen (secname
));
6999 symbol
[0] = leading_char
;
7000 sprintf (symbol
+ (leading_char
!= 0), "__start_%s", secname
);
7001 lang_define_start_stop (symbol
, s
);
7003 symbol
[1] = leading_char
;
7004 memcpy (symbol
+ 1 + (leading_char
!= 0), "__stop", 6);
7005 lang_define_start_stop (symbol
+ 1, s
);
7012 /* Iterate over start_stop_syms. */
7015 foreach_start_stop (void (*func
) (struct bfd_link_hash_entry
*))
7019 for (i
= 0; i
< start_stop_count
; ++i
)
7020 func (start_stop_syms
[i
]);
7023 /* __start and __stop symbols are only supposed to be defined by the
7024 linker for orphan sections, but we now extend that to sections that
7025 map to an output section of the same name. The symbols were
7026 defined early for --gc-sections, before we mapped input to output
7027 sections, so undo those that don't satisfy this rule. */
7030 undef_start_stop (struct bfd_link_hash_entry
*h
)
7032 if (h
->ldscript_def
)
7035 if (h
->u
.def
.section
->output_section
== NULL
7036 || h
->u
.def
.section
->output_section
->owner
!= link_info
.output_bfd
7037 || strcmp (h
->u
.def
.section
->name
,
7038 h
->u
.def
.section
->output_section
->name
) != 0)
7040 asection
*sec
= bfd_get_section_by_name (link_info
.output_bfd
,
7041 h
->u
.def
.section
->name
);
7044 /* When there are more than one input sections with the same
7045 section name, SECNAME, linker picks the first one to define
7046 __start_SECNAME and __stop_SECNAME symbols. When the first
7047 input section is removed by comdat group, we need to check
7048 if there is still an output section with section name
7051 for (i
= sec
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
7052 if (strcmp (h
->u
.def
.section
->name
, i
->name
) == 0)
7054 h
->u
.def
.section
= i
;
7058 h
->type
= bfd_link_hash_undefined
;
7059 h
->u
.undef
.abfd
= NULL
;
7060 if (is_elf_hash_table (link_info
.hash
))
7062 const struct elf_backend_data
*bed
;
7063 struct elf_link_hash_entry
*eh
= (struct elf_link_hash_entry
*) h
;
7064 unsigned int was_forced
= eh
->forced_local
;
7066 bed
= get_elf_backend_data (link_info
.output_bfd
);
7067 (*bed
->elf_backend_hide_symbol
) (&link_info
, eh
, true);
7068 if (!eh
->ref_regular_nonweak
)
7069 h
->type
= bfd_link_hash_undefweak
;
7070 eh
->def_regular
= 0;
7071 eh
->forced_local
= was_forced
;
7077 lang_undef_start_stop (void)
7079 foreach_start_stop (undef_start_stop
);
7082 /* Check for output sections whose names match references to
7083 .startof.SECNAME or .sizeof.SECNAME symbols. Give the symbols
7084 preliminary definitions. */
7087 lang_init_startof_sizeof (void)
7091 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7093 const char *secname
= s
->name
;
7094 char *symbol
= (char *) xmalloc (10 + strlen (secname
));
7096 sprintf (symbol
, ".startof.%s", secname
);
7097 lang_define_start_stop (symbol
, s
);
7099 memcpy (symbol
+ 1, ".size", 5);
7100 lang_define_start_stop (symbol
+ 1, s
);
7105 /* Set .startof., .sizeof., __start and __stop symbols final values. */
7108 set_start_stop (struct bfd_link_hash_entry
*h
)
7111 || h
->type
!= bfd_link_hash_defined
)
7114 if (h
->root
.string
[0] == '.')
7116 /* .startof. or .sizeof. symbol.
7117 .startof. already has final value. */
7118 if (h
->root
.string
[2] == 'i')
7121 h
->u
.def
.value
= TO_ADDR (h
->u
.def
.section
->size
);
7122 h
->u
.def
.section
= bfd_abs_section_ptr
;
7127 /* __start or __stop symbol. */
7128 int has_lead
= bfd_get_symbol_leading_char (link_info
.output_bfd
) != 0;
7130 h
->u
.def
.section
= h
->u
.def
.section
->output_section
;
7131 if (h
->root
.string
[4 + has_lead
] == 'o')
7134 h
->u
.def
.value
= TO_ADDR (h
->u
.def
.section
->size
);
7140 lang_finalize_start_stop (void)
7142 foreach_start_stop (set_start_stop
);
7146 lang_symbol_tweaks (void)
7148 /* Give initial values for __start and __stop symbols, so that ELF
7149 gc_sections will keep sections referenced by these symbols. Must
7150 be done before lang_do_assignments. */
7151 if (config
.build_constructors
)
7152 lang_init_start_stop ();
7154 /* Make __ehdr_start hidden, and set def_regular even though it is
7155 likely undefined at this stage. For lang_check_relocs. */
7156 if (is_elf_hash_table (link_info
.hash
)
7157 && !bfd_link_relocatable (&link_info
))
7159 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*)
7160 bfd_link_hash_lookup (link_info
.hash
, "__ehdr_start",
7161 false, false, true);
7163 /* Only adjust the export class if the symbol was referenced
7164 and not defined, otherwise leave it alone. */
7166 && (h
->root
.type
== bfd_link_hash_new
7167 || h
->root
.type
== bfd_link_hash_undefined
7168 || h
->root
.type
== bfd_link_hash_undefweak
7169 || h
->root
.type
== bfd_link_hash_common
))
7171 const struct elf_backend_data
*bed
;
7172 bed
= get_elf_backend_data (link_info
.output_bfd
);
7173 (*bed
->elf_backend_hide_symbol
) (&link_info
, h
, true);
7174 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
7175 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
7177 h
->root
.linker_def
= 1;
7178 h
->root
.rel_from_abs
= 1;
7186 struct bfd_link_hash_entry
*h
;
7189 if ((bfd_link_relocatable (&link_info
) && !link_info
.gc_sections
)
7190 || bfd_link_dll (&link_info
))
7191 warn
= entry_from_cmdline
;
7195 /* Force the user to specify a root when generating a relocatable with
7196 --gc-sections, unless --gc-keep-exported was also given. */
7197 if (bfd_link_relocatable (&link_info
)
7198 && link_info
.gc_sections
7199 && !link_info
.gc_keep_exported
)
7201 struct bfd_sym_chain
*sym
;
7203 for (sym
= link_info
.gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
7205 h
= bfd_link_hash_lookup (link_info
.hash
, sym
->name
,
7206 false, false, false);
7208 && (h
->type
== bfd_link_hash_defined
7209 || h
->type
== bfd_link_hash_defweak
)
7210 && !bfd_is_const_section (h
->u
.def
.section
))
7214 einfo (_("%F%P: --gc-sections requires a defined symbol root "
7215 "specified by -e or -u\n"));
7218 if (entry_symbol
.name
== NULL
)
7220 /* No entry has been specified. Look for the default entry, but
7221 don't warn if we don't find it. */
7222 entry_symbol
.name
= entry_symbol_default
;
7226 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
.name
,
7227 false, false, true);
7229 && (h
->type
== bfd_link_hash_defined
7230 || h
->type
== bfd_link_hash_defweak
)
7231 && h
->u
.def
.section
->output_section
!= NULL
)
7235 val
= (h
->u
.def
.value
7236 + bfd_section_vma (h
->u
.def
.section
->output_section
)
7237 + h
->u
.def
.section
->output_offset
);
7238 if (!bfd_set_start_address (link_info
.output_bfd
, val
))
7239 einfo (_("%F%P: %s: can't set start address\n"), entry_symbol
.name
);
7246 /* We couldn't find the entry symbol. Try parsing it as a
7248 val
= bfd_scan_vma (entry_symbol
.name
, &send
, 0);
7251 if (!bfd_set_start_address (link_info
.output_bfd
, val
))
7252 einfo (_("%F%P: can't set start address\n"));
7254 /* BZ 2004952: Only use the start of the entry section for executables. */
7255 else if bfd_link_executable (&link_info
)
7259 /* Can't find the entry symbol, and it's not a number. Use
7260 the first address in the text section. */
7261 ts
= bfd_get_section_by_name (link_info
.output_bfd
, entry_section
);
7265 einfo (_("%P: warning: cannot find entry symbol %s;"
7266 " defaulting to %V\n"),
7268 bfd_section_vma (ts
));
7269 if (!bfd_set_start_address (link_info
.output_bfd
,
7270 bfd_section_vma (ts
)))
7271 einfo (_("%F%P: can't set start address\n"));
7276 einfo (_("%P: warning: cannot find entry symbol %s;"
7277 " not setting start address\n"),
7284 einfo (_("%P: warning: cannot find entry symbol %s;"
7285 " not setting start address\n"),
7291 /* This is a small function used when we want to ignore errors from
7295 ignore_bfd_errors (const char *fmt ATTRIBUTE_UNUSED
,
7296 va_list ap ATTRIBUTE_UNUSED
)
7298 /* Don't do anything. */
7301 /* Check that the architecture of all the input files is compatible
7302 with the output file. Also call the backend to let it do any
7303 other checking that is needed. */
7308 lang_input_statement_type
*file
;
7310 const bfd_arch_info_type
*compatible
;
7312 for (file
= (void *) file_chain
.head
;
7316 #if BFD_SUPPORTS_PLUGINS
7317 /* Don't check format of files claimed by plugin. */
7318 if (file
->flags
.claimed
)
7320 #endif /* BFD_SUPPORTS_PLUGINS */
7321 input_bfd
= file
->the_bfd
;
7323 = bfd_arch_get_compatible (input_bfd
, link_info
.output_bfd
,
7324 command_line
.accept_unknown_input_arch
);
7326 /* In general it is not possible to perform a relocatable
7327 link between differing object formats when the input
7328 file has relocations, because the relocations in the
7329 input format may not have equivalent representations in
7330 the output format (and besides BFD does not translate
7331 relocs for other link purposes than a final link). */
7332 if (!file
->flags
.just_syms
7333 && (bfd_link_relocatable (&link_info
)
7334 || link_info
.emitrelocations
)
7335 && (compatible
== NULL
7336 || (bfd_get_flavour (input_bfd
)
7337 != bfd_get_flavour (link_info
.output_bfd
)))
7338 && (bfd_get_file_flags (input_bfd
) & HAS_RELOC
) != 0)
7340 einfo (_("%F%P: relocatable linking with relocations from"
7341 " format %s (%pB) to format %s (%pB) is not supported\n"),
7342 bfd_get_target (input_bfd
), input_bfd
,
7343 bfd_get_target (link_info
.output_bfd
), link_info
.output_bfd
);
7344 /* einfo with %F exits. */
7347 if (compatible
== NULL
)
7349 if (command_line
.warn_mismatch
)
7350 einfo (_("%X%P: %s architecture of input file `%pB'"
7351 " is incompatible with %s output\n"),
7352 bfd_printable_name (input_bfd
), input_bfd
,
7353 bfd_printable_name (link_info
.output_bfd
));
7356 /* If the input bfd has no contents, it shouldn't set the
7357 private data of the output bfd. */
7358 else if (!file
->flags
.just_syms
7359 && ((input_bfd
->flags
& DYNAMIC
) != 0
7360 || bfd_count_sections (input_bfd
) != 0))
7362 bfd_error_handler_type pfn
= NULL
;
7364 /* If we aren't supposed to warn about mismatched input
7365 files, temporarily set the BFD error handler to a
7366 function which will do nothing. We still want to call
7367 bfd_merge_private_bfd_data, since it may set up
7368 information which is needed in the output file. */
7369 if (!command_line
.warn_mismatch
)
7370 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
7371 if (!bfd_merge_private_bfd_data (input_bfd
, &link_info
))
7373 if (command_line
.warn_mismatch
)
7374 einfo (_("%X%P: failed to merge target specific data"
7375 " of file %pB\n"), input_bfd
);
7377 if (!command_line
.warn_mismatch
)
7378 bfd_set_error_handler (pfn
);
7383 /* Look through all the global common symbols and attach them to the
7384 correct section. The -sort-common command line switch may be used
7385 to roughly sort the entries by alignment. */
7390 if (link_info
.inhibit_common_definition
)
7392 if (bfd_link_relocatable (&link_info
)
7393 && !command_line
.force_common_definition
)
7396 if (!config
.sort_common
)
7397 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, NULL
);
7402 if (config
.sort_common
== sort_descending
)
7404 for (power
= 4; power
> 0; power
--)
7405 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7408 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7412 for (power
= 0; power
<= 4; power
++)
7413 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7415 power
= (unsigned int) -1;
7416 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7421 /* Place one common symbol in the correct section. */
7424 lang_one_common (struct bfd_link_hash_entry
*h
, void *info
)
7426 unsigned int power_of_two
;
7430 if (h
->type
!= bfd_link_hash_common
)
7434 power_of_two
= h
->u
.c
.p
->alignment_power
;
7436 if (config
.sort_common
== sort_descending
7437 && power_of_two
< *(unsigned int *) info
)
7439 else if (config
.sort_common
== sort_ascending
7440 && power_of_two
> *(unsigned int *) info
)
7443 section
= h
->u
.c
.p
->section
;
7444 if (!bfd_define_common_symbol (link_info
.output_bfd
, &link_info
, h
))
7445 einfo (_("%F%P: could not define common symbol `%pT': %E\n"),
7448 if (config
.map_file
!= NULL
)
7450 static bool header_printed
;
7455 if (!header_printed
)
7457 minfo (_("\nAllocating common symbols\n"));
7458 minfo (_("Common symbol size file\n\n"));
7459 header_printed
= true;
7462 name
= bfd_demangle (link_info
.output_bfd
, h
->root
.string
,
7463 DMGL_ANSI
| DMGL_PARAMS
);
7466 minfo ("%s", h
->root
.string
);
7467 len
= strlen (h
->root
.string
);
7472 len
= strlen (name
);
7482 sprintf (buf
, "%" PRIx64
, (uint64_t) size
);
7483 fprintf (config
.map_file
, "%*s0x%-16s", 20 - len
, "", buf
);
7485 minfo ("%pB\n", section
->owner
);
7491 /* Handle a single orphan section S, placing the orphan into an appropriate
7492 output section. The effects of the --orphan-handling command line
7493 option are handled here. */
7496 ldlang_place_orphan (asection
*s
)
7498 if (config
.orphan_handling
== orphan_handling_discard
)
7500 lang_output_section_statement_type
*os
;
7501 os
= lang_output_section_statement_lookup (DISCARD_SECTION_NAME
, 0, 1);
7502 if (os
->addr_tree
== NULL
7503 && (bfd_link_relocatable (&link_info
)
7504 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0))
7505 os
->addr_tree
= exp_intop (0);
7506 lang_add_section (&os
->children
, s
, NULL
, NULL
, os
);
7510 lang_output_section_statement_type
*os
;
7511 const char *name
= s
->name
;
7514 if (config
.orphan_handling
== orphan_handling_error
)
7515 einfo (_("%X%P: error: unplaced orphan section `%pA' from `%pB'\n"),
7518 if (config
.unique_orphan_sections
|| unique_section_p (s
, NULL
))
7519 constraint
= SPECIAL
;
7521 os
= ldemul_place_orphan (s
, name
, constraint
);
7524 os
= lang_output_section_statement_lookup (name
, constraint
, 1);
7525 if (os
->addr_tree
== NULL
7526 && (bfd_link_relocatable (&link_info
)
7527 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0))
7528 os
->addr_tree
= exp_intop (0);
7529 lang_add_section (&os
->children
, s
, NULL
, NULL
, os
);
7532 if (config
.orphan_handling
== orphan_handling_warn
)
7533 einfo (_("%P: warning: orphan section `%pA' from `%pB' being "
7534 "placed in section `%s'\n"),
7535 s
, s
->owner
, os
->name
);
7539 /* Run through the input files and ensure that every input section has
7540 somewhere to go. If one is found without a destination then create
7541 an input request and place it into the statement tree. */
7544 lang_place_orphans (void)
7546 LANG_FOR_EACH_INPUT_STATEMENT (file
)
7550 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7552 if (s
->output_section
== NULL
)
7554 /* This section of the file is not attached, root
7555 around for a sensible place for it to go. */
7557 if (file
->flags
.just_syms
)
7558 bfd_link_just_syms (file
->the_bfd
, s
, &link_info
);
7559 else if (lang_discard_section_p (s
))
7560 s
->output_section
= bfd_abs_section_ptr
;
7561 else if (strcmp (s
->name
, "COMMON") == 0)
7563 /* This is a lonely common section which must have
7564 come from an archive. We attach to the section
7565 with the wildcard. */
7566 if (!bfd_link_relocatable (&link_info
)
7567 || command_line
.force_common_definition
)
7569 if (default_common_section
== NULL
)
7570 default_common_section
7571 = lang_output_section_statement_lookup (".bss", 0, 1);
7572 lang_add_section (&default_common_section
->children
, s
,
7573 NULL
, NULL
, default_common_section
);
7577 ldlang_place_orphan (s
);
7584 lang_set_flags (lang_memory_region_type
*ptr
, const char *flags
, int invert
)
7586 flagword
*ptr_flags
;
7588 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
7594 /* PR 17900: An exclamation mark in the attributes reverses
7595 the sense of any of the attributes that follow. */
7598 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
7602 *ptr_flags
|= SEC_ALLOC
;
7606 *ptr_flags
|= SEC_READONLY
;
7610 *ptr_flags
|= SEC_DATA
;
7614 *ptr_flags
|= SEC_CODE
;
7619 *ptr_flags
|= SEC_LOAD
;
7623 einfo (_("%F%P: invalid character %c (%d) in flags\n"),
7631 /* Call a function on each real input file. This function will be
7632 called on an archive, but not on the elements. */
7635 lang_for_each_input_file (void (*func
) (lang_input_statement_type
*))
7637 lang_input_statement_type
*f
;
7639 for (f
= (void *) input_file_chain
.head
;
7641 f
= f
->next_real_file
)
7646 /* Call a function on each real file. The function will be called on
7647 all the elements of an archive which are included in the link, but
7648 will not be called on the archive file itself. */
7651 lang_for_each_file (void (*func
) (lang_input_statement_type
*))
7653 LANG_FOR_EACH_INPUT_STATEMENT (f
)
7661 ldlang_add_file (lang_input_statement_type
*entry
)
7663 lang_statement_append (&file_chain
, entry
, &entry
->next
);
7665 /* The BFD linker needs to have a list of all input BFDs involved in
7667 ASSERT (link_info
.input_bfds_tail
!= &entry
->the_bfd
->link
.next
7668 && entry
->the_bfd
->link
.next
== NULL
);
7669 ASSERT (entry
->the_bfd
!= link_info
.output_bfd
);
7671 *link_info
.input_bfds_tail
= entry
->the_bfd
;
7672 link_info
.input_bfds_tail
= &entry
->the_bfd
->link
.next
;
7673 bfd_set_usrdata (entry
->the_bfd
, entry
);
7674 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
7676 /* Look through the sections and check for any which should not be
7677 included in the link. We need to do this now, so that we can
7678 notice when the backend linker tries to report multiple
7679 definition errors for symbols which are in sections we aren't
7680 going to link. FIXME: It might be better to entirely ignore
7681 symbols which are defined in sections which are going to be
7682 discarded. This would require modifying the backend linker for
7683 each backend which might set the SEC_LINK_ONCE flag. If we do
7684 this, we should probably handle SEC_EXCLUDE in the same way. */
7686 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, entry
);
7690 lang_add_output (const char *name
, int from_script
)
7692 /* Make -o on command line override OUTPUT in script. */
7693 if (!had_output_filename
|| !from_script
)
7695 output_filename
= name
;
7696 had_output_filename
= true;
7700 lang_output_section_statement_type
*
7701 lang_enter_output_section_statement (const char *output_section_statement_name
,
7702 etree_type
*address_exp
,
7703 enum section_type sectype
,
7704 etree_type
*sectype_value
,
7706 etree_type
*subalign
,
7709 int align_with_input
)
7711 lang_output_section_statement_type
*os
;
7713 os
= lang_output_section_statement_lookup (output_section_statement_name
,
7715 in_section_ordering
? 0 : 2);
7716 if (os
== NULL
) /* && in_section_ordering */
7717 einfo (_("%F%P:%pS: error: output section '%s' must already exist\n"),
7718 NULL
, output_section_statement_name
);
7719 current_section
= os
;
7721 /* Make next things chain into subchain of this. */
7722 push_stat_ptr (in_section_ordering
? &os
->sort_children
: &os
->children
);
7724 if (in_section_ordering
)
7727 if (os
->addr_tree
== NULL
)
7728 os
->addr_tree
= address_exp
;
7730 os
->sectype
= sectype
;
7731 if (sectype
== type_section
|| sectype
== typed_readonly_section
)
7732 os
->sectype_value
= sectype_value
;
7733 else if (sectype
== noload_section
)
7734 os
->flags
= SEC_NEVER_LOAD
;
7736 os
->flags
= SEC_NO_FLAGS
;
7737 os
->block_value
= 1;
7739 os
->align_lma_with_input
= align_with_input
== ALIGN_WITH_INPUT
;
7740 if (os
->align_lma_with_input
&& align
!= NULL
)
7741 einfo (_("%F%P:%pS: error: align with input and explicit align specified\n"),
7744 os
->subsection_alignment
= subalign
;
7745 os
->section_alignment
= align
;
7747 os
->load_base
= ebase
;
7754 lang_output_statement_type
*new_stmt
;
7756 new_stmt
= new_stat (lang_output_statement
, stat_ptr
);
7757 new_stmt
->name
= output_filename
;
7760 /* Reset the current counters in the regions. */
7763 lang_reset_memory_regions (void)
7765 lang_memory_region_type
*p
= lang_memory_region_list
;
7767 lang_output_section_statement_type
*os
;
7769 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
7771 p
->current
= p
->origin
;
7775 for (os
= (void *) lang_os_list
.head
;
7779 os
->processed_vma
= false;
7780 os
->processed_lma
= false;
7783 for (o
= link_info
.output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
7785 /* Save the last size for possible use by bfd_relax_section. */
7786 o
->rawsize
= o
->size
;
7787 if (!(o
->flags
& SEC_FIXED_SIZE
))
7792 /* Worker for lang_gc_sections_1. */
7795 gc_section_callback (lang_wild_statement_type
*ptr
,
7796 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
7798 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
7799 void *data ATTRIBUTE_UNUSED
)
7801 /* If the wild pattern was marked KEEP, the member sections
7802 should be as well. */
7803 if (ptr
->keep_sections
)
7804 section
->flags
|= SEC_KEEP
;
7807 /* Iterate over sections marking them against GC. */
7810 lang_gc_sections_1 (lang_statement_union_type
*s
)
7812 for (; s
!= NULL
; s
= s
->header
.next
)
7814 switch (s
->header
.type
)
7816 case lang_wild_statement_enum
:
7817 walk_wild (&s
->wild_statement
, gc_section_callback
, NULL
);
7819 case lang_constructors_statement_enum
:
7820 lang_gc_sections_1 (constructor_list
.head
);
7822 case lang_output_section_statement_enum
:
7823 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
7825 case lang_group_statement_enum
:
7826 lang_gc_sections_1 (s
->group_statement
.children
.head
);
7835 lang_gc_sections (void)
7837 /* Keep all sections so marked in the link script. */
7838 lang_gc_sections_1 (statement_list
.head
);
7840 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
7841 the special case of .stabstr debug info. (See bfd/stabs.c)
7842 Twiddle the flag here, to simplify later linker code. */
7843 if (bfd_link_relocatable (&link_info
))
7845 LANG_FOR_EACH_INPUT_STATEMENT (f
)
7848 #if BFD_SUPPORTS_PLUGINS
7849 if (f
->flags
.claimed
)
7852 for (sec
= f
->the_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7853 if ((sec
->flags
& SEC_DEBUGGING
) == 0
7854 || strcmp (sec
->name
, ".stabstr") != 0)
7855 sec
->flags
&= ~SEC_EXCLUDE
;
7859 if (link_info
.gc_sections
)
7860 bfd_gc_sections (link_info
.output_bfd
, &link_info
);
7863 /* Worker for lang_find_relro_sections_1. */
7866 find_relro_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
7867 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
7869 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
7872 /* Discarded, excluded and ignored sections effectively have zero
7874 if (section
->output_section
!= NULL
7875 && section
->output_section
->owner
== link_info
.output_bfd
7876 && (section
->output_section
->flags
& SEC_EXCLUDE
) == 0
7877 && !IGNORE_SECTION (section
)
7878 && section
->size
!= 0)
7880 bool *has_relro_section
= (bool *) data
;
7881 *has_relro_section
= true;
7885 /* Iterate over sections for relro sections. */
7888 lang_find_relro_sections_1 (lang_statement_union_type
*s
,
7889 bool *has_relro_section
)
7891 if (*has_relro_section
)
7894 for (; s
!= NULL
; s
= s
->header
.next
)
7896 if (s
== expld
.dataseg
.relro_end_stat
)
7899 switch (s
->header
.type
)
7901 case lang_wild_statement_enum
:
7902 walk_wild (&s
->wild_statement
,
7903 find_relro_section_callback
,
7906 case lang_constructors_statement_enum
:
7907 lang_find_relro_sections_1 (constructor_list
.head
,
7910 case lang_output_section_statement_enum
:
7911 lang_find_relro_sections_1 (s
->output_section_statement
.children
.head
,
7914 case lang_group_statement_enum
:
7915 lang_find_relro_sections_1 (s
->group_statement
.children
.head
,
7925 lang_find_relro_sections (void)
7927 bool has_relro_section
= false;
7929 /* Check all sections in the link script. */
7931 lang_find_relro_sections_1 (expld
.dataseg
.relro_start_stat
,
7932 &has_relro_section
);
7934 if (!has_relro_section
)
7935 link_info
.relro
= false;
7938 /* Relax all sections until bfd_relax_section gives up. */
7941 lang_relax_sections (bool need_layout
)
7943 /* NB: Also enable relaxation to layout sections for DT_RELR. */
7944 if (RELAXATION_ENABLED
|| link_info
.enable_dt_relr
)
7946 /* We may need more than one relaxation pass. */
7947 int i
= link_info
.relax_pass
;
7949 /* The backend can use it to determine the current pass. */
7950 link_info
.relax_pass
= 0;
7954 /* Keep relaxing until bfd_relax_section gives up. */
7957 link_info
.relax_trip
= -1;
7960 link_info
.relax_trip
++;
7962 /* Note: pe-dll.c does something like this also. If you find
7963 you need to change this code, you probably need to change
7964 pe-dll.c also. DJ */
7966 /* Do all the assignments with our current guesses as to
7968 lang_do_assignments (lang_assigning_phase_enum
);
7970 /* We must do this after lang_do_assignments, because it uses
7972 lang_reset_memory_regions ();
7974 /* Perform another relax pass - this time we know where the
7975 globals are, so can make a better guess. */
7976 relax_again
= false;
7977 lang_size_sections (&relax_again
, false);
7979 while (relax_again
);
7981 link_info
.relax_pass
++;
7988 /* Final extra sizing to report errors. */
7989 lang_do_assignments (lang_assigning_phase_enum
);
7990 lang_reset_memory_regions ();
7991 lang_size_sections (NULL
, true);
7995 #if BFD_SUPPORTS_PLUGINS
7996 /* Find the insert point for the plugin's replacement files. We
7997 place them after the first claimed real object file, or if the
7998 first claimed object is an archive member, after the last real
7999 object file immediately preceding the archive. In the event
8000 no objects have been claimed at all, we return the first dummy
8001 object file on the list as the insert point; that works, but
8002 the callee must be careful when relinking the file_chain as it
8003 is not actually on that chain, only the statement_list and the
8004 input_file list; in that case, the replacement files must be
8005 inserted at the head of the file_chain. */
8007 static lang_input_statement_type
*
8008 find_replacements_insert_point (bool *before
)
8010 lang_input_statement_type
*claim1
, *lastobject
;
8011 lastobject
= (void *) input_file_chain
.head
;
8012 for (claim1
= (void *) file_chain
.head
;
8014 claim1
= claim1
->next
)
8016 if (claim1
->flags
.claimed
)
8018 *before
= claim1
->flags
.claim_archive
;
8019 return claim1
->flags
.claim_archive
? lastobject
: claim1
;
8021 /* Update lastobject if this is a real object file. */
8022 if (claim1
->the_bfd
!= NULL
&& claim1
->the_bfd
->my_archive
== NULL
)
8023 lastobject
= claim1
;
8025 /* No files were claimed by the plugin. Choose the last object
8026 file found on the list (maybe the first, dummy entry) as the
8032 /* Find where to insert ADD, an archive element or shared library
8033 added during a rescan. */
8035 static lang_input_statement_type
**
8036 find_rescan_insertion (lang_input_statement_type
*add
)
8038 bfd
*add_bfd
= add
->the_bfd
;
8039 lang_input_statement_type
*f
;
8040 lang_input_statement_type
*last_loaded
= NULL
;
8041 lang_input_statement_type
*before
= NULL
;
8042 lang_input_statement_type
**iter
= NULL
;
8044 if (add_bfd
->my_archive
!= NULL
)
8045 add_bfd
= add_bfd
->my_archive
;
8047 /* First look through the input file chain, to find an object file
8048 before the one we've rescanned. Normal object files always
8049 appear on both the input file chain and the file chain, so this
8050 lets us get quickly to somewhere near the correct place on the
8051 file chain if it is full of archive elements. Archives don't
8052 appear on the file chain, but if an element has been extracted
8053 then their input_statement->next points at it. */
8054 for (f
= (void *) input_file_chain
.head
;
8056 f
= f
->next_real_file
)
8058 if (f
->the_bfd
== add_bfd
)
8060 before
= last_loaded
;
8061 if (f
->next
!= NULL
)
8062 return &f
->next
->next
;
8064 if (f
->the_bfd
!= NULL
&& f
->next
!= NULL
)
8068 for (iter
= before
? &before
->next
: &file_chain
.head
->input_statement
.next
;
8070 iter
= &(*iter
)->next
)
8071 if (!(*iter
)->flags
.claim_archive
8072 && (*iter
)->the_bfd
->my_archive
== NULL
)
8078 /* Detach new nodes added to DESTLIST since the time ORIGLIST
8079 was taken as a copy of it and leave them in ORIGLIST. */
8082 lang_list_remove_tail (lang_statement_list_type
*destlist
,
8083 lang_statement_list_type
*origlist
)
8085 union lang_statement_union
**savetail
;
8086 /* Check that ORIGLIST really is an earlier state of DESTLIST. */
8087 ASSERT (origlist
->head
== destlist
->head
);
8088 savetail
= origlist
->tail
;
8089 origlist
->head
= *(savetail
);
8090 origlist
->tail
= destlist
->tail
;
8091 destlist
->tail
= savetail
;
8095 static lang_statement_union_type
**
8096 find_next_input_statement (lang_statement_union_type
**s
)
8098 for ( ; *s
; s
= &(*s
)->header
.next
)
8100 lang_statement_union_type
**t
;
8101 switch ((*s
)->header
.type
)
8103 case lang_input_statement_enum
:
8105 case lang_wild_statement_enum
:
8106 t
= &(*s
)->wild_statement
.children
.head
;
8108 case lang_group_statement_enum
:
8109 t
= &(*s
)->group_statement
.children
.head
;
8111 case lang_output_section_statement_enum
:
8112 t
= &(*s
)->output_section_statement
.children
.head
;
8117 t
= find_next_input_statement (t
);
8123 #endif /* BFD_SUPPORTS_PLUGINS */
8125 /* Insert SRCLIST into DESTLIST after given element by chaining
8126 on FIELD as the next-pointer. (Counterintuitively does not need
8127 a pointer to the actual after-node itself, just its chain field.) */
8130 lang_list_insert_after (lang_statement_list_type
*destlist
,
8131 lang_statement_list_type
*srclist
,
8132 lang_statement_union_type
**field
)
8134 *(srclist
->tail
) = *field
;
8135 *field
= srclist
->head
;
8136 if (destlist
->tail
== field
)
8137 destlist
->tail
= srclist
->tail
;
8140 /* Add NAME to the list of garbage collection entry points. */
8143 lang_add_gc_name (const char *name
)
8145 struct bfd_sym_chain
*sym
;
8150 sym
= stat_alloc (sizeof (*sym
));
8152 sym
->next
= link_info
.gc_sym_list
;
8154 link_info
.gc_sym_list
= sym
;
8157 /* Check relocations. */
8160 lang_check_relocs (void)
8162 if (link_info
.check_relocs_after_open_input
)
8166 for (abfd
= link_info
.input_bfds
;
8167 abfd
!= (bfd
*) NULL
; abfd
= abfd
->link
.next
)
8168 if (!bfd_link_check_relocs (abfd
, &link_info
))
8170 /* No object output, fail return. */
8171 config
.make_executable
= false;
8172 /* Note: we do not abort the loop, but rather
8173 continue the scan in case there are other
8174 bad relocations to report. */
8179 /* Look through all output sections looking for places where we can
8180 propagate forward the lma region. */
8183 lang_propagate_lma_regions (void)
8185 lang_output_section_statement_type
*os
;
8187 for (os
= (void *) lang_os_list
.head
;
8191 if (os
->prev
!= NULL
8192 && os
->lma_region
== NULL
8193 && os
->load_base
== NULL
8194 && os
->addr_tree
== NULL
8195 && os
->region
== os
->prev
->region
)
8196 os
->lma_region
= os
->prev
->lma_region
;
8201 warn_non_contiguous_discards (void)
8203 LANG_FOR_EACH_INPUT_STATEMENT (file
)
8205 if ((file
->the_bfd
->flags
& (BFD_LINKER_CREATED
| DYNAMIC
)) != 0
8206 || file
->flags
.just_syms
)
8209 for (asection
*s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
8210 if (s
->output_section
== NULL
8211 && (s
->flags
& SEC_LINKER_CREATED
) == 0)
8212 einfo (_("%P: warning: --enable-non-contiguous-regions "
8213 "discards section `%pA' from `%pB'\n"),
8219 reset_one_wild (lang_statement_union_type
*statement
)
8221 if (statement
->header
.type
== lang_wild_statement_enum
)
8223 lang_wild_statement_type
*stmt
= &statement
->wild_statement
;
8224 lang_list_init (&stmt
->matching_sections
);
8229 reset_resolved_wilds (void)
8231 lang_for_each_statement (reset_one_wild
);
8234 /* For each output section statement, splice any entries on the
8235 sort_children list before the first wild statement on the children
8239 lang_os_merge_sort_children (void)
8241 lang_output_section_statement_type
*os
;
8242 for (os
= (void *) lang_os_list
.head
; os
!= NULL
; os
= os
->next
)
8244 if (os
->sort_children
.head
!= NULL
)
8246 lang_statement_union_type
**where
;
8247 for (where
= &os
->children
.head
;
8249 where
= &(*where
)->header
.next
)
8250 if ((*where
)->header
.type
== lang_wild_statement_enum
)
8252 lang_list_insert_after (&os
->children
, &os
->sort_children
, where
);
8260 lang_os_merge_sort_children ();
8262 /* Finalize dynamic list. */
8263 if (link_info
.dynamic_list
)
8264 lang_finalize_version_expr_head (&link_info
.dynamic_list
->head
);
8266 current_target
= default_target
;
8268 /* Open the output file. */
8269 lang_for_each_statement (ldlang_open_output
);
8272 ldemul_create_output_section_statements ();
8274 /* Add to the hash table all undefineds on the command line. */
8275 lang_place_undefineds ();
8277 if (!bfd_section_already_linked_table_init ())
8278 einfo (_("%F%P: can not create hash table: %E\n"));
8280 /* A first pass through the memory regions ensures that if any region
8281 references a symbol for its origin or length then this symbol will be
8282 added to the symbol table. Having these symbols in the symbol table
8283 means that when we call open_input_bfds PROVIDE statements will
8284 trigger to provide any needed symbols. The regions origins and
8285 lengths are not assigned as a result of this call. */
8286 lang_do_memory_regions (false);
8288 /* Create a bfd for each input file. */
8289 current_target
= default_target
;
8290 lang_statement_iteration
++;
8291 open_input_bfds (statement_list
.head
, NULL
, OPEN_BFD_NORMAL
);
8293 /* Now that open_input_bfds has processed assignments and provide
8294 statements we can give values to symbolic origin/length now. */
8295 lang_do_memory_regions (true);
8297 ldemul_before_plugin_all_symbols_read ();
8299 #if BFD_SUPPORTS_PLUGINS
8300 if (link_info
.lto_plugin_active
)
8302 lang_statement_list_type added
;
8303 lang_statement_list_type files
, inputfiles
;
8305 /* Now all files are read, let the plugin(s) decide if there
8306 are any more to be added to the link before we call the
8307 emulation's after_open hook. We create a private list of
8308 input statements for this purpose, which we will eventually
8309 insert into the global statement list after the first claimed
8312 /* We need to manipulate all three chains in synchrony. */
8314 inputfiles
= input_file_chain
;
8315 if (plugin_call_all_symbols_read ())
8316 einfo (_("%F%P: %s: plugin reported error after all symbols read\n"),
8317 plugin_error_plugin ());
8318 link_info
.lto_all_symbols_read
= true;
8319 /* Open any newly added files, updating the file chains. */
8320 plugin_undefs
= link_info
.hash
->undefs_tail
;
8321 lang_output_section_statement_type
*last_os
= NULL
;
8322 if (lang_os_list
.head
!= NULL
)
8323 last_os
= ((lang_output_section_statement_type
*)
8324 ((char *) lang_os_list
.tail
8325 - offsetof (lang_output_section_statement_type
, next
)));
8326 open_input_bfds (*added
.tail
, last_os
, OPEN_BFD_NORMAL
);
8327 if (plugin_undefs
== link_info
.hash
->undefs_tail
)
8328 plugin_undefs
= NULL
;
8329 /* Restore the global list pointer now they have all been added. */
8330 lang_list_remove_tail (stat_ptr
, &added
);
8331 /* And detach the fresh ends of the file lists. */
8332 lang_list_remove_tail (&file_chain
, &files
);
8333 lang_list_remove_tail (&input_file_chain
, &inputfiles
);
8334 /* Were any new files added? */
8335 if (added
.head
!= NULL
)
8337 /* If so, we will insert them into the statement list immediately
8338 after the first input file that was claimed by the plugin,
8339 unless that file was an archive in which case it is inserted
8340 immediately before. */
8342 lang_statement_union_type
**prev
;
8343 plugin_insert
= find_replacements_insert_point (&before
);
8344 /* If a plugin adds input files without having claimed any, we
8345 don't really have a good idea where to place them. Just putting
8346 them at the start or end of the list is liable to leave them
8347 outside the crtbegin...crtend range. */
8348 ASSERT (plugin_insert
!= NULL
);
8349 /* Splice the new statement list into the old one. */
8350 prev
= &plugin_insert
->header
.next
;
8353 prev
= find_next_input_statement (prev
);
8354 if (*prev
!= (void *) plugin_insert
->next_real_file
)
8356 /* We didn't find the expected input statement.
8357 Fall back to adding after plugin_insert. */
8358 prev
= &plugin_insert
->header
.next
;
8361 lang_list_insert_after (stat_ptr
, &added
, prev
);
8362 /* Likewise for the file chains. */
8363 lang_list_insert_after (&input_file_chain
, &inputfiles
,
8364 (void *) &plugin_insert
->next_real_file
);
8365 /* We must be careful when relinking file_chain; we may need to
8366 insert the new files at the head of the list if the insert
8367 point chosen is the dummy first input file. */
8368 if (plugin_insert
->filename
)
8369 lang_list_insert_after (&file_chain
, &files
,
8370 (void *) &plugin_insert
->next
);
8372 lang_list_insert_after (&file_chain
, &files
, &file_chain
.head
);
8374 /* Rescan archives in case new undefined symbols have appeared. */
8376 lang_statement_iteration
++;
8377 open_input_bfds (statement_list
.head
, NULL
, OPEN_BFD_RESCAN
);
8378 lang_list_remove_tail (&file_chain
, &files
);
8379 while (files
.head
!= NULL
)
8381 lang_input_statement_type
**insert
;
8382 lang_input_statement_type
**iter
, *temp
;
8385 insert
= find_rescan_insertion (&files
.head
->input_statement
);
8386 /* All elements from an archive can be added at once. */
8387 iter
= &files
.head
->input_statement
.next
;
8388 my_arch
= files
.head
->input_statement
.the_bfd
->my_archive
;
8389 if (my_arch
!= NULL
)
8390 for (; *iter
!= NULL
; iter
= &(*iter
)->next
)
8391 if ((*iter
)->the_bfd
->my_archive
!= my_arch
)
8394 *insert
= &files
.head
->input_statement
;
8395 files
.head
= (lang_statement_union_type
*) *iter
;
8397 if (file_chain
.tail
== (lang_statement_union_type
**) insert
)
8398 file_chain
.tail
= (lang_statement_union_type
**) iter
;
8399 if (my_arch
!= NULL
)
8401 lang_input_statement_type
*parent
= bfd_usrdata (my_arch
);
8403 parent
->next
= (lang_input_statement_type
*)
8405 - offsetof (lang_input_statement_type
, next
));
8411 #endif /* BFD_SUPPORTS_PLUGINS */
8412 if (bfd_link_relocatable (&link_info
))
8414 /* Check if .gnu_object_only section should be created. */
8419 for (p
= link_info
.input_bfds
; p
!= (bfd
*) NULL
; p
= p
->link
.next
)
8421 enum bfd_lto_object_type lto_type
= bfd_get_lto_type (p
);
8422 /* NB: Treat fat IR object as IR object here. */
8423 if (lto_type
== lto_fat_ir_object
)
8424 lto_type
= lto_slim_ir_object
;
8425 object_type
|= 1 << lto_type
;
8426 if ((object_type
& (1 << lto_mixed_object
)) != 0
8428 & (1 << lto_non_ir_object
8429 | 1 << lto_slim_ir_object
))
8430 == (1 << lto_non_ir_object
| 1 << lto_slim_ir_object
)))
8432 config
.emit_gnu_object_only
= true;
8438 && (cmdline_object_only_file_list
.head
8439 || cmdline_object_only_archive_list
.head
))
8441 info_msg (_("Object-only input files:\n "));
8442 print_cmdline_list (cmdline_object_only_file_list
.head
);
8443 print_cmdline_list (cmdline_object_only_archive_list
.head
);
8447 struct bfd_sym_chain
**sym
= &link_info
.gc_sym_list
;
8449 sym
= &(*sym
)->next
;
8451 *sym
= &entry_symbol
;
8453 if (entry_symbol
.name
== NULL
)
8455 *sym
= ldlang_undef_chain_list_head
;
8457 /* entry_symbol is normally initialised by an ENTRY definition in the
8458 linker script or the -e command line option. But if neither of
8459 these have been used, the target specific backend may still have
8460 provided an entry symbol via a call to lang_default_entry().
8461 Unfortunately this value will not be processed until lang_end()
8462 is called, long after this function has finished. So detect this
8463 case here and add the target's entry symbol to the list of starting
8464 points for garbage collection resolution. */
8465 lang_add_gc_name (entry_symbol_default
);
8468 lang_add_gc_name (link_info
.init_function
);
8469 lang_add_gc_name (link_info
.fini_function
);
8471 ldemul_after_open ();
8472 if (config
.map_file
!= NULL
)
8473 lang_print_asneeded ();
8477 bfd_section_already_linked_table_free ();
8479 /* Make sure that we're not mixing architectures. We call this
8480 after all the input files have been opened, but before we do any
8481 other processing, so that any operations merge_private_bfd_data
8482 does on the output file will be known during the rest of the
8486 /* Handle .exports instead of a version script if we're told to do so. */
8487 if (command_line
.version_exports_section
)
8488 lang_do_version_exports_section ();
8490 /* Build all sets based on the information gathered from the input
8492 ldctor_build_sets ();
8494 lang_symbol_tweaks ();
8496 /* PR 13683: We must rerun the assignments prior to running garbage
8497 collection in order to make sure that all symbol aliases are resolved. */
8498 lang_do_assignments (lang_mark_phase_enum
);
8499 expld
.phase
= lang_first_phase_enum
;
8501 /* Size up the common data. */
8505 debug_prefix_tree ();
8509 /* Remove unreferenced sections if asked to. */
8510 lang_gc_sections ();
8512 lang_mark_undefineds ();
8514 /* Check relocations. */
8515 lang_check_relocs ();
8517 ldemul_after_check_relocs ();
8519 /* There might have been new sections created (e.g. as result of
8520 checking relocs to need a .got, or suchlike), so to properly order
8521 them into our lists of matching sections reset them here. */
8522 reset_resolved_wilds ();
8525 /* Update wild statements in case the user gave --sort-section.
8526 Note how the option might have come after the linker script and
8527 so couldn't have been set when the wild statements were created. */
8528 update_wild_statements (statement_list
.head
);
8530 /* Run through the contours of the script and attach input sections
8531 to the correct output sections. */
8532 lang_statement_iteration
++;
8533 map_input_to_output_sections (statement_list
.head
, NULL
, NULL
);
8535 /* Start at the statement immediately after the special abs_section
8536 output statement, so that it isn't reordered. */
8537 process_insert_statements (&lang_os_list
.head
->header
.next
);
8539 ldemul_before_place_orphans ();
8541 /* Find any sections not attached explicitly and handle them. */
8542 lang_place_orphans ();
8544 if (!bfd_link_relocatable (&link_info
))
8548 /* Merge SEC_MERGE sections. This has to be done after GC of
8549 sections, so that GCed sections are not merged, but before
8550 assigning dynamic symbols, since removing whole input sections
8552 if (!bfd_merge_sections (link_info
.output_bfd
, &link_info
))
8553 einfo (_("%F%P: bfd_merge_sections failed: %E\n"));
8555 /* Look for a text section and set the readonly attribute in it. */
8556 found
= bfd_get_section_by_name (link_info
.output_bfd
, ".text");
8560 if (config
.text_read_only
)
8561 found
->flags
|= SEC_READONLY
;
8563 found
->flags
&= ~SEC_READONLY
;
8567 /* Merge together CTF sections. After this, only the symtab-dependent
8568 function and data object sections need adjustment. */
8571 /* Emit the CTF, iff the emulation doesn't need to do late emission after
8572 examining things laid out late, like the strtab. */
8575 /* Copy forward lma regions for output sections in same lma region. */
8576 lang_propagate_lma_regions ();
8578 /* Defining __start/__stop symbols early for --gc-sections to work
8579 around a glibc build problem can result in these symbols being
8580 defined when they should not be. Fix them now. */
8581 if (config
.build_constructors
)
8582 lang_undef_start_stop ();
8584 /* Define .startof./.sizeof. symbols with preliminary values before
8585 dynamic symbols are created. */
8586 if (!bfd_link_relocatable (&link_info
))
8587 lang_init_startof_sizeof ();
8589 /* Do anything special before sizing sections. This is where ELF
8590 and other back-ends size dynamic sections. */
8591 ldemul_before_allocation ();
8593 /* We must record the program headers before we try to fix the
8594 section positions, since they will affect SIZEOF_HEADERS. */
8595 lang_record_phdrs ();
8597 /* Check relro sections. */
8598 if (link_info
.relro
&& !bfd_link_relocatable (&link_info
))
8599 lang_find_relro_sections ();
8601 /* Size up the sections. */
8602 lang_size_sections (NULL
, !RELAXATION_ENABLED
);
8604 /* See if anything special should be done now we know how big
8605 everything is. This is where relaxation is done. */
8606 ldemul_after_allocation ();
8608 /* Fix any __start, __stop, .startof. or .sizeof. symbols. */
8609 lang_finalize_start_stop ();
8611 /* Do all the assignments again, to report errors. Assignment
8612 statements are processed multiple times, updating symbols; In
8613 open_input_bfds, lang_do_assignments, and lang_size_sections.
8614 Since lang_relax_sections calls lang_do_assignments, symbols are
8615 also updated in ldemul_after_allocation. */
8616 lang_do_assignments (lang_final_phase_enum
);
8620 /* Convert absolute symbols to section relative. */
8621 ldexp_finalize_syms ();
8623 /* Make sure that the section addresses make sense. */
8624 if (command_line
.check_section_addresses
)
8625 lang_check_section_addresses ();
8627 if (link_info
.non_contiguous_regions
8628 && link_info
.non_contiguous_regions_warnings
)
8629 warn_non_contiguous_discards ();
8631 /* Check any required symbols are known. */
8632 ldlang_check_require_defined_symbols ();
8638 lang_add_version_string (void)
8640 if (! enable_linker_version
)
8643 const char * str
= "GNU ld ";
8644 int len
= strlen (str
);
8647 for (i
= 0 ; i
< len
; i
++)
8648 lang_add_data (BYTE
, exp_intop (str
[i
]));
8650 str
= BFD_VERSION_STRING
;
8653 for (i
= 0 ; i
< len
; i
++)
8654 lang_add_data (BYTE
, exp_intop (str
[i
]));
8656 lang_add_data (BYTE
, exp_intop ('\0'));
8659 /* EXPORTED TO YACC */
8662 lang_add_wild (struct wildcard_spec
*filespec
,
8663 struct wildcard_list
*section_list
,
8666 struct wildcard_list
*curr
, *next
;
8667 lang_wild_statement_type
*new_stmt
;
8668 bool any_specs_sorted
= false;
8670 /* Reverse the list as the parser puts it back to front. */
8671 for (curr
= section_list
, section_list
= NULL
;
8673 section_list
= curr
, curr
= next
)
8675 if (curr
->spec
.sorted
!= none
&& curr
->spec
.sorted
!= by_none
)
8676 any_specs_sorted
= true;
8678 curr
->next
= section_list
;
8681 if (filespec
!= NULL
&& filespec
->name
!= NULL
)
8683 if (strcmp (filespec
->name
, "*") == 0)
8684 filespec
->name
= NULL
;
8685 else if (!wildcardp (filespec
->name
))
8686 lang_has_input_file
= true;
8689 new_stmt
= new_stat (lang_wild_statement
, stat_ptr
);
8690 new_stmt
->filename
= NULL
;
8691 new_stmt
->filenames_sorted
= false;
8692 new_stmt
->any_specs_sorted
= any_specs_sorted
;
8693 new_stmt
->section_flag_list
= NULL
;
8694 new_stmt
->exclude_name_list
= NULL
;
8695 if (filespec
!= NULL
)
8697 new_stmt
->filename
= filespec
->name
;
8698 new_stmt
->filenames_sorted
= (filespec
->sorted
== by_name
|| filespec
->reversed
);
8699 new_stmt
->section_flag_list
= filespec
->section_flag_list
;
8700 new_stmt
->exclude_name_list
= filespec
->exclude_name_list
;
8701 new_stmt
->filenames_reversed
= filespec
->reversed
;
8703 new_stmt
->section_list
= section_list
;
8704 new_stmt
->keep_sections
= keep_sections
;
8705 lang_list_init (&new_stmt
->children
);
8706 lang_list_init (&new_stmt
->matching_sections
);
8707 analyze_walk_wild_section_handler (new_stmt
);
8710 printf ("wild %s(", new_stmt
->filename
? new_stmt
->filename
: "*");
8711 for (curr
= new_stmt
->section_list
; curr
; curr
= curr
->next
)
8712 printf ("%s ", curr
->spec
.name
? curr
->spec
.name
: "*");
8718 lang_section_start (const char *name
, etree_type
*address
,
8719 const segment_type
*segment
)
8721 lang_address_statement_type
*ad
;
8723 ad
= new_stat (lang_address_statement
, stat_ptr
);
8724 ad
->section_name
= name
;
8725 ad
->address
= address
;
8726 ad
->segment
= segment
;
8729 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
8730 because of a -e argument on the command line, or zero if this is
8731 called by ENTRY in a linker script. Command line arguments take
8735 lang_add_entry (const char *name
, bool cmdline
)
8737 if (entry_symbol
.name
== NULL
8739 || !entry_from_cmdline
)
8741 entry_symbol
.name
= name
;
8742 entry_from_cmdline
= cmdline
;
8746 /* Set the default start symbol to NAME. .em files should use this,
8747 not lang_add_entry, to override the use of "start" if neither the
8748 linker script nor the command line specifies an entry point. NAME
8749 must be permanently allocated. */
8751 lang_default_entry (const char *name
)
8753 entry_symbol_default
= name
;
8757 lang_add_target (const char *name
)
8759 lang_target_statement_type
*new_stmt
;
8761 new_stmt
= new_stat (lang_target_statement
, stat_ptr
);
8762 new_stmt
->target
= name
;
8766 lang_add_map (const char *name
)
8773 map_option_f
= true;
8781 lang_add_fill (fill_type
*fill
)
8783 lang_fill_statement_type
*new_stmt
;
8785 new_stmt
= new_stat (lang_fill_statement
, stat_ptr
);
8786 new_stmt
->fill
= fill
;
8790 lang_add_data (int type
, union etree_union
*exp
)
8792 lang_data_statement_type
*new_stmt
;
8794 new_stmt
= new_stat (lang_data_statement
, stat_ptr
);
8795 new_stmt
->exp
= exp
;
8796 new_stmt
->type
= type
;
8800 lang_add_string (const char *s
)
8802 bfd_vma len
= strlen (s
);
8804 bool escape
= false;
8806 /* Add byte expressions until end of string. */
8807 for (i
= 0 ; i
< len
; i
++)
8816 /* Ignore the escape. */
8819 case 'n': c
= '\n'; break;
8820 case 'r': c
= '\r'; break;
8821 case 't': c
= '\t'; break;
8831 /* We have an octal number. */
8833 unsigned int value
= c
- '0';
8836 if ((c
>= '0') && (c
<= '7'))
8844 if ((c
>= '0') && (c
<= '7'))
8855 /* octal: \777 is treated as '\077' + '7' */
8866 lang_add_data (BYTE
, exp_intop (c
));
8874 lang_add_data (BYTE
, exp_intop (c
));
8878 /* Remeber to terminate the string. */
8879 lang_add_data (BYTE
, exp_intop (0));
8882 /* Create a new reloc statement. RELOC is the BFD relocation type to
8883 generate. HOWTO is the corresponding howto structure (we could
8884 look this up, but the caller has already done so). SECTION is the
8885 section to generate a reloc against, or NAME is the name of the
8886 symbol to generate a reloc against. Exactly one of SECTION and
8887 NAME must be NULL. ADDEND is an expression for the addend. */
8890 lang_add_reloc (bfd_reloc_code_real_type reloc
,
8891 reloc_howto_type
*howto
,
8894 union etree_union
*addend
)
8896 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
8900 p
->section
= section
;
8902 p
->addend_exp
= addend
;
8904 p
->addend_value
= 0;
8905 p
->output_section
= NULL
;
8906 p
->output_offset
= 0;
8909 lang_assignment_statement_type
*
8910 lang_add_assignment (etree_type
*exp
)
8912 lang_assignment_statement_type
*new_stmt
;
8914 new_stmt
= new_stat (lang_assignment_statement
, stat_ptr
);
8915 new_stmt
->exp
= exp
;
8920 lang_add_attribute (enum statement_enum attribute
)
8922 new_statement (attribute
, sizeof (lang_statement_header_type
), stat_ptr
);
8926 lang_startup (const char *name
)
8928 if (first_file
->filename
!= NULL
)
8930 einfo (_("%F%P: multiple STARTUP files\n"));
8932 first_file
->filename
= name
;
8933 first_file
->local_sym_name
= name
;
8934 first_file
->flags
.real
= true;
8938 lang_float (bool maybe
)
8940 lang_float_flag
= maybe
;
8944 /* Work out the load- and run-time regions from a script statement, and
8945 store them in *LMA_REGION and *REGION respectively.
8947 MEMSPEC is the name of the run-time region, or the value of
8948 DEFAULT_MEMORY_REGION if the statement didn't specify one.
8949 LMA_MEMSPEC is the name of the load-time region, or null if the
8950 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
8951 had an explicit load address.
8953 It is an error to specify both a load region and a load address. */
8956 lang_get_regions (lang_memory_region_type
**region
,
8957 lang_memory_region_type
**lma_region
,
8958 const char *memspec
,
8959 const char *lma_memspec
,
8963 *lma_region
= lang_memory_region_lookup (lma_memspec
, false);
8965 /* If no runtime region or VMA has been specified, but the load region
8966 has been specified, then use the load region for the runtime region
8968 if (lma_memspec
!= NULL
8970 && strcmp (memspec
, DEFAULT_MEMORY_REGION
) == 0)
8971 *region
= *lma_region
;
8973 *region
= lang_memory_region_lookup (memspec
, false);
8975 if (have_lma
&& lma_memspec
!= 0)
8976 einfo (_("%X%P:%pS: section has both a load address and a load region\n"),
8981 lang_leave_output_section_statement (fill_type
*fill
, const char *memspec
,
8982 lang_output_section_phdr_list
*phdrs
,
8983 const char *lma_memspec
)
8986 if (in_section_ordering
)
8989 lang_get_regions (¤t_section
->region
,
8990 ¤t_section
->lma_region
,
8991 memspec
, lma_memspec
,
8992 current_section
->load_base
!= NULL
,
8993 current_section
->addr_tree
!= NULL
);
8995 current_section
->fill
= fill
;
8996 current_section
->phdrs
= phdrs
;
8999 /* Set the output format type. -oformat overrides scripts. */
9002 lang_add_output_format (const char *format
,
9007 if (output_target
== NULL
|| !from_script
)
9009 if (command_line
.endian
== ENDIAN_BIG
9012 else if (command_line
.endian
== ENDIAN_LITTLE
9016 output_target
= format
;
9021 lang_add_insert (const char *where
, int is_before
)
9023 lang_insert_statement_type
*new_stmt
;
9025 new_stmt
= new_stat (lang_insert_statement
, stat_ptr
);
9026 new_stmt
->where
= where
;
9027 new_stmt
->is_before
= is_before
;
9028 saved_script_handle
= previous_script_handle
;
9031 /* Enter a group. This creates a new lang_group_statement, and sets
9032 stat_ptr to build new statements within the group. */
9035 lang_enter_group (void)
9037 lang_group_statement_type
*g
;
9039 g
= new_stat (lang_group_statement
, stat_ptr
);
9040 lang_list_init (&g
->children
);
9041 push_stat_ptr (&g
->children
);
9044 /* Leave a group. This just resets stat_ptr to start writing to the
9045 regular list of statements again. Note that this will not work if
9046 groups can occur inside anything else which can adjust stat_ptr,
9047 but currently they can't. */
9050 lang_leave_group (void)
9055 /* Add a new program header. This is called for each entry in a PHDRS
9056 command in a linker script. */
9059 lang_new_phdr (const char *name
,
9066 struct lang_phdr
*n
, **pp
;
9069 n
= stat_alloc (sizeof (struct lang_phdr
));
9072 n
->type
= exp_get_vma (type
, NULL
, 0, "program header type");
9073 n
->filehdr
= filehdr
;
9078 hdrs
= n
->type
== 1 && (phdrs
|| filehdr
);
9080 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
9083 && !((*pp
)->filehdr
|| (*pp
)->phdrs
))
9085 einfo (_("%X%P:%pS: PHDRS and FILEHDR are not supported"
9086 " when prior PT_LOAD headers lack them\n"), NULL
);
9093 /* Record the program header information in the output BFD. FIXME: We
9094 should not be calling an ELF specific function here. */
9097 lang_record_phdrs (void)
9101 lang_output_section_phdr_list
*last
;
9102 struct lang_phdr
*l
;
9103 lang_output_section_statement_type
*os
;
9106 secs
= (asection
**) xmalloc (alc
* sizeof (asection
*));
9109 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
9116 for (os
= (void *) lang_os_list
.head
;
9120 lang_output_section_phdr_list
*pl
;
9122 if (os
->constraint
< 0)
9130 if (os
->sectype
== noload_section
9131 || os
->bfd_section
== NULL
9132 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
9135 /* Don't add orphans to PT_INTERP header. */
9136 if (l
->type
== PT_INTERP
)
9141 lang_output_section_statement_type
*tmp_os
;
9143 /* If we have not run across a section with a program
9144 header assigned to it yet, then scan forwards to find
9145 one. This prevents inconsistencies in the linker's
9146 behaviour when a script has specified just a single
9147 header and there are sections in that script which are
9148 not assigned to it, and which occur before the first
9149 use of that header. See here for more details:
9150 http://sourceware.org/ml/binutils/2007-02/msg00291.html */
9151 for (tmp_os
= os
; tmp_os
; tmp_os
= tmp_os
->next
)
9154 last
= tmp_os
->phdrs
;
9158 einfo (_("%F%P: no sections assigned to phdrs\n"));
9163 if (os
->bfd_section
== NULL
)
9166 for (; pl
!= NULL
; pl
= pl
->next
)
9168 if (strcmp (pl
->name
, l
->name
) == 0)
9173 secs
= (asection
**) xrealloc (secs
,
9174 alc
* sizeof (asection
*));
9176 secs
[c
] = os
->bfd_section
;
9183 if (l
->flags
== NULL
)
9186 flags
= exp_get_vma (l
->flags
, NULL
, 0, "phdr flags");
9191 at
= exp_get_vma (l
->at
, NULL
, 0, "phdr load address");
9193 if (!bfd_record_phdr (link_info
.output_bfd
, l
->type
,
9194 l
->flags
!= NULL
, flags
, l
->at
!= NULL
,
9195 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
9196 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
9201 /* Make sure all the phdr assignments succeeded. */
9202 for (os
= (void *) lang_os_list
.head
;
9206 lang_output_section_phdr_list
*pl
;
9208 if (os
->constraint
< 0
9209 || os
->bfd_section
== NULL
)
9212 for (pl
= os
->phdrs
;
9215 if (!pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
9216 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
9217 os
->name
, pl
->name
);
9221 /* Record a list of sections which may not be cross referenced. */
9224 lang_add_nocrossref (lang_nocrossref_type
*l
)
9226 struct lang_nocrossrefs
*n
;
9228 n
= stat_alloc (sizeof *n
);
9229 n
->next
= nocrossref_list
;
9231 n
->onlyfirst
= false;
9232 nocrossref_list
= n
;
9234 /* Set notice_all so that we get informed about all symbols. */
9235 link_info
.notice_all
= true;
9238 /* Record a section that cannot be referenced from a list of sections. */
9241 lang_add_nocrossref_to (lang_nocrossref_type
*l
)
9243 lang_add_nocrossref (l
);
9244 nocrossref_list
->onlyfirst
= true;
9247 /* Overlay handling. We handle overlays with some static variables. */
9249 /* The overlay virtual address. */
9250 static etree_type
*overlay_vma
;
9251 /* And subsection alignment. */
9252 static etree_type
*overlay_subalign
;
9254 /* An expression for the maximum section size seen so far. */
9255 static etree_type
*overlay_max
;
9257 /* A list of all the sections in this overlay. */
9259 struct overlay_list
{
9260 struct overlay_list
*next
;
9261 lang_output_section_statement_type
*os
;
9264 static struct overlay_list
*overlay_list
;
9266 /* Start handling an overlay. */
9269 lang_enter_overlay (etree_type
*vma_expr
, etree_type
*subalign
)
9271 /* The grammar should prevent nested overlays from occurring. */
9272 ASSERT (overlay_vma
== NULL
9273 && overlay_subalign
== NULL
9274 && overlay_max
== NULL
);
9276 overlay_vma
= vma_expr
;
9277 overlay_subalign
= subalign
;
9280 /* Start a section in an overlay. We handle this by calling
9281 lang_enter_output_section_statement with the correct VMA.
9282 lang_leave_overlay sets up the LMA and memory regions. */
9285 lang_enter_overlay_section (const char *name
)
9287 struct overlay_list
*n
;
9290 lang_enter_output_section_statement (name
, overlay_vma
, overlay_section
,
9291 0, 0, overlay_subalign
, 0, 0, 0);
9293 /* If this is the first section, then base the VMA of future
9294 sections on this one. This will work correctly even if `.' is
9295 used in the addresses. */
9296 if (overlay_list
== NULL
)
9297 overlay_vma
= exp_nameop (ADDR
, name
);
9299 /* Remember the section. */
9300 n
= (struct overlay_list
*) xmalloc (sizeof *n
);
9301 n
->os
= current_section
;
9302 n
->next
= overlay_list
;
9305 size
= exp_nameop (SIZEOF
, name
);
9307 /* Arrange to work out the maximum section end address. */
9308 if (overlay_max
== NULL
)
9311 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
9314 /* Finish a section in an overlay. There isn't any special to do
9318 lang_leave_overlay_section (fill_type
*fill
,
9319 lang_output_section_phdr_list
*phdrs
)
9321 const char *name
= current_section
->name
;;
9323 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
9324 region and that no load-time region has been specified. It doesn't
9325 really matter what we say here, since lang_leave_overlay will
9327 lang_leave_output_section_statement (fill
, DEFAULT_MEMORY_REGION
, phdrs
, 0);
9329 /* Define the magic symbols. */
9331 char *clean
= xmalloc (strlen (name
) + 1);
9333 for (const char *s1
= name
; *s1
!= '\0'; s1
++)
9334 if (ISALNUM (*s1
) || *s1
== '_')
9338 char *buf
= xasprintf ("__load_start_%s", clean
);
9339 lang_add_assignment (exp_provide (buf
,
9340 exp_nameop (LOADADDR
, name
),
9343 buf
= xasprintf ("__load_stop_%s", clean
);
9344 lang_add_assignment (exp_provide (buf
,
9346 exp_nameop (LOADADDR
, name
),
9347 exp_nameop (SIZEOF
, name
)),
9353 /* Finish an overlay. If there are any overlay wide settings, this
9354 looks through all the sections in the overlay and sets them. */
9357 lang_leave_overlay (etree_type
*lma_expr
,
9360 const char *memspec
,
9361 lang_output_section_phdr_list
*phdrs
,
9362 const char *lma_memspec
)
9364 lang_memory_region_type
*region
;
9365 lang_memory_region_type
*lma_region
;
9366 struct overlay_list
*l
;
9367 lang_nocrossref_type
*nocrossref
;
9369 lang_get_regions (®ion
, &lma_region
,
9370 memspec
, lma_memspec
,
9371 lma_expr
!= NULL
, false);
9375 /* After setting the size of the last section, set '.' to end of the
9377 if (overlay_list
!= NULL
)
9379 overlay_list
->os
->update_dot
= 1;
9380 overlay_list
->os
->update_dot_tree
9381 = exp_assign (".", exp_binop ('+', overlay_vma
, overlay_max
), false);
9387 struct overlay_list
*next
;
9389 if (fill
!= NULL
&& l
->os
->fill
== NULL
)
9392 l
->os
->region
= region
;
9393 l
->os
->lma_region
= lma_region
;
9395 /* The first section has the load address specified in the
9396 OVERLAY statement. The rest are worked out from that.
9397 The base address is not needed (and should be null) if
9398 an LMA region was specified. */
9401 l
->os
->load_base
= lma_expr
;
9402 l
->os
->sectype
= first_overlay_section
;
9404 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
9405 l
->os
->phdrs
= phdrs
;
9409 lang_nocrossref_type
*nc
;
9411 nc
= stat_alloc (sizeof *nc
);
9412 nc
->name
= l
->os
->name
;
9413 nc
->next
= nocrossref
;
9422 if (nocrossref
!= NULL
)
9423 lang_add_nocrossref (nocrossref
);
9426 overlay_list
= NULL
;
9428 overlay_subalign
= NULL
;
9431 /* Version handling. This is only useful for ELF. */
9433 /* If PREV is NULL, return first version pattern matching particular symbol.
9434 If PREV is non-NULL, return first version pattern matching particular
9435 symbol after PREV (previously returned by lang_vers_match). */
9437 static struct bfd_elf_version_expr
*
9438 lang_vers_match (struct bfd_elf_version_expr_head
*head
,
9439 struct bfd_elf_version_expr
*prev
,
9443 const char *cxx_sym
= sym
;
9444 const char *java_sym
= sym
;
9445 struct bfd_elf_version_expr
*expr
= NULL
;
9446 enum demangling_styles curr_style
;
9448 curr_style
= CURRENT_DEMANGLING_STYLE
;
9449 cplus_demangle_set_style (no_demangling
);
9450 c_sym
= bfd_demangle (link_info
.output_bfd
, sym
, DMGL_NO_OPTS
);
9453 cplus_demangle_set_style (curr_style
);
9455 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
9457 cxx_sym
= bfd_demangle (link_info
.output_bfd
, sym
,
9458 DMGL_PARAMS
| DMGL_ANSI
);
9462 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
9464 java_sym
= bfd_demangle (link_info
.output_bfd
, sym
, DMGL_JAVA
);
9469 if (head
->htab
&& (prev
== NULL
|| prev
->literal
))
9471 struct bfd_elf_version_expr e
;
9473 switch (prev
? prev
->mask
: 0)
9476 if (head
->mask
& BFD_ELF_VERSION_C_TYPE
)
9479 expr
= (struct bfd_elf_version_expr
*)
9480 htab_find ((htab_t
) head
->htab
, &e
);
9481 while (expr
&& strcmp (expr
->pattern
, c_sym
) == 0)
9482 if (expr
->mask
== BFD_ELF_VERSION_C_TYPE
)
9488 case BFD_ELF_VERSION_C_TYPE
:
9489 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
9491 e
.pattern
= cxx_sym
;
9492 expr
= (struct bfd_elf_version_expr
*)
9493 htab_find ((htab_t
) head
->htab
, &e
);
9494 while (expr
&& strcmp (expr
->pattern
, cxx_sym
) == 0)
9495 if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
9501 case BFD_ELF_VERSION_CXX_TYPE
:
9502 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
9504 e
.pattern
= java_sym
;
9505 expr
= (struct bfd_elf_version_expr
*)
9506 htab_find ((htab_t
) head
->htab
, &e
);
9507 while (expr
&& strcmp (expr
->pattern
, java_sym
) == 0)
9508 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
9519 /* Finally, try the wildcards. */
9520 if (prev
== NULL
|| prev
->literal
)
9521 expr
= head
->remaining
;
9524 for (; expr
; expr
= expr
->next
)
9531 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
9534 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
9536 else if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
9540 if (fnmatch (expr
->pattern
, s
, 0) == 0)
9546 free ((char *) c_sym
);
9548 free ((char *) cxx_sym
);
9549 if (java_sym
!= sym
)
9550 free ((char *) java_sym
);
9554 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
9555 return a pointer to the symbol name with any backslash quotes removed. */
9558 realsymbol (const char *pattern
)
9561 bool changed
= false, backslash
= false;
9562 char *s
, *symbol
= (char *) xmalloc (strlen (pattern
) + 1);
9564 for (p
= pattern
, s
= symbol
; *p
!= '\0'; ++p
)
9566 /* It is a glob pattern only if there is no preceding
9570 /* Remove the preceding backslash. */
9577 if (*p
== '?' || *p
== '*' || *p
== '[')
9584 backslash
= *p
== '\\';
9591 pattern
= stat_strdup (symbol
);
9597 /* This is called for each variable name or match expression. NEW_NAME is
9598 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
9599 pattern to be matched against symbol names. */
9601 struct bfd_elf_version_expr
*
9602 lang_new_vers_pattern (struct bfd_elf_version_expr
*orig
,
9603 const char *new_name
,
9607 struct bfd_elf_version_expr
*ret
;
9609 ret
= stat_alloc (sizeof *ret
);
9613 ret
->literal
= true;
9614 ret
->pattern
= literal_p
? new_name
: realsymbol (new_name
);
9615 if (ret
->pattern
== NULL
)
9617 ret
->pattern
= new_name
;
9618 ret
->literal
= false;
9621 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
9622 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
9623 else if (strcasecmp (lang
, "C++") == 0)
9624 ret
->mask
= BFD_ELF_VERSION_CXX_TYPE
;
9625 else if (strcasecmp (lang
, "Java") == 0)
9626 ret
->mask
= BFD_ELF_VERSION_JAVA_TYPE
;
9629 einfo (_("%X%P: unknown language `%s' in version information\n"),
9631 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
9634 return ldemul_new_vers_pattern (ret
);
9637 /* This is called for each set of variable names and match
9640 struct bfd_elf_version_tree
*
9641 lang_new_vers_node (struct bfd_elf_version_expr
*globals
,
9642 struct bfd_elf_version_expr
*locals
)
9644 struct bfd_elf_version_tree
*ret
;
9646 ret
= stat_alloc (sizeof (*ret
));
9647 memset (ret
, 0, sizeof (*ret
));
9648 ret
->globals
.list
= globals
;
9649 ret
->locals
.list
= locals
;
9650 ret
->match
= lang_vers_match
;
9651 ret
->name_indx
= (unsigned int) -1;
9655 /* This static variable keeps track of version indices. */
9657 static int version_index
;
9660 version_expr_head_hash (const void *p
)
9662 const struct bfd_elf_version_expr
*e
=
9663 (const struct bfd_elf_version_expr
*) p
;
9665 return htab_hash_string (e
->pattern
);
9669 version_expr_head_eq (const void *p1
, const void *p2
)
9671 const struct bfd_elf_version_expr
*e1
=
9672 (const struct bfd_elf_version_expr
*) p1
;
9673 const struct bfd_elf_version_expr
*e2
=
9674 (const struct bfd_elf_version_expr
*) p2
;
9676 return strcmp (e1
->pattern
, e2
->pattern
) == 0;
9680 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head
*head
)
9683 struct bfd_elf_version_expr
*e
, *next
;
9684 struct bfd_elf_version_expr
**list_loc
, **remaining_loc
;
9686 for (e
= head
->list
; e
; e
= e
->next
)
9690 head
->mask
|= e
->mask
;
9695 head
->htab
= htab_create (count
* 2, version_expr_head_hash
,
9696 version_expr_head_eq
, NULL
);
9697 list_loc
= &head
->list
;
9698 remaining_loc
= &head
->remaining
;
9699 for (e
= head
->list
; e
; e
= next
)
9705 remaining_loc
= &e
->next
;
9709 void **loc
= htab_find_slot ((htab_t
) head
->htab
, e
, INSERT
);
9713 struct bfd_elf_version_expr
*e1
, *last
;
9715 e1
= (struct bfd_elf_version_expr
*) *loc
;
9719 if (e1
->mask
== e
->mask
)
9727 while (e1
&& strcmp (e1
->pattern
, e
->pattern
) == 0);
9731 e
->next
= last
->next
;
9739 list_loc
= &e
->next
;
9743 *remaining_loc
= NULL
;
9744 *list_loc
= head
->remaining
;
9747 head
->remaining
= head
->list
;
9750 /* This is called when we know the name and dependencies of the
9754 lang_register_vers_node (const char *name
,
9755 struct bfd_elf_version_tree
*version
,
9756 struct bfd_elf_version_deps
*deps
)
9758 struct bfd_elf_version_tree
*t
, **pp
;
9759 struct bfd_elf_version_expr
*e1
;
9764 if (link_info
.version_info
!= NULL
9765 && (name
[0] == '\0' || link_info
.version_info
->name
[0] == '\0'))
9767 einfo (_("%X%P: anonymous version tag cannot be combined"
9768 " with other version tags\n"));
9772 /* Make sure this node has a unique name. */
9773 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9774 if (strcmp (t
->name
, name
) == 0)
9775 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
9777 lang_finalize_version_expr_head (&version
->globals
);
9778 lang_finalize_version_expr_head (&version
->locals
);
9780 /* Check the global and local match names, and make sure there
9781 aren't any duplicates. */
9783 for (e1
= version
->globals
.list
; e1
!= NULL
; e1
= e1
->next
)
9785 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9787 struct bfd_elf_version_expr
*e2
;
9789 if (t
->locals
.htab
&& e1
->literal
)
9791 e2
= (struct bfd_elf_version_expr
*)
9792 htab_find ((htab_t
) t
->locals
.htab
, e1
);
9793 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
9795 if (e1
->mask
== e2
->mask
)
9796 einfo (_("%X%P: duplicate expression `%s'"
9797 " in version information\n"), e1
->pattern
);
9801 else if (!e1
->literal
)
9802 for (e2
= t
->locals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
9803 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
9804 && e1
->mask
== e2
->mask
)
9805 einfo (_("%X%P: duplicate expression `%s'"
9806 " in version information\n"), e1
->pattern
);
9810 for (e1
= version
->locals
.list
; e1
!= NULL
; e1
= e1
->next
)
9812 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9814 struct bfd_elf_version_expr
*e2
;
9816 if (t
->globals
.htab
&& e1
->literal
)
9818 e2
= (struct bfd_elf_version_expr
*)
9819 htab_find ((htab_t
) t
->globals
.htab
, e1
);
9820 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
9822 if (e1
->mask
== e2
->mask
)
9823 einfo (_("%X%P: duplicate expression `%s'"
9824 " in version information\n"),
9829 else if (!e1
->literal
)
9830 for (e2
= t
->globals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
9831 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
9832 && e1
->mask
== e2
->mask
)
9833 einfo (_("%X%P: duplicate expression `%s'"
9834 " in version information\n"), e1
->pattern
);
9838 version
->deps
= deps
;
9839 version
->name
= name
;
9840 if (name
[0] != '\0')
9843 version
->vernum
= version_index
;
9846 version
->vernum
= 0;
9848 for (pp
= &link_info
.version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
9853 /* This is called when we see a version dependency. */
9855 struct bfd_elf_version_deps
*
9856 lang_add_vers_depend (struct bfd_elf_version_deps
*list
, const char *name
)
9858 struct bfd_elf_version_deps
*ret
;
9859 struct bfd_elf_version_tree
*t
;
9861 ret
= stat_alloc (sizeof *ret
);
9864 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9866 if (strcmp (t
->name
, name
) == 0)
9868 ret
->version_needed
= t
;
9873 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
9875 ret
->version_needed
= NULL
;
9880 lang_do_version_exports_section (void)
9882 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
9884 LANG_FOR_EACH_INPUT_STATEMENT (is
)
9886 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
9894 contents
= stat_alloc (len
);
9895 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
9896 einfo (_("%X%P: unable to read .exports section contents\n"), sec
);
9899 while (p
< contents
+ len
)
9901 greg
= lang_new_vers_pattern (greg
, p
, NULL
, false);
9902 p
= strchr (p
, '\0') + 1;
9905 /* Do not include this section in the link. */
9906 sec
->flags
|= SEC_EXCLUDE
| SEC_KEEP
;
9909 lreg
= lang_new_vers_pattern (NULL
, "*", NULL
, false);
9910 lang_register_vers_node (command_line
.version_exports_section
,
9911 lang_new_vers_node (greg
, lreg
), NULL
);
9914 /* Evaluate LENGTH and ORIGIN parts of MEMORY spec. This is initially
9915 called with UPDATE_REGIONS_P set to FALSE, in this case no errors are
9916 thrown, however, references to symbols in the origin and length fields
9917 will be pushed into the symbol table, this allows PROVIDE statements to
9918 then provide these symbols. This function is called a second time with
9919 UPDATE_REGIONS_P set to TRUE, this time the we update the actual region
9920 data structures, and throw errors if missing symbols are encountered. */
9923 lang_do_memory_regions (bool update_regions_p
)
9925 lang_memory_region_type
*r
= lang_memory_region_list
;
9927 for (; r
!= NULL
; r
= r
->next
)
9931 exp_fold_tree_no_dot (r
->origin_exp
, NULL
);
9932 if (update_regions_p
)
9934 if (expld
.result
.valid_p
)
9936 r
->origin
= expld
.result
.value
;
9937 r
->current
= r
->origin
;
9940 einfo (_("%P: invalid origin for memory region %s\n"),
9946 exp_fold_tree_no_dot (r
->length_exp
, NULL
);
9947 if (update_regions_p
)
9949 if (expld
.result
.valid_p
)
9950 r
->length
= expld
.result
.value
;
9952 einfo (_("%P: invalid length for memory region %s\n"),
9960 lang_add_unique (const char *name
)
9962 struct unique_sections
*ent
;
9964 for (ent
= unique_section_list
; ent
; ent
= ent
->next
)
9965 if (strcmp (ent
->name
, name
) == 0)
9968 ent
= stat_alloc (sizeof *ent
);
9969 ent
->name
= stat_strdup (name
);
9970 ent
->next
= unique_section_list
;
9971 unique_section_list
= ent
;
9974 /* Append the list of dynamic symbols to the existing one. */
9977 lang_append_dynamic_list (struct bfd_elf_dynamic_list
**list_p
,
9978 struct bfd_elf_version_expr
*dynamic
)
9982 struct bfd_elf_version_expr
*tail
;
9983 for (tail
= dynamic
; tail
->next
!= NULL
; tail
= tail
->next
)
9985 tail
->next
= (*list_p
)->head
.list
;
9986 (*list_p
)->head
.list
= dynamic
;
9990 struct bfd_elf_dynamic_list
*d
;
9992 d
= stat_alloc (sizeof (*d
));
9993 memset (d
, 0, sizeof (*d
));
9994 d
->head
.list
= dynamic
;
9995 d
->match
= lang_vers_match
;
10000 /* Append the list of C++ typeinfo dynamic symbols to the existing
10004 lang_append_dynamic_list_cpp_typeinfo (void)
10006 const char *symbols
[] =
10008 "typeinfo name for*",
10011 struct bfd_elf_version_expr
*dynamic
= NULL
;
10014 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
10015 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
10018 lang_append_dynamic_list (&link_info
.dynamic_list
, dynamic
);
10021 /* Append the list of C++ operator new and delete dynamic symbols to the
10025 lang_append_dynamic_list_cpp_new (void)
10027 const char *symbols
[] =
10032 struct bfd_elf_version_expr
*dynamic
= NULL
;
10035 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
10036 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
10039 lang_append_dynamic_list (&link_info
.dynamic_list
, dynamic
);
10042 /* Scan a space and/or comma separated string of features. */
10045 lang_ld_feature (char *str
)
10053 while (*p
== ',' || ISSPACE (*p
))
10058 while (*q
&& *q
!= ',' && !ISSPACE (*q
))
10062 if (strcasecmp (p
, "SANE_EXPR") == 0)
10063 config
.sane_expr
= true;
10065 einfo (_("%X%P: unknown feature `%s'\n"), p
);
10071 /* Pretty print memory amount. */
10074 lang_print_memory_size (uint64_t sz
)
10077 printf (" %10" PRIu64
" B", sz
);
10078 else if ((sz
& 0x3fffffff) == 0)
10079 printf ("%10" PRIu64
" GB", sz
>> 30);
10080 else if ((sz
& 0xfffff) == 0)
10081 printf ("%10" PRIu64
" MB", sz
>> 20);
10082 else if ((sz
& 0x3ff) == 0)
10083 printf ("%10" PRIu64
" KB", sz
>> 10);
10085 printf (" %10" PRIu64
" B", sz
);
10088 /* Implement --print-memory-usage: disply per region memory usage. */
10091 lang_print_memory_usage (void)
10093 lang_memory_region_type
*r
;
10095 printf ("Memory region Used Size Region Size %%age Used\n");
10096 for (r
= lang_memory_region_list
; r
->next
!= NULL
; r
= r
->next
)
10098 bfd_vma used_length
= r
->current
- r
->origin
;
10100 printf ("%16s: ",r
->name_list
.name
);
10101 lang_print_memory_size (used_length
);
10102 lang_print_memory_size (r
->length
);
10104 if (r
->length
!= 0)
10106 double percent
= used_length
* 100.0 / r
->length
;
10107 printf (" %6.2f%%", percent
);
10114 cmdline_lists_init (void)
10116 cmdline_object_only_file_list
.tail
10117 = &cmdline_object_only_file_list
.head
;
10118 cmdline_object_only_archive_list
.tail
10119 = &cmdline_object_only_archive_list
.head
;
10120 cmdline_temp_object_only_list
.tail
10121 = &cmdline_temp_object_only_list
.head
;
10124 /* Allocate an item with TYPE and DATA. */
10126 static cmdline_union_type
*
10127 cmdline_list_new (cmdline_enum_type type
, void *data
)
10129 cmdline_union_type
*new_opt
;
10131 new_opt
= (cmdline_union_type
*) stat_alloc (sizeof (*new_opt
));
10132 new_opt
->header
.type
= type
;
10137 case cmdline_is_file_enum
:
10138 new_opt
->file
.filename
= (const char *) data
;
10140 case cmdline_is_bfd_enum
:
10141 new_opt
->abfd
.abfd
= (bfd
*) data
;
10147 /* Append an item with TYPE and DATA to LIST. */
10150 cmdline_list_append (cmdline_list_type
*list
, cmdline_enum_type type
,
10153 cmdline_union_type
*new_opt
= cmdline_list_new (type
, data
);
10154 new_opt
->header
.next
= NULL
;
10155 *list
->tail
= new_opt
;
10156 list
->tail
= &new_opt
->header
.next
;
10160 print_cmdline_list (cmdline_union_type
*c
)
10162 for (; c
!= NULL
; c
= c
->header
.next
)
10163 switch (c
->header
.type
)
10167 case cmdline_is_file_enum
:
10168 info_msg (" %s", c
->file
.filename
);
10170 case cmdline_is_bfd_enum
:
10171 info_msg (" [%B]", c
->abfd
.abfd
);
10178 /* Return TRUE if ABFD is on cmdline_object_only_archive_list. */
10181 cmdline_on_object_only_archive_list_p (bfd
*abfd
)
10183 cmdline_union_type
*c
, *next
;
10184 bfd
*archive
, *obfd
, *oarchive
;
10185 ufile_ptr origin
= abfd
->origin
;
10187 archive
= abfd
->my_archive
;
10188 for (c
= cmdline_object_only_archive_list
.head
; c
!= NULL
; c
= next
)
10190 if (c
->header
.type
!= cmdline_is_bfd_enum
)
10193 next
= c
->header
.next
;
10194 obfd
= c
->abfd
.abfd
;
10195 oarchive
= obfd
->my_archive
;
10197 /* The list is grouped by archive file name and sorted by member
10199 if (strcmp (archive
->filename
, oarchive
->filename
) != 0)
10202 if (origin
== obfd
->origin
)
10204 else if (origin
< obfd
->origin
)
10211 /* Append an item with TYPE and DATA to cmdline_object_only_file_list
10212 or cmdline_object_only_archive_list if needed. */
10215 cmdline_object_only_list_append (cmdline_enum_type type
, void *data
)
10217 cmdline_union_type
*c
;
10218 cmdline_union_type
*new_opt
, *next
, **prev
;
10219 bfd
*abfd
, *archive
;
10220 bfd
*obfd
, *oarchive
;
10221 bfd
*nbfd
, *narchive
;
10222 ufile_ptr origin
, norigin
;
10224 /* Put it on cmdline_object_only_file_list if it isn't an archive
10230 case cmdline_is_bfd_enum
:
10231 abfd
= (bfd
*) data
;
10232 archive
= abfd
->my_archive
;
10236 case cmdline_is_file_enum
:
10237 cmdline_list_append (&cmdline_object_only_file_list
, type
, data
);
10241 /* Put archive member on cmdline_object_only_archive_list and sort
10242 the list by archive name and archive member origin. */
10243 new_opt
= (cmdline_union_type
*) stat_alloc (sizeof (*new_opt
));
10244 new_opt
->header
.type
= cmdline_is_bfd_enum
;
10245 new_opt
->header
.next
= NULL
;
10246 new_opt
->abfd
.abfd
= (bfd
*) data
;
10248 c
= cmdline_object_only_archive_list
.head
;
10251 cmdline_object_only_archive_list
.head
= new_opt
;
10252 cmdline_object_only_archive_list
.tail
= &new_opt
->header
.next
;
10257 origin
= abfd
->origin
;
10258 for (; c
!= NULL
; c
= next
)
10260 if (c
->header
.type
!= cmdline_is_bfd_enum
)
10263 next
= c
->header
.next
;
10265 obfd
= c
->abfd
.abfd
;
10266 oarchive
= obfd
->my_archive
;
10268 if (strcmp (archive
->filename
, oarchive
->filename
) == 0)
10272 if (origin
< obfd
->origin
)
10274 /* Insert it before the current. */
10275 new_opt
->header
.next
= c
;
10279 cmdline_object_only_archive_list
.head
= new_opt
;
10285 /* Check origin. */
10288 if (next
->header
.type
!= cmdline_is_bfd_enum
)
10291 nbfd
= next
->abfd
.abfd
;
10292 norigin
= nbfd
->origin
;
10293 if (origin
> norigin
)
10295 /* Insert it after NEXT. */
10299 narchive
= nbfd
->my_archive
;
10300 if (strcmp (archive
->filename
, narchive
->filename
) != 0)
10302 /* Insert it befor NEXT. */
10308 next
= next
->header
.next
;
10314 next
= next
->header
.next
;
10317 if (*cmdline_object_only_archive_list
.tail
== c
->header
.next
)
10318 cmdline_object_only_archive_list
.tail
10319 = &new_opt
->header
.next
;
10321 prev
= &c
->header
.next
;
10322 new_opt
->header
.next
= next
;
10327 prev
= &c
->header
.next
;
10330 *cmdline_object_only_archive_list
.tail
= new_opt
;
10331 cmdline_object_only_archive_list
.tail
= &new_opt
->header
.next
;
10334 /* Get object-only input files. */
10337 cmdline_get_object_only_input_files (void)
10339 cmdline_union_type
*c
, *next
;
10340 bfd
*abfd
, *archive
;
10341 bfd
*nbfd
, *narchive
;
10343 /* Add files first. */
10344 for (c
= cmdline_object_only_file_list
.head
;
10345 c
!= NULL
; c
= c
->header
.next
)
10346 switch (c
->header
.type
)
10350 case cmdline_is_file_enum
:
10351 lang_add_input_file (c
->file
.filename
,
10352 lang_input_file_is_file_enum
, NULL
);
10354 case cmdline_is_bfd_enum
:
10355 abfd
= c
->abfd
.abfd
;
10356 if (abfd
->my_archive
)
10358 lang_add_input_file (abfd
->filename
,
10359 lang_input_file_is_file_enum
, NULL
);
10363 /* Add archive members next. */
10364 for (c
= cmdline_object_only_archive_list
.head
; c
!= NULL
; c
= next
)
10366 if (c
->header
.type
!= cmdline_is_bfd_enum
)
10369 next
= c
->header
.next
;
10371 abfd
= c
->abfd
.abfd
;
10372 archive
= abfd
->my_archive
;
10374 /* Add the first archive of the archive member group. */
10375 lang_add_input_file (archive
->filename
,
10376 lang_input_file_is_file_enum
, NULL
);
10378 /* Skip the rest members in the archive member group. */
10384 if (next
->header
.type
!= cmdline_is_bfd_enum
)
10387 next
= next
->header
.next
;
10390 nbfd
= next
->abfd
.abfd
;
10391 narchive
= nbfd
->my_archive
;
10393 while (strcmp (archive
->filename
, narchive
->filename
) == 0);
10404 /* Create a section in OBFD with the same
10405 name and attributes as ISECTION in IBFD. */
10408 setup_section (bfd
*ibfd
, sec_ptr isection
, void *p
)
10410 struct cmdline_arg
*arg
= (struct cmdline_arg
*) p
;
10411 bfd
*obfd
= arg
->obfd
;
10412 asymbol
**isympp
= arg
->isympp
;
10413 const char *name
= isection
->name
;
10417 /* Skip the object-only section. */
10418 if (ibfd
->object_only_section
== isection
)
10421 /* If we have already failed earlier on, do not keep on generating
10426 osection
= bfd_make_section_anyway_with_flags (obfd
, name
,
10429 if (osection
== NULL
)
10431 err
= _("failed to create output section");
10435 osection
->size
= isection
->size
;
10436 osection
->vma
= isection
->vma
;
10437 osection
->lma
= isection
->lma
;
10438 osection
->alignment_power
= isection
->alignment_power
;
10440 /* Copy merge entity size. */
10441 osection
->entsize
= isection
->entsize
;
10443 /* This used to be mangle_section; we do here to avoid using
10444 bfd_get_section_by_name since some formats allow multiple
10445 sections with the same name. */
10446 isection
->output_section
= osection
;
10447 isection
->output_offset
= 0;
10449 if ((isection
->flags
& SEC_GROUP
) != 0)
10451 asymbol
*gsym
= bfd_group_signature (isection
, isympp
);
10455 gsym
->flags
|= BSF_KEEP
;
10456 if (ibfd
->xvec
->flavour
== bfd_target_elf_flavour
)
10457 elf_group_id (isection
) = gsym
;
10461 /* Allow the BFD backend to copy any private data it understands
10462 from the input section to the output section. */
10463 if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
10465 err
= _("failed to copy private data");
10469 /* All went well. */
10474 einfo (_("%P%F: setup_section: %s: %s\n"), err
, name
);
10477 /* Copy the data of input section ISECTION of IBFD
10478 to an output section with the same name in OBFD.
10479 If stripping then don't copy any relocation info. */
10482 copy_section (bfd
*ibfd
, sec_ptr isection
, void *p
)
10484 struct cmdline_arg
*arg
= (struct cmdline_arg
*) p
;
10485 bfd
*obfd
= arg
->obfd
;
10486 asymbol
**isympp
= arg
->isympp
;
10490 bfd_size_type size
;
10495 /* Skip the object-only section. */
10496 if (ibfd
->object_only_section
== isection
)
10499 /* If we have already failed earlier on, do not keep on generating
10504 flags
= bfd_section_flags (isection
);
10505 if ((flags
& SEC_GROUP
) != 0)
10508 osection
= isection
->output_section
;
10509 size
= bfd_section_size (isection
);
10511 if (size
== 0 || osection
== 0)
10514 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
10518 /* Do not complain if the target does not support relocations. */
10520 && bfd_get_error () == bfd_error_invalid_operation
)
10524 err
= bfd_errmsg (bfd_get_error ());
10530 bfd_set_reloc (obfd
, osection
, NULL
, 0);
10533 relpp
= (arelent
**) xmalloc (relsize
);
10534 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
10537 err
= _("relocation count is negative");
10541 bfd_set_reloc (obfd
, osection
,
10542 relcount
== 0 ? NULL
: relpp
, relcount
);
10547 if (bfd_section_flags (isection
) & SEC_HAS_CONTENTS
)
10549 bfd_byte
*memhunk
= NULL
;
10551 if (!bfd_get_full_section_contents (ibfd
, isection
, &memhunk
))
10553 err
= bfd_errmsg (bfd_get_error ());
10557 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
10559 err
= bfd_errmsg (bfd_get_error ());
10565 /* All went well. */
10569 einfo (_("%P%F: copy_section: %s: %s\n"), err
, isection
->name
);
10571 /* Open the temporary bfd created in the same directory as PATH. */
10574 cmdline_fopen_temp (const char *path
, const char *target
,
10577 #define template "ldXXXXXX"
10578 const char *slash
= strrchr (path
, '/');
10583 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
10585 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
10586 char *bslash
= strrchr (path
, '\\');
10588 if (slash
== NULL
|| (bslash
!= NULL
&& bslash
> slash
))
10590 if (slash
== NULL
&& path
[0] != '\0' && path
[1] == ':')
10595 if (slash
!= (char *) NULL
)
10597 len
= slash
- path
;
10598 tmpname
= (char *) xmalloc (len
+ sizeof (template) + 2);
10599 memcpy (tmpname
, path
, len
);
10601 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
10602 /* If tmpname is "X:", appending a slash will make it a root
10603 directory on drive X, which is NOT the same as the current
10604 directory on drive X. */
10605 if (len
== 2 && tmpname
[1] == ':')
10606 tmpname
[len
++] = '.';
10608 tmpname
[len
++] = '/';
10612 tmpname
= (char *) xmalloc (sizeof (template));
10616 memcpy (tmpname
+ len
, template, sizeof (template));
10619 #ifdef HAVE_MKSTEMP
10620 fd
= mkstemp (tmpname
);
10622 tmpname
= mktemp (tmpname
);
10623 if (tmpname
== NULL
)
10625 fd
= open (tmpname
, O_RDWR
| O_CREAT
| O_EXCL
, 0600);
10629 return bfd_fopen (tmpname
, target
, mode
, fd
);
10632 /* Add the object-only section. */
10635 cmdline_add_object_only_section (bfd_byte
*contents
, size_t size
)
10639 enum bfd_architecture iarch
;
10640 unsigned int imach
;
10643 asymbol
**isympp
= NULL
;
10644 asymbol
**osympp
= NULL
;
10645 bfd
*obfd
= NULL
, *ibfd
;
10654 char *ofilename
= NULL
;
10657 ibfd
= bfd_openr (output_filename
, output_target
);
10660 err
= bfd_errmsg (bfd_get_error ());
10664 if (!bfd_check_format_matches (ibfd
, bfd_object
, &matching
))
10666 err
= bfd_errmsg (bfd_get_error ());
10670 obfd
= cmdline_fopen_temp (output_filename
, output_target
, FOPEN_WB
);
10673 err
= bfd_errmsg (bfd_get_error ());
10676 /* To be used after bfd_close (). */
10677 ofilename
= xstrdup (bfd_get_filename (obfd
));
10679 if (!bfd_set_format (obfd
, bfd_object
))
10681 err
= bfd_errmsg (bfd_get_error ());
10685 /* Copy the start address, flags and architecture of input file to
10687 flags
= bfd_get_file_flags (ibfd
);
10688 start
= bfd_get_start_address (ibfd
);
10689 iarch
= bfd_get_arch (ibfd
);
10690 imach
= bfd_get_mach (ibfd
);
10691 if (!bfd_set_start_address (obfd
, start
)
10692 || !bfd_set_file_flags (obfd
, flags
)
10693 || !bfd_set_arch_mach (obfd
, iarch
, imach
))
10695 err
= bfd_errmsg (bfd_get_error ());
10699 symsize
= bfd_get_symtab_upper_bound (ibfd
);
10702 err
= bfd_errmsg (bfd_get_error ());
10706 isympp
= (asymbol
**) xmalloc (symsize
);
10707 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
10710 err
= bfd_errmsg (bfd_get_error ());
10715 arg
.isympp
= isympp
;
10718 /* BFD mandates that all output sections be created and sizes set before
10719 any output is done. Thus, we traverse all sections multiple times. */
10720 bfd_map_over_sections (ibfd
, setup_section
, &arg
);
10724 err
= _("error setting up sections");
10728 /* Allow the BFD backend to copy any private data it understands
10729 from the input section to the output section. */
10730 if (! bfd_copy_private_header_data (ibfd
, obfd
))
10732 err
= _("error copying private header data");
10736 /* Create the object-only section. */
10737 sec
= bfd_make_section_with_flags (obfd
,
10738 GNU_OBJECT_ONLY_SECTION_NAME
,
10742 | SEC_LINKER_CREATED
));
10745 err
= _("can't create object-only section");
10749 if (! bfd_set_section_size (sec
, size
))
10751 err
= _("can't set object-only section size");
10755 if (ibfd
->object_only_section
)
10757 /* Filter out the object-only section symbol. */
10758 long src_count
= 0, dst_count
= 0;
10759 asymbol
**from
, **to
;
10761 osympp
= xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
10764 for (; src_count
< symcount
; src_count
++)
10766 asymbol
*sym
= from
[src_count
];
10767 if (bfd_asymbol_section (sym
) != ibfd
->object_only_section
)
10768 to
[dst_count
++] = sym
;
10770 to
[dst_count
] = NULL
;
10771 symcount
= dst_count
;
10772 bfd_set_symtab (obfd
, osympp
, symcount
);
10775 bfd_set_symtab (obfd
, isympp
, symcount
);
10777 /* This has to happen after the symbol table has been set. */
10778 bfd_map_over_sections (ibfd
, copy_section
, &arg
);
10782 err
= _("error copying sections");
10786 /* Copy the object-only section to the output. */
10787 if (! bfd_set_section_contents (obfd
, sec
, contents
, 0, size
))
10789 err
= _("error adding object-only section");
10793 /* Allow the BFD backend to copy any private data it understands
10794 from the input BFD to the output BFD. This is done last to
10795 permit the routine to look at the filtered symbol table, which is
10796 important for the ECOFF code at least. */
10797 if (! bfd_copy_private_bfd_data (ibfd
, obfd
))
10799 err
= _("error copying private BFD data");
10803 if (!bfd_close (obfd
))
10805 unlink (ofilename
);
10806 einfo (_("%P%F: failed to finish output with object-only section\n"));
10809 /* Must be freed after bfd_close (). */
10813 if (rename (ofilename
, output_filename
))
10815 unlink (ofilename
);
10816 einfo (_("%P%F: failed to rename output with object-only section\n"));
10829 unlink (ofilename
);
10832 einfo (_("%P%F: failed to add object-only section: %s\n"), err
);
10835 /* Emit the final output with object-only section. */
10838 cmdline_emit_object_only_section (void)
10840 const char *saved_output_filename
= output_filename
;
10843 bfd_byte
*contents
;
10846 /* Get a temporary object-only file. */
10847 output_filename
= make_temp_file (".obj-only.o");
10849 had_output_filename
= false;
10850 link_info
.input_bfds
= NULL
;
10851 link_info
.input_bfds_tail
= &link_info
.input_bfds
;
10856 /* Set up the object-only output. */
10859 /* Open the object-only file for output. */
10860 lang_for_each_statement (ldlang_open_output
);
10862 ldemul_create_output_section_statements ();
10864 if (!bfd_section_already_linked_table_init ())
10865 einfo (_("%P%F: Failed to create hash table\n"));
10867 /* Call cmdline_on_object_only_archive_list_p to check which member
10868 should be loaded. */
10869 input_flags
.whole_archive
= true;
10871 /* Set it to avoid adding more to cmdline lists. */
10872 config
.emitting_gnu_object_only
= true;
10874 /* Get object-only input files. */
10875 cmdline_get_object_only_input_files ();
10877 /* Open object-only input files. */
10878 open_input_bfds (statement_list
.head
, NULL
, OPEN_BFD_NORMAL
);
10880 ldemul_after_open ();
10882 bfd_section_already_linked_table_free ();
10884 /* Make sure that we're not mixing architectures. We call this
10885 after all the input files have been opened, but before we do any
10886 other processing, so that any operations merge_private_bfd_data
10887 does on the output file will be known during the rest of the
10891 /* Size up the common data. */
10894 /* Update wild statements. */
10895 update_wild_statements (statement_list
.head
);
10897 /* Run through the contours of the script and attach input sections
10898 to the correct output sections. */
10899 map_input_to_output_sections (statement_list
.head
, NULL
, NULL
);
10901 /* Find any sections not attached explicitly and handle them. */
10902 lang_place_orphans ();
10904 /* Do anything special before sizing sections. This is where ELF
10905 and other back-ends size dynamic sections. */
10906 ldemul_before_allocation ();
10908 /* Size up the sections. */
10909 lang_size_sections (NULL
, ! RELAXATION_ENABLED
);
10911 /* See if anything special should be done now we know how big
10912 everything is. This is where relaxation is done. */
10913 ldemul_after_allocation ();
10917 /* Make sure that the section addresses make sense. */
10918 if (command_line
.check_section_addresses
)
10919 lang_check_section_addresses ();
10925 ldexp_finish (true);
10928 if (! bfd_close (link_info
.output_bfd
))
10929 einfo (_("%P%F:%s: final close failed on object-only output: %E\n"),
10932 link_info
.output_bfd
= NULL
;
10934 /* Read in the object-only file. */
10935 fd
= open (output_filename
, O_RDONLY
| O_BINARY
);
10938 bfd_set_error (bfd_error_system_call
);
10939 einfo (_("%P%F:%s: cannot open object-only output: %E\n"),
10943 /* Get the object-only file size. */
10944 if (fstat (fd
, &st
) != 0)
10946 bfd_set_error (bfd_error_system_call
);
10947 einfo (_("%P%F:%s: cannot stat object-only output: %E\n"),
10953 contents
= (bfd_byte
*) xmalloc (size
);
10954 while (off
!= size
)
10958 got
= read (fd
, contents
+ off
, size
- off
);
10961 bfd_set_error (bfd_error_system_call
);
10962 einfo (_("%P%F:%s: read failed on object-only output: %E\n"),
10971 /* Remove the temporary object-only file. */
10972 unlink (output_filename
);
10974 output_filename
= saved_output_filename
;
10976 cmdline_add_object_only_section (contents
, size
);
10981 /* Extract the object-only section. */
10983 static const char *
10984 cmdline_extract_object_only_section (bfd
*abfd
)
10986 const char *name
= bfd_extract_object_only_section (abfd
);
10989 einfo (_("%P%F: cannot extract object-only section from %B: %E\n"),
10992 /* It should be removed after it is done. */
10993 cmdline_list_append (&cmdline_temp_object_only_list
,
10994 cmdline_is_file_enum
, (void *) name
);
10999 /* Load the object-only section. */
11002 cmdline_load_object_only_section (const char *name
)
11004 lang_input_statement_type
*entry
11005 = new_afile (name
, lang_input_file_is_file_enum
, NULL
, NULL
);
11010 ldfile_open_file (entry
);
11012 if (trace_files
|| verbose
)
11013 info_msg ("%pI\n", entry
);
11015 if (entry
->flags
.missing_file
11016 || bfd_get_format (entry
->the_bfd
) != bfd_object
)
11019 ldlang_add_file (entry
);
11021 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
11022 entry
->flags
.loaded
= true;
11024 einfo (_("%F%P: %pB: error adding symbols: %E\n"), entry
->the_bfd
);
11027 /* Check and handle the object-only section. */
11030 cmdline_check_object_only_section (bfd
*abfd
, bool lto
)
11032 const char *filename
;
11034 if (config
.emitting_gnu_object_only
|| abfd
->format
!= bfd_object
)
11039 /* For LTO link, we only need to extract object-only section
11040 from the mixed object, add it to input, and put it on LTO
11042 switch (bfd_get_lto_type (abfd
))
11046 case lto_mixed_object
:
11047 filename
= cmdline_extract_object_only_section (abfd
);
11048 cmdline_load_object_only_section (filename
);
11050 case lto_non_ir_object
:
11051 case lto_slim_ir_object
:
11052 case lto_fat_ir_object
:
11056 else if (bfd_link_relocatable (&link_info
))
11058 /* For non-LTO relocatable link, we need to append non-IR object
11059 file and the object file in object-only section to the object
11061 switch (bfd_get_lto_type (abfd
))
11065 case lto_mixed_object
:
11066 filename
= cmdline_extract_object_only_section (abfd
);
11067 cmdline_object_only_list_append (cmdline_is_file_enum
,
11068 (void *) filename
);
11070 case lto_non_ir_object
:
11071 cmdline_object_only_list_append (cmdline_is_bfd_enum
, abfd
);
11073 case lto_slim_ir_object
:
11074 case lto_fat_ir_object
:
11080 /* Remove temporary object-only files. */
11083 cmdline_remove_object_only_files (void)
11085 cmdline_union_type
*c
;
11087 if (config
.plugin_save_temps
)
11090 c
= cmdline_temp_object_only_list
.head
;
11091 for (; c
!= NULL
; c
= c
->header
.next
)
11092 switch (c
->header
.type
)
11096 case cmdline_is_file_enum
:
11097 unlink (c
->file
.filename
);