1 /* Linker command language support.
2 Copyright (C) 1991-2020 Free Software Foundation, Inc.
4 This file is part of the GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
24 #include "libiberty.h"
25 #include "filenames.h"
26 #include "safe-ctype.h"
47 #endif /* ENABLE_PLUGINS */
50 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
53 /* Convert between addresses in bytes and sizes in octets.
54 For currently supported targets, octets_per_byte is always a power
55 of two, so we can use shifts. */
56 #define TO_ADDR(X) ((X) >> opb_shift)
57 #define TO_SIZE(X) ((X) << opb_shift)
59 /* Local variables. */
60 static struct obstack stat_obstack
;
61 static struct obstack map_obstack
;
63 #define obstack_chunk_alloc xmalloc
64 #define obstack_chunk_free free
65 static const char *entry_symbol_default
= "start";
66 static bfd_boolean map_head_is_link_order
= FALSE
;
67 static lang_output_section_statement_type
*default_common_section
;
68 static bfd_boolean map_option_f
;
69 static bfd_vma print_dot
;
70 static lang_input_statement_type
*first_file
;
71 static const char *current_target
;
72 /* Header for list of statements corresponding to any files involved in the
73 link, either specified from the command-line or added implicitely (eg.
74 archive member used to resolved undefined symbol, wildcard statement from
75 linker script, etc.). Next pointer is in next field of a
76 lang_statement_header_type (reached via header field in a
77 lang_statement_union). */
78 static lang_statement_list_type statement_list
;
79 static lang_statement_list_type
*stat_save
[10];
80 static lang_statement_list_type
**stat_save_ptr
= &stat_save
[0];
81 static struct unique_sections
*unique_section_list
;
82 static struct asneeded_minfo
*asneeded_list_head
;
83 static unsigned int opb_shift
= 0;
85 /* Forward declarations. */
86 static void exp_init_os (etree_type
*);
87 static lang_input_statement_type
*lookup_name (const char *);
88 static void insert_undefined (const char *);
89 static bfd_boolean
sort_def_symbol (struct bfd_link_hash_entry
*, void *);
90 static void print_statement (lang_statement_union_type
*,
91 lang_output_section_statement_type
*);
92 static void print_statement_list (lang_statement_union_type
*,
93 lang_output_section_statement_type
*);
94 static void print_statements (void);
95 static void print_input_section (asection
*, bfd_boolean
);
96 static bfd_boolean
lang_one_common (struct bfd_link_hash_entry
*, void *);
97 static void lang_record_phdrs (void);
98 static void lang_do_version_exports_section (void);
99 static void lang_finalize_version_expr_head
100 (struct bfd_elf_version_expr_head
*);
101 static void lang_do_memory_regions (void);
103 /* Exported variables. */
104 const char *output_target
;
105 lang_output_section_statement_type
*abs_output_section
;
106 lang_statement_list_type lang_os_list
;
107 lang_statement_list_type
*stat_ptr
= &statement_list
;
108 /* Header for list of statements corresponding to files used in the final
109 executable. This can be either object file specified on the command-line
110 or library member resolving an undefined reference. Next pointer is in next
111 field of a lang_input_statement_type (reached via input_statement field in a
112 lang_statement_union). */
113 lang_statement_list_type file_chain
= { NULL
, NULL
};
114 /* Header for list of statements corresponding to files specified on the
115 command-line for linking. It thus contains real object files and archive
116 but not archive members. Next pointer is in next_real_file field of a
117 lang_input_statement_type statement (reached via input_statement field in a
118 lang_statement_union). */
119 lang_statement_list_type input_file_chain
;
120 static const char *current_input_file
;
121 struct bfd_sym_chain entry_symbol
= { NULL
, NULL
};
122 const char *entry_section
= ".text";
123 struct lang_input_statement_flags input_flags
;
124 bfd_boolean entry_from_cmdline
;
125 bfd_boolean undef_from_cmdline
;
126 bfd_boolean lang_has_input_file
= FALSE
;
127 bfd_boolean had_output_filename
= FALSE
;
128 bfd_boolean lang_float_flag
= FALSE
;
129 bfd_boolean delete_output_file_on_failure
= FALSE
;
130 struct lang_phdr
*lang_phdr_list
;
131 struct lang_nocrossrefs
*nocrossref_list
;
132 struct asneeded_minfo
**asneeded_list_tail
;
133 static ctf_file_t
*ctf_output
;
135 /* Functions that traverse the linker script and might evaluate
136 DEFINED() need to increment this at the start of the traversal. */
137 int lang_statement_iteration
= 0;
139 /* Count times through one_lang_size_sections_pass after mark phase. */
140 static int lang_sizing_iteration
= 0;
142 /* Return TRUE if the PATTERN argument is a wildcard pattern.
143 Although backslashes are treated specially if a pattern contains
144 wildcards, we do not consider the mere presence of a backslash to
145 be enough to cause the pattern to be treated as a wildcard.
146 That lets us handle DOS filenames more naturally. */
147 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
149 #define new_stat(x, y) \
150 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
152 #define outside_section_address(q) \
153 ((q)->output_offset + (q)->output_section->vma)
155 #define outside_symbol_address(q) \
156 ((q)->value + outside_section_address (q->section))
158 /* CTF sections smaller than this are not compressed: compression of
159 dictionaries this small doesn't gain much, and this lets consumers mmap the
160 sections directly out of the ELF file and use them with no decompression
161 overhead if they want to. */
162 #define CTF_COMPRESSION_THRESHOLD 4096
165 stat_alloc (size_t size
)
167 return obstack_alloc (&stat_obstack
, size
);
171 name_match (const char *pattern
, const char *name
)
173 if (wildcardp (pattern
))
174 return fnmatch (pattern
, name
, 0);
175 return strcmp (pattern
, name
);
179 ldirname (const char *name
)
181 const char *base
= lbasename (name
);
184 while (base
> name
&& IS_DIR_SEPARATOR (base
[-1]))
188 dirname
= strdup (name
);
189 dirname
[base
- name
] = '\0';
193 /* If PATTERN is of the form archive:file, return a pointer to the
194 separator. If not, return NULL. */
197 archive_path (const char *pattern
)
201 if (link_info
.path_separator
== 0)
204 p
= strchr (pattern
, link_info
.path_separator
);
205 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
206 if (p
== NULL
|| link_info
.path_separator
!= ':')
209 /* Assume a match on the second char is part of drive specifier,
210 as in "c:\silly.dos". */
211 if (p
== pattern
+ 1 && ISALPHA (*pattern
))
212 p
= strchr (p
+ 1, link_info
.path_separator
);
217 /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path,
218 return whether F matches FILE_SPEC. */
221 input_statement_is_archive_path (const char *file_spec
, char *sep
,
222 lang_input_statement_type
*f
)
224 bfd_boolean match
= FALSE
;
227 || name_match (sep
+ 1, f
->filename
) == 0)
228 && ((sep
!= file_spec
)
229 == (f
->the_bfd
!= NULL
&& f
->the_bfd
->my_archive
!= NULL
)))
233 if (sep
!= file_spec
)
235 const char *aname
= f
->the_bfd
->my_archive
->filename
;
237 match
= name_match (file_spec
, aname
) == 0;
238 *sep
= link_info
.path_separator
;
245 unique_section_p (const asection
*sec
,
246 const lang_output_section_statement_type
*os
)
248 struct unique_sections
*unam
;
251 if (!link_info
.resolve_section_groups
252 && sec
->owner
!= NULL
253 && bfd_is_group_section (sec
->owner
, sec
))
255 && strcmp (os
->name
, DISCARD_SECTION_NAME
) == 0);
258 for (unam
= unique_section_list
; unam
; unam
= unam
->next
)
259 if (name_match (unam
->name
, secnam
) == 0)
265 /* Generic traversal routines for finding matching sections. */
267 /* Return true if FILE matches a pattern in EXCLUDE_LIST, otherwise return
271 walk_wild_file_in_exclude_list (struct name_list
*exclude_list
,
272 lang_input_statement_type
*file
)
274 struct name_list
*list_tmp
;
276 for (list_tmp
= exclude_list
;
278 list_tmp
= list_tmp
->next
)
280 char *p
= archive_path (list_tmp
->name
);
284 if (input_statement_is_archive_path (list_tmp
->name
, p
, file
))
288 else if (name_match (list_tmp
->name
, file
->filename
) == 0)
291 /* FIXME: Perhaps remove the following at some stage? Matching
292 unadorned archives like this was never documented and has
293 been superceded by the archive:path syntax. */
294 else if (file
->the_bfd
!= NULL
295 && file
->the_bfd
->my_archive
!= NULL
296 && name_match (list_tmp
->name
,
297 file
->the_bfd
->my_archive
->filename
) == 0)
304 /* Try processing a section against a wildcard. This just calls
305 the callback unless the filename exclusion list is present
306 and excludes the file. It's hardly ever present so this
307 function is very fast. */
310 walk_wild_consider_section (lang_wild_statement_type
*ptr
,
311 lang_input_statement_type
*file
,
313 struct wildcard_list
*sec
,
317 /* Don't process sections from files which were excluded. */
318 if (walk_wild_file_in_exclude_list (sec
->spec
.exclude_name_list
, file
))
321 (*callback
) (ptr
, sec
, s
, ptr
->section_flag_list
, file
, data
);
324 /* Lowest common denominator routine that can handle everything correctly,
328 walk_wild_section_general (lang_wild_statement_type
*ptr
,
329 lang_input_statement_type
*file
,
334 struct wildcard_list
*sec
;
336 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
338 sec
= ptr
->section_list
;
340 (*callback
) (ptr
, sec
, s
, ptr
->section_flag_list
, file
, data
);
344 bfd_boolean skip
= FALSE
;
346 if (sec
->spec
.name
!= NULL
)
348 const char *sname
= bfd_section_name (s
);
350 skip
= name_match (sec
->spec
.name
, sname
) != 0;
354 walk_wild_consider_section (ptr
, file
, s
, sec
, callback
, data
);
361 /* Routines to find a single section given its name. If there's more
362 than one section with that name, we report that. */
366 asection
*found_section
;
367 bfd_boolean multiple_sections_found
;
368 } section_iterator_callback_data
;
371 section_iterator_callback (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*s
, void *data
)
373 section_iterator_callback_data
*d
= (section_iterator_callback_data
*) data
;
375 if (d
->found_section
!= NULL
)
377 d
->multiple_sections_found
= TRUE
;
381 d
->found_section
= s
;
386 find_section (lang_input_statement_type
*file
,
387 struct wildcard_list
*sec
,
388 bfd_boolean
*multiple_sections_found
)
390 section_iterator_callback_data cb_data
= { NULL
, FALSE
};
392 bfd_get_section_by_name_if (file
->the_bfd
, sec
->spec
.name
,
393 section_iterator_callback
, &cb_data
);
394 *multiple_sections_found
= cb_data
.multiple_sections_found
;
395 return cb_data
.found_section
;
398 /* Code for handling simple wildcards without going through fnmatch,
399 which can be expensive because of charset translations etc. */
401 /* A simple wild is a literal string followed by a single '*',
402 where the literal part is at least 4 characters long. */
405 is_simple_wild (const char *name
)
407 size_t len
= strcspn (name
, "*?[");
408 return len
>= 4 && name
[len
] == '*' && name
[len
+ 1] == '\0';
412 match_simple_wild (const char *pattern
, const char *name
)
414 /* The first four characters of the pattern are guaranteed valid
415 non-wildcard characters. So we can go faster. */
416 if (pattern
[0] != name
[0] || pattern
[1] != name
[1]
417 || pattern
[2] != name
[2] || pattern
[3] != name
[3])
422 while (*pattern
!= '*')
423 if (*name
++ != *pattern
++)
429 /* Return the numerical value of the init_priority attribute from
430 section name NAME. */
433 get_init_priority (const asection
*sec
)
435 const char *name
= bfd_section_name (sec
);
438 /* GCC uses the following section names for the init_priority
439 attribute with numerical values 101 to 65535 inclusive. A
440 lower value means a higher priority.
442 1: .init_array.NNNNN/.fini_array.NNNNN: Where NNNNN is the
443 decimal numerical value of the init_priority attribute.
444 The order of execution in .init_array is forward and
445 .fini_array is backward.
446 2: .ctors.NNNNN/.dtors.NNNNN: Where NNNNN is 65535 minus the
447 decimal numerical value of the init_priority attribute.
448 The order of execution in .ctors is backward and .dtors
451 .init_array.NNNNN sections would normally be placed in an output
452 .init_array section, .fini_array.NNNNN in .fini_array,
453 .ctors.NNNNN in .ctors, and .dtors.NNNNN in .dtors. This means
454 we should sort by increasing number (and could just use
455 SORT_BY_NAME in scripts). However if .ctors.NNNNN sections are
456 being placed in .init_array (which may also contain
457 .init_array.NNNNN sections) or .dtors.NNNNN sections are being
458 placed in .fini_array then we need to extract the init_priority
459 attribute and sort on that. */
460 dot
= strrchr (name
, '.');
461 if (dot
!= NULL
&& ISDIGIT (dot
[1]))
464 unsigned long init_priority
= strtoul (dot
+ 1, &end
, 10);
468 && (strncmp (name
, ".ctors", 6) == 0
469 || strncmp (name
, ".dtors", 6) == 0))
470 init_priority
= 65535 - init_priority
;
471 if (init_priority
<= INT_MAX
)
472 return init_priority
;
478 /* Compare sections ASEC and BSEC according to SORT. */
481 compare_section (sort_type sort
, asection
*asec
, asection
*bsec
)
484 int a_priority
, b_priority
;
491 case by_init_priority
:
492 a_priority
= get_init_priority (asec
);
493 b_priority
= get_init_priority (bsec
);
494 if (a_priority
< 0 || b_priority
< 0)
496 ret
= a_priority
- b_priority
;
502 case by_alignment_name
:
503 ret
= bfd_section_alignment (bsec
) - bfd_section_alignment (asec
);
510 ret
= strcmp (bfd_section_name (asec
), bfd_section_name (bsec
));
513 case by_name_alignment
:
514 ret
= strcmp (bfd_section_name (asec
), bfd_section_name (bsec
));
520 ret
= bfd_section_alignment (bsec
) - bfd_section_alignment (asec
);
527 /* Build a Binary Search Tree to sort sections, unlike insertion sort
528 used in wild_sort(). BST is considerably faster if the number of
529 of sections are large. */
531 static lang_section_bst_type
**
532 wild_sort_fast (lang_wild_statement_type
*wild
,
533 struct wildcard_list
*sec
,
534 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
537 lang_section_bst_type
**tree
;
540 if (!wild
->filenames_sorted
541 && (sec
== NULL
|| sec
->spec
.sorted
== none
))
543 /* Append at the right end of tree. */
545 tree
= &((*tree
)->right
);
551 /* Find the correct node to append this section. */
552 if (compare_section (sec
->spec
.sorted
, section
, (*tree
)->section
) < 0)
553 tree
= &((*tree
)->left
);
555 tree
= &((*tree
)->right
);
561 /* Use wild_sort_fast to build a BST to sort sections. */
564 output_section_callback_fast (lang_wild_statement_type
*ptr
,
565 struct wildcard_list
*sec
,
567 struct flag_info
*sflag_list ATTRIBUTE_UNUSED
,
568 lang_input_statement_type
*file
,
571 lang_section_bst_type
*node
;
572 lang_section_bst_type
**tree
;
573 lang_output_section_statement_type
*os
;
575 os
= (lang_output_section_statement_type
*) output
;
577 if (unique_section_p (section
, os
))
580 node
= (lang_section_bst_type
*) xmalloc (sizeof (lang_section_bst_type
));
583 node
->section
= section
;
585 tree
= wild_sort_fast (ptr
, sec
, file
, section
);
590 /* Convert a sorted sections' BST back to list form. */
593 output_section_callback_tree_to_list (lang_wild_statement_type
*ptr
,
594 lang_section_bst_type
*tree
,
598 output_section_callback_tree_to_list (ptr
, tree
->left
, output
);
600 lang_add_section (&ptr
->children
, tree
->section
, NULL
,
601 (lang_output_section_statement_type
*) output
);
604 output_section_callback_tree_to_list (ptr
, tree
->right
, output
);
609 /* Specialized, optimized routines for handling different kinds of
613 walk_wild_section_specs1_wild0 (lang_wild_statement_type
*ptr
,
614 lang_input_statement_type
*file
,
618 /* We can just do a hash lookup for the section with the right name.
619 But if that lookup discovers more than one section with the name
620 (should be rare), we fall back to the general algorithm because
621 we would otherwise have to sort the sections to make sure they
622 get processed in the bfd's order. */
623 bfd_boolean multiple_sections_found
;
624 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
625 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
);
627 if (multiple_sections_found
)
628 walk_wild_section_general (ptr
, file
, callback
, data
);
630 walk_wild_consider_section (ptr
, file
, s0
, sec0
, callback
, data
);
634 walk_wild_section_specs1_wild1 (lang_wild_statement_type
*ptr
,
635 lang_input_statement_type
*file
,
640 struct wildcard_list
*wildsec0
= ptr
->handler_data
[0];
642 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
644 const char *sname
= bfd_section_name (s
);
645 bfd_boolean skip
= !match_simple_wild (wildsec0
->spec
.name
, sname
);
648 walk_wild_consider_section (ptr
, file
, s
, wildsec0
, callback
, data
);
653 walk_wild_section_specs2_wild1 (lang_wild_statement_type
*ptr
,
654 lang_input_statement_type
*file
,
659 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
660 struct wildcard_list
*wildsec1
= ptr
->handler_data
[1];
661 bfd_boolean multiple_sections_found
;
662 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
);
664 if (multiple_sections_found
)
666 walk_wild_section_general (ptr
, file
, callback
, data
);
670 /* Note that if the section was not found, s0 is NULL and
671 we'll simply never succeed the s == s0 test below. */
672 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
674 /* Recall that in this code path, a section cannot satisfy more
675 than one spec, so if s == s0 then it cannot match
678 walk_wild_consider_section (ptr
, file
, s
, sec0
, callback
, data
);
681 const char *sname
= bfd_section_name (s
);
682 bfd_boolean skip
= !match_simple_wild (wildsec1
->spec
.name
, sname
);
685 walk_wild_consider_section (ptr
, file
, s
, wildsec1
, callback
,
692 walk_wild_section_specs3_wild2 (lang_wild_statement_type
*ptr
,
693 lang_input_statement_type
*file
,
698 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
699 struct wildcard_list
*wildsec1
= ptr
->handler_data
[1];
700 struct wildcard_list
*wildsec2
= ptr
->handler_data
[2];
701 bfd_boolean multiple_sections_found
;
702 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
);
704 if (multiple_sections_found
)
706 walk_wild_section_general (ptr
, file
, callback
, data
);
710 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
713 walk_wild_consider_section (ptr
, file
, s
, sec0
, callback
, data
);
716 const char *sname
= bfd_section_name (s
);
717 bfd_boolean skip
= !match_simple_wild (wildsec1
->spec
.name
, sname
);
720 walk_wild_consider_section (ptr
, file
, s
, wildsec1
, callback
, data
);
723 skip
= !match_simple_wild (wildsec2
->spec
.name
, sname
);
725 walk_wild_consider_section (ptr
, file
, s
, wildsec2
, callback
,
733 walk_wild_section_specs4_wild2 (lang_wild_statement_type
*ptr
,
734 lang_input_statement_type
*file
,
739 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
740 struct wildcard_list
*sec1
= ptr
->handler_data
[1];
741 struct wildcard_list
*wildsec2
= ptr
->handler_data
[2];
742 struct wildcard_list
*wildsec3
= ptr
->handler_data
[3];
743 bfd_boolean multiple_sections_found
;
744 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
), *s1
;
746 if (multiple_sections_found
)
748 walk_wild_section_general (ptr
, file
, callback
, data
);
752 s1
= find_section (file
, sec1
, &multiple_sections_found
);
753 if (multiple_sections_found
)
755 walk_wild_section_general (ptr
, file
, callback
, data
);
759 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
762 walk_wild_consider_section (ptr
, file
, s
, sec0
, callback
, data
);
765 walk_wild_consider_section (ptr
, file
, s
, sec1
, callback
, data
);
768 const char *sname
= bfd_section_name (s
);
769 bfd_boolean skip
= !match_simple_wild (wildsec2
->spec
.name
,
773 walk_wild_consider_section (ptr
, file
, s
, wildsec2
, callback
,
777 skip
= !match_simple_wild (wildsec3
->spec
.name
, sname
);
779 walk_wild_consider_section (ptr
, file
, s
, wildsec3
,
787 walk_wild_section (lang_wild_statement_type
*ptr
,
788 lang_input_statement_type
*file
,
792 if (file
->flags
.just_syms
)
795 (*ptr
->walk_wild_section_handler
) (ptr
, file
, callback
, data
);
798 /* Returns TRUE when name1 is a wildcard spec that might match
799 something name2 can match. We're conservative: we return FALSE
800 only if the prefixes of name1 and name2 are different up to the
801 first wildcard character. */
804 wild_spec_can_overlap (const char *name1
, const char *name2
)
806 size_t prefix1_len
= strcspn (name1
, "?*[");
807 size_t prefix2_len
= strcspn (name2
, "?*[");
808 size_t min_prefix_len
;
810 /* Note that if there is no wildcard character, then we treat the
811 terminating 0 as part of the prefix. Thus ".text" won't match
812 ".text." or ".text.*", for example. */
813 if (name1
[prefix1_len
] == '\0')
815 if (name2
[prefix2_len
] == '\0')
818 min_prefix_len
= prefix1_len
< prefix2_len
? prefix1_len
: prefix2_len
;
820 return memcmp (name1
, name2
, min_prefix_len
) == 0;
823 /* Select specialized code to handle various kinds of wildcard
827 analyze_walk_wild_section_handler (lang_wild_statement_type
*ptr
)
830 int wild_name_count
= 0;
831 struct wildcard_list
*sec
;
835 ptr
->walk_wild_section_handler
= walk_wild_section_general
;
836 ptr
->handler_data
[0] = NULL
;
837 ptr
->handler_data
[1] = NULL
;
838 ptr
->handler_data
[2] = NULL
;
839 ptr
->handler_data
[3] = NULL
;
842 /* Count how many wildcard_specs there are, and how many of those
843 actually use wildcards in the name. Also, bail out if any of the
844 wildcard names are NULL. (Can this actually happen?
845 walk_wild_section used to test for it.) And bail out if any
846 of the wildcards are more complex than a simple string
847 ending in a single '*'. */
848 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
851 if (sec
->spec
.name
== NULL
)
853 if (wildcardp (sec
->spec
.name
))
856 if (!is_simple_wild (sec
->spec
.name
))
861 /* The zero-spec case would be easy to optimize but it doesn't
862 happen in practice. Likewise, more than 4 specs doesn't
863 happen in practice. */
864 if (sec_count
== 0 || sec_count
> 4)
867 /* Check that no two specs can match the same section. */
868 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
870 struct wildcard_list
*sec2
;
871 for (sec2
= sec
->next
; sec2
!= NULL
; sec2
= sec2
->next
)
873 if (wild_spec_can_overlap (sec
->spec
.name
, sec2
->spec
.name
))
878 signature
= (sec_count
<< 8) + wild_name_count
;
882 ptr
->walk_wild_section_handler
= walk_wild_section_specs1_wild0
;
885 ptr
->walk_wild_section_handler
= walk_wild_section_specs1_wild1
;
888 ptr
->walk_wild_section_handler
= walk_wild_section_specs2_wild1
;
891 ptr
->walk_wild_section_handler
= walk_wild_section_specs3_wild2
;
894 ptr
->walk_wild_section_handler
= walk_wild_section_specs4_wild2
;
900 /* Now fill the data array with pointers to the specs, first the
901 specs with non-wildcard names, then the specs with wildcard
902 names. It's OK to process the specs in different order from the
903 given order, because we've already determined that no section
904 will match more than one spec. */
906 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
907 if (!wildcardp (sec
->spec
.name
))
908 ptr
->handler_data
[data_counter
++] = sec
;
909 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
910 if (wildcardp (sec
->spec
.name
))
911 ptr
->handler_data
[data_counter
++] = sec
;
914 /* Handle a wild statement for a single file F. */
917 walk_wild_file (lang_wild_statement_type
*s
,
918 lang_input_statement_type
*f
,
922 if (walk_wild_file_in_exclude_list (s
->exclude_name_list
, f
))
925 if (f
->the_bfd
== NULL
926 || !bfd_check_format (f
->the_bfd
, bfd_archive
))
927 walk_wild_section (s
, f
, callback
, data
);
932 /* This is an archive file. We must map each member of the
933 archive separately. */
934 member
= bfd_openr_next_archived_file (f
->the_bfd
, NULL
);
935 while (member
!= NULL
)
937 /* When lookup_name is called, it will call the add_symbols
938 entry point for the archive. For each element of the
939 archive which is included, BFD will call ldlang_add_file,
940 which will set the usrdata field of the member to the
941 lang_input_statement. */
942 if (bfd_usrdata (member
) != NULL
)
943 walk_wild_section (s
, bfd_usrdata (member
), callback
, data
);
945 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
951 walk_wild (lang_wild_statement_type
*s
, callback_t callback
, void *data
)
953 const char *file_spec
= s
->filename
;
956 if (file_spec
== NULL
)
958 /* Perform the iteration over all files in the list. */
959 LANG_FOR_EACH_INPUT_STATEMENT (f
)
961 walk_wild_file (s
, f
, callback
, data
);
964 else if ((p
= archive_path (file_spec
)) != NULL
)
966 LANG_FOR_EACH_INPUT_STATEMENT (f
)
968 if (input_statement_is_archive_path (file_spec
, p
, f
))
969 walk_wild_file (s
, f
, callback
, data
);
972 else if (wildcardp (file_spec
))
974 LANG_FOR_EACH_INPUT_STATEMENT (f
)
976 if (fnmatch (file_spec
, f
->filename
, 0) == 0)
977 walk_wild_file (s
, f
, callback
, data
);
982 lang_input_statement_type
*f
;
984 /* Perform the iteration over a single file. */
985 f
= lookup_name (file_spec
);
987 walk_wild_file (s
, f
, callback
, data
);
991 /* lang_for_each_statement walks the parse tree and calls the provided
992 function for each node, except those inside output section statements
993 with constraint set to -1. */
996 lang_for_each_statement_worker (void (*func
) (lang_statement_union_type
*),
997 lang_statement_union_type
*s
)
999 for (; s
!= NULL
; s
= s
->header
.next
)
1003 switch (s
->header
.type
)
1005 case lang_constructors_statement_enum
:
1006 lang_for_each_statement_worker (func
, constructor_list
.head
);
1008 case lang_output_section_statement_enum
:
1009 if (s
->output_section_statement
.constraint
!= -1)
1010 lang_for_each_statement_worker
1011 (func
, s
->output_section_statement
.children
.head
);
1013 case lang_wild_statement_enum
:
1014 lang_for_each_statement_worker (func
,
1015 s
->wild_statement
.children
.head
);
1017 case lang_group_statement_enum
:
1018 lang_for_each_statement_worker (func
,
1019 s
->group_statement
.children
.head
);
1021 case lang_data_statement_enum
:
1022 case lang_reloc_statement_enum
:
1023 case lang_object_symbols_statement_enum
:
1024 case lang_output_statement_enum
:
1025 case lang_target_statement_enum
:
1026 case lang_input_section_enum
:
1027 case lang_input_statement_enum
:
1028 case lang_assignment_statement_enum
:
1029 case lang_padding_statement_enum
:
1030 case lang_address_statement_enum
:
1031 case lang_fill_statement_enum
:
1032 case lang_insert_statement_enum
:
1042 lang_for_each_statement (void (*func
) (lang_statement_union_type
*))
1044 lang_for_each_statement_worker (func
, statement_list
.head
);
1047 /*----------------------------------------------------------------------*/
1050 lang_list_init (lang_statement_list_type
*list
)
1053 list
->tail
= &list
->head
;
1057 lang_statement_append (lang_statement_list_type
*list
,
1061 *(list
->tail
) = element
;
1066 push_stat_ptr (lang_statement_list_type
*new_ptr
)
1068 if (stat_save_ptr
>= stat_save
+ sizeof (stat_save
) / sizeof (stat_save
[0]))
1070 *stat_save_ptr
++ = stat_ptr
;
1077 if (stat_save_ptr
<= stat_save
)
1079 stat_ptr
= *--stat_save_ptr
;
1082 /* Build a new statement node for the parse tree. */
1084 static lang_statement_union_type
*
1085 new_statement (enum statement_enum type
,
1087 lang_statement_list_type
*list
)
1089 lang_statement_union_type
*new_stmt
;
1091 new_stmt
= stat_alloc (size
);
1092 new_stmt
->header
.type
= type
;
1093 new_stmt
->header
.next
= NULL
;
1094 lang_statement_append (list
, new_stmt
, &new_stmt
->header
.next
);
1098 /* Build a new input file node for the language. There are several
1099 ways in which we treat an input file, eg, we only look at symbols,
1100 or prefix it with a -l etc.
1102 We can be supplied with requests for input files more than once;
1103 they may, for example be split over several lines like foo.o(.text)
1104 foo.o(.data) etc, so when asked for a file we check that we haven't
1105 got it already so we don't duplicate the bfd. */
1107 static lang_input_statement_type
*
1108 new_afile (const char *name
,
1109 lang_input_file_enum_type file_type
,
1111 const char *from_filename
)
1113 lang_input_statement_type
*p
;
1115 lang_has_input_file
= TRUE
;
1117 p
= new_stat (lang_input_statement
, stat_ptr
);
1118 memset (&p
->the_bfd
, 0,
1119 sizeof (*p
) - offsetof (lang_input_statement_type
, the_bfd
));
1120 p
->extra_search_path
= NULL
;
1122 p
->flags
.dynamic
= input_flags
.dynamic
;
1123 p
->flags
.add_DT_NEEDED_for_dynamic
= input_flags
.add_DT_NEEDED_for_dynamic
;
1124 p
->flags
.add_DT_NEEDED_for_regular
= input_flags
.add_DT_NEEDED_for_regular
;
1125 p
->flags
.whole_archive
= input_flags
.whole_archive
;
1126 p
->flags
.sysrooted
= input_flags
.sysrooted
;
1130 case lang_input_file_is_symbols_only_enum
:
1132 p
->local_sym_name
= name
;
1133 p
->flags
.real
= TRUE
;
1134 p
->flags
.just_syms
= TRUE
;
1136 case lang_input_file_is_fake_enum
:
1138 p
->local_sym_name
= name
;
1140 case lang_input_file_is_l_enum
:
1141 if (name
[0] == ':' && name
[1] != '\0')
1143 p
->filename
= name
+ 1;
1144 p
->flags
.full_name_provided
= TRUE
;
1148 p
->local_sym_name
= concat ("-l", name
, (const char *) NULL
);
1149 p
->flags
.maybe_archive
= TRUE
;
1150 p
->flags
.real
= TRUE
;
1151 p
->flags
.search_dirs
= TRUE
;
1153 case lang_input_file_is_marker_enum
:
1155 p
->local_sym_name
= name
;
1156 p
->flags
.search_dirs
= TRUE
;
1158 case lang_input_file_is_search_file_enum
:
1160 p
->local_sym_name
= name
;
1161 /* If name is a relative path, search the directory of the current linker
1163 if (from_filename
&& !IS_ABSOLUTE_PATH (name
))
1164 p
->extra_search_path
= ldirname (from_filename
);
1165 p
->flags
.real
= TRUE
;
1166 p
->flags
.search_dirs
= TRUE
;
1168 case lang_input_file_is_file_enum
:
1170 p
->local_sym_name
= name
;
1171 p
->flags
.real
= TRUE
;
1177 lang_statement_append (&input_file_chain
, p
, &p
->next_real_file
);
1181 lang_input_statement_type
*
1182 lang_add_input_file (const char *name
,
1183 lang_input_file_enum_type file_type
,
1187 && (*name
== '=' || CONST_STRNEQ (name
, "$SYSROOT")))
1189 lang_input_statement_type
*ret
;
1190 char *sysrooted_name
1191 = concat (ld_sysroot
,
1192 name
+ (*name
== '=' ? 1 : strlen ("$SYSROOT")),
1193 (const char *) NULL
);
1195 /* We've now forcibly prepended the sysroot, making the input
1196 file independent of the context. Therefore, temporarily
1197 force a non-sysrooted context for this statement, so it won't
1198 get the sysroot prepended again when opened. (N.B. if it's a
1199 script, any child nodes with input files starting with "/"
1200 will be handled as "sysrooted" as they'll be found to be
1201 within the sysroot subdirectory.) */
1202 unsigned int outer_sysrooted
= input_flags
.sysrooted
;
1203 input_flags
.sysrooted
= 0;
1204 ret
= new_afile (sysrooted_name
, file_type
, target
, NULL
);
1205 input_flags
.sysrooted
= outer_sysrooted
;
1209 return new_afile (name
, file_type
, target
, current_input_file
);
1212 struct out_section_hash_entry
1214 struct bfd_hash_entry root
;
1215 lang_statement_union_type s
;
1218 /* The hash table. */
1220 static struct bfd_hash_table output_section_statement_table
;
1222 /* Support routines for the hash table used by lang_output_section_find,
1223 initialize the table, fill in an entry and remove the table. */
1225 static struct bfd_hash_entry
*
1226 output_section_statement_newfunc (struct bfd_hash_entry
*entry
,
1227 struct bfd_hash_table
*table
,
1230 lang_output_section_statement_type
**nextp
;
1231 struct out_section_hash_entry
*ret
;
1235 entry
= (struct bfd_hash_entry
*) bfd_hash_allocate (table
,
1241 entry
= bfd_hash_newfunc (entry
, table
, string
);
1245 ret
= (struct out_section_hash_entry
*) entry
;
1246 memset (&ret
->s
, 0, sizeof (ret
->s
));
1247 ret
->s
.header
.type
= lang_output_section_statement_enum
;
1248 ret
->s
.output_section_statement
.subsection_alignment
= NULL
;
1249 ret
->s
.output_section_statement
.section_alignment
= NULL
;
1250 ret
->s
.output_section_statement
.block_value
= 1;
1251 lang_list_init (&ret
->s
.output_section_statement
.children
);
1252 lang_statement_append (stat_ptr
, &ret
->s
, &ret
->s
.header
.next
);
1254 /* For every output section statement added to the list, except the
1255 first one, lang_os_list.tail points to the "next"
1256 field of the last element of the list. */
1257 if (lang_os_list
.head
!= NULL
)
1258 ret
->s
.output_section_statement
.prev
1259 = ((lang_output_section_statement_type
*)
1260 ((char *) lang_os_list
.tail
1261 - offsetof (lang_output_section_statement_type
, next
)));
1263 /* GCC's strict aliasing rules prevent us from just casting the
1264 address, so we store the pointer in a variable and cast that
1266 nextp
= &ret
->s
.output_section_statement
.next
;
1267 lang_statement_append (&lang_os_list
, &ret
->s
, nextp
);
1272 output_section_statement_table_init (void)
1274 if (!bfd_hash_table_init_n (&output_section_statement_table
,
1275 output_section_statement_newfunc
,
1276 sizeof (struct out_section_hash_entry
),
1278 einfo (_("%F%P: can not create hash table: %E\n"));
1282 output_section_statement_table_free (void)
1284 bfd_hash_table_free (&output_section_statement_table
);
1287 /* Build enough state so that the parser can build its tree. */
1292 obstack_begin (&stat_obstack
, 1000);
1294 stat_ptr
= &statement_list
;
1296 output_section_statement_table_init ();
1298 lang_list_init (stat_ptr
);
1300 lang_list_init (&input_file_chain
);
1301 lang_list_init (&lang_os_list
);
1302 lang_list_init (&file_chain
);
1303 first_file
= lang_add_input_file (NULL
, lang_input_file_is_marker_enum
,
1305 abs_output_section
=
1306 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
, 0, TRUE
);
1308 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
1310 asneeded_list_head
= NULL
;
1311 asneeded_list_tail
= &asneeded_list_head
;
1317 output_section_statement_table_free ();
1320 /*----------------------------------------------------------------------
1321 A region is an area of memory declared with the
1322 MEMORY { name:org=exp, len=exp ... }
1325 We maintain a list of all the regions here.
1327 If no regions are specified in the script, then the default is used
1328 which is created when looked up to be the entire data space.
1330 If create is true we are creating a region inside a MEMORY block.
1331 In this case it is probably an error to create a region that has
1332 already been created. If we are not inside a MEMORY block it is
1333 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
1334 and so we issue a warning.
1336 Each region has at least one name. The first name is either
1337 DEFAULT_MEMORY_REGION or the name given in the MEMORY block. You can add
1338 alias names to an existing region within a script with
1339 REGION_ALIAS (alias, region_name). Each name corresponds to at most one
1342 static lang_memory_region_type
*lang_memory_region_list
;
1343 static lang_memory_region_type
**lang_memory_region_list_tail
1344 = &lang_memory_region_list
;
1346 lang_memory_region_type
*
1347 lang_memory_region_lookup (const char *const name
, bfd_boolean create
)
1349 lang_memory_region_name
*n
;
1350 lang_memory_region_type
*r
;
1351 lang_memory_region_type
*new_region
;
1353 /* NAME is NULL for LMA memspecs if no region was specified. */
1357 for (r
= lang_memory_region_list
; r
!= NULL
; r
= r
->next
)
1358 for (n
= &r
->name_list
; n
!= NULL
; n
= n
->next
)
1359 if (strcmp (n
->name
, name
) == 0)
1362 einfo (_("%P:%pS: warning: redeclaration of memory region `%s'\n"),
1367 if (!create
&& strcmp (name
, DEFAULT_MEMORY_REGION
))
1368 einfo (_("%P:%pS: warning: memory region `%s' not declared\n"),
1371 new_region
= stat_alloc (sizeof (lang_memory_region_type
));
1373 new_region
->name_list
.name
= xstrdup (name
);
1374 new_region
->name_list
.next
= NULL
;
1375 new_region
->next
= NULL
;
1376 new_region
->origin_exp
= NULL
;
1377 new_region
->origin
= 0;
1378 new_region
->length_exp
= NULL
;
1379 new_region
->length
= ~(bfd_size_type
) 0;
1380 new_region
->current
= 0;
1381 new_region
->last_os
= NULL
;
1382 new_region
->flags
= 0;
1383 new_region
->not_flags
= 0;
1384 new_region
->had_full_message
= FALSE
;
1386 *lang_memory_region_list_tail
= new_region
;
1387 lang_memory_region_list_tail
= &new_region
->next
;
1393 lang_memory_region_alias (const char *alias
, const char *region_name
)
1395 lang_memory_region_name
*n
;
1396 lang_memory_region_type
*r
;
1397 lang_memory_region_type
*region
;
1399 /* The default region must be unique. This ensures that it is not necessary
1400 to iterate through the name list if someone wants the check if a region is
1401 the default memory region. */
1402 if (strcmp (region_name
, DEFAULT_MEMORY_REGION
) == 0
1403 || strcmp (alias
, DEFAULT_MEMORY_REGION
) == 0)
1404 einfo (_("%F%P:%pS: error: alias for default memory region\n"), NULL
);
1406 /* Look for the target region and check if the alias is not already
1409 for (r
= lang_memory_region_list
; r
!= NULL
; r
= r
->next
)
1410 for (n
= &r
->name_list
; n
!= NULL
; n
= n
->next
)
1412 if (region
== NULL
&& strcmp (n
->name
, region_name
) == 0)
1414 if (strcmp (n
->name
, alias
) == 0)
1415 einfo (_("%F%P:%pS: error: redefinition of memory region "
1420 /* Check if the target region exists. */
1422 einfo (_("%F%P:%pS: error: memory region `%s' "
1423 "for alias `%s' does not exist\n"),
1424 NULL
, region_name
, alias
);
1426 /* Add alias to region name list. */
1427 n
= stat_alloc (sizeof (lang_memory_region_name
));
1428 n
->name
= xstrdup (alias
);
1429 n
->next
= region
->name_list
.next
;
1430 region
->name_list
.next
= n
;
1433 static lang_memory_region_type
*
1434 lang_memory_default (asection
*section
)
1436 lang_memory_region_type
*p
;
1438 flagword sec_flags
= section
->flags
;
1440 /* Override SEC_DATA to mean a writable section. */
1441 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
1442 sec_flags
|= SEC_DATA
;
1444 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
1446 if ((p
->flags
& sec_flags
) != 0
1447 && (p
->not_flags
& sec_flags
) == 0)
1452 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, FALSE
);
1455 /* Get the output section statement directly from the userdata. */
1457 lang_output_section_statement_type
*
1458 lang_output_section_get (const asection
*output_section
)
1460 return bfd_section_userdata (output_section
);
1463 /* Find or create an output_section_statement with the given NAME.
1464 If CONSTRAINT is non-zero match one with that constraint, otherwise
1465 match any non-negative constraint. If CREATE, always make a
1466 new output_section_statement for SPECIAL CONSTRAINT. */
1468 lang_output_section_statement_type
*
1469 lang_output_section_statement_lookup (const char *name
,
1473 struct out_section_hash_entry
*entry
;
1475 entry
= ((struct out_section_hash_entry
*)
1476 bfd_hash_lookup (&output_section_statement_table
, name
,
1481 einfo (_("%F%P: failed creating section `%s': %E\n"), name
);
1485 if (entry
->s
.output_section_statement
.name
!= NULL
)
1487 /* We have a section of this name, but it might not have the correct
1489 struct out_section_hash_entry
*last_ent
;
1491 name
= entry
->s
.output_section_statement
.name
;
1492 if (create
&& constraint
== SPECIAL
)
1493 /* Not traversing to the end reverses the order of the second
1494 and subsequent SPECIAL sections in the hash table chain,
1495 but that shouldn't matter. */
1500 if (constraint
== entry
->s
.output_section_statement
.constraint
1502 && entry
->s
.output_section_statement
.constraint
>= 0))
1503 return &entry
->s
.output_section_statement
;
1505 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1507 while (entry
!= NULL
1508 && name
== entry
->s
.output_section_statement
.name
);
1514 = ((struct out_section_hash_entry
*)
1515 output_section_statement_newfunc (NULL
,
1516 &output_section_statement_table
,
1520 einfo (_("%F%P: failed creating section `%s': %E\n"), name
);
1523 entry
->root
= last_ent
->root
;
1524 last_ent
->root
.next
= &entry
->root
;
1527 entry
->s
.output_section_statement
.name
= name
;
1528 entry
->s
.output_section_statement
.constraint
= constraint
;
1529 return &entry
->s
.output_section_statement
;
1532 /* Find the next output_section_statement with the same name as OS.
1533 If CONSTRAINT is non-zero, find one with that constraint otherwise
1534 match any non-negative constraint. */
1536 lang_output_section_statement_type
*
1537 next_matching_output_section_statement (lang_output_section_statement_type
*os
,
1540 /* All output_section_statements are actually part of a
1541 struct out_section_hash_entry. */
1542 struct out_section_hash_entry
*entry
= (struct out_section_hash_entry
*)
1544 - offsetof (struct out_section_hash_entry
, s
.output_section_statement
));
1545 const char *name
= os
->name
;
1547 ASSERT (name
== entry
->root
.string
);
1550 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1552 || name
!= entry
->s
.output_section_statement
.name
)
1555 while (constraint
!= entry
->s
.output_section_statement
.constraint
1557 || entry
->s
.output_section_statement
.constraint
< 0));
1559 return &entry
->s
.output_section_statement
;
1562 /* A variant of lang_output_section_find used by place_orphan.
1563 Returns the output statement that should precede a new output
1564 statement for SEC. If an exact match is found on certain flags,
1567 lang_output_section_statement_type
*
1568 lang_output_section_find_by_flags (const asection
*sec
,
1570 lang_output_section_statement_type
**exact
,
1571 lang_match_sec_type_func match_type
)
1573 lang_output_section_statement_type
*first
, *look
, *found
;
1574 flagword look_flags
, differ
;
1576 /* We know the first statement on this list is *ABS*. May as well
1578 first
= (void *) lang_os_list
.head
;
1579 first
= first
->next
;
1581 /* First try for an exact match. */
1583 for (look
= first
; look
; look
= look
->next
)
1585 look_flags
= look
->flags
;
1586 if (look
->bfd_section
!= NULL
)
1588 look_flags
= look
->bfd_section
->flags
;
1589 if (match_type
&& !match_type (link_info
.output_bfd
,
1594 differ
= look_flags
^ sec_flags
;
1595 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
1596 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1606 if ((sec_flags
& SEC_CODE
) != 0
1607 && (sec_flags
& SEC_ALLOC
) != 0)
1609 /* Try for a rw code section. */
1610 for (look
= first
; look
; look
= look
->next
)
1612 look_flags
= look
->flags
;
1613 if (look
->bfd_section
!= NULL
)
1615 look_flags
= look
->bfd_section
->flags
;
1616 if (match_type
&& !match_type (link_info
.output_bfd
,
1621 differ
= look_flags
^ sec_flags
;
1622 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1623 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1627 else if ((sec_flags
& SEC_READONLY
) != 0
1628 && (sec_flags
& SEC_ALLOC
) != 0)
1630 /* .rodata can go after .text, .sdata2 after .rodata. */
1631 for (look
= first
; look
; look
= look
->next
)
1633 look_flags
= look
->flags
;
1634 if (look
->bfd_section
!= NULL
)
1636 look_flags
= look
->bfd_section
->flags
;
1637 if (match_type
&& !match_type (link_info
.output_bfd
,
1642 differ
= look_flags
^ sec_flags
;
1643 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1644 | SEC_READONLY
| SEC_SMALL_DATA
))
1645 || (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1647 && !(look_flags
& SEC_SMALL_DATA
)))
1651 else if ((sec_flags
& SEC_THREAD_LOCAL
) != 0
1652 && (sec_flags
& SEC_ALLOC
) != 0)
1654 /* .tdata can go after .data, .tbss after .tdata. Treat .tbss
1655 as if it were a loaded section, and don't use match_type. */
1656 bfd_boolean seen_thread_local
= FALSE
;
1659 for (look
= first
; look
; look
= look
->next
)
1661 look_flags
= look
->flags
;
1662 if (look
->bfd_section
!= NULL
)
1663 look_flags
= look
->bfd_section
->flags
;
1665 differ
= look_flags
^ (sec_flags
| SEC_LOAD
| SEC_HAS_CONTENTS
);
1666 if (!(differ
& (SEC_THREAD_LOCAL
| SEC_ALLOC
)))
1668 /* .tdata and .tbss must be adjacent and in that order. */
1669 if (!(look_flags
& SEC_LOAD
)
1670 && (sec_flags
& SEC_LOAD
))
1671 /* ..so if we're at a .tbss section and we're placing
1672 a .tdata section stop looking and return the
1673 previous section. */
1676 seen_thread_local
= TRUE
;
1678 else if (seen_thread_local
)
1680 else if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
)))
1684 else if ((sec_flags
& SEC_SMALL_DATA
) != 0
1685 && (sec_flags
& SEC_ALLOC
) != 0)
1687 /* .sdata goes after .data, .sbss after .sdata. */
1688 for (look
= first
; look
; look
= look
->next
)
1690 look_flags
= look
->flags
;
1691 if (look
->bfd_section
!= NULL
)
1693 look_flags
= look
->bfd_section
->flags
;
1694 if (match_type
&& !match_type (link_info
.output_bfd
,
1699 differ
= look_flags
^ sec_flags
;
1700 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1701 | SEC_THREAD_LOCAL
))
1702 || ((look_flags
& SEC_SMALL_DATA
)
1703 && !(sec_flags
& SEC_HAS_CONTENTS
)))
1707 else if ((sec_flags
& SEC_HAS_CONTENTS
) != 0
1708 && (sec_flags
& SEC_ALLOC
) != 0)
1710 /* .data goes after .rodata. */
1711 for (look
= first
; look
; look
= look
->next
)
1713 look_flags
= look
->flags
;
1714 if (look
->bfd_section
!= NULL
)
1716 look_flags
= look
->bfd_section
->flags
;
1717 if (match_type
&& !match_type (link_info
.output_bfd
,
1722 differ
= look_flags
^ sec_flags
;
1723 if (!(differ
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1724 | SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1728 else if ((sec_flags
& SEC_ALLOC
) != 0)
1730 /* .bss goes after any other alloc section. */
1731 for (look
= first
; look
; look
= look
->next
)
1733 look_flags
= look
->flags
;
1734 if (look
->bfd_section
!= NULL
)
1736 look_flags
= look
->bfd_section
->flags
;
1737 if (match_type
&& !match_type (link_info
.output_bfd
,
1742 differ
= look_flags
^ sec_flags
;
1743 if (!(differ
& SEC_ALLOC
))
1749 /* non-alloc go last. */
1750 for (look
= first
; look
; look
= look
->next
)
1752 look_flags
= look
->flags
;
1753 if (look
->bfd_section
!= NULL
)
1754 look_flags
= look
->bfd_section
->flags
;
1755 differ
= look_flags
^ sec_flags
;
1756 if (!(differ
& SEC_DEBUGGING
))
1762 if (found
|| !match_type
)
1765 return lang_output_section_find_by_flags (sec
, sec_flags
, NULL
, NULL
);
1768 /* Find the last output section before given output statement.
1769 Used by place_orphan. */
1772 output_prev_sec_find (lang_output_section_statement_type
*os
)
1774 lang_output_section_statement_type
*lookup
;
1776 for (lookup
= os
->prev
; lookup
!= NULL
; lookup
= lookup
->prev
)
1778 if (lookup
->constraint
< 0)
1781 if (lookup
->bfd_section
!= NULL
&& lookup
->bfd_section
->owner
!= NULL
)
1782 return lookup
->bfd_section
;
1788 /* Look for a suitable place for a new output section statement. The
1789 idea is to skip over anything that might be inside a SECTIONS {}
1790 statement in a script, before we find another output section
1791 statement. Assignments to "dot" before an output section statement
1792 are assumed to belong to it, except in two cases; The first
1793 assignment to dot, and assignments before non-alloc sections.
1794 Otherwise we might put an orphan before . = . + SIZEOF_HEADERS or
1795 similar assignments that set the initial address, or we might
1796 insert non-alloc note sections among assignments setting end of
1799 static lang_statement_union_type
**
1800 insert_os_after (lang_output_section_statement_type
*after
)
1802 lang_statement_union_type
**where
;
1803 lang_statement_union_type
**assign
= NULL
;
1804 bfd_boolean ignore_first
;
1806 ignore_first
= after
== (void *) lang_os_list
.head
;
1808 for (where
= &after
->header
.next
;
1810 where
= &(*where
)->header
.next
)
1812 switch ((*where
)->header
.type
)
1814 case lang_assignment_statement_enum
:
1817 lang_assignment_statement_type
*ass
;
1819 ass
= &(*where
)->assignment_statement
;
1820 if (ass
->exp
->type
.node_class
!= etree_assert
1821 && ass
->exp
->assign
.dst
[0] == '.'
1822 && ass
->exp
->assign
.dst
[1] == 0)
1826 ignore_first
= FALSE
;
1830 case lang_wild_statement_enum
:
1831 case lang_input_section_enum
:
1832 case lang_object_symbols_statement_enum
:
1833 case lang_fill_statement_enum
:
1834 case lang_data_statement_enum
:
1835 case lang_reloc_statement_enum
:
1836 case lang_padding_statement_enum
:
1837 case lang_constructors_statement_enum
:
1839 ignore_first
= FALSE
;
1841 case lang_output_section_statement_enum
:
1844 asection
*s
= (*where
)->output_section_statement
.bfd_section
;
1847 || s
->map_head
.s
== NULL
1848 || (s
->flags
& SEC_ALLOC
) != 0)
1852 case lang_input_statement_enum
:
1853 case lang_address_statement_enum
:
1854 case lang_target_statement_enum
:
1855 case lang_output_statement_enum
:
1856 case lang_group_statement_enum
:
1857 case lang_insert_statement_enum
:
1866 lang_output_section_statement_type
*
1867 lang_insert_orphan (asection
*s
,
1868 const char *secname
,
1870 lang_output_section_statement_type
*after
,
1871 struct orphan_save
*place
,
1872 etree_type
*address
,
1873 lang_statement_list_type
*add_child
)
1875 lang_statement_list_type add
;
1876 lang_output_section_statement_type
*os
;
1877 lang_output_section_statement_type
**os_tail
;
1879 /* If we have found an appropriate place for the output section
1880 statements for this orphan, add them to our own private list,
1881 inserting them later into the global statement list. */
1884 lang_list_init (&add
);
1885 push_stat_ptr (&add
);
1888 if (bfd_link_relocatable (&link_info
)
1889 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0)
1890 address
= exp_intop (0);
1892 os_tail
= (lang_output_section_statement_type
**) lang_os_list
.tail
;
1893 os
= lang_enter_output_section_statement (secname
, address
, normal_section
,
1894 NULL
, NULL
, NULL
, constraint
, 0);
1896 if (add_child
== NULL
)
1897 add_child
= &os
->children
;
1898 lang_add_section (add_child
, s
, NULL
, os
);
1900 if (after
&& (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) != 0)
1902 const char *region
= (after
->region
1903 ? after
->region
->name_list
.name
1904 : DEFAULT_MEMORY_REGION
);
1905 const char *lma_region
= (after
->lma_region
1906 ? after
->lma_region
->name_list
.name
1908 lang_leave_output_section_statement (NULL
, region
, after
->phdrs
,
1912 lang_leave_output_section_statement (NULL
, DEFAULT_MEMORY_REGION
, NULL
,
1915 /* Restore the global list pointer. */
1919 if (after
!= NULL
&& os
->bfd_section
!= NULL
)
1921 asection
*snew
, *as
;
1922 bfd_boolean place_after
= place
->stmt
== NULL
;
1923 bfd_boolean insert_after
= TRUE
;
1925 snew
= os
->bfd_section
;
1927 /* Shuffle the bfd section list to make the output file look
1928 neater. This is really only cosmetic. */
1929 if (place
->section
== NULL
1930 && after
!= (void *) lang_os_list
.head
)
1932 asection
*bfd_section
= after
->bfd_section
;
1934 /* If the output statement hasn't been used to place any input
1935 sections (and thus doesn't have an output bfd_section),
1936 look for the closest prior output statement having an
1938 if (bfd_section
== NULL
)
1939 bfd_section
= output_prev_sec_find (after
);
1941 if (bfd_section
!= NULL
&& bfd_section
!= snew
)
1942 place
->section
= &bfd_section
->next
;
1945 if (place
->section
== NULL
)
1946 place
->section
= &link_info
.output_bfd
->sections
;
1948 as
= *place
->section
;
1952 /* Put the section at the end of the list. */
1954 /* Unlink the section. */
1955 bfd_section_list_remove (link_info
.output_bfd
, snew
);
1957 /* Now tack it back on in the right place. */
1958 bfd_section_list_append (link_info
.output_bfd
, snew
);
1960 else if ((bfd_get_flavour (link_info
.output_bfd
)
1961 == bfd_target_elf_flavour
)
1962 && (bfd_get_flavour (s
->owner
)
1963 == bfd_target_elf_flavour
)
1964 && ((elf_section_type (s
) == SHT_NOTE
1965 && (s
->flags
& SEC_LOAD
) != 0)
1966 || (elf_section_type (as
) == SHT_NOTE
1967 && (as
->flags
& SEC_LOAD
) != 0)))
1969 /* Make sure that output note sections are grouped and sorted
1970 by alignments when inserting a note section or insert a
1971 section after a note section, */
1973 /* A specific section after which the output note section
1974 should be placed. */
1975 asection
*after_sec
;
1976 /* True if we need to insert the orphan section after a
1977 specific section to maintain output note section order. */
1978 bfd_boolean after_sec_note
= FALSE
;
1980 static asection
*first_orphan_note
= NULL
;
1982 /* Group and sort output note section by alignments in
1985 if (elf_section_type (s
) == SHT_NOTE
1986 && (s
->flags
& SEC_LOAD
) != 0)
1988 /* Search from the beginning for the last output note
1989 section with equal or larger alignments. NB: Don't
1990 place orphan note section after non-note sections. */
1992 first_orphan_note
= NULL
;
1993 for (sec
= link_info
.output_bfd
->sections
;
1995 && !bfd_is_abs_section (sec
));
1998 && elf_section_type (sec
) == SHT_NOTE
1999 && (sec
->flags
& SEC_LOAD
) != 0)
2001 if (!first_orphan_note
)
2002 first_orphan_note
= sec
;
2003 if (sec
->alignment_power
>= s
->alignment_power
)
2006 else if (first_orphan_note
)
2008 /* Stop if there is non-note section after the first
2009 orphan note section. */
2013 /* If this will be the first orphan note section, it can
2014 be placed at the default location. */
2015 after_sec_note
= first_orphan_note
!= NULL
;
2016 if (after_sec
== NULL
&& after_sec_note
)
2018 /* If all output note sections have smaller
2019 alignments, place the section before all
2020 output orphan note sections. */
2021 after_sec
= first_orphan_note
;
2022 insert_after
= FALSE
;
2025 else if (first_orphan_note
)
2027 /* Don't place non-note sections in the middle of orphan
2029 after_sec_note
= TRUE
;
2031 for (sec
= as
->next
;
2033 && !bfd_is_abs_section (sec
));
2035 if (elf_section_type (sec
) == SHT_NOTE
2036 && (sec
->flags
& SEC_LOAD
) != 0)
2044 /* Search forward to insert OS after AFTER_SEC output
2046 lang_output_section_statement_type
*stmt
, *next
;
2047 bfd_boolean found
= FALSE
;
2048 for (stmt
= after
; stmt
!= NULL
; stmt
= next
)
2053 if (stmt
->bfd_section
== after_sec
)
2063 /* If INSERT_AFTER is FALSE, place OS before
2064 AFTER_SEC output statement. */
2065 if (next
&& next
->bfd_section
== after_sec
)
2075 /* Search backward to insert OS after AFTER_SEC output
2078 for (stmt
= after
; stmt
!= NULL
; stmt
= stmt
->prev
)
2082 if (stmt
->bfd_section
== after_sec
)
2091 /* If INSERT_AFTER is FALSE, place OS before
2092 AFTER_SEC output statement. */
2093 if (stmt
->next
->bfd_section
== after_sec
)
2103 if (after_sec
== NULL
2104 || (insert_after
&& after_sec
->next
!= snew
)
2105 || (!insert_after
&& after_sec
->prev
!= snew
))
2107 /* Unlink the section. */
2108 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2110 /* Place SNEW after AFTER_SEC. If AFTER_SEC is NULL,
2115 bfd_section_list_insert_after (link_info
.output_bfd
,
2118 bfd_section_list_insert_before (link_info
.output_bfd
,
2122 bfd_section_list_prepend (link_info
.output_bfd
, snew
);
2125 else if (as
!= snew
&& as
->prev
!= snew
)
2127 /* Unlink the section. */
2128 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2130 /* Now tack it back on in the right place. */
2131 bfd_section_list_insert_before (link_info
.output_bfd
,
2135 else if (as
!= snew
&& as
->prev
!= snew
)
2137 /* Unlink the section. */
2138 bfd_section_list_remove (link_info
.output_bfd
, snew
);
2140 /* Now tack it back on in the right place. */
2141 bfd_section_list_insert_before (link_info
.output_bfd
, as
, snew
);
2144 /* Save the end of this list. Further ophans of this type will
2145 follow the one we've just added. */
2146 place
->section
= &snew
->next
;
2148 /* The following is non-cosmetic. We try to put the output
2149 statements in some sort of reasonable order here, because they
2150 determine the final load addresses of the orphan sections.
2151 In addition, placing output statements in the wrong order may
2152 require extra segments. For instance, given a typical
2153 situation of all read-only sections placed in one segment and
2154 following that a segment containing all the read-write
2155 sections, we wouldn't want to place an orphan read/write
2156 section before or amongst the read-only ones. */
2157 if (add
.head
!= NULL
)
2159 lang_output_section_statement_type
*newly_added_os
;
2161 /* Place OS after AFTER if AFTER_NOTE is TRUE. */
2164 lang_statement_union_type
**where
= insert_os_after (after
);
2169 place
->os_tail
= &after
->next
;
2173 /* Put it after the last orphan statement we added. */
2174 *add
.tail
= *place
->stmt
;
2175 *place
->stmt
= add
.head
;
2178 /* Fix the global list pointer if we happened to tack our
2179 new list at the tail. */
2180 if (*stat_ptr
->tail
== add
.head
)
2181 stat_ptr
->tail
= add
.tail
;
2183 /* Save the end of this list. */
2184 place
->stmt
= add
.tail
;
2186 /* Do the same for the list of output section statements. */
2187 newly_added_os
= *os_tail
;
2189 newly_added_os
->prev
= (lang_output_section_statement_type
*)
2190 ((char *) place
->os_tail
2191 - offsetof (lang_output_section_statement_type
, next
));
2192 newly_added_os
->next
= *place
->os_tail
;
2193 if (newly_added_os
->next
!= NULL
)
2194 newly_added_os
->next
->prev
= newly_added_os
;
2195 *place
->os_tail
= newly_added_os
;
2196 place
->os_tail
= &newly_added_os
->next
;
2198 /* Fixing the global list pointer here is a little different.
2199 We added to the list in lang_enter_output_section_statement,
2200 trimmed off the new output_section_statment above when
2201 assigning *os_tail = NULL, but possibly added it back in
2202 the same place when assigning *place->os_tail. */
2203 if (*os_tail
== NULL
)
2204 lang_os_list
.tail
= (lang_statement_union_type
**) os_tail
;
2211 lang_print_asneeded (void)
2213 struct asneeded_minfo
*m
;
2215 if (asneeded_list_head
== NULL
)
2218 minfo (_("\nAs-needed library included to satisfy reference by file (symbol)\n\n"));
2220 for (m
= asneeded_list_head
; m
!= NULL
; m
= m
->next
)
2224 minfo ("%s", m
->soname
);
2225 len
= strlen (m
->soname
);
2239 minfo ("%pB ", m
->ref
);
2240 minfo ("(%pT)\n", m
->name
);
2245 lang_map_flags (flagword flag
)
2247 if (flag
& SEC_ALLOC
)
2250 if (flag
& SEC_CODE
)
2253 if (flag
& SEC_READONLY
)
2256 if (flag
& SEC_DATA
)
2259 if (flag
& SEC_LOAD
)
2266 lang_memory_region_type
*m
;
2267 bfd_boolean dis_header_printed
= FALSE
;
2269 LANG_FOR_EACH_INPUT_STATEMENT (file
)
2273 if ((file
->the_bfd
->flags
& (BFD_LINKER_CREATED
| DYNAMIC
)) != 0
2274 || file
->flags
.just_syms
)
2277 if (config
.print_map_discarded
)
2278 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
2279 if ((s
->output_section
== NULL
2280 || s
->output_section
->owner
!= link_info
.output_bfd
)
2281 && (s
->flags
& (SEC_LINKER_CREATED
| SEC_KEEP
)) == 0)
2283 if (! dis_header_printed
)
2285 fprintf (config
.map_file
, _("\nDiscarded input sections\n\n"));
2286 dis_header_printed
= TRUE
;
2289 print_input_section (s
, TRUE
);
2293 minfo (_("\nMemory Configuration\n\n"));
2294 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
2295 _("Name"), _("Origin"), _("Length"), _("Attributes"));
2297 for (m
= lang_memory_region_list
; m
!= NULL
; m
= m
->next
)
2302 fprintf (config
.map_file
, "%-16s ", m
->name_list
.name
);
2304 sprintf_vma (buf
, m
->origin
);
2305 minfo ("0x%s ", buf
);
2313 minfo ("0x%V", m
->length
);
2314 if (m
->flags
|| m
->not_flags
)
2322 lang_map_flags (m
->flags
);
2328 lang_map_flags (m
->not_flags
);
2335 fprintf (config
.map_file
, _("\nLinker script and memory map\n\n"));
2337 if (!link_info
.reduce_memory_overheads
)
2339 obstack_begin (&map_obstack
, 1000);
2340 bfd_link_hash_traverse (link_info
.hash
, sort_def_symbol
, 0);
2342 expld
.phase
= lang_fixed_phase_enum
;
2343 lang_statement_iteration
++;
2344 print_statements ();
2346 ldemul_extra_map_file_text (link_info
.output_bfd
, &link_info
,
2351 sort_def_symbol (struct bfd_link_hash_entry
*hash_entry
,
2352 void *info ATTRIBUTE_UNUSED
)
2354 if ((hash_entry
->type
== bfd_link_hash_defined
2355 || hash_entry
->type
== bfd_link_hash_defweak
)
2356 && hash_entry
->u
.def
.section
->owner
!= link_info
.output_bfd
2357 && hash_entry
->u
.def
.section
->owner
!= NULL
)
2359 input_section_userdata_type
*ud
;
2360 struct map_symbol_def
*def
;
2362 ud
= bfd_section_userdata (hash_entry
->u
.def
.section
);
2365 ud
= stat_alloc (sizeof (*ud
));
2366 bfd_set_section_userdata (hash_entry
->u
.def
.section
, ud
);
2367 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
2368 ud
->map_symbol_def_count
= 0;
2370 else if (!ud
->map_symbol_def_tail
)
2371 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
2373 def
= (struct map_symbol_def
*) obstack_alloc (&map_obstack
, sizeof *def
);
2374 def
->entry
= hash_entry
;
2375 *(ud
->map_symbol_def_tail
) = def
;
2376 ud
->map_symbol_def_tail
= &def
->next
;
2377 ud
->map_symbol_def_count
++;
2382 /* Initialize an output section. */
2385 init_os (lang_output_section_statement_type
*s
, flagword flags
)
2387 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
2388 einfo (_("%F%P: illegal use of `%s' section\n"), DISCARD_SECTION_NAME
);
2390 if (s
->constraint
!= SPECIAL
)
2391 s
->bfd_section
= bfd_get_section_by_name (link_info
.output_bfd
, s
->name
);
2392 if (s
->bfd_section
== NULL
)
2393 s
->bfd_section
= bfd_make_section_anyway_with_flags (link_info
.output_bfd
,
2395 if (s
->bfd_section
== NULL
)
2397 einfo (_("%F%P: output format %s cannot represent section"
2398 " called %s: %E\n"),
2399 link_info
.output_bfd
->xvec
->name
, s
->name
);
2401 s
->bfd_section
->output_section
= s
->bfd_section
;
2402 s
->bfd_section
->output_offset
= 0;
2404 /* Set the userdata of the output section to the output section
2405 statement to avoid lookup. */
2406 bfd_set_section_userdata (s
->bfd_section
, s
);
2408 /* If there is a base address, make sure that any sections it might
2409 mention are initialized. */
2410 if (s
->addr_tree
!= NULL
)
2411 exp_init_os (s
->addr_tree
);
2413 if (s
->load_base
!= NULL
)
2414 exp_init_os (s
->load_base
);
2416 /* If supplied an alignment, set it. */
2417 if (s
->section_alignment
!= NULL
)
2418 s
->bfd_section
->alignment_power
= exp_get_power (s
->section_alignment
,
2419 "section alignment");
2422 /* Make sure that all output sections mentioned in an expression are
2426 exp_init_os (etree_type
*exp
)
2428 switch (exp
->type
.node_class
)
2432 case etree_provided
:
2433 exp_init_os (exp
->assign
.src
);
2437 exp_init_os (exp
->binary
.lhs
);
2438 exp_init_os (exp
->binary
.rhs
);
2442 exp_init_os (exp
->trinary
.cond
);
2443 exp_init_os (exp
->trinary
.lhs
);
2444 exp_init_os (exp
->trinary
.rhs
);
2448 exp_init_os (exp
->assert_s
.child
);
2452 exp_init_os (exp
->unary
.child
);
2456 switch (exp
->type
.node_code
)
2462 lang_output_section_statement_type
*os
;
2464 os
= lang_output_section_find (exp
->name
.name
);
2465 if (os
!= NULL
&& os
->bfd_section
== NULL
)
2477 section_already_linked (bfd
*abfd
, asection
*sec
, void *data
)
2479 lang_input_statement_type
*entry
= (lang_input_statement_type
*) data
;
2481 /* If we are only reading symbols from this object, then we want to
2482 discard all sections. */
2483 if (entry
->flags
.just_syms
)
2485 bfd_link_just_syms (abfd
, sec
, &link_info
);
2489 /* Deal with SHF_EXCLUDE ELF sections. */
2490 if (!bfd_link_relocatable (&link_info
)
2491 && (abfd
->flags
& BFD_PLUGIN
) == 0
2492 && (sec
->flags
& (SEC_GROUP
| SEC_KEEP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
2493 sec
->output_section
= bfd_abs_section_ptr
;
2495 if (!(abfd
->flags
& DYNAMIC
))
2496 bfd_section_already_linked (abfd
, sec
, &link_info
);
2500 /* Returns true if SECTION is one we know will be discarded based on its
2501 section flags, otherwise returns false. */
2504 lang_discard_section_p (asection
*section
)
2506 bfd_boolean discard
;
2507 flagword flags
= section
->flags
;
2509 /* Discard sections marked with SEC_EXCLUDE. */
2510 discard
= (flags
& SEC_EXCLUDE
) != 0;
2512 /* Discard the group descriptor sections when we're finally placing the
2513 sections from within the group. */
2514 if ((flags
& SEC_GROUP
) != 0
2515 && link_info
.resolve_section_groups
)
2518 /* Discard debugging sections if we are stripping debugging
2520 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
2521 && (flags
& SEC_DEBUGGING
) != 0)
2527 /* The wild routines.
2529 These expand statements like *(.text) and foo.o to a list of
2530 explicit actions, like foo.o(.text), bar.o(.text) and
2531 foo.o(.text, .data). */
2533 /* Add SECTION to the output section OUTPUT. Do this by creating a
2534 lang_input_section statement which is placed at PTR. */
2537 lang_add_section (lang_statement_list_type
*ptr
,
2539 struct flag_info
*sflag_info
,
2540 lang_output_section_statement_type
*output
)
2542 flagword flags
= section
->flags
;
2544 bfd_boolean discard
;
2545 lang_input_section_type
*new_section
;
2546 bfd
*abfd
= link_info
.output_bfd
;
2548 /* Is this section one we know should be discarded? */
2549 discard
= lang_discard_section_p (section
);
2551 /* Discard input sections which are assigned to a section named
2552 DISCARD_SECTION_NAME. */
2553 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
2558 if (section
->output_section
== NULL
)
2560 /* This prevents future calls from assigning this section. */
2561 section
->output_section
= bfd_abs_section_ptr
;
2563 else if (link_info
.non_contiguous_regions_warnings
)
2564 einfo (_("%P:%pS: warning: --enable-non-contiguous-regions makes "
2565 "section `%pA' from '%pB' match /DISCARD/ clause.\n"),
2566 NULL
, section
, section
->owner
);
2575 keep
= bfd_lookup_section_flags (&link_info
, sflag_info
, section
);
2580 if (section
->output_section
!= NULL
)
2582 if (!link_info
.non_contiguous_regions
)
2585 /* SECTION has already been handled in a special way
2586 (eg. LINK_ONCE): skip it. */
2587 if (bfd_is_abs_section (section
->output_section
))
2590 /* Already assigned to the same output section, do not process
2591 it again, to avoid creating loops between duplicate sections
2593 if (section
->output_section
== output
->bfd_section
)
2596 if (link_info
.non_contiguous_regions_warnings
&& output
->bfd_section
)
2597 einfo (_("%P:%pS: warning: --enable-non-contiguous-regions may "
2598 "change behaviour for section `%pA' from '%pB' (assigned to "
2599 "%pA, but additional match: %pA)\n"),
2600 NULL
, section
, section
->owner
, section
->output_section
,
2601 output
->bfd_section
);
2603 /* SECTION has already been assigned to an output section, but
2604 the user allows it to be mapped to another one in case it
2605 overflows. We'll later update the actual output section in
2606 size_input_section as appropriate. */
2609 /* We don't copy the SEC_NEVER_LOAD flag from an input section
2610 to an output section, because we want to be able to include a
2611 SEC_NEVER_LOAD section in the middle of an otherwise loaded
2612 section (I don't know why we want to do this, but we do).
2613 build_link_order in ldwrite.c handles this case by turning
2614 the embedded SEC_NEVER_LOAD section into a fill. */
2615 flags
&= ~ SEC_NEVER_LOAD
;
2617 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
2618 already been processed. One reason to do this is that on pe
2619 format targets, .text$foo sections go into .text and it's odd
2620 to see .text with SEC_LINK_ONCE set. */
2621 if ((flags
& (SEC_LINK_ONCE
| SEC_GROUP
)) == (SEC_LINK_ONCE
| SEC_GROUP
))
2623 if (link_info
.resolve_section_groups
)
2624 flags
&= ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
);
2626 flags
&= ~(SEC_LINK_DUPLICATES
| SEC_RELOC
);
2628 else if (!bfd_link_relocatable (&link_info
))
2629 flags
&= ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
);
2631 switch (output
->sectype
)
2633 case normal_section
:
2634 case overlay_section
:
2635 case first_overlay_section
:
2637 case noalloc_section
:
2638 flags
&= ~SEC_ALLOC
;
2640 case noload_section
:
2642 flags
|= SEC_NEVER_LOAD
;
2643 /* Unfortunately GNU ld has managed to evolve two different
2644 meanings to NOLOAD in scripts. ELF gets a .bss style noload,
2645 alloc, no contents section. All others get a noload, noalloc
2647 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
)
2648 flags
&= ~SEC_HAS_CONTENTS
;
2650 flags
&= ~SEC_ALLOC
;
2654 if (output
->bfd_section
== NULL
)
2655 init_os (output
, flags
);
2657 /* If SEC_READONLY is not set in the input section, then clear
2658 it from the output section. */
2659 output
->bfd_section
->flags
&= flags
| ~SEC_READONLY
;
2661 if (output
->bfd_section
->linker_has_input
)
2663 /* Only set SEC_READONLY flag on the first input section. */
2664 flags
&= ~ SEC_READONLY
;
2666 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
2667 if ((output
->bfd_section
->flags
& (SEC_MERGE
| SEC_STRINGS
))
2668 != (flags
& (SEC_MERGE
| SEC_STRINGS
))
2669 || ((flags
& SEC_MERGE
) != 0
2670 && output
->bfd_section
->entsize
!= section
->entsize
))
2672 output
->bfd_section
->flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2673 flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2676 output
->bfd_section
->flags
|= flags
;
2678 if (!output
->bfd_section
->linker_has_input
)
2680 output
->bfd_section
->linker_has_input
= 1;
2681 /* This must happen after flags have been updated. The output
2682 section may have been created before we saw its first input
2683 section, eg. for a data statement. */
2684 bfd_init_private_section_data (section
->owner
, section
,
2685 link_info
.output_bfd
,
2686 output
->bfd_section
,
2688 if ((flags
& SEC_MERGE
) != 0)
2689 output
->bfd_section
->entsize
= section
->entsize
;
2692 if ((flags
& SEC_TIC54X_BLOCK
) != 0
2693 && bfd_get_arch (section
->owner
) == bfd_arch_tic54x
)
2695 /* FIXME: This value should really be obtained from the bfd... */
2696 output
->block_value
= 128;
2699 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
2700 output
->bfd_section
->alignment_power
= section
->alignment_power
;
2702 section
->output_section
= output
->bfd_section
;
2704 if (!map_head_is_link_order
)
2706 asection
*s
= output
->bfd_section
->map_tail
.s
;
2707 output
->bfd_section
->map_tail
.s
= section
;
2708 section
->map_head
.s
= NULL
;
2709 section
->map_tail
.s
= s
;
2711 s
->map_head
.s
= section
;
2713 output
->bfd_section
->map_head
.s
= section
;
2716 /* Add a section reference to the list. */
2717 new_section
= new_stat (lang_input_section
, ptr
);
2718 new_section
->section
= section
;
2721 /* Handle wildcard sorting. This returns the lang_input_section which
2722 should follow the one we are going to create for SECTION and FILE,
2723 based on the sorting requirements of WILD. It returns NULL if the
2724 new section should just go at the end of the current list. */
2726 static lang_statement_union_type
*
2727 wild_sort (lang_wild_statement_type
*wild
,
2728 struct wildcard_list
*sec
,
2729 lang_input_statement_type
*file
,
2732 lang_statement_union_type
*l
;
2734 if (!wild
->filenames_sorted
2735 && (sec
== NULL
|| sec
->spec
.sorted
== none
))
2738 for (l
= wild
->children
.head
; l
!= NULL
; l
= l
->header
.next
)
2740 lang_input_section_type
*ls
;
2742 if (l
->header
.type
!= lang_input_section_enum
)
2744 ls
= &l
->input_section
;
2746 /* Sorting by filename takes precedence over sorting by section
2749 if (wild
->filenames_sorted
)
2751 const char *fn
, *ln
;
2755 /* The PE support for the .idata section as generated by
2756 dlltool assumes that files will be sorted by the name of
2757 the archive and then the name of the file within the
2760 if (file
->the_bfd
!= NULL
2761 && file
->the_bfd
->my_archive
!= NULL
)
2763 fn
= bfd_get_filename (file
->the_bfd
->my_archive
);
2768 fn
= file
->filename
;
2772 if (ls
->section
->owner
->my_archive
!= NULL
)
2774 ln
= bfd_get_filename (ls
->section
->owner
->my_archive
);
2779 ln
= ls
->section
->owner
->filename
;
2783 i
= filename_cmp (fn
, ln
);
2792 fn
= file
->filename
;
2794 ln
= ls
->section
->owner
->filename
;
2796 i
= filename_cmp (fn
, ln
);
2804 /* Here either the files are not sorted by name, or we are
2805 looking at the sections for this file. */
2808 && sec
->spec
.sorted
!= none
2809 && sec
->spec
.sorted
!= by_none
)
2810 if (compare_section (sec
->spec
.sorted
, section
, ls
->section
) < 0)
2817 /* Expand a wild statement for a particular FILE. SECTION may be
2818 NULL, in which case it is a wild card. */
2821 output_section_callback (lang_wild_statement_type
*ptr
,
2822 struct wildcard_list
*sec
,
2824 struct flag_info
*sflag_info
,
2825 lang_input_statement_type
*file
,
2828 lang_statement_union_type
*before
;
2829 lang_output_section_statement_type
*os
;
2831 os
= (lang_output_section_statement_type
*) output
;
2833 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2834 if (unique_section_p (section
, os
))
2837 before
= wild_sort (ptr
, sec
, file
, section
);
2839 /* Here BEFORE points to the lang_input_section which
2840 should follow the one we are about to add. If BEFORE
2841 is NULL, then the section should just go at the end
2842 of the current list. */
2845 lang_add_section (&ptr
->children
, section
, sflag_info
, os
);
2848 lang_statement_list_type list
;
2849 lang_statement_union_type
**pp
;
2851 lang_list_init (&list
);
2852 lang_add_section (&list
, section
, sflag_info
, os
);
2854 /* If we are discarding the section, LIST.HEAD will
2856 if (list
.head
!= NULL
)
2858 ASSERT (list
.head
->header
.next
== NULL
);
2860 for (pp
= &ptr
->children
.head
;
2862 pp
= &(*pp
)->header
.next
)
2863 ASSERT (*pp
!= NULL
);
2865 list
.head
->header
.next
= *pp
;
2871 /* Check if all sections in a wild statement for a particular FILE
2875 check_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
2876 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
2878 struct flag_info
*sflag_info ATTRIBUTE_UNUSED
,
2879 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
2882 lang_output_section_statement_type
*os
;
2884 os
= (lang_output_section_statement_type
*) output
;
2886 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2887 if (unique_section_p (section
, os
))
2890 if (section
->output_section
== NULL
&& (section
->flags
& SEC_READONLY
) == 0)
2891 os
->all_input_readonly
= FALSE
;
2894 /* This is passed a file name which must have been seen already and
2895 added to the statement tree. We will see if it has been opened
2896 already and had its symbols read. If not then we'll read it. */
2898 static lang_input_statement_type
*
2899 lookup_name (const char *name
)
2901 lang_input_statement_type
*search
;
2903 for (search
= (void *) input_file_chain
.head
;
2905 search
= search
->next_real_file
)
2907 /* Use the local_sym_name as the name of the file that has
2908 already been loaded as filename might have been transformed
2909 via the search directory lookup mechanism. */
2910 const char *filename
= search
->local_sym_name
;
2912 if (filename
!= NULL
2913 && filename_cmp (filename
, name
) == 0)
2919 /* Arrange to splice the input statement added by new_afile into
2920 statement_list after the current input_file_chain tail.
2921 We know input_file_chain is not an empty list, and that
2922 lookup_name was called via open_input_bfds. Later calls to
2923 lookup_name should always match an existing input_statement. */
2924 lang_statement_union_type
**tail
= stat_ptr
->tail
;
2925 lang_statement_union_type
**after
2926 = (void *) ((char *) input_file_chain
.tail
2927 - offsetof (lang_input_statement_type
, next_real_file
)
2928 + offsetof (lang_input_statement_type
, header
.next
));
2929 lang_statement_union_type
*rest
= *after
;
2930 stat_ptr
->tail
= after
;
2931 search
= new_afile (name
, lang_input_file_is_search_file_enum
,
2932 default_target
, NULL
);
2933 *stat_ptr
->tail
= rest
;
2935 stat_ptr
->tail
= tail
;
2938 /* If we have already added this file, or this file is not real
2939 don't add this file. */
2940 if (search
->flags
.loaded
|| !search
->flags
.real
)
2943 if (!load_symbols (search
, NULL
))
2949 /* Save LIST as a list of libraries whose symbols should not be exported. */
2954 struct excluded_lib
*next
;
2956 static struct excluded_lib
*excluded_libs
;
2959 add_excluded_libs (const char *list
)
2961 const char *p
= list
, *end
;
2965 struct excluded_lib
*entry
;
2966 end
= strpbrk (p
, ",:");
2968 end
= p
+ strlen (p
);
2969 entry
= (struct excluded_lib
*) xmalloc (sizeof (*entry
));
2970 entry
->next
= excluded_libs
;
2971 entry
->name
= (char *) xmalloc (end
- p
+ 1);
2972 memcpy (entry
->name
, p
, end
- p
);
2973 entry
->name
[end
- p
] = '\0';
2974 excluded_libs
= entry
;
2982 check_excluded_libs (bfd
*abfd
)
2984 struct excluded_lib
*lib
= excluded_libs
;
2988 int len
= strlen (lib
->name
);
2989 const char *filename
= lbasename (abfd
->filename
);
2991 if (strcmp (lib
->name
, "ALL") == 0)
2993 abfd
->no_export
= TRUE
;
2997 if (filename_ncmp (lib
->name
, filename
, len
) == 0
2998 && (filename
[len
] == '\0'
2999 || (filename
[len
] == '.' && filename
[len
+ 1] == 'a'
3000 && filename
[len
+ 2] == '\0')))
3002 abfd
->no_export
= TRUE
;
3010 /* Get the symbols for an input file. */
3013 load_symbols (lang_input_statement_type
*entry
,
3014 lang_statement_list_type
*place
)
3018 if (entry
->flags
.loaded
)
3021 ldfile_open_file (entry
);
3023 /* Do not process further if the file was missing. */
3024 if (entry
->flags
.missing_file
)
3027 if (trace_files
|| verbose
)
3028 info_msg ("%pI\n", entry
);
3030 if (!bfd_check_format (entry
->the_bfd
, bfd_archive
)
3031 && !bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
3034 struct lang_input_statement_flags save_flags
;
3037 err
= bfd_get_error ();
3039 /* See if the emulation has some special knowledge. */
3040 if (ldemul_unrecognized_file (entry
))
3043 if (err
== bfd_error_file_ambiguously_recognized
)
3047 einfo (_("%P: %pB: file not recognized: %E;"
3048 " matching formats:"), entry
->the_bfd
);
3049 for (p
= matching
; *p
!= NULL
; p
++)
3053 else if (err
!= bfd_error_file_not_recognized
3055 einfo (_("%F%P: %pB: file not recognized: %E\n"), entry
->the_bfd
);
3057 bfd_close (entry
->the_bfd
);
3058 entry
->the_bfd
= NULL
;
3060 /* Try to interpret the file as a linker script. */
3061 save_flags
= input_flags
;
3062 ldfile_open_command_file (entry
->filename
);
3064 push_stat_ptr (place
);
3065 input_flags
.add_DT_NEEDED_for_regular
3066 = entry
->flags
.add_DT_NEEDED_for_regular
;
3067 input_flags
.add_DT_NEEDED_for_dynamic
3068 = entry
->flags
.add_DT_NEEDED_for_dynamic
;
3069 input_flags
.whole_archive
= entry
->flags
.whole_archive
;
3070 input_flags
.dynamic
= entry
->flags
.dynamic
;
3072 ldfile_assumed_script
= TRUE
;
3073 parser_input
= input_script
;
3074 current_input_file
= entry
->filename
;
3076 current_input_file
= NULL
;
3077 ldfile_assumed_script
= FALSE
;
3079 /* missing_file is sticky. sysrooted will already have been
3080 restored when seeing EOF in yyparse, but no harm to restore
3082 save_flags
.missing_file
|= input_flags
.missing_file
;
3083 input_flags
= save_flags
;
3087 entry
->flags
.loaded
= TRUE
;
3092 if (ldemul_recognized_file (entry
))
3095 /* We don't call ldlang_add_file for an archive. Instead, the
3096 add_symbols entry point will call ldlang_add_file, via the
3097 add_archive_element callback, for each element of the archive
3099 switch (bfd_get_format (entry
->the_bfd
))
3105 if (!entry
->flags
.reload
)
3106 ldlang_add_file (entry
);
3110 check_excluded_libs (entry
->the_bfd
);
3112 bfd_set_usrdata (entry
->the_bfd
, entry
);
3113 if (entry
->flags
.whole_archive
)
3116 bfd_boolean loaded
= TRUE
;
3121 member
= bfd_openr_next_archived_file (entry
->the_bfd
, member
);
3126 if (!bfd_check_format (member
, bfd_object
))
3128 einfo (_("%F%P: %pB: member %pB in archive is not an object\n"),
3129 entry
->the_bfd
, member
);
3134 if (!(*link_info
.callbacks
3135 ->add_archive_element
) (&link_info
, member
,
3136 "--whole-archive", &subsbfd
))
3139 /* Potentially, the add_archive_element hook may have set a
3140 substitute BFD for us. */
3141 if (!bfd_link_add_symbols (subsbfd
, &link_info
))
3143 einfo (_("%F%P: %pB: error adding symbols: %E\n"), member
);
3148 entry
->flags
.loaded
= loaded
;
3154 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
3155 entry
->flags
.loaded
= TRUE
;
3157 einfo (_("%F%P: %pB: error adding symbols: %E\n"), entry
->the_bfd
);
3159 return entry
->flags
.loaded
;
3162 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
3163 may be NULL, indicating that it is a wildcard. Separate
3164 lang_input_section statements are created for each part of the
3165 expansion; they are added after the wild statement S. OUTPUT is
3166 the output section. */
3169 wild (lang_wild_statement_type
*s
,
3170 const char *target ATTRIBUTE_UNUSED
,
3171 lang_output_section_statement_type
*output
)
3173 struct wildcard_list
*sec
;
3175 if (s
->handler_data
[0]
3176 && s
->handler_data
[0]->spec
.sorted
== by_name
3177 && !s
->filenames_sorted
)
3179 lang_section_bst_type
*tree
;
3181 walk_wild (s
, output_section_callback_fast
, output
);
3186 output_section_callback_tree_to_list (s
, tree
, output
);
3191 walk_wild (s
, output_section_callback
, output
);
3193 if (default_common_section
== NULL
)
3194 for (sec
= s
->section_list
; sec
!= NULL
; sec
= sec
->next
)
3195 if (sec
->spec
.name
!= NULL
&& strcmp (sec
->spec
.name
, "COMMON") == 0)
3197 /* Remember the section that common is going to in case we
3198 later get something which doesn't know where to put it. */
3199 default_common_section
= output
;
3204 /* Return TRUE iff target is the sought target. */
3207 get_target (const bfd_target
*target
, void *data
)
3209 const char *sought
= (const char *) data
;
3211 return strcmp (target
->name
, sought
) == 0;
3214 /* Like strcpy() but convert to lower case as well. */
3217 stricpy (char *dest
, const char *src
)
3221 while ((c
= *src
++) != 0)
3222 *dest
++ = TOLOWER (c
);
3227 /* Remove the first occurrence of needle (if any) in haystack
3231 strcut (char *haystack
, const char *needle
)
3233 haystack
= strstr (haystack
, needle
);
3239 for (src
= haystack
+ strlen (needle
); *src
;)
3240 *haystack
++ = *src
++;
3246 /* Compare two target format name strings.
3247 Return a value indicating how "similar" they are. */
3250 name_compare (const char *first
, const char *second
)
3256 copy1
= (char *) xmalloc (strlen (first
) + 1);
3257 copy2
= (char *) xmalloc (strlen (second
) + 1);
3259 /* Convert the names to lower case. */
3260 stricpy (copy1
, first
);
3261 stricpy (copy2
, second
);
3263 /* Remove size and endian strings from the name. */
3264 strcut (copy1
, "big");
3265 strcut (copy1
, "little");
3266 strcut (copy2
, "big");
3267 strcut (copy2
, "little");
3269 /* Return a value based on how many characters match,
3270 starting from the beginning. If both strings are
3271 the same then return 10 * their length. */
3272 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
3273 if (copy1
[result
] == 0)
3285 /* Set by closest_target_match() below. */
3286 static const bfd_target
*winner
;
3288 /* Scan all the valid bfd targets looking for one that has the endianness
3289 requirement that was specified on the command line, and is the nearest
3290 match to the original output target. */
3293 closest_target_match (const bfd_target
*target
, void *data
)
3295 const bfd_target
*original
= (const bfd_target
*) data
;
3297 if (command_line
.endian
== ENDIAN_BIG
3298 && target
->byteorder
!= BFD_ENDIAN_BIG
)
3301 if (command_line
.endian
== ENDIAN_LITTLE
3302 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
3305 /* Must be the same flavour. */
3306 if (target
->flavour
!= original
->flavour
)
3309 /* Ignore generic big and little endian elf vectors. */
3310 if (strcmp (target
->name
, "elf32-big") == 0
3311 || strcmp (target
->name
, "elf64-big") == 0
3312 || strcmp (target
->name
, "elf32-little") == 0
3313 || strcmp (target
->name
, "elf64-little") == 0)
3316 /* If we have not found a potential winner yet, then record this one. */
3323 /* Oh dear, we now have two potential candidates for a successful match.
3324 Compare their names and choose the better one. */
3325 if (name_compare (target
->name
, original
->name
)
3326 > name_compare (winner
->name
, original
->name
))
3329 /* Keep on searching until wqe have checked them all. */
3333 /* Return the BFD target format of the first input file. */
3336 get_first_input_target (void)
3338 const char *target
= NULL
;
3340 LANG_FOR_EACH_INPUT_STATEMENT (s
)
3342 if (s
->header
.type
== lang_input_statement_enum
3345 ldfile_open_file (s
);
3347 if (s
->the_bfd
!= NULL
3348 && bfd_check_format (s
->the_bfd
, bfd_object
))
3350 target
= bfd_get_target (s
->the_bfd
);
3362 lang_get_output_target (void)
3366 /* Has the user told us which output format to use? */
3367 if (output_target
!= NULL
)
3368 return output_target
;
3370 /* No - has the current target been set to something other than
3372 if (current_target
!= default_target
&& current_target
!= NULL
)
3373 return current_target
;
3375 /* No - can we determine the format of the first input file? */
3376 target
= get_first_input_target ();
3380 /* Failed - use the default output target. */
3381 return default_target
;
3384 /* Open the output file. */
3387 open_output (const char *name
)
3389 output_target
= lang_get_output_target ();
3391 /* Has the user requested a particular endianness on the command
3393 if (command_line
.endian
!= ENDIAN_UNSET
)
3395 /* Get the chosen target. */
3396 const bfd_target
*target
3397 = bfd_iterate_over_targets (get_target
, (void *) output_target
);
3399 /* If the target is not supported, we cannot do anything. */
3402 enum bfd_endian desired_endian
;
3404 if (command_line
.endian
== ENDIAN_BIG
)
3405 desired_endian
= BFD_ENDIAN_BIG
;
3407 desired_endian
= BFD_ENDIAN_LITTLE
;
3409 /* See if the target has the wrong endianness. This should
3410 not happen if the linker script has provided big and
3411 little endian alternatives, but some scrips don't do
3413 if (target
->byteorder
!= desired_endian
)
3415 /* If it does, then see if the target provides
3416 an alternative with the correct endianness. */
3417 if (target
->alternative_target
!= NULL
3418 && (target
->alternative_target
->byteorder
== desired_endian
))
3419 output_target
= target
->alternative_target
->name
;
3422 /* Try to find a target as similar as possible to
3423 the default target, but which has the desired
3424 endian characteristic. */
3425 bfd_iterate_over_targets (closest_target_match
,
3428 /* Oh dear - we could not find any targets that
3429 satisfy our requirements. */
3431 einfo (_("%P: warning: could not find any targets"
3432 " that match endianness requirement\n"));
3434 output_target
= winner
->name
;
3440 link_info
.output_bfd
= bfd_openw (name
, output_target
);
3442 if (link_info
.output_bfd
== NULL
)
3444 if (bfd_get_error () == bfd_error_invalid_target
)
3445 einfo (_("%F%P: target %s not found\n"), output_target
);
3447 einfo (_("%F%P: cannot open output file %s: %E\n"), name
);
3450 delete_output_file_on_failure
= TRUE
;
3452 if (!bfd_set_format (link_info
.output_bfd
, bfd_object
))
3453 einfo (_("%F%P: %s: can not make object file: %E\n"), name
);
3454 if (!bfd_set_arch_mach (link_info
.output_bfd
,
3455 ldfile_output_architecture
,
3456 ldfile_output_machine
))
3457 einfo (_("%F%P: %s: can not set architecture: %E\n"), name
);
3459 link_info
.hash
= bfd_link_hash_table_create (link_info
.output_bfd
);
3460 if (link_info
.hash
== NULL
)
3461 einfo (_("%F%P: can not create hash table: %E\n"));
3463 bfd_set_gp_size (link_info
.output_bfd
, g_switch_value
);
3467 ldlang_open_output (lang_statement_union_type
*statement
)
3469 switch (statement
->header
.type
)
3471 case lang_output_statement_enum
:
3472 ASSERT (link_info
.output_bfd
== NULL
);
3473 open_output (statement
->output_statement
.name
);
3474 ldemul_set_output_arch ();
3475 if (config
.magic_demand_paged
3476 && !bfd_link_relocatable (&link_info
))
3477 link_info
.output_bfd
->flags
|= D_PAGED
;
3479 link_info
.output_bfd
->flags
&= ~D_PAGED
;
3480 if (config
.text_read_only
)
3481 link_info
.output_bfd
->flags
|= WP_TEXT
;
3483 link_info
.output_bfd
->flags
&= ~WP_TEXT
;
3484 if (link_info
.traditional_format
)
3485 link_info
.output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
3487 link_info
.output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
3490 case lang_target_statement_enum
:
3491 current_target
= statement
->target_statement
.target
;
3499 init_opb (asection
*s
)
3504 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
3506 && (s
->flags
& SEC_ELF_OCTETS
) != 0)
3509 x
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3510 ldfile_output_machine
);
3512 while ((x
& 1) == 0)
3520 /* Open all the input files. */
3524 OPEN_BFD_NORMAL
= 0,
3528 #ifdef ENABLE_PLUGINS
3529 static lang_input_statement_type
*plugin_insert
= NULL
;
3530 static struct bfd_link_hash_entry
*plugin_undefs
= NULL
;
3534 open_input_bfds (lang_statement_union_type
*s
, enum open_bfd_mode mode
)
3536 for (; s
!= NULL
; s
= s
->header
.next
)
3538 switch (s
->header
.type
)
3540 case lang_constructors_statement_enum
:
3541 open_input_bfds (constructor_list
.head
, mode
);
3543 case lang_output_section_statement_enum
:
3544 open_input_bfds (s
->output_section_statement
.children
.head
, mode
);
3546 case lang_wild_statement_enum
:
3547 /* Maybe we should load the file's symbols. */
3548 if ((mode
& OPEN_BFD_RESCAN
) == 0
3549 && s
->wild_statement
.filename
3550 && !wildcardp (s
->wild_statement
.filename
)
3551 && !archive_path (s
->wild_statement
.filename
))
3552 lookup_name (s
->wild_statement
.filename
);
3553 open_input_bfds (s
->wild_statement
.children
.head
, mode
);
3555 case lang_group_statement_enum
:
3557 struct bfd_link_hash_entry
*undefs
;
3558 #ifdef ENABLE_PLUGINS
3559 lang_input_statement_type
*plugin_insert_save
;
3562 /* We must continually search the entries in the group
3563 until no new symbols are added to the list of undefined
3568 #ifdef ENABLE_PLUGINS
3569 plugin_insert_save
= plugin_insert
;
3571 undefs
= link_info
.hash
->undefs_tail
;
3572 open_input_bfds (s
->group_statement
.children
.head
,
3573 mode
| OPEN_BFD_FORCE
);
3575 while (undefs
!= link_info
.hash
->undefs_tail
3576 #ifdef ENABLE_PLUGINS
3577 /* Objects inserted by a plugin, which are loaded
3578 before we hit this loop, may have added new
3580 || (plugin_insert
!= plugin_insert_save
&& plugin_undefs
)
3585 case lang_target_statement_enum
:
3586 current_target
= s
->target_statement
.target
;
3588 case lang_input_statement_enum
:
3589 if (s
->input_statement
.flags
.real
)
3591 lang_statement_union_type
**os_tail
;
3592 lang_statement_list_type add
;
3595 s
->input_statement
.target
= current_target
;
3597 /* If we are being called from within a group, and this
3598 is an archive which has already been searched, then
3599 force it to be researched unless the whole archive
3600 has been loaded already. Do the same for a rescan.
3601 Likewise reload --as-needed shared libs. */
3602 if (mode
!= OPEN_BFD_NORMAL
3603 #ifdef ENABLE_PLUGINS
3604 && ((mode
& OPEN_BFD_RESCAN
) == 0
3605 || plugin_insert
== NULL
)
3607 && s
->input_statement
.flags
.loaded
3608 && (abfd
= s
->input_statement
.the_bfd
) != NULL
3609 && ((bfd_get_format (abfd
) == bfd_archive
3610 && !s
->input_statement
.flags
.whole_archive
)
3611 || (bfd_get_format (abfd
) == bfd_object
3612 && ((abfd
->flags
) & DYNAMIC
) != 0
3613 && s
->input_statement
.flags
.add_DT_NEEDED_for_regular
3614 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
3615 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)))
3617 s
->input_statement
.flags
.loaded
= FALSE
;
3618 s
->input_statement
.flags
.reload
= TRUE
;
3621 os_tail
= lang_os_list
.tail
;
3622 lang_list_init (&add
);
3624 if (!load_symbols (&s
->input_statement
, &add
))
3625 config
.make_executable
= FALSE
;
3627 if (add
.head
!= NULL
)
3629 /* If this was a script with output sections then
3630 tack any added statements on to the end of the
3631 list. This avoids having to reorder the output
3632 section statement list. Very likely the user
3633 forgot -T, and whatever we do here will not meet
3634 naive user expectations. */
3635 if (os_tail
!= lang_os_list
.tail
)
3637 einfo (_("%P: warning: %s contains output sections;"
3638 " did you forget -T?\n"),
3639 s
->input_statement
.filename
);
3640 *stat_ptr
->tail
= add
.head
;
3641 stat_ptr
->tail
= add
.tail
;
3645 *add
.tail
= s
->header
.next
;
3646 s
->header
.next
= add
.head
;
3650 #ifdef ENABLE_PLUGINS
3651 /* If we have found the point at which a plugin added new
3652 files, clear plugin_insert to enable archive rescan. */
3653 if (&s
->input_statement
== plugin_insert
)
3654 plugin_insert
= NULL
;
3657 case lang_assignment_statement_enum
:
3658 if (s
->assignment_statement
.exp
->type
.node_class
!= etree_assert
)
3659 exp_fold_tree_no_dot (s
->assignment_statement
.exp
);
3666 /* Exit if any of the files were missing. */
3667 if (input_flags
.missing_file
)
3671 /* Open the CTF sections in the input files with libctf: if any were opened,
3672 create a fake input file that we'll write the merged CTF data to later
3676 ldlang_open_ctf (void)
3681 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3685 /* Incoming files from the compiler have a single ctf_file_t in them
3686 (which is presented to us by the libctf API in a ctf_archive_t
3687 wrapper): files derived from a previous relocatable link have a CTF
3688 archive containing possibly many CTF files. */
3690 if ((file
->the_ctf
= ctf_bfdopen (file
->the_bfd
, &err
)) == NULL
)
3692 if (err
!= ECTF_NOCTFDATA
)
3693 einfo (_("%P: warning: CTF section in `%pI' not loaded: "
3694 "its types will be discarded: `%s'\n"), file
,
3699 /* Prevent the contents of this section from being written, while
3700 requiring the section itself to be duplicated in the output. */
3701 /* This section must exist if ctf_bfdopen() succeeded. */
3702 sect
= bfd_get_section_by_name (file
->the_bfd
, ".ctf");
3704 sect
->flags
|= SEC_NEVER_LOAD
| SEC_HAS_CONTENTS
| SEC_LINKER_CREATED
;
3715 if ((ctf_output
= ctf_create (&err
)) != NULL
)
3718 einfo (_("%P: warning: CTF output not created: `%s'\n"),
3721 LANG_FOR_EACH_INPUT_STATEMENT (errfile
)
3722 ctf_close (errfile
->the_ctf
);
3725 /* Merge together CTF sections. After this, only the symtab-dependent
3726 function and data object sections need adjustment. */
3729 lang_merge_ctf (void)
3731 asection
*output_sect
;
3736 output_sect
= bfd_get_section_by_name (link_info
.output_bfd
, ".ctf");
3738 /* If the section was discarded, don't waste time merging. */
3739 if (output_sect
== NULL
)
3741 ctf_file_close (ctf_output
);
3744 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3746 ctf_close (file
->the_ctf
);
3747 file
->the_ctf
= NULL
;
3752 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3757 /* Takes ownership of file->u.the_ctfa. */
3758 if (ctf_link_add_ctf (ctf_output
, file
->the_ctf
, file
->filename
) < 0)
3760 einfo (_("%F%P: cannot link with CTF in %pB: %s\n"), file
->the_bfd
,
3761 ctf_errmsg (ctf_errno (ctf_output
)));
3762 ctf_close (file
->the_ctf
);
3763 file
->the_ctf
= NULL
;
3768 if (ctf_link (ctf_output
, CTF_LINK_SHARE_UNCONFLICTED
) < 0)
3770 einfo (_("%F%P: CTF linking failed; output will have no CTF section: %s\n"),
3771 ctf_errmsg (ctf_errno (ctf_output
)));
3774 output_sect
->size
= 0;
3775 output_sect
->flags
|= SEC_EXCLUDE
;
3780 /* Let the emulation examine the symbol table and strtab to help it optimize the
3781 CTF, if supported. */
3784 ldlang_ctf_apply_strsym (struct elf_sym_strtab
*syms
, bfd_size_type symcount
,
3785 struct elf_strtab_hash
*symstrtab
)
3787 ldemul_examine_strtab_for_ctf (ctf_output
, syms
, symcount
, symstrtab
);
3790 /* Write out the CTF section. Called early, if the emulation isn't going to
3791 need to dedup against the strtab and symtab, then possibly called from the
3792 target linker code if the dedup has happened. */
3794 lang_write_ctf (int late
)
3797 asection
*output_sect
;
3804 /* Emit CTF late if this emulation says it can do so. */
3805 if (ldemul_emit_ctf_early ())
3810 if (!ldemul_emit_ctf_early ())
3816 output_sect
= bfd_get_section_by_name (link_info
.output_bfd
, ".ctf");
3819 output_sect
->contents
= ctf_link_write (ctf_output
, &output_size
,
3820 CTF_COMPRESSION_THRESHOLD
);
3821 output_sect
->size
= output_size
;
3822 output_sect
->flags
|= SEC_IN_MEMORY
| SEC_KEEP
;
3824 if (!output_sect
->contents
)
3826 einfo (_("%F%P: CTF section emission failed; output will have no "
3827 "CTF section: %s\n"), ctf_errmsg (ctf_errno (ctf_output
)));
3828 output_sect
->size
= 0;
3829 output_sect
->flags
|= SEC_EXCLUDE
;
3833 /* This also closes every CTF input file used in the link. */
3834 ctf_file_close (ctf_output
);
3837 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3838 file
->the_ctf
= NULL
;
3841 /* Write out the CTF section late, if the emulation needs that. */
3844 ldlang_write_ctf_late (void)
3846 /* Trigger a "late call", if the emulation needs one. */
3851 /* Add the supplied name to the symbol table as an undefined reference.
3852 This is a two step process as the symbol table doesn't even exist at
3853 the time the ld command line is processed. First we put the name
3854 on a list, then, once the output file has been opened, transfer the
3855 name to the symbol table. */
3857 typedef struct bfd_sym_chain ldlang_undef_chain_list_type
;
3859 #define ldlang_undef_chain_list_head entry_symbol.next
3862 ldlang_add_undef (const char *const name
, bfd_boolean cmdline
)
3864 ldlang_undef_chain_list_type
*new_undef
;
3866 undef_from_cmdline
= undef_from_cmdline
|| cmdline
;
3867 new_undef
= stat_alloc (sizeof (*new_undef
));
3868 new_undef
->next
= ldlang_undef_chain_list_head
;
3869 ldlang_undef_chain_list_head
= new_undef
;
3871 new_undef
->name
= xstrdup (name
);
3873 if (link_info
.output_bfd
!= NULL
)
3874 insert_undefined (new_undef
->name
);
3877 /* Insert NAME as undefined in the symbol table. */
3880 insert_undefined (const char *name
)
3882 struct bfd_link_hash_entry
*h
;
3884 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, FALSE
, TRUE
);
3886 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
3887 if (h
->type
== bfd_link_hash_new
)
3889 h
->type
= bfd_link_hash_undefined
;
3890 h
->u
.undef
.abfd
= NULL
;
3891 h
->non_ir_ref_regular
= TRUE
;
3892 if (is_elf_hash_table (link_info
.hash
))
3893 ((struct elf_link_hash_entry
*) h
)->mark
= 1;
3894 bfd_link_add_undef (link_info
.hash
, h
);
3898 /* Run through the list of undefineds created above and place them
3899 into the linker hash table as undefined symbols belonging to the
3903 lang_place_undefineds (void)
3905 ldlang_undef_chain_list_type
*ptr
;
3907 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
3908 insert_undefined (ptr
->name
);
3911 /* Structure used to build the list of symbols that the user has required
3914 struct require_defined_symbol
3917 struct require_defined_symbol
*next
;
3920 /* The list of symbols that the user has required be defined. */
3922 static struct require_defined_symbol
*require_defined_symbol_list
;
3924 /* Add a new symbol NAME to the list of symbols that are required to be
3928 ldlang_add_require_defined (const char *const name
)
3930 struct require_defined_symbol
*ptr
;
3932 ldlang_add_undef (name
, TRUE
);
3933 ptr
= stat_alloc (sizeof (*ptr
));
3934 ptr
->next
= require_defined_symbol_list
;
3935 ptr
->name
= strdup (name
);
3936 require_defined_symbol_list
= ptr
;
3939 /* Check that all symbols the user required to be defined, are defined,
3940 raise an error if we find a symbol that is not defined. */
3943 ldlang_check_require_defined_symbols (void)
3945 struct require_defined_symbol
*ptr
;
3947 for (ptr
= require_defined_symbol_list
; ptr
!= NULL
; ptr
= ptr
->next
)
3949 struct bfd_link_hash_entry
*h
;
3951 h
= bfd_link_hash_lookup (link_info
.hash
, ptr
->name
,
3952 FALSE
, FALSE
, TRUE
);
3954 || (h
->type
!= bfd_link_hash_defined
3955 && h
->type
!= bfd_link_hash_defweak
))
3956 einfo(_("%X%P: required symbol `%s' not defined\n"), ptr
->name
);
3960 /* Check for all readonly or some readwrite sections. */
3963 check_input_sections
3964 (lang_statement_union_type
*s
,
3965 lang_output_section_statement_type
*output_section_statement
)
3967 for (; s
!= NULL
; s
= s
->header
.next
)
3969 switch (s
->header
.type
)
3971 case lang_wild_statement_enum
:
3972 walk_wild (&s
->wild_statement
, check_section_callback
,
3973 output_section_statement
);
3974 if (!output_section_statement
->all_input_readonly
)
3977 case lang_constructors_statement_enum
:
3978 check_input_sections (constructor_list
.head
,
3979 output_section_statement
);
3980 if (!output_section_statement
->all_input_readonly
)
3983 case lang_group_statement_enum
:
3984 check_input_sections (s
->group_statement
.children
.head
,
3985 output_section_statement
);
3986 if (!output_section_statement
->all_input_readonly
)
3995 /* Update wildcard statements if needed. */
3998 update_wild_statements (lang_statement_union_type
*s
)
4000 struct wildcard_list
*sec
;
4002 switch (sort_section
)
4012 for (; s
!= NULL
; s
= s
->header
.next
)
4014 switch (s
->header
.type
)
4019 case lang_wild_statement_enum
:
4020 for (sec
= s
->wild_statement
.section_list
; sec
!= NULL
;
4022 /* Don't sort .init/.fini sections. */
4023 if (strcmp (sec
->spec
.name
, ".init") != 0
4024 && strcmp (sec
->spec
.name
, ".fini") != 0)
4025 switch (sec
->spec
.sorted
)
4028 sec
->spec
.sorted
= sort_section
;
4031 if (sort_section
== by_alignment
)
4032 sec
->spec
.sorted
= by_name_alignment
;
4035 if (sort_section
== by_name
)
4036 sec
->spec
.sorted
= by_alignment_name
;
4043 case lang_constructors_statement_enum
:
4044 update_wild_statements (constructor_list
.head
);
4047 case lang_output_section_statement_enum
:
4048 update_wild_statements
4049 (s
->output_section_statement
.children
.head
);
4052 case lang_group_statement_enum
:
4053 update_wild_statements (s
->group_statement
.children
.head
);
4061 /* Open input files and attach to output sections. */
4064 map_input_to_output_sections
4065 (lang_statement_union_type
*s
, const char *target
,
4066 lang_output_section_statement_type
*os
)
4068 for (; s
!= NULL
; s
= s
->header
.next
)
4070 lang_output_section_statement_type
*tos
;
4073 switch (s
->header
.type
)
4075 case lang_wild_statement_enum
:
4076 wild (&s
->wild_statement
, target
, os
);
4078 case lang_constructors_statement_enum
:
4079 map_input_to_output_sections (constructor_list
.head
,
4083 case lang_output_section_statement_enum
:
4084 tos
= &s
->output_section_statement
;
4085 if (tos
->constraint
!= 0)
4087 if (tos
->constraint
!= ONLY_IF_RW
4088 && tos
->constraint
!= ONLY_IF_RO
)
4090 tos
->all_input_readonly
= TRUE
;
4091 check_input_sections (tos
->children
.head
, tos
);
4092 if (tos
->all_input_readonly
!= (tos
->constraint
== ONLY_IF_RO
))
4094 tos
->constraint
= -1;
4098 map_input_to_output_sections (tos
->children
.head
,
4102 case lang_output_statement_enum
:
4104 case lang_target_statement_enum
:
4105 target
= s
->target_statement
.target
;
4107 case lang_group_statement_enum
:
4108 map_input_to_output_sections (s
->group_statement
.children
.head
,
4112 case lang_data_statement_enum
:
4113 /* Make sure that any sections mentioned in the expression
4115 exp_init_os (s
->data_statement
.exp
);
4116 /* The output section gets CONTENTS, ALLOC and LOAD, but
4117 these may be overridden by the script. */
4118 flags
= SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
;
4119 switch (os
->sectype
)
4121 case normal_section
:
4122 case overlay_section
:
4123 case first_overlay_section
:
4125 case noalloc_section
:
4126 flags
= SEC_HAS_CONTENTS
;
4128 case noload_section
:
4129 if (bfd_get_flavour (link_info
.output_bfd
)
4130 == bfd_target_elf_flavour
)
4131 flags
= SEC_NEVER_LOAD
| SEC_ALLOC
;
4133 flags
= SEC_NEVER_LOAD
| SEC_HAS_CONTENTS
;
4136 if (os
->bfd_section
== NULL
)
4137 init_os (os
, flags
);
4139 os
->bfd_section
->flags
|= flags
;
4141 case lang_input_section_enum
:
4143 case lang_fill_statement_enum
:
4144 case lang_object_symbols_statement_enum
:
4145 case lang_reloc_statement_enum
:
4146 case lang_padding_statement_enum
:
4147 case lang_input_statement_enum
:
4148 if (os
!= NULL
&& os
->bfd_section
== NULL
)
4151 case lang_assignment_statement_enum
:
4152 if (os
!= NULL
&& os
->bfd_section
== NULL
)
4155 /* Make sure that any sections mentioned in the assignment
4157 exp_init_os (s
->assignment_statement
.exp
);
4159 case lang_address_statement_enum
:
4160 /* Mark the specified section with the supplied address.
4161 If this section was actually a segment marker, then the
4162 directive is ignored if the linker script explicitly
4163 processed the segment marker. Originally, the linker
4164 treated segment directives (like -Ttext on the
4165 command-line) as section directives. We honor the
4166 section directive semantics for backwards compatibility;
4167 linker scripts that do not specifically check for
4168 SEGMENT_START automatically get the old semantics. */
4169 if (!s
->address_statement
.segment
4170 || !s
->address_statement
.segment
->used
)
4172 const char *name
= s
->address_statement
.section_name
;
4174 /* Create the output section statement here so that
4175 orphans with a set address will be placed after other
4176 script sections. If we let the orphan placement code
4177 place them in amongst other sections then the address
4178 will affect following script sections, which is
4179 likely to surprise naive users. */
4180 tos
= lang_output_section_statement_lookup (name
, 0, TRUE
);
4181 tos
->addr_tree
= s
->address_statement
.address
;
4182 if (tos
->bfd_section
== NULL
)
4186 case lang_insert_statement_enum
:
4192 /* An insert statement snips out all the linker statements from the
4193 start of the list and places them after the output section
4194 statement specified by the insert. This operation is complicated
4195 by the fact that we keep a doubly linked list of output section
4196 statements as well as the singly linked list of all statements.
4197 FIXME someday: Twiddling with the list not only moves statements
4198 from the user's script but also input and group statements that are
4199 built from command line object files and --start-group. We only
4200 get away with this because the list pointers used by file_chain
4201 and input_file_chain are not reordered, and processing via
4202 statement_list after this point mostly ignores input statements.
4203 One exception is the map file, where LOAD and START GROUP/END GROUP
4204 can end up looking odd. */
4207 process_insert_statements (lang_statement_union_type
**start
)
4209 lang_statement_union_type
**s
;
4210 lang_output_section_statement_type
*first_os
= NULL
;
4211 lang_output_section_statement_type
*last_os
= NULL
;
4212 lang_output_section_statement_type
*os
;
4217 if ((*s
)->header
.type
== lang_output_section_statement_enum
)
4219 /* Keep pointers to the first and last output section
4220 statement in the sequence we may be about to move. */
4221 os
= &(*s
)->output_section_statement
;
4223 ASSERT (last_os
== NULL
|| last_os
->next
== os
);
4226 /* Set constraint negative so that lang_output_section_find
4227 won't match this output section statement. At this
4228 stage in linking constraint has values in the range
4229 [-1, ONLY_IN_RW]. */
4230 last_os
->constraint
= -2 - last_os
->constraint
;
4231 if (first_os
== NULL
)
4234 else if ((*s
)->header
.type
== lang_group_statement_enum
)
4236 /* A user might put -T between --start-group and
4237 --end-group. One way this odd construct might arise is
4238 from a wrapper around ld to change library search
4239 behaviour. For example:
4241 exec real_ld --start-group "$@" --end-group
4242 This isn't completely unreasonable so go looking inside a
4243 group statement for insert statements. */
4244 process_insert_statements (&(*s
)->group_statement
.children
.head
);
4246 else if ((*s
)->header
.type
== lang_insert_statement_enum
)
4248 lang_insert_statement_type
*i
= &(*s
)->insert_statement
;
4249 lang_output_section_statement_type
*where
;
4250 lang_statement_union_type
**ptr
;
4251 lang_statement_union_type
*first
;
4253 if (link_info
.non_contiguous_regions
)
4255 einfo (_("warning: INSERT statement in linker script is "
4256 "incompatible with --enable-non-contiguous-regions.\n"));
4259 where
= lang_output_section_find (i
->where
);
4260 if (where
!= NULL
&& i
->is_before
)
4263 where
= where
->prev
;
4264 while (where
!= NULL
&& where
->constraint
< 0);
4268 einfo (_("%F%P: %s not found for insert\n"), i
->where
);
4272 /* Deal with reordering the output section statement list. */
4273 if (last_os
!= NULL
)
4275 asection
*first_sec
, *last_sec
;
4276 struct lang_output_section_statement_struct
**next
;
4278 /* Snip out the output sections we are moving. */
4279 first_os
->prev
->next
= last_os
->next
;
4280 if (last_os
->next
== NULL
)
4282 next
= &first_os
->prev
->next
;
4283 lang_os_list
.tail
= (lang_statement_union_type
**) next
;
4286 last_os
->next
->prev
= first_os
->prev
;
4287 /* Add them in at the new position. */
4288 last_os
->next
= where
->next
;
4289 if (where
->next
== NULL
)
4291 next
= &last_os
->next
;
4292 lang_os_list
.tail
= (lang_statement_union_type
**) next
;
4295 where
->next
->prev
= last_os
;
4296 first_os
->prev
= where
;
4297 where
->next
= first_os
;
4299 /* Move the bfd sections in the same way. */
4302 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
4304 os
->constraint
= -2 - os
->constraint
;
4305 if (os
->bfd_section
!= NULL
4306 && os
->bfd_section
->owner
!= NULL
)
4308 last_sec
= os
->bfd_section
;
4309 if (first_sec
== NULL
)
4310 first_sec
= last_sec
;
4315 if (last_sec
!= NULL
)
4317 asection
*sec
= where
->bfd_section
;
4319 sec
= output_prev_sec_find (where
);
4321 /* The place we want to insert must come after the
4322 sections we are moving. So if we find no
4323 section or if the section is the same as our
4324 last section, then no move is needed. */
4325 if (sec
!= NULL
&& sec
!= last_sec
)
4327 /* Trim them off. */
4328 if (first_sec
->prev
!= NULL
)
4329 first_sec
->prev
->next
= last_sec
->next
;
4331 link_info
.output_bfd
->sections
= last_sec
->next
;
4332 if (last_sec
->next
!= NULL
)
4333 last_sec
->next
->prev
= first_sec
->prev
;
4335 link_info
.output_bfd
->section_last
= first_sec
->prev
;
4337 last_sec
->next
= sec
->next
;
4338 if (sec
->next
!= NULL
)
4339 sec
->next
->prev
= last_sec
;
4341 link_info
.output_bfd
->section_last
= last_sec
;
4342 first_sec
->prev
= sec
;
4343 sec
->next
= first_sec
;
4351 ptr
= insert_os_after (where
);
4352 /* Snip everything from the start of the list, up to and
4353 including the insert statement we are currently processing. */
4355 *start
= (*s
)->header
.next
;
4356 /* Add them back where they belong, minus the insert. */
4359 statement_list
.tail
= s
;
4364 s
= &(*s
)->header
.next
;
4367 /* Undo constraint twiddling. */
4368 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
4370 os
->constraint
= -2 - os
->constraint
;
4376 /* An output section might have been removed after its statement was
4377 added. For example, ldemul_before_allocation can remove dynamic
4378 sections if they turn out to be not needed. Clean them up here. */
4381 strip_excluded_output_sections (void)
4383 lang_output_section_statement_type
*os
;
4385 /* Run lang_size_sections (if not already done). */
4386 if (expld
.phase
!= lang_mark_phase_enum
)
4388 expld
.phase
= lang_mark_phase_enum
;
4389 expld
.dataseg
.phase
= exp_seg_none
;
4390 one_lang_size_sections_pass (NULL
, FALSE
);
4391 lang_reset_memory_regions ();
4394 for (os
= (void *) lang_os_list
.head
;
4398 asection
*output_section
;
4399 bfd_boolean exclude
;
4401 if (os
->constraint
< 0)
4404 output_section
= os
->bfd_section
;
4405 if (output_section
== NULL
)
4408 exclude
= (output_section
->rawsize
== 0
4409 && (output_section
->flags
& SEC_KEEP
) == 0
4410 && !bfd_section_removed_from_list (link_info
.output_bfd
,
4413 /* Some sections have not yet been sized, notably .gnu.version,
4414 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
4415 input sections, so don't drop output sections that have such
4416 input sections unless they are also marked SEC_EXCLUDE. */
4417 if (exclude
&& output_section
->map_head
.s
!= NULL
)
4421 for (s
= output_section
->map_head
.s
; s
!= NULL
; s
= s
->map_head
.s
)
4422 if ((s
->flags
& SEC_EXCLUDE
) == 0
4423 && ((s
->flags
& SEC_LINKER_CREATED
) != 0
4424 || link_info
.emitrelocations
))
4433 /* We don't set bfd_section to NULL since bfd_section of the
4434 removed output section statement may still be used. */
4435 if (!os
->update_dot
)
4437 output_section
->flags
|= SEC_EXCLUDE
;
4438 bfd_section_list_remove (link_info
.output_bfd
, output_section
);
4439 link_info
.output_bfd
->section_count
--;
4444 /* Called from ldwrite to clear out asection.map_head and
4445 asection.map_tail for use as link_orders in ldwrite. */
4448 lang_clear_os_map (void)
4450 lang_output_section_statement_type
*os
;
4452 if (map_head_is_link_order
)
4455 for (os
= (void *) lang_os_list
.head
;
4459 asection
*output_section
;
4461 if (os
->constraint
< 0)
4464 output_section
= os
->bfd_section
;
4465 if (output_section
== NULL
)
4468 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
4469 output_section
->map_head
.link_order
= NULL
;
4470 output_section
->map_tail
.link_order
= NULL
;
4473 /* Stop future calls to lang_add_section from messing with map_head
4474 and map_tail link_order fields. */
4475 map_head_is_link_order
= TRUE
;
4479 print_output_section_statement
4480 (lang_output_section_statement_type
*output_section_statement
)
4482 asection
*section
= output_section_statement
->bfd_section
;
4485 if (output_section_statement
!= abs_output_section
)
4487 minfo ("\n%s", output_section_statement
->name
);
4489 if (section
!= NULL
)
4491 print_dot
= section
->vma
;
4493 len
= strlen (output_section_statement
->name
);
4494 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
4499 while (len
< SECTION_NAME_MAP_LENGTH
)
4505 minfo ("0x%V %W", section
->vma
, TO_ADDR (section
->size
));
4507 if (section
->vma
!= section
->lma
)
4508 minfo (_(" load address 0x%V"), section
->lma
);
4510 if (output_section_statement
->update_dot_tree
!= NULL
)
4511 exp_fold_tree (output_section_statement
->update_dot_tree
,
4512 bfd_abs_section_ptr
, &print_dot
);
4518 print_statement_list (output_section_statement
->children
.head
,
4519 output_section_statement
);
4523 print_assignment (lang_assignment_statement_type
*assignment
,
4524 lang_output_section_statement_type
*output_section
)
4531 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
4534 if (assignment
->exp
->type
.node_class
== etree_assert
)
4537 tree
= assignment
->exp
->assert_s
.child
;
4541 const char *dst
= assignment
->exp
->assign
.dst
;
4543 is_dot
= (dst
[0] == '.' && dst
[1] == 0);
4544 tree
= assignment
->exp
;
4547 osec
= output_section
->bfd_section
;
4549 osec
= bfd_abs_section_ptr
;
4551 if (assignment
->exp
->type
.node_class
!= etree_provide
)
4552 exp_fold_tree (tree
, osec
, &print_dot
);
4554 expld
.result
.valid_p
= FALSE
;
4556 if (expld
.result
.valid_p
)
4560 if (assignment
->exp
->type
.node_class
== etree_assert
4562 || expld
.assign_name
!= NULL
)
4564 value
= expld
.result
.value
;
4566 if (expld
.result
.section
!= NULL
)
4567 value
+= expld
.result
.section
->vma
;
4569 minfo ("0x%V", value
);
4575 struct bfd_link_hash_entry
*h
;
4577 h
= bfd_link_hash_lookup (link_info
.hash
, assignment
->exp
->assign
.dst
,
4578 FALSE
, FALSE
, TRUE
);
4580 && (h
->type
== bfd_link_hash_defined
4581 || h
->type
== bfd_link_hash_defweak
))
4583 value
= h
->u
.def
.value
;
4584 value
+= h
->u
.def
.section
->output_section
->vma
;
4585 value
+= h
->u
.def
.section
->output_offset
;
4587 minfo ("[0x%V]", value
);
4590 minfo ("[unresolved]");
4595 if (assignment
->exp
->type
.node_class
== etree_provide
)
4596 minfo ("[!provide]");
4603 expld
.assign_name
= NULL
;
4606 exp_print_tree (assignment
->exp
);
4611 print_input_statement (lang_input_statement_type
*statm
)
4613 if (statm
->filename
!= NULL
)
4614 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
4617 /* Print all symbols defined in a particular section. This is called
4618 via bfd_link_hash_traverse, or by print_all_symbols. */
4621 print_one_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
)
4623 asection
*sec
= (asection
*) ptr
;
4625 if ((hash_entry
->type
== bfd_link_hash_defined
4626 || hash_entry
->type
== bfd_link_hash_defweak
)
4627 && sec
== hash_entry
->u
.def
.section
)
4631 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
4634 (hash_entry
->u
.def
.value
4635 + hash_entry
->u
.def
.section
->output_offset
4636 + hash_entry
->u
.def
.section
->output_section
->vma
));
4638 minfo (" %pT\n", hash_entry
->root
.string
);
4645 hash_entry_addr_cmp (const void *a
, const void *b
)
4647 const struct bfd_link_hash_entry
*l
= *(const struct bfd_link_hash_entry
**)a
;
4648 const struct bfd_link_hash_entry
*r
= *(const struct bfd_link_hash_entry
**)b
;
4650 if (l
->u
.def
.value
< r
->u
.def
.value
)
4652 else if (l
->u
.def
.value
> r
->u
.def
.value
)
4659 print_all_symbols (asection
*sec
)
4661 input_section_userdata_type
*ud
= bfd_section_userdata (sec
);
4662 struct map_symbol_def
*def
;
4663 struct bfd_link_hash_entry
**entries
;
4669 *ud
->map_symbol_def_tail
= 0;
4671 /* Sort the symbols by address. */
4672 entries
= (struct bfd_link_hash_entry
**)
4673 obstack_alloc (&map_obstack
,
4674 ud
->map_symbol_def_count
* sizeof (*entries
));
4676 for (i
= 0, def
= ud
->map_symbol_def_head
; def
; def
= def
->next
, i
++)
4677 entries
[i
] = def
->entry
;
4679 qsort (entries
, ud
->map_symbol_def_count
, sizeof (*entries
),
4680 hash_entry_addr_cmp
);
4682 /* Print the symbols. */
4683 for (i
= 0; i
< ud
->map_symbol_def_count
; i
++)
4684 ldemul_print_symbol (entries
[i
], sec
);
4686 obstack_free (&map_obstack
, entries
);
4689 /* Print information about an input section to the map file. */
4692 print_input_section (asection
*i
, bfd_boolean is_discarded
)
4694 bfd_size_type size
= i
->size
;
4701 minfo ("%s", i
->name
);
4703 len
= 1 + strlen (i
->name
);
4704 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
4709 while (len
< SECTION_NAME_MAP_LENGTH
)
4715 if (i
->output_section
!= NULL
4716 && i
->output_section
->owner
== link_info
.output_bfd
)
4717 addr
= i
->output_section
->vma
+ i
->output_offset
;
4725 minfo ("0x%V %W %pB\n", addr
, TO_ADDR (size
), i
->owner
);
4727 if (size
!= i
->rawsize
&& i
->rawsize
!= 0)
4729 len
= SECTION_NAME_MAP_LENGTH
+ 3;
4741 minfo (_("%W (size before relaxing)\n"), TO_ADDR (i
->rawsize
));
4744 if (i
->output_section
!= NULL
4745 && i
->output_section
->owner
== link_info
.output_bfd
)
4747 if (link_info
.reduce_memory_overheads
)
4748 bfd_link_hash_traverse (link_info
.hash
, ldemul_print_symbol
, i
);
4750 print_all_symbols (i
);
4752 /* Update print_dot, but make sure that we do not move it
4753 backwards - this could happen if we have overlays and a
4754 later overlay is shorter than an earier one. */
4755 if (addr
+ TO_ADDR (size
) > print_dot
)
4756 print_dot
= addr
+ TO_ADDR (size
);
4761 print_fill_statement (lang_fill_statement_type
*fill
)
4765 fputs (" FILL mask 0x", config
.map_file
);
4766 for (p
= fill
->fill
->data
, size
= fill
->fill
->size
; size
!= 0; p
++, size
--)
4767 fprintf (config
.map_file
, "%02x", *p
);
4768 fputs ("\n", config
.map_file
);
4772 print_data_statement (lang_data_statement_type
*data
)
4779 init_opb (data
->output_section
);
4780 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
4783 addr
= data
->output_offset
;
4784 if (data
->output_section
!= NULL
)
4785 addr
+= data
->output_section
->vma
;
4813 if (size
< TO_SIZE ((unsigned) 1))
4814 size
= TO_SIZE ((unsigned) 1);
4815 minfo ("0x%V %W %s 0x%v", addr
, TO_ADDR (size
), name
, data
->value
);
4817 if (data
->exp
->type
.node_class
!= etree_value
)
4820 exp_print_tree (data
->exp
);
4825 print_dot
= addr
+ TO_ADDR (size
);
4828 /* Print an address statement. These are generated by options like
4832 print_address_statement (lang_address_statement_type
*address
)
4834 minfo (_("Address of section %s set to "), address
->section_name
);
4835 exp_print_tree (address
->address
);
4839 /* Print a reloc statement. */
4842 print_reloc_statement (lang_reloc_statement_type
*reloc
)
4848 init_opb (reloc
->output_section
);
4849 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
4852 addr
= reloc
->output_offset
;
4853 if (reloc
->output_section
!= NULL
)
4854 addr
+= reloc
->output_section
->vma
;
4856 size
= bfd_get_reloc_size (reloc
->howto
);
4858 minfo ("0x%V %W RELOC %s ", addr
, TO_ADDR (size
), reloc
->howto
->name
);
4860 if (reloc
->name
!= NULL
)
4861 minfo ("%s+", reloc
->name
);
4863 minfo ("%s+", reloc
->section
->name
);
4865 exp_print_tree (reloc
->addend_exp
);
4869 print_dot
= addr
+ TO_ADDR (size
);
4873 print_padding_statement (lang_padding_statement_type
*s
)
4878 init_opb (s
->output_section
);
4881 len
= sizeof " *fill*" - 1;
4882 while (len
< SECTION_NAME_MAP_LENGTH
)
4888 addr
= s
->output_offset
;
4889 if (s
->output_section
!= NULL
)
4890 addr
+= s
->output_section
->vma
;
4891 minfo ("0x%V %W ", addr
, TO_ADDR (s
->size
));
4893 if (s
->fill
->size
!= 0)
4897 for (p
= s
->fill
->data
, size
= s
->fill
->size
; size
!= 0; p
++, size
--)
4898 fprintf (config
.map_file
, "%02x", *p
);
4903 print_dot
= addr
+ TO_ADDR (s
->size
);
4907 print_wild_statement (lang_wild_statement_type
*w
,
4908 lang_output_section_statement_type
*os
)
4910 struct wildcard_list
*sec
;
4914 if (w
->exclude_name_list
)
4917 minfo ("EXCLUDE_FILE(%s", w
->exclude_name_list
->name
);
4918 for (tmp
= w
->exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
4919 minfo (" %s", tmp
->name
);
4923 if (w
->filenames_sorted
)
4924 minfo ("SORT_BY_NAME(");
4925 if (w
->filename
!= NULL
)
4926 minfo ("%s", w
->filename
);
4929 if (w
->filenames_sorted
)
4933 for (sec
= w
->section_list
; sec
; sec
= sec
->next
)
4935 int closing_paren
= 0;
4937 switch (sec
->spec
.sorted
)
4943 minfo ("SORT_BY_NAME(");
4948 minfo ("SORT_BY_ALIGNMENT(");
4952 case by_name_alignment
:
4953 minfo ("SORT_BY_NAME(SORT_BY_ALIGNMENT(");
4957 case by_alignment_name
:
4958 minfo ("SORT_BY_ALIGNMENT(SORT_BY_NAME(");
4963 minfo ("SORT_NONE(");
4967 case by_init_priority
:
4968 minfo ("SORT_BY_INIT_PRIORITY(");
4973 if (sec
->spec
.exclude_name_list
!= NULL
)
4976 minfo ("EXCLUDE_FILE(%s", sec
->spec
.exclude_name_list
->name
);
4977 for (tmp
= sec
->spec
.exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
4978 minfo (" %s", tmp
->name
);
4981 if (sec
->spec
.name
!= NULL
)
4982 minfo ("%s", sec
->spec
.name
);
4985 for (;closing_paren
> 0; closing_paren
--)
4994 print_statement_list (w
->children
.head
, os
);
4997 /* Print a group statement. */
5000 print_group (lang_group_statement_type
*s
,
5001 lang_output_section_statement_type
*os
)
5003 fprintf (config
.map_file
, "START GROUP\n");
5004 print_statement_list (s
->children
.head
, os
);
5005 fprintf (config
.map_file
, "END GROUP\n");
5008 /* Print the list of statements in S.
5009 This can be called for any statement type. */
5012 print_statement_list (lang_statement_union_type
*s
,
5013 lang_output_section_statement_type
*os
)
5017 print_statement (s
, os
);
5022 /* Print the first statement in statement list S.
5023 This can be called for any statement type. */
5026 print_statement (lang_statement_union_type
*s
,
5027 lang_output_section_statement_type
*os
)
5029 switch (s
->header
.type
)
5032 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
5035 case lang_constructors_statement_enum
:
5036 if (constructor_list
.head
!= NULL
)
5038 if (constructors_sorted
)
5039 minfo (" SORT (CONSTRUCTORS)\n");
5041 minfo (" CONSTRUCTORS\n");
5042 print_statement_list (constructor_list
.head
, os
);
5045 case lang_wild_statement_enum
:
5046 print_wild_statement (&s
->wild_statement
, os
);
5048 case lang_address_statement_enum
:
5049 print_address_statement (&s
->address_statement
);
5051 case lang_object_symbols_statement_enum
:
5052 minfo (" CREATE_OBJECT_SYMBOLS\n");
5054 case lang_fill_statement_enum
:
5055 print_fill_statement (&s
->fill_statement
);
5057 case lang_data_statement_enum
:
5058 print_data_statement (&s
->data_statement
);
5060 case lang_reloc_statement_enum
:
5061 print_reloc_statement (&s
->reloc_statement
);
5063 case lang_input_section_enum
:
5064 print_input_section (s
->input_section
.section
, FALSE
);
5066 case lang_padding_statement_enum
:
5067 print_padding_statement (&s
->padding_statement
);
5069 case lang_output_section_statement_enum
:
5070 print_output_section_statement (&s
->output_section_statement
);
5072 case lang_assignment_statement_enum
:
5073 print_assignment (&s
->assignment_statement
, os
);
5075 case lang_target_statement_enum
:
5076 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
5078 case lang_output_statement_enum
:
5079 minfo ("OUTPUT(%s", s
->output_statement
.name
);
5080 if (output_target
!= NULL
)
5081 minfo (" %s", output_target
);
5084 case lang_input_statement_enum
:
5085 print_input_statement (&s
->input_statement
);
5087 case lang_group_statement_enum
:
5088 print_group (&s
->group_statement
, os
);
5090 case lang_insert_statement_enum
:
5091 minfo ("INSERT %s %s\n",
5092 s
->insert_statement
.is_before
? "BEFORE" : "AFTER",
5093 s
->insert_statement
.where
);
5099 print_statements (void)
5101 print_statement_list (statement_list
.head
, abs_output_section
);
5104 /* Print the first N statements in statement list S to STDERR.
5105 If N == 0, nothing is printed.
5106 If N < 0, the entire list is printed.
5107 Intended to be called from GDB. */
5110 dprint_statement (lang_statement_union_type
*s
, int n
)
5112 FILE *map_save
= config
.map_file
;
5114 config
.map_file
= stderr
;
5117 print_statement_list (s
, abs_output_section
);
5120 while (s
&& --n
>= 0)
5122 print_statement (s
, abs_output_section
);
5127 config
.map_file
= map_save
;
5131 insert_pad (lang_statement_union_type
**ptr
,
5133 bfd_size_type alignment_needed
,
5134 asection
*output_section
,
5137 static fill_type zero_fill
;
5138 lang_statement_union_type
*pad
= NULL
;
5140 if (ptr
!= &statement_list
.head
)
5141 pad
= ((lang_statement_union_type
*)
5142 ((char *) ptr
- offsetof (lang_statement_union_type
, header
.next
)));
5144 && pad
->header
.type
== lang_padding_statement_enum
5145 && pad
->padding_statement
.output_section
== output_section
)
5147 /* Use the existing pad statement. */
5149 else if ((pad
= *ptr
) != NULL
5150 && pad
->header
.type
== lang_padding_statement_enum
5151 && pad
->padding_statement
.output_section
== output_section
)
5153 /* Use the existing pad statement. */
5157 /* Make a new padding statement, linked into existing chain. */
5158 pad
= stat_alloc (sizeof (lang_padding_statement_type
));
5159 pad
->header
.next
= *ptr
;
5161 pad
->header
.type
= lang_padding_statement_enum
;
5162 pad
->padding_statement
.output_section
= output_section
;
5165 pad
->padding_statement
.fill
= fill
;
5167 pad
->padding_statement
.output_offset
= dot
- output_section
->vma
;
5168 pad
->padding_statement
.size
= alignment_needed
;
5169 if (!(output_section
->flags
& SEC_FIXED_SIZE
))
5170 output_section
->size
= TO_SIZE (dot
+ TO_ADDR (alignment_needed
)
5171 - output_section
->vma
);
5174 /* Work out how much this section will move the dot point. */
5178 (lang_statement_union_type
**this_ptr
,
5179 lang_output_section_statement_type
*output_section_statement
,
5181 bfd_boolean
*removed
,
5184 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
5185 asection
*i
= is
->section
;
5186 asection
*o
= output_section_statement
->bfd_section
;
5189 if (link_info
.non_contiguous_regions
)
5191 /* If the input section I has already been successfully assigned
5192 to an output section other than O, don't bother with it and
5193 let the caller remove it from the list. Keep processing in
5194 case we have already handled O, because the repeated passes
5195 have reinitialized its size. */
5196 if (i
->already_assigned
&& i
->already_assigned
!= o
)
5203 if (i
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
5204 i
->output_offset
= i
->vma
- o
->vma
;
5205 else if (((i
->flags
& SEC_EXCLUDE
) != 0)
5206 || output_section_statement
->ignored
)
5207 i
->output_offset
= dot
- o
->vma
;
5210 bfd_size_type alignment_needed
;
5212 /* Align this section first to the input sections requirement,
5213 then to the output section's requirement. If this alignment
5214 is greater than any seen before, then record it too. Perform
5215 the alignment by inserting a magic 'padding' statement. */
5217 if (output_section_statement
->subsection_alignment
!= NULL
)
5219 = exp_get_power (output_section_statement
->subsection_alignment
,
5220 "subsection alignment");
5222 if (o
->alignment_power
< i
->alignment_power
)
5223 o
->alignment_power
= i
->alignment_power
;
5225 alignment_needed
= align_power (dot
, i
->alignment_power
) - dot
;
5227 if (alignment_needed
!= 0)
5229 insert_pad (this_ptr
, fill
, TO_SIZE (alignment_needed
), o
, dot
);
5230 dot
+= alignment_needed
;
5233 if (link_info
.non_contiguous_regions
)
5235 /* If I would overflow O, let the caller remove I from the
5237 if (output_section_statement
->region
)
5239 bfd_vma end
= output_section_statement
->region
->origin
5240 + output_section_statement
->region
->length
;
5242 if (dot
+ TO_ADDR (i
->size
) > end
)
5244 if (i
->flags
& SEC_LINKER_CREATED
)
5245 einfo (_("%F%P: Output section '%s' not large enough for the "
5246 "linker-created stubs section '%s'.\n"),
5247 i
->output_section
->name
, i
->name
);
5249 if (i
->rawsize
&& i
->rawsize
!= i
->size
)
5250 einfo (_("%F%P: Relaxation not supported with "
5251 "--enable-non-contiguous-regions (section '%s' "
5252 "would overflow '%s' after it changed size).\n"),
5253 i
->name
, i
->output_section
->name
);
5257 i
->output_section
= NULL
;
5263 /* Remember where in the output section this input section goes. */
5264 i
->output_offset
= dot
- o
->vma
;
5266 /* Mark how big the output section must be to contain this now. */
5267 dot
+= TO_ADDR (i
->size
);
5268 if (!(o
->flags
& SEC_FIXED_SIZE
))
5269 o
->size
= TO_SIZE (dot
- o
->vma
);
5271 if (link_info
.non_contiguous_regions
)
5273 /* Record that I was successfully assigned to O, and update
5274 its actual output section too. */
5275 i
->already_assigned
= o
;
5276 i
->output_section
= o
;
5290 sort_sections_by_lma (const void *arg1
, const void *arg2
)
5292 const asection
*sec1
= ((const struct check_sec
*) arg1
)->sec
;
5293 const asection
*sec2
= ((const struct check_sec
*) arg2
)->sec
;
5295 if (sec1
->lma
< sec2
->lma
)
5297 else if (sec1
->lma
> sec2
->lma
)
5299 else if (sec1
->id
< sec2
->id
)
5301 else if (sec1
->id
> sec2
->id
)
5308 sort_sections_by_vma (const void *arg1
, const void *arg2
)
5310 const asection
*sec1
= ((const struct check_sec
*) arg1
)->sec
;
5311 const asection
*sec2
= ((const struct check_sec
*) arg2
)->sec
;
5313 if (sec1
->vma
< sec2
->vma
)
5315 else if (sec1
->vma
> sec2
->vma
)
5317 else if (sec1
->id
< sec2
->id
)
5319 else if (sec1
->id
> sec2
->id
)
5325 #define IS_TBSS(s) \
5326 ((s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == SEC_THREAD_LOCAL)
5328 #define IGNORE_SECTION(s) \
5329 ((s->flags & SEC_ALLOC) == 0 || IS_TBSS (s))
5331 /* Check to see if any allocated sections overlap with other allocated
5332 sections. This can happen if a linker script specifies the output
5333 section addresses of the two sections. Also check whether any memory
5334 region has overflowed. */
5337 lang_check_section_addresses (void)
5340 struct check_sec
*sections
;
5345 bfd_vma p_start
= 0;
5347 lang_memory_region_type
*m
;
5348 bfd_boolean overlays
;
5350 /* Detect address space overflow on allocated sections. */
5351 addr_mask
= ((bfd_vma
) 1 <<
5352 (bfd_arch_bits_per_address (link_info
.output_bfd
) - 1)) - 1;
5353 addr_mask
= (addr_mask
<< 1) + 1;
5354 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5355 if ((s
->flags
& SEC_ALLOC
) != 0)
5357 s_end
= (s
->vma
+ s
->size
) & addr_mask
;
5358 if (s_end
!= 0 && s_end
< (s
->vma
& addr_mask
))
5359 einfo (_("%X%P: section %s VMA wraps around address space\n"),
5363 s_end
= (s
->lma
+ s
->size
) & addr_mask
;
5364 if (s_end
!= 0 && s_end
< (s
->lma
& addr_mask
))
5365 einfo (_("%X%P: section %s LMA wraps around address space\n"),
5370 if (bfd_count_sections (link_info
.output_bfd
) <= 1)
5373 count
= bfd_count_sections (link_info
.output_bfd
);
5374 sections
= XNEWVEC (struct check_sec
, count
);
5376 /* Scan all sections in the output list. */
5378 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5380 if (IGNORE_SECTION (s
)
5384 sections
[count
].sec
= s
;
5385 sections
[count
].warned
= FALSE
;
5395 qsort (sections
, count
, sizeof (*sections
), sort_sections_by_lma
);
5397 /* First check section LMAs. There should be no overlap of LMAs on
5398 loadable sections, even with overlays. */
5399 for (p
= NULL
, i
= 0; i
< count
; i
++)
5401 s
= sections
[i
].sec
;
5403 if ((s
->flags
& SEC_LOAD
) != 0)
5406 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
5408 /* Look for an overlap. We have sorted sections by lma, so
5409 we know that s_start >= p_start. Besides the obvious
5410 case of overlap when the current section starts before
5411 the previous one ends, we also must have overlap if the
5412 previous section wraps around the address space. */
5414 && (s_start
<= p_end
5415 || p_end
< p_start
))
5417 einfo (_("%X%P: section %s LMA [%V,%V]"
5418 " overlaps section %s LMA [%V,%V]\n"),
5419 s
->name
, s_start
, s_end
, p
->name
, p_start
, p_end
);
5420 sections
[i
].warned
= TRUE
;
5428 /* If any non-zero size allocated section (excluding tbss) starts at
5429 exactly the same VMA as another such section, then we have
5430 overlays. Overlays generated by the OVERLAY keyword will have
5431 this property. It is possible to intentionally generate overlays
5432 that fail this test, but it would be unusual. */
5433 qsort (sections
, count
, sizeof (*sections
), sort_sections_by_vma
);
5435 p_start
= sections
[0].sec
->vma
;
5436 for (i
= 1; i
< count
; i
++)
5438 s_start
= sections
[i
].sec
->vma
;
5439 if (p_start
== s_start
)
5447 /* Now check section VMAs if no overlays were detected. */
5450 for (p
= NULL
, i
= 0; i
< count
; i
++)
5452 s
= sections
[i
].sec
;
5455 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
5458 && !sections
[i
].warned
5459 && (s_start
<= p_end
5460 || p_end
< p_start
))
5461 einfo (_("%X%P: section %s VMA [%V,%V]"
5462 " overlaps section %s VMA [%V,%V]\n"),
5463 s
->name
, s_start
, s_end
, p
->name
, p_start
, p_end
);
5472 /* If any memory region has overflowed, report by how much.
5473 We do not issue this diagnostic for regions that had sections
5474 explicitly placed outside their bounds; os_region_check's
5475 diagnostics are adequate for that case.
5477 FIXME: It is conceivable that m->current - (m->origin + m->length)
5478 might overflow a 32-bit integer. There is, alas, no way to print
5479 a bfd_vma quantity in decimal. */
5480 for (m
= lang_memory_region_list
; m
; m
= m
->next
)
5481 if (m
->had_full_message
)
5483 unsigned long over
= m
->current
- (m
->origin
+ m
->length
);
5484 einfo (ngettext ("%X%P: region `%s' overflowed by %lu byte\n",
5485 "%X%P: region `%s' overflowed by %lu bytes\n",
5487 m
->name_list
.name
, over
);
5491 /* Make sure the new address is within the region. We explicitly permit the
5492 current address to be at the exact end of the region when the address is
5493 non-zero, in case the region is at the end of addressable memory and the
5494 calculation wraps around. */
5497 os_region_check (lang_output_section_statement_type
*os
,
5498 lang_memory_region_type
*region
,
5502 if ((region
->current
< region
->origin
5503 || (region
->current
- region
->origin
> region
->length
))
5504 && ((region
->current
!= region
->origin
+ region
->length
)
5509 einfo (_("%X%P: address 0x%v of %pB section `%s'"
5510 " is not within region `%s'\n"),
5512 os
->bfd_section
->owner
,
5513 os
->bfd_section
->name
,
5514 region
->name_list
.name
);
5516 else if (!region
->had_full_message
)
5518 region
->had_full_message
= TRUE
;
5520 einfo (_("%X%P: %pB section `%s' will not fit in region `%s'\n"),
5521 os
->bfd_section
->owner
,
5522 os
->bfd_section
->name
,
5523 region
->name_list
.name
);
5529 ldlang_check_relro_region (lang_statement_union_type
*s
,
5530 seg_align_type
*seg
)
5532 if (seg
->relro
== exp_seg_relro_start
)
5534 if (!seg
->relro_start_stat
)
5535 seg
->relro_start_stat
= s
;
5538 ASSERT (seg
->relro_start_stat
== s
);
5541 else if (seg
->relro
== exp_seg_relro_end
)
5543 if (!seg
->relro_end_stat
)
5544 seg
->relro_end_stat
= s
;
5547 ASSERT (seg
->relro_end_stat
== s
);
5552 /* Set the sizes for all the output sections. */
5555 lang_size_sections_1
5556 (lang_statement_union_type
**prev
,
5557 lang_output_section_statement_type
*output_section_statement
,
5561 bfd_boolean check_regions
)
5563 lang_statement_union_type
*s
;
5564 lang_statement_union_type
*prev_s
= NULL
;
5565 bfd_boolean removed_prev_s
= FALSE
;
5567 /* Size up the sections from their constituent parts. */
5568 for (s
= *prev
; s
!= NULL
; prev_s
= s
, s
= s
->header
.next
)
5570 bfd_boolean removed
=FALSE
;
5572 switch (s
->header
.type
)
5574 case lang_output_section_statement_enum
:
5576 bfd_vma newdot
, after
, dotdelta
;
5577 lang_output_section_statement_type
*os
;
5578 lang_memory_region_type
*r
;
5579 int section_alignment
= 0;
5581 os
= &s
->output_section_statement
;
5582 init_opb (os
->bfd_section
);
5583 if (os
->constraint
== -1)
5586 /* FIXME: We shouldn't need to zero section vmas for ld -r
5587 here, in lang_insert_orphan, or in the default linker scripts.
5588 This is covering for coff backend linker bugs. See PR6945. */
5589 if (os
->addr_tree
== NULL
5590 && bfd_link_relocatable (&link_info
)
5591 && (bfd_get_flavour (link_info
.output_bfd
)
5592 == bfd_target_coff_flavour
))
5593 os
->addr_tree
= exp_intop (0);
5594 if (os
->addr_tree
!= NULL
)
5596 os
->processed_vma
= FALSE
;
5597 exp_fold_tree (os
->addr_tree
, bfd_abs_section_ptr
, &dot
);
5599 if (expld
.result
.valid_p
)
5601 dot
= expld
.result
.value
;
5602 if (expld
.result
.section
!= NULL
)
5603 dot
+= expld
.result
.section
->vma
;
5605 else if (expld
.phase
!= lang_mark_phase_enum
)
5606 einfo (_("%F%P:%pS: non constant or forward reference"
5607 " address expression for section %s\n"),
5608 os
->addr_tree
, os
->name
);
5611 if (os
->bfd_section
== NULL
)
5612 /* This section was removed or never actually created. */
5615 /* If this is a COFF shared library section, use the size and
5616 address from the input section. FIXME: This is COFF
5617 specific; it would be cleaner if there were some other way
5618 to do this, but nothing simple comes to mind. */
5619 if (((bfd_get_flavour (link_info
.output_bfd
)
5620 == bfd_target_ecoff_flavour
)
5621 || (bfd_get_flavour (link_info
.output_bfd
)
5622 == bfd_target_coff_flavour
))
5623 && (os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
5627 if (os
->children
.head
== NULL
5628 || os
->children
.head
->header
.next
!= NULL
5629 || (os
->children
.head
->header
.type
5630 != lang_input_section_enum
))
5631 einfo (_("%X%P: internal error on COFF shared library"
5632 " section %s\n"), os
->name
);
5634 input
= os
->children
.head
->input_section
.section
;
5635 bfd_set_section_vma (os
->bfd_section
,
5636 bfd_section_vma (input
));
5637 if (!(os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
5638 os
->bfd_section
->size
= input
->size
;
5644 if (bfd_is_abs_section (os
->bfd_section
))
5646 /* No matter what happens, an abs section starts at zero. */
5647 ASSERT (os
->bfd_section
->vma
== 0);
5651 if (os
->addr_tree
== NULL
)
5653 /* No address specified for this section, get one
5654 from the region specification. */
5655 if (os
->region
== NULL
5656 || ((os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))
5657 && os
->region
->name_list
.name
[0] == '*'
5658 && strcmp (os
->region
->name_list
.name
,
5659 DEFAULT_MEMORY_REGION
) == 0))
5661 os
->region
= lang_memory_default (os
->bfd_section
);
5664 /* If a loadable section is using the default memory
5665 region, and some non default memory regions were
5666 defined, issue an error message. */
5668 && !IGNORE_SECTION (os
->bfd_section
)
5669 && !bfd_link_relocatable (&link_info
)
5671 && strcmp (os
->region
->name_list
.name
,
5672 DEFAULT_MEMORY_REGION
) == 0
5673 && lang_memory_region_list
!= NULL
5674 && (strcmp (lang_memory_region_list
->name_list
.name
,
5675 DEFAULT_MEMORY_REGION
) != 0
5676 || lang_memory_region_list
->next
!= NULL
)
5677 && lang_sizing_iteration
== 1)
5679 /* By default this is an error rather than just a
5680 warning because if we allocate the section to the
5681 default memory region we can end up creating an
5682 excessively large binary, or even seg faulting when
5683 attempting to perform a negative seek. See
5684 sources.redhat.com/ml/binutils/2003-04/msg00423.html
5685 for an example of this. This behaviour can be
5686 overridden by the using the --no-check-sections
5688 if (command_line
.check_section_addresses
)
5689 einfo (_("%F%P: error: no memory region specified"
5690 " for loadable section `%s'\n"),
5691 bfd_section_name (os
->bfd_section
));
5693 einfo (_("%P: warning: no memory region specified"
5694 " for loadable section `%s'\n"),
5695 bfd_section_name (os
->bfd_section
));
5698 newdot
= os
->region
->current
;
5699 section_alignment
= os
->bfd_section
->alignment_power
;
5702 section_alignment
= exp_get_power (os
->section_alignment
,
5703 "section alignment");
5705 /* Align to what the section needs. */
5706 if (section_alignment
> 0)
5708 bfd_vma savedot
= newdot
;
5711 newdot
= align_power (newdot
, section_alignment
);
5712 dotdelta
= newdot
- savedot
;
5714 if (lang_sizing_iteration
== 1)
5716 else if (lang_sizing_iteration
> 1)
5718 /* Only report adjustments that would change
5719 alignment from what we have already reported. */
5720 diff
= newdot
- os
->bfd_section
->vma
;
5721 if (!(diff
& (((bfd_vma
) 1 << section_alignment
) - 1)))
5725 && (config
.warn_section_align
5726 || os
->addr_tree
!= NULL
))
5727 einfo (_("%P: warning: "
5728 "start of section %s changed by %ld\n"),
5729 os
->name
, (long) diff
);
5732 bfd_set_section_vma (os
->bfd_section
, newdot
);
5734 os
->bfd_section
->output_offset
= 0;
5737 lang_size_sections_1 (&os
->children
.head
, os
,
5738 os
->fill
, newdot
, relax
, check_regions
);
5740 os
->processed_vma
= TRUE
;
5742 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
5743 /* Except for some special linker created sections,
5744 no output section should change from zero size
5745 after strip_excluded_output_sections. A non-zero
5746 size on an ignored section indicates that some
5747 input section was not sized early enough. */
5748 ASSERT (os
->bfd_section
->size
== 0);
5751 dot
= os
->bfd_section
->vma
;
5753 /* Put the section within the requested block size, or
5754 align at the block boundary. */
5756 + TO_ADDR (os
->bfd_section
->size
)
5757 + os
->block_value
- 1)
5758 & - (bfd_vma
) os
->block_value
);
5760 if (!(os
->bfd_section
->flags
& SEC_FIXED_SIZE
))
5761 os
->bfd_section
->size
= TO_SIZE (after
5762 - os
->bfd_section
->vma
);
5765 /* Set section lma. */
5768 r
= lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, FALSE
);
5772 bfd_vma lma
= exp_get_abs_int (os
->load_base
, 0, "load base");
5773 os
->bfd_section
->lma
= lma
;
5775 else if (os
->lma_region
!= NULL
)
5777 bfd_vma lma
= os
->lma_region
->current
;
5779 if (os
->align_lma_with_input
)
5783 /* When LMA_REGION is the same as REGION, align the LMA
5784 as we did for the VMA, possibly including alignment
5785 from the bfd section. If a different region, then
5786 only align according to the value in the output
5788 if (os
->lma_region
!= os
->region
)
5789 section_alignment
= exp_get_power (os
->section_alignment
,
5790 "section alignment");
5791 if (section_alignment
> 0)
5792 lma
= align_power (lma
, section_alignment
);
5794 os
->bfd_section
->lma
= lma
;
5796 else if (r
->last_os
!= NULL
5797 && (os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
5802 last
= r
->last_os
->output_section_statement
.bfd_section
;
5804 /* A backwards move of dot should be accompanied by
5805 an explicit assignment to the section LMA (ie.
5806 os->load_base set) because backwards moves can
5807 create overlapping LMAs. */
5809 && os
->bfd_section
->size
!= 0
5810 && dot
+ TO_ADDR (os
->bfd_section
->size
) <= last
->vma
)
5812 /* If dot moved backwards then leave lma equal to
5813 vma. This is the old default lma, which might
5814 just happen to work when the backwards move is
5815 sufficiently large. Nag if this changes anything,
5816 so people can fix their linker scripts. */
5818 if (last
->vma
!= last
->lma
)
5819 einfo (_("%P: warning: dot moved backwards "
5820 "before `%s'\n"), os
->name
);
5824 /* If this is an overlay, set the current lma to that
5825 at the end of the previous section. */
5826 if (os
->sectype
== overlay_section
)
5827 lma
= last
->lma
+ TO_ADDR (last
->size
);
5829 /* Otherwise, keep the same lma to vma relationship
5830 as the previous section. */
5832 lma
= os
->bfd_section
->vma
+ last
->lma
- last
->vma
;
5834 if (section_alignment
> 0)
5835 lma
= align_power (lma
, section_alignment
);
5836 os
->bfd_section
->lma
= lma
;
5839 os
->processed_lma
= TRUE
;
5841 /* Keep track of normal sections using the default
5842 lma region. We use this to set the lma for
5843 following sections. Overlays or other linker
5844 script assignment to lma might mean that the
5845 default lma == vma is incorrect.
5846 To avoid warnings about dot moving backwards when using
5847 -Ttext, don't start tracking sections until we find one
5848 of non-zero size or with lma set differently to vma.
5849 Do this tracking before we short-cut the loop so that we
5850 track changes for the case where the section size is zero,
5851 but the lma is set differently to the vma. This is
5852 important, if an orphan section is placed after an
5853 otherwise empty output section that has an explicit lma
5854 set, we want that lma reflected in the orphans lma. */
5855 if (((!IGNORE_SECTION (os
->bfd_section
)
5856 && (os
->bfd_section
->size
!= 0
5857 || (r
->last_os
== NULL
5858 && os
->bfd_section
->vma
!= os
->bfd_section
->lma
)
5859 || (r
->last_os
!= NULL
5860 && dot
>= (r
->last_os
->output_section_statement
5861 .bfd_section
->vma
))))
5862 || os
->sectype
== first_overlay_section
)
5863 && os
->lma_region
== NULL
5864 && !bfd_link_relocatable (&link_info
))
5867 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
5870 /* .tbss sections effectively have zero size. */
5871 if (!IS_TBSS (os
->bfd_section
)
5872 || bfd_link_relocatable (&link_info
))
5873 dotdelta
= TO_ADDR (os
->bfd_section
->size
);
5878 if (os
->update_dot_tree
!= 0)
5879 exp_fold_tree (os
->update_dot_tree
, bfd_abs_section_ptr
, &dot
);
5881 /* Update dot in the region ?
5882 We only do this if the section is going to be allocated,
5883 since unallocated sections do not contribute to the region's
5884 overall size in memory. */
5885 if (os
->region
!= NULL
5886 && (os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
)))
5888 os
->region
->current
= dot
;
5891 /* Make sure the new address is within the region. */
5892 os_region_check (os
, os
->region
, os
->addr_tree
,
5893 os
->bfd_section
->vma
);
5895 if (os
->lma_region
!= NULL
&& os
->lma_region
!= os
->region
5896 && ((os
->bfd_section
->flags
& SEC_LOAD
)
5897 || os
->align_lma_with_input
))
5899 os
->lma_region
->current
= os
->bfd_section
->lma
+ dotdelta
;
5902 os_region_check (os
, os
->lma_region
, NULL
,
5903 os
->bfd_section
->lma
);
5909 case lang_constructors_statement_enum
:
5910 dot
= lang_size_sections_1 (&constructor_list
.head
,
5911 output_section_statement
,
5912 fill
, dot
, relax
, check_regions
);
5915 case lang_data_statement_enum
:
5917 unsigned int size
= 0;
5919 s
->data_statement
.output_offset
=
5920 dot
- output_section_statement
->bfd_section
->vma
;
5921 s
->data_statement
.output_section
=
5922 output_section_statement
->bfd_section
;
5924 /* We might refer to provided symbols in the expression, and
5925 need to mark them as needed. */
5926 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
5928 switch (s
->data_statement
.type
)
5946 if (size
< TO_SIZE ((unsigned) 1))
5947 size
= TO_SIZE ((unsigned) 1);
5948 dot
+= TO_ADDR (size
);
5949 if (!(output_section_statement
->bfd_section
->flags
5951 output_section_statement
->bfd_section
->size
5952 = TO_SIZE (dot
- output_section_statement
->bfd_section
->vma
);
5957 case lang_reloc_statement_enum
:
5961 s
->reloc_statement
.output_offset
=
5962 dot
- output_section_statement
->bfd_section
->vma
;
5963 s
->reloc_statement
.output_section
=
5964 output_section_statement
->bfd_section
;
5965 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
5966 dot
+= TO_ADDR (size
);
5967 if (!(output_section_statement
->bfd_section
->flags
5969 output_section_statement
->bfd_section
->size
5970 = TO_SIZE (dot
- output_section_statement
->bfd_section
->vma
);
5974 case lang_wild_statement_enum
:
5975 dot
= lang_size_sections_1 (&s
->wild_statement
.children
.head
,
5976 output_section_statement
,
5977 fill
, dot
, relax
, check_regions
);
5980 case lang_object_symbols_statement_enum
:
5981 link_info
.create_object_symbols_section
5982 = output_section_statement
->bfd_section
;
5983 output_section_statement
->bfd_section
->flags
|= SEC_KEEP
;
5986 case lang_output_statement_enum
:
5987 case lang_target_statement_enum
:
5990 case lang_input_section_enum
:
5994 i
= s
->input_section
.section
;
5999 if (!bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
6000 einfo (_("%F%P: can't relax section: %E\n"));
6004 dot
= size_input_section (prev
, output_section_statement
,
6005 fill
, &removed
, dot
);
6009 case lang_input_statement_enum
:
6012 case lang_fill_statement_enum
:
6013 s
->fill_statement
.output_section
=
6014 output_section_statement
->bfd_section
;
6016 fill
= s
->fill_statement
.fill
;
6019 case lang_assignment_statement_enum
:
6021 bfd_vma newdot
= dot
;
6022 etree_type
*tree
= s
->assignment_statement
.exp
;
6024 expld
.dataseg
.relro
= exp_seg_relro_none
;
6026 exp_fold_tree (tree
,
6027 output_section_statement
->bfd_section
,
6030 ldlang_check_relro_region (s
, &expld
.dataseg
);
6032 expld
.dataseg
.relro
= exp_seg_relro_none
;
6034 /* This symbol may be relative to this section. */
6035 if ((tree
->type
.node_class
== etree_provided
6036 || tree
->type
.node_class
== etree_assign
)
6037 && (tree
->assign
.dst
[0] != '.'
6038 || tree
->assign
.dst
[1] != '\0'))
6039 output_section_statement
->update_dot
= 1;
6041 if (!output_section_statement
->ignored
)
6043 if (output_section_statement
== abs_output_section
)
6045 /* If we don't have an output section, then just adjust
6046 the default memory address. */
6047 lang_memory_region_lookup (DEFAULT_MEMORY_REGION
,
6048 FALSE
)->current
= newdot
;
6050 else if (newdot
!= dot
)
6052 /* Insert a pad after this statement. We can't
6053 put the pad before when relaxing, in case the
6054 assignment references dot. */
6055 insert_pad (&s
->header
.next
, fill
, TO_SIZE (newdot
- dot
),
6056 output_section_statement
->bfd_section
, dot
);
6058 /* Don't neuter the pad below when relaxing. */
6061 /* If dot is advanced, this implies that the section
6062 should have space allocated to it, unless the
6063 user has explicitly stated that the section
6064 should not be allocated. */
6065 if (output_section_statement
->sectype
!= noalloc_section
6066 && (output_section_statement
->sectype
!= noload_section
6067 || (bfd_get_flavour (link_info
.output_bfd
)
6068 == bfd_target_elf_flavour
)))
6069 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
;
6076 case lang_padding_statement_enum
:
6077 /* If this is the first time lang_size_sections is called,
6078 we won't have any padding statements. If this is the
6079 second or later passes when relaxing, we should allow
6080 padding to shrink. If padding is needed on this pass, it
6081 will be added back in. */
6082 s
->padding_statement
.size
= 0;
6084 /* Make sure output_offset is valid. If relaxation shrinks
6085 the section and this pad isn't needed, it's possible to
6086 have output_offset larger than the final size of the
6087 section. bfd_set_section_contents will complain even for
6088 a pad size of zero. */
6089 s
->padding_statement
.output_offset
6090 = dot
- output_section_statement
->bfd_section
->vma
;
6093 case lang_group_statement_enum
:
6094 dot
= lang_size_sections_1 (&s
->group_statement
.children
.head
,
6095 output_section_statement
,
6096 fill
, dot
, relax
, check_regions
);
6099 case lang_insert_statement_enum
:
6102 /* We can only get here when relaxing is turned on. */
6103 case lang_address_statement_enum
:
6111 /* If an input section doesn't fit in the current output
6112 section, remove it from the list. Handle the case where we
6113 have to remove an input_section statement here: there is a
6114 special case to remove the first element of the list. */
6115 if (link_info
.non_contiguous_regions
&& removed
)
6117 /* If we removed the first element during the previous
6118 iteration, override the loop assignment of prev_s. */
6124 /* If there was a real previous input section, just skip
6126 prev_s
->header
.next
=s
->header
.next
;
6128 removed_prev_s
= FALSE
;
6132 /* Remove the first input section of the list. */
6133 *prev
= s
->header
.next
;
6134 removed_prev_s
= TRUE
;
6137 /* Move to next element, unless we removed the head of the
6139 if (!removed_prev_s
)
6140 prev
= &s
->header
.next
;
6144 prev
= &s
->header
.next
;
6145 removed_prev_s
= FALSE
;
6151 /* Callback routine that is used in _bfd_elf_map_sections_to_segments.
6152 The BFD library has set NEW_SEGMENT to TRUE iff it thinks that
6153 CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different
6154 segments. We are allowed an opportunity to override this decision. */
6157 ldlang_override_segment_assignment (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6158 bfd
*abfd ATTRIBUTE_UNUSED
,
6159 asection
*current_section
,
6160 asection
*previous_section
,
6161 bfd_boolean new_segment
)
6163 lang_output_section_statement_type
*cur
;
6164 lang_output_section_statement_type
*prev
;
6166 /* The checks below are only necessary when the BFD library has decided
6167 that the two sections ought to be placed into the same segment. */
6171 /* Paranoia checks. */
6172 if (current_section
== NULL
|| previous_section
== NULL
)
6175 /* If this flag is set, the target never wants code and non-code
6176 sections comingled in the same segment. */
6177 if (config
.separate_code
6178 && ((current_section
->flags
^ previous_section
->flags
) & SEC_CODE
))
6181 /* Find the memory regions associated with the two sections.
6182 We call lang_output_section_find() here rather than scanning the list
6183 of output sections looking for a matching section pointer because if
6184 we have a large number of sections then a hash lookup is faster. */
6185 cur
= lang_output_section_find (current_section
->name
);
6186 prev
= lang_output_section_find (previous_section
->name
);
6188 /* More paranoia. */
6189 if (cur
== NULL
|| prev
== NULL
)
6192 /* If the regions are different then force the sections to live in
6193 different segments. See the email thread starting at the following
6194 URL for the reasons why this is necessary:
6195 http://sourceware.org/ml/binutils/2007-02/msg00216.html */
6196 return cur
->region
!= prev
->region
;
6200 one_lang_size_sections_pass (bfd_boolean
*relax
, bfd_boolean check_regions
)
6202 lang_statement_iteration
++;
6203 if (expld
.phase
!= lang_mark_phase_enum
)
6204 lang_sizing_iteration
++;
6205 lang_size_sections_1 (&statement_list
.head
, abs_output_section
,
6206 0, 0, relax
, check_regions
);
6210 lang_size_segment (seg_align_type
*seg
)
6212 /* If XXX_SEGMENT_ALIGN XXX_SEGMENT_END pair was seen, check whether
6213 a page could be saved in the data segment. */
6214 bfd_vma first
, last
;
6216 first
= -seg
->base
& (seg
->pagesize
- 1);
6217 last
= seg
->end
& (seg
->pagesize
- 1);
6219 && ((seg
->base
& ~(seg
->pagesize
- 1))
6220 != (seg
->end
& ~(seg
->pagesize
- 1)))
6221 && first
+ last
<= seg
->pagesize
)
6223 seg
->phase
= exp_seg_adjust
;
6227 seg
->phase
= exp_seg_done
;
6232 lang_size_relro_segment_1 (seg_align_type
*seg
)
6234 bfd_vma relro_end
, desired_end
;
6237 /* Compute the expected PT_GNU_RELRO/PT_LOAD segment end. */
6238 relro_end
= ((seg
->relro_end
+ seg
->pagesize
- 1)
6239 & ~(seg
->pagesize
- 1));
6241 /* Adjust by the offset arg of XXX_SEGMENT_RELRO_END. */
6242 desired_end
= relro_end
- seg
->relro_offset
;
6244 /* For sections in the relro segment.. */
6245 for (sec
= link_info
.output_bfd
->section_last
; sec
; sec
= sec
->prev
)
6246 if ((sec
->flags
& SEC_ALLOC
) != 0
6247 && sec
->vma
>= seg
->base
6248 && sec
->vma
< seg
->relro_end
- seg
->relro_offset
)
6250 /* Where do we want to put this section so that it ends as
6252 bfd_vma start
, end
, bump
;
6254 end
= start
= sec
->vma
;
6256 end
+= TO_ADDR (sec
->size
);
6257 bump
= desired_end
- end
;
6258 /* We'd like to increase START by BUMP, but we must heed
6259 alignment so the increase might be less than optimum. */
6261 start
&= ~(((bfd_vma
) 1 << sec
->alignment_power
) - 1);
6262 /* This is now the desired end for the previous section. */
6263 desired_end
= start
;
6266 seg
->phase
= exp_seg_relro_adjust
;
6267 ASSERT (desired_end
>= seg
->base
);
6268 seg
->base
= desired_end
;
6273 lang_size_relro_segment (bfd_boolean
*relax
, bfd_boolean check_regions
)
6275 bfd_boolean do_reset
= FALSE
;
6276 bfd_boolean do_data_relro
;
6277 bfd_vma data_initial_base
, data_relro_end
;
6279 if (link_info
.relro
&& expld
.dataseg
.relro_end
)
6281 do_data_relro
= TRUE
;
6282 data_initial_base
= expld
.dataseg
.base
;
6283 data_relro_end
= lang_size_relro_segment_1 (&expld
.dataseg
);
6287 do_data_relro
= FALSE
;
6288 data_initial_base
= data_relro_end
= 0;
6293 lang_reset_memory_regions ();
6294 one_lang_size_sections_pass (relax
, check_regions
);
6296 /* Assignments to dot, or to output section address in a user
6297 script have increased padding over the original. Revert. */
6298 if (do_data_relro
&& expld
.dataseg
.relro_end
> data_relro_end
)
6300 expld
.dataseg
.base
= data_initial_base
;;
6305 if (!do_data_relro
&& lang_size_segment (&expld
.dataseg
))
6312 lang_size_sections (bfd_boolean
*relax
, bfd_boolean check_regions
)
6314 expld
.phase
= lang_allocating_phase_enum
;
6315 expld
.dataseg
.phase
= exp_seg_none
;
6317 one_lang_size_sections_pass (relax
, check_regions
);
6319 if (expld
.dataseg
.phase
!= exp_seg_end_seen
)
6320 expld
.dataseg
.phase
= exp_seg_done
;
6322 if (expld
.dataseg
.phase
== exp_seg_end_seen
)
6324 bfd_boolean do_reset
6325 = lang_size_relro_segment (relax
, check_regions
);
6329 lang_reset_memory_regions ();
6330 one_lang_size_sections_pass (relax
, check_regions
);
6333 if (link_info
.relro
&& expld
.dataseg
.relro_end
)
6335 link_info
.relro_start
= expld
.dataseg
.base
;
6336 link_info
.relro_end
= expld
.dataseg
.relro_end
;
6341 static lang_output_section_statement_type
*current_section
;
6342 static lang_assignment_statement_type
*current_assign
;
6343 static bfd_boolean prefer_next_section
;
6345 /* Worker function for lang_do_assignments. Recursiveness goes here. */
6348 lang_do_assignments_1 (lang_statement_union_type
*s
,
6349 lang_output_section_statement_type
*current_os
,
6352 bfd_boolean
*found_end
)
6354 for (; s
!= NULL
; s
= s
->header
.next
)
6356 switch (s
->header
.type
)
6358 case lang_constructors_statement_enum
:
6359 dot
= lang_do_assignments_1 (constructor_list
.head
,
6360 current_os
, fill
, dot
, found_end
);
6363 case lang_output_section_statement_enum
:
6365 lang_output_section_statement_type
*os
;
6368 os
= &(s
->output_section_statement
);
6369 os
->after_end
= *found_end
;
6370 init_opb (os
->bfd_section
);
6371 if (os
->bfd_section
!= NULL
&& !os
->ignored
)
6373 if ((os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
6375 current_section
= os
;
6376 prefer_next_section
= FALSE
;
6378 dot
= os
->bfd_section
->vma
;
6380 newdot
= lang_do_assignments_1 (os
->children
.head
,
6381 os
, os
->fill
, dot
, found_end
);
6384 if (os
->bfd_section
!= NULL
)
6386 /* .tbss sections effectively have zero size. */
6387 if (!IS_TBSS (os
->bfd_section
)
6388 || bfd_link_relocatable (&link_info
))
6389 dot
+= TO_ADDR (os
->bfd_section
->size
);
6391 if (os
->update_dot_tree
!= NULL
)
6392 exp_fold_tree (os
->update_dot_tree
,
6393 bfd_abs_section_ptr
, &dot
);
6401 case lang_wild_statement_enum
:
6403 dot
= lang_do_assignments_1 (s
->wild_statement
.children
.head
,
6404 current_os
, fill
, dot
, found_end
);
6407 case lang_object_symbols_statement_enum
:
6408 case lang_output_statement_enum
:
6409 case lang_target_statement_enum
:
6412 case lang_data_statement_enum
:
6413 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
6414 if (expld
.result
.valid_p
)
6416 s
->data_statement
.value
= expld
.result
.value
;
6417 if (expld
.result
.section
!= NULL
)
6418 s
->data_statement
.value
+= expld
.result
.section
->vma
;
6420 else if (expld
.phase
== lang_final_phase_enum
)
6421 einfo (_("%F%P: invalid data statement\n"));
6424 switch (s
->data_statement
.type
)
6442 if (size
< TO_SIZE ((unsigned) 1))
6443 size
= TO_SIZE ((unsigned) 1);
6444 dot
+= TO_ADDR (size
);
6448 case lang_reloc_statement_enum
:
6449 exp_fold_tree (s
->reloc_statement
.addend_exp
,
6450 bfd_abs_section_ptr
, &dot
);
6451 if (expld
.result
.valid_p
)
6452 s
->reloc_statement
.addend_value
= expld
.result
.value
;
6453 else if (expld
.phase
== lang_final_phase_enum
)
6454 einfo (_("%F%P: invalid reloc statement\n"));
6455 dot
+= TO_ADDR (bfd_get_reloc_size (s
->reloc_statement
.howto
));
6458 case lang_input_section_enum
:
6460 asection
*in
= s
->input_section
.section
;
6462 if ((in
->flags
& SEC_EXCLUDE
) == 0)
6463 dot
+= TO_ADDR (in
->size
);
6467 case lang_input_statement_enum
:
6470 case lang_fill_statement_enum
:
6471 fill
= s
->fill_statement
.fill
;
6474 case lang_assignment_statement_enum
:
6475 current_assign
= &s
->assignment_statement
;
6476 if (current_assign
->exp
->type
.node_class
!= etree_assert
)
6478 const char *p
= current_assign
->exp
->assign
.dst
;
6480 if (current_os
== abs_output_section
&& p
[0] == '.' && p
[1] == 0)
6481 prefer_next_section
= TRUE
;
6485 if (strcmp (p
, "end") == 0)
6488 exp_fold_tree (s
->assignment_statement
.exp
,
6489 (current_os
->bfd_section
!= NULL
6490 ? current_os
->bfd_section
: bfd_und_section_ptr
),
6494 case lang_padding_statement_enum
:
6495 dot
+= TO_ADDR (s
->padding_statement
.size
);
6498 case lang_group_statement_enum
:
6499 dot
= lang_do_assignments_1 (s
->group_statement
.children
.head
,
6500 current_os
, fill
, dot
, found_end
);
6503 case lang_insert_statement_enum
:
6506 case lang_address_statement_enum
:
6518 lang_do_assignments (lang_phase_type phase
)
6520 bfd_boolean found_end
= FALSE
;
6522 current_section
= NULL
;
6523 prefer_next_section
= FALSE
;
6524 expld
.phase
= phase
;
6525 lang_statement_iteration
++;
6526 lang_do_assignments_1 (statement_list
.head
,
6527 abs_output_section
, NULL
, 0, &found_end
);
6530 /* For an assignment statement outside of an output section statement,
6531 choose the best of neighbouring output sections to use for values
6535 section_for_dot (void)
6539 /* Assignments belong to the previous output section, unless there
6540 has been an assignment to "dot", in which case following
6541 assignments belong to the next output section. (The assumption
6542 is that an assignment to "dot" is setting up the address for the
6543 next output section.) Except that past the assignment to "_end"
6544 we always associate with the previous section. This exception is
6545 for targets like SH that define an alloc .stack or other
6546 weirdness after non-alloc sections. */
6547 if (current_section
== NULL
|| prefer_next_section
)
6549 lang_statement_union_type
*stmt
;
6550 lang_output_section_statement_type
*os
;
6552 for (stmt
= (lang_statement_union_type
*) current_assign
;
6554 stmt
= stmt
->header
.next
)
6555 if (stmt
->header
.type
== lang_output_section_statement_enum
)
6558 os
= &stmt
->output_section_statement
;
6561 && (os
->bfd_section
== NULL
6562 || (os
->bfd_section
->flags
& SEC_EXCLUDE
) != 0
6563 || bfd_section_removed_from_list (link_info
.output_bfd
,
6567 if (current_section
== NULL
|| os
== NULL
|| !os
->after_end
)
6570 s
= os
->bfd_section
;
6572 s
= link_info
.output_bfd
->section_last
;
6574 && ((s
->flags
& SEC_ALLOC
) == 0
6575 || (s
->flags
& SEC_THREAD_LOCAL
) != 0))
6580 return bfd_abs_section_ptr
;
6584 s
= current_section
->bfd_section
;
6586 /* The section may have been stripped. */
6588 && ((s
->flags
& SEC_EXCLUDE
) != 0
6589 || (s
->flags
& SEC_ALLOC
) == 0
6590 || (s
->flags
& SEC_THREAD_LOCAL
) != 0
6591 || bfd_section_removed_from_list (link_info
.output_bfd
, s
)))
6594 s
= link_info
.output_bfd
->sections
;
6596 && ((s
->flags
& SEC_ALLOC
) == 0
6597 || (s
->flags
& SEC_THREAD_LOCAL
) != 0))
6602 return bfd_abs_section_ptr
;
6605 /* Array of __start/__stop/.startof./.sizeof/ symbols. */
6607 static struct bfd_link_hash_entry
**start_stop_syms
;
6608 static size_t start_stop_count
= 0;
6609 static size_t start_stop_alloc
= 0;
6611 /* Give start/stop SYMBOL for SEC a preliminary definition, and add it
6612 to start_stop_syms. */
6615 lang_define_start_stop (const char *symbol
, asection
*sec
)
6617 struct bfd_link_hash_entry
*h
;
6619 h
= bfd_define_start_stop (link_info
.output_bfd
, &link_info
, symbol
, sec
);
6622 if (start_stop_count
== start_stop_alloc
)
6624 start_stop_alloc
= 2 * start_stop_alloc
+ 10;
6626 = xrealloc (start_stop_syms
,
6627 start_stop_alloc
* sizeof (*start_stop_syms
));
6629 start_stop_syms
[start_stop_count
++] = h
;
6633 /* Check for input sections whose names match references to
6634 __start_SECNAME or __stop_SECNAME symbols. Give the symbols
6635 preliminary definitions. */
6638 lang_init_start_stop (void)
6642 char leading_char
= bfd_get_symbol_leading_char (link_info
.output_bfd
);
6644 for (abfd
= link_info
.input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
6645 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
6648 const char *secname
= s
->name
;
6650 for (ps
= secname
; *ps
!= '\0'; ps
++)
6651 if (!ISALNUM ((unsigned char) *ps
) && *ps
!= '_')
6655 char *symbol
= (char *) xmalloc (10 + strlen (secname
));
6657 symbol
[0] = leading_char
;
6658 sprintf (symbol
+ (leading_char
!= 0), "__start_%s", secname
);
6659 lang_define_start_stop (symbol
, s
);
6661 symbol
[1] = leading_char
;
6662 memcpy (symbol
+ 1 + (leading_char
!= 0), "__stop", 6);
6663 lang_define_start_stop (symbol
+ 1, s
);
6670 /* Iterate over start_stop_syms. */
6673 foreach_start_stop (void (*func
) (struct bfd_link_hash_entry
*))
6677 for (i
= 0; i
< start_stop_count
; ++i
)
6678 func (start_stop_syms
[i
]);
6681 /* __start and __stop symbols are only supposed to be defined by the
6682 linker for orphan sections, but we now extend that to sections that
6683 map to an output section of the same name. The symbols were
6684 defined early for --gc-sections, before we mapped input to output
6685 sections, so undo those that don't satisfy this rule. */
6688 undef_start_stop (struct bfd_link_hash_entry
*h
)
6690 if (h
->ldscript_def
)
6693 if (h
->u
.def
.section
->output_section
== NULL
6694 || h
->u
.def
.section
->output_section
->owner
!= link_info
.output_bfd
6695 || strcmp (h
->u
.def
.section
->name
,
6696 h
->u
.def
.section
->output_section
->name
) != 0)
6698 asection
*sec
= bfd_get_section_by_name (link_info
.output_bfd
,
6699 h
->u
.def
.section
->name
);
6702 /* When there are more than one input sections with the same
6703 section name, SECNAME, linker picks the first one to define
6704 __start_SECNAME and __stop_SECNAME symbols. When the first
6705 input section is removed by comdat group, we need to check
6706 if there is still an output section with section name
6709 for (i
= sec
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
6710 if (strcmp (h
->u
.def
.section
->name
, i
->name
) == 0)
6712 h
->u
.def
.section
= i
;
6716 h
->type
= bfd_link_hash_undefined
;
6717 h
->u
.undef
.abfd
= NULL
;
6722 lang_undef_start_stop (void)
6724 foreach_start_stop (undef_start_stop
);
6727 /* Check for output sections whose names match references to
6728 .startof.SECNAME or .sizeof.SECNAME symbols. Give the symbols
6729 preliminary definitions. */
6732 lang_init_startof_sizeof (void)
6736 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6738 const char *secname
= s
->name
;
6739 char *symbol
= (char *) xmalloc (10 + strlen (secname
));
6741 sprintf (symbol
, ".startof.%s", secname
);
6742 lang_define_start_stop (symbol
, s
);
6744 memcpy (symbol
+ 1, ".size", 5);
6745 lang_define_start_stop (symbol
+ 1, s
);
6750 /* Set .startof., .sizeof., __start and __stop symbols final values. */
6753 set_start_stop (struct bfd_link_hash_entry
*h
)
6756 || h
->type
!= bfd_link_hash_defined
)
6759 if (h
->root
.string
[0] == '.')
6761 /* .startof. or .sizeof. symbol.
6762 .startof. already has final value. */
6763 if (h
->root
.string
[2] == 'i')
6766 h
->u
.def
.value
= TO_ADDR (h
->u
.def
.section
->size
);
6767 h
->u
.def
.section
= bfd_abs_section_ptr
;
6772 /* __start or __stop symbol. */
6773 int has_lead
= bfd_get_symbol_leading_char (link_info
.output_bfd
) != 0;
6775 h
->u
.def
.section
= h
->u
.def
.section
->output_section
;
6776 if (h
->root
.string
[4 + has_lead
] == 'o')
6779 h
->u
.def
.value
= TO_ADDR (h
->u
.def
.section
->size
);
6785 lang_finalize_start_stop (void)
6787 foreach_start_stop (set_start_stop
);
6793 struct bfd_link_hash_entry
*h
;
6796 if ((bfd_link_relocatable (&link_info
) && !link_info
.gc_sections
)
6797 || bfd_link_dll (&link_info
))
6798 warn
= entry_from_cmdline
;
6802 /* Force the user to specify a root when generating a relocatable with
6803 --gc-sections, unless --gc-keep-exported was also given. */
6804 if (bfd_link_relocatable (&link_info
)
6805 && link_info
.gc_sections
6806 && !link_info
.gc_keep_exported
6807 && !(entry_from_cmdline
|| undef_from_cmdline
))
6808 einfo (_("%F%P: gc-sections requires either an entry or "
6809 "an undefined symbol\n"));
6811 if (entry_symbol
.name
== NULL
)
6813 /* No entry has been specified. Look for the default entry, but
6814 don't warn if we don't find it. */
6815 entry_symbol
.name
= entry_symbol_default
;
6819 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
.name
,
6820 FALSE
, FALSE
, TRUE
);
6822 && (h
->type
== bfd_link_hash_defined
6823 || h
->type
== bfd_link_hash_defweak
)
6824 && h
->u
.def
.section
->output_section
!= NULL
)
6828 val
= (h
->u
.def
.value
6829 + bfd_section_vma (h
->u
.def
.section
->output_section
)
6830 + h
->u
.def
.section
->output_offset
);
6831 if (!bfd_set_start_address (link_info
.output_bfd
, val
))
6832 einfo (_("%F%P: %s: can't set start address\n"), entry_symbol
.name
);
6839 /* We couldn't find the entry symbol. Try parsing it as a
6841 val
= bfd_scan_vma (entry_symbol
.name
, &send
, 0);
6844 if (!bfd_set_start_address (link_info
.output_bfd
, val
))
6845 einfo (_("%F%P: can't set start address\n"));
6851 /* Can't find the entry symbol, and it's not a number. Use
6852 the first address in the text section. */
6853 ts
= bfd_get_section_by_name (link_info
.output_bfd
, entry_section
);
6857 einfo (_("%P: warning: cannot find entry symbol %s;"
6858 " defaulting to %V\n"),
6860 bfd_section_vma (ts
));
6861 if (!bfd_set_start_address (link_info
.output_bfd
,
6862 bfd_section_vma (ts
)))
6863 einfo (_("%F%P: can't set start address\n"));
6868 einfo (_("%P: warning: cannot find entry symbol %s;"
6869 " not setting start address\n"),
6876 /* This is a small function used when we want to ignore errors from
6880 ignore_bfd_errors (const char *fmt ATTRIBUTE_UNUSED
,
6881 va_list ap ATTRIBUTE_UNUSED
)
6883 /* Don't do anything. */
6886 /* Check that the architecture of all the input files is compatible
6887 with the output file. Also call the backend to let it do any
6888 other checking that is needed. */
6893 lang_input_statement_type
*file
;
6895 const bfd_arch_info_type
*compatible
;
6897 for (file
= (void *) file_chain
.head
;
6901 #ifdef ENABLE_PLUGINS
6902 /* Don't check format of files claimed by plugin. */
6903 if (file
->flags
.claimed
)
6905 #endif /* ENABLE_PLUGINS */
6906 input_bfd
= file
->the_bfd
;
6908 = bfd_arch_get_compatible (input_bfd
, link_info
.output_bfd
,
6909 command_line
.accept_unknown_input_arch
);
6911 /* In general it is not possible to perform a relocatable
6912 link between differing object formats when the input
6913 file has relocations, because the relocations in the
6914 input format may not have equivalent representations in
6915 the output format (and besides BFD does not translate
6916 relocs for other link purposes than a final link). */
6917 if ((bfd_link_relocatable (&link_info
)
6918 || link_info
.emitrelocations
)
6919 && (compatible
== NULL
6920 || (bfd_get_flavour (input_bfd
)
6921 != bfd_get_flavour (link_info
.output_bfd
)))
6922 && (bfd_get_file_flags (input_bfd
) & HAS_RELOC
) != 0)
6924 einfo (_("%F%P: relocatable linking with relocations from"
6925 " format %s (%pB) to format %s (%pB) is not supported\n"),
6926 bfd_get_target (input_bfd
), input_bfd
,
6927 bfd_get_target (link_info
.output_bfd
), link_info
.output_bfd
);
6928 /* einfo with %F exits. */
6931 if (compatible
== NULL
)
6933 if (command_line
.warn_mismatch
)
6934 einfo (_("%X%P: %s architecture of input file `%pB'"
6935 " is incompatible with %s output\n"),
6936 bfd_printable_name (input_bfd
), input_bfd
,
6937 bfd_printable_name (link_info
.output_bfd
));
6940 /* If the input bfd has no contents, it shouldn't set the
6941 private data of the output bfd. */
6942 else if ((input_bfd
->flags
& DYNAMIC
) != 0
6943 || bfd_count_sections (input_bfd
) != 0)
6945 bfd_error_handler_type pfn
= NULL
;
6947 /* If we aren't supposed to warn about mismatched input
6948 files, temporarily set the BFD error handler to a
6949 function which will do nothing. We still want to call
6950 bfd_merge_private_bfd_data, since it may set up
6951 information which is needed in the output file. */
6952 if (!command_line
.warn_mismatch
)
6953 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
6954 if (!bfd_merge_private_bfd_data (input_bfd
, &link_info
))
6956 if (command_line
.warn_mismatch
)
6957 einfo (_("%X%P: failed to merge target specific data"
6958 " of file %pB\n"), input_bfd
);
6960 if (!command_line
.warn_mismatch
)
6961 bfd_set_error_handler (pfn
);
6966 /* Look through all the global common symbols and attach them to the
6967 correct section. The -sort-common command line switch may be used
6968 to roughly sort the entries by alignment. */
6973 if (link_info
.inhibit_common_definition
)
6975 if (bfd_link_relocatable (&link_info
)
6976 && !command_line
.force_common_definition
)
6979 if (!config
.sort_common
)
6980 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, NULL
);
6985 if (config
.sort_common
== sort_descending
)
6987 for (power
= 4; power
> 0; power
--)
6988 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
6991 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
6995 for (power
= 0; power
<= 4; power
++)
6996 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
6998 power
= (unsigned int) -1;
6999 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
7004 /* Place one common symbol in the correct section. */
7007 lang_one_common (struct bfd_link_hash_entry
*h
, void *info
)
7009 unsigned int power_of_two
;
7013 if (h
->type
!= bfd_link_hash_common
)
7017 power_of_two
= h
->u
.c
.p
->alignment_power
;
7019 if (config
.sort_common
== sort_descending
7020 && power_of_two
< *(unsigned int *) info
)
7022 else if (config
.sort_common
== sort_ascending
7023 && power_of_two
> *(unsigned int *) info
)
7026 section
= h
->u
.c
.p
->section
;
7027 if (!bfd_define_common_symbol (link_info
.output_bfd
, &link_info
, h
))
7028 einfo (_("%F%P: could not define common symbol `%pT': %E\n"),
7031 if (config
.map_file
!= NULL
)
7033 static bfd_boolean header_printed
;
7038 if (!header_printed
)
7040 minfo (_("\nAllocating common symbols\n"));
7041 minfo (_("Common symbol size file\n\n"));
7042 header_printed
= TRUE
;
7045 name
= bfd_demangle (link_info
.output_bfd
, h
->root
.string
,
7046 DMGL_ANSI
| DMGL_PARAMS
);
7049 minfo ("%s", h
->root
.string
);
7050 len
= strlen (h
->root
.string
);
7055 len
= strlen (name
);
7071 if (size
<= 0xffffffff)
7072 sprintf (buf
, "%lx", (unsigned long) size
);
7074 sprintf_vma (buf
, size
);
7084 minfo ("%pB\n", section
->owner
);
7090 /* Handle a single orphan section S, placing the orphan into an appropriate
7091 output section. The effects of the --orphan-handling command line
7092 option are handled here. */
7095 ldlang_place_orphan (asection
*s
)
7097 if (config
.orphan_handling
== orphan_handling_discard
)
7099 lang_output_section_statement_type
*os
;
7100 os
= lang_output_section_statement_lookup (DISCARD_SECTION_NAME
, 0,
7102 if (os
->addr_tree
== NULL
7103 && (bfd_link_relocatable (&link_info
)
7104 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0))
7105 os
->addr_tree
= exp_intop (0);
7106 lang_add_section (&os
->children
, s
, NULL
, os
);
7110 lang_output_section_statement_type
*os
;
7111 const char *name
= s
->name
;
7114 if (config
.orphan_handling
== orphan_handling_error
)
7115 einfo (_("%X%P: error: unplaced orphan section `%pA' from `%pB'\n"),
7118 if (config
.unique_orphan_sections
|| unique_section_p (s
, NULL
))
7119 constraint
= SPECIAL
;
7121 os
= ldemul_place_orphan (s
, name
, constraint
);
7124 os
= lang_output_section_statement_lookup (name
, constraint
, TRUE
);
7125 if (os
->addr_tree
== NULL
7126 && (bfd_link_relocatable (&link_info
)
7127 || (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0))
7128 os
->addr_tree
= exp_intop (0);
7129 lang_add_section (&os
->children
, s
, NULL
, os
);
7132 if (config
.orphan_handling
== orphan_handling_warn
)
7133 einfo (_("%P: warning: orphan section `%pA' from `%pB' being "
7134 "placed in section `%s'\n"),
7135 s
, s
->owner
, os
->name
);
7139 /* Run through the input files and ensure that every input section has
7140 somewhere to go. If one is found without a destination then create
7141 an input request and place it into the statement tree. */
7144 lang_place_orphans (void)
7146 LANG_FOR_EACH_INPUT_STATEMENT (file
)
7150 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7152 if (s
->output_section
== NULL
)
7154 /* This section of the file is not attached, root
7155 around for a sensible place for it to go. */
7157 if (file
->flags
.just_syms
)
7158 bfd_link_just_syms (file
->the_bfd
, s
, &link_info
);
7159 else if (lang_discard_section_p (s
))
7160 s
->output_section
= bfd_abs_section_ptr
;
7161 else if (strcmp (s
->name
, "COMMON") == 0)
7163 /* This is a lonely common section which must have
7164 come from an archive. We attach to the section
7165 with the wildcard. */
7166 if (!bfd_link_relocatable (&link_info
)
7167 || command_line
.force_common_definition
)
7169 if (default_common_section
== NULL
)
7170 default_common_section
7171 = lang_output_section_statement_lookup (".bss", 0,
7173 lang_add_section (&default_common_section
->children
, s
,
7174 NULL
, default_common_section
);
7178 ldlang_place_orphan (s
);
7185 lang_set_flags (lang_memory_region_type
*ptr
, const char *flags
, int invert
)
7187 flagword
*ptr_flags
;
7189 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
7195 /* PR 17900: An exclamation mark in the attributes reverses
7196 the sense of any of the attributes that follow. */
7199 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
7203 *ptr_flags
|= SEC_ALLOC
;
7207 *ptr_flags
|= SEC_READONLY
;
7211 *ptr_flags
|= SEC_DATA
;
7215 *ptr_flags
|= SEC_CODE
;
7220 *ptr_flags
|= SEC_LOAD
;
7224 einfo (_("%F%P: invalid character %c (%d) in flags\n"),
7232 /* Call a function on each real input file. This function will be
7233 called on an archive, but not on the elements. */
7236 lang_for_each_input_file (void (*func
) (lang_input_statement_type
*))
7238 lang_input_statement_type
*f
;
7240 for (f
= (void *) input_file_chain
.head
;
7242 f
= f
->next_real_file
)
7247 /* Call a function on each real file. The function will be called on
7248 all the elements of an archive which are included in the link, but
7249 will not be called on the archive file itself. */
7252 lang_for_each_file (void (*func
) (lang_input_statement_type
*))
7254 LANG_FOR_EACH_INPUT_STATEMENT (f
)
7262 ldlang_add_file (lang_input_statement_type
*entry
)
7264 lang_statement_append (&file_chain
, entry
, &entry
->next
);
7266 /* The BFD linker needs to have a list of all input BFDs involved in
7268 ASSERT (entry
->the_bfd
->link
.next
== NULL
);
7269 ASSERT (entry
->the_bfd
!= link_info
.output_bfd
);
7271 *link_info
.input_bfds_tail
= entry
->the_bfd
;
7272 link_info
.input_bfds_tail
= &entry
->the_bfd
->link
.next
;
7273 bfd_set_usrdata (entry
->the_bfd
, entry
);
7274 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
7276 /* Look through the sections and check for any which should not be
7277 included in the link. We need to do this now, so that we can
7278 notice when the backend linker tries to report multiple
7279 definition errors for symbols which are in sections we aren't
7280 going to link. FIXME: It might be better to entirely ignore
7281 symbols which are defined in sections which are going to be
7282 discarded. This would require modifying the backend linker for
7283 each backend which might set the SEC_LINK_ONCE flag. If we do
7284 this, we should probably handle SEC_EXCLUDE in the same way. */
7286 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, entry
);
7290 lang_add_output (const char *name
, int from_script
)
7292 /* Make -o on command line override OUTPUT in script. */
7293 if (!had_output_filename
|| !from_script
)
7295 output_filename
= name
;
7296 had_output_filename
= TRUE
;
7300 lang_output_section_statement_type
*
7301 lang_enter_output_section_statement (const char *output_section_statement_name
,
7302 etree_type
*address_exp
,
7303 enum section_type sectype
,
7305 etree_type
*subalign
,
7308 int align_with_input
)
7310 lang_output_section_statement_type
*os
;
7312 os
= lang_output_section_statement_lookup (output_section_statement_name
,
7314 current_section
= os
;
7316 if (os
->addr_tree
== NULL
)
7318 os
->addr_tree
= address_exp
;
7320 os
->sectype
= sectype
;
7321 if (sectype
!= noload_section
)
7322 os
->flags
= SEC_NO_FLAGS
;
7324 os
->flags
= SEC_NEVER_LOAD
;
7325 os
->block_value
= 1;
7327 /* Make next things chain into subchain of this. */
7328 push_stat_ptr (&os
->children
);
7330 os
->align_lma_with_input
= align_with_input
== ALIGN_WITH_INPUT
;
7331 if (os
->align_lma_with_input
&& align
!= NULL
)
7332 einfo (_("%F%P:%pS: error: align with input and explicit align specified\n"),
7335 os
->subsection_alignment
= subalign
;
7336 os
->section_alignment
= align
;
7338 os
->load_base
= ebase
;
7345 lang_output_statement_type
*new_stmt
;
7347 new_stmt
= new_stat (lang_output_statement
, stat_ptr
);
7348 new_stmt
->name
= output_filename
;
7351 /* Reset the current counters in the regions. */
7354 lang_reset_memory_regions (void)
7356 lang_memory_region_type
*p
= lang_memory_region_list
;
7358 lang_output_section_statement_type
*os
;
7360 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
7362 p
->current
= p
->origin
;
7366 for (os
= (void *) lang_os_list
.head
;
7370 os
->processed_vma
= FALSE
;
7371 os
->processed_lma
= FALSE
;
7374 for (o
= link_info
.output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
7376 /* Save the last size for possible use by bfd_relax_section. */
7377 o
->rawsize
= o
->size
;
7378 if (!(o
->flags
& SEC_FIXED_SIZE
))
7383 /* Worker for lang_gc_sections_1. */
7386 gc_section_callback (lang_wild_statement_type
*ptr
,
7387 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
7389 struct flag_info
*sflag_info ATTRIBUTE_UNUSED
,
7390 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
7391 void *data ATTRIBUTE_UNUSED
)
7393 /* If the wild pattern was marked KEEP, the member sections
7394 should be as well. */
7395 if (ptr
->keep_sections
)
7396 section
->flags
|= SEC_KEEP
;
7399 /* Iterate over sections marking them against GC. */
7402 lang_gc_sections_1 (lang_statement_union_type
*s
)
7404 for (; s
!= NULL
; s
= s
->header
.next
)
7406 switch (s
->header
.type
)
7408 case lang_wild_statement_enum
:
7409 walk_wild (&s
->wild_statement
, gc_section_callback
, NULL
);
7411 case lang_constructors_statement_enum
:
7412 lang_gc_sections_1 (constructor_list
.head
);
7414 case lang_output_section_statement_enum
:
7415 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
7417 case lang_group_statement_enum
:
7418 lang_gc_sections_1 (s
->group_statement
.children
.head
);
7427 lang_gc_sections (void)
7429 /* Keep all sections so marked in the link script. */
7430 lang_gc_sections_1 (statement_list
.head
);
7432 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
7433 the special case of debug info. (See bfd/stabs.c)
7434 Twiddle the flag here, to simplify later linker code. */
7435 if (bfd_link_relocatable (&link_info
))
7437 LANG_FOR_EACH_INPUT_STATEMENT (f
)
7440 #ifdef ENABLE_PLUGINS
7441 if (f
->flags
.claimed
)
7444 for (sec
= f
->the_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7445 if ((sec
->flags
& SEC_DEBUGGING
) == 0)
7446 sec
->flags
&= ~SEC_EXCLUDE
;
7450 if (link_info
.gc_sections
)
7451 bfd_gc_sections (link_info
.output_bfd
, &link_info
);
7454 /* Worker for lang_find_relro_sections_1. */
7457 find_relro_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
7458 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
7460 struct flag_info
*sflag_info ATTRIBUTE_UNUSED
,
7461 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
7464 /* Discarded, excluded and ignored sections effectively have zero
7466 if (section
->output_section
!= NULL
7467 && section
->output_section
->owner
== link_info
.output_bfd
7468 && (section
->output_section
->flags
& SEC_EXCLUDE
) == 0
7469 && !IGNORE_SECTION (section
)
7470 && section
->size
!= 0)
7472 bfd_boolean
*has_relro_section
= (bfd_boolean
*) data
;
7473 *has_relro_section
= TRUE
;
7477 /* Iterate over sections for relro sections. */
7480 lang_find_relro_sections_1 (lang_statement_union_type
*s
,
7481 seg_align_type
*seg
,
7482 bfd_boolean
*has_relro_section
)
7484 if (*has_relro_section
)
7487 for (; s
!= NULL
; s
= s
->header
.next
)
7489 if (s
== seg
->relro_end_stat
)
7492 switch (s
->header
.type
)
7494 case lang_wild_statement_enum
:
7495 walk_wild (&s
->wild_statement
,
7496 find_relro_section_callback
,
7499 case lang_constructors_statement_enum
:
7500 lang_find_relro_sections_1 (constructor_list
.head
,
7501 seg
, has_relro_section
);
7503 case lang_output_section_statement_enum
:
7504 lang_find_relro_sections_1 (s
->output_section_statement
.children
.head
,
7505 seg
, has_relro_section
);
7507 case lang_group_statement_enum
:
7508 lang_find_relro_sections_1 (s
->group_statement
.children
.head
,
7509 seg
, has_relro_section
);
7518 lang_find_relro_sections (void)
7520 bfd_boolean has_relro_section
= FALSE
;
7522 /* Check all sections in the link script. */
7524 lang_find_relro_sections_1 (expld
.dataseg
.relro_start_stat
,
7525 &expld
.dataseg
, &has_relro_section
);
7527 if (!has_relro_section
)
7528 link_info
.relro
= FALSE
;
7531 /* Relax all sections until bfd_relax_section gives up. */
7534 lang_relax_sections (bfd_boolean need_layout
)
7536 if (RELAXATION_ENABLED
)
7538 /* We may need more than one relaxation pass. */
7539 int i
= link_info
.relax_pass
;
7541 /* The backend can use it to determine the current pass. */
7542 link_info
.relax_pass
= 0;
7546 /* Keep relaxing until bfd_relax_section gives up. */
7547 bfd_boolean relax_again
;
7549 link_info
.relax_trip
= -1;
7552 link_info
.relax_trip
++;
7554 /* Note: pe-dll.c does something like this also. If you find
7555 you need to change this code, you probably need to change
7556 pe-dll.c also. DJ */
7558 /* Do all the assignments with our current guesses as to
7560 lang_do_assignments (lang_assigning_phase_enum
);
7562 /* We must do this after lang_do_assignments, because it uses
7564 lang_reset_memory_regions ();
7566 /* Perform another relax pass - this time we know where the
7567 globals are, so can make a better guess. */
7568 relax_again
= FALSE
;
7569 lang_size_sections (&relax_again
, FALSE
);
7571 while (relax_again
);
7573 link_info
.relax_pass
++;
7580 /* Final extra sizing to report errors. */
7581 lang_do_assignments (lang_assigning_phase_enum
);
7582 lang_reset_memory_regions ();
7583 lang_size_sections (NULL
, TRUE
);
7587 #ifdef ENABLE_PLUGINS
7588 /* Find the insert point for the plugin's replacement files. We
7589 place them after the first claimed real object file, or if the
7590 first claimed object is an archive member, after the last real
7591 object file immediately preceding the archive. In the event
7592 no objects have been claimed at all, we return the first dummy
7593 object file on the list as the insert point; that works, but
7594 the callee must be careful when relinking the file_chain as it
7595 is not actually on that chain, only the statement_list and the
7596 input_file list; in that case, the replacement files must be
7597 inserted at the head of the file_chain. */
7599 static lang_input_statement_type
*
7600 find_replacements_insert_point (bfd_boolean
*before
)
7602 lang_input_statement_type
*claim1
, *lastobject
;
7603 lastobject
= (void *) input_file_chain
.head
;
7604 for (claim1
= (void *) file_chain
.head
;
7606 claim1
= claim1
->next
)
7608 if (claim1
->flags
.claimed
)
7610 *before
= claim1
->flags
.claim_archive
;
7611 return claim1
->flags
.claim_archive
? lastobject
: claim1
;
7613 /* Update lastobject if this is a real object file. */
7614 if (claim1
->the_bfd
!= NULL
&& claim1
->the_bfd
->my_archive
== NULL
)
7615 lastobject
= claim1
;
7617 /* No files were claimed by the plugin. Choose the last object
7618 file found on the list (maybe the first, dummy entry) as the
7624 /* Find where to insert ADD, an archive element or shared library
7625 added during a rescan. */
7627 static lang_input_statement_type
**
7628 find_rescan_insertion (lang_input_statement_type
*add
)
7630 bfd
*add_bfd
= add
->the_bfd
;
7631 lang_input_statement_type
*f
;
7632 lang_input_statement_type
*last_loaded
= NULL
;
7633 lang_input_statement_type
*before
= NULL
;
7634 lang_input_statement_type
**iter
= NULL
;
7636 if (add_bfd
->my_archive
!= NULL
)
7637 add_bfd
= add_bfd
->my_archive
;
7639 /* First look through the input file chain, to find an object file
7640 before the one we've rescanned. Normal object files always
7641 appear on both the input file chain and the file chain, so this
7642 lets us get quickly to somewhere near the correct place on the
7643 file chain if it is full of archive elements. Archives don't
7644 appear on the file chain, but if an element has been extracted
7645 then their input_statement->next points at it. */
7646 for (f
= (void *) input_file_chain
.head
;
7648 f
= f
->next_real_file
)
7650 if (f
->the_bfd
== add_bfd
)
7652 before
= last_loaded
;
7653 if (f
->next
!= NULL
)
7654 return &f
->next
->next
;
7656 if (f
->the_bfd
!= NULL
&& f
->next
!= NULL
)
7660 for (iter
= before
? &before
->next
: &file_chain
.head
->input_statement
.next
;
7662 iter
= &(*iter
)->next
)
7663 if (!(*iter
)->flags
.claim_archive
7664 && (*iter
)->the_bfd
->my_archive
== NULL
)
7670 /* Insert SRCLIST into DESTLIST after given element by chaining
7671 on FIELD as the next-pointer. (Counterintuitively does not need
7672 a pointer to the actual after-node itself, just its chain field.) */
7675 lang_list_insert_after (lang_statement_list_type
*destlist
,
7676 lang_statement_list_type
*srclist
,
7677 lang_statement_union_type
**field
)
7679 *(srclist
->tail
) = *field
;
7680 *field
= srclist
->head
;
7681 if (destlist
->tail
== field
)
7682 destlist
->tail
= srclist
->tail
;
7685 /* Detach new nodes added to DESTLIST since the time ORIGLIST
7686 was taken as a copy of it and leave them in ORIGLIST. */
7689 lang_list_remove_tail (lang_statement_list_type
*destlist
,
7690 lang_statement_list_type
*origlist
)
7692 union lang_statement_union
**savetail
;
7693 /* Check that ORIGLIST really is an earlier state of DESTLIST. */
7694 ASSERT (origlist
->head
== destlist
->head
);
7695 savetail
= origlist
->tail
;
7696 origlist
->head
= *(savetail
);
7697 origlist
->tail
= destlist
->tail
;
7698 destlist
->tail
= savetail
;
7702 static lang_statement_union_type
**
7703 find_next_input_statement (lang_statement_union_type
**s
)
7705 for ( ; *s
; s
= &(*s
)->header
.next
)
7707 lang_statement_union_type
**t
;
7708 switch ((*s
)->header
.type
)
7710 case lang_input_statement_enum
:
7712 case lang_wild_statement_enum
:
7713 t
= &(*s
)->wild_statement
.children
.head
;
7715 case lang_group_statement_enum
:
7716 t
= &(*s
)->group_statement
.children
.head
;
7718 case lang_output_section_statement_enum
:
7719 t
= &(*s
)->output_section_statement
.children
.head
;
7724 t
= find_next_input_statement (t
);
7730 #endif /* ENABLE_PLUGINS */
7732 /* Add NAME to the list of garbage collection entry points. */
7735 lang_add_gc_name (const char *name
)
7737 struct bfd_sym_chain
*sym
;
7742 sym
= stat_alloc (sizeof (*sym
));
7744 sym
->next
= link_info
.gc_sym_list
;
7746 link_info
.gc_sym_list
= sym
;
7749 /* Check relocations. */
7752 lang_check_relocs (void)
7754 if (link_info
.check_relocs_after_open_input
)
7758 for (abfd
= link_info
.input_bfds
;
7759 abfd
!= (bfd
*) NULL
; abfd
= abfd
->link
.next
)
7760 if (!bfd_link_check_relocs (abfd
, &link_info
))
7762 /* No object output, fail return. */
7763 config
.make_executable
= FALSE
;
7764 /* Note: we do not abort the loop, but rather
7765 continue the scan in case there are other
7766 bad relocations to report. */
7771 /* Look through all output sections looking for places where we can
7772 propagate forward the lma region. */
7775 lang_propagate_lma_regions (void)
7777 lang_output_section_statement_type
*os
;
7779 for (os
= (void *) lang_os_list
.head
;
7783 if (os
->prev
!= NULL
7784 && os
->lma_region
== NULL
7785 && os
->load_base
== NULL
7786 && os
->addr_tree
== NULL
7787 && os
->region
== os
->prev
->region
)
7788 os
->lma_region
= os
->prev
->lma_region
;
7795 /* Finalize dynamic list. */
7796 if (link_info
.dynamic_list
)
7797 lang_finalize_version_expr_head (&link_info
.dynamic_list
->head
);
7799 current_target
= default_target
;
7801 /* Open the output file. */
7802 lang_for_each_statement (ldlang_open_output
);
7805 ldemul_create_output_section_statements ();
7807 /* Add to the hash table all undefineds on the command line. */
7808 lang_place_undefineds ();
7810 if (!bfd_section_already_linked_table_init ())
7811 einfo (_("%F%P: can not create hash table: %E\n"));
7813 /* Create a bfd for each input file. */
7814 current_target
= default_target
;
7815 lang_statement_iteration
++;
7816 open_input_bfds (statement_list
.head
, OPEN_BFD_NORMAL
);
7817 /* open_input_bfds also handles assignments, so we can give values
7818 to symbolic origin/length now. */
7819 lang_do_memory_regions ();
7821 #ifdef ENABLE_PLUGINS
7822 if (link_info
.lto_plugin_active
)
7824 lang_statement_list_type added
;
7825 lang_statement_list_type files
, inputfiles
;
7827 /* Now all files are read, let the plugin(s) decide if there
7828 are any more to be added to the link before we call the
7829 emulation's after_open hook. We create a private list of
7830 input statements for this purpose, which we will eventually
7831 insert into the global statement list after the first claimed
7834 /* We need to manipulate all three chains in synchrony. */
7836 inputfiles
= input_file_chain
;
7837 if (plugin_call_all_symbols_read ())
7838 einfo (_("%F%P: %s: plugin reported error after all symbols read\n"),
7839 plugin_error_plugin ());
7840 /* Open any newly added files, updating the file chains. */
7841 plugin_undefs
= link_info
.hash
->undefs_tail
;
7842 open_input_bfds (*added
.tail
, OPEN_BFD_NORMAL
);
7843 if (plugin_undefs
== link_info
.hash
->undefs_tail
)
7844 plugin_undefs
= NULL
;
7845 /* Restore the global list pointer now they have all been added. */
7846 lang_list_remove_tail (stat_ptr
, &added
);
7847 /* And detach the fresh ends of the file lists. */
7848 lang_list_remove_tail (&file_chain
, &files
);
7849 lang_list_remove_tail (&input_file_chain
, &inputfiles
);
7850 /* Were any new files added? */
7851 if (added
.head
!= NULL
)
7853 /* If so, we will insert them into the statement list immediately
7854 after the first input file that was claimed by the plugin,
7855 unless that file was an archive in which case it is inserted
7856 immediately before. */
7858 lang_statement_union_type
**prev
;
7859 plugin_insert
= find_replacements_insert_point (&before
);
7860 /* If a plugin adds input files without having claimed any, we
7861 don't really have a good idea where to place them. Just putting
7862 them at the start or end of the list is liable to leave them
7863 outside the crtbegin...crtend range. */
7864 ASSERT (plugin_insert
!= NULL
);
7865 /* Splice the new statement list into the old one. */
7866 prev
= &plugin_insert
->header
.next
;
7869 prev
= find_next_input_statement (prev
);
7870 if (*prev
!= (void *) plugin_insert
->next_real_file
)
7872 /* We didn't find the expected input statement.
7873 Fall back to adding after plugin_insert. */
7874 prev
= &plugin_insert
->header
.next
;
7877 lang_list_insert_after (stat_ptr
, &added
, prev
);
7878 /* Likewise for the file chains. */
7879 lang_list_insert_after (&input_file_chain
, &inputfiles
,
7880 (void *) &plugin_insert
->next_real_file
);
7881 /* We must be careful when relinking file_chain; we may need to
7882 insert the new files at the head of the list if the insert
7883 point chosen is the dummy first input file. */
7884 if (plugin_insert
->filename
)
7885 lang_list_insert_after (&file_chain
, &files
,
7886 (void *) &plugin_insert
->next
);
7888 lang_list_insert_after (&file_chain
, &files
, &file_chain
.head
);
7890 /* Rescan archives in case new undefined symbols have appeared. */
7892 lang_statement_iteration
++;
7893 open_input_bfds (statement_list
.head
, OPEN_BFD_RESCAN
);
7894 lang_list_remove_tail (&file_chain
, &files
);
7895 while (files
.head
!= NULL
)
7897 lang_input_statement_type
**insert
;
7898 lang_input_statement_type
**iter
, *temp
;
7901 insert
= find_rescan_insertion (&files
.head
->input_statement
);
7902 /* All elements from an archive can be added at once. */
7903 iter
= &files
.head
->input_statement
.next
;
7904 my_arch
= files
.head
->input_statement
.the_bfd
->my_archive
;
7905 if (my_arch
!= NULL
)
7906 for (; *iter
!= NULL
; iter
= &(*iter
)->next
)
7907 if ((*iter
)->the_bfd
->my_archive
!= my_arch
)
7910 *insert
= &files
.head
->input_statement
;
7911 files
.head
= (lang_statement_union_type
*) *iter
;
7913 if (my_arch
!= NULL
)
7915 lang_input_statement_type
*parent
= bfd_usrdata (my_arch
);
7917 parent
->next
= (lang_input_statement_type
*)
7919 - offsetof (lang_input_statement_type
, next
));
7924 #endif /* ENABLE_PLUGINS */
7926 /* Make sure that nobody has tried to add a symbol to this list
7928 ASSERT (link_info
.gc_sym_list
== NULL
);
7930 link_info
.gc_sym_list
= &entry_symbol
;
7932 if (entry_symbol
.name
== NULL
)
7934 link_info
.gc_sym_list
= ldlang_undef_chain_list_head
;
7936 /* entry_symbol is normally initialied by a ENTRY definition in the
7937 linker script or the -e command line option. But if neither of
7938 these have been used, the target specific backend may still have
7939 provided an entry symbol via a call to lang_default_entry().
7940 Unfortunately this value will not be processed until lang_end()
7941 is called, long after this function has finished. So detect this
7942 case here and add the target's entry symbol to the list of starting
7943 points for garbage collection resolution. */
7944 lang_add_gc_name (entry_symbol_default
);
7947 lang_add_gc_name (link_info
.init_function
);
7948 lang_add_gc_name (link_info
.fini_function
);
7950 ldemul_after_open ();
7951 if (config
.map_file
!= NULL
)
7952 lang_print_asneeded ();
7956 bfd_section_already_linked_table_free ();
7958 /* Make sure that we're not mixing architectures. We call this
7959 after all the input files have been opened, but before we do any
7960 other processing, so that any operations merge_private_bfd_data
7961 does on the output file will be known during the rest of the
7965 /* Handle .exports instead of a version script if we're told to do so. */
7966 if (command_line
.version_exports_section
)
7967 lang_do_version_exports_section ();
7969 /* Build all sets based on the information gathered from the input
7971 ldctor_build_sets ();
7973 /* Give initial values for __start and __stop symbols, so that ELF
7974 gc_sections will keep sections referenced by these symbols. Must
7975 be done before lang_do_assignments below. */
7976 if (config
.build_constructors
)
7977 lang_init_start_stop ();
7979 /* PR 13683: We must rerun the assignments prior to running garbage
7980 collection in order to make sure that all symbol aliases are resolved. */
7981 lang_do_assignments (lang_mark_phase_enum
);
7982 expld
.phase
= lang_first_phase_enum
;
7984 /* Size up the common data. */
7987 /* Remove unreferenced sections if asked to. */
7988 lang_gc_sections ();
7990 /* Check relocations. */
7991 lang_check_relocs ();
7993 ldemul_after_check_relocs ();
7995 /* Update wild statements. */
7996 update_wild_statements (statement_list
.head
);
7998 /* Run through the contours of the script and attach input sections
7999 to the correct output sections. */
8000 lang_statement_iteration
++;
8001 map_input_to_output_sections (statement_list
.head
, NULL
, NULL
);
8003 /* Start at the statement immediately after the special abs_section
8004 output statement, so that it isn't reordered. */
8005 process_insert_statements (&lang_os_list
.head
->header
.next
);
8007 ldemul_before_place_orphans ();
8009 /* Find any sections not attached explicitly and handle them. */
8010 lang_place_orphans ();
8012 if (!bfd_link_relocatable (&link_info
))
8016 /* Merge SEC_MERGE sections. This has to be done after GC of
8017 sections, so that GCed sections are not merged, but before
8018 assigning dynamic symbols, since removing whole input sections
8020 bfd_merge_sections (link_info
.output_bfd
, &link_info
);
8022 /* Look for a text section and set the readonly attribute in it. */
8023 found
= bfd_get_section_by_name (link_info
.output_bfd
, ".text");
8027 if (config
.text_read_only
)
8028 found
->flags
|= SEC_READONLY
;
8030 found
->flags
&= ~SEC_READONLY
;
8034 /* Merge together CTF sections. After this, only the symtab-dependent
8035 function and data object sections need adjustment. */
8038 /* Emit the CTF, iff the emulation doesn't need to do late emission after
8039 examining things laid out late, like the strtab. */
8042 /* Copy forward lma regions for output sections in same lma region. */
8043 lang_propagate_lma_regions ();
8045 /* Defining __start/__stop symbols early for --gc-sections to work
8046 around a glibc build problem can result in these symbols being
8047 defined when they should not be. Fix them now. */
8048 if (config
.build_constructors
)
8049 lang_undef_start_stop ();
8051 /* Define .startof./.sizeof. symbols with preliminary values before
8052 dynamic symbols are created. */
8053 if (!bfd_link_relocatable (&link_info
))
8054 lang_init_startof_sizeof ();
8056 /* Do anything special before sizing sections. This is where ELF
8057 and other back-ends size dynamic sections. */
8058 ldemul_before_allocation ();
8060 /* We must record the program headers before we try to fix the
8061 section positions, since they will affect SIZEOF_HEADERS. */
8062 lang_record_phdrs ();
8064 /* Check relro sections. */
8065 if (link_info
.relro
&& !bfd_link_relocatable (&link_info
))
8066 lang_find_relro_sections ();
8068 /* Size up the sections. */
8069 lang_size_sections (NULL
, !RELAXATION_ENABLED
);
8071 /* See if anything special should be done now we know how big
8072 everything is. This is where relaxation is done. */
8073 ldemul_after_allocation ();
8075 /* Fix any __start, __stop, .startof. or .sizeof. symbols. */
8076 lang_finalize_start_stop ();
8078 /* Do all the assignments again, to report errors. Assignment
8079 statements are processed multiple times, updating symbols; In
8080 open_input_bfds, lang_do_assignments, and lang_size_sections.
8081 Since lang_relax_sections calls lang_do_assignments, symbols are
8082 also updated in ldemul_after_allocation. */
8083 lang_do_assignments (lang_final_phase_enum
);
8087 /* Convert absolute symbols to section relative. */
8088 ldexp_finalize_syms ();
8090 /* Make sure that the section addresses make sense. */
8091 if (command_line
.check_section_addresses
)
8092 lang_check_section_addresses ();
8094 /* Check any required symbols are known. */
8095 ldlang_check_require_defined_symbols ();
8100 /* EXPORTED TO YACC */
8103 lang_add_wild (struct wildcard_spec
*filespec
,
8104 struct wildcard_list
*section_list
,
8105 bfd_boolean keep_sections
)
8107 struct wildcard_list
*curr
, *next
;
8108 lang_wild_statement_type
*new_stmt
;
8110 /* Reverse the list as the parser puts it back to front. */
8111 for (curr
= section_list
, section_list
= NULL
;
8113 section_list
= curr
, curr
= next
)
8116 curr
->next
= section_list
;
8119 if (filespec
!= NULL
&& filespec
->name
!= NULL
)
8121 if (strcmp (filespec
->name
, "*") == 0)
8122 filespec
->name
= NULL
;
8123 else if (!wildcardp (filespec
->name
))
8124 lang_has_input_file
= TRUE
;
8127 new_stmt
= new_stat (lang_wild_statement
, stat_ptr
);
8128 new_stmt
->filename
= NULL
;
8129 new_stmt
->filenames_sorted
= FALSE
;
8130 new_stmt
->section_flag_list
= NULL
;
8131 new_stmt
->exclude_name_list
= NULL
;
8132 if (filespec
!= NULL
)
8134 new_stmt
->filename
= filespec
->name
;
8135 new_stmt
->filenames_sorted
= filespec
->sorted
== by_name
;
8136 new_stmt
->section_flag_list
= filespec
->section_flag_list
;
8137 new_stmt
->exclude_name_list
= filespec
->exclude_name_list
;
8139 new_stmt
->section_list
= section_list
;
8140 new_stmt
->keep_sections
= keep_sections
;
8141 lang_list_init (&new_stmt
->children
);
8142 analyze_walk_wild_section_handler (new_stmt
);
8146 lang_section_start (const char *name
, etree_type
*address
,
8147 const segment_type
*segment
)
8149 lang_address_statement_type
*ad
;
8151 ad
= new_stat (lang_address_statement
, stat_ptr
);
8152 ad
->section_name
= name
;
8153 ad
->address
= address
;
8154 ad
->segment
= segment
;
8157 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
8158 because of a -e argument on the command line, or zero if this is
8159 called by ENTRY in a linker script. Command line arguments take
8163 lang_add_entry (const char *name
, bfd_boolean cmdline
)
8165 if (entry_symbol
.name
== NULL
8167 || !entry_from_cmdline
)
8169 entry_symbol
.name
= name
;
8170 entry_from_cmdline
= cmdline
;
8174 /* Set the default start symbol to NAME. .em files should use this,
8175 not lang_add_entry, to override the use of "start" if neither the
8176 linker script nor the command line specifies an entry point. NAME
8177 must be permanently allocated. */
8179 lang_default_entry (const char *name
)
8181 entry_symbol_default
= name
;
8185 lang_add_target (const char *name
)
8187 lang_target_statement_type
*new_stmt
;
8189 new_stmt
= new_stat (lang_target_statement
, stat_ptr
);
8190 new_stmt
->target
= name
;
8194 lang_add_map (const char *name
)
8201 map_option_f
= TRUE
;
8209 lang_add_fill (fill_type
*fill
)
8211 lang_fill_statement_type
*new_stmt
;
8213 new_stmt
= new_stat (lang_fill_statement
, stat_ptr
);
8214 new_stmt
->fill
= fill
;
8218 lang_add_data (int type
, union etree_union
*exp
)
8220 lang_data_statement_type
*new_stmt
;
8222 new_stmt
= new_stat (lang_data_statement
, stat_ptr
);
8223 new_stmt
->exp
= exp
;
8224 new_stmt
->type
= type
;
8227 /* Create a new reloc statement. RELOC is the BFD relocation type to
8228 generate. HOWTO is the corresponding howto structure (we could
8229 look this up, but the caller has already done so). SECTION is the
8230 section to generate a reloc against, or NAME is the name of the
8231 symbol to generate a reloc against. Exactly one of SECTION and
8232 NAME must be NULL. ADDEND is an expression for the addend. */
8235 lang_add_reloc (bfd_reloc_code_real_type reloc
,
8236 reloc_howto_type
*howto
,
8239 union etree_union
*addend
)
8241 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
8245 p
->section
= section
;
8247 p
->addend_exp
= addend
;
8249 p
->addend_value
= 0;
8250 p
->output_section
= NULL
;
8251 p
->output_offset
= 0;
8254 lang_assignment_statement_type
*
8255 lang_add_assignment (etree_type
*exp
)
8257 lang_assignment_statement_type
*new_stmt
;
8259 new_stmt
= new_stat (lang_assignment_statement
, stat_ptr
);
8260 new_stmt
->exp
= exp
;
8265 lang_add_attribute (enum statement_enum attribute
)
8267 new_statement (attribute
, sizeof (lang_statement_header_type
), stat_ptr
);
8271 lang_startup (const char *name
)
8273 if (first_file
->filename
!= NULL
)
8275 einfo (_("%F%P: multiple STARTUP files\n"));
8277 first_file
->filename
= name
;
8278 first_file
->local_sym_name
= name
;
8279 first_file
->flags
.real
= TRUE
;
8283 lang_float (bfd_boolean maybe
)
8285 lang_float_flag
= maybe
;
8289 /* Work out the load- and run-time regions from a script statement, and
8290 store them in *LMA_REGION and *REGION respectively.
8292 MEMSPEC is the name of the run-time region, or the value of
8293 DEFAULT_MEMORY_REGION if the statement didn't specify one.
8294 LMA_MEMSPEC is the name of the load-time region, or null if the
8295 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
8296 had an explicit load address.
8298 It is an error to specify both a load region and a load address. */
8301 lang_get_regions (lang_memory_region_type
**region
,
8302 lang_memory_region_type
**lma_region
,
8303 const char *memspec
,
8304 const char *lma_memspec
,
8305 bfd_boolean have_lma
,
8306 bfd_boolean have_vma
)
8308 *lma_region
= lang_memory_region_lookup (lma_memspec
, FALSE
);
8310 /* If no runtime region or VMA has been specified, but the load region
8311 has been specified, then use the load region for the runtime region
8313 if (lma_memspec
!= NULL
8315 && strcmp (memspec
, DEFAULT_MEMORY_REGION
) == 0)
8316 *region
= *lma_region
;
8318 *region
= lang_memory_region_lookup (memspec
, FALSE
);
8320 if (have_lma
&& lma_memspec
!= 0)
8321 einfo (_("%X%P:%pS: section has both a load address and a load region\n"),
8326 lang_leave_output_section_statement (fill_type
*fill
, const char *memspec
,
8327 lang_output_section_phdr_list
*phdrs
,
8328 const char *lma_memspec
)
8330 lang_get_regions (¤t_section
->region
,
8331 ¤t_section
->lma_region
,
8332 memspec
, lma_memspec
,
8333 current_section
->load_base
!= NULL
,
8334 current_section
->addr_tree
!= NULL
);
8336 current_section
->fill
= fill
;
8337 current_section
->phdrs
= phdrs
;
8341 /* Set the output format type. -oformat overrides scripts. */
8344 lang_add_output_format (const char *format
,
8349 if (output_target
== NULL
|| !from_script
)
8351 if (command_line
.endian
== ENDIAN_BIG
8354 else if (command_line
.endian
== ENDIAN_LITTLE
8358 output_target
= format
;
8363 lang_add_insert (const char *where
, int is_before
)
8365 lang_insert_statement_type
*new_stmt
;
8367 new_stmt
= new_stat (lang_insert_statement
, stat_ptr
);
8368 new_stmt
->where
= where
;
8369 new_stmt
->is_before
= is_before
;
8370 saved_script_handle
= previous_script_handle
;
8373 /* Enter a group. This creates a new lang_group_statement, and sets
8374 stat_ptr to build new statements within the group. */
8377 lang_enter_group (void)
8379 lang_group_statement_type
*g
;
8381 g
= new_stat (lang_group_statement
, stat_ptr
);
8382 lang_list_init (&g
->children
);
8383 push_stat_ptr (&g
->children
);
8386 /* Leave a group. This just resets stat_ptr to start writing to the
8387 regular list of statements again. Note that this will not work if
8388 groups can occur inside anything else which can adjust stat_ptr,
8389 but currently they can't. */
8392 lang_leave_group (void)
8397 /* Add a new program header. This is called for each entry in a PHDRS
8398 command in a linker script. */
8401 lang_new_phdr (const char *name
,
8403 bfd_boolean filehdr
,
8408 struct lang_phdr
*n
, **pp
;
8411 n
= stat_alloc (sizeof (struct lang_phdr
));
8414 n
->type
= exp_get_vma (type
, 0, "program header type");
8415 n
->filehdr
= filehdr
;
8420 hdrs
= n
->type
== 1 && (phdrs
|| filehdr
);
8422 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
8425 && !((*pp
)->filehdr
|| (*pp
)->phdrs
))
8427 einfo (_("%X%P:%pS: PHDRS and FILEHDR are not supported"
8428 " when prior PT_LOAD headers lack them\n"), NULL
);
8435 /* Record the program header information in the output BFD. FIXME: We
8436 should not be calling an ELF specific function here. */
8439 lang_record_phdrs (void)
8443 lang_output_section_phdr_list
*last
;
8444 struct lang_phdr
*l
;
8445 lang_output_section_statement_type
*os
;
8448 secs
= (asection
**) xmalloc (alc
* sizeof (asection
*));
8451 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
8458 for (os
= (void *) lang_os_list
.head
;
8462 lang_output_section_phdr_list
*pl
;
8464 if (os
->constraint
< 0)
8472 if (os
->sectype
== noload_section
8473 || os
->bfd_section
== NULL
8474 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
8477 /* Don't add orphans to PT_INTERP header. */
8483 lang_output_section_statement_type
*tmp_os
;
8485 /* If we have not run across a section with a program
8486 header assigned to it yet, then scan forwards to find
8487 one. This prevents inconsistencies in the linker's
8488 behaviour when a script has specified just a single
8489 header and there are sections in that script which are
8490 not assigned to it, and which occur before the first
8491 use of that header. See here for more details:
8492 http://sourceware.org/ml/binutils/2007-02/msg00291.html */
8493 for (tmp_os
= os
; tmp_os
; tmp_os
= tmp_os
->next
)
8496 last
= tmp_os
->phdrs
;
8500 einfo (_("%F%P: no sections assigned to phdrs\n"));
8505 if (os
->bfd_section
== NULL
)
8508 for (; pl
!= NULL
; pl
= pl
->next
)
8510 if (strcmp (pl
->name
, l
->name
) == 0)
8515 secs
= (asection
**) xrealloc (secs
,
8516 alc
* sizeof (asection
*));
8518 secs
[c
] = os
->bfd_section
;
8525 if (l
->flags
== NULL
)
8528 flags
= exp_get_vma (l
->flags
, 0, "phdr flags");
8533 at
= exp_get_vma (l
->at
, 0, "phdr load address");
8535 if (!bfd_record_phdr (link_info
.output_bfd
, l
->type
,
8536 l
->flags
!= NULL
, flags
, l
->at
!= NULL
,
8537 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
8538 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
8543 /* Make sure all the phdr assignments succeeded. */
8544 for (os
= (void *) lang_os_list
.head
;
8548 lang_output_section_phdr_list
*pl
;
8550 if (os
->constraint
< 0
8551 || os
->bfd_section
== NULL
)
8554 for (pl
= os
->phdrs
;
8557 if (!pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
8558 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
8559 os
->name
, pl
->name
);
8563 /* Record a list of sections which may not be cross referenced. */
8566 lang_add_nocrossref (lang_nocrossref_type
*l
)
8568 struct lang_nocrossrefs
*n
;
8570 n
= (struct lang_nocrossrefs
*) xmalloc (sizeof *n
);
8571 n
->next
= nocrossref_list
;
8573 n
->onlyfirst
= FALSE
;
8574 nocrossref_list
= n
;
8576 /* Set notice_all so that we get informed about all symbols. */
8577 link_info
.notice_all
= TRUE
;
8580 /* Record a section that cannot be referenced from a list of sections. */
8583 lang_add_nocrossref_to (lang_nocrossref_type
*l
)
8585 lang_add_nocrossref (l
);
8586 nocrossref_list
->onlyfirst
= TRUE
;
8589 /* Overlay handling. We handle overlays with some static variables. */
8591 /* The overlay virtual address. */
8592 static etree_type
*overlay_vma
;
8593 /* And subsection alignment. */
8594 static etree_type
*overlay_subalign
;
8596 /* An expression for the maximum section size seen so far. */
8597 static etree_type
*overlay_max
;
8599 /* A list of all the sections in this overlay. */
8601 struct overlay_list
{
8602 struct overlay_list
*next
;
8603 lang_output_section_statement_type
*os
;
8606 static struct overlay_list
*overlay_list
;
8608 /* Start handling an overlay. */
8611 lang_enter_overlay (etree_type
*vma_expr
, etree_type
*subalign
)
8613 /* The grammar should prevent nested overlays from occurring. */
8614 ASSERT (overlay_vma
== NULL
8615 && overlay_subalign
== NULL
8616 && overlay_max
== NULL
);
8618 overlay_vma
= vma_expr
;
8619 overlay_subalign
= subalign
;
8622 /* Start a section in an overlay. We handle this by calling
8623 lang_enter_output_section_statement with the correct VMA.
8624 lang_leave_overlay sets up the LMA and memory regions. */
8627 lang_enter_overlay_section (const char *name
)
8629 struct overlay_list
*n
;
8632 lang_enter_output_section_statement (name
, overlay_vma
, overlay_section
,
8633 0, overlay_subalign
, 0, 0, 0);
8635 /* If this is the first section, then base the VMA of future
8636 sections on this one. This will work correctly even if `.' is
8637 used in the addresses. */
8638 if (overlay_list
== NULL
)
8639 overlay_vma
= exp_nameop (ADDR
, name
);
8641 /* Remember the section. */
8642 n
= (struct overlay_list
*) xmalloc (sizeof *n
);
8643 n
->os
= current_section
;
8644 n
->next
= overlay_list
;
8647 size
= exp_nameop (SIZEOF
, name
);
8649 /* Arrange to work out the maximum section end address. */
8650 if (overlay_max
== NULL
)
8653 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
8656 /* Finish a section in an overlay. There isn't any special to do
8660 lang_leave_overlay_section (fill_type
*fill
,
8661 lang_output_section_phdr_list
*phdrs
)
8668 name
= current_section
->name
;
8670 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
8671 region and that no load-time region has been specified. It doesn't
8672 really matter what we say here, since lang_leave_overlay will
8674 lang_leave_output_section_statement (fill
, DEFAULT_MEMORY_REGION
, phdrs
, 0);
8676 /* Define the magic symbols. */
8678 clean
= (char *) xmalloc (strlen (name
) + 1);
8680 for (s1
= name
; *s1
!= '\0'; s1
++)
8681 if (ISALNUM (*s1
) || *s1
== '_')
8685 buf
= (char *) xmalloc (strlen (clean
) + sizeof "__load_start_");
8686 sprintf (buf
, "__load_start_%s", clean
);
8687 lang_add_assignment (exp_provide (buf
,
8688 exp_nameop (LOADADDR
, name
),
8691 buf
= (char *) xmalloc (strlen (clean
) + sizeof "__load_stop_");
8692 sprintf (buf
, "__load_stop_%s", clean
);
8693 lang_add_assignment (exp_provide (buf
,
8695 exp_nameop (LOADADDR
, name
),
8696 exp_nameop (SIZEOF
, name
)),
8702 /* Finish an overlay. If there are any overlay wide settings, this
8703 looks through all the sections in the overlay and sets them. */
8706 lang_leave_overlay (etree_type
*lma_expr
,
8709 const char *memspec
,
8710 lang_output_section_phdr_list
*phdrs
,
8711 const char *lma_memspec
)
8713 lang_memory_region_type
*region
;
8714 lang_memory_region_type
*lma_region
;
8715 struct overlay_list
*l
;
8716 lang_nocrossref_type
*nocrossref
;
8718 lang_get_regions (®ion
, &lma_region
,
8719 memspec
, lma_memspec
,
8720 lma_expr
!= NULL
, FALSE
);
8724 /* After setting the size of the last section, set '.' to end of the
8726 if (overlay_list
!= NULL
)
8728 overlay_list
->os
->update_dot
= 1;
8729 overlay_list
->os
->update_dot_tree
8730 = exp_assign (".", exp_binop ('+', overlay_vma
, overlay_max
), FALSE
);
8736 struct overlay_list
*next
;
8738 if (fill
!= NULL
&& l
->os
->fill
== NULL
)
8741 l
->os
->region
= region
;
8742 l
->os
->lma_region
= lma_region
;
8744 /* The first section has the load address specified in the
8745 OVERLAY statement. The rest are worked out from that.
8746 The base address is not needed (and should be null) if
8747 an LMA region was specified. */
8750 l
->os
->load_base
= lma_expr
;
8751 l
->os
->sectype
= first_overlay_section
;
8753 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
8754 l
->os
->phdrs
= phdrs
;
8758 lang_nocrossref_type
*nc
;
8760 nc
= (lang_nocrossref_type
*) xmalloc (sizeof *nc
);
8761 nc
->name
= l
->os
->name
;
8762 nc
->next
= nocrossref
;
8771 if (nocrossref
!= NULL
)
8772 lang_add_nocrossref (nocrossref
);
8775 overlay_list
= NULL
;
8777 overlay_subalign
= NULL
;
8780 /* Version handling. This is only useful for ELF. */
8782 /* If PREV is NULL, return first version pattern matching particular symbol.
8783 If PREV is non-NULL, return first version pattern matching particular
8784 symbol after PREV (previously returned by lang_vers_match). */
8786 static struct bfd_elf_version_expr
*
8787 lang_vers_match (struct bfd_elf_version_expr_head
*head
,
8788 struct bfd_elf_version_expr
*prev
,
8792 const char *cxx_sym
= sym
;
8793 const char *java_sym
= sym
;
8794 struct bfd_elf_version_expr
*expr
= NULL
;
8795 enum demangling_styles curr_style
;
8797 curr_style
= CURRENT_DEMANGLING_STYLE
;
8798 cplus_demangle_set_style (no_demangling
);
8799 c_sym
= bfd_demangle (link_info
.output_bfd
, sym
, DMGL_NO_OPTS
);
8802 cplus_demangle_set_style (curr_style
);
8804 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
8806 cxx_sym
= bfd_demangle (link_info
.output_bfd
, sym
,
8807 DMGL_PARAMS
| DMGL_ANSI
);
8811 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
8813 java_sym
= bfd_demangle (link_info
.output_bfd
, sym
, DMGL_JAVA
);
8818 if (head
->htab
&& (prev
== NULL
|| prev
->literal
))
8820 struct bfd_elf_version_expr e
;
8822 switch (prev
? prev
->mask
: 0)
8825 if (head
->mask
& BFD_ELF_VERSION_C_TYPE
)
8828 expr
= (struct bfd_elf_version_expr
*)
8829 htab_find ((htab_t
) head
->htab
, &e
);
8830 while (expr
&& strcmp (expr
->pattern
, c_sym
) == 0)
8831 if (expr
->mask
== BFD_ELF_VERSION_C_TYPE
)
8837 case BFD_ELF_VERSION_C_TYPE
:
8838 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
8840 e
.pattern
= cxx_sym
;
8841 expr
= (struct bfd_elf_version_expr
*)
8842 htab_find ((htab_t
) head
->htab
, &e
);
8843 while (expr
&& strcmp (expr
->pattern
, cxx_sym
) == 0)
8844 if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
8850 case BFD_ELF_VERSION_CXX_TYPE
:
8851 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
8853 e
.pattern
= java_sym
;
8854 expr
= (struct bfd_elf_version_expr
*)
8855 htab_find ((htab_t
) head
->htab
, &e
);
8856 while (expr
&& strcmp (expr
->pattern
, java_sym
) == 0)
8857 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
8868 /* Finally, try the wildcards. */
8869 if (prev
== NULL
|| prev
->literal
)
8870 expr
= head
->remaining
;
8873 for (; expr
; expr
= expr
->next
)
8880 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
8883 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
8885 else if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
8889 if (fnmatch (expr
->pattern
, s
, 0) == 0)
8895 free ((char *) c_sym
);
8897 free ((char *) cxx_sym
);
8898 if (java_sym
!= sym
)
8899 free ((char *) java_sym
);
8903 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
8904 return a pointer to the symbol name with any backslash quotes removed. */
8907 realsymbol (const char *pattern
)
8910 bfd_boolean changed
= FALSE
, backslash
= FALSE
;
8911 char *s
, *symbol
= (char *) xmalloc (strlen (pattern
) + 1);
8913 for (p
= pattern
, s
= symbol
; *p
!= '\0'; ++p
)
8915 /* It is a glob pattern only if there is no preceding
8919 /* Remove the preceding backslash. */
8926 if (*p
== '?' || *p
== '*' || *p
== '[')
8933 backslash
= *p
== '\\';
8949 /* This is called for each variable name or match expression. NEW_NAME is
8950 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
8951 pattern to be matched against symbol names. */
8953 struct bfd_elf_version_expr
*
8954 lang_new_vers_pattern (struct bfd_elf_version_expr
*orig
,
8955 const char *new_name
,
8957 bfd_boolean literal_p
)
8959 struct bfd_elf_version_expr
*ret
;
8961 ret
= (struct bfd_elf_version_expr
*) xmalloc (sizeof *ret
);
8965 ret
->literal
= TRUE
;
8966 ret
->pattern
= literal_p
? new_name
: realsymbol (new_name
);
8967 if (ret
->pattern
== NULL
)
8969 ret
->pattern
= new_name
;
8970 ret
->literal
= FALSE
;
8973 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
8974 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
8975 else if (strcasecmp (lang
, "C++") == 0)
8976 ret
->mask
= BFD_ELF_VERSION_CXX_TYPE
;
8977 else if (strcasecmp (lang
, "Java") == 0)
8978 ret
->mask
= BFD_ELF_VERSION_JAVA_TYPE
;
8981 einfo (_("%X%P: unknown language `%s' in version information\n"),
8983 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
8986 return ldemul_new_vers_pattern (ret
);
8989 /* This is called for each set of variable names and match
8992 struct bfd_elf_version_tree
*
8993 lang_new_vers_node (struct bfd_elf_version_expr
*globals
,
8994 struct bfd_elf_version_expr
*locals
)
8996 struct bfd_elf_version_tree
*ret
;
8998 ret
= (struct bfd_elf_version_tree
*) xcalloc (1, sizeof *ret
);
8999 ret
->globals
.list
= globals
;
9000 ret
->locals
.list
= locals
;
9001 ret
->match
= lang_vers_match
;
9002 ret
->name_indx
= (unsigned int) -1;
9006 /* This static variable keeps track of version indices. */
9008 static int version_index
;
9011 version_expr_head_hash (const void *p
)
9013 const struct bfd_elf_version_expr
*e
=
9014 (const struct bfd_elf_version_expr
*) p
;
9016 return htab_hash_string (e
->pattern
);
9020 version_expr_head_eq (const void *p1
, const void *p2
)
9022 const struct bfd_elf_version_expr
*e1
=
9023 (const struct bfd_elf_version_expr
*) p1
;
9024 const struct bfd_elf_version_expr
*e2
=
9025 (const struct bfd_elf_version_expr
*) p2
;
9027 return strcmp (e1
->pattern
, e2
->pattern
) == 0;
9031 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head
*head
)
9034 struct bfd_elf_version_expr
*e
, *next
;
9035 struct bfd_elf_version_expr
**list_loc
, **remaining_loc
;
9037 for (e
= head
->list
; e
; e
= e
->next
)
9041 head
->mask
|= e
->mask
;
9046 head
->htab
= htab_create (count
* 2, version_expr_head_hash
,
9047 version_expr_head_eq
, NULL
);
9048 list_loc
= &head
->list
;
9049 remaining_loc
= &head
->remaining
;
9050 for (e
= head
->list
; e
; e
= next
)
9056 remaining_loc
= &e
->next
;
9060 void **loc
= htab_find_slot ((htab_t
) head
->htab
, e
, INSERT
);
9064 struct bfd_elf_version_expr
*e1
, *last
;
9066 e1
= (struct bfd_elf_version_expr
*) *loc
;
9070 if (e1
->mask
== e
->mask
)
9078 while (e1
&& strcmp (e1
->pattern
, e
->pattern
) == 0);
9082 /* This is a duplicate. */
9083 /* FIXME: Memory leak. Sometimes pattern is not
9084 xmalloced alone, but in larger chunk of memory. */
9085 /* free (e->pattern); */
9090 e
->next
= last
->next
;
9098 list_loc
= &e
->next
;
9102 *remaining_loc
= NULL
;
9103 *list_loc
= head
->remaining
;
9106 head
->remaining
= head
->list
;
9109 /* This is called when we know the name and dependencies of the
9113 lang_register_vers_node (const char *name
,
9114 struct bfd_elf_version_tree
*version
,
9115 struct bfd_elf_version_deps
*deps
)
9117 struct bfd_elf_version_tree
*t
, **pp
;
9118 struct bfd_elf_version_expr
*e1
;
9123 if (link_info
.version_info
!= NULL
9124 && (name
[0] == '\0' || link_info
.version_info
->name
[0] == '\0'))
9126 einfo (_("%X%P: anonymous version tag cannot be combined"
9127 " with other version tags\n"));
9132 /* Make sure this node has a unique name. */
9133 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9134 if (strcmp (t
->name
, name
) == 0)
9135 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
9137 lang_finalize_version_expr_head (&version
->globals
);
9138 lang_finalize_version_expr_head (&version
->locals
);
9140 /* Check the global and local match names, and make sure there
9141 aren't any duplicates. */
9143 for (e1
= version
->globals
.list
; e1
!= NULL
; e1
= e1
->next
)
9145 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9147 struct bfd_elf_version_expr
*e2
;
9149 if (t
->locals
.htab
&& e1
->literal
)
9151 e2
= (struct bfd_elf_version_expr
*)
9152 htab_find ((htab_t
) t
->locals
.htab
, e1
);
9153 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
9155 if (e1
->mask
== e2
->mask
)
9156 einfo (_("%X%P: duplicate expression `%s'"
9157 " in version information\n"), e1
->pattern
);
9161 else if (!e1
->literal
)
9162 for (e2
= t
->locals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
9163 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
9164 && e1
->mask
== e2
->mask
)
9165 einfo (_("%X%P: duplicate expression `%s'"
9166 " in version information\n"), e1
->pattern
);
9170 for (e1
= version
->locals
.list
; e1
!= NULL
; e1
= e1
->next
)
9172 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9174 struct bfd_elf_version_expr
*e2
;
9176 if (t
->globals
.htab
&& e1
->literal
)
9178 e2
= (struct bfd_elf_version_expr
*)
9179 htab_find ((htab_t
) t
->globals
.htab
, e1
);
9180 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
9182 if (e1
->mask
== e2
->mask
)
9183 einfo (_("%X%P: duplicate expression `%s'"
9184 " in version information\n"),
9189 else if (!e1
->literal
)
9190 for (e2
= t
->globals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
9191 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
9192 && e1
->mask
== e2
->mask
)
9193 einfo (_("%X%P: duplicate expression `%s'"
9194 " in version information\n"), e1
->pattern
);
9198 version
->deps
= deps
;
9199 version
->name
= name
;
9200 if (name
[0] != '\0')
9203 version
->vernum
= version_index
;
9206 version
->vernum
= 0;
9208 for (pp
= &link_info
.version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
9213 /* This is called when we see a version dependency. */
9215 struct bfd_elf_version_deps
*
9216 lang_add_vers_depend (struct bfd_elf_version_deps
*list
, const char *name
)
9218 struct bfd_elf_version_deps
*ret
;
9219 struct bfd_elf_version_tree
*t
;
9221 ret
= (struct bfd_elf_version_deps
*) xmalloc (sizeof *ret
);
9224 for (t
= link_info
.version_info
; t
!= NULL
; t
= t
->next
)
9226 if (strcmp (t
->name
, name
) == 0)
9228 ret
->version_needed
= t
;
9233 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
9235 ret
->version_needed
= NULL
;
9240 lang_do_version_exports_section (void)
9242 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
9244 LANG_FOR_EACH_INPUT_STATEMENT (is
)
9246 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
9254 contents
= (char *) xmalloc (len
);
9255 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
9256 einfo (_("%X%P: unable to read .exports section contents\n"), sec
);
9259 while (p
< contents
+ len
)
9261 greg
= lang_new_vers_pattern (greg
, p
, NULL
, FALSE
);
9262 p
= strchr (p
, '\0') + 1;
9265 /* Do not free the contents, as we used them creating the regex. */
9267 /* Do not include this section in the link. */
9268 sec
->flags
|= SEC_EXCLUDE
| SEC_KEEP
;
9271 lreg
= lang_new_vers_pattern (NULL
, "*", NULL
, FALSE
);
9272 lang_register_vers_node (command_line
.version_exports_section
,
9273 lang_new_vers_node (greg
, lreg
), NULL
);
9276 /* Evaluate LENGTH and ORIGIN parts of MEMORY spec */
9279 lang_do_memory_regions (void)
9281 lang_memory_region_type
*r
= lang_memory_region_list
;
9283 for (; r
!= NULL
; r
= r
->next
)
9287 exp_fold_tree_no_dot (r
->origin_exp
);
9288 if (expld
.result
.valid_p
)
9290 r
->origin
= expld
.result
.value
;
9291 r
->current
= r
->origin
;
9294 einfo (_("%F%P: invalid origin for memory region %s\n"),
9299 exp_fold_tree_no_dot (r
->length_exp
);
9300 if (expld
.result
.valid_p
)
9301 r
->length
= expld
.result
.value
;
9303 einfo (_("%F%P: invalid length for memory region %s\n"),
9310 lang_add_unique (const char *name
)
9312 struct unique_sections
*ent
;
9314 for (ent
= unique_section_list
; ent
; ent
= ent
->next
)
9315 if (strcmp (ent
->name
, name
) == 0)
9318 ent
= (struct unique_sections
*) xmalloc (sizeof *ent
);
9319 ent
->name
= xstrdup (name
);
9320 ent
->next
= unique_section_list
;
9321 unique_section_list
= ent
;
9324 /* Append the list of dynamic symbols to the existing one. */
9327 lang_append_dynamic_list (struct bfd_elf_version_expr
*dynamic
)
9329 if (link_info
.dynamic_list
)
9331 struct bfd_elf_version_expr
*tail
;
9332 for (tail
= dynamic
; tail
->next
!= NULL
; tail
= tail
->next
)
9334 tail
->next
= link_info
.dynamic_list
->head
.list
;
9335 link_info
.dynamic_list
->head
.list
= dynamic
;
9339 struct bfd_elf_dynamic_list
*d
;
9341 d
= (struct bfd_elf_dynamic_list
*) xcalloc (1, sizeof *d
);
9342 d
->head
.list
= dynamic
;
9343 d
->match
= lang_vers_match
;
9344 link_info
.dynamic_list
= d
;
9348 /* Append the list of C++ typeinfo dynamic symbols to the existing
9352 lang_append_dynamic_list_cpp_typeinfo (void)
9354 const char *symbols
[] =
9356 "typeinfo name for*",
9359 struct bfd_elf_version_expr
*dynamic
= NULL
;
9362 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
9363 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
9366 lang_append_dynamic_list (dynamic
);
9369 /* Append the list of C++ operator new and delete dynamic symbols to the
9373 lang_append_dynamic_list_cpp_new (void)
9375 const char *symbols
[] =
9380 struct bfd_elf_version_expr
*dynamic
= NULL
;
9383 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
9384 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
9387 lang_append_dynamic_list (dynamic
);
9390 /* Scan a space and/or comma separated string of features. */
9393 lang_ld_feature (char *str
)
9401 while (*p
== ',' || ISSPACE (*p
))
9406 while (*q
&& *q
!= ',' && !ISSPACE (*q
))
9410 if (strcasecmp (p
, "SANE_EXPR") == 0)
9411 config
.sane_expr
= TRUE
;
9413 einfo (_("%X%P: unknown feature `%s'\n"), p
);
9419 /* Pretty print memory amount. */
9422 lang_print_memory_size (bfd_vma sz
)
9424 if ((sz
& 0x3fffffff) == 0)
9425 printf ("%10" BFD_VMA_FMT
"u GB", sz
>> 30);
9426 else if ((sz
& 0xfffff) == 0)
9427 printf ("%10" BFD_VMA_FMT
"u MB", sz
>> 20);
9428 else if ((sz
& 0x3ff) == 0)
9429 printf ("%10" BFD_VMA_FMT
"u KB", sz
>> 10);
9431 printf (" %10" BFD_VMA_FMT
"u B", sz
);
9434 /* Implement --print-memory-usage: disply per region memory usage. */
9437 lang_print_memory_usage (void)
9439 lang_memory_region_type
*r
;
9441 printf ("Memory region Used Size Region Size %%age Used\n");
9442 for (r
= lang_memory_region_list
; r
->next
!= NULL
; r
= r
->next
)
9444 bfd_vma used_length
= r
->current
- r
->origin
;
9446 printf ("%16s: ",r
->name_list
.name
);
9447 lang_print_memory_size (used_length
);
9448 lang_print_memory_size ((bfd_vma
) r
->length
);
9452 double percent
= used_length
* 100.0 / r
->length
;
9453 printf (" %6.2f%%", percent
);