[gdbserver] Add missing include of gdbsupport/agent.h
[deliverable/binutils-gdb.git] / ld / ldelfgen.c
1 /* Emulation code used by all ELF targets.
2 Copyright (C) 1991-2020 Free Software Foundation, Inc.
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"
23 #include "bfdlink.h"
24 #include "ctf-api.h"
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"
31 #include "ldelfgen.h"
32
33 void
34 ldelf_map_segments (bfd_boolean need_layout)
35 {
36 int tries = 10;
37
38 do
39 {
40 lang_relax_sections (need_layout);
41 need_layout = FALSE;
42
43 if (link_info.output_bfd->xvec->flavour == bfd_target_elf_flavour
44 && !bfd_link_relocatable (&link_info))
45 {
46 bfd_size_type phdr_size;
47
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,
54 &link_info))
55 einfo (_("%F%P: map sections to segments failed: %E\n"));
56
57 if (phdr_size != elf_program_header_size (link_info.output_bfd))
58 {
59 if (tries > 6)
60 /* The first few times we allow any change to
61 phdr_size . */
62 need_layout = TRUE;
63 else if (phdr_size
64 < elf_program_header_size (link_info.output_bfd))
65 /* After that we only allow the size to grow. */
66 need_layout = TRUE;
67 else
68 elf_program_header_size (link_info.output_bfd) = phdr_size;
69 }
70 }
71 }
72 while (need_layout && --tries);
73
74 if (tries == 0)
75 einfo (_("%F%P: looping in map_segments"));
76
77 if (link_info.output_bfd->xvec->flavour == bfd_target_elf_flavour
78 && lang_phdr_list == NULL)
79 {
80 /* If we don't have user supplied phdrs, strip zero-sized dynamic
81 sections and regenerate program headers. */
82 const struct elf_backend_data *bed
83 = get_elf_backend_data (link_info.output_bfd);
84 if (bed->elf_backend_strip_zero_sized_dynamic_sections
85 && !bed->elf_backend_strip_zero_sized_dynamic_sections
86 (&link_info))
87 einfo (_("%F%P: failed to strip zero-sized dynamic sections"));
88 }
89 }
90
91 #ifdef ENABLE_LIBCTF
92 /* We want to emit CTF early if and only if we are not targetting ELF with this
93 invocation. */
94
95 int
96 ldelf_emit_ctf_early (void)
97 {
98 if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
99 return 0;
100 return 1;
101 }
102
103 /* Callbacks used to map from bfd types to libctf types, under libctf's
104 control. */
105
106 struct ctf_strsym_iter_cb_arg
107 {
108 struct elf_sym_strtab *syms;
109 bfd_size_type symcount;
110 struct elf_strtab_hash *symstrtab;
111 size_t next_i;
112 size_t next_idx;
113 };
114
115 /* Return strings from the strtab to libctf, one by one. Returns NULL when
116 iteration is complete. */
117
118 static const char *
119 ldelf_ctf_strtab_iter_cb (uint32_t *offset, void *arg_)
120 {
121 bfd_size_type off;
122 const char *ret;
123
124 struct ctf_strsym_iter_cb_arg *arg =
125 (struct ctf_strsym_iter_cb_arg *) arg_;
126
127 /* There is no zeroth string. */
128 if (arg->next_i == 0)
129 arg->next_i = 1;
130
131 if (arg->next_i >= _bfd_elf_strtab_len (arg->symstrtab))
132 {
133 arg->next_i = 0;
134 return NULL;
135 }
136
137 ret = _bfd_elf_strtab_str (arg->symstrtab, arg->next_i++, &off);
138 *offset = off;
139
140 /* If we've overflowed, we cannot share any further strings: the CTF
141 format cannot encode strings with such high offsets. */
142 if (*offset != off)
143 return NULL;
144
145 return ret;
146 }
147
148 /* Return symbols from the symbol table to libctf, one by one. We assume (and
149 assert) that the symbols in the elf_link_hash_table are in strictly ascending
150 order, and that none will be added in between existing ones. Returns NULL
151 when iteration is complete. */
152
153 static struct ctf_link_sym *
154 ldelf_ctf_symbols_iter_cb (struct ctf_link_sym *dest,
155 void *arg_)
156 {
157 struct ctf_strsym_iter_cb_arg *arg =
158 (struct ctf_strsym_iter_cb_arg *) arg_;
159
160 if (arg->next_i > arg->symcount)
161 {
162 arg->next_i = 0;
163 arg->next_idx = 0;
164 return NULL;
165 }
166
167 ASSERT (arg->syms[arg->next_i].dest_index == arg->next_idx);
168 dest->st_name = _bfd_elf_strtab_str (arg->symstrtab, arg->next_i, NULL);
169 dest->st_shndx = arg->syms[arg->next_i].sym.st_shndx;
170 dest->st_type = ELF_ST_TYPE (arg->syms[arg->next_i].sym.st_info);
171 dest->st_value = arg->syms[arg->next_i].sym.st_value;
172 arg->next_i++;
173 return dest;
174 }
175
176 void
177 ldelf_examine_strtab_for_ctf
178 (struct ctf_file *ctf_output, struct elf_sym_strtab *syms,
179 bfd_size_type symcount, struct elf_strtab_hash *symstrtab)
180 {
181 struct ctf_strsym_iter_cb_arg args = { syms, symcount, symstrtab,
182 0, 0 };
183 if (!ctf_output)
184 return;
185
186 if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour
187 && !bfd_link_relocatable (&link_info))
188 {
189 if (ctf_link_add_strtab (ctf_output, ldelf_ctf_strtab_iter_cb,
190 &args) < 0)
191 einfo (_("%F%P: warning: CTF strtab association failed; strings will "
192 "not be shared: %s\n"),
193 ctf_errmsg (ctf_errno (ctf_output)));
194
195 if (ctf_link_shuffle_syms (ctf_output, ldelf_ctf_symbols_iter_cb,
196 &args) < 0)
197 einfo (_("%F%P: warning: CTF symbol shuffling failed; slight space "
198 "cost: %s\n"), ctf_errmsg (ctf_errno (ctf_output)));
199 }
200 }
201 #else
202 extern int ldelf_emit_ctf_early (void)
203 {
204 return 0;
205 }
206
207 extern void ldelf_examine_strtab_for_ctf
208 (struct ctf_file *ctf_output ATTRIBUTE_UNUSED,
209 struct elf_sym_strtab *syms ATTRIBUTE_UNUSED,
210 bfd_size_type symcount ATTRIBUTE_UNUSED,
211 struct elf_strtab_hash *symstrtab ATTRIBUTE_UNUSED)
212 {}
213 #endif
This page took 0.032904 seconds and 4 git commands to generate.