1 /* Linker command language support.
2 Copyright (C) 1991-2019 Free Software Foundation, Inc.
4 This file is part of the GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
24 #include "libiberty.h"
25 #include "filenames.h"
26 #include "safe-ctype.h"
47 #endif /* ENABLE_PLUGINS */
50 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
53 /* Convert between addresses in bytes and sizes in octets.
54 For currently supported targets, octets_per_byte is always a power
55 of two, so we can use shifts. */
56 #define TO_ADDR(X) ((X) >> opb_shift)
57 #define TO_SIZE(X) ((X) << opb_shift)
59 /* Local variables. */
60 static struct obstack stat_obstack
;
61 static struct obstack map_obstack
;
63 #define obstack_chunk_alloc xmalloc
64 #define obstack_chunk_free free
65 static const char *entry_symbol_default
= "start";
66 static bfd_boolean map_head_is_link_order
= FALSE
;
67 static lang_output_section_statement_type
*default_common_section
;
68 static bfd_boolean map_option_f
;
69 static bfd_vma print_dot
;
70 static lang_input_statement_type
*first_file
;
71 static const char *current_target
;
72 /* Header for list of statements corresponding to any files involved in the
73 link, either specified from the command-line or added implicitely (eg.
74 archive member used to resolved undefined symbol, wildcard statement from
75 linker script, etc.). Next pointer is in next field of a
76 lang_statement_header_type (reached via header field in a
77 lang_statement_union). */
78 static lang_statement_list_type statement_list
;
79 static lang_statement_list_type
*stat_save
[10];
80 static lang_statement_list_type
**stat_save_ptr
= &stat_save
[0];
81 static struct unique_sections
*unique_section_list
;
82 static struct asneeded_minfo
*asneeded_list_head
;
83 static unsigned int opb_shift
= 0;
85 /* Forward declarations. */
86 static void exp_init_os (etree_type
*);
87 static lang_input_statement_type
*lookup_name (const char *);
88 static void insert_undefined (const char *);
89 static bfd_boolean
sort_def_symbol (struct bfd_link_hash_entry
*, void *);
90 static void print_statement (lang_statement_union_type
*,
91 lang_output_section_statement_type
*);
92 static void print_statement_list (lang_statement_union_type
*,
93 lang_output_section_statement_type
*);
94 static void print_statements (void);
95 static void print_input_section (asection
*, bfd_boolean
);
96 static bfd_boolean
lang_one_common (struct bfd_link_hash_entry
*, void *);
97 static void lang_record_phdrs (void);
98 static void lang_do_version_exports_section (void);
99 static void lang_finalize_version_expr_head
100 (struct bfd_elf_version_expr_head
*);
101 static void lang_do_memory_regions (void);
103 /* Exported variables. */
104 const char *output_target
;
105 lang_output_section_statement_type
*abs_output_section
;
106 lang_statement_list_type lang_os_list
;
107 lang_statement_list_type
*stat_ptr
= &statement_list
;
108 /* Header for list of statements corresponding to files used in the final
109 executable. This can be either object file specified on the command-line
110 or library member resolving an undefined reference. Next pointer is in next
111 field of a lang_input_statement_type (reached via input_statement field in a
112 lang_statement_union). */
113 lang_statement_list_type file_chain
= { NULL
, NULL
};
114 /* Header for list of statements corresponding to files specified on the
115 command-line for linking. It thus contains real object files and archive
116 but not archive members. Next pointer is in next_real_file field of a
117 lang_input_statement_type statement (reached via input_statement field in a
118 lang_statement_union). */
119 lang_statement_list_type input_file_chain
;
120 struct bfd_sym_chain entry_symbol
= { NULL
, NULL
};
121 const char *entry_section
= ".text";
122 struct lang_input_statement_flags input_flags
;
123 bfd_boolean entry_from_cmdline
;
124 bfd_boolean undef_from_cmdline
;
125 bfd_boolean lang_has_input_file
= FALSE
;
126 bfd_boolean had_output_filename
= FALSE
;
127 bfd_boolean lang_float_flag
= FALSE
;
128 bfd_boolean delete_output_file_on_failure
= FALSE
;
129 struct lang_phdr
*lang_phdr_list
;
130 struct lang_nocrossrefs
*nocrossref_list
;
131 struct asneeded_minfo
**asneeded_list_tail
;
132 static ctf_file_t
*ctf_output
;
134 /* Functions that traverse the linker script and might evaluate
135 DEFINED() need to increment this at the start of the traversal. */
136 int lang_statement_iteration
= 0;
138 /* Return TRUE if the PATTERN argument is a wildcard pattern.
139 Although backslashes are treated specially if a pattern contains
140 wildcards, we do not consider the mere presence of a backslash to
141 be enough to cause the pattern to be treated as a wildcard.
142 That lets us handle DOS filenames more naturally. */
143 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
145 #define new_stat(x, y) \
146 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
148 #define outside_section_address(q) \
149 ((q)->output_offset + (q)->output_section->vma)
151 #define outside_symbol_address(q) \
152 ((q)->value + outside_section_address (q->section))
154 #define SECTION_NAME_MAP_LENGTH (16)
156 /* CTF sections smaller than this are not compressed: compression of
157 dictionaries this small doesn't gain much, and this lets consumers mmap the
158 sections directly out of the ELF file and use them with no decompression
159 overhead if they want to. */
160 #define CTF_COMPRESSION_THRESHOLD 4096
163 stat_alloc (size_t size
)
165 return obstack_alloc (&stat_obstack
, size
);
169 name_match (const char *pattern
, const char *name
)
171 if (wildcardp (pattern
))
172 return fnmatch (pattern
, name
, 0);
173 return strcmp (pattern
, name
);
176 /* If PATTERN is of the form archive:file, return a pointer to the
177 separator. If not, return NULL. */
180 archive_path (const char *pattern
)
184 if (link_info
.path_separator
== 0)
187 p
= strchr (pattern
, link_info
.path_separator
);
188 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
189 if (p
== NULL
|| link_info
.path_separator
!= ':')
192 /* Assume a match on the second char is part of drive specifier,
193 as in "c:\silly.dos". */
194 if (p
== pattern
+ 1 && ISALPHA (*pattern
))
195 p
= strchr (p
+ 1, link_info
.path_separator
);
200 /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path,
201 return whether F matches FILE_SPEC. */
204 input_statement_is_archive_path (const char *file_spec
, char *sep
,
205 lang_input_statement_type
*f
)
207 bfd_boolean match
= FALSE
;
210 || name_match (sep
+ 1, f
->filename
) == 0)
211 && ((sep
!= file_spec
)
212 == (f
->the_bfd
!= NULL
&& f
->the_bfd
->my_archive
!= NULL
)))
216 if (sep
!= file_spec
)
218 const char *aname
= f
->the_bfd
->my_archive
->filename
;
220 match
= name_match (file_spec
, aname
) == 0;
221 *sep
= link_info
.path_separator
;
228 unique_section_p (const asection
*sec
,
229 const lang_output_section_statement_type
*os
)
231 struct unique_sections
*unam
;
234 if (!link_info
.resolve_section_groups
235 && sec
->owner
!= NULL
236 && bfd_is_group_section (sec
->owner
, sec
))
238 && strcmp (os
->name
, DISCARD_SECTION_NAME
) == 0);
241 for (unam
= unique_section_list
; unam
; unam
= unam
->next
)
242 if (name_match (unam
->name
, secnam
) == 0)
248 /* Generic traversal routines for finding matching sections. */
250 /* Return true if FILE matches a pattern in EXCLUDE_LIST, otherwise return
254 walk_wild_file_in_exclude_list (struct name_list
*exclude_list
,
255 lang_input_statement_type
*file
)
257 struct name_list
*list_tmp
;
259 for (list_tmp
= exclude_list
;
261 list_tmp
= list_tmp
->next
)
263 char *p
= archive_path (list_tmp
->name
);
267 if (input_statement_is_archive_path (list_tmp
->name
, p
, file
))
271 else if (name_match (list_tmp
->name
, file
->filename
) == 0)
274 /* FIXME: Perhaps remove the following at some stage? Matching
275 unadorned archives like this was never documented and has
276 been superceded by the archive:path syntax. */
277 else if (file
->the_bfd
!= NULL
278 && file
->the_bfd
->my_archive
!= NULL
279 && name_match (list_tmp
->name
,
280 file
->the_bfd
->my_archive
->filename
) == 0)
287 /* Try processing a section against a wildcard. This just calls
288 the callback unless the filename exclusion list is present
289 and excludes the file. It's hardly ever present so this
290 function is very fast. */
293 walk_wild_consider_section (lang_wild_statement_type
*ptr
,
294 lang_input_statement_type
*file
,
296 struct wildcard_list
*sec
,
300 /* Don't process sections from files which were excluded. */
301 if (walk_wild_file_in_exclude_list (sec
->spec
.exclude_name_list
, file
))
304 (*callback
) (ptr
, sec
, s
, ptr
->section_flag_list
, file
, data
);
307 /* Lowest common denominator routine that can handle everything correctly,
311 walk_wild_section_general (lang_wild_statement_type
*ptr
,
312 lang_input_statement_type
*file
,
317 struct wildcard_list
*sec
;
319 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
321 sec
= ptr
->section_list
;
323 (*callback
) (ptr
, sec
, s
, ptr
->section_flag_list
, file
, data
);
327 bfd_boolean skip
= FALSE
;
329 if (sec
->spec
.name
!= NULL
)
331 const char *sname
= bfd_section_name (s
);
333 skip
= name_match (sec
->spec
.name
, sname
) != 0;
337 walk_wild_consider_section (ptr
, file
, s
, sec
, callback
, data
);
344 /* Routines to find a single section given its name. If there's more
345 than one section with that name, we report that. */
349 asection
*found_section
;
350 bfd_boolean multiple_sections_found
;
351 } section_iterator_callback_data
;
354 section_iterator_callback (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*s
, void *data
)
356 section_iterator_callback_data
*d
= (section_iterator_callback_data
*) data
;
358 if (d
->found_section
!= NULL
)
360 d
->multiple_sections_found
= TRUE
;
364 d
->found_section
= s
;
369 find_section (lang_input_statement_type
*file
,
370 struct wildcard_list
*sec
,
371 bfd_boolean
*multiple_sections_found
)
373 section_iterator_callback_data cb_data
= { NULL
, FALSE
};
375 bfd_get_section_by_name_if (file
->the_bfd
, sec
->spec
.name
,
376 section_iterator_callback
, &cb_data
);
377 *multiple_sections_found
= cb_data
.multiple_sections_found
;
378 return cb_data
.found_section
;
381 /* Code for handling simple wildcards without going through fnmatch,
382 which can be expensive because of charset translations etc. */
384 /* A simple wild is a literal string followed by a single '*',
385 where the literal part is at least 4 characters long. */
388 is_simple_wild (const char *name
)
390 size_t len
= strcspn (name
, "*?[");
391 return len
>= 4 && name
[len
] == '*' && name
[len
+ 1] == '\0';
395 match_simple_wild (const char *pattern
, const char *name
)
397 /* The first four characters of the pattern are guaranteed valid
398 non-wildcard characters. So we can go faster. */
399 if (pattern
[0] != name
[0] || pattern
[1] != name
[1]
400 || pattern
[2] != name
[2] || pattern
[3] != name
[3])
405 while (*pattern
!= '*')
406 if (*name
++ != *pattern
++)
412 /* Return the numerical value of the init_priority attribute from
413 section name NAME. */
416 get_init_priority (const asection
*sec
)
418 const char *name
= bfd_section_name (sec
);
421 /* GCC uses the following section names for the init_priority
422 attribute with numerical values 101 to 65535 inclusive. A
423 lower value means a higher priority.
425 1: .init_array.NNNNN/.fini_array.NNNNN: Where NNNNN is the
426 decimal numerical value of the init_priority attribute.
427 The order of execution in .init_array is forward and
428 .fini_array is backward.
429 2: .ctors.NNNNN/.dtors.NNNNN: Where NNNNN is 65535 minus the
430 decimal numerical value of the init_priority attribute.
431 The order of execution in .ctors is backward and .dtors
434 .init_array.NNNNN sections would normally be placed in an output
435 .init_array section, .fini_array.NNNNN in .fini_array,
436 .ctors.NNNNN in .ctors, and .dtors.NNNNN in .dtors. This means
437 we should sort by increasing number (and could just use
438 SORT_BY_NAME in scripts). However if .ctors.NNNNN sections are
439 being placed in .init_array (which may also contain
440 .init_array.NNNNN sections) or .dtors.NNNNN sections are being
441 placed in .fini_array then we need to extract the init_priority
442 attribute and sort on that. */
443 dot
= strrchr (name
, '.');
444 if (dot
!= NULL
&& ISDIGIT (dot
[1]))
447 unsigned long init_priority
= strtoul (dot
+ 1, &end
, 10);
451 && (strncmp (name
, ".ctors", 6) == 0
452 || strncmp (name
, ".dtors", 6) == 0))
453 init_priority
= 65535 - init_priority
;
454 if (init_priority
<= INT_MAX
)
455 return init_priority
;
461 /* Compare sections ASEC and BSEC according to SORT. */
464 compare_section (sort_type sort
, asection
*asec
, asection
*bsec
)
467 int a_priority
, b_priority
;
474 case by_init_priority
:
475 a_priority
= get_init_priority (asec
);
476 b_priority
= get_init_priority (bsec
);
477 if (a_priority
< 0 || b_priority
< 0)
479 ret
= a_priority
- b_priority
;
485 case by_alignment_name
:
486 ret
= bfd_section_alignment (bsec
) - bfd_section_alignment (asec
);
493 ret
= strcmp (bfd_section_name (asec
), bfd_section_name (bsec
));
496 case by_name_alignment
:
497 ret
= strcmp (bfd_section_name (asec
), bfd_section_name (bsec
));
503 ret
= bfd_section_alignment (bsec
) - bfd_section_alignment (asec
);
510 /* Build a Binary Search Tree to sort sections, unlike insertion sort
511 used in wild_sort(). BST is considerably faster if the number of
512 of sections are large. */
514 static lang_section_bst_type
**
515 wild_sort_fast (lang_wild_statement_type
*wild
,
516 struct wildcard_list
*sec
,
517 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
520 lang_section_bst_type
**tree
;
523 if (!wild
->filenames_sorted
524 && (sec
== NULL
|| sec
->spec
.sorted
== none
))
526 /* Append at the right end of tree. */
528 tree
= &((*tree
)->right
);
534 /* Find the correct node to append this section. */
535 if (compare_section (sec
->spec
.sorted
, section
, (*tree
)->section
) < 0)
536 tree
= &((*tree
)->left
);
538 tree
= &((*tree
)->right
);
544 /* Use wild_sort_fast to build a BST to sort sections. */
547 output_section_callback_fast (lang_wild_statement_type
*ptr
,
548 struct wildcard_list
*sec
,
550 struct flag_info
*sflag_list ATTRIBUTE_UNUSED
,
551 lang_input_statement_type
*file
,
554 lang_section_bst_type
*node
;
555 lang_section_bst_type
**tree
;
556 lang_output_section_statement_type
*os
;
558 os
= (lang_output_section_statement_type
*) output
;
560 if (unique_section_p (section
, os
))
563 node
= (lang_section_bst_type
*) xmalloc (sizeof (lang_section_bst_type
));
566 node
->section
= section
;
568 tree
= wild_sort_fast (ptr
, sec
, file
, section
);
573 /* Convert a sorted sections' BST back to list form. */
576 output_section_callback_tree_to_list (lang_wild_statement_type
*ptr
,
577 lang_section_bst_type
*tree
,
581 output_section_callback_tree_to_list (ptr
, tree
->left
, output
);
583 lang_add_section (&ptr
->children
, tree
->section
, NULL
,
584 (lang_output_section_statement_type
*) output
);
587 output_section_callback_tree_to_list (ptr
, tree
->right
, output
);
592 /* Specialized, optimized routines for handling different kinds of
596 walk_wild_section_specs1_wild0 (lang_wild_statement_type
*ptr
,
597 lang_input_statement_type
*file
,
601 /* We can just do a hash lookup for the section with the right name.
602 But if that lookup discovers more than one section with the name
603 (should be rare), we fall back to the general algorithm because
604 we would otherwise have to sort the sections to make sure they
605 get processed in the bfd's order. */
606 bfd_boolean multiple_sections_found
;
607 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
608 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
);
610 if (multiple_sections_found
)
611 walk_wild_section_general (ptr
, file
, callback
, data
);
613 walk_wild_consider_section (ptr
, file
, s0
, sec0
, callback
, data
);
617 walk_wild_section_specs1_wild1 (lang_wild_statement_type
*ptr
,
618 lang_input_statement_type
*file
,
623 struct wildcard_list
*wildsec0
= ptr
->handler_data
[0];
625 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
627 const char *sname
= bfd_section_name (s
);
628 bfd_boolean skip
= !match_simple_wild (wildsec0
->spec
.name
, sname
);
631 walk_wild_consider_section (ptr
, file
, s
, wildsec0
, callback
, data
);
636 walk_wild_section_specs2_wild1 (lang_wild_statement_type
*ptr
,
637 lang_input_statement_type
*file
,
642 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
643 struct wildcard_list
*wildsec1
= ptr
->handler_data
[1];
644 bfd_boolean multiple_sections_found
;
645 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
);
647 if (multiple_sections_found
)
649 walk_wild_section_general (ptr
, file
, callback
, data
);
653 /* Note that if the section was not found, s0 is NULL and
654 we'll simply never succeed the s == s0 test below. */
655 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
657 /* Recall that in this code path, a section cannot satisfy more
658 than one spec, so if s == s0 then it cannot match
661 walk_wild_consider_section (ptr
, file
, s
, sec0
, callback
, data
);
664 const char *sname
= bfd_section_name (s
);
665 bfd_boolean skip
= !match_simple_wild (wildsec1
->spec
.name
, sname
);
668 walk_wild_consider_section (ptr
, file
, s
, wildsec1
, callback
,
675 walk_wild_section_specs3_wild2 (lang_wild_statement_type
*ptr
,
676 lang_input_statement_type
*file
,
681 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
682 struct wildcard_list
*wildsec1
= ptr
->handler_data
[1];
683 struct wildcard_list
*wildsec2
= ptr
->handler_data
[2];
684 bfd_boolean multiple_sections_found
;
685 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
);
687 if (multiple_sections_found
)
689 walk_wild_section_general (ptr
, file
, callback
, data
);
693 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
696 walk_wild_consider_section (ptr
, file
, s
, sec0
, callback
, data
);
699 const char *sname
= bfd_section_name (s
);
700 bfd_boolean skip
= !match_simple_wild (wildsec1
->spec
.name
, sname
);
703 walk_wild_consider_section (ptr
, file
, s
, wildsec1
, callback
, data
);
706 skip
= !match_simple_wild (wildsec2
->spec
.name
, sname
);
708 walk_wild_consider_section (ptr
, file
, s
, wildsec2
, callback
,
716 walk_wild_section_specs4_wild2 (lang_wild_statement_type
*ptr
,
717 lang_input_statement_type
*file
,
722 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
723 struct wildcard_list
*sec1
= ptr
->handler_data
[1];
724 struct wildcard_list
*wildsec2
= ptr
->handler_data
[2];
725 struct wildcard_list
*wildsec3
= ptr
->handler_data
[3];
726 bfd_boolean multiple_sections_found
;
727 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
), *s1
;
729 if (multiple_sections_found
)
731 walk_wild_section_general (ptr
, file
, callback
, data
);
735 s1
= find_section (file
, sec1
, &multiple_sections_found
);
736 if (multiple_sections_found
)
738 walk_wild_section_general (ptr
, file
, callback
, data
);
742 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
745 walk_wild_consider_section (ptr
, file
, s
, sec0
, callback
, data
);
748 walk_wild_consider_section (ptr
, file
, s
, sec1
, callback
, data
);
751 const char *sname
= bfd_section_name (s
);
752 bfd_boolean skip
= !match_simple_wild (wildsec2
->spec
.name
,
756 walk_wild_consider_section (ptr
, file
, s
, wildsec2
, callback
,
760 skip
= !match_simple_wild (wildsec3
->spec
.name
, sname
);
762 walk_wild_consider_section (ptr
, file
, s
, wildsec3
,
770 walk_wild_section (lang_wild_statement_type
*ptr
,
771 lang_input_statement_type
*file
,
775 if (file
->flags
.just_syms
)
778 (*ptr
->walk_wild_section_handler
) (ptr
, file
, callback
, data
);
781 /* Returns TRUE when name1 is a wildcard spec that might match
782 something name2 can match. We're conservative: we return FALSE
783 only if the prefixes of name1 and name2 are different up to the
784 first wildcard character. */
787 wild_spec_can_overlap (const char *name1
, const char *name2
)
789 size_t prefix1_len
= strcspn (name1
, "?*[");
790 size_t prefix2_len
= strcspn (name2
, "?*[");
791 size_t min_prefix_len
;
793 /* Note that if there is no wildcard character, then we treat the
794 terminating 0 as part of the prefix. Thus ".text" won't match
795 ".text." or ".text.*", for example. */
796 if (name1
[prefix1_len
] == '\0')
798 if (name2
[prefix2_len
] == '\0')
801 min_prefix_len
= prefix1_len
< prefix2_len
? prefix1_len
: prefix2_len
;
803 return memcmp (name1
, name2
, min_prefix_len
) == 0;
806 /* Select specialized code to handle various kinds of wildcard
810 analyze_walk_wild_section_handler (lang_wild_statement_type
*ptr
)
813 int wild_name_count
= 0;
814 struct wildcard_list
*sec
;
818 ptr
->walk_wild_section_handler
= walk_wild_section_general
;
819 ptr
->handler_data
[0] = NULL
;
820 ptr
->handler_data
[1] = NULL
;
821 ptr
->handler_data
[2] = NULL
;
822 ptr
->handler_data
[3] = NULL
;
825 /* Count how many wildcard_specs there are, and how many of those
826 actually use wildcards in the name. Also, bail out if any of the
827 wildcard names are NULL. (Can this actually happen?
828 walk_wild_section used to test for it.) And bail out if any
829 of the wildcards are more complex than a simple string
830 ending in a single '*'. */
831 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
834 if (sec
->spec
.name
== NULL
)
836 if (wildcardp (sec
->spec
.name
))
839 if (!is_simple_wild (sec
->spec
.name
))
844 /* The zero-spec case would be easy to optimize but it doesn't
845 happen in practice. Likewise, more than 4 specs doesn't
846 happen in practice. */
847 if (sec_count
== 0 || sec_count
> 4)
850 /* Check that no two specs can match the same section. */
851 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
853 struct wildcard_list
*sec2
;
854 for (sec2
= sec
->next
; sec2
!= NULL
; sec2
= sec2
->next
)
856 if (wild_spec_can_overlap (sec
->spec
.name
, sec2
->spec
.name
))
861 signature
= (sec_count
<< 8) + wild_name_count
;
865 ptr
->walk_wild_section_handler
= walk_wild_section_specs1_wild0
;
868 ptr
->walk_wild_section_handler
= walk_wild_section_specs1_wild1
;
871 ptr
->walk_wild_section_handler
= walk_wild_section_specs2_wild1
;
874 ptr
->walk_wild_section_handler
= walk_wild_section_specs3_wild2
;
877 ptr
->walk_wild_section_handler
= walk_wild_section_specs4_wild2
;
883 /* Now fill the data array with pointers to the specs, first the
884 specs with non-wildcard names, then the specs with wildcard
885 names. It's OK to process the specs in different order from the
886 given order, because we've already determined that no section
887 will match more than one spec. */
889 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
890 if (!wildcardp (sec
->spec
.name
))
891 ptr
->handler_data
[data_counter
++] = sec
;
892 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
893 if (wildcardp (sec
->spec
.name
))
894 ptr
->handler_data
[data_counter
++] = sec
;
897 /* Handle a wild statement for a single file F. */
900 walk_wild_file (lang_wild_statement_type
*s
,
901 lang_input_statement_type
*f
,
905 if (walk_wild_file_in_exclude_list (s
->exclude_name_list
, f
))
908 if (f
->the_bfd
== NULL
909 || !bfd_check_format (f
->the_bfd
, bfd_archive
))
910 walk_wild_section (s
, f
, callback
, data
);
915 /* This is an archive file. We must map each member of the
916 archive separately. */
917 member
= bfd_openr_next_archived_file (f
->the_bfd
, NULL
);
918 while (member
!= NULL
)
920 /* When lookup_name is called, it will call the add_symbols
921 entry point for the archive. For each element of the
922 archive which is included, BFD will call ldlang_add_file,
923 which will set the usrdata field of the member to the
924 lang_input_statement. */
925 if (bfd_usrdata (member
) != NULL
)
926 walk_wild_section (s
, bfd_usrdata (member
), callback
, data
);
928 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
934 walk_wild (lang_wild_statement_type
*s
, callback_t callback
, void *data
)
936 const char *file_spec
= s
->filename
;
939 if (file_spec
== NULL
)
941 /* Perform the iteration over all files in the list. */
942 LANG_FOR_EACH_INPUT_STATEMENT (f
)
944 walk_wild_file (s
, f
, callback
, data
);
947 else if ((p
= archive_path (file_spec
)) != NULL
)
949 LANG_FOR_EACH_INPUT_STATEMENT (f
)
951 if (input_statement_is_archive_path (file_spec
, p
, f
))
952 walk_wild_file (s
, f
, callback
, data
);
955 else if (wildcardp (file_spec
))
957 LANG_FOR_EACH_INPUT_STATEMENT (f
)
959 if (fnmatch (file_spec
, f
->filename
, 0) == 0)
960 walk_wild_file (s
, f
, callback
, data
);
965 lang_input_statement_type
*f
;
967 /* Perform the iteration over a single file. */
968 f
= lookup_name (file_spec
);
970 walk_wild_file (s
, f
, callback
, data
);
974 /* lang_for_each_statement walks the parse tree and calls the provided
975 function for each node, except those inside output section statements
976 with constraint set to -1. */
979 lang_for_each_statement_worker (void (*func
) (lang_statement_union_type
*),
980 lang_statement_union_type
*s
)
982 for (; s
!= NULL
; s
= s
->header
.next
)
986 switch (s
->header
.type
)
988 case lang_constructors_statement_enum
:
989 lang_for_each_statement_worker (func
, constructor_list
.head
);
991 case lang_output_section_statement_enum
:
992 if (s
->output_section_statement
.constraint
!= -1)
993 lang_for_each_statement_worker
994 (func
, s
->output_section_statement
.children
.head
);
996 case lang_wild_statement_enum
:
997 lang_for_each_statement_worker (func
,
998 s
->wild_statement
.children
.head
);
1000 case lang_group_statement_enum
:
1001 lang_for_each_statement_worker (func
,
1002 s
->group_statement
.children
.head
);
1004 case lang_data_statement_enum
:
1005 case lang_reloc_statement_enum
:
1006 case lang_object_symbols_statement_enum
:
1007 case lang_output_statement_enum
:
1008 case lang_target_statement_enum
:
1009 case lang_input_section_enum
:
1010 case lang_input_statement_enum
:
1011 case lang_assignment_statement_enum
:
1012 case lang_padding_statement_enum
:
1013 case lang_address_statement_enum
:
1014 case lang_fill_statement_enum
:
1015 case lang_insert_statement_enum
:
1025 lang_for_each_statement (void (*func
) (lang_statement_union_type
*))
1027 lang_for_each_statement_worker (func
, statement_list
.head
);
1030 /*----------------------------------------------------------------------*/
1033 lang_list_init (lang_statement_list_type
*list
)
1036 list
->tail
= &list
->head
;
1040 lang_statement_append (lang_statement_list_type
*list
,
1044 *(list
->tail
) = element
;
1049 push_stat_ptr (lang_statement_list_type
*new_ptr
)
1051 if (stat_save_ptr
>= stat_save
+ sizeof (stat_save
) / sizeof (stat_save
[0]))
1053 *stat_save_ptr
++ = stat_ptr
;
1060 if (stat_save_ptr
<= stat_save
)
1062 stat_ptr
= *--stat_save_ptr
;
1065 /* Build a new statement node for the parse tree. */
1067 static lang_statement_union_type
*
1068 new_statement (enum statement_enum type
,
1070 lang_statement_list_type
*list
)
1072 lang_statement_union_type
*new_stmt
;
1074 new_stmt
= stat_alloc (size
);
1075 new_stmt
->header
.type
= type
;
1076 new_stmt
->header
.next
= NULL
;
1077 lang_statement_append (list
, new_stmt
, &new_stmt
->header
.next
);
1081 /* Build a new input file node for the language. There are several
1082 ways in which we treat an input file, eg, we only look at symbols,
1083 or prefix it with a -l etc.
1085 We can be supplied with requests for input files more than once;
1086 they may, for example be split over several lines like foo.o(.text)
1087 foo.o(.data) etc, so when asked for a file we check that we haven't
1088 got it already so we don't duplicate the bfd. */
1090 static lang_input_statement_type
*
1091 new_afile (const char *name
,
1092 lang_input_file_enum_type file_type
,
1095 lang_input_statement_type
*p
;
1097 lang_has_input_file
= TRUE
;
1099 p
= new_stat (lang_input_statement
, stat_ptr
);
1100 memset (&p
->the_bfd
, 0,
1101 sizeof (*p
) - offsetof (lang_input_statement_type
, the_bfd
));
1103 p
->flags
.dynamic
= input_flags
.dynamic
;
1104 p
->flags
.add_DT_NEEDED_for_dynamic
= input_flags
.add_DT_NEEDED_for_dynamic
;
1105 p
->flags
.add_DT_NEEDED_for_regular
= input_flags
.add_DT_NEEDED_for_regular
;
1106 p
->flags
.whole_archive
= input_flags
.whole_archive
;
1107 p
->flags
.sysrooted
= input_flags
.sysrooted
;
1111 case lang_input_file_is_symbols_only_enum
:
1113 p
->local_sym_name
= name
;
1114 p
->flags
.real
= TRUE
;
1115 p
->flags
.just_syms
= TRUE
;
1117 case lang_input_file_is_fake_enum
:
1119 p
->local_sym_name
= name
;
1121 case lang_input_file_is_l_enum
:
1122 if (name
[0] == ':' && name
[1] != '\0')
1124 p
->filename
= name
+ 1;
1125 p
->flags
.full_name_provided
= TRUE
;
1129 p
->local_sym_name
= concat ("-l", name
, (const char *) NULL
);
1130 p
->flags
.maybe_archive
= TRUE
;
1131 p
->flags
.real
= TRUE
;
1132 p
->flags
.search_dirs
= TRUE
;
1134 case lang_input_file_is_marker_enum
:
1136 p
->local_sym_name
= name
;
1137 p
->flags
.search_dirs
= TRUE
;
1139 case lang_input_file_is_search_file_enum
:
1141 p
->local_sym_name
= name
;
1142 p
->flags
.real
= TRUE
;
1143 p
->flags
.search_dirs
= TRUE
;
1145 case lang_input_file_is_file_enum
:
1147 p
->local_sym_name
= name
;
1148 p
->flags
.real
= TRUE
;
1154 lang_statement_append (&input_file_chain
, p
, &p
->next_real_file
);
1158 lang_input_statement_type
*
1159 lang_add_input_file (const char *name
,
1160 lang_input_file_enum_type file_type
,
1164 && (*name
== '=' || CONST_STRNEQ (name
, "$SYSROOT")))
1166 lang_input_statement_type
*ret
;
1167 char *sysrooted_name
1168 = concat (ld_sysroot
,
1169 name
+ (*name
== '=' ? 1 : strlen ("$SYSROOT")),
1170 (const char *) NULL
);
1172 /* We've now forcibly prepended the sysroot, making the input
1173 file independent of the context. Therefore, temporarily
1174 force a non-sysrooted context for this statement, so it won't
1175 get the sysroot prepended again when opened. (N.B. if it's a
1176 script, any child nodes with input files starting with "/"
1177 will be handled as "sysrooted" as they'll be found to be
1178 within the sysroot subdirectory.) */
1179 unsigned int outer_sysrooted
= input_flags
.sysrooted
;
1180 input_flags
.sysrooted
= 0;
1181 ret
= new_afile (sysrooted_name
, file_type
, target
);
1182 input_flags
.sysrooted
= outer_sysrooted
;
1186 return new_afile (name
, file_type
, target
);
1189 struct out_section_hash_entry
1191 struct bfd_hash_entry root
;
1192 lang_statement_union_type s
;
1195 /* The hash table. */
1197 static struct bfd_hash_table output_section_statement_table
;
1199 /* Support routines for the hash table used by lang_output_section_find,
1200 initialize the table, fill in an entry and remove the table. */
1202 static struct bfd_hash_entry
*
1203 output_section_statement_newfunc (struct bfd_hash_entry
*entry
,
1204 struct bfd_hash_table
*table
,
1207 lang_output_section_statement_type
**nextp
;
1208 struct out_section_hash_entry
*ret
;
1212 entry
= (struct bfd_hash_entry
*) bfd_hash_allocate (table
,
1218 entry
= bfd_hash_newfunc (entry
, table
, string
);
1222 ret
= (struct out_section_hash_entry
*) entry
;
1223 memset (&ret
->s
, 0, sizeof (ret
->s
));
1224 ret
->s
.header
.type
= lang_output_section_statement_enum
;
1225 ret
->s
.output_section_statement
.subsection_alignment
= NULL
;
1226 ret
->s
.output_section_statement
.section_alignment
= NULL
;
1227 ret
->s
.output_section_statement
.block_value
= 1;
1228 lang_list_init (&ret
->s
.output_section_statement
.children
);
1229 lang_statement_append (stat_ptr
, &ret
->s
, &ret
->s
.header
.next
);
1231 /* For every output section statement added to the list, except the
1232 first one, lang_os_list.tail points to the "next"
1233 field of the last element of the list. */
1234 if (lang_os_list
.head
!= NULL
)
1235 ret
->s
.output_section_statement
.prev
1236 = ((lang_output_section_statement_type
*)
1237 ((char *) lang_os_list
.tail
1238 - offsetof (lang_output_section_statement_type
, next
)));
1240 /* GCC's strict aliasing rules prevent us from just casting the
1241 address, so we store the pointer in a variable and cast that
1243 nextp
= &ret
->s
.output_section_statement
.next
;
1244 lang_statement_append (&lang_os_list
, &ret
->s
, nextp
);
1249 output_section_statement_table_init (void)
1251 if (!bfd_hash_table_init_n (&output_section_statement_table
,
1252 output_section_statement_newfunc
,
1253 sizeof (struct out_section_hash_entry
),
1255 einfo (_("%F%P: can not create hash table: %E\n"));
1259 output_section_statement_table_free (void)
1261 bfd_hash_table_free (&output_section_statement_table
);
1264 /* Build enough state so that the parser can build its tree. */
1269 obstack_begin (&stat_obstack
, 1000);
1271 stat_ptr
= &statement_list
;
1273 output_section_statement_table_init ();
1275 lang_list_init (stat_ptr
);
1277 lang_list_init (&input_file_chain
);
1278 lang_list_init (&lang_os_list
);
1279 lang_list_init (&file_chain
);
1280 first_file
= lang_add_input_file (NULL
, lang_input_file_is_marker_enum
,
1282 abs_output_section
=
1283 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
, 0, TRUE
);
1285 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
1287 asneeded_list_head
= NULL
;
1288 asneeded_list_tail
= &asneeded_list_head
;
1294 output_section_statement_table_free ();
1297 /*----------------------------------------------------------------------
1298 A region is an area of memory declared with the
1299 MEMORY { name:org=exp, len=exp ... }
1302 We maintain a list of all the regions here.
1304 If no regions are specified in the script, then the default is used
1305 which is created when looked up to be the entire data space.
1307 If create is true we are creating a region inside a MEMORY block.
1308 In this case it is probably an error to create a region that has
1309 already been created. If we are not inside a MEMORY block it is
1310 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
1311 and so we issue a warning.
1313 Each region has at least one name. The first name is either
1314 DEFAULT_MEMORY_REGION or the name given in the MEMORY block. You can add
1315 alias names to an existing region within a script with
1316 REGION_ALIAS (alias, region_name). Each name corresponds to at most one
1319 static lang_memory_region_type
*lang_memory_region_list
;
1320 static lang_memory_region_type
**lang_memory_region_list_tail
1321 = &lang_memory_region_list
;
1323 lang_memory_region_type
*
1324 lang_memory_region_lookup (const char *const name
, bfd_boolean create
)
1326 lang_memory_region_name
*n
;
1327 lang_memory_region_type
*r
;
1328 lang_memory_region_type
*new_region
;
1330 /* NAME is NULL for LMA memspecs if no region was specified. */
1334 for (r
= lang_memory_region_list
; r
!= NULL
; r
= r
->next
)
1335 for (n
= &r
->name_list
; n
!= NULL
; n
= n
->next
)
1336 if (strcmp (n
->name
, name
) == 0)
1339 einfo (_("%P:%pS: warning: redeclaration of memory region `%s'\n"),
1344 if (!create
&& strcmp (name
, DEFAULT_MEMORY_REGION
))
1345 einfo (_("%P:%pS: warning: memory region `%s' not declared\n"),
1348 new_region
= stat_alloc (sizeof (lang_memory_region_type
));
1350 new_region
->name_list
.name
= xstrdup (name
);
1351 new_region
->name_list
.next
= NULL
;
1352 new_region
->next
= NULL
;
1353 new_region
->origin_exp
= NULL
;
1354 new_region
->origin
= 0;
1355 new_region
->length_exp
= NULL
;
1356 new_region
->length
= ~(bfd_size_type
) 0;
1357 new_region
->current
= 0;
1358 new_region
->last_os
= NULL
;
1359 new_region
->flags
= 0;
1360 new_region
->not_flags
= 0;
1361 new_region
->had_full_message
= FALSE
;
1363 *lang_memory_region_list_tail
= new_region
;
1364 lang_memory_region_list_tail
= &new_region
->next
;
1370 lang_memory_region_alias (const char *alias
, const char *region_name
)
1372 lang_memory_region_name
*n
;
1373 lang_memory_region_type
*r
;
1374 lang_memory_region_type
*region
;
1376 /* The default region must be unique. This ensures that it is not necessary
1377 to iterate through the name list if someone wants the check if a region is
1378 the default memory region. */
1379 if (strcmp (region_name
, DEFAULT_MEMORY_REGION
) == 0
1380 || strcmp (alias
, DEFAULT_MEMORY_REGION
) == 0)
1381 einfo (_("%F%P:%pS: error: alias for default memory region\n"), NULL
);
1383 /* Look for the target region and check if the alias is not already
1386 for (r
= lang_memory_region_list
; r
!= NULL
; r
= r
->next
)
1387 for (n
= &r
->name_list
; n
!= NULL
; n
= n
->next
)
1389 if (region
== NULL
&& strcmp (n
->name
, region_name
) == 0)
1391 if (strcmp (n
->name
, alias
) == 0)
1392 einfo (_("%F%P:%pS: error: redefinition of memory region "
1397 /* Check if the target region exists. */
1399 einfo (_("%F%P:%pS: error: memory region `%s' "
1400 "for alias `%s' does not exist\n"),
1401 NULL
, region_name
, alias
);
1403 /* Add alias to region name list. */
1404 n
= stat_alloc (sizeof (lang_memory_region_name
));
1405 n
->name
= xstrdup (alias
);
1406 n
->next
= region
->name_list
.next
;
1407 region
->name_list
.next
= n
;
1410 static lang_memory_region_type
*
1411 lang_memory_default (asection
*section
)
1413 lang_memory_region_type
*p
;
1415 flagword sec_flags
= section
->flags
;
1417 /* Override SEC_DATA to mean a writable section. */
1418 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
1419 sec_flags
|= SEC_DATA
;
1421 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
1423 if ((p
->flags
& sec_flags
) != 0
1424 && (p
->not_flags
& sec_flags
) == 0)
1429 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, FALSE
);
1432 /* Get the output section statement directly from the userdata. */
1434 lang_output_section_statement_type
*
1435 lang_output_section_get (const asection
*output_section
)
1437 return bfd_section_userdata (output_section
);
1440 /* Find or create an output_section_statement with the given NAME.
1441 If CONSTRAINT is non-zero match one with that constraint, otherwise
1442 match any non-negative constraint. If CREATE, always make a
1443 new output_section_statement for SPECIAL CONSTRAINT. */
1445 lang_output_section_statement_type
*
1446 lang_output_section_statement_lookup (const char *name
,
1450 struct out_section_hash_entry
*entry
;
1452 entry
= ((struct out_section_hash_entry
*)
1453 bfd_hash_lookup (&output_section_statement_table
, name
,
1458 einfo (_("%F%P: failed creating section `%s': %E\n"), name
);
1462 if (entry
->s
.output_section_statement
.name
!= NULL
)
1464 /* We have a section of this name, but it might not have the correct
1466 struct out_section_hash_entry
*last_ent
;
1468 name
= entry
->s
.output_section_statement
.name
;
1469 if (create
&& constraint
== SPECIAL
)
1470 /* Not traversing to the end reverses the order of the second
1471 and subsequent SPECIAL sections in the hash table chain,
1472 but that shouldn't matter. */
1477 if (constraint
== entry
->s
.output_section_statement
.constraint
1479 && entry
->s
.output_section_statement
.constraint
>= 0))
1480 return &entry
->s
.output_section_statement
;
1482 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1484 while (entry
!= NULL
1485 && name
== entry
->s
.output_section_statement
.name
);
1491 = ((struct out_section_hash_entry
*)
1492 output_section_statement_newfunc (NULL
,
1493 &output_section_statement_table
,
1497 einfo (_("%F%P: failed creating section `%s': %E\n"), name
);
1500 entry
->root
= last_ent
->root
;
1501 last_ent
->root
.next
= &entry
->root
;
1504 entry
->s
.output_section_statement
.name
= name
;
1505 entry
->s
.output_section_statement
.constraint
= constraint
;
1506 return &entry
->s
.output_section_statement
;
1509 /* Find the next output_section_statement with the same name as OS.
1510 If CONSTRAINT is non-zero, find one with that constraint otherwise
1511 match any non-negative constraint. */
1513 lang_output_section_statement_type
*
1514 next_matching_output_section_statement (lang_output_section_statement_type
*os
,
1517 /* All output_section_statements are actually part of a
1518 struct out_section_hash_entry. */
1519 struct out_section_hash_entry
*entry
= (struct out_section_hash_entry
*)
1521 - offsetof (struct out_section_hash_entry
, s
.output_section_statement
));
1522 const char *name
= os
->name
;
1524 ASSERT (name
== entry
->root
.string
);
1527 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1529 || name
!= entry
->s
.output_section_statement
.name
)
1532 while (constraint
!= entry
->s
.output_section_statement
.constraint
1534 || entry
->s
.output_section_statement
.constraint
< 0));
1536 return &entry
->s
.output_section_statement
;
1539 /* A variant of lang_output_section_find used by place_orphan.
1540 Returns the output statement that should precede a new output
1541 statement for SEC. If an exact match is found on certain flags,
1544 lang_output_section_statement_type
*
1545 lang_output_section_find_by_flags (const asection
*sec
,
1547 lang_output_section_statement_type
**exact
,
1548 lang_match_sec_type_func match_type
)
1550 lang_output_section_statement_type
*first
, *look
, *found
;
1551 flagword look_flags
, differ
;
1553 /* We know the first statement on this list is *ABS*. May as well
1555 first
= &lang_os_list
.head
->output_section_statement
;
1556 first
= first
->next
;
1558 /* First try for an exact match. */
1560 for (look
= first
; look
; look
= look
->next
)
1562 look_flags
= look
->flags
;
1563 if (look
->bfd_section
!= NULL
)
1565 look_flags
= look
->bfd_section
->flags
;
1566 if (match_type
&& !match_type (link_info
.output_bfd
,
1571 differ
= look_flags
^ sec_flags
;
1572 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
1573 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1583 if ((sec_flags
& SEC_CODE
) != 0
1584 && (sec_flags
& SEC_ALLOC
) != 0)
1586 /* Try for a rw code section. */
1587 for (look
= first
; look
; look
= look
->next
)
1589 look_flags
= look
->flags
;
1590 if (look
->bfd_section
!= NULL
)
1592 look_flags
= look
->bfd_section
->flags
;
1593 if (match_type
&& !match_type (link_info
.output_bfd
,
1598 differ
= look_flags
^ sec_flags
;
1599 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1600 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1604 else if ((sec_flags
& SEC_READONLY
) != 0
1605 && (sec_flags
& SEC_ALLOC
) != 0)
1607 /* .rodata can go after .text, .sdata2 after .rodata. */
1608 for (look
= first
; look
; look
= look
->next
)
1610 look_flags
= look
->flags
;
1611 if (look
->bfd_section
!= NULL
)
1613 look_flags
= look
->bfd_section
->flags
;
1614 if (match_type
&& !match_type (link_info
.output_bfd
,
1619 differ
= look_flags
^ sec_flags
;
1620 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1621 | SEC_READONLY
| SEC_SMALL_DATA
))
1622 || (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1624 && !(look_flags
& SEC_SMALL_DATA
)))
1628 else if ((sec_flags
& SEC_THREAD_LOCAL
) != 0
1629 && (sec_flags
& SEC_ALLOC
) != 0)
1631 /* .tdata can go after .data, .tbss after .tdata. Treat .tbss
1632 as if it were a loaded section, and don't use match_type. */
1633 bfd_boolean seen_thread_local
= FALSE
;
1636 for (look
= first
; look
; look
= look
->next
)
1638 look_flags
= look
->flags
;
1639 if (look
->bfd_section
!= NULL
)
1640 look_flags
= look
->bfd_section
->flags
;
1642 differ
= look_flags
^ (sec_flags
| SEC_LOAD
| SEC_HAS_CONTENTS
);
1643 if (!(differ
& (SEC_THREAD_LOCAL
| SEC_ALLOC
)))
1645 /* .tdata and .tbss must be adjacent and in that order. */
1646 if (!(look_flags
& SEC_LOAD
)
1647 && (sec_flags
& SEC_LOAD
))
1648 /* ..so if we're at a .tbss section and we're placing
1649 a .tdata section stop looking and return the
1650 previous section. */
1653 seen_thread_local
= TRUE
;
1655 else if (seen_thread_local
)
1657 else if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
)))
1661 else if ((sec_flags
& SEC_SMALL_DATA
) != 0
1662 && (sec_flags
& SEC_ALLOC
) != 0)
1664 /* .sdata goes after .data, .sbss after .sdata. */
1665 for (look
= first
; look
; look
= look
->next
)
1667 look_flags
= look
->flags
;
1668 if (look
->bfd_section
!= NULL
)
1670 look_flags
= look
->bfd_section
->flags
;
1671 if (match_type
&& !match_type (link_info
.output_bfd
,
1676 differ
= look_flags
^ sec_flags
;
1677 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1678 | SEC_THREAD_LOCAL
))
1679 || ((look_flags
& SEC_SMALL_DATA
)
1680 && !(sec_flags
& SEC_HAS_CONTENTS
)))
1684 else if ((sec_flags
& SEC_HAS_CONTENTS
) != 0
1685 && (sec_flags
& SEC_ALLOC
) != 0)
1687 /* .data goes after .rodata. */
1688 for (look
= first
; look
; look
= look
->next
)
1690 look_flags
= look
->flags
;
1691 if (look
->bfd_section
!= NULL
)
1693 look_flags
= look
->bfd_section
->flags
;
1694 if (match_type
&& !match_type (link_info
.output_bfd
,
1699 differ
= look_flags
^ sec_flags
;
1700 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1701 | SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1705 else if ((sec_flags
& SEC_ALLOC
) != 0)
1707 /* .bss goes after any other alloc section. */
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_ALLOC
))
1726 /* non-alloc go last. */
1727 for (look
= first
; look
; look
= look
->next
)
1729 look_flags
= look
->flags
;
1730 if (look
->bfd_section
!= NULL
)
1731 look_flags
= look
->bfd_section
->flags
;
1732 differ
= look_flags
^ sec_flags
;
1733 if (!(differ
& SEC_DEBUGGING
))
1739 if (found
|| !match_type
)
1742 return lang_output_section_find_by_flags (sec
, sec_flags
, NULL
, NULL
);
1745 /* Find the last output section before given output statement.
1746 Used by place_orphan. */
1749 output_prev_sec_find (lang_output_section_statement_type
*os
)
1751 lang_output_section_statement_type
*lookup
;
1753 for (lookup
= os
->prev
; lookup
!= NULL
; lookup
= lookup
->prev
)
1755 if (lookup
->constraint
< 0)
1758 if (lookup
->bfd_section
!= NULL
&& lookup
->bfd_section
->owner
!= NULL
)
1759 return lookup
->bfd_section
;
1765 /* Look for a suitable place for a new output section statement. The
1766 idea is to skip over anything that might be inside a SECTIONS {}
1767 statement in a script, before we find another output section
1768 statement. Assignments to "dot" before an output section statement
1769 are assumed to belong to it, except in two cases; The first
1770 assignment to dot, and assignments before non-alloc sections.
1771 Otherwise we might put an orphan before . = . + SIZEOF_HEADERS or
1772 similar assignments that set the initial address, or we might
1773 insert non-alloc note sections among assignments setting end of
1776 static lang_statement_union_type
**
1777 insert_os_after (lang_output_section_statement_type
*after
)
1779 lang_statement_union_type
**where
;
1780 lang_statement_union_type
**assign
= NULL
;
1781 bfd_boolean ignore_first
;
1783 ignore_first
= after
== &lang_os_list
.head
->output_section_statement
;
1785 for (where
= &after
->header
.next
;
1787 where
= &(*where
)->header
.next
)
1789 switch ((*where
)->header
.type
)
1791 case lang_assignment_statement_enum
:
1794 lang_assignment_statement_type
*ass
;
1796 ass
= &(*where
)->assignment_statement
;
1797 if (ass
->exp
->type
.node_class
!= etree_assert
1798 && ass
->exp
->assign
.dst
[0] == '.'
1799 && ass
->exp
->assign
.dst
[1] == 0)
1803 ignore_first
= FALSE
;
1807 case lang_wild_statement_enum
:
1808 case lang_input_section_enum
:
1809 case lang_object_symbols_statement_enum
:
1810 case lang_fill_statement_enum
:
1811 case lang_data_statement_enum
:
1812 case lang_reloc_statement_enum
:
1813 case lang_padding_statement_enum
:
1814 case lang_constructors_statement_enum
:
1816 ignore_first
= FALSE
;
1818 case lang_output_section_statement_enum
:
1821 asection
*s
= (*where
)->output_section_statement
.bfd_section
;
1824 || s
->map_head
.s
== NULL
1825 || (s
->flags
& SEC_ALLOC
) != 0)
1829 case lang_input_statement_enum
:
1830 case lang_address_statement_enum
:
1831 case lang_target_statement_enum
:
1832 case lang_output_statement_enum
:
1833 case lang_group_statement_enum
:
1834 case lang_insert_statement_enum
:
1843 lang_output_section_statement_type
*
1844 lang_insert_orphan (asection
*s
,
1845 const char *secname
,
1847 lang_output_section_statement_type
*after
,
1848 struct orphan_save
*place
,
1849 etree_type
*address
,
1850 lang_statement_list_type
*add_child
)
1852 lang_statement_list_type add
;
1853 lang_output_section_statement_type
*os
;
1854 lang_output_section_statement_type
**os_tail
;
1856 /* If we have found an appropriate place for the output section
1857 statements for this orphan, add them to our own private list,
1858 inserting them later into the global statement list. */
1861 lang_list_init (&add
);
1862 push_stat_ptr (&add
);
1865 if (bfd_link_relocatable (&link_info
)
1866 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0)
1867 address
= exp_intop (0);
1869 os_tail
= (lang_output_section_statement_type
**) lang_os_list
.tail
;
1870 os
= lang_enter_output_section_statement (secname
, address
, normal_section
,
1871 NULL
, NULL
, NULL
, constraint
, 0);
1873 if (add_child
== NULL
)
1874 add_child
= &os
->children
;
1875 lang_add_section (add_child
, s
, NULL
, os
);
1877 if (after
&& (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) != 0)
1879 const char *region
= (after
->region
1880 ? after
->region
->name_list
.name
1881 : DEFAULT_MEMORY_REGION
);
1882 const char *lma_region
= (after
->lma_region
1883 ? after
->lma_region
->name_list
.name
1885 lang_leave_output_section_statement (NULL
, region
, after
->phdrs
,
1889 lang_leave_output_section_statement (NULL
, DEFAULT_MEMORY_REGION
, NULL
,
1892 /* Restore the global list pointer. */
1896 if (after
!= NULL
&& os
->bfd_section
!= NULL
)
1898 asection
*snew
, *as
;
1899 bfd_boolean place_after
= place
->stmt
== NULL
;
1900 bfd_boolean insert_after
= TRUE
;
1902 snew
= os
->bfd_section
;
1904 /* Shuffle the bfd section list to make the output file look
1905 neater. This is really only cosmetic. */
1906 if (place
->section
== NULL
1907 && after
!= &lang_os_list
.head
->output_section_statement
)
1909 asection
*bfd_section
= after
->bfd_section
;
1911 /* If the output statement hasn't been used to place any input
1912 sections (and thus doesn't have an output bfd_section),
1913 look for the closest prior output statement having an
1915 if (bfd_section
== NULL
)
1916 bfd_section
= output_prev_sec_find (after
);
1918 if (bfd_section
!= NULL
&& bfd_section
!= snew
)
1919 place
->section
= &bfd_section
->next
;
1922 if (place
->section
== NULL
)
1923 place
->section
= &link_info
.output_bfd
->sections
;
1925 as
= *place
->section
;
1929 /* Put the section at the end of the list. */
1931 /* Unlink the section. */
1932 bfd_section_list_remove (link_info
.output_bfd
, snew
);
1934 /* Now tack it back on in the right place. */
1935 bfd_section_list_append (link_info
.output_bfd
, snew
);
1937 else if ((bfd_get_flavour (link_info
.output_bfd
)
1938 == bfd_target_elf_flavour
)
1939 && (bfd_get_flavour (s
->owner
)
1940 == bfd_target_elf_flavour
)
1941 && ((elf_section_type (s
) == SHT_NOTE
1942 && (s
->flags
& SEC_LOAD
) != 0)
1943 || (elf_section_type (as
) == SHT_NOTE
1944 && (as
->flags
& SEC_LOAD
) != 0)))
1946 /* Make sure that output note sections are grouped and sorted
1947 by alignments when inserting a note section or insert a
1948 section after a note section, */
1950 /* A specific section after which the output note section
1951 should be placed. */
1952 asection
*after_sec
;
1953 /* True if we need to insert the orphan section after a
1954 specific section to maintain output note section order. */
1955 bfd_boolean after_sec_note
= FALSE
;
1957 static asection
*first_orphan_note
= NULL
;
1959 /* Group and sort output note section by alignments in
1962 if (elf_section_type (s
) == SHT_NOTE
1963 && (s
->flags
& SEC_LOAD
) != 0)
1965 /* Search from the beginning for the last output note
1966 section with equal or larger alignments. NB: Don't
1967 place orphan note section after non-note sections. */
1969 first_orphan_note
= NULL
;
1970 for (sec
= link_info
.output_bfd
->sections
;
1972 && !bfd_is_abs_section (sec
));
1975 && elf_section_type (sec
) == SHT_NOTE
1976 && (sec
->flags
& SEC_LOAD
) != 0)
1978 if (!first_orphan_note
)
1979 first_orphan_note
= sec
;
1980 if (sec
->alignment_power
>= s
->alignment_power
)
1983 else if (first_orphan_note
)
1985 /* Stop if there is non-note section after the first
1986 orphan note section. */
1990 /* If this will be the first orphan note section, it can
1991 be placed at the default location. */
1992 after_sec_note
= first_orphan_note
!= NULL
;
1993 if (after_sec
== NULL
&& after_sec_note
)
1995 /* If all output note sections have smaller
1996 alignments, place the section before all
1997 output orphan note sections. */
1998 after_sec
= first_orphan_note
;
1999 insert_after
= FALSE
;
2002 else if (first_orphan_note
)
2004 /* Don't place non-note sections in the middle of orphan
2006 after_sec_note
= TRUE
;
2008 for (sec
= as
->next
;
2010 && !bfd_is_abs_section (sec
));
2012 if (elf_section_type (sec
) == SHT_NOTE
2013 && (sec
->flags
& SEC_LOAD
) != 0)
2021 /* Search forward to insert OS after AFTER_SEC output
2023 lang_output_section_statement_type
*stmt
, *next
;
2024 bfd_boolean found
= FALSE
;
2025 for (stmt
= after
; stmt
!= NULL
; stmt
= next
)
2030 if (stmt
->bfd_section
== after_sec
)
2040 /* If INSERT_AFTER is FALSE, place OS before
2041 AFTER_SEC output statement. */
2042 if (next
&& next
->bfd_section
== after_sec
)
2052 /* Search backward to insert OS after AFTER_SEC output
2055 for (stmt
= after
; stmt
!= NULL
; stmt
= stmt
->prev
)
2059 if (stmt
->bfd_section
== after_sec
)
2068 /* If INSERT_AFTER is FALSE, place OS before
2069 AFTER_SEC output statement. */
2070 if (stmt
->next
->bfd_section
== after_sec
)
2080 if (after_sec
== NULL
2081 || (insert_after
&& after_sec
->next
!= snew
)
2082 || (!insert_after
&& after_sec
->prev
!= snew
))
2084 /* Unlink the section. */
2085 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2087 /* Place SNEW after AFTER_SEC. If AFTER_SEC is NULL,
2092 bfd_section_list_insert_after (link_info
.output_bfd
,
2095 bfd_section_list_insert_before (link_info
.output_bfd
,
2099 bfd_section_list_prepend (link_info
.output_bfd
, snew
);
2102 else if (as
!= snew
&& as
->prev
!= snew
)
2104 /* Unlink the section. */
2105 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2107 /* Now tack it back on in the right place. */
2108 bfd_section_list_insert_before (link_info
.output_bfd
,
2112 else if (as
!= snew
&& as
->prev
!= snew
)
2114 /* Unlink the section. */
2115 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2117 /* Now tack it back on in the right place. */
2118 bfd_section_list_insert_before (link_info
.output_bfd
, as
, snew
);
2121 /* Save the end of this list. Further ophans of this type will
2122 follow the one we've just added. */
2123 place
->section
= &snew
->next
;
2125 /* The following is non-cosmetic. We try to put the output
2126 statements in some sort of reasonable order here, because they
2127 determine the final load addresses of the orphan sections.
2128 In addition, placing output statements in the wrong order may
2129 require extra segments. For instance, given a typical
2130 situation of all read-only sections placed in one segment and
2131 following that a segment containing all the read-write
2132 sections, we wouldn't want to place an orphan read/write
2133 section before or amongst the read-only ones. */
2134 if (add
.head
!= NULL
)
2136 lang_output_section_statement_type
*newly_added_os
;
2138 /* Place OS after AFTER if AFTER_NOTE is TRUE. */
2141 lang_statement_union_type
**where
= insert_os_after (after
);
2146 place
->os_tail
= &after
->next
;
2150 /* Put it after the last orphan statement we added. */
2151 *add
.tail
= *place
->stmt
;
2152 *place
->stmt
= add
.head
;
2155 /* Fix the global list pointer if we happened to tack our
2156 new list at the tail. */
2157 if (*stat_ptr
->tail
== add
.head
)
2158 stat_ptr
->tail
= add
.tail
;
2160 /* Save the end of this list. */
2161 place
->stmt
= add
.tail
;
2163 /* Do the same for the list of output section statements. */
2164 newly_added_os
= *os_tail
;
2166 newly_added_os
->prev
= (lang_output_section_statement_type
*)
2167 ((char *) place
->os_tail
2168 - offsetof (lang_output_section_statement_type
, next
));
2169 newly_added_os
->next
= *place
->os_tail
;
2170 if (newly_added_os
->next
!= NULL
)
2171 newly_added_os
->next
->prev
= newly_added_os
;
2172 *place
->os_tail
= newly_added_os
;
2173 place
->os_tail
= &newly_added_os
->next
;
2175 /* Fixing the global list pointer here is a little different.
2176 We added to the list in lang_enter_output_section_statement,
2177 trimmed off the new output_section_statment above when
2178 assigning *os_tail = NULL, but possibly added it back in
2179 the same place when assigning *place->os_tail. */
2180 if (*os_tail
== NULL
)
2181 lang_os_list
.tail
= (lang_statement_union_type
**) os_tail
;
2188 lang_print_asneeded (void)
2190 struct asneeded_minfo
*m
;
2192 if (asneeded_list_head
== NULL
)
2195 minfo (_("\nAs-needed library included to satisfy reference by file (symbol)\n\n"));
2197 for (m
= asneeded_list_head
; m
!= NULL
; m
= m
->next
)
2201 minfo ("%s", m
->soname
);
2202 len
= strlen (m
->soname
);
2216 minfo ("%pB ", m
->ref
);
2217 minfo ("(%pT)\n", m
->name
);
2222 lang_map_flags (flagword flag
)
2224 if (flag
& SEC_ALLOC
)
2227 if (flag
& SEC_CODE
)
2230 if (flag
& SEC_READONLY
)
2233 if (flag
& SEC_DATA
)
2236 if (flag
& SEC_LOAD
)
2243 lang_memory_region_type
*m
;
2244 bfd_boolean dis_header_printed
= FALSE
;
2246 LANG_FOR_EACH_INPUT_STATEMENT (file
)
2250 if ((file
->the_bfd
->flags
& (BFD_LINKER_CREATED
| DYNAMIC
)) != 0
2251 || file
->flags
.just_syms
)
2254 if (config
.print_map_discarded
)
2255 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
2256 if ((s
->output_section
== NULL
2257 || s
->output_section
->owner
!= link_info
.output_bfd
)
2258 && (s
->flags
& (SEC_LINKER_CREATED
| SEC_KEEP
)) == 0)
2260 if (! dis_header_printed
)
2262 fprintf (config
.map_file
, _("\nDiscarded input sections\n\n"));
2263 dis_header_printed
= TRUE
;
2266 print_input_section (s
, TRUE
);
2270 minfo (_("\nMemory Configuration\n\n"));
2271 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
2272 _("Name"), _("Origin"), _("Length"), _("Attributes"));
2274 for (m
= lang_memory_region_list
; m
!= NULL
; m
= m
->next
)
2279 fprintf (config
.map_file
, "%-16s ", m
->name_list
.name
);
2281 sprintf_vma (buf
, m
->origin
);
2282 minfo ("0x%s ", buf
);
2290 minfo ("0x%V", m
->length
);
2291 if (m
->flags
|| m
->not_flags
)
2299 lang_map_flags (m
->flags
);
2305 lang_map_flags (m
->not_flags
);
2312 fprintf (config
.map_file
, _("\nLinker script and memory map\n\n"));
2314 if (!link_info
.reduce_memory_overheads
)
2316 obstack_begin (&map_obstack
, 1000);
2317 bfd_link_hash_traverse (link_info
.hash
, sort_def_symbol
, 0);
2319 expld
.phase
= lang_fixed_phase_enum
;
2320 lang_statement_iteration
++;
2321 print_statements ();
2323 ldemul_extra_map_file_text (link_info
.output_bfd
, &link_info
,
2328 sort_def_symbol (struct bfd_link_hash_entry
*hash_entry
,
2329 void *info ATTRIBUTE_UNUSED
)
2331 if ((hash_entry
->type
== bfd_link_hash_defined
2332 || hash_entry
->type
== bfd_link_hash_defweak
)
2333 && hash_entry
->u
.def
.section
->owner
!= link_info
.output_bfd
2334 && hash_entry
->u
.def
.section
->owner
!= NULL
)
2336 input_section_userdata_type
*ud
;
2337 struct map_symbol_def
*def
;
2339 ud
= bfd_section_userdata (hash_entry
->u
.def
.section
);
2342 ud
= stat_alloc (sizeof (*ud
));
2343 bfd_set_section_userdata (hash_entry
->u
.def
.section
, ud
);
2344 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
2345 ud
->map_symbol_def_count
= 0;
2347 else if (!ud
->map_symbol_def_tail
)
2348 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
2350 def
= (struct map_symbol_def
*) obstack_alloc (&map_obstack
, sizeof *def
);
2351 def
->entry
= hash_entry
;
2352 *(ud
->map_symbol_def_tail
) = def
;
2353 ud
->map_symbol_def_tail
= &def
->next
;
2354 ud
->map_symbol_def_count
++;
2359 /* Initialize an output section. */
2362 init_os (lang_output_section_statement_type
*s
, flagword flags
)
2364 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
2365 einfo (_("%F%P: illegal use of `%s' section\n"), DISCARD_SECTION_NAME
);
2367 if (s
->constraint
!= SPECIAL
)
2368 s
->bfd_section
= bfd_get_section_by_name (link_info
.output_bfd
, s
->name
);
2369 if (s
->bfd_section
== NULL
)
2370 s
->bfd_section
= bfd_make_section_anyway_with_flags (link_info
.output_bfd
,
2372 if (s
->bfd_section
== NULL
)
2374 einfo (_("%F%P: output format %s cannot represent section"
2375 " called %s: %E\n"),
2376 link_info
.output_bfd
->xvec
->name
, s
->name
);
2378 s
->bfd_section
->output_section
= s
->bfd_section
;
2379 s
->bfd_section
->output_offset
= 0;
2381 /* Set the userdata of the output section to the output section
2382 statement to avoid lookup. */
2383 bfd_set_section_userdata (s
->bfd_section
, s
);
2385 /* If there is a base address, make sure that any sections it might
2386 mention are initialized. */
2387 if (s
->addr_tree
!= NULL
)
2388 exp_init_os (s
->addr_tree
);
2390 if (s
->load_base
!= NULL
)
2391 exp_init_os (s
->load_base
);
2393 /* If supplied an alignment, set it. */
2394 if (s
->section_alignment
!= NULL
)
2395 s
->bfd_section
->alignment_power
= exp_get_power (s
->section_alignment
,
2396 "section alignment");
2399 /* Make sure that all output sections mentioned in an expression are
2403 exp_init_os (etree_type
*exp
)
2405 switch (exp
->type
.node_class
)
2409 case etree_provided
:
2410 exp_init_os (exp
->assign
.src
);
2414 exp_init_os (exp
->binary
.lhs
);
2415 exp_init_os (exp
->binary
.rhs
);
2419 exp_init_os (exp
->trinary
.cond
);
2420 exp_init_os (exp
->trinary
.lhs
);
2421 exp_init_os (exp
->trinary
.rhs
);
2425 exp_init_os (exp
->assert_s
.child
);
2429 exp_init_os (exp
->unary
.child
);
2433 switch (exp
->type
.node_code
)
2439 lang_output_section_statement_type
*os
;
2441 os
= lang_output_section_find (exp
->name
.name
);
2442 if (os
!= NULL
&& os
->bfd_section
== NULL
)
2454 section_already_linked (bfd
*abfd
, asection
*sec
, void *data
)
2456 lang_input_statement_type
*entry
= (lang_input_statement_type
*) data
;
2458 /* If we are only reading symbols from this object, then we want to
2459 discard all sections. */
2460 if (entry
->flags
.just_syms
)
2462 bfd_link_just_syms (abfd
, sec
, &link_info
);
2466 /* Deal with SHF_EXCLUDE ELF sections. */
2467 if (!bfd_link_relocatable (&link_info
)
2468 && (abfd
->flags
& BFD_PLUGIN
) == 0
2469 && (sec
->flags
& (SEC_GROUP
| SEC_KEEP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
2470 sec
->output_section
= bfd_abs_section_ptr
;
2472 if (!(abfd
->flags
& DYNAMIC
))
2473 bfd_section_already_linked (abfd
, sec
, &link_info
);
2477 /* Returns true if SECTION is one we know will be discarded based on its
2478 section flags, otherwise returns false. */
2481 lang_discard_section_p (asection
*section
)
2483 bfd_boolean discard
;
2484 flagword flags
= section
->flags
;
2486 /* Discard sections marked with SEC_EXCLUDE. */
2487 discard
= (flags
& SEC_EXCLUDE
) != 0;
2489 /* Discard the group descriptor sections when we're finally placing the
2490 sections from within the group. */
2491 if ((flags
& SEC_GROUP
) != 0
2492 && link_info
.resolve_section_groups
)
2495 /* Discard debugging sections if we are stripping debugging
2497 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
2498 && (flags
& SEC_DEBUGGING
) != 0)
2504 /* The wild routines.
2506 These expand statements like *(.text) and foo.o to a list of
2507 explicit actions, like foo.o(.text), bar.o(.text) and
2508 foo.o(.text, .data). */
2510 /* Add SECTION to the output section OUTPUT. Do this by creating a
2511 lang_input_section statement which is placed at PTR. */
2514 lang_add_section (lang_statement_list_type
*ptr
,
2516 struct flag_info
*sflag_info
,
2517 lang_output_section_statement_type
*output
)
2519 flagword flags
= section
->flags
;
2521 bfd_boolean discard
;
2522 lang_input_section_type
*new_section
;
2523 bfd
*abfd
= link_info
.output_bfd
;
2525 /* Is this section one we know should be discarded? */
2526 discard
= lang_discard_section_p (section
);
2528 /* Discard input sections which are assigned to a section named
2529 DISCARD_SECTION_NAME. */
2530 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
2535 if (section
->output_section
== NULL
)
2537 /* This prevents future calls from assigning this section. */
2538 section
->output_section
= bfd_abs_section_ptr
;
2547 keep
= bfd_lookup_section_flags (&link_info
, sflag_info
, section
);
2552 if (section
->output_section
!= NULL
)
2555 /* We don't copy the SEC_NEVER_LOAD flag from an input section
2556 to an output section, because we want to be able to include a
2557 SEC_NEVER_LOAD section in the middle of an otherwise loaded
2558 section (I don't know why we want to do this, but we do).
2559 build_link_order in ldwrite.c handles this case by turning
2560 the embedded SEC_NEVER_LOAD section into a fill. */
2561 flags
&= ~ SEC_NEVER_LOAD
;
2563 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
2564 already been processed. One reason to do this is that on pe
2565 format targets, .text$foo sections go into .text and it's odd
2566 to see .text with SEC_LINK_ONCE set. */
2567 if ((flags
& (SEC_LINK_ONCE
| SEC_GROUP
)) == (SEC_LINK_ONCE
| SEC_GROUP
))
2569 if (link_info
.resolve_section_groups
)
2570 flags
&= ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
);
2572 flags
&= ~(SEC_LINK_DUPLICATES
| SEC_RELOC
);
2574 else if (!bfd_link_relocatable (&link_info
))
2575 flags
&= ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
);
2577 switch (output
->sectype
)
2579 case normal_section
:
2580 case overlay_section
:
2581 case first_overlay_section
:
2583 case noalloc_section
:
2584 flags
&= ~SEC_ALLOC
;
2586 case noload_section
:
2588 flags
|= SEC_NEVER_LOAD
;
2589 /* Unfortunately GNU ld has managed to evolve two different
2590 meanings to NOLOAD in scripts. ELF gets a .bss style noload,
2591 alloc, no contents section. All others get a noload, noalloc
2593 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
)
2594 flags
&= ~SEC_HAS_CONTENTS
;
2596 flags
&= ~SEC_ALLOC
;
2600 if (output
->bfd_section
== NULL
)
2601 init_os (output
, flags
);
2603 /* If SEC_READONLY is not set in the input section, then clear
2604 it from the output section. */
2605 output
->bfd_section
->flags
&= flags
| ~SEC_READONLY
;
2607 if (output
->bfd_section
->linker_has_input
)
2609 /* Only set SEC_READONLY flag on the first input section. */
2610 flags
&= ~ SEC_READONLY
;
2612 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
2613 if ((output
->bfd_section
->flags
& (SEC_MERGE
| SEC_STRINGS
))
2614 != (flags
& (SEC_MERGE
| SEC_STRINGS
))
2615 || ((flags
& SEC_MERGE
) != 0
2616 && output
->bfd_section
->entsize
!= section
->entsize
))
2618 output
->bfd_section
->flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2619 flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2622 output
->bfd_section
->flags
|= flags
;
2624 if (!output
->bfd_section
->linker_has_input
)
2626 output
->bfd_section
->linker_has_input
= 1;
2627 /* This must happen after flags have been updated. The output
2628 section may have been created before we saw its first input
2629 section, eg. for a data statement. */
2630 bfd_init_private_section_data (section
->owner
, section
,
2631 link_info
.output_bfd
,
2632 output
->bfd_section
,
2634 if ((flags
& SEC_MERGE
) != 0)
2635 output
->bfd_section
->entsize
= section
->entsize
;
2638 if ((flags
& SEC_TIC54X_BLOCK
) != 0
2639 && bfd_get_arch (section
->owner
) == bfd_arch_tic54x
)
2641 /* FIXME: This value should really be obtained from the bfd... */
2642 output
->block_value
= 128;
2645 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
2646 output
->bfd_section
->alignment_power
= section
->alignment_power
;
2648 section
->output_section
= output
->bfd_section
;
2650 if (!map_head_is_link_order
)
2652 asection
*s
= output
->bfd_section
->map_tail
.s
;
2653 output
->bfd_section
->map_tail
.s
= section
;
2654 section
->map_head
.s
= NULL
;
2655 section
->map_tail
.s
= s
;
2657 s
->map_head
.s
= section
;
2659 output
->bfd_section
->map_head
.s
= section
;
2662 /* Add a section reference to the list. */
2663 new_section
= new_stat (lang_input_section
, ptr
);
2664 new_section
->section
= section
;
2667 /* Handle wildcard sorting. This returns the lang_input_section which
2668 should follow the one we are going to create for SECTION and FILE,
2669 based on the sorting requirements of WILD. It returns NULL if the
2670 new section should just go at the end of the current list. */
2672 static lang_statement_union_type
*
2673 wild_sort (lang_wild_statement_type
*wild
,
2674 struct wildcard_list
*sec
,
2675 lang_input_statement_type
*file
,
2678 lang_statement_union_type
*l
;
2680 if (!wild
->filenames_sorted
2681 && (sec
== NULL
|| sec
->spec
.sorted
== none
))
2684 for (l
= wild
->children
.head
; l
!= NULL
; l
= l
->header
.next
)
2686 lang_input_section_type
*ls
;
2688 if (l
->header
.type
!= lang_input_section_enum
)
2690 ls
= &l
->input_section
;
2692 /* Sorting by filename takes precedence over sorting by section
2695 if (wild
->filenames_sorted
)
2697 const char *fn
, *ln
;
2701 /* The PE support for the .idata section as generated by
2702 dlltool assumes that files will be sorted by the name of
2703 the archive and then the name of the file within the
2706 if (file
->the_bfd
!= NULL
2707 && file
->the_bfd
->my_archive
!= NULL
)
2709 fn
= bfd_get_filename (file
->the_bfd
->my_archive
);
2714 fn
= file
->filename
;
2718 if (ls
->section
->owner
->my_archive
!= NULL
)
2720 ln
= bfd_get_filename (ls
->section
->owner
->my_archive
);
2725 ln
= ls
->section
->owner
->filename
;
2729 i
= filename_cmp (fn
, ln
);
2738 fn
= file
->filename
;
2740 ln
= ls
->section
->owner
->filename
;
2742 i
= filename_cmp (fn
, ln
);
2750 /* Here either the files are not sorted by name, or we are
2751 looking at the sections for this file. */
2754 && sec
->spec
.sorted
!= none
2755 && sec
->spec
.sorted
!= by_none
)
2756 if (compare_section (sec
->spec
.sorted
, section
, ls
->section
) < 0)
2763 /* Expand a wild statement for a particular FILE. SECTION may be
2764 NULL, in which case it is a wild card. */
2767 output_section_callback (lang_wild_statement_type
*ptr
,
2768 struct wildcard_list
*sec
,
2770 struct flag_info
*sflag_info
,
2771 lang_input_statement_type
*file
,
2774 lang_statement_union_type
*before
;
2775 lang_output_section_statement_type
*os
;
2777 os
= (lang_output_section_statement_type
*) output
;
2779 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2780 if (unique_section_p (section
, os
))
2783 before
= wild_sort (ptr
, sec
, file
, section
);
2785 /* Here BEFORE points to the lang_input_section which
2786 should follow the one we are about to add. If BEFORE
2787 is NULL, then the section should just go at the end
2788 of the current list. */
2791 lang_add_section (&ptr
->children
, section
, sflag_info
, os
);
2794 lang_statement_list_type list
;
2795 lang_statement_union_type
**pp
;
2797 lang_list_init (&list
);
2798 lang_add_section (&list
, section
, sflag_info
, os
);
2800 /* If we are discarding the section, LIST.HEAD will
2802 if (list
.head
!= NULL
)
2804 ASSERT (list
.head
->header
.next
== NULL
);
2806 for (pp
= &ptr
->children
.head
;
2808 pp
= &(*pp
)->header
.next
)
2809 ASSERT (*pp
!= NULL
);
2811 list
.head
->header
.next
= *pp
;
2817 /* Check if all sections in a wild statement for a particular FILE
2821 check_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
2822 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
2824 struct flag_info
*sflag_info ATTRIBUTE_UNUSED
,
2825 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
2828 lang_output_section_statement_type
*os
;
2830 os
= (lang_output_section_statement_type
*) output
;
2832 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2833 if (unique_section_p (section
, os
))
2836 if (section
->output_section
== NULL
&& (section
->flags
& SEC_READONLY
) == 0)
2837 os
->all_input_readonly
= FALSE
;
2840 /* This is passed a file name which must have been seen already and
2841 added to the statement tree. We will see if it has been opened
2842 already and had its symbols read. If not then we'll read it. */
2844 static lang_input_statement_type
*
2845 lookup_name (const char *name
)
2847 lang_input_statement_type
*search
;
2849 for (search
= &input_file_chain
.head
->input_statement
;
2851 search
= search
->next_real_file
)
2853 /* Use the local_sym_name as the name of the file that has
2854 already been loaded as filename might have been transformed
2855 via the search directory lookup mechanism. */
2856 const char *filename
= search
->local_sym_name
;
2858 if (filename
!= NULL
2859 && filename_cmp (filename
, name
) == 0)
2865 /* Arrange to splice the input statement added by new_afile into
2866 statement_list after the current input_file_chain tail.
2867 We know input_file_chain is not an empty list, and that
2868 lookup_name was called via open_input_bfds. Later calls to
2869 lookup_name should always match an existing input_statement. */
2870 lang_statement_union_type
**tail
= stat_ptr
->tail
;
2871 lang_statement_union_type
**after
2872 = (void *) ((char *) input_file_chain
.tail
2873 - offsetof (lang_input_statement_type
, next_real_file
)
2874 + offsetof (lang_input_statement_type
, header
.next
));
2875 lang_statement_union_type
*rest
= *after
;
2876 stat_ptr
->tail
= after
;
2877 search
= new_afile (name
, lang_input_file_is_search_file_enum
,
2879 *stat_ptr
->tail
= rest
;
2881 stat_ptr
->tail
= tail
;
2884 /* If we have already added this file, or this file is not real
2885 don't add this file. */
2886 if (search
->flags
.loaded
|| !search
->flags
.real
)
2889 if (!load_symbols (search
, NULL
))
2895 /* Save LIST as a list of libraries whose symbols should not be exported. */
2900 struct excluded_lib
*next
;
2902 static struct excluded_lib
*excluded_libs
;
2905 add_excluded_libs (const char *list
)
2907 const char *p
= list
, *end
;
2911 struct excluded_lib
*entry
;
2912 end
= strpbrk (p
, ",:");
2914 end
= p
+ strlen (p
);
2915 entry
= (struct excluded_lib
*) xmalloc (sizeof (*entry
));
2916 entry
->next
= excluded_libs
;
2917 entry
->name
= (char *) xmalloc (end
- p
+ 1);
2918 memcpy (entry
->name
, p
, end
- p
);
2919 entry
->name
[end
- p
] = '\0';
2920 excluded_libs
= entry
;
2928 check_excluded_libs (bfd
*abfd
)
2930 struct excluded_lib
*lib
= excluded_libs
;
2934 int len
= strlen (lib
->name
);
2935 const char *filename
= lbasename (abfd
->filename
);
2937 if (strcmp (lib
->name
, "ALL") == 0)
2939 abfd
->no_export
= TRUE
;
2943 if (filename_ncmp (lib
->name
, filename
, len
) == 0
2944 && (filename
[len
] == '\0'
2945 || (filename
[len
] == '.' && filename
[len
+ 1] == 'a'
2946 && filename
[len
+ 2] == '\0')))
2948 abfd
->no_export
= TRUE
;
2956 /* Get the symbols for an input file. */
2959 load_symbols (lang_input_statement_type
*entry
,
2960 lang_statement_list_type
*place
)
2964 if (entry
->flags
.loaded
)
2967 ldfile_open_file (entry
);
2969 /* Do not process further if the file was missing. */
2970 if (entry
->flags
.missing_file
)
2973 if (trace_files
|| verbose
)
2974 info_msg ("%pI\n", entry
);
2976 if (!bfd_check_format (entry
->the_bfd
, bfd_archive
)
2977 && !bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
2980 struct lang_input_statement_flags save_flags
;
2983 err
= bfd_get_error ();
2985 /* See if the emulation has some special knowledge. */
2986 if (ldemul_unrecognized_file (entry
))
2989 if (err
== bfd_error_file_ambiguously_recognized
)
2993 einfo (_("%P: %pB: file not recognized: %E;"
2994 " matching formats:"), entry
->the_bfd
);
2995 for (p
= matching
; *p
!= NULL
; p
++)
2999 else if (err
!= bfd_error_file_not_recognized
3001 einfo (_("%F%P: %pB: file not recognized: %E\n"), entry
->the_bfd
);
3003 bfd_close (entry
->the_bfd
);
3004 entry
->the_bfd
= NULL
;
3006 /* Try to interpret the file as a linker script. */
3007 save_flags
= input_flags
;
3008 ldfile_open_command_file (entry
->filename
);
3010 push_stat_ptr (place
);
3011 input_flags
.add_DT_NEEDED_for_regular
3012 = entry
->flags
.add_DT_NEEDED_for_regular
;
3013 input_flags
.add_DT_NEEDED_for_dynamic
3014 = entry
->flags
.add_DT_NEEDED_for_dynamic
;
3015 input_flags
.whole_archive
= entry
->flags
.whole_archive
;
3016 input_flags
.dynamic
= entry
->flags
.dynamic
;
3018 ldfile_assumed_script
= TRUE
;
3019 parser_input
= input_script
;
3021 ldfile_assumed_script
= FALSE
;
3023 /* missing_file is sticky. sysrooted will already have been
3024 restored when seeing EOF in yyparse, but no harm to restore
3026 save_flags
.missing_file
|= input_flags
.missing_file
;
3027 input_flags
= save_flags
;
3031 entry
->flags
.loaded
= TRUE
;
3036 if (ldemul_recognized_file (entry
))
3039 /* We don't call ldlang_add_file for an archive. Instead, the
3040 add_symbols entry point will call ldlang_add_file, via the
3041 add_archive_element callback, for each element of the archive
3043 switch (bfd_get_format (entry
->the_bfd
))
3049 if (!entry
->flags
.reload
)
3050 ldlang_add_file (entry
);
3054 check_excluded_libs (entry
->the_bfd
);
3056 bfd_set_usrdata (entry
->the_bfd
, entry
);
3057 if (entry
->flags
.whole_archive
)
3060 bfd_boolean loaded
= TRUE
;
3065 member
= bfd_openr_next_archived_file (entry
->the_bfd
, member
);
3070 if (!bfd_check_format (member
, bfd_object
))
3072 einfo (_("%F%P: %pB: member %pB in archive is not an object\n"),
3073 entry
->the_bfd
, member
);
3078 if (!(*link_info
.callbacks
3079 ->add_archive_element
) (&link_info
, member
,
3080 "--whole-archive", &subsbfd
))
3083 /* Potentially, the add_archive_element hook may have set a
3084 substitute BFD for us. */
3085 if (!bfd_link_add_symbols (subsbfd
, &link_info
))
3087 einfo (_("%F%P: %pB: error adding symbols: %E\n"), member
);
3092 entry
->flags
.loaded
= loaded
;
3098 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
3099 entry
->flags
.loaded
= TRUE
;
3101 einfo (_("%F%P: %pB: error adding symbols: %E\n"), entry
->the_bfd
);
3103 return entry
->flags
.loaded
;
3106 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
3107 may be NULL, indicating that it is a wildcard. Separate
3108 lang_input_section statements are created for each part of the
3109 expansion; they are added after the wild statement S. OUTPUT is
3110 the output section. */
3113 wild (lang_wild_statement_type
*s
,
3114 const char *target ATTRIBUTE_UNUSED
,
3115 lang_output_section_statement_type
*output
)
3117 struct wildcard_list
*sec
;
3119 if (s
->handler_data
[0]
3120 && s
->handler_data
[0]->spec
.sorted
== by_name
3121 && !s
->filenames_sorted
)
3123 lang_section_bst_type
*tree
;
3125 walk_wild (s
, output_section_callback_fast
, output
);
3130 output_section_callback_tree_to_list (s
, tree
, output
);
3135 walk_wild (s
, output_section_callback
, output
);
3137 if (default_common_section
== NULL
)
3138 for (sec
= s
->section_list
; sec
!= NULL
; sec
= sec
->next
)
3139 if (sec
->spec
.name
!= NULL
&& strcmp (sec
->spec
.name
, "COMMON") == 0)
3141 /* Remember the section that common is going to in case we
3142 later get something which doesn't know where to put it. */
3143 default_common_section
= output
;
3148 /* Return TRUE iff target is the sought target. */
3151 get_target (const bfd_target
*target
, void *data
)
3153 const char *sought
= (const char *) data
;
3155 return strcmp (target
->name
, sought
) == 0;
3158 /* Like strcpy() but convert to lower case as well. */
3161 stricpy (char *dest
, const char *src
)
3165 while ((c
= *src
++) != 0)
3166 *dest
++ = TOLOWER (c
);
3171 /* Remove the first occurrence of needle (if any) in haystack
3175 strcut (char *haystack
, const char *needle
)
3177 haystack
= strstr (haystack
, needle
);
3183 for (src
= haystack
+ strlen (needle
); *src
;)
3184 *haystack
++ = *src
++;
3190 /* Compare two target format name strings.
3191 Return a value indicating how "similar" they are. */
3194 name_compare (const char *first
, const char *second
)
3200 copy1
= (char *) xmalloc (strlen (first
) + 1);
3201 copy2
= (char *) xmalloc (strlen (second
) + 1);
3203 /* Convert the names to lower case. */
3204 stricpy (copy1
, first
);
3205 stricpy (copy2
, second
);
3207 /* Remove size and endian strings from the name. */
3208 strcut (copy1
, "big");
3209 strcut (copy1
, "little");
3210 strcut (copy2
, "big");
3211 strcut (copy2
, "little");
3213 /* Return a value based on how many characters match,
3214 starting from the beginning. If both strings are
3215 the same then return 10 * their length. */
3216 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
3217 if (copy1
[result
] == 0)
3229 /* Set by closest_target_match() below. */
3230 static const bfd_target
*winner
;
3232 /* Scan all the valid bfd targets looking for one that has the endianness
3233 requirement that was specified on the command line, and is the nearest
3234 match to the original output target. */
3237 closest_target_match (const bfd_target
*target
, void *data
)
3239 const bfd_target
*original
= (const bfd_target
*) data
;
3241 if (command_line
.endian
== ENDIAN_BIG
3242 && target
->byteorder
!= BFD_ENDIAN_BIG
)
3245 if (command_line
.endian
== ENDIAN_LITTLE
3246 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
3249 /* Must be the same flavour. */
3250 if (target
->flavour
!= original
->flavour
)
3253 /* Ignore generic big and little endian elf vectors. */
3254 if (strcmp (target
->name
, "elf32-big") == 0
3255 || strcmp (target
->name
, "elf64-big") == 0
3256 || strcmp (target
->name
, "elf32-little") == 0
3257 || strcmp (target
->name
, "elf64-little") == 0)
3260 /* If we have not found a potential winner yet, then record this one. */
3267 /* Oh dear, we now have two potential candidates for a successful match.
3268 Compare their names and choose the better one. */
3269 if (name_compare (target
->name
, original
->name
)
3270 > name_compare (winner
->name
, original
->name
))
3273 /* Keep on searching until wqe have checked them all. */
3277 /* Return the BFD target format of the first input file. */
3280 get_first_input_target (void)
3282 const char *target
= NULL
;
3284 LANG_FOR_EACH_INPUT_STATEMENT (s
)
3286 if (s
->header
.type
== lang_input_statement_enum
3289 ldfile_open_file (s
);
3291 if (s
->the_bfd
!= NULL
3292 && bfd_check_format (s
->the_bfd
, bfd_object
))
3294 target
= bfd_get_target (s
->the_bfd
);
3306 lang_get_output_target (void)
3310 /* Has the user told us which output format to use? */
3311 if (output_target
!= NULL
)
3312 return output_target
;
3314 /* No - has the current target been set to something other than
3316 if (current_target
!= default_target
&& current_target
!= NULL
)
3317 return current_target
;
3319 /* No - can we determine the format of the first input file? */
3320 target
= get_first_input_target ();
3324 /* Failed - use the default output target. */
3325 return default_target
;
3328 /* Open the output file. */
3331 open_output (const char *name
)
3333 output_target
= lang_get_output_target ();
3335 /* Has the user requested a particular endianness on the command
3337 if (command_line
.endian
!= ENDIAN_UNSET
)
3339 /* Get the chosen target. */
3340 const bfd_target
*target
3341 = bfd_iterate_over_targets (get_target
, (void *) output_target
);
3343 /* If the target is not supported, we cannot do anything. */
3346 enum bfd_endian desired_endian
;
3348 if (command_line
.endian
== ENDIAN_BIG
)
3349 desired_endian
= BFD_ENDIAN_BIG
;
3351 desired_endian
= BFD_ENDIAN_LITTLE
;
3353 /* See if the target has the wrong endianness. This should
3354 not happen if the linker script has provided big and
3355 little endian alternatives, but some scrips don't do
3357 if (target
->byteorder
!= desired_endian
)
3359 /* If it does, then see if the target provides
3360 an alternative with the correct endianness. */
3361 if (target
->alternative_target
!= NULL
3362 && (target
->alternative_target
->byteorder
== desired_endian
))
3363 output_target
= target
->alternative_target
->name
;
3366 /* Try to find a target as similar as possible to
3367 the default target, but which has the desired
3368 endian characteristic. */
3369 bfd_iterate_over_targets (closest_target_match
,
3372 /* Oh dear - we could not find any targets that
3373 satisfy our requirements. */
3375 einfo (_("%P: warning: could not find any targets"
3376 " that match endianness requirement\n"));
3378 output_target
= winner
->name
;
3384 link_info
.output_bfd
= bfd_openw (name
, output_target
);
3386 if (link_info
.output_bfd
== NULL
)
3388 if (bfd_get_error () == bfd_error_invalid_target
)
3389 einfo (_("%F%P: target %s not found\n"), output_target
);
3391 einfo (_("%F%P: cannot open output file %s: %E\n"), name
);
3394 delete_output_file_on_failure
= TRUE
;
3396 if (!bfd_set_format (link_info
.output_bfd
, bfd_object
))
3397 einfo (_("%F%P: %s: can not make object file: %E\n"), name
);
3398 if (!bfd_set_arch_mach (link_info
.output_bfd
,
3399 ldfile_output_architecture
,
3400 ldfile_output_machine
))
3401 einfo (_("%F%P: %s: can not set architecture: %E\n"), name
);
3403 link_info
.hash
= bfd_link_hash_table_create (link_info
.output_bfd
);
3404 if (link_info
.hash
== NULL
)
3405 einfo (_("%F%P: can not create hash table: %E\n"));
3407 bfd_set_gp_size (link_info
.output_bfd
, g_switch_value
);
3411 ldlang_open_output (lang_statement_union_type
*statement
)
3413 switch (statement
->header
.type
)
3415 case lang_output_statement_enum
:
3416 ASSERT (link_info
.output_bfd
== NULL
);
3417 open_output (statement
->output_statement
.name
);
3418 ldemul_set_output_arch ();
3419 if (config
.magic_demand_paged
3420 && !bfd_link_relocatable (&link_info
))
3421 link_info
.output_bfd
->flags
|= D_PAGED
;
3423 link_info
.output_bfd
->flags
&= ~D_PAGED
;
3424 if (config
.text_read_only
)
3425 link_info
.output_bfd
->flags
|= WP_TEXT
;
3427 link_info
.output_bfd
->flags
&= ~WP_TEXT
;
3428 if (link_info
.traditional_format
)
3429 link_info
.output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
3431 link_info
.output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
3434 case lang_target_statement_enum
:
3435 current_target
= statement
->target_statement
.target
;
3445 unsigned x
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3446 ldfile_output_machine
);
3449 while ((x
& 1) == 0)
3457 /* Open all the input files. */
3461 OPEN_BFD_NORMAL
= 0,
3465 #ifdef ENABLE_PLUGINS
3466 static lang_input_statement_type
*plugin_insert
= NULL
;
3467 static struct bfd_link_hash_entry
*plugin_undefs
= NULL
;
3471 open_input_bfds (lang_statement_union_type
*s
, enum open_bfd_mode mode
)
3473 for (; s
!= NULL
; s
= s
->header
.next
)
3475 switch (s
->header
.type
)
3477 case lang_constructors_statement_enum
:
3478 open_input_bfds (constructor_list
.head
, mode
);
3480 case lang_output_section_statement_enum
:
3481 open_input_bfds (s
->output_section_statement
.children
.head
, mode
);
3483 case lang_wild_statement_enum
:
3484 /* Maybe we should load the file's symbols. */
3485 if ((mode
& OPEN_BFD_RESCAN
) == 0
3486 && s
->wild_statement
.filename
3487 && !wildcardp (s
->wild_statement
.filename
)
3488 && !archive_path (s
->wild_statement
.filename
))
3489 lookup_name (s
->wild_statement
.filename
);
3490 open_input_bfds (s
->wild_statement
.children
.head
, mode
);
3492 case lang_group_statement_enum
:
3494 struct bfd_link_hash_entry
*undefs
;
3495 #ifdef ENABLE_PLUGINS
3496 lang_input_statement_type
*plugin_insert_save
;
3499 /* We must continually search the entries in the group
3500 until no new symbols are added to the list of undefined
3505 #ifdef ENABLE_PLUGINS
3506 plugin_insert_save
= plugin_insert
;
3508 undefs
= link_info
.hash
->undefs_tail
;
3509 open_input_bfds (s
->group_statement
.children
.head
,
3510 mode
| OPEN_BFD_FORCE
);
3512 while (undefs
!= link_info
.hash
->undefs_tail
3513 #ifdef ENABLE_PLUGINS
3514 /* Objects inserted by a plugin, which are loaded
3515 before we hit this loop, may have added new
3517 || (plugin_insert
!= plugin_insert_save
&& plugin_undefs
)
3522 case lang_target_statement_enum
:
3523 current_target
= s
->target_statement
.target
;
3525 case lang_input_statement_enum
:
3526 if (s
->input_statement
.flags
.real
)
3528 lang_statement_union_type
**os_tail
;
3529 lang_statement_list_type add
;
3532 s
->input_statement
.target
= current_target
;
3534 /* If we are being called from within a group, and this
3535 is an archive which has already been searched, then
3536 force it to be researched unless the whole archive
3537 has been loaded already. Do the same for a rescan.
3538 Likewise reload --as-needed shared libs. */
3539 if (mode
!= OPEN_BFD_NORMAL
3540 #ifdef ENABLE_PLUGINS
3541 && ((mode
& OPEN_BFD_RESCAN
) == 0
3542 || plugin_insert
== NULL
)
3544 && s
->input_statement
.flags
.loaded
3545 && (abfd
= s
->input_statement
.the_bfd
) != NULL
3546 && ((bfd_get_format (abfd
) == bfd_archive
3547 && !s
->input_statement
.flags
.whole_archive
)
3548 || (bfd_get_format (abfd
) == bfd_object
3549 && ((abfd
->flags
) & DYNAMIC
) != 0
3550 && s
->input_statement
.flags
.add_DT_NEEDED_for_regular
3551 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
3552 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)))
3554 s
->input_statement
.flags
.loaded
= FALSE
;
3555 s
->input_statement
.flags
.reload
= TRUE
;
3558 os_tail
= lang_os_list
.tail
;
3559 lang_list_init (&add
);
3561 if (!load_symbols (&s
->input_statement
, &add
))
3562 config
.make_executable
= FALSE
;
3564 if (add
.head
!= NULL
)
3566 /* If this was a script with output sections then
3567 tack any added statements on to the end of the
3568 list. This avoids having to reorder the output
3569 section statement list. Very likely the user
3570 forgot -T, and whatever we do here will not meet
3571 naive user expectations. */
3572 if (os_tail
!= lang_os_list
.tail
)
3574 einfo (_("%P: warning: %s contains output sections;"
3575 " did you forget -T?\n"),
3576 s
->input_statement
.filename
);
3577 *stat_ptr
->tail
= add
.head
;
3578 stat_ptr
->tail
= add
.tail
;
3582 *add
.tail
= s
->header
.next
;
3583 s
->header
.next
= add
.head
;
3587 #ifdef ENABLE_PLUGINS
3588 /* If we have found the point at which a plugin added new
3589 files, clear plugin_insert to enable archive rescan. */
3590 if (&s
->input_statement
== plugin_insert
)
3591 plugin_insert
= NULL
;
3594 case lang_assignment_statement_enum
:
3595 if (s
->assignment_statement
.exp
->type
.node_class
!= etree_assert
)
3596 exp_fold_tree_no_dot (s
->assignment_statement
.exp
);
3603 /* Exit if any of the files were missing. */
3604 if (input_flags
.missing_file
)
3608 /* Open the CTF sections in the input files with libctf: if any were opened,
3609 create a fake input file that we'll write the merged CTF data to later
3613 ldlang_open_ctf (void)
3618 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3622 /* Incoming files from the compiler have a single ctf_file_t in them
3623 (which is presented to us by the libctf API in a ctf_archive_t
3624 wrapper): files derived from a previous relocatable link have a CTF
3625 archive containing possibly many CTF files. */
3627 if ((file
->the_ctf
= ctf_bfdopen (file
->the_bfd
, &err
)) == NULL
)
3629 if (err
!= ECTF_NOCTFDATA
)
3630 einfo (_("%P: warning: CTF section in `%pI' not loaded: "
3631 "its types will be discarded: `%s'\n"), file
,
3636 /* Prevent the contents of this section from being written, while
3637 requiring the section itself to be duplicated in the output. */
3638 /* This section must exist if ctf_bfdopen() succeeded. */
3639 sect
= bfd_get_section_by_name (file
->the_bfd
, ".ctf");
3641 sect
->flags
|= SEC_NEVER_LOAD
| SEC_HAS_CONTENTS
| SEC_LINKER_CREATED
;
3652 if ((ctf_output
= ctf_create (&err
)) != NULL
)
3655 einfo (_("%P: warning: CTF output not created: `s'\n"),
3658 LANG_FOR_EACH_INPUT_STATEMENT (errfile
)
3659 ctf_close (errfile
->the_ctf
);
3662 /* Merge together CTF sections. After this, only the symtab-dependent
3663 function and data object sections need adjustment. */
3666 lang_merge_ctf (void)
3668 asection
*output_sect
;
3673 output_sect
= bfd_get_section_by_name (link_info
.output_bfd
, ".ctf");
3675 /* If the section was discarded, don't waste time merging. */
3676 if (output_sect
== NULL
)
3678 ctf_file_close (ctf_output
);
3681 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3683 ctf_close (file
->the_ctf
);
3684 file
->the_ctf
= NULL
;
3689 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3694 /* Takes ownership of file->u.the_ctfa. */
3695 if (ctf_link_add_ctf (ctf_output
, file
->the_ctf
, file
->filename
) < 0)
3697 einfo (_("%F%P: cannot link with CTF in %pB: %s\n"), file
->the_bfd
,
3698 ctf_errmsg (ctf_errno (ctf_output
)));
3699 ctf_close (file
->the_ctf
);
3700 file
->the_ctf
= NULL
;
3705 if (ctf_link (ctf_output
, CTF_LINK_SHARE_UNCONFLICTED
) < 0)
3707 einfo (_("%F%P: CTF linking failed; 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
;
3717 /* Let the emulation examine the symbol table and strtab to help it optimize the
3718 CTF, if supported. */
3721 ldlang_ctf_apply_strsym (struct elf_sym_strtab
*syms
, bfd_size_type symcount
,
3722 struct elf_strtab_hash
*symstrtab
)
3724 ldemul_examine_strtab_for_ctf (ctf_output
, syms
, symcount
, symstrtab
);
3727 /* Write out the CTF section. Called early, if the emulation isn't going to
3728 need to dedup against the strtab and symtab, then possibly called from the
3729 target linker code if the dedup has happened. */
3731 lang_write_ctf (int late
)
3734 asection
*output_sect
;
3741 /* Emit CTF late if this emulation says it can do so. */
3742 if (ldemul_emit_ctf_early ())
3747 if (!ldemul_emit_ctf_early ())
3753 output_sect
= bfd_get_section_by_name (link_info
.output_bfd
, ".ctf");
3756 output_sect
->contents
= ctf_link_write (ctf_output
, &output_size
,
3757 CTF_COMPRESSION_THRESHOLD
);
3758 output_sect
->size
= output_size
;
3759 output_sect
->flags
|= SEC_IN_MEMORY
| SEC_KEEP
;
3761 if (!output_sect
->contents
)
3763 einfo (_("%F%P: CTF section emission failed; output will have no "
3764 "CTF section: %s\n"), ctf_errmsg (ctf_errno (ctf_output
)));
3765 output_sect
->size
= 0;
3766 output_sect
->flags
|= SEC_EXCLUDE
;
3770 /* This also closes every CTF input file used in the link. */
3771 ctf_file_close (ctf_output
);
3774 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3775 file
->the_ctf
= NULL
;
3778 /* Write out the CTF section late, if the emulation needs that. */
3781 ldlang_write_ctf_late (void)
3783 /* Trigger a "late call", if the emulation needs one. */
3788 /* Add the supplied name to the symbol table as an undefined reference.
3789 This is a two step process as the symbol table doesn't even exist at
3790 the time the ld command line is processed. First we put the name
3791 on a list, then, once the output file has been opened, transfer the
3792 name to the symbol table. */
3794 typedef struct bfd_sym_chain ldlang_undef_chain_list_type
;
3796 #define ldlang_undef_chain_list_head entry_symbol.next
3799 ldlang_add_undef (const char *const name
, bfd_boolean cmdline
)
3801 ldlang_undef_chain_list_type
*new_undef
;
3803 undef_from_cmdline
= undef_from_cmdline
|| cmdline
;
3804 new_undef
= stat_alloc (sizeof (*new_undef
));
3805 new_undef
->next
= ldlang_undef_chain_list_head
;
3806 ldlang_undef_chain_list_head
= new_undef
;
3808 new_undef
->name
= xstrdup (name
);
3810 if (link_info
.output_bfd
!= NULL
)
3811 insert_undefined (new_undef
->name
);
3814 /* Insert NAME as undefined in the symbol table. */
3817 insert_undefined (const char *name
)
3819 struct bfd_link_hash_entry
*h
;
3821 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, FALSE
, TRUE
);
3823 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
3824 if (h
->type
== bfd_link_hash_new
)
3826 h
->type
= bfd_link_hash_undefined
;
3827 h
->u
.undef
.abfd
= NULL
;
3828 h
->non_ir_ref_regular
= TRUE
;
3829 if (is_elf_hash_table (link_info
.hash
))
3830 ((struct elf_link_hash_entry
*) h
)->mark
= 1;
3831 bfd_link_add_undef (link_info
.hash
, h
);
3835 /* Run through the list of undefineds created above and place them
3836 into the linker hash table as undefined symbols belonging to the
3840 lang_place_undefineds (void)
3842 ldlang_undef_chain_list_type
*ptr
;
3844 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
3845 insert_undefined (ptr
->name
);
3848 /* Structure used to build the list of symbols that the user has required
3851 struct require_defined_symbol
3854 struct require_defined_symbol
*next
;
3857 /* The list of symbols that the user has required be defined. */
3859 static struct require_defined_symbol
*require_defined_symbol_list
;
3861 /* Add a new symbol NAME to the list of symbols that are required to be
3865 ldlang_add_require_defined (const char *const name
)
3867 struct require_defined_symbol
*ptr
;
3869 ldlang_add_undef (name
, TRUE
);
3870 ptr
= stat_alloc (sizeof (*ptr
));
3871 ptr
->next
= require_defined_symbol_list
;
3872 ptr
->name
= strdup (name
);
3873 require_defined_symbol_list
= ptr
;
3876 /* Check that all symbols the user required to be defined, are defined,
3877 raise an error if we find a symbol that is not defined. */
3880 ldlang_check_require_defined_symbols (void)
3882 struct require_defined_symbol
*ptr
;
3884 for (ptr
= require_defined_symbol_list
; ptr
!= NULL
; ptr
= ptr
->next
)
3886 struct bfd_link_hash_entry
*h
;
3888 h
= bfd_link_hash_lookup (link_info
.hash
, ptr
->name
,
3889 FALSE
, FALSE
, TRUE
);
3891 || (h
->type
!= bfd_link_hash_defined
3892 && h
->type
!= bfd_link_hash_defweak
))
3893 einfo(_("%X%P: required symbol `%s' not defined\n"), ptr
->name
);
3897 /* Check for all readonly or some readwrite sections. */
3900 check_input_sections
3901 (lang_statement_union_type
*s
,
3902 lang_output_section_statement_type
*output_section_statement
)
3904 for (; s
!= NULL
; s
= s
->header
.next
)
3906 switch (s
->header
.type
)
3908 case lang_wild_statement_enum
:
3909 walk_wild (&s
->wild_statement
, check_section_callback
,
3910 output_section_statement
);
3911 if (!output_section_statement
->all_input_readonly
)
3914 case lang_constructors_statement_enum
:
3915 check_input_sections (constructor_list
.head
,
3916 output_section_statement
);
3917 if (!output_section_statement
->all_input_readonly
)
3920 case lang_group_statement_enum
:
3921 check_input_sections (s
->group_statement
.children
.head
,
3922 output_section_statement
);
3923 if (!output_section_statement
->all_input_readonly
)
3932 /* Update wildcard statements if needed. */
3935 update_wild_statements (lang_statement_union_type
*s
)
3937 struct wildcard_list
*sec
;
3939 switch (sort_section
)
3949 for (; s
!= NULL
; s
= s
->header
.next
)
3951 switch (s
->header
.type
)
3956 case lang_wild_statement_enum
:
3957 for (sec
= s
->wild_statement
.section_list
; sec
!= NULL
;
3959 /* Don't sort .init/.fini sections. */
3960 if (strcmp (sec
->spec
.name
, ".init") != 0
3961 && strcmp (sec
->spec
.name
, ".fini") != 0)
3962 switch (sec
->spec
.sorted
)
3965 sec
->spec
.sorted
= sort_section
;
3968 if (sort_section
== by_alignment
)
3969 sec
->spec
.sorted
= by_name_alignment
;
3972 if (sort_section
== by_name
)
3973 sec
->spec
.sorted
= by_alignment_name
;
3980 case lang_constructors_statement_enum
:
3981 update_wild_statements (constructor_list
.head
);
3984 case lang_output_section_statement_enum
:
3985 update_wild_statements
3986 (s
->output_section_statement
.children
.head
);
3989 case lang_group_statement_enum
:
3990 update_wild_statements (s
->group_statement
.children
.head
);
3998 /* Open input files and attach to output sections. */
4001 map_input_to_output_sections
4002 (lang_statement_union_type
*s
, const char *target
,
4003 lang_output_section_statement_type
*os
)
4005 for (; s
!= NULL
; s
= s
->header
.next
)
4007 lang_output_section_statement_type
*tos
;
4010 switch (s
->header
.type
)
4012 case lang_wild_statement_enum
:
4013 wild (&s
->wild_statement
, target
, os
);
4015 case lang_constructors_statement_enum
:
4016 map_input_to_output_sections (constructor_list
.head
,
4020 case lang_output_section_statement_enum
:
4021 tos
= &s
->output_section_statement
;
4022 if (tos
->constraint
!= 0)
4024 if (tos
->constraint
!= ONLY_IF_RW
4025 && tos
->constraint
!= ONLY_IF_RO
)
4027 tos
->all_input_readonly
= TRUE
;
4028 check_input_sections (tos
->children
.head
, tos
);
4029 if (tos
->all_input_readonly
!= (tos
->constraint
== ONLY_IF_RO
))
4031 tos
->constraint
= -1;
4035 map_input_to_output_sections (tos
->children
.head
,
4039 case lang_output_statement_enum
:
4041 case lang_target_statement_enum
:
4042 target
= s
->target_statement
.target
;
4044 case lang_group_statement_enum
:
4045 map_input_to_output_sections (s
->group_statement
.children
.head
,
4049 case lang_data_statement_enum
:
4050 /* Make sure that any sections mentioned in the expression
4052 exp_init_os (s
->data_statement
.exp
);
4053 /* The output section gets CONTENTS, ALLOC and LOAD, but
4054 these may be overridden by the script. */
4055 flags
= SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
;
4056 switch (os
->sectype
)
4058 case normal_section
:
4059 case overlay_section
:
4060 case first_overlay_section
:
4062 case noalloc_section
:
4063 flags
= SEC_HAS_CONTENTS
;
4065 case noload_section
:
4066 if (bfd_get_flavour (link_info
.output_bfd
)
4067 == bfd_target_elf_flavour
)
4068 flags
= SEC_NEVER_LOAD
| SEC_ALLOC
;
4070 flags
= SEC_NEVER_LOAD
| SEC_HAS_CONTENTS
;
4073 if (os
->bfd_section
== NULL
)
4074 init_os (os
, flags
);
4076 os
->bfd_section
->flags
|= flags
;
4078 case lang_input_section_enum
:
4080 case lang_fill_statement_enum
:
4081 case lang_object_symbols_statement_enum
:
4082 case lang_reloc_statement_enum
:
4083 case lang_padding_statement_enum
:
4084 case lang_input_statement_enum
:
4085 if (os
!= NULL
&& os
->bfd_section
== NULL
)
4088 case lang_assignment_statement_enum
:
4089 if (os
!= NULL
&& os
->bfd_section
== NULL
)
4092 /* Make sure that any sections mentioned in the assignment
4094 exp_init_os (s
->assignment_statement
.exp
);
4096 case lang_address_statement_enum
:
4097 /* Mark the specified section with the supplied address.
4098 If this section was actually a segment marker, then the
4099 directive is ignored if the linker script explicitly
4100 processed the segment marker. Originally, the linker
4101 treated segment directives (like -Ttext on the
4102 command-line) as section directives. We honor the
4103 section directive semantics for backwards compatibility;
4104 linker scripts that do not specifically check for
4105 SEGMENT_START automatically get the old semantics. */
4106 if (!s
->address_statement
.segment
4107 || !s
->address_statement
.segment
->used
)
4109 const char *name
= s
->address_statement
.section_name
;
4111 /* Create the output section statement here so that
4112 orphans with a set address will be placed after other
4113 script sections. If we let the orphan placement code
4114 place them in amongst other sections then the address
4115 will affect following script sections, which is
4116 likely to surprise naive users. */
4117 tos
= lang_output_section_statement_lookup (name
, 0, TRUE
);
4118 tos
->addr_tree
= s
->address_statement
.address
;
4119 if (tos
->bfd_section
== NULL
)
4123 case lang_insert_statement_enum
:
4129 /* An insert statement snips out all the linker statements from the
4130 start of the list and places them after the output section
4131 statement specified by the insert. This operation is complicated
4132 by the fact that we keep a doubly linked list of output section
4133 statements as well as the singly linked list of all statements.
4134 FIXME someday: Twiddling with the list not only moves statements
4135 from the user's script but also input and group statements that are
4136 built from command line object files and --start-group. We only
4137 get away with this because the list pointers used by file_chain
4138 and input_file_chain are not reordered, and processing via
4139 statement_list after this point mostly ignores input statements.
4140 One exception is the map file, where LOAD and START GROUP/END GROUP
4141 can end up looking odd. */
4144 process_insert_statements (lang_statement_union_type
**start
)
4146 lang_statement_union_type
**s
;
4147 lang_output_section_statement_type
*first_os
= NULL
;
4148 lang_output_section_statement_type
*last_os
= NULL
;
4149 lang_output_section_statement_type
*os
;
4154 if ((*s
)->header
.type
== lang_output_section_statement_enum
)
4156 /* Keep pointers to the first and last output section
4157 statement in the sequence we may be about to move. */
4158 os
= &(*s
)->output_section_statement
;
4160 ASSERT (last_os
== NULL
|| last_os
->next
== os
);
4163 /* Set constraint negative so that lang_output_section_find
4164 won't match this output section statement. At this
4165 stage in linking constraint has values in the range
4166 [-1, ONLY_IN_RW]. */
4167 last_os
->constraint
= -2 - last_os
->constraint
;
4168 if (first_os
== NULL
)
4171 else if ((*s
)->header
.type
== lang_group_statement_enum
)
4173 /* A user might put -T between --start-group and
4174 --end-group. One way this odd construct might arise is
4175 from a wrapper around ld to change library search
4176 behaviour. For example:
4178 exec real_ld --start-group "$@" --end-group
4179 This isn't completely unreasonable so go looking inside a
4180 group statement for insert statements. */
4181 process_insert_statements (&(*s
)->group_statement
.children
.head
);
4183 else if ((*s
)->header
.type
== lang_insert_statement_enum
)
4185 lang_insert_statement_type
*i
= &(*s
)->insert_statement
;
4186 lang_output_section_statement_type
*where
;
4187 lang_statement_union_type
**ptr
;
4188 lang_statement_union_type
*first
;
4190 where
= lang_output_section_find (i
->where
);
4191 if (where
!= NULL
&& i
->is_before
)
4194 where
= where
->prev
;
4195 while (where
!= NULL
&& where
->constraint
< 0);
4199 einfo (_("%F%P: %s not found for insert\n"), i
->where
);
4203 /* Deal with reordering the output section statement list. */
4204 if (last_os
!= NULL
)
4206 asection
*first_sec
, *last_sec
;
4207 struct lang_output_section_statement_struct
**next
;
4209 /* Snip out the output sections we are moving. */
4210 first_os
->prev
->next
= last_os
->next
;
4211 if (last_os
->next
== NULL
)
4213 next
= &first_os
->prev
->next
;
4214 lang_os_list
.tail
= (lang_statement_union_type
**) next
;
4217 last_os
->next
->prev
= first_os
->prev
;
4218 /* Add them in at the new position. */
4219 last_os
->next
= where
->next
;
4220 if (where
->next
== NULL
)
4222 next
= &last_os
->next
;
4223 lang_os_list
.tail
= (lang_statement_union_type
**) next
;
4226 where
->next
->prev
= last_os
;
4227 first_os
->prev
= where
;
4228 where
->next
= first_os
;
4230 /* Move the bfd sections in the same way. */
4233 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
4235 os
->constraint
= -2 - os
->constraint
;
4236 if (os
->bfd_section
!= NULL
4237 && os
->bfd_section
->owner
!= NULL
)
4239 last_sec
= os
->bfd_section
;
4240 if (first_sec
== NULL
)
4241 first_sec
= last_sec
;
4246 if (last_sec
!= NULL
)
4248 asection
*sec
= where
->bfd_section
;
4250 sec
= output_prev_sec_find (where
);
4252 /* The place we want to insert must come after the
4253 sections we are moving. So if we find no
4254 section or if the section is the same as our
4255 last section, then no move is needed. */
4256 if (sec
!= NULL
&& sec
!= last_sec
)
4258 /* Trim them off. */
4259 if (first_sec
->prev
!= NULL
)
4260 first_sec
->prev
->next
= last_sec
->next
;
4262 link_info
.output_bfd
->sections
= last_sec
->next
;
4263 if (last_sec
->next
!= NULL
)
4264 last_sec
->next
->prev
= first_sec
->prev
;
4266 link_info
.output_bfd
->section_last
= first_sec
->prev
;
4268 last_sec
->next
= sec
->next
;
4269 if (sec
->next
!= NULL
)
4270 sec
->next
->prev
= last_sec
;
4272 link_info
.output_bfd
->section_last
= last_sec
;
4273 first_sec
->prev
= sec
;
4274 sec
->next
= first_sec
;
4282 ptr
= insert_os_after (where
);
4283 /* Snip everything from the start of the list, up to and
4284 including the insert statement we are currently processing. */
4286 *start
= (*s
)->header
.next
;
4287 /* Add them back where they belong, minus the insert. */
4290 statement_list
.tail
= s
;
4295 s
= &(*s
)->header
.next
;
4298 /* Undo constraint twiddling. */
4299 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
4301 os
->constraint
= -2 - os
->constraint
;
4307 /* An output section might have been removed after its statement was
4308 added. For example, ldemul_before_allocation can remove dynamic
4309 sections if they turn out to be not needed. Clean them up here. */
4312 strip_excluded_output_sections (void)
4314 lang_output_section_statement_type
*os
;
4316 /* Run lang_size_sections (if not already done). */
4317 if (expld
.phase
!= lang_mark_phase_enum
)
4319 expld
.phase
= lang_mark_phase_enum
;
4320 expld
.dataseg
.phase
= exp_seg_none
;
4321 one_lang_size_sections_pass (NULL
, FALSE
);
4322 lang_reset_memory_regions ();
4325 for (os
= &lang_os_list
.head
->output_section_statement
;
4329 asection
*output_section
;
4330 bfd_boolean exclude
;
4332 if (os
->constraint
< 0)
4335 output_section
= os
->bfd_section
;
4336 if (output_section
== NULL
)
4339 exclude
= (output_section
->rawsize
== 0
4340 && (output_section
->flags
& SEC_KEEP
) == 0
4341 && !bfd_section_removed_from_list (link_info
.output_bfd
,
4344 /* Some sections have not yet been sized, notably .gnu.version,
4345 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
4346 input sections, so don't drop output sections that have such
4347 input sections unless they are also marked SEC_EXCLUDE. */
4348 if (exclude
&& output_section
->map_head
.s
!= NULL
)
4352 for (s
= output_section
->map_head
.s
; s
!= NULL
; s
= s
->map_head
.s
)
4353 if ((s
->flags
& SEC_EXCLUDE
) == 0
4354 && ((s
->flags
& SEC_LINKER_CREATED
) != 0
4355 || link_info
.emitrelocations
))
4364 /* We don't set bfd_section to NULL since bfd_section of the
4365 removed output section statement may still be used. */
4366 if (!os
->update_dot
)
4368 output_section
->flags
|= SEC_EXCLUDE
;
4369 bfd_section_list_remove (link_info
.output_bfd
, output_section
);
4370 link_info
.output_bfd
->section_count
--;
4375 /* Called from ldwrite to clear out asection.map_head and
4376 asection.map_tail for use as link_orders in ldwrite. */
4379 lang_clear_os_map (void)
4381 lang_output_section_statement_type
*os
;
4383 if (map_head_is_link_order
)
4386 for (os
= &lang_os_list
.head
->output_section_statement
;
4390 asection
*output_section
;
4392 if (os
->constraint
< 0)
4395 output_section
= os
->bfd_section
;
4396 if (output_section
== NULL
)
4399 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
4400 output_section
->map_head
.link_order
= NULL
;
4401 output_section
->map_tail
.link_order
= NULL
;
4404 /* Stop future calls to lang_add_section from messing with map_head
4405 and map_tail link_order fields. */
4406 map_head_is_link_order
= TRUE
;
4410 print_output_section_statement
4411 (lang_output_section_statement_type
*output_section_statement
)
4413 asection
*section
= output_section_statement
->bfd_section
;
4416 if (output_section_statement
!= abs_output_section
)
4418 minfo ("\n%s", output_section_statement
->name
);
4420 if (section
!= NULL
)
4422 print_dot
= section
->vma
;
4424 len
= strlen (output_section_statement
->name
);
4425 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
4430 while (len
< SECTION_NAME_MAP_LENGTH
)
4436 minfo ("0x%V %W", section
->vma
, TO_ADDR (section
->size
));
4438 if (section
->vma
!= section
->lma
)
4439 minfo (_(" load address 0x%V"), section
->lma
);
4441 if (output_section_statement
->update_dot_tree
!= NULL
)
4442 exp_fold_tree (output_section_statement
->update_dot_tree
,
4443 bfd_abs_section_ptr
, &print_dot
);
4449 print_statement_list (output_section_statement
->children
.head
,
4450 output_section_statement
);
4454 print_assignment (lang_assignment_statement_type
*assignment
,
4455 lang_output_section_statement_type
*output_section
)
4462 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
4465 if (assignment
->exp
->type
.node_class
== etree_assert
)
4468 tree
= assignment
->exp
->assert_s
.child
;
4472 const char *dst
= assignment
->exp
->assign
.dst
;
4474 is_dot
= (dst
[0] == '.' && dst
[1] == 0);
4475 tree
= assignment
->exp
;
4478 osec
= output_section
->bfd_section
;
4480 osec
= bfd_abs_section_ptr
;
4482 if (assignment
->exp
->type
.node_class
!= etree_provide
)
4483 exp_fold_tree (tree
, osec
, &print_dot
);
4485 expld
.result
.valid_p
= FALSE
;
4487 if (expld
.result
.valid_p
)
4491 if (assignment
->exp
->type
.node_class
== etree_assert
4493 || expld
.assign_name
!= NULL
)
4495 value
= expld
.result
.value
;
4497 if (expld
.result
.section
!= NULL
)
4498 value
+= expld
.result
.section
->vma
;
4500 minfo ("0x%V", value
);
4506 struct bfd_link_hash_entry
*h
;
4508 h
= bfd_link_hash_lookup (link_info
.hash
, assignment
->exp
->assign
.dst
,
4509 FALSE
, FALSE
, TRUE
);
4511 && (h
->type
== bfd_link_hash_defined
4512 || h
->type
== bfd_link_hash_defweak
))
4514 value
= h
->u
.def
.value
;
4515 value
+= h
->u
.def
.section
->output_section
->vma
;
4516 value
+= h
->u
.def
.section
->output_offset
;
4518 minfo ("[0x%V]", value
);
4521 minfo ("[unresolved]");
4526 if (assignment
->exp
->type
.node_class
== etree_provide
)
4527 minfo ("[!provide]");
4534 expld
.assign_name
= NULL
;
4537 exp_print_tree (assignment
->exp
);
4542 print_input_statement (lang_input_statement_type
*statm
)
4544 if (statm
->filename
!= NULL
)
4545 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
4548 /* Print all symbols defined in a particular section. This is called
4549 via bfd_link_hash_traverse, or by print_all_symbols. */
4552 print_one_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
)
4554 asection
*sec
= (asection
*) ptr
;
4556 if ((hash_entry
->type
== bfd_link_hash_defined
4557 || hash_entry
->type
== bfd_link_hash_defweak
)
4558 && sec
== hash_entry
->u
.def
.section
)
4562 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
4565 (hash_entry
->u
.def
.value
4566 + hash_entry
->u
.def
.section
->output_offset
4567 + hash_entry
->u
.def
.section
->output_section
->vma
));
4569 minfo (" %pT\n", hash_entry
->root
.string
);
4576 hash_entry_addr_cmp (const void *a
, const void *b
)
4578 const struct bfd_link_hash_entry
*l
= *(const struct bfd_link_hash_entry
**)a
;
4579 const struct bfd_link_hash_entry
*r
= *(const struct bfd_link_hash_entry
**)b
;
4581 if (l
->u
.def
.value
< r
->u
.def
.value
)
4583 else if (l
->u
.def
.value
> r
->u
.def
.value
)
4590 print_all_symbols (asection
*sec
)
4592 input_section_userdata_type
*ud
= bfd_section_userdata (sec
);
4593 struct map_symbol_def
*def
;
4594 struct bfd_link_hash_entry
**entries
;
4600 *ud
->map_symbol_def_tail
= 0;
4602 /* Sort the symbols by address. */
4603 entries
= (struct bfd_link_hash_entry
**)
4604 obstack_alloc (&map_obstack
,
4605 ud
->map_symbol_def_count
* sizeof (*entries
));
4607 for (i
= 0, def
= ud
->map_symbol_def_head
; def
; def
= def
->next
, i
++)
4608 entries
[i
] = def
->entry
;
4610 qsort (entries
, ud
->map_symbol_def_count
, sizeof (*entries
),
4611 hash_entry_addr_cmp
);
4613 /* Print the symbols. */
4614 for (i
= 0; i
< ud
->map_symbol_def_count
; i
++)
4615 print_one_symbol (entries
[i
], sec
);
4617 obstack_free (&map_obstack
, entries
);
4620 /* Print information about an input section to the map file. */
4623 print_input_section (asection
*i
, bfd_boolean is_discarded
)
4625 bfd_size_type size
= i
->size
;
4632 minfo ("%s", i
->name
);
4634 len
= 1 + strlen (i
->name
);
4635 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
4640 while (len
< SECTION_NAME_MAP_LENGTH
)
4646 if (i
->output_section
!= NULL
4647 && i
->output_section
->owner
== link_info
.output_bfd
)
4648 addr
= i
->output_section
->vma
+ i
->output_offset
;
4656 minfo ("0x%V %W %pB\n", addr
, TO_ADDR (size
), i
->owner
);
4658 if (size
!= i
->rawsize
&& i
->rawsize
!= 0)
4660 len
= SECTION_NAME_MAP_LENGTH
+ 3;
4672 minfo (_("%W (size before relaxing)\n"), TO_ADDR (i
->rawsize
));
4675 if (i
->output_section
!= NULL
4676 && i
->output_section
->owner
== link_info
.output_bfd
)
4678 if (link_info
.reduce_memory_overheads
)
4679 bfd_link_hash_traverse (link_info
.hash
, print_one_symbol
, i
);
4681 print_all_symbols (i
);
4683 /* Update print_dot, but make sure that we do not move it
4684 backwards - this could happen if we have overlays and a
4685 later overlay is shorter than an earier one. */
4686 if (addr
+ TO_ADDR (size
) > print_dot
)
4687 print_dot
= addr
+ TO_ADDR (size
);
4692 print_fill_statement (lang_fill_statement_type
*fill
)
4696 fputs (" FILL mask 0x", config
.map_file
);
4697 for (p
= fill
->fill
->data
, size
= fill
->fill
->size
; size
!= 0; p
++, size
--)
4698 fprintf (config
.map_file
, "%02x", *p
);
4699 fputs ("\n", config
.map_file
);
4703 print_data_statement (lang_data_statement_type
*data
)
4711 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
4714 addr
= data
->output_offset
;
4715 if (data
->output_section
!= NULL
)
4716 addr
+= data
->output_section
->vma
;
4744 if (size
< TO_SIZE ((unsigned) 1))
4745 size
= TO_SIZE ((unsigned) 1);
4746 minfo ("0x%V %W %s 0x%v", addr
, TO_ADDR (size
), name
, data
->value
);
4748 if (data
->exp
->type
.node_class
!= etree_value
)
4751 exp_print_tree (data
->exp
);
4756 print_dot
= addr
+ TO_ADDR (size
);
4759 /* Print an address statement. These are generated by options like
4763 print_address_statement (lang_address_statement_type
*address
)
4765 minfo (_("Address of section %s set to "), address
->section_name
);
4766 exp_print_tree (address
->address
);
4770 /* Print a reloc statement. */
4773 print_reloc_statement (lang_reloc_statement_type
*reloc
)
4780 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
4783 addr
= reloc
->output_offset
;
4784 if (reloc
->output_section
!= NULL
)
4785 addr
+= reloc
->output_section
->vma
;
4787 size
= bfd_get_reloc_size (reloc
->howto
);
4789 minfo ("0x%V %W RELOC %s ", addr
, TO_ADDR (size
), reloc
->howto
->name
);
4791 if (reloc
->name
!= NULL
)
4792 minfo ("%s+", reloc
->name
);
4794 minfo ("%s+", reloc
->section
->name
);
4796 exp_print_tree (reloc
->addend_exp
);
4800 print_dot
= addr
+ TO_ADDR (size
);
4804 print_padding_statement (lang_padding_statement_type
*s
)
4812 len
= sizeof " *fill*" - 1;
4813 while (len
< SECTION_NAME_MAP_LENGTH
)
4819 addr
= s
->output_offset
;
4820 if (s
->output_section
!= NULL
)
4821 addr
+= s
->output_section
->vma
;
4822 minfo ("0x%V %W ", addr
, TO_ADDR (s
->size
));
4824 if (s
->fill
->size
!= 0)
4828 for (p
= s
->fill
->data
, size
= s
->fill
->size
; size
!= 0; p
++, size
--)
4829 fprintf (config
.map_file
, "%02x", *p
);
4834 print_dot
= addr
+ TO_ADDR (s
->size
);
4838 print_wild_statement (lang_wild_statement_type
*w
,
4839 lang_output_section_statement_type
*os
)
4841 struct wildcard_list
*sec
;
4845 if (w
->exclude_name_list
)
4848 minfo ("EXCLUDE_FILE(%s", w
->exclude_name_list
->name
);
4849 for (tmp
= w
->exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
4850 minfo (" %s", tmp
->name
);
4854 if (w
->filenames_sorted
)
4855 minfo ("SORT_BY_NAME(");
4856 if (w
->filename
!= NULL
)
4857 minfo ("%s", w
->filename
);
4860 if (w
->filenames_sorted
)
4864 for (sec
= w
->section_list
; sec
; sec
= sec
->next
)
4866 int closing_paren
= 0;
4868 switch (sec
->spec
.sorted
)
4874 minfo ("SORT_BY_NAME(");
4879 minfo ("SORT_BY_ALIGNMENT(");
4883 case by_name_alignment
:
4884 minfo ("SORT_BY_NAME(SORT_BY_ALIGNMENT(");
4888 case by_alignment_name
:
4889 minfo ("SORT_BY_ALIGNMENT(SORT_BY_NAME(");
4894 minfo ("SORT_NONE(");
4898 case by_init_priority
:
4899 minfo ("SORT_BY_INIT_PRIORITY(");
4904 if (sec
->spec
.exclude_name_list
!= NULL
)
4907 minfo ("EXCLUDE_FILE(%s", sec
->spec
.exclude_name_list
->name
);
4908 for (tmp
= sec
->spec
.exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
4909 minfo (" %s", tmp
->name
);
4912 if (sec
->spec
.name
!= NULL
)
4913 minfo ("%s", sec
->spec
.name
);
4916 for (;closing_paren
> 0; closing_paren
--)
4925 print_statement_list (w
->children
.head
, os
);
4928 /* Print a group statement. */
4931 print_group (lang_group_statement_type
*s
,
4932 lang_output_section_statement_type
*os
)
4934 fprintf (config
.map_file
, "START GROUP\n");
4935 print_statement_list (s
->children
.head
, os
);
4936 fprintf (config
.map_file
, "END GROUP\n");
4939 /* Print the list of statements in S.
4940 This can be called for any statement type. */
4943 print_statement_list (lang_statement_union_type
*s
,
4944 lang_output_section_statement_type
*os
)
4948 print_statement (s
, os
);
4953 /* Print the first statement in statement list S.
4954 This can be called for any statement type. */
4957 print_statement (lang_statement_union_type
*s
,
4958 lang_output_section_statement_type
*os
)
4960 switch (s
->header
.type
)
4963 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
4966 case lang_constructors_statement_enum
:
4967 if (constructor_list
.head
!= NULL
)
4969 if (constructors_sorted
)
4970 minfo (" SORT (CONSTRUCTORS)\n");
4972 minfo (" CONSTRUCTORS\n");
4973 print_statement_list (constructor_list
.head
, os
);
4976 case lang_wild_statement_enum
:
4977 print_wild_statement (&s
->wild_statement
, os
);
4979 case lang_address_statement_enum
:
4980 print_address_statement (&s
->address_statement
);
4982 case lang_object_symbols_statement_enum
:
4983 minfo (" CREATE_OBJECT_SYMBOLS\n");
4985 case lang_fill_statement_enum
:
4986 print_fill_statement (&s
->fill_statement
);
4988 case lang_data_statement_enum
:
4989 print_data_statement (&s
->data_statement
);
4991 case lang_reloc_statement_enum
:
4992 print_reloc_statement (&s
->reloc_statement
);
4994 case lang_input_section_enum
:
4995 print_input_section (s
->input_section
.section
, FALSE
);
4997 case lang_padding_statement_enum
:
4998 print_padding_statement (&s
->padding_statement
);
5000 case lang_output_section_statement_enum
:
5001 print_output_section_statement (&s
->output_section_statement
);
5003 case lang_assignment_statement_enum
:
5004 print_assignment (&s
->assignment_statement
, os
);
5006 case lang_target_statement_enum
:
5007 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
5009 case lang_output_statement_enum
:
5010 minfo ("OUTPUT(%s", s
->output_statement
.name
);
5011 if (output_target
!= NULL
)
5012 minfo (" %s", output_target
);
5015 case lang_input_statement_enum
:
5016 print_input_statement (&s
->input_statement
);
5018 case lang_group_statement_enum
:
5019 print_group (&s
->group_statement
, os
);
5021 case lang_insert_statement_enum
:
5022 minfo ("INSERT %s %s\n",
5023 s
->insert_statement
.is_before
? "BEFORE" : "AFTER",
5024 s
->insert_statement
.where
);
5030 print_statements (void)
5032 print_statement_list (statement_list
.head
, abs_output_section
);
5035 /* Print the first N statements in statement list S to STDERR.
5036 If N == 0, nothing is printed.
5037 If N < 0, the entire list is printed.
5038 Intended to be called from GDB. */
5041 dprint_statement (lang_statement_union_type
*s
, int n
)
5043 FILE *map_save
= config
.map_file
;
5045 config
.map_file
= stderr
;
5048 print_statement_list (s
, abs_output_section
);
5051 while (s
&& --n
>= 0)
5053 print_statement (s
, abs_output_section
);
5058 config
.map_file
= map_save
;
5062 insert_pad (lang_statement_union_type
**ptr
,
5064 bfd_size_type alignment_needed
,
5065 asection
*output_section
,
5068 static fill_type zero_fill
;
5069 lang_statement_union_type
*pad
= NULL
;
5071 if (ptr
!= &statement_list
.head
)
5072 pad
= ((lang_statement_union_type
*)
5073 ((char *) ptr
- offsetof (lang_statement_union_type
, header
.next
)));
5075 && pad
->header
.type
== lang_padding_statement_enum
5076 && pad
->padding_statement
.output_section
== output_section
)
5078 /* Use the existing pad statement. */
5080 else if ((pad
= *ptr
) != NULL
5081 && pad
->header
.type
== lang_padding_statement_enum
5082 && pad
->padding_statement
.output_section
== output_section
)
5084 /* Use the existing pad statement. */
5088 /* Make a new padding statement, linked into existing chain. */
5089 pad
= stat_alloc (sizeof (lang_padding_statement_type
));
5090 pad
->header
.next
= *ptr
;
5092 pad
->header
.type
= lang_padding_statement_enum
;
5093 pad
->padding_statement
.output_section
= output_section
;
5096 pad
->padding_statement
.fill
= fill
;
5098 pad
->padding_statement
.output_offset
= dot
- output_section
->vma
;
5099 pad
->padding_statement
.size
= alignment_needed
;
5100 if (!(output_section
->flags
& SEC_FIXED_SIZE
))
5101 output_section
->size
= TO_SIZE (dot
+ TO_ADDR (alignment_needed
)
5102 - output_section
->vma
);
5105 /* Work out how much this section will move the dot point. */
5109 (lang_statement_union_type
**this_ptr
,
5110 lang_output_section_statement_type
*output_section_statement
,
5114 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
5115 asection
*i
= is
->section
;
5116 asection
*o
= output_section_statement
->bfd_section
;
5118 if (i
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
5119 i
->output_offset
= i
->vma
- o
->vma
;
5120 else if (((i
->flags
& SEC_EXCLUDE
) != 0)
5121 || output_section_statement
->ignored
)
5122 i
->output_offset
= dot
- o
->vma
;
5125 bfd_size_type alignment_needed
;
5127 /* Align this section first to the input sections requirement,
5128 then to the output section's requirement. If this alignment
5129 is greater than any seen before, then record it too. Perform
5130 the alignment by inserting a magic 'padding' statement. */
5132 if (output_section_statement
->subsection_alignment
!= NULL
)
5134 = exp_get_power (output_section_statement
->subsection_alignment
,
5135 "subsection alignment");
5137 if (o
->alignment_power
< i
->alignment_power
)
5138 o
->alignment_power
= i
->alignment_power
;
5140 alignment_needed
= align_power (dot
, i
->alignment_power
) - dot
;
5142 if (alignment_needed
!= 0)
5144 insert_pad (this_ptr
, fill
, TO_SIZE (alignment_needed
), o
, dot
);
5145 dot
+= alignment_needed
;
5148 /* Remember where in the output section this input section goes. */
5149 i
->output_offset
= dot
- o
->vma
;
5151 /* Mark how big the output section must be to contain this now. */
5152 dot
+= TO_ADDR (i
->size
);
5153 if (!(o
->flags
& SEC_FIXED_SIZE
))
5154 o
->size
= TO_SIZE (dot
- o
->vma
);
5167 sort_sections_by_lma (const void *arg1
, const void *arg2
)
5169 const asection
*sec1
= ((const struct check_sec
*) arg1
)->sec
;
5170 const asection
*sec2
= ((const struct check_sec
*) arg2
)->sec
;
5172 if (sec1
->lma
< sec2
->lma
)
5174 else if (sec1
->lma
> sec2
->lma
)
5176 else if (sec1
->id
< sec2
->id
)
5178 else if (sec1
->id
> sec2
->id
)
5185 sort_sections_by_vma (const void *arg1
, const void *arg2
)
5187 const asection
*sec1
= ((const struct check_sec
*) arg1
)->sec
;
5188 const asection
*sec2
= ((const struct check_sec
*) arg2
)->sec
;
5190 if (sec1
->vma
< sec2
->vma
)
5192 else if (sec1
->vma
> sec2
->vma
)
5194 else if (sec1
->id
< sec2
->id
)
5196 else if (sec1
->id
> sec2
->id
)
5202 #define IS_TBSS(s) \
5203 ((s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == SEC_THREAD_LOCAL)
5205 #define IGNORE_SECTION(s) \
5206 ((s->flags & SEC_ALLOC) == 0 || IS_TBSS (s))
5208 /* Check to see if any allocated sections overlap with other allocated
5209 sections. This can happen if a linker script specifies the output
5210 section addresses of the two sections. Also check whether any memory
5211 region has overflowed. */
5214 lang_check_section_addresses (void)
5217 struct check_sec
*sections
;
5222 bfd_vma p_start
= 0;
5224 lang_memory_region_type
*m
;
5225 bfd_boolean overlays
;
5227 /* Detect address space overflow on allocated sections. */
5228 addr_mask
= ((bfd_vma
) 1 <<
5229 (bfd_arch_bits_per_address (link_info
.output_bfd
) - 1)) - 1;
5230 addr_mask
= (addr_mask
<< 1) + 1;
5231 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5232 if ((s
->flags
& SEC_ALLOC
) != 0)
5234 s_end
= (s
->vma
+ s
->size
) & addr_mask
;
5235 if (s_end
!= 0 && s_end
< (s
->vma
& addr_mask
))
5236 einfo (_("%X%P: section %s VMA wraps around address space\n"),
5240 s_end
= (s
->lma
+ s
->size
) & addr_mask
;
5241 if (s_end
!= 0 && s_end
< (s
->lma
& addr_mask
))
5242 einfo (_("%X%P: section %s LMA wraps around address space\n"),
5247 if (bfd_count_sections (link_info
.output_bfd
) <= 1)
5250 count
= bfd_count_sections (link_info
.output_bfd
);
5251 sections
= XNEWVEC (struct check_sec
, count
);
5253 /* Scan all sections in the output list. */
5255 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5257 if (IGNORE_SECTION (s
)
5261 sections
[count
].sec
= s
;
5262 sections
[count
].warned
= FALSE
;
5272 qsort (sections
, count
, sizeof (*sections
), sort_sections_by_lma
);
5274 /* First check section LMAs. There should be no overlap of LMAs on
5275 loadable sections, even with overlays. */
5276 for (p
= NULL
, i
= 0; i
< count
; i
++)
5278 s
= sections
[i
].sec
;
5279 if ((s
->flags
& SEC_LOAD
) != 0)
5282 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
5284 /* Look for an overlap. We have sorted sections by lma, so
5285 we know that s_start >= p_start. Besides the obvious
5286 case of overlap when the current section starts before
5287 the previous one ends, we also must have overlap if the
5288 previous section wraps around the address space. */
5290 && (s_start
<= p_end
5291 || p_end
< p_start
))
5293 einfo (_("%X%P: section %s LMA [%V,%V]"
5294 " overlaps section %s LMA [%V,%V]\n"),
5295 s
->name
, s_start
, s_end
, p
->name
, p_start
, p_end
);
5296 sections
[i
].warned
= TRUE
;
5304 /* If any non-zero size allocated section (excluding tbss) starts at
5305 exactly the same VMA as another such section, then we have
5306 overlays. Overlays generated by the OVERLAY keyword will have
5307 this property. It is possible to intentionally generate overlays
5308 that fail this test, but it would be unusual. */
5309 qsort (sections
, count
, sizeof (*sections
), sort_sections_by_vma
);
5311 p_start
= sections
[0].sec
->vma
;
5312 for (i
= 1; i
< count
; i
++)
5314 s_start
= sections
[i
].sec
->vma
;
5315 if (p_start
== s_start
)
5323 /* Now check section VMAs if no overlays were detected. */
5326 for (p
= NULL
, i
= 0; i
< count
; i
++)
5328 s
= sections
[i
].sec
;
5330 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
5333 && !sections
[i
].warned
5334 && (s_start
<= p_end
5335 || p_end
< p_start
))
5336 einfo (_("%X%P: section %s VMA [%V,%V]"
5337 " overlaps section %s VMA [%V,%V]\n"),
5338 s
->name
, s_start
, s_end
, p
->name
, p_start
, p_end
);
5347 /* If any memory region has overflowed, report by how much.
5348 We do not issue this diagnostic for regions that had sections
5349 explicitly placed outside their bounds; os_region_check's
5350 diagnostics are adequate for that case.
5352 FIXME: It is conceivable that m->current - (m->origin + m->length)
5353 might overflow a 32-bit integer. There is, alas, no way to print
5354 a bfd_vma quantity in decimal. */
5355 for (m
= lang_memory_region_list
; m
; m
= m
->next
)
5356 if (m
->had_full_message
)
5358 unsigned long over
= m
->current
- (m
->origin
+ m
->length
);
5359 einfo (ngettext ("%X%P: region `%s' overflowed by %lu byte\n",
5360 "%X%P: region `%s' overflowed by %lu bytes\n",
5362 m
->name_list
.name
, over
);
5366 /* Make sure the new address is within the region. We explicitly permit the
5367 current address to be at the exact end of the region when the address is
5368 non-zero, in case the region is at the end of addressable memory and the
5369 calculation wraps around. */
5372 os_region_check (lang_output_section_statement_type
*os
,
5373 lang_memory_region_type
*region
,
5377 if ((region
->current
< region
->origin
5378 || (region
->current
- region
->origin
> region
->length
))
5379 && ((region
->current
!= region
->origin
+ region
->length
)
5384 einfo (_("%X%P: address 0x%v of %pB section `%s'"
5385 " is not within region `%s'\n"),
5387 os
->bfd_section
->owner
,
5388 os
->bfd_section
->name
,
5389 region
->name_list
.name
);
5391 else if (!region
->had_full_message
)
5393 region
->had_full_message
= TRUE
;
5395 einfo (_("%X%P: %pB section `%s' will not fit in region `%s'\n"),
5396 os
->bfd_section
->owner
,
5397 os
->bfd_section
->name
,
5398 region
->name_list
.name
);
5404 ldlang_check_relro_region (lang_statement_union_type
*s
,
5405 seg_align_type
*seg
)
5407 if (seg
->relro
== exp_seg_relro_start
)
5409 if (!seg
->relro_start_stat
)
5410 seg
->relro_start_stat
= s
;
5413 ASSERT (seg
->relro_start_stat
== s
);
5416 else if (seg
->relro
== exp_seg_relro_end
)
5418 if (!seg
->relro_end_stat
)
5419 seg
->relro_end_stat
= s
;
5422 ASSERT (seg
->relro_end_stat
== s
);
5427 /* Set the sizes for all the output sections. */
5430 lang_size_sections_1
5431 (lang_statement_union_type
**prev
,
5432 lang_output_section_statement_type
*output_section_statement
,
5436 bfd_boolean check_regions
)
5438 lang_statement_union_type
*s
;
5440 /* Size up the sections from their constituent parts. */
5441 for (s
= *prev
; s
!= NULL
; s
= s
->header
.next
)
5443 switch (s
->header
.type
)
5445 case lang_output_section_statement_enum
:
5447 bfd_vma newdot
, after
, dotdelta
;
5448 lang_output_section_statement_type
*os
;
5449 lang_memory_region_type
*r
;
5450 int section_alignment
= 0;
5452 os
= &s
->output_section_statement
;
5453 if (os
->constraint
== -1)
5456 /* FIXME: We shouldn't need to zero section vmas for ld -r
5457 here, in lang_insert_orphan, or in the default linker scripts.
5458 This is covering for coff backend linker bugs. See PR6945. */
5459 if (os
->addr_tree
== NULL
5460 && bfd_link_relocatable (&link_info
)
5461 && (bfd_get_flavour (link_info
.output_bfd
)
5462 == bfd_target_coff_flavour
))
5463 os
->addr_tree
= exp_intop (0);
5464 if (os
->addr_tree
!= NULL
)
5466 os
->processed_vma
= FALSE
;
5467 exp_fold_tree (os
->addr_tree
, bfd_abs_section_ptr
, &dot
);
5469 if (expld
.result
.valid_p
)
5471 dot
= expld
.result
.value
;
5472 if (expld
.result
.section
!= NULL
)
5473 dot
+= expld
.result
.section
->vma
;
5475 else if (expld
.phase
!= lang_mark_phase_enum
)
5476 einfo (_("%F%P:%pS: non constant or forward reference"
5477 " address expression for section %s\n"),
5478 os
->addr_tree
, os
->name
);
5481 if (os
->bfd_section
== NULL
)
5482 /* This section was removed or never actually created. */
5485 /* If this is a COFF shared library section, use the size and
5486 address from the input section. FIXME: This is COFF
5487 specific; it would be cleaner if there were some other way
5488 to do this, but nothing simple comes to mind. */
5489 if (((bfd_get_flavour (link_info
.output_bfd
)
5490 == bfd_target_ecoff_flavour
)
5491 || (bfd_get_flavour (link_info
.output_bfd
)
5492 == bfd_target_coff_flavour
))
5493 && (os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
5497 if (os
->children
.head
== NULL
5498 || os
->children
.head
->header
.next
!= NULL
5499 || (os
->children
.head
->header
.type
5500 != lang_input_section_enum
))
5501 einfo (_("%X%P: internal error on COFF shared library"
5502 " section %s\n"), os
->name
);
5504 input
= os
->children
.head
->input_section
.section
;
5505 bfd_set_section_vma (os
->bfd_section
,
5506 bfd_section_vma (input
));
5507 if (!(os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
5508 os
->bfd_section
->size
= input
->size
;
5514 if (bfd_is_abs_section (os
->bfd_section
))
5516 /* No matter what happens, an abs section starts at zero. */
5517 ASSERT (os
->bfd_section
->vma
== 0);
5521 if (os
->addr_tree
== NULL
)
5523 /* No address specified for this section, get one
5524 from the region specification. */
5525 if (os
->region
== NULL
5526 || ((os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))
5527 && os
->region
->name_list
.name
[0] == '*'
5528 && strcmp (os
->region
->name_list
.name
,
5529 DEFAULT_MEMORY_REGION
) == 0))
5531 os
->region
= lang_memory_default (os
->bfd_section
);
5534 /* If a loadable section is using the default memory
5535 region, and some non default memory regions were
5536 defined, issue an error message. */
5538 && !IGNORE_SECTION (os
->bfd_section
)
5539 && !bfd_link_relocatable (&link_info
)
5541 && strcmp (os
->region
->name_list
.name
,
5542 DEFAULT_MEMORY_REGION
) == 0
5543 && lang_memory_region_list
!= NULL
5544 && (strcmp (lang_memory_region_list
->name_list
.name
,
5545 DEFAULT_MEMORY_REGION
) != 0
5546 || lang_memory_region_list
->next
!= NULL
)
5547 && expld
.phase
!= lang_mark_phase_enum
)
5549 /* By default this is an error rather than just a
5550 warning because if we allocate the section to the
5551 default memory region we can end up creating an
5552 excessively large binary, or even seg faulting when
5553 attempting to perform a negative seek. See
5554 sources.redhat.com/ml/binutils/2003-04/msg00423.html
5555 for an example of this. This behaviour can be
5556 overridden by the using the --no-check-sections
5558 if (command_line
.check_section_addresses
)
5559 einfo (_("%F%P: error: no memory region specified"
5560 " for loadable section `%s'\n"),
5561 bfd_section_name (os
->bfd_section
));
5563 einfo (_("%P: warning: no memory region specified"
5564 " for loadable section `%s'\n"),
5565 bfd_section_name (os
->bfd_section
));
5568 newdot
= os
->region
->current
;
5569 section_alignment
= os
->bfd_section
->alignment_power
;
5572 section_alignment
= exp_get_power (os
->section_alignment
,
5573 "section alignment");
5575 /* Align to what the section needs. */
5576 if (section_alignment
> 0)
5578 bfd_vma savedot
= newdot
;
5579 newdot
= align_power (newdot
, section_alignment
);
5581 dotdelta
= newdot
- savedot
;
5583 && (config
.warn_section_align
5584 || os
->addr_tree
!= NULL
)
5585 && expld
.phase
!= lang_mark_phase_enum
)
5586 einfo (ngettext ("%P: warning: changing start of "
5587 "section %s by %lu byte\n",
5588 "%P: warning: changing start of "
5589 "section %s by %lu bytes\n",
5590 (unsigned long) dotdelta
),
5591 os
->name
, (unsigned long) dotdelta
);
5594 bfd_set_section_vma (os
->bfd_section
, newdot
);
5596 os
->bfd_section
->output_offset
= 0;
5599 lang_size_sections_1 (&os
->children
.head
, os
,
5600 os
->fill
, newdot
, relax
, check_regions
);
5602 os
->processed_vma
= TRUE
;
5604 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
5605 /* Except for some special linker created sections,
5606 no output section should change from zero size
5607 after strip_excluded_output_sections. A non-zero
5608 size on an ignored section indicates that some
5609 input section was not sized early enough. */
5610 ASSERT (os
->bfd_section
->size
== 0);
5613 dot
= os
->bfd_section
->vma
;
5615 /* Put the section within the requested block size, or
5616 align at the block boundary. */
5618 + TO_ADDR (os
->bfd_section
->size
)
5619 + os
->block_value
- 1)
5620 & - (bfd_vma
) os
->block_value
);
5622 if (!(os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
5623 os
->bfd_section
->size
= TO_SIZE (after
5624 - os
->bfd_section
->vma
);
5627 /* Set section lma. */
5630 r
= lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, FALSE
);
5634 bfd_vma lma
= exp_get_abs_int (os
->load_base
, 0, "load base");
5635 os
->bfd_section
->lma
= lma
;
5637 else if (os
->lma_region
!= NULL
)
5639 bfd_vma lma
= os
->lma_region
->current
;
5641 if (os
->align_lma_with_input
)
5645 /* When LMA_REGION is the same as REGION, align the LMA
5646 as we did for the VMA, possibly including alignment
5647 from the bfd section. If a different region, then
5648 only align according to the value in the output
5650 if (os
->lma_region
!= os
->region
)
5651 section_alignment
= exp_get_power (os
->section_alignment
,
5652 "section alignment");
5653 if (section_alignment
> 0)
5654 lma
= align_power (lma
, section_alignment
);
5656 os
->bfd_section
->lma
= lma
;
5658 else if (r
->last_os
!= NULL
5659 && (os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
5664 last
= r
->last_os
->output_section_statement
.bfd_section
;
5666 /* A backwards move of dot should be accompanied by
5667 an explicit assignment to the section LMA (ie.
5668 os->load_base set) because backwards moves can
5669 create overlapping LMAs. */
5671 && os
->bfd_section
->size
!= 0
5672 && dot
+ TO_ADDR (os
->bfd_section
->size
) <= last
->vma
)
5674 /* If dot moved backwards then leave lma equal to
5675 vma. This is the old default lma, which might
5676 just happen to work when the backwards move is
5677 sufficiently large. Nag if this changes anything,
5678 so people can fix their linker scripts. */
5680 if (last
->vma
!= last
->lma
)
5681 einfo (_("%P: warning: dot moved backwards "
5682 "before `%s'\n"), os
->name
);
5686 /* If this is an overlay, set the current lma to that
5687 at the end of the previous section. */
5688 if (os
->sectype
== overlay_section
)
5689 lma
= last
->lma
+ TO_ADDR (last
->size
);
5691 /* Otherwise, keep the same lma to vma relationship
5692 as the previous section. */
5694 lma
= os
->bfd_section
->vma
+ last
->lma
- last
->vma
;
5696 if (section_alignment
> 0)
5697 lma
= align_power (lma
, section_alignment
);
5698 os
->bfd_section
->lma
= lma
;
5701 os
->processed_lma
= TRUE
;
5703 /* Keep track of normal sections using the default
5704 lma region. We use this to set the lma for
5705 following sections. Overlays or other linker
5706 script assignment to lma might mean that the
5707 default lma == vma is incorrect.
5708 To avoid warnings about dot moving backwards when using
5709 -Ttext, don't start tracking sections until we find one
5710 of non-zero size or with lma set differently to vma.
5711 Do this tracking before we short-cut the loop so that we
5712 track changes for the case where the section size is zero,
5713 but the lma is set differently to the vma. This is
5714 important, if an orphan section is placed after an
5715 otherwise empty output section that has an explicit lma
5716 set, we want that lma reflected in the orphans lma. */
5717 if (((!IGNORE_SECTION (os
->bfd_section
)
5718 && (os
->bfd_section
->size
!= 0
5719 || (r
->last_os
== NULL
5720 && os
->bfd_section
->vma
!= os
->bfd_section
->lma
)
5721 || (r
->last_os
!= NULL
5722 && dot
>= (r
->last_os
->output_section_statement
5723 .bfd_section
->vma
))))
5724 || os
->sectype
== first_overlay_section
)
5725 && os
->lma_region
== NULL
5726 && !bfd_link_relocatable (&link_info
))
5729 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
5732 /* .tbss sections effectively have zero size. */
5733 if (!IS_TBSS (os
->bfd_section
)
5734 || bfd_link_relocatable (&link_info
))
5735 dotdelta
= TO_ADDR (os
->bfd_section
->size
);
5740 if (os
->update_dot_tree
!= 0)
5741 exp_fold_tree (os
->update_dot_tree
, bfd_abs_section_ptr
, &dot
);
5743 /* Update dot in the region ?
5744 We only do this if the section is going to be allocated,
5745 since unallocated sections do not contribute to the region's
5746 overall size in memory. */
5747 if (os
->region
!= NULL
5748 && (os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
)))
5750 os
->region
->current
= dot
;
5753 /* Make sure the new address is within the region. */
5754 os_region_check (os
, os
->region
, os
->addr_tree
,
5755 os
->bfd_section
->vma
);
5757 if (os
->lma_region
!= NULL
&& os
->lma_region
!= os
->region
5758 && ((os
->bfd_section
->flags
& SEC_LOAD
)
5759 || os
->align_lma_with_input
))
5761 os
->lma_region
->current
= os
->bfd_section
->lma
+ dotdelta
;
5764 os_region_check (os
, os
->lma_region
, NULL
,
5765 os
->bfd_section
->lma
);
5771 case lang_constructors_statement_enum
:
5772 dot
= lang_size_sections_1 (&constructor_list
.head
,
5773 output_section_statement
,
5774 fill
, dot
, relax
, check_regions
);
5777 case lang_data_statement_enum
:
5779 unsigned int size
= 0;
5781 s
->data_statement
.output_offset
=
5782 dot
- output_section_statement
->bfd_section
->vma
;
5783 s
->data_statement
.output_section
=
5784 output_section_statement
->bfd_section
;
5786 /* We might refer to provided symbols in the expression, and
5787 need to mark them as needed. */
5788 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
5790 switch (s
->data_statement
.type
)
5808 if (size
< TO_SIZE ((unsigned) 1))
5809 size
= TO_SIZE ((unsigned) 1);
5810 dot
+= TO_ADDR (size
);
5811 if (!(output_section_statement
->bfd_section
->flags
5813 output_section_statement
->bfd_section
->size
5814 = TO_SIZE (dot
- output_section_statement
->bfd_section
->vma
);
5819 case lang_reloc_statement_enum
:
5823 s
->reloc_statement
.output_offset
=
5824 dot
- output_section_statement
->bfd_section
->vma
;
5825 s
->reloc_statement
.output_section
=
5826 output_section_statement
->bfd_section
;
5827 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
5828 dot
+= TO_ADDR (size
);
5829 if (!(output_section_statement
->bfd_section
->flags
5831 output_section_statement
->bfd_section
->size
5832 = TO_SIZE (dot
- output_section_statement
->bfd_section
->vma
);
5836 case lang_wild_statement_enum
:
5837 dot
= lang_size_sections_1 (&s
->wild_statement
.children
.head
,
5838 output_section_statement
,
5839 fill
, dot
, relax
, check_regions
);
5842 case lang_object_symbols_statement_enum
:
5843 link_info
.create_object_symbols_section
5844 = output_section_statement
->bfd_section
;
5845 output_section_statement
->bfd_section
->flags
|= SEC_KEEP
;
5848 case lang_output_statement_enum
:
5849 case lang_target_statement_enum
:
5852 case lang_input_section_enum
:
5856 i
= s
->input_section
.section
;
5861 if (!bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
5862 einfo (_("%F%P: can't relax section: %E\n"));
5866 dot
= size_input_section (prev
, output_section_statement
,
5871 case lang_input_statement_enum
:
5874 case lang_fill_statement_enum
:
5875 s
->fill_statement
.output_section
=
5876 output_section_statement
->bfd_section
;
5878 fill
= s
->fill_statement
.fill
;
5881 case lang_assignment_statement_enum
:
5883 bfd_vma newdot
= dot
;
5884 etree_type
*tree
= s
->assignment_statement
.exp
;
5886 expld
.dataseg
.relro
= exp_seg_relro_none
;
5888 exp_fold_tree (tree
,
5889 output_section_statement
->bfd_section
,
5892 ldlang_check_relro_region (s
, &expld
.dataseg
);
5894 expld
.dataseg
.relro
= exp_seg_relro_none
;
5896 /* This symbol may be relative to this section. */
5897 if ((tree
->type
.node_class
== etree_provided
5898 || tree
->type
.node_class
== etree_assign
)
5899 && (tree
->assign
.dst
[0] != '.'
5900 || tree
->assign
.dst
[1] != '\0'))
5901 output_section_statement
->update_dot
= 1;
5903 if (!output_section_statement
->ignored
)
5905 if (output_section_statement
== abs_output_section
)
5907 /* If we don't have an output section, then just adjust
5908 the default memory address. */
5909 lang_memory_region_lookup (DEFAULT_MEMORY_REGION
,
5910 FALSE
)->current
= newdot
;
5912 else if (newdot
!= dot
)
5914 /* Insert a pad after this statement. We can't
5915 put the pad before when relaxing, in case the
5916 assignment references dot. */
5917 insert_pad (&s
->header
.next
, fill
, TO_SIZE (newdot
- dot
),
5918 output_section_statement
->bfd_section
, dot
);
5920 /* Don't neuter the pad below when relaxing. */
5923 /* If dot is advanced, this implies that the section
5924 should have space allocated to it, unless the
5925 user has explicitly stated that the section
5926 should not be allocated. */
5927 if (output_section_statement
->sectype
!= noalloc_section
5928 && (output_section_statement
->sectype
!= noload_section
5929 || (bfd_get_flavour (link_info
.output_bfd
)
5930 == bfd_target_elf_flavour
)))
5931 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
;
5938 case lang_padding_statement_enum
:
5939 /* If this is the first time lang_size_sections is called,
5940 we won't have any padding statements. If this is the
5941 second or later passes when relaxing, we should allow
5942 padding to shrink. If padding is needed on this pass, it
5943 will be added back in. */
5944 s
->padding_statement
.size
= 0;
5946 /* Make sure output_offset is valid. If relaxation shrinks
5947 the section and this pad isn't needed, it's possible to
5948 have output_offset larger than the final size of the
5949 section. bfd_set_section_contents will complain even for
5950 a pad size of zero. */
5951 s
->padding_statement
.output_offset
5952 = dot
- output_section_statement
->bfd_section
->vma
;
5955 case lang_group_statement_enum
:
5956 dot
= lang_size_sections_1 (&s
->group_statement
.children
.head
,
5957 output_section_statement
,
5958 fill
, dot
, relax
, check_regions
);
5961 case lang_insert_statement_enum
:
5964 /* We can only get here when relaxing is turned on. */
5965 case lang_address_statement_enum
:
5972 prev
= &s
->header
.next
;
5977 /* Callback routine that is used in _bfd_elf_map_sections_to_segments.
5978 The BFD library has set NEW_SEGMENT to TRUE iff it thinks that
5979 CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different
5980 segments. We are allowed an opportunity to override this decision. */
5983 ldlang_override_segment_assignment (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
5984 bfd
*abfd ATTRIBUTE_UNUSED
,
5985 asection
*current_section
,
5986 asection
*previous_section
,
5987 bfd_boolean new_segment
)
5989 lang_output_section_statement_type
*cur
;
5990 lang_output_section_statement_type
*prev
;
5992 /* The checks below are only necessary when the BFD library has decided
5993 that the two sections ought to be placed into the same segment. */
5997 /* Paranoia checks. */
5998 if (current_section
== NULL
|| previous_section
== NULL
)
6001 /* If this flag is set, the target never wants code and non-code
6002 sections comingled in the same segment. */
6003 if (config
.separate_code
6004 && ((current_section
->flags
^ previous_section
->flags
) & SEC_CODE
))
6007 /* Find the memory regions associated with the two sections.
6008 We call lang_output_section_find() here rather than scanning the list
6009 of output sections looking for a matching section pointer because if
6010 we have a large number of sections then a hash lookup is faster. */
6011 cur
= lang_output_section_find (current_section
->name
);
6012 prev
= lang_output_section_find (previous_section
->name
);
6014 /* More paranoia. */
6015 if (cur
== NULL
|| prev
== NULL
)
6018 /* If the regions are different then force the sections to live in
6019 different segments. See the email thread starting at the following
6020 URL for the reasons why this is necessary:
6021 http://sourceware.org/ml/binutils/2007-02/msg00216.html */
6022 return cur
->region
!= prev
->region
;
6026 one_lang_size_sections_pass (bfd_boolean
*relax
, bfd_boolean check_regions
)
6028 lang_statement_iteration
++;
6029 lang_size_sections_1 (&statement_list
.head
, abs_output_section
,
6030 0, 0, relax
, check_regions
);
6034 lang_size_segment (seg_align_type
*seg
)
6036 /* If XXX_SEGMENT_ALIGN XXX_SEGMENT_END pair was seen, check whether
6037 a page could be saved in the data segment. */
6038 bfd_vma first
, last
;
6040 first
= -seg
->base
& (seg
->pagesize
- 1);
6041 last
= seg
->end
& (seg
->pagesize
- 1);
6043 && ((seg
->base
& ~(seg
->pagesize
- 1))
6044 != (seg
->end
& ~(seg
->pagesize
- 1)))
6045 && first
+ last
<= seg
->pagesize
)
6047 seg
->phase
= exp_seg_adjust
;
6051 seg
->phase
= exp_seg_done
;
6056 lang_size_relro_segment_1 (seg_align_type
*seg
)
6058 bfd_vma relro_end
, desired_end
;
6061 /* Compute the expected PT_GNU_RELRO/PT_LOAD segment end. */
6062 relro_end
= ((seg
->relro_end
+ seg
->pagesize
- 1)
6063 & ~(seg
->pagesize
- 1));
6065 /* Adjust by the offset arg of XXX_SEGMENT_RELRO_END. */
6066 desired_end
= relro_end
- seg
->relro_offset
;
6068 /* For sections in the relro segment.. */
6069 for (sec
= link_info
.output_bfd
->section_last
; sec
; sec
= sec
->prev
)
6070 if ((sec
->flags
& SEC_ALLOC
) != 0
6071 && sec
->vma
>= seg
->base
6072 && sec
->vma
< seg
->relro_end
- seg
->relro_offset
)
6074 /* Where do we want to put this section so that it ends as
6076 bfd_vma start
, end
, bump
;
6078 end
= start
= sec
->vma
;
6080 end
+= TO_ADDR (sec
->size
);
6081 bump
= desired_end
- end
;
6082 /* We'd like to increase START by BUMP, but we must heed
6083 alignment so the increase might be less than optimum. */
6085 start
&= ~(((bfd_vma
) 1 << sec
->alignment_power
) - 1);
6086 /* This is now the desired end for the previous section. */
6087 desired_end
= start
;
6090 seg
->phase
= exp_seg_relro_adjust
;
6091 ASSERT (desired_end
>= seg
->base
);
6092 seg
->base
= desired_end
;
6097 lang_size_relro_segment (bfd_boolean
*relax
, bfd_boolean check_regions
)
6099 bfd_boolean do_reset
= FALSE
;
6100 bfd_boolean do_data_relro
;
6101 bfd_vma data_initial_base
, data_relro_end
;
6103 if (link_info
.relro
&& expld
.dataseg
.relro_end
)
6105 do_data_relro
= TRUE
;
6106 data_initial_base
= expld
.dataseg
.base
;
6107 data_relro_end
= lang_size_relro_segment_1 (&expld
.dataseg
);
6111 do_data_relro
= FALSE
;
6112 data_initial_base
= data_relro_end
= 0;
6117 lang_reset_memory_regions ();
6118 one_lang_size_sections_pass (relax
, check_regions
);
6120 /* Assignments to dot, or to output section address in a user
6121 script have increased padding over the original. Revert. */
6122 if (do_data_relro
&& expld
.dataseg
.relro_end
> data_relro_end
)
6124 expld
.dataseg
.base
= data_initial_base
;;
6129 if (!do_data_relro
&& lang_size_segment (&expld
.dataseg
))
6136 lang_size_sections (bfd_boolean
*relax
, bfd_boolean check_regions
)
6138 expld
.phase
= lang_allocating_phase_enum
;
6139 expld
.dataseg
.phase
= exp_seg_none
;
6141 one_lang_size_sections_pass (relax
, check_regions
);
6143 if (expld
.dataseg
.phase
!= exp_seg_end_seen
)
6144 expld
.dataseg
.phase
= exp_seg_done
;
6146 if (expld
.dataseg
.phase
== exp_seg_end_seen
)
6148 bfd_boolean do_reset
6149 = lang_size_relro_segment (relax
, check_regions
);
6153 lang_reset_memory_regions ();
6154 one_lang_size_sections_pass (relax
, check_regions
);
6157 if (link_info
.relro
&& expld
.dataseg
.relro_end
)
6159 link_info
.relro_start
= expld
.dataseg
.base
;
6160 link_info
.relro_end
= expld
.dataseg
.relro_end
;
6165 static lang_output_section_statement_type
*current_section
;
6166 static lang_assignment_statement_type
*current_assign
;
6167 static bfd_boolean prefer_next_section
;
6169 /* Worker function for lang_do_assignments. Recursiveness goes here. */
6172 lang_do_assignments_1 (lang_statement_union_type
*s
,
6173 lang_output_section_statement_type
*current_os
,
6176 bfd_boolean
*found_end
)
6178 for (; s
!= NULL
; s
= s
->header
.next
)
6180 switch (s
->header
.type
)
6182 case lang_constructors_statement_enum
:
6183 dot
= lang_do_assignments_1 (constructor_list
.head
,
6184 current_os
, fill
, dot
, found_end
);
6187 case lang_output_section_statement_enum
:
6189 lang_output_section_statement_type
*os
;
6192 os
= &(s
->output_section_statement
);
6193 os
->after_end
= *found_end
;
6194 if (os
->bfd_section
!= NULL
&& !os
->ignored
)
6196 if ((os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
6198 current_section
= os
;
6199 prefer_next_section
= FALSE
;
6201 dot
= os
->bfd_section
->vma
;
6203 newdot
= lang_do_assignments_1 (os
->children
.head
,
6204 os
, os
->fill
, dot
, found_end
);
6207 if (os
->bfd_section
!= NULL
)
6209 /* .tbss sections effectively have zero size. */
6210 if (!IS_TBSS (os
->bfd_section
)
6211 || bfd_link_relocatable (&link_info
))
6212 dot
+= TO_ADDR (os
->bfd_section
->size
);
6214 if (os
->update_dot_tree
!= NULL
)
6215 exp_fold_tree (os
->update_dot_tree
,
6216 bfd_abs_section_ptr
, &dot
);
6224 case lang_wild_statement_enum
:
6226 dot
= lang_do_assignments_1 (s
->wild_statement
.children
.head
,
6227 current_os
, fill
, dot
, found_end
);
6230 case lang_object_symbols_statement_enum
:
6231 case lang_output_statement_enum
:
6232 case lang_target_statement_enum
:
6235 case lang_data_statement_enum
:
6236 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
6237 if (expld
.result
.valid_p
)
6239 s
->data_statement
.value
= expld
.result
.value
;
6240 if (expld
.result
.section
!= NULL
)
6241 s
->data_statement
.value
+= expld
.result
.section
->vma
;
6243 else if (expld
.phase
== lang_final_phase_enum
)
6244 einfo (_("%F%P: invalid data statement\n"));
6247 switch (s
->data_statement
.type
)
6265 if (size
< TO_SIZE ((unsigned) 1))
6266 size
= TO_SIZE ((unsigned) 1);
6267 dot
+= TO_ADDR (size
);
6271 case lang_reloc_statement_enum
:
6272 exp_fold_tree (s
->reloc_statement
.addend_exp
,
6273 bfd_abs_section_ptr
, &dot
);
6274 if (expld
.result
.valid_p
)
6275 s
->reloc_statement
.addend_value
= expld
.result
.value
;
6276 else if (expld
.phase
== lang_final_phase_enum
)
6277 einfo (_("%F%P: invalid reloc statement\n"));
6278 dot
+= TO_ADDR (bfd_get_reloc_size (s
->reloc_statement
.howto
));
6281 case lang_input_section_enum
:
6283 asection
*in
= s
->input_section
.section
;
6285 if ((in
->flags
& SEC_EXCLUDE
) == 0)
6286 dot
+= TO_ADDR (in
->size
);
6290 case lang_input_statement_enum
:
6293 case lang_fill_statement_enum
:
6294 fill
= s
->fill_statement
.fill
;
6297 case lang_assignment_statement_enum
:
6298 current_assign
= &s
->assignment_statement
;
6299 if (current_assign
->exp
->type
.node_class
!= etree_assert
)
6301 const char *p
= current_assign
->exp
->assign
.dst
;
6303 if (current_os
== abs_output_section
&& p
[0] == '.' && p
[1] == 0)
6304 prefer_next_section
= TRUE
;
6308 if (strcmp (p
, "end") == 0)
6311 exp_fold_tree (s
->assignment_statement
.exp
,
6312 (current_os
->bfd_section
!= NULL
6313 ? current_os
->bfd_section
: bfd_und_section_ptr
),
6317 case lang_padding_statement_enum
:
6318 dot
+= TO_ADDR (s
->padding_statement
.size
);
6321 case lang_group_statement_enum
:
6322 dot
= lang_do_assignments_1 (s
->group_statement
.children
.head
,
6323 current_os
, fill
, dot
, found_end
);
6326 case lang_insert_statement_enum
:
6329 case lang_address_statement_enum
:
6341 lang_do_assignments (lang_phase_type phase
)
6343 bfd_boolean found_end
= FALSE
;
6345 current_section
= NULL
;
6346 prefer_next_section
= FALSE
;
6347 expld
.phase
= phase
;
6348 lang_statement_iteration
++;
6349 lang_do_assignments_1 (statement_list
.head
,
6350 abs_output_section
, NULL
, 0, &found_end
);
6353 /* For an assignment statement outside of an output section statement,
6354 choose the best of neighbouring output sections to use for values
6358 section_for_dot (void)
6362 /* Assignments belong to the previous output section, unless there
6363 has been an assignment to "dot", in which case following
6364 assignments belong to the next output section. (The assumption
6365 is that an assignment to "dot" is setting up the address for the
6366 next output section.) Except that past the assignment to "_end"
6367 we always associate with the previous section. This exception is
6368 for targets like SH that define an alloc .stack or other
6369 weirdness after non-alloc sections. */
6370 if (current_section
== NULL
|| prefer_next_section
)
6372 lang_statement_union_type
*stmt
;
6373 lang_output_section_statement_type
*os
;
6375 for (stmt
= (lang_statement_union_type
*) current_assign
;
6377 stmt
= stmt
->header
.next
)
6378 if (stmt
->header
.type
== lang_output_section_statement_enum
)
6381 os
= &stmt
->output_section_statement
;
6384 && (os
->bfd_section
== NULL
6385 || (os
->bfd_section
->flags
& SEC_EXCLUDE
) != 0
6386 || bfd_section_removed_from_list (link_info
.output_bfd
,
6390 if (current_section
== NULL
|| os
== NULL
|| !os
->after_end
)
6393 s
= os
->bfd_section
;
6395 s
= link_info
.output_bfd
->section_last
;
6397 && ((s
->flags
& SEC_ALLOC
) == 0
6398 || (s
->flags
& SEC_THREAD_LOCAL
) != 0))
6403 return bfd_abs_section_ptr
;
6407 s
= current_section
->bfd_section
;
6409 /* The section may have been stripped. */
6411 && ((s
->flags
& SEC_EXCLUDE
) != 0
6412 || (s
->flags
& SEC_ALLOC
) == 0
6413 || (s
->flags
& SEC_THREAD_LOCAL
) != 0
6414 || bfd_section_removed_from_list (link_info
.output_bfd
, s
)))
6417 s
= link_info
.output_bfd
->sections
;
6419 && ((s
->flags
& SEC_ALLOC
) == 0
6420 || (s
->flags
& SEC_THREAD_LOCAL
) != 0))
6425 return bfd_abs_section_ptr
;
6428 /* Array of __start/__stop/.startof./.sizeof/ symbols. */
6430 static struct bfd_link_hash_entry
**start_stop_syms
;
6431 static size_t start_stop_count
= 0;
6432 static size_t start_stop_alloc
= 0;
6434 /* Give start/stop SYMBOL for SEC a preliminary definition, and add it
6435 to start_stop_syms. */
6438 lang_define_start_stop (const char *symbol
, asection
*sec
)
6440 struct bfd_link_hash_entry
*h
;
6442 h
= bfd_define_start_stop (link_info
.output_bfd
, &link_info
, symbol
, sec
);
6445 if (start_stop_count
== start_stop_alloc
)
6447 start_stop_alloc
= 2 * start_stop_alloc
+ 10;
6449 = xrealloc (start_stop_syms
,
6450 start_stop_alloc
* sizeof (*start_stop_syms
));
6452 start_stop_syms
[start_stop_count
++] = h
;
6456 /* Check for input sections whose names match references to
6457 __start_SECNAME or __stop_SECNAME symbols. Give the symbols
6458 preliminary definitions. */
6461 lang_init_start_stop (void)
6465 char leading_char
= bfd_get_symbol_leading_char (link_info
.output_bfd
);
6467 for (abfd
= link_info
.input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
6468 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
6471 const char *secname
= s
->name
;
6473 for (ps
= secname
; *ps
!= '\0'; ps
++)
6474 if (!ISALNUM ((unsigned char) *ps
) && *ps
!= '_')
6478 char *symbol
= (char *) xmalloc (10 + strlen (secname
));
6480 symbol
[0] = leading_char
;
6481 sprintf (symbol
+ (leading_char
!= 0), "__start_%s", secname
);
6482 lang_define_start_stop (symbol
, s
);
6484 symbol
[1] = leading_char
;
6485 memcpy (symbol
+ 1 + (leading_char
!= 0), "__stop", 6);
6486 lang_define_start_stop (symbol
+ 1, s
);
6493 /* Iterate over start_stop_syms. */
6496 foreach_start_stop (void (*func
) (struct bfd_link_hash_entry
*))
6500 for (i
= 0; i
< start_stop_count
; ++i
)
6501 func (start_stop_syms
[i
]);
6504 /* __start and __stop symbols are only supposed to be defined by the
6505 linker for orphan sections, but we now extend that to sections that
6506 map to an output section of the same name. The symbols were
6507 defined early for --gc-sections, before we mapped input to output
6508 sections, so undo those that don't satisfy this rule. */
6511 undef_start_stop (struct bfd_link_hash_entry
*h
)
6513 if (h
->ldscript_def
)
6516 if (h
->u
.def
.section
->output_section
== NULL
6517 || h
->u
.def
.section
->output_section
->owner
!= link_info
.output_bfd
6518 || strcmp (h
->u
.def
.section
->name
,
6519 h
->u
.def
.section
->output_section
->name
) != 0)
6521 asection
*sec
= bfd_get_section_by_name (link_info
.output_bfd
,
6522 h
->u
.def
.section
->name
);
6525 /* When there are more than one input sections with the same
6526 section name, SECNAME, linker picks the first one to define
6527 __start_SECNAME and __stop_SECNAME symbols. When the first
6528 input section is removed by comdat group, we need to check
6529 if there is still an output section with section name
6532 for (i
= sec
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
6533 if (strcmp (h
->u
.def
.section
->name
, i
->name
) == 0)
6535 h
->u
.def
.section
= i
;
6539 h
->type
= bfd_link_hash_undefined
;
6540 h
->u
.undef
.abfd
= NULL
;
6545 lang_undef_start_stop (void)
6547 foreach_start_stop (undef_start_stop
);
6550 /* Check for output sections whose names match references to
6551 .startof.SECNAME or .sizeof.SECNAME symbols. Give the symbols
6552 preliminary definitions. */
6555 lang_init_startof_sizeof (void)
6559 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6561 const char *secname
= s
->name
;
6562 char *symbol
= (char *) xmalloc (10 + strlen (secname
));
6564 sprintf (symbol
, ".startof.%s", secname
);
6565 lang_define_start_stop (symbol
, s
);
6567 memcpy (symbol
+ 1, ".size", 5);
6568 lang_define_start_stop (symbol
+ 1, s
);
6573 /* Set .startof., .sizeof., __start and __stop symbols final values. */
6576 set_start_stop (struct bfd_link_hash_entry
*h
)
6579 || h
->type
!= bfd_link_hash_defined
)
6582 if (h
->root
.string
[0] == '.')
6584 /* .startof. or .sizeof. symbol.
6585 .startof. already has final value. */
6586 if (h
->root
.string
[2] == 'i')
6589 h
->u
.def
.value
= TO_ADDR (h
->u
.def
.section
->size
);
6590 h
->u
.def
.section
= bfd_abs_section_ptr
;
6595 /* __start or __stop symbol. */
6596 int has_lead
= bfd_get_symbol_leading_char (link_info
.output_bfd
) != 0;
6598 h
->u
.def
.section
= h
->u
.def
.section
->output_section
;
6599 if (h
->root
.string
[4 + has_lead
] == 'o')
6602 h
->u
.def
.value
= TO_ADDR (h
->u
.def
.section
->size
);
6608 lang_finalize_start_stop (void)
6610 foreach_start_stop (set_start_stop
);
6616 struct bfd_link_hash_entry
*h
;
6619 if ((bfd_link_relocatable (&link_info
) && !link_info
.gc_sections
)
6620 || bfd_link_dll (&link_info
))
6621 warn
= entry_from_cmdline
;
6625 /* Force the user to specify a root when generating a relocatable with
6626 --gc-sections, unless --gc-keep-exported was also given. */
6627 if (bfd_link_relocatable (&link_info
)
6628 && link_info
.gc_sections
6629 && !link_info
.gc_keep_exported
6630 && !(entry_from_cmdline
|| undef_from_cmdline
))
6631 einfo (_("%F%P: gc-sections requires either an entry or "
6632 "an undefined symbol\n"));
6634 if (entry_symbol
.name
== NULL
)
6636 /* No entry has been specified. Look for the default entry, but
6637 don't warn if we don't find it. */
6638 entry_symbol
.name
= entry_symbol_default
;
6642 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
.name
,
6643 FALSE
, FALSE
, TRUE
);
6645 && (h
->type
== bfd_link_hash_defined
6646 || h
->type
== bfd_link_hash_defweak
)
6647 && h
->u
.def
.section
->output_section
!= NULL
)
6651 val
= (h
->u
.def
.value
6652 + bfd_section_vma (h
->u
.def
.section
->output_section
)
6653 + h
->u
.def
.section
->output_offset
);
6654 if (!bfd_set_start_address (link_info
.output_bfd
, val
))
6655 einfo (_("%F%P: %s: can't set start address\n"), entry_symbol
.name
);
6662 /* We couldn't find the entry symbol. Try parsing it as a
6664 val
= bfd_scan_vma (entry_symbol
.name
, &send
, 0);
6667 if (!bfd_set_start_address (link_info
.output_bfd
, val
))
6668 einfo (_("%F%P: can't set start address\n"));
6674 /* Can't find the entry symbol, and it's not a number. Use
6675 the first address in the text section. */
6676 ts
= bfd_get_section_by_name (link_info
.output_bfd
, entry_section
);
6680 einfo (_("%P: warning: cannot find entry symbol %s;"
6681 " defaulting to %V\n"),
6683 bfd_section_vma (ts
));
6684 if (!bfd_set_start_address (link_info
.output_bfd
,
6685 bfd_section_vma (ts
)))
6686 einfo (_("%F%P: can't set start address\n"));
6691 einfo (_("%P: warning: cannot find entry symbol %s;"
6692 " not setting start address\n"),
6699 /* This is a small function used when we want to ignore errors from
6703 ignore_bfd_errors (const char *fmt ATTRIBUTE_UNUSED
,
6704 va_list ap ATTRIBUTE_UNUSED
)
6706 /* Don't do anything. */
6709 /* Check that the architecture of all the input files is compatible
6710 with the output file. Also call the backend to let it do any
6711 other checking that is needed. */
6716 lang_input_statement_type
*file
;
6718 const bfd_arch_info_type
*compatible
;
6720 for (file
= &file_chain
.head
->input_statement
;
6724 #ifdef ENABLE_PLUGINS
6725 /* Don't check format of files claimed by plugin. */
6726 if (file
->flags
.claimed
)
6728 #endif /* ENABLE_PLUGINS */
6729 input_bfd
= file
->the_bfd
;
6731 = bfd_arch_get_compatible (input_bfd
, link_info
.output_bfd
,
6732 command_line
.accept_unknown_input_arch
);
6734 /* In general it is not possible to perform a relocatable
6735 link between differing object formats when the input
6736 file has relocations, because the relocations in the
6737 input format may not have equivalent representations in
6738 the output format (and besides BFD does not translate
6739 relocs for other link purposes than a final link). */
6740 if ((bfd_link_relocatable (&link_info
)
6741 || link_info
.emitrelocations
)
6742 && (compatible
== NULL
6743 || (bfd_get_flavour (input_bfd
)
6744 != bfd_get_flavour (link_info
.output_bfd
)))
6745 && (bfd_get_file_flags (input_bfd
) & HAS_RELOC
) != 0)
6747 einfo (_("%F%P: relocatable linking with relocations from"
6748 " format %s (%pB) to format %s (%pB) is not supported\n"),
6749 bfd_get_target (input_bfd
), input_bfd
,
6750 bfd_get_target (link_info
.output_bfd
), link_info
.output_bfd
);
6751 /* einfo with %F exits. */
6754 if (compatible
== NULL
)
6756 if (command_line
.warn_mismatch
)
6757 einfo (_("%X%P: %s architecture of input file `%pB'"
6758 " is incompatible with %s output\n"),
6759 bfd_printable_name (input_bfd
), input_bfd
,
6760 bfd_printable_name (link_info
.output_bfd
));
6762 else if (bfd_count_sections (input_bfd
))
6764 /* If the input bfd has no contents, it shouldn't set the
6765 private data of the output bfd. */
6767 bfd_error_handler_type pfn
= NULL
;
6769 /* If we aren't supposed to warn about mismatched input
6770 files, temporarily set the BFD error handler to a
6771 function which will do nothing. We still want to call
6772 bfd_merge_private_bfd_data, since it may set up
6773 information which is needed in the output file. */
6774 if (!command_line
.warn_mismatch
)
6775 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
6776 if (!bfd_merge_private_bfd_data (input_bfd
, &link_info
))
6778 if (command_line
.warn_mismatch
)
6779 einfo (_("%X%P: failed to merge target specific data"
6780 " of file %pB\n"), input_bfd
);
6782 if (!command_line
.warn_mismatch
)
6783 bfd_set_error_handler (pfn
);
6788 /* Look through all the global common symbols and attach them to the
6789 correct section. The -sort-common command line switch may be used
6790 to roughly sort the entries by alignment. */
6795 if (link_info
.inhibit_common_definition
)
6797 if (bfd_link_relocatable (&link_info
)
6798 && !command_line
.force_common_definition
)
6801 if (!config
.sort_common
)
6802 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, NULL
);
6807 if (config
.sort_common
== sort_descending
)
6809 for (power
= 4; power
> 0; power
--)
6810 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
6813 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
6817 for (power
= 0; power
<= 4; power
++)
6818 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
6820 power
= (unsigned int) -1;
6821 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
6826 /* Place one common symbol in the correct section. */
6829 lang_one_common (struct bfd_link_hash_entry
*h
, void *info
)
6831 unsigned int power_of_two
;
6835 if (h
->type
!= bfd_link_hash_common
)
6839 power_of_two
= h
->u
.c
.p
->alignment_power
;
6841 if (config
.sort_common
== sort_descending
6842 && power_of_two
< *(unsigned int *) info
)
6844 else if (config
.sort_common
== sort_ascending
6845 && power_of_two
> *(unsigned int *) info
)
6848 section
= h
->u
.c
.p
->section
;
6849 if (!bfd_define_common_symbol (link_info
.output_bfd
, &link_info
, h
))
6850 einfo (_("%F%P: could not define common symbol `%pT': %E\n"),
6853 if (config
.map_file
!= NULL
)
6855 static bfd_boolean header_printed
;
6860 if (!header_printed
)
6862 minfo (_("\nAllocating common symbols\n"));
6863 minfo (_("Common symbol size file\n\n"));
6864 header_printed
= TRUE
;
6867 name
= bfd_demangle (link_info
.output_bfd
, h
->root
.string
,
6868 DMGL_ANSI
| DMGL_PARAMS
);
6871 minfo ("%s", h
->root
.string
);
6872 len
= strlen (h
->root
.string
);
6877 len
= strlen (name
);
6893 if (size
<= 0xffffffff)
6894 sprintf (buf
, "%lx", (unsigned long) size
);
6896 sprintf_vma (buf
, size
);
6906 minfo ("%pB\n", section
->owner
);
6912 /* Handle a single orphan section S, placing the orphan into an appropriate
6913 output section. The effects of the --orphan-handling command line
6914 option are handled here. */
6917 ldlang_place_orphan (asection
*s
)
6919 if (config
.orphan_handling
== orphan_handling_discard
)
6921 lang_output_section_statement_type
*os
;
6922 os
= lang_output_section_statement_lookup (DISCARD_SECTION_NAME
, 0,
6924 if (os
->addr_tree
== NULL
6925 && (bfd_link_relocatable (&link_info
)
6926 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0))
6927 os
->addr_tree
= exp_intop (0);
6928 lang_add_section (&os
->children
, s
, NULL
, os
);
6932 lang_output_section_statement_type
*os
;
6933 const char *name
= s
->name
;
6936 if (config
.orphan_handling
== orphan_handling_error
)
6937 einfo (_("%X%P: error: unplaced orphan section `%pA' from `%pB'\n"),
6940 if (config
.unique_orphan_sections
|| unique_section_p (s
, NULL
))
6941 constraint
= SPECIAL
;
6943 os
= ldemul_place_orphan (s
, name
, constraint
);
6946 os
= lang_output_section_statement_lookup (name
, constraint
, TRUE
);
6947 if (os
->addr_tree
== NULL
6948 && (bfd_link_relocatable (&link_info
)
6949 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0))
6950 os
->addr_tree
= exp_intop (0);
6951 lang_add_section (&os
->children
, s
, NULL
, os
);
6954 if (config
.orphan_handling
== orphan_handling_warn
)
6955 einfo (_("%P: warning: orphan section `%pA' from `%pB' being "
6956 "placed in section `%s'\n"),
6957 s
, s
->owner
, os
->name
);
6961 /* Run through the input files and ensure that every input section has
6962 somewhere to go. If one is found without a destination then create
6963 an input request and place it into the statement tree. */
6966 lang_place_orphans (void)
6968 LANG_FOR_EACH_INPUT_STATEMENT (file
)
6972 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6974 if (s
->output_section
== NULL
)
6976 /* This section of the file is not attached, root
6977 around for a sensible place for it to go. */
6979 if (file
->flags
.just_syms
)
6980 bfd_link_just_syms (file
->the_bfd
, s
, &link_info
);
6981 else if (lang_discard_section_p (s
))
6982 s
->output_section
= bfd_abs_section_ptr
;
6983 else if (strcmp (s
->name
, "COMMON") == 0)
6985 /* This is a lonely common section which must have
6986 come from an archive. We attach to the section
6987 with the wildcard. */
6988 if (!bfd_link_relocatable (&link_info
)
6989 || command_line
.force_common_definition
)
6991 if (default_common_section
== NULL
)
6992 default_common_section
6993 = lang_output_section_statement_lookup (".bss", 0,
6995 lang_add_section (&default_common_section
->children
, s
,
6996 NULL
, default_common_section
);
7000 ldlang_place_orphan (s
);
7007 lang_set_flags (lang_memory_region_type
*ptr
, const char *flags
, int invert
)
7009 flagword
*ptr_flags
;
7011 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
7017 /* PR 17900: An exclamation mark in the attributes reverses
7018 the sense of any of the attributes that follow. */
7021 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
7025 *ptr_flags
|= SEC_ALLOC
;
7029 *ptr_flags
|= SEC_READONLY
;
7033 *ptr_flags
|= SEC_DATA
;
7037 *ptr_flags
|= SEC_CODE
;
7042 *ptr_flags
|= SEC_LOAD
;
7046 einfo (_("%F%P: invalid character %c (%d) in flags\n"),
7054 /* Call a function on each real input file. This function will be
7055 called on an archive, but not on the elements. */
7058 lang_for_each_input_file (void (*func
) (lang_input_statement_type
*))
7060 lang_input_statement_type
*f
;
7062 for (f
= &input_file_chain
.head
->input_statement
;
7064 f
= f
->next_real_file
)
7069 /* Call a function on each real file. The function will be called on
7070 all the elements of an archive which are included in the link, but
7071 will not be called on the archive file itself. */
7074 lang_for_each_file (void (*func
) (lang_input_statement_type
*))
7076 LANG_FOR_EACH_INPUT_STATEMENT (f
)
7084 ldlang_add_file (lang_input_statement_type
*entry
)
7086 lang_statement_append (&file_chain
, entry
, &entry
->next
);
7088 /* The BFD linker needs to have a list of all input BFDs involved in
7090 ASSERT (entry
->the_bfd
->link
.next
== NULL
);
7091 ASSERT (entry
->the_bfd
!= link_info
.output_bfd
);
7093 *link_info
.input_bfds_tail
= entry
->the_bfd
;
7094 link_info
.input_bfds_tail
= &entry
->the_bfd
->link
.next
;
7095 bfd_set_usrdata (entry
->the_bfd
, entry
);
7096 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
7098 /* Look through the sections and check for any which should not be
7099 included in the link. We need to do this now, so that we can
7100 notice when the backend linker tries to report multiple
7101 definition errors for symbols which are in sections we aren't
7102 going to link. FIXME: It might be better to entirely ignore
7103 symbols which are defined in sections which are going to be
7104 discarded. This would require modifying the backend linker for
7105 each backend which might set the SEC_LINK_ONCE flag. If we do
7106 this, we should probably handle SEC_EXCLUDE in the same way. */
7108 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, entry
);
7112 lang_add_output (const char *name
, int from_script
)
7114 /* Make -o on command line override OUTPUT in script. */
7115 if (!had_output_filename
|| !from_script
)
7117 output_filename
= name
;
7118 had_output_filename
= TRUE
;
7122 lang_output_section_statement_type
*
7123 lang_enter_output_section_statement (const char *output_section_statement_name
,
7124 etree_type
*address_exp
,
7125 enum section_type sectype
,
7127 etree_type
*subalign
,
7130 int align_with_input
)
7132 lang_output_section_statement_type
*os
;
7134 os
= lang_output_section_statement_lookup (output_section_statement_name
,
7136 current_section
= os
;
7138 if (os
->addr_tree
== NULL
)
7140 os
->addr_tree
= address_exp
;
7142 os
->sectype
= sectype
;
7143 if (sectype
!= noload_section
)
7144 os
->flags
= SEC_NO_FLAGS
;
7146 os
->flags
= SEC_NEVER_LOAD
;
7147 os
->block_value
= 1;
7149 /* Make next things chain into subchain of this. */
7150 push_stat_ptr (&os
->children
);
7152 os
->align_lma_with_input
= align_with_input
== ALIGN_WITH_INPUT
;
7153 if (os
->align_lma_with_input
&& align
!= NULL
)
7154 einfo (_("%F%P:%pS: error: align with input and explicit align specified\n"),
7157 os
->subsection_alignment
= subalign
;
7158 os
->section_alignment
= align
;
7160 os
->load_base
= ebase
;
7167 lang_output_statement_type
*new_stmt
;
7169 new_stmt
= new_stat (lang_output_statement
, stat_ptr
);
7170 new_stmt
->name
= output_filename
;
7173 /* Reset the current counters in the regions. */
7176 lang_reset_memory_regions (void)
7178 lang_memory_region_type
*p
= lang_memory_region_list
;
7180 lang_output_section_statement_type
*os
;
7182 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
7184 p
->current
= p
->origin
;
7188 for (os
= &lang_os_list
.head
->output_section_statement
;
7192 os
->processed_vma
= FALSE
;
7193 os
->processed_lma
= FALSE
;
7196 for (o
= link_info
.output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
7198 /* Save the last size for possible use by bfd_relax_section. */
7199 o
->rawsize
= o
->size
;
7200 if (!(o
->flags
& SEC_FIXED_SIZE
))
7205 /* Worker for lang_gc_sections_1. */
7208 gc_section_callback (lang_wild_statement_type
*ptr
,
7209 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
7211 struct flag_info
*sflag_info ATTRIBUTE_UNUSED
,
7212 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
7213 void *data ATTRIBUTE_UNUSED
)
7215 /* If the wild pattern was marked KEEP, the member sections
7216 should be as well. */
7217 if (ptr
->keep_sections
)
7218 section
->flags
|= SEC_KEEP
;
7221 /* Iterate over sections marking them against GC. */
7224 lang_gc_sections_1 (lang_statement_union_type
*s
)
7226 for (; s
!= NULL
; s
= s
->header
.next
)
7228 switch (s
->header
.type
)
7230 case lang_wild_statement_enum
:
7231 walk_wild (&s
->wild_statement
, gc_section_callback
, NULL
);
7233 case lang_constructors_statement_enum
:
7234 lang_gc_sections_1 (constructor_list
.head
);
7236 case lang_output_section_statement_enum
:
7237 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
7239 case lang_group_statement_enum
:
7240 lang_gc_sections_1 (s
->group_statement
.children
.head
);
7249 lang_gc_sections (void)
7251 /* Keep all sections so marked in the link script. */
7252 lang_gc_sections_1 (statement_list
.head
);
7254 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
7255 the special case of debug info. (See bfd/stabs.c)
7256 Twiddle the flag here, to simplify later linker code. */
7257 if (bfd_link_relocatable (&link_info
))
7259 LANG_FOR_EACH_INPUT_STATEMENT (f
)
7262 #ifdef ENABLE_PLUGINS
7263 if (f
->flags
.claimed
)
7266 for (sec
= f
->the_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7267 if ((sec
->flags
& SEC_DEBUGGING
) == 0)
7268 sec
->flags
&= ~SEC_EXCLUDE
;
7272 if (link_info
.gc_sections
)
7273 bfd_gc_sections (link_info
.output_bfd
, &link_info
);
7276 /* Worker for lang_find_relro_sections_1. */
7279 find_relro_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
7280 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
7282 struct flag_info
*sflag_info ATTRIBUTE_UNUSED
,
7283 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
7286 /* Discarded, excluded and ignored sections effectively have zero
7288 if (section
->output_section
!= NULL
7289 && section
->output_section
->owner
== link_info
.output_bfd
7290 && (section
->output_section
->flags
& SEC_EXCLUDE
) == 0
7291 && !IGNORE_SECTION (section
)
7292 && section
->size
!= 0)
7294 bfd_boolean
*has_relro_section
= (bfd_boolean
*) data
;
7295 *has_relro_section
= TRUE
;
7299 /* Iterate over sections for relro sections. */
7302 lang_find_relro_sections_1 (lang_statement_union_type
*s
,
7303 seg_align_type
*seg
,
7304 bfd_boolean
*has_relro_section
)
7306 if (*has_relro_section
)
7309 for (; s
!= NULL
; s
= s
->header
.next
)
7311 if (s
== seg
->relro_end_stat
)
7314 switch (s
->header
.type
)
7316 case lang_wild_statement_enum
:
7317 walk_wild (&s
->wild_statement
,
7318 find_relro_section_callback
,
7321 case lang_constructors_statement_enum
:
7322 lang_find_relro_sections_1 (constructor_list
.head
,
7323 seg
, has_relro_section
);
7325 case lang_output_section_statement_enum
:
7326 lang_find_relro_sections_1 (s
->output_section_statement
.children
.head
,
7327 seg
, has_relro_section
);
7329 case lang_group_statement_enum
:
7330 lang_find_relro_sections_1 (s
->group_statement
.children
.head
,
7331 seg
, has_relro_section
);
7340 lang_find_relro_sections (void)
7342 bfd_boolean has_relro_section
= FALSE
;
7344 /* Check all sections in the link script. */
7346 lang_find_relro_sections_1 (expld
.dataseg
.relro_start_stat
,
7347 &expld
.dataseg
, &has_relro_section
);
7349 if (!has_relro_section
)
7350 link_info
.relro
= FALSE
;
7353 /* Relax all sections until bfd_relax_section gives up. */
7356 lang_relax_sections (bfd_boolean need_layout
)
7358 if (RELAXATION_ENABLED
)
7360 /* We may need more than one relaxation pass. */
7361 int i
= link_info
.relax_pass
;
7363 /* The backend can use it to determine the current pass. */
7364 link_info
.relax_pass
= 0;
7368 /* Keep relaxing until bfd_relax_section gives up. */
7369 bfd_boolean relax_again
;
7371 link_info
.relax_trip
= -1;
7374 link_info
.relax_trip
++;
7376 /* Note: pe-dll.c does something like this also. If you find
7377 you need to change this code, you probably need to change
7378 pe-dll.c also. DJ */
7380 /* Do all the assignments with our current guesses as to
7382 lang_do_assignments (lang_assigning_phase_enum
);
7384 /* We must do this after lang_do_assignments, because it uses
7386 lang_reset_memory_regions ();
7388 /* Perform another relax pass - this time we know where the
7389 globals are, so can make a better guess. */
7390 relax_again
= FALSE
;
7391 lang_size_sections (&relax_again
, FALSE
);
7393 while (relax_again
);
7395 link_info
.relax_pass
++;
7402 /* Final extra sizing to report errors. */
7403 lang_do_assignments (lang_assigning_phase_enum
);
7404 lang_reset_memory_regions ();
7405 lang_size_sections (NULL
, TRUE
);
7409 #ifdef ENABLE_PLUGINS
7410 /* Find the insert point for the plugin's replacement files. We
7411 place them after the first claimed real object file, or if the
7412 first claimed object is an archive member, after the last real
7413 object file immediately preceding the archive. In the event
7414 no objects have been claimed at all, we return the first dummy
7415 object file on the list as the insert point; that works, but
7416 the callee must be careful when relinking the file_chain as it
7417 is not actually on that chain, only the statement_list and the
7418 input_file list; in that case, the replacement files must be
7419 inserted at the head of the file_chain. */
7421 static lang_input_statement_type
*
7422 find_replacements_insert_point (bfd_boolean
*before
)
7424 lang_input_statement_type
*claim1
, *lastobject
;
7425 lastobject
= &input_file_chain
.head
->input_statement
;
7426 for (claim1
= &file_chain
.head
->input_statement
;
7428 claim1
= claim1
->next
)
7430 if (claim1
->flags
.claimed
)
7432 *before
= claim1
->flags
.claim_archive
;
7433 return claim1
->flags
.claim_archive
? lastobject
: claim1
;
7435 /* Update lastobject if this is a real object file. */
7436 if (claim1
->the_bfd
!= NULL
&& claim1
->the_bfd
->my_archive
== NULL
)
7437 lastobject
= claim1
;
7439 /* No files were claimed by the plugin. Choose the last object
7440 file found on the list (maybe the first, dummy entry) as the
7446 /* Find where to insert ADD, an archive element or shared library
7447 added during a rescan. */
7449 static lang_input_statement_type
**
7450 find_rescan_insertion (lang_input_statement_type
*add
)
7452 bfd
*add_bfd
= add
->the_bfd
;
7453 lang_input_statement_type
*f
;
7454 lang_input_statement_type
*last_loaded
= NULL
;
7455 lang_input_statement_type
*before
= NULL
;
7456 lang_input_statement_type
**iter
= NULL
;
7458 if (add_bfd
->my_archive
!= NULL
)
7459 add_bfd
= add_bfd
->my_archive
;
7461 /* First look through the input file chain, to find an object file
7462 before the one we've rescanned. Normal object files always
7463 appear on both the input file chain and the file chain, so this
7464 lets us get quickly to somewhere near the correct place on the
7465 file chain if it is full of archive elements. Archives don't
7466 appear on the file chain, but if an element has been extracted
7467 then their input_statement->next points at it. */
7468 for (f
= &input_file_chain
.head
->input_statement
;
7470 f
= f
->next_real_file
)
7472 if (f
->the_bfd
== add_bfd
)
7474 before
= last_loaded
;
7475 if (f
->next
!= NULL
)
7476 return &f
->next
->next
;
7478 if (f
->the_bfd
!= NULL
&& f
->next
!= NULL
)
7482 for (iter
= before
? &before
->next
: &file_chain
.head
->input_statement
.next
;
7484 iter
= &(*iter
)->next
)
7485 if (!(*iter
)->flags
.claim_archive
7486 && (*iter
)->the_bfd
->my_archive
== NULL
)
7492 /* Insert SRCLIST into DESTLIST after given element by chaining
7493 on FIELD as the next-pointer. (Counterintuitively does not need
7494 a pointer to the actual after-node itself, just its chain field.) */
7497 lang_list_insert_after (lang_statement_list_type
*destlist
,
7498 lang_statement_list_type
*srclist
,
7499 lang_statement_union_type
**field
)
7501 *(srclist
->tail
) = *field
;
7502 *field
= srclist
->head
;
7503 if (destlist
->tail
== field
)
7504 destlist
->tail
= srclist
->tail
;
7507 /* Detach new nodes added to DESTLIST since the time ORIGLIST
7508 was taken as a copy of it and leave them in ORIGLIST. */
7511 lang_list_remove_tail (lang_statement_list_type
*destlist
,
7512 lang_statement_list_type
*origlist
)
7514 union lang_statement_union
**savetail
;
7515 /* Check that ORIGLIST really is an earlier state of DESTLIST. */
7516 ASSERT (origlist
->head
== destlist
->head
);
7517 savetail
= origlist
->tail
;
7518 origlist
->head
= *(savetail
);
7519 origlist
->tail
= destlist
->tail
;
7520 destlist
->tail
= savetail
;
7524 static lang_statement_union_type
**
7525 find_next_input_statement (lang_statement_union_type
**s
)
7527 for ( ; *s
; s
= &(*s
)->header
.next
)
7529 lang_statement_union_type
**t
;
7530 switch ((*s
)->header
.type
)
7532 case lang_input_statement_enum
:
7534 case lang_wild_statement_enum
:
7535 t
= &(*s
)->wild_statement
.children
.head
;
7537 case lang_group_statement_enum
:
7538 t
= &(*s
)->group_statement
.children
.head
;
7540 case lang_output_section_statement_enum
:
7541 t
= &(*s
)->output_section_statement
.children
.head
;
7546 t
= find_next_input_statement (t
);
7552 #endif /* ENABLE_PLUGINS */
7554 /* Add NAME to the list of garbage collection entry points. */
7557 lang_add_gc_name (const char *name
)
7559 struct bfd_sym_chain
*sym
;
7564 sym
= stat_alloc (sizeof (*sym
));
7566 sym
->next
= link_info
.gc_sym_list
;
7568 link_info
.gc_sym_list
= sym
;
7571 /* Check relocations. */
7574 lang_check_relocs (void)
7576 if (link_info
.check_relocs_after_open_input
)
7580 for (abfd
= link_info
.input_bfds
;
7581 abfd
!= (bfd
*) NULL
; abfd
= abfd
->link
.next
)
7582 if (!bfd_link_check_relocs (abfd
, &link_info
))
7584 /* No object output, fail return. */
7585 config
.make_executable
= FALSE
;
7586 /* Note: we do not abort the loop, but rather
7587 continue the scan in case there are other
7588 bad relocations to report. */
7593 /* Look through all output sections looking for places where we can
7594 propagate forward the lma region. */
7597 lang_propagate_lma_regions (void)
7599 lang_output_section_statement_type
*os
;
7601 for (os
= &lang_os_list
.head
->output_section_statement
;
7605 if (os
->prev
!= NULL
7606 && os
->lma_region
== NULL
7607 && os
->load_base
== NULL
7608 && os
->addr_tree
== NULL
7609 && os
->region
== os
->prev
->region
)
7610 os
->lma_region
= os
->prev
->lma_region
;
7617 /* Finalize dynamic list. */
7618 if (link_info
.dynamic_list
)
7619 lang_finalize_version_expr_head (&link_info
.dynamic_list
->head
);
7621 current_target
= default_target
;
7623 /* Open the output file. */
7624 lang_for_each_statement (ldlang_open_output
);
7627 ldemul_create_output_section_statements ();
7629 /* Add to the hash table all undefineds on the command line. */
7630 lang_place_undefineds ();
7632 if (!bfd_section_already_linked_table_init ())
7633 einfo (_("%F%P: can not create hash table: %E\n"));
7635 /* Create a bfd for each input file. */
7636 current_target
= default_target
;
7637 lang_statement_iteration
++;
7638 open_input_bfds (statement_list
.head
, OPEN_BFD_NORMAL
);
7639 /* open_input_bfds also handles assignments, so we can give values
7640 to symbolic origin/length now. */
7641 lang_do_memory_regions ();
7643 #ifdef ENABLE_PLUGINS
7644 if (link_info
.lto_plugin_active
)
7646 lang_statement_list_type added
;
7647 lang_statement_list_type files
, inputfiles
;
7649 /* Now all files are read, let the plugin(s) decide if there
7650 are any more to be added to the link before we call the
7651 emulation's after_open hook. We create a private list of
7652 input statements for this purpose, which we will eventually
7653 insert into the global statement list after the first claimed
7656 /* We need to manipulate all three chains in synchrony. */
7658 inputfiles
= input_file_chain
;
7659 if (plugin_call_all_symbols_read ())
7660 einfo (_("%F%P: %s: plugin reported error after all symbols read\n"),
7661 plugin_error_plugin ());
7662 /* Open any newly added files, updating the file chains. */
7663 plugin_undefs
= link_info
.hash
->undefs_tail
;
7664 open_input_bfds (*added
.tail
, OPEN_BFD_NORMAL
);
7665 if (plugin_undefs
== link_info
.hash
->undefs_tail
)
7666 plugin_undefs
= NULL
;
7667 /* Restore the global list pointer now they have all been added. */
7668 lang_list_remove_tail (stat_ptr
, &added
);
7669 /* And detach the fresh ends of the file lists. */
7670 lang_list_remove_tail (&file_chain
, &files
);
7671 lang_list_remove_tail (&input_file_chain
, &inputfiles
);
7672 /* Were any new files added? */
7673 if (added
.head
!= NULL
)
7675 /* If so, we will insert them into the statement list immediately
7676 after the first input file that was claimed by the plugin,
7677 unless that file was an archive in which case it is inserted
7678 immediately before. */
7680 lang_statement_union_type
**prev
;
7681 plugin_insert
= find_replacements_insert_point (&before
);
7682 /* If a plugin adds input files without having claimed any, we
7683 don't really have a good idea where to place them. Just putting
7684 them at the start or end of the list is liable to leave them
7685 outside the crtbegin...crtend range. */
7686 ASSERT (plugin_insert
!= NULL
);
7687 /* Splice the new statement list into the old one. */
7688 prev
= &plugin_insert
->header
.next
;
7691 prev
= find_next_input_statement (prev
);
7692 if (*prev
!= (void *) plugin_insert
->next_real_file
)
7694 /* We didn't find the expected input statement.
7695 Fall back to adding after plugin_insert. */
7696 prev
= &plugin_insert
->header
.next
;
7699 lang_list_insert_after (stat_ptr
, &added
, prev
);
7700 /* Likewise for the file chains. */
7701 lang_list_insert_after (&input_file_chain
, &inputfiles
,
7702 (void *) &plugin_insert
->next_real_file
);
7703 /* We must be careful when relinking file_chain; we may need to
7704 insert the new files at the head of the list if the insert
7705 point chosen is the dummy first input file. */
7706 if (plugin_insert
->filename
)
7707 lang_list_insert_after (&file_chain
, &files
,
7708 (void *) &plugin_insert
->next
);
7710 lang_list_insert_after (&file_chain
, &files
, &file_chain
.head
);
7712 /* Rescan archives in case new undefined symbols have appeared. */
7714 lang_statement_iteration
++;
7715 open_input_bfds (statement_list
.head
, OPEN_BFD_RESCAN
);
7716 lang_list_remove_tail (&file_chain
, &files
);
7717 while (files
.head
!= NULL
)
7719 lang_input_statement_type
**insert
;
7720 lang_input_statement_type
**iter
, *temp
;
7723 insert
= find_rescan_insertion (&files
.head
->input_statement
);
7724 /* All elements from an archive can be added at once. */
7725 iter
= &files
.head
->input_statement
.next
;
7726 my_arch
= files
.head
->input_statement
.the_bfd
->my_archive
;
7727 if (my_arch
!= NULL
)
7728 for (; *iter
!= NULL
; iter
= &(*iter
)->next
)
7729 if ((*iter
)->the_bfd
->my_archive
!= my_arch
)
7732 *insert
= &files
.head
->input_statement
;
7733 files
.head
= (lang_statement_union_type
*) *iter
;
7735 if (my_arch
!= NULL
)
7737 lang_input_statement_type
*parent
= bfd_usrdata (my_arch
);
7739 parent
->next
= (lang_input_statement_type
*)
7741 - offsetof (lang_input_statement_type
, next
));
7746 #endif /* ENABLE_PLUGINS */
7748 /* Make sure that nobody has tried to add a symbol to this list
7750 ASSERT (link_info
.gc_sym_list
== NULL
);
7752 link_info
.gc_sym_list
= &entry_symbol
;
7754 if (entry_symbol
.name
== NULL
)
7756 link_info
.gc_sym_list
= ldlang_undef_chain_list_head
;
7758 /* entry_symbol is normally initialied by a ENTRY definition in the
7759 linker script or the -e command line option. But if neither of
7760 these have been used, the target specific backend may still have
7761 provided an entry symbol via a call to lang_default_entry().
7762 Unfortunately this value will not be processed until lang_end()
7763 is called, long after this function has finished. So detect this
7764 case here and add the target's entry symbol to the list of starting
7765 points for garbage collection resolution. */
7766 lang_add_gc_name (entry_symbol_default
);
7769 lang_add_gc_name (link_info
.init_function
);
7770 lang_add_gc_name (link_info
.fini_function
);
7772 ldemul_after_open ();
7773 if (config
.map_file
!= NULL
)
7774 lang_print_asneeded ();
7778 bfd_section_already_linked_table_free ();
7780 /* Make sure that we're not mixing architectures. We call this
7781 after all the input files have been opened, but before we do any
7782 other processing, so that any operations merge_private_bfd_data
7783 does on the output file will be known during the rest of the
7787 /* Handle .exports instead of a version script if we're told to do so. */
7788 if (command_line
.version_exports_section
)
7789 lang_do_version_exports_section ();
7791 /* Build all sets based on the information gathered from the input
7793 ldctor_build_sets ();
7795 /* Give initial values for __start and __stop symbols, so that ELF
7796 gc_sections will keep sections referenced by these symbols. Must
7797 be done before lang_do_assignments below. */
7798 if (config
.build_constructors
)
7799 lang_init_start_stop ();
7801 /* PR 13683: We must rerun the assignments prior to running garbage
7802 collection in order to make sure that all symbol aliases are resolved. */
7803 lang_do_assignments (lang_mark_phase_enum
);
7804 expld
.phase
= lang_first_phase_enum
;
7806 /* Size up the common data. */
7809 /* Remove unreferenced sections if asked to. */
7810 lang_gc_sections ();
7812 /* Check relocations. */
7813 lang_check_relocs ();
7815 ldemul_after_check_relocs ();
7817 /* Update wild statements. */
7818 update_wild_statements (statement_list
.head
);
7820 /* Run through the contours of the script and attach input sections
7821 to the correct output sections. */
7822 lang_statement_iteration
++;
7823 map_input_to_output_sections (statement_list
.head
, NULL
, NULL
);
7825 /* Start at the statement immediately after the special abs_section
7826 output statement, so that it isn't reordered. */
7827 process_insert_statements (&lang_os_list
.head
->header
.next
);
7829 /* Find any sections not attached explicitly and handle them. */
7830 lang_place_orphans ();
7832 if (!bfd_link_relocatable (&link_info
))
7836 /* Merge SEC_MERGE sections. This has to be done after GC of
7837 sections, so that GCed sections are not merged, but before
7838 assigning dynamic symbols, since removing whole input sections
7840 bfd_merge_sections (link_info
.output_bfd
, &link_info
);
7842 /* Look for a text section and set the readonly attribute in it. */
7843 found
= bfd_get_section_by_name (link_info
.output_bfd
, ".text");
7847 if (config
.text_read_only
)
7848 found
->flags
|= SEC_READONLY
;
7850 found
->flags
&= ~SEC_READONLY
;
7854 /* Merge together CTF sections. After this, only the symtab-dependent
7855 function and data object sections need adjustment. */
7858 /* Emit the CTF, iff the emulation doesn't need to do late emission after
7859 examining things laid out late, like the strtab. */
7862 /* Copy forward lma regions for output sections in same lma region. */
7863 lang_propagate_lma_regions ();
7865 /* Defining __start/__stop symbols early for --gc-sections to work
7866 around a glibc build problem can result in these symbols being
7867 defined when they should not be. Fix them now. */
7868 if (config
.build_constructors
)
7869 lang_undef_start_stop ();
7871 /* Define .startof./.sizeof. symbols with preliminary values before
7872 dynamic symbols are created. */
7873 if (!bfd_link_relocatable (&link_info
))
7874 lang_init_startof_sizeof ();
7876 /* Do anything special before sizing sections. This is where ELF
7877 and other back-ends size dynamic sections. */
7878 ldemul_before_allocation ();
7880 /* We must record the program headers before we try to fix the
7881 section positions, since they will affect SIZEOF_HEADERS. */
7882 lang_record_phdrs ();
7884 /* Check relro sections. */
7885 if (link_info
.relro
&& !bfd_link_relocatable (&link_info
))
7886 lang_find_relro_sections ();
7888 /* Size up the sections. */
7889 lang_size_sections (NULL
, !RELAXATION_ENABLED
);
7891 /* See if anything special should be done now we know how big
7892 everything is. This is where relaxation is done. */
7893 ldemul_after_allocation ();
7895 /* Fix any __start, __stop, .startof. or .sizeof. symbols. */
7896 lang_finalize_start_stop ();
7898 /* Do all the assignments again, to report errors. Assignment
7899 statements are processed multiple times, updating symbols; In
7900 open_input_bfds, lang_do_assignments, and lang_size_sections.
7901 Since lang_relax_sections calls lang_do_assignments, symbols are
7902 also updated in ldemul_after_allocation. */
7903 lang_do_assignments (lang_final_phase_enum
);
7907 /* Convert absolute symbols to section relative. */
7908 ldexp_finalize_syms ();
7910 /* Make sure that the section addresses make sense. */
7911 if (command_line
.check_section_addresses
)
7912 lang_check_section_addresses ();
7914 /* Check any required symbols are known. */
7915 ldlang_check_require_defined_symbols ();
7920 /* EXPORTED TO YACC */
7923 lang_add_wild (struct wildcard_spec
*filespec
,
7924 struct wildcard_list
*section_list
,
7925 bfd_boolean keep_sections
)
7927 struct wildcard_list
*curr
, *next
;
7928 lang_wild_statement_type
*new_stmt
;
7930 /* Reverse the list as the parser puts it back to front. */
7931 for (curr
= section_list
, section_list
= NULL
;
7933 section_list
= curr
, curr
= next
)
7936 curr
->next
= section_list
;
7939 if (filespec
!= NULL
&& filespec
->name
!= NULL
)
7941 if (strcmp (filespec
->name
, "*") == 0)
7942 filespec
->name
= NULL
;
7943 else if (!wildcardp (filespec
->name
))
7944 lang_has_input_file
= TRUE
;
7947 new_stmt
= new_stat (lang_wild_statement
, stat_ptr
);
7948 new_stmt
->filename
= NULL
;
7949 new_stmt
->filenames_sorted
= FALSE
;
7950 new_stmt
->section_flag_list
= NULL
;
7951 new_stmt
->exclude_name_list
= NULL
;
7952 if (filespec
!= NULL
)
7954 new_stmt
->filename
= filespec
->name
;
7955 new_stmt
->filenames_sorted
= filespec
->sorted
== by_name
;
7956 new_stmt
->section_flag_list
= filespec
->section_flag_list
;
7957 new_stmt
->exclude_name_list
= filespec
->exclude_name_list
;
7959 new_stmt
->section_list
= section_list
;
7960 new_stmt
->keep_sections
= keep_sections
;
7961 lang_list_init (&new_stmt
->children
);
7962 analyze_walk_wild_section_handler (new_stmt
);
7966 lang_section_start (const char *name
, etree_type
*address
,
7967 const segment_type
*segment
)
7969 lang_address_statement_type
*ad
;
7971 ad
= new_stat (lang_address_statement
, stat_ptr
);
7972 ad
->section_name
= name
;
7973 ad
->address
= address
;
7974 ad
->segment
= segment
;
7977 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
7978 because of a -e argument on the command line, or zero if this is
7979 called by ENTRY in a linker script. Command line arguments take
7983 lang_add_entry (const char *name
, bfd_boolean cmdline
)
7985 if (entry_symbol
.name
== NULL
7987 || !entry_from_cmdline
)
7989 entry_symbol
.name
= name
;
7990 entry_from_cmdline
= cmdline
;
7994 /* Set the default start symbol to NAME. .em files should use this,
7995 not lang_add_entry, to override the use of "start" if neither the
7996 linker script nor the command line specifies an entry point. NAME
7997 must be permanently allocated. */
7999 lang_default_entry (const char *name
)
8001 entry_symbol_default
= name
;
8005 lang_add_target (const char *name
)
8007 lang_target_statement_type
*new_stmt
;
8009 new_stmt
= new_stat (lang_target_statement
, stat_ptr
);
8010 new_stmt
->target
= name
;
8014 lang_add_map (const char *name
)
8021 map_option_f
= TRUE
;
8029 lang_add_fill (fill_type
*fill
)
8031 lang_fill_statement_type
*new_stmt
;
8033 new_stmt
= new_stat (lang_fill_statement
, stat_ptr
);
8034 new_stmt
->fill
= fill
;
8038 lang_add_data (int type
, union etree_union
*exp
)
8040 lang_data_statement_type
*new_stmt
;
8042 new_stmt
= new_stat (lang_data_statement
, stat_ptr
);
8043 new_stmt
->exp
= exp
;
8044 new_stmt
->type
= type
;
8047 /* Create a new reloc statement. RELOC is the BFD relocation type to
8048 generate. HOWTO is the corresponding howto structure (we could
8049 look this up, but the caller has already done so). SECTION is the
8050 section to generate a reloc against, or NAME is the name of the
8051 symbol to generate a reloc against. Exactly one of SECTION and
8052 NAME must be NULL. ADDEND is an expression for the addend. */
8055 lang_add_reloc (bfd_reloc_code_real_type reloc
,
8056 reloc_howto_type
*howto
,
8059 union etree_union
*addend
)
8061 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
8065 p
->section
= section
;
8067 p
->addend_exp
= addend
;
8069 p
->addend_value
= 0;
8070 p
->output_section
= NULL
;
8071 p
->output_offset
= 0;
8074 lang_assignment_statement_type
*
8075 lang_add_assignment (etree_type
*exp
)
8077 lang_assignment_statement_type
*new_stmt
;
8079 new_stmt
= new_stat (lang_assignment_statement
, stat_ptr
);
8080 new_stmt
->exp
= exp
;
8085 lang_add_attribute (enum statement_enum attribute
)
8087 new_statement (attribute
, sizeof (lang_statement_header_type
), stat_ptr
);
8091 lang_startup (const char *name
)
8093 if (first_file
->filename
!= NULL
)
8095 einfo (_("%F%P: multiple STARTUP files\n"));
8097 first_file
->filename
= name
;
8098 first_file
->local_sym_name
= name
;
8099 first_file
->flags
.real
= TRUE
;
8103 lang_float (bfd_boolean maybe
)
8105 lang_float_flag
= maybe
;
8109 /* Work out the load- and run-time regions from a script statement, and
8110 store them in *LMA_REGION and *REGION respectively.
8112 MEMSPEC is the name of the run-time region, or the value of
8113 DEFAULT_MEMORY_REGION if the statement didn't specify one.
8114 LMA_MEMSPEC is the name of the load-time region, or null if the
8115 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
8116 had an explicit load address.
8118 It is an error to specify both a load region and a load address. */
8121 lang_get_regions (lang_memory_region_type
**region
,
8122 lang_memory_region_type
**lma_region
,
8123 const char *memspec
,
8124 const char *lma_memspec
,
8125 bfd_boolean have_lma
,
8126 bfd_boolean have_vma
)
8128 *lma_region
= lang_memory_region_lookup (lma_memspec
, FALSE
);
8130 /* If no runtime region or VMA has been specified, but the load region
8131 has been specified, then use the load region for the runtime region
8133 if (lma_memspec
!= NULL
8135 && strcmp (memspec
, DEFAULT_MEMORY_REGION
) == 0)
8136 *region
= *lma_region
;
8138 *region
= lang_memory_region_lookup (memspec
, FALSE
);
8140 if (have_lma
&& lma_memspec
!= 0)
8141 einfo (_("%X%P:%pS: section has both a load address and a load region\n"),
8146 lang_leave_output_section_statement (fill_type
*fill
, const char *memspec
,
8147 lang_output_section_phdr_list
*phdrs
,
8148 const char *lma_memspec
)
8150 lang_get_regions (¤t_section
->region
,
8151 ¤t_section
->lma_region
,
8152 memspec
, lma_memspec
,
8153 current_section
->load_base
!= NULL
,
8154 current_section
->addr_tree
!= NULL
);
8156 current_section
->fill
= fill
;
8157 current_section
->phdrs
= phdrs
;
8161 /* Set the output format type. -oformat overrides scripts. */
8164 lang_add_output_format (const char *format
,
8169 if (output_target
== NULL
|| !from_script
)
8171 if (command_line
.endian
== ENDIAN_BIG
8174 else if (command_line
.endian
== ENDIAN_LITTLE
8178 output_target
= format
;
8183 lang_add_insert (const char *where
, int is_before
)
8185 lang_insert_statement_type
*new_stmt
;
8187 new_stmt
= new_stat (lang_insert_statement
, stat_ptr
);
8188 new_stmt
->where
= where
;
8189 new_stmt
->is_before
= is_before
;
8190 saved_script_handle
= previous_script_handle
;
8193 /* Enter a group. This creates a new lang_group_statement, and sets
8194 stat_ptr to build new statements within the group. */
8197 lang_enter_group (void)
8199 lang_group_statement_type
*g
;
8201 g
= new_stat (lang_group_statement
, stat_ptr
);
8202 lang_list_init (&g
->children
);
8203 push_stat_ptr (&g
->children
);
8206 /* Leave a group. This just resets stat_ptr to start writing to the
8207 regular list of statements again. Note that this will not work if
8208 groups can occur inside anything else which can adjust stat_ptr,
8209 but currently they can't. */
8212 lang_leave_group (void)
8217 /* Add a new program header. This is called for each entry in a PHDRS
8218 command in a linker script. */
8221 lang_new_phdr (const char *name
,
8223 bfd_boolean filehdr
,
8228 struct lang_phdr
*n
, **pp
;
8231 n
= stat_alloc (sizeof (struct lang_phdr
));
8234 n
->type
= exp_get_vma (type
, 0, "program header type");
8235 n
->filehdr
= filehdr
;
8240 hdrs
= n
->type
== 1 && (phdrs
|| filehdr
);
8242 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
8245 && !((*pp
)->filehdr
|| (*pp
)->phdrs
))
8247 einfo (_("%X%P:%pS: PHDRS and FILEHDR are not supported"
8248 " when prior PT_LOAD headers lack them\n"), NULL
);
8255 /* Record the program header information in the output BFD. FIXME: We
8256 should not be calling an ELF specific function here. */
8259 lang_record_phdrs (void)
8263 lang_output_section_phdr_list
*last
;
8264 struct lang_phdr
*l
;
8265 lang_output_section_statement_type
*os
;
8268 secs
= (asection
**) xmalloc (alc
* sizeof (asection
*));
8271 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
8278 for (os
= &lang_os_list
.head
->output_section_statement
;
8282 lang_output_section_phdr_list
*pl
;
8284 if (os
->constraint
< 0)
8292 if (os
->sectype
== noload_section
8293 || os
->bfd_section
== NULL
8294 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
8297 /* Don't add orphans to PT_INTERP header. */
8303 lang_output_section_statement_type
*tmp_os
;
8305 /* If we have not run across a section with a program
8306 header assigned to it yet, then scan forwards to find
8307 one. This prevents inconsistencies in the linker's
8308 behaviour when a script has specified just a single
8309 header and there are sections in that script which are
8310 not assigned to it, and which occur before the first
8311 use of that header. See here for more details:
8312 http://sourceware.org/ml/binutils/2007-02/msg00291.html */
8313 for (tmp_os
= os
; tmp_os
; tmp_os
= tmp_os
->next
)
8316 last
= tmp_os
->phdrs
;
8320 einfo (_("%F%P: no sections assigned to phdrs\n"));
8325 if (os
->bfd_section
== NULL
)
8328 for (; pl
!= NULL
; pl
= pl
->next
)
8330 if (strcmp (pl
->name
, l
->name
) == 0)
8335 secs
= (asection
**) xrealloc (secs
,
8336 alc
* sizeof (asection
*));
8338 secs
[c
] = os
->bfd_section
;
8345 if (l
->flags
== NULL
)
8348 flags
= exp_get_vma (l
->flags
, 0, "phdr flags");
8353 at
= exp_get_vma (l
->at
, 0, "phdr load address");
8355 if (!bfd_record_phdr (link_info
.output_bfd
, l
->type
,
8356 l
->flags
!= NULL
, flags
, l
->at
!= NULL
,
8357 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
8358 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
8363 /* Make sure all the phdr assignments succeeded. */
8364 for (os
= &lang_os_list
.head
->output_section_statement
;
8368 lang_output_section_phdr_list
*pl
;
8370 if (os
->constraint
< 0
8371 || os
->bfd_section
== NULL
)
8374 for (pl
= os
->phdrs
;
8377 if (!pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
8378 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
8379 os
->name
, pl
->name
);
8383 /* Record a list of sections which may not be cross referenced. */
8386 lang_add_nocrossref (lang_nocrossref_type
*l
)
8388 struct lang_nocrossrefs
*n
;
8390 n
= (struct lang_nocrossrefs
*) xmalloc (sizeof *n
);
8391 n
->next
= nocrossref_list
;
8393 n
->onlyfirst
= FALSE
;
8394 nocrossref_list
= n
;
8396 /* Set notice_all so that we get informed about all symbols. */
8397 link_info
.notice_all
= TRUE
;
8400 /* Record a section that cannot be referenced from a list of sections. */
8403 lang_add_nocrossref_to (lang_nocrossref_type
*l
)
8405 lang_add_nocrossref (l
);
8406 nocrossref_list
->onlyfirst
= TRUE
;
8409 /* Overlay handling. We handle overlays with some static variables. */
8411 /* The overlay virtual address. */
8412 static etree_type
*overlay_vma
;
8413 /* And subsection alignment. */
8414 static etree_type
*overlay_subalign
;
8416 /* An expression for the maximum section size seen so far. */
8417 static etree_type
*overlay_max
;
8419 /* A list of all the sections in this overlay. */
8421 struct overlay_list
{
8422 struct overlay_list
*next
;
8423 lang_output_section_statement_type
*os
;
8426 static struct overlay_list
*overlay_list
;
8428 /* Start handling an overlay. */
8431 lang_enter_overlay (etree_type
*vma_expr
, etree_type
*subalign
)
8433 /* The grammar should prevent nested overlays from occurring. */
8434 ASSERT (overlay_vma
== NULL
8435 && overlay_subalign
== NULL
8436 && overlay_max
== NULL
);
8438 overlay_vma
= vma_expr
;
8439 overlay_subalign
= subalign
;
8442 /* Start a section in an overlay. We handle this by calling
8443 lang_enter_output_section_statement with the correct VMA.
8444 lang_leave_overlay sets up the LMA and memory regions. */
8447 lang_enter_overlay_section (const char *name
)
8449 struct overlay_list
*n
;
8452 lang_enter_output_section_statement (name
, overlay_vma
, overlay_section
,
8453 0, overlay_subalign
, 0, 0, 0);
8455 /* If this is the first section, then base the VMA of future
8456 sections on this one. This will work correctly even if `.' is
8457 used in the addresses. */
8458 if (overlay_list
== NULL
)
8459 overlay_vma
= exp_nameop (ADDR
, name
);
8461 /* Remember the section. */
8462 n
= (struct overlay_list
*) xmalloc (sizeof *n
);
8463 n
->os
= current_section
;
8464 n
->next
= overlay_list
;
8467 size
= exp_nameop (SIZEOF
, name
);
8469 /* Arrange to work out the maximum section end address. */
8470 if (overlay_max
== NULL
)
8473 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
8476 /* Finish a section in an overlay. There isn't any special to do
8480 lang_leave_overlay_section (fill_type
*fill
,
8481 lang_output_section_phdr_list
*phdrs
)
8488 name
= current_section
->name
;
8490 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
8491 region and that no load-time region has been specified. It doesn't
8492 really matter what we say here, since lang_leave_overlay will
8494 lang_leave_output_section_statement (fill
, DEFAULT_MEMORY_REGION
, phdrs
, 0);
8496 /* Define the magic symbols. */
8498 clean
= (char *) xmalloc (strlen (name
) + 1);
8500 for (s1
= name
; *s1
!= '\0'; s1
++)
8501 if (ISALNUM (*s1
) || *s1
== '_')
8505 buf
= (char *) xmalloc (strlen (clean
) + sizeof "__load_start_");
8506 sprintf (buf
, "__load_start_%s", clean
);
8507 lang_add_assignment (exp_provide (buf
,
8508 exp_nameop (LOADADDR
, name
),
8511 buf
= (char *) xmalloc (strlen (clean
) + sizeof "__load_stop_");
8512 sprintf (buf
, "__load_stop_%s", clean
);
8513 lang_add_assignment (exp_provide (buf
,
8515 exp_nameop (LOADADDR
, name
),
8516 exp_nameop (SIZEOF
, name
)),
8522 /* Finish an overlay. If there are any overlay wide settings, this
8523 looks through all the sections in the overlay and sets them. */
8526 lang_leave_overlay (etree_type
*lma_expr
,
8529 const char *memspec
,
8530 lang_output_section_phdr_list
*phdrs
,
8531 const char *lma_memspec
)
8533 lang_memory_region_type
*region
;
8534 lang_memory_region_type
*lma_region
;
8535 struct overlay_list
*l
;
8536 lang_nocrossref_type
*nocrossref
;
8538 lang_get_regions (®ion
, &lma_region
,
8539 memspec
, lma_memspec
,
8540 lma_expr
!= NULL
, FALSE
);
8544 /* After setting the size of the last section, set '.' to end of the
8546 if (overlay_list
!= NULL
)
8548 overlay_list
->os
->update_dot
= 1;
8549 overlay_list
->os
->update_dot_tree
8550 = exp_assign (".", exp_binop ('+', overlay_vma
, overlay_max
), FALSE
);
8556 struct overlay_list
*next
;
8558 if (fill
!= NULL
&& l
->os
->fill
== NULL
)
8561 l
->os
->region
= region
;
8562 l
->os
->lma_region
= lma_region
;
8564 /* The first section has the load address specified in the
8565 OVERLAY statement. The rest are worked out from that.
8566 The base address is not needed (and should be null) if
8567 an LMA region was specified. */
8570 l
->os
->load_base
= lma_expr
;
8571 l
->os
->sectype
= first_overlay_section
;
8573 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
8574 l
->os
->phdrs
= phdrs
;
8578 lang_nocrossref_type
*nc
;
8580 nc
= (lang_nocrossref_type
*) xmalloc (sizeof *nc
);
8581 nc
->name
= l
->os
->name
;
8582 nc
->next
= nocrossref
;
8591 if (nocrossref
!= NULL
)
8592 lang_add_nocrossref (nocrossref
);
8595 overlay_list
= NULL
;
8597 overlay_subalign
= NULL
;
8600 /* Version handling. This is only useful for ELF. */
8602 /* If PREV is NULL, return first version pattern matching particular symbol.
8603 If PREV is non-NULL, return first version pattern matching particular
8604 symbol after PREV (previously returned by lang_vers_match). */
8606 static struct bfd_elf_version_expr
*
8607 lang_vers_match (struct bfd_elf_version_expr_head
*head
,
8608 struct bfd_elf_version_expr
*prev
,
8612 const char *cxx_sym
= sym
;
8613 const char *java_sym
= sym
;
8614 struct bfd_elf_version_expr
*expr
= NULL
;
8615 enum demangling_styles curr_style
;
8617 curr_style
= CURRENT_DEMANGLING_STYLE
;
8618 cplus_demangle_set_style (no_demangling
);
8619 c_sym
= bfd_demangle (link_info
.output_bfd
, sym
, DMGL_NO_OPTS
);
8622 cplus_demangle_set_style (curr_style
);
8624 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
8626 cxx_sym
= bfd_demangle (link_info
.output_bfd
, sym
,
8627 DMGL_PARAMS
| DMGL_ANSI
);
8631 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
8633 java_sym
= bfd_demangle (link_info
.output_bfd
, sym
, DMGL_JAVA
);
8638 if (head
->htab
&& (prev
== NULL
|| prev
->literal
))
8640 struct bfd_elf_version_expr e
;
8642 switch (prev
? prev
->mask
: 0)
8645 if (head
->mask
& BFD_ELF_VERSION_C_TYPE
)
8648 expr
= (struct bfd_elf_version_expr
*)
8649 htab_find ((htab_t
) head
->htab
, &e
);
8650 while (expr
&& strcmp (expr
->pattern
, c_sym
) == 0)
8651 if (expr
->mask
== BFD_ELF_VERSION_C_TYPE
)
8657 case BFD_ELF_VERSION_C_TYPE
:
8658 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
8660 e
.pattern
= cxx_sym
;
8661 expr
= (struct bfd_elf_version_expr
*)
8662 htab_find ((htab_t
) head
->htab
, &e
);
8663 while (expr
&& strcmp (expr
->pattern
, cxx_sym
) == 0)
8664 if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
8670 case BFD_ELF_VERSION_CXX_TYPE
:
8671 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
8673 e
.pattern
= java_sym
;
8674 expr
= (struct bfd_elf_version_expr
*)
8675 htab_find ((htab_t
) head
->htab
, &e
);
8676 while (expr
&& strcmp (expr
->pattern
, java_sym
) == 0)
8677 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
8688 /* Finally, try the wildcards. */
8689 if (prev
== NULL
|| prev
->literal
)
8690 expr
= head
->remaining
;
8693 for (; expr
; expr
= expr
->next
)
8700 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
8703 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
8705 else if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
8709 if (fnmatch (expr
->pattern
, s
, 0) == 0)
8715 free ((char *) c_sym
);
8717 free ((char *) cxx_sym
);
8718 if (java_sym
!= sym
)
8719 free ((char *) java_sym
);
8723 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
8724 return a pointer to the symbol name with any backslash quotes removed. */
8727 realsymbol (const char *pattern
)
8730 bfd_boolean changed
= FALSE
, backslash
= FALSE
;
8731 char *s
, *symbol
= (char *) xmalloc (strlen (pattern
) + 1);
8733 for (p
= pattern
, s
= symbol
; *p
!= '\0'; ++p
)
8735 /* It is a glob pattern only if there is no preceding
8739 /* Remove the preceding backslash. */
8746 if (*p
== '?' || *p
== '*' || *p
== '[')
8753 backslash
= *p
== '\\';
8769 /* This is called for each variable name or match expression. NEW_NAME is
8770 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
8771 pattern to be matched against symbol names. */
8773 struct bfd_elf_version_expr
*
8774 lang_new_vers_pattern (struct bfd_elf_version_expr
*orig
,
8775 const char *new_name
,
8777 bfd_boolean literal_p
)
8779 struct bfd_elf_version_expr
*ret
;
8781 ret
= (struct bfd_elf_version_expr
*) xmalloc (sizeof *ret
);
8785 ret
->literal
= TRUE
;
8786 ret
->pattern
= literal_p
? new_name
: realsymbol (new_name
);
8787 if (ret
->pattern
== NULL
)
8789 ret
->pattern
= new_name
;
8790 ret
->literal
= FALSE
;
8793 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
8794 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
8795 else if (strcasecmp (lang
, "C++") == 0)
8796 ret
->mask
= BFD_ELF_VERSION_CXX_TYPE
;
8797 else if (strcasecmp (lang
, "Java") == 0)
8798 ret
->mask
= BFD_ELF_VERSION_JAVA_TYPE
;
8801 einfo (_("%X%P: unknown language `%s' in version information\n"),
8803 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
8806 return ldemul_new_vers_pattern (ret
);
8809 /* This is called for each set of variable names and match
8812 struct bfd_elf_version_tree
*
8813 lang_new_vers_node (struct bfd_elf_version_expr
*globals
,
8814 struct bfd_elf_version_expr
*locals
)
8816 struct bfd_elf_version_tree
*ret
;
8818 ret
= (struct bfd_elf_version_tree
*) xcalloc (1, sizeof *ret
);
8819 ret
->globals
.list
= globals
;
8820 ret
->locals
.list
= locals
;
8821 ret
->match
= lang_vers_match
;
8822 ret
->name_indx
= (unsigned int) -1;
8826 /* This static variable keeps track of version indices. */
8828 static int version_index
;
8831 version_expr_head_hash (const void *p
)
8833 const struct bfd_elf_version_expr
*e
=
8834 (const struct bfd_elf_version_expr
*) p
;
8836 return htab_hash_string (e
->pattern
);
8840 version_expr_head_eq (const void *p1
, const void *p2
)
8842 const struct bfd_elf_version_expr
*e1
=
8843 (const struct bfd_elf_version_expr
*) p1
;
8844 const struct bfd_elf_version_expr
*e2
=
8845 (const struct bfd_elf_version_expr
*) p2
;
8847 return strcmp (e1
->pattern
, e2
->pattern
) == 0;
8851 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head
*head
)
8854 struct bfd_elf_version_expr
*e
, *next
;
8855 struct bfd_elf_version_expr
**list_loc
, **remaining_loc
;
8857 for (e
= head
->list
; e
; e
= e
->next
)
8861 head
->mask
|= e
->mask
;
8866 head
->htab
= htab_create (count
* 2, version_expr_head_hash
,
8867 version_expr_head_eq
, NULL
);
8868 list_loc
= &head
->list
;
8869 remaining_loc
= &head
->remaining
;
8870 for (e
= head
->list
; e
; e
= next
)
8876 remaining_loc
= &e
->next
;
8880 void **loc
= htab_find_slot ((htab_t
) head
->htab
, e
, INSERT
);
8884 struct bfd_elf_version_expr
*e1
, *last
;
8886 e1
= (struct bfd_elf_version_expr
*) *loc
;
8890 if (e1
->mask
== e
->mask
)
8898 while (e1
&& strcmp (e1
->pattern
, e
->pattern
) == 0);
8902 /* This is a duplicate. */
8903 /* FIXME: Memory leak. Sometimes pattern is not
8904 xmalloced alone, but in larger chunk of memory. */
8905 /* free (e->pattern); */
8910 e
->next
= last
->next
;
8918 list_loc
= &e
->next
;
8922 *remaining_loc
= NULL
;
8923 *list_loc
= head
->remaining
;
8926 head
->remaining
= head
->list
;
8929 /* This is called when we know the name and dependencies of the
8933 lang_register_vers_node (const char *name
,
8934 struct bfd_elf_version_tree
*version
,
8935 struct bfd_elf_version_deps
*deps
)
8937 struct bfd_elf_version_tree
*t
, **pp
;
8938 struct bfd_elf_version_expr
*e1
;
8943 if (link_info
.version_info
!= NULL
8944 && (name
[0] == '\0' || link_info
.version_info
->name
[0] == '\0'))
8946 einfo (_("%X%P: anonymous version tag cannot be combined"
8947 " with other version tags\n"));
8952 /* Make sure this node has a unique name. */
8953 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
8954 if (strcmp (t
->name
, name
) == 0)
8955 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
8957 lang_finalize_version_expr_head (&version
->globals
);
8958 lang_finalize_version_expr_head (&version
->locals
);
8960 /* Check the global and local match names, and make sure there
8961 aren't any duplicates. */
8963 for (e1
= version
->globals
.list
; e1
!= NULL
; e1
= e1
->next
)
8965 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
8967 struct bfd_elf_version_expr
*e2
;
8969 if (t
->locals
.htab
&& e1
->literal
)
8971 e2
= (struct bfd_elf_version_expr
*)
8972 htab_find ((htab_t
) t
->locals
.htab
, e1
);
8973 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
8975 if (e1
->mask
== e2
->mask
)
8976 einfo (_("%X%P: duplicate expression `%s'"
8977 " in version information\n"), e1
->pattern
);
8981 else if (!e1
->literal
)
8982 for (e2
= t
->locals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
8983 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
8984 && e1
->mask
== e2
->mask
)
8985 einfo (_("%X%P: duplicate expression `%s'"
8986 " in version information\n"), e1
->pattern
);
8990 for (e1
= version
->locals
.list
; e1
!= NULL
; e1
= e1
->next
)
8992 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
8994 struct bfd_elf_version_expr
*e2
;
8996 if (t
->globals
.htab
&& e1
->literal
)
8998 e2
= (struct bfd_elf_version_expr
*)
8999 htab_find ((htab_t
) t
->globals
.htab
, e1
);
9000 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
9002 if (e1
->mask
== e2
->mask
)
9003 einfo (_("%X%P: duplicate expression `%s'"
9004 " in version information\n"),
9009 else if (!e1
->literal
)
9010 for (e2
= t
->globals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
9011 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
9012 && e1
->mask
== e2
->mask
)
9013 einfo (_("%X%P: duplicate expression `%s'"
9014 " in version information\n"), e1
->pattern
);
9018 version
->deps
= deps
;
9019 version
->name
= name
;
9020 if (name
[0] != '\0')
9023 version
->vernum
= version_index
;
9026 version
->vernum
= 0;
9028 for (pp
= &link_info
.version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
9033 /* This is called when we see a version dependency. */
9035 struct bfd_elf_version_deps
*
9036 lang_add_vers_depend (struct bfd_elf_version_deps
*list
, const char *name
)
9038 struct bfd_elf_version_deps
*ret
;
9039 struct bfd_elf_version_tree
*t
;
9041 ret
= (struct bfd_elf_version_deps
*) xmalloc (sizeof *ret
);
9044 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9046 if (strcmp (t
->name
, name
) == 0)
9048 ret
->version_needed
= t
;
9053 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
9055 ret
->version_needed
= NULL
;
9060 lang_do_version_exports_section (void)
9062 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
9064 LANG_FOR_EACH_INPUT_STATEMENT (is
)
9066 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
9074 contents
= (char *) xmalloc (len
);
9075 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
9076 einfo (_("%X%P: unable to read .exports section contents\n"), sec
);
9079 while (p
< contents
+ len
)
9081 greg
= lang_new_vers_pattern (greg
, p
, NULL
, FALSE
);
9082 p
= strchr (p
, '\0') + 1;
9085 /* Do not free the contents, as we used them creating the regex. */
9087 /* Do not include this section in the link. */
9088 sec
->flags
|= SEC_EXCLUDE
| SEC_KEEP
;
9091 lreg
= lang_new_vers_pattern (NULL
, "*", NULL
, FALSE
);
9092 lang_register_vers_node (command_line
.version_exports_section
,
9093 lang_new_vers_node (greg
, lreg
), NULL
);
9096 /* Evaluate LENGTH and ORIGIN parts of MEMORY spec */
9099 lang_do_memory_regions (void)
9101 lang_memory_region_type
*r
= lang_memory_region_list
;
9103 for (; r
!= NULL
; r
= r
->next
)
9107 exp_fold_tree_no_dot (r
->origin_exp
);
9108 if (expld
.result
.valid_p
)
9110 r
->origin
= expld
.result
.value
;
9111 r
->current
= r
->origin
;
9114 einfo (_("%F%P: invalid origin for memory region %s\n"),
9119 exp_fold_tree_no_dot (r
->length_exp
);
9120 if (expld
.result
.valid_p
)
9121 r
->length
= expld
.result
.value
;
9123 einfo (_("%F%P: invalid length for memory region %s\n"),
9130 lang_add_unique (const char *name
)
9132 struct unique_sections
*ent
;
9134 for (ent
= unique_section_list
; ent
; ent
= ent
->next
)
9135 if (strcmp (ent
->name
, name
) == 0)
9138 ent
= (struct unique_sections
*) xmalloc (sizeof *ent
);
9139 ent
->name
= xstrdup (name
);
9140 ent
->next
= unique_section_list
;
9141 unique_section_list
= ent
;
9144 /* Append the list of dynamic symbols to the existing one. */
9147 lang_append_dynamic_list (struct bfd_elf_version_expr
*dynamic
)
9149 if (link_info
.dynamic_list
)
9151 struct bfd_elf_version_expr
*tail
;
9152 for (tail
= dynamic
; tail
->next
!= NULL
; tail
= tail
->next
)
9154 tail
->next
= link_info
.dynamic_list
->head
.list
;
9155 link_info
.dynamic_list
->head
.list
= dynamic
;
9159 struct bfd_elf_dynamic_list
*d
;
9161 d
= (struct bfd_elf_dynamic_list
*) xcalloc (1, sizeof *d
);
9162 d
->head
.list
= dynamic
;
9163 d
->match
= lang_vers_match
;
9164 link_info
.dynamic_list
= d
;
9168 /* Append the list of C++ typeinfo dynamic symbols to the existing
9172 lang_append_dynamic_list_cpp_typeinfo (void)
9174 const char *symbols
[] =
9176 "typeinfo name for*",
9179 struct bfd_elf_version_expr
*dynamic
= NULL
;
9182 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
9183 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
9186 lang_append_dynamic_list (dynamic
);
9189 /* Append the list of C++ operator new and delete dynamic symbols to the
9193 lang_append_dynamic_list_cpp_new (void)
9195 const char *symbols
[] =
9200 struct bfd_elf_version_expr
*dynamic
= NULL
;
9203 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
9204 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
9207 lang_append_dynamic_list (dynamic
);
9210 /* Scan a space and/or comma separated string of features. */
9213 lang_ld_feature (char *str
)
9221 while (*p
== ',' || ISSPACE (*p
))
9226 while (*q
&& *q
!= ',' && !ISSPACE (*q
))
9230 if (strcasecmp (p
, "SANE_EXPR") == 0)
9231 config
.sane_expr
= TRUE
;
9233 einfo (_("%X%P: unknown feature `%s'\n"), p
);
9239 /* Pretty print memory amount. */
9242 lang_print_memory_size (bfd_vma sz
)
9244 if ((sz
& 0x3fffffff) == 0)
9245 printf ("%10" BFD_VMA_FMT
"u GB", sz
>> 30);
9246 else if ((sz
& 0xfffff) == 0)
9247 printf ("%10" BFD_VMA_FMT
"u MB", sz
>> 20);
9248 else if ((sz
& 0x3ff) == 0)
9249 printf ("%10" BFD_VMA_FMT
"u KB", sz
>> 10);
9251 printf (" %10" BFD_VMA_FMT
"u B", sz
);
9254 /* Implement --print-memory-usage: disply per region memory usage. */
9257 lang_print_memory_usage (void)
9259 lang_memory_region_type
*r
;
9261 printf ("Memory region Used Size Region Size %%age Used\n");
9262 for (r
= lang_memory_region_list
; r
->next
!= NULL
; r
= r
->next
)
9264 bfd_vma used_length
= r
->current
- r
->origin
;
9267 printf ("%16s: ",r
->name_list
.name
);
9268 lang_print_memory_size (used_length
);
9269 lang_print_memory_size ((bfd_vma
) r
->length
);
9271 percent
= used_length
* 100.0 / r
->length
;
9273 printf (" %6.2f%%\n", percent
);