bfd, include, ld, binutils, libctf: CTF should use the dynstr/sym
[deliverable/binutils-gdb.git] / ld / ldelfgen.c
CommitLineData
d871d478 1/* Emulation code used by all ELF targets.
b3adc24a 2 Copyright (C) 1991-2020 Free Software Foundation, Inc.
d871d478
AM
3
4 This file is part of the GNU Binutils.
5
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.
10
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.
15
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. */
20
21#include "sysdep.h"
22#include "bfd.h"
0b4453c7 23#include "bfdlink.h"
1ff6de03 24#include "ctf-api.h"
d871d478
AM
25#include "ld.h"
26#include "ldmain.h"
27#include "ldmisc.h"
28#include "ldexp.h"
29#include "ldlang.h"
30#include "elf-bfd.h"
3d16b64e 31#include "elf/internal.h"
d871d478
AM
32#include "ldelfgen.h"
33
34void
35ldelf_map_segments (bfd_boolean need_layout)
36{
37 int tries = 10;
38
39 do
40 {
41 lang_relax_sections (need_layout);
42 need_layout = FALSE;
43
44 if (link_info.output_bfd->xvec->flavour == bfd_target_elf_flavour
45 && !bfd_link_relocatable (&link_info))
46 {
47 bfd_size_type phdr_size;
48
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,
55 &link_info))
56 einfo (_("%F%P: map sections to segments failed: %E\n"));
57
58 if (phdr_size != elf_program_header_size (link_info.output_bfd))
59 {
60 if (tries > 6)
61 /* The first few times we allow any change to
62 phdr_size . */
63 need_layout = TRUE;
64 else if (phdr_size
65 < elf_program_header_size (link_info.output_bfd))
66 /* After that we only allow the size to grow. */
67 need_layout = TRUE;
68 else
69 elf_program_header_size (link_info.output_bfd) = phdr_size;
70 }
71 }
72 }
73 while (need_layout && --tries);
74
75 if (tries == 0)
76 einfo (_("%F%P: looping in map_segments"));
6f6fd151
L
77
78 if (link_info.output_bfd->xvec->flavour == bfd_target_elf_flavour
79 && lang_phdr_list == NULL)
80 {
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
87 (&link_info))
88 einfo (_("%F%P: failed to strip zero-sized dynamic sections"));
89 }
d871d478 90}
1ff6de03 91
094e34f2 92#ifdef ENABLE_LIBCTF
1ff6de03
NA
93/* We want to emit CTF early if and only if we are not targetting ELF with this
94 invocation. */
95
96int
97ldelf_emit_ctf_early (void)
98{
99 if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
100 return 0;
101 return 1;
102}
103
104/* Callbacks used to map from bfd types to libctf types, under libctf's
105 control. */
106
3d16b64e 107struct ctf_strtab_iter_cb_arg
1ff6de03 108{
3d16b64e 109 struct elf_strtab_hash *strtab;
1ff6de03
NA
110 size_t next_i;
111 size_t next_idx;
112};
113
114/* Return strings from the strtab to libctf, one by one. Returns NULL when
115 iteration is complete. */
116
117static const char *
118ldelf_ctf_strtab_iter_cb (uint32_t *offset, void *arg_)
119{
120 bfd_size_type off;
121 const char *ret;
122
3d16b64e
NA
123 struct ctf_strtab_iter_cb_arg *arg =
124 (struct ctf_strtab_iter_cb_arg *) arg_;
1ff6de03
NA
125
126 /* There is no zeroth string. */
127 if (arg->next_i == 0)
128 arg->next_i = 1;
129
3d16b64e 130 if (arg->next_i >= _bfd_elf_strtab_len (arg->strtab))
1ff6de03
NA
131 {
132 arg->next_i = 0;
133 return NULL;
134 }
135
3d16b64e 136 ret = _bfd_elf_strtab_str (arg->strtab, arg->next_i++, &off);
1ff6de03
NA
137 *offset = off;
138
139 /* If we've overflowed, we cannot share any further strings: the CTF
140 format cannot encode strings with such high offsets. */
141 if (*offset != off)
142 return NULL;
143
144 return ret;
145}
146
3d16b64e
NA
147void
148ldelf_acquire_strings_for_ctf
149 (struct ctf_dict *ctf_output, struct elf_strtab_hash *strtab)
1ff6de03 150{
3d16b64e
NA
151 struct ctf_strtab_iter_cb_arg args = { strtab, 0, 0 };
152 if (!ctf_output)
153 return;
1ff6de03 154
3d16b64e 155 if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
1ff6de03 156 {
3d16b64e
NA
157 if (ctf_link_add_strtab (ctf_output, ldelf_ctf_strtab_iter_cb,
158 &args) < 0)
159 einfo (_("%F%P: warning: CTF strtab association failed; strings will "
160 "not be shared: %s\n"),
161 ctf_errmsg (ctf_errno (ctf_output)));
1ff6de03 162 }
1ff6de03
NA
163}
164
165void
3d16b64e
NA
166ldelf_new_dynsym_for_ctf (struct ctf_dict *ctf_output, int symidx,
167 struct elf_internal_sym *sym)
1ff6de03 168{
3d16b64e
NA
169 ctf_link_sym_t lsym;
170
171 if (!ctf_output)
1ff6de03
NA
172 return;
173
3d16b64e
NA
174 /* New symbol. */
175 if (sym != NULL)
1ff6de03 176 {
3d16b64e
NA
177 lsym.st_name = NULL;
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)
185 {
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)));
189 }
190 }
191 else
192 {
193 /* Shuffle all the symbols. */
1ff6de03 194
3d16b64e
NA
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)));
1ff6de03
NA
199 }
200}
094e34f2 201#else
3d16b64e
NA
202int
203ldelf_emit_ctf_early (void)
094e34f2
NA
204{
205 return 0;
206}
207
3d16b64e
NA
208void
209ldelf_acquire_strings_for_ctf (struct ctf_dict *ctf_output ATTRIBUTE_UNUSED,
210 struct elf_strtab_hash *strtab ATTRIBUTE_UNUSED)
211{}
212void
213ldelf_new_dynsym_for_ctf (struct ctf_dict *ctf_output ATTRIBUTE_UNUSED,
214 int symidx ATTRIBUTE_UNUSED,
215 struct elf_internal_sym *sym ATTRIBUTE_UNUSED)
094e34f2
NA
216{}
217#endif
This page took 0.095597 seconds and 4 git commands to generate.