1 /* Emulation code used by all ELF targets.
2 Copyright (C) 1991-2019 Free Software Foundation, Inc.
4 This file is part of the GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
34 ldelf_map_segments (bfd_boolean need_layout
)
40 lang_relax_sections (need_layout
);
43 if (link_info
.output_bfd
->xvec
->flavour
== bfd_target_elf_flavour
44 && !bfd_link_relocatable (&link_info
))
46 bfd_size_type phdr_size
;
48 phdr_size
= elf_program_header_size (link_info
.output_bfd
);
49 /* If we don't have user supplied phdrs, throw away any
50 previous linker generated program headers. */
51 if (lang_phdr_list
== NULL
)
52 elf_seg_map (link_info
.output_bfd
) = NULL
;
53 if (!_bfd_elf_map_sections_to_segments (link_info
.output_bfd
,
55 einfo (_("%F%P: map sections to segments failed: %E\n"));
57 if (phdr_size
!= elf_program_header_size (link_info
.output_bfd
))
60 /* The first few times we allow any change to
64 < elf_program_header_size (link_info
.output_bfd
))
65 /* After that we only allow the size to grow. */
68 elf_program_header_size (link_info
.output_bfd
) = phdr_size
;
72 while (need_layout
&& --tries
);
75 einfo (_("%F%P: looping in map_segments"));
78 /* We want to emit CTF early if and only if we are not targetting ELF with this
82 ldelf_emit_ctf_early (void)
84 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
)
89 /* Callbacks used to map from bfd types to libctf types, under libctf's
92 struct ctf_strsym_iter_cb_arg
94 struct elf_sym_strtab
*syms
;
95 bfd_size_type symcount
;
96 struct elf_strtab_hash
*symstrtab
;
101 /* Return strings from the strtab to libctf, one by one. Returns NULL when
102 iteration is complete. */
105 ldelf_ctf_strtab_iter_cb (uint32_t *offset
, void *arg_
)
110 struct ctf_strsym_iter_cb_arg
*arg
=
111 (struct ctf_strsym_iter_cb_arg
*) arg_
;
113 /* There is no zeroth string. */
114 if (arg
->next_i
== 0)
117 if (arg
->next_i
>= _bfd_elf_strtab_len (arg
->symstrtab
))
123 ret
= _bfd_elf_strtab_str (arg
->symstrtab
, arg
->next_i
++, &off
);
126 /* If we've overflowed, we cannot share any further strings: the CTF
127 format cannot encode strings with such high offsets. */
134 /* Return symbols from the symbol table to libctf, one by one. We assume (and
135 assert) that the symbols in the elf_link_hash_table are in strictly ascending
136 order, and that none will be added in between existing ones. Returns NULL
137 when iteration is complete. */
139 static struct ctf_link_sym
*
140 ldelf_ctf_symbols_iter_cb (struct ctf_link_sym
*dest
,
143 struct ctf_strsym_iter_cb_arg
*arg
=
144 (struct ctf_strsym_iter_cb_arg
*) arg_
;
146 if (arg
->next_i
> arg
->symcount
)
153 ASSERT (arg
->syms
[arg
->next_i
].dest_index
== arg
->next_idx
);
154 dest
->st_name
= _bfd_elf_strtab_str (arg
->symstrtab
, arg
->next_i
, NULL
);
155 dest
->st_shndx
= arg
->syms
[arg
->next_i
].sym
.st_shndx
;
156 dest
->st_type
= ELF_ST_TYPE (arg
->syms
[arg
->next_i
].sym
.st_info
);
157 dest
->st_value
= arg
->syms
[arg
->next_i
].sym
.st_value
;
163 ldelf_examine_strtab_for_ctf
164 (struct ctf_file
*ctf_output
, struct elf_sym_strtab
*syms
,
165 bfd_size_type symcount
, struct elf_strtab_hash
*symstrtab
)
167 struct ctf_strsym_iter_cb_arg args
= { syms
, symcount
, symstrtab
,
172 if (bfd_get_flavour (link_info
.output_bfd
) == bfd_target_elf_flavour
173 && !bfd_link_relocatable (&link_info
))
175 if (ctf_link_add_strtab (ctf_output
, ldelf_ctf_strtab_iter_cb
,
177 einfo (_("%F%P: warning: CTF strtab association failed; strings will "
178 "not be shared: %s\n"),
179 ctf_errmsg (ctf_errno (ctf_output
)));
181 if (ctf_link_shuffle_syms (ctf_output
, ldelf_ctf_symbols_iter_cb
,
183 einfo (_("%F%P: warning: CTF symbol shuffling failed; slight space "
184 "cost: %s\n"), ctf_errmsg (ctf_errno (ctf_output
)));