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. */
31 #include "elf/internal.h"
35 ldelf_map_segments (bfd_boolean need_layout
)
41 lang_relax_sections (need_layout
);
44 if (link_info
.output_bfd
->xvec
->flavour
== bfd_target_elf_flavour
45 && !bfd_link_relocatable (&link_info
))
47 bfd_size_type phdr_size
;
49 phdr_size
= elf_program_header_size (link_info
.output_bfd
);
50 /* If we don't have user supplied phdrs, throw away any
51 previous linker generated program headers. */
52 if (lang_phdr_list
== NULL
)
53 elf_seg_map (link_info
.output_bfd
) = NULL
;
54 if (!_bfd_elf_map_sections_to_segments (link_info
.output_bfd
,
56 einfo (_("%F%P: map sections to segments failed: %E\n"));
58 if (phdr_size
!= elf_program_header_size (link_info
.output_bfd
))
61 /* The first few times we allow any change to
65 < elf_program_header_size (link_info
.output_bfd
))
66 /* After that we only allow the size to grow. */
69 elf_program_header_size (link_info
.output_bfd
) = phdr_size
;
73 while (need_layout
&& --tries
);
76 einfo (_("%F%P: looping in map_segments"));
78 if (link_info
.output_bfd
->xvec
->flavour
== bfd_target_elf_flavour
79 && lang_phdr_list
== NULL
)
81 /* If we don't have user supplied phdrs, strip zero-sized dynamic
82 sections and regenerate program headers. */
83 const struct elf_backend_data
*bed
84 = get_elf_backend_data (link_info
.output_bfd
);
85 if (bed
->elf_backend_strip_zero_sized_dynamic_sections
86 && !bed
->elf_backend_strip_zero_sized_dynamic_sections
88 einfo (_("%F%P: failed to strip zero-sized dynamic sections"));
93 /* We want to emit CTF early if and only if we are not targetting ELF with this
97 ldelf_emit_ctf_early (void)
99 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
)
104 /* Callbacks used to map from bfd types to libctf types, under libctf's
107 struct ctf_strtab_iter_cb_arg
109 struct elf_strtab_hash
*strtab
;
114 /* Return strings from the strtab to libctf, one by one. Returns NULL when
115 iteration is complete. */
118 ldelf_ctf_strtab_iter_cb (uint32_t *offset
, void *arg_
)
123 struct ctf_strtab_iter_cb_arg
*arg
=
124 (struct ctf_strtab_iter_cb_arg
*) arg_
;
126 /* There is no zeroth string. */
127 if (arg
->next_i
== 0)
130 if (arg
->next_i
>= _bfd_elf_strtab_len (arg
->strtab
))
136 ret
= _bfd_elf_strtab_str (arg
->strtab
, arg
->next_i
++, &off
);
139 /* If we've overflowed, we cannot share any further strings: the CTF
140 format cannot encode strings with such high offsets. */
148 ldelf_acquire_strings_for_ctf
149 (struct ctf_dict
*ctf_output
, struct elf_strtab_hash
*strtab
)
151 struct ctf_strtab_iter_cb_arg args
= { strtab
, 0, 0 };
155 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
)
157 if (ctf_link_add_strtab (ctf_output
, ldelf_ctf_strtab_iter_cb
,
159 einfo (_("%F%P: warning: CTF strtab association failed; strings will "
160 "not be shared: %s\n"),
161 ctf_errmsg (ctf_errno (ctf_output
)));
166 ldelf_new_dynsym_for_ctf (struct ctf_dict
*ctf_output
, int symidx
,
167 struct elf_internal_sym
*sym
)
178 lsym
.st_nameidx
= sym
->st_name
;
179 lsym
.st_nameidx_set
= 1;
180 lsym
.st_symidx
= symidx
;
181 lsym
.st_shndx
= sym
->st_shndx
;
182 lsym
.st_type
= ELF_ST_TYPE (sym
->st_info
);
183 lsym
.st_value
= sym
->st_value
;
184 if (ctf_link_add_linker_symbol (ctf_output
, &lsym
) < 0)
186 einfo (_("%F%P: warning: CTF symbol addition failed; CTF will "
187 "not be tied to symbols: %s\n"),
188 ctf_errmsg (ctf_errno (ctf_output
)));
193 /* Shuffle all the symbols. */
195 if (ctf_link_shuffle_syms (ctf_output
) < 0)
196 einfo (_("%F%P: warning: CTF symbol shuffling failed; CTF will "
197 "not be tied to symbols: %s\n"),
198 ctf_errmsg (ctf_errno (ctf_output
)));
203 ldelf_emit_ctf_early (void)
209 ldelf_acquire_strings_for_ctf (struct ctf_dict
*ctf_output ATTRIBUTE_UNUSED
,
210 struct elf_strtab_hash
*strtab ATTRIBUTE_UNUSED
)
213 ldelf_new_dynsym_for_ctf (struct ctf_dict
*ctf_output ATTRIBUTE_UNUSED
,
214 int symidx ATTRIBUTE_UNUSED
,
215 struct elf_internal_sym
*sym ATTRIBUTE_UNUSED
)