1 /* Linker command language support.
2 Copyright (C) 1991-2023 Free Software Foundation, Inc.
4 This file is part of the GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
25 #include "libiberty.h"
26 #include "filenames.h"
27 #include "safe-ctype.h"
46 #if BFD_SUPPORTS_PLUGINS
48 #endif /* BFD_SUPPORTS_PLUGINS */
51 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
54 /* Convert between addresses in bytes and sizes in octets.
55 For currently supported targets, octets_per_byte is always a power
56 of two, so we can use shifts. */
57 #define TO_ADDR(X) ((X) >> opb_shift)
58 #define TO_SIZE(X) ((X) << opb_shift)
60 /* Local variables. */
61 static struct obstack stat_obstack
;
62 static struct obstack map_obstack
;
63 static struct obstack pt_obstack
;
65 #define obstack_chunk_alloc xmalloc
66 #define obstack_chunk_free free
67 static const char *entry_symbol_default
= "start";
68 static bool map_head_is_link_order
= false;
69 static lang_output_section_statement_type
*default_common_section
;
70 static bool map_option_f
;
71 static bfd_vma print_dot
;
72 static lang_input_statement_type
*first_file
;
73 static const char *current_target
;
74 static lang_statement_list_type
*stat_save
[10];
75 static lang_statement_list_type
**stat_save_ptr
= &stat_save
[0];
76 static struct unique_sections
*unique_section_list
;
77 static struct asneeded_minfo
*asneeded_list_head
;
78 static unsigned int opb_shift
= 0;
80 /* Forward declarations. */
81 static void exp_init_os (etree_type
*);
82 static lang_input_statement_type
*lookup_name (const char *);
83 static void insert_undefined (const char *);
84 static bool sort_def_symbol (struct bfd_link_hash_entry
*, void *);
85 static lang_statement_union_type
*new_statement (enum statement_enum type
,
87 lang_statement_list_type
*list
);
88 static void print_statement (lang_statement_union_type
*,
89 lang_output_section_statement_type
*);
90 static void print_statement_list (lang_statement_union_type
*,
91 lang_output_section_statement_type
*);
92 static void print_statements (void);
93 static void print_input_section (asection
*, bool);
94 static bool lang_one_common (struct bfd_link_hash_entry
*, void *);
95 static void lang_record_phdrs (void);
96 static void lang_do_version_exports_section (void);
97 static void lang_finalize_version_expr_head
98 (struct bfd_elf_version_expr_head
*);
99 static void lang_do_memory_regions (bool);
101 /* Exported variables. */
102 const char *output_target
;
103 lang_output_section_statement_type
*abs_output_section
;
104 /* Header for list of statements corresponding to any files involved in the
105 link, either specified from the command-line or added implicitely (eg.
106 archive member used to resolved undefined symbol, wildcard statement from
107 linker script, etc.). Next pointer is in next field of a
108 lang_statement_header_type (reached via header field in a
109 lang_statement_union). */
110 lang_statement_list_type statement_list
;
111 lang_statement_list_type lang_os_list
;
112 lang_statement_list_type
*stat_ptr
= &statement_list
;
113 /* Header for list of statements corresponding to files used in the final
114 executable. This can be either object file specified on the command-line
115 or library member resolving an undefined reference. Next pointer is in next
116 field of a lang_input_statement_type (reached via input_statement field in a
117 lang_statement_union). */
118 lang_statement_list_type file_chain
= { NULL
, NULL
};
119 /* Header for list of statements corresponding to files specified on the
120 command-line for linking. It thus contains real object files and archive
121 but not archive members. Next pointer is in next_real_file field of a
122 lang_input_statement_type statement (reached via input_statement field in a
123 lang_statement_union). */
124 lang_statement_list_type input_file_chain
;
125 static const char *current_input_file
;
126 struct bfd_elf_dynamic_list
**current_dynamic_list_p
;
127 struct bfd_sym_chain entry_symbol
= { NULL
, NULL
};
128 const char *entry_section
= ".text";
129 struct lang_input_statement_flags input_flags
;
130 bool entry_from_cmdline
;
131 bool lang_has_input_file
= false;
132 bool had_output_filename
= false;
133 bool lang_float_flag
= false;
134 bool delete_output_file_on_failure
= false;
135 struct lang_phdr
*lang_phdr_list
;
136 struct lang_nocrossrefs
*nocrossref_list
;
137 struct asneeded_minfo
**asneeded_list_tail
;
139 static ctf_dict_t
*ctf_output
;
142 /* Functions that traverse the linker script and might evaluate
143 DEFINED() need to increment this at the start of the traversal. */
144 int lang_statement_iteration
= 0;
146 /* Count times through one_lang_size_sections_pass after mark phase. */
147 static int lang_sizing_iteration
= 0;
149 /* Return TRUE if the PATTERN argument is a wildcard pattern.
150 Although backslashes are treated specially if a pattern contains
151 wildcards, we do not consider the mere presence of a backslash to
152 be enough to cause the pattern to be treated as a wildcard.
153 That lets us handle DOS filenames more naturally. */
154 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
156 #define new_stat(x, y) \
157 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
159 #define outside_section_address(q) \
160 ((q)->output_offset + (q)->output_section->vma)
162 #define outside_symbol_address(q) \
163 ((q)->value + outside_section_address (q->section))
165 /* CTF sections smaller than this are not compressed: compression of
166 dictionaries this small doesn't gain much, and this lets consumers mmap the
167 sections directly out of the ELF file and use them with no decompression
168 overhead if they want to. */
169 #define CTF_COMPRESSION_THRESHOLD 4096
172 stat_alloc (size_t size
)
174 return obstack_alloc (&stat_obstack
, size
);
177 /* Code for handling simple wildcards without going through fnmatch,
178 which can be expensive because of charset translations etc. */
180 /* A simple wild is a literal string followed by a single '*',
181 where the literal part is at least 4 characters long. */
184 is_simple_wild (const char *name
)
186 size_t len
= strcspn (name
, "*?[");
187 return len
>= 4 && name
[len
] == '*' && name
[len
+ 1] == '\0';
191 match_simple_wild (const char *pattern
, const char *name
)
193 /* The first four characters of the pattern are guaranteed valid
194 non-wildcard characters. So we can go faster. */
195 if (pattern
[0] != name
[0] || pattern
[1] != name
[1]
196 || pattern
[2] != name
[2] || pattern
[3] != name
[3])
201 while (*pattern
!= '*')
202 if (*name
++ != *pattern
++)
209 name_match (const char *pattern
, const char *name
)
211 if (is_simple_wild (pattern
))
212 return !match_simple_wild (pattern
, name
);
213 if (wildcardp (pattern
))
214 return fnmatch (pattern
, name
, 0);
215 return strcmp (pattern
, name
);
218 /* Given an analyzed wildcard_spec SPEC, match it against NAME,
219 returns zero on a match, non-zero if there's no match. */
222 spec_match (const struct wildcard_spec
*spec
, const char *name
)
224 size_t nl
= spec
->namelen
;
225 size_t pl
= spec
->prefixlen
;
226 size_t sl
= spec
->suffixlen
;
227 size_t inputlen
= strlen (name
);
235 r
= memcmp (spec
->name
, name
, pl
);
245 r
= memcmp (spec
->name
+ nl
- sl
, name
+ inputlen
- sl
, sl
);
250 if (nl
== pl
+ sl
+ 1 && spec
->name
[pl
] == '*')
254 return fnmatch (spec
->name
+ pl
, name
+ pl
, 0);
263 ldirname (const char *name
)
265 const char *base
= lbasename (name
);
268 while (base
> name
&& IS_DIR_SEPARATOR (base
[-1]))
272 dirname
= strdup (name
);
273 dirname
[base
- name
] = '\0';
277 /* If PATTERN is of the form archive:file, return a pointer to the
278 separator. If not, return NULL. */
281 archive_path (const char *pattern
)
285 if (link_info
.path_separator
== 0)
288 p
= strchr (pattern
, link_info
.path_separator
);
289 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
290 if (p
== NULL
|| link_info
.path_separator
!= ':')
293 /* Assume a match on the second char is part of drive specifier,
294 as in "c:\silly.dos". */
295 if (p
== pattern
+ 1 && ISALPHA (*pattern
))
296 p
= strchr (p
+ 1, link_info
.path_separator
);
301 /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path,
302 return whether F matches FILE_SPEC. */
305 input_statement_is_archive_path (const char *file_spec
, char *sep
,
306 lang_input_statement_type
*f
)
311 || name_match (sep
+ 1, f
->filename
) == 0)
312 && ((sep
!= file_spec
)
313 == (f
->the_bfd
!= NULL
&& f
->the_bfd
->my_archive
!= NULL
)))
317 if (sep
!= file_spec
)
319 const char *aname
= bfd_get_filename (f
->the_bfd
->my_archive
);
321 match
= name_match (file_spec
, aname
) == 0;
322 *sep
= link_info
.path_separator
;
329 unique_section_p (const asection
*sec
,
330 const lang_output_section_statement_type
*os
)
332 struct unique_sections
*unam
;
335 if (!link_info
.resolve_section_groups
336 && sec
->owner
!= NULL
337 && bfd_is_group_section (sec
->owner
, sec
))
339 && strcmp (os
->name
, DISCARD_SECTION_NAME
) == 0);
342 for (unam
= unique_section_list
; unam
; unam
= unam
->next
)
343 if (name_match (unam
->name
, secnam
) == 0)
349 /* Generic traversal routines for finding matching sections. */
351 /* Return true if FILE matches a pattern in EXCLUDE_LIST, otherwise return
355 walk_wild_file_in_exclude_list (struct name_list
*exclude_list
,
356 lang_input_statement_type
*file
)
358 struct name_list
*list_tmp
;
360 for (list_tmp
= exclude_list
;
362 list_tmp
= list_tmp
->next
)
364 char *p
= archive_path (list_tmp
->name
);
368 if (input_statement_is_archive_path (list_tmp
->name
, p
, file
))
372 else if (name_match (list_tmp
->name
, file
->filename
) == 0)
375 /* FIXME: Perhaps remove the following at some stage? Matching
376 unadorned archives like this was never documented and has
377 been superceded by the archive:path syntax. */
378 else if (file
->the_bfd
!= NULL
379 && file
->the_bfd
->my_archive
!= NULL
380 && name_match (list_tmp
->name
,
381 bfd_get_filename (file
->the_bfd
->my_archive
)) == 0)
388 /* Add SECTION (from input FILE) to the list of matching sections
389 within PTR (the matching wildcard is SEC). */
392 add_matching_section (lang_wild_statement_type
*ptr
,
393 struct wildcard_list
*sec
,
395 lang_input_statement_type
*file
)
397 lang_input_matcher_type
*new_section
;
398 /* Add a section reference to the list. */
399 new_section
= new_stat (lang_input_matcher
, &ptr
->matching_sections
);
400 new_section
->section
= section
;
401 new_section
->pattern
= sec
;
402 new_section
->input_stmt
= file
;
405 /* Process section S (from input file FILE) in relation to wildcard
406 statement PTR. We already know that a prefix of the name of S matches
407 some wildcard in PTR's wildcard list. Here we check if the filename
408 matches as well (if it's specified) and if any of the wildcards in fact
412 walk_wild_section_match (lang_wild_statement_type
*ptr
,
413 lang_input_statement_type
*file
,
416 struct wildcard_list
*sec
;
417 const char *file_spec
= ptr
->filename
;
420 /* Check if filenames match. */
421 if (file_spec
== NULL
)
423 else if ((p
= archive_path (file_spec
)) != NULL
)
425 if (!input_statement_is_archive_path (file_spec
, p
, file
))
428 else if (wildcardp (file_spec
))
430 if (fnmatch (file_spec
, file
->filename
, 0) != 0)
435 lang_input_statement_type
*f
;
436 /* Perform the iteration over a single file. */
437 f
= lookup_name (file_spec
);
442 /* If filename is excluded we're done. */
443 if (walk_wild_file_in_exclude_list (ptr
->exclude_name_list
, file
))
446 /* Check section name against each wildcard spec. If there's no
447 wildcard all sections match. */
448 sec
= ptr
->section_list
;
450 add_matching_section (ptr
, sec
, s
, file
);
453 const char *sname
= bfd_section_name (s
);
454 for (; sec
!= NULL
; sec
= sec
->next
)
456 if (sec
->spec
.name
!= NULL
457 && spec_match (&sec
->spec
, sname
) != 0)
460 /* Don't process sections from files which were excluded. */
461 if (!walk_wild_file_in_exclude_list (sec
->spec
.exclude_name_list
,
463 add_matching_section (ptr
, sec
, s
, file
);
468 /* Return the numerical value of the init_priority attribute from
469 section name NAME. */
472 get_init_priority (const asection
*sec
)
474 const char *name
= bfd_section_name (sec
);
477 /* GCC uses the following section names for the init_priority
478 attribute with numerical values 101 to 65535 inclusive. A
479 lower value means a higher priority.
481 1: .init_array.NNNNN/.fini_array.NNNNN: Where NNNNN is the
482 decimal numerical value of the init_priority attribute.
483 The order of execution in .init_array is forward and
484 .fini_array is backward.
485 2: .ctors.NNNNN/.dtors.NNNNN: Where NNNNN is 65535 minus the
486 decimal numerical value of the init_priority attribute.
487 The order of execution in .ctors is backward and .dtors
490 .init_array.NNNNN sections would normally be placed in an output
491 .init_array section, .fini_array.NNNNN in .fini_array,
492 .ctors.NNNNN in .ctors, and .dtors.NNNNN in .dtors. This means
493 we should sort by increasing number (and could just use
494 SORT_BY_NAME in scripts). However if .ctors.NNNNN sections are
495 being placed in .init_array (which may also contain
496 .init_array.NNNNN sections) or .dtors.NNNNN sections are being
497 placed in .fini_array then we need to extract the init_priority
498 attribute and sort on that. */
499 dot
= strrchr (name
, '.');
500 if (dot
!= NULL
&& ISDIGIT (dot
[1]))
503 unsigned long init_priority
= strtoul (dot
+ 1, &end
, 10);
507 && (strncmp (name
, ".ctors", 6) == 0
508 || strncmp (name
, ".dtors", 6) == 0))
509 init_priority
= 65535 - init_priority
;
510 if (init_priority
<= INT_MAX
)
511 return init_priority
;
517 /* Compare sections ASEC and BSEC according to SORT. */
520 compare_section (sort_type sort
, asection
*asec
, asection
*bsec
)
523 int a_priority
, b_priority
;
530 case by_init_priority
:
531 a_priority
= get_init_priority (asec
);
532 b_priority
= get_init_priority (bsec
);
533 if (a_priority
< 0 || b_priority
< 0)
535 ret
= a_priority
- b_priority
;
541 case by_alignment_name
:
542 ret
= bfd_section_alignment (bsec
) - bfd_section_alignment (asec
);
549 ret
= strcmp (bfd_section_name (asec
), bfd_section_name (bsec
));
552 case by_name_alignment
:
553 ret
= strcmp (bfd_section_name (asec
), bfd_section_name (bsec
));
559 ret
= bfd_section_alignment (bsec
) - bfd_section_alignment (asec
);
566 /* PE puts the sort key in the input statement. */
569 sort_filename (bfd
*abfd
)
571 lang_input_statement_type
*is
= bfd_usrdata (abfd
);
574 return bfd_get_filename (abfd
);
577 /* Handle wildcard sorting. This returns the place in a binary search tree
578 where this FILE:SECTION should be inserted for wild statement WILD where
579 the spec SEC was the matching one. The tree is later linearized. */
581 static lang_section_bst_type
**
582 wild_sort (lang_wild_statement_type
*wild
,
583 struct wildcard_list
*sec
,
584 lang_input_statement_type
*file
,
587 lang_section_bst_type
**tree
;
589 if (!wild
->filenames_sorted
590 && (sec
== NULL
|| sec
->spec
.sorted
== none
591 || sec
->spec
.sorted
== by_none
))
593 /* We might be called even if _this_ spec doesn't need sorting,
594 in which case we simply append at the right end of tree. */
595 return wild
->rightmost
;
601 /* Sorting by filename takes precedence over sorting by section
604 if (wild
->filenames_sorted
)
609 asection
*lsec
= (*tree
)->section
;
611 /* The PE support for the .idata section as generated by
612 dlltool assumes that files will be sorted by the name of
613 the archive and then the name of the file within the
616 fa
= file
->the_bfd
->my_archive
!= NULL
;
618 fn
= sort_filename (file
->the_bfd
->my_archive
);
620 fn
= sort_filename (file
->the_bfd
);
622 la
= lsec
->owner
->my_archive
!= NULL
;
624 ln
= sort_filename (lsec
->owner
->my_archive
);
626 ln
= sort_filename (lsec
->owner
);
628 i
= filename_cmp (fn
, ln
);
630 { tree
= &((*tree
)->right
); continue; }
632 { tree
= &((*tree
)->left
); continue; }
637 fn
= sort_filename (file
->the_bfd
);
639 ln
= sort_filename (lsec
->owner
);
641 i
= filename_cmp (fn
, ln
);
643 { tree
= &((*tree
)->right
); continue; }
645 { tree
= &((*tree
)->left
); continue; }
649 /* Here either the files are not sorted by name, or we are
650 looking at the sections for this file. */
652 /* Find the correct node to append this section. */
653 if (sec
&& sec
->spec
.sorted
!= none
&& sec
->spec
.sorted
!= by_none
654 && compare_section (sec
->spec
.sorted
, section
, (*tree
)->section
) < 0)
655 tree
= &((*tree
)->left
);
657 tree
= &((*tree
)->right
);
663 /* Use wild_sort to build a BST to sort sections. */
666 output_section_callback_sort (lang_wild_statement_type
*ptr
,
667 struct wildcard_list
*sec
,
669 lang_input_statement_type
*file
,
672 lang_section_bst_type
*node
;
673 lang_section_bst_type
**tree
;
674 lang_output_section_statement_type
*os
;
676 os
= (lang_output_section_statement_type
*) output
;
678 if (unique_section_p (section
, os
))
681 node
= (lang_section_bst_type
*) xmalloc (sizeof (lang_section_bst_type
));
684 node
->section
= section
;
685 node
->pattern
= ptr
->section_list
;
687 tree
= wild_sort (ptr
, sec
, file
, section
);
691 if (tree
== ptr
->rightmost
)
692 ptr
->rightmost
= &node
->right
;
696 /* Convert a sorted sections' BST back to list form. */
699 output_section_callback_tree_to_list (lang_wild_statement_type
*ptr
,
700 lang_section_bst_type
*tree
,
704 output_section_callback_tree_to_list (ptr
, tree
->left
, output
);
706 lang_add_section (&ptr
->children
, tree
->section
, tree
->pattern
,
707 ptr
->section_flag_list
,
708 (lang_output_section_statement_type
*) output
);
711 output_section_callback_tree_to_list (ptr
, tree
->right
, output
);
717 /* Sections are matched against wildcard statements via a prefix tree.
718 The prefix tree holds prefixes of all matching patterns (up to the first
719 wildcard character), and the wild statement from which those patterns
720 came. When matching a section name against the tree we're walking through
721 the tree character by character. Each statement we hit is one that
722 potentially matches. This is checked by actually going through the
723 (glob) matching routines.
725 When the section name turns out to actually match we record that section
726 in the wild statements list of matching sections. */
728 /* A prefix can be matched by multiple statement, so we need a list of them. */
729 struct wild_stmt_list
731 lang_wild_statement_type
*stmt
;
732 struct wild_stmt_list
*next
;
735 /* The prefix tree itself. */
738 /* The list of all children (linked via .next). */
739 struct prefixtree
*child
;
740 struct prefixtree
*next
;
741 /* This tree node is responsible for the prefix of parent plus 'c'. */
743 /* The statements that potentially can match this prefix. */
744 struct wild_stmt_list
*stmt
;
747 /* We always have a root node in the prefix tree. It corresponds to the
748 empty prefix. E.g. a glob like "*" would sit in this root. */
749 static struct prefixtree the_root
, *ptroot
= &the_root
;
751 /* Given a prefix tree in *TREE, corresponding to prefix P, find or
752 INSERT the tree node corresponding to prefix P+C. */
754 static struct prefixtree
*
755 get_prefix_tree (struct prefixtree
**tree
, char c
, bool insert
)
757 struct prefixtree
*t
;
758 for (t
= *tree
; t
; t
= t
->next
)
763 t
= (struct prefixtree
*) obstack_alloc (&pt_obstack
, sizeof *t
);
772 /* Add STMT to the set of statements that can be matched by the prefix
773 corresponding to prefix tree T. */
776 pt_add_stmt (struct prefixtree
*t
, lang_wild_statement_type
*stmt
)
778 struct wild_stmt_list
*sl
, **psl
;
779 sl
= (struct wild_stmt_list
*) obstack_alloc (&pt_obstack
, sizeof *sl
);
788 /* Insert STMT into the global prefix tree. */
791 insert_prefix_tree (lang_wild_statement_type
*stmt
)
793 struct wildcard_list
*sec
;
794 struct prefixtree
*t
;
796 if (!stmt
->section_list
)
798 /* If we have no section_list (no wildcards in the wild STMT),
799 then every section name will match, so add this to the root. */
800 pt_add_stmt (ptroot
, stmt
);
804 for (sec
= stmt
->section_list
; sec
; sec
= sec
->next
)
806 const char *name
= sec
->spec
.name
? sec
->spec
.name
: "*";
809 for (; (c
= *name
); name
++)
811 if (c
== '*' || c
== '[' || c
== '?')
813 t
= get_prefix_tree (&t
->child
, c
, true);
815 /* If we hit a glob character, the matching prefix is what we saw
816 until now. If we hit the end of pattern (hence it's no glob) then
817 we can do better: we only need to record a match when a section name
818 completely matches, not merely a prefix, so record the trailing 0
821 t
= get_prefix_tree (&t
->child
, 0, true);
822 pt_add_stmt (t
, stmt
);
826 /* Dump T indented by INDENT spaces. */
829 debug_prefix_tree_rec (struct prefixtree
*t
, int indent
)
831 for (; t
; t
= t
->next
)
833 struct wild_stmt_list
*sl
;
834 printf ("%*s %c", indent
, "", t
->c
);
835 for (sl
= t
->stmt
; sl
; sl
= sl
->next
)
837 struct wildcard_list
*curr
;
838 printf (" %p ", sl
->stmt
);
839 for (curr
= sl
->stmt
->section_list
; curr
; curr
= curr
->next
)
840 printf ("%s ", curr
->spec
.name
? curr
->spec
.name
: "*");
843 debug_prefix_tree_rec (t
->child
, indent
+ 2);
847 /* Dump the global prefix tree. */
850 debug_prefix_tree (void)
852 debug_prefix_tree_rec (ptroot
, 2);
855 /* Like strcspn() but start to look from the end to beginning of
856 S. Returns the length of the suffix of S consisting entirely
857 of characters not in REJECT. */
860 rstrcspn (const char *s
, const char *reject
)
862 size_t len
= strlen (s
), sufflen
= 0;
866 if (strchr (reject
, c
) != 0)
873 /* Analyze the wildcards in wild statement PTR to setup various
874 things for quick matching. */
877 analyze_walk_wild_section_handler (lang_wild_statement_type
*ptr
)
879 struct wildcard_list
*sec
;
882 ptr
->rightmost
= &ptr
->tree
;
884 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
888 sec
->spec
.namelen
= strlen (sec
->spec
.name
);
889 sec
->spec
.prefixlen
= strcspn (sec
->spec
.name
, "?*[");
890 sec
->spec
.suffixlen
= rstrcspn (sec
->spec
.name
+ sec
->spec
.prefixlen
,
894 sec
->spec
.namelen
= sec
->spec
.prefixlen
= sec
->spec
.suffixlen
= 0;
897 insert_prefix_tree (ptr
);
900 /* Match all sections from FILE against the global prefix tree,
901 and record them into each wild statement that has a match. */
904 resolve_wild_sections (lang_input_statement_type
*file
)
908 if (file
->flags
.just_syms
)
911 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
913 const char *sname
= bfd_section_name (s
);
915 struct prefixtree
*t
= ptroot
;
916 //printf (" YYY consider %s of %s\n", sname, file->the_bfd->filename);
921 struct wild_stmt_list
*sl
;
922 for (sl
= t
->stmt
; sl
; sl
= sl
->next
)
924 walk_wild_section_match (sl
->stmt
, file
, s
);
925 //printf (" ZZZ maybe place into %p\n", sl->stmt);
931 t
= get_prefix_tree (&t
->child
, c
, false);
937 /* Match all sections from all input files against the global prefix tree. */
942 LANG_FOR_EACH_INPUT_STATEMENT (f
)
944 //printf("XXX %s\n", f->filename);
945 if (f
->the_bfd
== NULL
946 || !bfd_check_format (f
->the_bfd
, bfd_archive
))
947 resolve_wild_sections (f
);
952 /* This is an archive file. We must map each member of the
953 archive separately. */
954 member
= bfd_openr_next_archived_file (f
->the_bfd
, NULL
);
955 while (member
!= NULL
)
957 /* When lookup_name is called, it will call the add_symbols
958 entry point for the archive. For each element of the
959 archive which is included, BFD will call ldlang_add_file,
960 which will set the usrdata field of the member to the
961 lang_input_statement. */
962 if (bfd_usrdata (member
) != NULL
)
963 resolve_wild_sections (bfd_usrdata (member
));
965 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
971 /* For each input section that matches wild statement S calls
972 CALLBACK with DATA. */
975 walk_wild (lang_wild_statement_type
*s
, callback_t callback
, void *data
)
977 lang_statement_union_type
*l
;
979 for (l
= s
->matching_sections
.head
; l
; l
= l
->header
.next
)
981 (*callback
) (s
, l
->input_matcher
.pattern
, l
->input_matcher
.section
,
982 l
->input_matcher
.input_stmt
, data
);
986 /* lang_for_each_statement walks the parse tree and calls the provided
987 function for each node, except those inside output section statements
988 with constraint set to -1. */
991 lang_for_each_statement_worker (void (*func
) (lang_statement_union_type
*),
992 lang_statement_union_type
*s
)
994 for (; s
!= NULL
; s
= s
->header
.next
)
998 switch (s
->header
.type
)
1000 case lang_constructors_statement_enum
:
1001 lang_for_each_statement_worker (func
, constructor_list
.head
);
1003 case lang_output_section_statement_enum
:
1004 if (s
->output_section_statement
.constraint
!= -1)
1005 lang_for_each_statement_worker
1006 (func
, s
->output_section_statement
.children
.head
);
1008 case lang_wild_statement_enum
:
1009 lang_for_each_statement_worker (func
,
1010 s
->wild_statement
.children
.head
);
1012 case lang_group_statement_enum
:
1013 lang_for_each_statement_worker (func
,
1014 s
->group_statement
.children
.head
);
1016 case lang_data_statement_enum
:
1017 case lang_reloc_statement_enum
:
1018 case lang_object_symbols_statement_enum
:
1019 case lang_output_statement_enum
:
1020 case lang_target_statement_enum
:
1021 case lang_input_section_enum
:
1022 case lang_input_statement_enum
:
1023 case lang_assignment_statement_enum
:
1024 case lang_padding_statement_enum
:
1025 case lang_address_statement_enum
:
1026 case lang_fill_statement_enum
:
1027 case lang_insert_statement_enum
:
1037 lang_for_each_statement (void (*func
) (lang_statement_union_type
*))
1039 lang_for_each_statement_worker (func
, statement_list
.head
);
1042 /*----------------------------------------------------------------------*/
1045 lang_list_init (lang_statement_list_type
*list
)
1048 list
->tail
= &list
->head
;
1052 lang_statement_append (lang_statement_list_type
*list
,
1056 *(list
->tail
) = element
;
1061 push_stat_ptr (lang_statement_list_type
*new_ptr
)
1063 if (stat_save_ptr
>= stat_save
+ sizeof (stat_save
) / sizeof (stat_save
[0]))
1065 *stat_save_ptr
++ = stat_ptr
;
1072 if (stat_save_ptr
<= stat_save
)
1074 stat_ptr
= *--stat_save_ptr
;
1077 /* Build a new statement node for the parse tree. */
1079 static lang_statement_union_type
*
1080 new_statement (enum statement_enum type
,
1082 lang_statement_list_type
*list
)
1084 lang_statement_union_type
*new_stmt
;
1086 new_stmt
= stat_alloc (size
);
1087 new_stmt
->header
.type
= type
;
1088 new_stmt
->header
.next
= NULL
;
1089 lang_statement_append (list
, new_stmt
, &new_stmt
->header
.next
);
1093 /* Build a new input file node for the language. There are several
1094 ways in which we treat an input file, eg, we only look at symbols,
1095 or prefix it with a -l etc.
1097 We can be supplied with requests for input files more than once;
1098 they may, for example be split over several lines like foo.o(.text)
1099 foo.o(.data) etc, so when asked for a file we check that we haven't
1100 got it already so we don't duplicate the bfd. */
1102 static lang_input_statement_type
*
1103 new_afile (const char *name
,
1104 lang_input_file_enum_type file_type
,
1106 const char *from_filename
)
1108 lang_input_statement_type
*p
;
1110 lang_has_input_file
= true;
1112 p
= new_stat (lang_input_statement
, stat_ptr
);
1113 memset (&p
->the_bfd
, 0,
1114 sizeof (*p
) - offsetof (lang_input_statement_type
, the_bfd
));
1115 p
->extra_search_path
= NULL
;
1117 p
->flags
.dynamic
= input_flags
.dynamic
;
1118 p
->flags
.add_DT_NEEDED_for_dynamic
= input_flags
.add_DT_NEEDED_for_dynamic
;
1119 p
->flags
.add_DT_NEEDED_for_regular
= input_flags
.add_DT_NEEDED_for_regular
;
1120 p
->flags
.whole_archive
= input_flags
.whole_archive
;
1121 p
->flags
.sysrooted
= input_flags
.sysrooted
;
1126 case lang_input_file_is_symbols_only_enum
:
1128 p
->local_sym_name
= name
;
1129 p
->flags
.real
= true;
1130 p
->flags
.just_syms
= true;
1132 case lang_input_file_is_fake_enum
:
1134 p
->local_sym_name
= name
;
1136 case lang_input_file_is_l_enum
:
1137 if (name
[0] == ':' && name
[1] != '\0')
1139 p
->filename
= name
+ 1;
1140 p
->flags
.full_name_provided
= true;
1144 p
->local_sym_name
= concat ("-l", name
, (const char *) NULL
);
1145 p
->flags
.maybe_archive
= true;
1146 p
->flags
.real
= true;
1147 p
->flags
.search_dirs
= true;
1149 case lang_input_file_is_marker_enum
:
1151 p
->local_sym_name
= name
;
1152 p
->flags
.search_dirs
= true;
1154 case lang_input_file_is_search_file_enum
:
1156 p
->local_sym_name
= name
;
1157 /* If name is a relative path, search the directory of the current linker
1159 if (from_filename
&& !IS_ABSOLUTE_PATH (name
))
1160 p
->extra_search_path
= ldirname (from_filename
);
1161 p
->flags
.real
= true;
1162 p
->flags
.search_dirs
= true;
1164 case lang_input_file_is_file_enum
:
1166 p
->local_sym_name
= name
;
1167 p
->flags
.real
= true;
1173 lang_statement_append (&input_file_chain
, p
, &p
->next_real_file
);
1177 lang_input_statement_type
*
1178 lang_add_input_file (const char *name
,
1179 lang_input_file_enum_type file_type
,
1183 && (*name
== '=' || startswith (name
, "$SYSROOT")))
1185 lang_input_statement_type
*ret
;
1186 char *sysrooted_name
1187 = concat (ld_sysroot
,
1188 name
+ (*name
== '=' ? 1 : strlen ("$SYSROOT")),
1189 (const char *) NULL
);
1191 /* We've now forcibly prepended the sysroot, making the input
1192 file independent of the context. Therefore, temporarily
1193 force a non-sysrooted context for this statement, so it won't
1194 get the sysroot prepended again when opened. (N.B. if it's a
1195 script, any child nodes with input files starting with "/"
1196 will be handled as "sysrooted" as they'll be found to be
1197 within the sysroot subdirectory.) */
1198 unsigned int outer_sysrooted
= input_flags
.sysrooted
;
1199 input_flags
.sysrooted
= 0;
1200 ret
= new_afile (sysrooted_name
, file_type
, target
, NULL
);
1201 input_flags
.sysrooted
= outer_sysrooted
;
1205 return new_afile (name
, file_type
, target
, current_input_file
);
1208 struct out_section_hash_entry
1210 struct bfd_hash_entry root
;
1211 lang_statement_union_type s
;
1214 /* The hash table. */
1216 static struct bfd_hash_table output_section_statement_table
;
1218 /* Support routines for the hash table used by lang_output_section_find,
1219 initialize the table, fill in an entry and remove the table. */
1221 static struct bfd_hash_entry
*
1222 output_section_statement_newfunc (struct bfd_hash_entry
*entry
,
1223 struct bfd_hash_table
*table
,
1226 lang_output_section_statement_type
**nextp
;
1227 struct out_section_hash_entry
*ret
;
1231 entry
= (struct bfd_hash_entry
*) bfd_hash_allocate (table
,
1237 entry
= bfd_hash_newfunc (entry
, table
, string
);
1241 ret
= (struct out_section_hash_entry
*) entry
;
1242 memset (&ret
->s
, 0, sizeof (ret
->s
));
1243 ret
->s
.header
.type
= lang_output_section_statement_enum
;
1244 ret
->s
.output_section_statement
.subsection_alignment
= NULL
;
1245 ret
->s
.output_section_statement
.section_alignment
= NULL
;
1246 ret
->s
.output_section_statement
.block_value
= 1;
1247 lang_list_init (&ret
->s
.output_section_statement
.children
);
1248 lang_statement_append (stat_ptr
, &ret
->s
, &ret
->s
.header
.next
);
1250 /* For every output section statement added to the list, except the
1251 first one, lang_os_list.tail points to the "next"
1252 field of the last element of the list. */
1253 if (lang_os_list
.head
!= NULL
)
1254 ret
->s
.output_section_statement
.prev
1255 = ((lang_output_section_statement_type
*)
1256 ((char *) lang_os_list
.tail
1257 - offsetof (lang_output_section_statement_type
, next
)));
1259 /* GCC's strict aliasing rules prevent us from just casting the
1260 address, so we store the pointer in a variable and cast that
1262 nextp
= &ret
->s
.output_section_statement
.next
;
1263 lang_statement_append (&lang_os_list
, &ret
->s
, nextp
);
1268 output_section_statement_table_init (void)
1270 if (!bfd_hash_table_init_n (&output_section_statement_table
,
1271 output_section_statement_newfunc
,
1272 sizeof (struct out_section_hash_entry
),
1274 einfo (_("%F%P: can not create hash table: %E\n"));
1278 output_section_statement_table_free (void)
1280 bfd_hash_table_free (&output_section_statement_table
);
1283 /* Build enough state so that the parser can build its tree. */
1288 obstack_begin (&stat_obstack
, 1000);
1289 obstack_init (&pt_obstack
);
1291 stat_ptr
= &statement_list
;
1293 output_section_statement_table_init ();
1295 lang_list_init (stat_ptr
);
1297 lang_list_init (&input_file_chain
);
1298 lang_list_init (&lang_os_list
);
1299 lang_list_init (&file_chain
);
1300 first_file
= lang_add_input_file (NULL
, lang_input_file_is_marker_enum
,
1302 abs_output_section
=
1303 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
, 0, 1);
1305 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
1307 asneeded_list_head
= NULL
;
1308 asneeded_list_tail
= &asneeded_list_head
;
1314 output_section_statement_table_free ();
1317 /*----------------------------------------------------------------------
1318 A region is an area of memory declared with the
1319 MEMORY { name:org=exp, len=exp ... }
1322 We maintain a list of all the regions here.
1324 If no regions are specified in the script, then the default is used
1325 which is created when looked up to be the entire data space.
1327 If create is true we are creating a region inside a MEMORY block.
1328 In this case it is probably an error to create a region that has
1329 already been created. If we are not inside a MEMORY block it is
1330 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
1331 and so we issue a warning.
1333 Each region has at least one name. The first name is either
1334 DEFAULT_MEMORY_REGION or the name given in the MEMORY block. You can add
1335 alias names to an existing region within a script with
1336 REGION_ALIAS (alias, region_name). Each name corresponds to at most one
1339 static lang_memory_region_type
*lang_memory_region_list
;
1340 static lang_memory_region_type
**lang_memory_region_list_tail
1341 = &lang_memory_region_list
;
1343 lang_memory_region_type
*
1344 lang_memory_region_lookup (const char *const name
, bool create
)
1346 lang_memory_region_name
*n
;
1347 lang_memory_region_type
*r
;
1348 lang_memory_region_type
*new_region
;
1350 /* NAME is NULL for LMA memspecs if no region was specified. */
1354 for (r
= lang_memory_region_list
; r
!= NULL
; r
= r
->next
)
1355 for (n
= &r
->name_list
; n
!= NULL
; n
= n
->next
)
1356 if (strcmp (n
->name
, name
) == 0)
1359 einfo (_("%P:%pS: warning: redeclaration of memory region `%s'\n"),
1364 if (!create
&& strcmp (name
, DEFAULT_MEMORY_REGION
))
1365 einfo (_("%P:%pS: warning: memory region `%s' not declared\n"),
1368 new_region
= stat_alloc (sizeof (lang_memory_region_type
));
1370 new_region
->name_list
.name
= xstrdup (name
);
1371 new_region
->name_list
.next
= NULL
;
1372 new_region
->next
= NULL
;
1373 new_region
->origin_exp
= NULL
;
1374 new_region
->origin
= 0;
1375 new_region
->length_exp
= NULL
;
1376 new_region
->length
= ~(bfd_size_type
) 0;
1377 new_region
->current
= 0;
1378 new_region
->last_os
= NULL
;
1379 new_region
->flags
= 0;
1380 new_region
->not_flags
= 0;
1381 new_region
->had_full_message
= false;
1383 *lang_memory_region_list_tail
= new_region
;
1384 lang_memory_region_list_tail
= &new_region
->next
;
1390 lang_memory_region_alias (const char *alias
, const char *region_name
)
1392 lang_memory_region_name
*n
;
1393 lang_memory_region_type
*r
;
1394 lang_memory_region_type
*region
;
1396 /* The default region must be unique. This ensures that it is not necessary
1397 to iterate through the name list if someone wants the check if a region is
1398 the default memory region. */
1399 if (strcmp (region_name
, DEFAULT_MEMORY_REGION
) == 0
1400 || strcmp (alias
, DEFAULT_MEMORY_REGION
) == 0)
1401 einfo (_("%F%P:%pS: error: alias for default memory region\n"), NULL
);
1403 /* Look for the target region and check if the alias is not already
1406 for (r
= lang_memory_region_list
; r
!= NULL
; r
= r
->next
)
1407 for (n
= &r
->name_list
; n
!= NULL
; n
= n
->next
)
1409 if (region
== NULL
&& strcmp (n
->name
, region_name
) == 0)
1411 if (strcmp (n
->name
, alias
) == 0)
1412 einfo (_("%F%P:%pS: error: redefinition of memory region "
1417 /* Check if the target region exists. */
1419 einfo (_("%F%P:%pS: error: memory region `%s' "
1420 "for alias `%s' does not exist\n"),
1421 NULL
, region_name
, alias
);
1423 /* Add alias to region name list. */
1424 n
= stat_alloc (sizeof (lang_memory_region_name
));
1425 n
->name
= xstrdup (alias
);
1426 n
->next
= region
->name_list
.next
;
1427 region
->name_list
.next
= n
;
1430 static lang_memory_region_type
*
1431 lang_memory_default (asection
*section
)
1433 lang_memory_region_type
*p
;
1435 flagword sec_flags
= section
->flags
;
1437 /* Override SEC_DATA to mean a writable section. */
1438 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
1439 sec_flags
|= SEC_DATA
;
1441 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
1443 if ((p
->flags
& sec_flags
) != 0
1444 && (p
->not_flags
& sec_flags
) == 0)
1449 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, false);
1452 /* Get the output section statement directly from the userdata. */
1454 lang_output_section_statement_type
*
1455 lang_output_section_get (const asection
*output_section
)
1457 return bfd_section_userdata (output_section
);
1460 /* Find or create an output_section_statement with the given NAME.
1461 If CONSTRAINT is non-zero match one with that constraint, otherwise
1462 match any non-negative constraint. If CREATE is 0 return NULL when
1463 no match exists. If CREATE is 1, create an output_section_statement
1464 when no match exists or if CONSTRAINT is SPECIAL. If CREATE is 2,
1465 always make a new output_section_statement. */
1467 lang_output_section_statement_type
*
1468 lang_output_section_statement_lookup (const char *name
,
1472 struct out_section_hash_entry
*entry
;
1474 entry
= ((struct out_section_hash_entry
*)
1475 bfd_hash_lookup (&output_section_statement_table
, name
,
1476 create
!= 0, false));
1480 einfo (_("%F%P: failed creating section `%s': %E\n"), name
);
1484 if (entry
->s
.output_section_statement
.name
!= NULL
)
1486 /* We have a section of this name, but it might not have the correct
1488 struct out_section_hash_entry
*last_ent
;
1490 name
= entry
->s
.output_section_statement
.name
;
1494 && !(create
&& constraint
== SPECIAL
)
1495 && (constraint
== entry
->s
.output_section_statement
.constraint
1497 && entry
->s
.output_section_statement
.constraint
>= 0)))
1498 return &entry
->s
.output_section_statement
;
1500 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1502 while (entry
!= NULL
1503 && name
== entry
->s
.output_section_statement
.name
);
1509 = ((struct out_section_hash_entry
*)
1510 output_section_statement_newfunc (NULL
,
1511 &output_section_statement_table
,
1515 einfo (_("%F%P: failed creating section `%s': %E\n"), name
);
1518 entry
->root
= last_ent
->root
;
1519 last_ent
->root
.next
= &entry
->root
;
1522 entry
->s
.output_section_statement
.name
= name
;
1523 entry
->s
.output_section_statement
.constraint
= constraint
;
1524 entry
->s
.output_section_statement
.dup_output
= (create
== 2
1525 || constraint
== SPECIAL
);
1526 return &entry
->s
.output_section_statement
;
1529 /* Find the next output_section_statement with the same name as OS.
1530 If CONSTRAINT is non-zero, find one with that constraint otherwise
1531 match any non-negative constraint. */
1533 lang_output_section_statement_type
*
1534 next_matching_output_section_statement (lang_output_section_statement_type
*os
,
1537 /* All output_section_statements are actually part of a
1538 struct out_section_hash_entry. */
1539 struct out_section_hash_entry
*entry
= (struct out_section_hash_entry
*)
1541 - offsetof (struct out_section_hash_entry
, s
.output_section_statement
));
1542 const char *name
= os
->name
;
1544 ASSERT (name
== entry
->root
.string
);
1547 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1549 || name
!= entry
->s
.output_section_statement
.name
)
1552 while (constraint
!= entry
->s
.output_section_statement
.constraint
1554 || entry
->s
.output_section_statement
.constraint
< 0));
1556 return &entry
->s
.output_section_statement
;
1559 /* A variant of lang_output_section_find used by place_orphan.
1560 Returns the output statement that should precede a new output
1561 statement for SEC. If an exact match is found on certain flags,
1564 lang_output_section_statement_type
*
1565 lang_output_section_find_by_flags (const asection
*sec
,
1567 lang_output_section_statement_type
**exact
,
1568 lang_match_sec_type_func match_type
)
1570 lang_output_section_statement_type
*first
, *look
, *found
;
1571 flagword look_flags
, differ
;
1573 /* We know the first statement on this list is *ABS*. May as well
1575 first
= (void *) lang_os_list
.head
;
1576 first
= first
->next
;
1578 /* First try for an exact match. */
1580 for (look
= first
; look
; look
= look
->next
)
1582 look_flags
= look
->flags
;
1583 if (look
->bfd_section
!= NULL
)
1585 look_flags
= look
->bfd_section
->flags
;
1586 if (match_type
&& !match_type (link_info
.output_bfd
,
1591 differ
= look_flags
^ sec_flags
;
1592 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
1593 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1603 if ((sec_flags
& SEC_CODE
) != 0
1604 && (sec_flags
& SEC_ALLOC
) != 0)
1606 /* Try for a rw code section. */
1607 for (look
= first
; look
; look
= look
->next
)
1609 look_flags
= look
->flags
;
1610 if (look
->bfd_section
!= NULL
)
1612 look_flags
= look
->bfd_section
->flags
;
1613 if (match_type
&& !match_type (link_info
.output_bfd
,
1618 differ
= look_flags
^ sec_flags
;
1619 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1620 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1624 else if ((sec_flags
& SEC_READONLY
) != 0
1625 && (sec_flags
& SEC_ALLOC
) != 0)
1627 /* .rodata can go after .text, .sdata2 after .rodata. */
1628 for (look
= first
; look
; look
= look
->next
)
1630 look_flags
= look
->flags
;
1631 if (look
->bfd_section
!= NULL
)
1633 look_flags
= look
->bfd_section
->flags
;
1634 if (match_type
&& !match_type (link_info
.output_bfd
,
1639 differ
= look_flags
^ sec_flags
;
1640 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1641 | SEC_READONLY
| SEC_SMALL_DATA
))
1642 || (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1644 && !(look_flags
& SEC_SMALL_DATA
)))
1648 else if ((sec_flags
& SEC_THREAD_LOCAL
) != 0
1649 && (sec_flags
& SEC_ALLOC
) != 0)
1651 /* .tdata can go after .data, .tbss after .tdata. Treat .tbss
1652 as if it were a loaded section, and don't use match_type. */
1653 bool seen_thread_local
= false;
1656 for (look
= first
; look
; look
= look
->next
)
1658 look_flags
= look
->flags
;
1659 if (look
->bfd_section
!= NULL
)
1660 look_flags
= look
->bfd_section
->flags
;
1662 differ
= look_flags
^ (sec_flags
| SEC_LOAD
| SEC_HAS_CONTENTS
);
1663 if (!(differ
& (SEC_THREAD_LOCAL
| SEC_ALLOC
)))
1665 /* .tdata and .tbss must be adjacent and in that order. */
1666 if (!(look_flags
& SEC_LOAD
)
1667 && (sec_flags
& SEC_LOAD
))
1668 /* ..so if we're at a .tbss section and we're placing
1669 a .tdata section stop looking and return the
1670 previous section. */
1673 seen_thread_local
= true;
1675 else if (seen_thread_local
)
1677 else if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
)))
1681 else if ((sec_flags
& SEC_SMALL_DATA
) != 0
1682 && (sec_flags
& SEC_ALLOC
) != 0)
1684 /* .sdata goes after .data, .sbss after .sdata. */
1685 for (look
= first
; look
; look
= look
->next
)
1687 look_flags
= look
->flags
;
1688 if (look
->bfd_section
!= NULL
)
1690 look_flags
= look
->bfd_section
->flags
;
1691 if (match_type
&& !match_type (link_info
.output_bfd
,
1696 differ
= look_flags
^ sec_flags
;
1697 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1698 | SEC_THREAD_LOCAL
))
1699 || ((look_flags
& SEC_SMALL_DATA
)
1700 && !(sec_flags
& SEC_HAS_CONTENTS
)))
1704 else if ((sec_flags
& SEC_HAS_CONTENTS
) != 0
1705 && (sec_flags
& SEC_ALLOC
) != 0)
1707 /* .data goes after .rodata. */
1708 for (look
= first
; look
; look
= look
->next
)
1710 look_flags
= look
->flags
;
1711 if (look
->bfd_section
!= NULL
)
1713 look_flags
= look
->bfd_section
->flags
;
1714 if (match_type
&& !match_type (link_info
.output_bfd
,
1719 differ
= look_flags
^ sec_flags
;
1720 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1721 | SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1725 else if ((sec_flags
& SEC_ALLOC
) != 0)
1727 /* .bss goes after any other alloc section. */
1728 for (look
= first
; look
; look
= look
->next
)
1730 look_flags
= look
->flags
;
1731 if (look
->bfd_section
!= NULL
)
1733 look_flags
= look
->bfd_section
->flags
;
1734 if (match_type
&& !match_type (link_info
.output_bfd
,
1739 differ
= look_flags
^ sec_flags
;
1740 if (!(differ
& SEC_ALLOC
))
1746 /* non-alloc go last. */
1747 for (look
= first
; look
; look
= look
->next
)
1749 look_flags
= look
->flags
;
1750 if (look
->bfd_section
!= NULL
)
1751 look_flags
= look
->bfd_section
->flags
;
1752 differ
= look_flags
^ sec_flags
;
1753 if (!(differ
& SEC_DEBUGGING
))
1759 if (found
|| !match_type
)
1762 return lang_output_section_find_by_flags (sec
, sec_flags
, NULL
, NULL
);
1765 /* Find the last output section before given output statement.
1766 Used by place_orphan. */
1769 output_prev_sec_find (lang_output_section_statement_type
*os
)
1771 lang_output_section_statement_type
*lookup
;
1773 for (lookup
= os
->prev
; lookup
!= NULL
; lookup
= lookup
->prev
)
1775 if (lookup
->constraint
< 0)
1778 if (lookup
->bfd_section
!= NULL
)
1779 return lookup
->bfd_section
;
1785 /* Look for a suitable place for a new output section statement. The
1786 idea is to skip over anything that might be inside a SECTIONS {}
1787 statement in a script, before we find another output section
1788 statement. Assignments to "dot" before an output section statement
1789 are assumed to belong to it, except in two cases; The first
1790 assignment to dot, and assignments before non-alloc sections.
1791 Otherwise we might put an orphan before . = . + SIZEOF_HEADERS or
1792 similar assignments that set the initial address, or we might
1793 insert non-alloc note sections among assignments setting end of
1796 static lang_statement_union_type
**
1797 insert_os_after (lang_statement_union_type
*after
)
1799 lang_statement_union_type
**where
;
1800 lang_statement_union_type
**assign
= NULL
;
1803 ignore_first
= after
== lang_os_list
.head
;
1805 for (where
= &after
->header
.next
;
1807 where
= &(*where
)->header
.next
)
1809 switch ((*where
)->header
.type
)
1811 case lang_assignment_statement_enum
:
1814 lang_assignment_statement_type
*ass
;
1816 ass
= &(*where
)->assignment_statement
;
1817 if (ass
->exp
->type
.node_class
!= etree_assert
1818 && ass
->exp
->assign
.dst
[0] == '.'
1819 && ass
->exp
->assign
.dst
[1] == 0)
1823 ignore_first
= false;
1827 case lang_wild_statement_enum
:
1828 case lang_input_section_enum
:
1829 case lang_object_symbols_statement_enum
:
1830 case lang_fill_statement_enum
:
1831 case lang_data_statement_enum
:
1832 case lang_reloc_statement_enum
:
1833 case lang_padding_statement_enum
:
1834 case lang_constructors_statement_enum
:
1836 ignore_first
= false;
1838 case lang_output_section_statement_enum
:
1841 asection
*s
= (*where
)->output_section_statement
.bfd_section
;
1844 || s
->map_head
.s
== NULL
1845 || (s
->flags
& SEC_ALLOC
) != 0)
1849 case lang_input_statement_enum
:
1850 case lang_address_statement_enum
:
1851 case lang_target_statement_enum
:
1852 case lang_output_statement_enum
:
1853 case lang_group_statement_enum
:
1854 case lang_insert_statement_enum
:
1856 case lang_input_matcher_enum
:
1865 lang_output_section_statement_type
*
1866 lang_insert_orphan (asection
*s
,
1867 const char *secname
,
1869 lang_output_section_statement_type
*after
,
1870 struct orphan_save
*place
,
1871 etree_type
*address
,
1872 lang_statement_list_type
*add_child
)
1874 lang_statement_list_type add
;
1875 lang_output_section_statement_type
*os
;
1876 lang_output_section_statement_type
**os_tail
;
1878 /* If we have found an appropriate place for the output section
1879 statements for this orphan, add them to our own private list,
1880 inserting them later into the global statement list. */
1883 lang_list_init (&add
);
1884 push_stat_ptr (&add
);
1887 if (bfd_link_relocatable (&link_info
)
1888 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0)
1889 address
= exp_intop (0);
1891 os_tail
= (lang_output_section_statement_type
**) lang_os_list
.tail
;
1892 os
= lang_enter_output_section_statement (
1893 secname
, address
, normal_section
, 0, NULL
, NULL
, NULL
, constraint
, 0);
1895 if (add_child
== NULL
)
1896 add_child
= &os
->children
;
1897 lang_add_section (add_child
, s
, NULL
, NULL
, os
);
1899 if (after
&& (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) != 0)
1901 const char *region
= (after
->region
1902 ? after
->region
->name_list
.name
1903 : DEFAULT_MEMORY_REGION
);
1904 const char *lma_region
= (after
->lma_region
1905 ? after
->lma_region
->name_list
.name
1907 lang_leave_output_section_statement (NULL
, region
, after
->phdrs
,
1911 lang_leave_output_section_statement (NULL
, DEFAULT_MEMORY_REGION
, NULL
,
1914 /* Restore the global list pointer. */
1918 if (after
!= NULL
&& os
->bfd_section
!= NULL
)
1920 asection
*snew
, *as
;
1921 bool place_after
= place
->stmt
== NULL
;
1922 bool insert_after
= true;
1924 snew
= os
->bfd_section
;
1926 /* Shuffle the bfd section list to make the output file look
1927 neater. This is really only cosmetic. */
1928 if (place
->section
== NULL
1929 && after
!= (void *) lang_os_list
.head
)
1931 asection
*bfd_section
= after
->bfd_section
;
1933 /* If the output statement hasn't been used to place any input
1934 sections (and thus doesn't have an output bfd_section),
1935 look for the closest prior output statement having an
1937 if (bfd_section
== NULL
)
1938 bfd_section
= output_prev_sec_find (after
);
1940 if (bfd_section
!= NULL
1941 && bfd_section
->owner
!= NULL
1942 && bfd_section
!= snew
)
1943 place
->section
= &bfd_section
->next
;
1946 if (place
->section
== NULL
)
1947 place
->section
= &link_info
.output_bfd
->sections
;
1949 as
= *place
->section
;
1953 /* Put the section at the end of the list. */
1955 /* Unlink the section. */
1956 bfd_section_list_remove (link_info
.output_bfd
, snew
);
1958 /* Now tack it back on in the right place. */
1959 bfd_section_list_append (link_info
.output_bfd
, snew
);
1961 else if ((bfd_get_flavour (link_info
.output_bfd
)
1962 == bfd_target_elf_flavour
)
1963 && (bfd_get_flavour (s
->owner
)
1964 == bfd_target_elf_flavour
)
1965 && ((elf_section_type (s
) == SHT_NOTE
1966 && (s
->flags
& SEC_LOAD
) != 0)
1967 || (elf_section_type (as
) == SHT_NOTE
1968 && (as
->flags
& SEC_LOAD
) != 0)))
1970 /* Make sure that output note sections are grouped and sorted
1971 by alignments when inserting a note section or insert a
1972 section after a note section, */
1974 /* A specific section after which the output note section
1975 should be placed. */
1976 asection
*after_sec
;
1977 /* True if we need to insert the orphan section after a
1978 specific section to maintain output note section order. */
1979 bool after_sec_note
= false;
1981 static asection
*first_orphan_note
= NULL
;
1983 /* Group and sort output note section by alignments in
1986 if (elf_section_type (s
) == SHT_NOTE
1987 && (s
->flags
& SEC_LOAD
) != 0)
1989 /* Search from the beginning for the last output note
1990 section with equal or larger alignments. NB: Don't
1991 place orphan note section after non-note sections. */
1993 first_orphan_note
= NULL
;
1994 for (sec
= link_info
.output_bfd
->sections
;
1996 && !bfd_is_abs_section (sec
));
1999 && elf_section_type (sec
) == SHT_NOTE
2000 && (sec
->flags
& SEC_LOAD
) != 0)
2002 if (!first_orphan_note
)
2003 first_orphan_note
= sec
;
2004 if (sec
->alignment_power
>= s
->alignment_power
)
2007 else if (first_orphan_note
)
2009 /* Stop if there is non-note section after the first
2010 orphan note section. */
2014 /* If this will be the first orphan note section, it can
2015 be placed at the default location. */
2016 after_sec_note
= first_orphan_note
!= NULL
;
2017 if (after_sec
== NULL
&& after_sec_note
)
2019 /* If all output note sections have smaller
2020 alignments, place the section before all
2021 output orphan note sections. */
2022 after_sec
= first_orphan_note
;
2023 insert_after
= false;
2026 else if (first_orphan_note
)
2028 /* Don't place non-note sections in the middle of orphan
2030 after_sec_note
= true;
2032 for (sec
= as
->next
;
2034 && !bfd_is_abs_section (sec
));
2036 if (elf_section_type (sec
) == SHT_NOTE
2037 && (sec
->flags
& SEC_LOAD
) != 0)
2045 /* Search forward to insert OS after AFTER_SEC output
2047 lang_output_section_statement_type
*stmt
, *next
;
2049 for (stmt
= after
; stmt
!= NULL
; stmt
= next
)
2054 if (stmt
->bfd_section
== after_sec
)
2064 /* If INSERT_AFTER is FALSE, place OS before
2065 AFTER_SEC output statement. */
2066 if (next
&& next
->bfd_section
== after_sec
)
2076 /* Search backward to insert OS after AFTER_SEC output
2079 for (stmt
= after
; stmt
!= NULL
; stmt
= stmt
->prev
)
2083 if (stmt
->bfd_section
== after_sec
)
2092 /* If INSERT_AFTER is FALSE, place OS before
2093 AFTER_SEC output statement. */
2094 if (stmt
->next
->bfd_section
== after_sec
)
2104 if (after_sec
== NULL
2105 || (insert_after
&& after_sec
->next
!= snew
)
2106 || (!insert_after
&& after_sec
->prev
!= snew
))
2108 /* Unlink the section. */
2109 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2111 /* Place SNEW after AFTER_SEC. If AFTER_SEC is NULL,
2116 bfd_section_list_insert_after (link_info
.output_bfd
,
2119 bfd_section_list_insert_before (link_info
.output_bfd
,
2123 bfd_section_list_prepend (link_info
.output_bfd
, snew
);
2126 else if (as
!= snew
&& as
->prev
!= snew
)
2128 /* Unlink the section. */
2129 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2131 /* Now tack it back on in the right place. */
2132 bfd_section_list_insert_before (link_info
.output_bfd
,
2136 else if (as
!= snew
&& as
->prev
!= snew
)
2138 /* Unlink the section. */
2139 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2141 /* Now tack it back on in the right place. */
2142 bfd_section_list_insert_before (link_info
.output_bfd
, as
, snew
);
2145 /* Save the end of this list. Further ophans of this type will
2146 follow the one we've just added. */
2147 place
->section
= &snew
->next
;
2149 /* The following is non-cosmetic. We try to put the output
2150 statements in some sort of reasonable order here, because they
2151 determine the final load addresses of the orphan sections.
2152 In addition, placing output statements in the wrong order may
2153 require extra segments. For instance, given a typical
2154 situation of all read-only sections placed in one segment and
2155 following that a segment containing all the read-write
2156 sections, we wouldn't want to place an orphan read/write
2157 section before or amongst the read-only ones. */
2158 if (add
.head
!= NULL
)
2160 lang_output_section_statement_type
*newly_added_os
;
2162 /* Place OS after AFTER if AFTER_NOTE is TRUE. */
2165 lang_statement_union_type
**where
;
2167 where
= insert_os_after ((lang_statement_union_type
*) after
);
2171 place
->os_tail
= &after
->next
;
2175 /* Put it after the last orphan statement we added. */
2176 *add
.tail
= *place
->stmt
;
2177 *place
->stmt
= add
.head
;
2180 /* Fix the global list pointer if we happened to tack our
2181 new list at the tail. */
2182 if (*stat_ptr
->tail
== add
.head
)
2183 stat_ptr
->tail
= add
.tail
;
2185 /* Save the end of this list. */
2186 place
->stmt
= add
.tail
;
2188 /* Do the same for the list of output section statements. */
2189 newly_added_os
= *os_tail
;
2191 newly_added_os
->prev
= (lang_output_section_statement_type
*)
2192 ((char *) place
->os_tail
2193 - offsetof (lang_output_section_statement_type
, next
));
2194 newly_added_os
->next
= *place
->os_tail
;
2195 if (newly_added_os
->next
!= NULL
)
2196 newly_added_os
->next
->prev
= newly_added_os
;
2197 *place
->os_tail
= newly_added_os
;
2198 place
->os_tail
= &newly_added_os
->next
;
2200 /* Fixing the global list pointer here is a little different.
2201 We added to the list in lang_enter_output_section_statement,
2202 trimmed off the new output_section_statment above when
2203 assigning *os_tail = NULL, but possibly added it back in
2204 the same place when assigning *place->os_tail. */
2205 if (*os_tail
== NULL
)
2206 lang_os_list
.tail
= (lang_statement_union_type
**) os_tail
;
2213 lang_print_asneeded (void)
2215 struct asneeded_minfo
*m
;
2217 if (asneeded_list_head
== NULL
)
2220 minfo (_("\nAs-needed library included to satisfy reference by file (symbol)\n\n"));
2222 for (m
= asneeded_list_head
; m
!= NULL
; m
= m
->next
)
2226 minfo ("%s", m
->soname
);
2227 len
= strlen (m
->soname
);
2234 print_spaces (30 - len
);
2237 minfo ("%pB ", m
->ref
);
2238 minfo ("(%pT)\n", m
->name
);
2243 lang_map_flags (flagword flag
)
2245 if (flag
& SEC_ALLOC
)
2248 if (flag
& SEC_CODE
)
2251 if (flag
& SEC_READONLY
)
2254 if (flag
& SEC_DATA
)
2257 if (flag
& SEC_LOAD
)
2264 lang_memory_region_type
*m
;
2265 bool dis_header_printed
= false;
2267 LANG_FOR_EACH_INPUT_STATEMENT (file
)
2271 if ((file
->the_bfd
->flags
& (BFD_LINKER_CREATED
| DYNAMIC
)) != 0
2272 || file
->flags
.just_syms
)
2275 if (config
.print_map_discarded
)
2276 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
2277 if ((s
->output_section
== NULL
2278 || s
->output_section
->owner
!= link_info
.output_bfd
)
2279 && (s
->flags
& (SEC_LINKER_CREATED
| SEC_KEEP
)) == 0)
2281 if (! dis_header_printed
)
2283 fprintf (config
.map_file
, _("\nDiscarded input sections\n\n"));
2284 dis_header_printed
= true;
2287 print_input_section (s
, true);
2291 minfo (_("\nMemory Configuration\n\n"));
2292 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
2293 _("Name"), _("Origin"), _("Length"), _("Attributes"));
2295 for (m
= lang_memory_region_list
; m
!= NULL
; m
= m
->next
)
2297 fprintf (config
.map_file
, "%-16s", m
->name_list
.name
);
2300 bfd_sprintf_vma (link_info
.output_bfd
, buf
, m
->origin
);
2301 fprintf (config
.map_file
, " 0x%-16s", buf
);
2302 bfd_sprintf_vma (link_info
.output_bfd
, buf
, m
->length
);
2303 fprintf (config
.map_file
,
2304 " 0x%*s", m
->flags
|| m
->not_flags
? -17 : 0, buf
);
2306 lang_map_flags (m
->flags
);
2311 lang_map_flags (m
->not_flags
);
2317 fprintf (config
.map_file
, _("\nLinker script and memory map\n\n"));
2319 if (!link_info
.reduce_memory_overheads
)
2321 obstack_begin (&map_obstack
, 1000);
2322 bfd_link_hash_traverse (link_info
.hash
, sort_def_symbol
, 0);
2324 expld
.phase
= lang_fixed_phase_enum
;
2325 lang_statement_iteration
++;
2326 print_statements ();
2328 ldemul_extra_map_file_text (link_info
.output_bfd
, &link_info
,
2333 sort_def_symbol (struct bfd_link_hash_entry
*hash_entry
,
2334 void *info ATTRIBUTE_UNUSED
)
2336 if ((hash_entry
->type
== bfd_link_hash_defined
2337 || hash_entry
->type
== bfd_link_hash_defweak
)
2338 && hash_entry
->u
.def
.section
->owner
!= link_info
.output_bfd
2339 && hash_entry
->u
.def
.section
->owner
!= NULL
)
2341 input_section_userdata_type
*ud
;
2342 struct map_symbol_def
*def
;
2344 ud
= bfd_section_userdata (hash_entry
->u
.def
.section
);
2347 ud
= stat_alloc (sizeof (*ud
));
2348 bfd_set_section_userdata (hash_entry
->u
.def
.section
, ud
);
2349 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
2350 ud
->map_symbol_def_count
= 0;
2352 else if (!ud
->map_symbol_def_tail
)
2353 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
2355 def
= (struct map_symbol_def
*) obstack_alloc (&map_obstack
, sizeof *def
);
2356 def
->entry
= hash_entry
;
2357 *(ud
->map_symbol_def_tail
) = def
;
2358 ud
->map_symbol_def_tail
= &def
->next
;
2359 ud
->map_symbol_def_count
++;
2364 /* Initialize an output section. */
2367 init_os (lang_output_section_statement_type
*s
, flagword flags
)
2369 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
2370 einfo (_("%F%P: illegal use of `%s' section\n"), DISCARD_SECTION_NAME
);
2373 s
->bfd_section
= bfd_get_section_by_name (link_info
.output_bfd
, s
->name
);
2374 if (s
->bfd_section
== NULL
)
2375 s
->bfd_section
= bfd_make_section_anyway_with_flags (link_info
.output_bfd
,
2377 if (s
->bfd_section
== NULL
)
2379 einfo (_("%F%P: output format %s cannot represent section"
2380 " called %s: %E\n"),
2381 link_info
.output_bfd
->xvec
->name
, s
->name
);
2383 s
->bfd_section
->output_section
= s
->bfd_section
;
2384 s
->bfd_section
->output_offset
= 0;
2386 /* Set the userdata of the output section to the output section
2387 statement to avoid lookup. */
2388 bfd_set_section_userdata (s
->bfd_section
, s
);
2390 /* If there is a base address, make sure that any sections it might
2391 mention are initialized. */
2392 if (s
->addr_tree
!= NULL
)
2393 exp_init_os (s
->addr_tree
);
2395 if (s
->load_base
!= NULL
)
2396 exp_init_os (s
->load_base
);
2398 /* If supplied an alignment, set it. */
2399 if (s
->section_alignment
!= NULL
)
2400 s
->bfd_section
->alignment_power
= exp_get_power (s
->section_alignment
,
2401 "section alignment");
2404 /* Make sure that all output sections mentioned in an expression are
2408 exp_init_os (etree_type
*exp
)
2410 switch (exp
->type
.node_class
)
2414 case etree_provided
:
2415 exp_init_os (exp
->assign
.src
);
2419 exp_init_os (exp
->binary
.lhs
);
2420 exp_init_os (exp
->binary
.rhs
);
2424 exp_init_os (exp
->trinary
.cond
);
2425 exp_init_os (exp
->trinary
.lhs
);
2426 exp_init_os (exp
->trinary
.rhs
);
2430 exp_init_os (exp
->assert_s
.child
);
2434 exp_init_os (exp
->unary
.child
);
2438 switch (exp
->type
.node_code
)
2443 lang_output_section_statement_type
*os
;
2445 os
= lang_output_section_find (exp
->name
.name
);
2446 if (os
!= NULL
&& os
->bfd_section
== NULL
)
2458 section_already_linked (bfd
*abfd
, asection
*sec
, void *data
)
2460 lang_input_statement_type
*entry
= (lang_input_statement_type
*) data
;
2462 /* If we are only reading symbols from this object, then we want to
2463 discard all sections. */
2464 if (entry
->flags
.just_syms
)
2466 bfd_link_just_syms (abfd
, sec
, &link_info
);
2470 /* Deal with SHF_EXCLUDE ELF sections. */
2471 if (!bfd_link_relocatable (&link_info
)
2472 && (abfd
->flags
& BFD_PLUGIN
) == 0
2473 && (sec
->flags
& (SEC_GROUP
| SEC_KEEP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
2474 sec
->output_section
= bfd_abs_section_ptr
;
2476 if (!(abfd
->flags
& DYNAMIC
))
2477 bfd_section_already_linked (abfd
, sec
, &link_info
);
2481 /* Returns true if SECTION is one we know will be discarded based on its
2482 section flags, otherwise returns false. */
2485 lang_discard_section_p (asection
*section
)
2488 flagword flags
= section
->flags
;
2490 /* Discard sections marked with SEC_EXCLUDE. */
2491 discard
= (flags
& SEC_EXCLUDE
) != 0;
2493 /* Discard the group descriptor sections when we're finally placing the
2494 sections from within the group. */
2495 if ((flags
& SEC_GROUP
) != 0
2496 && link_info
.resolve_section_groups
)
2499 /* Discard debugging sections if we are stripping debugging
2501 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
2502 && (flags
& SEC_DEBUGGING
) != 0)
2508 /* The wild routines.
2510 These expand statements like *(.text) and foo.o to a list of
2511 explicit actions, like foo.o(.text), bar.o(.text) and
2512 foo.o(.text, .data). */
2514 /* Add SECTION to the output section OUTPUT. Do this by creating a
2515 lang_input_section statement which is placed at PTR. */
2518 lang_add_section (lang_statement_list_type
*ptr
,
2520 struct wildcard_list
*pattern
,
2521 struct flag_info
*sflag_info
,
2522 lang_output_section_statement_type
*output
)
2524 flagword flags
= section
->flags
;
2527 lang_input_section_type
*new_section
;
2528 bfd
*abfd
= link_info
.output_bfd
;
2530 /* Is this section one we know should be discarded? */
2531 discard
= lang_discard_section_p (section
);
2533 /* Discard input sections which are assigned to a section named
2534 DISCARD_SECTION_NAME. */
2535 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
2540 if (section
->output_section
== NULL
)
2542 /* This prevents future calls from assigning this section. */
2543 section
->output_section
= bfd_abs_section_ptr
;
2545 else if (link_info
.non_contiguous_regions_warnings
)
2546 einfo (_("%P:%pS: warning: --enable-non-contiguous-regions makes "
2547 "section `%pA' from `%pB' match /DISCARD/ clause.\n"),
2548 NULL
, section
, section
->owner
);
2557 keep
= bfd_lookup_section_flags (&link_info
, sflag_info
, section
);
2562 if (section
->output_section
!= NULL
)
2564 if (!link_info
.non_contiguous_regions
)
2567 /* SECTION has already been handled in a special way
2568 (eg. LINK_ONCE): skip it. */
2569 if (bfd_is_abs_section (section
->output_section
))
2572 /* Already assigned to the same output section, do not process
2573 it again, to avoid creating loops between duplicate sections
2575 if (section
->output_section
== output
->bfd_section
)
2578 if (link_info
.non_contiguous_regions_warnings
&& output
->bfd_section
)
2579 einfo (_("%P:%pS: warning: --enable-non-contiguous-regions may "
2580 "change behaviour for section `%pA' from `%pB' (assigned to "
2581 "%pA, but additional match: %pA)\n"),
2582 NULL
, section
, section
->owner
, section
->output_section
,
2583 output
->bfd_section
);
2585 /* SECTION has already been assigned to an output section, but
2586 the user allows it to be mapped to another one in case it
2587 overflows. We'll later update the actual output section in
2588 size_input_section as appropriate. */
2591 /* We don't copy the SEC_NEVER_LOAD flag from an input section
2592 to an output section, because we want to be able to include a
2593 SEC_NEVER_LOAD section in the middle of an otherwise loaded
2594 section (I don't know why we want to do this, but we do).
2595 build_link_order in ldwrite.c handles this case by turning
2596 the embedded SEC_NEVER_LOAD section into a fill. */
2597 flags
&= ~ SEC_NEVER_LOAD
;
2599 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
2600 already been processed. One reason to do this is that on pe
2601 format targets, .text$foo sections go into .text and it's odd
2602 to see .text with SEC_LINK_ONCE set. */
2603 if ((flags
& (SEC_LINK_ONCE
| SEC_GROUP
)) == (SEC_LINK_ONCE
| SEC_GROUP
))
2605 if (link_info
.resolve_section_groups
)
2606 flags
&= ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
);
2608 flags
&= ~(SEC_LINK_DUPLICATES
| SEC_RELOC
);
2610 else if (!bfd_link_relocatable (&link_info
))
2611 flags
&= ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
);
2613 switch (output
->sectype
)
2615 case normal_section
:
2616 case overlay_section
:
2617 case first_overlay_section
:
2620 case noalloc_section
:
2621 flags
&= ~SEC_ALLOC
;
2623 case typed_readonly_section
:
2624 case readonly_section
:
2625 flags
|= SEC_READONLY
;
2627 case noload_section
:
2629 flags
|= SEC_NEVER_LOAD
;
2630 /* Unfortunately GNU ld has managed to evolve two different
2631 meanings to NOLOAD in scripts. ELF gets a .bss style noload,
2632 alloc, no contents section. All others get a noload, noalloc
2634 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
)
2635 flags
&= ~SEC_HAS_CONTENTS
;
2637 flags
&= ~SEC_ALLOC
;
2641 if (output
->bfd_section
== NULL
)
2642 init_os (output
, flags
);
2644 /* If SEC_READONLY is not set in the input section, then clear
2645 it from the output section. */
2646 output
->bfd_section
->flags
&= flags
| ~SEC_READONLY
;
2648 if (output
->bfd_section
->linker_has_input
)
2650 /* Only set SEC_READONLY flag on the first input section. */
2651 flags
&= ~ SEC_READONLY
;
2653 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
2654 if ((output
->bfd_section
->flags
& (SEC_MERGE
| SEC_STRINGS
))
2655 != (flags
& (SEC_MERGE
| SEC_STRINGS
))
2656 || ((flags
& SEC_MERGE
) != 0
2657 && output
->bfd_section
->entsize
!= section
->entsize
))
2659 output
->bfd_section
->flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2660 flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2663 output
->bfd_section
->flags
|= flags
;
2665 if (!output
->bfd_section
->linker_has_input
)
2667 output
->bfd_section
->linker_has_input
= 1;
2668 /* This must happen after flags have been updated. The output
2669 section may have been created before we saw its first input
2670 section, eg. for a data statement. */
2671 bfd_init_private_section_data (section
->owner
, section
,
2672 link_info
.output_bfd
,
2673 output
->bfd_section
,
2675 if ((flags
& SEC_MERGE
) != 0)
2676 output
->bfd_section
->entsize
= section
->entsize
;
2679 if ((flags
& SEC_TIC54X_BLOCK
) != 0
2680 && bfd_get_arch (section
->owner
) == bfd_arch_tic54x
)
2682 /* FIXME: This value should really be obtained from the bfd... */
2683 output
->block_value
= 128;
2686 /* When a .ctors section is placed in .init_array it must be copied
2687 in reverse order. Similarly for .dtors. Set that up. */
2688 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
2689 && ((startswith (section
->name
, ".ctors")
2690 && strcmp (output
->bfd_section
->name
, ".init_array") == 0)
2691 || (startswith (section
->name
, ".dtors")
2692 && strcmp (output
->bfd_section
->name
, ".fini_array") == 0))
2693 && (section
->name
[6] == 0 || section
->name
[6] == '.'))
2694 section
->flags
|= SEC_ELF_REVERSE_COPY
;
2696 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
2697 output
->bfd_section
->alignment_power
= section
->alignment_power
;
2699 section
->output_section
= output
->bfd_section
;
2701 if (!map_head_is_link_order
)
2703 asection
*s
= output
->bfd_section
->map_tail
.s
;
2704 output
->bfd_section
->map_tail
.s
= section
;
2705 section
->map_head
.s
= NULL
;
2706 section
->map_tail
.s
= s
;
2708 s
->map_head
.s
= section
;
2710 output
->bfd_section
->map_head
.s
= section
;
2713 /* Add a section reference to the list. */
2714 new_section
= new_stat (lang_input_section
, ptr
);
2715 new_section
->section
= section
;
2716 new_section
->pattern
= pattern
;
2719 /* Expand a wild statement for a particular FILE. SECTION may be
2720 NULL, in which case it is a wild card. This assumes that the
2721 wild statement doesn't need any sorting (of filenames or sections). */
2724 output_section_callback_nosort (lang_wild_statement_type
*ptr
,
2725 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
2727 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
2730 lang_output_section_statement_type
*os
;
2732 os
= (lang_output_section_statement_type
*) output
;
2734 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2735 if (unique_section_p (section
, os
))
2738 lang_add_section (&ptr
->children
, section
, ptr
->section_list
,
2739 ptr
->section_flag_list
, os
);
2742 /* Check if all sections in a wild statement for a particular FILE
2746 check_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
2747 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
2749 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
2752 lang_output_section_statement_type
*os
;
2754 os
= (lang_output_section_statement_type
*) output
;
2756 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2757 if (unique_section_p (section
, os
))
2760 if (section
->output_section
== NULL
&& (section
->flags
& SEC_READONLY
) == 0)
2761 os
->all_input_readonly
= false;
2764 /* This is passed a file name which must have been seen already and
2765 added to the statement tree. We will see if it has been opened
2766 already and had its symbols read. If not then we'll read it. */
2768 static lang_input_statement_type
*
2769 lookup_name (const char *name
)
2771 lang_input_statement_type
*search
;
2773 for (search
= (void *) input_file_chain
.head
;
2775 search
= search
->next_real_file
)
2777 /* Use the local_sym_name as the name of the file that has
2778 already been loaded as filename might have been transformed
2779 via the search directory lookup mechanism. */
2780 const char *filename
= search
->local_sym_name
;
2782 if (filename
!= NULL
2783 && filename_cmp (filename
, name
) == 0)
2789 /* Arrange to splice the input statement added by new_afile into
2790 statement_list after the current input_file_chain tail.
2791 We know input_file_chain is not an empty list, and that
2792 lookup_name was called via open_input_bfds. Later calls to
2793 lookup_name should always match an existing input_statement. */
2794 lang_statement_union_type
**tail
= stat_ptr
->tail
;
2795 lang_statement_union_type
**after
2796 = (void *) ((char *) input_file_chain
.tail
2797 - offsetof (lang_input_statement_type
, next_real_file
)
2798 + offsetof (lang_input_statement_type
, header
.next
));
2799 lang_statement_union_type
*rest
= *after
;
2800 stat_ptr
->tail
= after
;
2801 search
= new_afile (name
, lang_input_file_is_search_file_enum
,
2802 default_target
, NULL
);
2803 *stat_ptr
->tail
= rest
;
2805 stat_ptr
->tail
= tail
;
2808 /* If we have already added this file, or this file is not real
2809 don't add this file. */
2810 if (search
->flags
.loaded
|| !search
->flags
.real
)
2813 if (!load_symbols (search
, NULL
))
2819 /* Save LIST as a list of libraries whose symbols should not be exported. */
2824 struct excluded_lib
*next
;
2826 static struct excluded_lib
*excluded_libs
;
2829 add_excluded_libs (const char *list
)
2831 const char *p
= list
, *end
;
2835 struct excluded_lib
*entry
;
2836 end
= strpbrk (p
, ",:");
2838 end
= p
+ strlen (p
);
2839 entry
= (struct excluded_lib
*) xmalloc (sizeof (*entry
));
2840 entry
->next
= excluded_libs
;
2841 entry
->name
= (char *) xmalloc (end
- p
+ 1);
2842 memcpy (entry
->name
, p
, end
- p
);
2843 entry
->name
[end
- p
] = '\0';
2844 excluded_libs
= entry
;
2852 check_excluded_libs (bfd
*abfd
)
2854 struct excluded_lib
*lib
= excluded_libs
;
2858 int len
= strlen (lib
->name
);
2859 const char *filename
= lbasename (bfd_get_filename (abfd
));
2861 if (strcmp (lib
->name
, "ALL") == 0)
2863 abfd
->no_export
= true;
2867 if (filename_ncmp (lib
->name
, filename
, len
) == 0
2868 && (filename
[len
] == '\0'
2869 || (filename
[len
] == '.' && filename
[len
+ 1] == 'a'
2870 && filename
[len
+ 2] == '\0')))
2872 abfd
->no_export
= true;
2880 /* Get the symbols for an input file. */
2883 load_symbols (lang_input_statement_type
*entry
,
2884 lang_statement_list_type
*place
)
2888 if (entry
->flags
.loaded
)
2891 ldfile_open_file (entry
);
2893 /* Do not process further if the file was missing. */
2894 if (entry
->flags
.missing_file
)
2897 if (trace_files
|| verbose
)
2898 info_msg ("%pI\n", entry
);
2900 if (!bfd_check_format (entry
->the_bfd
, bfd_archive
)
2901 && !bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
2904 struct lang_input_statement_flags save_flags
;
2907 err
= bfd_get_error ();
2909 /* See if the emulation has some special knowledge. */
2910 if (ldemul_unrecognized_file (entry
))
2912 if (err
== bfd_error_file_ambiguously_recognized
)
2917 if (err
== bfd_error_file_ambiguously_recognized
)
2921 einfo (_("%P: %pB: file not recognized: %E;"
2922 " matching formats:"), entry
->the_bfd
);
2923 for (p
= matching
; *p
!= NULL
; p
++)
2928 else if (err
!= bfd_error_file_not_recognized
2930 einfo (_("%F%P: %pB: file not recognized: %E\n"), entry
->the_bfd
);
2932 bfd_close (entry
->the_bfd
);
2933 entry
->the_bfd
= NULL
;
2935 /* Try to interpret the file as a linker script. */
2936 save_flags
= input_flags
;
2937 ldfile_open_command_file (entry
->filename
);
2939 push_stat_ptr (place
);
2940 input_flags
.add_DT_NEEDED_for_regular
2941 = entry
->flags
.add_DT_NEEDED_for_regular
;
2942 input_flags
.add_DT_NEEDED_for_dynamic
2943 = entry
->flags
.add_DT_NEEDED_for_dynamic
;
2944 input_flags
.whole_archive
= entry
->flags
.whole_archive
;
2945 input_flags
.dynamic
= entry
->flags
.dynamic
;
2947 ldfile_assumed_script
= true;
2948 parser_input
= input_script
;
2949 current_input_file
= entry
->filename
;
2951 current_input_file
= NULL
;
2952 ldfile_assumed_script
= false;
2954 /* missing_file is sticky. sysrooted will already have been
2955 restored when seeing EOF in yyparse, but no harm to restore
2957 save_flags
.missing_file
|= input_flags
.missing_file
;
2958 input_flags
= save_flags
;
2962 entry
->flags
.loaded
= true;
2967 if (ldemul_recognized_file (entry
))
2970 /* We don't call ldlang_add_file for an archive. Instead, the
2971 add_symbols entry point will call ldlang_add_file, via the
2972 add_archive_element callback, for each element of the archive
2974 switch (bfd_get_format (entry
->the_bfd
))
2980 if (!entry
->flags
.reload
)
2981 ldlang_add_file (entry
);
2985 check_excluded_libs (entry
->the_bfd
);
2987 bfd_set_usrdata (entry
->the_bfd
, entry
);
2988 if (entry
->flags
.whole_archive
)
2996 member
= bfd_openr_next_archived_file (entry
->the_bfd
, member
);
3001 if (!bfd_check_format (member
, bfd_object
))
3003 einfo (_("%F%P: %pB: member %pB in archive is not an object\n"),
3004 entry
->the_bfd
, member
);
3009 if (!(*link_info
.callbacks
3010 ->add_archive_element
) (&link_info
, member
,
3011 "--whole-archive", &subsbfd
))
3014 /* Potentially, the add_archive_element hook may have set a
3015 substitute BFD for us. */
3016 if (!bfd_link_add_symbols (subsbfd
, &link_info
))
3018 einfo (_("%F%P: %pB: error adding symbols: %E\n"), member
);
3023 entry
->flags
.loaded
= loaded
;
3029 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
3030 entry
->flags
.loaded
= true;
3032 einfo (_("%F%P: %pB: error adding symbols: %E\n"), entry
->the_bfd
);
3034 return entry
->flags
.loaded
;
3037 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
3038 may be NULL, indicating that it is a wildcard. Separate
3039 lang_input_section statements are created for each part of the
3040 expansion; they are added after the wild statement S. OUTPUT is
3041 the output section. */
3044 wild (lang_wild_statement_type
*s
,
3045 const char *target ATTRIBUTE_UNUSED
,
3046 lang_output_section_statement_type
*output
)
3048 struct wildcard_list
*sec
;
3050 if (s
->filenames_sorted
|| s
->any_specs_sorted
)
3052 lang_section_bst_type
*tree
;
3054 walk_wild (s
, output_section_callback_sort
, output
);
3059 output_section_callback_tree_to_list (s
, tree
, output
);
3061 s
->rightmost
= &s
->tree
;
3065 walk_wild (s
, output_section_callback_nosort
, output
);
3067 if (default_common_section
== NULL
)
3068 for (sec
= s
->section_list
; sec
!= NULL
; sec
= sec
->next
)
3069 if (sec
->spec
.name
!= NULL
&& strcmp (sec
->spec
.name
, "COMMON") == 0)
3071 /* Remember the section that common is going to in case we
3072 later get something which doesn't know where to put it. */
3073 default_common_section
= output
;
3078 /* Return TRUE iff target is the sought target. */
3081 get_target (const bfd_target
*target
, void *data
)
3083 const char *sought
= (const char *) data
;
3085 return strcmp (target
->name
, sought
) == 0;
3088 /* Like strcpy() but convert to lower case as well. */
3091 stricpy (char *dest
, const char *src
)
3095 while ((c
= *src
++) != 0)
3096 *dest
++ = TOLOWER (c
);
3101 /* Remove the first occurrence of needle (if any) in haystack
3105 strcut (char *haystack
, const char *needle
)
3107 haystack
= strstr (haystack
, needle
);
3113 for (src
= haystack
+ strlen (needle
); *src
;)
3114 *haystack
++ = *src
++;
3120 /* Compare two target format name strings.
3121 Return a value indicating how "similar" they are. */
3124 name_compare (const char *first
, const char *second
)
3130 copy1
= (char *) xmalloc (strlen (first
) + 1);
3131 copy2
= (char *) xmalloc (strlen (second
) + 1);
3133 /* Convert the names to lower case. */
3134 stricpy (copy1
, first
);
3135 stricpy (copy2
, second
);
3137 /* Remove size and endian strings from the name. */
3138 strcut (copy1
, "big");
3139 strcut (copy1
, "little");
3140 strcut (copy2
, "big");
3141 strcut (copy2
, "little");
3143 /* Return a value based on how many characters match,
3144 starting from the beginning. If both strings are
3145 the same then return 10 * their length. */
3146 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
3147 if (copy1
[result
] == 0)
3159 /* Set by closest_target_match() below. */
3160 static const bfd_target
*winner
;
3162 /* Scan all the valid bfd targets looking for one that has the endianness
3163 requirement that was specified on the command line, and is the nearest
3164 match to the original output target. */
3167 closest_target_match (const bfd_target
*target
, void *data
)
3169 const bfd_target
*original
= (const bfd_target
*) data
;
3171 if (command_line
.endian
== ENDIAN_BIG
3172 && target
->byteorder
!= BFD_ENDIAN_BIG
)
3175 if (command_line
.endian
== ENDIAN_LITTLE
3176 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
3179 /* Must be the same flavour. */
3180 if (target
->flavour
!= original
->flavour
)
3183 /* Ignore generic big and little endian elf vectors. */
3184 if (strcmp (target
->name
, "elf32-big") == 0
3185 || strcmp (target
->name
, "elf64-big") == 0
3186 || strcmp (target
->name
, "elf32-little") == 0
3187 || strcmp (target
->name
, "elf64-little") == 0)
3190 /* If we have not found a potential winner yet, then record this one. */
3197 /* Oh dear, we now have two potential candidates for a successful match.
3198 Compare their names and choose the better one. */
3199 if (name_compare (target
->name
, original
->name
)
3200 > name_compare (winner
->name
, original
->name
))
3203 /* Keep on searching until wqe have checked them all. */
3207 /* Return the BFD target format of the first input file. */
3210 get_first_input_target (void)
3212 const char *target
= NULL
;
3214 LANG_FOR_EACH_INPUT_STATEMENT (s
)
3216 if (s
->header
.type
== lang_input_statement_enum
3219 ldfile_open_file (s
);
3221 if (s
->the_bfd
!= NULL
3222 && bfd_check_format (s
->the_bfd
, bfd_object
))
3224 target
= bfd_get_target (s
->the_bfd
);
3236 lang_get_output_target (void)
3240 /* Has the user told us which output format to use? */
3241 if (output_target
!= NULL
)
3242 return output_target
;
3244 /* No - has the current target been set to something other than
3246 if (current_target
!= default_target
&& current_target
!= NULL
)
3247 return current_target
;
3249 /* No - can we determine the format of the first input file? */
3250 target
= get_first_input_target ();
3254 /* Failed - use the default output target. */
3255 return default_target
;
3258 /* Open the output file. */
3261 open_output (const char *name
)
3263 lang_input_statement_type
*f
;
3264 char *out
= lrealpath (name
);
3266 for (f
= (void *) input_file_chain
.head
;
3268 f
= f
->next_real_file
)
3271 char *in
= lrealpath (f
->local_sym_name
);
3272 if (filename_cmp (in
, out
) == 0)
3273 einfo (_("%F%P: input file '%s' is the same as output file\n"),
3279 output_target
= lang_get_output_target ();
3281 /* Has the user requested a particular endianness on the command
3283 if (command_line
.endian
!= ENDIAN_UNSET
)
3285 /* Get the chosen target. */
3286 const bfd_target
*target
3287 = bfd_iterate_over_targets (get_target
, (void *) output_target
);
3289 /* If the target is not supported, we cannot do anything. */
3292 enum bfd_endian desired_endian
;
3294 if (command_line
.endian
== ENDIAN_BIG
)
3295 desired_endian
= BFD_ENDIAN_BIG
;
3297 desired_endian
= BFD_ENDIAN_LITTLE
;
3299 /* See if the target has the wrong endianness. This should
3300 not happen if the linker script has provided big and
3301 little endian alternatives, but some scrips don't do
3303 if (target
->byteorder
!= desired_endian
)
3305 /* If it does, then see if the target provides
3306 an alternative with the correct endianness. */
3307 if (target
->alternative_target
!= NULL
3308 && (target
->alternative_target
->byteorder
== desired_endian
))
3309 output_target
= target
->alternative_target
->name
;
3312 /* Try to find a target as similar as possible to
3313 the default target, but which has the desired
3314 endian characteristic. */
3315 bfd_iterate_over_targets (closest_target_match
,
3318 /* Oh dear - we could not find any targets that
3319 satisfy our requirements. */
3321 einfo (_("%P: warning: could not find any targets"
3322 " that match endianness requirement\n"));
3324 output_target
= winner
->name
;
3330 link_info
.output_bfd
= bfd_openw (name
, output_target
);
3332 if (link_info
.output_bfd
== NULL
)
3334 if (bfd_get_error () == bfd_error_invalid_target
)
3335 einfo (_("%F%P: target %s not found\n"), output_target
);
3337 einfo (_("%F%P: cannot open output file %s: %E\n"), name
);
3340 delete_output_file_on_failure
= true;
3342 if (!bfd_set_format (link_info
.output_bfd
, bfd_object
))
3343 einfo (_("%F%P: %s: can not make object file: %E\n"), name
);
3344 if (!bfd_set_arch_mach (link_info
.output_bfd
,
3345 ldfile_output_architecture
,
3346 ldfile_output_machine
))
3347 einfo (_("%F%P: %s: can not set architecture: %E\n"), name
);
3349 link_info
.hash
= bfd_link_hash_table_create (link_info
.output_bfd
);
3350 if (link_info
.hash
== NULL
)
3351 einfo (_("%F%P: can not create hash table: %E\n"));
3353 bfd_set_gp_size (link_info
.output_bfd
, g_switch_value
);
3357 ldlang_open_output (lang_statement_union_type
*statement
)
3359 switch (statement
->header
.type
)
3361 case lang_output_statement_enum
:
3362 ASSERT (link_info
.output_bfd
== NULL
);
3363 open_output (statement
->output_statement
.name
);
3364 ldemul_set_output_arch ();
3365 if (config
.magic_demand_paged
3366 && !bfd_link_relocatable (&link_info
))
3367 link_info
.output_bfd
->flags
|= D_PAGED
;
3369 link_info
.output_bfd
->flags
&= ~D_PAGED
;
3370 if (config
.text_read_only
)
3371 link_info
.output_bfd
->flags
|= WP_TEXT
;
3373 link_info
.output_bfd
->flags
&= ~WP_TEXT
;
3374 if (link_info
.traditional_format
)
3375 link_info
.output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
3377 link_info
.output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
3380 case lang_target_statement_enum
:
3381 current_target
= statement
->target_statement
.target
;
3389 init_opb (asection
*s
)
3394 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
3396 && (s
->flags
& SEC_ELF_OCTETS
) != 0)
3399 x
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3400 ldfile_output_machine
);
3402 while ((x
& 1) == 0)
3410 /* Open all the input files. */
3414 OPEN_BFD_NORMAL
= 0,
3418 #if BFD_SUPPORTS_PLUGINS
3419 static lang_input_statement_type
*plugin_insert
= NULL
;
3420 static struct bfd_link_hash_entry
*plugin_undefs
= NULL
;
3424 open_input_bfds (lang_statement_union_type
*s
, enum open_bfd_mode mode
)
3426 for (; s
!= NULL
; s
= s
->header
.next
)
3428 switch (s
->header
.type
)
3430 case lang_constructors_statement_enum
:
3431 open_input_bfds (constructor_list
.head
, mode
);
3433 case lang_output_section_statement_enum
:
3434 open_input_bfds (s
->output_section_statement
.children
.head
, mode
);
3436 case lang_wild_statement_enum
:
3437 /* Maybe we should load the file's symbols. */
3438 if ((mode
& OPEN_BFD_RESCAN
) == 0
3439 && s
->wild_statement
.filename
3440 && !wildcardp (s
->wild_statement
.filename
)
3441 && !archive_path (s
->wild_statement
.filename
))
3442 lookup_name (s
->wild_statement
.filename
);
3443 open_input_bfds (s
->wild_statement
.children
.head
, mode
);
3445 case lang_group_statement_enum
:
3447 struct bfd_link_hash_entry
*undefs
;
3448 #if BFD_SUPPORTS_PLUGINS
3449 lang_input_statement_type
*plugin_insert_save
;
3452 /* We must continually search the entries in the group
3453 until no new symbols are added to the list of undefined
3458 #if BFD_SUPPORTS_PLUGINS
3459 plugin_insert_save
= plugin_insert
;
3461 undefs
= link_info
.hash
->undefs_tail
;
3462 open_input_bfds (s
->group_statement
.children
.head
,
3463 mode
| OPEN_BFD_FORCE
);
3465 while (undefs
!= link_info
.hash
->undefs_tail
3466 #if BFD_SUPPORTS_PLUGINS
3467 /* Objects inserted by a plugin, which are loaded
3468 before we hit this loop, may have added new
3470 || (plugin_insert
!= plugin_insert_save
&& plugin_undefs
)
3475 case lang_target_statement_enum
:
3476 current_target
= s
->target_statement
.target
;
3478 case lang_input_statement_enum
:
3479 if (s
->input_statement
.flags
.real
)
3481 lang_statement_union_type
**os_tail
;
3482 lang_statement_list_type add
;
3485 s
->input_statement
.target
= current_target
;
3487 /* If we are being called from within a group, and this
3488 is an archive which has already been searched, then
3489 force it to be researched unless the whole archive
3490 has been loaded already. Do the same for a rescan.
3491 Likewise reload --as-needed shared libs. */
3492 if (mode
!= OPEN_BFD_NORMAL
3493 #if BFD_SUPPORTS_PLUGINS
3494 && ((mode
& OPEN_BFD_RESCAN
) == 0
3495 || plugin_insert
== NULL
)
3497 && s
->input_statement
.flags
.loaded
3498 && (abfd
= s
->input_statement
.the_bfd
) != NULL
3499 && ((bfd_get_format (abfd
) == bfd_archive
3500 && !s
->input_statement
.flags
.whole_archive
)
3501 || (bfd_get_format (abfd
) == bfd_object
3502 && ((abfd
->flags
) & DYNAMIC
) != 0
3503 && s
->input_statement
.flags
.add_DT_NEEDED_for_regular
3504 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
3505 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)))
3507 s
->input_statement
.flags
.loaded
= false;
3508 s
->input_statement
.flags
.reload
= true;
3511 os_tail
= lang_os_list
.tail
;
3512 lang_list_init (&add
);
3514 if (!load_symbols (&s
->input_statement
, &add
))
3515 config
.make_executable
= false;
3517 if (add
.head
!= NULL
)
3519 /* If this was a script with output sections then
3520 tack any added statements on to the end of the
3521 list. This avoids having to reorder the output
3522 section statement list. Very likely the user
3523 forgot -T, and whatever we do here will not meet
3524 naive user expectations. */
3525 if (os_tail
!= lang_os_list
.tail
)
3527 einfo (_("%P: warning: %s contains output sections;"
3528 " did you forget -T?\n"),
3529 s
->input_statement
.filename
);
3530 *stat_ptr
->tail
= add
.head
;
3531 stat_ptr
->tail
= add
.tail
;
3535 *add
.tail
= s
->header
.next
;
3536 s
->header
.next
= add
.head
;
3540 #if BFD_SUPPORTS_PLUGINS
3541 /* If we have found the point at which a plugin added new
3542 files, clear plugin_insert to enable archive rescan. */
3543 if (&s
->input_statement
== plugin_insert
)
3544 plugin_insert
= NULL
;
3547 case lang_assignment_statement_enum
:
3548 if (s
->assignment_statement
.exp
->type
.node_class
!= etree_assert
)
3549 exp_fold_tree_no_dot (s
->assignment_statement
.exp
);
3556 /* Exit if any of the files were missing. */
3557 if (input_flags
.missing_file
)
3561 #ifdef ENABLE_LIBCTF
3562 /* Emit CTF errors and warnings. fp can be NULL to report errors/warnings
3563 that happened specifically at CTF open time. */
3565 lang_ctf_errs_warnings (ctf_dict_t
*fp
)
3567 ctf_next_t
*i
= NULL
;
3572 while ((text
= ctf_errwarning_next (fp
, &i
, &is_warning
, &err
)) != NULL
)
3574 einfo (_("%s: %s\n"), is_warning
? _("CTF warning"): _("CTF error"),
3578 if (err
!= ECTF_NEXT_END
)
3580 einfo (_("CTF error: cannot get CTF errors: `%s'\n"),
3584 /* `err' returns errors from the error/warning iterator in particular.
3585 These never assert. But if we have an fp, that could have recorded
3586 an assertion failure: assert if it has done so. */
3587 ASSERT (!fp
|| ctf_errno (fp
) != ECTF_INTERNAL
);
3590 /* Open the CTF sections in the input files with libctf: if any were opened,
3591 create a fake input file that we'll write the merged CTF data to later
3595 ldlang_open_ctf (void)
3600 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3604 /* Incoming files from the compiler have a single ctf_dict_t in them
3605 (which is presented to us by the libctf API in a ctf_archive_t
3606 wrapper): files derived from a previous relocatable link have a CTF
3607 archive containing possibly many CTF files. */
3609 if ((file
->the_ctf
= ctf_bfdopen (file
->the_bfd
, &err
)) == NULL
)
3611 if (err
!= ECTF_NOCTFDATA
)
3613 lang_ctf_errs_warnings (NULL
);
3614 einfo (_("%P: warning: CTF section in %pB not loaded; "
3615 "its types will be discarded: %s\n"), file
->the_bfd
,
3621 /* Prevent the contents of this section from being written, while
3622 requiring the section itself to be duplicated in the output, but only
3624 /* This section must exist if ctf_bfdopen() succeeded. */
3625 sect
= bfd_get_section_by_name (file
->the_bfd
, ".ctf");
3627 sect
->flags
|= SEC_NEVER_LOAD
| SEC_HAS_CONTENTS
| SEC_LINKER_CREATED
;
3630 sect
->flags
|= SEC_EXCLUDE
;
3640 if ((ctf_output
= ctf_create (&err
)) != NULL
)
3643 einfo (_("%P: warning: CTF output not created: `%s'\n"),
3646 LANG_FOR_EACH_INPUT_STATEMENT (errfile
)
3647 ctf_close (errfile
->the_ctf
);
3650 /* Merge together CTF sections. After this, only the symtab-dependent
3651 function and data object sections need adjustment. */
3654 lang_merge_ctf (void)
3656 asection
*output_sect
;
3662 output_sect
= bfd_get_section_by_name (link_info
.output_bfd
, ".ctf");
3664 /* If the section was discarded, don't waste time merging. */
3665 if (output_sect
== NULL
)
3667 ctf_dict_close (ctf_output
);
3670 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3672 ctf_close (file
->the_ctf
);
3673 file
->the_ctf
= NULL
;
3678 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3683 /* Takes ownership of file->the_ctf. */
3684 if (ctf_link_add_ctf (ctf_output
, file
->the_ctf
, file
->filename
) < 0)
3686 einfo (_("%P: warning: CTF section in %pB cannot be linked: `%s'\n"),
3687 file
->the_bfd
, ctf_errmsg (ctf_errno (ctf_output
)));
3688 ctf_close (file
->the_ctf
);
3689 file
->the_ctf
= NULL
;
3694 if (!config
.ctf_share_duplicated
)
3695 flags
= CTF_LINK_SHARE_UNCONFLICTED
;
3697 flags
= CTF_LINK_SHARE_DUPLICATED
;
3698 if (!config
.ctf_variables
)
3699 flags
|= CTF_LINK_OMIT_VARIABLES_SECTION
;
3700 if (bfd_link_relocatable (&link_info
))
3701 flags
|= CTF_LINK_NO_FILTER_REPORTED_SYMS
;
3703 if (ctf_link (ctf_output
, flags
) < 0)
3705 lang_ctf_errs_warnings (ctf_output
);
3706 einfo (_("%P: warning: CTF linking failed; "
3707 "output will have no CTF section: %s\n"),
3708 ctf_errmsg (ctf_errno (ctf_output
)));
3711 output_sect
->size
= 0;
3712 output_sect
->flags
|= SEC_EXCLUDE
;
3715 /* Output any lingering errors that didn't come from ctf_link. */
3716 lang_ctf_errs_warnings (ctf_output
);
3719 /* Let the emulation acquire strings from the dynamic strtab to help it optimize
3720 the CTF, if supported. */
3723 ldlang_ctf_acquire_strings (struct elf_strtab_hash
*dynstrtab
)
3725 ldemul_acquire_strings_for_ctf (ctf_output
, dynstrtab
);
3728 /* Inform the emulation about the addition of a new dynamic symbol, in BFD
3730 void ldlang_ctf_new_dynsym (int symidx
, struct elf_internal_sym
*sym
)
3732 ldemul_new_dynsym_for_ctf (ctf_output
, symidx
, sym
);
3735 /* Write out the CTF section. Called early, if the emulation isn't going to
3736 need to dedup against the strtab and symtab, then possibly called from the
3737 target linker code if the dedup has happened. */
3739 lang_write_ctf (int late
)
3742 asection
*output_sect
;
3749 /* Emit CTF late if this emulation says it can do so. */
3750 if (ldemul_emit_ctf_early ())
3755 if (!ldemul_emit_ctf_early ())
3759 /* Inform the emulation that all the symbols that will be received have
3762 ldemul_new_dynsym_for_ctf (ctf_output
, 0, NULL
);
3766 output_sect
= bfd_get_section_by_name (link_info
.output_bfd
, ".ctf");
3769 output_sect
->contents
= ctf_link_write (ctf_output
, &output_size
,
3770 CTF_COMPRESSION_THRESHOLD
);
3771 output_sect
->size
= output_size
;
3772 output_sect
->flags
|= SEC_IN_MEMORY
| SEC_KEEP
;
3774 lang_ctf_errs_warnings (ctf_output
);
3775 if (!output_sect
->contents
)
3777 einfo (_("%P: warning: CTF section emission failed; "
3778 "output will have no CTF section: %s\n"),
3779 ctf_errmsg (ctf_errno (ctf_output
)));
3780 output_sect
->size
= 0;
3781 output_sect
->flags
|= SEC_EXCLUDE
;
3785 /* This also closes every CTF input file used in the link. */
3786 ctf_dict_close (ctf_output
);
3789 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3790 file
->the_ctf
= NULL
;
3793 /* Write out the CTF section late, if the emulation needs that. */
3796 ldlang_write_ctf_late (void)
3798 /* Trigger a "late call", if the emulation needs one. */
3804 ldlang_open_ctf (void)
3806 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3810 /* If built without CTF, warn and delete all CTF sections from the output.
3811 (The alternative would be to simply concatenate them, which does not
3812 yield a valid CTF section.) */
3814 if ((sect
= bfd_get_section_by_name (file
->the_bfd
, ".ctf")) != NULL
)
3816 einfo (_("%P: warning: CTF section in %pB not linkable: "
3817 "%P was built without support for CTF\n"), file
->the_bfd
);
3819 sect
->flags
|= SEC_EXCLUDE
;
3824 static void lang_merge_ctf (void) {}
3826 ldlang_ctf_acquire_strings (struct elf_strtab_hash
*dynstrtab
3827 ATTRIBUTE_UNUSED
) {}
3829 ldlang_ctf_new_dynsym (int symidx ATTRIBUTE_UNUSED
,
3830 struct elf_internal_sym
*sym ATTRIBUTE_UNUSED
) {}
3831 static void lang_write_ctf (int late ATTRIBUTE_UNUSED
) {}
3832 void ldlang_write_ctf_late (void) {}
3835 /* Add the supplied name to the symbol table as an undefined reference.
3836 This is a two step process as the symbol table doesn't even exist at
3837 the time the ld command line is processed. First we put the name
3838 on a list, then, once the output file has been opened, transfer the
3839 name to the symbol table. */
3841 typedef struct bfd_sym_chain ldlang_undef_chain_list_type
;
3843 #define ldlang_undef_chain_list_head entry_symbol.next
3846 ldlang_add_undef (const char *const name
, bool cmdline ATTRIBUTE_UNUSED
)
3848 ldlang_undef_chain_list_type
*new_undef
;
3850 new_undef
= stat_alloc (sizeof (*new_undef
));
3851 new_undef
->next
= ldlang_undef_chain_list_head
;
3852 ldlang_undef_chain_list_head
= new_undef
;
3854 new_undef
->name
= xstrdup (name
);
3856 if (link_info
.output_bfd
!= NULL
)
3857 insert_undefined (new_undef
->name
);
3860 /* Insert NAME as undefined in the symbol table. */
3863 insert_undefined (const char *name
)
3865 struct bfd_link_hash_entry
*h
;
3867 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, false, true);
3869 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
3870 if (h
->type
== bfd_link_hash_new
)
3872 h
->type
= bfd_link_hash_undefined
;
3873 h
->u
.undef
.abfd
= NULL
;
3874 h
->non_ir_ref_regular
= true;
3875 bfd_link_add_undef (link_info
.hash
, h
);
3879 /* Run through the list of undefineds created above and place them
3880 into the linker hash table as undefined symbols belonging to the
3884 lang_place_undefineds (void)
3886 ldlang_undef_chain_list_type
*ptr
;
3888 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
3889 insert_undefined (ptr
->name
);
3892 /* Mark -u symbols against garbage collection. */
3895 lang_mark_undefineds (void)
3897 ldlang_undef_chain_list_type
*ptr
;
3899 if (is_elf_hash_table (link_info
.hash
))
3900 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
3902 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*)
3903 bfd_link_hash_lookup (link_info
.hash
, ptr
->name
, false, false, true);
3909 /* Structure used to build the list of symbols that the user has required
3912 struct require_defined_symbol
3915 struct require_defined_symbol
*next
;
3918 /* The list of symbols that the user has required be defined. */
3920 static struct require_defined_symbol
*require_defined_symbol_list
;
3922 /* Add a new symbol NAME to the list of symbols that are required to be
3926 ldlang_add_require_defined (const char *const name
)
3928 struct require_defined_symbol
*ptr
;
3930 ldlang_add_undef (name
, true);
3931 ptr
= stat_alloc (sizeof (*ptr
));
3932 ptr
->next
= require_defined_symbol_list
;
3933 ptr
->name
= strdup (name
);
3934 require_defined_symbol_list
= ptr
;
3937 /* Check that all symbols the user required to be defined, are defined,
3938 raise an error if we find a symbol that is not defined. */
3941 ldlang_check_require_defined_symbols (void)
3943 struct require_defined_symbol
*ptr
;
3945 for (ptr
= require_defined_symbol_list
; ptr
!= NULL
; ptr
= ptr
->next
)
3947 struct bfd_link_hash_entry
*h
;
3949 h
= bfd_link_hash_lookup (link_info
.hash
, ptr
->name
,
3950 false, false, true);
3952 || (h
->type
!= bfd_link_hash_defined
3953 && h
->type
!= bfd_link_hash_defweak
))
3954 einfo(_("%X%P: required symbol `%s' not defined\n"), ptr
->name
);
3958 /* Check for all readonly or some readwrite sections. */
3961 check_input_sections
3962 (lang_statement_union_type
*s
,
3963 lang_output_section_statement_type
*output_section_statement
)
3965 for (; s
!= NULL
; s
= s
->header
.next
)
3967 switch (s
->header
.type
)
3969 case lang_wild_statement_enum
:
3970 walk_wild (&s
->wild_statement
, check_section_callback
,
3971 output_section_statement
);
3972 if (!output_section_statement
->all_input_readonly
)
3975 case lang_constructors_statement_enum
:
3976 check_input_sections (constructor_list
.head
,
3977 output_section_statement
);
3978 if (!output_section_statement
->all_input_readonly
)
3981 case lang_group_statement_enum
:
3982 check_input_sections (s
->group_statement
.children
.head
,
3983 output_section_statement
);
3984 if (!output_section_statement
->all_input_readonly
)
3993 /* Update wildcard statements if needed. */
3996 update_wild_statements (lang_statement_union_type
*s
)
3998 struct wildcard_list
*sec
;
4000 switch (sort_section
)
4010 for (; s
!= NULL
; s
= s
->header
.next
)
4012 switch (s
->header
.type
)
4017 case lang_wild_statement_enum
:
4018 for (sec
= s
->wild_statement
.section_list
; sec
!= NULL
;
4020 /* Don't sort .init/.fini sections. */
4021 if (strcmp (sec
->spec
.name
, ".init") != 0
4022 && strcmp (sec
->spec
.name
, ".fini") != 0)
4024 switch (sec
->spec
.sorted
)
4027 sec
->spec
.sorted
= sort_section
;
4030 if (sort_section
== by_alignment
)
4031 sec
->spec
.sorted
= by_name_alignment
;
4034 if (sort_section
== by_name
)
4035 sec
->spec
.sorted
= by_alignment_name
;
4040 s
->wild_statement
.any_specs_sorted
= true;
4044 case lang_constructors_statement_enum
:
4045 update_wild_statements (constructor_list
.head
);
4048 case lang_output_section_statement_enum
:
4049 update_wild_statements
4050 (s
->output_section_statement
.children
.head
);
4053 case lang_group_statement_enum
:
4054 update_wild_statements (s
->group_statement
.children
.head
);
4062 /* Open input files and attach to output sections. */
4065 map_input_to_output_sections
4066 (lang_statement_union_type
*s
, const char *target
,
4067 lang_output_section_statement_type
*os
)
4069 for (; s
!= NULL
; s
= s
->header
.next
)
4071 lang_output_section_statement_type
*tos
;
4073 unsigned int type
= 0;
4075 switch (s
->header
.type
)
4077 case lang_wild_statement_enum
:
4078 wild (&s
->wild_statement
, target
, os
);
4080 case lang_constructors_statement_enum
:
4081 map_input_to_output_sections (constructor_list
.head
,
4085 case lang_output_section_statement_enum
:
4086 tos
= &s
->output_section_statement
;
4087 if (tos
->constraint
== ONLY_IF_RW
4088 || tos
->constraint
== ONLY_IF_RO
)
4090 tos
->all_input_readonly
= true;
4091 check_input_sections (tos
->children
.head
, tos
);
4092 if (tos
->all_input_readonly
!= (tos
->constraint
== ONLY_IF_RO
))
4093 tos
->constraint
= -1;
4095 if (tos
->constraint
>= 0)
4096 map_input_to_output_sections (tos
->children
.head
,
4100 case lang_output_statement_enum
:
4102 case lang_target_statement_enum
:
4103 target
= s
->target_statement
.target
;
4105 case lang_group_statement_enum
:
4106 map_input_to_output_sections (s
->group_statement
.children
.head
,
4110 case lang_data_statement_enum
:
4111 /* Make sure that any sections mentioned in the expression
4113 exp_init_os (s
->data_statement
.exp
);
4114 /* The output section gets CONTENTS, ALLOC and LOAD, but
4115 these may be overridden by the script. */
4116 flags
= SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
;
4117 switch (os
->sectype
)
4119 case normal_section
:
4120 case overlay_section
:
4121 case first_overlay_section
:
4123 case noalloc_section
:
4124 flags
= SEC_HAS_CONTENTS
;
4126 case readonly_section
:
4127 flags
|= SEC_READONLY
;
4129 case typed_readonly_section
:
4130 flags
|= SEC_READONLY
;
4133 if (os
->sectype_value
->type
.node_class
== etree_name
4134 && os
->sectype_value
->type
.node_code
== NAME
)
4136 const char *name
= os
->sectype_value
->name
.name
;
4137 if (strcmp (name
, "SHT_PROGBITS") == 0)
4138 type
= SHT_PROGBITS
;
4139 else if (strcmp (name
, "SHT_STRTAB") == 0)
4141 else if (strcmp (name
, "SHT_NOTE") == 0)
4143 else if (strcmp (name
, "SHT_NOBITS") == 0)
4145 else if (strcmp (name
, "SHT_INIT_ARRAY") == 0)
4146 type
= SHT_INIT_ARRAY
;
4147 else if (strcmp (name
, "SHT_FINI_ARRAY") == 0)
4148 type
= SHT_FINI_ARRAY
;
4149 else if (strcmp (name
, "SHT_PREINIT_ARRAY") == 0)
4150 type
= SHT_PREINIT_ARRAY
;
4152 einfo (_ ("%F%P: invalid type for output section `%s'\n"),
4157 exp_fold_tree_no_dot (os
->sectype_value
);
4158 if (expld
.result
.valid_p
)
4159 type
= expld
.result
.value
;
4161 einfo (_ ("%F%P: invalid type for output section `%s'\n"),
4165 case noload_section
:
4166 if (bfd_get_flavour (link_info
.output_bfd
)
4167 == bfd_target_elf_flavour
)
4168 flags
= SEC_NEVER_LOAD
| SEC_ALLOC
;
4170 flags
= SEC_NEVER_LOAD
| SEC_HAS_CONTENTS
;
4173 if (os
->bfd_section
== NULL
)
4174 init_os (os
, flags
| SEC_READONLY
);
4176 os
->bfd_section
->flags
|= flags
;
4177 os
->bfd_section
->type
= type
;
4179 case lang_input_section_enum
:
4181 case lang_fill_statement_enum
:
4182 case lang_object_symbols_statement_enum
:
4183 case lang_reloc_statement_enum
:
4184 case lang_padding_statement_enum
:
4185 case lang_input_statement_enum
:
4186 if (os
!= NULL
&& os
->bfd_section
== NULL
)
4189 case lang_assignment_statement_enum
:
4190 if (os
!= NULL
&& os
->bfd_section
== NULL
)
4193 /* Make sure that any sections mentioned in the assignment
4195 exp_init_os (s
->assignment_statement
.exp
);
4197 case lang_address_statement_enum
:
4198 /* Mark the specified section with the supplied address.
4199 If this section was actually a segment marker, then the
4200 directive is ignored if the linker script explicitly
4201 processed the segment marker. Originally, the linker
4202 treated segment directives (like -Ttext on the
4203 command-line) as section directives. We honor the
4204 section directive semantics for backwards compatibility;
4205 linker scripts that do not specifically check for
4206 SEGMENT_START automatically get the old semantics. */
4207 if (!s
->address_statement
.segment
4208 || !s
->address_statement
.segment
->used
)
4210 const char *name
= s
->address_statement
.section_name
;
4212 /* Create the output section statement here so that
4213 orphans with a set address will be placed after other
4214 script sections. If we let the orphan placement code
4215 place them in amongst other sections then the address
4216 will affect following script sections, which is
4217 likely to surprise naive users. */
4218 tos
= lang_output_section_statement_lookup (name
, 0, 1);
4219 tos
->addr_tree
= s
->address_statement
.address
;
4220 if (tos
->bfd_section
== NULL
)
4224 case lang_insert_statement_enum
:
4226 case lang_input_matcher_enum
:
4232 /* An insert statement snips out all the linker statements from the
4233 start of the list and places them after the output section
4234 statement specified by the insert. This operation is complicated
4235 by the fact that we keep a doubly linked list of output section
4236 statements as well as the singly linked list of all statements.
4237 FIXME someday: Twiddling with the list not only moves statements
4238 from the user's script but also input and group statements that are
4239 built from command line object files and --start-group. We only
4240 get away with this because the list pointers used by file_chain
4241 and input_file_chain are not reordered, and processing via
4242 statement_list after this point mostly ignores input statements.
4243 One exception is the map file, where LOAD and START GROUP/END GROUP
4244 can end up looking odd. */
4247 process_insert_statements (lang_statement_union_type
**start
)
4249 lang_statement_union_type
**s
;
4250 lang_output_section_statement_type
*first_os
= NULL
;
4251 lang_output_section_statement_type
*last_os
= NULL
;
4252 lang_output_section_statement_type
*os
;
4257 if ((*s
)->header
.type
== lang_output_section_statement_enum
)
4259 /* Keep pointers to the first and last output section
4260 statement in the sequence we may be about to move. */
4261 os
= &(*s
)->output_section_statement
;
4263 ASSERT (last_os
== NULL
|| last_os
->next
== os
);
4266 /* Set constraint negative so that lang_output_section_find
4267 won't match this output section statement. At this
4268 stage in linking constraint has values in the range
4269 [-1, ONLY_IN_RW]. */
4270 last_os
->constraint
= -2 - last_os
->constraint
;
4271 if (first_os
== NULL
)
4274 else if ((*s
)->header
.type
== lang_group_statement_enum
)
4276 /* A user might put -T between --start-group and
4277 --end-group. One way this odd construct might arise is
4278 from a wrapper around ld to change library search
4279 behaviour. For example:
4281 exec real_ld --start-group "$@" --end-group
4282 This isn't completely unreasonable so go looking inside a
4283 group statement for insert statements. */
4284 process_insert_statements (&(*s
)->group_statement
.children
.head
);
4286 else if ((*s
)->header
.type
== lang_insert_statement_enum
)
4288 lang_insert_statement_type
*i
= &(*s
)->insert_statement
;
4289 lang_output_section_statement_type
*where
;
4290 lang_statement_union_type
**ptr
;
4291 lang_statement_union_type
*first
;
4293 if (link_info
.non_contiguous_regions
)
4295 einfo (_("warning: INSERT statement in linker script is "
4296 "incompatible with --enable-non-contiguous-regions.\n"));
4299 where
= lang_output_section_find (i
->where
);
4300 if (where
!= NULL
&& i
->is_before
)
4303 where
= where
->prev
;
4304 while (where
!= NULL
&& where
->constraint
< 0);
4308 einfo (_("%F%P: %s not found for insert\n"), i
->where
);
4312 /* Deal with reordering the output section statement list. */
4313 if (last_os
!= NULL
)
4315 asection
*first_sec
, *last_sec
;
4316 struct lang_output_section_statement_struct
**next
;
4318 /* Snip out the output sections we are moving. */
4319 first_os
->prev
->next
= last_os
->next
;
4320 if (last_os
->next
== NULL
)
4322 next
= &first_os
->prev
->next
;
4323 lang_os_list
.tail
= (lang_statement_union_type
**) next
;
4326 last_os
->next
->prev
= first_os
->prev
;
4327 /* Add them in at the new position. */
4328 last_os
->next
= where
->next
;
4329 if (where
->next
== NULL
)
4331 next
= &last_os
->next
;
4332 lang_os_list
.tail
= (lang_statement_union_type
**) next
;
4335 where
->next
->prev
= last_os
;
4336 first_os
->prev
= where
;
4337 where
->next
= first_os
;
4339 /* Move the bfd sections in the same way. */
4342 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
4344 os
->constraint
= -2 - os
->constraint
;
4345 if (os
->bfd_section
!= NULL
4346 && os
->bfd_section
->owner
!= NULL
)
4348 last_sec
= os
->bfd_section
;
4349 if (first_sec
== NULL
)
4350 first_sec
= last_sec
;
4355 if (last_sec
!= NULL
)
4357 asection
*sec
= where
->bfd_section
;
4359 sec
= output_prev_sec_find (where
);
4361 /* The place we want to insert must come after the
4362 sections we are moving. So if we find no
4363 section or if the section is the same as our
4364 last section, then no move is needed. */
4365 if (sec
!= NULL
&& sec
!= last_sec
)
4367 /* Trim them off. */
4368 if (first_sec
->prev
!= NULL
)
4369 first_sec
->prev
->next
= last_sec
->next
;
4371 link_info
.output_bfd
->sections
= last_sec
->next
;
4372 if (last_sec
->next
!= NULL
)
4373 last_sec
->next
->prev
= first_sec
->prev
;
4375 link_info
.output_bfd
->section_last
= first_sec
->prev
;
4377 if (sec
->owner
== NULL
)
4378 /* SEC is the absolute section, from the
4379 first dummy output section statement. Add
4380 back the sections we trimmed off to the
4381 start of the bfd sections. */
4384 last_sec
->next
= sec
->next
;
4386 last_sec
->next
= link_info
.output_bfd
->sections
;
4387 if (last_sec
->next
!= NULL
)
4388 last_sec
->next
->prev
= last_sec
;
4390 link_info
.output_bfd
->section_last
= last_sec
;
4391 first_sec
->prev
= sec
;
4392 if (first_sec
->prev
!= NULL
)
4393 first_sec
->prev
->next
= first_sec
;
4395 link_info
.output_bfd
->sections
= first_sec
;
4400 lang_statement_union_type
*after
= (void *) where
;
4401 if (where
== &lang_os_list
.head
->output_section_statement
4402 && where
->next
== first_os
)
4404 /* PR30155. Handle a corner case where the statement
4405 list is something like the following:
4407 . .data 0x0000000000000000 0x0
4408 . [0x0000000000000000] b = .
4410 . .data 0x0000000000000000 0x0 t.o
4411 . 0x0000000000000000 0x4 LONG 0x0
4412 . INSERT BEFORE .text.start
4413 . [0x0000000000000004] a = .
4414 . .text.start 0x0000000000000000 0x0
4415 . [0x0000000000000000] c = .
4416 . OUTPUT(a.out elf64-x86-64)
4417 Here we do not want to allow insert_os_after to
4418 choose a point inside the list we are moving.
4419 That would lose the list. Instead, let
4420 insert_os_after work from the INSERT, which in this
4421 particular example will result in inserting after
4422 the assignment "a = .". */
4425 ptr
= insert_os_after (after
);
4426 /* Snip everything from the start of the list, up to and
4427 including the insert statement we are currently processing. */
4429 *start
= (*s
)->header
.next
;
4430 /* Add them back where they belong, minus the insert. */
4433 statement_list
.tail
= s
;
4440 s
= &(*s
)->header
.next
;
4443 /* Undo constraint twiddling. */
4444 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
4446 os
->constraint
= -2 - os
->constraint
;
4452 /* An output section might have been removed after its statement was
4453 added. For example, ldemul_before_allocation can remove dynamic
4454 sections if they turn out to be not needed. Clean them up here. */
4457 strip_excluded_output_sections (void)
4459 lang_output_section_statement_type
*os
;
4461 /* Run lang_size_sections (if not already done). */
4462 if (expld
.phase
!= lang_mark_phase_enum
)
4464 expld
.phase
= lang_mark_phase_enum
;
4465 expld
.dataseg
.phase
= exp_seg_none
;
4466 one_lang_size_sections_pass (NULL
, false);
4467 lang_reset_memory_regions ();
4470 for (os
= (void *) lang_os_list
.head
;
4474 asection
*output_section
;
4477 if (os
->constraint
< 0)
4480 output_section
= os
->bfd_section
;
4481 if (output_section
== NULL
)
4484 exclude
= (output_section
->rawsize
== 0
4485 && (output_section
->flags
& SEC_KEEP
) == 0
4486 && !bfd_section_removed_from_list (link_info
.output_bfd
,
4489 /* Some sections have not yet been sized, notably .gnu.version,
4490 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
4491 input sections, so don't drop output sections that have such
4492 input sections unless they are also marked SEC_EXCLUDE. */
4493 if (exclude
&& output_section
->map_head
.s
!= NULL
)
4497 for (s
= output_section
->map_head
.s
; s
!= NULL
; s
= s
->map_head
.s
)
4498 if ((s
->flags
& SEC_EXCLUDE
) == 0
4499 && ((s
->flags
& SEC_LINKER_CREATED
) != 0
4500 || link_info
.emitrelocations
))
4509 /* We don't set bfd_section to NULL since bfd_section of the
4510 removed output section statement may still be used. */
4511 if (!os
->update_dot
)
4513 output_section
->flags
|= SEC_EXCLUDE
;
4514 bfd_section_list_remove (link_info
.output_bfd
, output_section
);
4515 link_info
.output_bfd
->section_count
--;
4520 /* Called from ldwrite to clear out asection.map_head and
4521 asection.map_tail for use as link_orders in ldwrite. */
4524 lang_clear_os_map (void)
4526 lang_output_section_statement_type
*os
;
4528 if (map_head_is_link_order
)
4531 for (os
= (void *) lang_os_list
.head
;
4535 asection
*output_section
;
4537 if (os
->constraint
< 0)
4540 output_section
= os
->bfd_section
;
4541 if (output_section
== NULL
)
4544 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
4545 output_section
->map_head
.link_order
= NULL
;
4546 output_section
->map_tail
.link_order
= NULL
;
4549 /* Stop future calls to lang_add_section from messing with map_head
4550 and map_tail link_order fields. */
4551 map_head_is_link_order
= true;
4555 print_output_section_statement
4556 (lang_output_section_statement_type
*output_section_statement
)
4558 asection
*section
= output_section_statement
->bfd_section
;
4561 if (output_section_statement
!= abs_output_section
)
4563 minfo ("\n%s", output_section_statement
->name
);
4565 if (section
!= NULL
)
4567 print_dot
= section
->vma
;
4569 len
= strlen (output_section_statement
->name
);
4570 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
4575 print_spaces (SECTION_NAME_MAP_LENGTH
- len
);
4577 minfo ("0x%V %W", section
->vma
, TO_ADDR (section
->size
));
4579 if (section
->vma
!= section
->lma
)
4580 minfo (_(" load address 0x%V"), section
->lma
);
4582 if (output_section_statement
->update_dot_tree
!= NULL
)
4583 exp_fold_tree (output_section_statement
->update_dot_tree
,
4584 bfd_abs_section_ptr
, &print_dot
);
4590 print_statement_list (output_section_statement
->children
.head
,
4591 output_section_statement
);
4595 print_assignment (lang_assignment_statement_type
*assignment
,
4596 lang_output_section_statement_type
*output_section
)
4602 print_spaces (SECTION_NAME_MAP_LENGTH
);
4604 if (assignment
->exp
->type
.node_class
== etree_assert
)
4607 tree
= assignment
->exp
->assert_s
.child
;
4611 const char *dst
= assignment
->exp
->assign
.dst
;
4613 is_dot
= (dst
[0] == '.' && dst
[1] == 0);
4614 tree
= assignment
->exp
;
4617 osec
= output_section
->bfd_section
;
4619 osec
= bfd_abs_section_ptr
;
4621 if (assignment
->exp
->type
.node_class
!= etree_provide
)
4622 exp_fold_tree (tree
, osec
, &print_dot
);
4624 expld
.result
.valid_p
= false;
4627 const char *str
= buf
;
4628 if (expld
.result
.valid_p
)
4632 if (assignment
->exp
->type
.node_class
== etree_assert
4634 || expld
.assign_name
!= NULL
)
4636 value
= expld
.result
.value
;
4638 if (expld
.result
.section
!= NULL
)
4639 value
+= expld
.result
.section
->vma
;
4643 bfd_sprintf_vma (link_info
.output_bfd
, buf
+ 2, value
);
4649 struct bfd_link_hash_entry
*h
;
4651 h
= bfd_link_hash_lookup (link_info
.hash
, assignment
->exp
->assign
.dst
,
4652 false, false, true);
4654 && (h
->type
== bfd_link_hash_defined
4655 || h
->type
== bfd_link_hash_defweak
))
4657 value
= h
->u
.def
.value
;
4658 value
+= h
->u
.def
.section
->output_section
->vma
;
4659 value
+= h
->u
.def
.section
->output_offset
;
4664 bfd_sprintf_vma (link_info
.output_bfd
, buf
+ 3, value
);
4668 str
= "[unresolved]";
4673 if (assignment
->exp
->type
.node_class
== etree_provide
)
4678 expld
.assign_name
= NULL
;
4680 fprintf (config
.map_file
, "%-34s", str
);
4681 exp_print_tree (assignment
->exp
);
4686 print_input_statement (lang_input_statement_type
*statm
)
4688 if (statm
->filename
!= NULL
)
4689 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
4692 /* Print all symbols defined in a particular section. This is called
4693 via bfd_link_hash_traverse, or by print_all_symbols. */
4696 print_one_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
)
4698 asection
*sec
= (asection
*) ptr
;
4700 if ((hash_entry
->type
== bfd_link_hash_defined
4701 || hash_entry
->type
== bfd_link_hash_defweak
)
4702 && sec
== hash_entry
->u
.def
.section
)
4704 print_spaces (SECTION_NAME_MAP_LENGTH
);
4706 (hash_entry
->u
.def
.value
4707 + hash_entry
->u
.def
.section
->output_offset
4708 + hash_entry
->u
.def
.section
->output_section
->vma
));
4710 minfo (" %pT\n", hash_entry
->root
.string
);
4717 hash_entry_addr_cmp (const void *a
, const void *b
)
4719 const struct bfd_link_hash_entry
*l
= *(const struct bfd_link_hash_entry
**)a
;
4720 const struct bfd_link_hash_entry
*r
= *(const struct bfd_link_hash_entry
**)b
;
4722 if (l
->u
.def
.value
< r
->u
.def
.value
)
4724 else if (l
->u
.def
.value
> r
->u
.def
.value
)
4731 print_all_symbols (asection
*sec
)
4733 input_section_userdata_type
*ud
= bfd_section_userdata (sec
);
4734 struct map_symbol_def
*def
;
4735 struct bfd_link_hash_entry
**entries
;
4741 *ud
->map_symbol_def_tail
= 0;
4743 /* Sort the symbols by address. */
4744 entries
= (struct bfd_link_hash_entry
**)
4745 obstack_alloc (&map_obstack
,
4746 ud
->map_symbol_def_count
* sizeof (*entries
));
4748 for (i
= 0, def
= ud
->map_symbol_def_head
; def
; def
= def
->next
, i
++)
4749 entries
[i
] = def
->entry
;
4751 qsort (entries
, ud
->map_symbol_def_count
, sizeof (*entries
),
4752 hash_entry_addr_cmp
);
4754 /* Print the symbols. */
4755 for (i
= 0; i
< ud
->map_symbol_def_count
; i
++)
4756 ldemul_print_symbol (entries
[i
], sec
);
4758 obstack_free (&map_obstack
, entries
);
4761 /* Print information about an input section to the map file. */
4764 print_input_section (asection
*i
, bool is_discarded
)
4766 bfd_size_type size
= i
->size
;
4772 minfo (" %s", i
->name
);
4774 len
= 1 + strlen (i
->name
);
4775 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
4780 print_spaces (SECTION_NAME_MAP_LENGTH
- len
);
4782 if (i
->output_section
!= NULL
4783 && i
->output_section
->owner
== link_info
.output_bfd
)
4784 addr
= i
->output_section
->vma
+ i
->output_offset
;
4793 bfd_sprintf_vma (link_info
.output_bfd
, buf
, addr
);
4794 minfo ("0x%s %W %pB\n", buf
, TO_ADDR (size
), i
->owner
);
4796 if (size
!= i
->rawsize
&& i
->rawsize
!= 0)
4798 len
= SECTION_NAME_MAP_LENGTH
+ 3 + strlen (buf
);
4800 minfo (_("%W (size before relaxing)\n"), TO_ADDR (i
->rawsize
));
4803 if (i
->output_section
!= NULL
4804 && i
->output_section
->owner
== link_info
.output_bfd
)
4806 if (link_info
.reduce_memory_overheads
)
4807 bfd_link_hash_traverse (link_info
.hash
, ldemul_print_symbol
, i
);
4809 print_all_symbols (i
);
4811 /* Update print_dot, but make sure that we do not move it
4812 backwards - this could happen if we have overlays and a
4813 later overlay is shorter than an earier one. */
4814 if (addr
+ TO_ADDR (size
) > print_dot
)
4815 print_dot
= addr
+ TO_ADDR (size
);
4820 print_fill_statement (lang_fill_statement_type
*fill
)
4824 fputs (" FILL mask 0x", config
.map_file
);
4825 for (p
= fill
->fill
->data
, size
= fill
->fill
->size
; size
!= 0; p
++, size
--)
4826 fprintf (config
.map_file
, "%02x", *p
);
4827 fputs ("\n", config
.map_file
);
4831 print_data_statement (lang_data_statement_type
*data
)
4837 init_opb (data
->output_section
);
4838 print_spaces (SECTION_NAME_MAP_LENGTH
);
4840 addr
= data
->output_offset
;
4841 if (data
->output_section
!= NULL
)
4842 addr
+= data
->output_section
->vma
;
4870 if (size
< TO_SIZE ((unsigned) 1))
4871 size
= TO_SIZE ((unsigned) 1);
4872 minfo ("0x%V %W %s 0x%v", addr
, TO_ADDR (size
), name
, data
->value
);
4874 if (data
->exp
->type
.node_class
!= etree_value
)
4877 exp_print_tree (data
->exp
);
4882 print_dot
= addr
+ TO_ADDR (size
);
4885 /* Print an address statement. These are generated by options like
4889 print_address_statement (lang_address_statement_type
*address
)
4891 minfo (_("Address of section %s set to "), address
->section_name
);
4892 exp_print_tree (address
->address
);
4896 /* Print a reloc statement. */
4899 print_reloc_statement (lang_reloc_statement_type
*reloc
)
4904 init_opb (reloc
->output_section
);
4905 print_spaces (SECTION_NAME_MAP_LENGTH
);
4907 addr
= reloc
->output_offset
;
4908 if (reloc
->output_section
!= NULL
)
4909 addr
+= reloc
->output_section
->vma
;
4911 size
= bfd_get_reloc_size (reloc
->howto
);
4913 minfo ("0x%V %W RELOC %s ", addr
, TO_ADDR (size
), reloc
->howto
->name
);
4915 if (reloc
->name
!= NULL
)
4916 minfo ("%s+", reloc
->name
);
4918 minfo ("%s+", reloc
->section
->name
);
4920 exp_print_tree (reloc
->addend_exp
);
4924 print_dot
= addr
+ TO_ADDR (size
);
4928 print_padding_statement (lang_padding_statement_type
*s
)
4933 init_opb (s
->output_section
);
4936 len
= sizeof " *fill*" - 1;
4937 print_spaces (SECTION_NAME_MAP_LENGTH
- len
);
4939 addr
= s
->output_offset
;
4940 if (s
->output_section
!= NULL
)
4941 addr
+= s
->output_section
->vma
;
4942 minfo ("0x%V %W ", addr
, TO_ADDR (s
->size
));
4944 if (s
->fill
->size
!= 0)
4948 for (p
= s
->fill
->data
, size
= s
->fill
->size
; size
!= 0; p
++, size
--)
4949 fprintf (config
.map_file
, "%02x", *p
);
4954 print_dot
= addr
+ TO_ADDR (s
->size
);
4958 print_wild_statement (lang_wild_statement_type
*w
,
4959 lang_output_section_statement_type
*os
)
4961 struct wildcard_list
*sec
;
4965 if (w
->exclude_name_list
)
4968 minfo ("EXCLUDE_FILE(%s", w
->exclude_name_list
->name
);
4969 for (tmp
= w
->exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
4970 minfo (" %s", tmp
->name
);
4974 if (w
->filenames_sorted
)
4975 minfo ("SORT_BY_NAME(");
4976 if (w
->filename
!= NULL
)
4977 minfo ("%s", w
->filename
);
4980 if (w
->filenames_sorted
)
4984 for (sec
= w
->section_list
; sec
; sec
= sec
->next
)
4986 int closing_paren
= 0;
4988 switch (sec
->spec
.sorted
)
4994 minfo ("SORT_BY_NAME(");
4999 minfo ("SORT_BY_ALIGNMENT(");
5003 case by_name_alignment
:
5004 minfo ("SORT_BY_NAME(SORT_BY_ALIGNMENT(");
5008 case by_alignment_name
:
5009 minfo ("SORT_BY_ALIGNMENT(SORT_BY_NAME(");
5014 minfo ("SORT_NONE(");
5018 case by_init_priority
:
5019 minfo ("SORT_BY_INIT_PRIORITY(");
5024 if (sec
->spec
.exclude_name_list
!= NULL
)
5027 minfo ("EXCLUDE_FILE(%s", sec
->spec
.exclude_name_list
->name
);
5028 for (tmp
= sec
->spec
.exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
5029 minfo (" %s", tmp
->name
);
5032 if (sec
->spec
.name
!= NULL
)
5033 minfo ("%s", sec
->spec
.name
);
5036 for (;closing_paren
> 0; closing_paren
--)
5045 print_statement_list (w
->children
.head
, os
);
5048 /* Print a group statement. */
5051 print_group (lang_group_statement_type
*s
,
5052 lang_output_section_statement_type
*os
)
5054 fprintf (config
.map_file
, "START GROUP\n");
5055 print_statement_list (s
->children
.head
, os
);
5056 fprintf (config
.map_file
, "END GROUP\n");
5059 /* Print the list of statements in S.
5060 This can be called for any statement type. */
5063 print_statement_list (lang_statement_union_type
*s
,
5064 lang_output_section_statement_type
*os
)
5068 print_statement (s
, os
);
5073 /* Print the first statement in statement list S.
5074 This can be called for any statement type. */
5077 print_statement (lang_statement_union_type
*s
,
5078 lang_output_section_statement_type
*os
)
5080 switch (s
->header
.type
)
5083 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
5086 case lang_constructors_statement_enum
:
5087 if (constructor_list
.head
!= NULL
)
5089 if (constructors_sorted
)
5090 minfo (" SORT (CONSTRUCTORS)\n");
5092 minfo (" CONSTRUCTORS\n");
5093 print_statement_list (constructor_list
.head
, os
);
5096 case lang_wild_statement_enum
:
5097 print_wild_statement (&s
->wild_statement
, os
);
5099 case lang_address_statement_enum
:
5100 print_address_statement (&s
->address_statement
);
5102 case lang_object_symbols_statement_enum
:
5103 minfo (" CREATE_OBJECT_SYMBOLS\n");
5105 case lang_fill_statement_enum
:
5106 print_fill_statement (&s
->fill_statement
);
5108 case lang_data_statement_enum
:
5109 print_data_statement (&s
->data_statement
);
5111 case lang_reloc_statement_enum
:
5112 print_reloc_statement (&s
->reloc_statement
);
5114 case lang_input_section_enum
:
5115 print_input_section (s
->input_section
.section
, false);
5117 case lang_padding_statement_enum
:
5118 print_padding_statement (&s
->padding_statement
);
5120 case lang_output_section_statement_enum
:
5121 print_output_section_statement (&s
->output_section_statement
);
5123 case lang_assignment_statement_enum
:
5124 print_assignment (&s
->assignment_statement
, os
);
5126 case lang_target_statement_enum
:
5127 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
5129 case lang_output_statement_enum
:
5130 minfo ("OUTPUT(%s", s
->output_statement
.name
);
5131 if (output_target
!= NULL
)
5132 minfo (" %s", output_target
);
5135 case lang_input_statement_enum
:
5136 print_input_statement (&s
->input_statement
);
5138 case lang_group_statement_enum
:
5139 print_group (&s
->group_statement
, os
);
5141 case lang_insert_statement_enum
:
5142 minfo ("INSERT %s %s\n",
5143 s
->insert_statement
.is_before
? "BEFORE" : "AFTER",
5144 s
->insert_statement
.where
);
5150 print_statements (void)
5152 print_statement_list (statement_list
.head
, abs_output_section
);
5155 /* Print the first N statements in statement list S to STDERR.
5156 If N == 0, nothing is printed.
5157 If N < 0, the entire list is printed.
5158 Intended to be called from GDB. */
5161 dprint_statement (lang_statement_union_type
*s
, int n
)
5163 FILE *map_save
= config
.map_file
;
5165 config
.map_file
= stderr
;
5168 print_statement_list (s
, abs_output_section
);
5171 while (s
&& --n
>= 0)
5173 print_statement (s
, abs_output_section
);
5178 config
.map_file
= map_save
;
5182 insert_pad (lang_statement_union_type
**ptr
,
5184 bfd_size_type alignment_needed
,
5185 asection
*output_section
,
5188 static fill_type zero_fill
;
5189 lang_statement_union_type
*pad
= NULL
;
5191 if (ptr
!= &statement_list
.head
)
5192 pad
= ((lang_statement_union_type
*)
5193 ((char *) ptr
- offsetof (lang_statement_union_type
, header
.next
)));
5195 && pad
->header
.type
== lang_padding_statement_enum
5196 && pad
->padding_statement
.output_section
== output_section
)
5198 /* Use the existing pad statement. */
5200 else if ((pad
= *ptr
) != NULL
5201 && pad
->header
.type
== lang_padding_statement_enum
5202 && pad
->padding_statement
.output_section
== output_section
)
5204 /* Use the existing pad statement. */
5208 /* Make a new padding statement, linked into existing chain. */
5209 pad
= stat_alloc (sizeof (lang_padding_statement_type
));
5210 pad
->header
.next
= *ptr
;
5212 pad
->header
.type
= lang_padding_statement_enum
;
5213 pad
->padding_statement
.output_section
= output_section
;
5216 pad
->padding_statement
.fill
= fill
;
5218 pad
->padding_statement
.output_offset
= dot
- output_section
->vma
;
5219 pad
->padding_statement
.size
= alignment_needed
;
5220 if (!(output_section
->flags
& SEC_FIXED_SIZE
))
5221 output_section
->size
= TO_SIZE (dot
+ TO_ADDR (alignment_needed
)
5222 - output_section
->vma
);
5225 /* Work out how much this section will move the dot point. */
5229 (lang_statement_union_type
**this_ptr
,
5230 lang_output_section_statement_type
*output_section_statement
,
5235 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
5236 asection
*i
= is
->section
;
5237 asection
*o
= output_section_statement
->bfd_section
;
5240 if (link_info
.non_contiguous_regions
)
5242 /* If the input section I has already been successfully assigned
5243 to an output section other than O, don't bother with it and
5244 let the caller remove it from the list. Keep processing in
5245 case we have already handled O, because the repeated passes
5246 have reinitialized its size. */
5247 if (i
->already_assigned
&& i
->already_assigned
!= o
)
5254 if (i
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
5255 i
->output_offset
= i
->vma
- o
->vma
;
5256 else if (((i
->flags
& SEC_EXCLUDE
) != 0)
5257 || output_section_statement
->ignored
)
5258 i
->output_offset
= dot
- o
->vma
;
5261 bfd_size_type alignment_needed
;
5263 /* Align this section first to the input sections requirement,
5264 then to the output section's requirement. If this alignment
5265 is greater than any seen before, then record it too. Perform
5266 the alignment by inserting a magic 'padding' statement. */
5268 if (output_section_statement
->subsection_alignment
!= NULL
)
5270 = exp_get_power (output_section_statement
->subsection_alignment
,
5271 "subsection alignment");
5273 if (o
->alignment_power
< i
->alignment_power
)
5274 o
->alignment_power
= i
->alignment_power
;
5276 alignment_needed
= align_power (dot
, i
->alignment_power
) - dot
;
5278 if (alignment_needed
!= 0)
5280 insert_pad (this_ptr
, fill
, TO_SIZE (alignment_needed
), o
, dot
);
5281 dot
+= alignment_needed
;
5284 if (link_info
.non_contiguous_regions
)
5286 /* If I would overflow O, let the caller remove I from the
5288 if (output_section_statement
->region
)
5290 bfd_vma end
= output_section_statement
->region
->origin
5291 + output_section_statement
->region
->length
;
5293 if (dot
+ TO_ADDR (i
->size
) > end
)
5295 if (i
->flags
& SEC_LINKER_CREATED
)
5296 einfo (_("%F%P: Output section `%pA' not large enough for "
5297 "the linker-created stubs section `%pA'.\n"),
5298 i
->output_section
, i
);
5300 if (i
->rawsize
&& i
->rawsize
!= i
->size
)
5301 einfo (_("%F%P: Relaxation not supported with "
5302 "--enable-non-contiguous-regions (section `%pA' "
5303 "would overflow `%pA' after it changed size).\n"),
5304 i
, i
->output_section
);
5308 i
->output_section
= NULL
;
5314 /* Remember where in the output section this input section goes. */
5315 i
->output_offset
= dot
- o
->vma
;
5317 /* Mark how big the output section must be to contain this now. */
5318 dot
+= TO_ADDR (i
->size
);
5319 if (!(o
->flags
& SEC_FIXED_SIZE
))
5320 o
->size
= TO_SIZE (dot
- o
->vma
);
5322 if (link_info
.non_contiguous_regions
)
5324 /* Record that I was successfully assigned to O, and update
5325 its actual output section too. */
5326 i
->already_assigned
= o
;
5327 i
->output_section
= o
;
5341 sort_sections_by_lma (const void *arg1
, const void *arg2
)
5343 const asection
*sec1
= ((const struct check_sec
*) arg1
)->sec
;
5344 const asection
*sec2
= ((const struct check_sec
*) arg2
)->sec
;
5346 if (sec1
->lma
< sec2
->lma
)
5348 else if (sec1
->lma
> sec2
->lma
)
5350 else if (sec1
->id
< sec2
->id
)
5352 else if (sec1
->id
> sec2
->id
)
5359 sort_sections_by_vma (const void *arg1
, const void *arg2
)
5361 const asection
*sec1
= ((const struct check_sec
*) arg1
)->sec
;
5362 const asection
*sec2
= ((const struct check_sec
*) arg2
)->sec
;
5364 if (sec1
->vma
< sec2
->vma
)
5366 else if (sec1
->vma
> sec2
->vma
)
5368 else if (sec1
->id
< sec2
->id
)
5370 else if (sec1
->id
> sec2
->id
)
5376 #define IS_TBSS(s) \
5377 ((s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == SEC_THREAD_LOCAL)
5379 #define IGNORE_SECTION(s) \
5380 ((s->flags & SEC_ALLOC) == 0 || IS_TBSS (s))
5382 /* Check to see if any allocated sections overlap with other allocated
5383 sections. This can happen if a linker script specifies the output
5384 section addresses of the two sections. Also check whether any memory
5385 region has overflowed. */
5388 lang_check_section_addresses (void)
5391 struct check_sec
*sections
;
5396 bfd_vma p_start
= 0;
5398 lang_memory_region_type
*m
;
5401 /* Detect address space overflow on allocated sections. */
5402 addr_mask
= ((bfd_vma
) 1 <<
5403 (bfd_arch_bits_per_address (link_info
.output_bfd
) - 1)) - 1;
5404 addr_mask
= (addr_mask
<< 1) + 1;
5405 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5406 if ((s
->flags
& SEC_ALLOC
) != 0)
5408 s_end
= (s
->vma
+ s
->size
) & addr_mask
;
5409 if (s_end
!= 0 && s_end
< (s
->vma
& addr_mask
))
5410 einfo (_("%X%P: section %s VMA wraps around address space\n"),
5414 s_end
= (s
->lma
+ s
->size
) & addr_mask
;
5415 if (s_end
!= 0 && s_end
< (s
->lma
& addr_mask
))
5416 einfo (_("%X%P: section %s LMA wraps around address space\n"),
5421 if (bfd_count_sections (link_info
.output_bfd
) <= 1)
5424 count
= bfd_count_sections (link_info
.output_bfd
);
5425 sections
= XNEWVEC (struct check_sec
, count
);
5427 /* Scan all sections in the output list. */
5429 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5431 if (IGNORE_SECTION (s
)
5435 sections
[count
].sec
= s
;
5436 sections
[count
].warned
= false;
5446 qsort (sections
, count
, sizeof (*sections
), sort_sections_by_lma
);
5448 /* First check section LMAs. There should be no overlap of LMAs on
5449 loadable sections, even with overlays. */
5450 for (p
= NULL
, i
= 0; i
< count
; i
++)
5452 s
= sections
[i
].sec
;
5454 if ((s
->flags
& SEC_LOAD
) != 0)
5457 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
5459 /* Look for an overlap. We have sorted sections by lma, so
5460 we know that s_start >= p_start. Besides the obvious
5461 case of overlap when the current section starts before
5462 the previous one ends, we also must have overlap if the
5463 previous section wraps around the address space. */
5465 && (s_start
<= p_end
5466 || p_end
< p_start
))
5468 einfo (_("%X%P: section %s LMA [%V,%V]"
5469 " overlaps section %s LMA [%V,%V]\n"),
5470 s
->name
, s_start
, s_end
, p
->name
, p_start
, p_end
);
5471 sections
[i
].warned
= true;
5479 /* If any non-zero size allocated section (excluding tbss) starts at
5480 exactly the same VMA as another such section, then we have
5481 overlays. Overlays generated by the OVERLAY keyword will have
5482 this property. It is possible to intentionally generate overlays
5483 that fail this test, but it would be unusual. */
5484 qsort (sections
, count
, sizeof (*sections
), sort_sections_by_vma
);
5486 p_start
= sections
[0].sec
->vma
;
5487 for (i
= 1; i
< count
; i
++)
5489 s_start
= sections
[i
].sec
->vma
;
5490 if (p_start
== s_start
)
5498 /* Now check section VMAs if no overlays were detected. */
5501 for (p
= NULL
, i
= 0; i
< count
; i
++)
5503 s
= sections
[i
].sec
;
5506 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
5509 && !sections
[i
].warned
5510 && (s_start
<= p_end
5511 || p_end
< p_start
))
5512 einfo (_("%X%P: section %s VMA [%V,%V]"
5513 " overlaps section %s VMA [%V,%V]\n"),
5514 s
->name
, s_start
, s_end
, p
->name
, p_start
, p_end
);
5523 /* If any memory region has overflowed, report by how much.
5524 We do not issue this diagnostic for regions that had sections
5525 explicitly placed outside their bounds; os_region_check's
5526 diagnostics are adequate for that case.
5528 FIXME: It is conceivable that m->current - (m->origin + m->length)
5529 might overflow a 32-bit integer. There is, alas, no way to print
5530 a bfd_vma quantity in decimal. */
5531 for (m
= lang_memory_region_list
; m
; m
= m
->next
)
5532 if (m
->had_full_message
)
5534 unsigned long over
= m
->current
- (m
->origin
+ m
->length
);
5535 einfo (ngettext ("%X%P: region `%s' overflowed by %lu byte\n",
5536 "%X%P: region `%s' overflowed by %lu bytes\n",
5538 m
->name_list
.name
, over
);
5542 /* Make sure the new address is within the region. We explicitly permit the
5543 current address to be at the exact end of the region when the address is
5544 non-zero, in case the region is at the end of addressable memory and the
5545 calculation wraps around. */
5548 os_region_check (lang_output_section_statement_type
*os
,
5549 lang_memory_region_type
*region
,
5553 if ((region
->current
< region
->origin
5554 || (region
->current
- region
->origin
> region
->length
))
5555 && ((region
->current
!= region
->origin
+ region
->length
)
5560 einfo (_("%X%P: address 0x%v of %pB section `%s'"
5561 " is not within region `%s'\n"),
5563 os
->bfd_section
->owner
,
5564 os
->bfd_section
->name
,
5565 region
->name_list
.name
);
5567 else if (!region
->had_full_message
)
5569 region
->had_full_message
= true;
5571 einfo (_("%X%P: %pB section `%s' will not fit in region `%s'\n"),
5572 os
->bfd_section
->owner
,
5573 os
->bfd_section
->name
,
5574 region
->name_list
.name
);
5580 ldlang_check_relro_region (lang_statement_union_type
*s
)
5582 seg_align_type
*seg
= &expld
.dataseg
;
5584 if (seg
->relro
== exp_seg_relro_start
)
5586 if (!seg
->relro_start_stat
)
5587 seg
->relro_start_stat
= s
;
5590 ASSERT (seg
->relro_start_stat
== s
);
5593 else if (seg
->relro
== exp_seg_relro_end
)
5595 if (!seg
->relro_end_stat
)
5596 seg
->relro_end_stat
= s
;
5599 ASSERT (seg
->relro_end_stat
== s
);
5604 /* Set the sizes for all the output sections. */
5607 lang_size_sections_1
5608 (lang_statement_union_type
**prev
,
5609 lang_output_section_statement_type
*output_section_statement
,
5615 lang_statement_union_type
*s
;
5616 lang_statement_union_type
*prev_s
= NULL
;
5617 bool removed_prev_s
= false;
5619 /* Size up the sections from their constituent parts. */
5620 for (s
= *prev
; s
!= NULL
; prev_s
= s
, s
= s
->header
.next
)
5622 bool removed
= false;
5624 switch (s
->header
.type
)
5626 case lang_output_section_statement_enum
:
5628 bfd_vma newdot
, after
, dotdelta
;
5629 lang_output_section_statement_type
*os
;
5630 lang_memory_region_type
*r
;
5631 int section_alignment
= 0;
5633 os
= &s
->output_section_statement
;
5634 init_opb (os
->bfd_section
);
5635 if (os
->constraint
== -1)
5638 /* FIXME: We shouldn't need to zero section vmas for ld -r
5639 here, in lang_insert_orphan, or in the default linker scripts.
5640 This is covering for coff backend linker bugs. See PR6945. */
5641 if (os
->addr_tree
== NULL
5642 && bfd_link_relocatable (&link_info
)
5643 && (bfd_get_flavour (link_info
.output_bfd
)
5644 == bfd_target_coff_flavour
))
5645 os
->addr_tree
= exp_intop (0);
5646 if (os
->addr_tree
!= NULL
)
5648 exp_fold_tree (os
->addr_tree
, bfd_abs_section_ptr
, &dot
);
5650 if (expld
.result
.valid_p
)
5652 dot
= expld
.result
.value
;
5653 if (expld
.result
.section
!= NULL
)
5654 dot
+= expld
.result
.section
->vma
;
5656 else if (expld
.phase
!= lang_mark_phase_enum
)
5657 einfo (_("%F%P:%pS: non constant or forward reference"
5658 " address expression for section %s\n"),
5659 os
->addr_tree
, os
->name
);
5662 if (os
->bfd_section
== NULL
)
5663 /* This section was removed or never actually created. */
5666 /* If this is a COFF shared library section, use the size and
5667 address from the input section. FIXME: This is COFF
5668 specific; it would be cleaner if there were some other way
5669 to do this, but nothing simple comes to mind. */
5670 if (((bfd_get_flavour (link_info
.output_bfd
)
5671 == bfd_target_ecoff_flavour
)
5672 || (bfd_get_flavour (link_info
.output_bfd
)
5673 == bfd_target_coff_flavour
))
5674 && (os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
5678 if (os
->children
.head
== NULL
5679 || os
->children
.head
->header
.next
!= NULL
5680 || (os
->children
.head
->header
.type
5681 != lang_input_section_enum
))
5682 einfo (_("%X%P: internal error on COFF shared library"
5683 " section %s\n"), os
->name
);
5685 input
= os
->children
.head
->input_section
.section
;
5686 bfd_set_section_vma (os
->bfd_section
,
5687 bfd_section_vma (input
));
5688 if (!(os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
5689 os
->bfd_section
->size
= input
->size
;
5695 if (bfd_is_abs_section (os
->bfd_section
))
5697 /* No matter what happens, an abs section starts at zero. */
5698 ASSERT (os
->bfd_section
->vma
== 0);
5702 if (os
->addr_tree
== NULL
)
5704 /* No address specified for this section, get one
5705 from the region specification. */
5706 if (os
->region
== NULL
5707 || ((os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))
5708 && os
->region
->name_list
.name
[0] == '*'
5709 && strcmp (os
->region
->name_list
.name
,
5710 DEFAULT_MEMORY_REGION
) == 0))
5712 os
->region
= lang_memory_default (os
->bfd_section
);
5715 /* If a loadable section is using the default memory
5716 region, and some non default memory regions were
5717 defined, issue an error message. */
5719 && !IGNORE_SECTION (os
->bfd_section
)
5720 && !bfd_link_relocatable (&link_info
)
5722 && strcmp (os
->region
->name_list
.name
,
5723 DEFAULT_MEMORY_REGION
) == 0
5724 && lang_memory_region_list
!= NULL
5725 && (strcmp (lang_memory_region_list
->name_list
.name
,
5726 DEFAULT_MEMORY_REGION
) != 0
5727 || lang_memory_region_list
->next
!= NULL
)
5728 && lang_sizing_iteration
== 1)
5730 /* By default this is an error rather than just a
5731 warning because if we allocate the section to the
5732 default memory region we can end up creating an
5733 excessively large binary, or even seg faulting when
5734 attempting to perform a negative seek. See
5735 sources.redhat.com/ml/binutils/2003-04/msg00423.html
5736 for an example of this. This behaviour can be
5737 overridden by the using the --no-check-sections
5739 if (command_line
.check_section_addresses
)
5740 einfo (_("%F%P: error: no memory region specified"
5741 " for loadable section `%s'\n"),
5742 bfd_section_name (os
->bfd_section
));
5744 einfo (_("%P: warning: no memory region specified"
5745 " for loadable section `%s'\n"),
5746 bfd_section_name (os
->bfd_section
));
5749 newdot
= os
->region
->current
;
5750 section_alignment
= os
->bfd_section
->alignment_power
;
5753 section_alignment
= exp_get_power (os
->section_alignment
,
5754 "section alignment");
5756 /* Align to what the section needs. */
5757 if (section_alignment
> 0)
5759 bfd_vma savedot
= newdot
;
5762 newdot
= align_power (newdot
, section_alignment
);
5763 dotdelta
= newdot
- savedot
;
5765 if (lang_sizing_iteration
== 1)
5767 else if (lang_sizing_iteration
> 1)
5769 /* Only report adjustments that would change
5770 alignment from what we have already reported. */
5771 diff
= newdot
- os
->bfd_section
->vma
;
5772 if (!(diff
& (((bfd_vma
) 1 << section_alignment
) - 1)))
5776 && (config
.warn_section_align
5777 || os
->addr_tree
!= NULL
))
5778 einfo (_("%P: warning: "
5779 "start of section %s changed by %ld\n"),
5780 os
->name
, (long) diff
);
5783 bfd_set_section_vma (os
->bfd_section
, newdot
);
5785 os
->bfd_section
->output_offset
= 0;
5788 lang_size_sections_1 (&os
->children
.head
, os
,
5789 os
->fill
, newdot
, relax
, check_regions
);
5791 os
->processed_vma
= true;
5793 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
5794 /* Except for some special linker created sections,
5795 no output section should change from zero size
5796 after strip_excluded_output_sections. A non-zero
5797 size on an ignored section indicates that some
5798 input section was not sized early enough. */
5799 ASSERT (os
->bfd_section
->size
== 0);
5802 dot
= os
->bfd_section
->vma
;
5804 /* Put the section within the requested block size, or
5805 align at the block boundary. */
5807 + TO_ADDR (os
->bfd_section
->size
)
5808 + os
->block_value
- 1)
5809 & - (bfd_vma
) os
->block_value
);
5811 if (!(os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
5812 os
->bfd_section
->size
= TO_SIZE (after
5813 - os
->bfd_section
->vma
);
5816 /* Set section lma. */
5819 r
= lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, false);
5823 bfd_vma lma
= exp_get_abs_int (os
->load_base
, 0, "load base");
5824 os
->bfd_section
->lma
= lma
;
5826 else if (os
->lma_region
!= NULL
)
5828 bfd_vma lma
= os
->lma_region
->current
;
5830 if (os
->align_lma_with_input
)
5834 /* When LMA_REGION is the same as REGION, align the LMA
5835 as we did for the VMA, possibly including alignment
5836 from the bfd section. If a different region, then
5837 only align according to the value in the output
5839 if (os
->lma_region
!= os
->region
)
5840 section_alignment
= exp_get_power (os
->section_alignment
,
5841 "section alignment");
5842 if (section_alignment
> 0)
5843 lma
= align_power (lma
, section_alignment
);
5845 os
->bfd_section
->lma
= lma
;
5847 else if (r
->last_os
!= NULL
5848 && (os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
5853 last
= r
->last_os
->output_section_statement
.bfd_section
;
5855 /* A backwards move of dot should be accompanied by
5856 an explicit assignment to the section LMA (ie.
5857 os->load_base set) because backwards moves can
5858 create overlapping LMAs. */
5860 && os
->bfd_section
->size
!= 0
5861 && dot
+ TO_ADDR (os
->bfd_section
->size
) <= last
->vma
)
5863 /* If dot moved backwards then leave lma equal to
5864 vma. This is the old default lma, which might
5865 just happen to work when the backwards move is
5866 sufficiently large. Nag if this changes anything,
5867 so people can fix their linker scripts. */
5869 if (last
->vma
!= last
->lma
)
5870 einfo (_("%P: warning: dot moved backwards "
5871 "before `%s'\n"), os
->name
);
5875 /* If this is an overlay, set the current lma to that
5876 at the end of the previous section. */
5877 if (os
->sectype
== overlay_section
)
5878 lma
= last
->lma
+ TO_ADDR (last
->size
);
5880 /* Otherwise, keep the same lma to vma relationship
5881 as the previous section. */
5883 lma
= os
->bfd_section
->vma
+ last
->lma
- last
->vma
;
5885 if (section_alignment
> 0)
5886 lma
= align_power (lma
, section_alignment
);
5887 os
->bfd_section
->lma
= lma
;
5890 os
->processed_lma
= true;
5892 /* Keep track of normal sections using the default
5893 lma region. We use this to set the lma for
5894 following sections. Overlays or other linker
5895 script assignment to lma might mean that the
5896 default lma == vma is incorrect.
5897 To avoid warnings about dot moving backwards when using
5898 -Ttext, don't start tracking sections until we find one
5899 of non-zero size or with lma set differently to vma.
5900 Do this tracking before we short-cut the loop so that we
5901 track changes for the case where the section size is zero,
5902 but the lma is set differently to the vma. This is
5903 important, if an orphan section is placed after an
5904 otherwise empty output section that has an explicit lma
5905 set, we want that lma reflected in the orphans lma. */
5906 if (((!IGNORE_SECTION (os
->bfd_section
)
5907 && (os
->bfd_section
->size
!= 0
5908 || (r
->last_os
== NULL
5909 && os
->bfd_section
->vma
!= os
->bfd_section
->lma
)
5910 || (r
->last_os
!= NULL
5911 && dot
>= (r
->last_os
->output_section_statement
5912 .bfd_section
->vma
))))
5913 || os
->sectype
== first_overlay_section
)
5914 && os
->lma_region
== NULL
5915 && !bfd_link_relocatable (&link_info
))
5918 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
5921 /* .tbss sections effectively have zero size. */
5922 if (!IS_TBSS (os
->bfd_section
)
5923 || bfd_link_relocatable (&link_info
))
5924 dotdelta
= TO_ADDR (os
->bfd_section
->size
);
5929 if (os
->update_dot_tree
!= 0)
5930 exp_fold_tree (os
->update_dot_tree
, bfd_abs_section_ptr
, &dot
);
5932 /* Update dot in the region ?
5933 We only do this if the section is going to be allocated,
5934 since unallocated sections do not contribute to the region's
5935 overall size in memory. */
5936 if (os
->region
!= NULL
5937 && (os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
)))
5939 os
->region
->current
= dot
;
5942 /* Make sure the new address is within the region. */
5943 os_region_check (os
, os
->region
, os
->addr_tree
,
5944 os
->bfd_section
->vma
);
5946 if (os
->lma_region
!= NULL
&& os
->lma_region
!= os
->region
5947 && ((os
->bfd_section
->flags
& SEC_LOAD
)
5948 || os
->align_lma_with_input
))
5950 os
->lma_region
->current
= os
->bfd_section
->lma
+ dotdelta
;
5953 os_region_check (os
, os
->lma_region
, NULL
,
5954 os
->bfd_section
->lma
);
5960 case lang_constructors_statement_enum
:
5961 dot
= lang_size_sections_1 (&constructor_list
.head
,
5962 output_section_statement
,
5963 fill
, dot
, relax
, check_regions
);
5966 case lang_data_statement_enum
:
5968 unsigned int size
= 0;
5970 s
->data_statement
.output_offset
=
5971 dot
- output_section_statement
->bfd_section
->vma
;
5972 s
->data_statement
.output_section
=
5973 output_section_statement
->bfd_section
;
5975 /* We might refer to provided symbols in the expression, and
5976 need to mark them as needed. */
5977 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
5979 switch (s
->data_statement
.type
)
5997 if (size
< TO_SIZE ((unsigned) 1))
5998 size
= TO_SIZE ((unsigned) 1);
5999 dot
+= TO_ADDR (size
);
6000 if (!(output_section_statement
->bfd_section
->flags
6002 output_section_statement
->bfd_section
->size
6003 = TO_SIZE (dot
- output_section_statement
->bfd_section
->vma
);
6008 case lang_reloc_statement_enum
:
6012 s
->reloc_statement
.output_offset
=
6013 dot
- output_section_statement
->bfd_section
->vma
;
6014 s
->reloc_statement
.output_section
=
6015 output_section_statement
->bfd_section
;
6016 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
6017 dot
+= TO_ADDR (size
);
6018 if (!(output_section_statement
->bfd_section
->flags
6020 output_section_statement
->bfd_section
->size
6021 = TO_SIZE (dot
- output_section_statement
->bfd_section
->vma
);
6025 case lang_wild_statement_enum
:
6026 dot
= lang_size_sections_1 (&s
->wild_statement
.children
.head
,
6027 output_section_statement
,
6028 fill
, dot
, relax
, check_regions
);
6031 case lang_object_symbols_statement_enum
:
6032 link_info
.create_object_symbols_section
6033 = output_section_statement
->bfd_section
;
6034 output_section_statement
->bfd_section
->flags
|= SEC_KEEP
;
6037 case lang_output_statement_enum
:
6038 case lang_target_statement_enum
:
6041 case lang_input_section_enum
:
6045 i
= s
->input_section
.section
;
6050 if (!bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
6051 einfo (_("%F%P: can't relax section: %E\n"));
6055 dot
= size_input_section (prev
, output_section_statement
,
6056 fill
, &removed
, dot
);
6060 case lang_input_statement_enum
:
6063 case lang_fill_statement_enum
:
6064 s
->fill_statement
.output_section
=
6065 output_section_statement
->bfd_section
;
6067 fill
= s
->fill_statement
.fill
;
6070 case lang_assignment_statement_enum
:
6072 bfd_vma newdot
= dot
;
6073 etree_type
*tree
= s
->assignment_statement
.exp
;
6075 expld
.dataseg
.relro
= exp_seg_relro_none
;
6077 exp_fold_tree (tree
,
6078 output_section_statement
->bfd_section
,
6081 ldlang_check_relro_region (s
);
6083 expld
.dataseg
.relro
= exp_seg_relro_none
;
6085 /* This symbol may be relative to this section. */
6086 if ((tree
->type
.node_class
== etree_provided
6087 || tree
->type
.node_class
== etree_assign
)
6088 && (tree
->assign
.dst
[0] != '.'
6089 || tree
->assign
.dst
[1] != '\0'))
6090 output_section_statement
->update_dot
= 1;
6092 if (!output_section_statement
->ignored
)
6094 if (output_section_statement
== abs_output_section
)
6096 /* If we don't have an output section, then just adjust
6097 the default memory address. */
6098 lang_memory_region_lookup (DEFAULT_MEMORY_REGION
,
6099 false)->current
= newdot
;
6101 else if (newdot
!= dot
)
6103 /* Insert a pad after this statement. We can't
6104 put the pad before when relaxing, in case the
6105 assignment references dot. */
6106 insert_pad (&s
->header
.next
, fill
, TO_SIZE (newdot
- dot
),
6107 output_section_statement
->bfd_section
, dot
);
6109 /* Don't neuter the pad below when relaxing. */
6112 /* If dot is advanced, this implies that the section
6113 should have space allocated to it, unless the
6114 user has explicitly stated that the section
6115 should not be allocated. */
6116 if (output_section_statement
->sectype
!= noalloc_section
6117 && (output_section_statement
->sectype
!= noload_section
6118 || (bfd_get_flavour (link_info
.output_bfd
)
6119 == bfd_target_elf_flavour
)))
6120 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
;
6127 case lang_padding_statement_enum
:
6128 /* If this is the first time lang_size_sections is called,
6129 we won't have any padding statements. If this is the
6130 second or later passes when relaxing, we should allow
6131 padding to shrink. If padding is needed on this pass, it
6132 will be added back in. */
6133 s
->padding_statement
.size
= 0;
6135 /* Make sure output_offset is valid. If relaxation shrinks
6136 the section and this pad isn't needed, it's possible to
6137 have output_offset larger than the final size of the
6138 section. bfd_set_section_contents will complain even for
6139 a pad size of zero. */
6140 s
->padding_statement
.output_offset
6141 = dot
- output_section_statement
->bfd_section
->vma
;
6144 case lang_group_statement_enum
:
6145 dot
= lang_size_sections_1 (&s
->group_statement
.children
.head
,
6146 output_section_statement
,
6147 fill
, dot
, relax
, check_regions
);
6150 case lang_insert_statement_enum
:
6153 /* We can only get here when relaxing is turned on. */
6154 case lang_address_statement_enum
:
6162 /* If an input section doesn't fit in the current output
6163 section, remove it from the list. Handle the case where we
6164 have to remove an input_section statement here: there is a
6165 special case to remove the first element of the list. */
6166 if (link_info
.non_contiguous_regions
&& removed
)
6168 /* If we removed the first element during the previous
6169 iteration, override the loop assignment of prev_s. */
6175 /* If there was a real previous input section, just skip
6177 prev_s
->header
.next
=s
->header
.next
;
6179 removed_prev_s
= false;
6183 /* Remove the first input section of the list. */
6184 *prev
= s
->header
.next
;
6185 removed_prev_s
= true;
6188 /* Move to next element, unless we removed the head of the
6190 if (!removed_prev_s
)
6191 prev
= &s
->header
.next
;
6195 prev
= &s
->header
.next
;
6196 removed_prev_s
= false;
6202 /* Callback routine that is used in _bfd_elf_map_sections_to_segments.
6203 The BFD library has set NEW_SEGMENT to TRUE iff it thinks that
6204 CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different
6205 segments. We are allowed an opportunity to override this decision. */
6208 ldlang_override_segment_assignment (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6209 bfd
*abfd ATTRIBUTE_UNUSED
,
6210 asection
*current_section
,
6211 asection
*previous_section
,
6214 lang_output_section_statement_type
*cur
;
6215 lang_output_section_statement_type
*prev
;
6217 /* The checks below are only necessary when the BFD library has decided
6218 that the two sections ought to be placed into the same segment. */
6222 /* Paranoia checks. */
6223 if (current_section
== NULL
|| previous_section
== NULL
)
6226 /* If this flag is set, the target never wants code and non-code
6227 sections comingled in the same segment. */
6228 if (config
.separate_code
6229 && ((current_section
->flags
^ previous_section
->flags
) & SEC_CODE
))
6232 /* Find the memory regions associated with the two sections.
6233 We call lang_output_section_find() here rather than scanning the list
6234 of output sections looking for a matching section pointer because if
6235 we have a large number of sections then a hash lookup is faster. */
6236 cur
= lang_output_section_find (current_section
->name
);
6237 prev
= lang_output_section_find (previous_section
->name
);
6239 /* More paranoia. */
6240 if (cur
== NULL
|| prev
== NULL
)
6243 /* If the regions are different then force the sections to live in
6244 different segments. See the email thread starting at the following
6245 URL for the reasons why this is necessary:
6246 http://sourceware.org/ml/binutils/2007-02/msg00216.html */
6247 return cur
->region
!= prev
->region
;
6251 one_lang_size_sections_pass (bool *relax
, bool check_regions
)
6253 lang_statement_iteration
++;
6254 if (expld
.phase
!= lang_mark_phase_enum
)
6255 lang_sizing_iteration
++;
6256 lang_size_sections_1 (&statement_list
.head
, abs_output_section
,
6257 0, 0, relax
, check_regions
);
6261 lang_size_segment (void)
6263 /* If XXX_SEGMENT_ALIGN XXX_SEGMENT_END pair was seen, check whether
6264 a page could be saved in the data segment. */
6265 seg_align_type
*seg
= &expld
.dataseg
;
6266 bfd_vma first
, last
;
6268 first
= -seg
->base
& (seg
->commonpagesize
- 1);
6269 last
= seg
->end
& (seg
->commonpagesize
- 1);
6271 && ((seg
->base
& ~(seg
->commonpagesize
- 1))
6272 != (seg
->end
& ~(seg
->commonpagesize
- 1)))
6273 && first
+ last
<= seg
->commonpagesize
)
6275 seg
->phase
= exp_seg_adjust
;
6279 seg
->phase
= exp_seg_done
;
6284 lang_size_relro_segment_1 (void)
6286 seg_align_type
*seg
= &expld
.dataseg
;
6287 bfd_vma relro_end
, desired_end
;
6290 /* Compute the expected PT_GNU_RELRO/PT_LOAD segment end. */
6291 relro_end
= (seg
->relro_end
+ seg
->relropagesize
- 1) & -seg
->relropagesize
;
6293 /* Adjust by the offset arg of XXX_SEGMENT_RELRO_END. */
6294 desired_end
= relro_end
- seg
->relro_offset
;
6296 /* For sections in the relro segment.. */
6297 for (sec
= link_info
.output_bfd
->section_last
; sec
; sec
= sec
->prev
)
6298 if ((sec
->flags
& SEC_ALLOC
) != 0
6299 && sec
->vma
>= seg
->base
6300 && sec
->vma
< seg
->relro_end
- seg
->relro_offset
)
6302 /* Where do we want to put this section so that it ends as
6304 bfd_vma start
, end
, bump
;
6306 end
= start
= sec
->vma
;
6308 end
+= TO_ADDR (sec
->size
);
6309 bump
= desired_end
- end
;
6310 /* We'd like to increase START by BUMP, but we must heed
6311 alignment so the increase might be less than optimum. */
6313 start
&= ~(((bfd_vma
) 1 << sec
->alignment_power
) - 1);
6314 /* This is now the desired end for the previous section. */
6315 desired_end
= start
;
6318 seg
->phase
= exp_seg_relro_adjust
;
6319 ASSERT (desired_end
>= seg
->base
);
6320 seg
->base
= desired_end
;
6325 lang_size_relro_segment (bool *relax
, bool check_regions
)
6327 bool do_reset
= false;
6329 if (link_info
.relro
&& expld
.dataseg
.relro_end
)
6331 bfd_vma data_initial_base
= expld
.dataseg
.base
;
6332 bfd_vma data_relro_end
= lang_size_relro_segment_1 ();
6334 lang_reset_memory_regions ();
6335 one_lang_size_sections_pass (relax
, check_regions
);
6337 /* Assignments to dot, or to output section address in a user
6338 script have increased padding over the original. Revert. */
6339 if (expld
.dataseg
.relro_end
> data_relro_end
)
6341 expld
.dataseg
.base
= data_initial_base
;
6345 else if (lang_size_segment ())
6352 lang_size_sections (bool *relax
, bool check_regions
)
6354 expld
.phase
= lang_allocating_phase_enum
;
6355 expld
.dataseg
.phase
= exp_seg_none
;
6357 one_lang_size_sections_pass (relax
, check_regions
);
6359 if (expld
.dataseg
.phase
!= exp_seg_end_seen
)
6360 expld
.dataseg
.phase
= exp_seg_done
;
6362 if (expld
.dataseg
.phase
== exp_seg_end_seen
)
6365 = lang_size_relro_segment (relax
, check_regions
);
6369 lang_reset_memory_regions ();
6370 one_lang_size_sections_pass (relax
, check_regions
);
6373 if (link_info
.relro
&& expld
.dataseg
.relro_end
)
6375 link_info
.relro_start
= expld
.dataseg
.base
;
6376 link_info
.relro_end
= expld
.dataseg
.relro_end
;
6381 static lang_output_section_statement_type
*current_section
;
6382 static lang_assignment_statement_type
*current_assign
;
6383 static bool prefer_next_section
;
6385 /* Worker function for lang_do_assignments. Recursiveness goes here. */
6388 lang_do_assignments_1 (lang_statement_union_type
*s
,
6389 lang_output_section_statement_type
*current_os
,
6394 for (; s
!= NULL
; s
= s
->header
.next
)
6396 switch (s
->header
.type
)
6398 case lang_constructors_statement_enum
:
6399 dot
= lang_do_assignments_1 (constructor_list
.head
,
6400 current_os
, fill
, dot
, found_end
);
6403 case lang_output_section_statement_enum
:
6405 lang_output_section_statement_type
*os
;
6408 os
= &(s
->output_section_statement
);
6409 os
->after_end
= *found_end
;
6410 init_opb (os
->bfd_section
);
6412 if (os
->bfd_section
!= NULL
)
6414 if (!os
->ignored
&& (os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
6416 current_section
= os
;
6417 prefer_next_section
= false;
6419 newdot
= os
->bfd_section
->vma
;
6421 newdot
= lang_do_assignments_1 (os
->children
.head
,
6422 os
, os
->fill
, newdot
, found_end
);
6425 if (os
->bfd_section
!= NULL
)
6427 newdot
= os
->bfd_section
->vma
;
6429 /* .tbss sections effectively have zero size. */
6430 if (!IS_TBSS (os
->bfd_section
)
6431 || bfd_link_relocatable (&link_info
))
6432 newdot
+= TO_ADDR (os
->bfd_section
->size
);
6434 if (os
->update_dot_tree
!= NULL
)
6435 exp_fold_tree (os
->update_dot_tree
,
6436 bfd_abs_section_ptr
, &newdot
);
6443 case lang_wild_statement_enum
:
6445 dot
= lang_do_assignments_1 (s
->wild_statement
.children
.head
,
6446 current_os
, fill
, dot
, found_end
);
6449 case lang_object_symbols_statement_enum
:
6450 case lang_output_statement_enum
:
6451 case lang_target_statement_enum
:
6454 case lang_data_statement_enum
:
6455 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
6456 if (expld
.result
.valid_p
)
6458 s
->data_statement
.value
= expld
.result
.value
;
6459 if (expld
.result
.section
!= NULL
)
6460 s
->data_statement
.value
+= expld
.result
.section
->vma
;
6462 else if (expld
.phase
== lang_final_phase_enum
)
6463 einfo (_("%F%P: invalid data statement\n"));
6466 switch (s
->data_statement
.type
)
6484 if (size
< TO_SIZE ((unsigned) 1))
6485 size
= TO_SIZE ((unsigned) 1);
6486 dot
+= TO_ADDR (size
);
6490 case lang_reloc_statement_enum
:
6491 exp_fold_tree (s
->reloc_statement
.addend_exp
,
6492 bfd_abs_section_ptr
, &dot
);
6493 if (expld
.result
.valid_p
)
6494 s
->reloc_statement
.addend_value
= expld
.result
.value
;
6495 else if (expld
.phase
== lang_final_phase_enum
)
6496 einfo (_("%F%P: invalid reloc statement\n"));
6497 dot
+= TO_ADDR (bfd_get_reloc_size (s
->reloc_statement
.howto
));
6500 case lang_input_section_enum
:
6502 asection
*in
= s
->input_section
.section
;
6504 if ((in
->flags
& SEC_EXCLUDE
) == 0)
6505 dot
+= TO_ADDR (in
->size
);
6509 case lang_input_statement_enum
:
6512 case lang_fill_statement_enum
:
6513 fill
= s
->fill_statement
.fill
;
6516 case lang_assignment_statement_enum
:
6517 current_assign
= &s
->assignment_statement
;
6518 if (current_assign
->exp
->type
.node_class
!= etree_assert
)
6520 const char *p
= current_assign
->exp
->assign
.dst
;
6522 if (current_os
== abs_output_section
&& p
[0] == '.' && p
[1] == 0)
6523 prefer_next_section
= true;
6527 if (strcmp (p
, "end") == 0)
6530 exp_fold_tree (s
->assignment_statement
.exp
,
6531 (current_os
->bfd_section
!= NULL
6532 ? current_os
->bfd_section
: bfd_und_section_ptr
),
6536 case lang_padding_statement_enum
:
6537 dot
+= TO_ADDR (s
->padding_statement
.size
);
6540 case lang_group_statement_enum
:
6541 dot
= lang_do_assignments_1 (s
->group_statement
.children
.head
,
6542 current_os
, fill
, dot
, found_end
);
6545 case lang_insert_statement_enum
:
6548 case lang_address_statement_enum
:
6560 lang_do_assignments (lang_phase_type phase
)
6562 bool found_end
= false;
6564 current_section
= NULL
;
6565 prefer_next_section
= false;
6566 expld
.phase
= phase
;
6567 lang_statement_iteration
++;
6568 lang_do_assignments_1 (statement_list
.head
,
6569 abs_output_section
, NULL
, 0, &found_end
);
6572 /* For an assignment statement outside of an output section statement,
6573 choose the best of neighbouring output sections to use for values
6577 section_for_dot (void)
6581 /* Assignments belong to the previous output section, unless there
6582 has been an assignment to "dot", in which case following
6583 assignments belong to the next output section. (The assumption
6584 is that an assignment to "dot" is setting up the address for the
6585 next output section.) Except that past the assignment to "_end"
6586 we always associate with the previous section. This exception is
6587 for targets like SH that define an alloc .stack or other
6588 weirdness after non-alloc sections. */
6589 if (current_section
== NULL
|| prefer_next_section
)
6591 lang_statement_union_type
*stmt
;
6592 lang_output_section_statement_type
*os
;
6594 for (stmt
= (lang_statement_union_type
*) current_assign
;
6596 stmt
= stmt
->header
.next
)
6597 if (stmt
->header
.type
== lang_output_section_statement_enum
)
6600 os
= stmt
? &stmt
->output_section_statement
: NULL
;
6603 && (os
->bfd_section
== NULL
6604 || (os
->bfd_section
->flags
& SEC_EXCLUDE
) != 0
6605 || bfd_section_removed_from_list (link_info
.output_bfd
,
6609 if (current_section
== NULL
|| os
== NULL
|| !os
->after_end
)
6612 s
= os
->bfd_section
;
6614 s
= link_info
.output_bfd
->section_last
;
6616 && ((s
->flags
& SEC_ALLOC
) == 0
6617 || (s
->flags
& SEC_THREAD_LOCAL
) != 0))
6622 return bfd_abs_section_ptr
;
6626 s
= current_section
->bfd_section
;
6628 /* The section may have been stripped. */
6630 && ((s
->flags
& SEC_EXCLUDE
) != 0
6631 || (s
->flags
& SEC_ALLOC
) == 0
6632 || (s
->flags
& SEC_THREAD_LOCAL
) != 0
6633 || bfd_section_removed_from_list (link_info
.output_bfd
, s
)))
6636 s
= link_info
.output_bfd
->sections
;
6638 && ((s
->flags
& SEC_ALLOC
) == 0
6639 || (s
->flags
& SEC_THREAD_LOCAL
) != 0))
6644 return bfd_abs_section_ptr
;
6647 /* Array of __start/__stop/.startof./.sizeof/ symbols. */
6649 static struct bfd_link_hash_entry
**start_stop_syms
;
6650 static size_t start_stop_count
= 0;
6651 static size_t start_stop_alloc
= 0;
6653 /* Give start/stop SYMBOL for SEC a preliminary definition, and add it
6654 to start_stop_syms. */
6657 lang_define_start_stop (const char *symbol
, asection
*sec
)
6659 struct bfd_link_hash_entry
*h
;
6661 h
= bfd_define_start_stop (link_info
.output_bfd
, &link_info
, symbol
, sec
);
6664 if (start_stop_count
== start_stop_alloc
)
6666 start_stop_alloc
= 2 * start_stop_alloc
+ 10;
6668 = xrealloc (start_stop_syms
,
6669 start_stop_alloc
* sizeof (*start_stop_syms
));
6671 start_stop_syms
[start_stop_count
++] = h
;
6675 /* Check for input sections whose names match references to
6676 __start_SECNAME or __stop_SECNAME symbols. Give the symbols
6677 preliminary definitions. */
6680 lang_init_start_stop (void)
6684 char leading_char
= bfd_get_symbol_leading_char (link_info
.output_bfd
);
6686 for (abfd
= link_info
.input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
6687 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
6690 const char *secname
= s
->name
;
6692 for (ps
= secname
; *ps
!= '\0'; ps
++)
6693 if (!ISALNUM ((unsigned char) *ps
) && *ps
!= '_')
6697 char *symbol
= (char *) xmalloc (10 + strlen (secname
));
6699 symbol
[0] = leading_char
;
6700 sprintf (symbol
+ (leading_char
!= 0), "__start_%s", secname
);
6701 lang_define_start_stop (symbol
, s
);
6703 symbol
[1] = leading_char
;
6704 memcpy (symbol
+ 1 + (leading_char
!= 0), "__stop", 6);
6705 lang_define_start_stop (symbol
+ 1, s
);
6712 /* Iterate over start_stop_syms. */
6715 foreach_start_stop (void (*func
) (struct bfd_link_hash_entry
*))
6719 for (i
= 0; i
< start_stop_count
; ++i
)
6720 func (start_stop_syms
[i
]);
6723 /* __start and __stop symbols are only supposed to be defined by the
6724 linker for orphan sections, but we now extend that to sections that
6725 map to an output section of the same name. The symbols were
6726 defined early for --gc-sections, before we mapped input to output
6727 sections, so undo those that don't satisfy this rule. */
6730 undef_start_stop (struct bfd_link_hash_entry
*h
)
6732 if (h
->ldscript_def
)
6735 if (h
->u
.def
.section
->output_section
== NULL
6736 || h
->u
.def
.section
->output_section
->owner
!= link_info
.output_bfd
6737 || strcmp (h
->u
.def
.section
->name
,
6738 h
->u
.def
.section
->output_section
->name
) != 0)
6740 asection
*sec
= bfd_get_section_by_name (link_info
.output_bfd
,
6741 h
->u
.def
.section
->name
);
6744 /* When there are more than one input sections with the same
6745 section name, SECNAME, linker picks the first one to define
6746 __start_SECNAME and __stop_SECNAME symbols. When the first
6747 input section is removed by comdat group, we need to check
6748 if there is still an output section with section name
6751 for (i
= sec
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
6752 if (strcmp (h
->u
.def
.section
->name
, i
->name
) == 0)
6754 h
->u
.def
.section
= i
;
6758 h
->type
= bfd_link_hash_undefined
;
6759 h
->u
.undef
.abfd
= NULL
;
6760 if (is_elf_hash_table (link_info
.hash
))
6762 const struct elf_backend_data
*bed
;
6763 struct elf_link_hash_entry
*eh
= (struct elf_link_hash_entry
*) h
;
6764 unsigned int was_forced
= eh
->forced_local
;
6766 bed
= get_elf_backend_data (link_info
.output_bfd
);
6767 (*bed
->elf_backend_hide_symbol
) (&link_info
, eh
, true);
6768 if (!eh
->ref_regular_nonweak
)
6769 h
->type
= bfd_link_hash_undefweak
;
6770 eh
->def_regular
= 0;
6771 eh
->forced_local
= was_forced
;
6777 lang_undef_start_stop (void)
6779 foreach_start_stop (undef_start_stop
);
6782 /* Check for output sections whose names match references to
6783 .startof.SECNAME or .sizeof.SECNAME symbols. Give the symbols
6784 preliminary definitions. */
6787 lang_init_startof_sizeof (void)
6791 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6793 const char *secname
= s
->name
;
6794 char *symbol
= (char *) xmalloc (10 + strlen (secname
));
6796 sprintf (symbol
, ".startof.%s", secname
);
6797 lang_define_start_stop (symbol
, s
);
6799 memcpy (symbol
+ 1, ".size", 5);
6800 lang_define_start_stop (symbol
+ 1, s
);
6805 /* Set .startof., .sizeof., __start and __stop symbols final values. */
6808 set_start_stop (struct bfd_link_hash_entry
*h
)
6811 || h
->type
!= bfd_link_hash_defined
)
6814 if (h
->root
.string
[0] == '.')
6816 /* .startof. or .sizeof. symbol.
6817 .startof. already has final value. */
6818 if (h
->root
.string
[2] == 'i')
6821 h
->u
.def
.value
= TO_ADDR (h
->u
.def
.section
->size
);
6822 h
->u
.def
.section
= bfd_abs_section_ptr
;
6827 /* __start or __stop symbol. */
6828 int has_lead
= bfd_get_symbol_leading_char (link_info
.output_bfd
) != 0;
6830 h
->u
.def
.section
= h
->u
.def
.section
->output_section
;
6831 if (h
->root
.string
[4 + has_lead
] == 'o')
6834 h
->u
.def
.value
= TO_ADDR (h
->u
.def
.section
->size
);
6840 lang_finalize_start_stop (void)
6842 foreach_start_stop (set_start_stop
);
6846 lang_symbol_tweaks (void)
6848 /* Give initial values for __start and __stop symbols, so that ELF
6849 gc_sections will keep sections referenced by these symbols. Must
6850 be done before lang_do_assignments. */
6851 if (config
.build_constructors
)
6852 lang_init_start_stop ();
6854 /* Make __ehdr_start hidden, and set def_regular even though it is
6855 likely undefined at this stage. For lang_check_relocs. */
6856 if (is_elf_hash_table (link_info
.hash
)
6857 && !bfd_link_relocatable (&link_info
))
6859 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*)
6860 bfd_link_hash_lookup (link_info
.hash
, "__ehdr_start",
6861 false, false, true);
6863 /* Only adjust the export class if the symbol was referenced
6864 and not defined, otherwise leave it alone. */
6866 && (h
->root
.type
== bfd_link_hash_new
6867 || h
->root
.type
== bfd_link_hash_undefined
6868 || h
->root
.type
== bfd_link_hash_undefweak
6869 || h
->root
.type
== bfd_link_hash_common
))
6871 const struct elf_backend_data
*bed
;
6872 bed
= get_elf_backend_data (link_info
.output_bfd
);
6873 (*bed
->elf_backend_hide_symbol
) (&link_info
, h
, true);
6874 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
6875 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
6877 h
->root
.linker_def
= 1;
6878 h
->root
.rel_from_abs
= 1;
6886 struct bfd_link_hash_entry
*h
;
6889 if ((bfd_link_relocatable (&link_info
) && !link_info
.gc_sections
)
6890 || bfd_link_dll (&link_info
))
6891 warn
= entry_from_cmdline
;
6895 /* Force the user to specify a root when generating a relocatable with
6896 --gc-sections, unless --gc-keep-exported was also given. */
6897 if (bfd_link_relocatable (&link_info
)
6898 && link_info
.gc_sections
6899 && !link_info
.gc_keep_exported
)
6901 struct bfd_sym_chain
*sym
;
6903 for (sym
= link_info
.gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
6905 h
= bfd_link_hash_lookup (link_info
.hash
, sym
->name
,
6906 false, false, false);
6908 && (h
->type
== bfd_link_hash_defined
6909 || h
->type
== bfd_link_hash_defweak
)
6910 && !bfd_is_const_section (h
->u
.def
.section
))
6914 einfo (_("%F%P: --gc-sections requires a defined symbol root "
6915 "specified by -e or -u\n"));
6918 if (entry_symbol
.name
== NULL
)
6920 /* No entry has been specified. Look for the default entry, but
6921 don't warn if we don't find it. */
6922 entry_symbol
.name
= entry_symbol_default
;
6926 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
.name
,
6927 false, false, true);
6929 && (h
->type
== bfd_link_hash_defined
6930 || h
->type
== bfd_link_hash_defweak
)
6931 && h
->u
.def
.section
->output_section
!= NULL
)
6935 val
= (h
->u
.def
.value
6936 + bfd_section_vma (h
->u
.def
.section
->output_section
)
6937 + h
->u
.def
.section
->output_offset
);
6938 if (!bfd_set_start_address (link_info
.output_bfd
, val
))
6939 einfo (_("%F%P: %s: can't set start address\n"), entry_symbol
.name
);
6946 /* We couldn't find the entry symbol. Try parsing it as a
6948 val
= bfd_scan_vma (entry_symbol
.name
, &send
, 0);
6951 if (!bfd_set_start_address (link_info
.output_bfd
, val
))
6952 einfo (_("%F%P: can't set start address\n"));
6954 /* BZ 2004952: Only use the start of the entry section for executables. */
6955 else if bfd_link_executable (&link_info
)
6959 /* Can't find the entry symbol, and it's not a number. Use
6960 the first address in the text section. */
6961 ts
= bfd_get_section_by_name (link_info
.output_bfd
, entry_section
);
6965 einfo (_("%P: warning: cannot find entry symbol %s;"
6966 " defaulting to %V\n"),
6968 bfd_section_vma (ts
));
6969 if (!bfd_set_start_address (link_info
.output_bfd
,
6970 bfd_section_vma (ts
)))
6971 einfo (_("%F%P: can't set start address\n"));
6976 einfo (_("%P: warning: cannot find entry symbol %s;"
6977 " not setting start address\n"),
6984 einfo (_("%P: warning: cannot find entry symbol %s;"
6985 " not setting start address\n"),
6991 /* This is a small function used when we want to ignore errors from
6995 ignore_bfd_errors (const char *fmt ATTRIBUTE_UNUSED
,
6996 va_list ap ATTRIBUTE_UNUSED
)
6998 /* Don't do anything. */
7001 /* Check that the architecture of all the input files is compatible
7002 with the output file. Also call the backend to let it do any
7003 other checking that is needed. */
7008 lang_input_statement_type
*file
;
7010 const bfd_arch_info_type
*compatible
;
7012 for (file
= (void *) file_chain
.head
;
7016 #if BFD_SUPPORTS_PLUGINS
7017 /* Don't check format of files claimed by plugin. */
7018 if (file
->flags
.claimed
)
7020 #endif /* BFD_SUPPORTS_PLUGINS */
7021 input_bfd
= file
->the_bfd
;
7023 = bfd_arch_get_compatible (input_bfd
, link_info
.output_bfd
,
7024 command_line
.accept_unknown_input_arch
);
7026 /* In general it is not possible to perform a relocatable
7027 link between differing object formats when the input
7028 file has relocations, because the relocations in the
7029 input format may not have equivalent representations in
7030 the output format (and besides BFD does not translate
7031 relocs for other link purposes than a final link). */
7032 if (!file
->flags
.just_syms
7033 && (bfd_link_relocatable (&link_info
)
7034 || link_info
.emitrelocations
)
7035 && (compatible
== NULL
7036 || (bfd_get_flavour (input_bfd
)
7037 != bfd_get_flavour (link_info
.output_bfd
)))
7038 && (bfd_get_file_flags (input_bfd
) & HAS_RELOC
) != 0)
7040 einfo (_("%F%P: relocatable linking with relocations from"
7041 " format %s (%pB) to format %s (%pB) is not supported\n"),
7042 bfd_get_target (input_bfd
), input_bfd
,
7043 bfd_get_target (link_info
.output_bfd
), link_info
.output_bfd
);
7044 /* einfo with %F exits. */
7047 if (compatible
== NULL
)
7049 if (command_line
.warn_mismatch
)
7050 einfo (_("%X%P: %s architecture of input file `%pB'"
7051 " is incompatible with %s output\n"),
7052 bfd_printable_name (input_bfd
), input_bfd
,
7053 bfd_printable_name (link_info
.output_bfd
));
7056 /* If the input bfd has no contents, it shouldn't set the
7057 private data of the output bfd. */
7058 else if (!file
->flags
.just_syms
7059 && ((input_bfd
->flags
& DYNAMIC
) != 0
7060 || bfd_count_sections (input_bfd
) != 0))
7062 bfd_error_handler_type pfn
= NULL
;
7064 /* If we aren't supposed to warn about mismatched input
7065 files, temporarily set the BFD error handler to a
7066 function which will do nothing. We still want to call
7067 bfd_merge_private_bfd_data, since it may set up
7068 information which is needed in the output file. */
7069 if (!command_line
.warn_mismatch
)
7070 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
7071 if (!bfd_merge_private_bfd_data (input_bfd
, &link_info
))
7073 if (command_line
.warn_mismatch
)
7074 einfo (_("%X%P: failed to merge target specific data"
7075 " of file %pB\n"), input_bfd
);
7077 if (!command_line
.warn_mismatch
)
7078 bfd_set_error_handler (pfn
);
7083 /* Look through all the global common symbols and attach them to the
7084 correct section. The -sort-common command line switch may be used
7085 to roughly sort the entries by alignment. */
7090 if (link_info
.inhibit_common_definition
)
7092 if (bfd_link_relocatable (&link_info
)
7093 && !command_line
.force_common_definition
)
7096 if (!config
.sort_common
)
7097 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, NULL
);
7102 if (config
.sort_common
== sort_descending
)
7104 for (power
= 4; power
> 0; power
--)
7105 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7108 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7112 for (power
= 0; power
<= 4; power
++)
7113 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7115 power
= (unsigned int) -1;
7116 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7121 /* Place one common symbol in the correct section. */
7124 lang_one_common (struct bfd_link_hash_entry
*h
, void *info
)
7126 unsigned int power_of_two
;
7130 if (h
->type
!= bfd_link_hash_common
)
7134 power_of_two
= h
->u
.c
.p
->alignment_power
;
7136 if (config
.sort_common
== sort_descending
7137 && power_of_two
< *(unsigned int *) info
)
7139 else if (config
.sort_common
== sort_ascending
7140 && power_of_two
> *(unsigned int *) info
)
7143 section
= h
->u
.c
.p
->section
;
7144 if (!bfd_define_common_symbol (link_info
.output_bfd
, &link_info
, h
))
7145 einfo (_("%F%P: could not define common symbol `%pT': %E\n"),
7148 if (config
.map_file
!= NULL
)
7150 static bool header_printed
;
7155 if (!header_printed
)
7157 minfo (_("\nAllocating common symbols\n"));
7158 minfo (_("Common symbol size file\n\n"));
7159 header_printed
= true;
7162 name
= bfd_demangle (link_info
.output_bfd
, h
->root
.string
,
7163 DMGL_ANSI
| DMGL_PARAMS
);
7166 minfo ("%s", h
->root
.string
);
7167 len
= strlen (h
->root
.string
);
7172 len
= strlen (name
);
7182 sprintf (buf
, "%" PRIx64
, (uint64_t) size
);
7183 fprintf (config
.map_file
, "%*s0x%-16s", 20 - len
, "", buf
);
7185 minfo ("%pB\n", section
->owner
);
7191 /* Handle a single orphan section S, placing the orphan into an appropriate
7192 output section. The effects of the --orphan-handling command line
7193 option are handled here. */
7196 ldlang_place_orphan (asection
*s
)
7198 if (config
.orphan_handling
== orphan_handling_discard
)
7200 lang_output_section_statement_type
*os
;
7201 os
= lang_output_section_statement_lookup (DISCARD_SECTION_NAME
, 0, 1);
7202 if (os
->addr_tree
== NULL
7203 && (bfd_link_relocatable (&link_info
)
7204 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0))
7205 os
->addr_tree
= exp_intop (0);
7206 lang_add_section (&os
->children
, s
, NULL
, NULL
, os
);
7210 lang_output_section_statement_type
*os
;
7211 const char *name
= s
->name
;
7214 if (config
.orphan_handling
== orphan_handling_error
)
7215 einfo (_("%X%P: error: unplaced orphan section `%pA' from `%pB'\n"),
7218 if (config
.unique_orphan_sections
|| unique_section_p (s
, NULL
))
7219 constraint
= SPECIAL
;
7221 os
= ldemul_place_orphan (s
, name
, constraint
);
7224 os
= lang_output_section_statement_lookup (name
, constraint
, 1);
7225 if (os
->addr_tree
== NULL
7226 && (bfd_link_relocatable (&link_info
)
7227 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0))
7228 os
->addr_tree
= exp_intop (0);
7229 lang_add_section (&os
->children
, s
, NULL
, NULL
, os
);
7232 if (config
.orphan_handling
== orphan_handling_warn
)
7233 einfo (_("%P: warning: orphan section `%pA' from `%pB' being "
7234 "placed in section `%s'\n"),
7235 s
, s
->owner
, os
->name
);
7239 /* Run through the input files and ensure that every input section has
7240 somewhere to go. If one is found without a destination then create
7241 an input request and place it into the statement tree. */
7244 lang_place_orphans (void)
7246 LANG_FOR_EACH_INPUT_STATEMENT (file
)
7250 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7252 if (s
->output_section
== NULL
)
7254 /* This section of the file is not attached, root
7255 around for a sensible place for it to go. */
7257 if (file
->flags
.just_syms
)
7258 bfd_link_just_syms (file
->the_bfd
, s
, &link_info
);
7259 else if (lang_discard_section_p (s
))
7260 s
->output_section
= bfd_abs_section_ptr
;
7261 else if (strcmp (s
->name
, "COMMON") == 0)
7263 /* This is a lonely common section which must have
7264 come from an archive. We attach to the section
7265 with the wildcard. */
7266 if (!bfd_link_relocatable (&link_info
)
7267 || command_line
.force_common_definition
)
7269 if (default_common_section
== NULL
)
7270 default_common_section
7271 = lang_output_section_statement_lookup (".bss", 0, 1);
7272 lang_add_section (&default_common_section
->children
, s
,
7273 NULL
, NULL
, default_common_section
);
7277 ldlang_place_orphan (s
);
7284 lang_set_flags (lang_memory_region_type
*ptr
, const char *flags
, int invert
)
7286 flagword
*ptr_flags
;
7288 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
7294 /* PR 17900: An exclamation mark in the attributes reverses
7295 the sense of any of the attributes that follow. */
7298 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
7302 *ptr_flags
|= SEC_ALLOC
;
7306 *ptr_flags
|= SEC_READONLY
;
7310 *ptr_flags
|= SEC_DATA
;
7314 *ptr_flags
|= SEC_CODE
;
7319 *ptr_flags
|= SEC_LOAD
;
7323 einfo (_("%F%P: invalid character %c (%d) in flags\n"),
7331 /* Call a function on each real input file. This function will be
7332 called on an archive, but not on the elements. */
7335 lang_for_each_input_file (void (*func
) (lang_input_statement_type
*))
7337 lang_input_statement_type
*f
;
7339 for (f
= (void *) input_file_chain
.head
;
7341 f
= f
->next_real_file
)
7346 /* Call a function on each real file. The function will be called on
7347 all the elements of an archive which are included in the link, but
7348 will not be called on the archive file itself. */
7351 lang_for_each_file (void (*func
) (lang_input_statement_type
*))
7353 LANG_FOR_EACH_INPUT_STATEMENT (f
)
7361 ldlang_add_file (lang_input_statement_type
*entry
)
7363 lang_statement_append (&file_chain
, entry
, &entry
->next
);
7365 /* The BFD linker needs to have a list of all input BFDs involved in
7367 ASSERT (link_info
.input_bfds_tail
!= &entry
->the_bfd
->link
.next
7368 && entry
->the_bfd
->link
.next
== NULL
);
7369 ASSERT (entry
->the_bfd
!= link_info
.output_bfd
);
7371 *link_info
.input_bfds_tail
= entry
->the_bfd
;
7372 link_info
.input_bfds_tail
= &entry
->the_bfd
->link
.next
;
7373 bfd_set_usrdata (entry
->the_bfd
, entry
);
7374 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
7376 /* Look through the sections and check for any which should not be
7377 included in the link. We need to do this now, so that we can
7378 notice when the backend linker tries to report multiple
7379 definition errors for symbols which are in sections we aren't
7380 going to link. FIXME: It might be better to entirely ignore
7381 symbols which are defined in sections which are going to be
7382 discarded. This would require modifying the backend linker for
7383 each backend which might set the SEC_LINK_ONCE flag. If we do
7384 this, we should probably handle SEC_EXCLUDE in the same way. */
7386 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, entry
);
7390 lang_add_output (const char *name
, int from_script
)
7392 /* Make -o on command line override OUTPUT in script. */
7393 if (!had_output_filename
|| !from_script
)
7395 output_filename
= name
;
7396 had_output_filename
= true;
7400 lang_output_section_statement_type
*
7401 lang_enter_output_section_statement (const char *output_section_statement_name
,
7402 etree_type
*address_exp
,
7403 enum section_type sectype
,
7404 etree_type
*sectype_value
,
7406 etree_type
*subalign
,
7409 int align_with_input
)
7411 lang_output_section_statement_type
*os
;
7413 os
= lang_output_section_statement_lookup (output_section_statement_name
,
7415 current_section
= os
;
7417 if (os
->addr_tree
== NULL
)
7419 os
->addr_tree
= address_exp
;
7421 os
->sectype
= sectype
;
7422 if (sectype
== type_section
|| sectype
== typed_readonly_section
)
7423 os
->sectype_value
= sectype_value
;
7424 else if (sectype
== noload_section
)
7425 os
->flags
= SEC_NEVER_LOAD
;
7427 os
->flags
= SEC_NO_FLAGS
;
7428 os
->block_value
= 1;
7430 /* Make next things chain into subchain of this. */
7431 push_stat_ptr (&os
->children
);
7433 os
->align_lma_with_input
= align_with_input
== ALIGN_WITH_INPUT
;
7434 if (os
->align_lma_with_input
&& align
!= NULL
)
7435 einfo (_("%F%P:%pS: error: align with input and explicit align specified\n"),
7438 os
->subsection_alignment
= subalign
;
7439 os
->section_alignment
= align
;
7441 os
->load_base
= ebase
;
7448 lang_output_statement_type
*new_stmt
;
7450 new_stmt
= new_stat (lang_output_statement
, stat_ptr
);
7451 new_stmt
->name
= output_filename
;
7454 /* Reset the current counters in the regions. */
7457 lang_reset_memory_regions (void)
7459 lang_memory_region_type
*p
= lang_memory_region_list
;
7461 lang_output_section_statement_type
*os
;
7463 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
7465 p
->current
= p
->origin
;
7469 for (os
= (void *) lang_os_list
.head
;
7473 os
->processed_vma
= false;
7474 os
->processed_lma
= false;
7477 for (o
= link_info
.output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
7479 /* Save the last size for possible use by bfd_relax_section. */
7480 o
->rawsize
= o
->size
;
7481 if (!(o
->flags
& SEC_FIXED_SIZE
))
7486 /* Worker for lang_gc_sections_1. */
7489 gc_section_callback (lang_wild_statement_type
*ptr
,
7490 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
7492 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
7493 void *data ATTRIBUTE_UNUSED
)
7495 /* If the wild pattern was marked KEEP, the member sections
7496 should be as well. */
7497 if (ptr
->keep_sections
)
7498 section
->flags
|= SEC_KEEP
;
7501 /* Iterate over sections marking them against GC. */
7504 lang_gc_sections_1 (lang_statement_union_type
*s
)
7506 for (; s
!= NULL
; s
= s
->header
.next
)
7508 switch (s
->header
.type
)
7510 case lang_wild_statement_enum
:
7511 walk_wild (&s
->wild_statement
, gc_section_callback
, NULL
);
7513 case lang_constructors_statement_enum
:
7514 lang_gc_sections_1 (constructor_list
.head
);
7516 case lang_output_section_statement_enum
:
7517 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
7519 case lang_group_statement_enum
:
7520 lang_gc_sections_1 (s
->group_statement
.children
.head
);
7529 lang_gc_sections (void)
7531 /* Keep all sections so marked in the link script. */
7532 lang_gc_sections_1 (statement_list
.head
);
7534 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
7535 the special case of .stabstr debug info. (See bfd/stabs.c)
7536 Twiddle the flag here, to simplify later linker code. */
7537 if (bfd_link_relocatable (&link_info
))
7539 LANG_FOR_EACH_INPUT_STATEMENT (f
)
7542 #if BFD_SUPPORTS_PLUGINS
7543 if (f
->flags
.claimed
)
7546 for (sec
= f
->the_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7547 if ((sec
->flags
& SEC_DEBUGGING
) == 0
7548 || strcmp (sec
->name
, ".stabstr") != 0)
7549 sec
->flags
&= ~SEC_EXCLUDE
;
7553 if (link_info
.gc_sections
)
7554 bfd_gc_sections (link_info
.output_bfd
, &link_info
);
7557 /* Worker for lang_find_relro_sections_1. */
7560 find_relro_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
7561 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
7563 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
7566 /* Discarded, excluded and ignored sections effectively have zero
7568 if (section
->output_section
!= NULL
7569 && section
->output_section
->owner
== link_info
.output_bfd
7570 && (section
->output_section
->flags
& SEC_EXCLUDE
) == 0
7571 && !IGNORE_SECTION (section
)
7572 && section
->size
!= 0)
7574 bool *has_relro_section
= (bool *) data
;
7575 *has_relro_section
= true;
7579 /* Iterate over sections for relro sections. */
7582 lang_find_relro_sections_1 (lang_statement_union_type
*s
,
7583 bool *has_relro_section
)
7585 if (*has_relro_section
)
7588 for (; s
!= NULL
; s
= s
->header
.next
)
7590 if (s
== expld
.dataseg
.relro_end_stat
)
7593 switch (s
->header
.type
)
7595 case lang_wild_statement_enum
:
7596 walk_wild (&s
->wild_statement
,
7597 find_relro_section_callback
,
7600 case lang_constructors_statement_enum
:
7601 lang_find_relro_sections_1 (constructor_list
.head
,
7604 case lang_output_section_statement_enum
:
7605 lang_find_relro_sections_1 (s
->output_section_statement
.children
.head
,
7608 case lang_group_statement_enum
:
7609 lang_find_relro_sections_1 (s
->group_statement
.children
.head
,
7619 lang_find_relro_sections (void)
7621 bool has_relro_section
= false;
7623 /* Check all sections in the link script. */
7625 lang_find_relro_sections_1 (expld
.dataseg
.relro_start_stat
,
7626 &has_relro_section
);
7628 if (!has_relro_section
)
7629 link_info
.relro
= false;
7632 /* Relax all sections until bfd_relax_section gives up. */
7635 lang_relax_sections (bool need_layout
)
7637 /* NB: Also enable relaxation to layout sections for DT_RELR. */
7638 if (RELAXATION_ENABLED
|| link_info
.enable_dt_relr
)
7640 /* We may need more than one relaxation pass. */
7641 int i
= link_info
.relax_pass
;
7643 /* The backend can use it to determine the current pass. */
7644 link_info
.relax_pass
= 0;
7648 /* Keep relaxing until bfd_relax_section gives up. */
7651 link_info
.relax_trip
= -1;
7654 link_info
.relax_trip
++;
7656 /* Note: pe-dll.c does something like this also. If you find
7657 you need to change this code, you probably need to change
7658 pe-dll.c also. DJ */
7660 /* Do all the assignments with our current guesses as to
7662 lang_do_assignments (lang_assigning_phase_enum
);
7664 /* We must do this after lang_do_assignments, because it uses
7666 lang_reset_memory_regions ();
7668 /* Perform another relax pass - this time we know where the
7669 globals are, so can make a better guess. */
7670 relax_again
= false;
7671 lang_size_sections (&relax_again
, false);
7673 while (relax_again
);
7675 link_info
.relax_pass
++;
7682 /* Final extra sizing to report errors. */
7683 lang_do_assignments (lang_assigning_phase_enum
);
7684 lang_reset_memory_regions ();
7685 lang_size_sections (NULL
, true);
7689 #if BFD_SUPPORTS_PLUGINS
7690 /* Find the insert point for the plugin's replacement files. We
7691 place them after the first claimed real object file, or if the
7692 first claimed object is an archive member, after the last real
7693 object file immediately preceding the archive. In the event
7694 no objects have been claimed at all, we return the first dummy
7695 object file on the list as the insert point; that works, but
7696 the callee must be careful when relinking the file_chain as it
7697 is not actually on that chain, only the statement_list and the
7698 input_file list; in that case, the replacement files must be
7699 inserted at the head of the file_chain. */
7701 static lang_input_statement_type
*
7702 find_replacements_insert_point (bool *before
)
7704 lang_input_statement_type
*claim1
, *lastobject
;
7705 lastobject
= (void *) input_file_chain
.head
;
7706 for (claim1
= (void *) file_chain
.head
;
7708 claim1
= claim1
->next
)
7710 if (claim1
->flags
.claimed
)
7712 *before
= claim1
->flags
.claim_archive
;
7713 return claim1
->flags
.claim_archive
? lastobject
: claim1
;
7715 /* Update lastobject if this is a real object file. */
7716 if (claim1
->the_bfd
!= NULL
&& claim1
->the_bfd
->my_archive
== NULL
)
7717 lastobject
= claim1
;
7719 /* No files were claimed by the plugin. Choose the last object
7720 file found on the list (maybe the first, dummy entry) as the
7726 /* Find where to insert ADD, an archive element or shared library
7727 added during a rescan. */
7729 static lang_input_statement_type
**
7730 find_rescan_insertion (lang_input_statement_type
*add
)
7732 bfd
*add_bfd
= add
->the_bfd
;
7733 lang_input_statement_type
*f
;
7734 lang_input_statement_type
*last_loaded
= NULL
;
7735 lang_input_statement_type
*before
= NULL
;
7736 lang_input_statement_type
**iter
= NULL
;
7738 if (add_bfd
->my_archive
!= NULL
)
7739 add_bfd
= add_bfd
->my_archive
;
7741 /* First look through the input file chain, to find an object file
7742 before the one we've rescanned. Normal object files always
7743 appear on both the input file chain and the file chain, so this
7744 lets us get quickly to somewhere near the correct place on the
7745 file chain if it is full of archive elements. Archives don't
7746 appear on the file chain, but if an element has been extracted
7747 then their input_statement->next points at it. */
7748 for (f
= (void *) input_file_chain
.head
;
7750 f
= f
->next_real_file
)
7752 if (f
->the_bfd
== add_bfd
)
7754 before
= last_loaded
;
7755 if (f
->next
!= NULL
)
7756 return &f
->next
->next
;
7758 if (f
->the_bfd
!= NULL
&& f
->next
!= NULL
)
7762 for (iter
= before
? &before
->next
: &file_chain
.head
->input_statement
.next
;
7764 iter
= &(*iter
)->next
)
7765 if (!(*iter
)->flags
.claim_archive
7766 && (*iter
)->the_bfd
->my_archive
== NULL
)
7772 /* Insert SRCLIST into DESTLIST after given element by chaining
7773 on FIELD as the next-pointer. (Counterintuitively does not need
7774 a pointer to the actual after-node itself, just its chain field.) */
7777 lang_list_insert_after (lang_statement_list_type
*destlist
,
7778 lang_statement_list_type
*srclist
,
7779 lang_statement_union_type
**field
)
7781 *(srclist
->tail
) = *field
;
7782 *field
= srclist
->head
;
7783 if (destlist
->tail
== field
)
7784 destlist
->tail
= srclist
->tail
;
7787 /* Detach new nodes added to DESTLIST since the time ORIGLIST
7788 was taken as a copy of it and leave them in ORIGLIST. */
7791 lang_list_remove_tail (lang_statement_list_type
*destlist
,
7792 lang_statement_list_type
*origlist
)
7794 union lang_statement_union
**savetail
;
7795 /* Check that ORIGLIST really is an earlier state of DESTLIST. */
7796 ASSERT (origlist
->head
== destlist
->head
);
7797 savetail
= origlist
->tail
;
7798 origlist
->head
= *(savetail
);
7799 origlist
->tail
= destlist
->tail
;
7800 destlist
->tail
= savetail
;
7804 static lang_statement_union_type
**
7805 find_next_input_statement (lang_statement_union_type
**s
)
7807 for ( ; *s
; s
= &(*s
)->header
.next
)
7809 lang_statement_union_type
**t
;
7810 switch ((*s
)->header
.type
)
7812 case lang_input_statement_enum
:
7814 case lang_wild_statement_enum
:
7815 t
= &(*s
)->wild_statement
.children
.head
;
7817 case lang_group_statement_enum
:
7818 t
= &(*s
)->group_statement
.children
.head
;
7820 case lang_output_section_statement_enum
:
7821 t
= &(*s
)->output_section_statement
.children
.head
;
7826 t
= find_next_input_statement (t
);
7832 #endif /* BFD_SUPPORTS_PLUGINS */
7834 /* Add NAME to the list of garbage collection entry points. */
7837 lang_add_gc_name (const char *name
)
7839 struct bfd_sym_chain
*sym
;
7844 sym
= stat_alloc (sizeof (*sym
));
7846 sym
->next
= link_info
.gc_sym_list
;
7848 link_info
.gc_sym_list
= sym
;
7851 /* Check relocations. */
7854 lang_check_relocs (void)
7856 if (link_info
.check_relocs_after_open_input
)
7860 for (abfd
= link_info
.input_bfds
;
7861 abfd
!= (bfd
*) NULL
; abfd
= abfd
->link
.next
)
7862 if (!bfd_link_check_relocs (abfd
, &link_info
))
7864 /* No object output, fail return. */
7865 config
.make_executable
= false;
7866 /* Note: we do not abort the loop, but rather
7867 continue the scan in case there are other
7868 bad relocations to report. */
7873 /* Look through all output sections looking for places where we can
7874 propagate forward the lma region. */
7877 lang_propagate_lma_regions (void)
7879 lang_output_section_statement_type
*os
;
7881 for (os
= (void *) lang_os_list
.head
;
7885 if (os
->prev
!= NULL
7886 && os
->lma_region
== NULL
7887 && os
->load_base
== NULL
7888 && os
->addr_tree
== NULL
7889 && os
->region
== os
->prev
->region
)
7890 os
->lma_region
= os
->prev
->lma_region
;
7895 warn_non_contiguous_discards (void)
7897 LANG_FOR_EACH_INPUT_STATEMENT (file
)
7899 if ((file
->the_bfd
->flags
& (BFD_LINKER_CREATED
| DYNAMIC
)) != 0
7900 || file
->flags
.just_syms
)
7903 for (asection
*s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7904 if (s
->output_section
== NULL
7905 && (s
->flags
& SEC_LINKER_CREATED
) == 0)
7906 einfo (_("%P: warning: --enable-non-contiguous-regions "
7907 "discards section `%pA' from `%pB'\n"),
7913 reset_one_wild (lang_statement_union_type
*statement
)
7915 if (statement
->header
.type
== lang_wild_statement_enum
)
7917 lang_wild_statement_type
*stmt
= &statement
->wild_statement
;
7918 lang_list_init (&stmt
->matching_sections
);
7923 reset_resolved_wilds (void)
7925 lang_for_each_statement (reset_one_wild
);
7931 /* Finalize dynamic list. */
7932 if (link_info
.dynamic_list
)
7933 lang_finalize_version_expr_head (&link_info
.dynamic_list
->head
);
7935 current_target
= default_target
;
7937 /* Open the output file. */
7938 lang_for_each_statement (ldlang_open_output
);
7941 ldemul_create_output_section_statements ();
7943 /* Add to the hash table all undefineds on the command line. */
7944 lang_place_undefineds ();
7946 if (!bfd_section_already_linked_table_init ())
7947 einfo (_("%F%P: can not create hash table: %E\n"));
7949 /* A first pass through the memory regions ensures that if any region
7950 references a symbol for its origin or length then this symbol will be
7951 added to the symbol table. Having these symbols in the symbol table
7952 means that when we call open_input_bfds PROVIDE statements will
7953 trigger to provide any needed symbols. The regions origins and
7954 lengths are not assigned as a result of this call. */
7955 lang_do_memory_regions (false);
7957 /* Create a bfd for each input file. */
7958 current_target
= default_target
;
7959 lang_statement_iteration
++;
7960 open_input_bfds (statement_list
.head
, OPEN_BFD_NORMAL
);
7962 /* Now that open_input_bfds has processed assignments and provide
7963 statements we can give values to symbolic origin/length now. */
7964 lang_do_memory_regions (true);
7966 #if BFD_SUPPORTS_PLUGINS
7967 if (link_info
.lto_plugin_active
)
7969 lang_statement_list_type added
;
7970 lang_statement_list_type files
, inputfiles
;
7972 ldemul_before_plugin_all_symbols_read ();
7974 /* Now all files are read, let the plugin(s) decide if there
7975 are any more to be added to the link before we call the
7976 emulation's after_open hook. We create a private list of
7977 input statements for this purpose, which we will eventually
7978 insert into the global statement list after the first claimed
7981 /* We need to manipulate all three chains in synchrony. */
7983 inputfiles
= input_file_chain
;
7984 if (plugin_call_all_symbols_read ())
7985 einfo (_("%F%P: %s: plugin reported error after all symbols read\n"),
7986 plugin_error_plugin ());
7987 link_info
.lto_all_symbols_read
= true;
7988 /* Open any newly added files, updating the file chains. */
7989 plugin_undefs
= link_info
.hash
->undefs_tail
;
7990 open_input_bfds (*added
.tail
, OPEN_BFD_NORMAL
);
7991 if (plugin_undefs
== link_info
.hash
->undefs_tail
)
7992 plugin_undefs
= NULL
;
7993 /* Restore the global list pointer now they have all been added. */
7994 lang_list_remove_tail (stat_ptr
, &added
);
7995 /* And detach the fresh ends of the file lists. */
7996 lang_list_remove_tail (&file_chain
, &files
);
7997 lang_list_remove_tail (&input_file_chain
, &inputfiles
);
7998 /* Were any new files added? */
7999 if (added
.head
!= NULL
)
8001 /* If so, we will insert them into the statement list immediately
8002 after the first input file that was claimed by the plugin,
8003 unless that file was an archive in which case it is inserted
8004 immediately before. */
8006 lang_statement_union_type
**prev
;
8007 plugin_insert
= find_replacements_insert_point (&before
);
8008 /* If a plugin adds input files without having claimed any, we
8009 don't really have a good idea where to place them. Just putting
8010 them at the start or end of the list is liable to leave them
8011 outside the crtbegin...crtend range. */
8012 ASSERT (plugin_insert
!= NULL
);
8013 /* Splice the new statement list into the old one. */
8014 prev
= &plugin_insert
->header
.next
;
8017 prev
= find_next_input_statement (prev
);
8018 if (*prev
!= (void *) plugin_insert
->next_real_file
)
8020 /* We didn't find the expected input statement.
8021 Fall back to adding after plugin_insert. */
8022 prev
= &plugin_insert
->header
.next
;
8025 lang_list_insert_after (stat_ptr
, &added
, prev
);
8026 /* Likewise for the file chains. */
8027 lang_list_insert_after (&input_file_chain
, &inputfiles
,
8028 (void *) &plugin_insert
->next_real_file
);
8029 /* We must be careful when relinking file_chain; we may need to
8030 insert the new files at the head of the list if the insert
8031 point chosen is the dummy first input file. */
8032 if (plugin_insert
->filename
)
8033 lang_list_insert_after (&file_chain
, &files
,
8034 (void *) &plugin_insert
->next
);
8036 lang_list_insert_after (&file_chain
, &files
, &file_chain
.head
);
8038 /* Rescan archives in case new undefined symbols have appeared. */
8040 lang_statement_iteration
++;
8041 open_input_bfds (statement_list
.head
, OPEN_BFD_RESCAN
);
8042 lang_list_remove_tail (&file_chain
, &files
);
8043 while (files
.head
!= NULL
)
8045 lang_input_statement_type
**insert
;
8046 lang_input_statement_type
**iter
, *temp
;
8049 insert
= find_rescan_insertion (&files
.head
->input_statement
);
8050 /* All elements from an archive can be added at once. */
8051 iter
= &files
.head
->input_statement
.next
;
8052 my_arch
= files
.head
->input_statement
.the_bfd
->my_archive
;
8053 if (my_arch
!= NULL
)
8054 for (; *iter
!= NULL
; iter
= &(*iter
)->next
)
8055 if ((*iter
)->the_bfd
->my_archive
!= my_arch
)
8058 *insert
= &files
.head
->input_statement
;
8059 files
.head
= (lang_statement_union_type
*) *iter
;
8061 if (file_chain
.tail
== (lang_statement_union_type
**) insert
)
8062 file_chain
.tail
= (lang_statement_union_type
**) iter
;
8063 if (my_arch
!= NULL
)
8065 lang_input_statement_type
*parent
= bfd_usrdata (my_arch
);
8067 parent
->next
= (lang_input_statement_type
*)
8069 - offsetof (lang_input_statement_type
, next
));
8074 #endif /* BFD_SUPPORTS_PLUGINS */
8076 /* Make sure that nobody has tried to add a symbol to this list
8078 ASSERT (link_info
.gc_sym_list
== NULL
);
8080 link_info
.gc_sym_list
= &entry_symbol
;
8082 if (entry_symbol
.name
== NULL
)
8084 link_info
.gc_sym_list
= ldlang_undef_chain_list_head
;
8086 /* entry_symbol is normally initialied by a ENTRY definition in the
8087 linker script or the -e command line option. But if neither of
8088 these have been used, the target specific backend may still have
8089 provided an entry symbol via a call to lang_default_entry().
8090 Unfortunately this value will not be processed until lang_end()
8091 is called, long after this function has finished. So detect this
8092 case here and add the target's entry symbol to the list of starting
8093 points for garbage collection resolution. */
8094 lang_add_gc_name (entry_symbol_default
);
8097 lang_add_gc_name (link_info
.init_function
);
8098 lang_add_gc_name (link_info
.fini_function
);
8100 ldemul_after_open ();
8101 if (config
.map_file
!= NULL
)
8102 lang_print_asneeded ();
8106 bfd_section_already_linked_table_free ();
8108 /* Make sure that we're not mixing architectures. We call this
8109 after all the input files have been opened, but before we do any
8110 other processing, so that any operations merge_private_bfd_data
8111 does on the output file will be known during the rest of the
8115 /* Handle .exports instead of a version script if we're told to do so. */
8116 if (command_line
.version_exports_section
)
8117 lang_do_version_exports_section ();
8119 /* Build all sets based on the information gathered from the input
8121 ldctor_build_sets ();
8123 lang_symbol_tweaks ();
8125 /* PR 13683: We must rerun the assignments prior to running garbage
8126 collection in order to make sure that all symbol aliases are resolved. */
8127 lang_do_assignments (lang_mark_phase_enum
);
8128 expld
.phase
= lang_first_phase_enum
;
8130 /* Size up the common data. */
8134 debug_prefix_tree ();
8138 /* Remove unreferenced sections if asked to. */
8139 lang_gc_sections ();
8141 lang_mark_undefineds ();
8143 /* Check relocations. */
8144 lang_check_relocs ();
8146 ldemul_after_check_relocs ();
8148 /* There might have been new sections created (e.g. as result of
8149 checking relocs to need a .got, or suchlike), so to properly order
8150 them into our lists of matching sections reset them here. */
8151 reset_resolved_wilds ();
8154 /* Update wild statements in case the user gave --sort-section.
8155 Note how the option might have come after the linker script and
8156 so couldn't have been set when the wild statements were created. */
8157 update_wild_statements (statement_list
.head
);
8159 /* Run through the contours of the script and attach input sections
8160 to the correct output sections. */
8161 lang_statement_iteration
++;
8162 map_input_to_output_sections (statement_list
.head
, NULL
, NULL
);
8164 /* Start at the statement immediately after the special abs_section
8165 output statement, so that it isn't reordered. */
8166 process_insert_statements (&lang_os_list
.head
->header
.next
);
8168 ldemul_before_place_orphans ();
8170 /* Find any sections not attached explicitly and handle them. */
8171 lang_place_orphans ();
8173 if (!bfd_link_relocatable (&link_info
))
8177 /* Merge SEC_MERGE sections. This has to be done after GC of
8178 sections, so that GCed sections are not merged, but before
8179 assigning dynamic symbols, since removing whole input sections
8181 bfd_merge_sections (link_info
.output_bfd
, &link_info
);
8183 /* Look for a text section and set the readonly attribute in it. */
8184 found
= bfd_get_section_by_name (link_info
.output_bfd
, ".text");
8188 if (config
.text_read_only
)
8189 found
->flags
|= SEC_READONLY
;
8191 found
->flags
&= ~SEC_READONLY
;
8195 /* Merge together CTF sections. After this, only the symtab-dependent
8196 function and data object sections need adjustment. */
8199 /* Emit the CTF, iff the emulation doesn't need to do late emission after
8200 examining things laid out late, like the strtab. */
8203 /* Copy forward lma regions for output sections in same lma region. */
8204 lang_propagate_lma_regions ();
8206 /* Defining __start/__stop symbols early for --gc-sections to work
8207 around a glibc build problem can result in these symbols being
8208 defined when they should not be. Fix them now. */
8209 if (config
.build_constructors
)
8210 lang_undef_start_stop ();
8212 /* Define .startof./.sizeof. symbols with preliminary values before
8213 dynamic symbols are created. */
8214 if (!bfd_link_relocatable (&link_info
))
8215 lang_init_startof_sizeof ();
8217 /* Do anything special before sizing sections. This is where ELF
8218 and other back-ends size dynamic sections. */
8219 ldemul_before_allocation ();
8221 /* We must record the program headers before we try to fix the
8222 section positions, since they will affect SIZEOF_HEADERS. */
8223 lang_record_phdrs ();
8225 /* Check relro sections. */
8226 if (link_info
.relro
&& !bfd_link_relocatable (&link_info
))
8227 lang_find_relro_sections ();
8229 /* Size up the sections. */
8230 lang_size_sections (NULL
, !RELAXATION_ENABLED
);
8232 /* See if anything special should be done now we know how big
8233 everything is. This is where relaxation is done. */
8234 ldemul_after_allocation ();
8236 /* Fix any __start, __stop, .startof. or .sizeof. symbols. */
8237 lang_finalize_start_stop ();
8239 /* Do all the assignments again, to report errors. Assignment
8240 statements are processed multiple times, updating symbols; In
8241 open_input_bfds, lang_do_assignments, and lang_size_sections.
8242 Since lang_relax_sections calls lang_do_assignments, symbols are
8243 also updated in ldemul_after_allocation. */
8244 lang_do_assignments (lang_final_phase_enum
);
8248 /* Convert absolute symbols to section relative. */
8249 ldexp_finalize_syms ();
8251 /* Make sure that the section addresses make sense. */
8252 if (command_line
.check_section_addresses
)
8253 lang_check_section_addresses ();
8255 if (link_info
.non_contiguous_regions
8256 && link_info
.non_contiguous_regions_warnings
)
8257 warn_non_contiguous_discards ();
8259 /* Check any required symbols are known. */
8260 ldlang_check_require_defined_symbols ();
8265 /* EXPORTED TO YACC */
8268 lang_add_wild (struct wildcard_spec
*filespec
,
8269 struct wildcard_list
*section_list
,
8272 struct wildcard_list
*curr
, *next
;
8273 lang_wild_statement_type
*new_stmt
;
8274 bool any_specs_sorted
= false;
8276 /* Reverse the list as the parser puts it back to front. */
8277 for (curr
= section_list
, section_list
= NULL
;
8279 section_list
= curr
, curr
= next
)
8281 if (curr
->spec
.sorted
!= none
&& curr
->spec
.sorted
!= by_none
)
8282 any_specs_sorted
= true;
8284 curr
->next
= section_list
;
8287 if (filespec
!= NULL
&& filespec
->name
!= NULL
)
8289 if (strcmp (filespec
->name
, "*") == 0)
8290 filespec
->name
= NULL
;
8291 else if (!wildcardp (filespec
->name
))
8292 lang_has_input_file
= true;
8295 new_stmt
= new_stat (lang_wild_statement
, stat_ptr
);
8296 new_stmt
->filename
= NULL
;
8297 new_stmt
->filenames_sorted
= false;
8298 new_stmt
->any_specs_sorted
= any_specs_sorted
;
8299 new_stmt
->section_flag_list
= NULL
;
8300 new_stmt
->exclude_name_list
= NULL
;
8301 if (filespec
!= NULL
)
8303 new_stmt
->filename
= filespec
->name
;
8304 new_stmt
->filenames_sorted
= filespec
->sorted
== by_name
;
8305 new_stmt
->section_flag_list
= filespec
->section_flag_list
;
8306 new_stmt
->exclude_name_list
= filespec
->exclude_name_list
;
8308 new_stmt
->section_list
= section_list
;
8309 new_stmt
->keep_sections
= keep_sections
;
8310 lang_list_init (&new_stmt
->children
);
8311 lang_list_init (&new_stmt
->matching_sections
);
8312 analyze_walk_wild_section_handler (new_stmt
);
8315 printf ("wild %s(", new_stmt
->filename
? new_stmt
->filename
: "*");
8316 for (curr
= new_stmt
->section_list
; curr
; curr
= curr
->next
)
8317 printf ("%s ", curr
->spec
.name
? curr
->spec
.name
: "*");
8323 lang_section_start (const char *name
, etree_type
*address
,
8324 const segment_type
*segment
)
8326 lang_address_statement_type
*ad
;
8328 ad
= new_stat (lang_address_statement
, stat_ptr
);
8329 ad
->section_name
= name
;
8330 ad
->address
= address
;
8331 ad
->segment
= segment
;
8334 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
8335 because of a -e argument on the command line, or zero if this is
8336 called by ENTRY in a linker script. Command line arguments take
8340 lang_add_entry (const char *name
, bool cmdline
)
8342 if (entry_symbol
.name
== NULL
8344 || !entry_from_cmdline
)
8346 entry_symbol
.name
= name
;
8347 entry_from_cmdline
= cmdline
;
8351 /* Set the default start symbol to NAME. .em files should use this,
8352 not lang_add_entry, to override the use of "start" if neither the
8353 linker script nor the command line specifies an entry point. NAME
8354 must be permanently allocated. */
8356 lang_default_entry (const char *name
)
8358 entry_symbol_default
= name
;
8362 lang_add_target (const char *name
)
8364 lang_target_statement_type
*new_stmt
;
8366 new_stmt
= new_stat (lang_target_statement
, stat_ptr
);
8367 new_stmt
->target
= name
;
8371 lang_add_map (const char *name
)
8378 map_option_f
= true;
8386 lang_add_fill (fill_type
*fill
)
8388 lang_fill_statement_type
*new_stmt
;
8390 new_stmt
= new_stat (lang_fill_statement
, stat_ptr
);
8391 new_stmt
->fill
= fill
;
8395 lang_add_data (int type
, union etree_union
*exp
)
8397 lang_data_statement_type
*new_stmt
;
8399 new_stmt
= new_stat (lang_data_statement
, stat_ptr
);
8400 new_stmt
->exp
= exp
;
8401 new_stmt
->type
= type
;
8404 /* Convert escape codes in S.
8405 Supports \n, \r, \t and \NNN octals.
8406 Returns a copy of S in a malloc'ed buffer. */
8409 convert_string (const char * s
)
8411 size_t len
= strlen (s
);
8413 bool escape
= false;
8414 char * buffer
= malloc (len
+ 1);
8417 for (i
= 0, b
= buffer
; i
< len
; i
++)
8426 /* Ignore the escape. */
8429 case 'n': c
= '\n'; break;
8430 case 'r': c
= '\r'; break;
8431 case 't': c
= '\t'; break;
8441 /* We have an octal number. */
8443 unsigned int value
= c
- '0';
8446 if ((c
>= '0') && (c
<= '7'))
8454 if ((c
>= '0') && (c
<= '7'))
8465 /* octal: \777 is treated as '\077' + '7' */
8494 lang_add_string (size_t size
, const char *s
)
8500 string
= convert_string (s
);
8501 len
= strlen (string
);
8503 /* Check if it is ASCIZ command (len == 0) */
8505 /* Make sure that we include the terminating nul byte. */
8507 else if (len
>= size
)
8511 einfo (_("%P:%pS: warning: ASCII string does not fit in allocated space,"
8512 " truncated\n"), NULL
);
8515 for (i
= 0 ; i
< len
; i
++)
8516 lang_add_data (BYTE
, exp_intop (string
[i
]));
8519 lang_add_data (BYTE
, exp_intop ('\0'));
8524 /* Store the time of linking in the image */
8526 lang_add_timestamp (void)
8528 lang_add_data (QUAD
, exp_intop ((bfd_vma
) time (0)));
8531 /* Create a new reloc statement. RELOC is the BFD relocation type to
8532 generate. HOWTO is the corresponding howto structure (we could
8533 look this up, but the caller has already done so). SECTION is the
8534 section to generate a reloc against, or NAME is the name of the
8535 symbol to generate a reloc against. Exactly one of SECTION and
8536 NAME must be NULL. ADDEND is an expression for the addend. */
8539 lang_add_reloc (bfd_reloc_code_real_type reloc
,
8540 reloc_howto_type
*howto
,
8543 union etree_union
*addend
)
8545 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
8549 p
->section
= section
;
8551 p
->addend_exp
= addend
;
8553 p
->addend_value
= 0;
8554 p
->output_section
= NULL
;
8555 p
->output_offset
= 0;
8558 lang_assignment_statement_type
*
8559 lang_add_assignment (etree_type
*exp
)
8561 lang_assignment_statement_type
*new_stmt
;
8563 new_stmt
= new_stat (lang_assignment_statement
, stat_ptr
);
8564 new_stmt
->exp
= exp
;
8569 lang_add_attribute (enum statement_enum attribute
)
8571 new_statement (attribute
, sizeof (lang_statement_header_type
), stat_ptr
);
8575 lang_startup (const char *name
)
8577 if (first_file
->filename
!= NULL
)
8579 einfo (_("%F%P: multiple STARTUP files\n"));
8581 first_file
->filename
= name
;
8582 first_file
->local_sym_name
= name
;
8583 first_file
->flags
.real
= true;
8587 lang_float (bool maybe
)
8589 lang_float_flag
= maybe
;
8593 /* Work out the load- and run-time regions from a script statement, and
8594 store them in *LMA_REGION and *REGION respectively.
8596 MEMSPEC is the name of the run-time region, or the value of
8597 DEFAULT_MEMORY_REGION if the statement didn't specify one.
8598 LMA_MEMSPEC is the name of the load-time region, or null if the
8599 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
8600 had an explicit load address.
8602 It is an error to specify both a load region and a load address. */
8605 lang_get_regions (lang_memory_region_type
**region
,
8606 lang_memory_region_type
**lma_region
,
8607 const char *memspec
,
8608 const char *lma_memspec
,
8612 *lma_region
= lang_memory_region_lookup (lma_memspec
, false);
8614 /* If no runtime region or VMA has been specified, but the load region
8615 has been specified, then use the load region for the runtime region
8617 if (lma_memspec
!= NULL
8619 && strcmp (memspec
, DEFAULT_MEMORY_REGION
) == 0)
8620 *region
= *lma_region
;
8622 *region
= lang_memory_region_lookup (memspec
, false);
8624 if (have_lma
&& lma_memspec
!= 0)
8625 einfo (_("%X%P:%pS: section has both a load address and a load region\n"),
8630 lang_leave_output_section_statement (fill_type
*fill
, const char *memspec
,
8631 lang_output_section_phdr_list
*phdrs
,
8632 const char *lma_memspec
)
8634 lang_get_regions (¤t_section
->region
,
8635 ¤t_section
->lma_region
,
8636 memspec
, lma_memspec
,
8637 current_section
->load_base
!= NULL
,
8638 current_section
->addr_tree
!= NULL
);
8640 current_section
->fill
= fill
;
8641 current_section
->phdrs
= phdrs
;
8645 /* Set the output format type. -oformat overrides scripts. */
8648 lang_add_output_format (const char *format
,
8653 if (output_target
== NULL
|| !from_script
)
8655 if (command_line
.endian
== ENDIAN_BIG
8658 else if (command_line
.endian
== ENDIAN_LITTLE
8662 output_target
= format
;
8667 lang_add_insert (const char *where
, int is_before
)
8669 lang_insert_statement_type
*new_stmt
;
8671 new_stmt
= new_stat (lang_insert_statement
, stat_ptr
);
8672 new_stmt
->where
= where
;
8673 new_stmt
->is_before
= is_before
;
8674 saved_script_handle
= previous_script_handle
;
8677 /* Enter a group. This creates a new lang_group_statement, and sets
8678 stat_ptr to build new statements within the group. */
8681 lang_enter_group (void)
8683 lang_group_statement_type
*g
;
8685 g
= new_stat (lang_group_statement
, stat_ptr
);
8686 lang_list_init (&g
->children
);
8687 push_stat_ptr (&g
->children
);
8690 /* Leave a group. This just resets stat_ptr to start writing to the
8691 regular list of statements again. Note that this will not work if
8692 groups can occur inside anything else which can adjust stat_ptr,
8693 but currently they can't. */
8696 lang_leave_group (void)
8701 /* Add a new program header. This is called for each entry in a PHDRS
8702 command in a linker script. */
8705 lang_new_phdr (const char *name
,
8712 struct lang_phdr
*n
, **pp
;
8715 n
= stat_alloc (sizeof (struct lang_phdr
));
8718 n
->type
= exp_get_vma (type
, 0, "program header type");
8719 n
->filehdr
= filehdr
;
8724 hdrs
= n
->type
== 1 && (phdrs
|| filehdr
);
8726 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
8729 && !((*pp
)->filehdr
|| (*pp
)->phdrs
))
8731 einfo (_("%X%P:%pS: PHDRS and FILEHDR are not supported"
8732 " when prior PT_LOAD headers lack them\n"), NULL
);
8739 /* Record the program header information in the output BFD. FIXME: We
8740 should not be calling an ELF specific function here. */
8743 lang_record_phdrs (void)
8747 lang_output_section_phdr_list
*last
;
8748 struct lang_phdr
*l
;
8749 lang_output_section_statement_type
*os
;
8752 secs
= (asection
**) xmalloc (alc
* sizeof (asection
*));
8755 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
8762 for (os
= (void *) lang_os_list
.head
;
8766 lang_output_section_phdr_list
*pl
;
8768 if (os
->constraint
< 0)
8776 if (os
->sectype
== noload_section
8777 || os
->bfd_section
== NULL
8778 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
8781 /* Don't add orphans to PT_INTERP header. */
8787 lang_output_section_statement_type
*tmp_os
;
8789 /* If we have not run across a section with a program
8790 header assigned to it yet, then scan forwards to find
8791 one. This prevents inconsistencies in the linker's
8792 behaviour when a script has specified just a single
8793 header and there are sections in that script which are
8794 not assigned to it, and which occur before the first
8795 use of that header. See here for more details:
8796 http://sourceware.org/ml/binutils/2007-02/msg00291.html */
8797 for (tmp_os
= os
; tmp_os
; tmp_os
= tmp_os
->next
)
8800 last
= tmp_os
->phdrs
;
8804 einfo (_("%F%P: no sections assigned to phdrs\n"));
8809 if (os
->bfd_section
== NULL
)
8812 for (; pl
!= NULL
; pl
= pl
->next
)
8814 if (strcmp (pl
->name
, l
->name
) == 0)
8819 secs
= (asection
**) xrealloc (secs
,
8820 alc
* sizeof (asection
*));
8822 secs
[c
] = os
->bfd_section
;
8829 if (l
->flags
== NULL
)
8832 flags
= exp_get_vma (l
->flags
, 0, "phdr flags");
8837 at
= exp_get_vma (l
->at
, 0, "phdr load address");
8839 if (!bfd_record_phdr (link_info
.output_bfd
, l
->type
,
8840 l
->flags
!= NULL
, flags
, l
->at
!= NULL
,
8841 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
8842 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
8847 /* Make sure all the phdr assignments succeeded. */
8848 for (os
= (void *) lang_os_list
.head
;
8852 lang_output_section_phdr_list
*pl
;
8854 if (os
->constraint
< 0
8855 || os
->bfd_section
== NULL
)
8858 for (pl
= os
->phdrs
;
8861 if (!pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
8862 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
8863 os
->name
, pl
->name
);
8867 /* Record a list of sections which may not be cross referenced. */
8870 lang_add_nocrossref (lang_nocrossref_type
*l
)
8872 struct lang_nocrossrefs
*n
;
8874 n
= (struct lang_nocrossrefs
*) xmalloc (sizeof *n
);
8875 n
->next
= nocrossref_list
;
8877 n
->onlyfirst
= false;
8878 nocrossref_list
= n
;
8880 /* Set notice_all so that we get informed about all symbols. */
8881 link_info
.notice_all
= true;
8884 /* Record a section that cannot be referenced from a list of sections. */
8887 lang_add_nocrossref_to (lang_nocrossref_type
*l
)
8889 lang_add_nocrossref (l
);
8890 nocrossref_list
->onlyfirst
= true;
8893 /* Overlay handling. We handle overlays with some static variables. */
8895 /* The overlay virtual address. */
8896 static etree_type
*overlay_vma
;
8897 /* And subsection alignment. */
8898 static etree_type
*overlay_subalign
;
8900 /* An expression for the maximum section size seen so far. */
8901 static etree_type
*overlay_max
;
8903 /* A list of all the sections in this overlay. */
8905 struct overlay_list
{
8906 struct overlay_list
*next
;
8907 lang_output_section_statement_type
*os
;
8910 static struct overlay_list
*overlay_list
;
8912 /* Start handling an overlay. */
8915 lang_enter_overlay (etree_type
*vma_expr
, etree_type
*subalign
)
8917 /* The grammar should prevent nested overlays from occurring. */
8918 ASSERT (overlay_vma
== NULL
8919 && overlay_subalign
== NULL
8920 && overlay_max
== NULL
);
8922 overlay_vma
= vma_expr
;
8923 overlay_subalign
= subalign
;
8926 /* Start a section in an overlay. We handle this by calling
8927 lang_enter_output_section_statement with the correct VMA.
8928 lang_leave_overlay sets up the LMA and memory regions. */
8931 lang_enter_overlay_section (const char *name
)
8933 struct overlay_list
*n
;
8936 lang_enter_output_section_statement (name
, overlay_vma
, overlay_section
,
8937 0, 0, overlay_subalign
, 0, 0, 0);
8939 /* If this is the first section, then base the VMA of future
8940 sections on this one. This will work correctly even if `.' is
8941 used in the addresses. */
8942 if (overlay_list
== NULL
)
8943 overlay_vma
= exp_nameop (ADDR
, name
);
8945 /* Remember the section. */
8946 n
= (struct overlay_list
*) xmalloc (sizeof *n
);
8947 n
->os
= current_section
;
8948 n
->next
= overlay_list
;
8951 size
= exp_nameop (SIZEOF
, name
);
8953 /* Arrange to work out the maximum section end address. */
8954 if (overlay_max
== NULL
)
8957 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
8960 /* Finish a section in an overlay. There isn't any special to do
8964 lang_leave_overlay_section (fill_type
*fill
,
8965 lang_output_section_phdr_list
*phdrs
)
8972 name
= current_section
->name
;
8974 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
8975 region and that no load-time region has been specified. It doesn't
8976 really matter what we say here, since lang_leave_overlay will
8978 lang_leave_output_section_statement (fill
, DEFAULT_MEMORY_REGION
, phdrs
, 0);
8980 /* Define the magic symbols. */
8982 clean
= (char *) xmalloc (strlen (name
) + 1);
8984 for (s1
= name
; *s1
!= '\0'; s1
++)
8985 if (ISALNUM (*s1
) || *s1
== '_')
8989 buf
= (char *) xmalloc (strlen (clean
) + sizeof "__load_start_");
8990 sprintf (buf
, "__load_start_%s", clean
);
8991 lang_add_assignment (exp_provide (buf
,
8992 exp_nameop (LOADADDR
, name
),
8995 buf
= (char *) xmalloc (strlen (clean
) + sizeof "__load_stop_");
8996 sprintf (buf
, "__load_stop_%s", clean
);
8997 lang_add_assignment (exp_provide (buf
,
8999 exp_nameop (LOADADDR
, name
),
9000 exp_nameop (SIZEOF
, name
)),
9006 /* Finish an overlay. If there are any overlay wide settings, this
9007 looks through all the sections in the overlay and sets them. */
9010 lang_leave_overlay (etree_type
*lma_expr
,
9013 const char *memspec
,
9014 lang_output_section_phdr_list
*phdrs
,
9015 const char *lma_memspec
)
9017 lang_memory_region_type
*region
;
9018 lang_memory_region_type
*lma_region
;
9019 struct overlay_list
*l
;
9020 lang_nocrossref_type
*nocrossref
;
9022 lang_get_regions (®ion
, &lma_region
,
9023 memspec
, lma_memspec
,
9024 lma_expr
!= NULL
, false);
9028 /* After setting the size of the last section, set '.' to end of the
9030 if (overlay_list
!= NULL
)
9032 overlay_list
->os
->update_dot
= 1;
9033 overlay_list
->os
->update_dot_tree
9034 = exp_assign (".", exp_binop ('+', overlay_vma
, overlay_max
), false);
9040 struct overlay_list
*next
;
9042 if (fill
!= NULL
&& l
->os
->fill
== NULL
)
9045 l
->os
->region
= region
;
9046 l
->os
->lma_region
= lma_region
;
9048 /* The first section has the load address specified in the
9049 OVERLAY statement. The rest are worked out from that.
9050 The base address is not needed (and should be null) if
9051 an LMA region was specified. */
9054 l
->os
->load_base
= lma_expr
;
9055 l
->os
->sectype
= first_overlay_section
;
9057 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
9058 l
->os
->phdrs
= phdrs
;
9062 lang_nocrossref_type
*nc
;
9064 nc
= (lang_nocrossref_type
*) xmalloc (sizeof *nc
);
9065 nc
->name
= l
->os
->name
;
9066 nc
->next
= nocrossref
;
9075 if (nocrossref
!= NULL
)
9076 lang_add_nocrossref (nocrossref
);
9079 overlay_list
= NULL
;
9081 overlay_subalign
= NULL
;
9084 /* Version handling. This is only useful for ELF. */
9086 /* If PREV is NULL, return first version pattern matching particular symbol.
9087 If PREV is non-NULL, return first version pattern matching particular
9088 symbol after PREV (previously returned by lang_vers_match). */
9090 static struct bfd_elf_version_expr
*
9091 lang_vers_match (struct bfd_elf_version_expr_head
*head
,
9092 struct bfd_elf_version_expr
*prev
,
9096 const char *cxx_sym
= sym
;
9097 const char *java_sym
= sym
;
9098 struct bfd_elf_version_expr
*expr
= NULL
;
9099 enum demangling_styles curr_style
;
9101 curr_style
= CURRENT_DEMANGLING_STYLE
;
9102 cplus_demangle_set_style (no_demangling
);
9103 c_sym
= bfd_demangle (link_info
.output_bfd
, sym
, DMGL_NO_OPTS
);
9106 cplus_demangle_set_style (curr_style
);
9108 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
9110 cxx_sym
= bfd_demangle (link_info
.output_bfd
, sym
,
9111 DMGL_PARAMS
| DMGL_ANSI
);
9115 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
9117 java_sym
= bfd_demangle (link_info
.output_bfd
, sym
, DMGL_JAVA
);
9122 if (head
->htab
&& (prev
== NULL
|| prev
->literal
))
9124 struct bfd_elf_version_expr e
;
9126 switch (prev
? prev
->mask
: 0)
9129 if (head
->mask
& BFD_ELF_VERSION_C_TYPE
)
9132 expr
= (struct bfd_elf_version_expr
*)
9133 htab_find ((htab_t
) head
->htab
, &e
);
9134 while (expr
&& strcmp (expr
->pattern
, c_sym
) == 0)
9135 if (expr
->mask
== BFD_ELF_VERSION_C_TYPE
)
9141 case BFD_ELF_VERSION_C_TYPE
:
9142 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
9144 e
.pattern
= cxx_sym
;
9145 expr
= (struct bfd_elf_version_expr
*)
9146 htab_find ((htab_t
) head
->htab
, &e
);
9147 while (expr
&& strcmp (expr
->pattern
, cxx_sym
) == 0)
9148 if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
9154 case BFD_ELF_VERSION_CXX_TYPE
:
9155 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
9157 e
.pattern
= java_sym
;
9158 expr
= (struct bfd_elf_version_expr
*)
9159 htab_find ((htab_t
) head
->htab
, &e
);
9160 while (expr
&& strcmp (expr
->pattern
, java_sym
) == 0)
9161 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
9172 /* Finally, try the wildcards. */
9173 if (prev
== NULL
|| prev
->literal
)
9174 expr
= head
->remaining
;
9177 for (; expr
; expr
= expr
->next
)
9184 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
9187 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
9189 else if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
9193 if (fnmatch (expr
->pattern
, s
, 0) == 0)
9199 free ((char *) c_sym
);
9201 free ((char *) cxx_sym
);
9202 if (java_sym
!= sym
)
9203 free ((char *) java_sym
);
9207 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
9208 return a pointer to the symbol name with any backslash quotes removed. */
9211 realsymbol (const char *pattern
)
9214 bool changed
= false, backslash
= false;
9215 char *s
, *symbol
= (char *) xmalloc (strlen (pattern
) + 1);
9217 for (p
= pattern
, s
= symbol
; *p
!= '\0'; ++p
)
9219 /* It is a glob pattern only if there is no preceding
9223 /* Remove the preceding backslash. */
9230 if (*p
== '?' || *p
== '*' || *p
== '[')
9237 backslash
= *p
== '\\';
9253 /* This is called for each variable name or match expression. NEW_NAME is
9254 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
9255 pattern to be matched against symbol names. */
9257 struct bfd_elf_version_expr
*
9258 lang_new_vers_pattern (struct bfd_elf_version_expr
*orig
,
9259 const char *new_name
,
9263 struct bfd_elf_version_expr
*ret
;
9265 ret
= (struct bfd_elf_version_expr
*) xmalloc (sizeof *ret
);
9269 ret
->literal
= true;
9270 ret
->pattern
= literal_p
? new_name
: realsymbol (new_name
);
9271 if (ret
->pattern
== NULL
)
9273 ret
->pattern
= new_name
;
9274 ret
->literal
= false;
9277 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
9278 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
9279 else if (strcasecmp (lang
, "C++") == 0)
9280 ret
->mask
= BFD_ELF_VERSION_CXX_TYPE
;
9281 else if (strcasecmp (lang
, "Java") == 0)
9282 ret
->mask
= BFD_ELF_VERSION_JAVA_TYPE
;
9285 einfo (_("%X%P: unknown language `%s' in version information\n"),
9287 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
9290 return ldemul_new_vers_pattern (ret
);
9293 /* This is called for each set of variable names and match
9296 struct bfd_elf_version_tree
*
9297 lang_new_vers_node (struct bfd_elf_version_expr
*globals
,
9298 struct bfd_elf_version_expr
*locals
)
9300 struct bfd_elf_version_tree
*ret
;
9302 ret
= (struct bfd_elf_version_tree
*) xcalloc (1, sizeof *ret
);
9303 ret
->globals
.list
= globals
;
9304 ret
->locals
.list
= locals
;
9305 ret
->match
= lang_vers_match
;
9306 ret
->name_indx
= (unsigned int) -1;
9310 /* This static variable keeps track of version indices. */
9312 static int version_index
;
9315 version_expr_head_hash (const void *p
)
9317 const struct bfd_elf_version_expr
*e
=
9318 (const struct bfd_elf_version_expr
*) p
;
9320 return htab_hash_string (e
->pattern
);
9324 version_expr_head_eq (const void *p1
, const void *p2
)
9326 const struct bfd_elf_version_expr
*e1
=
9327 (const struct bfd_elf_version_expr
*) p1
;
9328 const struct bfd_elf_version_expr
*e2
=
9329 (const struct bfd_elf_version_expr
*) p2
;
9331 return strcmp (e1
->pattern
, e2
->pattern
) == 0;
9335 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head
*head
)
9338 struct bfd_elf_version_expr
*e
, *next
;
9339 struct bfd_elf_version_expr
**list_loc
, **remaining_loc
;
9341 for (e
= head
->list
; e
; e
= e
->next
)
9345 head
->mask
|= e
->mask
;
9350 head
->htab
= htab_create (count
* 2, version_expr_head_hash
,
9351 version_expr_head_eq
, NULL
);
9352 list_loc
= &head
->list
;
9353 remaining_loc
= &head
->remaining
;
9354 for (e
= head
->list
; e
; e
= next
)
9360 remaining_loc
= &e
->next
;
9364 void **loc
= htab_find_slot ((htab_t
) head
->htab
, e
, INSERT
);
9368 struct bfd_elf_version_expr
*e1
, *last
;
9370 e1
= (struct bfd_elf_version_expr
*) *loc
;
9374 if (e1
->mask
== e
->mask
)
9382 while (e1
&& strcmp (e1
->pattern
, e
->pattern
) == 0);
9386 /* This is a duplicate. */
9387 /* FIXME: Memory leak. Sometimes pattern is not
9388 xmalloced alone, but in larger chunk of memory. */
9389 /* free (e->pattern); */
9394 e
->next
= last
->next
;
9402 list_loc
= &e
->next
;
9406 *remaining_loc
= NULL
;
9407 *list_loc
= head
->remaining
;
9410 head
->remaining
= head
->list
;
9413 /* This is called when we know the name and dependencies of the
9417 lang_register_vers_node (const char *name
,
9418 struct bfd_elf_version_tree
*version
,
9419 struct bfd_elf_version_deps
*deps
)
9421 struct bfd_elf_version_tree
*t
, **pp
;
9422 struct bfd_elf_version_expr
*e1
;
9427 if (link_info
.version_info
!= NULL
9428 && (name
[0] == '\0' || link_info
.version_info
->name
[0] == '\0'))
9430 einfo (_("%X%P: anonymous version tag cannot be combined"
9431 " with other version tags\n"));
9436 /* Make sure this node has a unique name. */
9437 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9438 if (strcmp (t
->name
, name
) == 0)
9439 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
9441 lang_finalize_version_expr_head (&version
->globals
);
9442 lang_finalize_version_expr_head (&version
->locals
);
9444 /* Check the global and local match names, and make sure there
9445 aren't any duplicates. */
9447 for (e1
= version
->globals
.list
; e1
!= NULL
; e1
= e1
->next
)
9449 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9451 struct bfd_elf_version_expr
*e2
;
9453 if (t
->locals
.htab
&& e1
->literal
)
9455 e2
= (struct bfd_elf_version_expr
*)
9456 htab_find ((htab_t
) t
->locals
.htab
, e1
);
9457 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
9459 if (e1
->mask
== e2
->mask
)
9460 einfo (_("%X%P: duplicate expression `%s'"
9461 " in version information\n"), e1
->pattern
);
9465 else if (!e1
->literal
)
9466 for (e2
= t
->locals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
9467 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
9468 && e1
->mask
== e2
->mask
)
9469 einfo (_("%X%P: duplicate expression `%s'"
9470 " in version information\n"), e1
->pattern
);
9474 for (e1
= version
->locals
.list
; e1
!= NULL
; e1
= e1
->next
)
9476 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9478 struct bfd_elf_version_expr
*e2
;
9480 if (t
->globals
.htab
&& e1
->literal
)
9482 e2
= (struct bfd_elf_version_expr
*)
9483 htab_find ((htab_t
) t
->globals
.htab
, e1
);
9484 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
9486 if (e1
->mask
== e2
->mask
)
9487 einfo (_("%X%P: duplicate expression `%s'"
9488 " in version information\n"),
9493 else if (!e1
->literal
)
9494 for (e2
= t
->globals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
9495 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
9496 && e1
->mask
== e2
->mask
)
9497 einfo (_("%X%P: duplicate expression `%s'"
9498 " in version information\n"), e1
->pattern
);
9502 version
->deps
= deps
;
9503 version
->name
= name
;
9504 if (name
[0] != '\0')
9507 version
->vernum
= version_index
;
9510 version
->vernum
= 0;
9512 for (pp
= &link_info
.version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
9517 /* This is called when we see a version dependency. */
9519 struct bfd_elf_version_deps
*
9520 lang_add_vers_depend (struct bfd_elf_version_deps
*list
, const char *name
)
9522 struct bfd_elf_version_deps
*ret
;
9523 struct bfd_elf_version_tree
*t
;
9525 ret
= (struct bfd_elf_version_deps
*) xmalloc (sizeof *ret
);
9528 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9530 if (strcmp (t
->name
, name
) == 0)
9532 ret
->version_needed
= t
;
9537 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
9539 ret
->version_needed
= NULL
;
9544 lang_do_version_exports_section (void)
9546 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
9548 LANG_FOR_EACH_INPUT_STATEMENT (is
)
9550 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
9558 contents
= (char *) xmalloc (len
);
9559 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
9560 einfo (_("%X%P: unable to read .exports section contents\n"), sec
);
9563 while (p
< contents
+ len
)
9565 greg
= lang_new_vers_pattern (greg
, p
, NULL
, false);
9566 p
= strchr (p
, '\0') + 1;
9569 /* Do not free the contents, as we used them creating the regex. */
9571 /* Do not include this section in the link. */
9572 sec
->flags
|= SEC_EXCLUDE
| SEC_KEEP
;
9575 lreg
= lang_new_vers_pattern (NULL
, "*", NULL
, false);
9576 lang_register_vers_node (command_line
.version_exports_section
,
9577 lang_new_vers_node (greg
, lreg
), NULL
);
9580 /* Evaluate LENGTH and ORIGIN parts of MEMORY spec. This is initially
9581 called with UPDATE_REGIONS_P set to FALSE, in this case no errors are
9582 thrown, however, references to symbols in the origin and length fields
9583 will be pushed into the symbol table, this allows PROVIDE statements to
9584 then provide these symbols. This function is called a second time with
9585 UPDATE_REGIONS_P set to TRUE, this time the we update the actual region
9586 data structures, and throw errors if missing symbols are encountered. */
9589 lang_do_memory_regions (bool update_regions_p
)
9591 lang_memory_region_type
*r
= lang_memory_region_list
;
9593 for (; r
!= NULL
; r
= r
->next
)
9597 exp_fold_tree_no_dot (r
->origin_exp
);
9598 if (update_regions_p
)
9600 if (expld
.result
.valid_p
)
9602 r
->origin
= expld
.result
.value
;
9603 r
->current
= r
->origin
;
9606 einfo (_("%P: invalid origin for memory region %s\n"),
9612 exp_fold_tree_no_dot (r
->length_exp
);
9613 if (update_regions_p
)
9615 if (expld
.result
.valid_p
)
9616 r
->length
= expld
.result
.value
;
9618 einfo (_("%P: invalid length for memory region %s\n"),
9626 lang_add_unique (const char *name
)
9628 struct unique_sections
*ent
;
9630 for (ent
= unique_section_list
; ent
; ent
= ent
->next
)
9631 if (strcmp (ent
->name
, name
) == 0)
9634 ent
= (struct unique_sections
*) xmalloc (sizeof *ent
);
9635 ent
->name
= xstrdup (name
);
9636 ent
->next
= unique_section_list
;
9637 unique_section_list
= ent
;
9640 /* Append the list of dynamic symbols to the existing one. */
9643 lang_append_dynamic_list (struct bfd_elf_dynamic_list
**list_p
,
9644 struct bfd_elf_version_expr
*dynamic
)
9648 struct bfd_elf_version_expr
*tail
;
9649 for (tail
= dynamic
; tail
->next
!= NULL
; tail
= tail
->next
)
9651 tail
->next
= (*list_p
)->head
.list
;
9652 (*list_p
)->head
.list
= dynamic
;
9656 struct bfd_elf_dynamic_list
*d
;
9658 d
= (struct bfd_elf_dynamic_list
*) xcalloc (1, sizeof *d
);
9659 d
->head
.list
= dynamic
;
9660 d
->match
= lang_vers_match
;
9665 /* Append the list of C++ typeinfo dynamic symbols to the existing
9669 lang_append_dynamic_list_cpp_typeinfo (void)
9671 const char *symbols
[] =
9673 "typeinfo name for*",
9676 struct bfd_elf_version_expr
*dynamic
= NULL
;
9679 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
9680 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
9683 lang_append_dynamic_list (&link_info
.dynamic_list
, dynamic
);
9686 /* Append the list of C++ operator new and delete dynamic symbols to the
9690 lang_append_dynamic_list_cpp_new (void)
9692 const char *symbols
[] =
9697 struct bfd_elf_version_expr
*dynamic
= NULL
;
9700 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
9701 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
9704 lang_append_dynamic_list (&link_info
.dynamic_list
, dynamic
);
9707 /* Scan a space and/or comma separated string of features. */
9710 lang_ld_feature (char *str
)
9718 while (*p
== ',' || ISSPACE (*p
))
9723 while (*q
&& *q
!= ',' && !ISSPACE (*q
))
9727 if (strcasecmp (p
, "SANE_EXPR") == 0)
9728 config
.sane_expr
= true;
9730 einfo (_("%X%P: unknown feature `%s'\n"), p
);
9736 /* Pretty print memory amount. */
9739 lang_print_memory_size (uint64_t sz
)
9741 if ((sz
& 0x3fffffff) == 0)
9742 printf ("%10" PRIu64
" GB", sz
>> 30);
9743 else if ((sz
& 0xfffff) == 0)
9744 printf ("%10" PRIu64
" MB", sz
>> 20);
9745 else if ((sz
& 0x3ff) == 0)
9746 printf ("%10" PRIu64
" KB", sz
>> 10);
9748 printf (" %10" PRIu64
" B", sz
);
9751 /* Implement --print-memory-usage: disply per region memory usage. */
9754 lang_print_memory_usage (void)
9756 lang_memory_region_type
*r
;
9758 printf ("Memory region Used Size Region Size %%age Used\n");
9759 for (r
= lang_memory_region_list
; r
->next
!= NULL
; r
= r
->next
)
9761 bfd_vma used_length
= r
->current
- r
->origin
;
9763 printf ("%16s: ",r
->name_list
.name
);
9764 lang_print_memory_size (used_length
);
9765 lang_print_memory_size (r
->length
);
9769 double percent
= used_length
* 100.0 / r
->length
;
9770 printf (" %6.2f%%", percent
);