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. */
23 #include "libiberty.h"
24 #include "filenames.h"
25 #include "safe-ctype.h"
45 #endif /* ENABLE_PLUGINS */
48 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
51 /* Convert between addresses in bytes and sizes in octets.
52 For currently supported targets, octets_per_byte is always a power
53 of two, so we can use shifts. */
54 #define TO_ADDR(X) ((X) >> opb_shift)
55 #define TO_SIZE(X) ((X) << opb_shift)
57 /* Local variables. */
58 static struct obstack stat_obstack
;
59 static struct obstack map_obstack
;
61 #define obstack_chunk_alloc xmalloc
62 #define obstack_chunk_free free
63 static const char *entry_symbol_default
= "start";
64 static bfd_boolean map_head_is_link_order
= FALSE
;
65 static lang_output_section_statement_type
*default_common_section
;
66 static bfd_boolean map_option_f
;
67 static bfd_vma print_dot
;
68 static lang_input_statement_type
*first_file
;
69 static const char *current_target
;
70 /* Header for list of statements corresponding to any files involved in the
71 link, either specified from the command-line or added implicitely (eg.
72 archive member used to resolved undefined symbol, wildcard statement from
73 linker script, etc.). Next pointer is in next field of a
74 lang_statement_header_type (reached via header field in a
75 lang_statement_union). */
76 static lang_statement_list_type statement_list
;
77 static lang_statement_list_type
*stat_save
[10];
78 static lang_statement_list_type
**stat_save_ptr
= &stat_save
[0];
79 static struct unique_sections
*unique_section_list
;
80 static struct asneeded_minfo
*asneeded_list_head
;
81 static unsigned int opb_shift
= 0;
83 /* Forward declarations. */
84 static void exp_init_os (etree_type
*);
85 static lang_input_statement_type
*lookup_name (const char *);
86 static void insert_undefined (const char *);
87 static bfd_boolean
sort_def_symbol (struct bfd_link_hash_entry
*, void *);
88 static void print_statement (lang_statement_union_type
*,
89 lang_output_section_statement_type
*);
90 static void print_statement_list (lang_statement_union_type
*,
91 lang_output_section_statement_type
*);
92 static void print_statements (void);
93 static void print_input_section (asection
*, bfd_boolean
);
94 static bfd_boolean
lang_one_common (struct bfd_link_hash_entry
*, void *);
95 static void lang_record_phdrs (void);
96 static void lang_do_version_exports_section (void);
97 static void lang_finalize_version_expr_head
98 (struct bfd_elf_version_expr_head
*);
99 static void lang_do_memory_regions (void);
101 /* Exported variables. */
102 const char *output_target
;
103 lang_output_section_statement_type
*abs_output_section
;
104 lang_statement_list_type lang_os_list
;
105 lang_statement_list_type
*stat_ptr
= &statement_list
;
106 /* Header for list of statements corresponding to files used in the final
107 executable. This can be either object file specified on the command-line
108 or library member resolving an undefined reference. Next pointer is in next
109 field of a lang_input_statement_type (reached via input_statement field in a
110 lang_statement_union). */
111 lang_statement_list_type file_chain
= { NULL
, NULL
};
112 /* Header for list of statements corresponding to files specified on the
113 command-line for linking. It thus contains real object files and archive
114 but not archive members. Next pointer is in next_real_file field of a
115 lang_input_statement_type statement (reached via input_statement field in a
116 lang_statement_union). */
117 lang_statement_list_type input_file_chain
;
118 struct bfd_sym_chain entry_symbol
= { NULL
, NULL
};
119 const char *entry_section
= ".text";
120 struct lang_input_statement_flags input_flags
;
121 bfd_boolean entry_from_cmdline
;
122 bfd_boolean undef_from_cmdline
;
123 bfd_boolean lang_has_input_file
= FALSE
;
124 bfd_boolean had_output_filename
= FALSE
;
125 bfd_boolean lang_float_flag
= FALSE
;
126 bfd_boolean delete_output_file_on_failure
= FALSE
;
127 struct lang_phdr
*lang_phdr_list
;
128 struct lang_nocrossrefs
*nocrossref_list
;
129 struct asneeded_minfo
**asneeded_list_tail
;
131 /* Functions that traverse the linker script and might evaluate
132 DEFINED() need to increment this at the start of the traversal. */
133 int lang_statement_iteration
= 0;
135 /* Return TRUE if the PATTERN argument is a wildcard pattern.
136 Although backslashes are treated specially if a pattern contains
137 wildcards, we do not consider the mere presence of a backslash to
138 be enough to cause the pattern to be treated as a wildcard.
139 That lets us handle DOS filenames more naturally. */
140 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
142 #define new_stat(x, y) \
143 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
145 #define outside_section_address(q) \
146 ((q)->output_offset + (q)->output_section->vma)
148 #define outside_symbol_address(q) \
149 ((q)->value + outside_section_address (q->section))
151 #define SECTION_NAME_MAP_LENGTH (16)
154 stat_alloc (size_t size
)
156 return obstack_alloc (&stat_obstack
, size
);
160 name_match (const char *pattern
, const char *name
)
162 if (wildcardp (pattern
))
163 return fnmatch (pattern
, name
, 0);
164 return strcmp (pattern
, name
);
167 /* If PATTERN is of the form archive:file, return a pointer to the
168 separator. If not, return NULL. */
171 archive_path (const char *pattern
)
175 if (link_info
.path_separator
== 0)
178 p
= strchr (pattern
, link_info
.path_separator
);
179 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
180 if (p
== NULL
|| link_info
.path_separator
!= ':')
183 /* Assume a match on the second char is part of drive specifier,
184 as in "c:\silly.dos". */
185 if (p
== pattern
+ 1 && ISALPHA (*pattern
))
186 p
= strchr (p
+ 1, link_info
.path_separator
);
191 /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path,
192 return whether F matches FILE_SPEC. */
195 input_statement_is_archive_path (const char *file_spec
, char *sep
,
196 lang_input_statement_type
*f
)
198 bfd_boolean match
= FALSE
;
201 || name_match (sep
+ 1, f
->filename
) == 0)
202 && ((sep
!= file_spec
)
203 == (f
->the_bfd
!= NULL
&& f
->the_bfd
->my_archive
!= NULL
)))
207 if (sep
!= file_spec
)
209 const char *aname
= f
->the_bfd
->my_archive
->filename
;
211 match
= name_match (file_spec
, aname
) == 0;
212 *sep
= link_info
.path_separator
;
219 unique_section_p (const asection
*sec
,
220 const lang_output_section_statement_type
*os
)
222 struct unique_sections
*unam
;
225 if (!link_info
.resolve_section_groups
226 && sec
->owner
!= NULL
227 && bfd_is_group_section (sec
->owner
, sec
))
229 && strcmp (os
->name
, DISCARD_SECTION_NAME
) == 0);
232 for (unam
= unique_section_list
; unam
; unam
= unam
->next
)
233 if (name_match (unam
->name
, secnam
) == 0)
239 /* Generic traversal routines for finding matching sections. */
241 /* Return true if FILE matches a pattern in EXCLUDE_LIST, otherwise return
245 walk_wild_file_in_exclude_list (struct name_list
*exclude_list
,
246 lang_input_statement_type
*file
)
248 struct name_list
*list_tmp
;
250 for (list_tmp
= exclude_list
;
252 list_tmp
= list_tmp
->next
)
254 char *p
= archive_path (list_tmp
->name
);
258 if (input_statement_is_archive_path (list_tmp
->name
, p
, file
))
262 else if (name_match (list_tmp
->name
, file
->filename
) == 0)
265 /* FIXME: Perhaps remove the following at some stage? Matching
266 unadorned archives like this was never documented and has
267 been superceded by the archive:path syntax. */
268 else if (file
->the_bfd
!= NULL
269 && file
->the_bfd
->my_archive
!= NULL
270 && name_match (list_tmp
->name
,
271 file
->the_bfd
->my_archive
->filename
) == 0)
278 /* Try processing a section against a wildcard. This just calls
279 the callback unless the filename exclusion list is present
280 and excludes the file. It's hardly ever present so this
281 function is very fast. */
284 walk_wild_consider_section (lang_wild_statement_type
*ptr
,
285 lang_input_statement_type
*file
,
287 struct wildcard_list
*sec
,
291 /* Don't process sections from files which were excluded. */
292 if (walk_wild_file_in_exclude_list (sec
->spec
.exclude_name_list
, file
))
295 (*callback
) (ptr
, sec
, s
, ptr
->section_flag_list
, file
, data
);
298 /* Lowest common denominator routine that can handle everything correctly,
302 walk_wild_section_general (lang_wild_statement_type
*ptr
,
303 lang_input_statement_type
*file
,
308 struct wildcard_list
*sec
;
310 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
312 sec
= ptr
->section_list
;
314 (*callback
) (ptr
, sec
, s
, ptr
->section_flag_list
, file
, data
);
318 bfd_boolean skip
= FALSE
;
320 if (sec
->spec
.name
!= NULL
)
322 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
324 skip
= name_match (sec
->spec
.name
, sname
) != 0;
328 walk_wild_consider_section (ptr
, file
, s
, sec
, callback
, data
);
335 /* Routines to find a single section given its name. If there's more
336 than one section with that name, we report that. */
340 asection
*found_section
;
341 bfd_boolean multiple_sections_found
;
342 } section_iterator_callback_data
;
345 section_iterator_callback (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*s
, void *data
)
347 section_iterator_callback_data
*d
= (section_iterator_callback_data
*) data
;
349 if (d
->found_section
!= NULL
)
351 d
->multiple_sections_found
= TRUE
;
355 d
->found_section
= s
;
360 find_section (lang_input_statement_type
*file
,
361 struct wildcard_list
*sec
,
362 bfd_boolean
*multiple_sections_found
)
364 section_iterator_callback_data cb_data
= { NULL
, FALSE
};
366 bfd_get_section_by_name_if (file
->the_bfd
, sec
->spec
.name
,
367 section_iterator_callback
, &cb_data
);
368 *multiple_sections_found
= cb_data
.multiple_sections_found
;
369 return cb_data
.found_section
;
372 /* Code for handling simple wildcards without going through fnmatch,
373 which can be expensive because of charset translations etc. */
375 /* A simple wild is a literal string followed by a single '*',
376 where the literal part is at least 4 characters long. */
379 is_simple_wild (const char *name
)
381 size_t len
= strcspn (name
, "*?[");
382 return len
>= 4 && name
[len
] == '*' && name
[len
+ 1] == '\0';
386 match_simple_wild (const char *pattern
, const char *name
)
388 /* The first four characters of the pattern are guaranteed valid
389 non-wildcard characters. So we can go faster. */
390 if (pattern
[0] != name
[0] || pattern
[1] != name
[1]
391 || pattern
[2] != name
[2] || pattern
[3] != name
[3])
396 while (*pattern
!= '*')
397 if (*name
++ != *pattern
++)
403 /* Return the numerical value of the init_priority attribute from
404 section name NAME. */
407 get_init_priority (const char *name
)
410 unsigned long init_priority
;
412 /* GCC uses the following section names for the init_priority
413 attribute with numerical values 101 and 65535 inclusive. A
414 lower value means a higher priority.
416 1: .init_array.NNNN/.fini_array.NNNN: Where NNNN is the
417 decimal numerical value of the init_priority attribute.
418 The order of execution in .init_array is forward and
419 .fini_array is backward.
420 2: .ctors.NNNN/.dtors.NNNN: Where NNNN is 65535 minus the
421 decimal numerical value of the init_priority attribute.
422 The order of execution in .ctors is backward and .dtors
425 if (strncmp (name
, ".init_array.", 12) == 0
426 || strncmp (name
, ".fini_array.", 12) == 0)
428 init_priority
= strtoul (name
+ 12, &end
, 10);
429 return *end
? 0 : init_priority
;
431 else if (strncmp (name
, ".ctors.", 7) == 0
432 || strncmp (name
, ".dtors.", 7) == 0)
434 init_priority
= strtoul (name
+ 7, &end
, 10);
435 return *end
? 0 : 65535 - init_priority
;
441 /* Compare sections ASEC and BSEC according to SORT. */
444 compare_section (sort_type sort
, asection
*asec
, asection
*bsec
)
447 unsigned long ainit_priority
, binit_priority
;
454 case by_init_priority
:
456 = get_init_priority (bfd_get_section_name (asec
->owner
, asec
));
458 = get_init_priority (bfd_get_section_name (bsec
->owner
, bsec
));
459 if (ainit_priority
== 0 || binit_priority
== 0)
461 ret
= ainit_priority
- binit_priority
;
467 case by_alignment_name
:
468 ret
= (bfd_section_alignment (bsec
->owner
, bsec
)
469 - bfd_section_alignment (asec
->owner
, asec
));
476 ret
= strcmp (bfd_get_section_name (asec
->owner
, asec
),
477 bfd_get_section_name (bsec
->owner
, bsec
));
480 case by_name_alignment
:
481 ret
= strcmp (bfd_get_section_name (asec
->owner
, asec
),
482 bfd_get_section_name (bsec
->owner
, bsec
));
488 ret
= (bfd_section_alignment (bsec
->owner
, bsec
)
489 - bfd_section_alignment (asec
->owner
, asec
));
496 /* Build a Binary Search Tree to sort sections, unlike insertion sort
497 used in wild_sort(). BST is considerably faster if the number of
498 of sections are large. */
500 static lang_section_bst_type
**
501 wild_sort_fast (lang_wild_statement_type
*wild
,
502 struct wildcard_list
*sec
,
503 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
506 lang_section_bst_type
**tree
;
509 if (!wild
->filenames_sorted
510 && (sec
== NULL
|| sec
->spec
.sorted
== none
))
512 /* Append at the right end of tree. */
514 tree
= &((*tree
)->right
);
520 /* Find the correct node to append this section. */
521 if (compare_section (sec
->spec
.sorted
, section
, (*tree
)->section
) < 0)
522 tree
= &((*tree
)->left
);
524 tree
= &((*tree
)->right
);
530 /* Use wild_sort_fast to build a BST to sort sections. */
533 output_section_callback_fast (lang_wild_statement_type
*ptr
,
534 struct wildcard_list
*sec
,
536 struct flag_info
*sflag_list ATTRIBUTE_UNUSED
,
537 lang_input_statement_type
*file
,
540 lang_section_bst_type
*node
;
541 lang_section_bst_type
**tree
;
542 lang_output_section_statement_type
*os
;
544 os
= (lang_output_section_statement_type
*) output
;
546 if (unique_section_p (section
, os
))
549 node
= (lang_section_bst_type
*) xmalloc (sizeof (lang_section_bst_type
));
552 node
->section
= section
;
554 tree
= wild_sort_fast (ptr
, sec
, file
, section
);
559 /* Convert a sorted sections' BST back to list form. */
562 output_section_callback_tree_to_list (lang_wild_statement_type
*ptr
,
563 lang_section_bst_type
*tree
,
567 output_section_callback_tree_to_list (ptr
, tree
->left
, output
);
569 lang_add_section (&ptr
->children
, tree
->section
, NULL
,
570 (lang_output_section_statement_type
*) output
);
573 output_section_callback_tree_to_list (ptr
, tree
->right
, output
);
578 /* Specialized, optimized routines for handling different kinds of
582 walk_wild_section_specs1_wild0 (lang_wild_statement_type
*ptr
,
583 lang_input_statement_type
*file
,
587 /* We can just do a hash lookup for the section with the right name.
588 But if that lookup discovers more than one section with the name
589 (should be rare), we fall back to the general algorithm because
590 we would otherwise have to sort the sections to make sure they
591 get processed in the bfd's order. */
592 bfd_boolean multiple_sections_found
;
593 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
594 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
);
596 if (multiple_sections_found
)
597 walk_wild_section_general (ptr
, file
, callback
, data
);
599 walk_wild_consider_section (ptr
, file
, s0
, sec0
, callback
, data
);
603 walk_wild_section_specs1_wild1 (lang_wild_statement_type
*ptr
,
604 lang_input_statement_type
*file
,
609 struct wildcard_list
*wildsec0
= ptr
->handler_data
[0];
611 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
613 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
614 bfd_boolean skip
= !match_simple_wild (wildsec0
->spec
.name
, sname
);
617 walk_wild_consider_section (ptr
, file
, s
, wildsec0
, callback
, data
);
622 walk_wild_section_specs2_wild1 (lang_wild_statement_type
*ptr
,
623 lang_input_statement_type
*file
,
628 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
629 struct wildcard_list
*wildsec1
= ptr
->handler_data
[1];
630 bfd_boolean multiple_sections_found
;
631 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
);
633 if (multiple_sections_found
)
635 walk_wild_section_general (ptr
, file
, callback
, data
);
639 /* Note that if the section was not found, s0 is NULL and
640 we'll simply never succeed the s == s0 test below. */
641 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
643 /* Recall that in this code path, a section cannot satisfy more
644 than one spec, so if s == s0 then it cannot match
647 walk_wild_consider_section (ptr
, file
, s
, sec0
, callback
, data
);
650 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
651 bfd_boolean skip
= !match_simple_wild (wildsec1
->spec
.name
, sname
);
654 walk_wild_consider_section (ptr
, file
, s
, wildsec1
, callback
,
661 walk_wild_section_specs3_wild2 (lang_wild_statement_type
*ptr
,
662 lang_input_statement_type
*file
,
667 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
668 struct wildcard_list
*wildsec1
= ptr
->handler_data
[1];
669 struct wildcard_list
*wildsec2
= ptr
->handler_data
[2];
670 bfd_boolean multiple_sections_found
;
671 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
);
673 if (multiple_sections_found
)
675 walk_wild_section_general (ptr
, file
, callback
, data
);
679 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
682 walk_wild_consider_section (ptr
, file
, s
, sec0
, callback
, data
);
685 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
686 bfd_boolean skip
= !match_simple_wild (wildsec1
->spec
.name
, sname
);
689 walk_wild_consider_section (ptr
, file
, s
, wildsec1
, callback
, data
);
692 skip
= !match_simple_wild (wildsec2
->spec
.name
, sname
);
694 walk_wild_consider_section (ptr
, file
, s
, wildsec2
, callback
,
702 walk_wild_section_specs4_wild2 (lang_wild_statement_type
*ptr
,
703 lang_input_statement_type
*file
,
708 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
709 struct wildcard_list
*sec1
= ptr
->handler_data
[1];
710 struct wildcard_list
*wildsec2
= ptr
->handler_data
[2];
711 struct wildcard_list
*wildsec3
= ptr
->handler_data
[3];
712 bfd_boolean multiple_sections_found
;
713 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
), *s1
;
715 if (multiple_sections_found
)
717 walk_wild_section_general (ptr
, file
, callback
, data
);
721 s1
= find_section (file
, sec1
, &multiple_sections_found
);
722 if (multiple_sections_found
)
724 walk_wild_section_general (ptr
, file
, callback
, data
);
728 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
731 walk_wild_consider_section (ptr
, file
, s
, sec0
, callback
, data
);
734 walk_wild_consider_section (ptr
, file
, s
, sec1
, callback
, data
);
737 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
738 bfd_boolean skip
= !match_simple_wild (wildsec2
->spec
.name
,
742 walk_wild_consider_section (ptr
, file
, s
, wildsec2
, callback
,
746 skip
= !match_simple_wild (wildsec3
->spec
.name
, sname
);
748 walk_wild_consider_section (ptr
, file
, s
, wildsec3
,
756 walk_wild_section (lang_wild_statement_type
*ptr
,
757 lang_input_statement_type
*file
,
761 if (file
->flags
.just_syms
)
764 (*ptr
->walk_wild_section_handler
) (ptr
, file
, callback
, data
);
767 /* Returns TRUE when name1 is a wildcard spec that might match
768 something name2 can match. We're conservative: we return FALSE
769 only if the prefixes of name1 and name2 are different up to the
770 first wildcard character. */
773 wild_spec_can_overlap (const char *name1
, const char *name2
)
775 size_t prefix1_len
= strcspn (name1
, "?*[");
776 size_t prefix2_len
= strcspn (name2
, "?*[");
777 size_t min_prefix_len
;
779 /* Note that if there is no wildcard character, then we treat the
780 terminating 0 as part of the prefix. Thus ".text" won't match
781 ".text." or ".text.*", for example. */
782 if (name1
[prefix1_len
] == '\0')
784 if (name2
[prefix2_len
] == '\0')
787 min_prefix_len
= prefix1_len
< prefix2_len
? prefix1_len
: prefix2_len
;
789 return memcmp (name1
, name2
, min_prefix_len
) == 0;
792 /* Select specialized code to handle various kinds of wildcard
796 analyze_walk_wild_section_handler (lang_wild_statement_type
*ptr
)
799 int wild_name_count
= 0;
800 struct wildcard_list
*sec
;
804 ptr
->walk_wild_section_handler
= walk_wild_section_general
;
805 ptr
->handler_data
[0] = NULL
;
806 ptr
->handler_data
[1] = NULL
;
807 ptr
->handler_data
[2] = NULL
;
808 ptr
->handler_data
[3] = NULL
;
811 /* Count how many wildcard_specs there are, and how many of those
812 actually use wildcards in the name. Also, bail out if any of the
813 wildcard names are NULL. (Can this actually happen?
814 walk_wild_section used to test for it.) And bail out if any
815 of the wildcards are more complex than a simple string
816 ending in a single '*'. */
817 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
820 if (sec
->spec
.name
== NULL
)
822 if (wildcardp (sec
->spec
.name
))
825 if (!is_simple_wild (sec
->spec
.name
))
830 /* The zero-spec case would be easy to optimize but it doesn't
831 happen in practice. Likewise, more than 4 specs doesn't
832 happen in practice. */
833 if (sec_count
== 0 || sec_count
> 4)
836 /* Check that no two specs can match the same section. */
837 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
839 struct wildcard_list
*sec2
;
840 for (sec2
= sec
->next
; sec2
!= NULL
; sec2
= sec2
->next
)
842 if (wild_spec_can_overlap (sec
->spec
.name
, sec2
->spec
.name
))
847 signature
= (sec_count
<< 8) + wild_name_count
;
851 ptr
->walk_wild_section_handler
= walk_wild_section_specs1_wild0
;
854 ptr
->walk_wild_section_handler
= walk_wild_section_specs1_wild1
;
857 ptr
->walk_wild_section_handler
= walk_wild_section_specs2_wild1
;
860 ptr
->walk_wild_section_handler
= walk_wild_section_specs3_wild2
;
863 ptr
->walk_wild_section_handler
= walk_wild_section_specs4_wild2
;
869 /* Now fill the data array with pointers to the specs, first the
870 specs with non-wildcard names, then the specs with wildcard
871 names. It's OK to process the specs in different order from the
872 given order, because we've already determined that no section
873 will match more than one spec. */
875 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
876 if (!wildcardp (sec
->spec
.name
))
877 ptr
->handler_data
[data_counter
++] = sec
;
878 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
879 if (wildcardp (sec
->spec
.name
))
880 ptr
->handler_data
[data_counter
++] = sec
;
883 /* Handle a wild statement for a single file F. */
886 walk_wild_file (lang_wild_statement_type
*s
,
887 lang_input_statement_type
*f
,
891 if (walk_wild_file_in_exclude_list (s
->exclude_name_list
, f
))
894 if (f
->the_bfd
== NULL
895 || !bfd_check_format (f
->the_bfd
, bfd_archive
))
896 walk_wild_section (s
, f
, callback
, data
);
901 /* This is an archive file. We must map each member of the
902 archive separately. */
903 member
= bfd_openr_next_archived_file (f
->the_bfd
, NULL
);
904 while (member
!= NULL
)
906 /* When lookup_name is called, it will call the add_symbols
907 entry point for the archive. For each element of the
908 archive which is included, BFD will call ldlang_add_file,
909 which will set the usrdata field of the member to the
910 lang_input_statement. */
911 if (member
->usrdata
!= NULL
)
913 walk_wild_section (s
,
914 (lang_input_statement_type
*) member
->usrdata
,
918 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
924 walk_wild (lang_wild_statement_type
*s
, callback_t callback
, void *data
)
926 const char *file_spec
= s
->filename
;
929 if (file_spec
== NULL
)
931 /* Perform the iteration over all files in the list. */
932 LANG_FOR_EACH_INPUT_STATEMENT (f
)
934 walk_wild_file (s
, f
, callback
, data
);
937 else if ((p
= archive_path (file_spec
)) != NULL
)
939 LANG_FOR_EACH_INPUT_STATEMENT (f
)
941 if (input_statement_is_archive_path (file_spec
, p
, f
))
942 walk_wild_file (s
, f
, callback
, data
);
945 else if (wildcardp (file_spec
))
947 LANG_FOR_EACH_INPUT_STATEMENT (f
)
949 if (fnmatch (file_spec
, f
->filename
, 0) == 0)
950 walk_wild_file (s
, f
, callback
, data
);
955 lang_input_statement_type
*f
;
957 /* Perform the iteration over a single file. */
958 f
= lookup_name (file_spec
);
960 walk_wild_file (s
, f
, callback
, data
);
964 /* lang_for_each_statement walks the parse tree and calls the provided
965 function for each node, except those inside output section statements
966 with constraint set to -1. */
969 lang_for_each_statement_worker (void (*func
) (lang_statement_union_type
*),
970 lang_statement_union_type
*s
)
972 for (; s
!= NULL
; s
= s
->header
.next
)
976 switch (s
->header
.type
)
978 case lang_constructors_statement_enum
:
979 lang_for_each_statement_worker (func
, constructor_list
.head
);
981 case lang_output_section_statement_enum
:
982 if (s
->output_section_statement
.constraint
!= -1)
983 lang_for_each_statement_worker
984 (func
, s
->output_section_statement
.children
.head
);
986 case lang_wild_statement_enum
:
987 lang_for_each_statement_worker (func
,
988 s
->wild_statement
.children
.head
);
990 case lang_group_statement_enum
:
991 lang_for_each_statement_worker (func
,
992 s
->group_statement
.children
.head
);
994 case lang_data_statement_enum
:
995 case lang_reloc_statement_enum
:
996 case lang_object_symbols_statement_enum
:
997 case lang_output_statement_enum
:
998 case lang_target_statement_enum
:
999 case lang_input_section_enum
:
1000 case lang_input_statement_enum
:
1001 case lang_assignment_statement_enum
:
1002 case lang_padding_statement_enum
:
1003 case lang_address_statement_enum
:
1004 case lang_fill_statement_enum
:
1005 case lang_insert_statement_enum
:
1015 lang_for_each_statement (void (*func
) (lang_statement_union_type
*))
1017 lang_for_each_statement_worker (func
, statement_list
.head
);
1020 /*----------------------------------------------------------------------*/
1023 lang_list_init (lang_statement_list_type
*list
)
1026 list
->tail
= &list
->head
;
1030 lang_statement_append (lang_statement_list_type
*list
,
1034 *(list
->tail
) = element
;
1039 push_stat_ptr (lang_statement_list_type
*new_ptr
)
1041 if (stat_save_ptr
>= stat_save
+ sizeof (stat_save
) / sizeof (stat_save
[0]))
1043 *stat_save_ptr
++ = stat_ptr
;
1050 if (stat_save_ptr
<= stat_save
)
1052 stat_ptr
= *--stat_save_ptr
;
1055 /* Build a new statement node for the parse tree. */
1057 static lang_statement_union_type
*
1058 new_statement (enum statement_enum type
,
1060 lang_statement_list_type
*list
)
1062 lang_statement_union_type
*new_stmt
;
1064 new_stmt
= stat_alloc (size
);
1065 new_stmt
->header
.type
= type
;
1066 new_stmt
->header
.next
= NULL
;
1067 lang_statement_append (list
, new_stmt
, &new_stmt
->header
.next
);
1071 /* Build a new input file node for the language. There are several
1072 ways in which we treat an input file, eg, we only look at symbols,
1073 or prefix it with a -l etc.
1075 We can be supplied with requests for input files more than once;
1076 they may, for example be split over several lines like foo.o(.text)
1077 foo.o(.data) etc, so when asked for a file we check that we haven't
1078 got it already so we don't duplicate the bfd. */
1080 static lang_input_statement_type
*
1081 new_afile (const char *name
,
1082 lang_input_file_enum_type file_type
,
1084 bfd_boolean add_to_list
)
1086 lang_input_statement_type
*p
;
1088 lang_has_input_file
= TRUE
;
1091 p
= new_stat (lang_input_statement
, stat_ptr
);
1094 p
= stat_alloc (sizeof (lang_input_statement_type
));
1095 p
->header
.type
= lang_input_statement_enum
;
1096 p
->header
.next
= NULL
;
1099 memset (&p
->the_bfd
, 0,
1100 sizeof (*p
) - offsetof (lang_input_statement_type
, the_bfd
));
1102 p
->flags
.dynamic
= input_flags
.dynamic
;
1103 p
->flags
.add_DT_NEEDED_for_dynamic
= input_flags
.add_DT_NEEDED_for_dynamic
;
1104 p
->flags
.add_DT_NEEDED_for_regular
= input_flags
.add_DT_NEEDED_for_regular
;
1105 p
->flags
.whole_archive
= input_flags
.whole_archive
;
1106 p
->flags
.sysrooted
= input_flags
.sysrooted
;
1110 case lang_input_file_is_symbols_only_enum
:
1112 p
->local_sym_name
= name
;
1113 p
->flags
.real
= TRUE
;
1114 p
->flags
.just_syms
= TRUE
;
1116 case lang_input_file_is_fake_enum
:
1118 p
->local_sym_name
= name
;
1120 case lang_input_file_is_l_enum
:
1121 if (name
[0] == ':' && name
[1] != '\0')
1123 p
->filename
= name
+ 1;
1124 p
->flags
.full_name_provided
= TRUE
;
1128 p
->local_sym_name
= concat ("-l", name
, (const char *) NULL
);
1129 p
->flags
.maybe_archive
= TRUE
;
1130 p
->flags
.real
= TRUE
;
1131 p
->flags
.search_dirs
= TRUE
;
1133 case lang_input_file_is_marker_enum
:
1135 p
->local_sym_name
= name
;
1136 p
->flags
.search_dirs
= TRUE
;
1138 case lang_input_file_is_search_file_enum
:
1140 p
->local_sym_name
= name
;
1141 p
->flags
.real
= TRUE
;
1142 p
->flags
.search_dirs
= TRUE
;
1144 case lang_input_file_is_file_enum
:
1146 p
->local_sym_name
= name
;
1147 p
->flags
.real
= TRUE
;
1153 lang_statement_append (&input_file_chain
, p
, &p
->next_real_file
);
1157 lang_input_statement_type
*
1158 lang_add_input_file (const char *name
,
1159 lang_input_file_enum_type file_type
,
1163 && (*name
== '=' || CONST_STRNEQ (name
, "$SYSROOT")))
1165 lang_input_statement_type
*ret
;
1166 char *sysrooted_name
1167 = concat (ld_sysroot
,
1168 name
+ (*name
== '=' ? 1 : strlen ("$SYSROOT")),
1169 (const char *) NULL
);
1171 /* We've now forcibly prepended the sysroot, making the input
1172 file independent of the context. Therefore, temporarily
1173 force a non-sysrooted context for this statement, so it won't
1174 get the sysroot prepended again when opened. (N.B. if it's a
1175 script, any child nodes with input files starting with "/"
1176 will be handled as "sysrooted" as they'll be found to be
1177 within the sysroot subdirectory.) */
1178 unsigned int outer_sysrooted
= input_flags
.sysrooted
;
1179 input_flags
.sysrooted
= 0;
1180 ret
= new_afile (sysrooted_name
, file_type
, target
, TRUE
);
1181 input_flags
.sysrooted
= outer_sysrooted
;
1185 return new_afile (name
, file_type
, target
, TRUE
);
1188 struct out_section_hash_entry
1190 struct bfd_hash_entry root
;
1191 lang_statement_union_type s
;
1194 /* The hash table. */
1196 static struct bfd_hash_table output_section_statement_table
;
1198 /* Support routines for the hash table used by lang_output_section_find,
1199 initialize the table, fill in an entry and remove the table. */
1201 static struct bfd_hash_entry
*
1202 output_section_statement_newfunc (struct bfd_hash_entry
*entry
,
1203 struct bfd_hash_table
*table
,
1206 lang_output_section_statement_type
**nextp
;
1207 struct out_section_hash_entry
*ret
;
1211 entry
= (struct bfd_hash_entry
*) bfd_hash_allocate (table
,
1217 entry
= bfd_hash_newfunc (entry
, table
, string
);
1221 ret
= (struct out_section_hash_entry
*) entry
;
1222 memset (&ret
->s
, 0, sizeof (ret
->s
));
1223 ret
->s
.header
.type
= lang_output_section_statement_enum
;
1224 ret
->s
.output_section_statement
.subsection_alignment
= NULL
;
1225 ret
->s
.output_section_statement
.section_alignment
= NULL
;
1226 ret
->s
.output_section_statement
.block_value
= 1;
1227 lang_list_init (&ret
->s
.output_section_statement
.children
);
1228 lang_statement_append (stat_ptr
, &ret
->s
, &ret
->s
.header
.next
);
1230 /* For every output section statement added to the list, except the
1231 first one, lang_os_list.tail points to the "next"
1232 field of the last element of the list. */
1233 if (lang_os_list
.head
!= NULL
)
1234 ret
->s
.output_section_statement
.prev
1235 = ((lang_output_section_statement_type
*)
1236 ((char *) lang_os_list
.tail
1237 - offsetof (lang_output_section_statement_type
, next
)));
1239 /* GCC's strict aliasing rules prevent us from just casting the
1240 address, so we store the pointer in a variable and cast that
1242 nextp
= &ret
->s
.output_section_statement
.next
;
1243 lang_statement_append (&lang_os_list
, &ret
->s
, nextp
);
1248 output_section_statement_table_init (void)
1250 if (!bfd_hash_table_init_n (&output_section_statement_table
,
1251 output_section_statement_newfunc
,
1252 sizeof (struct out_section_hash_entry
),
1254 einfo (_("%F%P: can not create hash table: %E\n"));
1258 output_section_statement_table_free (void)
1260 bfd_hash_table_free (&output_section_statement_table
);
1263 /* Build enough state so that the parser can build its tree. */
1268 obstack_begin (&stat_obstack
, 1000);
1270 stat_ptr
= &statement_list
;
1272 output_section_statement_table_init ();
1274 lang_list_init (stat_ptr
);
1276 lang_list_init (&input_file_chain
);
1277 lang_list_init (&lang_os_list
);
1278 lang_list_init (&file_chain
);
1279 first_file
= lang_add_input_file (NULL
, lang_input_file_is_marker_enum
,
1281 abs_output_section
=
1282 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
, 0, TRUE
);
1284 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
1286 asneeded_list_head
= NULL
;
1287 asneeded_list_tail
= &asneeded_list_head
;
1293 output_section_statement_table_free ();
1296 /*----------------------------------------------------------------------
1297 A region is an area of memory declared with the
1298 MEMORY { name:org=exp, len=exp ... }
1301 We maintain a list of all the regions here.
1303 If no regions are specified in the script, then the default is used
1304 which is created when looked up to be the entire data space.
1306 If create is true we are creating a region inside a MEMORY block.
1307 In this case it is probably an error to create a region that has
1308 already been created. If we are not inside a MEMORY block it is
1309 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
1310 and so we issue a warning.
1312 Each region has at least one name. The first name is either
1313 DEFAULT_MEMORY_REGION or the name given in the MEMORY block. You can add
1314 alias names to an existing region within a script with
1315 REGION_ALIAS (alias, region_name). Each name corresponds to at most one
1318 static lang_memory_region_type
*lang_memory_region_list
;
1319 static lang_memory_region_type
**lang_memory_region_list_tail
1320 = &lang_memory_region_list
;
1322 lang_memory_region_type
*
1323 lang_memory_region_lookup (const char *const name
, bfd_boolean create
)
1325 lang_memory_region_name
*n
;
1326 lang_memory_region_type
*r
;
1327 lang_memory_region_type
*new_region
;
1329 /* NAME is NULL for LMA memspecs if no region was specified. */
1333 for (r
= lang_memory_region_list
; r
!= NULL
; r
= r
->next
)
1334 for (n
= &r
->name_list
; n
!= NULL
; n
= n
->next
)
1335 if (strcmp (n
->name
, name
) == 0)
1338 einfo (_("%P:%pS: warning: redeclaration of memory region `%s'\n"),
1343 if (!create
&& strcmp (name
, DEFAULT_MEMORY_REGION
))
1344 einfo (_("%P:%pS: warning: memory region `%s' not declared\n"),
1347 new_region
= stat_alloc (sizeof (lang_memory_region_type
));
1349 new_region
->name_list
.name
= xstrdup (name
);
1350 new_region
->name_list
.next
= NULL
;
1351 new_region
->next
= NULL
;
1352 new_region
->origin_exp
= NULL
;
1353 new_region
->origin
= 0;
1354 new_region
->length_exp
= NULL
;
1355 new_region
->length
= ~(bfd_size_type
) 0;
1356 new_region
->current
= 0;
1357 new_region
->last_os
= NULL
;
1358 new_region
->flags
= 0;
1359 new_region
->not_flags
= 0;
1360 new_region
->had_full_message
= FALSE
;
1362 *lang_memory_region_list_tail
= new_region
;
1363 lang_memory_region_list_tail
= &new_region
->next
;
1369 lang_memory_region_alias (const char *alias
, const char *region_name
)
1371 lang_memory_region_name
*n
;
1372 lang_memory_region_type
*r
;
1373 lang_memory_region_type
*region
;
1375 /* The default region must be unique. This ensures that it is not necessary
1376 to iterate through the name list if someone wants the check if a region is
1377 the default memory region. */
1378 if (strcmp (region_name
, DEFAULT_MEMORY_REGION
) == 0
1379 || strcmp (alias
, DEFAULT_MEMORY_REGION
) == 0)
1380 einfo (_("%F%P:%pS: error: alias for default memory region\n"), NULL
);
1382 /* Look for the target region and check if the alias is not already
1385 for (r
= lang_memory_region_list
; r
!= NULL
; r
= r
->next
)
1386 for (n
= &r
->name_list
; n
!= NULL
; n
= n
->next
)
1388 if (region
== NULL
&& strcmp (n
->name
, region_name
) == 0)
1390 if (strcmp (n
->name
, alias
) == 0)
1391 einfo (_("%F%P:%pS: error: redefinition of memory region "
1396 /* Check if the target region exists. */
1398 einfo (_("%F%P:%pS: error: memory region `%s' "
1399 "for alias `%s' does not exist\n"),
1400 NULL
, region_name
, alias
);
1402 /* Add alias to region name list. */
1403 n
= stat_alloc (sizeof (lang_memory_region_name
));
1404 n
->name
= xstrdup (alias
);
1405 n
->next
= region
->name_list
.next
;
1406 region
->name_list
.next
= n
;
1409 static lang_memory_region_type
*
1410 lang_memory_default (asection
*section
)
1412 lang_memory_region_type
*p
;
1414 flagword sec_flags
= section
->flags
;
1416 /* Override SEC_DATA to mean a writable section. */
1417 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
1418 sec_flags
|= SEC_DATA
;
1420 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
1422 if ((p
->flags
& sec_flags
) != 0
1423 && (p
->not_flags
& sec_flags
) == 0)
1428 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, FALSE
);
1431 /* Get the output section statement directly from the userdata. */
1433 lang_output_section_statement_type
*
1434 lang_output_section_get (const asection
*output_section
)
1436 return get_userdata (output_section
);
1439 /* Find or create an output_section_statement with the given NAME.
1440 If CONSTRAINT is non-zero match one with that constraint, otherwise
1441 match any non-negative constraint. If CREATE, always make a
1442 new output_section_statement for SPECIAL CONSTRAINT. */
1444 lang_output_section_statement_type
*
1445 lang_output_section_statement_lookup (const char *name
,
1449 struct out_section_hash_entry
*entry
;
1451 entry
= ((struct out_section_hash_entry
*)
1452 bfd_hash_lookup (&output_section_statement_table
, name
,
1457 einfo (_("%F%P: failed creating section `%s': %E\n"), name
);
1461 if (entry
->s
.output_section_statement
.name
!= NULL
)
1463 /* We have a section of this name, but it might not have the correct
1465 struct out_section_hash_entry
*last_ent
;
1467 name
= entry
->s
.output_section_statement
.name
;
1468 if (create
&& constraint
== SPECIAL
)
1469 /* Not traversing to the end reverses the order of the second
1470 and subsequent SPECIAL sections in the hash table chain,
1471 but that shouldn't matter. */
1476 if (constraint
== entry
->s
.output_section_statement
.constraint
1478 && entry
->s
.output_section_statement
.constraint
>= 0))
1479 return &entry
->s
.output_section_statement
;
1481 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1483 while (entry
!= NULL
1484 && name
== entry
->s
.output_section_statement
.name
);
1490 = ((struct out_section_hash_entry
*)
1491 output_section_statement_newfunc (NULL
,
1492 &output_section_statement_table
,
1496 einfo (_("%F%P: failed creating section `%s': %E\n"), name
);
1499 entry
->root
= last_ent
->root
;
1500 last_ent
->root
.next
= &entry
->root
;
1503 entry
->s
.output_section_statement
.name
= name
;
1504 entry
->s
.output_section_statement
.constraint
= constraint
;
1505 return &entry
->s
.output_section_statement
;
1508 /* Find the next output_section_statement with the same name as OS.
1509 If CONSTRAINT is non-zero, find one with that constraint otherwise
1510 match any non-negative constraint. */
1512 lang_output_section_statement_type
*
1513 next_matching_output_section_statement (lang_output_section_statement_type
*os
,
1516 /* All output_section_statements are actually part of a
1517 struct out_section_hash_entry. */
1518 struct out_section_hash_entry
*entry
= (struct out_section_hash_entry
*)
1520 - offsetof (struct out_section_hash_entry
, s
.output_section_statement
));
1521 const char *name
= os
->name
;
1523 ASSERT (name
== entry
->root
.string
);
1526 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1528 || name
!= entry
->s
.output_section_statement
.name
)
1531 while (constraint
!= entry
->s
.output_section_statement
.constraint
1533 || entry
->s
.output_section_statement
.constraint
< 0));
1535 return &entry
->s
.output_section_statement
;
1538 /* A variant of lang_output_section_find used by place_orphan.
1539 Returns the output statement that should precede a new output
1540 statement for SEC. If an exact match is found on certain flags,
1543 lang_output_section_statement_type
*
1544 lang_output_section_find_by_flags (const asection
*sec
,
1546 lang_output_section_statement_type
**exact
,
1547 lang_match_sec_type_func match_type
)
1549 lang_output_section_statement_type
*first
, *look
, *found
;
1550 flagword look_flags
, differ
;
1552 /* We know the first statement on this list is *ABS*. May as well
1554 first
= &lang_os_list
.head
->output_section_statement
;
1555 first
= first
->next
;
1557 /* First try for an exact match. */
1559 for (look
= first
; look
; look
= look
->next
)
1561 look_flags
= look
->flags
;
1562 if (look
->bfd_section
!= NULL
)
1564 look_flags
= look
->bfd_section
->flags
;
1565 if (match_type
&& !match_type (link_info
.output_bfd
,
1570 differ
= look_flags
^ sec_flags
;
1571 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
1572 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1582 if ((sec_flags
& SEC_CODE
) != 0
1583 && (sec_flags
& SEC_ALLOC
) != 0)
1585 /* Try for a rw code section. */
1586 for (look
= first
; look
; look
= look
->next
)
1588 look_flags
= look
->flags
;
1589 if (look
->bfd_section
!= NULL
)
1591 look_flags
= look
->bfd_section
->flags
;
1592 if (match_type
&& !match_type (link_info
.output_bfd
,
1597 differ
= look_flags
^ sec_flags
;
1598 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1599 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1603 else if ((sec_flags
& SEC_READONLY
) != 0
1604 && (sec_flags
& SEC_ALLOC
) != 0)
1606 /* .rodata can go after .text, .sdata2 after .rodata. */
1607 for (look
= first
; look
; look
= look
->next
)
1609 look_flags
= look
->flags
;
1610 if (look
->bfd_section
!= NULL
)
1612 look_flags
= look
->bfd_section
->flags
;
1613 if (match_type
&& !match_type (link_info
.output_bfd
,
1618 differ
= look_flags
^ sec_flags
;
1619 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1620 | SEC_READONLY
| SEC_SMALL_DATA
))
1621 || (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1623 && !(look_flags
& SEC_SMALL_DATA
)))
1627 else if ((sec_flags
& SEC_THREAD_LOCAL
) != 0
1628 && (sec_flags
& SEC_ALLOC
) != 0)
1630 /* .tdata can go after .data, .tbss after .tdata. Treat .tbss
1631 as if it were a loaded section, and don't use match_type. */
1632 bfd_boolean seen_thread_local
= FALSE
;
1635 for (look
= first
; look
; look
= look
->next
)
1637 look_flags
= look
->flags
;
1638 if (look
->bfd_section
!= NULL
)
1639 look_flags
= look
->bfd_section
->flags
;
1641 differ
= look_flags
^ (sec_flags
| SEC_LOAD
| SEC_HAS_CONTENTS
);
1642 if (!(differ
& (SEC_THREAD_LOCAL
| SEC_ALLOC
)))
1644 /* .tdata and .tbss must be adjacent and in that order. */
1645 if (!(look_flags
& SEC_LOAD
)
1646 && (sec_flags
& SEC_LOAD
))
1647 /* ..so if we're at a .tbss section and we're placing
1648 a .tdata section stop looking and return the
1649 previous section. */
1652 seen_thread_local
= TRUE
;
1654 else if (seen_thread_local
)
1656 else if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
)))
1660 else if ((sec_flags
& SEC_SMALL_DATA
) != 0
1661 && (sec_flags
& SEC_ALLOC
) != 0)
1663 /* .sdata goes after .data, .sbss after .sdata. */
1664 for (look
= first
; look
; look
= look
->next
)
1666 look_flags
= look
->flags
;
1667 if (look
->bfd_section
!= NULL
)
1669 look_flags
= look
->bfd_section
->flags
;
1670 if (match_type
&& !match_type (link_info
.output_bfd
,
1675 differ
= look_flags
^ sec_flags
;
1676 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1677 | SEC_THREAD_LOCAL
))
1678 || ((look_flags
& SEC_SMALL_DATA
)
1679 && !(sec_flags
& SEC_HAS_CONTENTS
)))
1683 else if ((sec_flags
& SEC_HAS_CONTENTS
) != 0
1684 && (sec_flags
& SEC_ALLOC
) != 0)
1686 /* .data goes after .rodata. */
1687 for (look
= first
; look
; look
= look
->next
)
1689 look_flags
= look
->flags
;
1690 if (look
->bfd_section
!= NULL
)
1692 look_flags
= look
->bfd_section
->flags
;
1693 if (match_type
&& !match_type (link_info
.output_bfd
,
1698 differ
= look_flags
^ sec_flags
;
1699 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1700 | SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1704 else if ((sec_flags
& SEC_ALLOC
) != 0)
1706 /* .bss goes after any other alloc section. */
1707 for (look
= first
; look
; look
= look
->next
)
1709 look_flags
= look
->flags
;
1710 if (look
->bfd_section
!= NULL
)
1712 look_flags
= look
->bfd_section
->flags
;
1713 if (match_type
&& !match_type (link_info
.output_bfd
,
1718 differ
= look_flags
^ sec_flags
;
1719 if (!(differ
& SEC_ALLOC
))
1725 /* non-alloc go last. */
1726 for (look
= first
; look
; look
= look
->next
)
1728 look_flags
= look
->flags
;
1729 if (look
->bfd_section
!= NULL
)
1730 look_flags
= look
->bfd_section
->flags
;
1731 differ
= look_flags
^ sec_flags
;
1732 if (!(differ
& SEC_DEBUGGING
))
1738 if (found
|| !match_type
)
1741 return lang_output_section_find_by_flags (sec
, sec_flags
, NULL
, NULL
);
1744 /* Find the last output section before given output statement.
1745 Used by place_orphan. */
1748 output_prev_sec_find (lang_output_section_statement_type
*os
)
1750 lang_output_section_statement_type
*lookup
;
1752 for (lookup
= os
->prev
; lookup
!= NULL
; lookup
= lookup
->prev
)
1754 if (lookup
->constraint
< 0)
1757 if (lookup
->bfd_section
!= NULL
&& lookup
->bfd_section
->owner
!= NULL
)
1758 return lookup
->bfd_section
;
1764 /* Look for a suitable place for a new output section statement. The
1765 idea is to skip over anything that might be inside a SECTIONS {}
1766 statement in a script, before we find another output section
1767 statement. Assignments to "dot" before an output section statement
1768 are assumed to belong to it, except in two cases; The first
1769 assignment to dot, and assignments before non-alloc sections.
1770 Otherwise we might put an orphan before . = . + SIZEOF_HEADERS or
1771 similar assignments that set the initial address, or we might
1772 insert non-alloc note sections among assignments setting end of
1775 static lang_statement_union_type
**
1776 insert_os_after (lang_output_section_statement_type
*after
)
1778 lang_statement_union_type
**where
;
1779 lang_statement_union_type
**assign
= NULL
;
1780 bfd_boolean ignore_first
;
1782 ignore_first
= after
== &lang_os_list
.head
->output_section_statement
;
1784 for (where
= &after
->header
.next
;
1786 where
= &(*where
)->header
.next
)
1788 switch ((*where
)->header
.type
)
1790 case lang_assignment_statement_enum
:
1793 lang_assignment_statement_type
*ass
;
1795 ass
= &(*where
)->assignment_statement
;
1796 if (ass
->exp
->type
.node_class
!= etree_assert
1797 && ass
->exp
->assign
.dst
[0] == '.'
1798 && ass
->exp
->assign
.dst
[1] == 0)
1802 ignore_first
= FALSE
;
1806 case lang_wild_statement_enum
:
1807 case lang_input_section_enum
:
1808 case lang_object_symbols_statement_enum
:
1809 case lang_fill_statement_enum
:
1810 case lang_data_statement_enum
:
1811 case lang_reloc_statement_enum
:
1812 case lang_padding_statement_enum
:
1813 case lang_constructors_statement_enum
:
1815 ignore_first
= FALSE
;
1817 case lang_output_section_statement_enum
:
1820 asection
*s
= (*where
)->output_section_statement
.bfd_section
;
1823 || s
->map_head
.s
== NULL
1824 || (s
->flags
& SEC_ALLOC
) != 0)
1828 case lang_input_statement_enum
:
1829 case lang_address_statement_enum
:
1830 case lang_target_statement_enum
:
1831 case lang_output_statement_enum
:
1832 case lang_group_statement_enum
:
1833 case lang_insert_statement_enum
:
1842 lang_output_section_statement_type
*
1843 lang_insert_orphan (asection
*s
,
1844 const char *secname
,
1846 lang_output_section_statement_type
*after
,
1847 struct orphan_save
*place
,
1848 etree_type
*address
,
1849 lang_statement_list_type
*add_child
)
1851 lang_statement_list_type add
;
1852 lang_output_section_statement_type
*os
;
1853 lang_output_section_statement_type
**os_tail
;
1855 /* If we have found an appropriate place for the output section
1856 statements for this orphan, add them to our own private list,
1857 inserting them later into the global statement list. */
1860 lang_list_init (&add
);
1861 push_stat_ptr (&add
);
1864 if (bfd_link_relocatable (&link_info
)
1865 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0)
1866 address
= exp_intop (0);
1868 os_tail
= (lang_output_section_statement_type
**) lang_os_list
.tail
;
1869 os
= lang_enter_output_section_statement (secname
, address
, normal_section
,
1870 NULL
, NULL
, NULL
, constraint
, 0);
1872 if (add_child
== NULL
)
1873 add_child
= &os
->children
;
1874 lang_add_section (add_child
, s
, NULL
, os
);
1876 if (after
&& (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) != 0)
1878 const char *region
= (after
->region
1879 ? after
->region
->name_list
.name
1880 : DEFAULT_MEMORY_REGION
);
1881 const char *lma_region
= (after
->lma_region
1882 ? after
->lma_region
->name_list
.name
1884 lang_leave_output_section_statement (NULL
, region
, after
->phdrs
,
1888 lang_leave_output_section_statement (NULL
, DEFAULT_MEMORY_REGION
, NULL
,
1891 /* Restore the global list pointer. */
1895 if (after
!= NULL
&& os
->bfd_section
!= NULL
)
1897 asection
*snew
, *as
;
1898 bfd_boolean place_after
= place
->stmt
== NULL
;
1899 bfd_boolean insert_after
= TRUE
;
1901 snew
= os
->bfd_section
;
1903 /* Shuffle the bfd section list to make the output file look
1904 neater. This is really only cosmetic. */
1905 if (place
->section
== NULL
1906 && after
!= &lang_os_list
.head
->output_section_statement
)
1908 asection
*bfd_section
= after
->bfd_section
;
1910 /* If the output statement hasn't been used to place any input
1911 sections (and thus doesn't have an output bfd_section),
1912 look for the closest prior output statement having an
1914 if (bfd_section
== NULL
)
1915 bfd_section
= output_prev_sec_find (after
);
1917 if (bfd_section
!= NULL
&& bfd_section
!= snew
)
1918 place
->section
= &bfd_section
->next
;
1921 if (place
->section
== NULL
)
1922 place
->section
= &link_info
.output_bfd
->sections
;
1924 as
= *place
->section
;
1928 /* Put the section at the end of the list. */
1930 /* Unlink the section. */
1931 bfd_section_list_remove (link_info
.output_bfd
, snew
);
1933 /* Now tack it back on in the right place. */
1934 bfd_section_list_append (link_info
.output_bfd
, snew
);
1936 else if ((bfd_get_flavour (link_info
.output_bfd
)
1937 == bfd_target_elf_flavour
)
1938 && (bfd_get_flavour (s
->owner
)
1939 == bfd_target_elf_flavour
)
1940 && ((elf_section_type (s
) == SHT_NOTE
1941 && (s
->flags
& SEC_LOAD
) != 0)
1942 || (elf_section_type (as
) == SHT_NOTE
1943 && (as
->flags
& SEC_LOAD
) != 0)))
1945 /* Make sure that output note sections are grouped and sorted
1946 by alignments when inserting a note section or insert a
1947 section after a note section, */
1949 /* A specific section after which the output note section
1950 should be placed. */
1951 asection
*after_sec
;
1952 /* True if we need to insert the orphan section after a
1953 specific section to maintain output note section order. */
1954 bfd_boolean after_sec_note
= FALSE
;
1956 static asection
*first_orphan_note
= NULL
;
1958 /* Group and sort output note section by alignments in
1961 if (elf_section_type (s
) == SHT_NOTE
1962 && (s
->flags
& SEC_LOAD
) != 0)
1964 /* Search from the beginning for the last output note
1965 section with equal or larger alignments. NB: Don't
1966 place orphan note section after non-note sections. */
1968 first_orphan_note
= NULL
;
1969 for (sec
= link_info
.output_bfd
->sections
;
1971 && !bfd_is_abs_section (sec
));
1974 && elf_section_type (sec
) == SHT_NOTE
1975 && (sec
->flags
& SEC_LOAD
) != 0)
1977 if (!first_orphan_note
)
1978 first_orphan_note
= sec
;
1979 if (sec
->alignment_power
>= s
->alignment_power
)
1982 else if (first_orphan_note
)
1984 /* Stop if there is non-note section after the first
1985 orphan note section. */
1989 /* If this will be the first orphan note section, it can
1990 be placed at the default location. */
1991 after_sec_note
= first_orphan_note
!= NULL
;
1992 if (after_sec
== NULL
&& after_sec_note
)
1994 /* If all output note sections have smaller
1995 alignments, place the section before all
1996 output orphan note sections. */
1997 after_sec
= first_orphan_note
;
1998 insert_after
= FALSE
;
2001 else if (first_orphan_note
)
2003 /* Don't place non-note sections in the middle of orphan
2005 after_sec_note
= TRUE
;
2007 for (sec
= as
->next
;
2009 && !bfd_is_abs_section (sec
));
2011 if (elf_section_type (sec
) == SHT_NOTE
2012 && (sec
->flags
& SEC_LOAD
) != 0)
2020 /* Search forward to insert OS after AFTER_SEC output
2022 lang_output_section_statement_type
*stmt
, *next
;
2023 bfd_boolean found
= FALSE
;
2024 for (stmt
= after
; stmt
!= NULL
; stmt
= next
)
2029 if (stmt
->bfd_section
== after_sec
)
2039 /* If INSERT_AFTER is FALSE, place OS before
2040 AFTER_SEC output statement. */
2041 if (next
&& next
->bfd_section
== after_sec
)
2051 /* Search backward to insert OS after AFTER_SEC output
2054 for (stmt
= after
; stmt
!= NULL
; stmt
= stmt
->prev
)
2058 if (stmt
->bfd_section
== after_sec
)
2067 /* If INSERT_AFTER is FALSE, place OS before
2068 AFTER_SEC output statement. */
2069 if (stmt
->next
->bfd_section
== after_sec
)
2079 if (after_sec
== NULL
2080 || (insert_after
&& after_sec
->next
!= snew
)
2081 || (!insert_after
&& after_sec
->prev
!= snew
))
2083 /* Unlink the section. */
2084 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2086 /* Place SNEW after AFTER_SEC. If AFTER_SEC is NULL,
2091 bfd_section_list_insert_after (link_info
.output_bfd
,
2094 bfd_section_list_insert_before (link_info
.output_bfd
,
2098 bfd_section_list_prepend (link_info
.output_bfd
, snew
);
2101 else if (as
!= snew
&& as
->prev
!= snew
)
2103 /* Unlink the section. */
2104 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2106 /* Now tack it back on in the right place. */
2107 bfd_section_list_insert_before (link_info
.output_bfd
,
2111 else if (as
!= snew
&& as
->prev
!= snew
)
2113 /* Unlink the section. */
2114 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2116 /* Now tack it back on in the right place. */
2117 bfd_section_list_insert_before (link_info
.output_bfd
, as
, snew
);
2120 /* Save the end of this list. Further ophans of this type will
2121 follow the one we've just added. */
2122 place
->section
= &snew
->next
;
2124 /* The following is non-cosmetic. We try to put the output
2125 statements in some sort of reasonable order here, because they
2126 determine the final load addresses of the orphan sections.
2127 In addition, placing output statements in the wrong order may
2128 require extra segments. For instance, given a typical
2129 situation of all read-only sections placed in one segment and
2130 following that a segment containing all the read-write
2131 sections, we wouldn't want to place an orphan read/write
2132 section before or amongst the read-only ones. */
2133 if (add
.head
!= NULL
)
2135 lang_output_section_statement_type
*newly_added_os
;
2137 /* Place OS after AFTER if AFTER_NOTE is TRUE. */
2140 lang_statement_union_type
**where
= insert_os_after (after
);
2145 place
->os_tail
= &after
->next
;
2149 /* Put it after the last orphan statement we added. */
2150 *add
.tail
= *place
->stmt
;
2151 *place
->stmt
= add
.head
;
2154 /* Fix the global list pointer if we happened to tack our
2155 new list at the tail. */
2156 if (*stat_ptr
->tail
== add
.head
)
2157 stat_ptr
->tail
= add
.tail
;
2159 /* Save the end of this list. */
2160 place
->stmt
= add
.tail
;
2162 /* Do the same for the list of output section statements. */
2163 newly_added_os
= *os_tail
;
2165 newly_added_os
->prev
= (lang_output_section_statement_type
*)
2166 ((char *) place
->os_tail
2167 - offsetof (lang_output_section_statement_type
, next
));
2168 newly_added_os
->next
= *place
->os_tail
;
2169 if (newly_added_os
->next
!= NULL
)
2170 newly_added_os
->next
->prev
= newly_added_os
;
2171 *place
->os_tail
= newly_added_os
;
2172 place
->os_tail
= &newly_added_os
->next
;
2174 /* Fixing the global list pointer here is a little different.
2175 We added to the list in lang_enter_output_section_statement,
2176 trimmed off the new output_section_statment above when
2177 assigning *os_tail = NULL, but possibly added it back in
2178 the same place when assigning *place->os_tail. */
2179 if (*os_tail
== NULL
)
2180 lang_os_list
.tail
= (lang_statement_union_type
**) os_tail
;
2187 lang_print_asneeded (void)
2189 struct asneeded_minfo
*m
;
2191 if (asneeded_list_head
== NULL
)
2194 minfo (_("\nAs-needed library included to satisfy reference by file (symbol)\n\n"));
2196 for (m
= asneeded_list_head
; m
!= NULL
; m
= m
->next
)
2200 minfo ("%s", m
->soname
);
2201 len
= strlen (m
->soname
);
2215 minfo ("%pB ", m
->ref
);
2216 minfo ("(%pT)\n", m
->name
);
2221 lang_map_flags (flagword flag
)
2223 if (flag
& SEC_ALLOC
)
2226 if (flag
& SEC_CODE
)
2229 if (flag
& SEC_READONLY
)
2232 if (flag
& SEC_DATA
)
2235 if (flag
& SEC_LOAD
)
2242 lang_memory_region_type
*m
;
2243 bfd_boolean dis_header_printed
= FALSE
;
2245 LANG_FOR_EACH_INPUT_STATEMENT (file
)
2249 if ((file
->the_bfd
->flags
& (BFD_LINKER_CREATED
| DYNAMIC
)) != 0
2250 || file
->flags
.just_syms
)
2253 if (config
.print_map_discarded
)
2254 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
2255 if ((s
->output_section
== NULL
2256 || s
->output_section
->owner
!= link_info
.output_bfd
)
2257 && (s
->flags
& (SEC_LINKER_CREATED
| SEC_KEEP
)) == 0)
2259 if (! dis_header_printed
)
2261 fprintf (config
.map_file
, _("\nDiscarded input sections\n\n"));
2262 dis_header_printed
= TRUE
;
2265 print_input_section (s
, TRUE
);
2269 minfo (_("\nMemory Configuration\n\n"));
2270 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
2271 _("Name"), _("Origin"), _("Length"), _("Attributes"));
2273 for (m
= lang_memory_region_list
; m
!= NULL
; m
= m
->next
)
2278 fprintf (config
.map_file
, "%-16s ", m
->name_list
.name
);
2280 sprintf_vma (buf
, m
->origin
);
2281 minfo ("0x%s ", buf
);
2289 minfo ("0x%V", m
->length
);
2290 if (m
->flags
|| m
->not_flags
)
2298 lang_map_flags (m
->flags
);
2304 lang_map_flags (m
->not_flags
);
2311 fprintf (config
.map_file
, _("\nLinker script and memory map\n\n"));
2313 if (!link_info
.reduce_memory_overheads
)
2315 obstack_begin (&map_obstack
, 1000);
2316 bfd_link_hash_traverse (link_info
.hash
, sort_def_symbol
, 0);
2318 expld
.phase
= lang_fixed_phase_enum
;
2319 lang_statement_iteration
++;
2320 print_statements ();
2322 ldemul_extra_map_file_text (link_info
.output_bfd
, &link_info
,
2327 sort_def_symbol (struct bfd_link_hash_entry
*hash_entry
,
2328 void *info ATTRIBUTE_UNUSED
)
2330 if ((hash_entry
->type
== bfd_link_hash_defined
2331 || hash_entry
->type
== bfd_link_hash_defweak
)
2332 && hash_entry
->u
.def
.section
->owner
!= link_info
.output_bfd
2333 && hash_entry
->u
.def
.section
->owner
!= NULL
)
2335 input_section_userdata_type
*ud
;
2336 struct map_symbol_def
*def
;
2338 ud
= ((input_section_userdata_type
*)
2339 get_userdata (hash_entry
->u
.def
.section
));
2342 ud
= stat_alloc (sizeof (*ud
));
2343 get_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 get_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)
2864 search
= new_afile (name
, lang_input_file_is_search_file_enum
,
2865 default_target
, FALSE
);
2867 /* If we have already added this file, or this file is not real
2868 don't add this file. */
2869 if (search
->flags
.loaded
|| !search
->flags
.real
)
2872 if (!load_symbols (search
, NULL
))
2878 /* Save LIST as a list of libraries whose symbols should not be exported. */
2883 struct excluded_lib
*next
;
2885 static struct excluded_lib
*excluded_libs
;
2888 add_excluded_libs (const char *list
)
2890 const char *p
= list
, *end
;
2894 struct excluded_lib
*entry
;
2895 end
= strpbrk (p
, ",:");
2897 end
= p
+ strlen (p
);
2898 entry
= (struct excluded_lib
*) xmalloc (sizeof (*entry
));
2899 entry
->next
= excluded_libs
;
2900 entry
->name
= (char *) xmalloc (end
- p
+ 1);
2901 memcpy (entry
->name
, p
, end
- p
);
2902 entry
->name
[end
- p
] = '\0';
2903 excluded_libs
= entry
;
2911 check_excluded_libs (bfd
*abfd
)
2913 struct excluded_lib
*lib
= excluded_libs
;
2917 int len
= strlen (lib
->name
);
2918 const char *filename
= lbasename (abfd
->filename
);
2920 if (strcmp (lib
->name
, "ALL") == 0)
2922 abfd
->no_export
= TRUE
;
2926 if (filename_ncmp (lib
->name
, filename
, len
) == 0
2927 && (filename
[len
] == '\0'
2928 || (filename
[len
] == '.' && filename
[len
+ 1] == 'a'
2929 && filename
[len
+ 2] == '\0')))
2931 abfd
->no_export
= TRUE
;
2939 /* Get the symbols for an input file. */
2942 load_symbols (lang_input_statement_type
*entry
,
2943 lang_statement_list_type
*place
)
2947 if (entry
->flags
.loaded
)
2950 ldfile_open_file (entry
);
2952 /* Do not process further if the file was missing. */
2953 if (entry
->flags
.missing_file
)
2956 if (trace_files
|| verbose
)
2957 info_msg ("%pI\n", entry
);
2959 if (!bfd_check_format (entry
->the_bfd
, bfd_archive
)
2960 && !bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
2963 struct lang_input_statement_flags save_flags
;
2966 err
= bfd_get_error ();
2968 /* See if the emulation has some special knowledge. */
2969 if (ldemul_unrecognized_file (entry
))
2972 if (err
== bfd_error_file_ambiguously_recognized
)
2976 einfo (_("%P: %pB: file not recognized: %E;"
2977 " matching formats:"), entry
->the_bfd
);
2978 for (p
= matching
; *p
!= NULL
; p
++)
2982 else if (err
!= bfd_error_file_not_recognized
2984 einfo (_("%F%P: %pB: file not recognized: %E\n"), entry
->the_bfd
);
2986 bfd_close (entry
->the_bfd
);
2987 entry
->the_bfd
= NULL
;
2989 /* Try to interpret the file as a linker script. */
2990 save_flags
= input_flags
;
2991 ldfile_open_command_file (entry
->filename
);
2993 push_stat_ptr (place
);
2994 input_flags
.add_DT_NEEDED_for_regular
2995 = entry
->flags
.add_DT_NEEDED_for_regular
;
2996 input_flags
.add_DT_NEEDED_for_dynamic
2997 = entry
->flags
.add_DT_NEEDED_for_dynamic
;
2998 input_flags
.whole_archive
= entry
->flags
.whole_archive
;
2999 input_flags
.dynamic
= entry
->flags
.dynamic
;
3001 ldfile_assumed_script
= TRUE
;
3002 parser_input
= input_script
;
3004 ldfile_assumed_script
= FALSE
;
3006 /* missing_file is sticky. sysrooted will already have been
3007 restored when seeing EOF in yyparse, but no harm to restore
3009 save_flags
.missing_file
|= input_flags
.missing_file
;
3010 input_flags
= save_flags
;
3014 entry
->flags
.loaded
= TRUE
;
3019 if (ldemul_recognized_file (entry
))
3022 /* We don't call ldlang_add_file for an archive. Instead, the
3023 add_symbols entry point will call ldlang_add_file, via the
3024 add_archive_element callback, for each element of the archive
3026 switch (bfd_get_format (entry
->the_bfd
))
3032 if (!entry
->flags
.reload
)
3033 ldlang_add_file (entry
);
3037 check_excluded_libs (entry
->the_bfd
);
3039 entry
->the_bfd
->usrdata
= entry
;
3040 if (entry
->flags
.whole_archive
)
3043 bfd_boolean loaded
= TRUE
;
3048 member
= bfd_openr_next_archived_file (entry
->the_bfd
, member
);
3053 if (!bfd_check_format (member
, bfd_object
))
3055 einfo (_("%F%P: %pB: member %pB in archive is not an object\n"),
3056 entry
->the_bfd
, member
);
3061 if (!(*link_info
.callbacks
3062 ->add_archive_element
) (&link_info
, member
,
3063 "--whole-archive", &subsbfd
))
3066 /* Potentially, the add_archive_element hook may have set a
3067 substitute BFD for us. */
3068 if (!bfd_link_add_symbols (subsbfd
, &link_info
))
3070 einfo (_("%F%P: %pB: error adding symbols: %E\n"), member
);
3075 entry
->flags
.loaded
= loaded
;
3081 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
3082 entry
->flags
.loaded
= TRUE
;
3084 einfo (_("%F%P: %pB: error adding symbols: %E\n"), entry
->the_bfd
);
3086 return entry
->flags
.loaded
;
3089 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
3090 may be NULL, indicating that it is a wildcard. Separate
3091 lang_input_section statements are created for each part of the
3092 expansion; they are added after the wild statement S. OUTPUT is
3093 the output section. */
3096 wild (lang_wild_statement_type
*s
,
3097 const char *target ATTRIBUTE_UNUSED
,
3098 lang_output_section_statement_type
*output
)
3100 struct wildcard_list
*sec
;
3102 if (s
->handler_data
[0]
3103 && s
->handler_data
[0]->spec
.sorted
== by_name
3104 && !s
->filenames_sorted
)
3106 lang_section_bst_type
*tree
;
3108 walk_wild (s
, output_section_callback_fast
, output
);
3113 output_section_callback_tree_to_list (s
, tree
, output
);
3118 walk_wild (s
, output_section_callback
, output
);
3120 if (default_common_section
== NULL
)
3121 for (sec
= s
->section_list
; sec
!= NULL
; sec
= sec
->next
)
3122 if (sec
->spec
.name
!= NULL
&& strcmp (sec
->spec
.name
, "COMMON") == 0)
3124 /* Remember the section that common is going to in case we
3125 later get something which doesn't know where to put it. */
3126 default_common_section
= output
;
3131 /* Return TRUE iff target is the sought target. */
3134 get_target (const bfd_target
*target
, void *data
)
3136 const char *sought
= (const char *) data
;
3138 return strcmp (target
->name
, sought
) == 0;
3141 /* Like strcpy() but convert to lower case as well. */
3144 stricpy (char *dest
, char *src
)
3148 while ((c
= *src
++) != 0)
3149 *dest
++ = TOLOWER (c
);
3154 /* Remove the first occurrence of needle (if any) in haystack
3158 strcut (char *haystack
, char *needle
)
3160 haystack
= strstr (haystack
, needle
);
3166 for (src
= haystack
+ strlen (needle
); *src
;)
3167 *haystack
++ = *src
++;
3173 /* Compare two target format name strings.
3174 Return a value indicating how "similar" they are. */
3177 name_compare (char *first
, char *second
)
3183 copy1
= (char *) xmalloc (strlen (first
) + 1);
3184 copy2
= (char *) xmalloc (strlen (second
) + 1);
3186 /* Convert the names to lower case. */
3187 stricpy (copy1
, first
);
3188 stricpy (copy2
, second
);
3190 /* Remove size and endian strings from the name. */
3191 strcut (copy1
, "big");
3192 strcut (copy1
, "little");
3193 strcut (copy2
, "big");
3194 strcut (copy2
, "little");
3196 /* Return a value based on how many characters match,
3197 starting from the beginning. If both strings are
3198 the same then return 10 * their length. */
3199 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
3200 if (copy1
[result
] == 0)
3212 /* Set by closest_target_match() below. */
3213 static const bfd_target
*winner
;
3215 /* Scan all the valid bfd targets looking for one that has the endianness
3216 requirement that was specified on the command line, and is the nearest
3217 match to the original output target. */
3220 closest_target_match (const bfd_target
*target
, void *data
)
3222 const bfd_target
*original
= (const bfd_target
*) data
;
3224 if (command_line
.endian
== ENDIAN_BIG
3225 && target
->byteorder
!= BFD_ENDIAN_BIG
)
3228 if (command_line
.endian
== ENDIAN_LITTLE
3229 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
3232 /* Must be the same flavour. */
3233 if (target
->flavour
!= original
->flavour
)
3236 /* Ignore generic big and little endian elf vectors. */
3237 if (strcmp (target
->name
, "elf32-big") == 0
3238 || strcmp (target
->name
, "elf64-big") == 0
3239 || strcmp (target
->name
, "elf32-little") == 0
3240 || strcmp (target
->name
, "elf64-little") == 0)
3243 /* If we have not found a potential winner yet, then record this one. */
3250 /* Oh dear, we now have two potential candidates for a successful match.
3251 Compare their names and choose the better one. */
3252 if (name_compare (target
->name
, original
->name
)
3253 > name_compare (winner
->name
, original
->name
))
3256 /* Keep on searching until wqe have checked them all. */
3260 /* Return the BFD target format of the first input file. */
3263 get_first_input_target (void)
3265 char *target
= NULL
;
3267 LANG_FOR_EACH_INPUT_STATEMENT (s
)
3269 if (s
->header
.type
== lang_input_statement_enum
3272 ldfile_open_file (s
);
3274 if (s
->the_bfd
!= NULL
3275 && bfd_check_format (s
->the_bfd
, bfd_object
))
3277 target
= bfd_get_target (s
->the_bfd
);
3289 lang_get_output_target (void)
3293 /* Has the user told us which output format to use? */
3294 if (output_target
!= NULL
)
3295 return output_target
;
3297 /* No - has the current target been set to something other than
3299 if (current_target
!= default_target
&& current_target
!= NULL
)
3300 return current_target
;
3302 /* No - can we determine the format of the first input file? */
3303 target
= get_first_input_target ();
3307 /* Failed - use the default output target. */
3308 return default_target
;
3311 /* Open the output file. */
3314 open_output (const char *name
)
3316 output_target
= lang_get_output_target ();
3318 /* Has the user requested a particular endianness on the command
3320 if (command_line
.endian
!= ENDIAN_UNSET
)
3322 /* Get the chosen target. */
3323 const bfd_target
*target
3324 = bfd_iterate_over_targets (get_target
, (void *) output_target
);
3326 /* If the target is not supported, we cannot do anything. */
3329 enum bfd_endian desired_endian
;
3331 if (command_line
.endian
== ENDIAN_BIG
)
3332 desired_endian
= BFD_ENDIAN_BIG
;
3334 desired_endian
= BFD_ENDIAN_LITTLE
;
3336 /* See if the target has the wrong endianness. This should
3337 not happen if the linker script has provided big and
3338 little endian alternatives, but some scrips don't do
3340 if (target
->byteorder
!= desired_endian
)
3342 /* If it does, then see if the target provides
3343 an alternative with the correct endianness. */
3344 if (target
->alternative_target
!= NULL
3345 && (target
->alternative_target
->byteorder
== desired_endian
))
3346 output_target
= target
->alternative_target
->name
;
3349 /* Try to find a target as similar as possible to
3350 the default target, but which has the desired
3351 endian characteristic. */
3352 bfd_iterate_over_targets (closest_target_match
,
3355 /* Oh dear - we could not find any targets that
3356 satisfy our requirements. */
3358 einfo (_("%P: warning: could not find any targets"
3359 " that match endianness requirement\n"));
3361 output_target
= winner
->name
;
3367 link_info
.output_bfd
= bfd_openw (name
, output_target
);
3369 if (link_info
.output_bfd
== NULL
)
3371 if (bfd_get_error () == bfd_error_invalid_target
)
3372 einfo (_("%F%P: target %s not found\n"), output_target
);
3374 einfo (_("%F%P: cannot open output file %s: %E\n"), name
);
3377 delete_output_file_on_failure
= TRUE
;
3379 if (!bfd_set_format (link_info
.output_bfd
, bfd_object
))
3380 einfo (_("%F%P: %s: can not make object file: %E\n"), name
);
3381 if (!bfd_set_arch_mach (link_info
.output_bfd
,
3382 ldfile_output_architecture
,
3383 ldfile_output_machine
))
3384 einfo (_("%F%P: %s: can not set architecture: %E\n"), name
);
3386 link_info
.hash
= bfd_link_hash_table_create (link_info
.output_bfd
);
3387 if (link_info
.hash
== NULL
)
3388 einfo (_("%F%P: can not create hash table: %E\n"));
3390 bfd_set_gp_size (link_info
.output_bfd
, g_switch_value
);
3394 ldlang_open_output (lang_statement_union_type
*statement
)
3396 switch (statement
->header
.type
)
3398 case lang_output_statement_enum
:
3399 ASSERT (link_info
.output_bfd
== NULL
);
3400 open_output (statement
->output_statement
.name
);
3401 ldemul_set_output_arch ();
3402 if (config
.magic_demand_paged
3403 && !bfd_link_relocatable (&link_info
))
3404 link_info
.output_bfd
->flags
|= D_PAGED
;
3406 link_info
.output_bfd
->flags
&= ~D_PAGED
;
3407 if (config
.text_read_only
)
3408 link_info
.output_bfd
->flags
|= WP_TEXT
;
3410 link_info
.output_bfd
->flags
&= ~WP_TEXT
;
3411 if (link_info
.traditional_format
)
3412 link_info
.output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
3414 link_info
.output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
3417 case lang_target_statement_enum
:
3418 current_target
= statement
->target_statement
.target
;
3428 unsigned x
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3429 ldfile_output_machine
);
3432 while ((x
& 1) == 0)
3440 /* Open all the input files. */
3444 OPEN_BFD_NORMAL
= 0,
3448 #ifdef ENABLE_PLUGINS
3449 static lang_input_statement_type
*plugin_insert
= NULL
;
3450 static struct bfd_link_hash_entry
*plugin_undefs
= NULL
;
3454 open_input_bfds (lang_statement_union_type
*s
, enum open_bfd_mode mode
)
3456 for (; s
!= NULL
; s
= s
->header
.next
)
3458 switch (s
->header
.type
)
3460 case lang_constructors_statement_enum
:
3461 open_input_bfds (constructor_list
.head
, mode
);
3463 case lang_output_section_statement_enum
:
3464 open_input_bfds (s
->output_section_statement
.children
.head
, mode
);
3466 case lang_wild_statement_enum
:
3467 /* Maybe we should load the file's symbols. */
3468 if ((mode
& OPEN_BFD_RESCAN
) == 0
3469 && s
->wild_statement
.filename
3470 && !wildcardp (s
->wild_statement
.filename
)
3471 && !archive_path (s
->wild_statement
.filename
))
3472 lookup_name (s
->wild_statement
.filename
);
3473 open_input_bfds (s
->wild_statement
.children
.head
, mode
);
3475 case lang_group_statement_enum
:
3477 struct bfd_link_hash_entry
*undefs
;
3478 #ifdef ENABLE_PLUGINS
3479 lang_input_statement_type
*plugin_insert_save
;
3482 /* We must continually search the entries in the group
3483 until no new symbols are added to the list of undefined
3488 #ifdef ENABLE_PLUGINS
3489 plugin_insert_save
= plugin_insert
;
3491 undefs
= link_info
.hash
->undefs_tail
;
3492 open_input_bfds (s
->group_statement
.children
.head
,
3493 mode
| OPEN_BFD_FORCE
);
3495 while (undefs
!= link_info
.hash
->undefs_tail
3496 #ifdef ENABLE_PLUGINS
3497 /* Objects inserted by a plugin, which are loaded
3498 before we hit this loop, may have added new
3500 || (plugin_insert
!= plugin_insert_save
&& plugin_undefs
)
3505 case lang_target_statement_enum
:
3506 current_target
= s
->target_statement
.target
;
3508 case lang_input_statement_enum
:
3509 if (s
->input_statement
.flags
.real
)
3511 lang_statement_union_type
**os_tail
;
3512 lang_statement_list_type add
;
3515 s
->input_statement
.target
= current_target
;
3517 /* If we are being called from within a group, and this
3518 is an archive which has already been searched, then
3519 force it to be researched unless the whole archive
3520 has been loaded already. Do the same for a rescan.
3521 Likewise reload --as-needed shared libs. */
3522 if (mode
!= OPEN_BFD_NORMAL
3523 #ifdef ENABLE_PLUGINS
3524 && ((mode
& OPEN_BFD_RESCAN
) == 0
3525 || plugin_insert
== NULL
)
3527 && s
->input_statement
.flags
.loaded
3528 && (abfd
= s
->input_statement
.the_bfd
) != NULL
3529 && ((bfd_get_format (abfd
) == bfd_archive
3530 && !s
->input_statement
.flags
.whole_archive
)
3531 || (bfd_get_format (abfd
) == bfd_object
3532 && ((abfd
->flags
) & DYNAMIC
) != 0
3533 && s
->input_statement
.flags
.add_DT_NEEDED_for_regular
3534 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
3535 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)))
3537 s
->input_statement
.flags
.loaded
= FALSE
;
3538 s
->input_statement
.flags
.reload
= TRUE
;
3541 os_tail
= lang_os_list
.tail
;
3542 lang_list_init (&add
);
3544 if (!load_symbols (&s
->input_statement
, &add
))
3545 config
.make_executable
= FALSE
;
3547 if (add
.head
!= NULL
)
3549 /* If this was a script with output sections then
3550 tack any added statements on to the end of the
3551 list. This avoids having to reorder the output
3552 section statement list. Very likely the user
3553 forgot -T, and whatever we do here will not meet
3554 naive user expectations. */
3555 if (os_tail
!= lang_os_list
.tail
)
3557 einfo (_("%P: warning: %s contains output sections;"
3558 " did you forget -T?\n"),
3559 s
->input_statement
.filename
);
3560 *stat_ptr
->tail
= add
.head
;
3561 stat_ptr
->tail
= add
.tail
;
3565 *add
.tail
= s
->header
.next
;
3566 s
->header
.next
= add
.head
;
3570 #ifdef ENABLE_PLUGINS
3571 /* If we have found the point at which a plugin added new
3572 files, clear plugin_insert to enable archive rescan. */
3573 if (&s
->input_statement
== plugin_insert
)
3574 plugin_insert
= NULL
;
3577 case lang_assignment_statement_enum
:
3578 if (s
->assignment_statement
.exp
->type
.node_class
!= etree_assert
)
3579 exp_fold_tree_no_dot (s
->assignment_statement
.exp
);
3586 /* Exit if any of the files were missing. */
3587 if (input_flags
.missing_file
)
3591 /* Add the supplied name to the symbol table as an undefined reference.
3592 This is a two step process as the symbol table doesn't even exist at
3593 the time the ld command line is processed. First we put the name
3594 on a list, then, once the output file has been opened, transfer the
3595 name to the symbol table. */
3597 typedef struct bfd_sym_chain ldlang_undef_chain_list_type
;
3599 #define ldlang_undef_chain_list_head entry_symbol.next
3602 ldlang_add_undef (const char *const name
, bfd_boolean cmdline
)
3604 ldlang_undef_chain_list_type
*new_undef
;
3606 undef_from_cmdline
= undef_from_cmdline
|| cmdline
;
3607 new_undef
= stat_alloc (sizeof (*new_undef
));
3608 new_undef
->next
= ldlang_undef_chain_list_head
;
3609 ldlang_undef_chain_list_head
= new_undef
;
3611 new_undef
->name
= xstrdup (name
);
3613 if (link_info
.output_bfd
!= NULL
)
3614 insert_undefined (new_undef
->name
);
3617 /* Insert NAME as undefined in the symbol table. */
3620 insert_undefined (const char *name
)
3622 struct bfd_link_hash_entry
*h
;
3624 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, FALSE
, TRUE
);
3626 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
3627 if (h
->type
== bfd_link_hash_new
)
3629 h
->type
= bfd_link_hash_undefined
;
3630 h
->u
.undef
.abfd
= NULL
;
3631 h
->non_ir_ref_regular
= TRUE
;
3632 if (is_elf_hash_table (link_info
.hash
))
3633 ((struct elf_link_hash_entry
*) h
)->mark
= 1;
3634 bfd_link_add_undef (link_info
.hash
, h
);
3638 /* Run through the list of undefineds created above and place them
3639 into the linker hash table as undefined symbols belonging to the
3643 lang_place_undefineds (void)
3645 ldlang_undef_chain_list_type
*ptr
;
3647 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
3648 insert_undefined (ptr
->name
);
3651 /* Structure used to build the list of symbols that the user has required
3654 struct require_defined_symbol
3657 struct require_defined_symbol
*next
;
3660 /* The list of symbols that the user has required be defined. */
3662 static struct require_defined_symbol
*require_defined_symbol_list
;
3664 /* Add a new symbol NAME to the list of symbols that are required to be
3668 ldlang_add_require_defined (const char *const name
)
3670 struct require_defined_symbol
*ptr
;
3672 ldlang_add_undef (name
, TRUE
);
3673 ptr
= stat_alloc (sizeof (*ptr
));
3674 ptr
->next
= require_defined_symbol_list
;
3675 ptr
->name
= strdup (name
);
3676 require_defined_symbol_list
= ptr
;
3679 /* Check that all symbols the user required to be defined, are defined,
3680 raise an error if we find a symbol that is not defined. */
3683 ldlang_check_require_defined_symbols (void)
3685 struct require_defined_symbol
*ptr
;
3687 for (ptr
= require_defined_symbol_list
; ptr
!= NULL
; ptr
= ptr
->next
)
3689 struct bfd_link_hash_entry
*h
;
3691 h
= bfd_link_hash_lookup (link_info
.hash
, ptr
->name
,
3692 FALSE
, FALSE
, TRUE
);
3694 || (h
->type
!= bfd_link_hash_defined
3695 && h
->type
!= bfd_link_hash_defweak
))
3696 einfo(_("%X%P: required symbol `%s' not defined\n"), ptr
->name
);
3700 /* Check for all readonly or some readwrite sections. */
3703 check_input_sections
3704 (lang_statement_union_type
*s
,
3705 lang_output_section_statement_type
*output_section_statement
)
3707 for (; s
!= NULL
; s
= s
->header
.next
)
3709 switch (s
->header
.type
)
3711 case lang_wild_statement_enum
:
3712 walk_wild (&s
->wild_statement
, check_section_callback
,
3713 output_section_statement
);
3714 if (!output_section_statement
->all_input_readonly
)
3717 case lang_constructors_statement_enum
:
3718 check_input_sections (constructor_list
.head
,
3719 output_section_statement
);
3720 if (!output_section_statement
->all_input_readonly
)
3723 case lang_group_statement_enum
:
3724 check_input_sections (s
->group_statement
.children
.head
,
3725 output_section_statement
);
3726 if (!output_section_statement
->all_input_readonly
)
3735 /* Update wildcard statements if needed. */
3738 update_wild_statements (lang_statement_union_type
*s
)
3740 struct wildcard_list
*sec
;
3742 switch (sort_section
)
3752 for (; s
!= NULL
; s
= s
->header
.next
)
3754 switch (s
->header
.type
)
3759 case lang_wild_statement_enum
:
3760 for (sec
= s
->wild_statement
.section_list
; sec
!= NULL
;
3762 /* Don't sort .init/.fini sections. */
3763 if (strcmp (sec
->spec
.name
, ".init") != 0
3764 && strcmp (sec
->spec
.name
, ".fini") != 0)
3765 switch (sec
->spec
.sorted
)
3768 sec
->spec
.sorted
= sort_section
;
3771 if (sort_section
== by_alignment
)
3772 sec
->spec
.sorted
= by_name_alignment
;
3775 if (sort_section
== by_name
)
3776 sec
->spec
.sorted
= by_alignment_name
;
3783 case lang_constructors_statement_enum
:
3784 update_wild_statements (constructor_list
.head
);
3787 case lang_output_section_statement_enum
:
3788 update_wild_statements
3789 (s
->output_section_statement
.children
.head
);
3792 case lang_group_statement_enum
:
3793 update_wild_statements (s
->group_statement
.children
.head
);
3801 /* Open input files and attach to output sections. */
3804 map_input_to_output_sections
3805 (lang_statement_union_type
*s
, const char *target
,
3806 lang_output_section_statement_type
*os
)
3808 for (; s
!= NULL
; s
= s
->header
.next
)
3810 lang_output_section_statement_type
*tos
;
3813 switch (s
->header
.type
)
3815 case lang_wild_statement_enum
:
3816 wild (&s
->wild_statement
, target
, os
);
3818 case lang_constructors_statement_enum
:
3819 map_input_to_output_sections (constructor_list
.head
,
3823 case lang_output_section_statement_enum
:
3824 tos
= &s
->output_section_statement
;
3825 if (tos
->constraint
!= 0)
3827 if (tos
->constraint
!= ONLY_IF_RW
3828 && tos
->constraint
!= ONLY_IF_RO
)
3830 tos
->all_input_readonly
= TRUE
;
3831 check_input_sections (tos
->children
.head
, tos
);
3832 if (tos
->all_input_readonly
!= (tos
->constraint
== ONLY_IF_RO
))
3834 tos
->constraint
= -1;
3838 map_input_to_output_sections (tos
->children
.head
,
3842 case lang_output_statement_enum
:
3844 case lang_target_statement_enum
:
3845 target
= s
->target_statement
.target
;
3847 case lang_group_statement_enum
:
3848 map_input_to_output_sections (s
->group_statement
.children
.head
,
3852 case lang_data_statement_enum
:
3853 /* Make sure that any sections mentioned in the expression
3855 exp_init_os (s
->data_statement
.exp
);
3856 /* The output section gets CONTENTS, ALLOC and LOAD, but
3857 these may be overridden by the script. */
3858 flags
= SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
;
3859 switch (os
->sectype
)
3861 case normal_section
:
3862 case overlay_section
:
3863 case first_overlay_section
:
3865 case noalloc_section
:
3866 flags
= SEC_HAS_CONTENTS
;
3868 case noload_section
:
3869 if (bfd_get_flavour (link_info
.output_bfd
)
3870 == bfd_target_elf_flavour
)
3871 flags
= SEC_NEVER_LOAD
| SEC_ALLOC
;
3873 flags
= SEC_NEVER_LOAD
| SEC_HAS_CONTENTS
;
3876 if (os
->bfd_section
== NULL
)
3877 init_os (os
, flags
);
3879 os
->bfd_section
->flags
|= flags
;
3881 case lang_input_section_enum
:
3883 case lang_fill_statement_enum
:
3884 case lang_object_symbols_statement_enum
:
3885 case lang_reloc_statement_enum
:
3886 case lang_padding_statement_enum
:
3887 case lang_input_statement_enum
:
3888 if (os
!= NULL
&& os
->bfd_section
== NULL
)
3891 case lang_assignment_statement_enum
:
3892 if (os
!= NULL
&& os
->bfd_section
== NULL
)
3895 /* Make sure that any sections mentioned in the assignment
3897 exp_init_os (s
->assignment_statement
.exp
);
3899 case lang_address_statement_enum
:
3900 /* Mark the specified section with the supplied address.
3901 If this section was actually a segment marker, then the
3902 directive is ignored if the linker script explicitly
3903 processed the segment marker. Originally, the linker
3904 treated segment directives (like -Ttext on the
3905 command-line) as section directives. We honor the
3906 section directive semantics for backwards compatibility;
3907 linker scripts that do not specifically check for
3908 SEGMENT_START automatically get the old semantics. */
3909 if (!s
->address_statement
.segment
3910 || !s
->address_statement
.segment
->used
)
3912 const char *name
= s
->address_statement
.section_name
;
3914 /* Create the output section statement here so that
3915 orphans with a set address will be placed after other
3916 script sections. If we let the orphan placement code
3917 place them in amongst other sections then the address
3918 will affect following script sections, which is
3919 likely to surprise naive users. */
3920 tos
= lang_output_section_statement_lookup (name
, 0, TRUE
);
3921 tos
->addr_tree
= s
->address_statement
.address
;
3922 if (tos
->bfd_section
== NULL
)
3926 case lang_insert_statement_enum
:
3932 /* An insert statement snips out all the linker statements from the
3933 start of the list and places them after the output section
3934 statement specified by the insert. This operation is complicated
3935 by the fact that we keep a doubly linked list of output section
3936 statements as well as the singly linked list of all statements.
3937 FIXME someday: Twiddling with the list not only moves statements
3938 from the user's script but also input and group statements that are
3939 built from command line object files and --start-group. We only
3940 get away with this because the list pointers used by file_chain
3941 and input_file_chain are not reordered, and processing via
3942 statement_list after this point mostly ignores input statements.
3943 One exception is the map file, where LOAD and START GROUP/END GROUP
3944 can end up looking odd. */
3947 process_insert_statements (lang_statement_union_type
**start
)
3949 lang_statement_union_type
**s
;
3950 lang_output_section_statement_type
*first_os
= NULL
;
3951 lang_output_section_statement_type
*last_os
= NULL
;
3952 lang_output_section_statement_type
*os
;
3957 if ((*s
)->header
.type
== lang_output_section_statement_enum
)
3959 /* Keep pointers to the first and last output section
3960 statement in the sequence we may be about to move. */
3961 os
= &(*s
)->output_section_statement
;
3963 ASSERT (last_os
== NULL
|| last_os
->next
== os
);
3966 /* Set constraint negative so that lang_output_section_find
3967 won't match this output section statement. At this
3968 stage in linking constraint has values in the range
3969 [-1, ONLY_IN_RW]. */
3970 last_os
->constraint
= -2 - last_os
->constraint
;
3971 if (first_os
== NULL
)
3974 else if ((*s
)->header
.type
== lang_group_statement_enum
)
3976 /* A user might put -T between --start-group and
3977 --end-group. One way this odd construct might arise is
3978 from a wrapper around ld to change library search
3979 behaviour. For example:
3981 exec real_ld --start-group "$@" --end-group
3982 This isn't completely unreasonable so go looking inside a
3983 group statement for insert statements. */
3984 process_insert_statements (&(*s
)->group_statement
.children
.head
);
3986 else if ((*s
)->header
.type
== lang_insert_statement_enum
)
3988 lang_insert_statement_type
*i
= &(*s
)->insert_statement
;
3989 lang_output_section_statement_type
*where
;
3990 lang_statement_union_type
**ptr
;
3991 lang_statement_union_type
*first
;
3993 where
= lang_output_section_find (i
->where
);
3994 if (where
!= NULL
&& i
->is_before
)
3997 where
= where
->prev
;
3998 while (where
!= NULL
&& where
->constraint
< 0);
4002 einfo (_("%F%P: %s not found for insert\n"), i
->where
);
4006 /* Deal with reordering the output section statement list. */
4007 if (last_os
!= NULL
)
4009 asection
*first_sec
, *last_sec
;
4010 struct lang_output_section_statement_struct
**next
;
4012 /* Snip out the output sections we are moving. */
4013 first_os
->prev
->next
= last_os
->next
;
4014 if (last_os
->next
== NULL
)
4016 next
= &first_os
->prev
->next
;
4017 lang_os_list
.tail
= (lang_statement_union_type
**) next
;
4020 last_os
->next
->prev
= first_os
->prev
;
4021 /* Add them in at the new position. */
4022 last_os
->next
= where
->next
;
4023 if (where
->next
== NULL
)
4025 next
= &last_os
->next
;
4026 lang_os_list
.tail
= (lang_statement_union_type
**) next
;
4029 where
->next
->prev
= last_os
;
4030 first_os
->prev
= where
;
4031 where
->next
= first_os
;
4033 /* Move the bfd sections in the same way. */
4036 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
4038 os
->constraint
= -2 - os
->constraint
;
4039 if (os
->bfd_section
!= NULL
4040 && os
->bfd_section
->owner
!= NULL
)
4042 last_sec
= os
->bfd_section
;
4043 if (first_sec
== NULL
)
4044 first_sec
= last_sec
;
4049 if (last_sec
!= NULL
)
4051 asection
*sec
= where
->bfd_section
;
4053 sec
= output_prev_sec_find (where
);
4055 /* The place we want to insert must come after the
4056 sections we are moving. So if we find no
4057 section or if the section is the same as our
4058 last section, then no move is needed. */
4059 if (sec
!= NULL
&& sec
!= last_sec
)
4061 /* Trim them off. */
4062 if (first_sec
->prev
!= NULL
)
4063 first_sec
->prev
->next
= last_sec
->next
;
4065 link_info
.output_bfd
->sections
= last_sec
->next
;
4066 if (last_sec
->next
!= NULL
)
4067 last_sec
->next
->prev
= first_sec
->prev
;
4069 link_info
.output_bfd
->section_last
= first_sec
->prev
;
4071 last_sec
->next
= sec
->next
;
4072 if (sec
->next
!= NULL
)
4073 sec
->next
->prev
= last_sec
;
4075 link_info
.output_bfd
->section_last
= last_sec
;
4076 first_sec
->prev
= sec
;
4077 sec
->next
= first_sec
;
4085 ptr
= insert_os_after (where
);
4086 /* Snip everything from the start of the list, up to and
4087 including the insert statement we are currently processing. */
4089 *start
= (*s
)->header
.next
;
4090 /* Add them back where they belong, minus the insert. */
4093 statement_list
.tail
= s
;
4098 s
= &(*s
)->header
.next
;
4101 /* Undo constraint twiddling. */
4102 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
4104 os
->constraint
= -2 - os
->constraint
;
4110 /* An output section might have been removed after its statement was
4111 added. For example, ldemul_before_allocation can remove dynamic
4112 sections if they turn out to be not needed. Clean them up here. */
4115 strip_excluded_output_sections (void)
4117 lang_output_section_statement_type
*os
;
4119 /* Run lang_size_sections (if not already done). */
4120 if (expld
.phase
!= lang_mark_phase_enum
)
4122 expld
.phase
= lang_mark_phase_enum
;
4123 expld
.dataseg
.phase
= exp_seg_none
;
4124 one_lang_size_sections_pass (NULL
, FALSE
);
4125 lang_reset_memory_regions ();
4128 for (os
= &lang_os_list
.head
->output_section_statement
;
4132 asection
*output_section
;
4133 bfd_boolean exclude
;
4135 if (os
->constraint
< 0)
4138 output_section
= os
->bfd_section
;
4139 if (output_section
== NULL
)
4142 exclude
= (output_section
->rawsize
== 0
4143 && (output_section
->flags
& SEC_KEEP
) == 0
4144 && !bfd_section_removed_from_list (link_info
.output_bfd
,
4147 /* Some sections have not yet been sized, notably .gnu.version,
4148 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
4149 input sections, so don't drop output sections that have such
4150 input sections unless they are also marked SEC_EXCLUDE. */
4151 if (exclude
&& output_section
->map_head
.s
!= NULL
)
4155 for (s
= output_section
->map_head
.s
; s
!= NULL
; s
= s
->map_head
.s
)
4156 if ((s
->flags
& SEC_EXCLUDE
) == 0
4157 && ((s
->flags
& SEC_LINKER_CREATED
) != 0
4158 || link_info
.emitrelocations
))
4167 /* We don't set bfd_section to NULL since bfd_section of the
4168 removed output section statement may still be used. */
4169 if (!os
->update_dot
)
4171 output_section
->flags
|= SEC_EXCLUDE
;
4172 bfd_section_list_remove (link_info
.output_bfd
, output_section
);
4173 link_info
.output_bfd
->section_count
--;
4178 /* Called from ldwrite to clear out asection.map_head and
4179 asection.map_tail for use as link_orders in ldwrite. */
4182 lang_clear_os_map (void)
4184 lang_output_section_statement_type
*os
;
4186 if (map_head_is_link_order
)
4189 for (os
= &lang_os_list
.head
->output_section_statement
;
4193 asection
*output_section
;
4195 if (os
->constraint
< 0)
4198 output_section
= os
->bfd_section
;
4199 if (output_section
== NULL
)
4202 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
4203 output_section
->map_head
.link_order
= NULL
;
4204 output_section
->map_tail
.link_order
= NULL
;
4207 /* Stop future calls to lang_add_section from messing with map_head
4208 and map_tail link_order fields. */
4209 map_head_is_link_order
= TRUE
;
4213 print_output_section_statement
4214 (lang_output_section_statement_type
*output_section_statement
)
4216 asection
*section
= output_section_statement
->bfd_section
;
4219 if (output_section_statement
!= abs_output_section
)
4221 minfo ("\n%s", output_section_statement
->name
);
4223 if (section
!= NULL
)
4225 print_dot
= section
->vma
;
4227 len
= strlen (output_section_statement
->name
);
4228 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
4233 while (len
< SECTION_NAME_MAP_LENGTH
)
4239 minfo ("0x%V %W", section
->vma
, TO_ADDR (section
->size
));
4241 if (section
->vma
!= section
->lma
)
4242 minfo (_(" load address 0x%V"), section
->lma
);
4244 if (output_section_statement
->update_dot_tree
!= NULL
)
4245 exp_fold_tree (output_section_statement
->update_dot_tree
,
4246 bfd_abs_section_ptr
, &print_dot
);
4252 print_statement_list (output_section_statement
->children
.head
,
4253 output_section_statement
);
4257 print_assignment (lang_assignment_statement_type
*assignment
,
4258 lang_output_section_statement_type
*output_section
)
4265 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
4268 if (assignment
->exp
->type
.node_class
== etree_assert
)
4271 tree
= assignment
->exp
->assert_s
.child
;
4275 const char *dst
= assignment
->exp
->assign
.dst
;
4277 is_dot
= (dst
[0] == '.' && dst
[1] == 0);
4278 tree
= assignment
->exp
;
4281 osec
= output_section
->bfd_section
;
4283 osec
= bfd_abs_section_ptr
;
4285 if (assignment
->exp
->type
.node_class
!= etree_provide
)
4286 exp_fold_tree (tree
, osec
, &print_dot
);
4288 expld
.result
.valid_p
= FALSE
;
4290 if (expld
.result
.valid_p
)
4294 if (assignment
->exp
->type
.node_class
== etree_assert
4296 || expld
.assign_name
!= NULL
)
4298 value
= expld
.result
.value
;
4300 if (expld
.result
.section
!= NULL
)
4301 value
+= expld
.result
.section
->vma
;
4303 minfo ("0x%V", value
);
4309 struct bfd_link_hash_entry
*h
;
4311 h
= bfd_link_hash_lookup (link_info
.hash
, assignment
->exp
->assign
.dst
,
4312 FALSE
, FALSE
, TRUE
);
4314 && (h
->type
== bfd_link_hash_defined
4315 || h
->type
== bfd_link_hash_defweak
))
4317 value
= h
->u
.def
.value
;
4318 value
+= h
->u
.def
.section
->output_section
->vma
;
4319 value
+= h
->u
.def
.section
->output_offset
;
4321 minfo ("[0x%V]", value
);
4324 minfo ("[unresolved]");
4329 if (assignment
->exp
->type
.node_class
== etree_provide
)
4330 minfo ("[!provide]");
4337 expld
.assign_name
= NULL
;
4340 exp_print_tree (assignment
->exp
);
4345 print_input_statement (lang_input_statement_type
*statm
)
4347 if (statm
->filename
!= NULL
)
4348 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
4351 /* Print all symbols defined in a particular section. This is called
4352 via bfd_link_hash_traverse, or by print_all_symbols. */
4355 print_one_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
)
4357 asection
*sec
= (asection
*) ptr
;
4359 if ((hash_entry
->type
== bfd_link_hash_defined
4360 || hash_entry
->type
== bfd_link_hash_defweak
)
4361 && sec
== hash_entry
->u
.def
.section
)
4365 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
4368 (hash_entry
->u
.def
.value
4369 + hash_entry
->u
.def
.section
->output_offset
4370 + hash_entry
->u
.def
.section
->output_section
->vma
));
4372 minfo (" %pT\n", hash_entry
->root
.string
);
4379 hash_entry_addr_cmp (const void *a
, const void *b
)
4381 const struct bfd_link_hash_entry
*l
= *(const struct bfd_link_hash_entry
**)a
;
4382 const struct bfd_link_hash_entry
*r
= *(const struct bfd_link_hash_entry
**)b
;
4384 if (l
->u
.def
.value
< r
->u
.def
.value
)
4386 else if (l
->u
.def
.value
> r
->u
.def
.value
)
4393 print_all_symbols (asection
*sec
)
4395 input_section_userdata_type
*ud
4396 = (input_section_userdata_type
*) get_userdata (sec
);
4397 struct map_symbol_def
*def
;
4398 struct bfd_link_hash_entry
**entries
;
4404 *ud
->map_symbol_def_tail
= 0;
4406 /* Sort the symbols by address. */
4407 entries
= (struct bfd_link_hash_entry
**)
4408 obstack_alloc (&map_obstack
,
4409 ud
->map_symbol_def_count
* sizeof (*entries
));
4411 for (i
= 0, def
= ud
->map_symbol_def_head
; def
; def
= def
->next
, i
++)
4412 entries
[i
] = def
->entry
;
4414 qsort (entries
, ud
->map_symbol_def_count
, sizeof (*entries
),
4415 hash_entry_addr_cmp
);
4417 /* Print the symbols. */
4418 for (i
= 0; i
< ud
->map_symbol_def_count
; i
++)
4419 print_one_symbol (entries
[i
], sec
);
4421 obstack_free (&map_obstack
, entries
);
4424 /* Print information about an input section to the map file. */
4427 print_input_section (asection
*i
, bfd_boolean is_discarded
)
4429 bfd_size_type size
= i
->size
;
4436 minfo ("%s", i
->name
);
4438 len
= 1 + strlen (i
->name
);
4439 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
4444 while (len
< SECTION_NAME_MAP_LENGTH
)
4450 if (i
->output_section
!= NULL
4451 && i
->output_section
->owner
== link_info
.output_bfd
)
4452 addr
= i
->output_section
->vma
+ i
->output_offset
;
4460 minfo ("0x%V %W %pB\n", addr
, size
, i
->owner
);
4462 if (size
!= i
->rawsize
&& i
->rawsize
!= 0)
4464 len
= SECTION_NAME_MAP_LENGTH
+ 3;
4476 minfo (_("%W (size before relaxing)\n"), i
->rawsize
);
4479 if (i
->output_section
!= NULL
4480 && i
->output_section
->owner
== link_info
.output_bfd
)
4482 if (link_info
.reduce_memory_overheads
)
4483 bfd_link_hash_traverse (link_info
.hash
, print_one_symbol
, i
);
4485 print_all_symbols (i
);
4487 /* Update print_dot, but make sure that we do not move it
4488 backwards - this could happen if we have overlays and a
4489 later overlay is shorter than an earier one. */
4490 if (addr
+ TO_ADDR (size
) > print_dot
)
4491 print_dot
= addr
+ TO_ADDR (size
);
4496 print_fill_statement (lang_fill_statement_type
*fill
)
4500 fputs (" FILL mask 0x", config
.map_file
);
4501 for (p
= fill
->fill
->data
, size
= fill
->fill
->size
; size
!= 0; p
++, size
--)
4502 fprintf (config
.map_file
, "%02x", *p
);
4503 fputs ("\n", config
.map_file
);
4507 print_data_statement (lang_data_statement_type
*data
)
4515 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
4518 addr
= data
->output_offset
;
4519 if (data
->output_section
!= NULL
)
4520 addr
+= data
->output_section
->vma
;
4548 if (size
< TO_SIZE ((unsigned) 1))
4549 size
= TO_SIZE ((unsigned) 1);
4550 minfo ("0x%V %W %s 0x%v", addr
, TO_ADDR (size
), name
, data
->value
);
4552 if (data
->exp
->type
.node_class
!= etree_value
)
4555 exp_print_tree (data
->exp
);
4560 print_dot
= addr
+ TO_ADDR (size
);
4563 /* Print an address statement. These are generated by options like
4567 print_address_statement (lang_address_statement_type
*address
)
4569 minfo (_("Address of section %s set to "), address
->section_name
);
4570 exp_print_tree (address
->address
);
4574 /* Print a reloc statement. */
4577 print_reloc_statement (lang_reloc_statement_type
*reloc
)
4584 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
4587 addr
= reloc
->output_offset
;
4588 if (reloc
->output_section
!= NULL
)
4589 addr
+= reloc
->output_section
->vma
;
4591 size
= bfd_get_reloc_size (reloc
->howto
);
4593 minfo ("0x%V %W RELOC %s ", addr
, TO_ADDR (size
), reloc
->howto
->name
);
4595 if (reloc
->name
!= NULL
)
4596 minfo ("%s+", reloc
->name
);
4598 minfo ("%s+", reloc
->section
->name
);
4600 exp_print_tree (reloc
->addend_exp
);
4604 print_dot
= addr
+ TO_ADDR (size
);
4608 print_padding_statement (lang_padding_statement_type
*s
)
4616 len
= sizeof " *fill*" - 1;
4617 while (len
< SECTION_NAME_MAP_LENGTH
)
4623 addr
= s
->output_offset
;
4624 if (s
->output_section
!= NULL
)
4625 addr
+= s
->output_section
->vma
;
4626 minfo ("0x%V %W ", addr
, TO_ADDR (s
->size
));
4628 if (s
->fill
->size
!= 0)
4632 for (p
= s
->fill
->data
, size
= s
->fill
->size
; size
!= 0; p
++, size
--)
4633 fprintf (config
.map_file
, "%02x", *p
);
4638 print_dot
= addr
+ TO_ADDR (s
->size
);
4642 print_wild_statement (lang_wild_statement_type
*w
,
4643 lang_output_section_statement_type
*os
)
4645 struct wildcard_list
*sec
;
4649 if (w
->exclude_name_list
)
4652 minfo ("EXCLUDE_FILE(%s", w
->exclude_name_list
->name
);
4653 for (tmp
= w
->exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
4654 minfo (" %s", tmp
->name
);
4658 if (w
->filenames_sorted
)
4659 minfo ("SORT_BY_NAME(");
4660 if (w
->filename
!= NULL
)
4661 minfo ("%s", w
->filename
);
4664 if (w
->filenames_sorted
)
4668 for (sec
= w
->section_list
; sec
; sec
= sec
->next
)
4670 int closing_paren
= 0;
4672 switch (sec
->spec
.sorted
)
4678 minfo ("SORT_BY_NAME(");
4683 minfo ("SORT_BY_ALIGNMENT(");
4687 case by_name_alignment
:
4688 minfo ("SORT_BY_NAME(SORT_BY_ALIGNMENT(");
4692 case by_alignment_name
:
4693 minfo ("SORT_BY_ALIGNMENT(SORT_BY_NAME(");
4698 minfo ("SORT_NONE(");
4702 case by_init_priority
:
4703 minfo ("SORT_BY_INIT_PRIORITY(");
4708 if (sec
->spec
.exclude_name_list
!= NULL
)
4711 minfo ("EXCLUDE_FILE(%s", sec
->spec
.exclude_name_list
->name
);
4712 for (tmp
= sec
->spec
.exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
4713 minfo (" %s", tmp
->name
);
4716 if (sec
->spec
.name
!= NULL
)
4717 minfo ("%s", sec
->spec
.name
);
4720 for (;closing_paren
> 0; closing_paren
--)
4729 print_statement_list (w
->children
.head
, os
);
4732 /* Print a group statement. */
4735 print_group (lang_group_statement_type
*s
,
4736 lang_output_section_statement_type
*os
)
4738 fprintf (config
.map_file
, "START GROUP\n");
4739 print_statement_list (s
->children
.head
, os
);
4740 fprintf (config
.map_file
, "END GROUP\n");
4743 /* Print the list of statements in S.
4744 This can be called for any statement type. */
4747 print_statement_list (lang_statement_union_type
*s
,
4748 lang_output_section_statement_type
*os
)
4752 print_statement (s
, os
);
4757 /* Print the first statement in statement list S.
4758 This can be called for any statement type. */
4761 print_statement (lang_statement_union_type
*s
,
4762 lang_output_section_statement_type
*os
)
4764 switch (s
->header
.type
)
4767 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
4770 case lang_constructors_statement_enum
:
4771 if (constructor_list
.head
!= NULL
)
4773 if (constructors_sorted
)
4774 minfo (" SORT (CONSTRUCTORS)\n");
4776 minfo (" CONSTRUCTORS\n");
4777 print_statement_list (constructor_list
.head
, os
);
4780 case lang_wild_statement_enum
:
4781 print_wild_statement (&s
->wild_statement
, os
);
4783 case lang_address_statement_enum
:
4784 print_address_statement (&s
->address_statement
);
4786 case lang_object_symbols_statement_enum
:
4787 minfo (" CREATE_OBJECT_SYMBOLS\n");
4789 case lang_fill_statement_enum
:
4790 print_fill_statement (&s
->fill_statement
);
4792 case lang_data_statement_enum
:
4793 print_data_statement (&s
->data_statement
);
4795 case lang_reloc_statement_enum
:
4796 print_reloc_statement (&s
->reloc_statement
);
4798 case lang_input_section_enum
:
4799 print_input_section (s
->input_section
.section
, FALSE
);
4801 case lang_padding_statement_enum
:
4802 print_padding_statement (&s
->padding_statement
);
4804 case lang_output_section_statement_enum
:
4805 print_output_section_statement (&s
->output_section_statement
);
4807 case lang_assignment_statement_enum
:
4808 print_assignment (&s
->assignment_statement
, os
);
4810 case lang_target_statement_enum
:
4811 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
4813 case lang_output_statement_enum
:
4814 minfo ("OUTPUT(%s", s
->output_statement
.name
);
4815 if (output_target
!= NULL
)
4816 minfo (" %s", output_target
);
4819 case lang_input_statement_enum
:
4820 print_input_statement (&s
->input_statement
);
4822 case lang_group_statement_enum
:
4823 print_group (&s
->group_statement
, os
);
4825 case lang_insert_statement_enum
:
4826 minfo ("INSERT %s %s\n",
4827 s
->insert_statement
.is_before
? "BEFORE" : "AFTER",
4828 s
->insert_statement
.where
);
4834 print_statements (void)
4836 print_statement_list (statement_list
.head
, abs_output_section
);
4839 /* Print the first N statements in statement list S to STDERR.
4840 If N == 0, nothing is printed.
4841 If N < 0, the entire list is printed.
4842 Intended to be called from GDB. */
4845 dprint_statement (lang_statement_union_type
*s
, int n
)
4847 FILE *map_save
= config
.map_file
;
4849 config
.map_file
= stderr
;
4852 print_statement_list (s
, abs_output_section
);
4855 while (s
&& --n
>= 0)
4857 print_statement (s
, abs_output_section
);
4862 config
.map_file
= map_save
;
4866 insert_pad (lang_statement_union_type
**ptr
,
4868 bfd_size_type alignment_needed
,
4869 asection
*output_section
,
4872 static fill_type zero_fill
;
4873 lang_statement_union_type
*pad
= NULL
;
4875 if (ptr
!= &statement_list
.head
)
4876 pad
= ((lang_statement_union_type
*)
4877 ((char *) ptr
- offsetof (lang_statement_union_type
, header
.next
)));
4879 && pad
->header
.type
== lang_padding_statement_enum
4880 && pad
->padding_statement
.output_section
== output_section
)
4882 /* Use the existing pad statement. */
4884 else if ((pad
= *ptr
) != NULL
4885 && pad
->header
.type
== lang_padding_statement_enum
4886 && pad
->padding_statement
.output_section
== output_section
)
4888 /* Use the existing pad statement. */
4892 /* Make a new padding statement, linked into existing chain. */
4893 pad
= stat_alloc (sizeof (lang_padding_statement_type
));
4894 pad
->header
.next
= *ptr
;
4896 pad
->header
.type
= lang_padding_statement_enum
;
4897 pad
->padding_statement
.output_section
= output_section
;
4900 pad
->padding_statement
.fill
= fill
;
4902 pad
->padding_statement
.output_offset
= dot
- output_section
->vma
;
4903 pad
->padding_statement
.size
= alignment_needed
;
4904 if (!(output_section
->flags
& SEC_FIXED_SIZE
))
4905 output_section
->size
= TO_SIZE (dot
+ TO_ADDR (alignment_needed
)
4906 - output_section
->vma
);
4909 /* Work out how much this section will move the dot point. */
4913 (lang_statement_union_type
**this_ptr
,
4914 lang_output_section_statement_type
*output_section_statement
,
4918 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
4919 asection
*i
= is
->section
;
4920 asection
*o
= output_section_statement
->bfd_section
;
4922 if (i
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4923 i
->output_offset
= i
->vma
- o
->vma
;
4924 else if (((i
->flags
& SEC_EXCLUDE
) != 0)
4925 || output_section_statement
->ignored
)
4926 i
->output_offset
= dot
- o
->vma
;
4929 bfd_size_type alignment_needed
;
4931 /* Align this section first to the input sections requirement,
4932 then to the output section's requirement. If this alignment
4933 is greater than any seen before, then record it too. Perform
4934 the alignment by inserting a magic 'padding' statement. */
4936 if (output_section_statement
->subsection_alignment
!= NULL
)
4938 = exp_get_power (output_section_statement
->subsection_alignment
,
4939 "subsection alignment");
4941 if (o
->alignment_power
< i
->alignment_power
)
4942 o
->alignment_power
= i
->alignment_power
;
4944 alignment_needed
= align_power (dot
, i
->alignment_power
) - dot
;
4946 if (alignment_needed
!= 0)
4948 insert_pad (this_ptr
, fill
, TO_SIZE (alignment_needed
), o
, dot
);
4949 dot
+= alignment_needed
;
4952 /* Remember where in the output section this input section goes. */
4953 i
->output_offset
= dot
- o
->vma
;
4955 /* Mark how big the output section must be to contain this now. */
4956 dot
+= TO_ADDR (i
->size
);
4957 if (!(o
->flags
& SEC_FIXED_SIZE
))
4958 o
->size
= TO_SIZE (dot
- o
->vma
);
4971 sort_sections_by_lma (const void *arg1
, const void *arg2
)
4973 const asection
*sec1
= ((const struct check_sec
*) arg1
)->sec
;
4974 const asection
*sec2
= ((const struct check_sec
*) arg2
)->sec
;
4976 if (sec1
->lma
< sec2
->lma
)
4978 else if (sec1
->lma
> sec2
->lma
)
4980 else if (sec1
->id
< sec2
->id
)
4982 else if (sec1
->id
> sec2
->id
)
4989 sort_sections_by_vma (const void *arg1
, const void *arg2
)
4991 const asection
*sec1
= ((const struct check_sec
*) arg1
)->sec
;
4992 const asection
*sec2
= ((const struct check_sec
*) arg2
)->sec
;
4994 if (sec1
->vma
< sec2
->vma
)
4996 else if (sec1
->vma
> sec2
->vma
)
4998 else if (sec1
->id
< sec2
->id
)
5000 else if (sec1
->id
> sec2
->id
)
5006 #define IS_TBSS(s) \
5007 ((s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == SEC_THREAD_LOCAL)
5009 #define IGNORE_SECTION(s) \
5010 ((s->flags & SEC_ALLOC) == 0 || IS_TBSS (s))
5012 /* Check to see if any allocated sections overlap with other allocated
5013 sections. This can happen if a linker script specifies the output
5014 section addresses of the two sections. Also check whether any memory
5015 region has overflowed. */
5018 lang_check_section_addresses (void)
5021 struct check_sec
*sections
;
5026 bfd_vma p_start
= 0;
5028 lang_memory_region_type
*m
;
5029 bfd_boolean overlays
;
5031 /* Detect address space overflow on allocated sections. */
5032 addr_mask
= ((bfd_vma
) 1 <<
5033 (bfd_arch_bits_per_address (link_info
.output_bfd
) - 1)) - 1;
5034 addr_mask
= (addr_mask
<< 1) + 1;
5035 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5036 if ((s
->flags
& SEC_ALLOC
) != 0)
5038 s_end
= (s
->vma
+ s
->size
) & addr_mask
;
5039 if (s_end
!= 0 && s_end
< (s
->vma
& addr_mask
))
5040 einfo (_("%X%P: section %s VMA wraps around address space\n"),
5044 s_end
= (s
->lma
+ s
->size
) & addr_mask
;
5045 if (s_end
!= 0 && s_end
< (s
->lma
& addr_mask
))
5046 einfo (_("%X%P: section %s LMA wraps around address space\n"),
5051 if (bfd_count_sections (link_info
.output_bfd
) <= 1)
5054 count
= bfd_count_sections (link_info
.output_bfd
);
5055 sections
= XNEWVEC (struct check_sec
, count
);
5057 /* Scan all sections in the output list. */
5059 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5061 if (IGNORE_SECTION (s
)
5065 sections
[count
].sec
= s
;
5066 sections
[count
].warned
= FALSE
;
5076 qsort (sections
, count
, sizeof (*sections
), sort_sections_by_lma
);
5078 /* First check section LMAs. There should be no overlap of LMAs on
5079 loadable sections, even with overlays. */
5080 for (p
= NULL
, i
= 0; i
< count
; i
++)
5082 s
= sections
[i
].sec
;
5083 if ((s
->flags
& SEC_LOAD
) != 0)
5086 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
5088 /* Look for an overlap. We have sorted sections by lma, so
5089 we know that s_start >= p_start. Besides the obvious
5090 case of overlap when the current section starts before
5091 the previous one ends, we also must have overlap if the
5092 previous section wraps around the address space. */
5094 && (s_start
<= p_end
5095 || p_end
< p_start
))
5097 einfo (_("%X%P: section %s LMA [%V,%V]"
5098 " overlaps section %s LMA [%V,%V]\n"),
5099 s
->name
, s_start
, s_end
, p
->name
, p_start
, p_end
);
5100 sections
[i
].warned
= TRUE
;
5108 /* If any non-zero size allocated section (excluding tbss) starts at
5109 exactly the same VMA as another such section, then we have
5110 overlays. Overlays generated by the OVERLAY keyword will have
5111 this property. It is possible to intentionally generate overlays
5112 that fail this test, but it would be unusual. */
5113 qsort (sections
, count
, sizeof (*sections
), sort_sections_by_vma
);
5115 p_start
= sections
[0].sec
->vma
;
5116 for (i
= 1; i
< count
; i
++)
5118 s_start
= sections
[i
].sec
->vma
;
5119 if (p_start
== s_start
)
5127 /* Now check section VMAs if no overlays were detected. */
5130 for (p
= NULL
, i
= 0; i
< count
; i
++)
5132 s
= sections
[i
].sec
;
5134 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
5137 && !sections
[i
].warned
5138 && (s_start
<= p_end
5139 || p_end
< p_start
))
5140 einfo (_("%X%P: section %s VMA [%V,%V]"
5141 " overlaps section %s VMA [%V,%V]\n"),
5142 s
->name
, s_start
, s_end
, p
->name
, p_start
, p_end
);
5151 /* If any memory region has overflowed, report by how much.
5152 We do not issue this diagnostic for regions that had sections
5153 explicitly placed outside their bounds; os_region_check's
5154 diagnostics are adequate for that case.
5156 FIXME: It is conceivable that m->current - (m->origin + m->length)
5157 might overflow a 32-bit integer. There is, alas, no way to print
5158 a bfd_vma quantity in decimal. */
5159 for (m
= lang_memory_region_list
; m
; m
= m
->next
)
5160 if (m
->had_full_message
)
5162 unsigned long over
= m
->current
- (m
->origin
+ m
->length
);
5163 einfo (ngettext ("%X%P: region `%s' overflowed by %lu byte\n",
5164 "%X%P: region `%s' overflowed by %lu bytes\n",
5166 m
->name_list
.name
, over
);
5170 /* Make sure the new address is within the region. We explicitly permit the
5171 current address to be at the exact end of the region when the address is
5172 non-zero, in case the region is at the end of addressable memory and the
5173 calculation wraps around. */
5176 os_region_check (lang_output_section_statement_type
*os
,
5177 lang_memory_region_type
*region
,
5181 if ((region
->current
< region
->origin
5182 || (region
->current
- region
->origin
> region
->length
))
5183 && ((region
->current
!= region
->origin
+ region
->length
)
5188 einfo (_("%X%P: address 0x%v of %pB section `%s'"
5189 " is not within region `%s'\n"),
5191 os
->bfd_section
->owner
,
5192 os
->bfd_section
->name
,
5193 region
->name_list
.name
);
5195 else if (!region
->had_full_message
)
5197 region
->had_full_message
= TRUE
;
5199 einfo (_("%X%P: %pB section `%s' will not fit in region `%s'\n"),
5200 os
->bfd_section
->owner
,
5201 os
->bfd_section
->name
,
5202 region
->name_list
.name
);
5208 ldlang_check_relro_region (lang_statement_union_type
*s
,
5209 seg_align_type
*seg
)
5211 if (seg
->relro
== exp_seg_relro_start
)
5213 if (!seg
->relro_start_stat
)
5214 seg
->relro_start_stat
= s
;
5217 ASSERT (seg
->relro_start_stat
== s
);
5220 else if (seg
->relro
== exp_seg_relro_end
)
5222 if (!seg
->relro_end_stat
)
5223 seg
->relro_end_stat
= s
;
5226 ASSERT (seg
->relro_end_stat
== s
);
5231 /* Set the sizes for all the output sections. */
5234 lang_size_sections_1
5235 (lang_statement_union_type
**prev
,
5236 lang_output_section_statement_type
*output_section_statement
,
5240 bfd_boolean check_regions
)
5242 lang_statement_union_type
*s
;
5244 /* Size up the sections from their constituent parts. */
5245 for (s
= *prev
; s
!= NULL
; s
= s
->header
.next
)
5247 switch (s
->header
.type
)
5249 case lang_output_section_statement_enum
:
5251 bfd_vma newdot
, after
, dotdelta
;
5252 lang_output_section_statement_type
*os
;
5253 lang_memory_region_type
*r
;
5254 int section_alignment
= 0;
5256 os
= &s
->output_section_statement
;
5257 if (os
->constraint
== -1)
5260 /* FIXME: We shouldn't need to zero section vmas for ld -r
5261 here, in lang_insert_orphan, or in the default linker scripts.
5262 This is covering for coff backend linker bugs. See PR6945. */
5263 if (os
->addr_tree
== NULL
5264 && bfd_link_relocatable (&link_info
)
5265 && (bfd_get_flavour (link_info
.output_bfd
)
5266 == bfd_target_coff_flavour
))
5267 os
->addr_tree
= exp_intop (0);
5268 if (os
->addr_tree
!= NULL
)
5270 os
->processed_vma
= FALSE
;
5271 exp_fold_tree (os
->addr_tree
, bfd_abs_section_ptr
, &dot
);
5273 if (expld
.result
.valid_p
)
5275 dot
= expld
.result
.value
;
5276 if (expld
.result
.section
!= NULL
)
5277 dot
+= expld
.result
.section
->vma
;
5279 else if (expld
.phase
!= lang_mark_phase_enum
)
5280 einfo (_("%F%P:%pS: non constant or forward reference"
5281 " address expression for section %s\n"),
5282 os
->addr_tree
, os
->name
);
5285 if (os
->bfd_section
== NULL
)
5286 /* This section was removed or never actually created. */
5289 /* If this is a COFF shared library section, use the size and
5290 address from the input section. FIXME: This is COFF
5291 specific; it would be cleaner if there were some other way
5292 to do this, but nothing simple comes to mind. */
5293 if (((bfd_get_flavour (link_info
.output_bfd
)
5294 == bfd_target_ecoff_flavour
)
5295 || (bfd_get_flavour (link_info
.output_bfd
)
5296 == bfd_target_coff_flavour
))
5297 && (os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
5301 if (os
->children
.head
== NULL
5302 || os
->children
.head
->header
.next
!= NULL
5303 || (os
->children
.head
->header
.type
5304 != lang_input_section_enum
))
5305 einfo (_("%X%P: internal error on COFF shared library"
5306 " section %s\n"), os
->name
);
5308 input
= os
->children
.head
->input_section
.section
;
5309 bfd_set_section_vma (os
->bfd_section
->owner
,
5311 bfd_section_vma (input
->owner
, input
));
5312 if (!(os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
5313 os
->bfd_section
->size
= input
->size
;
5319 if (bfd_is_abs_section (os
->bfd_section
))
5321 /* No matter what happens, an abs section starts at zero. */
5322 ASSERT (os
->bfd_section
->vma
== 0);
5326 if (os
->addr_tree
== NULL
)
5328 /* No address specified for this section, get one
5329 from the region specification. */
5330 if (os
->region
== NULL
5331 || ((os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))
5332 && os
->region
->name_list
.name
[0] == '*'
5333 && strcmp (os
->region
->name_list
.name
,
5334 DEFAULT_MEMORY_REGION
) == 0))
5336 os
->region
= lang_memory_default (os
->bfd_section
);
5339 /* If a loadable section is using the default memory
5340 region, and some non default memory regions were
5341 defined, issue an error message. */
5343 && !IGNORE_SECTION (os
->bfd_section
)
5344 && !bfd_link_relocatable (&link_info
)
5346 && strcmp (os
->region
->name_list
.name
,
5347 DEFAULT_MEMORY_REGION
) == 0
5348 && lang_memory_region_list
!= NULL
5349 && (strcmp (lang_memory_region_list
->name_list
.name
,
5350 DEFAULT_MEMORY_REGION
) != 0
5351 || lang_memory_region_list
->next
!= NULL
)
5352 && expld
.phase
!= lang_mark_phase_enum
)
5354 /* By default this is an error rather than just a
5355 warning because if we allocate the section to the
5356 default memory region we can end up creating an
5357 excessively large binary, or even seg faulting when
5358 attempting to perform a negative seek. See
5359 sources.redhat.com/ml/binutils/2003-04/msg00423.html
5360 for an example of this. This behaviour can be
5361 overridden by the using the --no-check-sections
5363 if (command_line
.check_section_addresses
)
5364 einfo (_("%F%P: error: no memory region specified"
5365 " for loadable section `%s'\n"),
5366 bfd_get_section_name (link_info
.output_bfd
,
5369 einfo (_("%P: warning: no memory region specified"
5370 " for loadable section `%s'\n"),
5371 bfd_get_section_name (link_info
.output_bfd
,
5375 newdot
= os
->region
->current
;
5376 section_alignment
= os
->bfd_section
->alignment_power
;
5379 section_alignment
= exp_get_power (os
->section_alignment
,
5380 "section alignment");
5382 /* Align to what the section needs. */
5383 if (section_alignment
> 0)
5385 bfd_vma savedot
= newdot
;
5386 newdot
= align_power (newdot
, section_alignment
);
5388 dotdelta
= newdot
- savedot
;
5390 && (config
.warn_section_align
5391 || os
->addr_tree
!= NULL
)
5392 && expld
.phase
!= lang_mark_phase_enum
)
5393 einfo (ngettext ("%P: warning: changing start of "
5394 "section %s by %lu byte\n",
5395 "%P: warning: changing start of "
5396 "section %s by %lu bytes\n",
5397 (unsigned long) dotdelta
),
5398 os
->name
, (unsigned long) dotdelta
);
5401 bfd_set_section_vma (0, os
->bfd_section
, newdot
);
5403 os
->bfd_section
->output_offset
= 0;
5406 lang_size_sections_1 (&os
->children
.head
, os
,
5407 os
->fill
, newdot
, relax
, check_regions
);
5409 os
->processed_vma
= TRUE
;
5411 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
5412 /* Except for some special linker created sections,
5413 no output section should change from zero size
5414 after strip_excluded_output_sections. A non-zero
5415 size on an ignored section indicates that some
5416 input section was not sized early enough. */
5417 ASSERT (os
->bfd_section
->size
== 0);
5420 dot
= os
->bfd_section
->vma
;
5422 /* Put the section within the requested block size, or
5423 align at the block boundary. */
5425 + TO_ADDR (os
->bfd_section
->size
)
5426 + os
->block_value
- 1)
5427 & - (bfd_vma
) os
->block_value
);
5429 if (!(os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
5430 os
->bfd_section
->size
= TO_SIZE (after
5431 - os
->bfd_section
->vma
);
5434 /* Set section lma. */
5437 r
= lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, FALSE
);
5441 bfd_vma lma
= exp_get_abs_int (os
->load_base
, 0, "load base");
5442 os
->bfd_section
->lma
= lma
;
5444 else if (os
->lma_region
!= NULL
)
5446 bfd_vma lma
= os
->lma_region
->current
;
5448 if (os
->align_lma_with_input
)
5452 /* When LMA_REGION is the same as REGION, align the LMA
5453 as we did for the VMA, possibly including alignment
5454 from the bfd section. If a different region, then
5455 only align according to the value in the output
5457 if (os
->lma_region
!= os
->region
)
5458 section_alignment
= exp_get_power (os
->section_alignment
,
5459 "section alignment");
5460 if (section_alignment
> 0)
5461 lma
= align_power (lma
, section_alignment
);
5463 os
->bfd_section
->lma
= lma
;
5465 else if (r
->last_os
!= NULL
5466 && (os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
5471 last
= r
->last_os
->output_section_statement
.bfd_section
;
5473 /* A backwards move of dot should be accompanied by
5474 an explicit assignment to the section LMA (ie.
5475 os->load_base set) because backwards moves can
5476 create overlapping LMAs. */
5478 && os
->bfd_section
->size
!= 0
5479 && dot
+ TO_ADDR (os
->bfd_section
->size
) <= last
->vma
)
5481 /* If dot moved backwards then leave lma equal to
5482 vma. This is the old default lma, which might
5483 just happen to work when the backwards move is
5484 sufficiently large. Nag if this changes anything,
5485 so people can fix their linker scripts. */
5487 if (last
->vma
!= last
->lma
)
5488 einfo (_("%P: warning: dot moved backwards "
5489 "before `%s'\n"), os
->name
);
5493 /* If this is an overlay, set the current lma to that
5494 at the end of the previous section. */
5495 if (os
->sectype
== overlay_section
)
5496 lma
= last
->lma
+ TO_ADDR (last
->size
);
5498 /* Otherwise, keep the same lma to vma relationship
5499 as the previous section. */
5501 lma
= dot
+ last
->lma
- last
->vma
;
5503 if (section_alignment
> 0)
5504 lma
= align_power (lma
, section_alignment
);
5505 os
->bfd_section
->lma
= lma
;
5508 os
->processed_lma
= TRUE
;
5510 /* Keep track of normal sections using the default
5511 lma region. We use this to set the lma for
5512 following sections. Overlays or other linker
5513 script assignment to lma might mean that the
5514 default lma == vma is incorrect.
5515 To avoid warnings about dot moving backwards when using
5516 -Ttext, don't start tracking sections until we find one
5517 of non-zero size or with lma set differently to vma.
5518 Do this tracking before we short-cut the loop so that we
5519 track changes for the case where the section size is zero,
5520 but the lma is set differently to the vma. This is
5521 important, if an orphan section is placed after an
5522 otherwise empty output section that has an explicit lma
5523 set, we want that lma reflected in the orphans lma. */
5524 if (((!IGNORE_SECTION (os
->bfd_section
)
5525 && (os
->bfd_section
->size
!= 0
5526 || (r
->last_os
== NULL
5527 && os
->bfd_section
->vma
!= os
->bfd_section
->lma
)
5528 || (r
->last_os
!= NULL
5529 && dot
>= (r
->last_os
->output_section_statement
5530 .bfd_section
->vma
))))
5531 || os
->sectype
== first_overlay_section
)
5532 && os
->lma_region
== NULL
5533 && !bfd_link_relocatable (&link_info
))
5536 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
5539 /* .tbss sections effectively have zero size. */
5540 if (!IS_TBSS (os
->bfd_section
)
5541 || bfd_link_relocatable (&link_info
))
5542 dotdelta
= TO_ADDR (os
->bfd_section
->size
);
5547 if (os
->update_dot_tree
!= 0)
5548 exp_fold_tree (os
->update_dot_tree
, bfd_abs_section_ptr
, &dot
);
5550 /* Update dot in the region ?
5551 We only do this if the section is going to be allocated,
5552 since unallocated sections do not contribute to the region's
5553 overall size in memory. */
5554 if (os
->region
!= NULL
5555 && (os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
)))
5557 os
->region
->current
= dot
;
5560 /* Make sure the new address is within the region. */
5561 os_region_check (os
, os
->region
, os
->addr_tree
,
5562 os
->bfd_section
->vma
);
5564 if (os
->lma_region
!= NULL
&& os
->lma_region
!= os
->region
5565 && ((os
->bfd_section
->flags
& SEC_LOAD
)
5566 || os
->align_lma_with_input
))
5568 os
->lma_region
->current
= os
->bfd_section
->lma
+ dotdelta
;
5571 os_region_check (os
, os
->lma_region
, NULL
,
5572 os
->bfd_section
->lma
);
5578 case lang_constructors_statement_enum
:
5579 dot
= lang_size_sections_1 (&constructor_list
.head
,
5580 output_section_statement
,
5581 fill
, dot
, relax
, check_regions
);
5584 case lang_data_statement_enum
:
5586 unsigned int size
= 0;
5588 s
->data_statement
.output_offset
=
5589 dot
- output_section_statement
->bfd_section
->vma
;
5590 s
->data_statement
.output_section
=
5591 output_section_statement
->bfd_section
;
5593 /* We might refer to provided symbols in the expression, and
5594 need to mark them as needed. */
5595 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
5597 switch (s
->data_statement
.type
)
5615 if (size
< TO_SIZE ((unsigned) 1))
5616 size
= TO_SIZE ((unsigned) 1);
5617 dot
+= TO_ADDR (size
);
5618 if (!(output_section_statement
->bfd_section
->flags
5620 output_section_statement
->bfd_section
->size
5621 = TO_SIZE (dot
- output_section_statement
->bfd_section
->vma
);
5626 case lang_reloc_statement_enum
:
5630 s
->reloc_statement
.output_offset
=
5631 dot
- output_section_statement
->bfd_section
->vma
;
5632 s
->reloc_statement
.output_section
=
5633 output_section_statement
->bfd_section
;
5634 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
5635 dot
+= TO_ADDR (size
);
5636 if (!(output_section_statement
->bfd_section
->flags
5638 output_section_statement
->bfd_section
->size
5639 = TO_SIZE (dot
- output_section_statement
->bfd_section
->vma
);
5643 case lang_wild_statement_enum
:
5644 dot
= lang_size_sections_1 (&s
->wild_statement
.children
.head
,
5645 output_section_statement
,
5646 fill
, dot
, relax
, check_regions
);
5649 case lang_object_symbols_statement_enum
:
5650 link_info
.create_object_symbols_section
5651 = output_section_statement
->bfd_section
;
5652 output_section_statement
->bfd_section
->flags
|= SEC_KEEP
;
5655 case lang_output_statement_enum
:
5656 case lang_target_statement_enum
:
5659 case lang_input_section_enum
:
5663 i
= s
->input_section
.section
;
5668 if (!bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
5669 einfo (_("%F%P: can't relax section: %E\n"));
5673 dot
= size_input_section (prev
, output_section_statement
,
5678 case lang_input_statement_enum
:
5681 case lang_fill_statement_enum
:
5682 s
->fill_statement
.output_section
=
5683 output_section_statement
->bfd_section
;
5685 fill
= s
->fill_statement
.fill
;
5688 case lang_assignment_statement_enum
:
5690 bfd_vma newdot
= dot
;
5691 etree_type
*tree
= s
->assignment_statement
.exp
;
5693 expld
.dataseg
.relro
= exp_seg_relro_none
;
5695 exp_fold_tree (tree
,
5696 output_section_statement
->bfd_section
,
5699 ldlang_check_relro_region (s
, &expld
.dataseg
);
5701 expld
.dataseg
.relro
= exp_seg_relro_none
;
5703 /* This symbol may be relative to this section. */
5704 if ((tree
->type
.node_class
== etree_provided
5705 || tree
->type
.node_class
== etree_assign
)
5706 && (tree
->assign
.dst
[0] != '.'
5707 || tree
->assign
.dst
[1] != '\0'))
5708 output_section_statement
->update_dot
= 1;
5710 if (!output_section_statement
->ignored
)
5712 if (output_section_statement
== abs_output_section
)
5714 /* If we don't have an output section, then just adjust
5715 the default memory address. */
5716 lang_memory_region_lookup (DEFAULT_MEMORY_REGION
,
5717 FALSE
)->current
= newdot
;
5719 else if (newdot
!= dot
)
5721 /* Insert a pad after this statement. We can't
5722 put the pad before when relaxing, in case the
5723 assignment references dot. */
5724 insert_pad (&s
->header
.next
, fill
, TO_SIZE (newdot
- dot
),
5725 output_section_statement
->bfd_section
, dot
);
5727 /* Don't neuter the pad below when relaxing. */
5730 /* If dot is advanced, this implies that the section
5731 should have space allocated to it, unless the
5732 user has explicitly stated that the section
5733 should not be allocated. */
5734 if (output_section_statement
->sectype
!= noalloc_section
5735 && (output_section_statement
->sectype
!= noload_section
5736 || (bfd_get_flavour (link_info
.output_bfd
)
5737 == bfd_target_elf_flavour
)))
5738 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
;
5745 case lang_padding_statement_enum
:
5746 /* If this is the first time lang_size_sections is called,
5747 we won't have any padding statements. If this is the
5748 second or later passes when relaxing, we should allow
5749 padding to shrink. If padding is needed on this pass, it
5750 will be added back in. */
5751 s
->padding_statement
.size
= 0;
5753 /* Make sure output_offset is valid. If relaxation shrinks
5754 the section and this pad isn't needed, it's possible to
5755 have output_offset larger than the final size of the
5756 section. bfd_set_section_contents will complain even for
5757 a pad size of zero. */
5758 s
->padding_statement
.output_offset
5759 = dot
- output_section_statement
->bfd_section
->vma
;
5762 case lang_group_statement_enum
:
5763 dot
= lang_size_sections_1 (&s
->group_statement
.children
.head
,
5764 output_section_statement
,
5765 fill
, dot
, relax
, check_regions
);
5768 case lang_insert_statement_enum
:
5771 /* We can only get here when relaxing is turned on. */
5772 case lang_address_statement_enum
:
5779 prev
= &s
->header
.next
;
5784 /* Callback routine that is used in _bfd_elf_map_sections_to_segments.
5785 The BFD library has set NEW_SEGMENT to TRUE iff it thinks that
5786 CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different
5787 segments. We are allowed an opportunity to override this decision. */
5790 ldlang_override_segment_assignment (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
5791 bfd
*abfd ATTRIBUTE_UNUSED
,
5792 asection
*current_section
,
5793 asection
*previous_section
,
5794 bfd_boolean new_segment
)
5796 lang_output_section_statement_type
*cur
;
5797 lang_output_section_statement_type
*prev
;
5799 /* The checks below are only necessary when the BFD library has decided
5800 that the two sections ought to be placed into the same segment. */
5804 /* Paranoia checks. */
5805 if (current_section
== NULL
|| previous_section
== NULL
)
5808 /* If this flag is set, the target never wants code and non-code
5809 sections comingled in the same segment. */
5810 if (config
.separate_code
5811 && ((current_section
->flags
^ previous_section
->flags
) & SEC_CODE
))
5814 /* Find the memory regions associated with the two sections.
5815 We call lang_output_section_find() here rather than scanning the list
5816 of output sections looking for a matching section pointer because if
5817 we have a large number of sections then a hash lookup is faster. */
5818 cur
= lang_output_section_find (current_section
->name
);
5819 prev
= lang_output_section_find (previous_section
->name
);
5821 /* More paranoia. */
5822 if (cur
== NULL
|| prev
== NULL
)
5825 /* If the regions are different then force the sections to live in
5826 different segments. See the email thread starting at the following
5827 URL for the reasons why this is necessary:
5828 http://sourceware.org/ml/binutils/2007-02/msg00216.html */
5829 return cur
->region
!= prev
->region
;
5833 one_lang_size_sections_pass (bfd_boolean
*relax
, bfd_boolean check_regions
)
5835 lang_statement_iteration
++;
5836 lang_size_sections_1 (&statement_list
.head
, abs_output_section
,
5837 0, 0, relax
, check_regions
);
5841 lang_size_segment (seg_align_type
*seg
)
5843 /* If XXX_SEGMENT_ALIGN XXX_SEGMENT_END pair was seen, check whether
5844 a page could be saved in the data segment. */
5845 bfd_vma first
, last
;
5847 first
= -seg
->base
& (seg
->pagesize
- 1);
5848 last
= seg
->end
& (seg
->pagesize
- 1);
5850 && ((seg
->base
& ~(seg
->pagesize
- 1))
5851 != (seg
->end
& ~(seg
->pagesize
- 1)))
5852 && first
+ last
<= seg
->pagesize
)
5854 seg
->phase
= exp_seg_adjust
;
5858 seg
->phase
= exp_seg_done
;
5863 lang_size_relro_segment_1 (seg_align_type
*seg
)
5865 bfd_vma relro_end
, desired_end
;
5868 /* Compute the expected PT_GNU_RELRO/PT_LOAD segment end. */
5869 relro_end
= ((seg
->relro_end
+ seg
->pagesize
- 1)
5870 & ~(seg
->pagesize
- 1));
5872 /* Adjust by the offset arg of XXX_SEGMENT_RELRO_END. */
5873 desired_end
= relro_end
- seg
->relro_offset
;
5875 /* For sections in the relro segment.. */
5876 for (sec
= link_info
.output_bfd
->section_last
; sec
; sec
= sec
->prev
)
5877 if ((sec
->flags
& SEC_ALLOC
) != 0
5878 && sec
->vma
>= seg
->base
5879 && sec
->vma
< seg
->relro_end
- seg
->relro_offset
)
5881 /* Where do we want to put this section so that it ends as
5883 bfd_vma start
, end
, bump
;
5885 end
= start
= sec
->vma
;
5887 end
+= TO_ADDR (sec
->size
);
5888 bump
= desired_end
- end
;
5889 /* We'd like to increase START by BUMP, but we must heed
5890 alignment so the increase might be less than optimum. */
5892 start
&= ~(((bfd_vma
) 1 << sec
->alignment_power
) - 1);
5893 /* This is now the desired end for the previous section. */
5894 desired_end
= start
;
5897 seg
->phase
= exp_seg_relro_adjust
;
5898 ASSERT (desired_end
>= seg
->base
);
5899 seg
->base
= desired_end
;
5904 lang_size_relro_segment (bfd_boolean
*relax
, bfd_boolean check_regions
)
5906 bfd_boolean do_reset
= FALSE
;
5907 bfd_boolean do_data_relro
;
5908 bfd_vma data_initial_base
, data_relro_end
;
5910 if (link_info
.relro
&& expld
.dataseg
.relro_end
)
5912 do_data_relro
= TRUE
;
5913 data_initial_base
= expld
.dataseg
.base
;
5914 data_relro_end
= lang_size_relro_segment_1 (&expld
.dataseg
);
5918 do_data_relro
= FALSE
;
5919 data_initial_base
= data_relro_end
= 0;
5924 lang_reset_memory_regions ();
5925 one_lang_size_sections_pass (relax
, check_regions
);
5927 /* Assignments to dot, or to output section address in a user
5928 script have increased padding over the original. Revert. */
5929 if (do_data_relro
&& expld
.dataseg
.relro_end
> data_relro_end
)
5931 expld
.dataseg
.base
= data_initial_base
;;
5936 if (!do_data_relro
&& lang_size_segment (&expld
.dataseg
))
5943 lang_size_sections (bfd_boolean
*relax
, bfd_boolean check_regions
)
5945 expld
.phase
= lang_allocating_phase_enum
;
5946 expld
.dataseg
.phase
= exp_seg_none
;
5948 one_lang_size_sections_pass (relax
, check_regions
);
5950 if (expld
.dataseg
.phase
!= exp_seg_end_seen
)
5951 expld
.dataseg
.phase
= exp_seg_done
;
5953 if (expld
.dataseg
.phase
== exp_seg_end_seen
)
5955 bfd_boolean do_reset
5956 = lang_size_relro_segment (relax
, check_regions
);
5960 lang_reset_memory_regions ();
5961 one_lang_size_sections_pass (relax
, check_regions
);
5964 if (link_info
.relro
&& expld
.dataseg
.relro_end
)
5966 link_info
.relro_start
= expld
.dataseg
.base
;
5967 link_info
.relro_end
= expld
.dataseg
.relro_end
;
5972 static lang_output_section_statement_type
*current_section
;
5973 static lang_assignment_statement_type
*current_assign
;
5974 static bfd_boolean prefer_next_section
;
5976 /* Worker function for lang_do_assignments. Recursiveness goes here. */
5979 lang_do_assignments_1 (lang_statement_union_type
*s
,
5980 lang_output_section_statement_type
*current_os
,
5983 bfd_boolean
*found_end
)
5985 for (; s
!= NULL
; s
= s
->header
.next
)
5987 switch (s
->header
.type
)
5989 case lang_constructors_statement_enum
:
5990 dot
= lang_do_assignments_1 (constructor_list
.head
,
5991 current_os
, fill
, dot
, found_end
);
5994 case lang_output_section_statement_enum
:
5996 lang_output_section_statement_type
*os
;
5999 os
= &(s
->output_section_statement
);
6000 os
->after_end
= *found_end
;
6001 if (os
->bfd_section
!= NULL
&& !os
->ignored
)
6003 if ((os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
6005 current_section
= os
;
6006 prefer_next_section
= FALSE
;
6008 dot
= os
->bfd_section
->vma
;
6010 newdot
= lang_do_assignments_1 (os
->children
.head
,
6011 os
, os
->fill
, dot
, found_end
);
6014 if (os
->bfd_section
!= NULL
)
6016 /* .tbss sections effectively have zero size. */
6017 if (!IS_TBSS (os
->bfd_section
)
6018 || bfd_link_relocatable (&link_info
))
6019 dot
+= TO_ADDR (os
->bfd_section
->size
);
6021 if (os
->update_dot_tree
!= NULL
)
6022 exp_fold_tree (os
->update_dot_tree
,
6023 bfd_abs_section_ptr
, &dot
);
6031 case lang_wild_statement_enum
:
6033 dot
= lang_do_assignments_1 (s
->wild_statement
.children
.head
,
6034 current_os
, fill
, dot
, found_end
);
6037 case lang_object_symbols_statement_enum
:
6038 case lang_output_statement_enum
:
6039 case lang_target_statement_enum
:
6042 case lang_data_statement_enum
:
6043 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
6044 if (expld
.result
.valid_p
)
6046 s
->data_statement
.value
= expld
.result
.value
;
6047 if (expld
.result
.section
!= NULL
)
6048 s
->data_statement
.value
+= expld
.result
.section
->vma
;
6050 else if (expld
.phase
== lang_final_phase_enum
)
6051 einfo (_("%F%P: invalid data statement\n"));
6054 switch (s
->data_statement
.type
)
6072 if (size
< TO_SIZE ((unsigned) 1))
6073 size
= TO_SIZE ((unsigned) 1);
6074 dot
+= TO_ADDR (size
);
6078 case lang_reloc_statement_enum
:
6079 exp_fold_tree (s
->reloc_statement
.addend_exp
,
6080 bfd_abs_section_ptr
, &dot
);
6081 if (expld
.result
.valid_p
)
6082 s
->reloc_statement
.addend_value
= expld
.result
.value
;
6083 else if (expld
.phase
== lang_final_phase_enum
)
6084 einfo (_("%F%P: invalid reloc statement\n"));
6085 dot
+= TO_ADDR (bfd_get_reloc_size (s
->reloc_statement
.howto
));
6088 case lang_input_section_enum
:
6090 asection
*in
= s
->input_section
.section
;
6092 if ((in
->flags
& SEC_EXCLUDE
) == 0)
6093 dot
+= TO_ADDR (in
->size
);
6097 case lang_input_statement_enum
:
6100 case lang_fill_statement_enum
:
6101 fill
= s
->fill_statement
.fill
;
6104 case lang_assignment_statement_enum
:
6105 current_assign
= &s
->assignment_statement
;
6106 if (current_assign
->exp
->type
.node_class
!= etree_assert
)
6108 const char *p
= current_assign
->exp
->assign
.dst
;
6110 if (current_os
== abs_output_section
&& p
[0] == '.' && p
[1] == 0)
6111 prefer_next_section
= TRUE
;
6115 if (strcmp (p
, "end") == 0)
6118 exp_fold_tree (s
->assignment_statement
.exp
,
6119 (current_os
->bfd_section
!= NULL
6120 ? current_os
->bfd_section
: bfd_und_section_ptr
),
6124 case lang_padding_statement_enum
:
6125 dot
+= TO_ADDR (s
->padding_statement
.size
);
6128 case lang_group_statement_enum
:
6129 dot
= lang_do_assignments_1 (s
->group_statement
.children
.head
,
6130 current_os
, fill
, dot
, found_end
);
6133 case lang_insert_statement_enum
:
6136 case lang_address_statement_enum
:
6148 lang_do_assignments (lang_phase_type phase
)
6150 bfd_boolean found_end
= FALSE
;
6152 current_section
= NULL
;
6153 prefer_next_section
= FALSE
;
6154 expld
.phase
= phase
;
6155 lang_statement_iteration
++;
6156 lang_do_assignments_1 (statement_list
.head
,
6157 abs_output_section
, NULL
, 0, &found_end
);
6160 /* For an assignment statement outside of an output section statement,
6161 choose the best of neighbouring output sections to use for values
6165 section_for_dot (void)
6169 /* Assignments belong to the previous output section, unless there
6170 has been an assignment to "dot", in which case following
6171 assignments belong to the next output section. (The assumption
6172 is that an assignment to "dot" is setting up the address for the
6173 next output section.) Except that past the assignment to "_end"
6174 we always associate with the previous section. This exception is
6175 for targets like SH that define an alloc .stack or other
6176 weirdness after non-alloc sections. */
6177 if (current_section
== NULL
|| prefer_next_section
)
6179 lang_statement_union_type
*stmt
;
6180 lang_output_section_statement_type
*os
;
6182 for (stmt
= (lang_statement_union_type
*) current_assign
;
6184 stmt
= stmt
->header
.next
)
6185 if (stmt
->header
.type
== lang_output_section_statement_enum
)
6188 os
= &stmt
->output_section_statement
;
6191 && (os
->bfd_section
== NULL
6192 || (os
->bfd_section
->flags
& SEC_EXCLUDE
) != 0
6193 || bfd_section_removed_from_list (link_info
.output_bfd
,
6197 if (current_section
== NULL
|| os
== NULL
|| !os
->after_end
)
6200 s
= os
->bfd_section
;
6202 s
= link_info
.output_bfd
->section_last
;
6204 && ((s
->flags
& SEC_ALLOC
) == 0
6205 || (s
->flags
& SEC_THREAD_LOCAL
) != 0))
6210 return bfd_abs_section_ptr
;
6214 s
= current_section
->bfd_section
;
6216 /* The section may have been stripped. */
6218 && ((s
->flags
& SEC_EXCLUDE
) != 0
6219 || (s
->flags
& SEC_ALLOC
) == 0
6220 || (s
->flags
& SEC_THREAD_LOCAL
) != 0
6221 || bfd_section_removed_from_list (link_info
.output_bfd
, s
)))
6224 s
= link_info
.output_bfd
->sections
;
6226 && ((s
->flags
& SEC_ALLOC
) == 0
6227 || (s
->flags
& SEC_THREAD_LOCAL
) != 0))
6232 return bfd_abs_section_ptr
;
6235 /* Array of __start/__stop/.startof./.sizeof/ symbols. */
6237 static struct bfd_link_hash_entry
**start_stop_syms
;
6238 static size_t start_stop_count
= 0;
6239 static size_t start_stop_alloc
= 0;
6241 /* Give start/stop SYMBOL for SEC a preliminary definition, and add it
6242 to start_stop_syms. */
6245 lang_define_start_stop (const char *symbol
, asection
*sec
)
6247 struct bfd_link_hash_entry
*h
;
6249 h
= bfd_define_start_stop (link_info
.output_bfd
, &link_info
, symbol
, sec
);
6252 if (start_stop_count
== start_stop_alloc
)
6254 start_stop_alloc
= 2 * start_stop_alloc
+ 10;
6256 = xrealloc (start_stop_syms
,
6257 start_stop_alloc
* sizeof (*start_stop_syms
));
6259 start_stop_syms
[start_stop_count
++] = h
;
6263 /* Check for input sections whose names match references to
6264 __start_SECNAME or __stop_SECNAME symbols. Give the symbols
6265 preliminary definitions. */
6268 lang_init_start_stop (void)
6272 char leading_char
= bfd_get_symbol_leading_char (link_info
.output_bfd
);
6274 for (abfd
= link_info
.input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
6275 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
6278 const char *secname
= s
->name
;
6280 for (ps
= secname
; *ps
!= '\0'; ps
++)
6281 if (!ISALNUM ((unsigned char) *ps
) && *ps
!= '_')
6285 char *symbol
= (char *) xmalloc (10 + strlen (secname
));
6287 symbol
[0] = leading_char
;
6288 sprintf (symbol
+ (leading_char
!= 0), "__start_%s", secname
);
6289 lang_define_start_stop (symbol
, s
);
6291 symbol
[1] = leading_char
;
6292 memcpy (symbol
+ 1 + (leading_char
!= 0), "__stop", 6);
6293 lang_define_start_stop (symbol
+ 1, s
);
6300 /* Iterate over start_stop_syms. */
6303 foreach_start_stop (void (*func
) (struct bfd_link_hash_entry
*))
6307 for (i
= 0; i
< start_stop_count
; ++i
)
6308 func (start_stop_syms
[i
]);
6311 /* __start and __stop symbols are only supposed to be defined by the
6312 linker for orphan sections, but we now extend that to sections that
6313 map to an output section of the same name. The symbols were
6314 defined early for --gc-sections, before we mapped input to output
6315 sections, so undo those that don't satisfy this rule. */
6318 undef_start_stop (struct bfd_link_hash_entry
*h
)
6320 if (h
->ldscript_def
)
6323 if (h
->u
.def
.section
->output_section
== NULL
6324 || h
->u
.def
.section
->output_section
->owner
!= link_info
.output_bfd
6325 || strcmp (h
->u
.def
.section
->name
,
6326 h
->u
.def
.section
->output_section
->name
) != 0)
6328 asection
*sec
= bfd_get_section_by_name (link_info
.output_bfd
,
6329 h
->u
.def
.section
->name
);
6332 /* When there are more than one input sections with the same
6333 section name, SECNAME, linker picks the first one to define
6334 __start_SECNAME and __stop_SECNAME symbols. When the first
6335 input section is removed by comdat group, we need to check
6336 if there is still an output section with section name
6339 for (i
= sec
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
6340 if (strcmp (h
->u
.def
.section
->name
, i
->name
) == 0)
6342 h
->u
.def
.section
= i
;
6346 h
->type
= bfd_link_hash_undefined
;
6347 h
->u
.undef
.abfd
= NULL
;
6352 lang_undef_start_stop (void)
6354 foreach_start_stop (undef_start_stop
);
6357 /* Check for output sections whose names match references to
6358 .startof.SECNAME or .sizeof.SECNAME symbols. Give the symbols
6359 preliminary definitions. */
6362 lang_init_startof_sizeof (void)
6366 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6368 const char *secname
= s
->name
;
6369 char *symbol
= (char *) xmalloc (10 + strlen (secname
));
6371 sprintf (symbol
, ".startof.%s", secname
);
6372 lang_define_start_stop (symbol
, s
);
6374 memcpy (symbol
+ 1, ".size", 5);
6375 lang_define_start_stop (symbol
+ 1, s
);
6380 /* Set .startof., .sizeof., __start and __stop symbols final values. */
6383 set_start_stop (struct bfd_link_hash_entry
*h
)
6386 || h
->type
!= bfd_link_hash_defined
)
6389 if (h
->root
.string
[0] == '.')
6391 /* .startof. or .sizeof. symbol.
6392 .startof. already has final value. */
6393 if (h
->root
.string
[2] == 'i')
6396 h
->u
.def
.value
= TO_ADDR (h
->u
.def
.section
->size
);
6397 h
->u
.def
.section
= bfd_abs_section_ptr
;
6402 /* __start or __stop symbol. */
6403 int has_lead
= bfd_get_symbol_leading_char (link_info
.output_bfd
) != 0;
6405 h
->u
.def
.section
= h
->u
.def
.section
->output_section
;
6406 if (h
->root
.string
[4 + has_lead
] == 'o')
6409 h
->u
.def
.value
= TO_ADDR (h
->u
.def
.section
->size
);
6415 lang_finalize_start_stop (void)
6417 foreach_start_stop (set_start_stop
);
6423 struct bfd_link_hash_entry
*h
;
6426 if ((bfd_link_relocatable (&link_info
) && !link_info
.gc_sections
)
6427 || bfd_link_dll (&link_info
))
6428 warn
= entry_from_cmdline
;
6432 /* Force the user to specify a root when generating a relocatable with
6433 --gc-sections, unless --gc-keep-exported was also given. */
6434 if (bfd_link_relocatable (&link_info
)
6435 && link_info
.gc_sections
6436 && !link_info
.gc_keep_exported
6437 && !(entry_from_cmdline
|| undef_from_cmdline
))
6438 einfo (_("%F%P: gc-sections requires either an entry or "
6439 "an undefined symbol\n"));
6441 if (entry_symbol
.name
== NULL
)
6443 /* No entry has been specified. Look for the default entry, but
6444 don't warn if we don't find it. */
6445 entry_symbol
.name
= entry_symbol_default
;
6449 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
.name
,
6450 FALSE
, FALSE
, TRUE
);
6452 && (h
->type
== bfd_link_hash_defined
6453 || h
->type
== bfd_link_hash_defweak
)
6454 && h
->u
.def
.section
->output_section
!= NULL
)
6458 val
= (h
->u
.def
.value
6459 + bfd_get_section_vma (link_info
.output_bfd
,
6460 h
->u
.def
.section
->output_section
)
6461 + h
->u
.def
.section
->output_offset
);
6462 if (!bfd_set_start_address (link_info
.output_bfd
, val
))
6463 einfo (_("%F%P: %s: can't set start address\n"), entry_symbol
.name
);
6470 /* We couldn't find the entry symbol. Try parsing it as a
6472 val
= bfd_scan_vma (entry_symbol
.name
, &send
, 0);
6475 if (!bfd_set_start_address (link_info
.output_bfd
, val
))
6476 einfo (_("%F%P: can't set start address\n"));
6482 /* Can't find the entry symbol, and it's not a number. Use
6483 the first address in the text section. */
6484 ts
= bfd_get_section_by_name (link_info
.output_bfd
, entry_section
);
6488 einfo (_("%P: warning: cannot find entry symbol %s;"
6489 " defaulting to %V\n"),
6491 bfd_get_section_vma (link_info
.output_bfd
, ts
));
6492 if (!(bfd_set_start_address
6493 (link_info
.output_bfd
,
6494 bfd_get_section_vma (link_info
.output_bfd
, ts
))))
6495 einfo (_("%F%P: can't set start address\n"));
6500 einfo (_("%P: warning: cannot find entry symbol %s;"
6501 " not setting start address\n"),
6508 /* This is a small function used when we want to ignore errors from
6512 ignore_bfd_errors (const char *fmt ATTRIBUTE_UNUSED
,
6513 va_list ap ATTRIBUTE_UNUSED
)
6515 /* Don't do anything. */
6518 /* Check that the architecture of all the input files is compatible
6519 with the output file. Also call the backend to let it do any
6520 other checking that is needed. */
6525 lang_input_statement_type
*file
;
6527 const bfd_arch_info_type
*compatible
;
6529 for (file
= &file_chain
.head
->input_statement
;
6533 #ifdef ENABLE_PLUGINS
6534 /* Don't check format of files claimed by plugin. */
6535 if (file
->flags
.claimed
)
6537 #endif /* ENABLE_PLUGINS */
6538 input_bfd
= file
->the_bfd
;
6540 = bfd_arch_get_compatible (input_bfd
, link_info
.output_bfd
,
6541 command_line
.accept_unknown_input_arch
);
6543 /* In general it is not possible to perform a relocatable
6544 link between differing object formats when the input
6545 file has relocations, because the relocations in the
6546 input format may not have equivalent representations in
6547 the output format (and besides BFD does not translate
6548 relocs for other link purposes than a final link). */
6549 if ((bfd_link_relocatable (&link_info
)
6550 || link_info
.emitrelocations
)
6551 && (compatible
== NULL
6552 || (bfd_get_flavour (input_bfd
)
6553 != bfd_get_flavour (link_info
.output_bfd
)))
6554 && (bfd_get_file_flags (input_bfd
) & HAS_RELOC
) != 0)
6556 einfo (_("%F%P: relocatable linking with relocations from"
6557 " format %s (%pB) to format %s (%pB) is not supported\n"),
6558 bfd_get_target (input_bfd
), input_bfd
,
6559 bfd_get_target (link_info
.output_bfd
), link_info
.output_bfd
);
6560 /* einfo with %F exits. */
6563 if (compatible
== NULL
)
6565 if (command_line
.warn_mismatch
)
6566 einfo (_("%X%P: %s architecture of input file `%pB'"
6567 " is incompatible with %s output\n"),
6568 bfd_printable_name (input_bfd
), input_bfd
,
6569 bfd_printable_name (link_info
.output_bfd
));
6571 else if (bfd_count_sections (input_bfd
))
6573 /* If the input bfd has no contents, it shouldn't set the
6574 private data of the output bfd. */
6576 bfd_error_handler_type pfn
= NULL
;
6578 /* If we aren't supposed to warn about mismatched input
6579 files, temporarily set the BFD error handler to a
6580 function which will do nothing. We still want to call
6581 bfd_merge_private_bfd_data, since it may set up
6582 information which is needed in the output file. */
6583 if (!command_line
.warn_mismatch
)
6584 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
6585 if (!bfd_merge_private_bfd_data (input_bfd
, &link_info
))
6587 if (command_line
.warn_mismatch
)
6588 einfo (_("%X%P: failed to merge target specific data"
6589 " of file %pB\n"), input_bfd
);
6591 if (!command_line
.warn_mismatch
)
6592 bfd_set_error_handler (pfn
);
6597 /* Look through all the global common symbols and attach them to the
6598 correct section. The -sort-common command line switch may be used
6599 to roughly sort the entries by alignment. */
6604 if (link_info
.inhibit_common_definition
)
6606 if (bfd_link_relocatable (&link_info
)
6607 && !command_line
.force_common_definition
)
6610 if (!config
.sort_common
)
6611 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, NULL
);
6616 if (config
.sort_common
== sort_descending
)
6618 for (power
= 4; power
> 0; power
--)
6619 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
6622 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
6626 for (power
= 0; power
<= 4; power
++)
6627 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
6629 power
= (unsigned int) -1;
6630 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
6635 /* Place one common symbol in the correct section. */
6638 lang_one_common (struct bfd_link_hash_entry
*h
, void *info
)
6640 unsigned int power_of_two
;
6644 if (h
->type
!= bfd_link_hash_common
)
6648 power_of_two
= h
->u
.c
.p
->alignment_power
;
6650 if (config
.sort_common
== sort_descending
6651 && power_of_two
< *(unsigned int *) info
)
6653 else if (config
.sort_common
== sort_ascending
6654 && power_of_two
> *(unsigned int *) info
)
6657 section
= h
->u
.c
.p
->section
;
6658 if (!bfd_define_common_symbol (link_info
.output_bfd
, &link_info
, h
))
6659 einfo (_("%F%P: could not define common symbol `%pT': %E\n"),
6662 if (config
.map_file
!= NULL
)
6664 static bfd_boolean header_printed
;
6669 if (!header_printed
)
6671 minfo (_("\nAllocating common symbols\n"));
6672 minfo (_("Common symbol size file\n\n"));
6673 header_printed
= TRUE
;
6676 name
= bfd_demangle (link_info
.output_bfd
, h
->root
.string
,
6677 DMGL_ANSI
| DMGL_PARAMS
);
6680 minfo ("%s", h
->root
.string
);
6681 len
= strlen (h
->root
.string
);
6686 len
= strlen (name
);
6702 if (size
<= 0xffffffff)
6703 sprintf (buf
, "%lx", (unsigned long) size
);
6705 sprintf_vma (buf
, size
);
6715 minfo ("%pB\n", section
->owner
);
6721 /* Handle a single orphan section S, placing the orphan into an appropriate
6722 output section. The effects of the --orphan-handling command line
6723 option are handled here. */
6726 ldlang_place_orphan (asection
*s
)
6728 if (config
.orphan_handling
== orphan_handling_discard
)
6730 lang_output_section_statement_type
*os
;
6731 os
= lang_output_section_statement_lookup (DISCARD_SECTION_NAME
, 0,
6733 if (os
->addr_tree
== NULL
6734 && (bfd_link_relocatable (&link_info
)
6735 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0))
6736 os
->addr_tree
= exp_intop (0);
6737 lang_add_section (&os
->children
, s
, NULL
, os
);
6741 lang_output_section_statement_type
*os
;
6742 const char *name
= s
->name
;
6745 if (config
.orphan_handling
== orphan_handling_error
)
6746 einfo (_("%X%P: error: unplaced orphan section `%pA' from `%pB'\n"),
6749 if (config
.unique_orphan_sections
|| unique_section_p (s
, NULL
))
6750 constraint
= SPECIAL
;
6752 os
= ldemul_place_orphan (s
, name
, constraint
);
6755 os
= lang_output_section_statement_lookup (name
, constraint
, TRUE
);
6756 if (os
->addr_tree
== NULL
6757 && (bfd_link_relocatable (&link_info
)
6758 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0))
6759 os
->addr_tree
= exp_intop (0);
6760 lang_add_section (&os
->children
, s
, NULL
, os
);
6763 if (config
.orphan_handling
== orphan_handling_warn
)
6764 einfo (_("%P: warning: orphan section `%pA' from `%pB' being "
6765 "placed in section `%s'\n"),
6766 s
, s
->owner
, os
->name
);
6770 /* Run through the input files and ensure that every input section has
6771 somewhere to go. If one is found without a destination then create
6772 an input request and place it into the statement tree. */
6775 lang_place_orphans (void)
6777 LANG_FOR_EACH_INPUT_STATEMENT (file
)
6781 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6783 if (s
->output_section
== NULL
)
6785 /* This section of the file is not attached, root
6786 around for a sensible place for it to go. */
6788 if (file
->flags
.just_syms
)
6789 bfd_link_just_syms (file
->the_bfd
, s
, &link_info
);
6790 else if (lang_discard_section_p (s
))
6791 s
->output_section
= bfd_abs_section_ptr
;
6792 else if (strcmp (s
->name
, "COMMON") == 0)
6794 /* This is a lonely common section which must have
6795 come from an archive. We attach to the section
6796 with the wildcard. */
6797 if (!bfd_link_relocatable (&link_info
)
6798 || command_line
.force_common_definition
)
6800 if (default_common_section
== NULL
)
6801 default_common_section
6802 = lang_output_section_statement_lookup (".bss", 0,
6804 lang_add_section (&default_common_section
->children
, s
,
6805 NULL
, default_common_section
);
6809 ldlang_place_orphan (s
);
6816 lang_set_flags (lang_memory_region_type
*ptr
, const char *flags
, int invert
)
6818 flagword
*ptr_flags
;
6820 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
6826 /* PR 17900: An exclamation mark in the attributes reverses
6827 the sense of any of the attributes that follow. */
6830 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
6834 *ptr_flags
|= SEC_ALLOC
;
6838 *ptr_flags
|= SEC_READONLY
;
6842 *ptr_flags
|= SEC_DATA
;
6846 *ptr_flags
|= SEC_CODE
;
6851 *ptr_flags
|= SEC_LOAD
;
6855 einfo (_("%F%P: invalid character %c (%d) in flags\n"),
6863 /* Call a function on each real input file. This function will be
6864 called on an archive, but not on the elements. */
6867 lang_for_each_input_file (void (*func
) (lang_input_statement_type
*))
6869 lang_input_statement_type
*f
;
6871 for (f
= &input_file_chain
.head
->input_statement
;
6873 f
= f
->next_real_file
)
6878 /* Call a function on each real file. The function will be called on
6879 all the elements of an archive which are included in the link, but
6880 will not be called on the archive file itself. */
6883 lang_for_each_file (void (*func
) (lang_input_statement_type
*))
6885 LANG_FOR_EACH_INPUT_STATEMENT (f
)
6893 ldlang_add_file (lang_input_statement_type
*entry
)
6895 lang_statement_append (&file_chain
, entry
, &entry
->next
);
6897 /* The BFD linker needs to have a list of all input BFDs involved in
6899 ASSERT (entry
->the_bfd
->link
.next
== NULL
);
6900 ASSERT (entry
->the_bfd
!= link_info
.output_bfd
);
6902 *link_info
.input_bfds_tail
= entry
->the_bfd
;
6903 link_info
.input_bfds_tail
= &entry
->the_bfd
->link
.next
;
6904 entry
->the_bfd
->usrdata
= entry
;
6905 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
6907 /* Look through the sections and check for any which should not be
6908 included in the link. We need to do this now, so that we can
6909 notice when the backend linker tries to report multiple
6910 definition errors for symbols which are in sections we aren't
6911 going to link. FIXME: It might be better to entirely ignore
6912 symbols which are defined in sections which are going to be
6913 discarded. This would require modifying the backend linker for
6914 each backend which might set the SEC_LINK_ONCE flag. If we do
6915 this, we should probably handle SEC_EXCLUDE in the same way. */
6917 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, entry
);
6921 lang_add_output (const char *name
, int from_script
)
6923 /* Make -o on command line override OUTPUT in script. */
6924 if (!had_output_filename
|| !from_script
)
6926 output_filename
= name
;
6927 had_output_filename
= TRUE
;
6931 lang_output_section_statement_type
*
6932 lang_enter_output_section_statement (const char *output_section_statement_name
,
6933 etree_type
*address_exp
,
6934 enum section_type sectype
,
6936 etree_type
*subalign
,
6939 int align_with_input
)
6941 lang_output_section_statement_type
*os
;
6943 os
= lang_output_section_statement_lookup (output_section_statement_name
,
6945 current_section
= os
;
6947 if (os
->addr_tree
== NULL
)
6949 os
->addr_tree
= address_exp
;
6951 os
->sectype
= sectype
;
6952 if (sectype
!= noload_section
)
6953 os
->flags
= SEC_NO_FLAGS
;
6955 os
->flags
= SEC_NEVER_LOAD
;
6956 os
->block_value
= 1;
6958 /* Make next things chain into subchain of this. */
6959 push_stat_ptr (&os
->children
);
6961 os
->align_lma_with_input
= align_with_input
== ALIGN_WITH_INPUT
;
6962 if (os
->align_lma_with_input
&& align
!= NULL
)
6963 einfo (_("%F%P:%pS: error: align with input and explicit align specified\n"),
6966 os
->subsection_alignment
= subalign
;
6967 os
->section_alignment
= align
;
6969 os
->load_base
= ebase
;
6976 lang_output_statement_type
*new_stmt
;
6978 new_stmt
= new_stat (lang_output_statement
, stat_ptr
);
6979 new_stmt
->name
= output_filename
;
6982 /* Reset the current counters in the regions. */
6985 lang_reset_memory_regions (void)
6987 lang_memory_region_type
*p
= lang_memory_region_list
;
6989 lang_output_section_statement_type
*os
;
6991 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
6993 p
->current
= p
->origin
;
6997 for (os
= &lang_os_list
.head
->output_section_statement
;
7001 os
->processed_vma
= FALSE
;
7002 os
->processed_lma
= FALSE
;
7005 for (o
= link_info
.output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
7007 /* Save the last size for possible use by bfd_relax_section. */
7008 o
->rawsize
= o
->size
;
7009 if (!(o
->flags
& SEC_FIXED_SIZE
))
7014 /* Worker for lang_gc_sections_1. */
7017 gc_section_callback (lang_wild_statement_type
*ptr
,
7018 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
7020 struct flag_info
*sflag_info ATTRIBUTE_UNUSED
,
7021 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
7022 void *data ATTRIBUTE_UNUSED
)
7024 /* If the wild pattern was marked KEEP, the member sections
7025 should be as well. */
7026 if (ptr
->keep_sections
)
7027 section
->flags
|= SEC_KEEP
;
7030 /* Iterate over sections marking them against GC. */
7033 lang_gc_sections_1 (lang_statement_union_type
*s
)
7035 for (; s
!= NULL
; s
= s
->header
.next
)
7037 switch (s
->header
.type
)
7039 case lang_wild_statement_enum
:
7040 walk_wild (&s
->wild_statement
, gc_section_callback
, NULL
);
7042 case lang_constructors_statement_enum
:
7043 lang_gc_sections_1 (constructor_list
.head
);
7045 case lang_output_section_statement_enum
:
7046 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
7048 case lang_group_statement_enum
:
7049 lang_gc_sections_1 (s
->group_statement
.children
.head
);
7058 lang_gc_sections (void)
7060 /* Keep all sections so marked in the link script. */
7061 lang_gc_sections_1 (statement_list
.head
);
7063 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
7064 the special case of debug info. (See bfd/stabs.c)
7065 Twiddle the flag here, to simplify later linker code. */
7066 if (bfd_link_relocatable (&link_info
))
7068 LANG_FOR_EACH_INPUT_STATEMENT (f
)
7071 #ifdef ENABLE_PLUGINS
7072 if (f
->flags
.claimed
)
7075 for (sec
= f
->the_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7076 if ((sec
->flags
& SEC_DEBUGGING
) == 0)
7077 sec
->flags
&= ~SEC_EXCLUDE
;
7081 if (link_info
.gc_sections
)
7082 bfd_gc_sections (link_info
.output_bfd
, &link_info
);
7085 /* Worker for lang_find_relro_sections_1. */
7088 find_relro_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
7089 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
7091 struct flag_info
*sflag_info ATTRIBUTE_UNUSED
,
7092 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
7095 /* Discarded, excluded and ignored sections effectively have zero
7097 if (section
->output_section
!= NULL
7098 && section
->output_section
->owner
== link_info
.output_bfd
7099 && (section
->output_section
->flags
& SEC_EXCLUDE
) == 0
7100 && !IGNORE_SECTION (section
)
7101 && section
->size
!= 0)
7103 bfd_boolean
*has_relro_section
= (bfd_boolean
*) data
;
7104 *has_relro_section
= TRUE
;
7108 /* Iterate over sections for relro sections. */
7111 lang_find_relro_sections_1 (lang_statement_union_type
*s
,
7112 seg_align_type
*seg
,
7113 bfd_boolean
*has_relro_section
)
7115 if (*has_relro_section
)
7118 for (; s
!= NULL
; s
= s
->header
.next
)
7120 if (s
== seg
->relro_end_stat
)
7123 switch (s
->header
.type
)
7125 case lang_wild_statement_enum
:
7126 walk_wild (&s
->wild_statement
,
7127 find_relro_section_callback
,
7130 case lang_constructors_statement_enum
:
7131 lang_find_relro_sections_1 (constructor_list
.head
,
7132 seg
, has_relro_section
);
7134 case lang_output_section_statement_enum
:
7135 lang_find_relro_sections_1 (s
->output_section_statement
.children
.head
,
7136 seg
, has_relro_section
);
7138 case lang_group_statement_enum
:
7139 lang_find_relro_sections_1 (s
->group_statement
.children
.head
,
7140 seg
, has_relro_section
);
7149 lang_find_relro_sections (void)
7151 bfd_boolean has_relro_section
= FALSE
;
7153 /* Check all sections in the link script. */
7155 lang_find_relro_sections_1 (expld
.dataseg
.relro_start_stat
,
7156 &expld
.dataseg
, &has_relro_section
);
7158 if (!has_relro_section
)
7159 link_info
.relro
= FALSE
;
7162 /* Relax all sections until bfd_relax_section gives up. */
7165 lang_relax_sections (bfd_boolean need_layout
)
7167 if (RELAXATION_ENABLED
)
7169 /* We may need more than one relaxation pass. */
7170 int i
= link_info
.relax_pass
;
7172 /* The backend can use it to determine the current pass. */
7173 link_info
.relax_pass
= 0;
7177 /* Keep relaxing until bfd_relax_section gives up. */
7178 bfd_boolean relax_again
;
7180 link_info
.relax_trip
= -1;
7183 link_info
.relax_trip
++;
7185 /* Note: pe-dll.c does something like this also. If you find
7186 you need to change this code, you probably need to change
7187 pe-dll.c also. DJ */
7189 /* Do all the assignments with our current guesses as to
7191 lang_do_assignments (lang_assigning_phase_enum
);
7193 /* We must do this after lang_do_assignments, because it uses
7195 lang_reset_memory_regions ();
7197 /* Perform another relax pass - this time we know where the
7198 globals are, so can make a better guess. */
7199 relax_again
= FALSE
;
7200 lang_size_sections (&relax_again
, FALSE
);
7202 while (relax_again
);
7204 link_info
.relax_pass
++;
7211 /* Final extra sizing to report errors. */
7212 lang_do_assignments (lang_assigning_phase_enum
);
7213 lang_reset_memory_regions ();
7214 lang_size_sections (NULL
, TRUE
);
7218 #ifdef ENABLE_PLUGINS
7219 /* Find the insert point for the plugin's replacement files. We
7220 place them after the first claimed real object file, or if the
7221 first claimed object is an archive member, after the last real
7222 object file immediately preceding the archive. In the event
7223 no objects have been claimed at all, we return the first dummy
7224 object file on the list as the insert point; that works, but
7225 the callee must be careful when relinking the file_chain as it
7226 is not actually on that chain, only the statement_list and the
7227 input_file list; in that case, the replacement files must be
7228 inserted at the head of the file_chain. */
7230 static lang_input_statement_type
*
7231 find_replacements_insert_point (bfd_boolean
*before
)
7233 lang_input_statement_type
*claim1
, *lastobject
;
7234 lastobject
= &input_file_chain
.head
->input_statement
;
7235 for (claim1
= &file_chain
.head
->input_statement
;
7237 claim1
= claim1
->next
)
7239 if (claim1
->flags
.claimed
)
7241 *before
= claim1
->flags
.claim_archive
;
7242 return claim1
->flags
.claim_archive
? lastobject
: claim1
;
7244 /* Update lastobject if this is a real object file. */
7245 if (claim1
->the_bfd
!= NULL
&& claim1
->the_bfd
->my_archive
== NULL
)
7246 lastobject
= claim1
;
7248 /* No files were claimed by the plugin. Choose the last object
7249 file found on the list (maybe the first, dummy entry) as the
7255 /* Find where to insert ADD, an archive element or shared library
7256 added during a rescan. */
7258 static lang_input_statement_type
**
7259 find_rescan_insertion (lang_input_statement_type
*add
)
7261 bfd
*add_bfd
= add
->the_bfd
;
7262 lang_input_statement_type
*f
;
7263 lang_input_statement_type
*last_loaded
= NULL
;
7264 lang_input_statement_type
*before
= NULL
;
7265 lang_input_statement_type
**iter
= NULL
;
7267 if (add_bfd
->my_archive
!= NULL
)
7268 add_bfd
= add_bfd
->my_archive
;
7270 /* First look through the input file chain, to find an object file
7271 before the one we've rescanned. Normal object files always
7272 appear on both the input file chain and the file chain, so this
7273 lets us get quickly to somewhere near the correct place on the
7274 file chain if it is full of archive elements. Archives don't
7275 appear on the file chain, but if an element has been extracted
7276 then their input_statement->next points at it. */
7277 for (f
= &input_file_chain
.head
->input_statement
;
7279 f
= f
->next_real_file
)
7281 if (f
->the_bfd
== add_bfd
)
7283 before
= last_loaded
;
7284 if (f
->next
!= NULL
)
7285 return &f
->next
->next
;
7287 if (f
->the_bfd
!= NULL
&& f
->next
!= NULL
)
7291 for (iter
= before
? &before
->next
: &file_chain
.head
->input_statement
.next
;
7293 iter
= &(*iter
)->next
)
7294 if (!(*iter
)->flags
.claim_archive
7295 && (*iter
)->the_bfd
->my_archive
== NULL
)
7301 /* Insert SRCLIST into DESTLIST after given element by chaining
7302 on FIELD as the next-pointer. (Counterintuitively does not need
7303 a pointer to the actual after-node itself, just its chain field.) */
7306 lang_list_insert_after (lang_statement_list_type
*destlist
,
7307 lang_statement_list_type
*srclist
,
7308 lang_statement_union_type
**field
)
7310 *(srclist
->tail
) = *field
;
7311 *field
= srclist
->head
;
7312 if (destlist
->tail
== field
)
7313 destlist
->tail
= srclist
->tail
;
7316 /* Detach new nodes added to DESTLIST since the time ORIGLIST
7317 was taken as a copy of it and leave them in ORIGLIST. */
7320 lang_list_remove_tail (lang_statement_list_type
*destlist
,
7321 lang_statement_list_type
*origlist
)
7323 union lang_statement_union
**savetail
;
7324 /* Check that ORIGLIST really is an earlier state of DESTLIST. */
7325 ASSERT (origlist
->head
== destlist
->head
);
7326 savetail
= origlist
->tail
;
7327 origlist
->head
= *(savetail
);
7328 origlist
->tail
= destlist
->tail
;
7329 destlist
->tail
= savetail
;
7333 static lang_statement_union_type
**
7334 find_next_input_statement (lang_statement_union_type
**s
)
7336 for ( ; *s
; s
= &(*s
)->header
.next
)
7338 lang_statement_union_type
**t
;
7339 switch ((*s
)->header
.type
)
7341 case lang_input_statement_enum
:
7343 case lang_wild_statement_enum
:
7344 t
= &(*s
)->wild_statement
.children
.head
;
7346 case lang_group_statement_enum
:
7347 t
= &(*s
)->group_statement
.children
.head
;
7349 case lang_output_section_statement_enum
:
7350 t
= &(*s
)->output_section_statement
.children
.head
;
7355 t
= find_next_input_statement (t
);
7361 #endif /* ENABLE_PLUGINS */
7363 /* Add NAME to the list of garbage collection entry points. */
7366 lang_add_gc_name (const char *name
)
7368 struct bfd_sym_chain
*sym
;
7373 sym
= stat_alloc (sizeof (*sym
));
7375 sym
->next
= link_info
.gc_sym_list
;
7377 link_info
.gc_sym_list
= sym
;
7380 /* Check relocations. */
7383 lang_check_relocs (void)
7385 if (link_info
.check_relocs_after_open_input
)
7389 for (abfd
= link_info
.input_bfds
;
7390 abfd
!= (bfd
*) NULL
; abfd
= abfd
->link
.next
)
7391 if (!bfd_link_check_relocs (abfd
, &link_info
))
7393 /* No object output, fail return. */
7394 config
.make_executable
= FALSE
;
7395 /* Note: we do not abort the loop, but rather
7396 continue the scan in case there are other
7397 bad relocations to report. */
7402 /* Look through all output sections looking for places where we can
7403 propagate forward the lma region. */
7406 lang_propagate_lma_regions (void)
7408 lang_output_section_statement_type
*os
;
7410 for (os
= &lang_os_list
.head
->output_section_statement
;
7414 if (os
->prev
!= NULL
7415 && os
->lma_region
== NULL
7416 && os
->load_base
== NULL
7417 && os
->addr_tree
== NULL
7418 && os
->region
== os
->prev
->region
)
7419 os
->lma_region
= os
->prev
->lma_region
;
7426 /* Finalize dynamic list. */
7427 if (link_info
.dynamic_list
)
7428 lang_finalize_version_expr_head (&link_info
.dynamic_list
->head
);
7430 current_target
= default_target
;
7432 /* Open the output file. */
7433 lang_for_each_statement (ldlang_open_output
);
7436 ldemul_create_output_section_statements ();
7438 /* Add to the hash table all undefineds on the command line. */
7439 lang_place_undefineds ();
7441 if (!bfd_section_already_linked_table_init ())
7442 einfo (_("%F%P: can not create hash table: %E\n"));
7444 /* Create a bfd for each input file. */
7445 current_target
= default_target
;
7446 lang_statement_iteration
++;
7447 open_input_bfds (statement_list
.head
, OPEN_BFD_NORMAL
);
7448 /* open_input_bfds also handles assignments, so we can give values
7449 to symbolic origin/length now. */
7450 lang_do_memory_regions ();
7452 #ifdef ENABLE_PLUGINS
7453 if (link_info
.lto_plugin_active
)
7455 lang_statement_list_type added
;
7456 lang_statement_list_type files
, inputfiles
;
7458 /* Now all files are read, let the plugin(s) decide if there
7459 are any more to be added to the link before we call the
7460 emulation's after_open hook. We create a private list of
7461 input statements for this purpose, which we will eventually
7462 insert into the global statement list after the first claimed
7465 /* We need to manipulate all three chains in synchrony. */
7467 inputfiles
= input_file_chain
;
7468 if (plugin_call_all_symbols_read ())
7469 einfo (_("%F%P: %s: plugin reported error after all symbols read\n"),
7470 plugin_error_plugin ());
7471 /* Open any newly added files, updating the file chains. */
7472 plugin_undefs
= link_info
.hash
->undefs_tail
;
7473 open_input_bfds (*added
.tail
, OPEN_BFD_NORMAL
);
7474 if (plugin_undefs
== link_info
.hash
->undefs_tail
)
7475 plugin_undefs
= NULL
;
7476 /* Restore the global list pointer now they have all been added. */
7477 lang_list_remove_tail (stat_ptr
, &added
);
7478 /* And detach the fresh ends of the file lists. */
7479 lang_list_remove_tail (&file_chain
, &files
);
7480 lang_list_remove_tail (&input_file_chain
, &inputfiles
);
7481 /* Were any new files added? */
7482 if (added
.head
!= NULL
)
7484 /* If so, we will insert them into the statement list immediately
7485 after the first input file that was claimed by the plugin,
7486 unless that file was an archive in which case it is inserted
7487 immediately before. */
7489 lang_statement_union_type
**prev
;
7490 plugin_insert
= find_replacements_insert_point (&before
);
7491 /* If a plugin adds input files without having claimed any, we
7492 don't really have a good idea where to place them. Just putting
7493 them at the start or end of the list is liable to leave them
7494 outside the crtbegin...crtend range. */
7495 ASSERT (plugin_insert
!= NULL
);
7496 /* Splice the new statement list into the old one. */
7497 prev
= &plugin_insert
->header
.next
;
7500 prev
= find_next_input_statement (prev
);
7501 if (*prev
!= (void *) plugin_insert
->next_real_file
)
7503 /* We didn't find the expected input statement.
7504 This can happen due to lookup_name creating input
7505 statements not linked into the statement list. */
7506 prev
= &plugin_insert
->header
.next
;
7509 lang_list_insert_after (stat_ptr
, &added
, prev
);
7510 /* Likewise for the file chains. */
7511 lang_list_insert_after (&input_file_chain
, &inputfiles
,
7512 (void *) &plugin_insert
->next_real_file
);
7513 /* We must be careful when relinking file_chain; we may need to
7514 insert the new files at the head of the list if the insert
7515 point chosen is the dummy first input file. */
7516 if (plugin_insert
->filename
)
7517 lang_list_insert_after (&file_chain
, &files
,
7518 (void *) &plugin_insert
->next
);
7520 lang_list_insert_after (&file_chain
, &files
, &file_chain
.head
);
7522 /* Rescan archives in case new undefined symbols have appeared. */
7524 lang_statement_iteration
++;
7525 open_input_bfds (statement_list
.head
, OPEN_BFD_RESCAN
);
7526 lang_list_remove_tail (&file_chain
, &files
);
7527 while (files
.head
!= NULL
)
7529 lang_input_statement_type
**insert
;
7530 lang_input_statement_type
**iter
, *temp
;
7533 insert
= find_rescan_insertion (&files
.head
->input_statement
);
7534 /* All elements from an archive can be added at once. */
7535 iter
= &files
.head
->input_statement
.next
;
7536 my_arch
= files
.head
->input_statement
.the_bfd
->my_archive
;
7537 if (my_arch
!= NULL
)
7538 for (; *iter
!= NULL
; iter
= &(*iter
)->next
)
7539 if ((*iter
)->the_bfd
->my_archive
!= my_arch
)
7542 *insert
= &files
.head
->input_statement
;
7543 files
.head
= (lang_statement_union_type
*) *iter
;
7545 if (my_arch
!= NULL
)
7547 lang_input_statement_type
*parent
= my_arch
->usrdata
;
7549 parent
->next
= (lang_input_statement_type
*)
7551 - offsetof (lang_input_statement_type
, next
));
7556 #endif /* ENABLE_PLUGINS */
7558 /* Make sure that nobody has tried to add a symbol to this list
7560 ASSERT (link_info
.gc_sym_list
== NULL
);
7562 link_info
.gc_sym_list
= &entry_symbol
;
7564 if (entry_symbol
.name
== NULL
)
7566 link_info
.gc_sym_list
= ldlang_undef_chain_list_head
;
7568 /* entry_symbol is normally initialied by a ENTRY definition in the
7569 linker script or the -e command line option. But if neither of
7570 these have been used, the target specific backend may still have
7571 provided an entry symbol via a call to lang_default_entry().
7572 Unfortunately this value will not be processed until lang_end()
7573 is called, long after this function has finished. So detect this
7574 case here and add the target's entry symbol to the list of starting
7575 points for garbage collection resolution. */
7576 lang_add_gc_name (entry_symbol_default
);
7579 lang_add_gc_name (link_info
.init_function
);
7580 lang_add_gc_name (link_info
.fini_function
);
7582 ldemul_after_open ();
7583 if (config
.map_file
!= NULL
)
7584 lang_print_asneeded ();
7586 bfd_section_already_linked_table_free ();
7588 /* Make sure that we're not mixing architectures. We call this
7589 after all the input files have been opened, but before we do any
7590 other processing, so that any operations merge_private_bfd_data
7591 does on the output file will be known during the rest of the
7595 /* Handle .exports instead of a version script if we're told to do so. */
7596 if (command_line
.version_exports_section
)
7597 lang_do_version_exports_section ();
7599 /* Build all sets based on the information gathered from the input
7601 ldctor_build_sets ();
7603 /* Give initial values for __start and __stop symbols, so that ELF
7604 gc_sections will keep sections referenced by these symbols. Must
7605 be done before lang_do_assignments below. */
7606 if (config
.build_constructors
)
7607 lang_init_start_stop ();
7609 /* PR 13683: We must rerun the assignments prior to running garbage
7610 collection in order to make sure that all symbol aliases are resolved. */
7611 lang_do_assignments (lang_mark_phase_enum
);
7612 expld
.phase
= lang_first_phase_enum
;
7614 /* Size up the common data. */
7617 /* Remove unreferenced sections if asked to. */
7618 lang_gc_sections ();
7620 /* Check relocations. */
7621 lang_check_relocs ();
7623 ldemul_after_check_relocs ();
7625 /* Update wild statements. */
7626 update_wild_statements (statement_list
.head
);
7628 /* Run through the contours of the script and attach input sections
7629 to the correct output sections. */
7630 lang_statement_iteration
++;
7631 map_input_to_output_sections (statement_list
.head
, NULL
, NULL
);
7633 /* Start at the statement immediately after the special abs_section
7634 output statement, so that it isn't reordered. */
7635 process_insert_statements (&lang_os_list
.head
->header
.next
);
7637 /* Find any sections not attached explicitly and handle them. */
7638 lang_place_orphans ();
7640 if (!bfd_link_relocatable (&link_info
))
7644 /* Merge SEC_MERGE sections. This has to be done after GC of
7645 sections, so that GCed sections are not merged, but before
7646 assigning dynamic symbols, since removing whole input sections
7648 bfd_merge_sections (link_info
.output_bfd
, &link_info
);
7650 /* Look for a text section and set the readonly attribute in it. */
7651 found
= bfd_get_section_by_name (link_info
.output_bfd
, ".text");
7655 if (config
.text_read_only
)
7656 found
->flags
|= SEC_READONLY
;
7658 found
->flags
&= ~SEC_READONLY
;
7662 /* Copy forward lma regions for output sections in same lma region. */
7663 lang_propagate_lma_regions ();
7665 /* Defining __start/__stop symbols early for --gc-sections to work
7666 around a glibc build problem can result in these symbols being
7667 defined when they should not be. Fix them now. */
7668 if (config
.build_constructors
)
7669 lang_undef_start_stop ();
7671 /* Define .startof./.sizeof. symbols with preliminary values before
7672 dynamic symbols are created. */
7673 if (!bfd_link_relocatable (&link_info
))
7674 lang_init_startof_sizeof ();
7676 /* Do anything special before sizing sections. This is where ELF
7677 and other back-ends size dynamic sections. */
7678 ldemul_before_allocation ();
7680 /* We must record the program headers before we try to fix the
7681 section positions, since they will affect SIZEOF_HEADERS. */
7682 lang_record_phdrs ();
7684 /* Check relro sections. */
7685 if (link_info
.relro
&& !bfd_link_relocatable (&link_info
))
7686 lang_find_relro_sections ();
7688 /* Size up the sections. */
7689 lang_size_sections (NULL
, !RELAXATION_ENABLED
);
7691 /* See if anything special should be done now we know how big
7692 everything is. This is where relaxation is done. */
7693 ldemul_after_allocation ();
7695 /* Fix any __start, __stop, .startof. or .sizeof. symbols. */
7696 lang_finalize_start_stop ();
7698 /* Do all the assignments again, to report errors. Assignment
7699 statements are processed multiple times, updating symbols; In
7700 open_input_bfds, lang_do_assignments, and lang_size_sections.
7701 Since lang_relax_sections calls lang_do_assignments, symbols are
7702 also updated in ldemul_after_allocation. */
7703 lang_do_assignments (lang_final_phase_enum
);
7707 /* Convert absolute symbols to section relative. */
7708 ldexp_finalize_syms ();
7710 /* Make sure that the section addresses make sense. */
7711 if (command_line
.check_section_addresses
)
7712 lang_check_section_addresses ();
7714 /* Check any required symbols are known. */
7715 ldlang_check_require_defined_symbols ();
7720 /* EXPORTED TO YACC */
7723 lang_add_wild (struct wildcard_spec
*filespec
,
7724 struct wildcard_list
*section_list
,
7725 bfd_boolean keep_sections
)
7727 struct wildcard_list
*curr
, *next
;
7728 lang_wild_statement_type
*new_stmt
;
7730 /* Reverse the list as the parser puts it back to front. */
7731 for (curr
= section_list
, section_list
= NULL
;
7733 section_list
= curr
, curr
= next
)
7736 curr
->next
= section_list
;
7739 if (filespec
!= NULL
&& filespec
->name
!= NULL
)
7741 if (strcmp (filespec
->name
, "*") == 0)
7742 filespec
->name
= NULL
;
7743 else if (!wildcardp (filespec
->name
))
7744 lang_has_input_file
= TRUE
;
7747 new_stmt
= new_stat (lang_wild_statement
, stat_ptr
);
7748 new_stmt
->filename
= NULL
;
7749 new_stmt
->filenames_sorted
= FALSE
;
7750 new_stmt
->section_flag_list
= NULL
;
7751 new_stmt
->exclude_name_list
= NULL
;
7752 if (filespec
!= NULL
)
7754 new_stmt
->filename
= filespec
->name
;
7755 new_stmt
->filenames_sorted
= filespec
->sorted
== by_name
;
7756 new_stmt
->section_flag_list
= filespec
->section_flag_list
;
7757 new_stmt
->exclude_name_list
= filespec
->exclude_name_list
;
7759 new_stmt
->section_list
= section_list
;
7760 new_stmt
->keep_sections
= keep_sections
;
7761 lang_list_init (&new_stmt
->children
);
7762 analyze_walk_wild_section_handler (new_stmt
);
7766 lang_section_start (const char *name
, etree_type
*address
,
7767 const segment_type
*segment
)
7769 lang_address_statement_type
*ad
;
7771 ad
= new_stat (lang_address_statement
, stat_ptr
);
7772 ad
->section_name
= name
;
7773 ad
->address
= address
;
7774 ad
->segment
= segment
;
7777 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
7778 because of a -e argument on the command line, or zero if this is
7779 called by ENTRY in a linker script. Command line arguments take
7783 lang_add_entry (const char *name
, bfd_boolean cmdline
)
7785 if (entry_symbol
.name
== NULL
7787 || !entry_from_cmdline
)
7789 entry_symbol
.name
= name
;
7790 entry_from_cmdline
= cmdline
;
7794 /* Set the default start symbol to NAME. .em files should use this,
7795 not lang_add_entry, to override the use of "start" if neither the
7796 linker script nor the command line specifies an entry point. NAME
7797 must be permanently allocated. */
7799 lang_default_entry (const char *name
)
7801 entry_symbol_default
= name
;
7805 lang_add_target (const char *name
)
7807 lang_target_statement_type
*new_stmt
;
7809 new_stmt
= new_stat (lang_target_statement
, stat_ptr
);
7810 new_stmt
->target
= name
;
7814 lang_add_map (const char *name
)
7821 map_option_f
= TRUE
;
7829 lang_add_fill (fill_type
*fill
)
7831 lang_fill_statement_type
*new_stmt
;
7833 new_stmt
= new_stat (lang_fill_statement
, stat_ptr
);
7834 new_stmt
->fill
= fill
;
7838 lang_add_data (int type
, union etree_union
*exp
)
7840 lang_data_statement_type
*new_stmt
;
7842 new_stmt
= new_stat (lang_data_statement
, stat_ptr
);
7843 new_stmt
->exp
= exp
;
7844 new_stmt
->type
= type
;
7847 /* Create a new reloc statement. RELOC is the BFD relocation type to
7848 generate. HOWTO is the corresponding howto structure (we could
7849 look this up, but the caller has already done so). SECTION is the
7850 section to generate a reloc against, or NAME is the name of the
7851 symbol to generate a reloc against. Exactly one of SECTION and
7852 NAME must be NULL. ADDEND is an expression for the addend. */
7855 lang_add_reloc (bfd_reloc_code_real_type reloc
,
7856 reloc_howto_type
*howto
,
7859 union etree_union
*addend
)
7861 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
7865 p
->section
= section
;
7867 p
->addend_exp
= addend
;
7869 p
->addend_value
= 0;
7870 p
->output_section
= NULL
;
7871 p
->output_offset
= 0;
7874 lang_assignment_statement_type
*
7875 lang_add_assignment (etree_type
*exp
)
7877 lang_assignment_statement_type
*new_stmt
;
7879 new_stmt
= new_stat (lang_assignment_statement
, stat_ptr
);
7880 new_stmt
->exp
= exp
;
7885 lang_add_attribute (enum statement_enum attribute
)
7887 new_statement (attribute
, sizeof (lang_statement_header_type
), stat_ptr
);
7891 lang_startup (const char *name
)
7893 if (first_file
->filename
!= NULL
)
7895 einfo (_("%F%P: multiple STARTUP files\n"));
7897 first_file
->filename
= name
;
7898 first_file
->local_sym_name
= name
;
7899 first_file
->flags
.real
= TRUE
;
7903 lang_float (bfd_boolean maybe
)
7905 lang_float_flag
= maybe
;
7909 /* Work out the load- and run-time regions from a script statement, and
7910 store them in *LMA_REGION and *REGION respectively.
7912 MEMSPEC is the name of the run-time region, or the value of
7913 DEFAULT_MEMORY_REGION if the statement didn't specify one.
7914 LMA_MEMSPEC is the name of the load-time region, or null if the
7915 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
7916 had an explicit load address.
7918 It is an error to specify both a load region and a load address. */
7921 lang_get_regions (lang_memory_region_type
**region
,
7922 lang_memory_region_type
**lma_region
,
7923 const char *memspec
,
7924 const char *lma_memspec
,
7925 bfd_boolean have_lma
,
7926 bfd_boolean have_vma
)
7928 *lma_region
= lang_memory_region_lookup (lma_memspec
, FALSE
);
7930 /* If no runtime region or VMA has been specified, but the load region
7931 has been specified, then use the load region for the runtime region
7933 if (lma_memspec
!= NULL
7935 && strcmp (memspec
, DEFAULT_MEMORY_REGION
) == 0)
7936 *region
= *lma_region
;
7938 *region
= lang_memory_region_lookup (memspec
, FALSE
);
7940 if (have_lma
&& lma_memspec
!= 0)
7941 einfo (_("%X%P:%pS: section has both a load address and a load region\n"),
7946 lang_leave_output_section_statement (fill_type
*fill
, const char *memspec
,
7947 lang_output_section_phdr_list
*phdrs
,
7948 const char *lma_memspec
)
7950 lang_get_regions (¤t_section
->region
,
7951 ¤t_section
->lma_region
,
7952 memspec
, lma_memspec
,
7953 current_section
->load_base
!= NULL
,
7954 current_section
->addr_tree
!= NULL
);
7956 current_section
->fill
= fill
;
7957 current_section
->phdrs
= phdrs
;
7961 /* Set the output format type. -oformat overrides scripts. */
7964 lang_add_output_format (const char *format
,
7969 if (output_target
== NULL
|| !from_script
)
7971 if (command_line
.endian
== ENDIAN_BIG
7974 else if (command_line
.endian
== ENDIAN_LITTLE
7978 output_target
= format
;
7983 lang_add_insert (const char *where
, int is_before
)
7985 lang_insert_statement_type
*new_stmt
;
7987 new_stmt
= new_stat (lang_insert_statement
, stat_ptr
);
7988 new_stmt
->where
= where
;
7989 new_stmt
->is_before
= is_before
;
7990 saved_script_handle
= previous_script_handle
;
7993 /* Enter a group. This creates a new lang_group_statement, and sets
7994 stat_ptr to build new statements within the group. */
7997 lang_enter_group (void)
7999 lang_group_statement_type
*g
;
8001 g
= new_stat (lang_group_statement
, stat_ptr
);
8002 lang_list_init (&g
->children
);
8003 push_stat_ptr (&g
->children
);
8006 /* Leave a group. This just resets stat_ptr to start writing to the
8007 regular list of statements again. Note that this will not work if
8008 groups can occur inside anything else which can adjust stat_ptr,
8009 but currently they can't. */
8012 lang_leave_group (void)
8017 /* Add a new program header. This is called for each entry in a PHDRS
8018 command in a linker script. */
8021 lang_new_phdr (const char *name
,
8023 bfd_boolean filehdr
,
8028 struct lang_phdr
*n
, **pp
;
8031 n
= stat_alloc (sizeof (struct lang_phdr
));
8034 n
->type
= exp_get_vma (type
, 0, "program header type");
8035 n
->filehdr
= filehdr
;
8040 hdrs
= n
->type
== 1 && (phdrs
|| filehdr
);
8042 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
8045 && !((*pp
)->filehdr
|| (*pp
)->phdrs
))
8047 einfo (_("%X%P:%pS: PHDRS and FILEHDR are not supported"
8048 " when prior PT_LOAD headers lack them\n"), NULL
);
8055 /* Record the program header information in the output BFD. FIXME: We
8056 should not be calling an ELF specific function here. */
8059 lang_record_phdrs (void)
8063 lang_output_section_phdr_list
*last
;
8064 struct lang_phdr
*l
;
8065 lang_output_section_statement_type
*os
;
8068 secs
= (asection
**) xmalloc (alc
* sizeof (asection
*));
8071 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
8078 for (os
= &lang_os_list
.head
->output_section_statement
;
8082 lang_output_section_phdr_list
*pl
;
8084 if (os
->constraint
< 0)
8092 if (os
->sectype
== noload_section
8093 || os
->bfd_section
== NULL
8094 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
8097 /* Don't add orphans to PT_INTERP header. */
8103 lang_output_section_statement_type
*tmp_os
;
8105 /* If we have not run across a section with a program
8106 header assigned to it yet, then scan forwards to find
8107 one. This prevents inconsistencies in the linker's
8108 behaviour when a script has specified just a single
8109 header and there are sections in that script which are
8110 not assigned to it, and which occur before the first
8111 use of that header. See here for more details:
8112 http://sourceware.org/ml/binutils/2007-02/msg00291.html */
8113 for (tmp_os
= os
; tmp_os
; tmp_os
= tmp_os
->next
)
8116 last
= tmp_os
->phdrs
;
8120 einfo (_("%F%P: no sections assigned to phdrs\n"));
8125 if (os
->bfd_section
== NULL
)
8128 for (; pl
!= NULL
; pl
= pl
->next
)
8130 if (strcmp (pl
->name
, l
->name
) == 0)
8135 secs
= (asection
**) xrealloc (secs
,
8136 alc
* sizeof (asection
*));
8138 secs
[c
] = os
->bfd_section
;
8145 if (l
->flags
== NULL
)
8148 flags
= exp_get_vma (l
->flags
, 0, "phdr flags");
8153 at
= exp_get_vma (l
->at
, 0, "phdr load address");
8155 if (!bfd_record_phdr (link_info
.output_bfd
, l
->type
,
8156 l
->flags
!= NULL
, flags
, l
->at
!= NULL
,
8157 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
8158 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
8163 /* Make sure all the phdr assignments succeeded. */
8164 for (os
= &lang_os_list
.head
->output_section_statement
;
8168 lang_output_section_phdr_list
*pl
;
8170 if (os
->constraint
< 0
8171 || os
->bfd_section
== NULL
)
8174 for (pl
= os
->phdrs
;
8177 if (!pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
8178 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
8179 os
->name
, pl
->name
);
8183 /* Record a list of sections which may not be cross referenced. */
8186 lang_add_nocrossref (lang_nocrossref_type
*l
)
8188 struct lang_nocrossrefs
*n
;
8190 n
= (struct lang_nocrossrefs
*) xmalloc (sizeof *n
);
8191 n
->next
= nocrossref_list
;
8193 n
->onlyfirst
= FALSE
;
8194 nocrossref_list
= n
;
8196 /* Set notice_all so that we get informed about all symbols. */
8197 link_info
.notice_all
= TRUE
;
8200 /* Record a section that cannot be referenced from a list of sections. */
8203 lang_add_nocrossref_to (lang_nocrossref_type
*l
)
8205 lang_add_nocrossref (l
);
8206 nocrossref_list
->onlyfirst
= TRUE
;
8209 /* Overlay handling. We handle overlays with some static variables. */
8211 /* The overlay virtual address. */
8212 static etree_type
*overlay_vma
;
8213 /* And subsection alignment. */
8214 static etree_type
*overlay_subalign
;
8216 /* An expression for the maximum section size seen so far. */
8217 static etree_type
*overlay_max
;
8219 /* A list of all the sections in this overlay. */
8221 struct overlay_list
{
8222 struct overlay_list
*next
;
8223 lang_output_section_statement_type
*os
;
8226 static struct overlay_list
*overlay_list
;
8228 /* Start handling an overlay. */
8231 lang_enter_overlay (etree_type
*vma_expr
, etree_type
*subalign
)
8233 /* The grammar should prevent nested overlays from occurring. */
8234 ASSERT (overlay_vma
== NULL
8235 && overlay_subalign
== NULL
8236 && overlay_max
== NULL
);
8238 overlay_vma
= vma_expr
;
8239 overlay_subalign
= subalign
;
8242 /* Start a section in an overlay. We handle this by calling
8243 lang_enter_output_section_statement with the correct VMA.
8244 lang_leave_overlay sets up the LMA and memory regions. */
8247 lang_enter_overlay_section (const char *name
)
8249 struct overlay_list
*n
;
8252 lang_enter_output_section_statement (name
, overlay_vma
, overlay_section
,
8253 0, overlay_subalign
, 0, 0, 0);
8255 /* If this is the first section, then base the VMA of future
8256 sections on this one. This will work correctly even if `.' is
8257 used in the addresses. */
8258 if (overlay_list
== NULL
)
8259 overlay_vma
= exp_nameop (ADDR
, name
);
8261 /* Remember the section. */
8262 n
= (struct overlay_list
*) xmalloc (sizeof *n
);
8263 n
->os
= current_section
;
8264 n
->next
= overlay_list
;
8267 size
= exp_nameop (SIZEOF
, name
);
8269 /* Arrange to work out the maximum section end address. */
8270 if (overlay_max
== NULL
)
8273 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
8276 /* Finish a section in an overlay. There isn't any special to do
8280 lang_leave_overlay_section (fill_type
*fill
,
8281 lang_output_section_phdr_list
*phdrs
)
8288 name
= current_section
->name
;
8290 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
8291 region and that no load-time region has been specified. It doesn't
8292 really matter what we say here, since lang_leave_overlay will
8294 lang_leave_output_section_statement (fill
, DEFAULT_MEMORY_REGION
, phdrs
, 0);
8296 /* Define the magic symbols. */
8298 clean
= (char *) xmalloc (strlen (name
) + 1);
8300 for (s1
= name
; *s1
!= '\0'; s1
++)
8301 if (ISALNUM (*s1
) || *s1
== '_')
8305 buf
= (char *) xmalloc (strlen (clean
) + sizeof "__load_start_");
8306 sprintf (buf
, "__load_start_%s", clean
);
8307 lang_add_assignment (exp_provide (buf
,
8308 exp_nameop (LOADADDR
, name
),
8311 buf
= (char *) xmalloc (strlen (clean
) + sizeof "__load_stop_");
8312 sprintf (buf
, "__load_stop_%s", clean
);
8313 lang_add_assignment (exp_provide (buf
,
8315 exp_nameop (LOADADDR
, name
),
8316 exp_nameop (SIZEOF
, name
)),
8322 /* Finish an overlay. If there are any overlay wide settings, this
8323 looks through all the sections in the overlay and sets them. */
8326 lang_leave_overlay (etree_type
*lma_expr
,
8329 const char *memspec
,
8330 lang_output_section_phdr_list
*phdrs
,
8331 const char *lma_memspec
)
8333 lang_memory_region_type
*region
;
8334 lang_memory_region_type
*lma_region
;
8335 struct overlay_list
*l
;
8336 lang_nocrossref_type
*nocrossref
;
8338 lang_get_regions (®ion
, &lma_region
,
8339 memspec
, lma_memspec
,
8340 lma_expr
!= NULL
, FALSE
);
8344 /* After setting the size of the last section, set '.' to end of the
8346 if (overlay_list
!= NULL
)
8348 overlay_list
->os
->update_dot
= 1;
8349 overlay_list
->os
->update_dot_tree
8350 = exp_assign (".", exp_binop ('+', overlay_vma
, overlay_max
), FALSE
);
8356 struct overlay_list
*next
;
8358 if (fill
!= NULL
&& l
->os
->fill
== NULL
)
8361 l
->os
->region
= region
;
8362 l
->os
->lma_region
= lma_region
;
8364 /* The first section has the load address specified in the
8365 OVERLAY statement. The rest are worked out from that.
8366 The base address is not needed (and should be null) if
8367 an LMA region was specified. */
8370 l
->os
->load_base
= lma_expr
;
8371 l
->os
->sectype
= first_overlay_section
;
8373 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
8374 l
->os
->phdrs
= phdrs
;
8378 lang_nocrossref_type
*nc
;
8380 nc
= (lang_nocrossref_type
*) xmalloc (sizeof *nc
);
8381 nc
->name
= l
->os
->name
;
8382 nc
->next
= nocrossref
;
8391 if (nocrossref
!= NULL
)
8392 lang_add_nocrossref (nocrossref
);
8395 overlay_list
= NULL
;
8397 overlay_subalign
= NULL
;
8400 /* Version handling. This is only useful for ELF. */
8402 /* If PREV is NULL, return first version pattern matching particular symbol.
8403 If PREV is non-NULL, return first version pattern matching particular
8404 symbol after PREV (previously returned by lang_vers_match). */
8406 static struct bfd_elf_version_expr
*
8407 lang_vers_match (struct bfd_elf_version_expr_head
*head
,
8408 struct bfd_elf_version_expr
*prev
,
8412 const char *cxx_sym
= sym
;
8413 const char *java_sym
= sym
;
8414 struct bfd_elf_version_expr
*expr
= NULL
;
8415 enum demangling_styles curr_style
;
8417 curr_style
= CURRENT_DEMANGLING_STYLE
;
8418 cplus_demangle_set_style (no_demangling
);
8419 c_sym
= bfd_demangle (link_info
.output_bfd
, sym
, DMGL_NO_OPTS
);
8422 cplus_demangle_set_style (curr_style
);
8424 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
8426 cxx_sym
= bfd_demangle (link_info
.output_bfd
, sym
,
8427 DMGL_PARAMS
| DMGL_ANSI
);
8431 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
8433 java_sym
= bfd_demangle (link_info
.output_bfd
, sym
, DMGL_JAVA
);
8438 if (head
->htab
&& (prev
== NULL
|| prev
->literal
))
8440 struct bfd_elf_version_expr e
;
8442 switch (prev
? prev
->mask
: 0)
8445 if (head
->mask
& BFD_ELF_VERSION_C_TYPE
)
8448 expr
= (struct bfd_elf_version_expr
*)
8449 htab_find ((htab_t
) head
->htab
, &e
);
8450 while (expr
&& strcmp (expr
->pattern
, c_sym
) == 0)
8451 if (expr
->mask
== BFD_ELF_VERSION_C_TYPE
)
8457 case BFD_ELF_VERSION_C_TYPE
:
8458 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
8460 e
.pattern
= cxx_sym
;
8461 expr
= (struct bfd_elf_version_expr
*)
8462 htab_find ((htab_t
) head
->htab
, &e
);
8463 while (expr
&& strcmp (expr
->pattern
, cxx_sym
) == 0)
8464 if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
8470 case BFD_ELF_VERSION_CXX_TYPE
:
8471 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
8473 e
.pattern
= java_sym
;
8474 expr
= (struct bfd_elf_version_expr
*)
8475 htab_find ((htab_t
) head
->htab
, &e
);
8476 while (expr
&& strcmp (expr
->pattern
, java_sym
) == 0)
8477 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
8488 /* Finally, try the wildcards. */
8489 if (prev
== NULL
|| prev
->literal
)
8490 expr
= head
->remaining
;
8493 for (; expr
; expr
= expr
->next
)
8500 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
8503 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
8505 else if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
8509 if (fnmatch (expr
->pattern
, s
, 0) == 0)
8515 free ((char *) c_sym
);
8517 free ((char *) cxx_sym
);
8518 if (java_sym
!= sym
)
8519 free ((char *) java_sym
);
8523 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
8524 return a pointer to the symbol name with any backslash quotes removed. */
8527 realsymbol (const char *pattern
)
8530 bfd_boolean changed
= FALSE
, backslash
= FALSE
;
8531 char *s
, *symbol
= (char *) xmalloc (strlen (pattern
) + 1);
8533 for (p
= pattern
, s
= symbol
; *p
!= '\0'; ++p
)
8535 /* It is a glob pattern only if there is no preceding
8539 /* Remove the preceding backslash. */
8546 if (*p
== '?' || *p
== '*' || *p
== '[')
8553 backslash
= *p
== '\\';
8569 /* This is called for each variable name or match expression. NEW_NAME is
8570 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
8571 pattern to be matched against symbol names. */
8573 struct bfd_elf_version_expr
*
8574 lang_new_vers_pattern (struct bfd_elf_version_expr
*orig
,
8575 const char *new_name
,
8577 bfd_boolean literal_p
)
8579 struct bfd_elf_version_expr
*ret
;
8581 ret
= (struct bfd_elf_version_expr
*) xmalloc (sizeof *ret
);
8585 ret
->literal
= TRUE
;
8586 ret
->pattern
= literal_p
? new_name
: realsymbol (new_name
);
8587 if (ret
->pattern
== NULL
)
8589 ret
->pattern
= new_name
;
8590 ret
->literal
= FALSE
;
8593 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
8594 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
8595 else if (strcasecmp (lang
, "C++") == 0)
8596 ret
->mask
= BFD_ELF_VERSION_CXX_TYPE
;
8597 else if (strcasecmp (lang
, "Java") == 0)
8598 ret
->mask
= BFD_ELF_VERSION_JAVA_TYPE
;
8601 einfo (_("%X%P: unknown language `%s' in version information\n"),
8603 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
8606 return ldemul_new_vers_pattern (ret
);
8609 /* This is called for each set of variable names and match
8612 struct bfd_elf_version_tree
*
8613 lang_new_vers_node (struct bfd_elf_version_expr
*globals
,
8614 struct bfd_elf_version_expr
*locals
)
8616 struct bfd_elf_version_tree
*ret
;
8618 ret
= (struct bfd_elf_version_tree
*) xcalloc (1, sizeof *ret
);
8619 ret
->globals
.list
= globals
;
8620 ret
->locals
.list
= locals
;
8621 ret
->match
= lang_vers_match
;
8622 ret
->name_indx
= (unsigned int) -1;
8626 /* This static variable keeps track of version indices. */
8628 static int version_index
;
8631 version_expr_head_hash (const void *p
)
8633 const struct bfd_elf_version_expr
*e
=
8634 (const struct bfd_elf_version_expr
*) p
;
8636 return htab_hash_string (e
->pattern
);
8640 version_expr_head_eq (const void *p1
, const void *p2
)
8642 const struct bfd_elf_version_expr
*e1
=
8643 (const struct bfd_elf_version_expr
*) p1
;
8644 const struct bfd_elf_version_expr
*e2
=
8645 (const struct bfd_elf_version_expr
*) p2
;
8647 return strcmp (e1
->pattern
, e2
->pattern
) == 0;
8651 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head
*head
)
8654 struct bfd_elf_version_expr
*e
, *next
;
8655 struct bfd_elf_version_expr
**list_loc
, **remaining_loc
;
8657 for (e
= head
->list
; e
; e
= e
->next
)
8661 head
->mask
|= e
->mask
;
8666 head
->htab
= htab_create (count
* 2, version_expr_head_hash
,
8667 version_expr_head_eq
, NULL
);
8668 list_loc
= &head
->list
;
8669 remaining_loc
= &head
->remaining
;
8670 for (e
= head
->list
; e
; e
= next
)
8676 remaining_loc
= &e
->next
;
8680 void **loc
= htab_find_slot ((htab_t
) head
->htab
, e
, INSERT
);
8684 struct bfd_elf_version_expr
*e1
, *last
;
8686 e1
= (struct bfd_elf_version_expr
*) *loc
;
8690 if (e1
->mask
== e
->mask
)
8698 while (e1
&& strcmp (e1
->pattern
, e
->pattern
) == 0);
8702 /* This is a duplicate. */
8703 /* FIXME: Memory leak. Sometimes pattern is not
8704 xmalloced alone, but in larger chunk of memory. */
8705 /* free (e->pattern); */
8710 e
->next
= last
->next
;
8718 list_loc
= &e
->next
;
8722 *remaining_loc
= NULL
;
8723 *list_loc
= head
->remaining
;
8726 head
->remaining
= head
->list
;
8729 /* This is called when we know the name and dependencies of the
8733 lang_register_vers_node (const char *name
,
8734 struct bfd_elf_version_tree
*version
,
8735 struct bfd_elf_version_deps
*deps
)
8737 struct bfd_elf_version_tree
*t
, **pp
;
8738 struct bfd_elf_version_expr
*e1
;
8743 if (link_info
.version_info
!= NULL
8744 && (name
[0] == '\0' || link_info
.version_info
->name
[0] == '\0'))
8746 einfo (_("%X%P: anonymous version tag cannot be combined"
8747 " with other version tags\n"));
8752 /* Make sure this node has a unique name. */
8753 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
8754 if (strcmp (t
->name
, name
) == 0)
8755 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
8757 lang_finalize_version_expr_head (&version
->globals
);
8758 lang_finalize_version_expr_head (&version
->locals
);
8760 /* Check the global and local match names, and make sure there
8761 aren't any duplicates. */
8763 for (e1
= version
->globals
.list
; e1
!= NULL
; e1
= e1
->next
)
8765 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
8767 struct bfd_elf_version_expr
*e2
;
8769 if (t
->locals
.htab
&& e1
->literal
)
8771 e2
= (struct bfd_elf_version_expr
*)
8772 htab_find ((htab_t
) t
->locals
.htab
, e1
);
8773 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
8775 if (e1
->mask
== e2
->mask
)
8776 einfo (_("%X%P: duplicate expression `%s'"
8777 " in version information\n"), e1
->pattern
);
8781 else if (!e1
->literal
)
8782 for (e2
= t
->locals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
8783 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
8784 && e1
->mask
== e2
->mask
)
8785 einfo (_("%X%P: duplicate expression `%s'"
8786 " in version information\n"), e1
->pattern
);
8790 for (e1
= version
->locals
.list
; e1
!= NULL
; e1
= e1
->next
)
8792 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
8794 struct bfd_elf_version_expr
*e2
;
8796 if (t
->globals
.htab
&& e1
->literal
)
8798 e2
= (struct bfd_elf_version_expr
*)
8799 htab_find ((htab_t
) t
->globals
.htab
, e1
);
8800 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
8802 if (e1
->mask
== e2
->mask
)
8803 einfo (_("%X%P: duplicate expression `%s'"
8804 " in version information\n"),
8809 else if (!e1
->literal
)
8810 for (e2
= t
->globals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
8811 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
8812 && e1
->mask
== e2
->mask
)
8813 einfo (_("%X%P: duplicate expression `%s'"
8814 " in version information\n"), e1
->pattern
);
8818 version
->deps
= deps
;
8819 version
->name
= name
;
8820 if (name
[0] != '\0')
8823 version
->vernum
= version_index
;
8826 version
->vernum
= 0;
8828 for (pp
= &link_info
.version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
8833 /* This is called when we see a version dependency. */
8835 struct bfd_elf_version_deps
*
8836 lang_add_vers_depend (struct bfd_elf_version_deps
*list
, const char *name
)
8838 struct bfd_elf_version_deps
*ret
;
8839 struct bfd_elf_version_tree
*t
;
8841 ret
= (struct bfd_elf_version_deps
*) xmalloc (sizeof *ret
);
8844 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
8846 if (strcmp (t
->name
, name
) == 0)
8848 ret
->version_needed
= t
;
8853 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
8855 ret
->version_needed
= NULL
;
8860 lang_do_version_exports_section (void)
8862 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
8864 LANG_FOR_EACH_INPUT_STATEMENT (is
)
8866 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
8874 contents
= (char *) xmalloc (len
);
8875 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
8876 einfo (_("%X%P: unable to read .exports section contents\n"), sec
);
8879 while (p
< contents
+ len
)
8881 greg
= lang_new_vers_pattern (greg
, p
, NULL
, FALSE
);
8882 p
= strchr (p
, '\0') + 1;
8885 /* Do not free the contents, as we used them creating the regex. */
8887 /* Do not include this section in the link. */
8888 sec
->flags
|= SEC_EXCLUDE
| SEC_KEEP
;
8891 lreg
= lang_new_vers_pattern (NULL
, "*", NULL
, FALSE
);
8892 lang_register_vers_node (command_line
.version_exports_section
,
8893 lang_new_vers_node (greg
, lreg
), NULL
);
8896 /* Evaluate LENGTH and ORIGIN parts of MEMORY spec */
8899 lang_do_memory_regions (void)
8901 lang_memory_region_type
*r
= lang_memory_region_list
;
8903 for (; r
!= NULL
; r
= r
->next
)
8907 exp_fold_tree_no_dot (r
->origin_exp
);
8908 if (expld
.result
.valid_p
)
8910 r
->origin
= expld
.result
.value
;
8911 r
->current
= r
->origin
;
8914 einfo (_("%F%P: invalid origin for memory region %s\n"),
8919 exp_fold_tree_no_dot (r
->length_exp
);
8920 if (expld
.result
.valid_p
)
8921 r
->length
= expld
.result
.value
;
8923 einfo (_("%F%P: invalid length for memory region %s\n"),
8930 lang_add_unique (const char *name
)
8932 struct unique_sections
*ent
;
8934 for (ent
= unique_section_list
; ent
; ent
= ent
->next
)
8935 if (strcmp (ent
->name
, name
) == 0)
8938 ent
= (struct unique_sections
*) xmalloc (sizeof *ent
);
8939 ent
->name
= xstrdup (name
);
8940 ent
->next
= unique_section_list
;
8941 unique_section_list
= ent
;
8944 /* Append the list of dynamic symbols to the existing one. */
8947 lang_append_dynamic_list (struct bfd_elf_version_expr
*dynamic
)
8949 if (link_info
.dynamic_list
)
8951 struct bfd_elf_version_expr
*tail
;
8952 for (tail
= dynamic
; tail
->next
!= NULL
; tail
= tail
->next
)
8954 tail
->next
= link_info
.dynamic_list
->head
.list
;
8955 link_info
.dynamic_list
->head
.list
= dynamic
;
8959 struct bfd_elf_dynamic_list
*d
;
8961 d
= (struct bfd_elf_dynamic_list
*) xcalloc (1, sizeof *d
);
8962 d
->head
.list
= dynamic
;
8963 d
->match
= lang_vers_match
;
8964 link_info
.dynamic_list
= d
;
8968 /* Append the list of C++ typeinfo dynamic symbols to the existing
8972 lang_append_dynamic_list_cpp_typeinfo (void)
8974 const char *symbols
[] =
8976 "typeinfo name for*",
8979 struct bfd_elf_version_expr
*dynamic
= NULL
;
8982 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
8983 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
8986 lang_append_dynamic_list (dynamic
);
8989 /* Append the list of C++ operator new and delete dynamic symbols to the
8993 lang_append_dynamic_list_cpp_new (void)
8995 const char *symbols
[] =
9000 struct bfd_elf_version_expr
*dynamic
= NULL
;
9003 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
9004 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
9007 lang_append_dynamic_list (dynamic
);
9010 /* Scan a space and/or comma separated string of features. */
9013 lang_ld_feature (char *str
)
9021 while (*p
== ',' || ISSPACE (*p
))
9026 while (*q
&& *q
!= ',' && !ISSPACE (*q
))
9030 if (strcasecmp (p
, "SANE_EXPR") == 0)
9031 config
.sane_expr
= TRUE
;
9033 einfo (_("%X%P: unknown feature `%s'\n"), p
);
9039 /* Pretty print memory amount. */
9042 lang_print_memory_size (bfd_vma sz
)
9044 if ((sz
& 0x3fffffff) == 0)
9045 printf ("%10" BFD_VMA_FMT
"u GB", sz
>> 30);
9046 else if ((sz
& 0xfffff) == 0)
9047 printf ("%10" BFD_VMA_FMT
"u MB", sz
>> 20);
9048 else if ((sz
& 0x3ff) == 0)
9049 printf ("%10" BFD_VMA_FMT
"u KB", sz
>> 10);
9051 printf (" %10" BFD_VMA_FMT
"u B", sz
);
9054 /* Implement --print-memory-usage: disply per region memory usage. */
9057 lang_print_memory_usage (void)
9059 lang_memory_region_type
*r
;
9061 printf ("Memory region Used Size Region Size %%age Used\n");
9062 for (r
= lang_memory_region_list
; r
->next
!= NULL
; r
= r
->next
)
9064 bfd_vma used_length
= r
->current
- r
->origin
;
9067 printf ("%16s: ",r
->name_list
.name
);
9068 lang_print_memory_size (used_length
);
9069 lang_print_memory_size ((bfd_vma
) r
->length
);
9071 percent
= used_length
* 100.0 / r
->length
;
9073 printf (" %6.2f%%\n", percent
);