1 /* Emulation code used by all ELF targets.
2 Copyright (C) 1991-2021 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. */
32 #include "elf/internal.h"
35 /* Info attached to an output_section_statement about input sections,
36 used when sorting SHF_LINK_ORDER sections. */
40 /* Size allocated for isec. */
42 /* Used entries in isec. */
44 /* How many are SHF_LINK_ORDER. */
46 /* Input sections attached to this output section. */
47 struct os_sections_input
{
48 lang_input_section_type
*is
;
53 /* Add IS to data kept for OS. */
56 add_link_order_input_section (lang_input_section_type
*is
,
57 lang_output_section_statement_type
*os
)
59 struct os_sections
*os_info
= os
->data
;
64 os_info
= xmalloc (sizeof (*os_info
) + 63 * sizeof (*os_info
->isec
));
70 if (os_info
->count
== os_info
->alloc
)
74 want
= sizeof (*os_info
) + (os_info
->alloc
- 1) * sizeof (*os_info
->isec
);
75 os_info
= xrealloc (os_info
, want
);
78 os_info
->isec
[os_info
->count
].is
= is
;
79 os_info
->isec
[os_info
->count
].idx
= os_info
->count
;
82 if (bfd_get_flavour (s
->owner
) == bfd_target_elf_flavour
83 && (s
->flags
& SEC_LINKER_CREATED
) == 0
84 && elf_linked_to_section (s
) != NULL
)
89 /* Run over the linker's statement list, extracting info about input
90 sections attached to each output section. */
93 link_order_scan (lang_statement_union_type
*u
,
94 lang_output_section_statement_type
*os
)
97 bfd_boolean ret
= FALSE
;
99 for (; u
!= NULL
; u
= u
->header
.next
)
101 switch (u
->header
.type
)
103 case lang_wild_statement_enum
:
104 if (link_order_scan (u
->wild_statement
.children
.head
, os
))
107 case lang_constructors_statement_enum
:
108 if (link_order_scan (constructor_list
.head
, os
))
111 case lang_output_section_statement_enum
:
112 if (u
->output_section_statement
.constraint
!= -1
113 && link_order_scan (u
->output_section_statement
.children
.head
,
114 &u
->output_section_statement
))
117 case lang_group_statement_enum
:
118 if (link_order_scan (u
->group_statement
.children
.head
, os
))
121 case lang_input_section_enum
:
122 s
= u
->input_section
.section
;
123 if (s
->output_section
!= NULL
124 && s
->output_section
->owner
== link_info
.output_bfd
125 && (s
->output_section
->flags
& SEC_EXCLUDE
) == 0
126 && ((s
->output_section
->flags
& SEC_HAS_CONTENTS
) != 0
127 || ((s
->output_section
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
))
128 == (SEC_LOAD
| SEC_THREAD_LOCAL
))))
129 if (add_link_order_input_section (&u
->input_section
, os
))
139 /* Compare two sections based on the locations of the sections they are
140 linked to. Used by fixup_link_order. */
143 compare_link_order (const void *a
, const void *b
)
145 const struct os_sections_input
*ai
= a
;
146 const struct os_sections_input
*bi
= b
;
147 asection
*asec
= NULL
;
148 asection
*bsec
= NULL
;
151 if (bfd_get_flavour (ai
->is
->section
->owner
) == bfd_target_elf_flavour
)
152 asec
= elf_linked_to_section (ai
->is
->section
);
153 if (bfd_get_flavour (bi
->is
->section
->owner
) == bfd_target_elf_flavour
)
154 bsec
= elf_linked_to_section (bi
->is
->section
);
156 /* Place unordered sections before ordered sections. */
157 if (asec
== NULL
|| bsec
== NULL
)
161 else if (asec
!= NULL
)
163 return ai
->idx
- bi
->idx
;
166 apos
= asec
->output_section
->lma
+ asec
->output_offset
;
167 bpos
= bsec
->output_section
->lma
+ bsec
->output_offset
;
171 else if (apos
> bpos
)
174 /* The only way we should get matching LMAs is when the first of two
175 sections has zero size. */
176 if (asec
->size
< bsec
->size
)
178 else if (asec
->size
> bsec
->size
)
181 /* If they are both zero size then they almost certainly have the same
182 VMA and thus are not ordered with respect to each other. Test VMA
183 anyway, and fall back to id to make the result reproducible across
184 qsort implementations. */
185 apos
= asec
->output_section
->vma
+ asec
->output_offset
;
186 bpos
= bsec
->output_section
->vma
+ bsec
->output_offset
;
189 else if (apos
> bpos
)
192 return asec
->id
- bsec
->id
;
195 /* Rearrange sections with SHF_LINK_ORDER into the same order as their
199 fixup_link_order (lang_output_section_statement_type
*os
)
201 struct os_sections
*os_info
= os
->data
;
203 lang_input_section_type
**orig_is
;
206 for (i
= 0; i
< os_info
->count
; i
= j
)
208 /* Normally a linker script will select SHF_LINK_ORDER sections
209 with an input section wildcard something like the following:
210 *(.IA_64.unwind* .gnu.linkonce.ia64unw.*)
211 However if some other random sections are smashed into an
212 output section, or if SHF_LINK_ORDER are split up by the
213 linker script, then we only want to sort sections matching a
214 given wildcard. That's the purpose of the pattern test. */
215 for (j
= i
+ 1; j
< os_info
->count
; j
++)
216 if (os_info
->isec
[j
].is
->pattern
!= os_info
->isec
[i
].is
->pattern
)
219 qsort (&os_info
->isec
[i
], j
- i
, sizeof (*os_info
->isec
),
222 for (i
= 0; i
< os_info
->count
; i
++)
223 if (os_info
->isec
[i
].idx
!= i
)
225 if (i
== os_info
->count
)
228 /* Now reorder the linker input section statements to reflect the
229 proper sorting. The is done by rewriting the existing statements
230 rather than fiddling with lists, since the only thing we need to
231 change is the bfd section pointer. */
232 orig_is
= xmalloc (os_info
->count
* sizeof (*orig_is
));
233 save_s
= xmalloc (os_info
->count
* sizeof (*save_s
));
234 for (i
= 0; i
< os_info
->count
; i
++)
236 orig_is
[os_info
->isec
[i
].idx
] = os_info
->isec
[i
].is
;
237 save_s
[i
] = os_info
->isec
[i
].is
->section
;
239 for (i
= 0; i
< os_info
->count
; i
++)
240 if (os_info
->isec
[i
].idx
!= i
)
242 orig_is
[i
]->section
= save_s
[i
];
243 /* Restore os_info to pristine state before the qsort, for the
244 next pass over sections. */
245 os_info
->isec
[i
].is
= orig_is
[i
];
246 os_info
->isec
[i
].idx
= i
;
254 ldelf_map_segments (bfd_boolean need_layout
)
257 static bfd_boolean done_link_order_scan
= FALSE
;
261 lang_relax_sections (need_layout
);
264 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
)
266 lang_output_section_statement_type
*os
;
267 if (!done_link_order_scan
)
269 link_order_scan (statement_list
.head
, NULL
);
270 done_link_order_scan
= TRUE
;
272 for (os
= (void *) lang_os_list
.head
; os
!= NULL
; os
= os
->next
)
274 struct os_sections
*os_info
= os
->data
;
275 if (os_info
!= NULL
&& os_info
->ordered
!= 0)
277 if (os_info
->ordered
!= os_info
->count
278 && bfd_link_relocatable (&link_info
))
281 "%pA has both ordered and unordered sections"),
285 if (os_info
->count
> 1
286 && fixup_link_order (os
))
292 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
293 && !bfd_link_relocatable (&link_info
))
295 bfd_size_type phdr_size
;
297 phdr_size
= elf_program_header_size (link_info
.output_bfd
);
298 /* If we don't have user supplied phdrs, throw away any
299 previous linker generated program headers. */
300 if (lang_phdr_list
== NULL
)
301 elf_seg_map (link_info
.output_bfd
) = NULL
;
302 if (!_bfd_elf_map_sections_to_segments (link_info
.output_bfd
,
304 einfo (_("%F%P: map sections to segments failed: %E\n"));
306 if (phdr_size
!= elf_program_header_size (link_info
.output_bfd
))
309 /* The first few times we allow any change to
313 < elf_program_header_size (link_info
.output_bfd
))
314 /* After that we only allow the size to grow. */
317 elf_program_header_size (link_info
.output_bfd
) = phdr_size
;
321 while (need_layout
&& --tries
);
324 einfo (_("%F%P: looping in map_segments"));
326 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
327 && lang_phdr_list
== NULL
)
329 /* If we don't have user supplied phdrs, strip zero-sized dynamic
330 sections and regenerate program headers. */
331 const struct elf_backend_data
*bed
332 = get_elf_backend_data (link_info
.output_bfd
);
333 if (bed
->elf_backend_strip_zero_sized_dynamic_sections
334 && !bed
->elf_backend_strip_zero_sized_dynamic_sections
336 einfo (_("%F%P: failed to strip zero-sized dynamic sections"));
341 /* We want to emit CTF early if and only if we are not targetting ELF with this
345 ldelf_emit_ctf_early (void)
347 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
)
352 /* Callbacks used to map from bfd types to libctf types, under libctf's
355 struct ctf_strtab_iter_cb_arg
357 struct elf_strtab_hash
*strtab
;
362 /* Return strings from the strtab to libctf, one by one. Returns NULL when
363 iteration is complete. */
366 ldelf_ctf_strtab_iter_cb (uint32_t *offset
, void *arg_
)
371 struct ctf_strtab_iter_cb_arg
*arg
=
372 (struct ctf_strtab_iter_cb_arg
*) arg_
;
374 /* There is no zeroth string. */
375 if (arg
->next_i
== 0)
378 /* Hunt through strings until we fall off the end or find one with
379 a nonzero refcount. */
382 if (arg
->next_i
>= _bfd_elf_strtab_len (arg
->strtab
))
388 ret
= _bfd_elf_strtab_str (arg
->strtab
, arg
->next_i
++, &off
);
394 /* If we've overflowed, we cannot share any further strings: the CTF
395 format cannot encode strings with such high offsets. */
403 ldelf_acquire_strings_for_ctf
404 (struct ctf_dict
*ctf_output
, struct elf_strtab_hash
*strtab
)
406 struct ctf_strtab_iter_cb_arg args
= { strtab
, 0, 0 };
410 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
)
412 if (ctf_link_add_strtab (ctf_output
, ldelf_ctf_strtab_iter_cb
,
414 einfo (_("%F%P: warning: CTF strtab association failed; strings will "
415 "not be shared: %s\n"),
416 ctf_errmsg (ctf_errno (ctf_output
)));
421 ldelf_new_dynsym_for_ctf (struct ctf_dict
*ctf_output
, int symidx
,
422 struct elf_internal_sym
*sym
)
433 lsym
.st_nameidx
= sym
->st_name
;
434 lsym
.st_nameidx_set
= 1;
435 lsym
.st_symidx
= symidx
;
436 lsym
.st_shndx
= sym
->st_shndx
;
437 lsym
.st_type
= ELF_ST_TYPE (sym
->st_info
);
438 lsym
.st_value
= sym
->st_value
;
439 if (ctf_link_add_linker_symbol (ctf_output
, &lsym
) < 0)
441 einfo (_("%F%P: warning: CTF symbol addition failed; CTF will "
442 "not be tied to symbols: %s\n"),
443 ctf_errmsg (ctf_errno (ctf_output
)));
448 /* Shuffle all the symbols. */
450 if (ctf_link_shuffle_syms (ctf_output
) < 0)
451 einfo (_("%F%P: warning: CTF symbol shuffling failed; CTF will "
452 "not be tied to symbols: %s\n"),
453 ctf_errmsg (ctf_errno (ctf_output
)));
458 ldelf_emit_ctf_early (void)
464 ldelf_acquire_strings_for_ctf (struct ctf_dict
*ctf_output ATTRIBUTE_UNUSED
,
465 struct elf_strtab_hash
*strtab ATTRIBUTE_UNUSED
)
468 ldelf_new_dynsym_for_ctf (struct ctf_dict
*ctf_output ATTRIBUTE_UNUSED
,
469 int symidx ATTRIBUTE_UNUSED
,
470 struct elf_internal_sym
*sym ATTRIBUTE_UNUSED
)