elf.c revision 1.15 1 1.1 christos /* ELF executable support for BFD.
2 1.1 christos
3 1.14 christos Copyright (C) 1993-2022 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This file is part of BFD, the Binary File Descriptor library.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program; if not, write to the Free Software
19 1.1 christos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 1.1 christos MA 02110-1301, USA. */
21 1.1 christos
22 1.1 christos
23 1.1 christos /*
24 1.1 christos SECTION
25 1.1 christos ELF backends
26 1.1 christos
27 1.1 christos BFD support for ELF formats is being worked on.
28 1.1 christos Currently, the best supported back ends are for sparc and i386
29 1.1 christos (running svr4 or Solaris 2).
30 1.1 christos
31 1.1 christos Documentation of the internals of the support code still needs
32 1.1 christos to be written. The code is changing quickly enough that we
33 1.1 christos haven't bothered yet. */
34 1.1 christos
35 1.1 christos /* For sparc64-cross-sparc32. */
36 1.1 christos #define _SYSCALL32
37 1.1 christos #include "sysdep.h"
38 1.11 christos #include <limits.h>
39 1.1 christos #include "bfd.h"
40 1.1 christos #include "bfdlink.h"
41 1.1 christos #include "libbfd.h"
42 1.1 christos #define ARCH_SIZE 0
43 1.1 christos #include "elf-bfd.h"
44 1.1 christos #include "libiberty.h"
45 1.1 christos #include "safe-ctype.h"
46 1.8 christos #include "elf-linux-core.h"
47 1.1 christos
48 1.1 christos #ifdef CORE_HEADER
49 1.1 christos #include CORE_HEADER
50 1.1 christos #endif
51 1.1 christos
52 1.1 christos static int elf_sort_sections (const void *, const void *);
53 1.14 christos static bool assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
54 1.14 christos static bool swap_out_syms (bfd *, struct elf_strtab_hash **, int,
55 1.14 christos struct bfd_link_info *);
56 1.14 christos static bool elf_parse_notes (bfd *abfd, char *buf, size_t size,
57 1.14 christos file_ptr offset, size_t align);
58 1.1 christos
59 1.1 christos /* Swap version information in and out. The version information is
60 1.1 christos currently size independent. If that ever changes, this code will
61 1.1 christos need to move into elfcode.h. */
62 1.1 christos
63 1.1 christos /* Swap in a Verdef structure. */
64 1.1 christos
65 1.1 christos void
66 1.1 christos _bfd_elf_swap_verdef_in (bfd *abfd,
67 1.1 christos const Elf_External_Verdef *src,
68 1.1 christos Elf_Internal_Verdef *dst)
69 1.1 christos {
70 1.1 christos dst->vd_version = H_GET_16 (abfd, src->vd_version);
71 1.1 christos dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
72 1.1 christos dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
73 1.1 christos dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
74 1.1 christos dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
75 1.1 christos dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
76 1.1 christos dst->vd_next = H_GET_32 (abfd, src->vd_next);
77 1.1 christos }
78 1.1 christos
79 1.1 christos /* Swap out a Verdef structure. */
80 1.1 christos
81 1.1 christos void
82 1.1 christos _bfd_elf_swap_verdef_out (bfd *abfd,
83 1.1 christos const Elf_Internal_Verdef *src,
84 1.1 christos Elf_External_Verdef *dst)
85 1.1 christos {
86 1.1 christos H_PUT_16 (abfd, src->vd_version, dst->vd_version);
87 1.1 christos H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
88 1.1 christos H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
89 1.1 christos H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
90 1.1 christos H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
91 1.1 christos H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
92 1.1 christos H_PUT_32 (abfd, src->vd_next, dst->vd_next);
93 1.1 christos }
94 1.1 christos
95 1.1 christos /* Swap in a Verdaux structure. */
96 1.1 christos
97 1.1 christos void
98 1.1 christos _bfd_elf_swap_verdaux_in (bfd *abfd,
99 1.1 christos const Elf_External_Verdaux *src,
100 1.1 christos Elf_Internal_Verdaux *dst)
101 1.1 christos {
102 1.1 christos dst->vda_name = H_GET_32 (abfd, src->vda_name);
103 1.1 christos dst->vda_next = H_GET_32 (abfd, src->vda_next);
104 1.1 christos }
105 1.1 christos
106 1.1 christos /* Swap out a Verdaux structure. */
107 1.1 christos
108 1.1 christos void
109 1.1 christos _bfd_elf_swap_verdaux_out (bfd *abfd,
110 1.1 christos const Elf_Internal_Verdaux *src,
111 1.1 christos Elf_External_Verdaux *dst)
112 1.1 christos {
113 1.1 christos H_PUT_32 (abfd, src->vda_name, dst->vda_name);
114 1.1 christos H_PUT_32 (abfd, src->vda_next, dst->vda_next);
115 1.1 christos }
116 1.1 christos
117 1.1 christos /* Swap in a Verneed structure. */
118 1.1 christos
119 1.1 christos void
120 1.1 christos _bfd_elf_swap_verneed_in (bfd *abfd,
121 1.1 christos const Elf_External_Verneed *src,
122 1.1 christos Elf_Internal_Verneed *dst)
123 1.1 christos {
124 1.1 christos dst->vn_version = H_GET_16 (abfd, src->vn_version);
125 1.1 christos dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
126 1.1 christos dst->vn_file = H_GET_32 (abfd, src->vn_file);
127 1.1 christos dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
128 1.1 christos dst->vn_next = H_GET_32 (abfd, src->vn_next);
129 1.1 christos }
130 1.1 christos
131 1.1 christos /* Swap out a Verneed structure. */
132 1.1 christos
133 1.1 christos void
134 1.1 christos _bfd_elf_swap_verneed_out (bfd *abfd,
135 1.1 christos const Elf_Internal_Verneed *src,
136 1.1 christos Elf_External_Verneed *dst)
137 1.1 christos {
138 1.1 christos H_PUT_16 (abfd, src->vn_version, dst->vn_version);
139 1.1 christos H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
140 1.1 christos H_PUT_32 (abfd, src->vn_file, dst->vn_file);
141 1.1 christos H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
142 1.1 christos H_PUT_32 (abfd, src->vn_next, dst->vn_next);
143 1.1 christos }
144 1.1 christos
145 1.1 christos /* Swap in a Vernaux structure. */
146 1.1 christos
147 1.1 christos void
148 1.1 christos _bfd_elf_swap_vernaux_in (bfd *abfd,
149 1.1 christos const Elf_External_Vernaux *src,
150 1.1 christos Elf_Internal_Vernaux *dst)
151 1.1 christos {
152 1.1 christos dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
153 1.1 christos dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
154 1.1 christos dst->vna_other = H_GET_16 (abfd, src->vna_other);
155 1.1 christos dst->vna_name = H_GET_32 (abfd, src->vna_name);
156 1.1 christos dst->vna_next = H_GET_32 (abfd, src->vna_next);
157 1.1 christos }
158 1.1 christos
159 1.1 christos /* Swap out a Vernaux structure. */
160 1.1 christos
161 1.1 christos void
162 1.1 christos _bfd_elf_swap_vernaux_out (bfd *abfd,
163 1.1 christos const Elf_Internal_Vernaux *src,
164 1.1 christos Elf_External_Vernaux *dst)
165 1.1 christos {
166 1.1 christos H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
167 1.1 christos H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
168 1.1 christos H_PUT_16 (abfd, src->vna_other, dst->vna_other);
169 1.1 christos H_PUT_32 (abfd, src->vna_name, dst->vna_name);
170 1.1 christos H_PUT_32 (abfd, src->vna_next, dst->vna_next);
171 1.1 christos }
172 1.1 christos
173 1.1 christos /* Swap in a Versym structure. */
174 1.1 christos
175 1.1 christos void
176 1.1 christos _bfd_elf_swap_versym_in (bfd *abfd,
177 1.1 christos const Elf_External_Versym *src,
178 1.1 christos Elf_Internal_Versym *dst)
179 1.1 christos {
180 1.1 christos dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
181 1.1 christos }
182 1.1 christos
183 1.1 christos /* Swap out a Versym structure. */
184 1.1 christos
185 1.1 christos void
186 1.1 christos _bfd_elf_swap_versym_out (bfd *abfd,
187 1.1 christos const Elf_Internal_Versym *src,
188 1.1 christos Elf_External_Versym *dst)
189 1.1 christos {
190 1.1 christos H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
191 1.1 christos }
192 1.1 christos
193 1.1 christos /* Standard ELF hash function. Do not change this function; you will
194 1.1 christos cause invalid hash tables to be generated. */
195 1.1 christos
196 1.1 christos unsigned long
197 1.1 christos bfd_elf_hash (const char *namearg)
198 1.1 christos {
199 1.1 christos const unsigned char *name = (const unsigned char *) namearg;
200 1.1 christos unsigned long h = 0;
201 1.1 christos unsigned long g;
202 1.1 christos int ch;
203 1.1 christos
204 1.1 christos while ((ch = *name++) != '\0')
205 1.1 christos {
206 1.1 christos h = (h << 4) + ch;
207 1.1 christos if ((g = (h & 0xf0000000)) != 0)
208 1.1 christos {
209 1.1 christos h ^= g >> 24;
210 1.1 christos /* The ELF ABI says `h &= ~g', but this is equivalent in
211 1.1 christos this case and on some machines one insn instead of two. */
212 1.1 christos h ^= g;
213 1.1 christos }
214 1.1 christos }
215 1.1 christos return h & 0xffffffff;
216 1.1 christos }
217 1.1 christos
218 1.1 christos /* DT_GNU_HASH hash function. Do not change this function; you will
219 1.1 christos cause invalid hash tables to be generated. */
220 1.1 christos
221 1.1 christos unsigned long
222 1.1 christos bfd_elf_gnu_hash (const char *namearg)
223 1.1 christos {
224 1.1 christos const unsigned char *name = (const unsigned char *) namearg;
225 1.1 christos unsigned long h = 5381;
226 1.1 christos unsigned char ch;
227 1.1 christos
228 1.1 christos while ((ch = *name++) != '\0')
229 1.1 christos h = (h << 5) + h + ch;
230 1.1 christos return h & 0xffffffff;
231 1.1 christos }
232 1.1 christos
233 1.1 christos /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
234 1.1 christos the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
235 1.14 christos bool
236 1.1 christos bfd_elf_allocate_object (bfd *abfd,
237 1.1 christos size_t object_size,
238 1.1 christos enum elf_target_id object_id)
239 1.1 christos {
240 1.1 christos BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
241 1.1 christos abfd->tdata.any = bfd_zalloc (abfd, object_size);
242 1.1 christos if (abfd->tdata.any == NULL)
243 1.14 christos return false;
244 1.1 christos
245 1.1 christos elf_object_id (abfd) = object_id;
246 1.3 christos if (abfd->direction != read_direction)
247 1.3 christos {
248 1.3 christos struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
249 1.3 christos if (o == NULL)
250 1.14 christos return false;
251 1.3 christos elf_tdata (abfd)->o = o;
252 1.3 christos elf_program_header_size (abfd) = (bfd_size_type) -1;
253 1.3 christos }
254 1.14 christos return true;
255 1.1 christos }
256 1.1 christos
257 1.1 christos
258 1.14 christos bool
259 1.1 christos bfd_elf_make_object (bfd *abfd)
260 1.1 christos {
261 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
262 1.1 christos return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
263 1.1 christos bed->target_id);
264 1.1 christos }
265 1.1 christos
266 1.14 christos bool
267 1.1 christos bfd_elf_mkcorefile (bfd *abfd)
268 1.1 christos {
269 1.1 christos /* I think this can be done just like an object file. */
270 1.3 christos if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
271 1.14 christos return false;
272 1.3 christos elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
273 1.3 christos return elf_tdata (abfd)->core != NULL;
274 1.1 christos }
275 1.1 christos
276 1.12 christos char *
277 1.1 christos bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
278 1.1 christos {
279 1.1 christos Elf_Internal_Shdr **i_shdrp;
280 1.1 christos bfd_byte *shstrtab = NULL;
281 1.1 christos file_ptr offset;
282 1.1 christos bfd_size_type shstrtabsize;
283 1.1 christos
284 1.1 christos i_shdrp = elf_elfsections (abfd);
285 1.1 christos if (i_shdrp == 0
286 1.1 christos || shindex >= elf_numsections (abfd)
287 1.1 christos || i_shdrp[shindex] == 0)
288 1.1 christos return NULL;
289 1.1 christos
290 1.1 christos shstrtab = i_shdrp[shindex]->contents;
291 1.1 christos if (shstrtab == NULL)
292 1.1 christos {
293 1.1 christos /* No cached one, attempt to read, and cache what we read. */
294 1.1 christos offset = i_shdrp[shindex]->sh_offset;
295 1.1 christos shstrtabsize = i_shdrp[shindex]->sh_size;
296 1.1 christos
297 1.1 christos /* Allocate and clear an extra byte at the end, to prevent crashes
298 1.1 christos in case the string table is not terminated. */
299 1.1 christos if (shstrtabsize + 1 <= 1
300 1.5 christos || bfd_seek (abfd, offset, SEEK_SET) != 0
301 1.12 christos || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1,
302 1.12 christos shstrtabsize)) == NULL)
303 1.1 christos {
304 1.1 christos /* Once we've failed to read it, make sure we don't keep
305 1.1 christos trying. Otherwise, we'll keep allocating space for
306 1.1 christos the string table over and over. */
307 1.1 christos i_shdrp[shindex]->sh_size = 0;
308 1.1 christos }
309 1.1 christos else
310 1.1 christos shstrtab[shstrtabsize] = '\0';
311 1.1 christos i_shdrp[shindex]->contents = shstrtab;
312 1.1 christos }
313 1.1 christos return (char *) shstrtab;
314 1.1 christos }
315 1.1 christos
316 1.1 christos char *
317 1.1 christos bfd_elf_string_from_elf_section (bfd *abfd,
318 1.1 christos unsigned int shindex,
319 1.1 christos unsigned int strindex)
320 1.1 christos {
321 1.1 christos Elf_Internal_Shdr *hdr;
322 1.1 christos
323 1.1 christos if (strindex == 0)
324 1.1 christos return "";
325 1.1 christos
326 1.1 christos if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
327 1.1 christos return NULL;
328 1.1 christos
329 1.1 christos hdr = elf_elfsections (abfd)[shindex];
330 1.1 christos
331 1.5 christos if (hdr->contents == NULL)
332 1.5 christos {
333 1.5 christos if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
334 1.5 christos {
335 1.5 christos /* PR 17512: file: f057ec89. */
336 1.9 christos /* xgettext:c-format */
337 1.11 christos _bfd_error_handler (_("%pB: attempt to load strings from"
338 1.9 christos " a non-string section (number %d)"),
339 1.5 christos abfd, shindex);
340 1.5 christos return NULL;
341 1.5 christos }
342 1.6 christos
343 1.5 christos if (bfd_elf_get_str_section (abfd, shindex) == NULL)
344 1.5 christos return NULL;
345 1.5 christos }
346 1.12 christos else
347 1.12 christos {
348 1.12 christos /* PR 24273: The string section's contents may have already
349 1.12 christos been loaded elsewhere, eg because a corrupt file has the
350 1.12 christos string section index in the ELF header pointing at a group
351 1.12 christos section. So be paranoid, and test that the last byte of
352 1.12 christos the section is zero. */
353 1.12 christos if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
354 1.12 christos return NULL;
355 1.12 christos }
356 1.1 christos
357 1.1 christos if (strindex >= hdr->sh_size)
358 1.1 christos {
359 1.1 christos unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
360 1.9 christos _bfd_error_handler
361 1.9 christos /* xgettext:c-format */
362 1.11 christos (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
363 1.11 christos abfd, strindex, (uint64_t) hdr->sh_size,
364 1.1 christos (shindex == shstrndx && strindex == hdr->sh_name
365 1.1 christos ? ".shstrtab"
366 1.1 christos : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
367 1.1 christos return NULL;
368 1.1 christos }
369 1.1 christos
370 1.1 christos return ((char *) hdr->contents) + strindex;
371 1.1 christos }
372 1.1 christos
373 1.1 christos /* Read and convert symbols to internal format.
374 1.1 christos SYMCOUNT specifies the number of symbols to read, starting from
375 1.1 christos symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
376 1.1 christos are non-NULL, they are used to store the internal symbols, external
377 1.1 christos symbols, and symbol section index extensions, respectively.
378 1.1 christos Returns a pointer to the internal symbol buffer (malloced if necessary)
379 1.1 christos or NULL if there were no symbols or some kind of problem. */
380 1.1 christos
381 1.1 christos Elf_Internal_Sym *
382 1.1 christos bfd_elf_get_elf_syms (bfd *ibfd,
383 1.1 christos Elf_Internal_Shdr *symtab_hdr,
384 1.1 christos size_t symcount,
385 1.1 christos size_t symoffset,
386 1.1 christos Elf_Internal_Sym *intsym_buf,
387 1.1 christos void *extsym_buf,
388 1.1 christos Elf_External_Sym_Shndx *extshndx_buf)
389 1.1 christos {
390 1.1 christos Elf_Internal_Shdr *shndx_hdr;
391 1.1 christos void *alloc_ext;
392 1.1 christos const bfd_byte *esym;
393 1.1 christos Elf_External_Sym_Shndx *alloc_extshndx;
394 1.1 christos Elf_External_Sym_Shndx *shndx;
395 1.1 christos Elf_Internal_Sym *alloc_intsym;
396 1.1 christos Elf_Internal_Sym *isym;
397 1.1 christos Elf_Internal_Sym *isymend;
398 1.1 christos const struct elf_backend_data *bed;
399 1.1 christos size_t extsym_size;
400 1.12 christos size_t amt;
401 1.1 christos file_ptr pos;
402 1.1 christos
403 1.1 christos if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
404 1.1 christos abort ();
405 1.1 christos
406 1.1 christos if (symcount == 0)
407 1.1 christos return intsym_buf;
408 1.1 christos
409 1.1 christos /* Normal syms might have section extension entries. */
410 1.1 christos shndx_hdr = NULL;
411 1.8 christos if (elf_symtab_shndx_list (ibfd) != NULL)
412 1.8 christos {
413 1.8 christos elf_section_list * entry;
414 1.8 christos Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
415 1.8 christos
416 1.8 christos /* Find an index section that is linked to this symtab section. */
417 1.8 christos for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
418 1.8 christos {
419 1.8 christos /* PR 20063. */
420 1.8 christos if (entry->hdr.sh_link >= elf_numsections (ibfd))
421 1.8 christos continue;
422 1.8 christos
423 1.8 christos if (sections[entry->hdr.sh_link] == symtab_hdr)
424 1.8 christos {
425 1.8 christos shndx_hdr = & entry->hdr;
426 1.8 christos break;
427 1.8 christos };
428 1.8 christos }
429 1.8 christos
430 1.8 christos if (shndx_hdr == NULL)
431 1.8 christos {
432 1.14 christos if (symtab_hdr == &elf_symtab_hdr (ibfd))
433 1.14 christos /* Not really accurate, but this was how the old code used
434 1.14 christos to work. */
435 1.14 christos shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
436 1.8 christos /* Otherwise we do nothing. The assumption is that
437 1.8 christos the index table will not be needed. */
438 1.8 christos }
439 1.8 christos }
440 1.1 christos
441 1.1 christos /* Read the symbols. */
442 1.1 christos alloc_ext = NULL;
443 1.1 christos alloc_extshndx = NULL;
444 1.1 christos alloc_intsym = NULL;
445 1.1 christos bed = get_elf_backend_data (ibfd);
446 1.1 christos extsym_size = bed->s->sizeof_sym;
447 1.12 christos if (_bfd_mul_overflow (symcount, extsym_size, &amt))
448 1.12 christos {
449 1.12 christos bfd_set_error (bfd_error_file_too_big);
450 1.12 christos intsym_buf = NULL;
451 1.12 christos goto out;
452 1.12 christos }
453 1.1 christos pos = symtab_hdr->sh_offset + symoffset * extsym_size;
454 1.1 christos if (extsym_buf == NULL)
455 1.1 christos {
456 1.12 christos alloc_ext = bfd_malloc (amt);
457 1.1 christos extsym_buf = alloc_ext;
458 1.1 christos }
459 1.1 christos if (extsym_buf == NULL
460 1.1 christos || bfd_seek (ibfd, pos, SEEK_SET) != 0
461 1.1 christos || bfd_bread (extsym_buf, amt, ibfd) != amt)
462 1.1 christos {
463 1.1 christos intsym_buf = NULL;
464 1.1 christos goto out;
465 1.1 christos }
466 1.1 christos
467 1.1 christos if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
468 1.1 christos extshndx_buf = NULL;
469 1.1 christos else
470 1.1 christos {
471 1.12 christos if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
472 1.12 christos {
473 1.12 christos bfd_set_error (bfd_error_file_too_big);
474 1.12 christos intsym_buf = NULL;
475 1.12 christos goto out;
476 1.12 christos }
477 1.1 christos pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
478 1.1 christos if (extshndx_buf == NULL)
479 1.1 christos {
480 1.12 christos alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
481 1.1 christos extshndx_buf = alloc_extshndx;
482 1.1 christos }
483 1.1 christos if (extshndx_buf == NULL
484 1.1 christos || bfd_seek (ibfd, pos, SEEK_SET) != 0
485 1.1 christos || bfd_bread (extshndx_buf, amt, ibfd) != amt)
486 1.1 christos {
487 1.1 christos intsym_buf = NULL;
488 1.1 christos goto out;
489 1.1 christos }
490 1.1 christos }
491 1.1 christos
492 1.1 christos if (intsym_buf == NULL)
493 1.1 christos {
494 1.12 christos if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
495 1.12 christos {
496 1.12 christos bfd_set_error (bfd_error_file_too_big);
497 1.12 christos goto out;
498 1.12 christos }
499 1.12 christos alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
500 1.1 christos intsym_buf = alloc_intsym;
501 1.1 christos if (intsym_buf == NULL)
502 1.1 christos goto out;
503 1.1 christos }
504 1.1 christos
505 1.1 christos /* Convert the symbols to internal form. */
506 1.1 christos isymend = intsym_buf + symcount;
507 1.1 christos for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
508 1.11 christos shndx = extshndx_buf;
509 1.1 christos isym < isymend;
510 1.1 christos esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
511 1.1 christos if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
512 1.1 christos {
513 1.1 christos symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
514 1.9 christos /* xgettext:c-format */
515 1.11 christos _bfd_error_handler (_("%pB symbol number %lu references"
516 1.9 christos " nonexistent SHT_SYMTAB_SHNDX section"),
517 1.9 christos ibfd, (unsigned long) symoffset);
518 1.12 christos free (alloc_intsym);
519 1.1 christos intsym_buf = NULL;
520 1.1 christos goto out;
521 1.1 christos }
522 1.1 christos
523 1.1 christos out:
524 1.12 christos free (alloc_ext);
525 1.12 christos free (alloc_extshndx);
526 1.1 christos
527 1.1 christos return intsym_buf;
528 1.1 christos }
529 1.1 christos
530 1.1 christos /* Look up a symbol name. */
531 1.1 christos const char *
532 1.1 christos bfd_elf_sym_name (bfd *abfd,
533 1.1 christos Elf_Internal_Shdr *symtab_hdr,
534 1.1 christos Elf_Internal_Sym *isym,
535 1.1 christos asection *sym_sec)
536 1.1 christos {
537 1.1 christos const char *name;
538 1.1 christos unsigned int iname = isym->st_name;
539 1.1 christos unsigned int shindex = symtab_hdr->sh_link;
540 1.1 christos
541 1.1 christos if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
542 1.1 christos /* Check for a bogus st_shndx to avoid crashing. */
543 1.1 christos && isym->st_shndx < elf_numsections (abfd))
544 1.1 christos {
545 1.1 christos iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
546 1.1 christos shindex = elf_elfheader (abfd)->e_shstrndx;
547 1.1 christos }
548 1.1 christos
549 1.1 christos name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
550 1.1 christos if (name == NULL)
551 1.1 christos name = "(null)";
552 1.1 christos else if (sym_sec && *name == '\0')
553 1.12 christos name = bfd_section_name (sym_sec);
554 1.1 christos
555 1.1 christos return name;
556 1.1 christos }
557 1.1 christos
558 1.1 christos /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
559 1.1 christos sections. The first element is the flags, the rest are section
560 1.1 christos pointers. */
561 1.1 christos
562 1.1 christos typedef union elf_internal_group {
563 1.1 christos Elf_Internal_Shdr *shdr;
564 1.1 christos unsigned int flags;
565 1.1 christos } Elf_Internal_Group;
566 1.1 christos
567 1.1 christos /* Return the name of the group signature symbol. Why isn't the
568 1.1 christos signature just a string? */
569 1.1 christos
570 1.1 christos static const char *
571 1.1 christos group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
572 1.1 christos {
573 1.1 christos Elf_Internal_Shdr *hdr;
574 1.1 christos unsigned char esym[sizeof (Elf64_External_Sym)];
575 1.1 christos Elf_External_Sym_Shndx eshndx;
576 1.1 christos Elf_Internal_Sym isym;
577 1.1 christos
578 1.1 christos /* First we need to ensure the symbol table is available. Make sure
579 1.1 christos that it is a symbol table section. */
580 1.1 christos if (ghdr->sh_link >= elf_numsections (abfd))
581 1.1 christos return NULL;
582 1.1 christos hdr = elf_elfsections (abfd) [ghdr->sh_link];
583 1.1 christos if (hdr->sh_type != SHT_SYMTAB
584 1.1 christos || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
585 1.1 christos return NULL;
586 1.1 christos
587 1.1 christos /* Go read the symbol. */
588 1.1 christos hdr = &elf_tdata (abfd)->symtab_hdr;
589 1.1 christos if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
590 1.1 christos &isym, esym, &eshndx) == NULL)
591 1.1 christos return NULL;
592 1.1 christos
593 1.1 christos return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
594 1.1 christos }
595 1.1 christos
596 1.1 christos /* Set next_in_group list pointer, and group name for NEWSECT. */
597 1.1 christos
598 1.14 christos static bool
599 1.1 christos setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
600 1.1 christos {
601 1.1 christos unsigned int num_group = elf_tdata (abfd)->num_group;
602 1.1 christos
603 1.1 christos /* If num_group is zero, read in all SHT_GROUP sections. The count
604 1.1 christos is set to -1 if there are no SHT_GROUP sections. */
605 1.1 christos if (num_group == 0)
606 1.1 christos {
607 1.1 christos unsigned int i, shnum;
608 1.1 christos
609 1.1 christos /* First count the number of groups. If we have a SHT_GROUP
610 1.1 christos section with just a flag word (ie. sh_size is 4), ignore it. */
611 1.1 christos shnum = elf_numsections (abfd);
612 1.1 christos num_group = 0;
613 1.1 christos
614 1.3 christos #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
615 1.1 christos ( (shdr)->sh_type == SHT_GROUP \
616 1.3 christos && (shdr)->sh_size >= minsize \
617 1.1 christos && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
618 1.1 christos && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
619 1.1 christos
620 1.1 christos for (i = 0; i < shnum; i++)
621 1.1 christos {
622 1.1 christos Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
623 1.1 christos
624 1.3 christos if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
625 1.1 christos num_group += 1;
626 1.1 christos }
627 1.1 christos
628 1.1 christos if (num_group == 0)
629 1.1 christos {
630 1.1 christos num_group = (unsigned) -1;
631 1.1 christos elf_tdata (abfd)->num_group = num_group;
632 1.11 christos elf_tdata (abfd)->group_sect_ptr = NULL;
633 1.1 christos }
634 1.1 christos else
635 1.1 christos {
636 1.1 christos /* We keep a list of elf section headers for group sections,
637 1.1 christos so we can find them quickly. */
638 1.12 christos size_t amt;
639 1.1 christos
640 1.1 christos elf_tdata (abfd)->num_group = num_group;
641 1.12 christos amt = num_group * sizeof (Elf_Internal_Shdr *);
642 1.12 christos elf_tdata (abfd)->group_sect_ptr
643 1.12 christos = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
644 1.1 christos if (elf_tdata (abfd)->group_sect_ptr == NULL)
645 1.14 christos return false;
646 1.11 christos num_group = 0;
647 1.1 christos
648 1.1 christos for (i = 0; i < shnum; i++)
649 1.1 christos {
650 1.1 christos Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
651 1.1 christos
652 1.3 christos if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
653 1.1 christos {
654 1.1 christos unsigned char *src;
655 1.1 christos Elf_Internal_Group *dest;
656 1.1 christos
657 1.11 christos /* Make sure the group section has a BFD section
658 1.11 christos attached to it. */
659 1.11 christos if (!bfd_section_from_shdr (abfd, i))
660 1.14 christos return false;
661 1.11 christos
662 1.1 christos /* Add to list of sections. */
663 1.1 christos elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
664 1.1 christos num_group += 1;
665 1.1 christos
666 1.1 christos /* Read the raw contents. */
667 1.12 christos BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
668 1.12 christos shdr->contents = NULL;
669 1.12 christos if (_bfd_mul_overflow (shdr->sh_size,
670 1.12 christos sizeof (*dest) / 4, &amt)
671 1.12 christos || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
672 1.12 christos || !(shdr->contents
673 1.12 christos = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
674 1.5 christos {
675 1.5 christos _bfd_error_handler
676 1.9 christos /* xgettext:c-format */
677 1.11 christos (_("%pB: invalid size field in group section"
678 1.11 christos " header: %#" PRIx64 ""),
679 1.11 christos abfd, (uint64_t) shdr->sh_size);
680 1.5 christos bfd_set_error (bfd_error_bad_value);
681 1.5 christos -- num_group;
682 1.5 christos continue;
683 1.5 christos }
684 1.1 christos
685 1.1 christos /* Translate raw contents, a flag word followed by an
686 1.1 christos array of elf section indices all in target byte order,
687 1.1 christos to the flag word followed by an array of elf section
688 1.1 christos pointers. */
689 1.1 christos src = shdr->contents + shdr->sh_size;
690 1.1 christos dest = (Elf_Internal_Group *) (shdr->contents + amt);
691 1.5 christos
692 1.1 christos while (1)
693 1.1 christos {
694 1.1 christos unsigned int idx;
695 1.1 christos
696 1.1 christos src -= 4;
697 1.1 christos --dest;
698 1.1 christos idx = H_GET_32 (abfd, src);
699 1.1 christos if (src == shdr->contents)
700 1.1 christos {
701 1.12 christos dest->shdr = NULL;
702 1.1 christos dest->flags = idx;
703 1.1 christos if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
704 1.1 christos shdr->bfd_section->flags
705 1.1 christos |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
706 1.1 christos break;
707 1.1 christos }
708 1.11 christos if (idx < shnum)
709 1.11 christos {
710 1.11 christos dest->shdr = elf_elfsections (abfd)[idx];
711 1.11 christos /* PR binutils/23199: All sections in a
712 1.11 christos section group should be marked with
713 1.11 christos SHF_GROUP. But some tools generate
714 1.11 christos broken objects without SHF_GROUP. Fix
715 1.11 christos them up here. */
716 1.11 christos dest->shdr->sh_flags |= SHF_GROUP;
717 1.11 christos }
718 1.11 christos if (idx >= shnum
719 1.11 christos || dest->shdr->sh_type == SHT_GROUP)
720 1.1 christos {
721 1.9 christos _bfd_error_handler
722 1.11 christos (_("%pB: invalid entry in SHT_GROUP section [%u]"),
723 1.11 christos abfd, i);
724 1.11 christos dest->shdr = NULL;
725 1.1 christos }
726 1.1 christos }
727 1.1 christos }
728 1.1 christos }
729 1.5 christos
730 1.5 christos /* PR 17510: Corrupt binaries might contain invalid groups. */
731 1.5 christos if (num_group != (unsigned) elf_tdata (abfd)->num_group)
732 1.5 christos {
733 1.5 christos elf_tdata (abfd)->num_group = num_group;
734 1.5 christos
735 1.5 christos /* If all groups are invalid then fail. */
736 1.5 christos if (num_group == 0)
737 1.5 christos {
738 1.5 christos elf_tdata (abfd)->group_sect_ptr = NULL;
739 1.5 christos elf_tdata (abfd)->num_group = num_group = -1;
740 1.9 christos _bfd_error_handler
741 1.11 christos (_("%pB: no valid group sections found"), abfd);
742 1.5 christos bfd_set_error (bfd_error_bad_value);
743 1.5 christos }
744 1.5 christos }
745 1.1 christos }
746 1.1 christos }
747 1.1 christos
748 1.1 christos if (num_group != (unsigned) -1)
749 1.1 christos {
750 1.11 christos unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
751 1.11 christos unsigned int j;
752 1.1 christos
753 1.11 christos for (j = 0; j < num_group; j++)
754 1.1 christos {
755 1.11 christos /* Begin search from previous found group. */
756 1.11 christos unsigned i = (j + search_offset) % num_group;
757 1.11 christos
758 1.1 christos Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
759 1.11 christos Elf_Internal_Group *idx;
760 1.11 christos bfd_size_type n_elt;
761 1.11 christos
762 1.11 christos if (shdr == NULL)
763 1.11 christos continue;
764 1.11 christos
765 1.11 christos idx = (Elf_Internal_Group *) shdr->contents;
766 1.11 christos if (idx == NULL || shdr->sh_size < 4)
767 1.11 christos {
768 1.11 christos /* See PR 21957 for a reproducer. */
769 1.11 christos /* xgettext:c-format */
770 1.11 christos _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
771 1.11 christos abfd, shdr->bfd_section);
772 1.11 christos elf_tdata (abfd)->group_sect_ptr[i] = NULL;
773 1.11 christos bfd_set_error (bfd_error_bad_value);
774 1.14 christos return false;
775 1.11 christos }
776 1.11 christos n_elt = shdr->sh_size / 4;
777 1.1 christos
778 1.1 christos /* Look through this group's sections to see if current
779 1.1 christos section is a member. */
780 1.1 christos while (--n_elt != 0)
781 1.1 christos if ((++idx)->shdr == hdr)
782 1.1 christos {
783 1.1 christos asection *s = NULL;
784 1.1 christos
785 1.1 christos /* We are a member of this group. Go looking through
786 1.1 christos other members to see if any others are linked via
787 1.1 christos next_in_group. */
788 1.1 christos idx = (Elf_Internal_Group *) shdr->contents;
789 1.1 christos n_elt = shdr->sh_size / 4;
790 1.1 christos while (--n_elt != 0)
791 1.11 christos if ((++idx)->shdr != NULL
792 1.11 christos && (s = idx->shdr->bfd_section) != NULL
793 1.1 christos && elf_next_in_group (s) != NULL)
794 1.1 christos break;
795 1.1 christos if (n_elt != 0)
796 1.1 christos {
797 1.1 christos /* Snarf the group name from other member, and
798 1.1 christos insert current section in circular list. */
799 1.1 christos elf_group_name (newsect) = elf_group_name (s);
800 1.1 christos elf_next_in_group (newsect) = elf_next_in_group (s);
801 1.1 christos elf_next_in_group (s) = newsect;
802 1.1 christos }
803 1.1 christos else
804 1.1 christos {
805 1.1 christos const char *gname;
806 1.1 christos
807 1.1 christos gname = group_signature (abfd, shdr);
808 1.1 christos if (gname == NULL)
809 1.14 christos return false;
810 1.1 christos elf_group_name (newsect) = gname;
811 1.1 christos
812 1.1 christos /* Start a circular list with one element. */
813 1.1 christos elf_next_in_group (newsect) = newsect;
814 1.1 christos }
815 1.1 christos
816 1.1 christos /* If the group section has been created, point to the
817 1.1 christos new member. */
818 1.1 christos if (shdr->bfd_section != NULL)
819 1.1 christos elf_next_in_group (shdr->bfd_section) = newsect;
820 1.1 christos
821 1.11 christos elf_tdata (abfd)->group_search_offset = i;
822 1.11 christos j = num_group - 1;
823 1.1 christos break;
824 1.1 christos }
825 1.1 christos }
826 1.1 christos }
827 1.1 christos
828 1.1 christos if (elf_group_name (newsect) == NULL)
829 1.1 christos {
830 1.9 christos /* xgettext:c-format */
831 1.11 christos _bfd_error_handler (_("%pB: no group info for section '%pA'"),
832 1.9 christos abfd, newsect);
833 1.14 christos /* PR 29532: Return true here, even though the group info has not been
834 1.14 christos read. Separate debug info files can have empty group sections, but
835 1.14 christos we do not want this to prevent them from being loaded as otherwise
836 1.14 christos GDB will not be able to use them. */
837 1.14 christos return true;
838 1.1 christos }
839 1.14 christos return true;
840 1.1 christos }
841 1.1 christos
842 1.14 christos bool
843 1.1 christos _bfd_elf_setup_sections (bfd *abfd)
844 1.1 christos {
845 1.1 christos unsigned int i;
846 1.1 christos unsigned int num_group = elf_tdata (abfd)->num_group;
847 1.14 christos bool result = true;
848 1.1 christos asection *s;
849 1.1 christos
850 1.1 christos /* Process SHF_LINK_ORDER. */
851 1.1 christos for (s = abfd->sections; s != NULL; s = s->next)
852 1.1 christos {
853 1.1 christos Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
854 1.1 christos if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
855 1.1 christos {
856 1.1 christos unsigned int elfsec = this_hdr->sh_link;
857 1.14 christos /* An sh_link value of 0 is now allowed. It indicates that linked
858 1.14 christos to section has already been discarded, but that the current
859 1.14 christos section has been retained for some other reason. This linking
860 1.14 christos section is still a candidate for later garbage collection
861 1.14 christos however. */
862 1.1 christos if (elfsec == 0)
863 1.1 christos {
864 1.14 christos elf_linked_to_section (s) = NULL;
865 1.1 christos }
866 1.1 christos else
867 1.1 christos {
868 1.1 christos asection *linksec = NULL;
869 1.1 christos
870 1.1 christos if (elfsec < elf_numsections (abfd))
871 1.1 christos {
872 1.1 christos this_hdr = elf_elfsections (abfd)[elfsec];
873 1.1 christos linksec = this_hdr->bfd_section;
874 1.1 christos }
875 1.1 christos
876 1.1 christos /* PR 1991, 2008:
877 1.1 christos Some strip/objcopy may leave an incorrect value in
878 1.1 christos sh_link. We don't want to proceed. */
879 1.1 christos if (linksec == NULL)
880 1.1 christos {
881 1.9 christos _bfd_error_handler
882 1.9 christos /* xgettext:c-format */
883 1.11 christos (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
884 1.9 christos s->owner, elfsec, s);
885 1.14 christos result = false;
886 1.1 christos }
887 1.1 christos
888 1.1 christos elf_linked_to_section (s) = linksec;
889 1.1 christos }
890 1.1 christos }
891 1.9 christos else if (this_hdr->sh_type == SHT_GROUP
892 1.9 christos && elf_next_in_group (s) == NULL)
893 1.9 christos {
894 1.9 christos _bfd_error_handler
895 1.9 christos /* xgettext:c-format */
896 1.11 christos (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
897 1.9 christos abfd, elf_section_data (s)->this_idx);
898 1.14 christos result = false;
899 1.9 christos }
900 1.1 christos }
901 1.1 christos
902 1.1 christos /* Process section groups. */
903 1.1 christos if (num_group == (unsigned) -1)
904 1.1 christos return result;
905 1.1 christos
906 1.1 christos for (i = 0; i < num_group; i++)
907 1.1 christos {
908 1.1 christos Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
909 1.8 christos Elf_Internal_Group *idx;
910 1.8 christos unsigned int n_elt;
911 1.8 christos
912 1.14 christos /* PR binutils/18758: Beware of corrupt binaries with invalid
913 1.14 christos group data. */
914 1.8 christos if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
915 1.8 christos {
916 1.9 christos _bfd_error_handler
917 1.9 christos /* xgettext:c-format */
918 1.11 christos (_("%pB: section group entry number %u is corrupt"),
919 1.8 christos abfd, i);
920 1.14 christos result = false;
921 1.8 christos continue;
922 1.8 christos }
923 1.8 christos
924 1.8 christos idx = (Elf_Internal_Group *) shdr->contents;
925 1.8 christos n_elt = shdr->sh_size / 4;
926 1.1 christos
927 1.1 christos while (--n_elt != 0)
928 1.11 christos {
929 1.11 christos ++ idx;
930 1.11 christos
931 1.11 christos if (idx->shdr == NULL)
932 1.11 christos continue;
933 1.11 christos else if (idx->shdr->bfd_section)
934 1.11 christos elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
935 1.11 christos else if (idx->shdr->sh_type != SHT_RELA
936 1.11 christos && idx->shdr->sh_type != SHT_REL)
937 1.11 christos {
938 1.11 christos /* There are some unknown sections in the group. */
939 1.11 christos _bfd_error_handler
940 1.11 christos /* xgettext:c-format */
941 1.11 christos (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
942 1.11 christos abfd,
943 1.11 christos idx->shdr->sh_type,
944 1.11 christos bfd_elf_string_from_elf_section (abfd,
945 1.11 christos (elf_elfheader (abfd)
946 1.11 christos ->e_shstrndx),
947 1.11 christos idx->shdr->sh_name),
948 1.11 christos shdr->bfd_section);
949 1.14 christos result = false;
950 1.11 christos }
951 1.11 christos }
952 1.1 christos }
953 1.11 christos
954 1.1 christos return result;
955 1.1 christos }
956 1.1 christos
957 1.14 christos bool
958 1.1 christos bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
959 1.1 christos {
960 1.1 christos return elf_next_in_group (sec) != NULL;
961 1.1 christos }
962 1.1 christos
963 1.12 christos const char *
964 1.12 christos bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
965 1.12 christos {
966 1.12 christos if (elf_sec_group (sec) != NULL)
967 1.12 christos return elf_group_name (sec);
968 1.12 christos return NULL;
969 1.12 christos }
970 1.12 christos
971 1.12 christos /* This a copy of lto_section defined in GCC (lto-streamer.h). */
972 1.12 christos
973 1.12 christos struct lto_section
974 1.12 christos {
975 1.12 christos int16_t major_version;
976 1.12 christos int16_t minor_version;
977 1.12 christos unsigned char slim_object;
978 1.12 christos
979 1.12 christos /* Flags is a private field that is not defined publicly. */
980 1.12 christos uint16_t flags;
981 1.12 christos };
982 1.12 christos
983 1.1 christos /* Make a BFD section from an ELF section. We store a pointer to the
984 1.1 christos BFD section in the bfd_section field of the header. */
985 1.1 christos
986 1.14 christos bool
987 1.1 christos _bfd_elf_make_section_from_shdr (bfd *abfd,
988 1.1 christos Elf_Internal_Shdr *hdr,
989 1.1 christos const char *name,
990 1.1 christos int shindex)
991 1.1 christos {
992 1.1 christos asection *newsect;
993 1.1 christos flagword flags;
994 1.1 christos const struct elf_backend_data *bed;
995 1.12 christos unsigned int opb = bfd_octets_per_byte (abfd, NULL);
996 1.1 christos
997 1.1 christos if (hdr->bfd_section != NULL)
998 1.14 christos return true;
999 1.1 christos
1000 1.1 christos newsect = bfd_make_section_anyway (abfd, name);
1001 1.1 christos if (newsect == NULL)
1002 1.14 christos return false;
1003 1.1 christos
1004 1.1 christos hdr->bfd_section = newsect;
1005 1.1 christos elf_section_data (newsect)->this_hdr = *hdr;
1006 1.1 christos elf_section_data (newsect)->this_idx = shindex;
1007 1.1 christos
1008 1.1 christos /* Always use the real type/flags. */
1009 1.1 christos elf_section_type (newsect) = hdr->sh_type;
1010 1.1 christos elf_section_flags (newsect) = hdr->sh_flags;
1011 1.1 christos
1012 1.1 christos newsect->filepos = hdr->sh_offset;
1013 1.1 christos
1014 1.1 christos flags = SEC_NO_FLAGS;
1015 1.1 christos if (hdr->sh_type != SHT_NOBITS)
1016 1.1 christos flags |= SEC_HAS_CONTENTS;
1017 1.1 christos if (hdr->sh_type == SHT_GROUP)
1018 1.11 christos flags |= SEC_GROUP;
1019 1.1 christos if ((hdr->sh_flags & SHF_ALLOC) != 0)
1020 1.1 christos {
1021 1.1 christos flags |= SEC_ALLOC;
1022 1.1 christos if (hdr->sh_type != SHT_NOBITS)
1023 1.1 christos flags |= SEC_LOAD;
1024 1.1 christos }
1025 1.1 christos if ((hdr->sh_flags & SHF_WRITE) == 0)
1026 1.1 christos flags |= SEC_READONLY;
1027 1.1 christos if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1028 1.1 christos flags |= SEC_CODE;
1029 1.1 christos else if ((flags & SEC_LOAD) != 0)
1030 1.1 christos flags |= SEC_DATA;
1031 1.1 christos if ((hdr->sh_flags & SHF_MERGE) != 0)
1032 1.1 christos {
1033 1.1 christos flags |= SEC_MERGE;
1034 1.1 christos newsect->entsize = hdr->sh_entsize;
1035 1.1 christos }
1036 1.8 christos if ((hdr->sh_flags & SHF_STRINGS) != 0)
1037 1.8 christos flags |= SEC_STRINGS;
1038 1.1 christos if (hdr->sh_flags & SHF_GROUP)
1039 1.1 christos if (!setup_group (abfd, hdr, newsect))
1040 1.14 christos return false;
1041 1.1 christos if ((hdr->sh_flags & SHF_TLS) != 0)
1042 1.1 christos flags |= SEC_THREAD_LOCAL;
1043 1.1 christos if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1044 1.1 christos flags |= SEC_EXCLUDE;
1045 1.1 christos
1046 1.12 christos switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1047 1.12 christos {
1048 1.12 christos /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1049 1.12 christos but binutils as of 2019-07-23 did not set the EI_OSABI header
1050 1.12 christos byte. */
1051 1.12 christos case ELFOSABI_GNU:
1052 1.12 christos case ELFOSABI_FREEBSD:
1053 1.14 christos if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
1054 1.14 christos elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
1055 1.14 christos /* Fall through */
1056 1.14 christos case ELFOSABI_NONE:
1057 1.12 christos if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1058 1.12 christos elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1059 1.12 christos break;
1060 1.12 christos }
1061 1.12 christos
1062 1.1 christos if ((flags & SEC_ALLOC) == 0)
1063 1.1 christos {
1064 1.1 christos /* The debugging sections appear to be recognized only by name,
1065 1.1 christos not any sort of flag. Their SEC_ALLOC bits are cleared. */
1066 1.1 christos if (name [0] == '.')
1067 1.1 christos {
1068 1.14 christos if (startswith (name, ".debug")
1069 1.14 christos || startswith (name, ".gnu.debuglto_.debug_")
1070 1.14 christos || startswith (name, ".gnu.linkonce.wi.")
1071 1.14 christos || startswith (name, ".zdebug"))
1072 1.12 christos flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
1073 1.14 christos else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
1074 1.14 christos || startswith (name, ".note.gnu"))
1075 1.12 christos {
1076 1.12 christos flags |= SEC_ELF_OCTETS;
1077 1.12 christos opb = 1;
1078 1.12 christos }
1079 1.14 christos else if (startswith (name, ".line")
1080 1.14 christos || startswith (name, ".stab")
1081 1.12 christos || strcmp (name, ".gdb_index") == 0)
1082 1.1 christos flags |= SEC_DEBUGGING;
1083 1.1 christos }
1084 1.1 christos }
1085 1.1 christos
1086 1.12 christos if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
1087 1.12 christos || !bfd_set_section_size (newsect, hdr->sh_size)
1088 1.14 christos || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
1089 1.14 christos & -hdr->sh_addralign)))
1090 1.14 christos return false;
1091 1.12 christos
1092 1.1 christos /* As a GNU extension, if the name begins with .gnu.linkonce, we
1093 1.1 christos only link a single copy of the section. This is used to support
1094 1.1 christos g++. g++ will emit each template expansion in its own section.
1095 1.1 christos The symbols will be defined as weak, so that multiple definitions
1096 1.1 christos are permitted. The GNU linker extension is to actually discard
1097 1.1 christos all but one of the sections. */
1098 1.14 christos if (startswith (name, ".gnu.linkonce")
1099 1.1 christos && elf_next_in_group (newsect) == NULL)
1100 1.1 christos flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1101 1.1 christos
1102 1.12 christos if (!bfd_set_section_flags (newsect, flags))
1103 1.14 christos return false;
1104 1.12 christos
1105 1.1 christos bed = get_elf_backend_data (abfd);
1106 1.1 christos if (bed->elf_backend_section_flags)
1107 1.12 christos if (!bed->elf_backend_section_flags (hdr))
1108 1.14 christos return false;
1109 1.1 christos
1110 1.1 christos /* We do not parse the PT_NOTE segments as we are interested even in the
1111 1.1 christos separate debug info files which may have the segments offsets corrupted.
1112 1.1 christos PT_NOTEs from the core files are currently not parsed using BFD. */
1113 1.14 christos if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
1114 1.1 christos {
1115 1.1 christos bfd_byte *contents;
1116 1.1 christos
1117 1.1 christos if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
1118 1.14 christos return false;
1119 1.1 christos
1120 1.11 christos elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1121 1.11 christos hdr->sh_offset, hdr->sh_addralign);
1122 1.1 christos free (contents);
1123 1.1 christos }
1124 1.1 christos
1125 1.12 christos if ((newsect->flags & SEC_ALLOC) != 0)
1126 1.1 christos {
1127 1.1 christos Elf_Internal_Phdr *phdr;
1128 1.1 christos unsigned int i, nload;
1129 1.1 christos
1130 1.1 christos /* Some ELF linkers produce binaries with all the program header
1131 1.1 christos p_paddr fields zero. If we have such a binary with more than
1132 1.1 christos one PT_LOAD header, then leave the section lma equal to vma
1133 1.1 christos so that we don't create sections with overlapping lma. */
1134 1.1 christos phdr = elf_tdata (abfd)->phdr;
1135 1.1 christos for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1136 1.1 christos if (phdr->p_paddr != 0)
1137 1.1 christos break;
1138 1.1 christos else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1139 1.1 christos ++nload;
1140 1.1 christos if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1141 1.14 christos return true;
1142 1.1 christos
1143 1.1 christos phdr = elf_tdata (abfd)->phdr;
1144 1.1 christos for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1145 1.1 christos {
1146 1.3 christos if (((phdr->p_type == PT_LOAD
1147 1.3 christos && (hdr->sh_flags & SHF_TLS) == 0)
1148 1.3 christos || phdr->p_type == PT_TLS)
1149 1.1 christos && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1150 1.1 christos {
1151 1.12 christos if ((newsect->flags & SEC_LOAD) == 0)
1152 1.1 christos newsect->lma = (phdr->p_paddr
1153 1.12 christos + hdr->sh_addr - phdr->p_vaddr) / opb;
1154 1.1 christos else
1155 1.1 christos /* We used to use the same adjustment for SEC_LOAD
1156 1.1 christos sections, but that doesn't work if the segment
1157 1.1 christos is packed with code from multiple VMAs.
1158 1.1 christos Instead we calculate the section LMA based on
1159 1.1 christos the segment LMA. It is assumed that the
1160 1.1 christos segment will contain sections with contiguous
1161 1.1 christos LMAs, even if the VMAs are not. */
1162 1.1 christos newsect->lma = (phdr->p_paddr
1163 1.12 christos + hdr->sh_offset - phdr->p_offset) / opb;
1164 1.1 christos
1165 1.1 christos /* With contiguous segments, we can't tell from file
1166 1.1 christos offsets whether a section with zero size should
1167 1.1 christos be placed at the end of one segment or the
1168 1.1 christos beginning of the next. Decide based on vaddr. */
1169 1.1 christos if (hdr->sh_addr >= phdr->p_vaddr
1170 1.1 christos && (hdr->sh_addr + hdr->sh_size
1171 1.1 christos <= phdr->p_vaddr + phdr->p_memsz))
1172 1.1 christos break;
1173 1.1 christos }
1174 1.1 christos }
1175 1.1 christos }
1176 1.1 christos
1177 1.14 christos /* Compress/decompress DWARF debug sections with names: .debug_*,
1178 1.14 christos .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set. */
1179 1.14 christos if ((newsect->flags & SEC_DEBUGGING) != 0
1180 1.14 christos && (newsect->flags & SEC_HAS_CONTENTS) != 0
1181 1.14 christos && (newsect->flags & SEC_ELF_OCTETS) != 0)
1182 1.1 christos {
1183 1.1 christos enum { nothing, compress, decompress } action = nothing;
1184 1.6 christos int compression_header_size;
1185 1.6 christos bfd_size_type uncompressed_size;
1186 1.11 christos unsigned int uncompressed_align_power;
1187 1.14 christos enum compression_type ch_type = ch_none;
1188 1.14 christos bool compressed
1189 1.14 christos = bfd_is_section_compressed_info (abfd, newsect,
1190 1.14 christos &compression_header_size,
1191 1.14 christos &uncompressed_size,
1192 1.14 christos &uncompressed_align_power,
1193 1.14 christos &ch_type);
1194 1.14 christos
1195 1.14 christos /* Should we decompress? */
1196 1.14 christos if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
1197 1.14 christos action = decompress;
1198 1.14 christos
1199 1.14 christos /* Should we compress? Or convert to a different compression? */
1200 1.14 christos else if ((abfd->flags & BFD_COMPRESS) != 0
1201 1.14 christos && newsect->size != 0
1202 1.14 christos && compression_header_size >= 0
1203 1.14 christos && uncompressed_size > 0)
1204 1.14 christos {
1205 1.14 christos if (!compressed)
1206 1.1 christos action = compress;
1207 1.6 christos else
1208 1.14 christos {
1209 1.14 christos enum compression_type new_ch_type = ch_none;
1210 1.14 christos if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
1211 1.14 christos new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
1212 1.14 christos ? ch_compress_zstd : ch_compress_zlib);
1213 1.14 christos if (new_ch_type != ch_type)
1214 1.14 christos action = compress;
1215 1.14 christos }
1216 1.1 christos }
1217 1.1 christos
1218 1.6 christos if (action == compress)
1219 1.1 christos {
1220 1.1 christos if (!bfd_init_section_compress_status (abfd, newsect))
1221 1.1 christos {
1222 1.9 christos _bfd_error_handler
1223 1.9 christos /* xgettext:c-format */
1224 1.14 christos (_("%pB: unable to compress section %s"), abfd, name);
1225 1.14 christos return false;
1226 1.1 christos }
1227 1.6 christos }
1228 1.14 christos else if (action == decompress)
1229 1.6 christos {
1230 1.1 christos if (!bfd_init_section_decompress_status (abfd, newsect))
1231 1.1 christos {
1232 1.9 christos _bfd_error_handler
1233 1.9 christos /* xgettext:c-format */
1234 1.14 christos (_("%pB: unable to decompress section %s"), abfd, name);
1235 1.14 christos return false;
1236 1.1 christos }
1237 1.14 christos #ifndef HAVE_ZSTD
1238 1.14 christos if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
1239 1.6 christos {
1240 1.14 christos _bfd_error_handler
1241 1.14 christos /* xgettext:c-format */
1242 1.14 christos (_ ("%pB: section %s is compressed with zstd, but BFD "
1243 1.14 christos "is not built with zstd support"),
1244 1.14 christos abfd, name);
1245 1.14 christos newsect->compress_status = COMPRESS_SECTION_NONE;
1246 1.14 christos return false;
1247 1.14 christos }
1248 1.14 christos #endif
1249 1.14 christos if (abfd->is_linker_input
1250 1.14 christos && name[1] == 'z')
1251 1.14 christos {
1252 1.14 christos /* Rename section from .zdebug_* to .debug_* so that ld
1253 1.14 christos scripts will see this section as a debug section. */
1254 1.14 christos char *new_name = bfd_zdebug_name_to_debug (abfd, name);
1255 1.1 christos if (new_name == NULL)
1256 1.14 christos return false;
1257 1.12 christos bfd_rename_section (newsect, new_name);
1258 1.1 christos }
1259 1.1 christos }
1260 1.1 christos }
1261 1.1 christos
1262 1.12 christos /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1263 1.12 christos section. */
1264 1.14 christos if (startswith (name, ".gnu.lto_.lto."))
1265 1.12 christos {
1266 1.12 christos struct lto_section lsection;
1267 1.12 christos if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
1268 1.12 christos sizeof (struct lto_section)))
1269 1.12 christos abfd->lto_slim_object = lsection.slim_object;
1270 1.12 christos }
1271 1.12 christos
1272 1.14 christos return true;
1273 1.1 christos }
1274 1.1 christos
1275 1.8 christos const char *const bfd_elf_section_type_names[] =
1276 1.8 christos {
1277 1.1 christos "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1278 1.1 christos "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1279 1.1 christos "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1280 1.1 christos };
1281 1.1 christos
1282 1.1 christos /* ELF relocs are against symbols. If we are producing relocatable
1283 1.1 christos output, and the reloc is against an external symbol, and nothing
1284 1.1 christos has given us any additional addend, the resulting reloc will also
1285 1.1 christos be against the same symbol. In such a case, we don't want to
1286 1.1 christos change anything about the way the reloc is handled, since it will
1287 1.1 christos all be done at final link time. Rather than put special case code
1288 1.1 christos into bfd_perform_relocation, all the reloc types use this howto
1289 1.14 christos function, or should call this function for relocatable output. */
1290 1.1 christos
1291 1.1 christos bfd_reloc_status_type
1292 1.1 christos bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1293 1.1 christos arelent *reloc_entry,
1294 1.1 christos asymbol *symbol,
1295 1.1 christos void *data ATTRIBUTE_UNUSED,
1296 1.1 christos asection *input_section,
1297 1.1 christos bfd *output_bfd,
1298 1.1 christos char **error_message ATTRIBUTE_UNUSED)
1299 1.1 christos {
1300 1.1 christos if (output_bfd != NULL
1301 1.1 christos && (symbol->flags & BSF_SECTION_SYM) == 0
1302 1.1 christos && (! reloc_entry->howto->partial_inplace
1303 1.1 christos || reloc_entry->addend == 0))
1304 1.1 christos {
1305 1.1 christos reloc_entry->address += input_section->output_offset;
1306 1.1 christos return bfd_reloc_ok;
1307 1.1 christos }
1308 1.1 christos
1309 1.14 christos /* In some cases the relocation should be treated as output section
1310 1.14 christos relative, as when linking ELF DWARF into PE COFF. Many ELF
1311 1.14 christos targets lack section relative relocations and instead use
1312 1.14 christos ordinary absolute relocations for references between DWARF
1313 1.14 christos sections. That is arguably a bug in those targets but it happens
1314 1.14 christos to work for the usual case of linking to non-loaded ELF debug
1315 1.14 christos sections with VMAs forced to zero. PE COFF on the other hand
1316 1.14 christos doesn't allow a section VMA of zero. */
1317 1.14 christos if (output_bfd == NULL
1318 1.14 christos && !reloc_entry->howto->pc_relative
1319 1.14 christos && (symbol->section->flags & SEC_DEBUGGING) != 0
1320 1.14 christos && (input_section->flags & SEC_DEBUGGING) != 0)
1321 1.14 christos reloc_entry->addend -= symbol->section->output_section->vma;
1322 1.14 christos
1323 1.1 christos return bfd_reloc_continue;
1324 1.1 christos }
1325 1.1 christos
1326 1.8 christos /* Returns TRUE if section A matches section B.
1328 1.8 christos Names, addresses and links may be different, but everything else
1329 1.8 christos should be the same. */
1330 1.14 christos
1331 1.8 christos static bool
1332 1.8 christos section_match (const Elf_Internal_Shdr * a,
1333 1.8 christos const Elf_Internal_Shdr * b)
1334 1.11 christos {
1335 1.11 christos if (a->sh_type != b->sh_type
1336 1.11 christos || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1337 1.11 christos || a->sh_addralign != b->sh_addralign
1338 1.14 christos || a->sh_entsize != b->sh_entsize)
1339 1.11 christos return false;
1340 1.11 christos if (a->sh_type == SHT_SYMTAB
1341 1.14 christos || a->sh_type == SHT_STRTAB)
1342 1.11 christos return true;
1343 1.8 christos return a->sh_size == b->sh_size;
1344 1.8 christos }
1345 1.8 christos
1346 1.8 christos /* Find a section in OBFD that has the same characteristics
1347 1.8 christos as IHEADER. Return the index of this section or SHN_UNDEF if
1348 1.8 christos none can be found. Check's section HINT first, as this is likely
1349 1.8 christos to be the correct section. */
1350 1.8 christos
1351 1.11 christos static unsigned int
1352 1.11 christos find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1353 1.8 christos const unsigned int hint)
1354 1.8 christos {
1355 1.8 christos Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1356 1.8 christos unsigned int i;
1357 1.9 christos
1358 1.9 christos BFD_ASSERT (iheader != NULL);
1359 1.9 christos
1360 1.11 christos /* See PR 20922 for a reproducer of the NULL test. */
1361 1.11 christos if (hint < elf_numsections (obfd)
1362 1.9 christos && oheaders[hint] != NULL
1363 1.8 christos && section_match (oheaders[hint], iheader))
1364 1.8 christos return hint;
1365 1.8 christos
1366 1.8 christos for (i = 1; i < elf_numsections (obfd); i++)
1367 1.8 christos {
1368 1.8 christos Elf_Internal_Shdr * oheader = oheaders[i];
1369 1.9 christos
1370 1.9 christos if (oheader == NULL)
1371 1.8 christos continue;
1372 1.8 christos if (section_match (oheader, iheader))
1373 1.8 christos /* FIXME: Do we care if there is a potential for
1374 1.8 christos multiple matches ? */
1375 1.8 christos return i;
1376 1.8 christos }
1377 1.8 christos
1378 1.8 christos return SHN_UNDEF;
1379 1.8 christos }
1380 1.8 christos
1381 1.8 christos /* PR 19938: Attempt to set the ELF section header fields of an OS or
1382 1.8 christos Processor specific section, based upon a matching input section.
1383 1.11 christos Returns TRUE upon success, FALSE otherwise. */
1384 1.14 christos
1385 1.8 christos static bool
1386 1.8 christos copy_special_section_fields (const bfd *ibfd,
1387 1.8 christos bfd *obfd,
1388 1.8 christos const Elf_Internal_Shdr *iheader,
1389 1.8 christos Elf_Internal_Shdr *oheader,
1390 1.8 christos const unsigned int secnum)
1391 1.8 christos {
1392 1.14 christos const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1393 1.14 christos const Elf_Internal_Shdr **iheaders
1394 1.14 christos = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1395 1.8 christos bool changed = false;
1396 1.8 christos unsigned int sh_link;
1397 1.8 christos
1398 1.8 christos if (oheader->sh_type == SHT_NOBITS)
1399 1.8 christos {
1400 1.8 christos /* This is a feature for objcopy --only-keep-debug:
1401 1.8 christos When a section's type is changed to NOBITS, we preserve
1402 1.8 christos the sh_link and sh_info fields so that they can be
1403 1.8 christos matched up with the original.
1404 1.8 christos
1405 1.8 christos Note: Strictly speaking these assignments are wrong.
1406 1.8 christos The sh_link and sh_info fields should point to the
1407 1.8 christos relevent sections in the output BFD, which may not be in
1408 1.8 christos the same location as they were in the input BFD. But
1409 1.8 christos the whole point of this action is to preserve the
1410 1.8 christos original values of the sh_link and sh_info fields, so
1411 1.8 christos that they can be matched up with the section headers in
1412 1.8 christos the original file. So strictly speaking we may be
1413 1.8 christos creating an invalid ELF file, but it is only for a file
1414 1.8 christos that just contains debug info and only for sections
1415 1.8 christos without any contents. */
1416 1.8 christos if (oheader->sh_link == 0)
1417 1.8 christos oheader->sh_link = iheader->sh_link;
1418 1.8 christos if (oheader->sh_info == 0)
1419 1.14 christos oheader->sh_info = iheader->sh_info;
1420 1.8 christos return true;
1421 1.8 christos }
1422 1.8 christos
1423 1.12 christos /* Allow the target a chance to decide how these fields should be set. */
1424 1.12 christos if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1425 1.14 christos iheader, oheader))
1426 1.8 christos return true;
1427 1.8 christos
1428 1.8 christos /* We have an iheader which might match oheader, and which has non-zero
1429 1.8 christos sh_info and/or sh_link fields. Attempt to follow those links and find
1430 1.8 christos the section in the output bfd which corresponds to the linked section
1431 1.8 christos in the input bfd. */
1432 1.8 christos if (iheader->sh_link != SHN_UNDEF)
1433 1.9 christos {
1434 1.9 christos /* See PR 20931 for a reproducer. */
1435 1.9 christos if (iheader->sh_link >= elf_numsections (ibfd))
1436 1.11 christos {
1437 1.9 christos _bfd_error_handler
1438 1.11 christos /* xgettext:c-format */
1439 1.9 christos (_("%pB: invalid sh_link field (%d) in section number %d"),
1440 1.14 christos ibfd, iheader->sh_link, secnum);
1441 1.9 christos return false;
1442 1.9 christos }
1443 1.8 christos
1444 1.8 christos sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1445 1.8 christos if (sh_link != SHN_UNDEF)
1446 1.8 christos {
1447 1.14 christos oheader->sh_link = sh_link;
1448 1.8 christos changed = true;
1449 1.8 christos }
1450 1.8 christos else
1451 1.8 christos /* FIXME: Should we install iheader->sh_link
1452 1.11 christos if we could not find a match ? */
1453 1.9 christos _bfd_error_handler
1454 1.11 christos /* xgettext:c-format */
1455 1.8 christos (_("%pB: failed to find link section for section %d"), obfd, secnum);
1456 1.8 christos }
1457 1.8 christos
1458 1.8 christos if (iheader->sh_info)
1459 1.8 christos {
1460 1.8 christos /* The sh_info field can hold arbitrary information, but if the
1461 1.8 christos SHF_LINK_INFO flag is set then it should be interpreted as a
1462 1.8 christos section index. */
1463 1.8 christos if (iheader->sh_flags & SHF_INFO_LINK)
1464 1.8 christos {
1465 1.8 christos sh_link = find_link (obfd, iheaders[iheader->sh_info],
1466 1.8 christos iheader->sh_info);
1467 1.8 christos if (sh_link != SHN_UNDEF)
1468 1.8 christos oheader->sh_flags |= SHF_INFO_LINK;
1469 1.8 christos }
1470 1.8 christos else
1471 1.8 christos /* No idea what it means - just copy it. */
1472 1.8 christos sh_link = iheader->sh_info;
1473 1.8 christos
1474 1.8 christos if (sh_link != SHN_UNDEF)
1475 1.8 christos {
1476 1.14 christos oheader->sh_info = sh_link;
1477 1.8 christos changed = true;
1478 1.8 christos }
1479 1.11 christos else
1480 1.9 christos _bfd_error_handler
1481 1.11 christos /* xgettext:c-format */
1482 1.8 christos (_("%pB: failed to find info section for section %d"), obfd, secnum);
1483 1.8 christos }
1484 1.8 christos
1485 1.8 christos return changed;
1486 1.11 christos }
1487 1.1 christos
1488 1.1 christos /* Copy the program header and other data from one object module to
1489 1.1 christos another. */
1490 1.14 christos
1491 1.1 christos bool
1492 1.1 christos _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1493 1.14 christos {
1494 1.14 christos const Elf_Internal_Shdr **iheaders
1495 1.8 christos = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1496 1.8 christos Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1497 1.8 christos const struct elf_backend_data *bed;
1498 1.8 christos unsigned int i;
1499 1.1 christos
1500 1.8 christos if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1501 1.14 christos || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1502 1.1 christos return true;
1503 1.4 christos
1504 1.4 christos if (!elf_flags_init (obfd))
1505 1.4 christos {
1506 1.14 christos elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1507 1.4 christos elf_flags_init (obfd) = true;
1508 1.1 christos }
1509 1.1 christos
1510 1.4 christos elf_gp (obfd) = elf_gp (ibfd);
1511 1.4 christos
1512 1.4 christos /* Also copy the EI_OSABI field. */
1513 1.4 christos elf_elfheader (obfd)->e_ident[EI_OSABI] =
1514 1.1 christos elf_elfheader (ibfd)->e_ident[EI_OSABI];
1515 1.8 christos
1516 1.8 christos /* If set, copy the EI_ABIVERSION field. */
1517 1.8 christos if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1518 1.8 christos elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1519 1.11 christos = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1520 1.1 christos
1521 1.1 christos /* Copy object attributes. */
1522 1.8 christos _bfd_elf_copy_obj_attributes (ibfd, obfd);
1523 1.8 christos
1524 1.14 christos if (iheaders == NULL || oheaders == NULL)
1525 1.8 christos return true;
1526 1.8 christos
1527 1.8 christos bed = get_elf_backend_data (obfd);
1528 1.8 christos
1529 1.8 christos /* Possibly copy other fields in the section header. */
1530 1.8 christos for (i = 1; i < elf_numsections (obfd); i++)
1531 1.8 christos {
1532 1.8 christos unsigned int j;
1533 1.8 christos Elf_Internal_Shdr * oheader = oheaders[i];
1534 1.8 christos
1535 1.8 christos /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1536 1.8 christos because of a special case need for generating separate debug info
1537 1.8 christos files. See below for more details. */
1538 1.8 christos if (oheader == NULL
1539 1.8 christos || (oheader->sh_type != SHT_NOBITS
1540 1.8 christos && oheader->sh_type < SHT_LOOS))
1541 1.8 christos continue;
1542 1.8 christos
1543 1.8 christos /* Ignore empty sections, and sections whose
1544 1.8 christos fields have already been initialised. */
1545 1.8 christos if (oheader->sh_size == 0
1546 1.8 christos || (oheader->sh_info != 0 && oheader->sh_link != 0))
1547 1.8 christos continue;
1548 1.8 christos
1549 1.14 christos /* Scan for the matching section in the input bfd.
1550 1.14 christos First we try for a direct mapping between the input and
1551 1.8 christos output sections. */
1552 1.8 christos for (j = 1; j < elf_numsections (ibfd); j++)
1553 1.8 christos {
1554 1.8 christos const Elf_Internal_Shdr * iheader = iheaders[j];
1555 1.8 christos
1556 1.8 christos if (iheader == NULL)
1557 1.8 christos continue;
1558 1.8 christos
1559 1.8 christos if (oheader->bfd_section != NULL
1560 1.8 christos && iheader->bfd_section != NULL
1561 1.8 christos && iheader->bfd_section->output_section != NULL
1562 1.8 christos && iheader->bfd_section->output_section == oheader->bfd_section)
1563 1.14 christos {
1564 1.14 christos /* We have found a connection from the input section to
1565 1.14 christos the output section. Attempt to copy the header fields.
1566 1.14 christos If this fails then do not try any further sections -
1567 1.14 christos there should only be a one-to-one mapping between
1568 1.14 christos input and output. */
1569 1.14 christos if (!copy_special_section_fields (ibfd, obfd,
1570 1.8 christos iheader, oheader, i))
1571 1.8 christos j = elf_numsections (ibfd);
1572 1.8 christos break;
1573 1.8 christos }
1574 1.8 christos }
1575 1.8 christos
1576 1.8 christos if (j < elf_numsections (ibfd))
1577 1.8 christos continue;
1578 1.8 christos
1579 1.8 christos /* That failed. So try to deduce the corresponding input section.
1580 1.8 christos Unfortunately we cannot compare names as the output string table
1581 1.8 christos is empty, so instead we check size, address and type. */
1582 1.8 christos for (j = 1; j < elf_numsections (ibfd); j++)
1583 1.8 christos {
1584 1.8 christos const Elf_Internal_Shdr * iheader = iheaders[j];
1585 1.8 christos
1586 1.8 christos if (iheader == NULL)
1587 1.8 christos continue;
1588 1.8 christos
1589 1.8 christos /* Try matching fields in the input section's header.
1590 1.8 christos Since --only-keep-debug turns all non-debug sections into
1591 1.8 christos SHT_NOBITS sections, the output SHT_NOBITS type matches any
1592 1.8 christos input type. */
1593 1.8 christos if ((oheader->sh_type == SHT_NOBITS
1594 1.8 christos || iheader->sh_type == oheader->sh_type)
1595 1.8 christos && (iheader->sh_flags & ~ SHF_INFO_LINK)
1596 1.8 christos == (oheader->sh_flags & ~ SHF_INFO_LINK)
1597 1.8 christos && iheader->sh_addralign == oheader->sh_addralign
1598 1.8 christos && iheader->sh_entsize == oheader->sh_entsize
1599 1.8 christos && iheader->sh_size == oheader->sh_size
1600 1.8 christos && iheader->sh_addr == oheader->sh_addr
1601 1.8 christos && (iheader->sh_info != oheader->sh_info
1602 1.8 christos || iheader->sh_link != oheader->sh_link))
1603 1.8 christos {
1604 1.8 christos if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1605 1.8 christos break;
1606 1.8 christos }
1607 1.8 christos }
1608 1.8 christos
1609 1.8 christos if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1610 1.8 christos {
1611 1.8 christos /* Final attempt. Call the backend copy function
1612 1.12 christos with a NULL input section. */
1613 1.12 christos (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1614 1.8 christos NULL, oheader);
1615 1.8 christos }
1616 1.8 christos }
1617 1.14 christos
1618 1.1 christos return true;
1619 1.1 christos }
1620 1.1 christos
1621 1.1 christos static const char *
1622 1.1 christos get_segment_type (unsigned int p_type)
1623 1.1 christos {
1624 1.1 christos const char *pt;
1625 1.1 christos switch (p_type)
1626 1.1 christos {
1627 1.1 christos case PT_NULL: pt = "NULL"; break;
1628 1.1 christos case PT_LOAD: pt = "LOAD"; break;
1629 1.1 christos case PT_DYNAMIC: pt = "DYNAMIC"; break;
1630 1.1 christos case PT_INTERP: pt = "INTERP"; break;
1631 1.1 christos case PT_NOTE: pt = "NOTE"; break;
1632 1.1 christos case PT_SHLIB: pt = "SHLIB"; break;
1633 1.1 christos case PT_PHDR: pt = "PHDR"; break;
1634 1.1 christos case PT_TLS: pt = "TLS"; break;
1635 1.1 christos case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1636 1.1 christos case PT_GNU_STACK: pt = "STACK"; break;
1637 1.14 christos case PT_GNU_RELRO: pt = "RELRO"; break;
1638 1.1 christos case PT_GNU_SFRAME: pt = "SFRAME"; break;
1639 1.1 christos default: pt = NULL; break;
1640 1.1 christos }
1641 1.1 christos return pt;
1642 1.1 christos }
1643 1.1 christos
1644 1.1 christos /* Print out the program headers. */
1645 1.14 christos
1646 1.1 christos bool
1647 1.1 christos _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1648 1.1 christos {
1649 1.1 christos FILE *f = (FILE *) farg;
1650 1.1 christos Elf_Internal_Phdr *p;
1651 1.1 christos asection *s;
1652 1.1 christos bfd_byte *dynbuf = NULL;
1653 1.1 christos
1654 1.1 christos p = elf_tdata (abfd)->phdr;
1655 1.1 christos if (p != NULL)
1656 1.1 christos {
1657 1.1 christos unsigned int i, c;
1658 1.1 christos
1659 1.1 christos fprintf (f, _("\nProgram Header:\n"));
1660 1.1 christos c = elf_elfheader (abfd)->e_phnum;
1661 1.1 christos for (i = 0; i < c; i++, p++)
1662 1.1 christos {
1663 1.1 christos const char *pt = get_segment_type (p->p_type);
1664 1.1 christos char buf[20];
1665 1.1 christos
1666 1.1 christos if (pt == NULL)
1667 1.1 christos {
1668 1.1 christos sprintf (buf, "0x%lx", p->p_type);
1669 1.1 christos pt = buf;
1670 1.1 christos }
1671 1.1 christos fprintf (f, "%8s off 0x", pt);
1672 1.1 christos bfd_fprintf_vma (abfd, f, p->p_offset);
1673 1.1 christos fprintf (f, " vaddr 0x");
1674 1.1 christos bfd_fprintf_vma (abfd, f, p->p_vaddr);
1675 1.1 christos fprintf (f, " paddr 0x");
1676 1.1 christos bfd_fprintf_vma (abfd, f, p->p_paddr);
1677 1.1 christos fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1678 1.1 christos fprintf (f, " filesz 0x");
1679 1.1 christos bfd_fprintf_vma (abfd, f, p->p_filesz);
1680 1.1 christos fprintf (f, " memsz 0x");
1681 1.1 christos bfd_fprintf_vma (abfd, f, p->p_memsz);
1682 1.1 christos fprintf (f, " flags %c%c%c",
1683 1.1 christos (p->p_flags & PF_R) != 0 ? 'r' : '-',
1684 1.1 christos (p->p_flags & PF_W) != 0 ? 'w' : '-',
1685 1.1 christos (p->p_flags & PF_X) != 0 ? 'x' : '-');
1686 1.1 christos if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1687 1.1 christos fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1688 1.1 christos fprintf (f, "\n");
1689 1.1 christos }
1690 1.1 christos }
1691 1.1 christos
1692 1.1 christos s = bfd_get_section_by_name (abfd, ".dynamic");
1693 1.1 christos if (s != NULL)
1694 1.1 christos {
1695 1.1 christos unsigned int elfsec;
1696 1.1 christos unsigned long shlink;
1697 1.1 christos bfd_byte *extdyn, *extdynend;
1698 1.1 christos size_t extdynsize;
1699 1.1 christos void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1700 1.1 christos
1701 1.1 christos fprintf (f, _("\nDynamic Section:\n"));
1702 1.1 christos
1703 1.1 christos if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1704 1.1 christos goto error_return;
1705 1.1 christos
1706 1.1 christos elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1707 1.1 christos if (elfsec == SHN_BAD)
1708 1.1 christos goto error_return;
1709 1.1 christos shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1710 1.1 christos
1711 1.1 christos extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1712 1.1 christos swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1713 1.14 christos
1714 1.14 christos for (extdyn = dynbuf, extdynend = dynbuf + s->size;
1715 1.14 christos (size_t) (extdynend - extdyn) >= extdynsize;
1716 1.1 christos extdyn += extdynsize)
1717 1.1 christos {
1718 1.1 christos Elf_Internal_Dyn dyn;
1719 1.1 christos const char *name = "";
1720 1.14 christos char ab[20];
1721 1.1 christos bool stringp;
1722 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1723 1.1 christos
1724 1.1 christos (*swap_dyn_in) (abfd, extdyn, &dyn);
1725 1.1 christos
1726 1.1 christos if (dyn.d_tag == DT_NULL)
1727 1.1 christos break;
1728 1.14 christos
1729 1.1 christos stringp = false;
1730 1.1 christos switch (dyn.d_tag)
1731 1.1 christos {
1732 1.1 christos default:
1733 1.1 christos if (bed->elf_backend_get_target_dtag)
1734 1.1 christos name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1735 1.1 christos
1736 1.1 christos if (!strcmp (name, ""))
1737 1.14 christos {
1738 1.1 christos sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
1739 1.1 christos name = ab;
1740 1.1 christos }
1741 1.1 christos break;
1742 1.14 christos
1743 1.1 christos case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1744 1.1 christos case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1745 1.1 christos case DT_PLTGOT: name = "PLTGOT"; break;
1746 1.1 christos case DT_HASH: name = "HASH"; break;
1747 1.1 christos case DT_STRTAB: name = "STRTAB"; break;
1748 1.1 christos case DT_SYMTAB: name = "SYMTAB"; break;
1749 1.1 christos case DT_RELA: name = "RELA"; break;
1750 1.1 christos case DT_RELASZ: name = "RELASZ"; break;
1751 1.1 christos case DT_RELAENT: name = "RELAENT"; break;
1752 1.1 christos case DT_STRSZ: name = "STRSZ"; break;
1753 1.1 christos case DT_SYMENT: name = "SYMENT"; break;
1754 1.1 christos case DT_INIT: name = "INIT"; break;
1755 1.14 christos case DT_FINI: name = "FINI"; break;
1756 1.14 christos case DT_SONAME: name = "SONAME"; stringp = true; break;
1757 1.1 christos case DT_RPATH: name = "RPATH"; stringp = true; break;
1758 1.1 christos case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1759 1.1 christos case DT_REL: name = "REL"; break;
1760 1.1 christos case DT_RELSZ: name = "RELSZ"; break;
1761 1.14 christos case DT_RELENT: name = "RELENT"; break;
1762 1.14 christos case DT_RELR: name = "RELR"; break;
1763 1.14 christos case DT_RELRSZ: name = "RELRSZ"; break;
1764 1.1 christos case DT_RELRENT: name = "RELRENT"; break;
1765 1.1 christos case DT_PLTREL: name = "PLTREL"; break;
1766 1.1 christos case DT_DEBUG: name = "DEBUG"; break;
1767 1.1 christos case DT_TEXTREL: name = "TEXTREL"; break;
1768 1.1 christos case DT_JMPREL: name = "JMPREL"; break;
1769 1.1 christos case DT_BIND_NOW: name = "BIND_NOW"; break;
1770 1.1 christos case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1771 1.1 christos case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1772 1.1 christos case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1773 1.14 christos case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1774 1.1 christos case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1775 1.1 christos case DT_FLAGS: name = "FLAGS"; break;
1776 1.1 christos case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1777 1.1 christos case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1778 1.1 christos case DT_CHECKSUM: name = "CHECKSUM"; break;
1779 1.1 christos case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1780 1.1 christos case DT_MOVEENT: name = "MOVEENT"; break;
1781 1.1 christos case DT_MOVESZ: name = "MOVESZ"; break;
1782 1.1 christos case DT_FEATURE: name = "FEATURE"; break;
1783 1.1 christos case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1784 1.1 christos case DT_SYMINSZ: name = "SYMINSZ"; break;
1785 1.14 christos case DT_SYMINENT: name = "SYMINENT"; break;
1786 1.14 christos case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1787 1.14 christos case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1788 1.1 christos case DT_AUDIT: name = "AUDIT"; stringp = true; break;
1789 1.1 christos case DT_PLTPAD: name = "PLTPAD"; break;
1790 1.1 christos case DT_MOVETAB: name = "MOVETAB"; break;
1791 1.1 christos case DT_SYMINFO: name = "SYMINFO"; break;
1792 1.1 christos case DT_RELACOUNT: name = "RELACOUNT"; break;
1793 1.1 christos case DT_RELCOUNT: name = "RELCOUNT"; break;
1794 1.1 christos case DT_FLAGS_1: name = "FLAGS_1"; break;
1795 1.1 christos case DT_VERSYM: name = "VERSYM"; break;
1796 1.1 christos case DT_VERDEF: name = "VERDEF"; break;
1797 1.1 christos case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1798 1.1 christos case DT_VERNEED: name = "VERNEED"; break;
1799 1.14 christos case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1800 1.1 christos case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1801 1.14 christos case DT_USED: name = "USED"; break;
1802 1.1 christos case DT_FILTER: name = "FILTER"; stringp = true; break;
1803 1.1 christos case DT_GNU_HASH: name = "GNU_HASH"; break;
1804 1.1 christos }
1805 1.1 christos
1806 1.1 christos fprintf (f, " %-20s ", name);
1807 1.1 christos if (! stringp)
1808 1.1 christos {
1809 1.1 christos fprintf (f, "0x");
1810 1.1 christos bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1811 1.1 christos }
1812 1.1 christos else
1813 1.1 christos {
1814 1.1 christos const char *string;
1815 1.1 christos unsigned int tagv = dyn.d_un.d_val;
1816 1.1 christos
1817 1.1 christos string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1818 1.1 christos if (string == NULL)
1819 1.1 christos goto error_return;
1820 1.1 christos fprintf (f, "%s", string);
1821 1.1 christos }
1822 1.1 christos fprintf (f, "\n");
1823 1.1 christos }
1824 1.1 christos
1825 1.1 christos free (dynbuf);
1826 1.1 christos dynbuf = NULL;
1827 1.1 christos }
1828 1.1 christos
1829 1.1 christos if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1830 1.1 christos || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1831 1.14 christos {
1832 1.14 christos if (! _bfd_elf_slurp_version_tables (abfd, false))
1833 1.1 christos return false;
1834 1.1 christos }
1835 1.1 christos
1836 1.1 christos if (elf_dynverdef (abfd) != 0)
1837 1.1 christos {
1838 1.1 christos Elf_Internal_Verdef *t;
1839 1.1 christos
1840 1.1 christos fprintf (f, _("\nVersion definitions:\n"));
1841 1.1 christos for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1842 1.1 christos {
1843 1.1 christos fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1844 1.1 christos t->vd_flags, t->vd_hash,
1845 1.1 christos t->vd_nodename ? t->vd_nodename : "<corrupt>");
1846 1.1 christos if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1847 1.1 christos {
1848 1.1 christos Elf_Internal_Verdaux *a;
1849 1.1 christos
1850 1.1 christos fprintf (f, "\t");
1851 1.1 christos for (a = t->vd_auxptr->vda_nextptr;
1852 1.1 christos a != NULL;
1853 1.1 christos a = a->vda_nextptr)
1854 1.1 christos fprintf (f, "%s ",
1855 1.1 christos a->vda_nodename ? a->vda_nodename : "<corrupt>");
1856 1.1 christos fprintf (f, "\n");
1857 1.1 christos }
1858 1.1 christos }
1859 1.1 christos }
1860 1.1 christos
1861 1.1 christos if (elf_dynverref (abfd) != 0)
1862 1.1 christos {
1863 1.1 christos Elf_Internal_Verneed *t;
1864 1.1 christos
1865 1.1 christos fprintf (f, _("\nVersion References:\n"));
1866 1.1 christos for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1867 1.1 christos {
1868 1.1 christos Elf_Internal_Vernaux *a;
1869 1.1 christos
1870 1.1 christos fprintf (f, _(" required from %s:\n"),
1871 1.1 christos t->vn_filename ? t->vn_filename : "<corrupt>");
1872 1.1 christos for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1873 1.1 christos fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1874 1.1 christos a->vna_flags, a->vna_other,
1875 1.1 christos a->vna_nodename ? a->vna_nodename : "<corrupt>");
1876 1.1 christos }
1877 1.1 christos }
1878 1.14 christos
1879 1.1 christos return true;
1880 1.1 christos
1881 1.12 christos error_return:
1882 1.14 christos free (dynbuf);
1883 1.1 christos return false;
1884 1.1 christos }
1885 1.12 christos
1886 1.12 christos /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
1887 1.5 christos and return symbol version for symbol version itself. */
1888 1.5 christos
1889 1.5 christos const char *
1890 1.14 christos _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1891 1.14 christos bool base_p,
1892 1.5 christos bool *hidden)
1893 1.5 christos {
1894 1.5 christos const char *version_string = NULL;
1895 1.5 christos if (elf_dynversym (abfd) != 0
1896 1.5 christos && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1897 1.5 christos {
1898 1.5 christos unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1899 1.5 christos
1900 1.5 christos *hidden = (vernum & VERSYM_HIDDEN) != 0;
1901 1.5 christos vernum &= VERSYM_VERSION;
1902 1.5 christos
1903 1.5 christos if (vernum == 0)
1904 1.11 christos version_string = "";
1905 1.11 christos else if (vernum == 1
1906 1.11 christos && (vernum > elf_tdata (abfd)->cverdefs
1907 1.11 christos || (elf_tdata (abfd)->verdef[0].vd_flags
1908 1.12 christos == VER_FLG_BASE)))
1909 1.5 christos version_string = base_p ? "Base" : "";
1910 1.12 christos else if (vernum <= elf_tdata (abfd)->cverdefs)
1911 1.12 christos {
1912 1.12 christos const char *nodename
1913 1.12 christos = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1914 1.12 christos version_string = "";
1915 1.12 christos if (base_p
1916 1.12 christos || nodename == NULL
1917 1.12 christos || symbol->name == NULL
1918 1.12 christos || strcmp (symbol->name, nodename) != 0)
1919 1.12 christos version_string = nodename;
1920 1.5 christos }
1921 1.5 christos else
1922 1.5 christos {
1923 1.5 christos Elf_Internal_Verneed *t;
1924 1.11 christos
1925 1.5 christos version_string = _("<corrupt>");
1926 1.5 christos for (t = elf_tdata (abfd)->verref;
1927 1.5 christos t != NULL;
1928 1.5 christos t = t->vn_nextref)
1929 1.5 christos {
1930 1.5 christos Elf_Internal_Vernaux *a;
1931 1.5 christos
1932 1.5 christos for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1933 1.5 christos {
1934 1.5 christos if (a->vna_other == vernum)
1935 1.14 christos {
1936 1.5 christos *hidden = true;
1937 1.5 christos version_string = a->vna_nodename;
1938 1.5 christos break;
1939 1.5 christos }
1940 1.5 christos }
1941 1.5 christos }
1942 1.5 christos }
1943 1.5 christos }
1944 1.5 christos return version_string;
1945 1.5 christos }
1946 1.1 christos
1947 1.1 christos /* Display ELF-specific fields of a symbol. */
1948 1.1 christos
1949 1.1 christos void
1950 1.1 christos bfd_elf_print_symbol (bfd *abfd,
1951 1.1 christos void *filep,
1952 1.1 christos asymbol *symbol,
1953 1.1 christos bfd_print_symbol_type how)
1954 1.1 christos {
1955 1.1 christos FILE *file = (FILE *) filep;
1956 1.1 christos switch (how)
1957 1.1 christos {
1958 1.1 christos case bfd_print_symbol_name:
1959 1.1 christos fprintf (file, "%s", symbol->name);
1960 1.1 christos break;
1961 1.1 christos case bfd_print_symbol_more:
1962 1.1 christos fprintf (file, "elf ");
1963 1.11 christos bfd_fprintf_vma (abfd, file, symbol->value);
1964 1.1 christos fprintf (file, " %x", symbol->flags);
1965 1.1 christos break;
1966 1.1 christos case bfd_print_symbol_all:
1967 1.1 christos {
1968 1.1 christos const char *section_name;
1969 1.1 christos const char *name = NULL;
1970 1.1 christos const struct elf_backend_data *bed;
1971 1.1 christos unsigned char st_other;
1972 1.5 christos bfd_vma val;
1973 1.14 christos const char *version_string;
1974 1.1 christos bool hidden;
1975 1.1 christos
1976 1.1 christos section_name = symbol->section ? symbol->section->name : "(*none*)";
1977 1.1 christos
1978 1.1 christos bed = get_elf_backend_data (abfd);
1979 1.1 christos if (bed->elf_backend_print_symbol_all)
1980 1.1 christos name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1981 1.1 christos
1982 1.1 christos if (name == NULL)
1983 1.1 christos {
1984 1.1 christos name = symbol->name;
1985 1.1 christos bfd_print_symbol_vandf (abfd, file, symbol);
1986 1.1 christos }
1987 1.1 christos
1988 1.1 christos fprintf (file, " %s\t", section_name);
1989 1.1 christos /* Print the "other" value for a symbol. For common symbols,
1990 1.1 christos we've already printed the size; now print the alignment.
1991 1.1 christos For other symbols, we have no specified alignment, and
1992 1.1 christos we've printed the address; now print the size. */
1993 1.1 christos if (symbol->section && bfd_is_com_section (symbol->section))
1994 1.1 christos val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1995 1.1 christos else
1996 1.1 christos val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1997 1.1 christos bfd_fprintf_vma (abfd, file, val);
1998 1.1 christos
1999 1.5 christos /* If we have version information, print it. */
2000 1.5 christos version_string = _bfd_elf_get_symbol_version_string (abfd,
2001 1.14 christos symbol,
2002 1.5 christos true,
2003 1.5 christos &hidden);
2004 1.1 christos if (version_string)
2005 1.5 christos {
2006 1.1 christos if (!hidden)
2007 1.1 christos fprintf (file, " %-11s", version_string);
2008 1.1 christos else
2009 1.1 christos {
2010 1.1 christos int i;
2011 1.1 christos
2012 1.1 christos fprintf (file, " (%s)", version_string);
2013 1.1 christos for (i = 10 - strlen (version_string); i > 0; --i)
2014 1.1 christos putc (' ', file);
2015 1.1 christos }
2016 1.1 christos }
2017 1.1 christos
2018 1.1 christos /* If the st_other field is not zero, print it. */
2019 1.1 christos st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2020 1.1 christos
2021 1.1 christos switch (st_other)
2022 1.1 christos {
2023 1.1 christos case 0: break;
2024 1.1 christos case STV_INTERNAL: fprintf (file, " .internal"); break;
2025 1.1 christos case STV_HIDDEN: fprintf (file, " .hidden"); break;
2026 1.1 christos case STV_PROTECTED: fprintf (file, " .protected"); break;
2027 1.1 christos default:
2028 1.1 christos /* Some other non-defined flags are also present, so print
2029 1.1 christos everything hex. */
2030 1.1 christos fprintf (file, " 0x%02x", (unsigned int) st_other);
2031 1.1 christos }
2032 1.1 christos
2033 1.1 christos fprintf (file, " %s", name);
2034 1.1 christos }
2035 1.1 christos break;
2036 1.1 christos }
2037 1.1 christos }
2038 1.1 christos
2039 1.1 christos /* ELF .o/exec file reading */
2041 1.1 christos
2042 1.14 christos /* Create a new bfd section from an ELF section header. */
2043 1.1 christos
2044 1.1 christos bool
2045 1.1 christos bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2046 1.1 christos {
2047 1.1 christos Elf_Internal_Shdr *hdr;
2048 1.1 christos Elf_Internal_Ehdr *ehdr;
2049 1.14 christos const struct elf_backend_data *bed;
2050 1.1 christos const char *name;
2051 1.1 christos bool ret = true;
2052 1.14 christos
2053 1.1 christos if (shindex >= elf_numsections (abfd))
2054 1.14 christos return false;
2055 1.14 christos
2056 1.14 christos /* PR17512: A corrupt ELF binary might contain a loop of sections via
2057 1.14 christos sh_link or sh_info. Detect this here, by refusing to load a
2058 1.5 christos section that we are already in the process of loading. */
2059 1.14 christos if (elf_tdata (abfd)->being_created[shindex])
2060 1.14 christos {
2061 1.14 christos _bfd_error_handler
2062 1.5 christos (_("%pB: warning: loop in section dependencies detected"), abfd);
2063 1.14 christos return false;
2064 1.5 christos }
2065 1.1 christos elf_tdata (abfd)->being_created[shindex] = true;
2066 1.1 christos
2067 1.1 christos hdr = elf_elfsections (abfd)[shindex];
2068 1.1 christos ehdr = elf_elfheader (abfd);
2069 1.1 christos name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2070 1.5 christos hdr->sh_name);
2071 1.1 christos if (name == NULL)
2072 1.1 christos goto fail;
2073 1.1 christos
2074 1.1 christos bed = get_elf_backend_data (abfd);
2075 1.1 christos switch (hdr->sh_type)
2076 1.1 christos {
2077 1.5 christos case SHT_NULL:
2078 1.1 christos /* Inactive section. Throw it away. */
2079 1.5 christos goto success;
2080 1.5 christos
2081 1.5 christos case SHT_PROGBITS: /* Normal section with contents. */
2082 1.5 christos case SHT_NOBITS: /* .bss section. */
2083 1.1 christos case SHT_HASH: /* .hash section. */
2084 1.1 christos case SHT_NOTE: /* .note section. */
2085 1.1 christos case SHT_INIT_ARRAY: /* .init_array section. */
2086 1.1 christos case SHT_FINI_ARRAY: /* .fini_array section. */
2087 1.1 christos case SHT_PREINIT_ARRAY: /* .preinit_array section. */
2088 1.5 christos case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
2089 1.5 christos case SHT_GNU_HASH: /* .gnu.hash section. */
2090 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2091 1.1 christos goto success;
2092 1.1 christos
2093 1.5 christos case SHT_DYNAMIC: /* Dynamic linking information. */
2094 1.5 christos if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2095 1.1 christos goto fail;
2096 1.1 christos
2097 1.14 christos if (hdr->sh_link > elf_numsections (abfd))
2098 1.14 christos {
2099 1.1 christos /* PR 10478: Accept Solaris binaries with a sh_link field
2100 1.1 christos set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1). */
2101 1.1 christos switch (bfd_get_arch (abfd))
2102 1.1 christos {
2103 1.14 christos case bfd_arch_i386:
2104 1.14 christos case bfd_arch_sparc:
2105 1.1 christos if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
2106 1.1 christos || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
2107 1.1 christos break;
2108 1.5 christos /* Otherwise fall through. */
2109 1.1 christos default:
2110 1.1 christos goto fail;
2111 1.1 christos }
2112 1.5 christos }
2113 1.1 christos else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2114 1.1 christos goto fail;
2115 1.1 christos else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2116 1.1 christos {
2117 1.1 christos Elf_Internal_Shdr *dynsymhdr;
2118 1.1 christos
2119 1.1 christos /* The shared libraries distributed with hpux11 have a bogus
2120 1.1 christos sh_link field for the ".dynamic" section. Find the
2121 1.1 christos string table for the ".dynsym" section instead. */
2122 1.1 christos if (elf_dynsymtab (abfd) != 0)
2123 1.1 christos {
2124 1.1 christos dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2125 1.1 christos hdr->sh_link = dynsymhdr->sh_link;
2126 1.1 christos }
2127 1.1 christos else
2128 1.1 christos {
2129 1.1 christos unsigned int i, num_sec;
2130 1.1 christos
2131 1.1 christos num_sec = elf_numsections (abfd);
2132 1.1 christos for (i = 1; i < num_sec; i++)
2133 1.1 christos {
2134 1.1 christos dynsymhdr = elf_elfsections (abfd)[i];
2135 1.1 christos if (dynsymhdr->sh_type == SHT_DYNSYM)
2136 1.1 christos {
2137 1.1 christos hdr->sh_link = dynsymhdr->sh_link;
2138 1.1 christos break;
2139 1.1 christos }
2140 1.1 christos }
2141 1.5 christos }
2142 1.1 christos }
2143 1.5 christos goto success;
2144 1.1 christos
2145 1.5 christos case SHT_SYMTAB: /* A symbol table. */
2146 1.1 christos if (elf_onesymtab (abfd) == shindex)
2147 1.1 christos goto success;
2148 1.5 christos
2149 1.5 christos if (hdr->sh_entsize != bed->s->sizeof_sym)
2150 1.1 christos goto fail;
2151 1.3 christos
2152 1.3 christos if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2153 1.5 christos {
2154 1.3 christos if (hdr->sh_size != 0)
2155 1.3 christos goto fail;
2156 1.3 christos /* Some assemblers erroneously set sh_info to one with a
2157 1.3 christos zero sh_size. ld sees this as a global symbol count
2158 1.5 christos of (unsigned) -1. Fix it here. */
2159 1.3 christos hdr->sh_info = 0;
2160 1.5 christos goto success;
2161 1.8 christos }
2162 1.8 christos
2163 1.8 christos /* PR 18854: A binary might contain more than one symbol table.
2164 1.8 christos Unusual, but possible. Warn, but continue. */
2165 1.9 christos if (elf_onesymtab (abfd) != 0)
2166 1.9 christos {
2167 1.11 christos _bfd_error_handler
2168 1.9 christos /* xgettext:c-format */
2169 1.8 christos (_("%pB: warning: multiple symbol tables detected"
2170 1.8 christos " - ignoring the table in section %u"),
2171 1.8 christos abfd, shindex);
2172 1.1 christos goto success;
2173 1.8 christos }
2174 1.8 christos elf_onesymtab (abfd) = shindex;
2175 1.1 christos elf_symtab_hdr (abfd) = *hdr;
2176 1.1 christos elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2177 1.1 christos abfd->flags |= HAS_SYMS;
2178 1.1 christos
2179 1.1 christos /* Sometimes a shared object will map in the symbol table. If
2180 1.1 christos SHF_ALLOC is set, and this is a shared object, then we also
2181 1.1 christos treat this section as a BFD section. We can not base the
2182 1.1 christos decision purely on SHF_ALLOC, because that flag is sometimes
2183 1.1 christos set in a relocatable object file, which would confuse the
2184 1.1 christos linker. */
2185 1.1 christos if ((hdr->sh_flags & SHF_ALLOC) != 0
2186 1.1 christos && (abfd->flags & DYNAMIC) != 0
2187 1.5 christos && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2188 1.1 christos shindex))
2189 1.1 christos goto fail;
2190 1.1 christos
2191 1.1 christos /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2192 1.8 christos can't read symbols without that section loaded as well. It
2193 1.8 christos is most likely specified by the next section header. */
2194 1.8 christos {
2195 1.8 christos elf_section_list * entry;
2196 1.14 christos unsigned int i, num_sec;
2197 1.8 christos
2198 1.8 christos for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2199 1.8 christos if (entry->hdr.sh_link == shindex)
2200 1.8 christos goto success;
2201 1.8 christos
2202 1.8 christos num_sec = elf_numsections (abfd);
2203 1.8 christos for (i = shindex + 1; i < num_sec; i++)
2204 1.8 christos {
2205 1.8 christos Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2206 1.8 christos
2207 1.8 christos if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2208 1.8 christos && hdr2->sh_link == shindex)
2209 1.1 christos break;
2210 1.8 christos }
2211 1.8 christos
2212 1.1 christos if (i == num_sec)
2213 1.1 christos for (i = 1; i < shindex; i++)
2214 1.8 christos {
2215 1.1 christos Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2216 1.1 christos
2217 1.1 christos if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2218 1.1 christos && hdr2->sh_link == shindex)
2219 1.8 christos break;
2220 1.8 christos }
2221 1.8 christos
2222 1.14 christos if (i != shindex)
2223 1.14 christos ret = bfd_section_from_shdr (abfd, i);
2224 1.8 christos /* else FIXME: we have failed to find the symbol table.
2225 1.8 christos Should we issue an error? */
2226 1.1 christos goto success;
2227 1.5 christos }
2228 1.1 christos
2229 1.5 christos case SHT_DYNSYM: /* A dynamic symbol table. */
2230 1.1 christos if (elf_dynsymtab (abfd) == shindex)
2231 1.1 christos goto success;
2232 1.5 christos
2233 1.5 christos if (hdr->sh_entsize != bed->s->sizeof_sym)
2234 1.3 christos goto fail;
2235 1.3 christos
2236 1.3 christos if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2237 1.5 christos {
2238 1.5 christos if (hdr->sh_size != 0)
2239 1.3 christos goto fail;
2240 1.3 christos
2241 1.3 christos /* Some linkers erroneously set sh_info to one with a
2242 1.3 christos zero sh_size. ld sees this as a global symbol count
2243 1.5 christos of (unsigned) -1. Fix it here. */
2244 1.3 christos hdr->sh_info = 0;
2245 1.5 christos goto success;
2246 1.8 christos }
2247 1.8 christos
2248 1.8 christos /* PR 18854: A binary might contain more than one dynamic symbol table.
2249 1.8 christos Unusual, but possible. Warn, but continue. */
2250 1.9 christos if (elf_dynsymtab (abfd) != 0)
2251 1.9 christos {
2252 1.11 christos _bfd_error_handler
2253 1.9 christos /* xgettext:c-format */
2254 1.8 christos (_("%pB: warning: multiple dynamic symbol tables detected"
2255 1.8 christos " - ignoring the table in section %u"),
2256 1.8 christos abfd, shindex);
2257 1.1 christos goto success;
2258 1.1 christos }
2259 1.1 christos elf_dynsymtab (abfd) = shindex;
2260 1.1 christos elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2261 1.1 christos elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2262 1.1 christos abfd->flags |= HAS_SYMS;
2263 1.1 christos
2264 1.5 christos /* Besides being a symbol table, we also treat this as a regular
2265 1.5 christos section, so that objcopy can handle it. */
2266 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2267 1.14 christos goto success;
2268 1.8 christos
2269 1.8 christos case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
2270 1.8 christos {
2271 1.14 christos elf_section_list * entry;
2272 1.8 christos
2273 1.8 christos for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2274 1.11 christos if (entry->ndx == shindex)
2275 1.12 christos goto success;
2276 1.8 christos
2277 1.8 christos entry = bfd_alloc (abfd, sizeof (*entry));
2278 1.8 christos if (entry == NULL)
2279 1.8 christos goto fail;
2280 1.8 christos entry->ndx = shindex;
2281 1.8 christos entry->hdr = * hdr;
2282 1.8 christos entry->next = elf_symtab_shndx_list (abfd);
2283 1.5 christos elf_symtab_shndx_list (abfd) = entry;
2284 1.8 christos elf_elfsections (abfd)[shindex] = & entry->hdr;
2285 1.1 christos goto success;
2286 1.5 christos }
2287 1.1 christos
2288 1.5 christos case SHT_STRTAB: /* A string table. */
2289 1.5 christos if (hdr->bfd_section != NULL)
2290 1.1 christos goto success;
2291 1.1 christos
2292 1.1 christos if (ehdr->e_shstrndx == shindex)
2293 1.1 christos {
2294 1.5 christos elf_tdata (abfd)->shstrtab_hdr = *hdr;
2295 1.1 christos elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2296 1.5 christos goto success;
2297 1.1 christos }
2298 1.1 christos
2299 1.1 christos if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2300 1.1 christos {
2301 1.1 christos symtab_strtab:
2302 1.5 christos elf_tdata (abfd)->strtab_hdr = *hdr;
2303 1.1 christos elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2304 1.5 christos goto success;
2305 1.1 christos }
2306 1.1 christos
2307 1.1 christos if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2308 1.1 christos {
2309 1.1 christos dynsymtab_strtab:
2310 1.1 christos elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2311 1.1 christos hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2312 1.1 christos elf_elfsections (abfd)[shindex] = hdr;
2313 1.5 christos /* We also treat this as a regular section, so that objcopy
2314 1.5 christos can handle it. */
2315 1.5 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2316 1.1 christos shindex);
2317 1.1 christos goto success;
2318 1.1 christos }
2319 1.1 christos
2320 1.1 christos /* If the string table isn't one of the above, then treat it as a
2321 1.1 christos regular section. We need to scan all the headers to be sure,
2322 1.1 christos just in case this strtab section appeared before the above. */
2323 1.1 christos if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2324 1.1 christos {
2325 1.1 christos unsigned int i, num_sec;
2326 1.1 christos
2327 1.1 christos num_sec = elf_numsections (abfd);
2328 1.1 christos for (i = 1; i < num_sec; i++)
2329 1.1 christos {
2330 1.1 christos Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2331 1.1 christos if (hdr2->sh_link == shindex)
2332 1.1 christos {
2333 1.5 christos /* Prevent endless recursion on broken objects. */
2334 1.1 christos if (i == shindex)
2335 1.5 christos goto fail;
2336 1.1 christos if (! bfd_section_from_shdr (abfd, i))
2337 1.1 christos goto fail;
2338 1.1 christos if (elf_onesymtab (abfd) == i)
2339 1.1 christos goto symtab_strtab;
2340 1.1 christos if (elf_dynsymtab (abfd) == i)
2341 1.1 christos goto dynsymtab_strtab;
2342 1.1 christos }
2343 1.5 christos }
2344 1.5 christos }
2345 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2346 1.1 christos goto success;
2347 1.1 christos
2348 1.14 christos case SHT_REL:
2349 1.1 christos case SHT_RELA:
2350 1.1 christos case SHT_RELR:
2351 1.1 christos /* *These* do a lot of work -- but build no sections! */
2352 1.1 christos {
2353 1.1 christos asection *target_sect;
2354 1.1 christos Elf_Internal_Shdr *hdr2, **p_hdr;
2355 1.14 christos unsigned int num_sec = elf_numsections (abfd);
2356 1.1 christos struct bfd_elf_section_data *esdt;
2357 1.14 christos bfd_size_type size;
2358 1.14 christos
2359 1.14 christos if (hdr->sh_type == SHT_REL)
2360 1.14 christos size = bed->s->sizeof_rel;
2361 1.14 christos else if (hdr->sh_type == SHT_RELA)
2362 1.14 christos size = bed->s->sizeof_rela;
2363 1.14 christos else
2364 1.5 christos size = bed->s->arch_size / 8;
2365 1.1 christos if (hdr->sh_entsize != size)
2366 1.1 christos goto fail;
2367 1.1 christos
2368 1.1 christos /* Check for a bogus link to avoid crashing. */
2369 1.9 christos if (hdr->sh_link >= num_sec)
2370 1.9 christos {
2371 1.11 christos _bfd_error_handler
2372 1.9 christos /* xgettext:c-format */
2373 1.14 christos (_("%pB: invalid link %u for reloc section %s (index %u)"),
2374 1.5 christos abfd, hdr->sh_link, name, shindex);
2375 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2376 1.1 christos goto success;
2377 1.1 christos }
2378 1.1 christos
2379 1.1 christos /* Get the symbol table. */
2380 1.1 christos if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2381 1.5 christos || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2382 1.1 christos && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2383 1.11 christos goto fail;
2384 1.11 christos
2385 1.11 christos /* If this is an alloc section in an executable or shared
2386 1.11 christos library, or the reloc section does not use the main symbol
2387 1.11 christos table we don't treat it as a reloc section. BFD can't
2388 1.11 christos adequately represent such a section, so at least for now,
2389 1.11 christos we don't try. We just present it as a normal section. We
2390 1.11 christos also can't use it as a reloc section if it points to the
2391 1.11 christos null section, an invalid section, another reloc section, or
2392 1.11 christos its sh_link points to the null section. */
2393 1.14 christos if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2394 1.1 christos && (hdr->sh_flags & SHF_ALLOC) != 0)
2395 1.11 christos || hdr->sh_type == SHT_RELR
2396 1.1 christos || hdr->sh_link == SHN_UNDEF
2397 1.1 christos || hdr->sh_link != elf_onesymtab (abfd)
2398 1.1 christos || hdr->sh_info == SHN_UNDEF
2399 1.1 christos || hdr->sh_info >= num_sec
2400 1.5 christos || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2401 1.14 christos || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2402 1.5 christos {
2403 1.5 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2404 1.1 christos goto success;
2405 1.1 christos }
2406 1.5 christos
2407 1.5 christos if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2408 1.1 christos goto fail;
2409 1.1 christos
2410 1.5 christos target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2411 1.1 christos if (target_sect == NULL)
2412 1.1 christos goto fail;
2413 1.1 christos
2414 1.1 christos esdt = elf_section_data (target_sect);
2415 1.1 christos if (hdr->sh_type == SHT_RELA)
2416 1.1 christos p_hdr = &esdt->rela.hdr;
2417 1.1 christos else
2418 1.12 christos p_hdr = &esdt->rel.hdr;
2419 1.12 christos
2420 1.12 christos /* PR 17512: file: 0b4f81b7.
2421 1.5 christos Also see PR 24456, for a file which deliberately has two reloc
2422 1.12 christos sections. */
2423 1.12 christos if (*p_hdr != NULL)
2424 1.12 christos {
2425 1.12 christos if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
2426 1.12 christos {
2427 1.12 christos _bfd_error_handler
2428 1.12 christos /* xgettext:c-format */
2429 1.12 christos (_("%pB: warning: secondary relocation section '%s' "
2430 1.12 christos "for section %pA found - ignoring"),
2431 1.14 christos abfd, name, target_sect);
2432 1.14 christos }
2433 1.12 christos else
2434 1.12 christos esdt->has_secondary_relocs = true;
2435 1.12 christos goto success;
2436 1.8 christos }
2437 1.1 christos
2438 1.5 christos hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2439 1.1 christos if (hdr2 == NULL)
2440 1.1 christos goto fail;
2441 1.1 christos *hdr2 = *hdr;
2442 1.11 christos *p_hdr = hdr2;
2443 1.11 christos elf_elfsections (abfd)[shindex] = hdr2;
2444 1.1 christos target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2445 1.1 christos * bed->s->int_rels_per_ext_rel);
2446 1.1 christos target_sect->flags |= SEC_RELOC;
2447 1.1 christos target_sect->relocation = NULL;
2448 1.1 christos target_sect->rel_filepos = hdr->sh_offset;
2449 1.1 christos /* In the section to which the relocations apply, mark whether
2450 1.1 christos its relocations are of the REL or RELA variety. */
2451 1.1 christos if (hdr->sh_size != 0)
2452 1.1 christos {
2453 1.1 christos if (hdr->sh_type == SHT_RELA)
2454 1.1 christos target_sect->use_rela_p = 1;
2455 1.5 christos }
2456 1.1 christos abfd->flags |= HAS_RELOC;
2457 1.1 christos goto success;
2458 1.1 christos }
2459 1.14 christos
2460 1.14 christos case SHT_GNU_verdef:
2461 1.1 christos if (hdr->sh_info != 0)
2462 1.5 christos elf_dynverdef (abfd) = shindex;
2463 1.5 christos elf_tdata (abfd)->dynverdef_hdr = *hdr;
2464 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2465 1.1 christos goto success;
2466 1.1 christos
2467 1.5 christos case SHT_GNU_versym:
2468 1.5 christos if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2469 1.1 christos goto fail;
2470 1.1 christos
2471 1.5 christos elf_dynversym (abfd) = shindex;
2472 1.5 christos elf_tdata (abfd)->dynversym_hdr = *hdr;
2473 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2474 1.1 christos goto success;
2475 1.14 christos
2476 1.14 christos case SHT_GNU_verneed:
2477 1.1 christos if (hdr->sh_info != 0)
2478 1.5 christos elf_dynverref (abfd) = shindex;
2479 1.5 christos elf_tdata (abfd)->dynverref_hdr = *hdr;
2480 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2481 1.1 christos goto success;
2482 1.5 christos
2483 1.1 christos case SHT_SHLIB:
2484 1.1 christos goto success;
2485 1.3 christos
2486 1.5 christos case SHT_GROUP:
2487 1.5 christos if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
2488 1.1 christos goto fail;
2489 1.5 christos
2490 1.5 christos if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2491 1.5 christos goto fail;
2492 1.1 christos
2493 1.1 christos goto success;
2494 1.1 christos
2495 1.1 christos default:
2496 1.1 christos /* Possibly an attributes section. */
2497 1.1 christos if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2498 1.1 christos || hdr->sh_type == bed->obj_attrs_section_type)
2499 1.5 christos {
2500 1.1 christos if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2501 1.5 christos goto fail;
2502 1.1 christos _bfd_elf_parse_attributes (abfd, hdr);
2503 1.1 christos goto success;
2504 1.1 christos }
2505 1.1 christos
2506 1.5 christos /* Check for any processor-specific section types. */
2507 1.1 christos if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2508 1.1 christos goto success;
2509 1.1 christos
2510 1.1 christos if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2511 1.1 christos {
2512 1.1 christos if ((hdr->sh_flags & SHF_ALLOC) != 0)
2513 1.9 christos /* FIXME: How to properly handle allocated section reserved
2514 1.9 christos for applications? */
2515 1.11 christos _bfd_error_handler
2516 1.11 christos /* xgettext:c-format */
2517 1.1 christos (_("%pB: unknown type [%#x] section `%s'"),
2518 1.5 christos abfd, hdr->sh_type, name);
2519 1.5 christos else
2520 1.14 christos {
2521 1.5 christos /* Allow sections reserved for applications. */
2522 1.5 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2523 1.1 christos goto success;
2524 1.1 christos }
2525 1.1 christos }
2526 1.1 christos else if (hdr->sh_type >= SHT_LOPROC
2527 1.9 christos && hdr->sh_type <= SHT_HIPROC)
2528 1.9 christos /* FIXME: We should handle this section. */
2529 1.11 christos _bfd_error_handler
2530 1.11 christos /* xgettext:c-format */
2531 1.1 christos (_("%pB: unknown type [%#x] section `%s'"),
2532 1.1 christos abfd, hdr->sh_type, name);
2533 1.1 christos else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2534 1.1 christos {
2535 1.1 christos /* Unrecognised OS-specific sections. */
2536 1.1 christos if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2537 1.1 christos /* SHF_OS_NONCONFORMING indicates that special knowledge is
2538 1.9 christos required to correctly process the section and the file should
2539 1.9 christos be rejected with an error message. */
2540 1.11 christos _bfd_error_handler
2541 1.11 christos /* xgettext:c-format */
2542 1.1 christos (_("%pB: unknown type [%#x] section `%s'"),
2543 1.5 christos abfd, hdr->sh_type, name);
2544 1.5 christos else
2545 1.5 christos {
2546 1.5 christos /* Otherwise it should be processed. */
2547 1.5 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2548 1.1 christos goto success;
2549 1.1 christos }
2550 1.1 christos }
2551 1.9 christos else
2552 1.9 christos /* FIXME: We should handle this section. */
2553 1.11 christos _bfd_error_handler
2554 1.11 christos /* xgettext:c-format */
2555 1.1 christos (_("%pB: unknown type [%#x] section `%s'"),
2556 1.5 christos abfd, hdr->sh_type, name);
2557 1.1 christos
2558 1.1 christos goto fail;
2559 1.5 christos }
2560 1.14 christos
2561 1.5 christos fail:
2562 1.14 christos ret = false;
2563 1.5 christos success:
2564 1.1 christos elf_tdata (abfd)->being_created[shindex] = false;
2565 1.1 christos return ret;
2566 1.1 christos }
2567 1.1 christos
2568 1.1 christos /* Return the local symbol specified by ABFD, R_SYMNDX. */
2569 1.1 christos
2570 1.1 christos Elf_Internal_Sym *
2571 1.1 christos bfd_sym_from_r_symndx (struct sym_cache *cache,
2572 1.1 christos bfd *abfd,
2573 1.1 christos unsigned long r_symndx)
2574 1.1 christos {
2575 1.1 christos unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2576 1.1 christos
2577 1.1 christos if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2578 1.1 christos {
2579 1.1 christos Elf_Internal_Shdr *symtab_hdr;
2580 1.1 christos unsigned char esym[sizeof (Elf64_External_Sym)];
2581 1.1 christos Elf_External_Sym_Shndx eshndx;
2582 1.1 christos
2583 1.1 christos symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2584 1.1 christos if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2585 1.1 christos &cache->sym[ent], esym, &eshndx) == NULL)
2586 1.1 christos return NULL;
2587 1.1 christos
2588 1.1 christos if (cache->abfd != abfd)
2589 1.1 christos {
2590 1.1 christos memset (cache->indx, -1, sizeof (cache->indx));
2591 1.1 christos cache->abfd = abfd;
2592 1.1 christos }
2593 1.1 christos cache->indx[ent] = r_symndx;
2594 1.1 christos }
2595 1.1 christos
2596 1.1 christos return &cache->sym[ent];
2597 1.1 christos }
2598 1.1 christos
2599 1.1 christos /* Given an ELF section number, retrieve the corresponding BFD
2600 1.1 christos section. */
2601 1.1 christos
2602 1.1 christos asection *
2603 1.1 christos bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2604 1.1 christos {
2605 1.1 christos if (sec_index >= elf_numsections (abfd))
2606 1.1 christos return NULL;
2607 1.1 christos return elf_elfsections (abfd)[sec_index]->bfd_section;
2608 1.1 christos }
2609 1.1 christos
2610 1.1 christos static const struct bfd_elf_special_section special_sections_b[] =
2611 1.11 christos {
2612 1.1 christos { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2613 1.1 christos { NULL, 0, 0, 0, 0 }
2614 1.1 christos };
2615 1.1 christos
2616 1.1 christos static const struct bfd_elf_special_section special_sections_c[] =
2617 1.12 christos {
2618 1.11 christos { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2619 1.1 christos { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 },
2620 1.1 christos { NULL, 0, 0, 0, 0 }
2621 1.1 christos };
2622 1.1 christos
2623 1.11 christos static const struct bfd_elf_special_section special_sections_d[] =
2624 1.11 christos {
2625 1.3 christos { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2626 1.3 christos { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2627 1.3 christos /* There are more DWARF sections than these, but they needn't be added here
2628 1.11 christos unless you have to cope with broken compilers that don't emit section
2629 1.11 christos attributes or you want to help the user writing assembler. */
2630 1.11 christos { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2631 1.11 christos { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2632 1.1 christos { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2633 1.11 christos { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2634 1.11 christos { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2635 1.11 christos { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2636 1.11 christos { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2637 1.1 christos { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2638 1.1 christos { NULL, 0, 0, 0, 0 }
2639 1.1 christos };
2640 1.1 christos
2641 1.11 christos static const struct bfd_elf_special_section special_sections_f[] =
2642 1.9 christos {
2643 1.11 christos { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2644 1.1 christos { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2645 1.1 christos { NULL, 0 , 0, 0, 0 }
2646 1.1 christos };
2647 1.1 christos
2648 1.1 christos static const struct bfd_elf_special_section special_sections_g[] =
2649 1.14 christos {
2650 1.14 christos { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2651 1.11 christos { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2652 1.11 christos { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2653 1.11 christos { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2654 1.1 christos { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2655 1.1 christos { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2656 1.11 christos { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2657 1.11 christos { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2658 1.11 christos { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2659 1.11 christos { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2660 1.1 christos { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2661 1.1 christos { NULL, 0, 0, 0, 0 }
2662 1.1 christos };
2663 1.1 christos
2664 1.11 christos static const struct bfd_elf_special_section special_sections_h[] =
2665 1.11 christos {
2666 1.1 christos { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2667 1.1 christos { NULL, 0, 0, 0, 0 }
2668 1.1 christos };
2669 1.1 christos
2670 1.11 christos static const struct bfd_elf_special_section special_sections_i[] =
2671 1.9 christos {
2672 1.11 christos { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2673 1.11 christos { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2674 1.1 christos { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2675 1.1 christos { NULL, 0, 0, 0, 0 }
2676 1.1 christos };
2677 1.1 christos
2678 1.1 christos static const struct bfd_elf_special_section special_sections_l[] =
2679 1.11 christos {
2680 1.1 christos { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2681 1.1 christos { NULL, 0, 0, 0, 0 }
2682 1.1 christos };
2683 1.1 christos
2684 1.14 christos static const struct bfd_elf_special_section special_sections_n[] =
2685 1.1 christos {
2686 1.11 christos { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2687 1.11 christos { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2688 1.1 christos { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2689 1.1 christos { NULL, 0, 0, 0, 0 }
2690 1.1 christos };
2691 1.1 christos
2692 1.14 christos static const struct bfd_elf_special_section special_sections_p[] =
2693 1.14 christos {
2694 1.9 christos { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2695 1.11 christos { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2696 1.11 christos { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2697 1.1 christos { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2698 1.1 christos { NULL, 0, 0, 0, 0 }
2699 1.1 christos };
2700 1.1 christos
2701 1.1 christos static const struct bfd_elf_special_section special_sections_r[] =
2702 1.1 christos {
2703 1.14 christos { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2704 1.11 christos { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2705 1.11 christos { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
2706 1.11 christos { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2707 1.1 christos { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2708 1.1 christos { NULL, 0, 0, 0, 0 }
2709 1.1 christos };
2710 1.1 christos
2711 1.1 christos static const struct bfd_elf_special_section special_sections_s[] =
2712 1.1 christos {
2713 1.1 christos { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2714 1.1 christos { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2715 1.1 christos { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2716 1.1 christos /* See struct bfd_elf_special_section declaration for the semantics of
2717 1.11 christos this special case where .prefix_length != strlen (.prefix). */
2718 1.1 christos { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2719 1.1 christos { NULL, 0, 0, 0, 0 }
2720 1.1 christos };
2721 1.1 christos
2722 1.11 christos static const struct bfd_elf_special_section special_sections_t[] =
2723 1.11 christos {
2724 1.1 christos { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2725 1.11 christos { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2726 1.1 christos { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2727 1.1 christos { NULL, 0, 0, 0, 0 }
2728 1.1 christos };
2729 1.1 christos
2730 1.11 christos static const struct bfd_elf_special_section special_sections_z[] =
2731 1.11 christos {
2732 1.1 christos { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2733 1.1 christos { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2734 1.11 christos { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2735 1.1 christos { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2736 1.1 christos { NULL, 0, 0, 0, 0 }
2737 1.3 christos };
2738 1.1 christos
2739 1.1 christos static const struct bfd_elf_special_section * const special_sections[] =
2740 1.1 christos {
2741 1.1 christos special_sections_b, /* 'b' */
2742 1.1 christos special_sections_c, /* 'c' */
2743 1.1 christos special_sections_d, /* 'd' */
2744 1.1 christos NULL, /* 'e' */
2745 1.1 christos special_sections_f, /* 'f' */
2746 1.1 christos special_sections_g, /* 'g' */
2747 1.1 christos special_sections_h, /* 'h' */
2748 1.1 christos special_sections_i, /* 'i' */
2749 1.1 christos NULL, /* 'j' */
2750 1.1 christos NULL, /* 'k' */
2751 1.1 christos special_sections_l, /* 'l' */
2752 1.1 christos NULL, /* 'm' */
2753 1.1 christos special_sections_n, /* 'n' */
2754 1.1 christos NULL, /* 'o' */
2755 1.1 christos special_sections_p, /* 'p' */
2756 1.1 christos NULL, /* 'q' */
2757 1.1 christos special_sections_r, /* 'r' */
2758 1.1 christos special_sections_s, /* 's' */
2759 1.1 christos special_sections_t, /* 't' */
2760 1.1 christos NULL, /* 'u' */
2761 1.1 christos NULL, /* 'v' */
2762 1.1 christos NULL, /* 'w' */
2763 1.1 christos NULL, /* 'x' */
2764 1.1 christos NULL, /* 'y' */
2765 1.1 christos special_sections_z /* 'z' */
2766 1.1 christos };
2767 1.1 christos
2768 1.1 christos const struct bfd_elf_special_section *
2769 1.1 christos _bfd_elf_get_special_section (const char *name,
2770 1.1 christos const struct bfd_elf_special_section *spec,
2771 1.1 christos unsigned int rela)
2772 1.1 christos {
2773 1.1 christos int i;
2774 1.1 christos int len;
2775 1.1 christos
2776 1.1 christos len = strlen (name);
2777 1.1 christos
2778 1.1 christos for (i = 0; spec[i].prefix != NULL; i++)
2779 1.1 christos {
2780 1.1 christos int suffix_len;
2781 1.1 christos int prefix_len = spec[i].prefix_length;
2782 1.1 christos
2783 1.1 christos if (len < prefix_len)
2784 1.1 christos continue;
2785 1.1 christos if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2786 1.1 christos continue;
2787 1.1 christos
2788 1.1 christos suffix_len = spec[i].suffix_length;
2789 1.1 christos if (suffix_len <= 0)
2790 1.1 christos {
2791 1.1 christos if (name[prefix_len] != 0)
2792 1.1 christos {
2793 1.1 christos if (suffix_len == 0)
2794 1.1 christos continue;
2795 1.1 christos if (name[prefix_len] != '.'
2796 1.1 christos && (suffix_len == -2
2797 1.1 christos || (rela && spec[i].type == SHT_REL)))
2798 1.1 christos continue;
2799 1.1 christos }
2800 1.1 christos }
2801 1.1 christos else
2802 1.1 christos {
2803 1.1 christos if (len < prefix_len + suffix_len)
2804 1.1 christos continue;
2805 1.1 christos if (memcmp (name + len - suffix_len,
2806 1.1 christos spec[i].prefix + prefix_len,
2807 1.1 christos suffix_len) != 0)
2808 1.1 christos continue;
2809 1.1 christos }
2810 1.1 christos return &spec[i];
2811 1.1 christos }
2812 1.1 christos
2813 1.1 christos return NULL;
2814 1.1 christos }
2815 1.1 christos
2816 1.1 christos const struct bfd_elf_special_section *
2817 1.1 christos _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2818 1.1 christos {
2819 1.1 christos int i;
2820 1.1 christos const struct bfd_elf_special_section *spec;
2821 1.1 christos const struct elf_backend_data *bed;
2822 1.1 christos
2823 1.1 christos /* See if this is one of the special sections. */
2824 1.1 christos if (sec->name == NULL)
2825 1.1 christos return NULL;
2826 1.1 christos
2827 1.1 christos bed = get_elf_backend_data (abfd);
2828 1.1 christos spec = bed->special_sections;
2829 1.1 christos if (spec)
2830 1.1 christos {
2831 1.1 christos spec = _bfd_elf_get_special_section (sec->name,
2832 1.1 christos bed->special_sections,
2833 1.1 christos sec->use_rela_p);
2834 1.1 christos if (spec != NULL)
2835 1.1 christos return spec;
2836 1.1 christos }
2837 1.1 christos
2838 1.1 christos if (sec->name[0] != '.')
2839 1.1 christos return NULL;
2840 1.1 christos
2841 1.1 christos i = sec->name[1] - 'b';
2842 1.1 christos if (i < 0 || i > 'z' - 'b')
2843 1.1 christos return NULL;
2844 1.1 christos
2845 1.1 christos spec = special_sections[i];
2846 1.1 christos
2847 1.1 christos if (spec == NULL)
2848 1.1 christos return NULL;
2849 1.1 christos
2850 1.1 christos return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2851 1.14 christos }
2852 1.1 christos
2853 1.1 christos bool
2854 1.1 christos _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2855 1.1 christos {
2856 1.1 christos struct bfd_elf_section_data *sdata;
2857 1.1 christos const struct elf_backend_data *bed;
2858 1.1 christos const struct bfd_elf_special_section *ssect;
2859 1.1 christos
2860 1.1 christos sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2861 1.1 christos if (sdata == NULL)
2862 1.11 christos {
2863 1.1 christos sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2864 1.14 christos sizeof (*sdata));
2865 1.1 christos if (sdata == NULL)
2866 1.1 christos return false;
2867 1.1 christos sec->used_by_bfd = sdata;
2868 1.1 christos }
2869 1.1 christos
2870 1.1 christos /* Indicate whether or not this section should use RELA relocations. */
2871 1.1 christos bed = get_elf_backend_data (abfd);
2872 1.12 christos sec->use_rela_p = bed->default_use_rela_p;
2873 1.12 christos
2874 1.12 christos /* Set up ELF section type and flags for newly created sections, if
2875 1.12 christos there is an ABI mandated section. */
2876 1.12 christos ssect = (*bed->get_sec_type_attr) (abfd, sec);
2877 1.12 christos if (ssect != NULL)
2878 1.12 christos {
2879 1.1 christos elf_section_type (sec) = ssect->type;
2880 1.1 christos elf_section_flags (sec) = ssect->attr;
2881 1.1 christos }
2882 1.1 christos
2883 1.1 christos return _bfd_generic_new_section_hook (abfd, sec);
2884 1.1 christos }
2885 1.1 christos
2886 1.1 christos /* Create a new bfd section from an ELF program header.
2887 1.1 christos
2888 1.1 christos Since program segments have no names, we generate a synthetic name
2889 1.1 christos of the form segment<NUM>, where NUM is generally the index in the
2890 1.1 christos program header table. For segments that are split (see below) we
2891 1.1 christos generate the names segment<NUM>a and segment<NUM>b.
2892 1.1 christos
2893 1.1 christos Note that some program segments may have a file size that is different than
2894 1.1 christos (less than) the memory size. All this means is that at execution the
2895 1.1 christos system must allocate the amount of memory specified by the memory size,
2896 1.1 christos but only initialize it with the first "file size" bytes read from the
2897 1.1 christos file. This would occur for example, with program segments consisting
2898 1.1 christos of combined data+bss.
2899 1.1 christos
2900 1.1 christos To handle the above situation, this routine generates TWO bfd sections
2901 1.1 christos for the single program segment. The first has the length specified by
2902 1.14 christos the file size of the segment, and the second has the length specified
2903 1.1 christos by the difference between the two sizes. In effect, the segment is split
2904 1.14 christos into its initialized and uninitialized parts. */
2905 1.1 christos
2906 1.1 christos bool
2907 1.1 christos _bfd_elf_make_section_from_phdr (bfd *abfd,
2908 1.1 christos Elf_Internal_Phdr *hdr,
2909 1.1 christos int hdr_index,
2910 1.1 christos const char *type_name)
2911 1.1 christos {
2912 1.1 christos asection *newsect;
2913 1.1 christos char *name;
2914 1.1 christos char namebuf[64];
2915 1.12 christos size_t len;
2916 1.1 christos int split;
2917 1.1 christos unsigned int opb = bfd_octets_per_byte (abfd, NULL);
2918 1.1 christos
2919 1.1 christos split = ((hdr->p_memsz > 0)
2920 1.1 christos && (hdr->p_filesz > 0)
2921 1.1 christos && (hdr->p_memsz > hdr->p_filesz));
2922 1.1 christos
2923 1.1 christos if (hdr->p_filesz > 0)
2924 1.1 christos {
2925 1.1 christos sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2926 1.1 christos len = strlen (namebuf) + 1;
2927 1.14 christos name = (char *) bfd_alloc (abfd, len);
2928 1.1 christos if (!name)
2929 1.1 christos return false;
2930 1.1 christos memcpy (name, namebuf, len);
2931 1.14 christos newsect = bfd_make_section (abfd, name);
2932 1.12 christos if (newsect == NULL)
2933 1.12 christos return false;
2934 1.1 christos newsect->vma = hdr->p_vaddr / opb;
2935 1.1 christos newsect->lma = hdr->p_paddr / opb;
2936 1.1 christos newsect->size = hdr->p_filesz;
2937 1.1 christos newsect->filepos = hdr->p_offset;
2938 1.1 christos newsect->flags |= SEC_HAS_CONTENTS;
2939 1.1 christos newsect->alignment_power = bfd_log2 (hdr->p_align);
2940 1.1 christos if (hdr->p_type == PT_LOAD)
2941 1.1 christos {
2942 1.1 christos newsect->flags |= SEC_ALLOC;
2943 1.1 christos newsect->flags |= SEC_LOAD;
2944 1.1 christos if (hdr->p_flags & PF_X)
2945 1.1 christos {
2946 1.1 christos /* FIXME: all we known is that it has execute PERMISSION,
2947 1.1 christos may be data. */
2948 1.1 christos newsect->flags |= SEC_CODE;
2949 1.1 christos }
2950 1.1 christos }
2951 1.1 christos if (!(hdr->p_flags & PF_W))
2952 1.1 christos {
2953 1.1 christos newsect->flags |= SEC_READONLY;
2954 1.1 christos }
2955 1.1 christos }
2956 1.1 christos
2957 1.1 christos if (hdr->p_memsz > hdr->p_filesz)
2958 1.1 christos {
2959 1.1 christos bfd_vma align;
2960 1.1 christos
2961 1.1 christos sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
2962 1.1 christos len = strlen (namebuf) + 1;
2963 1.14 christos name = (char *) bfd_alloc (abfd, len);
2964 1.1 christos if (!name)
2965 1.1 christos return false;
2966 1.1 christos memcpy (name, namebuf, len);
2967 1.14 christos newsect = bfd_make_section (abfd, name);
2968 1.12 christos if (newsect == NULL)
2969 1.12 christos return false;
2970 1.1 christos newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
2971 1.1 christos newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
2972 1.1 christos newsect->size = hdr->p_memsz - hdr->p_filesz;
2973 1.1 christos newsect->filepos = hdr->p_offset + hdr->p_filesz;
2974 1.1 christos align = newsect->vma & -newsect->vma;
2975 1.1 christos if (align == 0 || align > hdr->p_align)
2976 1.1 christos align = hdr->p_align;
2977 1.1 christos newsect->alignment_power = bfd_log2 (align);
2978 1.1 christos if (hdr->p_type == PT_LOAD)
2979 1.1 christos {
2980 1.1 christos newsect->flags |= SEC_ALLOC;
2981 1.1 christos if (hdr->p_flags & PF_X)
2982 1.1 christos newsect->flags |= SEC_CODE;
2983 1.1 christos }
2984 1.1 christos if (!(hdr->p_flags & PF_W))
2985 1.1 christos newsect->flags |= SEC_READONLY;
2986 1.14 christos }
2987 1.1 christos
2988 1.1 christos return true;
2989 1.14 christos }
2990 1.12 christos
2991 1.12 christos static bool
2992 1.12 christos _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
2993 1.12 christos {
2994 1.12 christos /* The return value is ignored. Build-ids are considered optional. */
2995 1.12 christos if (templ->xvec->flavour == bfd_target_elf_flavour)
2996 1.14 christos return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
2997 1.12 christos (templ, offset);
2998 1.12 christos return false;
2999 1.14 christos }
3000 1.1 christos
3001 1.1 christos bool
3002 1.1 christos bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3003 1.1 christos {
3004 1.1 christos const struct elf_backend_data *bed;
3005 1.1 christos
3006 1.1 christos switch (hdr->p_type)
3007 1.1 christos {
3008 1.1 christos case PT_NULL:
3009 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3010 1.12 christos
3011 1.14 christos case PT_LOAD:
3012 1.12 christos if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3013 1.12 christos return false;
3014 1.14 christos if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3015 1.1 christos _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3016 1.1 christos return true;
3017 1.1 christos
3018 1.1 christos case PT_DYNAMIC:
3019 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3020 1.1 christos
3021 1.1 christos case PT_INTERP:
3022 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3023 1.1 christos
3024 1.14 christos case PT_NOTE:
3025 1.11 christos if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3026 1.11 christos return false;
3027 1.14 christos if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3028 1.14 christos hdr->p_align))
3029 1.1 christos return false;
3030 1.1 christos return true;
3031 1.1 christos
3032 1.1 christos case PT_SHLIB:
3033 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3034 1.1 christos
3035 1.1 christos case PT_PHDR:
3036 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3037 1.1 christos
3038 1.1 christos case PT_GNU_EH_FRAME:
3039 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3040 1.1 christos "eh_frame_hdr");
3041 1.1 christos
3042 1.1 christos case PT_GNU_STACK:
3043 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3044 1.1 christos
3045 1.1 christos case PT_GNU_RELRO:
3046 1.14 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3047 1.14 christos
3048 1.14 christos case PT_GNU_SFRAME:
3049 1.14 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3050 1.1 christos "sframe");
3051 1.1 christos
3052 1.1 christos default:
3053 1.1 christos /* Check for any processor-specific program segment types. */
3054 1.1 christos bed = get_elf_backend_data (abfd);
3055 1.1 christos return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3056 1.1 christos }
3057 1.1 christos }
3058 1.1 christos
3059 1.1 christos /* Return the REL_HDR for SEC, assuming there is only a single one, either
3060 1.1 christos REL or RELA. */
3061 1.1 christos
3062 1.1 christos Elf_Internal_Shdr *
3063 1.1 christos _bfd_elf_single_rel_hdr (asection *sec)
3064 1.1 christos {
3065 1.1 christos if (elf_section_data (sec)->rel.hdr)
3066 1.1 christos {
3067 1.1 christos BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3068 1.1 christos return elf_section_data (sec)->rel.hdr;
3069 1.1 christos }
3070 1.1 christos else
3071 1.1 christos return elf_section_data (sec)->rela.hdr;
3072 1.14 christos }
3073 1.6 christos
3074 1.6 christos static bool
3075 1.6 christos _bfd_elf_set_reloc_sh_name (bfd *abfd,
3076 1.14 christos Elf_Internal_Shdr *rel_hdr,
3077 1.6 christos const char *sec_name,
3078 1.6 christos bool use_rela_p)
3079 1.6 christos {
3080 1.6 christos char *name = (char *) bfd_alloc (abfd,
3081 1.14 christos sizeof ".rela" + strlen (sec_name));
3082 1.6 christos if (name == NULL)
3083 1.6 christos return false;
3084 1.6 christos
3085 1.6 christos sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3086 1.14 christos rel_hdr->sh_name =
3087 1.6 christos (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3088 1.14 christos false);
3089 1.6 christos if (rel_hdr->sh_name == (unsigned int) -1)
3090 1.14 christos return false;
3091 1.6 christos
3092 1.6 christos return true;
3093 1.1 christos }
3094 1.1 christos
3095 1.1 christos /* Allocate and initialize a section-header for a new reloc section,
3096 1.1 christos containing relocations against ASECT. It is stored in RELDATA. If
3097 1.1 christos USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3098 1.14 christos relocations. */
3099 1.1 christos
3100 1.1 christos static bool
3101 1.6 christos _bfd_elf_init_reloc_shdr (bfd *abfd,
3102 1.14 christos struct bfd_elf_section_reloc_data *reldata,
3103 1.14 christos const char *sec_name,
3104 1.1 christos bool use_rela_p,
3105 1.1 christos bool delay_st_name_p)
3106 1.1 christos {
3107 1.1 christos Elf_Internal_Shdr *rel_hdr;
3108 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3109 1.8 christos
3110 1.1 christos BFD_ASSERT (reldata->hdr == NULL);
3111 1.1 christos rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3112 1.6 christos reldata->hdr = rel_hdr;
3113 1.6 christos
3114 1.6 christos if (delay_st_name_p)
3115 1.6 christos rel_hdr->sh_name = (unsigned int) -1;
3116 1.14 christos else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3117 1.1 christos use_rela_p))
3118 1.1 christos return false;
3119 1.1 christos rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3120 1.1 christos rel_hdr->sh_entsize = (use_rela_p
3121 1.1 christos ? bed->s->sizeof_rela
3122 1.1 christos : bed->s->sizeof_rel);
3123 1.1 christos rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3124 1.1 christos rel_hdr->sh_flags = 0;
3125 1.1 christos rel_hdr->sh_addr = 0;
3126 1.1 christos rel_hdr->sh_size = 0;
3127 1.14 christos rel_hdr->sh_offset = 0;
3128 1.1 christos
3129 1.1 christos return true;
3130 1.1 christos }
3131 1.1 christos
3132 1.1 christos /* Return the default section type based on the passed in section flags. */
3133 1.1 christos
3134 1.1 christos int
3135 1.11 christos bfd_elf_get_default_section_type (flagword flags)
3136 1.1 christos {
3137 1.1 christos if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3138 1.1 christos && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3139 1.1 christos return SHT_NOBITS;
3140 1.1 christos return SHT_PROGBITS;
3141 1.1 christos }
3142 1.1 christos
3143 1.1 christos struct fake_section_arg
3144 1.14 christos {
3145 1.1 christos struct bfd_link_info *link_info;
3146 1.1 christos bool failed;
3147 1.1 christos };
3148 1.1 christos
3149 1.1 christos /* Set up an ELF internal section header for a section. */
3150 1.1 christos
3151 1.1 christos static void
3152 1.1 christos elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3153 1.1 christos {
3154 1.1 christos struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3155 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3156 1.1 christos struct bfd_elf_section_data *esd = elf_section_data (asect);
3157 1.6 christos Elf_Internal_Shdr *this_hdr;
3158 1.14 christos unsigned int sh_type;
3159 1.12 christos const char *name = asect->name;
3160 1.1 christos bool delay_st_name_p = false;
3161 1.1 christos bfd_vma mask;
3162 1.1 christos
3163 1.1 christos if (arg->failed)
3164 1.1 christos {
3165 1.1 christos /* We already failed; just get out of the bfd_map_over_sections
3166 1.1 christos loop. */
3167 1.1 christos return;
3168 1.1 christos }
3169 1.1 christos
3170 1.14 christos this_hdr = &esd->this_hdr;
3171 1.14 christos
3172 1.14 christos /* ld: compress DWARF debug sections with names: .debug_*. */
3173 1.14 christos if (arg->link_info
3174 1.14 christos && (abfd->flags & BFD_COMPRESS) != 0
3175 1.14 christos && (asect->flags & SEC_DEBUGGING) != 0
3176 1.14 christos && name[1] == 'd'
3177 1.14 christos && name[6] == '_')
3178 1.14 christos {
3179 1.14 christos /* If this section will be compressed, delay adding section
3180 1.14 christos name to section name section after it is compressed in
3181 1.6 christos _bfd_elf_assign_file_positions_for_non_load. */
3182 1.6 christos delay_st_name_p = true;
3183 1.6 christos }
3184 1.6 christos
3185 1.6 christos if (delay_st_name_p)
3186 1.1 christos this_hdr->sh_name = (unsigned int) -1;
3187 1.6 christos else
3188 1.6 christos {
3189 1.14 christos this_hdr->sh_name
3190 1.6 christos = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3191 1.6 christos name, false);
3192 1.14 christos if (this_hdr->sh_name == (unsigned int) -1)
3193 1.6 christos {
3194 1.6 christos arg->failed = true;
3195 1.1 christos return;
3196 1.1 christos }
3197 1.1 christos }
3198 1.1 christos
3199 1.1 christos /* Don't clear sh_flags. Assembler may set additional bits. */
3200 1.1 christos
3201 1.12 christos if ((asect->flags & SEC_ALLOC) != 0
3202 1.1 christos || asect->user_set_vma)
3203 1.1 christos this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
3204 1.1 christos else
3205 1.1 christos this_hdr->sh_addr = 0;
3206 1.1 christos
3207 1.1 christos this_hdr->sh_offset = 0;
3208 1.6 christos this_hdr->sh_size = asect->size;
3209 1.6 christos this_hdr->sh_link = 0;
3210 1.6 christos /* PR 17512: file: 0eb809fe, 8b0535ee. */
3211 1.9 christos if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3212 1.9 christos {
3213 1.11 christos _bfd_error_handler
3214 1.9 christos /* xgettext:c-format */
3215 1.14 christos (_("%pB: error: alignment power %d of section `%pA' is too big"),
3216 1.6 christos abfd, asect->alignment_power, asect);
3217 1.6 christos arg->failed = true;
3218 1.12 christos return;
3219 1.12 christos }
3220 1.12 christos /* Set sh_addralign to the highest power of two given by alignment
3221 1.12 christos consistent with the section VMA. Linker scripts can force VMA. */
3222 1.1 christos mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3223 1.1 christos this_hdr->sh_addralign = mask & -mask;
3224 1.1 christos /* The sh_entsize and sh_info fields may have been set already by
3225 1.1 christos copy_private_section_data. */
3226 1.1 christos
3227 1.1 christos this_hdr->bfd_section = asect;
3228 1.1 christos this_hdr->contents = NULL;
3229 1.1 christos
3230 1.14 christos /* If the section type is unspecified, we set it based on
3231 1.14 christos asect->flags. */
3232 1.14 christos if (asect->type != 0)
3233 1.1 christos sh_type = asect->type;
3234 1.1 christos else if ((asect->flags & SEC_GROUP) != 0)
3235 1.1 christos sh_type = SHT_GROUP;
3236 1.1 christos else
3237 1.1 christos sh_type = bfd_elf_get_default_section_type (asect->flags);
3238 1.1 christos
3239 1.1 christos if (this_hdr->sh_type == SHT_NULL)
3240 1.1 christos this_hdr->sh_type = sh_type;
3241 1.1 christos else if (this_hdr->sh_type == SHT_NOBITS
3242 1.1 christos && sh_type == SHT_PROGBITS
3243 1.1 christos && (asect->flags & SEC_ALLOC) != 0)
3244 1.1 christos {
3245 1.1 christos /* Warn if we are changing a NOBITS section to PROGBITS, but
3246 1.1 christos allow the link to proceed. This can happen when users link
3247 1.9 christos non-bss input sections to bss output sections, or emit data
3248 1.11 christos to a bss output section via a linker script. */
3249 1.1 christos _bfd_error_handler
3250 1.1 christos (_("warning: section `%pA' type changed to PROGBITS"), asect);
3251 1.1 christos this_hdr->sh_type = sh_type;
3252 1.1 christos }
3253 1.1 christos
3254 1.1 christos switch (this_hdr->sh_type)
3255 1.1 christos {
3256 1.1 christos default:
3257 1.1 christos break;
3258 1.8 christos
3259 1.8 christos case SHT_STRTAB:
3260 1.8 christos case SHT_NOTE:
3261 1.8 christos case SHT_NOBITS:
3262 1.8 christos case SHT_PROGBITS:
3263 1.1 christos break;
3264 1.1 christos
3265 1.1 christos case SHT_INIT_ARRAY:
3266 1.8 christos case SHT_FINI_ARRAY:
3267 1.1 christos case SHT_PREINIT_ARRAY:
3268 1.1 christos this_hdr->sh_entsize = bed->s->arch_size / 8;
3269 1.1 christos break;
3270 1.1 christos
3271 1.1 christos case SHT_HASH:
3272 1.1 christos this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3273 1.1 christos break;
3274 1.1 christos
3275 1.1 christos case SHT_DYNSYM:
3276 1.1 christos this_hdr->sh_entsize = bed->s->sizeof_sym;
3277 1.1 christos break;
3278 1.1 christos
3279 1.1 christos case SHT_DYNAMIC:
3280 1.1 christos this_hdr->sh_entsize = bed->s->sizeof_dyn;
3281 1.1 christos break;
3282 1.1 christos
3283 1.1 christos case SHT_RELA:
3284 1.1 christos if (get_elf_backend_data (abfd)->may_use_rela_p)
3285 1.1 christos this_hdr->sh_entsize = bed->s->sizeof_rela;
3286 1.1 christos break;
3287 1.1 christos
3288 1.1 christos case SHT_REL:
3289 1.1 christos if (get_elf_backend_data (abfd)->may_use_rel_p)
3290 1.1 christos this_hdr->sh_entsize = bed->s->sizeof_rel;
3291 1.1 christos break;
3292 1.1 christos
3293 1.1 christos case SHT_GNU_versym:
3294 1.1 christos this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3295 1.1 christos break;
3296 1.1 christos
3297 1.1 christos case SHT_GNU_verdef:
3298 1.1 christos this_hdr->sh_entsize = 0;
3299 1.1 christos /* objcopy or strip will copy over sh_info, but may not set
3300 1.1 christos cverdefs. The linker will set cverdefs, but sh_info will be
3301 1.1 christos zero. */
3302 1.1 christos if (this_hdr->sh_info == 0)
3303 1.1 christos this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3304 1.1 christos else
3305 1.1 christos BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3306 1.1 christos || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3307 1.1 christos break;
3308 1.1 christos
3309 1.1 christos case SHT_GNU_verneed:
3310 1.1 christos this_hdr->sh_entsize = 0;
3311 1.1 christos /* objcopy or strip will copy over sh_info, but may not set
3312 1.1 christos cverrefs. The linker will set cverrefs, but sh_info will be
3313 1.1 christos zero. */
3314 1.1 christos if (this_hdr->sh_info == 0)
3315 1.1 christos this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3316 1.1 christos else
3317 1.1 christos BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3318 1.1 christos || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3319 1.1 christos break;
3320 1.1 christos
3321 1.1 christos case SHT_GROUP:
3322 1.1 christos this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3323 1.1 christos break;
3324 1.1 christos
3325 1.1 christos case SHT_GNU_HASH:
3326 1.1 christos this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3327 1.1 christos break;
3328 1.1 christos }
3329 1.1 christos
3330 1.1 christos if ((asect->flags & SEC_ALLOC) != 0)
3331 1.1 christos this_hdr->sh_flags |= SHF_ALLOC;
3332 1.1 christos if ((asect->flags & SEC_READONLY) == 0)
3333 1.1 christos this_hdr->sh_flags |= SHF_WRITE;
3334 1.1 christos if ((asect->flags & SEC_CODE) != 0)
3335 1.1 christos this_hdr->sh_flags |= SHF_EXECINSTR;
3336 1.1 christos if ((asect->flags & SEC_MERGE) != 0)
3337 1.1 christos {
3338 1.1 christos this_hdr->sh_flags |= SHF_MERGE;
3339 1.8 christos this_hdr->sh_entsize = asect->entsize;
3340 1.8 christos }
3341 1.1 christos if ((asect->flags & SEC_STRINGS) != 0)
3342 1.1 christos this_hdr->sh_flags |= SHF_STRINGS;
3343 1.1 christos if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3344 1.1 christos this_hdr->sh_flags |= SHF_GROUP;
3345 1.1 christos if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3346 1.1 christos {
3347 1.1 christos this_hdr->sh_flags |= SHF_TLS;
3348 1.1 christos if (asect->size == 0
3349 1.1 christos && (asect->flags & SEC_HAS_CONTENTS) == 0)
3350 1.1 christos {
3351 1.1 christos struct bfd_link_order *o = asect->map_tail.link_order;
3352 1.1 christos
3353 1.1 christos this_hdr->sh_size = 0;
3354 1.1 christos if (o != NULL)
3355 1.1 christos {
3356 1.1 christos this_hdr->sh_size = o->offset + o->size;
3357 1.1 christos if (this_hdr->sh_size != 0)
3358 1.1 christos this_hdr->sh_type = SHT_NOBITS;
3359 1.1 christos }
3360 1.1 christos }
3361 1.1 christos }
3362 1.1 christos if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3363 1.1 christos this_hdr->sh_flags |= SHF_EXCLUDE;
3364 1.1 christos
3365 1.1 christos /* If the section has relocs, set up a section header for the
3366 1.1 christos SHT_REL[A] section. If two relocation sections are required for
3367 1.1 christos this section, it is up to the processor-specific back-end to
3368 1.1 christos create the other. */
3369 1.1 christos if ((asect->flags & SEC_RELOC) != 0)
3370 1.1 christos {
3371 1.1 christos /* When doing a relocatable link, create both REL and RELA sections if
3372 1.1 christos needed. */
3373 1.1 christos if (arg->link_info
3374 1.8 christos /* Do the normal setup if we wouldn't create any sections here. */
3375 1.8 christos && esd->rel.count + esd->rela.count > 0
3376 1.1 christos && (bfd_link_relocatable (arg->link_info)
3377 1.1 christos || arg->link_info->emitrelocations))
3378 1.11 christos {
3379 1.14 christos if (esd->rel.count && esd->rel.hdr == NULL
3380 1.1 christos && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3381 1.14 christos false, delay_st_name_p))
3382 1.1 christos {
3383 1.1 christos arg->failed = true;
3384 1.1 christos return;
3385 1.11 christos }
3386 1.14 christos if (esd->rela.count && esd->rela.hdr == NULL
3387 1.1 christos && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3388 1.14 christos true, delay_st_name_p))
3389 1.1 christos {
3390 1.1 christos arg->failed = true;
3391 1.1 christos return;
3392 1.1 christos }
3393 1.1 christos }
3394 1.1 christos else if (!_bfd_elf_init_reloc_shdr (abfd,
3395 1.6 christos (asect->use_rela_p
3396 1.6 christos ? &esd->rela : &esd->rel),
3397 1.6 christos name,
3398 1.11 christos asect->use_rela_p,
3399 1.14 christos delay_st_name_p))
3400 1.11 christos {
3401 1.11 christos arg->failed = true;
3402 1.1 christos return;
3403 1.1 christos }
3404 1.1 christos }
3405 1.1 christos
3406 1.1 christos /* Check for processor-specific section types. */
3407 1.1 christos sh_type = this_hdr->sh_type;
3408 1.11 christos if (bed->elf_backend_fake_sections
3409 1.14 christos && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3410 1.11 christos {
3411 1.11 christos arg->failed = true;
3412 1.1 christos return;
3413 1.1 christos }
3414 1.1 christos
3415 1.1 christos if (sh_type == SHT_NOBITS && asect->size != 0)
3416 1.1 christos {
3417 1.1 christos /* Don't change the header type from NOBITS if we are being
3418 1.1 christos called for objcopy --only-keep-debug. */
3419 1.1 christos this_hdr->sh_type = sh_type;
3420 1.1 christos }
3421 1.1 christos }
3422 1.1 christos
3423 1.1 christos /* Fill in the contents of a SHT_GROUP section. Called from
3424 1.1 christos _bfd_elf_compute_section_file_positions for gas, objcopy, and
3425 1.1 christos when ELF targets use the generic linker, ld. Called for ld -r
3426 1.1 christos from bfd_elf_final_link. */
3427 1.1 christos
3428 1.1 christos void
3429 1.14 christos bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3430 1.1 christos {
3431 1.1 christos bool *failedptr = (bool *) failedptrarg;
3432 1.14 christos asection *elt, *first;
3433 1.1 christos unsigned char *loc;
3434 1.1 christos bool gas;
3435 1.1 christos
3436 1.12 christos /* Ignore linker created group section. See elfNN_ia64_object_p in
3437 1.12 christos elfxx-ia64.c. */
3438 1.1 christos if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3439 1.1 christos || sec->size == 0
3440 1.1 christos || *failedptr)
3441 1.1 christos return;
3442 1.1 christos
3443 1.1 christos if (elf_section_data (sec)->this_hdr.sh_info == 0)
3444 1.1 christos {
3445 1.1 christos unsigned long symindx = 0;
3446 1.1 christos
3447 1.1 christos /* elf_group_id will have been set up by objcopy and the
3448 1.1 christos generic linker. */
3449 1.1 christos if (elf_group_id (sec) != NULL)
3450 1.1 christos symindx = elf_group_id (sec)->udata.i;
3451 1.1 christos
3452 1.1 christos if (symindx == 0)
3453 1.12 christos {
3454 1.12 christos /* If called from the assembler, swap_out_syms will have set up
3455 1.14 christos elf_section_syms.
3456 1.14 christos PR 25699: A corrupt input file could contain bogus group info. */
3457 1.12 christos if (sec->index >= elf_num_section_syms (abfd)
3458 1.14 christos || elf_section_syms (abfd)[sec->index] == NULL)
3459 1.12 christos {
3460 1.12 christos *failedptr = true;
3461 1.1 christos return;
3462 1.1 christos }
3463 1.1 christos symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3464 1.1 christos }
3465 1.1 christos elf_section_data (sec)->this_hdr.sh_info = symindx;
3466 1.1 christos }
3467 1.1 christos else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
3468 1.1 christos {
3469 1.1 christos /* The ELF backend linker sets sh_info to -2 when the group
3470 1.9 christos signature symbol is global, and thus the index can't be
3471 1.9 christos set until all local symbols are output. */
3472 1.9 christos asection *igroup;
3473 1.9 christos struct bfd_elf_section_data *sec_data;
3474 1.1 christos unsigned long symndx;
3475 1.1 christos unsigned long extsymoff;
3476 1.9 christos struct elf_link_hash_entry *h;
3477 1.9 christos
3478 1.9 christos /* The point of this little dance to the first SHF_GROUP section
3479 1.9 christos then back to the SHT_GROUP section is that this gets us to
3480 1.9 christos the SHT_GROUP in the input object. */
3481 1.9 christos igroup = elf_sec_group (elf_next_in_group (sec));
3482 1.9 christos sec_data = elf_section_data (igroup);
3483 1.1 christos symndx = sec_data->this_hdr.sh_info;
3484 1.1 christos extsymoff = 0;
3485 1.1 christos if (!elf_bad_symtab (igroup->owner))
3486 1.1 christos {
3487 1.1 christos Elf_Internal_Shdr *symtab_hdr;
3488 1.1 christos
3489 1.1 christos symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3490 1.1 christos extsymoff = symtab_hdr->sh_info;
3491 1.1 christos }
3492 1.1 christos h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3493 1.1 christos while (h->root.type == bfd_link_hash_indirect
3494 1.1 christos || h->root.type == bfd_link_hash_warning)
3495 1.1 christos h = (struct elf_link_hash_entry *) h->root.u.i.link;
3496 1.1 christos
3497 1.1 christos elf_section_data (sec)->this_hdr.sh_info = h->indx;
3498 1.1 christos }
3499 1.14 christos
3500 1.1 christos /* The contents won't be allocated for "ld -r" or objcopy. */
3501 1.1 christos gas = true;
3502 1.14 christos if (sec->contents == NULL)
3503 1.1 christos {
3504 1.1 christos gas = false;
3505 1.1 christos sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3506 1.1 christos
3507 1.1 christos /* Arrange for the section to be written out. */
3508 1.1 christos elf_section_data (sec)->this_hdr.contents = sec->contents;
3509 1.14 christos if (sec->contents == NULL)
3510 1.1 christos {
3511 1.1 christos *failedptr = true;
3512 1.1 christos return;
3513 1.1 christos }
3514 1.1 christos }
3515 1.1 christos
3516 1.1 christos loc = sec->contents + sec->size;
3517 1.1 christos
3518 1.1 christos /* Get the pointer to the first section in the group that gas
3519 1.1 christos squirreled away here. objcopy arranges for this to be set to the
3520 1.1 christos start of the input section group. */
3521 1.1 christos first = elt = elf_next_in_group (sec);
3522 1.1 christos
3523 1.1 christos /* First element is a flag word. Rest of section is elf section
3524 1.1 christos indices for all the sections of the group. Write them backwards
3525 1.1 christos just to keep the group in the same order as given in .section
3526 1.1 christos directives, not that it matters. */
3527 1.1 christos while (elt != NULL)
3528 1.1 christos {
3529 1.1 christos asection *s;
3530 1.1 christos
3531 1.1 christos s = elt;
3532 1.1 christos if (!gas)
3533 1.1 christos s = s->output_section;
3534 1.1 christos if (s != NULL
3535 1.11 christos && !bfd_is_abs_section (s))
3536 1.11 christos {
3537 1.1 christos struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3538 1.11 christos struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3539 1.11 christos
3540 1.11 christos if (elf_sec->rel.hdr != NULL
3541 1.11 christos && (gas
3542 1.11 christos || (input_elf_sec->rel.hdr != NULL
3543 1.11 christos && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
3544 1.11 christos {
3545 1.14 christos elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
3546 1.14 christos loc -= 4;
3547 1.11 christos if (loc == sec->contents)
3548 1.11 christos break;
3549 1.11 christos H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3550 1.11 christos }
3551 1.11 christos if (elf_sec->rela.hdr != NULL
3552 1.11 christos && (gas
3553 1.11 christos || (input_elf_sec->rela.hdr != NULL
3554 1.11 christos && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
3555 1.11 christos {
3556 1.14 christos elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
3557 1.14 christos loc -= 4;
3558 1.11 christos if (loc == sec->contents)
3559 1.11 christos break;
3560 1.1 christos H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3561 1.14 christos }
3562 1.14 christos loc -= 4;
3563 1.11 christos if (loc == sec->contents)
3564 1.1 christos break;
3565 1.1 christos H_PUT_32 (abfd, elf_sec->this_idx, loc);
3566 1.1 christos }
3567 1.1 christos elt = elf_next_in_group (elt);
3568 1.1 christos if (elt == first)
3569 1.1 christos break;
3570 1.14 christos }
3571 1.14 christos
3572 1.14 christos /* We should always get here with loc == sec->contents + 4, but it is
3573 1.14 christos possible to craft bogus SHT_GROUP sections that will cause segfaults
3574 1.14 christos in objcopy without checking loc here and in the loop above. */
3575 1.14 christos if (loc == sec->contents)
3576 1.14 christos BFD_ASSERT (0);
3577 1.14 christos else
3578 1.14 christos {
3579 1.14 christos loc -= 4;
3580 1.14 christos if (loc != sec->contents)
3581 1.14 christos {
3582 1.14 christos BFD_ASSERT (0);
3583 1.14 christos memset (sec->contents + 4, 0, loc - sec->contents);
3584 1.14 christos loc = sec->contents;
3585 1.1 christos }
3586 1.1 christos }
3587 1.1 christos
3588 1.1 christos H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3589 1.11 christos }
3590 1.11 christos
3591 1.11 christos /* Given NAME, the name of a relocation section stripped of its
3592 1.6 christos .rel/.rela prefix, return the section in ABFD to which the
3593 1.6 christos relocations apply. */
3594 1.11 christos
3595 1.11 christos asection *
3596 1.11 christos _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3597 1.11 christos {
3598 1.11 christos /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3599 1.11 christos section likely apply to .got.plt or .got section. */
3600 1.11 christos if (get_elf_backend_data (abfd)->want_got_plt
3601 1.11 christos && strcmp (name, ".plt") == 0)
3602 1.11 christos {
3603 1.11 christos asection *sec;
3604 1.11 christos
3605 1.11 christos name = ".got.plt";
3606 1.11 christos sec = bfd_get_section_by_name (abfd, name);
3607 1.11 christos if (sec != NULL)
3608 1.11 christos return sec;
3609 1.11 christos name = ".got";
3610 1.11 christos }
3611 1.11 christos
3612 1.11 christos return bfd_get_section_by_name (abfd, name);
3613 1.11 christos }
3614 1.11 christos
3615 1.11 christos /* Return the section to which RELOC_SEC applies. */
3616 1.11 christos
3617 1.6 christos static asection *
3618 1.6 christos elf_get_reloc_section (asection *reloc_sec)
3619 1.6 christos {
3620 1.6 christos const char *name;
3621 1.11 christos unsigned int type;
3622 1.6 christos bfd *abfd;
3623 1.6 christos const struct elf_backend_data *bed;
3624 1.6 christos
3625 1.6 christos type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3626 1.6 christos if (type != SHT_REL && type != SHT_RELA)
3627 1.6 christos return NULL;
3628 1.6 christos
3629 1.14 christos /* We look up the section the relocs apply to by name. */
3630 1.11 christos name = reloc_sec->name;
3631 1.11 christos if (!startswith (name, ".rel"))
3632 1.11 christos return NULL;
3633 1.11 christos name += 4;
3634 1.6 christos if (type == SHT_RELA && *name++ != 'a')
3635 1.6 christos return NULL;
3636 1.11 christos
3637 1.11 christos abfd = reloc_sec->owner;
3638 1.6 christos bed = get_elf_backend_data (abfd);
3639 1.6 christos return bed->get_reloc_section (abfd, name);
3640 1.1 christos }
3641 1.1 christos
3642 1.12 christos /* Assign all ELF section numbers. The dummy first section is handled here
3643 1.14 christos too. The link/info pointers for the standard section types are filled
3644 1.1 christos in here too, while we're at it. LINK_INFO will be 0 when arriving
3645 1.14 christos here for gas, objcopy, and when using the generic ELF linker. */
3646 1.1 christos
3647 1.1 christos static bool
3648 1.1 christos assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
3649 1.1 christos {
3650 1.6 christos struct elf_obj_tdata *t = elf_tdata (abfd);
3651 1.1 christos asection *sec;
3652 1.1 christos unsigned int section_number;
3653 1.14 christos Elf_Internal_Shdr **i_shdrp;
3654 1.12 christos struct bfd_elf_section_data *d;
3655 1.1 christos bool need_symtab;
3656 1.1 christos size_t amt;
3657 1.1 christos
3658 1.1 christos section_number = 1;
3659 1.1 christos
3660 1.1 christos _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3661 1.11 christos
3662 1.1 christos /* SHT_GROUP sections are in relocatable files only. */
3663 1.8 christos if (link_info == NULL || !link_info->resolve_section_groups)
3664 1.8 christos {
3665 1.1 christos size_t reloc_count = 0;
3666 1.1 christos
3667 1.1 christos /* Put SHT_GROUP sections first. */
3668 1.1 christos for (sec = abfd->sections; sec != NULL; sec = sec->next)
3669 1.1 christos {
3670 1.1 christos d = elf_section_data (sec);
3671 1.1 christos
3672 1.1 christos if (d->this_hdr.sh_type == SHT_GROUP)
3673 1.1 christos {
3674 1.1 christos if (sec->flags & SEC_LINKER_CREATED)
3675 1.1 christos {
3676 1.1 christos /* Remove the linker created SHT_GROUP sections. */
3677 1.1 christos bfd_section_list_remove (abfd, sec);
3678 1.1 christos abfd->section_count--;
3679 1.1 christos }
3680 1.1 christos else
3681 1.8 christos d->this_idx = section_number++;
3682 1.8 christos }
3683 1.8 christos
3684 1.1 christos /* Count relocations. */
3685 1.8 christos reloc_count += sec->reloc_count;
3686 1.14 christos }
3687 1.8 christos
3688 1.8 christos /* Set/clear HAS_RELOC depending on whether there are relocations. */
3689 1.14 christos if (reloc_count == 0)
3690 1.14 christos abfd->flags &= ~HAS_RELOC;
3691 1.1 christos else
3692 1.1 christos abfd->flags |= HAS_RELOC;
3693 1.1 christos }
3694 1.1 christos
3695 1.1 christos for (sec = abfd->sections; sec; sec = sec->next)
3696 1.1 christos {
3697 1.1 christos d = elf_section_data (sec);
3698 1.1 christos
3699 1.6 christos if (d->this_hdr.sh_type != SHT_GROUP)
3700 1.6 christos d->this_idx = section_number++;
3701 1.1 christos if (d->this_hdr.sh_name != (unsigned int) -1)
3702 1.1 christos _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
3703 1.1 christos if (d->rel.hdr)
3704 1.6 christos {
3705 1.6 christos d->rel.idx = section_number++;
3706 1.1 christos if (d->rel.hdr->sh_name != (unsigned int) -1)
3707 1.1 christos _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
3708 1.1 christos }
3709 1.1 christos else
3710 1.1 christos d->rel.idx = 0;
3711 1.1 christos
3712 1.1 christos if (d->rela.hdr)
3713 1.6 christos {
3714 1.6 christos d->rela.idx = section_number++;
3715 1.1 christos if (d->rela.hdr->sh_name != (unsigned int) -1)
3716 1.1 christos _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
3717 1.1 christos }
3718 1.1 christos else
3719 1.1 christos d->rela.idx = 0;
3720 1.1 christos }
3721 1.14 christos
3722 1.14 christos need_symtab = (bfd_get_symcount (abfd) > 0
3723 1.14 christos || (link_info == NULL
3724 1.1 christos && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3725 1.1 christos == HAS_RELOC)));
3726 1.3 christos if (need_symtab)
3727 1.1 christos {
3728 1.1 christos elf_onesymtab (abfd) = section_number++;
3729 1.1 christos _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
3730 1.12 christos if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
3731 1.8 christos {
3732 1.8 christos elf_section_list *entry;
3733 1.8 christos
3734 1.12 christos BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3735 1.8 christos
3736 1.8 christos entry = bfd_zalloc (abfd, sizeof (*entry));
3737 1.8 christos entry->ndx = section_number++;
3738 1.1 christos elf_symtab_shndx_list (abfd) = entry;
3739 1.14 christos entry->hdr.sh_name
3740 1.8 christos = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3741 1.14 christos ".symtab_shndx", false);
3742 1.1 christos if (entry->hdr.sh_name == (unsigned int) -1)
3743 1.3 christos return false;
3744 1.1 christos }
3745 1.1 christos elf_strtab_sec (abfd) = section_number++;
3746 1.1 christos _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
3747 1.9 christos }
3748 1.9 christos
3749 1.9 christos elf_shstrtab_sec (abfd) = section_number++;
3750 1.9 christos _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3751 1.3 christos elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3752 1.3 christos
3753 1.9 christos if (section_number >= SHN_LORESERVE)
3754 1.11 christos {
3755 1.3 christos /* xgettext:c-format */
3756 1.14 christos _bfd_error_handler (_("%pB: too many sections: %u"),
3757 1.3 christos abfd, section_number);
3758 1.3 christos return false;
3759 1.1 christos }
3760 1.1 christos
3761 1.1 christos elf_numsections (abfd) = section_number;
3762 1.1 christos elf_elfheader (abfd)->e_shnum = section_number;
3763 1.1 christos
3764 1.12 christos /* Set up the list of section header pointers, in agreement with the
3765 1.12 christos indices. */
3766 1.1 christos amt = section_number * sizeof (Elf_Internal_Shdr *);
3767 1.14 christos i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
3768 1.1 christos if (i_shdrp == NULL)
3769 1.1 christos return false;
3770 1.11 christos
3771 1.1 christos i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
3772 1.1 christos sizeof (Elf_Internal_Shdr));
3773 1.1 christos if (i_shdrp[0] == NULL)
3774 1.14 christos {
3775 1.1 christos bfd_release (abfd, i_shdrp);
3776 1.1 christos return false;
3777 1.1 christos }
3778 1.1 christos
3779 1.3 christos elf_elfsections (abfd) = i_shdrp;
3780 1.1 christos
3781 1.1 christos i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3782 1.3 christos if (need_symtab)
3783 1.1 christos {
3784 1.1 christos i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
3785 1.8 christos if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
3786 1.8 christos {
3787 1.8 christos elf_section_list * entry = elf_symtab_shndx_list (abfd);
3788 1.8 christos BFD_ASSERT (entry != NULL);
3789 1.1 christos i_shdrp[entry->ndx] = & entry->hdr;
3790 1.3 christos entry->hdr.sh_link = elf_onesymtab (abfd);
3791 1.3 christos }
3792 1.1 christos i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3793 1.1 christos t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
3794 1.1 christos }
3795 1.1 christos
3796 1.1 christos for (sec = abfd->sections; sec; sec = sec->next)
3797 1.1 christos {
3798 1.1 christos asection *s;
3799 1.1 christos
3800 1.1 christos d = elf_section_data (sec);
3801 1.1 christos
3802 1.1 christos i_shdrp[d->this_idx] = &d->this_hdr;
3803 1.1 christos if (d->rel.idx != 0)
3804 1.1 christos i_shdrp[d->rel.idx] = d->rel.hdr;
3805 1.1 christos if (d->rela.idx != 0)
3806 1.1 christos i_shdrp[d->rela.idx] = d->rela.hdr;
3807 1.1 christos
3808 1.1 christos /* Fill in the sh_link and sh_info fields while we're at it. */
3809 1.1 christos
3810 1.1 christos /* sh_link of a reloc section is the section index of the symbol
3811 1.1 christos table. sh_info is the section index of the section to which
3812 1.1 christos the relocation entries apply. */
3813 1.3 christos if (d->rel.idx != 0)
3814 1.1 christos {
3815 1.4 christos d->rel.hdr->sh_link = elf_onesymtab (abfd);
3816 1.1 christos d->rel.hdr->sh_info = d->this_idx;
3817 1.1 christos d->rel.hdr->sh_flags |= SHF_INFO_LINK;
3818 1.1 christos }
3819 1.3 christos if (d->rela.idx != 0)
3820 1.1 christos {
3821 1.4 christos d->rela.hdr->sh_link = elf_onesymtab (abfd);
3822 1.1 christos d->rela.hdr->sh_info = d->this_idx;
3823 1.1 christos d->rela.hdr->sh_flags |= SHF_INFO_LINK;
3824 1.1 christos }
3825 1.1 christos
3826 1.1 christos /* We need to set up sh_link for SHF_LINK_ORDER. */
3827 1.1 christos if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3828 1.14 christos {
3829 1.14 christos s = elf_linked_to_section (sec);
3830 1.14 christos /* We can now have a NULL linked section pointer.
3831 1.14 christos This happens when the sh_link field is 0, which is done
3832 1.1 christos when a linked to section is discarded but the linking
3833 1.1 christos section has been retained for some reason. */
3834 1.12 christos if (s)
3835 1.12 christos {
3836 1.1 christos /* Check discarded linkonce section. */
3837 1.12 christos if (discarded_section (s))
3838 1.12 christos {
3839 1.12 christos asection *kept;
3840 1.12 christos _bfd_error_handler
3841 1.12 christos /* xgettext:c-format */
3842 1.12 christos (_("%pB: sh_link of section `%pA' points to"
3843 1.12 christos " discarded section `%pA' of `%pB'"),
3844 1.12 christos abfd, d->this_hdr.bfd_section, s, s->owner);
3845 1.12 christos /* Point to the kept section if it has the same
3846 1.12 christos size as the discarded one. */
3847 1.1 christos kept = _bfd_elf_check_kept_section (s, link_info);
3848 1.12 christos if (kept == NULL)
3849 1.14 christos {
3850 1.1 christos bfd_set_error (bfd_error_bad_value);
3851 1.12 christos return false;
3852 1.1 christos }
3853 1.12 christos s = kept;
3854 1.12 christos }
3855 1.1 christos /* Handle objcopy. */
3856 1.12 christos else if (s->output_section == NULL)
3857 1.12 christos {
3858 1.12 christos _bfd_error_handler
3859 1.12 christos /* xgettext:c-format */
3860 1.12 christos (_("%pB: sh_link of section `%pA' points to"
3861 1.12 christos " removed section `%pA' of `%pB'"),
3862 1.14 christos abfd, d->this_hdr.bfd_section, s, s->owner);
3863 1.1 christos bfd_set_error (bfd_error_bad_value);
3864 1.12 christos return false;
3865 1.1 christos }
3866 1.1 christos s = s->output_section;
3867 1.1 christos d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3868 1.1 christos }
3869 1.1 christos }
3870 1.1 christos
3871 1.1 christos switch (d->this_hdr.sh_type)
3872 1.1 christos {
3873 1.1 christos case SHT_REL:
3874 1.1 christos case SHT_RELA:
3875 1.1 christos /* A reloc section which we are treating as a normal BFD
3876 1.1 christos section. sh_link is the section index of the symbol
3877 1.14 christos table. sh_info is the section index of the section to
3878 1.14 christos which the relocation entries apply. We assume that an
3879 1.14 christos allocated reloc section uses the dynamic symbol table
3880 1.14 christos if there is one. Otherwise we guess the normal symbol
3881 1.14 christos table. FIXME: How can we be sure? */
3882 1.14 christos if (d->this_hdr.sh_link == 0 && (sec->flags & SEC_ALLOC) != 0)
3883 1.14 christos {
3884 1.14 christos s = bfd_get_section_by_name (abfd, ".dynsym");
3885 1.14 christos if (s != NULL)
3886 1.14 christos d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3887 1.14 christos }
3888 1.1 christos if (d->this_hdr.sh_link == 0)
3889 1.11 christos d->this_hdr.sh_link = elf_onesymtab (abfd);
3890 1.1 christos
3891 1.4 christos s = elf_get_reloc_section (sec);
3892 1.4 christos if (s != NULL)
3893 1.4 christos {
3894 1.4 christos d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3895 1.1 christos d->this_hdr.sh_flags |= SHF_INFO_LINK;
3896 1.1 christos }
3897 1.1 christos break;
3898 1.1 christos
3899 1.1 christos case SHT_STRTAB:
3900 1.1 christos /* We assume that a section named .stab*str is a stabs
3901 1.1 christos string section. We look for a section with the same name
3902 1.14 christos but without the trailing ``str'', and set its sh_link
3903 1.1 christos field to point to this section. */
3904 1.1 christos if (startswith (sec->name, ".stab")
3905 1.1 christos && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3906 1.1 christos {
3907 1.1 christos size_t len;
3908 1.1 christos char *alc;
3909 1.1 christos
3910 1.1 christos len = strlen (sec->name);
3911 1.14 christos alc = (char *) bfd_malloc (len - 2);
3912 1.1 christos if (alc == NULL)
3913 1.1 christos return false;
3914 1.1 christos memcpy (alc, sec->name, len - 3);
3915 1.1 christos alc[len - 3] = '\0';
3916 1.1 christos s = bfd_get_section_by_name (abfd, alc);
3917 1.1 christos free (alc);
3918 1.1 christos if (s != NULL)
3919 1.1 christos {
3920 1.1 christos elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3921 1.12 christos
3922 1.1 christos /* This is a .stab section. */
3923 1.1 christos elf_section_data (s)->this_hdr.sh_entsize = 12;
3924 1.1 christos }
3925 1.1 christos }
3926 1.1 christos break;
3927 1.1 christos
3928 1.1 christos case SHT_DYNAMIC:
3929 1.1 christos case SHT_DYNSYM:
3930 1.1 christos case SHT_GNU_verneed:
3931 1.1 christos case SHT_GNU_verdef:
3932 1.1 christos /* sh_link is the section header index of the string table
3933 1.1 christos used for the dynamic entries, or the symbol table, or the
3934 1.1 christos version strings. */
3935 1.1 christos s = bfd_get_section_by_name (abfd, ".dynstr");
3936 1.1 christos if (s != NULL)
3937 1.1 christos d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3938 1.1 christos break;
3939 1.1 christos
3940 1.1 christos case SHT_GNU_LIBLIST:
3941 1.1 christos /* sh_link is the section header index of the prelink library
3942 1.14 christos list used for the dynamic entries, or the symbol table, or
3943 1.14 christos the version strings. */
3944 1.1 christos s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
3945 1.1 christos ? ".dynstr" : ".gnu.libstr"));
3946 1.1 christos if (s != NULL)
3947 1.1 christos d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3948 1.1 christos break;
3949 1.1 christos
3950 1.1 christos case SHT_HASH:
3951 1.1 christos case SHT_GNU_HASH:
3952 1.1 christos case SHT_GNU_versym:
3953 1.1 christos /* sh_link is the section header index of the symbol table
3954 1.1 christos this hash table or version table is for. */
3955 1.1 christos s = bfd_get_section_by_name (abfd, ".dynsym");
3956 1.1 christos if (s != NULL)
3957 1.1 christos d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3958 1.1 christos break;
3959 1.3 christos
3960 1.1 christos case SHT_GROUP:
3961 1.1 christos d->this_hdr.sh_link = elf_onesymtab (abfd);
3962 1.1 christos }
3963 1.6 christos }
3964 1.6 christos
3965 1.6 christos /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3966 1.6 christos _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3967 1.14 christos debug section name from .debug_* to .zdebug_* if needed. */
3968 1.1 christos
3969 1.1 christos return true;
3970 1.14 christos }
3971 1.1 christos
3972 1.1 christos static bool
3973 1.1 christos sym_is_global (bfd *abfd, asymbol *sym)
3974 1.1 christos {
3975 1.1 christos /* If the backend has a special mapping, use it. */
3976 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3977 1.1 christos if (bed->elf_backend_sym_is_global)
3978 1.1 christos return (*bed->elf_backend_sym_is_global) (abfd, sym);
3979 1.12 christos
3980 1.12 christos return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
3981 1.1 christos || bfd_is_und_section (bfd_asymbol_section (sym))
3982 1.1 christos || bfd_is_com_section (bfd_asymbol_section (sym)));
3983 1.8 christos }
3984 1.8 christos
3985 1.8 christos /* Filter global symbols of ABFD to include in the import library. All
3986 1.8 christos SYMCOUNT symbols of ABFD can be examined from their pointers in
3987 1.8 christos SYMS. Pointers of symbols to keep should be stored contiguously at
3988 1.8 christos the beginning of that array.
3989 1.8 christos
3990 1.8 christos Returns the number of symbols to keep. */
3991 1.8 christos
3992 1.8 christos unsigned int
3993 1.8 christos _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
3994 1.8 christos asymbol **syms, long symcount)
3995 1.8 christos {
3996 1.8 christos long src_count, dst_count = 0;
3997 1.8 christos
3998 1.8 christos for (src_count = 0; src_count < symcount; src_count++)
3999 1.8 christos {
4000 1.8 christos asymbol *sym = syms[src_count];
4001 1.8 christos char *name = (char *) bfd_asymbol_name (sym);
4002 1.8 christos struct bfd_link_hash_entry *h;
4003 1.8 christos
4004 1.8 christos if (!sym_is_global (abfd, sym))
4005 1.14 christos continue;
4006 1.8 christos
4007 1.8 christos h = bfd_link_hash_lookup (info->hash, name, false, false, false);
4008 1.8 christos if (h == NULL)
4009 1.8 christos continue;
4010 1.8 christos if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4011 1.8 christos continue;
4012 1.8 christos if (h->linker_def || h->ldscript_def)
4013 1.8 christos continue;
4014 1.8 christos
4015 1.8 christos syms[dst_count++] = sym;
4016 1.8 christos }
4017 1.8 christos
4018 1.8 christos syms[dst_count] = NULL;
4019 1.8 christos
4020 1.8 christos return dst_count;
4021 1.1 christos }
4022 1.3 christos
4023 1.1 christos /* Don't output section symbols for sections that are not going to be
4024 1.14 christos output, that are duplicates or there is no BFD section. */
4025 1.1 christos
4026 1.1 christos static bool
4027 1.3 christos ignore_section_sym (bfd *abfd, asymbol *sym)
4028 1.3 christos {
4029 1.11 christos elf_symbol_type *type_ptr;
4030 1.14 christos
4031 1.11 christos if (sym == NULL)
4032 1.3 christos return false;
4033 1.14 christos
4034 1.14 christos if ((sym->flags & BSF_SECTION_SYM) == 0)
4035 1.14 christos return false;
4036 1.14 christos
4037 1.14 christos /* Ignore the section symbol if it isn't used. */
4038 1.3 christos if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4039 1.11 christos return true;
4040 1.14 christos
4041 1.11 christos if (sym->section == NULL)
4042 1.14 christos return true;
4043 1.3 christos
4044 1.3 christos type_ptr = elf_symbol_from (sym);
4045 1.3 christos return ((type_ptr != NULL
4046 1.3 christos && type_ptr->internal_elf_sym.st_shndx != 0
4047 1.11 christos && bfd_is_abs_section (sym->section))
4048 1.11 christos || !(sym->section->owner == abfd
4049 1.3 christos || (sym->section->output_section != NULL
4050 1.3 christos && sym->section->output_section->owner == abfd
4051 1.1 christos && sym->section->output_offset == 0)
4052 1.1 christos || bfd_is_abs_section (sym->section)));
4053 1.3 christos }
4054 1.3 christos
4055 1.3 christos /* Map symbol from it's internal number to the external number, moving
4056 1.14 christos all local symbols to be at the head of the list. */
4057 1.3 christos
4058 1.1 christos static bool
4059 1.1 christos elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4060 1.1 christos {
4061 1.1 christos unsigned int symcount = bfd_get_symcount (abfd);
4062 1.1 christos asymbol **syms = bfd_get_outsymbols (abfd);
4063 1.1 christos asymbol **sect_syms;
4064 1.1 christos unsigned int num_locals = 0;
4065 1.1 christos unsigned int num_globals = 0;
4066 1.8 christos unsigned int num_locals2 = 0;
4067 1.1 christos unsigned int num_globals2 = 0;
4068 1.1 christos unsigned int max_index = 0;
4069 1.1 christos unsigned int idx;
4070 1.12 christos asection *asect;
4071 1.1 christos asymbol **new_syms;
4072 1.1 christos size_t amt;
4073 1.1 christos
4074 1.1 christos #ifdef DEBUG
4075 1.1 christos fprintf (stderr, "elf_map_symbols\n");
4076 1.1 christos fflush (stderr);
4077 1.1 christos #endif
4078 1.1 christos
4079 1.1 christos for (asect = abfd->sections; asect; asect = asect->next)
4080 1.1 christos {
4081 1.1 christos if (max_index < asect->index)
4082 1.1 christos max_index = asect->index;
4083 1.1 christos }
4084 1.12 christos
4085 1.12 christos max_index++;
4086 1.1 christos amt = max_index * sizeof (asymbol *);
4087 1.14 christos sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
4088 1.1 christos if (sect_syms == NULL)
4089 1.1 christos return false;
4090 1.1 christos elf_section_syms (abfd) = sect_syms;
4091 1.1 christos elf_num_section_syms (abfd) = max_index;
4092 1.1 christos
4093 1.1 christos /* Init sect_syms entries for any section symbols we have already
4094 1.1 christos decided to output. */
4095 1.1 christos for (idx = 0; idx < symcount; idx++)
4096 1.1 christos {
4097 1.1 christos asymbol *sym = syms[idx];
4098 1.1 christos
4099 1.3 christos if ((sym->flags & BSF_SECTION_SYM) != 0
4100 1.3 christos && sym->value == 0
4101 1.1 christos && !ignore_section_sym (abfd, sym)
4102 1.1 christos && !bfd_is_abs_section (sym->section))
4103 1.1 christos {
4104 1.1 christos asection *sec = sym->section;
4105 1.1 christos
4106 1.1 christos if (sec->owner != abfd)
4107 1.1 christos sec = sec->output_section;
4108 1.1 christos
4109 1.1 christos sect_syms[sec->index] = syms[idx];
4110 1.1 christos }
4111 1.1 christos }
4112 1.1 christos
4113 1.1 christos /* Classify all of the symbols. */
4114 1.3 christos for (idx = 0; idx < symcount; idx++)
4115 1.3 christos {
4116 1.3 christos if (sym_is_global (abfd, syms[idx]))
4117 1.1 christos num_globals++;
4118 1.1 christos else if (!ignore_section_sym (abfd, syms[idx]))
4119 1.1 christos num_locals++;
4120 1.1 christos }
4121 1.1 christos
4122 1.1 christos /* We will be adding a section symbol for each normal BFD section. Most
4123 1.1 christos sections will already have a section symbol in outsymbols, but
4124 1.1 christos eg. SHT_GROUP sections will not, and we need the section symbol mapped
4125 1.1 christos at least in that case. */
4126 1.14 christos for (asect = abfd->sections; asect; asect = asect->next)
4127 1.14 christos {
4128 1.14 christos asymbol *sym = asect->symbol;
4129 1.14 christos /* Don't include ignored section symbols. */
4130 1.1 christos if (!ignore_section_sym (abfd, sym)
4131 1.1 christos && sect_syms[asect->index] == NULL)
4132 1.1 christos {
4133 1.1 christos if (!sym_is_global (abfd, asect->symbol))
4134 1.1 christos num_locals++;
4135 1.1 christos else
4136 1.1 christos num_globals++;
4137 1.1 christos }
4138 1.1 christos }
4139 1.12 christos
4140 1.12 christos /* Now sort the symbols so the local symbols are first. */
4141 1.1 christos amt = (num_locals + num_globals) * sizeof (asymbol *);
4142 1.14 christos new_syms = (asymbol **) bfd_alloc (abfd, amt);
4143 1.1 christos if (new_syms == NULL)
4144 1.1 christos return false;
4145 1.1 christos
4146 1.1 christos for (idx = 0; idx < symcount; idx++)
4147 1.1 christos {
4148 1.1 christos asymbol *sym = syms[idx];
4149 1.3 christos unsigned int i;
4150 1.3 christos
4151 1.14 christos if (sym_is_global (abfd, sym))
4152 1.3 christos i = num_locals + num_globals2++;
4153 1.1 christos /* Don't include ignored section symbols. */
4154 1.1 christos else if (!ignore_section_sym (abfd, sym))
4155 1.3 christos i = num_locals2++;
4156 1.1 christos else
4157 1.1 christos continue;
4158 1.1 christos new_syms[i] = sym;
4159 1.1 christos sym->udata.i = i + 1;
4160 1.1 christos }
4161 1.14 christos for (asect = abfd->sections; asect; asect = asect->next)
4162 1.14 christos {
4163 1.14 christos asymbol *sym = asect->symbol;
4164 1.1 christos if (!ignore_section_sym (abfd, sym)
4165 1.1 christos && sect_syms[asect->index] == NULL)
4166 1.1 christos {
4167 1.1 christos unsigned int i;
4168 1.1 christos
4169 1.1 christos sect_syms[asect->index] = sym;
4170 1.1 christos if (!sym_is_global (abfd, sym))
4171 1.1 christos i = num_locals2++;
4172 1.1 christos else
4173 1.1 christos i = num_locals + num_globals2++;
4174 1.1 christos new_syms[i] = sym;
4175 1.1 christos sym->udata.i = i + 1;
4176 1.1 christos }
4177 1.1 christos }
4178 1.1 christos
4179 1.3 christos bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4180 1.14 christos
4181 1.1 christos *pnum_locals = num_locals;
4182 1.1 christos return true;
4183 1.1 christos }
4184 1.1 christos
4185 1.1 christos /* Align to the maximum file alignment that could be required for any
4186 1.1 christos ELF data structure. */
4187 1.1 christos
4188 1.1 christos static inline file_ptr
4189 1.1 christos align_file_position (file_ptr off, int align)
4190 1.1 christos {
4191 1.1 christos return (off + align - 1) & ~(align - 1);
4192 1.1 christos }
4193 1.1 christos
4194 1.1 christos /* Assign a file position to a section, optionally aligning to the
4195 1.1 christos required section alignment. */
4196 1.1 christos
4197 1.1 christos file_ptr
4198 1.14 christos _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4199 1.1 christos file_ptr offset,
4200 1.1 christos bool align)
4201 1.14 christos {
4202 1.1 christos if (align && i_shdrp->sh_addralign > 1)
4203 1.1 christos offset = BFD_ALIGN (offset, i_shdrp->sh_addralign & -i_shdrp->sh_addralign);
4204 1.1 christos i_shdrp->sh_offset = offset;
4205 1.1 christos if (i_shdrp->bfd_section != NULL)
4206 1.1 christos i_shdrp->bfd_section->filepos = offset;
4207 1.1 christos if (i_shdrp->sh_type != SHT_NOBITS)
4208 1.1 christos offset += i_shdrp->sh_size;
4209 1.1 christos return offset;
4210 1.1 christos }
4211 1.1 christos
4212 1.1 christos /* Compute the file positions we are going to put the sections at, and
4213 1.1 christos otherwise prepare to begin writing out the ELF file. If LINK_INFO
4214 1.14 christos is not NULL, this is being called by the ELF backend linker. */
4215 1.1 christos
4216 1.1 christos bool
4217 1.1 christos _bfd_elf_compute_section_file_positions (bfd *abfd,
4218 1.1 christos struct bfd_link_info *link_info)
4219 1.1 christos {
4220 1.14 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4221 1.6 christos struct fake_section_arg fsargs;
4222 1.1 christos bool failed;
4223 1.14 christos struct elf_strtab_hash *strtab = NULL;
4224 1.1 christos Elf_Internal_Shdr *shstrtab_hdr;
4225 1.1 christos bool need_symtab;
4226 1.14 christos
4227 1.1 christos if (abfd->output_has_begun)
4228 1.1 christos return true;
4229 1.1 christos
4230 1.1 christos /* Do any elf backend specific processing first. */
4231 1.1 christos if (bed->elf_backend_begin_write_processing)
4232 1.12 christos (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4233 1.14 christos
4234 1.1 christos if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
4235 1.14 christos return false;
4236 1.1 christos
4237 1.1 christos fsargs.failed = false;
4238 1.1 christos fsargs.link_info = link_info;
4239 1.14 christos bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4240 1.1 christos if (fsargs.failed)
4241 1.1 christos return false;
4242 1.14 christos
4243 1.1 christos if (!assign_section_numbers (abfd, link_info))
4244 1.1 christos return false;
4245 1.1 christos
4246 1.1 christos /* The backend linker builds symbol table information itself. */
4247 1.1 christos need_symtab = (link_info == NULL
4248 1.1 christos && (bfd_get_symcount (abfd) > 0
4249 1.1 christos || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4250 1.1 christos == HAS_RELOC)));
4251 1.1 christos if (need_symtab)
4252 1.1 christos {
4253 1.1 christos /* Non-zero if doing a relocatable link. */
4254 1.14 christos int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4255 1.14 christos
4256 1.1 christos if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
4257 1.1 christos return false;
4258 1.14 christos }
4259 1.1 christos
4260 1.1 christos failed = false;
4261 1.1 christos if (link_info == NULL)
4262 1.1 christos {
4263 1.14 christos bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4264 1.1 christos if (failed)
4265 1.1 christos return false;
4266 1.1 christos }
4267 1.12 christos
4268 1.1 christos shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4269 1.8 christos /* sh_name was set in init_file_header. */
4270 1.1 christos shstrtab_hdr->sh_type = SHT_STRTAB;
4271 1.6 christos shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
4272 1.1 christos shstrtab_hdr->sh_addr = 0;
4273 1.1 christos /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4274 1.1 christos shstrtab_hdr->sh_entsize = 0;
4275 1.6 christos shstrtab_hdr->sh_link = 0;
4276 1.1 christos shstrtab_hdr->sh_info = 0;
4277 1.1 christos /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4278 1.1 christos shstrtab_hdr->sh_addralign = 1;
4279 1.14 christos
4280 1.1 christos if (!assign_file_positions_except_relocs (abfd, link_info))
4281 1.1 christos return false;
4282 1.1 christos
4283 1.1 christos if (need_symtab)
4284 1.1 christos {
4285 1.1 christos file_ptr off;
4286 1.3 christos Elf_Internal_Shdr *hdr;
4287 1.1 christos
4288 1.8 christos off = elf_next_file_pos (abfd);
4289 1.14 christos
4290 1.1 christos hdr = & elf_symtab_hdr (abfd);
4291 1.8 christos off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4292 1.8 christos
4293 1.8 christos if (elf_symtab_shndx_list (abfd) != NULL)
4294 1.8 christos {
4295 1.14 christos hdr = & elf_symtab_shndx_list (abfd)->hdr;
4296 1.8 christos if (hdr->sh_size != 0)
4297 1.8 christos off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4298 1.1 christos /* FIXME: What about other symtab_shndx sections in the list ? */
4299 1.1 christos }
4300 1.14 christos
4301 1.1 christos hdr = &elf_tdata (abfd)->strtab_hdr;
4302 1.3 christos off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4303 1.1 christos
4304 1.1 christos elf_next_file_pos (abfd) = off;
4305 1.1 christos
4306 1.1 christos /* Now that we know where the .strtab section goes, write it
4307 1.6 christos out. */
4308 1.14 christos if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4309 1.6 christos || ! _bfd_elf_strtab_emit (abfd, strtab))
4310 1.1 christos return false;
4311 1.1 christos _bfd_elf_strtab_free (strtab);
4312 1.14 christos }
4313 1.1 christos
4314 1.14 christos abfd->output_has_begun = true;
4315 1.14 christos
4316 1.14 christos return true;
4317 1.14 christos }
4318 1.14 christos
4319 1.14 christos /* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the
4320 1.14 christos function effectively returns whether --eh-frame-hdr is given on the
4321 1.14 christos command line. After size_dynamic_sections the result reflects
4322 1.14 christos whether .eh_frame_hdr will actually be output (sizing isn't done
4323 1.14 christos until ldemul_after_allocation). */
4324 1.14 christos
4325 1.14 christos static asection *
4326 1.14 christos elf_eh_frame_hdr (const struct bfd_link_info *info)
4327 1.14 christos {
4328 1.14 christos if (info != NULL && is_elf_hash_table (info->hash))
4329 1.1 christos return elf_hash_table (info)->eh_info.hdr_sec;
4330 1.1 christos return NULL;
4331 1.1 christos }
4332 1.1 christos
4333 1.1 christos /* Make an initial estimate of the size of the program header. If we
4334 1.1 christos get the number wrong here, we'll redo section placement. */
4335 1.1 christos
4336 1.1 christos static bfd_size_type
4337 1.1 christos get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4338 1.1 christos {
4339 1.1 christos size_t segs;
4340 1.1 christos asection *s;
4341 1.1 christos const struct elf_backend_data *bed;
4342 1.1 christos
4343 1.1 christos /* Assume we will need exactly two PT_LOAD segments: one for text
4344 1.1 christos and one for data. */
4345 1.1 christos segs = 2;
4346 1.11 christos
4347 1.1 christos s = bfd_get_section_by_name (abfd, ".interp");
4348 1.1 christos if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4349 1.1 christos {
4350 1.1 christos /* If we have a loadable interpreter section, we need a
4351 1.1 christos PT_INTERP segment. In this case, assume we also need a
4352 1.1 christos PT_PHDR segment, although that may not be true for all
4353 1.1 christos targets. */
4354 1.1 christos segs += 2;
4355 1.1 christos }
4356 1.1 christos
4357 1.1 christos if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4358 1.1 christos {
4359 1.1 christos /* We need a PT_DYNAMIC segment. */
4360 1.1 christos ++segs;
4361 1.1 christos }
4362 1.1 christos
4363 1.1 christos if (info != NULL && info->relro)
4364 1.1 christos {
4365 1.1 christos /* We need a PT_GNU_RELRO segment. */
4366 1.1 christos ++segs;
4367 1.14 christos }
4368 1.1 christos
4369 1.1 christos if (elf_eh_frame_hdr (info))
4370 1.1 christos {
4371 1.1 christos /* We need a PT_GNU_EH_FRAME segment. */
4372 1.1 christos ++segs;
4373 1.3 christos }
4374 1.1 christos
4375 1.1 christos if (elf_stack_flags (abfd))
4376 1.1 christos {
4377 1.1 christos /* We need a PT_GNU_STACK segment. */
4378 1.1 christos ++segs;
4379 1.14 christos }
4380 1.14 christos
4381 1.14 christos if (elf_sframe (abfd))
4382 1.14 christos {
4383 1.14 christos /* We need a PT_GNU_SFRAME segment. */
4384 1.14 christos ++segs;
4385 1.11 christos }
4386 1.11 christos
4387 1.11 christos s = bfd_get_section_by_name (abfd,
4388 1.11 christos NOTE_GNU_PROPERTY_SECTION_NAME);
4389 1.11 christos if (s != NULL && s->size != 0)
4390 1.11 christos {
4391 1.11 christos /* We need a PT_GNU_PROPERTY segment. */
4392 1.11 christos ++segs;
4393 1.1 christos }
4394 1.1 christos
4395 1.1 christos for (s = abfd->sections; s != NULL; s = s->next)
4396 1.11 christos {
4397 1.1 christos if ((s->flags & SEC_LOAD) != 0
4398 1.11 christos && elf_section_type (s) == SHT_NOTE)
4399 1.1 christos {
4400 1.1 christos unsigned int alignment_power;
4401 1.11 christos /* We need a PT_NOTE segment. */
4402 1.11 christos ++segs;
4403 1.11 christos /* Try to create just one PT_NOTE segment for all adjacent
4404 1.11 christos loadable SHT_NOTE sections. gABI requires that within a
4405 1.11 christos PT_NOTE segment (and also inside of each SHT_NOTE section)
4406 1.11 christos each note should have the same alignment. So we check
4407 1.11 christos whether the sections are correctly aligned. */
4408 1.11 christos alignment_power = s->alignment_power;
4409 1.11 christos while (s->next != NULL
4410 1.11 christos && s->next->alignment_power == alignment_power
4411 1.11 christos && (s->next->flags & SEC_LOAD) != 0
4412 1.1 christos && elf_section_type (s->next) == SHT_NOTE)
4413 1.1 christos s = s->next;
4414 1.1 christos }
4415 1.1 christos }
4416 1.1 christos
4417 1.1 christos for (s = abfd->sections; s != NULL; s = s->next)
4418 1.1 christos {
4419 1.1 christos if (s->flags & SEC_THREAD_LOCAL)
4420 1.1 christos {
4421 1.1 christos /* We need a PT_TLS segment. */
4422 1.1 christos ++segs;
4423 1.1 christos break;
4424 1.1 christos }
4425 1.1 christos }
4426 1.9 christos
4427 1.12 christos bed = get_elf_backend_data (abfd);
4428 1.12 christos
4429 1.12 christos if ((abfd->flags & D_PAGED) != 0
4430 1.12 christos && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4431 1.14 christos {
4432 1.14 christos /* Add a PT_GNU_MBIND segment for each mbind section. */
4433 1.14 christos bfd_vma commonpagesize;
4434 1.14 christos unsigned int page_align_power;
4435 1.14 christos
4436 1.14 christos if (info != NULL)
4437 1.14 christos commonpagesize = info->commonpagesize;
4438 1.14 christos else
4439 1.12 christos commonpagesize = bed->commonpagesize;
4440 1.12 christos page_align_power = bfd_log2 (commonpagesize);
4441 1.12 christos for (s = abfd->sections; s != NULL; s = s->next)
4442 1.12 christos if (elf_section_flags (s) & SHF_GNU_MBIND)
4443 1.12 christos {
4444 1.12 christos if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4445 1.12 christos {
4446 1.12 christos _bfd_error_handler
4447 1.12 christos /* xgettext:c-format */
4448 1.12 christos (_("%pB: GNU_MBIND section `%pA' has invalid "
4449 1.12 christos "sh_info field: %d"),
4450 1.12 christos abfd, s, elf_section_data (s)->this_hdr.sh_info);
4451 1.12 christos continue;
4452 1.12 christos }
4453 1.12 christos /* Align mbind section to page size. */
4454 1.12 christos if (s->alignment_power < page_align_power)
4455 1.12 christos s->alignment_power = page_align_power;
4456 1.12 christos segs ++;
4457 1.9 christos }
4458 1.12 christos }
4459 1.12 christos
4460 1.1 christos /* Let the backend count up any program headers it might need. */
4461 1.1 christos if (bed->elf_backend_additional_program_headers)
4462 1.1 christos {
4463 1.1 christos int a;
4464 1.1 christos
4465 1.1 christos a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4466 1.1 christos if (a == -1)
4467 1.1 christos abort ();
4468 1.1 christos segs += a;
4469 1.1 christos }
4470 1.1 christos
4471 1.1 christos return segs * bed->s->sizeof_phdr;
4472 1.1 christos }
4473 1.1 christos
4474 1.1 christos /* Find the segment that contains the output_section of section. */
4475 1.1 christos
4476 1.1 christos Elf_Internal_Phdr *
4477 1.1 christos _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4478 1.1 christos {
4479 1.1 christos struct elf_segment_map *m;
4480 1.3 christos Elf_Internal_Phdr *p;
4481 1.1 christos
4482 1.1 christos for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
4483 1.1 christos m != NULL;
4484 1.1 christos m = m->next, p++)
4485 1.1 christos {
4486 1.1 christos int i;
4487 1.1 christos
4488 1.1 christos for (i = m->count - 1; i >= 0; i--)
4489 1.1 christos if (m->sections[i] == section)
4490 1.1 christos return p;
4491 1.1 christos }
4492 1.1 christos
4493 1.1 christos return NULL;
4494 1.1 christos }
4495 1.1 christos
4496 1.1 christos /* Create a mapping from a set of sections to a program segment. */
4497 1.1 christos
4498 1.1 christos static struct elf_segment_map *
4499 1.1 christos make_mapping (bfd *abfd,
4500 1.1 christos asection **sections,
4501 1.14 christos unsigned int from,
4502 1.1 christos unsigned int to,
4503 1.1 christos bool phdr)
4504 1.1 christos {
4505 1.1 christos struct elf_segment_map *m;
4506 1.12 christos unsigned int i;
4507 1.1 christos asection **hdrpp;
4508 1.11 christos size_t amt;
4509 1.11 christos
4510 1.1 christos amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4511 1.1 christos amt += (to - from) * sizeof (asection *);
4512 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4513 1.1 christos if (m == NULL)
4514 1.1 christos return NULL;
4515 1.1 christos m->next = NULL;
4516 1.1 christos m->p_type = PT_LOAD;
4517 1.1 christos for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4518 1.1 christos m->sections[i - from] = *hdrpp;
4519 1.1 christos m->count = to - from;
4520 1.1 christos
4521 1.1 christos if (from == 0 && phdr)
4522 1.1 christos {
4523 1.1 christos /* Include the headers in the first PT_LOAD segment. */
4524 1.1 christos m->includes_filehdr = 1;
4525 1.1 christos m->includes_phdrs = 1;
4526 1.1 christos }
4527 1.1 christos
4528 1.1 christos return m;
4529 1.1 christos }
4530 1.1 christos
4531 1.1 christos /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4532 1.1 christos on failure. */
4533 1.1 christos
4534 1.1 christos struct elf_segment_map *
4535 1.1 christos _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4536 1.1 christos {
4537 1.1 christos struct elf_segment_map *m;
4538 1.11 christos
4539 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd,
4540 1.1 christos sizeof (struct elf_segment_map));
4541 1.1 christos if (m == NULL)
4542 1.1 christos return NULL;
4543 1.1 christos m->next = NULL;
4544 1.1 christos m->p_type = PT_DYNAMIC;
4545 1.1 christos m->count = 1;
4546 1.1 christos m->sections[0] = dynsec;
4547 1.1 christos
4548 1.1 christos return m;
4549 1.1 christos }
4550 1.1 christos
4551 1.14 christos /* Possibly add or remove segments from the segment map. */
4552 1.1 christos
4553 1.1 christos static bool
4554 1.14 christos elf_modify_segment_map (bfd *abfd,
4555 1.1 christos struct bfd_link_info *info,
4556 1.1 christos bool remove_empty_load)
4557 1.1 christos {
4558 1.1 christos struct elf_segment_map **m;
4559 1.1 christos const struct elf_backend_data *bed;
4560 1.1 christos
4561 1.1 christos /* The placement algorithm assumes that non allocated sections are
4562 1.1 christos not in PT_LOAD segments. We ensure this here by removing such
4563 1.1 christos sections from the segment map. We also remove excluded
4564 1.3 christos sections. Finally, any PT_LOAD segment without sections is
4565 1.1 christos removed. */
4566 1.1 christos m = &elf_seg_map (abfd);
4567 1.1 christos while (*m)
4568 1.1 christos {
4569 1.1 christos unsigned int i, new_count;
4570 1.1 christos
4571 1.1 christos for (new_count = 0, i = 0; i < (*m)->count; i++)
4572 1.1 christos {
4573 1.1 christos if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4574 1.1 christos && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4575 1.1 christos || (*m)->p_type != PT_LOAD))
4576 1.1 christos {
4577 1.1 christos (*m)->sections[new_count] = (*m)->sections[i];
4578 1.1 christos new_count++;
4579 1.1 christos }
4580 1.1 christos }
4581 1.9 christos (*m)->count = new_count;
4582 1.9 christos
4583 1.9 christos if (remove_empty_load
4584 1.9 christos && (*m)->p_type == PT_LOAD
4585 1.1 christos && (*m)->count == 0
4586 1.1 christos && !(*m)->includes_phdrs)
4587 1.1 christos *m = (*m)->next;
4588 1.1 christos else
4589 1.1 christos m = &(*m)->next;
4590 1.1 christos }
4591 1.1 christos
4592 1.1 christos bed = get_elf_backend_data (abfd);
4593 1.1 christos if (bed->elf_backend_modify_segment_map != NULL)
4594 1.14 christos {
4595 1.1 christos if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
4596 1.1 christos return false;
4597 1.14 christos }
4598 1.1 christos
4599 1.1 christos return true;
4600 1.11 christos }
4601 1.11 christos
4602 1.11 christos #define IS_TBSS(s) \
4603 1.14 christos ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4604 1.14 christos
4605 1.1 christos /* Set up a mapping from BFD sections to program segments. Update
4606 1.14 christos NEED_LAYOUT if the section layout is changed. */
4607 1.14 christos
4608 1.14 christos bool
4609 1.14 christos _bfd_elf_map_sections_to_segments (bfd *abfd,
4610 1.1 christos struct bfd_link_info *info,
4611 1.1 christos bool *need_layout)
4612 1.1 christos {
4613 1.1 christos unsigned int count;
4614 1.1 christos struct elf_segment_map *m;
4615 1.14 christos asection **sections = NULL;
4616 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4617 1.3 christos bool no_user_phdrs;
4618 1.3 christos
4619 1.3 christos no_user_phdrs = elf_seg_map (abfd) == NULL;
4620 1.14 christos
4621 1.14 christos if (info != NULL)
4622 1.14 christos {
4623 1.14 christos info->user_phdrs = !no_user_phdrs;
4624 1.14 christos
4625 1.14 christos /* Size the relative relocations if DT_RELR is enabled. */
4626 1.14 christos if (info->enable_dt_relr
4627 1.14 christos && need_layout != NULL
4628 1.14 christos && bed->size_relative_relocs
4629 1.14 christos && !bed->size_relative_relocs (info, need_layout))
4630 1.14 christos info->callbacks->einfo
4631 1.3 christos (_("%F%P: failed to size relative relocations\n"));
4632 1.1 christos }
4633 1.1 christos
4634 1.1 christos if (no_user_phdrs && bfd_count_sections (abfd) != 0)
4635 1.1 christos {
4636 1.1 christos asection *s;
4637 1.1 christos unsigned int i;
4638 1.1 christos struct elf_segment_map *mfirst;
4639 1.1 christos struct elf_segment_map **pm;
4640 1.11 christos asection *last_hdr;
4641 1.1 christos bfd_vma last_size;
4642 1.1 christos unsigned int hdr_index;
4643 1.14 christos bfd_vma maxpagesize;
4644 1.14 christos asection **hdrpp;
4645 1.14 christos bool phdr_in_segment;
4646 1.12 christos bool writable;
4647 1.1 christos bool executable;
4648 1.9 christos unsigned int tls_count = 0;
4649 1.1 christos asection *first_tls = NULL;
4650 1.14 christos asection *first_mbind = NULL;
4651 1.12 christos asection *dynsec, *eh_frame_hdr;
4652 1.12 christos asection *sframe;
4653 1.12 christos size_t amt;
4654 1.12 christos bfd_vma addr_mask, wrap_to = 0; /* Bytes. */
4655 1.1 christos bfd_size_type phdr_size; /* Octets/bytes. */
4656 1.1 christos unsigned int opb = bfd_octets_per_byte (abfd, NULL);
4657 1.1 christos
4658 1.12 christos /* Select the allocated sections, and sort them. */
4659 1.12 christos
4660 1.1 christos amt = bfd_count_sections (abfd) * sizeof (asection *);
4661 1.1 christos sections = (asection **) bfd_malloc (amt);
4662 1.1 christos if (sections == NULL)
4663 1.1 christos goto error_return;
4664 1.1 christos
4665 1.1 christos /* Calculate top address, avoiding undefined behaviour of shift
4666 1.1 christos left operator when shift count is equal to size of type
4667 1.1 christos being shifted. */
4668 1.1 christos addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4669 1.1 christos addr_mask = (addr_mask << 1) + 1;
4670 1.1 christos
4671 1.1 christos i = 0;
4672 1.1 christos for (s = abfd->sections; s != NULL; s = s->next)
4673 1.1 christos {
4674 1.12 christos if ((s->flags & SEC_ALLOC) != 0)
4675 1.12 christos {
4676 1.12 christos /* target_index is unused until bfd_elf_final_link
4677 1.12 christos starts output of section symbols. Use it to make
4678 1.1 christos qsort stable. */
4679 1.1 christos s->target_index = i;
4680 1.1 christos sections[i] = s;
4681 1.12 christos ++i;
4682 1.12 christos /* A wrapping section potentially clashes with header. */
4683 1.1 christos if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
4684 1.1 christos wrap_to = (s->lma + s->size / opb) & addr_mask;
4685 1.1 christos }
4686 1.1 christos }
4687 1.1 christos BFD_ASSERT (i <= bfd_count_sections (abfd));
4688 1.1 christos count = i;
4689 1.1 christos
4690 1.11 christos qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
4691 1.11 christos
4692 1.11 christos phdr_size = elf_program_header_size (abfd);
4693 1.11 christos if (phdr_size == (bfd_size_type) -1)
4694 1.12 christos phdr_size = get_program_header_size (abfd, info);
4695 1.12 christos phdr_size += bed->s->sizeof_ehdr;
4696 1.14 christos /* phdr_size is compared to LMA values which are in bytes. */
4697 1.14 christos phdr_size /= opb;
4698 1.14 christos if (info != NULL)
4699 1.14 christos maxpagesize = info->maxpagesize;
4700 1.11 christos else
4701 1.11 christos maxpagesize = bed->maxpagesize;
4702 1.11 christos if (maxpagesize == 0)
4703 1.11 christos maxpagesize = 1;
4704 1.11 christos phdr_in_segment = info != NULL && info->load_phdrs;
4705 1.11 christos if (count != 0
4706 1.11 christos && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
4707 1.11 christos >= (phdr_size & (maxpagesize - 1))))
4708 1.11 christos /* For compatibility with old scripts that may not be using
4709 1.14 christos SIZEOF_HEADERS, add headers when it looks like space has
4710 1.11 christos been left for them. */
4711 1.1 christos phdr_in_segment = true;
4712 1.1 christos
4713 1.1 christos /* Build the mapping. */
4714 1.1 christos mfirst = NULL;
4715 1.1 christos pm = &mfirst;
4716 1.1 christos
4717 1.1 christos /* If we have a .interp section, then create a PT_PHDR segment for
4718 1.1 christos the program headers and a PT_INTERP segment for the .interp
4719 1.11 christos section. */
4720 1.1 christos s = bfd_get_section_by_name (abfd, ".interp");
4721 1.1 christos if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4722 1.1 christos {
4723 1.1 christos amt = sizeof (struct elf_segment_map);
4724 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4725 1.1 christos if (m == NULL)
4726 1.1 christos goto error_return;
4727 1.11 christos m->next = NULL;
4728 1.1 christos m->p_type = PT_PHDR;
4729 1.1 christos m->p_flags = PF_R;
4730 1.14 christos m->p_flags_valid = 1;
4731 1.1 christos m->includes_phdrs = 1;
4732 1.1 christos phdr_in_segment = true;
4733 1.1 christos *pm = m;
4734 1.1 christos pm = &m->next;
4735 1.1 christos
4736 1.1 christos amt = sizeof (struct elf_segment_map);
4737 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4738 1.1 christos if (m == NULL)
4739 1.1 christos goto error_return;
4740 1.1 christos m->next = NULL;
4741 1.1 christos m->p_type = PT_INTERP;
4742 1.1 christos m->count = 1;
4743 1.1 christos m->sections[0] = s;
4744 1.1 christos
4745 1.1 christos *pm = m;
4746 1.1 christos pm = &m->next;
4747 1.1 christos }
4748 1.1 christos
4749 1.1 christos /* Look through the sections. We put sections in the same program
4750 1.1 christos segment when the start of the second section can be placed within
4751 1.1 christos a few bytes of the end of the first section. */
4752 1.11 christos last_hdr = NULL;
4753 1.14 christos last_size = 0;
4754 1.14 christos hdr_index = 0;
4755 1.1 christos writable = false;
4756 1.1 christos executable = false;
4757 1.1 christos dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4758 1.1 christos if (dynsec != NULL
4759 1.1 christos && (dynsec->flags & SEC_LOAD) == 0)
4760 1.11 christos dynsec = NULL;
4761 1.14 christos
4762 1.11 christos if ((abfd->flags & D_PAGED) == 0)
4763 1.1 christos phdr_in_segment = false;
4764 1.1 christos
4765 1.1 christos /* Deal with -Ttext or something similar such that the first section
4766 1.1 christos is not adjacent to the program headers. This is an
4767 1.11 christos approximation, since at this point we don't know exactly how many
4768 1.1 christos program headers we will need. */
4769 1.12 christos if (phdr_in_segment && count > 0)
4770 1.14 christos {
4771 1.1 christos bfd_vma phdr_lma; /* Bytes. */
4772 1.11 christos bool separate_phdr = false;
4773 1.11 christos
4774 1.11 christos phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
4775 1.11 christos if (info != NULL
4776 1.11 christos && info->separate_code
4777 1.11 christos && (sections[0]->flags & SEC_CODE) != 0)
4778 1.11 christos {
4779 1.11 christos /* If data sections should be separate from code and
4780 1.11 christos thus not executable, and the first section is
4781 1.14 christos executable then put the file and program headers in
4782 1.11 christos their own PT_LOAD. */
4783 1.11 christos separate_phdr = true;
4784 1.11 christos if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
4785 1.11 christos == (sections[0]->lma & addr_mask & -maxpagesize)))
4786 1.11 christos {
4787 1.11 christos /* The file and program headers are currently on the
4788 1.11 christos same page as the first section. Put them on the
4789 1.11 christos previous page if we can. */
4790 1.11 christos if (phdr_lma >= maxpagesize)
4791 1.14 christos phdr_lma -= maxpagesize;
4792 1.11 christos else
4793 1.11 christos separate_phdr = false;
4794 1.11 christos }
4795 1.11 christos }
4796 1.11 christos if ((sections[0]->lma & addr_mask) < phdr_lma
4797 1.11 christos || (sections[0]->lma & addr_mask) < phdr_size)
4798 1.14 christos /* If file and program headers would be placed at the end
4799 1.11 christos of memory then it's probably better to omit them. */
4800 1.11 christos phdr_in_segment = false;
4801 1.11 christos else if (phdr_lma < wrap_to)
4802 1.11 christos /* If a section wraps around to where we'll be placing
4803 1.14 christos file and program headers, then the headers will be
4804 1.11 christos overwritten. */
4805 1.11 christos phdr_in_segment = false;
4806 1.11 christos else if (separate_phdr)
4807 1.11 christos {
4808 1.11 christos m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
4809 1.12 christos if (m == NULL)
4810 1.11 christos goto error_return;
4811 1.11 christos m->p_paddr = phdr_lma * opb;
4812 1.11 christos m->p_vaddr_offset
4813 1.11 christos = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
4814 1.11 christos m->p_paddr_valid = 1;
4815 1.14 christos *pm = m;
4816 1.9 christos pm = &m->next;
4817 1.1 christos phdr_in_segment = false;
4818 1.1 christos }
4819 1.1 christos }
4820 1.1 christos
4821 1.1 christos for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
4822 1.14 christos {
4823 1.1 christos asection *hdr;
4824 1.1 christos bool new_segment;
4825 1.1 christos
4826 1.1 christos hdr = *hdrpp;
4827 1.1 christos
4828 1.1 christos /* See if this section and the last one will fit in the same
4829 1.1 christos segment. */
4830 1.1 christos
4831 1.1 christos if (last_hdr == NULL)
4832 1.1 christos {
4833 1.14 christos /* If we don't have a segment yet, then we don't need a new
4834 1.1 christos one (we build the last one after this loop). */
4835 1.1 christos new_segment = false;
4836 1.1 christos }
4837 1.1 christos else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4838 1.1 christos {
4839 1.1 christos /* If this section has a different relation between the
4840 1.14 christos virtual address and the load address, then we need a new
4841 1.1 christos segment. */
4842 1.1 christos new_segment = true;
4843 1.1 christos }
4844 1.1 christos else if (hdr->lma < last_hdr->lma + last_size
4845 1.1 christos || last_hdr->lma + last_size < last_hdr->lma)
4846 1.1 christos {
4847 1.14 christos /* If this section has a load address that makes it overlap
4848 1.1 christos the previous section, then we need a new segment. */
4849 1.11 christos new_segment = true;
4850 1.11 christos }
4851 1.11 christos else if ((abfd->flags & D_PAGED) != 0
4852 1.11 christos && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4853 1.11 christos == (hdr->lma & -maxpagesize)))
4854 1.11 christos {
4855 1.14 christos /* If we are demand paged then we can't map two disk
4856 1.11 christos pages onto the same memory page. */
4857 1.1 christos new_segment = false;
4858 1.1 christos }
4859 1.1 christos /* In the next test we have to be careful when last_hdr->lma is close
4860 1.1 christos to the end of the address space. If the aligned address wraps
4861 1.1 christos around to the start of the address space, then there are no more
4862 1.11 christos pages left in memory and it is OK to assume that the current
4863 1.11 christos section can be included in the current segment. */
4864 1.11 christos else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4865 1.11 christos + maxpagesize > last_hdr->lma)
4866 1.1 christos && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4867 1.1 christos + maxpagesize <= hdr->lma))
4868 1.1 christos {
4869 1.14 christos /* If putting this section in this segment would force us to
4870 1.1 christos skip a page in the segment, then we need a new segment. */
4871 1.1 christos new_segment = true;
4872 1.11 christos }
4873 1.8 christos else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
4874 1.8 christos && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
4875 1.8 christos {
4876 1.8 christos /* We don't want to put a loaded section after a
4877 1.11 christos nonloaded (ie. bss style) section in the same segment
4878 1.14 christos as that will force the non-loaded section to be loaded.
4879 1.1 christos Consider .tbss sections as loaded for this purpose. */
4880 1.1 christos new_segment = true;
4881 1.1 christos }
4882 1.1 christos else if ((abfd->flags & D_PAGED) == 0)
4883 1.1 christos {
4884 1.1 christos /* If the file is not demand paged, which means that we
4885 1.14 christos don't require the sections to be correctly aligned in the
4886 1.1 christos file, then there is no other reason for a new segment. */
4887 1.11 christos new_segment = false;
4888 1.11 christos }
4889 1.11 christos else if (info != NULL
4890 1.11 christos && info->separate_code
4891 1.14 christos && executable != ((hdr->flags & SEC_CODE) != 0))
4892 1.11 christos {
4893 1.1 christos new_segment = true;
4894 1.11 christos }
4895 1.1 christos else if (! writable
4896 1.1 christos && (hdr->flags & SEC_READONLY) == 0)
4897 1.11 christos {
4898 1.14 christos /* We don't want to put a writable section in a read only
4899 1.1 christos segment. */
4900 1.1 christos new_segment = true;
4901 1.1 christos }
4902 1.1 christos else
4903 1.14 christos {
4904 1.1 christos /* Otherwise, we can use the same segment. */
4905 1.1 christos new_segment = false;
4906 1.1 christos }
4907 1.1 christos
4908 1.1 christos /* Allow interested parties a chance to override our decision. */
4909 1.1 christos if (last_hdr != NULL
4910 1.1 christos && info != NULL
4911 1.1 christos && info->callbacks->override_segment_assignment != NULL)
4912 1.1 christos new_segment
4913 1.1 christos = info->callbacks->override_segment_assignment (info, abfd, hdr,
4914 1.1 christos last_hdr,
4915 1.1 christos new_segment);
4916 1.1 christos
4917 1.1 christos if (! new_segment)
4918 1.14 christos {
4919 1.11 christos if ((hdr->flags & SEC_READONLY) == 0)
4920 1.14 christos writable = true;
4921 1.1 christos if ((hdr->flags & SEC_CODE) != 0)
4922 1.1 christos executable = true;
4923 1.12 christos last_hdr = hdr;
4924 1.1 christos /* .tbss sections effectively have zero size. */
4925 1.1 christos last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
4926 1.1 christos continue;
4927 1.1 christos }
4928 1.11 christos
4929 1.1 christos /* We need a new program segment. We must create a new program
4930 1.11 christos header holding all the sections from hdr_index until hdr. */
4931 1.1 christos
4932 1.1 christos m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
4933 1.1 christos if (m == NULL)
4934 1.1 christos goto error_return;
4935 1.1 christos
4936 1.1 christos *pm = m;
4937 1.1 christos pm = &m->next;
4938 1.14 christos
4939 1.1 christos if ((hdr->flags & SEC_READONLY) == 0)
4940 1.14 christos writable = true;
4941 1.1 christos else
4942 1.11 christos writable = false;
4943 1.14 christos
4944 1.11 christos if ((hdr->flags & SEC_CODE) == 0)
4945 1.14 christos executable = false;
4946 1.11 christos else
4947 1.1 christos executable = true;
4948 1.1 christos
4949 1.12 christos last_hdr = hdr;
4950 1.11 christos /* .tbss sections effectively have zero size. */
4951 1.14 christos last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
4952 1.1 christos hdr_index = i;
4953 1.1 christos phdr_in_segment = false;
4954 1.3 christos }
4955 1.3 christos
4956 1.3 christos /* Create a final PT_LOAD program segment, but not if it's just
4957 1.11 christos for .tbss. */
4958 1.11 christos if (last_hdr != NULL
4959 1.1 christos && (i - hdr_index != 1
4960 1.11 christos || !IS_TBSS (last_hdr)))
4961 1.1 christos {
4962 1.1 christos m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
4963 1.1 christos if (m == NULL)
4964 1.1 christos goto error_return;
4965 1.1 christos
4966 1.1 christos *pm = m;
4967 1.1 christos pm = &m->next;
4968 1.1 christos }
4969 1.1 christos
4970 1.1 christos /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4971 1.1 christos if (dynsec != NULL)
4972 1.1 christos {
4973 1.1 christos m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4974 1.1 christos if (m == NULL)
4975 1.1 christos goto error_return;
4976 1.1 christos *pm = m;
4977 1.1 christos pm = &m->next;
4978 1.11 christos }
4979 1.1 christos
4980 1.1 christos /* For each batch of consecutive loadable SHT_NOTE sections,
4981 1.1 christos add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4982 1.11 christos because if we link together nonloadable .note sections and
4983 1.1 christos loadable .note sections, we will generate two .note sections
4984 1.1 christos in the output file. */
4985 1.1 christos for (s = abfd->sections; s != NULL; s = s->next)
4986 1.11 christos {
4987 1.1 christos if ((s->flags & SEC_LOAD) != 0
4988 1.1 christos && elf_section_type (s) == SHT_NOTE)
4989 1.11 christos {
4990 1.1 christos asection *s2;
4991 1.1 christos unsigned int alignment_power = s->alignment_power;
4992 1.11 christos
4993 1.11 christos count = 1;
4994 1.11 christos for (s2 = s; s2->next != NULL; s2 = s2->next)
4995 1.11 christos {
4996 1.11 christos if (s2->next->alignment_power == alignment_power
4997 1.12 christos && (s2->next->flags & SEC_LOAD) != 0
4998 1.11 christos && elf_section_type (s2->next) == SHT_NOTE
4999 1.11 christos && align_power (s2->lma + s2->size / opb,
5000 1.11 christos alignment_power)
5001 1.11 christos == s2->next->lma)
5002 1.11 christos count++;
5003 1.11 christos else
5004 1.11 christos break;
5005 1.11 christos }
5006 1.1 christos amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5007 1.1 christos amt += count * sizeof (asection *);
5008 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5009 1.1 christos if (m == NULL)
5010 1.1 christos goto error_return;
5011 1.1 christos m->next = NULL;
5012 1.1 christos m->p_type = PT_NOTE;
5013 1.1 christos m->count = count;
5014 1.1 christos while (count > 1)
5015 1.1 christos {
5016 1.1 christos m->sections[m->count - count--] = s;
5017 1.1 christos BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5018 1.1 christos s = s->next;
5019 1.1 christos }
5020 1.1 christos m->sections[m->count - 1] = s;
5021 1.1 christos BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5022 1.1 christos *pm = m;
5023 1.1 christos pm = &m->next;
5024 1.1 christos }
5025 1.1 christos if (s->flags & SEC_THREAD_LOCAL)
5026 1.1 christos {
5027 1.1 christos if (! tls_count)
5028 1.1 christos first_tls = s;
5029 1.9 christos tls_count++;
5030 1.9 christos }
5031 1.9 christos if (first_mbind == NULL
5032 1.1 christos && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5033 1.1 christos first_mbind = s;
5034 1.1 christos }
5035 1.1 christos
5036 1.1 christos /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5037 1.11 christos if (tls_count > 0)
5038 1.11 christos {
5039 1.1 christos amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5040 1.1 christos amt += tls_count * sizeof (asection *);
5041 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5042 1.1 christos if (m == NULL)
5043 1.1 christos goto error_return;
5044 1.1 christos m->next = NULL;
5045 1.1 christos m->p_type = PT_TLS;
5046 1.1 christos m->count = tls_count;
5047 1.1 christos /* Mandated PF_R. */
5048 1.5 christos m->p_flags = PF_R;
5049 1.12 christos m->p_flags_valid = 1;
5050 1.1 christos s = first_tls;
5051 1.5 christos for (i = 0; i < tls_count; ++i)
5052 1.5 christos {
5053 1.5 christos if ((s->flags & SEC_THREAD_LOCAL) == 0)
5054 1.11 christos {
5055 1.5 christos _bfd_error_handler
5056 1.5 christos (_("%pB: TLS sections are not adjacent:"), abfd);
5057 1.12 christos s = first_tls;
5058 1.5 christos i = 0;
5059 1.5 christos while (i < tls_count)
5060 1.5 christos {
5061 1.11 christos if ((s->flags & SEC_THREAD_LOCAL) != 0)
5062 1.5 christos {
5063 1.5 christos _bfd_error_handler (_(" TLS: %pA"), s);
5064 1.5 christos i++;
5065 1.11 christos }
5066 1.5 christos else
5067 1.5 christos _bfd_error_handler (_(" non-TLS: %pA"), s);
5068 1.5 christos s = s->next;
5069 1.5 christos }
5070 1.5 christos bfd_set_error (bfd_error_bad_value);
5071 1.5 christos goto error_return;
5072 1.5 christos }
5073 1.1 christos m->sections[i] = s;
5074 1.1 christos s = s->next;
5075 1.1 christos }
5076 1.1 christos
5077 1.1 christos *pm = m;
5078 1.1 christos pm = &m->next;
5079 1.12 christos }
5080 1.12 christos
5081 1.12 christos if (first_mbind
5082 1.9 christos && (abfd->flags & D_PAGED) != 0
5083 1.9 christos && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5084 1.12 christos for (s = first_mbind; s != NULL; s = s->next)
5085 1.9 christos if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5086 1.9 christos && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5087 1.9 christos {
5088 1.9 christos /* Mandated PF_R. */
5089 1.9 christos unsigned long p_flags = PF_R;
5090 1.9 christos if ((s->flags & SEC_READONLY) == 0)
5091 1.9 christos p_flags |= PF_W;
5092 1.9 christos if ((s->flags & SEC_CODE) != 0)
5093 1.9 christos p_flags |= PF_X;
5094 1.9 christos
5095 1.9 christos amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5096 1.9 christos m = bfd_zalloc (abfd, amt);
5097 1.9 christos if (m == NULL)
5098 1.9 christos goto error_return;
5099 1.9 christos m->next = NULL;
5100 1.9 christos m->p_type = (PT_GNU_MBIND_LO
5101 1.9 christos + elf_section_data (s)->this_hdr.sh_info);
5102 1.9 christos m->count = 1;
5103 1.9 christos m->p_flags_valid = 1;
5104 1.9 christos m->sections[0] = s;
5105 1.9 christos m->p_flags = p_flags;
5106 1.9 christos
5107 1.9 christos *pm = m;
5108 1.9 christos pm = &m->next;
5109 1.11 christos }
5110 1.11 christos
5111 1.11 christos s = bfd_get_section_by_name (abfd,
5112 1.11 christos NOTE_GNU_PROPERTY_SECTION_NAME);
5113 1.11 christos if (s != NULL && s->size != 0)
5114 1.11 christos {
5115 1.11 christos amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5116 1.11 christos m = bfd_zalloc (abfd, amt);
5117 1.11 christos if (m == NULL)
5118 1.11 christos goto error_return;
5119 1.11 christos m->next = NULL;
5120 1.11 christos m->p_type = PT_GNU_PROPERTY;
5121 1.11 christos m->count = 1;
5122 1.11 christos m->p_flags_valid = 1;
5123 1.11 christos m->sections[0] = s;
5124 1.11 christos m->p_flags = PF_R;
5125 1.11 christos *pm = m;
5126 1.11 christos pm = &m->next;
5127 1.1 christos }
5128 1.1 christos
5129 1.14 christos /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5130 1.1 christos segment. */
5131 1.1 christos eh_frame_hdr = elf_eh_frame_hdr (info);
5132 1.1 christos if (eh_frame_hdr != NULL
5133 1.1 christos && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5134 1.1 christos {
5135 1.1 christos amt = sizeof (struct elf_segment_map);
5136 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5137 1.1 christos if (m == NULL)
5138 1.1 christos goto error_return;
5139 1.1 christos m->next = NULL;
5140 1.1 christos m->p_type = PT_GNU_EH_FRAME;
5141 1.1 christos m->count = 1;
5142 1.1 christos m->sections[0] = eh_frame_hdr->output_section;
5143 1.1 christos
5144 1.1 christos *pm = m;
5145 1.1 christos pm = &m->next;
5146 1.14 christos }
5147 1.14 christos
5148 1.14 christos /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5149 1.14 christos segment. */
5150 1.14 christos sframe = elf_sframe (abfd);
5151 1.14 christos if (sframe != NULL
5152 1.14 christos && (sframe->output_section->flags & SEC_LOAD) != 0
5153 1.14 christos && sframe->size != 0)
5154 1.14 christos {
5155 1.14 christos amt = sizeof (struct elf_segment_map);
5156 1.14 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5157 1.14 christos if (m == NULL)
5158 1.14 christos goto error_return;
5159 1.14 christos m->next = NULL;
5160 1.14 christos m->p_type = PT_GNU_SFRAME;
5161 1.14 christos m->count = 1;
5162 1.14 christos m->sections[0] = sframe->output_section;
5163 1.14 christos
5164 1.14 christos *pm = m;
5165 1.14 christos pm = &m->next;
5166 1.3 christos }
5167 1.1 christos
5168 1.1 christos if (elf_stack_flags (abfd))
5169 1.1 christos {
5170 1.1 christos amt = sizeof (struct elf_segment_map);
5171 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5172 1.1 christos if (m == NULL)
5173 1.1 christos goto error_return;
5174 1.3 christos m->next = NULL;
5175 1.3 christos m->p_type = PT_GNU_STACK;
5176 1.1 christos m->p_flags = elf_stack_flags (abfd);
5177 1.3 christos m->p_align = bed->stack_align;
5178 1.3 christos m->p_flags_valid = 1;
5179 1.3 christos m->p_align_valid = m->p_align != 0;
5180 1.3 christos if (info->stacksize > 0)
5181 1.3 christos {
5182 1.3 christos m->p_size = info->stacksize;
5183 1.1 christos m->p_size_valid = 1;
5184 1.1 christos }
5185 1.1 christos
5186 1.1 christos *pm = m;
5187 1.1 christos pm = &m->next;
5188 1.1 christos }
5189 1.1 christos
5190 1.1 christos if (info != NULL && info->relro)
5191 1.1 christos {
5192 1.3 christos for (m = mfirst; m != NULL; m = m->next)
5193 1.3 christos {
5194 1.3 christos if (m->p_type == PT_LOAD
5195 1.3 christos && m->count != 0
5196 1.1 christos && m->sections[0]->vma >= info->relro_start
5197 1.3 christos && m->sections[0]->vma < info->relro_end)
5198 1.3 christos {
5199 1.12 christos i = m->count;
5200 1.12 christos while (--i != (unsigned) -1)
5201 1.14 christos {
5202 1.14 christos if (m->sections[i]->size > 0
5203 1.12 christos && (m->sections[i]->flags & SEC_LOAD) != 0
5204 1.12 christos && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
5205 1.3 christos break;
5206 1.5 christos }
5207 1.1 christos
5208 1.1 christos if (i != (unsigned) -1)
5209 1.3 christos break;
5210 1.1 christos }
5211 1.1 christos }
5212 1.1 christos
5213 1.1 christos /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5214 1.1 christos if (m != NULL)
5215 1.1 christos {
5216 1.1 christos amt = sizeof (struct elf_segment_map);
5217 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5218 1.1 christos if (m == NULL)
5219 1.1 christos goto error_return;
5220 1.1 christos m->next = NULL;
5221 1.1 christos m->p_type = PT_GNU_RELRO;
5222 1.1 christos *pm = m;
5223 1.1 christos pm = &m->next;
5224 1.1 christos }
5225 1.1 christos }
5226 1.3 christos
5227 1.1 christos free (sections);
5228 1.1 christos elf_seg_map (abfd) = mfirst;
5229 1.1 christos }
5230 1.14 christos
5231 1.1 christos if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
5232 1.3 christos return false;
5233 1.1 christos
5234 1.3 christos for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5235 1.1 christos ++count;
5236 1.14 christos elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5237 1.1 christos
5238 1.1 christos return true;
5239 1.12 christos
5240 1.14 christos error_return:
5241 1.1 christos free (sections);
5242 1.1 christos return false;
5243 1.1 christos }
5244 1.1 christos
5245 1.1 christos /* Sort sections by address. */
5246 1.1 christos
5247 1.1 christos static int
5248 1.1 christos elf_sort_sections (const void *arg1, const void *arg2)
5249 1.1 christos {
5250 1.1 christos const asection *sec1 = *(const asection **) arg1;
5251 1.1 christos const asection *sec2 = *(const asection **) arg2;
5252 1.1 christos bfd_size_type size1, size2;
5253 1.1 christos
5254 1.1 christos /* Sort by LMA first, since this is the address used to
5255 1.1 christos place the section into a segment. */
5256 1.1 christos if (sec1->lma < sec2->lma)
5257 1.1 christos return -1;
5258 1.1 christos else if (sec1->lma > sec2->lma)
5259 1.1 christos return 1;
5260 1.1 christos
5261 1.1 christos /* Then sort by VMA. Normally the LMA and the VMA will be
5262 1.1 christos the same, and this will do nothing. */
5263 1.1 christos if (sec1->vma < sec2->vma)
5264 1.1 christos return -1;
5265 1.1 christos else if (sec1->vma > sec2->vma)
5266 1.1 christos return 1;
5267 1.1 christos
5268 1.14 christos /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5269 1.14 christos
5270 1.1 christos #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5271 1.1 christos && (x)->size != 0)
5272 1.1 christos
5273 1.12 christos if (TOEND (sec1))
5274 1.1 christos {
5275 1.1 christos if (!TOEND (sec2))
5276 1.1 christos return 1;
5277 1.1 christos }
5278 1.1 christos else if (TOEND (sec2))
5279 1.1 christos return -1;
5280 1.1 christos
5281 1.1 christos #undef TOEND
5282 1.1 christos
5283 1.1 christos /* Sort by size, to put zero sized sections
5284 1.1 christos before others at the same address. */
5285 1.1 christos
5286 1.1 christos size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5287 1.1 christos size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5288 1.1 christos
5289 1.1 christos if (size1 < size2)
5290 1.1 christos return -1;
5291 1.1 christos if (size1 > size2)
5292 1.1 christos return 1;
5293 1.1 christos
5294 1.1 christos return sec1->target_index - sec2->target_index;
5295 1.12 christos }
5296 1.12 christos
5297 1.12 christos /* This qsort comparison functions sorts PT_LOAD segments first and
5298 1.12 christos by p_paddr, for assign_file_positions_for_load_sections. */
5299 1.12 christos
5300 1.12 christos static int
5301 1.12 christos elf_sort_segments (const void *arg1, const void *arg2)
5302 1.12 christos {
5303 1.12 christos const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5304 1.12 christos const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5305 1.12 christos
5306 1.12 christos if (m1->p_type != m2->p_type)
5307 1.12 christos {
5308 1.12 christos if (m1->p_type == PT_NULL)
5309 1.12 christos return 1;
5310 1.12 christos if (m2->p_type == PT_NULL)
5311 1.12 christos return -1;
5312 1.12 christos return m1->p_type < m2->p_type ? -1 : 1;
5313 1.12 christos }
5314 1.12 christos if (m1->includes_filehdr != m2->includes_filehdr)
5315 1.12 christos return m1->includes_filehdr ? -1 : 1;
5316 1.12 christos if (m1->no_sort_lma != m2->no_sort_lma)
5317 1.12 christos return m1->no_sort_lma ? -1 : 1;
5318 1.12 christos if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5319 1.12 christos {
5320 1.12 christos bfd_vma lma1, lma2; /* Octets. */
5321 1.12 christos lma1 = 0;
5322 1.12 christos if (m1->p_paddr_valid)
5323 1.12 christos lma1 = m1->p_paddr;
5324 1.12 christos else if (m1->count != 0)
5325 1.12 christos {
5326 1.12 christos unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5327 1.12 christos m1->sections[0]);
5328 1.12 christos lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5329 1.12 christos }
5330 1.12 christos lma2 = 0;
5331 1.12 christos if (m2->p_paddr_valid)
5332 1.12 christos lma2 = m2->p_paddr;
5333 1.12 christos else if (m2->count != 0)
5334 1.12 christos {
5335 1.12 christos unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5336 1.12 christos m2->sections[0]);
5337 1.12 christos lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5338 1.12 christos }
5339 1.12 christos if (lma1 != lma2)
5340 1.12 christos return lma1 < lma2 ? -1 : 1;
5341 1.12 christos }
5342 1.12 christos if (m1->idx != m2->idx)
5343 1.12 christos return m1->idx < m2->idx ? -1 : 1;
5344 1.12 christos return 0;
5345 1.1 christos }
5346 1.1 christos
5347 1.1 christos /* Ian Lance Taylor writes:
5348 1.1 christos
5349 1.1 christos We shouldn't be using % with a negative signed number. That's just
5350 1.1 christos not good. We have to make sure either that the number is not
5351 1.1 christos negative, or that the number has an unsigned type. When the types
5352 1.1 christos are all the same size they wind up as unsigned. When file_ptr is a
5353 1.1 christos larger signed type, the arithmetic winds up as signed long long,
5354 1.1 christos which is wrong.
5355 1.1 christos
5356 1.1 christos What we're trying to say here is something like ``increase OFF by
5357 1.1 christos the least amount that will cause it to be equal to the VMA modulo
5358 1.1 christos the page size.'' */
5359 1.1 christos /* In other words, something like:
5360 1.1 christos
5361 1.1 christos vma_offset = m->sections[0]->vma % bed->maxpagesize;
5362 1.1 christos off_offset = off % bed->maxpagesize;
5363 1.1 christos if (vma_offset < off_offset)
5364 1.1 christos adjustment = vma_offset + bed->maxpagesize - off_offset;
5365 1.1 christos else
5366 1.11 christos adjustment = vma_offset - off_offset;
5367 1.1 christos
5368 1.1 christos which can be collapsed into the expression below. */
5369 1.1 christos
5370 1.1 christos static file_ptr
5371 1.4 christos vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5372 1.4 christos {
5373 1.4 christos /* PR binutils/16199: Handle an alignment of zero. */
5374 1.1 christos if (maxpagesize == 0)
5375 1.1 christos maxpagesize = 1;
5376 1.1 christos return ((vma - off) % maxpagesize);
5377 1.1 christos }
5378 1.1 christos
5379 1.1 christos static void
5380 1.1 christos print_segment_map (const struct elf_segment_map *m)
5381 1.1 christos {
5382 1.1 christos unsigned int j;
5383 1.1 christos const char *pt = get_segment_type (m->p_type);
5384 1.1 christos char buf[32];
5385 1.1 christos
5386 1.1 christos if (pt == NULL)
5387 1.1 christos {
5388 1.1 christos if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5389 1.1 christos sprintf (buf, "LOPROC+%7.7x",
5390 1.1 christos (unsigned int) (m->p_type - PT_LOPROC));
5391 1.1 christos else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5392 1.1 christos sprintf (buf, "LOOS+%7.7x",
5393 1.1 christos (unsigned int) (m->p_type - PT_LOOS));
5394 1.1 christos else
5395 1.1 christos snprintf (buf, sizeof (buf), "%8.8x",
5396 1.1 christos (unsigned int) m->p_type);
5397 1.1 christos pt = buf;
5398 1.1 christos }
5399 1.1 christos fflush (stdout);
5400 1.1 christos fprintf (stderr, "%s:", pt);
5401 1.1 christos for (j = 0; j < m->count; j++)
5402 1.1 christos fprintf (stderr, " %s", m->sections [j]->name);
5403 1.1 christos putc ('\n',stderr);
5404 1.1 christos fflush (stderr);
5405 1.14 christos }
5406 1.1 christos
5407 1.1 christos static bool
5408 1.1 christos write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
5409 1.14 christos {
5410 1.1 christos void *buf;
5411 1.1 christos bool ret;
5412 1.14 christos
5413 1.1 christos if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5414 1.1 christos return false;
5415 1.14 christos buf = bfd_zmalloc (len);
5416 1.1 christos if (buf == NULL)
5417 1.1 christos return false;
5418 1.1 christos ret = bfd_bwrite (buf, len, abfd) == len;
5419 1.1 christos free (buf);
5420 1.1 christos return ret;
5421 1.1 christos }
5422 1.1 christos
5423 1.1 christos /* Assign file positions to the sections based on the mapping from
5424 1.1 christos sections to segments. This function also sets up some fields in
5425 1.14 christos the file header. */
5426 1.1 christos
5427 1.1 christos static bool
5428 1.1 christos assign_file_positions_for_load_sections (bfd *abfd,
5429 1.1 christos struct bfd_link_info *link_info)
5430 1.1 christos {
5431 1.12 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5432 1.1 christos struct elf_segment_map *m;
5433 1.1 christos struct elf_segment_map *phdr_load_seg;
5434 1.12 christos Elf_Internal_Phdr *phdrs;
5435 1.1 christos Elf_Internal_Phdr *p;
5436 1.12 christos file_ptr off; /* Octets. */
5437 1.1 christos bfd_size_type maxpagesize;
5438 1.12 christos unsigned int alloc, actual;
5439 1.12 christos unsigned int i, j;
5440 1.1 christos struct elf_segment_map **sorted_seg_map;
5441 1.1 christos unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5442 1.14 christos
5443 1.14 christos if (link_info == NULL
5444 1.1 christos && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
5445 1.1 christos return false;
5446 1.3 christos
5447 1.12 christos alloc = 0;
5448 1.1 christos for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5449 1.1 christos m->idx = alloc++;
5450 1.1 christos
5451 1.1 christos if (alloc)
5452 1.1 christos {
5453 1.1 christos elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5454 1.1 christos elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5455 1.1 christos }
5456 1.1 christos else
5457 1.1 christos {
5458 1.1 christos /* PR binutils/12467. */
5459 1.1 christos elf_elfheader (abfd)->e_phoff = 0;
5460 1.3 christos elf_elfheader (abfd)->e_phentsize = 0;
5461 1.1 christos }
5462 1.1 christos
5463 1.3 christos elf_elfheader (abfd)->e_phnum = alloc;
5464 1.12 christos
5465 1.12 christos if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5466 1.12 christos {
5467 1.12 christos actual = alloc;
5468 1.1 christos elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5469 1.12 christos }
5470 1.12 christos else
5471 1.12 christos {
5472 1.12 christos actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5473 1.12 christos BFD_ASSERT (elf_program_header_size (abfd)
5474 1.12 christos == actual * bed->s->sizeof_phdr);
5475 1.1 christos BFD_ASSERT (actual >= alloc);
5476 1.1 christos }
5477 1.1 christos
5478 1.3 christos if (alloc == 0)
5479 1.14 christos {
5480 1.1 christos elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5481 1.1 christos return true;
5482 1.3 christos }
5483 1.1 christos
5484 1.1 christos /* We're writing the size in elf_program_header_size (abfd),
5485 1.3 christos see assign_file_positions_except_relocs, so make sure we have
5486 1.3 christos that amount allocated, with trailing space cleared.
5487 1.1 christos The variable alloc contains the computed need, while
5488 1.1 christos elf_program_header_size (abfd) contains the size used for the
5489 1.1 christos layout.
5490 1.1 christos See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5491 1.12 christos where the layout is forced to according to a larger size in the
5492 1.12 christos last iterations for the testcase ld-elf/header. */
5493 1.12 christos phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5494 1.1 christos + alloc * sizeof (*sorted_seg_map)));
5495 1.1 christos sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
5496 1.14 christos elf_tdata (abfd)->phdr = phdrs;
5497 1.1 christos if (phdrs == NULL)
5498 1.12 christos return false;
5499 1.12 christos
5500 1.12 christos for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
5501 1.12 christos {
5502 1.12 christos sorted_seg_map[j] = m;
5503 1.12 christos /* If elf_segment_map is not from map_sections_to_segments, the
5504 1.12 christos sections may not be correctly ordered. NOTE: sorting should
5505 1.12 christos not be done to the PT_NOTE section of a corefile, which may
5506 1.12 christos contain several pseudo-sections artificially created by bfd.
5507 1.12 christos Sorting these pseudo-sections breaks things badly. */
5508 1.12 christos if (m->count > 1
5509 1.12 christos && !(elf_elfheader (abfd)->e_type == ET_CORE
5510 1.12 christos && m->p_type == PT_NOTE))
5511 1.12 christos {
5512 1.12 christos for (i = 0; i < m->count; i++)
5513 1.12 christos m->sections[i]->target_index = i;
5514 1.12 christos qsort (m->sections, (size_t) m->count, sizeof (asection *),
5515 1.12 christos elf_sort_sections);
5516 1.12 christos }
5517 1.12 christos }
5518 1.12 christos if (alloc > 1)
5519 1.12 christos qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5520 1.1 christos elf_sort_segments);
5521 1.1 christos
5522 1.14 christos maxpagesize = 1;
5523 1.14 christos if ((abfd->flags & D_PAGED) != 0)
5524 1.14 christos {
5525 1.14 christos if (link_info != NULL)
5526 1.14 christos maxpagesize = link_info->maxpagesize;
5527 1.14 christos else
5528 1.1 christos maxpagesize = bed->maxpagesize;
5529 1.12 christos }
5530 1.1 christos
5531 1.12 christos /* Sections must map to file offsets past the ELF file header. */
5532 1.12 christos off = bed->s->sizeof_ehdr;
5533 1.12 christos /* And if one of the PT_LOAD headers doesn't include the program
5534 1.12 christos headers then we'll be mapping program headers in the usual
5535 1.12 christos position after the ELF file header. */
5536 1.12 christos phdr_load_seg = NULL;
5537 1.12 christos for (j = 0; j < alloc; j++)
5538 1.12 christos {
5539 1.12 christos m = sorted_seg_map[j];
5540 1.12 christos if (m->p_type != PT_LOAD)
5541 1.12 christos break;
5542 1.12 christos if (m->includes_phdrs)
5543 1.12 christos {
5544 1.12 christos phdr_load_seg = m;
5545 1.12 christos break;
5546 1.12 christos }
5547 1.12 christos }
5548 1.1 christos if (phdr_load_seg == NULL)
5549 1.12 christos off += actual * bed->s->sizeof_phdr;
5550 1.1 christos
5551 1.1 christos for (j = 0; j < alloc; j++)
5552 1.12 christos {
5553 1.14 christos asection **secpp;
5554 1.14 christos bfd_vma off_adjust; /* Octets. */
5555 1.14 christos bool no_contents;
5556 1.1 christos bfd_size_type p_align;
5557 1.1 christos bool p_align_p;
5558 1.1 christos
5559 1.1 christos /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5560 1.1 christos number of sections with contents contributing to both p_filesz
5561 1.1 christos and p_memsz, followed by a number of sections with no contents
5562 1.12 christos that just contribute to p_memsz. In this loop, OFF tracks next
5563 1.12 christos available file offset for PT_LOAD and PT_NOTE segments. */
5564 1.1 christos m = sorted_seg_map[j];
5565 1.1 christos p = phdrs + m->idx;
5566 1.14 christos p->p_type = m->p_type;
5567 1.14 christos p->p_flags = m->p_flags;
5568 1.1 christos p_align = bed->p_align;
5569 1.1 christos p_align_p = false;
5570 1.12 christos
5571 1.1 christos if (m->count == 0)
5572 1.12 christos p->p_vaddr = m->p_vaddr_offset * opb;
5573 1.1 christos else
5574 1.1 christos p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
5575 1.1 christos
5576 1.1 christos if (m->p_paddr_valid)
5577 1.1 christos p->p_paddr = m->p_paddr;
5578 1.1 christos else if (m->count == 0)
5579 1.12 christos p->p_paddr = 0;
5580 1.1 christos else
5581 1.1 christos p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
5582 1.1 christos
5583 1.1 christos if (p->p_type == PT_LOAD
5584 1.1 christos && (abfd->flags & D_PAGED) != 0)
5585 1.1 christos {
5586 1.1 christos /* p_align in demand paged PT_LOAD segments effectively stores
5587 1.1 christos the maximum page size. When copying an executable with
5588 1.1 christos objcopy, we set m->p_align from the input file. Use this
5589 1.1 christos value for maxpagesize rather than bed->maxpagesize, which
5590 1.1 christos may be different. Note that we use maxpagesize for PT_TLS
5591 1.1 christos segment alignment later in this function, so we are relying
5592 1.1 christos on at least one PT_LOAD segment appearing before a PT_TLS
5593 1.1 christos segment. */
5594 1.14 christos if (m->p_align_valid)
5595 1.14 christos maxpagesize = m->p_align;
5596 1.14 christos else if (p_align != 0
5597 1.14 christos && (link_info == NULL
5598 1.14 christos || !link_info->maxpagesize_is_set))
5599 1.14 christos /* Set p_align to the default p_align value while laying
5600 1.14 christos out segments aligning to the maximum page size or the
5601 1.14 christos largest section alignment. The run-time loader can
5602 1.14 christos align segments to the default p_align value or the
5603 1.1 christos maximum page size, depending on system page size. */
5604 1.1 christos p_align_p = true;
5605 1.1 christos
5606 1.1 christos p->p_align = maxpagesize;
5607 1.1 christos }
5608 1.1 christos else if (m->p_align_valid)
5609 1.1 christos p->p_align = m->p_align;
5610 1.12 christos else if (m->count == 0)
5611 1.12 christos p->p_align = 1 << bed->s->log_file_align;
5612 1.12 christos
5613 1.12 christos if (m == phdr_load_seg)
5614 1.12 christos {
5615 1.12 christos if (!m->includes_filehdr)
5616 1.12 christos p->p_offset = off;
5617 1.1 christos off += actual * bed->s->sizeof_phdr;
5618 1.14 christos }
5619 1.1 christos
5620 1.1 christos no_contents = false;
5621 1.1 christos off_adjust = 0;
5622 1.1 christos if (p->p_type == PT_LOAD
5623 1.12 christos && m->count > 0)
5624 1.1 christos {
5625 1.1 christos bfd_size_type align; /* Bytes. */
5626 1.1 christos unsigned int align_power = 0;
5627 1.1 christos
5628 1.1 christos if (m->p_align_valid)
5629 1.1 christos align = p->p_align;
5630 1.1 christos else
5631 1.1 christos {
5632 1.1 christos for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5633 1.1 christos {
5634 1.12 christos unsigned int secalign;
5635 1.1 christos
5636 1.1 christos secalign = bfd_section_alignment (*secpp);
5637 1.1 christos if (secalign > align_power)
5638 1.1 christos align_power = secalign;
5639 1.1 christos }
5640 1.14 christos align = (bfd_size_type) 1 << align_power;
5641 1.14 christos if (align < maxpagesize)
5642 1.14 christos {
5643 1.14 christos /* If a section requires alignment higher than the
5644 1.14 christos default p_align value, don't set p_align to the
5645 1.14 christos default p_align value. */
5646 1.14 christos if (align > p_align)
5647 1.14 christos p_align_p = false;
5648 1.14 christos align = maxpagesize;
5649 1.14 christos }
5650 1.14 christos else
5651 1.14 christos {
5652 1.14 christos /* If a section requires alignment higher than the
5653 1.14 christos maximum page size, set p_align to the section
5654 1.14 christos alignment. */
5655 1.14 christos p_align_p = true;
5656 1.1 christos p_align = align;
5657 1.1 christos }
5658 1.1 christos }
5659 1.1 christos
5660 1.1 christos for (i = 0; i < m->count; i++)
5661 1.1 christos if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5662 1.1 christos /* If we aren't making room for this section, then
5663 1.1 christos it must be SHT_NOBITS regardless of what we've
5664 1.1 christos set via struct bfd_elf_special_section. */
5665 1.1 christos elf_section_type (m->sections[i]) = SHT_NOBITS;
5666 1.1 christos
5667 1.14 christos /* Find out whether this segment contains any loadable
5668 1.1 christos sections. */
5669 1.1 christos no_contents = true;
5670 1.1 christos for (i = 0; i < m->count; i++)
5671 1.14 christos if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5672 1.1 christos {
5673 1.1 christos no_contents = false;
5674 1.1 christos break;
5675 1.12 christos }
5676 1.9 christos
5677 1.9 christos off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
5678 1.9 christos
5679 1.9 christos /* Broken hardware and/or kernel require that files do not
5680 1.12 christos map the same page with different permissions on some hppa
5681 1.12 christos processors. */
5682 1.9 christos if (j != 0
5683 1.9 christos && (abfd->flags & D_PAGED) != 0
5684 1.12 christos && bed->no_page_alias
5685 1.12 christos && (off & (maxpagesize - 1)) != 0
5686 1.9 christos && ((off & -maxpagesize)
5687 1.1 christos == ((off + off_adjust) & -maxpagesize)))
5688 1.1 christos off_adjust += maxpagesize;
5689 1.1 christos off += off_adjust;
5690 1.1 christos if (no_contents)
5691 1.1 christos {
5692 1.1 christos /* We shouldn't need to align the segment on disk since
5693 1.1 christos the segment doesn't need file space, but the gABI
5694 1.1 christos arguably requires the alignment and glibc ld.so
5695 1.1 christos checks it. So to comply with the alignment
5696 1.1 christos requirement but not waste file space, we adjust
5697 1.1 christos p_offset for just this segment. (OFF_ADJUST is
5698 1.1 christos subtracted from OFF later.) This may put p_offset
5699 1.1 christos past the end of file, but that shouldn't matter. */
5700 1.1 christos }
5701 1.1 christos else
5702 1.1 christos off_adjust = 0;
5703 1.1 christos }
5704 1.1 christos /* Make sure the .dynamic section is the first section in the
5705 1.1 christos PT_DYNAMIC segment. */
5706 1.1 christos else if (p->p_type == PT_DYNAMIC
5707 1.1 christos && m->count > 1
5708 1.1 christos && strcmp (m->sections[0]->name, ".dynamic") != 0)
5709 1.11 christos {
5710 1.9 christos _bfd_error_handler
5711 1.1 christos (_("%pB: The first section in the PT_DYNAMIC segment"
5712 1.1 christos " is not the .dynamic section"),
5713 1.14 christos abfd);
5714 1.1 christos bfd_set_error (bfd_error_bad_value);
5715 1.1 christos return false;
5716 1.1 christos }
5717 1.1 christos /* Set the note section type to SHT_NOTE. */
5718 1.1 christos else if (p->p_type == PT_NOTE)
5719 1.1 christos for (i = 0; i < m->count; i++)
5720 1.1 christos elf_section_type (m->sections[i]) = SHT_NOTE;
5721 1.1 christos
5722 1.1 christos if (m->includes_filehdr)
5723 1.1 christos {
5724 1.1 christos if (!m->p_flags_valid)
5725 1.1 christos p->p_flags |= PF_R;
5726 1.12 christos p->p_filesz = bed->s->sizeof_ehdr;
5727 1.1 christos p->p_memsz = bed->s->sizeof_ehdr;
5728 1.12 christos if (p->p_type == PT_LOAD)
5729 1.1 christos {
5730 1.12 christos if (m->count > 0)
5731 1.12 christos {
5732 1.12 christos if (p->p_vaddr < (bfd_vma) off
5733 1.12 christos || (!m->p_paddr_valid
5734 1.12 christos && p->p_paddr < (bfd_vma) off))
5735 1.12 christos {
5736 1.12 christos _bfd_error_handler
5737 1.12 christos (_("%pB: not enough room for program headers,"
5738 1.12 christos " try linking with -N"),
5739 1.14 christos abfd);
5740 1.12 christos bfd_set_error (bfd_error_bad_value);
5741 1.12 christos return false;
5742 1.12 christos }
5743 1.12 christos p->p_vaddr -= off;
5744 1.1 christos if (!m->p_paddr_valid)
5745 1.12 christos p->p_paddr -= off;
5746 1.12 christos }
5747 1.12 christos }
5748 1.12 christos else if (sorted_seg_map[0]->includes_filehdr)
5749 1.12 christos {
5750 1.1 christos Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
5751 1.12 christos p->p_vaddr = filehdr->p_vaddr;
5752 1.1 christos if (!m->p_paddr_valid)
5753 1.1 christos p->p_paddr = filehdr->p_paddr;
5754 1.1 christos }
5755 1.1 christos }
5756 1.1 christos
5757 1.1 christos if (m->includes_phdrs)
5758 1.1 christos {
5759 1.12 christos if (!m->p_flags_valid)
5760 1.12 christos p->p_flags |= PF_R;
5761 1.1 christos p->p_filesz += actual * bed->s->sizeof_phdr;
5762 1.1 christos p->p_memsz += actual * bed->s->sizeof_phdr;
5763 1.12 christos if (!m->includes_filehdr)
5764 1.12 christos {
5765 1.12 christos if (p->p_type == PT_LOAD)
5766 1.12 christos {
5767 1.12 christos elf_elfheader (abfd)->e_phoff = p->p_offset;
5768 1.12 christos if (m->count > 0)
5769 1.12 christos {
5770 1.12 christos p->p_vaddr -= off - p->p_offset;
5771 1.12 christos if (!m->p_paddr_valid)
5772 1.12 christos p->p_paddr -= off - p->p_offset;
5773 1.12 christos }
5774 1.1 christos }
5775 1.12 christos else if (phdr_load_seg != NULL)
5776 1.12 christos {
5777 1.12 christos Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
5778 1.12 christos bfd_vma phdr_off = 0; /* Octets. */
5779 1.12 christos if (phdr_load_seg->includes_filehdr)
5780 1.1 christos phdr_off = bed->s->sizeof_ehdr;
5781 1.12 christos p->p_vaddr = phdr->p_vaddr + phdr_off;
5782 1.12 christos if (!m->p_paddr_valid)
5783 1.1 christos p->p_paddr = phdr->p_paddr + phdr_off;
5784 1.12 christos p->p_offset = phdr->p_offset + phdr_off;
5785 1.12 christos }
5786 1.1 christos else
5787 1.1 christos p->p_offset = bed->s->sizeof_ehdr;
5788 1.1 christos }
5789 1.1 christos }
5790 1.1 christos
5791 1.1 christos if (p->p_type == PT_LOAD
5792 1.1 christos || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5793 1.12 christos {
5794 1.12 christos if (!m->includes_filehdr && !m->includes_phdrs)
5795 1.12 christos {
5796 1.12 christos p->p_offset = off;
5797 1.12 christos if (no_contents)
5798 1.12 christos {
5799 1.12 christos /* Put meaningless p_offset for PT_LOAD segments
5800 1.12 christos without file contents somewhere within the first
5801 1.12 christos page, in an attempt to not point past EOF. */
5802 1.12 christos bfd_size_type align = maxpagesize;
5803 1.12 christos if (align < p->p_align)
5804 1.12 christos align = p->p_align;
5805 1.12 christos if (align < 1)
5806 1.12 christos align = 1;
5807 1.12 christos p->p_offset = off % align;
5808 1.1 christos }
5809 1.1 christos }
5810 1.12 christos else
5811 1.1 christos {
5812 1.1 christos file_ptr adjust; /* Octets. */
5813 1.1 christos
5814 1.1 christos adjust = off - (p->p_offset + p->p_filesz);
5815 1.1 christos if (!no_contents)
5816 1.1 christos p->p_filesz += adjust;
5817 1.1 christos p->p_memsz += adjust;
5818 1.1 christos }
5819 1.1 christos }
5820 1.1 christos
5821 1.1 christos /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5822 1.1 christos maps. Set filepos for sections in PT_LOAD segments, and in
5823 1.1 christos core files, for sections in PT_NOTE segments.
5824 1.1 christos assign_file_positions_for_non_load_sections will set filepos
5825 1.1 christos for other sections and update p_filesz for other segments. */
5826 1.1 christos for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5827 1.1 christos {
5828 1.1 christos asection *sec;
5829 1.1 christos bfd_size_type align;
5830 1.1 christos Elf_Internal_Shdr *this_hdr;
5831 1.1 christos
5832 1.12 christos sec = *secpp;
5833 1.1 christos this_hdr = &elf_section_data (sec)->this_hdr;
5834 1.1 christos align = (bfd_size_type) 1 << bfd_section_alignment (sec);
5835 1.1 christos
5836 1.1 christos if ((p->p_type == PT_LOAD
5837 1.1 christos || p->p_type == PT_TLS)
5838 1.1 christos && (this_hdr->sh_type != SHT_NOBITS
5839 1.1 christos || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5840 1.1 christos && ((this_hdr->sh_flags & SHF_TLS) == 0
5841 1.14 christos || p->p_type == PT_TLS))))
5842 1.14 christos {
5843 1.14 christos bfd_vma p_start = p->p_paddr; /* Octets. */
5844 1.14 christos bfd_vma p_end = p_start + p->p_memsz; /* Octets. */
5845 1.1 christos bfd_vma s_start = sec->lma * opb; /* Octets. */
5846 1.1 christos bfd_vma adjust = s_start - p_end; /* Octets. */
5847 1.1 christos
5848 1.1 christos if (adjust != 0
5849 1.1 christos && (s_start < p_end
5850 1.9 christos || p_end < p_start))
5851 1.9 christos {
5852 1.14 christos _bfd_error_handler
5853 1.14 christos /* xgettext:c-format */
5854 1.12 christos (_("%pB: section %pA lma %#" PRIx64
5855 1.12 christos " adjusted to %#" PRIx64),
5856 1.1 christos abfd, sec, (uint64_t) s_start / opb,
5857 1.12 christos (uint64_t) p_end / opb);
5858 1.1 christos adjust = 0;
5859 1.1 christos sec->lma = p_end / opb;
5860 1.1 christos }
5861 1.12 christos p->p_memsz += adjust;
5862 1.1 christos
5863 1.12 christos if (p->p_type == PT_LOAD)
5864 1.12 christos {
5865 1.12 christos if (this_hdr->sh_type != SHT_NOBITS)
5866 1.12 christos {
5867 1.12 christos off_adjust = 0;
5868 1.12 christos if (p->p_filesz + adjust < p->p_memsz)
5869 1.12 christos {
5870 1.12 christos /* We have a PROGBITS section following NOBITS ones.
5871 1.12 christos Allocate file space for the NOBITS section(s) and
5872 1.12 christos zero it. */
5873 1.14 christos adjust = p->p_memsz - p->p_filesz;
5874 1.12 christos if (!write_zeros (abfd, off, adjust))
5875 1.12 christos return false;
5876 1.12 christos }
5877 1.12 christos }
5878 1.12 christos /* We only adjust sh_offset in SHT_NOBITS sections
5879 1.12 christos as would seem proper for their address when the
5880 1.12 christos section is first in the segment. sh_offset
5881 1.12 christos doesn't really have any significance for
5882 1.12 christos SHT_NOBITS anyway, apart from a notional position
5883 1.12 christos relative to other sections. Historically we
5884 1.12 christos didn't bother with adjusting sh_offset and some
5885 1.12 christos programs depend on it not being adjusted. See
5886 1.1 christos pr12921 and pr25662. */
5887 1.12 christos if (this_hdr->sh_type != SHT_NOBITS || i == 0)
5888 1.12 christos {
5889 1.12 christos off += adjust;
5890 1.1 christos if (this_hdr->sh_type == SHT_NOBITS)
5891 1.1 christos off_adjust += adjust;
5892 1.12 christos }
5893 1.12 christos }
5894 1.1 christos if (this_hdr->sh_type != SHT_NOBITS)
5895 1.1 christos p->p_filesz += adjust;
5896 1.1 christos }
5897 1.1 christos
5898 1.1 christos if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5899 1.1 christos {
5900 1.1 christos /* The section at i == 0 is the one that actually contains
5901 1.1 christos everything. */
5902 1.1 christos if (i == 0)
5903 1.1 christos {
5904 1.1 christos this_hdr->sh_offset = sec->filepos = off;
5905 1.1 christos off += this_hdr->sh_size;
5906 1.1 christos p->p_filesz = this_hdr->sh_size;
5907 1.1 christos p->p_memsz = 0;
5908 1.1 christos p->p_align = 1;
5909 1.1 christos }
5910 1.1 christos else
5911 1.1 christos {
5912 1.1 christos /* The rest are fake sections that shouldn't be written. */
5913 1.1 christos sec->filepos = 0;
5914 1.1 christos sec->size = 0;
5915 1.1 christos sec->flags = 0;
5916 1.1 christos continue;
5917 1.1 christos }
5918 1.1 christos }
5919 1.1 christos else
5920 1.1 christos {
5921 1.1 christos if (p->p_type == PT_LOAD)
5922 1.1 christos {
5923 1.1 christos this_hdr->sh_offset = sec->filepos = off;
5924 1.1 christos if (this_hdr->sh_type != SHT_NOBITS)
5925 1.3 christos off += this_hdr->sh_size;
5926 1.3 christos }
5927 1.3 christos else if (this_hdr->sh_type == SHT_NOBITS
5928 1.3 christos && (this_hdr->sh_flags & SHF_TLS) != 0
5929 1.3 christos && this_hdr->sh_offset == 0)
5930 1.3 christos {
5931 1.3 christos /* This is a .tbss section that didn't get a PT_LOAD.
5932 1.3 christos (See _bfd_elf_map_sections_to_segments "Create a
5933 1.3 christos final PT_LOAD".) Set sh_offset to the value it
5934 1.3 christos would have if we had created a zero p_filesz and
5935 1.3 christos p_memsz PT_LOAD header for the section. This
5936 1.3 christos also makes the PT_TLS header have the same
5937 1.3 christos p_offset value. */
5938 1.3 christos bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5939 1.3 christos off, align);
5940 1.1 christos this_hdr->sh_offset = sec->filepos = off + adjust;
5941 1.1 christos }
5942 1.1 christos
5943 1.1 christos if (this_hdr->sh_type != SHT_NOBITS)
5944 1.1 christos {
5945 1.1 christos p->p_filesz += this_hdr->sh_size;
5946 1.1 christos /* A load section without SHF_ALLOC is something like
5947 1.1 christos a note section in a PT_NOTE segment. These take
5948 1.1 christos file space but are not loaded into memory. */
5949 1.1 christos if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5950 1.1 christos p->p_memsz += this_hdr->sh_size;
5951 1.1 christos }
5952 1.1 christos else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5953 1.1 christos {
5954 1.1 christos if (p->p_type == PT_TLS)
5955 1.1 christos p->p_memsz += this_hdr->sh_size;
5956 1.1 christos
5957 1.1 christos /* .tbss is special. It doesn't contribute to p_memsz of
5958 1.1 christos normal segments. */
5959 1.1 christos else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5960 1.1 christos p->p_memsz += this_hdr->sh_size;
5961 1.1 christos }
5962 1.1 christos
5963 1.1 christos if (align > p->p_align
5964 1.1 christos && !m->p_align_valid
5965 1.1 christos && (p->p_type != PT_LOAD
5966 1.1 christos || (abfd->flags & D_PAGED) == 0))
5967 1.1 christos p->p_align = align;
5968 1.1 christos }
5969 1.1 christos
5970 1.1 christos if (!m->p_flags_valid)
5971 1.1 christos {
5972 1.1 christos p->p_flags |= PF_R;
5973 1.1 christos if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
5974 1.1 christos p->p_flags |= PF_X;
5975 1.1 christos if ((this_hdr->sh_flags & SHF_WRITE) != 0)
5976 1.1 christos p->p_flags |= PF_W;
5977 1.5 christos }
5978 1.1 christos }
5979 1.1 christos
5980 1.12 christos off -= off_adjust;
5981 1.12 christos
5982 1.12 christos /* PR ld/20815 - Check that the program header segment, if
5983 1.12 christos present, will be loaded into memory. */
5984 1.12 christos if (p->p_type == PT_PHDR
5985 1.12 christos && phdr_load_seg == NULL
5986 1.12 christos && !(bed->elf_backend_allow_non_load_phdr != NULL
5987 1.12 christos && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
5988 1.12 christos {
5989 1.12 christos /* The fix for this error is usually to edit the linker script being
5990 1.12 christos used and set up the program headers manually. Either that or
5991 1.12 christos leave room for the headers at the start of the SECTIONS. */
5992 1.12 christos _bfd_error_handler (_("%pB: error: PHDR segment not covered"
5993 1.12 christos " by LOAD segment"),
5994 1.14 christos abfd);
5995 1.12 christos if (link_info == NULL)
5996 1.12 christos return false;
5997 1.12 christos /* Arrange for the linker to exit with an error, deleting
5998 1.12 christos the output file unless --noinhibit-exec is given. */
5999 1.12 christos link_info->callbacks->info ("%X");
6000 1.1 christos }
6001 1.1 christos
6002 1.1 christos /* Check that all sections are in a PT_LOAD segment.
6003 1.1 christos Don't check funky gdb generated core files. */
6004 1.14 christos if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
6005 1.1 christos {
6006 1.1 christos bool check_vma = true;
6007 1.1 christos
6008 1.1 christos for (i = 1; i < m->count; i++)
6009 1.1 christos if (m->sections[i]->vma == m->sections[i - 1]->vma
6010 1.1 christos && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
6011 1.1 christos ->this_hdr), p) != 0
6012 1.1 christos && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
6013 1.1 christos ->this_hdr), p) != 0)
6014 1.14 christos {
6015 1.1 christos /* Looks like we have overlays packed into the segment. */
6016 1.1 christos check_vma = false;
6017 1.1 christos break;
6018 1.1 christos }
6019 1.1 christos
6020 1.1 christos for (i = 0; i < m->count; i++)
6021 1.1 christos {
6022 1.1 christos Elf_Internal_Shdr *this_hdr;
6023 1.1 christos asection *sec;
6024 1.1 christos
6025 1.3 christos sec = m->sections[i];
6026 1.3 christos this_hdr = &(elf_section_data(sec)->this_hdr);
6027 1.1 christos if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6028 1.9 christos && !ELF_TBSS_SPECIAL (this_hdr, p))
6029 1.9 christos {
6030 1.11 christos _bfd_error_handler
6031 1.1 christos /* xgettext:c-format */
6032 1.1 christos (_("%pB: section `%pA' can't be allocated in segment %d"),
6033 1.1 christos abfd, sec, j);
6034 1.1 christos print_segment_map (m);
6035 1.14 christos }
6036 1.14 christos }
6037 1.14 christos
6038 1.1 christos if (p_align_p)
6039 1.1 christos p->p_align = p_align;
6040 1.1 christos }
6041 1.3 christos }
6042 1.1 christos
6043 1.12 christos elf_next_file_pos (abfd) = off;
6044 1.12 christos
6045 1.12 christos if (link_info != NULL
6046 1.1 christos && phdr_load_seg != NULL
6047 1.12 christos && phdr_load_seg->includes_filehdr)
6048 1.12 christos {
6049 1.12 christos /* There is a segment that contains both the file headers and the
6050 1.12 christos program headers, so provide a symbol __ehdr_start pointing there.
6051 1.12 christos A program can use this to examine itself robustly. */
6052 1.12 christos
6053 1.14 christos struct elf_link_hash_entry *hash
6054 1.12 christos = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
6055 1.12 christos false, false, true);
6056 1.12 christos /* If the symbol was referenced and not defined, define it. */
6057 1.12 christos if (hash != NULL
6058 1.12 christos && (hash->root.type == bfd_link_hash_new
6059 1.12 christos || hash->root.type == bfd_link_hash_undefined
6060 1.12 christos || hash->root.type == bfd_link_hash_undefweak
6061 1.12 christos || hash->root.type == bfd_link_hash_common))
6062 1.12 christos {
6063 1.12 christos asection *s = NULL;
6064 1.12 christos bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
6065 1.12 christos
6066 1.12 christos if (phdr_load_seg->count != 0)
6067 1.12 christos /* The segment contains sections, so use the first one. */
6068 1.12 christos s = phdr_load_seg->sections[0];
6069 1.12 christos else
6070 1.12 christos /* Use the first (i.e. lowest-addressed) section in any segment. */
6071 1.12 christos for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6072 1.12 christos if (m->p_type == PT_LOAD && m->count != 0)
6073 1.12 christos {
6074 1.12 christos s = m->sections[0];
6075 1.12 christos break;
6076 1.12 christos }
6077 1.12 christos
6078 1.12 christos if (s != NULL)
6079 1.12 christos {
6080 1.12 christos hash->root.u.def.value = filehdr_vaddr - s->vma;
6081 1.12 christos hash->root.u.def.section = s;
6082 1.12 christos }
6083 1.12 christos else
6084 1.12 christos {
6085 1.12 christos hash->root.u.def.value = filehdr_vaddr;
6086 1.12 christos hash->root.u.def.section = bfd_abs_section_ptr;
6087 1.12 christos }
6088 1.12 christos
6089 1.12 christos hash->root.type = bfd_link_hash_defined;
6090 1.12 christos hash->def_regular = 1;
6091 1.12 christos hash->non_elf = 0;
6092 1.12 christos }
6093 1.14 christos }
6094 1.12 christos
6095 1.12 christos return true;
6096 1.12 christos }
6097 1.12 christos
6098 1.12 christos /* Determine if a bfd is a debuginfo file. Unfortunately there
6099 1.12 christos is no defined method for detecting such files, so we have to
6100 1.14 christos use heuristics instead. */
6101 1.12 christos
6102 1.12 christos bool
6103 1.12 christos is_debuginfo_file (bfd *abfd)
6104 1.14 christos {
6105 1.12 christos if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6106 1.12 christos return false;
6107 1.12 christos
6108 1.12 christos Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6109 1.12 christos Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6110 1.12 christos Elf_Internal_Shdr **headerp;
6111 1.12 christos
6112 1.12 christos for (headerp = start_headers; headerp < end_headers; headerp ++)
6113 1.12 christos {
6114 1.12 christos Elf_Internal_Shdr *header = * headerp;
6115 1.12 christos
6116 1.12 christos /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6117 1.12 christos The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6118 1.12 christos if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6119 1.14 christos && header->sh_type != SHT_NOBITS
6120 1.12 christos && header->sh_type != SHT_NOTE)
6121 1.12 christos return false;
6122 1.14 christos }
6123 1.12 christos
6124 1.12 christos return true;
6125 1.14 christos }
6126 1.14 christos
6127 1.12 christos /* Assign file positions for other sections, except for compressed debug
6128 1.14 christos and sections assigned in _bfd_elf_assign_file_positions_for_non_load. */
6129 1.12 christos
6130 1.12 christos static bool
6131 1.12 christos assign_file_positions_for_non_load_sections (bfd *abfd,
6132 1.12 christos struct bfd_link_info *link_info)
6133 1.12 christos {
6134 1.12 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6135 1.12 christos Elf_Internal_Shdr **i_shdrpp;
6136 1.12 christos Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6137 1.12 christos Elf_Internal_Phdr *phdrs;
6138 1.12 christos Elf_Internal_Phdr *p;
6139 1.12 christos struct elf_segment_map *m;
6140 1.14 christos file_ptr off;
6141 1.12 christos unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6142 1.14 christos bfd_vma maxpagesize;
6143 1.14 christos
6144 1.14 christos if (link_info != NULL)
6145 1.14 christos maxpagesize = link_info->maxpagesize;
6146 1.12 christos else
6147 1.12 christos maxpagesize = bed->maxpagesize;
6148 1.12 christos i_shdrpp = elf_elfsections (abfd);
6149 1.12 christos end_hdrpp = i_shdrpp + elf_numsections (abfd);
6150 1.12 christos off = elf_next_file_pos (abfd);
6151 1.12 christos for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
6152 1.14 christos {
6153 1.1 christos Elf_Internal_Shdr *hdr;
6154 1.1 christos bfd_vma align;
6155 1.1 christos
6156 1.1 christos hdr = *hdrpp;
6157 1.1 christos if (hdr->bfd_section != NULL
6158 1.1 christos && (hdr->bfd_section->filepos != 0
6159 1.1 christos || (hdr->sh_type == SHT_NOBITS
6160 1.1 christos && hdr->contents == NULL)))
6161 1.1 christos BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6162 1.12 christos else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6163 1.12 christos {
6164 1.12 christos if (hdr->sh_size != 0
6165 1.12 christos /* PR 24717 - debuginfo files are known to be not strictly
6166 1.12 christos compliant with the ELF standard. In particular they often
6167 1.12 christos have .note.gnu.property sections that are outside of any
6168 1.12 christos loadable segment. This is not a problem for such files,
6169 1.9 christos so do not warn about them. */
6170 1.9 christos && ! is_debuginfo_file (abfd))
6171 1.11 christos _bfd_error_handler
6172 1.3 christos /* xgettext:c-format */
6173 1.3 christos (_("%pB: warning: allocated section `%s' not in segment"),
6174 1.3 christos abfd,
6175 1.3 christos (hdr->bfd_section == NULL
6176 1.1 christos ? "*unknown*"
6177 1.1 christos : hdr->bfd_section->name));
6178 1.14 christos /* We don't need to page align empty sections. */
6179 1.1 christos if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6180 1.14 christos align = maxpagesize;
6181 1.14 christos else
6182 1.1 christos align = hdr->sh_addralign & -hdr->sh_addralign;
6183 1.14 christos off += vma_page_aligned_bias (hdr->sh_addr, off, align);
6184 1.1 christos off = _bfd_elf_assign_file_position_for_section (hdr, off,
6185 1.1 christos false);
6186 1.1 christos }
6187 1.14 christos else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6188 1.14 christos && hdr->bfd_section == NULL)
6189 1.14 christos /* We don't know the offset of these sections yet:
6190 1.14 christos their size has not been decided. */
6191 1.14 christos || (abfd->is_linker_output
6192 1.14 christos && hdr->bfd_section != NULL
6193 1.3 christos && (hdr->sh_name == -1u
6194 1.8 christos || bfd_section_is_ctf (hdr->bfd_section)))
6195 1.8 christos || hdr == i_shdrpp[elf_onesymtab (abfd)]
6196 1.6 christos || (elf_symtab_shndx_list (abfd) != NULL
6197 1.6 christos && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6198 1.1 christos || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6199 1.1 christos || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6200 1.14 christos hdr->sh_offset = -1;
6201 1.1 christos else
6202 1.12 christos off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6203 1.1 christos }
6204 1.1 christos elf_next_file_pos (abfd) = off;
6205 1.1 christos
6206 1.1 christos /* Now that we have set the section file positions, we can set up
6207 1.3 christos the file positions for the non PT_LOAD segments. */
6208 1.1 christos phdrs = elf_tdata (abfd)->phdr;
6209 1.1 christos for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6210 1.1 christos {
6211 1.12 christos if (p->p_type == PT_GNU_RELRO)
6212 1.14 christos {
6213 1.1 christos bfd_vma start, end; /* Bytes. */
6214 1.1 christos bool ok;
6215 1.1 christos
6216 1.1 christos if (link_info != NULL)
6217 1.11 christos {
6218 1.11 christos /* During linking the range of the RELRO segment is passed
6219 1.11 christos in link_info. Note that there may be padding between
6220 1.11 christos relro_start and the first RELRO section. */
6221 1.11 christos start = link_info->relro_start;
6222 1.11 christos end = link_info->relro_end;
6223 1.11 christos }
6224 1.11 christos else if (m->count != 0)
6225 1.11 christos {
6226 1.11 christos if (!m->p_size_valid)
6227 1.12 christos abort ();
6228 1.11 christos start = m->sections[0]->vma;
6229 1.11 christos end = start + m->p_size / opb;
6230 1.11 christos }
6231 1.11 christos else
6232 1.11 christos {
6233 1.11 christos start = 0;
6234 1.11 christos end = 0;
6235 1.14 christos }
6236 1.11 christos
6237 1.11 christos ok = false;
6238 1.11 christos if (start < end)
6239 1.11 christos {
6240 1.11 christos struct elf_segment_map *lm;
6241 1.11 christos const Elf_Internal_Phdr *lp;
6242 1.11 christos unsigned int i;
6243 1.11 christos
6244 1.3 christos /* Find a LOAD segment containing a section in the RELRO
6245 1.3 christos segment. */
6246 1.3 christos for (lm = elf_seg_map (abfd), lp = phdrs;
6247 1.1 christos lm != NULL;
6248 1.1 christos lm = lm->next, lp++)
6249 1.3 christos {
6250 1.11 christos if (lp->p_type == PT_LOAD
6251 1.11 christos && lm->count != 0
6252 1.12 christos && (lm->sections[lm->count - 1]->vma
6253 1.11 christos + (!IS_TBSS (lm->sections[lm->count - 1])
6254 1.11 christos ? lm->sections[lm->count - 1]->size / opb
6255 1.1 christos : 0)) > start
6256 1.1 christos && lm->sections[0]->vma < end)
6257 1.3 christos break;
6258 1.11 christos }
6259 1.1 christos
6260 1.11 christos if (lm != NULL)
6261 1.11 christos {
6262 1.11 christos /* Find the section starting the RELRO segment. */
6263 1.11 christos for (i = 0; i < lm->count; i++)
6264 1.11 christos {
6265 1.11 christos asection *s = lm->sections[i];
6266 1.11 christos if (s->vma >= start
6267 1.11 christos && s->vma < end
6268 1.11 christos && s->size != 0)
6269 1.11 christos break;
6270 1.11 christos }
6271 1.11 christos
6272 1.12 christos if (i < lm->count)
6273 1.12 christos {
6274 1.11 christos p->p_vaddr = lm->sections[i]->vma * opb;
6275 1.12 christos p->p_paddr = lm->sections[i]->lma * opb;
6276 1.11 christos p->p_offset = lm->sections[i]->filepos;
6277 1.11 christos p->p_memsz = end * opb - p->p_vaddr;
6278 1.11 christos p->p_filesz = p->p_memsz;
6279 1.11 christos
6280 1.11 christos /* The RELRO segment typically ends a few bytes
6281 1.11 christos into .got.plt but other layouts are possible.
6282 1.11 christos In cases where the end does not match any
6283 1.11 christos loaded section (for instance is in file
6284 1.11 christos padding), trim p_filesz back to correspond to
6285 1.11 christos the end of loaded section contents. */
6286 1.11 christos if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6287 1.11 christos p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6288 1.11 christos
6289 1.11 christos /* Preserve the alignment and flags if they are
6290 1.11 christos valid. The gold linker generates RW/4 for
6291 1.11 christos the PT_GNU_RELRO section. It is better for
6292 1.11 christos objcopy/strip to honor these attributes
6293 1.11 christos otherwise gdb will choke when using separate
6294 1.11 christos debug files. */
6295 1.11 christos if (!m->p_align_valid)
6296 1.11 christos p->p_align = 1;
6297 1.14 christos if (!m->p_flags_valid)
6298 1.11 christos p->p_flags = PF_R;
6299 1.1 christos ok = true;
6300 1.1 christos }
6301 1.14 christos }
6302 1.11 christos }
6303 1.14 christos
6304 1.14 christos if (!ok)
6305 1.14 christos {
6306 1.14 christos if (link_info != NULL)
6307 1.14 christos _bfd_error_handler
6308 1.14 christos (_("%pB: warning: unable to allocate any sections"
6309 1.14 christos " to PT_GNU_RELRO segment"),
6310 1.14 christos abfd);
6311 1.1 christos memset (p, 0, sizeof *p);
6312 1.3 christos }
6313 1.3 christos }
6314 1.3 christos else if (p->p_type == PT_GNU_STACK)
6315 1.3 christos {
6316 1.3 christos if (m->p_size_valid)
6317 1.1 christos p->p_memsz = m->p_size;
6318 1.1 christos }
6319 1.6 christos else if (m->count != 0)
6320 1.9 christos {
6321 1.1 christos unsigned int i;
6322 1.1 christos
6323 1.1 christos if (p->p_type != PT_LOAD
6324 1.1 christos && (p->p_type != PT_NOTE
6325 1.9 christos || bfd_get_format (abfd) != bfd_core))
6326 1.9 christos {
6327 1.9 christos /* A user specified segment layout may include a PHDR
6328 1.9 christos segment that overlaps with a LOAD segment... */
6329 1.9 christos if (p->p_type == PT_PHDR)
6330 1.9 christos {
6331 1.9 christos m->count = 0;
6332 1.9 christos continue;
6333 1.6 christos }
6334 1.6 christos
6335 1.6 christos if (m->includes_filehdr || m->includes_phdrs)
6336 1.9 christos {
6337 1.11 christos /* PR 17512: file: 2195325e. */
6338 1.11 christos _bfd_error_handler
6339 1.11 christos (_("%pB: error: non-load segment %d includes file header "
6340 1.14 christos "and/or program header"),
6341 1.6 christos abfd, (int) (p - phdrs));
6342 1.1 christos return false;
6343 1.3 christos }
6344 1.1 christos
6345 1.3 christos p->p_filesz = 0;
6346 1.3 christos p->p_offset = m->sections[0]->filepos;
6347 1.3 christos for (i = m->count; i-- != 0;)
6348 1.3 christos {
6349 1.3 christos asection *sect = m->sections[i];
6350 1.3 christos Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6351 1.14 christos if (hdr->sh_type != SHT_NOBITS)
6352 1.14 christos {
6353 1.14 christos p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
6354 1.14 christos /* NB: p_memsz of the loadable PT_NOTE segment
6355 1.14 christos should be the same as p_filesz. */
6356 1.14 christos if (p->p_type == PT_NOTE
6357 1.3 christos && (hdr->sh_flags & SHF_ALLOC) != 0)
6358 1.3 christos p->p_memsz = p->p_filesz;
6359 1.3 christos break;
6360 1.1 christos }
6361 1.1 christos }
6362 1.1 christos }
6363 1.1 christos }
6364 1.14 christos }
6365 1.1 christos
6366 1.1 christos return true;
6367 1.8 christos }
6368 1.8 christos
6369 1.8 christos static elf_section_list *
6370 1.8 christos find_section_in_list (unsigned int i, elf_section_list * list)
6371 1.8 christos {
6372 1.8 christos for (;list != NULL; list = list->next)
6373 1.8 christos if (list->ndx == i)
6374 1.8 christos break;
6375 1.8 christos return list;
6376 1.1 christos }
6377 1.1 christos
6378 1.1 christos /* Work out the file positions of all the sections. This is called by
6379 1.1 christos _bfd_elf_compute_section_file_positions. All the section sizes and
6380 1.1 christos VMAs must be known before this is called.
6381 1.14 christos
6382 1.14 christos Reloc sections come in two flavours: Those processed specially as
6383 1.14 christos "side-channel" data attached to a section to which they apply, and
6384 1.14 christos those that bfd doesn't process as relocations. The latter sort are
6385 1.14 christos stored in a normal bfd section by bfd_section_from_shdr. We don't
6386 1.14 christos consider the former sort here, unless they form part of the loadable
6387 1.14 christos image. Reloc sections not assigned here (and compressed debugging
6388 1.1 christos sections and CTF sections which nothing else in the file can rely
6389 1.1 christos upon) will be handled later by assign_file_positions_for_relocs.
6390 1.1 christos
6391 1.14 christos We also don't set the positions of the .symtab and .strtab here. */
6392 1.1 christos
6393 1.1 christos static bool
6394 1.1 christos assign_file_positions_except_relocs (bfd *abfd,
6395 1.1 christos struct bfd_link_info *link_info)
6396 1.1 christos {
6397 1.1 christos struct elf_obj_tdata *tdata = elf_tdata (abfd);
6398 1.12 christos Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6399 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6400 1.1 christos unsigned int alloc;
6401 1.1 christos
6402 1.1 christos if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6403 1.1 christos && bfd_get_format (abfd) != bfd_core)
6404 1.1 christos {
6405 1.1 christos Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6406 1.1 christos unsigned int num_sec = elf_numsections (abfd);
6407 1.5 christos Elf_Internal_Shdr **hdrpp;
6408 1.1 christos unsigned int i;
6409 1.1 christos file_ptr off;
6410 1.1 christos
6411 1.1 christos /* Start after the ELF header. */
6412 1.1 christos off = i_ehdrp->e_ehsize;
6413 1.1 christos
6414 1.1 christos /* We are not creating an executable, which means that we are
6415 1.1 christos not creating a program header, and that the actual order of
6416 1.1 christos the sections in the file is unimportant. */
6417 1.1 christos for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6418 1.1 christos {
6419 1.1 christos Elf_Internal_Shdr *hdr;
6420 1.1 christos
6421 1.1 christos hdr = *hdrpp;
6422 1.12 christos if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6423 1.12 christos && hdr->bfd_section == NULL)
6424 1.14 christos /* Do not assign offsets for these sections yet: we don't know
6425 1.14 christos their sizes. */
6426 1.14 christos || (abfd->is_linker_output
6427 1.14 christos && hdr->bfd_section != NULL
6428 1.3 christos && (hdr->sh_name == -1u
6429 1.8 christos || bfd_section_is_ctf (hdr->bfd_section)))
6430 1.8 christos || i == elf_onesymtab (abfd)
6431 1.6 christos || (elf_symtab_shndx_list (abfd) != NULL
6432 1.6 christos && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6433 1.1 christos || i == elf_strtab_sec (abfd)
6434 1.1 christos || i == elf_shstrtab_sec (abfd))
6435 1.1 christos {
6436 1.1 christos hdr->sh_offset = -1;
6437 1.14 christos }
6438 1.1 christos else
6439 1.5 christos off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6440 1.5 christos }
6441 1.12 christos
6442 1.1 christos elf_next_file_pos (abfd) = off;
6443 1.1 christos elf_program_header_size (abfd) = 0;
6444 1.1 christos }
6445 1.1 christos else
6446 1.1 christos {
6447 1.1 christos /* Assign file positions for the loaded sections based on the
6448 1.14 christos assignment of sections to segments. */
6449 1.1 christos if (!assign_file_positions_for_load_sections (abfd, link_info))
6450 1.1 christos return false;
6451 1.1 christos
6452 1.14 christos /* And for non-load sections. */
6453 1.12 christos if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6454 1.1 christos return false;
6455 1.12 christos }
6456 1.14 christos
6457 1.1 christos if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
6458 1.12 christos return false;
6459 1.12 christos
6460 1.12 christos /* Write out the program headers. */
6461 1.12 christos alloc = i_ehdrp->e_phnum;
6462 1.14 christos if (alloc != 0)
6463 1.14 christos {
6464 1.14 christos if (link_info != NULL && ! link_info->no_warn_rwx_segments)
6465 1.14 christos {
6466 1.14 christos /* Memory resident segments with non-zero size and RWX
6467 1.14 christos permissions are a security risk, so we generate a warning
6468 1.14 christos here if we are creating any. */
6469 1.14 christos unsigned int i;
6470 1.14 christos
6471 1.14 christos for (i = 0; i < alloc; i++)
6472 1.14 christos {
6473 1.14 christos const Elf_Internal_Phdr * phdr = tdata->phdr + i;
6474 1.14 christos
6475 1.14 christos if (phdr->p_memsz == 0)
6476 1.14 christos continue;
6477 1.14 christos
6478 1.14 christos if (phdr->p_type == PT_TLS && (phdr->p_flags & PF_X))
6479 1.14 christos _bfd_error_handler (_("warning: %pB has a TLS segment"
6480 1.14 christos " with execute permission"),
6481 1.14 christos abfd);
6482 1.14 christos else if (phdr->p_type == PT_LOAD
6483 1.14 christos && ((phdr->p_flags & (PF_R | PF_W | PF_X))
6484 1.14 christos == (PF_R | PF_W | PF_X)))
6485 1.14 christos _bfd_error_handler (_("warning: %pB has a LOAD segment"
6486 1.14 christos " with RWX permissions"),
6487 1.14 christos abfd);
6488 1.14 christos }
6489 1.12 christos }
6490 1.1 christos
6491 1.14 christos if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
6492 1.1 christos || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6493 1.1 christos return false;
6494 1.14 christos }
6495 1.1 christos
6496 1.1 christos return true;
6497 1.14 christos }
6498 1.12 christos
6499 1.12 christos bool
6500 1.1 christos _bfd_elf_init_file_header (bfd *abfd,
6501 1.1 christos struct bfd_link_info *info ATTRIBUTE_UNUSED)
6502 1.1 christos {
6503 1.1 christos Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
6504 1.1 christos struct elf_strtab_hash *shstrtab;
6505 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6506 1.1 christos
6507 1.1 christos i_ehdrp = elf_elfheader (abfd);
6508 1.1 christos
6509 1.14 christos shstrtab = _bfd_elf_strtab_init ();
6510 1.1 christos if (shstrtab == NULL)
6511 1.1 christos return false;
6512 1.1 christos
6513 1.1 christos elf_shstrtab (abfd) = shstrtab;
6514 1.1 christos
6515 1.1 christos i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6516 1.1 christos i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6517 1.1 christos i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6518 1.1 christos i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6519 1.1 christos
6520 1.1 christos i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6521 1.1 christos i_ehdrp->e_ident[EI_DATA] =
6522 1.1 christos bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6523 1.1 christos i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6524 1.1 christos
6525 1.1 christos if ((abfd->flags & DYNAMIC) != 0)
6526 1.1 christos i_ehdrp->e_type = ET_DYN;
6527 1.1 christos else if ((abfd->flags & EXEC_P) != 0)
6528 1.1 christos i_ehdrp->e_type = ET_EXEC;
6529 1.1 christos else if (bfd_get_format (abfd) == bfd_core)
6530 1.1 christos i_ehdrp->e_type = ET_CORE;
6531 1.1 christos else
6532 1.1 christos i_ehdrp->e_type = ET_REL;
6533 1.1 christos
6534 1.1 christos switch (bfd_get_arch (abfd))
6535 1.1 christos {
6536 1.1 christos case bfd_arch_unknown:
6537 1.1 christos i_ehdrp->e_machine = EM_NONE;
6538 1.1 christos break;
6539 1.1 christos
6540 1.1 christos /* There used to be a long list of cases here, each one setting
6541 1.1 christos e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6542 1.1 christos in the corresponding bfd definition. To avoid duplication,
6543 1.1 christos the switch was removed. Machines that need special handling
6544 1.1 christos can generally do it in elf_backend_final_write_processing(),
6545 1.1 christos unless they need the information earlier than the final write.
6546 1.1 christos Such need can generally be supplied by replacing the tests for
6547 1.1 christos e_machine with the conditions used to determine it. */
6548 1.1 christos default:
6549 1.1 christos i_ehdrp->e_machine = bed->elf_machine_code;
6550 1.1 christos }
6551 1.1 christos
6552 1.1 christos i_ehdrp->e_version = bed->s->ev_current;
6553 1.1 christos i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6554 1.1 christos
6555 1.1 christos /* No program header, for now. */
6556 1.1 christos i_ehdrp->e_phoff = 0;
6557 1.1 christos i_ehdrp->e_phentsize = 0;
6558 1.1 christos i_ehdrp->e_phnum = 0;
6559 1.1 christos
6560 1.1 christos /* Each bfd section is section header entry. */
6561 1.1 christos i_ehdrp->e_entry = bfd_get_start_address (abfd);
6562 1.1 christos i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6563 1.14 christos
6564 1.1 christos elf_tdata (abfd)->symtab_hdr.sh_name =
6565 1.14 christos (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
6566 1.1 christos elf_tdata (abfd)->strtab_hdr.sh_name =
6567 1.14 christos (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
6568 1.1 christos elf_tdata (abfd)->shstrtab_hdr.sh_name =
6569 1.5 christos (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
6570 1.1 christos if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
6571 1.14 christos || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
6572 1.1 christos || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
6573 1.14 christos return false;
6574 1.1 christos
6575 1.1 christos return true;
6576 1.12 christos }
6577 1.12 christos
6578 1.12 christos /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6579 1.12 christos
6580 1.12 christos FIXME: We used to have code here to sort the PT_LOAD segments into
6581 1.12 christos ascending order, as per the ELF spec. But this breaks some programs,
6582 1.12 christos including the Linux kernel. But really either the spec should be
6583 1.14 christos changed or the programs updated. */
6584 1.12 christos
6585 1.12 christos bool
6586 1.12 christos _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
6587 1.12 christos {
6588 1.12 christos if (link_info != NULL && bfd_link_pie (link_info))
6589 1.12 christos {
6590 1.12 christos Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
6591 1.12 christos unsigned int num_segments = i_ehdrp->e_phnum;
6592 1.12 christos struct elf_obj_tdata *tdata = elf_tdata (obfd);
6593 1.12 christos Elf_Internal_Phdr *segment = tdata->phdr;
6594 1.12 christos Elf_Internal_Phdr *end_segment = &segment[num_segments];
6595 1.12 christos
6596 1.12 christos /* Find the lowest p_vaddr in PT_LOAD segments. */
6597 1.12 christos bfd_vma p_vaddr = (bfd_vma) -1;
6598 1.12 christos for (; segment < end_segment; segment++)
6599 1.12 christos if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6600 1.12 christos p_vaddr = segment->p_vaddr;
6601 1.12 christos
6602 1.12 christos /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6603 1.12 christos segments is non-zero. */
6604 1.12 christos if (p_vaddr)
6605 1.14 christos i_ehdrp->e_type = ET_EXEC;
6606 1.12 christos }
6607 1.12 christos return true;
6608 1.1 christos }
6609 1.5 christos
6610 1.1 christos /* Assign file positions for all the reloc sections which are not part
6611 1.14 christos of the loadable file image, and the file position of section headers. */
6612 1.6 christos
6613 1.1 christos static bool
6614 1.1 christos _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
6615 1.6 christos {
6616 1.6 christos file_ptr off;
6617 1.5 christos Elf_Internal_Shdr **shdrpp, **end_shdrpp;
6618 1.5 christos Elf_Internal_Shdr *shdrp;
6619 1.1 christos Elf_Internal_Ehdr *i_ehdrp;
6620 1.3 christos const struct elf_backend_data *bed;
6621 1.1 christos
6622 1.6 christos off = elf_next_file_pos (abfd);
6623 1.6 christos
6624 1.6 christos shdrpp = elf_elfsections (abfd);
6625 1.1 christos end_shdrpp = shdrpp + elf_numsections (abfd);
6626 1.6 christos for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
6627 1.6 christos {
6628 1.6 christos shdrp = *shdrpp;
6629 1.6 christos if (shdrp->sh_offset == -1)
6630 1.14 christos {
6631 1.14 christos asection *sec = shdrp->bfd_section;
6632 1.14 christos if (sec == NULL
6633 1.14 christos || shdrp->sh_type == SHT_REL
6634 1.14 christos || shdrp->sh_type == SHT_RELA)
6635 1.14 christos ;
6636 1.14 christos else if (bfd_section_is_ctf (sec))
6637 1.14 christos {
6638 1.14 christos /* Update section size and contents. */
6639 1.14 christos shdrp->sh_size = sec->size;
6640 1.14 christos shdrp->contents = sec->contents;
6641 1.14 christos }
6642 1.14 christos else if (shdrp->sh_name == -1u)
6643 1.14 christos {
6644 1.14 christos const char *name = sec->name;
6645 1.14 christos struct bfd_elf_section_data *d;
6646 1.14 christos
6647 1.14 christos /* Compress DWARF debug sections. */
6648 1.14 christos if (!bfd_compress_section (abfd, sec, shdrp->contents))
6649 1.14 christos return false;
6650 1.14 christos
6651 1.14 christos if (sec->compress_status == COMPRESS_SECTION_DONE
6652 1.14 christos && (abfd->flags & BFD_COMPRESS_GABI) == 0
6653 1.14 christos && name[1] == 'd')
6654 1.14 christos {
6655 1.14 christos /* If section is compressed with zlib-gnu, convert
6656 1.14 christos section name from .debug_* to .zdebug_*. */
6657 1.14 christos char *new_name = bfd_debug_name_to_zdebug (abfd, name);
6658 1.14 christos if (new_name == NULL)
6659 1.14 christos return false;
6660 1.14 christos name = new_name;
6661 1.14 christos }
6662 1.14 christos /* Add section name to section name section. */
6663 1.14 christos shdrp->sh_name
6664 1.14 christos = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
6665 1.14 christos name, false);
6666 1.14 christos d = elf_section_data (sec);
6667 1.14 christos
6668 1.14 christos /* Add reloc section name to section name section. */
6669 1.14 christos if (d->rel.hdr
6670 1.14 christos && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
6671 1.14 christos name, false))
6672 1.14 christos return false;
6673 1.14 christos if (d->rela.hdr
6674 1.14 christos && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
6675 1.14 christos name, true))
6676 1.14 christos return false;
6677 1.14 christos
6678 1.14 christos /* Update section size and contents. */
6679 1.14 christos shdrp->sh_size = sec->size;
6680 1.14 christos shdrp->contents = sec->contents;
6681 1.1 christos sec->contents = NULL;
6682 1.14 christos }
6683 1.6 christos
6684 1.1 christos off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
6685 1.1 christos }
6686 1.6 christos }
6687 1.6 christos
6688 1.6 christos /* Place section name section after DWARF debug sections have been
6689 1.6 christos compressed. */
6690 1.6 christos _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6691 1.14 christos shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6692 1.6 christos shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
6693 1.6 christos off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
6694 1.5 christos
6695 1.5 christos /* Place the section headers. */
6696 1.5 christos i_ehdrp = elf_elfheader (abfd);
6697 1.5 christos bed = get_elf_backend_data (abfd);
6698 1.5 christos off = align_file_position (off, 1 << bed->s->log_file_align);
6699 1.3 christos i_ehdrp->e_shoff = off;
6700 1.6 christos off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
6701 1.14 christos elf_next_file_pos (abfd) = off;
6702 1.1 christos
6703 1.1 christos return true;
6704 1.14 christos }
6705 1.1 christos
6706 1.1 christos bool
6707 1.1 christos _bfd_elf_write_object_contents (bfd *abfd)
6708 1.1 christos {
6709 1.14 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6710 1.1 christos Elf_Internal_Shdr **i_shdrp;
6711 1.3 christos bool failed;
6712 1.1 christos unsigned int count, num_sec;
6713 1.1 christos struct elf_obj_tdata *t;
6714 1.1 christos
6715 1.14 christos if (! abfd->output_has_begun
6716 1.11 christos && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
6717 1.14 christos return false;
6718 1.14 christos /* Do not rewrite ELF data when the BFD has been opened for update.
6719 1.14 christos abfd->output_has_begun was set to TRUE on opening, so creation of
6720 1.14 christos new sections, and modification of existing section sizes was
6721 1.14 christos restricted. This means the ELF header, program headers and
6722 1.14 christos section headers can't have changed. If the contents of any
6723 1.11 christos sections has been modified, then those changes have already been
6724 1.11 christos written to the BFD. */
6725 1.11 christos else if (abfd->direction == both_direction)
6726 1.14 christos {
6727 1.11 christos BFD_ASSERT (abfd->output_has_begun);
6728 1.1 christos return true;
6729 1.1 christos }
6730 1.1 christos
6731 1.14 christos i_shdrp = elf_elfsections (abfd);
6732 1.1 christos
6733 1.1 christos failed = false;
6734 1.14 christos bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6735 1.1 christos if (failed)
6736 1.6 christos return false;
6737 1.14 christos
6738 1.1 christos if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
6739 1.1 christos return false;
6740 1.1 christos
6741 1.1 christos /* After writing the headers, we need to write the sections too... */
6742 1.1 christos num_sec = elf_numsections (abfd);
6743 1.6 christos for (count = 1; count < num_sec; count++)
6744 1.6 christos {
6745 1.6 christos i_shdrp[count]->sh_name
6746 1.1 christos = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6747 1.11 christos i_shdrp[count]->sh_name);
6748 1.14 christos if (bed->elf_backend_section_processing)
6749 1.1 christos if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
6750 1.1 christos return false;
6751 1.1 christos if (i_shdrp[count]->contents)
6752 1.1 christos {
6753 1.1 christos bfd_size_type amt = i_shdrp[count]->sh_size;
6754 1.1 christos
6755 1.14 christos if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
6756 1.1 christos || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
6757 1.1 christos return false;
6758 1.1 christos }
6759 1.1 christos }
6760 1.3 christos
6761 1.1 christos /* Write out the section header names. */
6762 1.3 christos t = elf_tdata (abfd);
6763 1.1 christos if (elf_shstrtab (abfd) != NULL
6764 1.14 christos && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
6765 1.1 christos || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
6766 1.12 christos return false;
6767 1.14 christos
6768 1.1 christos if (!(*bed->elf_backend_final_write_processing) (abfd))
6769 1.1 christos return false;
6770 1.14 christos
6771 1.1 christos if (!bed->s->write_shdrs_and_ehdr (abfd))
6772 1.1 christos return false;
6773 1.14 christos
6774 1.14 christos /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6775 1.14 christos if (t->o->build_id.after_write_object_contents != NULL
6776 1.14 christos && !(*t->o->build_id.after_write_object_contents) (abfd))
6777 1.14 christos return false;
6778 1.14 christos if (t->o->package_metadata.after_write_object_contents != NULL
6779 1.1 christos && !(*t->o->package_metadata.after_write_object_contents) (abfd))
6780 1.14 christos return false;
6781 1.1 christos
6782 1.1 christos return true;
6783 1.14 christos }
6784 1.1 christos
6785 1.1 christos bool
6786 1.1 christos _bfd_elf_write_corefile_contents (bfd *abfd)
6787 1.1 christos {
6788 1.1 christos /* Hopefully this can be done just like an object file. */
6789 1.1 christos return _bfd_elf_write_object_contents (abfd);
6790 1.1 christos }
6791 1.1 christos
6792 1.1 christos /* Given a section, search the header to find them. */
6793 1.1 christos
6794 1.1 christos unsigned int
6795 1.1 christos _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
6796 1.1 christos {
6797 1.1 christos const struct elf_backend_data *bed;
6798 1.1 christos unsigned int sec_index;
6799 1.1 christos
6800 1.1 christos if (elf_section_data (asect) != NULL
6801 1.1 christos && elf_section_data (asect)->this_idx != 0)
6802 1.1 christos return elf_section_data (asect)->this_idx;
6803 1.1 christos
6804 1.1 christos if (bfd_is_abs_section (asect))
6805 1.1 christos sec_index = SHN_ABS;
6806 1.1 christos else if (bfd_is_com_section (asect))
6807 1.1 christos sec_index = SHN_COMMON;
6808 1.1 christos else if (bfd_is_und_section (asect))
6809 1.1 christos sec_index = SHN_UNDEF;
6810 1.1 christos else
6811 1.1 christos sec_index = SHN_BAD;
6812 1.1 christos
6813 1.1 christos bed = get_elf_backend_data (abfd);
6814 1.1 christos if (bed->elf_backend_section_from_bfd_section)
6815 1.1 christos {
6816 1.1 christos int retval = sec_index;
6817 1.1 christos
6818 1.1 christos if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6819 1.1 christos return retval;
6820 1.1 christos }
6821 1.1 christos
6822 1.1 christos if (sec_index == SHN_BAD)
6823 1.1 christos bfd_set_error (bfd_error_nonrepresentable_section);
6824 1.1 christos
6825 1.1 christos return sec_index;
6826 1.1 christos }
6827 1.1 christos
6828 1.1 christos /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6829 1.1 christos on error. */
6830 1.1 christos
6831 1.1 christos int
6832 1.1 christos _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
6833 1.1 christos {
6834 1.1 christos asymbol *asym_ptr = *asym_ptr_ptr;
6835 1.1 christos int idx;
6836 1.1 christos flagword flags = asym_ptr->flags;
6837 1.1 christos
6838 1.1 christos /* When gas creates relocations against local labels, it creates its
6839 1.1 christos own symbol for the section, but does put the symbol into the
6840 1.1 christos symbol chain, so udata is 0. When the linker is generating
6841 1.1 christos relocatable output, this section symbol may be for one of the
6842 1.1 christos input sections rather than the output section. */
6843 1.1 christos if (asym_ptr->udata.i == 0
6844 1.1 christos && (flags & BSF_SECTION_SYM)
6845 1.1 christos && asym_ptr->section)
6846 1.1 christos {
6847 1.1 christos asection *sec;
6848 1.1 christos
6849 1.1 christos sec = asym_ptr->section;
6850 1.1 christos if (sec->owner != abfd && sec->output_section != NULL)
6851 1.14 christos sec = sec->output_section;
6852 1.14 christos if (sec->owner == abfd
6853 1.14 christos && sec->index < elf_num_section_syms (abfd)
6854 1.1 christos && elf_section_syms (abfd)[sec->index] != NULL)
6855 1.1 christos asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
6856 1.1 christos }
6857 1.1 christos
6858 1.1 christos idx = asym_ptr->udata.i;
6859 1.1 christos
6860 1.1 christos if (idx == 0)
6861 1.1 christos {
6862 1.9 christos /* This case can occur when using --strip-symbol on a symbol
6863 1.9 christos which is used in a relocation entry. */
6864 1.11 christos _bfd_error_handler
6865 1.1 christos /* xgettext:c-format */
6866 1.1 christos (_("%pB: symbol `%s' required but not present"),
6867 1.1 christos abfd, bfd_asymbol_name (asym_ptr));
6868 1.1 christos bfd_set_error (bfd_error_no_symbols);
6869 1.1 christos return -1;
6870 1.1 christos }
6871 1.1 christos
6872 1.1 christos #if DEBUG & 4
6873 1.14 christos {
6874 1.14 christos fprintf (stderr,
6875 1.11 christos "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
6876 1.1 christos " flags = 0x%.8x\n",
6877 1.1 christos (long) asym_ptr, asym_ptr->name, idx, flags);
6878 1.1 christos fflush (stderr);
6879 1.1 christos }
6880 1.1 christos #endif
6881 1.1 christos
6882 1.1 christos return idx;
6883 1.14 christos }
6884 1.14 christos
6885 1.14 christos static inline bfd_vma
6886 1.14 christos segment_size (Elf_Internal_Phdr *segment)
6887 1.14 christos {
6888 1.14 christos return (segment->p_memsz > segment->p_filesz
6889 1.14 christos ? segment->p_memsz : segment->p_filesz);
6890 1.14 christos }
6891 1.14 christos
6892 1.14 christos
6893 1.14 christos /* Returns the end address of the segment + 1. */
6894 1.14 christos static inline bfd_vma
6895 1.14 christos segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
6896 1.14 christos {
6897 1.14 christos return start + segment_size (segment);
6898 1.14 christos }
6899 1.14 christos
6900 1.14 christos static inline bfd_size_type
6901 1.14 christos section_size (asection *section, Elf_Internal_Phdr *segment)
6902 1.14 christos {
6903 1.14 christos if ((section->flags & SEC_HAS_CONTENTS) != 0
6904 1.14 christos || (section->flags & SEC_THREAD_LOCAL) == 0
6905 1.14 christos || segment->p_type == PT_TLS)
6906 1.14 christos return section->size;
6907 1.14 christos return 0;
6908 1.14 christos }
6909 1.14 christos
6910 1.14 christos /* Returns TRUE if the given section is contained within the given
6911 1.14 christos segment. LMA addresses are compared against PADDR when
6912 1.14 christos bed->want_p_paddr_set_to_zero is false, VMA against VADDR when true. */
6913 1.14 christos static bool
6914 1.14 christos is_contained_by (asection *section, Elf_Internal_Phdr *segment,
6915 1.14 christos bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
6916 1.14 christos const struct elf_backend_data *bed)
6917 1.14 christos {
6918 1.14 christos bfd_vma seg_addr = !bed->want_p_paddr_set_to_zero ? paddr : vaddr;
6919 1.14 christos bfd_vma addr = !bed->want_p_paddr_set_to_zero ? section->lma : section->vma;
6920 1.14 christos bfd_vma octet;
6921 1.14 christos if (_bfd_mul_overflow (addr, opb, &octet))
6922 1.14 christos return false;
6923 1.14 christos /* The third and fourth lines below are testing that the section end
6924 1.14 christos address is within the segment. It's written this way to avoid
6925 1.14 christos overflow. Add seg_addr + section_size to both sides of the
6926 1.14 christos inequality to make it obvious. */
6927 1.14 christos return (octet >= seg_addr
6928 1.14 christos && segment_size (segment) >= section_size (section, segment)
6929 1.14 christos && (octet - seg_addr
6930 1.14 christos <= segment_size (segment) - section_size (section, segment)));
6931 1.14 christos }
6932 1.14 christos
6933 1.14 christos /* Handle PT_NOTE segment. */
6934 1.14 christos static bool
6935 1.14 christos is_note (asection *s, Elf_Internal_Phdr *p)
6936 1.14 christos {
6937 1.14 christos return (p->p_type == PT_NOTE
6938 1.14 christos && elf_section_type (s) == SHT_NOTE
6939 1.14 christos && (ufile_ptr) s->filepos >= p->p_offset
6940 1.14 christos && p->p_filesz >= s->size
6941 1.14 christos && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
6942 1.1 christos }
6943 1.1 christos
6944 1.14 christos /* Rewrite program header information. */
6945 1.14 christos
6946 1.1 christos static bool
6947 1.1 christos rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
6948 1.1 christos {
6949 1.1 christos Elf_Internal_Ehdr *iehdr;
6950 1.1 christos struct elf_segment_map *map;
6951 1.1 christos struct elf_segment_map *map_first;
6952 1.1 christos struct elf_segment_map **pointer_to_map;
6953 1.1 christos Elf_Internal_Phdr *segment;
6954 1.1 christos asection *section;
6955 1.14 christos unsigned int i;
6956 1.14 christos unsigned int num_segments;
6957 1.1 christos bool phdr_included = false;
6958 1.1 christos bool p_paddr_valid;
6959 1.1 christos struct elf_segment_map *phdr_adjust_seg = NULL;
6960 1.12 christos unsigned int phdr_adjust_num = 0;
6961 1.1 christos const struct elf_backend_data *bed;
6962 1.1 christos unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
6963 1.1 christos
6964 1.1 christos bed = get_elf_backend_data (ibfd);
6965 1.1 christos iehdr = elf_elfheader (ibfd);
6966 1.1 christos
6967 1.1 christos map_first = NULL;
6968 1.1 christos pointer_to_map = &map_first;
6969 1.1 christos
6970 1.1 christos num_segments = elf_elfheader (ibfd)->e_phnum;
6971 1.1 christos
6972 1.1 christos /* The complicated case when p_vaddr is 0 is to handle the Solaris
6973 1.1 christos linker, which generates a PT_INTERP section with p_vaddr and
6974 1.1 christos p_memsz set to 0. */
6975 1.1 christos #define IS_SOLARIS_PT_INTERP(p, s) \
6976 1.1 christos (p->p_vaddr == 0 \
6977 1.1 christos && p->p_paddr == 0 \
6978 1.1 christos && p->p_memsz == 0 \
6979 1.1 christos && p->p_filesz > 0 \
6980 1.1 christos && (s->flags & SEC_HAS_CONTENTS) != 0 \
6981 1.1 christos && s->size > 0 \
6982 1.1 christos && (bfd_vma) s->filepos >= p->p_offset \
6983 1.1 christos && ((bfd_vma) s->filepos + s->size \
6984 1.1 christos <= p->p_offset + p->p_filesz))
6985 1.1 christos
6986 1.1 christos /* Decide if the given section should be included in the given segment.
6987 1.1 christos A section will be included if:
6988 1.1 christos 1. It is within the address space of the segment -- we use the LMA
6989 1.3 christos if that is set for the segment and the VMA otherwise,
6990 1.1 christos 2. It is an allocated section or a NOTE section in a PT_NOTE
6991 1.1 christos segment.
6992 1.1 christos 3. There is an output section associated with it,
6993 1.1 christos 4. The section has not already been allocated to a previous segment.
6994 1.1 christos 5. PT_GNU_STACK segments do not include any sections.
6995 1.1 christos 6. PT_TLS segment includes only SHF_TLS sections.
6996 1.1 christos 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6997 1.12 christos 8. PT_DYNAMIC should not contain empty sections at the beginning
6998 1.14 christos (with the possible exception of .dynamic). */
6999 1.14 christos #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
7000 1.1 christos (((is_contained_by (section, segment, segment->p_paddr, \
7001 1.14 christos segment->p_vaddr, opb, bed) \
7002 1.1 christos && (section->flags & SEC_ALLOC) != 0) \
7003 1.1 christos || is_note (section, segment)) \
7004 1.1 christos && segment->p_type != PT_GNU_STACK \
7005 1.1 christos && (segment->p_type != PT_TLS \
7006 1.1 christos || (section->flags & SEC_THREAD_LOCAL)) \
7007 1.1 christos && (segment->p_type == PT_LOAD \
7008 1.1 christos || segment->p_type == PT_TLS \
7009 1.14 christos || (section->flags & SEC_THREAD_LOCAL) == 0) \
7010 1.1 christos && (segment->p_type != PT_DYNAMIC \
7011 1.12 christos || section_size (section, segment) > 0 \
7012 1.12 christos || (segment->p_paddr \
7013 1.12 christos ? segment->p_paddr != section->lma * (opb) \
7014 1.11 christos : segment->p_vaddr != section->vma * (opb)) \
7015 1.1 christos || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
7016 1.1 christos && (segment->p_type != PT_LOAD || !section->segment_mark))
7017 1.1 christos
7018 1.12 christos /* If the output section of a section in the input segment is NULL,
7019 1.12 christos it is removed from the corresponding output segment. */
7020 1.1 christos #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
7021 1.1 christos (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
7022 1.1 christos && section->output_section != NULL)
7023 1.1 christos
7024 1.14 christos /* Returns TRUE iff seg1 starts after the end of seg2. */
7025 1.1 christos #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
7026 1.1 christos (seg1->field >= segment_end (seg2, seg2->field))
7027 1.1 christos
7028 1.1 christos /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7029 1.1 christos their VMA address ranges and their LMA address ranges overlap.
7030 1.1 christos It is possible to have overlapping VMA ranges without overlapping LMA
7031 1.1 christos ranges. RedBoot images for example can have both .data and .bss mapped
7032 1.1 christos to the same VMA range, but with the .data section mapped to a different
7033 1.1 christos LMA. */
7034 1.1 christos #define SEGMENT_OVERLAPS(seg1, seg2) \
7035 1.1 christos ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
7036 1.1 christos || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
7037 1.1 christos && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
7038 1.14 christos || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7039 1.1 christos
7040 1.14 christos /* Initialise the segment mark field, and discard stupid alignment. */
7041 1.14 christos for (section = ibfd->sections; section != NULL; section = section->next)
7042 1.14 christos {
7043 1.14 christos asection *o = section->output_section;
7044 1.14 christos if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7045 1.14 christos o->alignment_power = 0;
7046 1.1 christos section->segment_mark = false;
7047 1.1 christos }
7048 1.1 christos
7049 1.1 christos /* The Solaris linker creates program headers in which all the
7050 1.1 christos p_paddr fields are zero. When we try to objcopy or strip such a
7051 1.14 christos file, we get confused. Check for this case, and if we find it
7052 1.1 christos don't set the p_paddr_valid fields. */
7053 1.1 christos p_paddr_valid = false;
7054 1.1 christos for (i = 0, segment = elf_tdata (ibfd)->phdr;
7055 1.1 christos i < num_segments;
7056 1.1 christos i++, segment++)
7057 1.14 christos if (segment->p_paddr != 0)
7058 1.1 christos {
7059 1.1 christos p_paddr_valid = true;
7060 1.1 christos break;
7061 1.1 christos }
7062 1.1 christos
7063 1.1 christos /* Scan through the segments specified in the program header
7064 1.1 christos of the input BFD. For this first scan we look for overlaps
7065 1.1 christos in the loadable segments. These can be created by weird
7066 1.1 christos parameters to objcopy. Also, fix some solaris weirdness. */
7067 1.1 christos for (i = 0, segment = elf_tdata (ibfd)->phdr;
7068 1.1 christos i < num_segments;
7069 1.1 christos i++, segment++)
7070 1.1 christos {
7071 1.1 christos unsigned int j;
7072 1.1 christos Elf_Internal_Phdr *segment2;
7073 1.1 christos
7074 1.1 christos if (segment->p_type == PT_INTERP)
7075 1.1 christos for (section = ibfd->sections; section; section = section->next)
7076 1.1 christos if (IS_SOLARIS_PT_INTERP (segment, section))
7077 1.1 christos {
7078 1.12 christos /* Mininal change so that the normal section to segment
7079 1.1 christos assignment code will work. */
7080 1.1 christos segment->p_vaddr = section->vma * opb;
7081 1.1 christos break;
7082 1.1 christos }
7083 1.1 christos
7084 1.1 christos if (segment->p_type != PT_LOAD)
7085 1.1 christos {
7086 1.1 christos /* Remove PT_GNU_RELRO segment. */
7087 1.1 christos if (segment->p_type == PT_GNU_RELRO)
7088 1.1 christos segment->p_type = PT_NULL;
7089 1.1 christos continue;
7090 1.1 christos }
7091 1.1 christos
7092 1.1 christos /* Determine if this segment overlaps any previous segments. */
7093 1.1 christos for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
7094 1.1 christos {
7095 1.1 christos bfd_signed_vma extra_length;
7096 1.1 christos
7097 1.1 christos if (segment2->p_type != PT_LOAD
7098 1.1 christos || !SEGMENT_OVERLAPS (segment, segment2))
7099 1.1 christos continue;
7100 1.1 christos
7101 1.1 christos /* Merge the two segments together. */
7102 1.1 christos if (segment2->p_vaddr < segment->p_vaddr)
7103 1.1 christos {
7104 1.14 christos /* Extend SEGMENT2 to include SEGMENT and then delete
7105 1.14 christos SEGMENT. */
7106 1.1 christos extra_length = (segment_end (segment, segment->p_vaddr)
7107 1.1 christos - segment_end (segment2, segment2->p_vaddr));
7108 1.1 christos
7109 1.1 christos if (extra_length > 0)
7110 1.1 christos {
7111 1.1 christos segment2->p_memsz += extra_length;
7112 1.1 christos segment2->p_filesz += extra_length;
7113 1.1 christos }
7114 1.1 christos
7115 1.1 christos segment->p_type = PT_NULL;
7116 1.1 christos
7117 1.1 christos /* Since we have deleted P we must restart the outer loop. */
7118 1.1 christos i = 0;
7119 1.1 christos segment = elf_tdata (ibfd)->phdr;
7120 1.1 christos break;
7121 1.1 christos }
7122 1.1 christos else
7123 1.1 christos {
7124 1.14 christos /* Extend SEGMENT to include SEGMENT2 and then delete
7125 1.14 christos SEGMENT2. */
7126 1.1 christos extra_length = (segment_end (segment2, segment2->p_vaddr)
7127 1.1 christos - segment_end (segment, segment->p_vaddr));
7128 1.1 christos
7129 1.1 christos if (extra_length > 0)
7130 1.1 christos {
7131 1.1 christos segment->p_memsz += extra_length;
7132 1.1 christos segment->p_filesz += extra_length;
7133 1.1 christos }
7134 1.1 christos
7135 1.1 christos segment2->p_type = PT_NULL;
7136 1.1 christos }
7137 1.1 christos }
7138 1.1 christos }
7139 1.1 christos
7140 1.1 christos /* The second scan attempts to assign sections to segments. */
7141 1.1 christos for (i = 0, segment = elf_tdata (ibfd)->phdr;
7142 1.1 christos i < num_segments;
7143 1.1 christos i++, segment++)
7144 1.1 christos {
7145 1.1 christos unsigned int section_count;
7146 1.1 christos asection **sections;
7147 1.11 christos asection *output_section;
7148 1.11 christos unsigned int isec;
7149 1.1 christos asection *matching_lma;
7150 1.12 christos asection *suggested_lma;
7151 1.1 christos unsigned int j;
7152 1.1 christos size_t amt;
7153 1.1 christos asection *first_section;
7154 1.1 christos
7155 1.1 christos if (segment->p_type == PT_NULL)
7156 1.1 christos continue;
7157 1.1 christos
7158 1.1 christos first_section = NULL;
7159 1.1 christos /* Compute how many sections might be placed into this segment. */
7160 1.1 christos for (section = ibfd->sections, section_count = 0;
7161 1.1 christos section != NULL;
7162 1.1 christos section = section->next)
7163 1.1 christos {
7164 1.12 christos /* Find the first section in the input segment, which may be
7165 1.1 christos removed from the corresponding output segment. */
7166 1.1 christos if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
7167 1.1 christos {
7168 1.1 christos if (first_section == NULL)
7169 1.1 christos first_section = section;
7170 1.1 christos if (section->output_section != NULL)
7171 1.1 christos ++section_count;
7172 1.1 christos }
7173 1.1 christos }
7174 1.1 christos
7175 1.11 christos /* Allocate a segment map big enough to contain
7176 1.12 christos all of the sections we have selected. */
7177 1.1 christos amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7178 1.1 christos amt += section_count * sizeof (asection *);
7179 1.14 christos map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7180 1.1 christos if (map == NULL)
7181 1.1 christos return false;
7182 1.1 christos
7183 1.1 christos /* Initialise the fields of the segment map. Default to
7184 1.1 christos using the physical address of the segment in the input BFD. */
7185 1.1 christos map->next = NULL;
7186 1.1 christos map->p_type = segment->p_type;
7187 1.1 christos map->p_flags = segment->p_flags;
7188 1.14 christos map->p_flags_valid = 1;
7189 1.14 christos
7190 1.14 christos if (map->p_type == PT_LOAD
7191 1.14 christos && (ibfd->flags & D_PAGED) != 0
7192 1.14 christos && maxpagesize > 1
7193 1.14 christos && segment->p_align > 1)
7194 1.14 christos {
7195 1.14 christos map->p_align = segment->p_align;
7196 1.14 christos if (segment->p_align > maxpagesize)
7197 1.14 christos map->p_align = maxpagesize;
7198 1.14 christos map->p_align_valid = 1;
7199 1.1 christos }
7200 1.1 christos
7201 1.1 christos /* If the first section in the input segment is removed, there is
7202 1.1 christos no need to preserve segment physical address in the corresponding
7203 1.1 christos output segment. */
7204 1.1 christos if (!first_section || first_section->output_section != NULL)
7205 1.1 christos {
7206 1.1 christos map->p_paddr = segment->p_paddr;
7207 1.1 christos map->p_paddr_valid = p_paddr_valid;
7208 1.1 christos }
7209 1.1 christos
7210 1.1 christos /* Determine if this segment contains the ELF file header
7211 1.1 christos and if it contains the program headers themselves. */
7212 1.1 christos map->includes_filehdr = (segment->p_offset == 0
7213 1.1 christos && segment->p_filesz >= iehdr->e_ehsize);
7214 1.1 christos map->includes_phdrs = 0;
7215 1.1 christos
7216 1.1 christos if (!phdr_included || segment->p_type != PT_LOAD)
7217 1.1 christos {
7218 1.1 christos map->includes_phdrs =
7219 1.1 christos (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7220 1.1 christos && (segment->p_offset + segment->p_filesz
7221 1.1 christos >= ((bfd_vma) iehdr->e_phoff
7222 1.1 christos + iehdr->e_phnum * iehdr->e_phentsize)));
7223 1.14 christos
7224 1.1 christos if (segment->p_type == PT_LOAD && map->includes_phdrs)
7225 1.1 christos phdr_included = true;
7226 1.1 christos }
7227 1.1 christos
7228 1.1 christos if (section_count == 0)
7229 1.1 christos {
7230 1.1 christos /* Special segments, such as the PT_PHDR segment, may contain
7231 1.11 christos no sections, but ordinary, loadable segments should contain
7232 1.9 christos something. They are allowed by the ELF spec however, so only
7233 1.9 christos a warning is produced.
7234 1.9 christos There is however the valid use case of embedded systems which
7235 1.9 christos have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7236 1.9 christos flash memory with zeros. No warning is shown for that case. */
7237 1.9 christos if (segment->p_type == PT_LOAD
7238 1.11 christos && (segment->p_filesz > 0 || segment->p_memsz == 0))
7239 1.11 christos /* xgettext:c-format */
7240 1.11 christos _bfd_error_handler
7241 1.11 christos (_("%pB: warning: empty loadable segment detected"
7242 1.1 christos " at vaddr=%#" PRIx64 ", is this intentional?"),
7243 1.12 christos ibfd, (uint64_t) segment->p_vaddr);
7244 1.1 christos
7245 1.1 christos map->p_vaddr_offset = segment->p_vaddr / opb;
7246 1.1 christos map->count = 0;
7247 1.1 christos *pointer_to_map = map;
7248 1.1 christos pointer_to_map = &map->next;
7249 1.1 christos
7250 1.1 christos continue;
7251 1.1 christos }
7252 1.1 christos
7253 1.1 christos /* Now scan the sections in the input BFD again and attempt
7254 1.1 christos to add their corresponding output sections to the segment map.
7255 1.1 christos The problem here is how to handle an output section which has
7256 1.1 christos been moved (ie had its LMA changed). There are four possibilities:
7257 1.1 christos
7258 1.1 christos 1. None of the sections have been moved.
7259 1.1 christos In this case we can continue to use the segment LMA from the
7260 1.1 christos input BFD.
7261 1.1 christos
7262 1.1 christos 2. All of the sections have been moved by the same amount.
7263 1.1 christos In this case we can change the segment's LMA to match the LMA
7264 1.1 christos of the first section.
7265 1.1 christos
7266 1.1 christos 3. Some of the sections have been moved, others have not.
7267 1.1 christos In this case those sections which have not been moved can be
7268 1.1 christos placed in the current segment which will have to have its size,
7269 1.1 christos and possibly its LMA changed, and a new segment or segments will
7270 1.1 christos have to be created to contain the other sections.
7271 1.1 christos
7272 1.1 christos 4. The sections have been moved, but not by the same amount.
7273 1.1 christos In this case we can change the segment's LMA to match the LMA
7274 1.1 christos of the first section and we will have to create a new segment
7275 1.1 christos or segments to contain the other sections.
7276 1.1 christos
7277 1.1 christos In order to save time, we allocate an array to hold the section
7278 1.1 christos pointers that we are interested in. As these sections get assigned
7279 1.12 christos to a segment, they are removed from this array. */
7280 1.12 christos
7281 1.1 christos amt = section_count * sizeof (asection *);
7282 1.14 christos sections = (asection **) bfd_malloc (amt);
7283 1.1 christos if (sections == NULL)
7284 1.1 christos return false;
7285 1.1 christos
7286 1.1 christos /* Step One: Scan for segment vs section LMA conflicts.
7287 1.1 christos Also add the sections to the section array allocated above.
7288 1.1 christos Also add the sections to the current segment. In the common
7289 1.1 christos case, where the sections have not been moved, this means that
7290 1.1 christos we have completely filled the segment, and there is nothing
7291 1.11 christos more to do. */
7292 1.11 christos isec = 0;
7293 1.1 christos matching_lma = NULL;
7294 1.8 christos suggested_lma = NULL;
7295 1.1 christos
7296 1.1 christos for (section = first_section, j = 0;
7297 1.1 christos section != NULL;
7298 1.12 christos section = section->next)
7299 1.1 christos {
7300 1.1 christos if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
7301 1.1 christos {
7302 1.1 christos output_section = section->output_section;
7303 1.1 christos
7304 1.1 christos sections[j++] = section;
7305 1.1 christos
7306 1.1 christos /* The Solaris native linker always sets p_paddr to 0.
7307 1.1 christos We try to catch that case here, and set it to the
7308 1.1 christos correct value. Note - some backends require that
7309 1.1 christos p_paddr be left as zero. */
7310 1.1 christos if (!p_paddr_valid
7311 1.1 christos && segment->p_vaddr != 0
7312 1.1 christos && !bed->want_p_paddr_set_to_zero
7313 1.11 christos && isec == 0
7314 1.11 christos && output_section->lma != 0
7315 1.11 christos && (align_power (segment->p_vaddr
7316 1.11 christos + (map->includes_filehdr
7317 1.11 christos ? iehdr->e_ehsize : 0)
7318 1.11 christos + (map->includes_phdrs
7319 1.12 christos ? iehdr->e_phnum * iehdr->e_phentsize
7320 1.12 christos : 0),
7321 1.1 christos output_section->alignment_power * opb)
7322 1.1 christos == (output_section->vma * opb)))
7323 1.1 christos map->p_paddr = segment->p_vaddr;
7324 1.1 christos
7325 1.14 christos /* Match up the physical address of the segment with the
7326 1.14 christos LMA address of the output section. */
7327 1.14 christos if (is_contained_by (output_section, segment, map->p_paddr,
7328 1.1 christos map->p_paddr + map->p_vaddr_offset, opb, bed)
7329 1.11 christos || is_note (section, segment))
7330 1.11 christos {
7331 1.11 christos if (matching_lma == NULL
7332 1.1 christos || output_section->lma < matching_lma->lma)
7333 1.1 christos matching_lma = output_section;
7334 1.1 christos
7335 1.1 christos /* We assume that if the section fits within the segment
7336 1.1 christos then it does not overlap any other section within that
7337 1.1 christos segment. */
7338 1.11 christos map->sections[isec++] = output_section;
7339 1.11 christos }
7340 1.1 christos else if (suggested_lma == NULL)
7341 1.1 christos suggested_lma = output_section;
7342 1.1 christos
7343 1.1 christos if (j == section_count)
7344 1.1 christos break;
7345 1.1 christos }
7346 1.1 christos }
7347 1.1 christos
7348 1.1 christos BFD_ASSERT (j == section_count);
7349 1.1 christos
7350 1.1 christos /* Step Two: Adjust the physical address of the current segment,
7351 1.1 christos if necessary. */
7352 1.1 christos if (isec == section_count)
7353 1.1 christos {
7354 1.1 christos /* All of the sections fitted within the segment as currently
7355 1.1 christos specified. This is the default case. Add the segment to
7356 1.1 christos the list of built segments and carry on to process the next
7357 1.1 christos program header in the input BFD. */
7358 1.1 christos map->count = section_count;
7359 1.1 christos *pointer_to_map = map;
7360 1.1 christos pointer_to_map = &map->next;
7361 1.12 christos
7362 1.12 christos if (p_paddr_valid
7363 1.12 christos && !bed->want_p_paddr_set_to_zero)
7364 1.12 christos {
7365 1.12 christos bfd_vma hdr_size = 0;
7366 1.12 christos if (map->includes_filehdr)
7367 1.12 christos hdr_size = iehdr->e_ehsize;
7368 1.12 christos if (map->includes_phdrs)
7369 1.12 christos hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7370 1.12 christos
7371 1.12 christos /* Account for padding before the first section in the
7372 1.12 christos segment. */
7373 1.12 christos map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7374 1.1 christos - matching_lma->lma);
7375 1.1 christos }
7376 1.1 christos
7377 1.1 christos free (sections);
7378 1.1 christos continue;
7379 1.1 christos }
7380 1.11 christos else
7381 1.11 christos {
7382 1.11 christos /* Change the current segment's physical address to match
7383 1.11 christos the LMA of the first section that fitted, or if no
7384 1.11 christos section fitted, the first section. */
7385 1.11 christos if (matching_lma == NULL)
7386 1.12 christos matching_lma = suggested_lma;
7387 1.1 christos
7388 1.1 christos map->p_paddr = matching_lma->lma * opb;
7389 1.1 christos
7390 1.11 christos /* Offset the segment physical address from the lma
7391 1.1 christos to allow for space taken up by elf headers. */
7392 1.11 christos if (map->includes_phdrs)
7393 1.11 christos {
7394 1.11 christos map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7395 1.11 christos
7396 1.11 christos /* iehdr->e_phnum is just an estimate of the number
7397 1.11 christos of program headers that we will need. Make a note
7398 1.11 christos here of the number we used and the segment we chose
7399 1.11 christos to hold these headers, so that we can adjust the
7400 1.11 christos offset when we know the correct value. */
7401 1.1 christos phdr_adjust_num = iehdr->e_phnum;
7402 1.1 christos phdr_adjust_seg = map;
7403 1.11 christos }
7404 1.1 christos
7405 1.11 christos if (map->includes_filehdr)
7406 1.11 christos {
7407 1.11 christos bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7408 1.11 christos map->p_paddr -= iehdr->e_ehsize;
7409 1.11 christos /* We've subtracted off the size of headers from the
7410 1.11 christos first section lma, but there may have been some
7411 1.11 christos alignment padding before that section too. Try to
7412 1.11 christos account for that by adjusting the segment lma down to
7413 1.11 christos the same alignment. */
7414 1.12 christos if (segment->p_align != 0 && segment->p_align < align)
7415 1.1 christos align = segment->p_align;
7416 1.1 christos map->p_paddr &= -(align * opb);
7417 1.1 christos }
7418 1.1 christos }
7419 1.1 christos
7420 1.1 christos /* Step Three: Loop over the sections again, this time assigning
7421 1.1 christos those that fit to the current segment and removing them from the
7422 1.1 christos sections array; but making sure not to leave large gaps. Once all
7423 1.1 christos possible sections have been assigned to the current segment it is
7424 1.1 christos added to the list of built segments and if sections still remain
7425 1.1 christos to be assigned, a new segment is constructed before repeating
7426 1.1 christos the loop. */
7427 1.1 christos isec = 0;
7428 1.1 christos do
7429 1.11 christos {
7430 1.1 christos map->count = 0;
7431 1.1 christos suggested_lma = NULL;
7432 1.1 christos
7433 1.1 christos /* Fill the current segment with sections that fit. */
7434 1.1 christos for (j = 0; j < section_count; j++)
7435 1.1 christos {
7436 1.1 christos section = sections[j];
7437 1.1 christos
7438 1.1 christos if (section == NULL)
7439 1.1 christos continue;
7440 1.1 christos
7441 1.1 christos output_section = section->output_section;
7442 1.1 christos
7443 1.14 christos BFD_ASSERT (output_section != NULL);
7444 1.14 christos
7445 1.14 christos if (is_contained_by (output_section, segment, map->p_paddr,
7446 1.1 christos map->p_paddr + map->p_vaddr_offset, opb, bed)
7447 1.1 christos || is_note (section, segment))
7448 1.1 christos {
7449 1.1 christos if (map->count == 0)
7450 1.1 christos {
7451 1.1 christos /* If the first section in a segment does not start at
7452 1.11 christos the beginning of the segment, then something is
7453 1.11 christos wrong. */
7454 1.11 christos if (align_power (map->p_paddr
7455 1.11 christos + (map->includes_filehdr
7456 1.11 christos ? iehdr->e_ehsize : 0)
7457 1.11 christos + (map->includes_phdrs
7458 1.12 christos ? iehdr->e_phnum * iehdr->e_phentsize
7459 1.12 christos : 0),
7460 1.12 christos output_section->alignment_power * opb)
7461 1.1 christos != output_section->lma * opb)
7462 1.1 christos goto sorry;
7463 1.1 christos }
7464 1.1 christos else
7465 1.1 christos {
7466 1.1 christos asection *prev_sec;
7467 1.1 christos
7468 1.1 christos prev_sec = map->sections[map->count - 1];
7469 1.1 christos
7470 1.1 christos /* If the gap between the end of the previous section
7471 1.1 christos and the start of this section is more than
7472 1.1 christos maxpagesize then we need to start a new segment. */
7473 1.1 christos if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
7474 1.1 christos maxpagesize)
7475 1.1 christos < BFD_ALIGN (output_section->lma, maxpagesize))
7476 1.1 christos || (prev_sec->lma + prev_sec->size
7477 1.11 christos > output_section->lma))
7478 1.11 christos {
7479 1.1 christos if (suggested_lma == NULL)
7480 1.1 christos suggested_lma = output_section;
7481 1.1 christos
7482 1.1 christos continue;
7483 1.1 christos }
7484 1.1 christos }
7485 1.1 christos
7486 1.1 christos map->sections[map->count++] = output_section;
7487 1.11 christos ++isec;
7488 1.14 christos sections[j] = NULL;
7489 1.1 christos if (segment->p_type == PT_LOAD)
7490 1.11 christos section->segment_mark = true;
7491 1.11 christos }
7492 1.1 christos else if (suggested_lma == NULL)
7493 1.1 christos suggested_lma = output_section;
7494 1.11 christos }
7495 1.11 christos
7496 1.12 christos /* PR 23932. A corrupt input file may contain sections that cannot
7497 1.12 christos be assigned to any segment - because for example they have a
7498 1.12 christos negative size - or segments that do not contain any sections.
7499 1.1 christos But there are also valid reasons why a segment can be empty.
7500 1.1 christos So allow a count of zero. */
7501 1.1 christos
7502 1.1 christos /* Add the current segment to the list of built segments. */
7503 1.1 christos *pointer_to_map = map;
7504 1.1 christos pointer_to_map = &map->next;
7505 1.1 christos
7506 1.1 christos if (isec < section_count)
7507 1.1 christos {
7508 1.1 christos /* We still have not allocated all of the sections to
7509 1.11 christos segments. Create a new segment here, initialise it
7510 1.12 christos and carry on looping. */
7511 1.3 christos amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7512 1.1 christos amt += section_count * sizeof (asection *);
7513 1.1 christos map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7514 1.1 christos if (map == NULL)
7515 1.14 christos {
7516 1.1 christos free (sections);
7517 1.1 christos return false;
7518 1.1 christos }
7519 1.1 christos
7520 1.1 christos /* Initialise the fields of the segment map. Set the physical
7521 1.1 christos physical address to the LMA of the first section that has
7522 1.1 christos not yet been assigned. */
7523 1.1 christos map->next = NULL;
7524 1.1 christos map->p_type = segment->p_type;
7525 1.12 christos map->p_flags = segment->p_flags;
7526 1.1 christos map->p_flags_valid = 1;
7527 1.1 christos map->p_paddr = suggested_lma->lma * opb;
7528 1.1 christos map->p_paddr_valid = p_paddr_valid;
7529 1.1 christos map->includes_filehdr = 0;
7530 1.12 christos map->includes_phdrs = 0;
7531 1.12 christos }
7532 1.12 christos
7533 1.12 christos continue;
7534 1.12 christos sorry:
7535 1.14 christos bfd_set_error (bfd_error_sorry);
7536 1.1 christos free (sections);
7537 1.1 christos return false;
7538 1.1 christos }
7539 1.1 christos while (isec < section_count);
7540 1.1 christos
7541 1.1 christos free (sections);
7542 1.3 christos }
7543 1.1 christos
7544 1.1 christos elf_seg_map (obfd) = map_first;
7545 1.1 christos
7546 1.1 christos /* If we had to estimate the number of program headers that were
7547 1.1 christos going to be needed, then check our estimate now and adjust
7548 1.1 christos the offset if necessary. */
7549 1.1 christos if (phdr_adjust_seg != NULL)
7550 1.1 christos {
7551 1.1 christos unsigned int count;
7552 1.1 christos
7553 1.1 christos for (count = 0, map = map_first; map != NULL; map = map->next)
7554 1.1 christos count++;
7555 1.1 christos
7556 1.1 christos if (count > phdr_adjust_num)
7557 1.11 christos phdr_adjust_seg->p_paddr
7558 1.11 christos -= (count - phdr_adjust_num) * iehdr->e_phentsize;
7559 1.11 christos
7560 1.11 christos for (map = map_first; map != NULL; map = map->next)
7561 1.11 christos if (map->p_type == PT_PHDR)
7562 1.11 christos {
7563 1.11 christos bfd_vma adjust
7564 1.11 christos = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7565 1.11 christos map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7566 1.1 christos break;
7567 1.1 christos }
7568 1.1 christos }
7569 1.1 christos
7570 1.1 christos #undef IS_SOLARIS_PT_INTERP
7571 1.1 christos #undef IS_SECTION_IN_INPUT_SEGMENT
7572 1.1 christos #undef INCLUDE_SECTION_IN_SEGMENT
7573 1.14 christos #undef SEGMENT_AFTER_SEGMENT
7574 1.14 christos #undef SEGMENT_OVERLAPS
7575 1.14 christos return true;
7576 1.14 christos }
7577 1.14 christos
7578 1.14 christos /* Return true if p_align in the ELF program header in ABFD is valid. */
7579 1.14 christos
7580 1.14 christos static bool
7581 1.14 christos elf_is_p_align_valid (bfd *abfd)
7582 1.14 christos {
7583 1.14 christos unsigned int i;
7584 1.14 christos Elf_Internal_Phdr *segment;
7585 1.14 christos unsigned int num_segments;
7586 1.14 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7587 1.14 christos bfd_size_type maxpagesize = bed->maxpagesize;
7588 1.14 christos bfd_size_type p_align = bed->p_align;
7589 1.14 christos
7590 1.14 christos /* Return true if the default p_align value isn't set or the maximum
7591 1.14 christos page size is the same as the minimum page size. */
7592 1.14 christos if (p_align == 0 || maxpagesize == bed->minpagesize)
7593 1.14 christos return true;
7594 1.14 christos
7595 1.14 christos /* When the default p_align value is set, p_align may be set to the
7596 1.14 christos default p_align value while segments are aligned to the maximum
7597 1.14 christos page size. In this case, the input p_align will be ignored and
7598 1.14 christos the maximum page size will be used to align the output segments. */
7599 1.14 christos segment = elf_tdata (abfd)->phdr;
7600 1.14 christos num_segments = elf_elfheader (abfd)->e_phnum;
7601 1.14 christos for (i = 0; i < num_segments; i++, segment++)
7602 1.14 christos if (segment->p_type == PT_LOAD
7603 1.14 christos && (segment->p_align != p_align
7604 1.14 christos || vma_page_aligned_bias (segment->p_vaddr,
7605 1.14 christos segment->p_offset,
7606 1.14 christos maxpagesize) != 0))
7607 1.14 christos return true;
7608 1.1 christos
7609 1.1 christos return false;
7610 1.1 christos }
7611 1.1 christos
7612 1.14 christos /* Copy ELF program header information. */
7613 1.1 christos
7614 1.1 christos static bool
7615 1.1 christos copy_elf_program_header (bfd *ibfd, bfd *obfd)
7616 1.1 christos {
7617 1.1 christos Elf_Internal_Ehdr *iehdr;
7618 1.1 christos struct elf_segment_map *map;
7619 1.1 christos struct elf_segment_map *map_first;
7620 1.1 christos struct elf_segment_map **pointer_to_map;
7621 1.1 christos Elf_Internal_Phdr *segment;
7622 1.14 christos unsigned int i;
7623 1.14 christos unsigned int num_segments;
7624 1.14 christos bool phdr_included = false;
7625 1.12 christos bool p_paddr_valid;
7626 1.1 christos bool p_palign_valid;
7627 1.1 christos unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7628 1.1 christos
7629 1.1 christos iehdr = elf_elfheader (ibfd);
7630 1.1 christos
7631 1.1 christos map_first = NULL;
7632 1.1 christos pointer_to_map = &map_first;
7633 1.1 christos
7634 1.14 christos /* If all the segment p_paddr fields are zero, don't set
7635 1.1 christos map->p_paddr_valid. */
7636 1.1 christos p_paddr_valid = false;
7637 1.1 christos num_segments = elf_elfheader (ibfd)->e_phnum;
7638 1.1 christos for (i = 0, segment = elf_tdata (ibfd)->phdr;
7639 1.1 christos i < num_segments;
7640 1.1 christos i++, segment++)
7641 1.14 christos if (segment->p_paddr != 0)
7642 1.1 christos {
7643 1.1 christos p_paddr_valid = true;
7644 1.1 christos break;
7645 1.14 christos }
7646 1.14 christos
7647 1.1 christos p_palign_valid = elf_is_p_align_valid (ibfd);
7648 1.1 christos
7649 1.1 christos for (i = 0, segment = elf_tdata (ibfd)->phdr;
7650 1.1 christos i < num_segments;
7651 1.1 christos i++, segment++)
7652 1.1 christos {
7653 1.12 christos asection *section;
7654 1.1 christos unsigned int section_count;
7655 1.1 christos size_t amt;
7656 1.1 christos Elf_Internal_Shdr *this_hdr;
7657 1.1 christos asection *first_section = NULL;
7658 1.1 christos asection *lowest_section;
7659 1.1 christos
7660 1.1 christos /* Compute how many sections are in this segment. */
7661 1.1 christos for (section = ibfd->sections, section_count = 0;
7662 1.1 christos section != NULL;
7663 1.1 christos section = section->next)
7664 1.1 christos {
7665 1.1 christos this_hdr = &(elf_section_data(section)->this_hdr);
7666 1.1 christos if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7667 1.1 christos {
7668 1.1 christos if (first_section == NULL)
7669 1.1 christos first_section = section;
7670 1.1 christos section_count++;
7671 1.1 christos }
7672 1.1 christos }
7673 1.1 christos
7674 1.11 christos /* Allocate a segment map big enough to contain
7675 1.12 christos all of the sections we have selected. */
7676 1.1 christos amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7677 1.1 christos amt += section_count * sizeof (asection *);
7678 1.14 christos map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7679 1.1 christos if (map == NULL)
7680 1.1 christos return false;
7681 1.1 christos
7682 1.1 christos /* Initialize the fields of the output segment map with the
7683 1.1 christos input segment. */
7684 1.1 christos map->next = NULL;
7685 1.1 christos map->p_type = segment->p_type;
7686 1.1 christos map->p_flags = segment->p_flags;
7687 1.1 christos map->p_flags_valid = 1;
7688 1.1 christos map->p_paddr = segment->p_paddr;
7689 1.14 christos map->p_paddr_valid = p_paddr_valid;
7690 1.14 christos map->p_align = segment->p_align;
7691 1.14 christos /* Keep p_align of PT_GNU_STACK for stack alignment. */
7692 1.1 christos map->p_align_valid = (map->p_type == PT_GNU_STACK
7693 1.1 christos || p_palign_valid);
7694 1.3 christos map->p_vaddr_offset = 0;
7695 1.3 christos
7696 1.1 christos if (map->p_type == PT_GNU_RELRO
7697 1.1 christos || map->p_type == PT_GNU_STACK)
7698 1.1 christos {
7699 1.1 christos /* The PT_GNU_RELRO segment may contain the first a few
7700 1.3 christos bytes in the .got.plt section even if the whole .got.plt
7701 1.3 christos section isn't in the PT_GNU_RELRO segment. We won't
7702 1.3 christos change the size of the PT_GNU_RELRO segment.
7703 1.1 christos Similarly, PT_GNU_STACK size is significant on uclinux
7704 1.1 christos systems. */
7705 1.1 christos map->p_size = segment->p_memsz;
7706 1.1 christos map->p_size_valid = 1;
7707 1.1 christos }
7708 1.1 christos
7709 1.1 christos /* Determine if this segment contains the ELF file header
7710 1.1 christos and if it contains the program headers themselves. */
7711 1.1 christos map->includes_filehdr = (segment->p_offset == 0
7712 1.1 christos && segment->p_filesz >= iehdr->e_ehsize);
7713 1.1 christos
7714 1.1 christos map->includes_phdrs = 0;
7715 1.1 christos if (! phdr_included || segment->p_type != PT_LOAD)
7716 1.1 christos {
7717 1.1 christos map->includes_phdrs =
7718 1.1 christos (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7719 1.1 christos && (segment->p_offset + segment->p_filesz
7720 1.1 christos >= ((bfd_vma) iehdr->e_phoff
7721 1.1 christos + iehdr->e_phnum * iehdr->e_phentsize)));
7722 1.14 christos
7723 1.1 christos if (segment->p_type == PT_LOAD && map->includes_phdrs)
7724 1.1 christos phdr_included = true;
7725 1.5 christos }
7726 1.1 christos
7727 1.1 christos lowest_section = NULL;
7728 1.1 christos if (section_count != 0)
7729 1.1 christos {
7730 1.1 christos unsigned int isec = 0;
7731 1.1 christos
7732 1.1 christos for (section = first_section;
7733 1.1 christos section != NULL;
7734 1.1 christos section = section->next)
7735 1.1 christos {
7736 1.1 christos this_hdr = &(elf_section_data(section)->this_hdr);
7737 1.1 christos if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7738 1.1 christos {
7739 1.1 christos map->sections[isec++] = section->output_section;
7740 1.1 christos if ((section->flags & SEC_ALLOC) != 0)
7741 1.1 christos {
7742 1.5 christos bfd_vma seg_off;
7743 1.5 christos
7744 1.4 christos if (lowest_section == NULL
7745 1.4 christos || section->lma < lowest_section->lma)
7746 1.1 christos lowest_section = section;
7747 1.1 christos
7748 1.1 christos /* Section lmas are set up from PT_LOAD header
7749 1.1 christos p_paddr in _bfd_elf_make_section_from_shdr.
7750 1.1 christos If this header has a p_paddr that disagrees
7751 1.1 christos with the section lma, flag the p_paddr as
7752 1.1 christos invalid. */
7753 1.1 christos if ((section->flags & SEC_LOAD) != 0)
7754 1.1 christos seg_off = this_hdr->sh_offset - segment->p_offset;
7755 1.12 christos else
7756 1.14 christos seg_off = this_hdr->sh_addr - segment->p_vaddr;
7757 1.1 christos if (section->lma * opb - segment->p_paddr != seg_off)
7758 1.1 christos map->p_paddr_valid = false;
7759 1.1 christos }
7760 1.1 christos if (isec == section_count)
7761 1.1 christos break;
7762 1.1 christos }
7763 1.1 christos }
7764 1.12 christos }
7765 1.12 christos
7766 1.12 christos if (section_count == 0)
7767 1.11 christos map->p_vaddr_offset = segment->p_vaddr / opb;
7768 1.12 christos else if (map->p_paddr_valid)
7769 1.12 christos {
7770 1.12 christos /* Account for padding before the first section in the segment. */
7771 1.12 christos bfd_vma hdr_size = 0;
7772 1.12 christos if (map->includes_filehdr)
7773 1.12 christos hdr_size = iehdr->e_ehsize;
7774 1.12 christos if (map->includes_phdrs)
7775 1.12 christos hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7776 1.12 christos
7777 1.11 christos map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7778 1.3 christos - (lowest_section ? lowest_section->lma : 0));
7779 1.1 christos }
7780 1.1 christos
7781 1.1 christos map->count = section_count;
7782 1.1 christos *pointer_to_map = map;
7783 1.1 christos pointer_to_map = &map->next;
7784 1.3 christos }
7785 1.14 christos
7786 1.1 christos elf_seg_map (obfd) = map_first;
7787 1.1 christos return true;
7788 1.1 christos }
7789 1.1 christos
7790 1.1 christos /* Copy private BFD data. This copies or rewrites ELF program header
7791 1.14 christos information. */
7792 1.1 christos
7793 1.1 christos static bool
7794 1.14 christos copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7795 1.14 christos {
7796 1.1 christos bfd_vma maxpagesize;
7797 1.1 christos
7798 1.14 christos if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7799 1.1 christos || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7800 1.1 christos return true;
7801 1.14 christos
7802 1.1 christos if (elf_tdata (ibfd)->phdr == NULL)
7803 1.1 christos return true;
7804 1.1 christos
7805 1.1 christos if (ibfd->xvec == obfd->xvec)
7806 1.1 christos {
7807 1.1 christos /* Check to see if any sections in the input BFD
7808 1.1 christos covered by ELF program header have changed. */
7809 1.1 christos Elf_Internal_Phdr *segment;
7810 1.1 christos asection *section, *osec;
7811 1.1 christos unsigned int i, num_segments;
7812 1.1 christos Elf_Internal_Shdr *this_hdr;
7813 1.1 christos const struct elf_backend_data *bed;
7814 1.1 christos
7815 1.1 christos bed = get_elf_backend_data (ibfd);
7816 1.1 christos
7817 1.1 christos /* Regenerate the segment map if p_paddr is set to 0. */
7818 1.1 christos if (bed->want_p_paddr_set_to_zero)
7819 1.1 christos goto rewrite;
7820 1.1 christos
7821 1.1 christos /* Initialize the segment mark field. */
7822 1.14 christos for (section = obfd->sections; section != NULL;
7823 1.1 christos section = section->next)
7824 1.1 christos section->segment_mark = false;
7825 1.1 christos
7826 1.1 christos num_segments = elf_elfheader (ibfd)->e_phnum;
7827 1.1 christos for (i = 0, segment = elf_tdata (ibfd)->phdr;
7828 1.1 christos i < num_segments;
7829 1.1 christos i++, segment++)
7830 1.1 christos {
7831 1.1 christos /* PR binutils/3535. The Solaris linker always sets the p_paddr
7832 1.1 christos and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7833 1.1 christos which severly confuses things, so always regenerate the segment
7834 1.1 christos map in this case. */
7835 1.14 christos if (segment->p_paddr == 0
7836 1.14 christos && segment->p_memsz == 0
7837 1.1 christos && (segment->p_type == PT_INTERP
7838 1.1 christos || segment->p_type == PT_DYNAMIC))
7839 1.1 christos goto rewrite;
7840 1.1 christos
7841 1.1 christos for (section = ibfd->sections;
7842 1.1 christos section != NULL; section = section->next)
7843 1.1 christos {
7844 1.1 christos /* We mark the output section so that we know it comes
7845 1.1 christos from the input BFD. */
7846 1.14 christos osec = section->output_section;
7847 1.1 christos if (osec)
7848 1.1 christos osec->segment_mark = true;
7849 1.1 christos
7850 1.1 christos /* Check if this section is covered by the segment. */
7851 1.1 christos this_hdr = &(elf_section_data(section)->this_hdr);
7852 1.1 christos if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7853 1.1 christos {
7854 1.1 christos /* FIXME: Check if its output section is changed or
7855 1.1 christos removed. What else do we need to check? */
7856 1.1 christos if (osec == NULL
7857 1.1 christos || section->flags != osec->flags
7858 1.1 christos || section->lma != osec->lma
7859 1.1 christos || section->vma != osec->vma
7860 1.1 christos || section->size != osec->size
7861 1.1 christos || section->rawsize != osec->rawsize
7862 1.1 christos || section->alignment_power != osec->alignment_power)
7863 1.1 christos goto rewrite;
7864 1.1 christos }
7865 1.1 christos }
7866 1.1 christos }
7867 1.1 christos
7868 1.1 christos /* Check to see if any output section do not come from the
7869 1.1 christos input BFD. */
7870 1.1 christos for (section = obfd->sections; section != NULL;
7871 1.11 christos section = section->next)
7872 1.1 christos {
7873 1.1 christos if (!section->segment_mark)
7874 1.14 christos goto rewrite;
7875 1.1 christos else
7876 1.1 christos section->segment_mark = false;
7877 1.1 christos }
7878 1.1 christos
7879 1.1 christos return copy_elf_program_header (ibfd, obfd);
7880 1.12 christos }
7881 1.14 christos
7882 1.3 christos rewrite:
7883 1.3 christos maxpagesize = 0;
7884 1.3 christos if (ibfd->xvec == obfd->xvec)
7885 1.3 christos {
7886 1.3 christos /* When rewriting program header, set the output maxpagesize to
7887 1.3 christos the maximum alignment of input PT_LOAD segments. */
7888 1.3 christos Elf_Internal_Phdr *segment;
7889 1.3 christos unsigned int i;
7890 1.3 christos unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
7891 1.3 christos
7892 1.3 christos for (i = 0, segment = elf_tdata (ibfd)->phdr;
7893 1.3 christos i < num_segments;
7894 1.3 christos i++, segment++)
7895 1.6 christos if (segment->p_type == PT_LOAD
7896 1.6 christos && maxpagesize < segment->p_align)
7897 1.6 christos {
7898 1.9 christos /* PR 17512: file: f17299af. */
7899 1.11 christos if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
7900 1.11 christos /* xgettext:c-format */
7901 1.11 christos _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7902 1.6 christos PRIx64 " is too large"),
7903 1.6 christos ibfd, (uint64_t) segment->p_align);
7904 1.6 christos else
7905 1.3 christos maxpagesize = segment->p_align;
7906 1.14 christos }
7907 1.14 christos }
7908 1.3 christos if (maxpagesize == 0)
7909 1.14 christos maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
7910 1.1 christos
7911 1.1 christos return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
7912 1.1 christos }
7913 1.1 christos
7914 1.14 christos /* Initialize private output section information from input section. */
7915 1.1 christos
7916 1.1 christos bool
7917 1.1 christos _bfd_elf_init_private_section_data (bfd *ibfd,
7918 1.1 christos asection *isec,
7919 1.1 christos bfd *obfd,
7920 1.1 christos asection *osec,
7921 1.1 christos struct bfd_link_info *link_info)
7922 1.1 christos
7923 1.14 christos {
7924 1.14 christos Elf_Internal_Shdr *ihdr, *ohdr;
7925 1.1 christos bool final_link = (link_info != NULL
7926 1.1 christos && !bfd_link_relocatable (link_info));
7927 1.1 christos
7928 1.14 christos if (ibfd->xvec->flavour != bfd_target_elf_flavour
7929 1.1 christos || obfd->xvec->flavour != bfd_target_elf_flavour)
7930 1.3 christos return true;
7931 1.3 christos
7932 1.12 christos BFD_ASSERT (elf_section_data (osec) != NULL);
7933 1.12 christos
7934 1.12 christos /* If this is a known ABI section, ELF section type and flags may
7935 1.12 christos have been set up when OSEC was created. For normal sections we
7936 1.12 christos allow the user to override the type and flags other than
7937 1.12 christos SHF_MASKOS and SHF_MASKPROC. */
7938 1.12 christos if (elf_section_type (osec) == SHT_PROGBITS
7939 1.12 christos || elf_section_type (osec) == SHT_NOTE
7940 1.12 christos || elf_section_type (osec) == SHT_NOBITS)
7941 1.12 christos elf_section_type (osec) = SHT_NULL;
7942 1.12 christos /* For objcopy and relocatable link, copy the ELF section type from
7943 1.12 christos the input file if the BFD section flags are the same. (If they
7944 1.12 christos are different the user may be doing something like
7945 1.1 christos "objcopy --set-section-flags .text=alloc,data".) For a final
7946 1.1 christos link allow some flags that the linker clears to differ. */
7947 1.1 christos if (elf_section_type (osec) == SHT_NULL
7948 1.1 christos && (osec->flags == isec->flags
7949 1.1 christos || (final_link
7950 1.1 christos && ((osec->flags ^ isec->flags)
7951 1.1 christos & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
7952 1.1 christos elf_section_type (osec) = elf_section_type (isec);
7953 1.12 christos
7954 1.12 christos /* FIXME: Is this correct for all OS/PROC specific flags? */
7955 1.1 christos elf_section_flags (osec) = (elf_section_flags (isec)
7956 1.9 christos & (SHF_MASKOS | SHF_MASKPROC));
7957 1.12 christos
7958 1.12 christos /* Copy sh_info from input for mbind section. */
7959 1.9 christos if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
7960 1.9 christos && elf_section_flags (isec) & SHF_GNU_MBIND)
7961 1.9 christos elf_section_data (osec)->this_hdr.sh_info
7962 1.1 christos = elf_section_data (isec)->this_hdr.sh_info;
7963 1.1 christos
7964 1.1 christos /* Set things up for objcopy and relocatable link. The output
7965 1.1 christos SHT_GROUP section will have its elf_next_in_group pointing back
7966 1.11 christos to the input group members. Ignore linker created group section.
7967 1.11 christos See elfNN_ia64_object_p in elfxx-ia64.c. */
7968 1.11 christos if ((link_info == NULL
7969 1.11 christos || !link_info->resolve_section_groups)
7970 1.11 christos && (elf_sec_group (isec) == NULL
7971 1.11 christos || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
7972 1.11 christos {
7973 1.11 christos if (elf_section_flags (isec) & SHF_GROUP)
7974 1.11 christos elf_section_flags (osec) |= SHF_GROUP;
7975 1.11 christos elf_next_in_group (osec) = elf_next_in_group (isec);
7976 1.6 christos elf_section_data (osec)->group = elf_section_data (isec)->group;
7977 1.11 christos }
7978 1.11 christos
7979 1.11 christos /* If not decompress, preserve SHF_COMPRESSED. */
7980 1.11 christos if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
7981 1.1 christos elf_section_flags (osec) |= (elf_section_flags (isec)
7982 1.1 christos & SHF_COMPRESSED);
7983 1.1 christos
7984 1.1 christos ihdr = &elf_section_data (isec)->this_hdr;
7985 1.1 christos
7986 1.1 christos /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7987 1.1 christos don't use the output section of the linked-to section since it
7988 1.1 christos may be NULL at this point. */
7989 1.1 christos if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7990 1.1 christos {
7991 1.1 christos ohdr = &elf_section_data (osec)->this_hdr;
7992 1.1 christos ohdr->sh_flags |= SHF_LINK_ORDER;
7993 1.1 christos elf_linked_to_section (osec) = elf_linked_to_section (isec);
7994 1.1 christos }
7995 1.1 christos
7996 1.14 christos osec->use_rela_p = isec->use_rela_p;
7997 1.1 christos
7998 1.1 christos return true;
7999 1.1 christos }
8000 1.1 christos
8001 1.1 christos /* Copy private section information. This copies over the entsize
8002 1.14 christos field, and sometimes the info field. */
8003 1.1 christos
8004 1.1 christos bool
8005 1.1 christos _bfd_elf_copy_private_section_data (bfd *ibfd,
8006 1.1 christos asection *isec,
8007 1.1 christos bfd *obfd,
8008 1.1 christos asection *osec)
8009 1.1 christos {
8010 1.1 christos Elf_Internal_Shdr *ihdr, *ohdr;
8011 1.1 christos
8012 1.14 christos if (ibfd->xvec->flavour != bfd_target_elf_flavour
8013 1.1 christos || obfd->xvec->flavour != bfd_target_elf_flavour)
8014 1.1 christos return true;
8015 1.1 christos
8016 1.1 christos ihdr = &elf_section_data (isec)->this_hdr;
8017 1.1 christos ohdr = &elf_section_data (osec)->this_hdr;
8018 1.1 christos
8019 1.1 christos ohdr->sh_entsize = ihdr->sh_entsize;
8020 1.1 christos
8021 1.1 christos if (ihdr->sh_type == SHT_SYMTAB
8022 1.1 christos || ihdr->sh_type == SHT_DYNSYM
8023 1.1 christos || ihdr->sh_type == SHT_GNU_verneed
8024 1.1 christos || ihdr->sh_type == SHT_GNU_verdef)
8025 1.1 christos ohdr->sh_info = ihdr->sh_info;
8026 1.1 christos
8027 1.1 christos return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
8028 1.1 christos NULL);
8029 1.1 christos }
8030 1.1 christos
8031 1.1 christos /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8032 1.1 christos necessary if we are removing either the SHT_GROUP section or any of
8033 1.1 christos the group member sections. DISCARDED is the value that a section's
8034 1.1 christos output_section has if the section will be discarded, NULL when this
8035 1.1 christos function is called from objcopy, bfd_abs_section_ptr when called
8036 1.14 christos from the linker. */
8037 1.1 christos
8038 1.1 christos bool
8039 1.1 christos _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
8040 1.1 christos {
8041 1.1 christos asection *isec;
8042 1.1 christos
8043 1.1 christos for (isec = ibfd->sections; isec != NULL; isec = isec->next)
8044 1.1 christos if (elf_section_type (isec) == SHT_GROUP)
8045 1.1 christos {
8046 1.1 christos asection *first = elf_next_in_group (isec);
8047 1.1 christos asection *s = first;
8048 1.1 christos bfd_size_type removed = 0;
8049 1.1 christos
8050 1.1 christos while (s != NULL)
8051 1.1 christos {
8052 1.1 christos /* If this member section is being output but the
8053 1.1 christos SHT_GROUP section is not, then clear the group info
8054 1.1 christos set up by _bfd_elf_copy_private_section_data. */
8055 1.1 christos if (s->output_section != discarded
8056 1.1 christos && isec->output_section == discarded)
8057 1.1 christos {
8058 1.1 christos elf_section_flags (s->output_section) &= ~SHF_GROUP;
8059 1.12 christos elf_group_name (s->output_section) = NULL;
8060 1.11 christos }
8061 1.11 christos else
8062 1.12 christos {
8063 1.12 christos struct bfd_elf_section_data *elf_sec = elf_section_data (s);
8064 1.12 christos if (s->output_section == discarded
8065 1.12 christos && isec->output_section != discarded)
8066 1.12 christos {
8067 1.12 christos /* Conversely, if the member section is not being
8068 1.12 christos output but the SHT_GROUP section is, then adjust
8069 1.12 christos its size. */
8070 1.12 christos removed += 4;
8071 1.12 christos if (elf_sec->rel.hdr != NULL
8072 1.12 christos && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
8073 1.12 christos removed += 4;
8074 1.12 christos if (elf_sec->rela.hdr != NULL
8075 1.12 christos && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
8076 1.12 christos removed += 4;
8077 1.12 christos }
8078 1.12 christos else
8079 1.12 christos {
8080 1.12 christos /* Also adjust for zero-sized relocation member
8081 1.12 christos section. */
8082 1.12 christos if (elf_sec->rel.hdr != NULL
8083 1.12 christos && elf_sec->rel.hdr->sh_size == 0)
8084 1.12 christos removed += 4;
8085 1.12 christos if (elf_sec->rela.hdr != NULL
8086 1.12 christos && elf_sec->rela.hdr->sh_size == 0)
8087 1.11 christos removed += 4;
8088 1.1 christos }
8089 1.1 christos }
8090 1.1 christos s = elf_next_in_group (s);
8091 1.1 christos if (s == first)
8092 1.1 christos break;
8093 1.1 christos }
8094 1.1 christos if (removed != 0)
8095 1.1 christos {
8096 1.1 christos if (discarded != NULL)
8097 1.11 christos {
8098 1.1 christos /* If we've been called for ld -r, then we need to
8099 1.1 christos adjust the input section size. */
8100 1.1 christos if (isec->rawsize == 0)
8101 1.11 christos isec->rawsize = isec->size;
8102 1.11 christos isec->size = isec->rawsize - removed;
8103 1.11 christos if (isec->size <= 4)
8104 1.11 christos {
8105 1.11 christos isec->size = 0;
8106 1.1 christos isec->flags |= SEC_EXCLUDE;
8107 1.14 christos }
8108 1.1 christos }
8109 1.1 christos else if (isec->output_section != NULL)
8110 1.1 christos {
8111 1.1 christos /* Adjust the output section size when called from
8112 1.11 christos objcopy. */
8113 1.11 christos isec->output_section->size -= removed;
8114 1.11 christos if (isec->output_section->size <= 4)
8115 1.11 christos {
8116 1.11 christos isec->output_section->size = 0;
8117 1.1 christos isec->output_section->flags |= SEC_EXCLUDE;
8118 1.1 christos }
8119 1.1 christos }
8120 1.1 christos }
8121 1.14 christos }
8122 1.1 christos
8123 1.1 christos return true;
8124 1.1 christos }
8125 1.1 christos
8126 1.14 christos /* Copy private header information. */
8127 1.1 christos
8128 1.1 christos bool
8129 1.1 christos _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8130 1.1 christos {
8131 1.14 christos if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8132 1.1 christos || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8133 1.1 christos return true;
8134 1.1 christos
8135 1.1 christos /* Copy over private BFD data if it has not already been copied.
8136 1.1 christos This must be done here, rather than in the copy_private_bfd_data
8137 1.1 christos entry point, because the latter is called after the section
8138 1.3 christos contents have been set, which means that the program headers have
8139 1.1 christos already been worked out. */
8140 1.1 christos if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
8141 1.14 christos {
8142 1.1 christos if (! copy_private_bfd_data (ibfd, obfd))
8143 1.1 christos return false;
8144 1.1 christos }
8145 1.1 christos
8146 1.1 christos return _bfd_elf_fixup_group_sections (ibfd, NULL);
8147 1.1 christos }
8148 1.1 christos
8149 1.1 christos /* Copy private symbol information. If this symbol is in a section
8150 1.1 christos which we did not map into a BFD section, try to map the section
8151 1.1 christos index correctly. We use special macro definitions for the mapped
8152 1.1 christos section indices; these definitions are interpreted by the
8153 1.1 christos swap_out_syms function. */
8154 1.1 christos
8155 1.1 christos #define MAP_ONESYMTAB (SHN_HIOS + 1)
8156 1.1 christos #define MAP_DYNSYMTAB (SHN_HIOS + 2)
8157 1.1 christos #define MAP_STRTAB (SHN_HIOS + 3)
8158 1.1 christos #define MAP_SHSTRTAB (SHN_HIOS + 4)
8159 1.14 christos #define MAP_SYM_SHNDX (SHN_HIOS + 5)
8160 1.1 christos
8161 1.1 christos bool
8162 1.1 christos _bfd_elf_copy_private_symbol_data (bfd *ibfd,
8163 1.1 christos asymbol *isymarg,
8164 1.1 christos bfd *obfd,
8165 1.1 christos asymbol *osymarg)
8166 1.1 christos {
8167 1.1 christos elf_symbol_type *isym, *osym;
8168 1.1 christos
8169 1.14 christos if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8170 1.1 christos || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8171 1.14 christos return true;
8172 1.14 christos
8173 1.1 christos isym = elf_symbol_from (isymarg);
8174 1.1 christos osym = elf_symbol_from (osymarg);
8175 1.1 christos
8176 1.1 christos if (isym != NULL
8177 1.1 christos && isym->internal_elf_sym.st_shndx != 0
8178 1.1 christos && osym != NULL
8179 1.1 christos && bfd_is_abs_section (isym->symbol.section))
8180 1.1 christos {
8181 1.1 christos unsigned int shndx;
8182 1.1 christos
8183 1.1 christos shndx = isym->internal_elf_sym.st_shndx;
8184 1.1 christos if (shndx == elf_onesymtab (ibfd))
8185 1.1 christos shndx = MAP_ONESYMTAB;
8186 1.3 christos else if (shndx == elf_dynsymtab (ibfd))
8187 1.1 christos shndx = MAP_DYNSYMTAB;
8188 1.3 christos else if (shndx == elf_strtab_sec (ibfd))
8189 1.1 christos shndx = MAP_STRTAB;
8190 1.8 christos else if (shndx == elf_shstrtab_sec (ibfd))
8191 1.1 christos shndx = MAP_SHSTRTAB;
8192 1.1 christos else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8193 1.1 christos shndx = MAP_SYM_SHNDX;
8194 1.1 christos osym->internal_elf_sym.st_shndx = shndx;
8195 1.14 christos }
8196 1.1 christos
8197 1.1 christos return true;
8198 1.1 christos }
8199 1.1 christos
8200 1.14 christos /* Swap out the symbols. */
8201 1.1 christos
8202 1.6 christos static bool
8203 1.14 christos swap_out_syms (bfd *abfd,
8204 1.14 christos struct elf_strtab_hash **sttp,
8205 1.1 christos int relocatable_p,
8206 1.1 christos struct bfd_link_info *info)
8207 1.12 christos {
8208 1.1 christos const struct elf_backend_data *bed;
8209 1.6 christos unsigned int symcount;
8210 1.1 christos asymbol **syms;
8211 1.1 christos struct elf_strtab_hash *stt;
8212 1.1 christos Elf_Internal_Shdr *symtab_hdr;
8213 1.6 christos Elf_Internal_Shdr *symtab_shndx_hdr;
8214 1.1 christos Elf_Internal_Shdr *symstrtab_hdr;
8215 1.1 christos struct elf_sym_strtab *symstrtab;
8216 1.6 christos bfd_byte *outbound_syms;
8217 1.12 christos bfd_byte *outbound_shndx;
8218 1.3 christos unsigned long outbound_syms_index;
8219 1.12 christos unsigned int idx;
8220 1.14 christos unsigned int num_locals;
8221 1.1 christos size_t amt;
8222 1.3 christos bool name_local_sections;
8223 1.14 christos
8224 1.1 christos if (!elf_map_symbols (abfd, &num_locals))
8225 1.1 christos return false;
8226 1.6 christos
8227 1.1 christos /* Dump out the symtabs. */
8228 1.14 christos stt = _bfd_elf_strtab_init ();
8229 1.1 christos if (stt == NULL)
8230 1.1 christos return false;
8231 1.1 christos
8232 1.1 christos bed = get_elf_backend_data (abfd);
8233 1.1 christos symcount = bfd_get_symcount (abfd);
8234 1.1 christos symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8235 1.1 christos symtab_hdr->sh_type = SHT_SYMTAB;
8236 1.3 christos symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8237 1.1 christos symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8238 1.1 christos symtab_hdr->sh_info = num_locals + 1;
8239 1.1 christos symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8240 1.1 christos
8241 1.1 christos symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8242 1.6 christos symstrtab_hdr->sh_type = SHT_STRTAB;
8243 1.12 christos
8244 1.12 christos /* Allocate buffer to swap out the .strtab section. */
8245 1.6 christos if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8246 1.12 christos || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
8247 1.6 christos {
8248 1.14 christos bfd_set_error (bfd_error_no_memory);
8249 1.6 christos _bfd_elf_strtab_free (stt);
8250 1.6 christos return false;
8251 1.12 christos }
8252 1.12 christos
8253 1.1 christos if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8254 1.12 christos || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
8255 1.12 christos {
8256 1.12 christos error_no_mem:
8257 1.12 christos bfd_set_error (bfd_error_no_memory);
8258 1.6 christos error_return:
8259 1.14 christos free (symstrtab);
8260 1.1 christos _bfd_elf_strtab_free (stt);
8261 1.1 christos return false;
8262 1.6 christos }
8263 1.1 christos symtab_hdr->contents = outbound_syms;
8264 1.1 christos outbound_syms_index = 0;
8265 1.8 christos
8266 1.8 christos outbound_shndx = NULL;
8267 1.1 christos
8268 1.8 christos if (elf_symtab_shndx_list (abfd))
8269 1.8 christos {
8270 1.8 christos symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8271 1.12 christos if (symtab_shndx_hdr->sh_name != 0)
8272 1.12 christos {
8273 1.12 christos if (_bfd_mul_overflow (symcount + 1,
8274 1.12 christos sizeof (Elf_External_Sym_Shndx), &amt))
8275 1.8 christos goto error_no_mem;
8276 1.8 christos outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt);
8277 1.1 christos if (outbound_shndx == NULL)
8278 1.8 christos goto error_return;
8279 1.8 christos
8280 1.8 christos symtab_shndx_hdr->contents = outbound_shndx;
8281 1.8 christos symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8282 1.8 christos symtab_shndx_hdr->sh_size = amt;
8283 1.8 christos symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8284 1.8 christos symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8285 1.1 christos }
8286 1.1 christos /* FIXME: What about any other headers in the list ? */
8287 1.1 christos }
8288 1.1 christos
8289 1.1 christos /* Now generate the data (for "contents"). */
8290 1.1 christos {
8291 1.1 christos /* Fill in zeroth symbol and swap it out. */
8292 1.1 christos Elf_Internal_Sym sym;
8293 1.1 christos sym.st_name = 0;
8294 1.1 christos sym.st_value = 0;
8295 1.1 christos sym.st_size = 0;
8296 1.1 christos sym.st_info = 0;
8297 1.1 christos sym.st_other = 0;
8298 1.6 christos sym.st_shndx = SHN_UNDEF;
8299 1.6 christos sym.st_target_internal = 0;
8300 1.6 christos symstrtab[0].sym = sym;
8301 1.1 christos symstrtab[0].dest_index = outbound_syms_index;
8302 1.1 christos outbound_syms_index++;
8303 1.1 christos }
8304 1.1 christos
8305 1.1 christos name_local_sections
8306 1.1 christos = (bed->elf_backend_name_local_section_symbols
8307 1.1 christos && bed->elf_backend_name_local_section_symbols (abfd));
8308 1.6 christos
8309 1.1 christos syms = bfd_get_outsymbols (abfd);
8310 1.1 christos for (idx = 0; idx < symcount;)
8311 1.1 christos {
8312 1.1 christos Elf_Internal_Sym sym;
8313 1.1 christos bfd_vma value = syms[idx]->value;
8314 1.1 christos elf_symbol_type *type_ptr;
8315 1.1 christos flagword flags = syms[idx]->flags;
8316 1.1 christos int type;
8317 1.1 christos
8318 1.1 christos if (!name_local_sections
8319 1.1 christos && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8320 1.6 christos {
8321 1.1 christos /* Local section symbols have no name. */
8322 1.1 christos sym.st_name = (unsigned long) -1;
8323 1.1 christos }
8324 1.6 christos else
8325 1.6 christos {
8326 1.6 christos /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8327 1.6 christos to get the final offset for st_name. */
8328 1.14 christos sym.st_name
8329 1.1 christos = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
8330 1.6 christos false);
8331 1.1 christos if (sym.st_name == (unsigned long) -1)
8332 1.1 christos goto error_return;
8333 1.14 christos }
8334 1.1 christos
8335 1.1 christos type_ptr = elf_symbol_from (syms[idx]);
8336 1.1 christos
8337 1.1 christos if ((flags & BSF_SECTION_SYM) == 0
8338 1.1 christos && bfd_is_com_section (syms[idx]->section))
8339 1.1 christos {
8340 1.1 christos /* ELF common symbols put the alignment into the `value' field,
8341 1.1 christos and the size into the `size' field. This is backwards from
8342 1.1 christos how BFD handles it, so reverse it here. */
8343 1.1 christos sym.st_size = value;
8344 1.1 christos if (type_ptr == NULL
8345 1.1 christos || type_ptr->internal_elf_sym.st_value == 0)
8346 1.1 christos sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8347 1.1 christos else
8348 1.1 christos sym.st_value = type_ptr->internal_elf_sym.st_value;
8349 1.1 christos sym.st_shndx = _bfd_elf_section_from_bfd_section
8350 1.1 christos (abfd, syms[idx]->section);
8351 1.1 christos }
8352 1.1 christos else
8353 1.1 christos {
8354 1.1 christos asection *sec = syms[idx]->section;
8355 1.1 christos unsigned int shndx;
8356 1.1 christos
8357 1.1 christos if (sec->output_section)
8358 1.1 christos {
8359 1.1 christos value += sec->output_offset;
8360 1.1 christos sec = sec->output_section;
8361 1.1 christos }
8362 1.1 christos
8363 1.1 christos /* Don't add in the section vma for relocatable output. */
8364 1.1 christos if (! relocatable_p)
8365 1.1 christos value += sec->vma;
8366 1.1 christos sym.st_value = value;
8367 1.1 christos sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8368 1.1 christos
8369 1.1 christos if (bfd_is_abs_section (sec)
8370 1.1 christos && type_ptr != NULL
8371 1.1 christos && type_ptr->internal_elf_sym.st_shndx != 0)
8372 1.1 christos {
8373 1.1 christos /* This symbol is in a real ELF section which we did
8374 1.1 christos not create as a BFD section. Undo the mapping done
8375 1.1 christos by copy_private_symbol_data. */
8376 1.1 christos shndx = type_ptr->internal_elf_sym.st_shndx;
8377 1.1 christos switch (shndx)
8378 1.1 christos {
8379 1.1 christos case MAP_ONESYMTAB:
8380 1.1 christos shndx = elf_onesymtab (abfd);
8381 1.1 christos break;
8382 1.1 christos case MAP_DYNSYMTAB:
8383 1.1 christos shndx = elf_dynsymtab (abfd);
8384 1.3 christos break;
8385 1.1 christos case MAP_STRTAB:
8386 1.1 christos shndx = elf_strtab_sec (abfd);
8387 1.3 christos break;
8388 1.1 christos case MAP_SHSTRTAB:
8389 1.1 christos shndx = elf_shstrtab_sec (abfd);
8390 1.8 christos break;
8391 1.8 christos case MAP_SYM_SHNDX:
8392 1.1 christos if (elf_symtab_shndx_list (abfd))
8393 1.12 christos shndx = elf_symtab_shndx_list (abfd)->ndx;
8394 1.12 christos break;
8395 1.12 christos case SHN_COMMON:
8396 1.12 christos case SHN_ABS:
8397 1.1 christos shndx = SHN_ABS;
8398 1.12 christos break;
8399 1.12 christos default:
8400 1.12 christos if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8401 1.12 christos {
8402 1.12 christos if (bed->symbol_section_index)
8403 1.12 christos shndx = bed->symbol_section_index (abfd, type_ptr);
8404 1.12 christos /* Otherwise just leave the index alone. */
8405 1.12 christos }
8406 1.12 christos else
8407 1.12 christos {
8408 1.12 christos if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8409 1.12 christos _bfd_error_handler (_("%pB: \
8410 1.12 christos Unable to handle section index %x in ELF symbol. Using ABS instead."),
8411 1.12 christos abfd, shndx);
8412 1.1 christos shndx = SHN_ABS;
8413 1.1 christos }
8414 1.1 christos break;
8415 1.1 christos }
8416 1.1 christos }
8417 1.1 christos else
8418 1.1 christos {
8419 1.1 christos shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8420 1.1 christos
8421 1.1 christos if (shndx == SHN_BAD)
8422 1.1 christos {
8423 1.1 christos asection *sec2;
8424 1.1 christos
8425 1.1 christos /* Writing this would be a hell of a lot easier if
8426 1.1 christos we had some decent documentation on bfd, and
8427 1.1 christos knew what to expect of the library, and what to
8428 1.1 christos demand of applications. For example, it
8429 1.1 christos appears that `objcopy' might not set the
8430 1.1 christos section of a symbol to be a section that is
8431 1.8 christos actually in the output file. */
8432 1.8 christos sec2 = bfd_get_section_by_name (abfd, sec->name);
8433 1.8 christos if (sec2 != NULL)
8434 1.1 christos shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8435 1.9 christos if (shndx == SHN_BAD)
8436 1.11 christos {
8437 1.11 christos /* xgettext:c-format */
8438 1.11 christos _bfd_error_handler
8439 1.11 christos (_("unable to find equivalent output section"
8440 1.11 christos " for symbol '%s' from section '%s'"),
8441 1.1 christos syms[idx]->name ? syms[idx]->name : "<Local sym>",
8442 1.6 christos sec->name);
8443 1.1 christos bfd_set_error (bfd_error_invalid_operation);
8444 1.1 christos goto error_return;
8445 1.1 christos }
8446 1.1 christos }
8447 1.1 christos }
8448 1.1 christos
8449 1.1 christos sym.st_shndx = shndx;
8450 1.1 christos }
8451 1.1 christos
8452 1.1 christos if ((flags & BSF_THREAD_LOCAL) != 0)
8453 1.1 christos type = STT_TLS;
8454 1.1 christos else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8455 1.1 christos type = STT_GNU_IFUNC;
8456 1.1 christos else if ((flags & BSF_FUNCTION) != 0)
8457 1.1 christos type = STT_FUNC;
8458 1.1 christos else if ((flags & BSF_OBJECT) != 0)
8459 1.1 christos type = STT_OBJECT;
8460 1.1 christos else if ((flags & BSF_RELC) != 0)
8461 1.1 christos type = STT_RELC;
8462 1.1 christos else if ((flags & BSF_SRELC) != 0)
8463 1.1 christos type = STT_SRELC;
8464 1.1 christos else
8465 1.1 christos type = STT_NOTYPE;
8466 1.1 christos
8467 1.1 christos if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8468 1.1 christos type = STT_TLS;
8469 1.1 christos
8470 1.1 christos /* Processor-specific types. */
8471 1.1 christos if (type_ptr != NULL
8472 1.1 christos && bed->elf_backend_get_symbol_type)
8473 1.1 christos type = ((*bed->elf_backend_get_symbol_type)
8474 1.1 christos (&type_ptr->internal_elf_sym, type));
8475 1.1 christos
8476 1.1 christos if (flags & BSF_SECTION_SYM)
8477 1.1 christos {
8478 1.1 christos if (flags & BSF_GLOBAL)
8479 1.1 christos sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8480 1.1 christos else
8481 1.1 christos sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8482 1.1 christos }
8483 1.8 christos else if (bfd_is_com_section (syms[idx]->section))
8484 1.8 christos {
8485 1.8 christos if (type != STT_TLS)
8486 1.8 christos {
8487 1.8 christos if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8488 1.8 christos type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8489 1.8 christos ? STT_COMMON : STT_OBJECT);
8490 1.8 christos else
8491 1.8 christos type = ((flags & BSF_ELF_COMMON) != 0
8492 1.8 christos ? STT_COMMON : STT_OBJECT);
8493 1.1 christos }
8494 1.1 christos sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
8495 1.1 christos }
8496 1.1 christos else if (bfd_is_und_section (syms[idx]->section))
8497 1.1 christos sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8498 1.1 christos ? STB_WEAK
8499 1.1 christos : STB_GLOBAL),
8500 1.1 christos type);
8501 1.1 christos else if (flags & BSF_FILE)
8502 1.1 christos sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8503 1.1 christos else
8504 1.1 christos {
8505 1.1 christos int bind = STB_LOCAL;
8506 1.1 christos
8507 1.1 christos if (flags & BSF_LOCAL)
8508 1.1 christos bind = STB_LOCAL;
8509 1.1 christos else if (flags & BSF_GNU_UNIQUE)
8510 1.1 christos bind = STB_GNU_UNIQUE;
8511 1.1 christos else if (flags & BSF_WEAK)
8512 1.1 christos bind = STB_WEAK;
8513 1.1 christos else if (flags & BSF_GLOBAL)
8514 1.1 christos bind = STB_GLOBAL;
8515 1.1 christos
8516 1.1 christos sym.st_info = ELF_ST_INFO (bind, type);
8517 1.1 christos }
8518 1.1 christos
8519 1.1 christos if (type_ptr != NULL)
8520 1.1 christos {
8521 1.1 christos sym.st_other = type_ptr->internal_elf_sym.st_other;
8522 1.1 christos sym.st_target_internal
8523 1.1 christos = type_ptr->internal_elf_sym.st_target_internal;
8524 1.1 christos }
8525 1.1 christos else
8526 1.1 christos {
8527 1.1 christos sym.st_other = 0;
8528 1.1 christos sym.st_target_internal = 0;
8529 1.6 christos }
8530 1.6 christos
8531 1.6 christos idx++;
8532 1.6 christos symstrtab[idx].sym = sym;
8533 1.6 christos symstrtab[idx].dest_index = outbound_syms_index;
8534 1.6 christos
8535 1.6 christos outbound_syms_index++;
8536 1.6 christos }
8537 1.6 christos
8538 1.6 christos /* Finalize the .strtab section. */
8539 1.6 christos _bfd_elf_strtab_finalize (stt);
8540 1.6 christos
8541 1.6 christos /* Swap out the .strtab section. */
8542 1.6 christos for (idx = 0; idx <= symcount; idx++)
8543 1.6 christos {
8544 1.6 christos struct elf_sym_strtab *elfsym = &symstrtab[idx];
8545 1.6 christos if (elfsym->sym.st_name == (unsigned long) -1)
8546 1.6 christos elfsym->sym.st_name = 0;
8547 1.6 christos else
8548 1.14 christos elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8549 1.14 christos elfsym->sym.st_name);
8550 1.14 christos if (info && info->callbacks->ctf_new_symbol)
8551 1.14 christos info->callbacks->ctf_new_symbol (elfsym->dest_index,
8552 1.14 christos &elfsym->sym);
8553 1.14 christos
8554 1.6 christos /* Inform the linker of the addition of this symbol. */
8555 1.6 christos
8556 1.6 christos bed->s->swap_symbol_out (abfd, &elfsym->sym,
8557 1.6 christos (outbound_syms
8558 1.14 christos + (elfsym->dest_index
8559 1.14 christos * bed->s->sizeof_sym)),
8560 1.14 christos NPTR_ADD (outbound_shndx,
8561 1.1 christos (elfsym->dest_index
8562 1.6 christos * sizeof (Elf_External_Sym_Shndx))));
8563 1.1 christos }
8564 1.1 christos free (symstrtab);
8565 1.6 christos
8566 1.1 christos *sttp = stt;
8567 1.8 christos symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
8568 1.1 christos symstrtab_hdr->sh_type = SHT_STRTAB;
8569 1.1 christos symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
8570 1.1 christos symstrtab_hdr->sh_addr = 0;
8571 1.1 christos symstrtab_hdr->sh_entsize = 0;
8572 1.1 christos symstrtab_hdr->sh_link = 0;
8573 1.1 christos symstrtab_hdr->sh_info = 0;
8574 1.14 christos symstrtab_hdr->sh_addralign = 1;
8575 1.1 christos
8576 1.1 christos return true;
8577 1.1 christos }
8578 1.1 christos
8579 1.1 christos /* Return the number of bytes required to hold the symtab vector.
8580 1.1 christos
8581 1.1 christos Note that we base it on the count plus 1, since we will null terminate
8582 1.1 christos the vector allocated based on this size. However, the ELF symbol table
8583 1.1 christos always has a dummy entry as symbol #0, so it ends up even. */
8584 1.1 christos
8585 1.1 christos long
8586 1.11 christos _bfd_elf_get_symtab_upper_bound (bfd *abfd)
8587 1.1 christos {
8588 1.1 christos bfd_size_type symcount;
8589 1.1 christos long symtab_size;
8590 1.1 christos Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8591 1.12 christos
8592 1.11 christos symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8593 1.11 christos if (symcount > LONG_MAX / sizeof (asymbol *))
8594 1.11 christos {
8595 1.11 christos bfd_set_error (bfd_error_file_too_big);
8596 1.12 christos return -1;
8597 1.12 christos }
8598 1.12 christos symtab_size = symcount * (sizeof (asymbol *));
8599 1.12 christos if (symcount == 0)
8600 1.12 christos symtab_size = sizeof (asymbol *);
8601 1.12 christos else if (!bfd_write_p (abfd))
8602 1.12 christos {
8603 1.12 christos ufile_ptr filesize = bfd_get_file_size (abfd);
8604 1.12 christos
8605 1.12 christos if (filesize != 0 && (unsigned long) symtab_size > filesize)
8606 1.12 christos {
8607 1.12 christos bfd_set_error (bfd_error_file_truncated);
8608 1.12 christos return -1;
8609 1.1 christos }
8610 1.1 christos }
8611 1.1 christos
8612 1.1 christos return symtab_size;
8613 1.1 christos }
8614 1.1 christos
8615 1.1 christos long
8616 1.11 christos _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
8617 1.1 christos {
8618 1.1 christos bfd_size_type symcount;
8619 1.1 christos long symtab_size;
8620 1.1 christos Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8621 1.1 christos
8622 1.1 christos if (elf_dynsymtab (abfd) == 0)
8623 1.1 christos {
8624 1.1 christos bfd_set_error (bfd_error_invalid_operation);
8625 1.1 christos return -1;
8626 1.1 christos }
8627 1.12 christos
8628 1.11 christos symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8629 1.11 christos if (symcount > LONG_MAX / sizeof (asymbol *))
8630 1.11 christos {
8631 1.11 christos bfd_set_error (bfd_error_file_too_big);
8632 1.12 christos return -1;
8633 1.12 christos }
8634 1.12 christos symtab_size = symcount * (sizeof (asymbol *));
8635 1.12 christos if (symcount == 0)
8636 1.12 christos symtab_size = sizeof (asymbol *);
8637 1.12 christos else if (!bfd_write_p (abfd))
8638 1.12 christos {
8639 1.12 christos ufile_ptr filesize = bfd_get_file_size (abfd);
8640 1.12 christos
8641 1.12 christos if (filesize != 0 && (unsigned long) symtab_size > filesize)
8642 1.12 christos {
8643 1.12 christos bfd_set_error (bfd_error_file_truncated);
8644 1.12 christos return -1;
8645 1.1 christos }
8646 1.1 christos }
8647 1.1 christos
8648 1.1 christos return symtab_size;
8649 1.1 christos }
8650 1.12 christos
8651 1.1 christos long
8652 1.12 christos _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
8653 1.12 christos {
8654 1.12 christos if (asect->reloc_count != 0 && !bfd_write_p (abfd))
8655 1.12 christos {
8656 1.12 christos /* Sanity check reloc section size. */
8657 1.14 christos ufile_ptr filesize = bfd_get_file_size (abfd);
8658 1.12 christos
8659 1.14 christos if (filesize != 0)
8660 1.14 christos {
8661 1.14 christos struct bfd_elf_section_data *d = elf_section_data (asect);
8662 1.14 christos bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
8663 1.14 christos bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
8664 1.14 christos
8665 1.14 christos if (rel_size + rela_size > filesize
8666 1.14 christos || rel_size + rela_size < rel_size)
8667 1.14 christos {
8668 1.14 christos bfd_set_error (bfd_error_file_truncated);
8669 1.12 christos return -1;
8670 1.12 christos }
8671 1.12 christos }
8672 1.12 christos }
8673 1.12 christos
8674 1.12 christos #if SIZEOF_LONG == SIZEOF_INT
8675 1.12 christos if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
8676 1.12 christos {
8677 1.12 christos bfd_set_error (bfd_error_file_too_big);
8678 1.12 christos return -1;
8679 1.14 christos }
8680 1.1 christos #endif
8681 1.1 christos return (asect->reloc_count + 1L) * sizeof (arelent *);
8682 1.1 christos }
8683 1.1 christos
8684 1.1 christos /* Canonicalize the relocs. */
8685 1.1 christos
8686 1.1 christos long
8687 1.1 christos _bfd_elf_canonicalize_reloc (bfd *abfd,
8688 1.1 christos sec_ptr section,
8689 1.1 christos arelent **relptr,
8690 1.1 christos asymbol **symbols)
8691 1.1 christos {
8692 1.1 christos arelent *tblptr;
8693 1.1 christos unsigned int i;
8694 1.14 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8695 1.1 christos
8696 1.1 christos if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
8697 1.1 christos return -1;
8698 1.1 christos
8699 1.1 christos tblptr = section->relocation;
8700 1.1 christos for (i = 0; i < section->reloc_count; i++)
8701 1.1 christos *relptr++ = tblptr++;
8702 1.1 christos
8703 1.1 christos *relptr = NULL;
8704 1.1 christos
8705 1.1 christos return section->reloc_count;
8706 1.1 christos }
8707 1.1 christos
8708 1.1 christos long
8709 1.1 christos _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
8710 1.14 christos {
8711 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8712 1.1 christos long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
8713 1.12 christos
8714 1.1 christos if (symcount >= 0)
8715 1.1 christos abfd->symcount = symcount;
8716 1.1 christos return symcount;
8717 1.1 christos }
8718 1.1 christos
8719 1.1 christos long
8720 1.1 christos _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8721 1.1 christos asymbol **allocation)
8722 1.14 christos {
8723 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8724 1.1 christos long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
8725 1.12 christos
8726 1.1 christos if (symcount >= 0)
8727 1.1 christos abfd->dynsymcount = symcount;
8728 1.1 christos return symcount;
8729 1.1 christos }
8730 1.1 christos
8731 1.1 christos /* Return the size required for the dynamic reloc entries. Any loadable
8732 1.1 christos section that was actually installed in the BFD, and has type SHT_REL
8733 1.1 christos or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8734 1.1 christos dynamic reloc section. */
8735 1.1 christos
8736 1.1 christos long
8737 1.12 christos _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
8738 1.1 christos {
8739 1.1 christos bfd_size_type count, ext_rel_size;
8740 1.1 christos asection *s;
8741 1.1 christos
8742 1.1 christos if (elf_dynsymtab (abfd) == 0)
8743 1.1 christos {
8744 1.1 christos bfd_set_error (bfd_error_invalid_operation);
8745 1.1 christos return -1;
8746 1.11 christos }
8747 1.12 christos
8748 1.1 christos count = 1;
8749 1.1 christos ext_rel_size = 0;
8750 1.1 christos for (s = abfd->sections; s != NULL; s = s->next)
8751 1.1 christos if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8752 1.11 christos && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8753 1.12 christos || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8754 1.12 christos {
8755 1.12 christos ext_rel_size += s->size;
8756 1.12 christos if (ext_rel_size < s->size)
8757 1.12 christos {
8758 1.12 christos bfd_set_error (bfd_error_file_truncated);
8759 1.11 christos return -1;
8760 1.11 christos }
8761 1.11 christos count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
8762 1.11 christos if (count > LONG_MAX / sizeof (arelent *))
8763 1.11 christos {
8764 1.11 christos bfd_set_error (bfd_error_file_too_big);
8765 1.11 christos return -1;
8766 1.12 christos }
8767 1.12 christos }
8768 1.12 christos if (count > 1 && !bfd_write_p (abfd))
8769 1.12 christos {
8770 1.12 christos /* Sanity check reloc section sizes. */
8771 1.12 christos ufile_ptr filesize = bfd_get_file_size (abfd);
8772 1.12 christos if (filesize != 0 && ext_rel_size > filesize)
8773 1.12 christos {
8774 1.12 christos bfd_set_error (bfd_error_file_truncated);
8775 1.12 christos return -1;
8776 1.11 christos }
8777 1.1 christos }
8778 1.1 christos return count * sizeof (arelent *);
8779 1.1 christos }
8780 1.1 christos
8781 1.1 christos /* Canonicalize the dynamic relocation entries. Note that we return the
8782 1.1 christos dynamic relocations as a single block, although they are actually
8783 1.1 christos associated with particular sections; the interface, which was
8784 1.1 christos designed for SunOS style shared libraries, expects that there is only
8785 1.1 christos one set of dynamic relocs. Any loadable section that was actually
8786 1.1 christos installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8787 1.1 christos dynamic symbol table, is considered to be a dynamic reloc section. */
8788 1.1 christos
8789 1.1 christos long
8790 1.1 christos _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8791 1.1 christos arelent **storage,
8792 1.14 christos asymbol **syms)
8793 1.1 christos {
8794 1.1 christos bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
8795 1.1 christos asection *s;
8796 1.1 christos long ret;
8797 1.1 christos
8798 1.1 christos if (elf_dynsymtab (abfd) == 0)
8799 1.1 christos {
8800 1.1 christos bfd_set_error (bfd_error_invalid_operation);
8801 1.1 christos return -1;
8802 1.1 christos }
8803 1.1 christos
8804 1.1 christos slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8805 1.1 christos ret = 0;
8806 1.1 christos for (s = abfd->sections; s != NULL; s = s->next)
8807 1.1 christos {
8808 1.1 christos if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8809 1.1 christos && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8810 1.1 christos || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8811 1.1 christos {
8812 1.1 christos arelent *p;
8813 1.14 christos long count, i;
8814 1.1 christos
8815 1.1 christos if (! (*slurp_relocs) (abfd, s, syms, true))
8816 1.1 christos return -1;
8817 1.1 christos count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
8818 1.1 christos p = s->relocation;
8819 1.1 christos for (i = 0; i < count; i++)
8820 1.1 christos *storage++ = p++;
8821 1.1 christos ret += count;
8822 1.1 christos }
8823 1.1 christos }
8824 1.1 christos
8825 1.1 christos *storage = NULL;
8826 1.1 christos
8827 1.1 christos return ret;
8828 1.1 christos }
8829 1.1 christos
8830 1.14 christos /* Read in the version information. */
8832 1.1 christos
8833 1.1 christos bool
8834 1.1 christos _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
8835 1.12 christos {
8836 1.1 christos bfd_byte *contents = NULL;
8837 1.1 christos unsigned int freeidx = 0;
8838 1.1 christos size_t amt;
8839 1.1 christos
8840 1.1 christos if (elf_dynverref (abfd) != 0)
8841 1.1 christos {
8842 1.1 christos Elf_Internal_Shdr *hdr;
8843 1.1 christos Elf_External_Verneed *everneed;
8844 1.1 christos Elf_Internal_Verneed *iverneed;
8845 1.1 christos unsigned int i;
8846 1.1 christos bfd_byte *contents_end;
8847 1.14 christos
8848 1.1 christos hdr = &elf_tdata (abfd)->dynverref_hdr;
8849 1.12 christos
8850 1.9 christos if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
8851 1.11 christos {
8852 1.5 christos error_return_bad_verref:
8853 1.12 christos _bfd_error_handler
8854 1.1 christos (_("%pB: .gnu.version_r invalid entry"), abfd);
8855 1.1 christos bfd_set_error (bfd_error_bad_value);
8856 1.1 christos error_return_verref:
8857 1.1 christos elf_tdata (abfd)->verref = NULL;
8858 1.5 christos elf_tdata (abfd)->cverrefs = 0;
8859 1.12 christos goto error_return;
8860 1.12 christos }
8861 1.12 christos
8862 1.5 christos if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
8863 1.5 christos goto error_return_verref;
8864 1.5 christos contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8865 1.12 christos if (contents == NULL)
8866 1.12 christos goto error_return_verref;
8867 1.12 christos
8868 1.12 christos if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt))
8869 1.12 christos {
8870 1.14 christos bfd_set_error (bfd_error_file_too_big);
8871 1.14 christos goto error_return_verref;
8872 1.14 christos }
8873 1.5 christos if (amt == 0)
8874 1.1 christos goto error_return_verref;
8875 1.1 christos elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
8876 1.1 christos if (elf_tdata (abfd)->verref == NULL)
8877 1.1 christos goto error_return_verref;
8878 1.1 christos
8879 1.1 christos BFD_ASSERT (sizeof (Elf_External_Verneed)
8880 1.1 christos == sizeof (Elf_External_Vernaux));
8881 1.1 christos contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
8882 1.1 christos everneed = (Elf_External_Verneed *) contents;
8883 1.1 christos iverneed = elf_tdata (abfd)->verref;
8884 1.1 christos for (i = 0; i < hdr->sh_info; i++, iverneed++)
8885 1.1 christos {
8886 1.1 christos Elf_External_Vernaux *evernaux;
8887 1.1 christos Elf_Internal_Vernaux *ivernaux;
8888 1.1 christos unsigned int j;
8889 1.1 christos
8890 1.1 christos _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8891 1.1 christos
8892 1.1 christos iverneed->vn_bfd = abfd;
8893 1.1 christos
8894 1.1 christos iverneed->vn_filename =
8895 1.5 christos bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8896 1.1 christos iverneed->vn_file);
8897 1.1 christos if (iverneed->vn_filename == NULL)
8898 1.1 christos goto error_return_bad_verref;
8899 1.1 christos
8900 1.1 christos if (iverneed->vn_cnt == 0)
8901 1.12 christos iverneed->vn_auxptr = NULL;
8902 1.12 christos else
8903 1.12 christos {
8904 1.12 christos if (_bfd_mul_overflow (iverneed->vn_cnt,
8905 1.12 christos sizeof (Elf_Internal_Vernaux), &amt))
8906 1.12 christos {
8907 1.1 christos bfd_set_error (bfd_error_file_too_big);
8908 1.12 christos goto error_return_verref;
8909 1.1 christos }
8910 1.1 christos iverneed->vn_auxptr = (struct elf_internal_vernaux *)
8911 1.1 christos bfd_alloc (abfd, amt);
8912 1.1 christos if (iverneed->vn_auxptr == NULL)
8913 1.1 christos goto error_return_verref;
8914 1.1 christos }
8915 1.5 christos
8916 1.1 christos if (iverneed->vn_aux
8917 1.1 christos > (size_t) (contents_end - (bfd_byte *) everneed))
8918 1.1 christos goto error_return_bad_verref;
8919 1.1 christos
8920 1.1 christos evernaux = ((Elf_External_Vernaux *)
8921 1.1 christos ((bfd_byte *) everneed + iverneed->vn_aux));
8922 1.1 christos ivernaux = iverneed->vn_auxptr;
8923 1.1 christos for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8924 1.1 christos {
8925 1.1 christos _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8926 1.1 christos
8927 1.1 christos ivernaux->vna_nodename =
8928 1.5 christos bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8929 1.5 christos ivernaux->vna_name);
8930 1.5 christos if (ivernaux->vna_nodename == NULL)
8931 1.5 christos goto error_return_bad_verref;
8932 1.1 christos
8933 1.5 christos if (ivernaux->vna_other > freeidx)
8934 1.5 christos freeidx = ivernaux->vna_other;
8935 1.5 christos
8936 1.5 christos ivernaux->vna_nextptr = NULL;
8937 1.5 christos if (ivernaux->vna_next == 0)
8938 1.5 christos {
8939 1.1 christos iverneed->vn_cnt = j + 1;
8940 1.1 christos break;
8941 1.1 christos }
8942 1.1 christos if (j + 1 < iverneed->vn_cnt)
8943 1.1 christos ivernaux->vna_nextptr = ivernaux + 1;
8944 1.5 christos
8945 1.1 christos if (ivernaux->vna_next
8946 1.1 christos > (size_t) (contents_end - (bfd_byte *) evernaux))
8947 1.1 christos goto error_return_bad_verref;
8948 1.1 christos
8949 1.1 christos evernaux = ((Elf_External_Vernaux *)
8950 1.5 christos ((bfd_byte *) evernaux + ivernaux->vna_next));
8951 1.5 christos }
8952 1.5 christos
8953 1.1 christos iverneed->vn_nextref = NULL;
8954 1.1 christos if (iverneed->vn_next == 0)
8955 1.1 christos break;
8956 1.1 christos if (i + 1 < hdr->sh_info)
8957 1.1 christos iverneed->vn_nextref = iverneed + 1;
8958 1.5 christos
8959 1.1 christos if (iverneed->vn_next
8960 1.1 christos > (size_t) (contents_end - (bfd_byte *) everneed))
8961 1.1 christos goto error_return_bad_verref;
8962 1.1 christos
8963 1.5 christos everneed = ((Elf_External_Verneed *)
8964 1.1 christos ((bfd_byte *) everneed + iverneed->vn_next));
8965 1.1 christos }
8966 1.1 christos elf_tdata (abfd)->cverrefs = i;
8967 1.1 christos
8968 1.1 christos free (contents);
8969 1.1 christos contents = NULL;
8970 1.1 christos }
8971 1.1 christos
8972 1.1 christos if (elf_dynverdef (abfd) != 0)
8973 1.1 christos {
8974 1.1 christos Elf_Internal_Shdr *hdr;
8975 1.1 christos Elf_External_Verdef *everdef;
8976 1.1 christos Elf_Internal_Verdef *iverdef;
8977 1.1 christos Elf_Internal_Verdef *iverdefarr;
8978 1.1 christos Elf_Internal_Verdef iverdefmem;
8979 1.1 christos unsigned int i;
8980 1.1 christos unsigned int maxidx;
8981 1.1 christos bfd_byte *contents_end_def, *contents_end_aux;
8982 1.14 christos
8983 1.5 christos hdr = &elf_tdata (abfd)->dynverdef_hdr;
8984 1.5 christos
8985 1.9 christos if (hdr->sh_size < sizeof (Elf_External_Verdef))
8986 1.11 christos {
8987 1.5 christos error_return_bad_verdef:
8988 1.5 christos _bfd_error_handler
8989 1.5 christos (_("%pB: .gnu.version_d invalid entry"), abfd);
8990 1.5 christos bfd_set_error (bfd_error_bad_value);
8991 1.5 christos error_return_verdef:
8992 1.5 christos elf_tdata (abfd)->verdef = NULL;
8993 1.5 christos elf_tdata (abfd)->cverdefs = 0;
8994 1.12 christos goto error_return;
8995 1.12 christos }
8996 1.12 christos
8997 1.1 christos if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
8998 1.5 christos goto error_return_verdef;
8999 1.1 christos contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
9000 1.1 christos if (contents == NULL)
9001 1.1 christos goto error_return_verdef;
9002 1.1 christos
9003 1.1 christos BFD_ASSERT (sizeof (Elf_External_Verdef)
9004 1.1 christos >= sizeof (Elf_External_Verdaux));
9005 1.1 christos contents_end_def = contents + hdr->sh_size
9006 1.1 christos - sizeof (Elf_External_Verdef);
9007 1.1 christos contents_end_aux = contents + hdr->sh_size
9008 1.1 christos - sizeof (Elf_External_Verdaux);
9009 1.1 christos
9010 1.1 christos /* We know the number of entries in the section but not the maximum
9011 1.1 christos index. Therefore we have to run through all entries and find
9012 1.1 christos the maximum. */
9013 1.1 christos everdef = (Elf_External_Verdef *) contents;
9014 1.1 christos maxidx = 0;
9015 1.1 christos for (i = 0; i < hdr->sh_info; ++i)
9016 1.5 christos {
9017 1.5 christos _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9018 1.1 christos
9019 1.1 christos if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
9020 1.1 christos goto error_return_bad_verdef;
9021 1.5 christos if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
9022 1.5 christos maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
9023 1.5 christos
9024 1.1 christos if (iverdefmem.vd_next == 0)
9025 1.1 christos break;
9026 1.5 christos
9027 1.1 christos if (iverdefmem.vd_next
9028 1.1 christos > (size_t) (contents_end_def - (bfd_byte *) everdef))
9029 1.1 christos goto error_return_bad_verdef;
9030 1.1 christos
9031 1.1 christos everdef = ((Elf_External_Verdef *)
9032 1.1 christos ((bfd_byte *) everdef + iverdefmem.vd_next));
9033 1.1 christos }
9034 1.1 christos
9035 1.1 christos if (default_imported_symver)
9036 1.1 christos {
9037 1.1 christos if (freeidx > maxidx)
9038 1.1 christos maxidx = ++freeidx;
9039 1.12 christos else
9040 1.12 christos freeidx = ++maxidx;
9041 1.12 christos }
9042 1.12 christos if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
9043 1.12 christos {
9044 1.12 christos bfd_set_error (bfd_error_file_too_big);
9045 1.1 christos goto error_return_verdef;
9046 1.5 christos }
9047 1.1 christos elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9048 1.1 christos if (elf_tdata (abfd)->verdef == NULL)
9049 1.1 christos goto error_return_verdef;
9050 1.1 christos
9051 1.1 christos elf_tdata (abfd)->cverdefs = maxidx;
9052 1.1 christos
9053 1.1 christos everdef = (Elf_External_Verdef *) contents;
9054 1.1 christos iverdefarr = elf_tdata (abfd)->verdef;
9055 1.1 christos for (i = 0; i < hdr->sh_info; i++)
9056 1.1 christos {
9057 1.1 christos Elf_External_Verdaux *everdaux;
9058 1.1 christos Elf_Internal_Verdaux *iverdaux;
9059 1.1 christos unsigned int j;
9060 1.1 christos
9061 1.5 christos _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9062 1.1 christos
9063 1.1 christos if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
9064 1.8 christos goto error_return_bad_verdef;
9065 1.1 christos
9066 1.1 christos iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
9067 1.1 christos memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
9068 1.1 christos
9069 1.1 christos iverdef->vd_bfd = abfd;
9070 1.1 christos
9071 1.1 christos if (iverdef->vd_cnt == 0)
9072 1.12 christos iverdef->vd_auxptr = NULL;
9073 1.12 christos else
9074 1.12 christos {
9075 1.12 christos if (_bfd_mul_overflow (iverdef->vd_cnt,
9076 1.12 christos sizeof (Elf_Internal_Verdaux), &amt))
9077 1.12 christos {
9078 1.1 christos bfd_set_error (bfd_error_file_too_big);
9079 1.12 christos goto error_return_verdef;
9080 1.1 christos }
9081 1.1 christos iverdef->vd_auxptr = (struct elf_internal_verdaux *)
9082 1.1 christos bfd_alloc (abfd, amt);
9083 1.1 christos if (iverdef->vd_auxptr == NULL)
9084 1.1 christos goto error_return_verdef;
9085 1.1 christos }
9086 1.5 christos
9087 1.1 christos if (iverdef->vd_aux
9088 1.1 christos > (size_t) (contents_end_aux - (bfd_byte *) everdef))
9089 1.1 christos goto error_return_bad_verdef;
9090 1.1 christos
9091 1.1 christos everdaux = ((Elf_External_Verdaux *)
9092 1.1 christos ((bfd_byte *) everdef + iverdef->vd_aux));
9093 1.1 christos iverdaux = iverdef->vd_auxptr;
9094 1.1 christos for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9095 1.1 christos {
9096 1.1 christos _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9097 1.1 christos
9098 1.1 christos iverdaux->vda_nodename =
9099 1.5 christos bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9100 1.1 christos iverdaux->vda_name);
9101 1.5 christos if (iverdaux->vda_nodename == NULL)
9102 1.5 christos goto error_return_bad_verdef;
9103 1.5 christos
9104 1.5 christos iverdaux->vda_nextptr = NULL;
9105 1.5 christos if (iverdaux->vda_next == 0)
9106 1.5 christos {
9107 1.1 christos iverdef->vd_cnt = j + 1;
9108 1.1 christos break;
9109 1.1 christos }
9110 1.1 christos if (j + 1 < iverdef->vd_cnt)
9111 1.1 christos iverdaux->vda_nextptr = iverdaux + 1;
9112 1.5 christos
9113 1.1 christos if (iverdaux->vda_next
9114 1.1 christos > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
9115 1.1 christos goto error_return_bad_verdef;
9116 1.1 christos
9117 1.1 christos everdaux = ((Elf_External_Verdaux *)
9118 1.8 christos ((bfd_byte *) everdaux + iverdaux->vda_next));
9119 1.1 christos }
9120 1.1 christos
9121 1.1 christos iverdef->vd_nodename = NULL;
9122 1.5 christos if (iverdef->vd_cnt)
9123 1.5 christos iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
9124 1.5 christos
9125 1.1 christos iverdef->vd_nextdef = NULL;
9126 1.1 christos if (iverdef->vd_next == 0)
9127 1.1 christos break;
9128 1.1 christos if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
9129 1.1 christos iverdef->vd_nextdef = iverdef + 1;
9130 1.1 christos
9131 1.1 christos everdef = ((Elf_External_Verdef *)
9132 1.1 christos ((bfd_byte *) everdef + iverdef->vd_next));
9133 1.1 christos }
9134 1.1 christos
9135 1.1 christos free (contents);
9136 1.1 christos contents = NULL;
9137 1.1 christos }
9138 1.1 christos else if (default_imported_symver)
9139 1.1 christos {
9140 1.1 christos if (freeidx < 3)
9141 1.1 christos freeidx = 3;
9142 1.12 christos else
9143 1.12 christos freeidx++;
9144 1.12 christos
9145 1.12 christos if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9146 1.12 christos {
9147 1.12 christos bfd_set_error (bfd_error_file_too_big);
9148 1.1 christos goto error_return;
9149 1.1 christos }
9150 1.1 christos elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9151 1.1 christos if (elf_tdata (abfd)->verdef == NULL)
9152 1.1 christos goto error_return;
9153 1.1 christos
9154 1.1 christos elf_tdata (abfd)->cverdefs = freeidx;
9155 1.1 christos }
9156 1.1 christos
9157 1.1 christos /* Create a default version based on the soname. */
9158 1.1 christos if (default_imported_symver)
9159 1.1 christos {
9160 1.3 christos Elf_Internal_Verdef *iverdef;
9161 1.1 christos Elf_Internal_Verdaux *iverdaux;
9162 1.1 christos
9163 1.1 christos iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
9164 1.1 christos
9165 1.1 christos iverdef->vd_version = VER_DEF_CURRENT;
9166 1.1 christos iverdef->vd_flags = 0;
9167 1.1 christos iverdef->vd_ndx = freeidx;
9168 1.1 christos iverdef->vd_cnt = 1;
9169 1.1 christos
9170 1.1 christos iverdef->vd_bfd = abfd;
9171 1.1 christos
9172 1.1 christos iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9173 1.5 christos if (iverdef->vd_nodename == NULL)
9174 1.5 christos goto error_return_verdef;
9175 1.1 christos iverdef->vd_nextdef = NULL;
9176 1.1 christos iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9177 1.1 christos bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
9178 1.1 christos if (iverdef->vd_auxptr == NULL)
9179 1.1 christos goto error_return_verdef;
9180 1.1 christos
9181 1.1 christos iverdaux = iverdef->vd_auxptr;
9182 1.14 christos iverdaux->vda_nodename = iverdef->vd_nodename;
9183 1.1 christos }
9184 1.1 christos
9185 1.12 christos return true;
9186 1.14 christos
9187 1.1 christos error_return:
9188 1.1 christos free (contents);
9189 1.1 christos return false;
9190 1.1 christos }
9191 1.1 christos
9192 1.1 christos asymbol *
9194 1.12 christos _bfd_elf_make_empty_symbol (bfd *abfd)
9195 1.1 christos {
9196 1.1 christos elf_symbol_type *newsym;
9197 1.5 christos
9198 1.5 christos newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
9199 1.1 christos if (!newsym)
9200 1.1 christos return NULL;
9201 1.1 christos newsym->symbol.the_bfd = abfd;
9202 1.1 christos return &newsym->symbol;
9203 1.1 christos }
9204 1.1 christos
9205 1.1 christos void
9206 1.1 christos _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9207 1.1 christos asymbol *symbol,
9208 1.1 christos symbol_info *ret)
9209 1.1 christos {
9210 1.1 christos bfd_symbol_info (symbol, ret);
9211 1.1 christos }
9212 1.1 christos
9213 1.14 christos /* Return whether a symbol name implies a local symbol. Most targets
9214 1.1 christos use this function for the is_local_label_name entry point, but some
9215 1.1 christos override it. */
9216 1.1 christos
9217 1.1 christos bool
9218 1.1 christos _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9219 1.14 christos const char *name)
9220 1.1 christos {
9221 1.1 christos /* Normal local symbols start with ``.L''. */
9222 1.1 christos if (name[0] == '.' && name[1] == 'L')
9223 1.1 christos return true;
9224 1.14 christos
9225 1.1 christos /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9226 1.1 christos DWARF debugging symbols starting with ``..''. */
9227 1.1 christos if (name[0] == '.' && name[1] == '.')
9228 1.1 christos return true;
9229 1.1 christos
9230 1.1 christos /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9231 1.1 christos emitting DWARF debugging output. I suspect this is actually a
9232 1.1 christos small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9233 1.14 christos ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9234 1.1 christos underscore to be emitted on some ELF targets). For ease of use,
9235 1.6 christos we treat such symbols as local. */
9236 1.6 christos if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
9237 1.6 christos return true;
9238 1.6 christos
9239 1.11 christos /* Treat assembler generated fake symbols, dollar local labels and
9240 1.6 christos forward-backward labels (aka local labels) as locals.
9241 1.6 christos These labels have the form:
9242 1.6 christos
9243 1.6 christos L0^A.* (fake symbols)
9244 1.6 christos
9245 1.6 christos [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9246 1.6 christos
9247 1.14 christos Versions which start with .L will have already been matched above,
9248 1.6 christos so we only need to match the rest. */
9249 1.6 christos if (name[0] == 'L' && ISDIGIT (name[1]))
9250 1.6 christos {
9251 1.6 christos bool ret = false;
9252 1.6 christos const char * p;
9253 1.6 christos char c;
9254 1.6 christos
9255 1.6 christos for (p = name + 2; (c = *p); p++)
9256 1.6 christos {
9257 1.14 christos if (c == 1 || c == 2)
9258 1.6 christos {
9259 1.6 christos if (c == 1 && p == name + 2)
9260 1.6 christos /* A fake symbol. */
9261 1.6 christos return true;
9262 1.6 christos
9263 1.6 christos /* FIXME: We are being paranoid here and treating symbols like
9264 1.14 christos L0^Bfoo as if there were non-local, on the grounds that the
9265 1.6 christos assembler will never generate them. But can any symbol
9266 1.6 christos containing an ASCII value in the range 1-31 ever be anything
9267 1.6 christos other than some kind of local ? */
9268 1.6 christos ret = true;
9269 1.14 christos }
9270 1.6 christos
9271 1.6 christos if (! ISDIGIT (c))
9272 1.6 christos {
9273 1.6 christos ret = false;
9274 1.6 christos break;
9275 1.6 christos }
9276 1.14 christos }
9277 1.1 christos return ret;
9278 1.1 christos }
9279 1.1 christos
9280 1.1 christos return false;
9281 1.1 christos }
9282 1.1 christos
9283 1.1 christos alent *
9284 1.1 christos _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9285 1.1 christos asymbol *symbol ATTRIBUTE_UNUSED)
9286 1.1 christos {
9287 1.14 christos abort ();
9288 1.1 christos return NULL;
9289 1.1 christos }
9290 1.1 christos
9291 1.1 christos bool
9292 1.1 christos _bfd_elf_set_arch_mach (bfd *abfd,
9293 1.1 christos enum bfd_architecture arch,
9294 1.1 christos unsigned long machine)
9295 1.1 christos {
9296 1.1 christos /* If this isn't the right architecture for this backend, and this
9297 1.14 christos isn't the generic backend, fail. */
9298 1.1 christos if (arch != get_elf_backend_data (abfd)->arch
9299 1.1 christos && arch != bfd_arch_unknown
9300 1.1 christos && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
9301 1.1 christos return false;
9302 1.1 christos
9303 1.1 christos return bfd_default_set_arch_mach (abfd, arch, machine);
9304 1.1 christos }
9305 1.14 christos
9306 1.1 christos /* Find the nearest line to a particular section and offset,
9307 1.5 christos for error reporting. */
9308 1.1 christos
9309 1.1 christos bool
9310 1.1 christos _bfd_elf_find_nearest_line (bfd *abfd,
9311 1.1 christos asymbol **symbols,
9312 1.5 christos asection *section,
9313 1.5 christos bfd_vma offset,
9314 1.3 christos const char **filename_ptr,
9315 1.14 christos const char **functionname_ptr,
9316 1.14 christos unsigned int *line_ptr,
9317 1.14 christos unsigned int *discriminator_ptr)
9318 1.14 christos {
9319 1.14 christos return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
9320 1.14 christos offset, filename_ptr,
9321 1.14 christos functionname_ptr, line_ptr,
9322 1.14 christos discriminator_ptr);
9323 1.14 christos }
9324 1.1 christos
9325 1.14 christos /* Find the nearest line to a particular section and offset,
9326 1.14 christos for error reporting. ALT_BFD representing a .gnu_debugaltlink file
9327 1.14 christos can be optionally specified. */
9328 1.14 christos
9329 1.14 christos bool
9330 1.14 christos _bfd_elf_find_nearest_line_with_alt (bfd *abfd,
9331 1.14 christos const char *alt_filename,
9332 1.14 christos asymbol **symbols,
9333 1.14 christos asection *section,
9334 1.14 christos bfd_vma offset,
9335 1.14 christos const char **filename_ptr,
9336 1.14 christos const char **functionname_ptr,
9337 1.14 christos unsigned int *line_ptr,
9338 1.14 christos unsigned int *discriminator_ptr)
9339 1.14 christos {
9340 1.14 christos bool found;
9341 1.14 christos
9342 1.14 christos if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
9343 1.14 christos section, offset, filename_ptr,
9344 1.14 christos functionname_ptr, line_ptr,
9345 1.12 christos discriminator_ptr,
9346 1.12 christos dwarf_debug_sections,
9347 1.12 christos &elf_tdata (abfd)->dwarf2_find_line_info))
9348 1.1 christos return true;
9349 1.1 christos
9350 1.5 christos if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9351 1.5 christos filename_ptr, functionname_ptr, line_ptr))
9352 1.5 christos {
9353 1.14 christos if (!*functionname_ptr)
9354 1.1 christos _bfd_elf_find_function (abfd, symbols, section, offset,
9355 1.1 christos *filename_ptr ? NULL : filename_ptr,
9356 1.1 christos functionname_ptr);
9357 1.1 christos return true;
9358 1.1 christos }
9359 1.1 christos
9360 1.14 christos if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
9361 1.1 christos &found, filename_ptr,
9362 1.14 christos functionname_ptr, line_ptr,
9363 1.1 christos &elf_tdata (abfd)->line_info))
9364 1.1 christos return false;
9365 1.14 christos if (found && (*functionname_ptr || *line_ptr))
9366 1.1 christos return true;
9367 1.5 christos
9368 1.5 christos if (symbols == NULL)
9369 1.14 christos return false;
9370 1.1 christos
9371 1.1 christos if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9372 1.14 christos filename_ptr, functionname_ptr))
9373 1.1 christos return false;
9374 1.1 christos
9375 1.1 christos *line_ptr = 0;
9376 1.1 christos return true;
9377 1.14 christos }
9378 1.1 christos
9379 1.1 christos /* Find the line for a symbol. */
9380 1.1 christos
9381 1.14 christos bool
9382 1.5 christos _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9383 1.5 christos const char **filename_ptr, unsigned int *line_ptr)
9384 1.12 christos {
9385 1.14 christos struct elf_obj_tdata *tdata = elf_tdata (abfd);
9386 1.1 christos return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9387 1.1 christos filename_ptr, NULL, line_ptr, NULL,
9388 1.1 christos dwarf_debug_sections,
9389 1.1 christos &tdata->dwarf2_find_line_info);
9390 1.1 christos }
9391 1.1 christos
9392 1.1 christos /* After a call to bfd_find_nearest_line, successive calls to
9393 1.1 christos bfd_find_inliner_info can be used to get source information about
9394 1.14 christos each level of function inlining that terminated at the address
9395 1.1 christos passed to bfd_find_nearest_line. Currently this is only supported
9396 1.1 christos for DWARF2 with appropriate DWARF3 extensions. */
9397 1.1 christos
9398 1.1 christos bool
9399 1.1 christos _bfd_elf_find_inliner_info (bfd *abfd,
9400 1.14 christos const char **filename_ptr,
9401 1.14 christos const char **functionname_ptr,
9402 1.14 christos unsigned int *line_ptr)
9403 1.14 christos {
9404 1.1 christos struct elf_obj_tdata *tdata = elf_tdata (abfd);
9405 1.1 christos return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9406 1.1 christos functionname_ptr, line_ptr,
9407 1.1 christos &tdata->dwarf2_find_line_info);
9408 1.1 christos }
9409 1.1 christos
9410 1.1 christos int
9411 1.1 christos _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
9412 1.8 christos {
9413 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9414 1.3 christos int ret = bed->s->sizeof_ehdr;
9415 1.1 christos
9416 1.1 christos if (!bfd_link_relocatable (info))
9417 1.1 christos {
9418 1.1 christos bfd_size_type phdr_size = elf_program_header_size (abfd);
9419 1.1 christos
9420 1.1 christos if (phdr_size == (bfd_size_type) -1)
9421 1.3 christos {
9422 1.1 christos struct elf_segment_map *m;
9423 1.1 christos
9424 1.1 christos phdr_size = 0;
9425 1.1 christos for (m = elf_seg_map (abfd); m != NULL; m = m->next)
9426 1.1 christos phdr_size += bed->s->sizeof_phdr;
9427 1.1 christos
9428 1.3 christos if (phdr_size == 0)
9429 1.1 christos phdr_size = get_program_header_size (abfd, info);
9430 1.1 christos }
9431 1.1 christos
9432 1.1 christos elf_program_header_size (abfd) = phdr_size;
9433 1.1 christos ret += phdr_size;
9434 1.1 christos }
9435 1.14 christos
9436 1.1 christos return ret;
9437 1.1 christos }
9438 1.1 christos
9439 1.1 christos bool
9440 1.1 christos _bfd_elf_set_section_contents (bfd *abfd,
9441 1.1 christos sec_ptr section,
9442 1.1 christos const void *location,
9443 1.1 christos file_ptr offset,
9444 1.1 christos bfd_size_type count)
9445 1.1 christos {
9446 1.14 christos Elf_Internal_Shdr *hdr;
9447 1.1 christos
9448 1.6 christos if (! abfd->output_has_begun
9449 1.14 christos && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
9450 1.6 christos return false;
9451 1.1 christos
9452 1.6 christos if (!count)
9453 1.6 christos return true;
9454 1.12 christos
9455 1.12 christos hdr = &elf_section_data (section)->this_hdr;
9456 1.12 christos if (hdr->sh_offset == (file_ptr) -1)
9457 1.12 christos {
9458 1.12 christos unsigned char *contents;
9459 1.14 christos
9460 1.12 christos if (bfd_section_is_ctf (section))
9461 1.12 christos /* Nothing to do with this section: the contents are generated
9462 1.12 christos later. */
9463 1.12 christos return true;
9464 1.14 christos
9465 1.14 christos if ((offset + count) > hdr->sh_size)
9466 1.12 christos {
9467 1.12 christos _bfd_error_handler
9468 1.12 christos (_("%pB:%pA: error: attempting to write"
9469 1.14 christos " over the end of the section"),
9470 1.12 christos abfd, section);
9471 1.12 christos
9472 1.12 christos bfd_set_error (bfd_error_invalid_operation);
9473 1.12 christos return false;
9474 1.12 christos }
9475 1.12 christos
9476 1.14 christos contents = hdr->contents;
9477 1.14 christos if (contents == NULL)
9478 1.12 christos {
9479 1.12 christos _bfd_error_handler
9480 1.12 christos (_("%pB:%pA: error: attempting to write"
9481 1.14 christos " section into an empty buffer"),
9482 1.12 christos abfd, section);
9483 1.12 christos
9484 1.6 christos bfd_set_error (bfd_error_invalid_operation);
9485 1.14 christos return false;
9486 1.6 christos }
9487 1.12 christos
9488 1.14 christos memcpy (contents + offset, location, count);
9489 1.14 christos return true;
9490 1.1 christos }
9491 1.1 christos
9492 1.14 christos return _bfd_generic_set_section_contents (abfd, section,
9493 1.1 christos location, offset, count);
9494 1.1 christos }
9495 1.1 christos
9496 1.1 christos bool
9497 1.1 christos _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
9498 1.14 christos arelent *cache_ptr ATTRIBUTE_UNUSED,
9499 1.1 christos Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
9500 1.1 christos {
9501 1.1 christos abort ();
9502 1.1 christos return false;
9503 1.14 christos }
9504 1.1 christos
9505 1.1 christos /* Try to convert a non-ELF reloc into an ELF one. */
9506 1.1 christos
9507 1.1 christos bool
9508 1.1 christos _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
9509 1.1 christos {
9510 1.1 christos /* Check whether we really have an ELF howto. */
9511 1.1 christos
9512 1.1 christos if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
9513 1.1 christos {
9514 1.1 christos bfd_reloc_code_real_type code;
9515 1.1 christos reloc_howto_type *howto;
9516 1.1 christos
9517 1.1 christos /* Alien reloc: Try to determine its type to replace it with an
9518 1.1 christos equivalent ELF reloc. */
9519 1.1 christos
9520 1.1 christos if (areloc->howto->pc_relative)
9521 1.1 christos {
9522 1.1 christos switch (areloc->howto->bitsize)
9523 1.1 christos {
9524 1.1 christos case 8:
9525 1.1 christos code = BFD_RELOC_8_PCREL;
9526 1.1 christos break;
9527 1.1 christos case 12:
9528 1.1 christos code = BFD_RELOC_12_PCREL;
9529 1.1 christos break;
9530 1.1 christos case 16:
9531 1.1 christos code = BFD_RELOC_16_PCREL;
9532 1.1 christos break;
9533 1.1 christos case 24:
9534 1.1 christos code = BFD_RELOC_24_PCREL;
9535 1.1 christos break;
9536 1.1 christos case 32:
9537 1.1 christos code = BFD_RELOC_32_PCREL;
9538 1.1 christos break;
9539 1.1 christos case 64:
9540 1.1 christos code = BFD_RELOC_64_PCREL;
9541 1.1 christos break;
9542 1.1 christos default:
9543 1.1 christos goto fail;
9544 1.12 christos }
9545 1.1 christos
9546 1.1 christos howto = bfd_reloc_type_lookup (abfd, code);
9547 1.1 christos
9548 1.1 christos if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
9549 1.1 christos {
9550 1.1 christos if (howto->pcrel_offset)
9551 1.1 christos areloc->addend += areloc->address;
9552 1.1 christos else
9553 1.1 christos areloc->addend -= areloc->address; /* addend is unsigned!! */
9554 1.1 christos }
9555 1.1 christos }
9556 1.1 christos else
9557 1.1 christos {
9558 1.1 christos switch (areloc->howto->bitsize)
9559 1.1 christos {
9560 1.1 christos case 8:
9561 1.1 christos code = BFD_RELOC_8;
9562 1.1 christos break;
9563 1.1 christos case 14:
9564 1.1 christos code = BFD_RELOC_14;
9565 1.1 christos break;
9566 1.1 christos case 16:
9567 1.1 christos code = BFD_RELOC_16;
9568 1.1 christos break;
9569 1.1 christos case 26:
9570 1.1 christos code = BFD_RELOC_26;
9571 1.1 christos break;
9572 1.1 christos case 32:
9573 1.1 christos code = BFD_RELOC_32;
9574 1.1 christos break;
9575 1.1 christos case 64:
9576 1.1 christos code = BFD_RELOC_64;
9577 1.1 christos break;
9578 1.1 christos default:
9579 1.1 christos goto fail;
9580 1.1 christos }
9581 1.1 christos
9582 1.1 christos howto = bfd_reloc_type_lookup (abfd, code);
9583 1.1 christos }
9584 1.1 christos
9585 1.1 christos if (howto)
9586 1.1 christos areloc->howto = howto;
9587 1.14 christos else
9588 1.1 christos goto fail;
9589 1.1 christos }
9590 1.11 christos
9591 1.11 christos return true;
9592 1.11 christos
9593 1.12 christos fail:
9594 1.14 christos /* xgettext:c-format */
9595 1.1 christos _bfd_error_handler (_("%pB: %s unsupported"),
9596 1.1 christos abfd, areloc->howto->name);
9597 1.14 christos bfd_set_error (bfd_error_sorry);
9598 1.1 christos return false;
9599 1.1 christos }
9600 1.3 christos
9601 1.12 christos bool
9602 1.12 christos _bfd_elf_close_and_cleanup (bfd *abfd)
9603 1.12 christos {
9604 1.1 christos struct elf_obj_tdata *tdata = elf_tdata (abfd);
9605 1.3 christos if (tdata != NULL
9606 1.1 christos && (bfd_get_format (abfd) == bfd_object
9607 1.3 christos || bfd_get_format (abfd) == bfd_core))
9608 1.14 christos {
9609 1.1 christos if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
9610 1.1 christos _bfd_elf_strtab_free (elf_shstrtab (abfd));
9611 1.1 christos _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
9612 1.1 christos _bfd_stab_cleanup (abfd, &tdata->line_info);
9613 1.1 christos }
9614 1.1 christos
9615 1.1 christos return _bfd_generic_close_and_cleanup (abfd);
9616 1.1 christos }
9617 1.1 christos
9618 1.1 christos /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9619 1.1 christos in the relocation's offset. Thus we cannot allow any sort of sanity
9620 1.1 christos range-checking to interfere. There is nothing else to do in processing
9621 1.1 christos this reloc. */
9622 1.1 christos
9623 1.1 christos bfd_reloc_status_type
9624 1.1 christos _bfd_elf_rel_vtable_reloc_fn
9625 1.1 christos (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
9626 1.1 christos struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
9627 1.1 christos void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9628 1.1 christos bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
9629 1.1 christos {
9630 1.1 christos return bfd_reloc_ok;
9631 1.1 christos }
9632 1.1 christos
9633 1.1 christos /* Elf core file support. Much of this only works on native
9635 1.1 christos toolchains, since we rely on knowing the
9636 1.1 christos machine-dependent procfs structure in order to pick
9637 1.1 christos out details about the corefile. */
9638 1.1 christos
9639 1.1 christos #ifdef HAVE_SYS_PROCFS_H
9640 1.1 christos # include <sys/procfs.h>
9641 1.1 christos #endif
9642 1.1 christos
9643 1.1 christos /* Return a PID that identifies a "thread" for threaded cores, or the
9644 1.1 christos PID of the main process for non-threaded cores. */
9645 1.1 christos
9646 1.3 christos static int
9647 1.1 christos elfcore_make_pid (bfd *abfd)
9648 1.3 christos {
9649 1.1 christos int pid;
9650 1.1 christos
9651 1.1 christos pid = elf_tdata (abfd)->core->lwpid;
9652 1.1 christos if (pid == 0)
9653 1.14 christos pid = elf_tdata (abfd)->core->pid;
9654 1.14 christos
9655 1.14 christos return pid;
9656 1.1 christos }
9657 1.14 christos
9658 1.1 christos /* If there isn't a section called NAME, make one, using data from
9659 1.1 christos SECT. Note, this function will generate a reference to NAME, so
9660 1.1 christos you shouldn't deallocate or overwrite it. */
9661 1.1 christos
9662 1.1 christos static bool
9663 1.14 christos elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
9664 1.1 christos {
9665 1.1 christos asection *sect2;
9666 1.1 christos
9667 1.14 christos if (bfd_get_section_by_name (abfd, name) != NULL)
9668 1.1 christos return true;
9669 1.1 christos
9670 1.1 christos sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
9671 1.1 christos if (sect2 == NULL)
9672 1.14 christos return false;
9673 1.1 christos
9674 1.1 christos sect2->size = sect->size;
9675 1.1 christos sect2->filepos = sect->filepos;
9676 1.1 christos sect2->alignment_power = sect->alignment_power;
9677 1.1 christos return true;
9678 1.1 christos }
9679 1.1 christos
9680 1.1 christos /* Create a pseudosection containing SIZE bytes at FILEPOS. This
9681 1.11 christos actually creates up to two pseudosections:
9682 1.14 christos - For the single-threaded case, a section named NAME, unless
9683 1.1 christos such a section already exists.
9684 1.1 christos - For the multi-threaded case, a section named "NAME/PID", where
9685 1.1 christos PID is elfcore_make_pid (abfd).
9686 1.1 christos Both pseudosections have identical contents. */
9687 1.1 christos bool
9688 1.1 christos _bfd_elfcore_make_pseudosection (bfd *abfd,
9689 1.1 christos char *name,
9690 1.1 christos size_t size,
9691 1.1 christos ufile_ptr filepos)
9692 1.1 christos {
9693 1.1 christos char buf[100];
9694 1.1 christos char *threaded_name;
9695 1.1 christos size_t len;
9696 1.1 christos asection *sect;
9697 1.1 christos
9698 1.1 christos /* Build the section name. */
9699 1.14 christos
9700 1.1 christos sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
9701 1.1 christos len = strlen (buf) + 1;
9702 1.1 christos threaded_name = (char *) bfd_alloc (abfd, len);
9703 1.1 christos if (threaded_name == NULL)
9704 1.1 christos return false;
9705 1.14 christos memcpy (threaded_name, buf, len);
9706 1.1 christos
9707 1.1 christos sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9708 1.1 christos SEC_HAS_CONTENTS);
9709 1.1 christos if (sect == NULL)
9710 1.1 christos return false;
9711 1.1 christos sect->size = size;
9712 1.1 christos sect->filepos = filepos;
9713 1.14 christos sect->alignment_power = 2;
9714 1.12 christos
9715 1.12 christos return elfcore_maybe_make_sect (abfd, name, sect);
9716 1.12 christos }
9717 1.12 christos
9718 1.12 christos static bool
9719 1.12 christos elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
9720 1.12 christos size_t offs)
9721 1.14 christos {
9722 1.12 christos asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9723 1.12 christos SEC_HAS_CONTENTS);
9724 1.12 christos
9725 1.12 christos if (sect == NULL)
9726 1.12 christos return false;
9727 1.14 christos
9728 1.12 christos sect->size = note->descsz - offs;
9729 1.12 christos sect->filepos = note->descpos + offs;
9730 1.1 christos sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9731 1.1 christos
9732 1.1 christos return true;
9733 1.1 christos }
9734 1.1 christos
9735 1.1 christos /* prstatus_t exists on:
9736 1.1 christos solaris 2.5+
9737 1.1 christos linux 2.[01] + glibc
9738 1.14 christos unixware 4.2
9739 1.1 christos */
9740 1.1 christos
9741 1.1 christos #if defined (HAVE_PRSTATUS_T)
9742 1.1 christos
9743 1.1 christos static bool
9744 1.1 christos elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
9745 1.1 christos {
9746 1.1 christos size_t size;
9747 1.1 christos int offset;
9748 1.1 christos
9749 1.1 christos if (note->descsz == sizeof (prstatus_t))
9750 1.1 christos {
9751 1.1 christos prstatus_t prstat;
9752 1.1 christos
9753 1.1 christos size = sizeof (prstat.pr_reg);
9754 1.3 christos offset = offsetof (prstatus_t, pr_reg);
9755 1.3 christos memcpy (&prstat, note->descdata, sizeof (prstat));
9756 1.3 christos
9757 1.3 christos /* Do not overwrite the core signal if it
9758 1.1 christos has already been set by another thread. */
9759 1.1 christos if (elf_tdata (abfd)->core->signal == 0)
9760 1.1 christos elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9761 1.1 christos if (elf_tdata (abfd)->core->pid == 0)
9762 1.1 christos elf_tdata (abfd)->core->pid = prstat.pr_pid;
9763 1.1 christos
9764 1.1 christos /* pr_who exists on:
9765 1.1 christos solaris 2.5+
9766 1.3 christos unixware 4.2
9767 1.1 christos pr_who doesn't exist on:
9768 1.3 christos linux 2.[01]
9769 1.1 christos */
9770 1.1 christos #if defined (HAVE_PRSTATUS_T_PR_WHO)
9771 1.1 christos elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9772 1.1 christos #else
9773 1.1 christos elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9774 1.1 christos #endif
9775 1.1 christos }
9776 1.1 christos #if defined (HAVE_PRSTATUS32_T)
9777 1.1 christos else if (note->descsz == sizeof (prstatus32_t))
9778 1.1 christos {
9779 1.1 christos /* 64-bit host, 32-bit corefile */
9780 1.1 christos prstatus32_t prstat;
9781 1.1 christos
9782 1.1 christos size = sizeof (prstat.pr_reg);
9783 1.3 christos offset = offsetof (prstatus32_t, pr_reg);
9784 1.3 christos memcpy (&prstat, note->descdata, sizeof (prstat));
9785 1.3 christos
9786 1.3 christos /* Do not overwrite the core signal if it
9787 1.1 christos has already been set by another thread. */
9788 1.1 christos if (elf_tdata (abfd)->core->signal == 0)
9789 1.1 christos elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9790 1.1 christos if (elf_tdata (abfd)->core->pid == 0)
9791 1.1 christos elf_tdata (abfd)->core->pid = prstat.pr_pid;
9792 1.1 christos
9793 1.1 christos /* pr_who exists on:
9794 1.1 christos solaris 2.5+
9795 1.3 christos unixware 4.2
9796 1.1 christos pr_who doesn't exist on:
9797 1.3 christos linux 2.[01]
9798 1.1 christos */
9799 1.1 christos #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9800 1.1 christos elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9801 1.1 christos #else
9802 1.1 christos elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9803 1.1 christos #endif
9804 1.1 christos }
9805 1.14 christos #endif /* HAVE_PRSTATUS32_T */
9806 1.1 christos else
9807 1.1 christos {
9808 1.1 christos /* Fail - we don't know how to handle any other
9809 1.1 christos note size (ie. data object type). */
9810 1.1 christos return true;
9811 1.1 christos }
9812 1.1 christos
9813 1.1 christos /* Make a ".reg/999" section and a ".reg" section. */
9814 1.1 christos return _bfd_elfcore_make_pseudosection (abfd, ".reg",
9815 1.14 christos size, note->descpos + offset);
9816 1.1 christos }
9817 1.1 christos #endif /* defined (HAVE_PRSTATUS_T) */
9818 1.1 christos
9819 1.1 christos /* Create a pseudosection containing the exact contents of NOTE. */
9820 1.1 christos static bool
9821 1.1 christos elfcore_make_note_pseudosection (bfd *abfd,
9822 1.1 christos char *name,
9823 1.1 christos Elf_Internal_Note *note)
9824 1.1 christos {
9825 1.1 christos return _bfd_elfcore_make_pseudosection (abfd, name,
9826 1.1 christos note->descsz, note->descpos);
9827 1.1 christos }
9828 1.14 christos
9829 1.1 christos /* There isn't a consistent prfpregset_t across platforms,
9830 1.1 christos but it doesn't matter, because we don't have to pick this
9831 1.1 christos data structure apart. */
9832 1.1 christos
9833 1.1 christos static bool
9834 1.1 christos elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
9835 1.1 christos {
9836 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9837 1.1 christos }
9838 1.14 christos
9839 1.1 christos /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9840 1.1 christos type of NT_PRXFPREG. Just include the whole note's contents
9841 1.1 christos literally. */
9842 1.1 christos
9843 1.1 christos static bool
9844 1.1 christos elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
9845 1.1 christos {
9846 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9847 1.1 christos }
9848 1.14 christos
9849 1.1 christos /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9850 1.1 christos with a note type of NT_X86_XSTATE. Just include the whole note's
9851 1.1 christos contents literally. */
9852 1.1 christos
9853 1.1 christos static bool
9854 1.14 christos elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9855 1.1 christos {
9856 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9857 1.1 christos }
9858 1.1 christos
9859 1.1 christos static bool
9860 1.14 christos elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9861 1.1 christos {
9862 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9863 1.1 christos }
9864 1.1 christos
9865 1.1 christos static bool
9866 1.14 christos elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9867 1.11 christos {
9868 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9869 1.11 christos }
9870 1.11 christos
9871 1.11 christos static bool
9872 1.14 christos elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9873 1.11 christos {
9874 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9875 1.11 christos }
9876 1.11 christos
9877 1.11 christos static bool
9878 1.14 christos elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9879 1.11 christos {
9880 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9881 1.11 christos }
9882 1.11 christos
9883 1.11 christos static bool
9884 1.14 christos elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9885 1.11 christos {
9886 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9887 1.11 christos }
9888 1.11 christos
9889 1.11 christos static bool
9890 1.14 christos elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9891 1.11 christos {
9892 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9893 1.11 christos }
9894 1.11 christos
9895 1.11 christos static bool
9896 1.14 christos elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9897 1.11 christos {
9898 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9899 1.11 christos }
9900 1.11 christos
9901 1.11 christos static bool
9902 1.14 christos elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9903 1.11 christos {
9904 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9905 1.11 christos }
9906 1.11 christos
9907 1.11 christos static bool
9908 1.14 christos elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
9909 1.11 christos {
9910 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
9911 1.11 christos }
9912 1.11 christos
9913 1.11 christos static bool
9914 1.14 christos elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
9915 1.11 christos {
9916 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
9917 1.11 christos }
9918 1.11 christos
9919 1.11 christos static bool
9920 1.14 christos elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
9921 1.11 christos {
9922 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
9923 1.11 christos }
9924 1.11 christos
9925 1.11 christos static bool
9926 1.14 christos elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
9927 1.11 christos {
9928 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
9929 1.11 christos }
9930 1.11 christos
9931 1.11 christos static bool
9932 1.14 christos elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
9933 1.11 christos {
9934 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
9935 1.11 christos }
9936 1.11 christos
9937 1.11 christos static bool
9938 1.14 christos elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
9939 1.11 christos {
9940 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
9941 1.11 christos }
9942 1.11 christos
9943 1.11 christos static bool
9944 1.14 christos elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
9945 1.1 christos {
9946 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
9947 1.1 christos }
9948 1.1 christos
9949 1.1 christos static bool
9950 1.14 christos elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9951 1.1 christos {
9952 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9953 1.1 christos }
9954 1.1 christos
9955 1.1 christos static bool
9956 1.14 christos elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9957 1.1 christos {
9958 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9959 1.1 christos }
9960 1.1 christos
9961 1.1 christos static bool
9962 1.14 christos elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9963 1.1 christos {
9964 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9965 1.1 christos }
9966 1.1 christos
9967 1.1 christos static bool
9968 1.14 christos elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
9969 1.1 christos {
9970 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
9971 1.1 christos }
9972 1.1 christos
9973 1.1 christos static bool
9974 1.14 christos elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
9975 1.1 christos {
9976 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
9977 1.1 christos }
9978 1.1 christos
9979 1.1 christos static bool
9980 1.14 christos elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
9981 1.3 christos {
9982 1.3 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
9983 1.3 christos }
9984 1.3 christos
9985 1.3 christos static bool
9986 1.14 christos elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
9987 1.3 christos {
9988 1.3 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
9989 1.3 christos }
9990 1.3 christos
9991 1.3 christos static bool
9992 1.14 christos elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
9993 1.3 christos {
9994 1.3 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
9995 1.3 christos }
9996 1.3 christos
9997 1.3 christos static bool
9998 1.14 christos elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
9999 1.6 christos {
10000 1.6 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
10001 1.6 christos }
10002 1.6 christos
10003 1.6 christos static bool
10004 1.14 christos elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
10005 1.6 christos {
10006 1.6 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
10007 1.6 christos }
10008 1.6 christos
10009 1.6 christos static bool
10010 1.14 christos elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
10011 1.11 christos {
10012 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
10013 1.11 christos }
10014 1.11 christos
10015 1.11 christos static bool
10016 1.14 christos elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
10017 1.11 christos {
10018 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
10019 1.11 christos }
10020 1.11 christos
10021 1.11 christos static bool
10022 1.14 christos elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
10023 1.3 christos {
10024 1.3 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
10025 1.3 christos }
10026 1.3 christos
10027 1.3 christos static bool
10028 1.14 christos elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
10029 1.3 christos {
10030 1.3 christos return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
10031 1.3 christos }
10032 1.3 christos
10033 1.3 christos static bool
10034 1.14 christos elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
10035 1.3 christos {
10036 1.3 christos return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
10037 1.3 christos }
10038 1.3 christos
10039 1.3 christos static bool
10040 1.14 christos elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
10041 1.3 christos {
10042 1.3 christos return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
10043 1.3 christos }
10044 1.3 christos
10045 1.3 christos static bool
10046 1.14 christos elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
10047 1.11 christos {
10048 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
10049 1.11 christos }
10050 1.11 christos
10051 1.11 christos static bool
10052 1.14 christos elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
10053 1.11 christos {
10054 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
10055 1.11 christos }
10056 1.11 christos
10057 1.11 christos static bool
10058 1.14 christos elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
10059 1.14 christos {
10060 1.14 christos return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
10061 1.14 christos }
10062 1.14 christos
10063 1.14 christos static bool
10064 1.14 christos elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
10065 1.14 christos {
10066 1.12 christos return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
10067 1.12 christos note);
10068 1.12 christos }
10069 1.12 christos
10070 1.12 christos static bool
10071 1.14 christos elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
10072 1.14 christos {
10073 1.14 christos return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
10074 1.14 christos }
10075 1.14 christos
10076 1.14 christos /* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if
10077 1.14 christos successful otherwise, return FALSE. */
10078 1.14 christos
10079 1.14 christos static bool
10080 1.14 christos elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
10081 1.14 christos {
10082 1.14 christos return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
10083 1.14 christos }
10084 1.14 christos
10085 1.14 christos /* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if
10086 1.14 christos successful otherwise, return FALSE. */
10087 1.14 christos
10088 1.14 christos static bool
10089 1.14 christos elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
10090 1.14 christos {
10091 1.14 christos return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
10092 1.14 christos }
10093 1.14 christos
10094 1.14 christos static bool
10095 1.14 christos elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
10096 1.14 christos {
10097 1.14 christos return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
10098 1.14 christos }
10099 1.14 christos
10100 1.14 christos static bool
10101 1.14 christos elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
10102 1.14 christos {
10103 1.14 christos return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
10104 1.14 christos }
10105 1.14 christos
10106 1.14 christos static bool
10107 1.14 christos elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
10108 1.14 christos {
10109 1.14 christos return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
10110 1.14 christos }
10111 1.14 christos
10112 1.14 christos static bool
10113 1.1 christos elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
10114 1.1 christos {
10115 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
10116 1.1 christos }
10117 1.1 christos
10118 1.1 christos #if defined (HAVE_PRPSINFO_T)
10119 1.1 christos typedef prpsinfo_t elfcore_psinfo_t;
10120 1.1 christos #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
10121 1.1 christos typedef prpsinfo32_t elfcore_psinfo32_t;
10122 1.1 christos #endif
10123 1.1 christos #endif
10124 1.1 christos
10125 1.1 christos #if defined (HAVE_PSINFO_T)
10126 1.1 christos typedef psinfo_t elfcore_psinfo_t;
10127 1.1 christos #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
10128 1.1 christos typedef psinfo32_t elfcore_psinfo32_t;
10129 1.1 christos #endif
10130 1.1 christos #endif
10131 1.1 christos
10132 1.1 christos /* return a malloc'ed copy of a string at START which is at
10133 1.1 christos most MAX bytes long, possibly without a terminating '\0'.
10134 1.1 christos the copy will always have a terminating '\0'. */
10135 1.1 christos
10136 1.1 christos char *
10137 1.1 christos _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
10138 1.1 christos {
10139 1.1 christos char *dups;
10140 1.1 christos char *end = (char *) memchr (start, '\0', max);
10141 1.1 christos size_t len;
10142 1.1 christos
10143 1.1 christos if (end == NULL)
10144 1.1 christos len = max;
10145 1.1 christos else
10146 1.1 christos len = end - start;
10147 1.1 christos
10148 1.1 christos dups = (char *) bfd_alloc (abfd, len + 1);
10149 1.1 christos if (dups == NULL)
10150 1.1 christos return NULL;
10151 1.1 christos
10152 1.1 christos memcpy (dups, start, len);
10153 1.1 christos dups[len] = '\0';
10154 1.14 christos
10155 1.1 christos return dups;
10156 1.1 christos }
10157 1.1 christos
10158 1.1 christos #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10159 1.1 christos static bool
10160 1.1 christos elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
10161 1.1 christos {
10162 1.1 christos if (note->descsz == sizeof (elfcore_psinfo_t))
10163 1.3 christos {
10164 1.3 christos elfcore_psinfo_t psinfo;
10165 1.3 christos
10166 1.3 christos memcpy (&psinfo, note->descdata, sizeof (psinfo));
10167 1.1 christos
10168 1.1 christos #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10169 1.1 christos elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10170 1.3 christos #endif
10171 1.1 christos elf_tdata (abfd)->core->program
10172 1.1 christos = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10173 1.1 christos sizeof (psinfo.pr_fname));
10174 1.1 christos
10175 1.1 christos elf_tdata (abfd)->core->command
10176 1.1 christos = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10177 1.1 christos sizeof (psinfo.pr_psargs));
10178 1.1 christos }
10179 1.1 christos #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10180 1.1 christos else if (note->descsz == sizeof (elfcore_psinfo32_t))
10181 1.1 christos {
10182 1.3 christos /* 64-bit host, 32-bit corefile */
10183 1.3 christos elfcore_psinfo32_t psinfo;
10184 1.3 christos
10185 1.3 christos memcpy (&psinfo, note->descdata, sizeof (psinfo));
10186 1.1 christos
10187 1.1 christos #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10188 1.1 christos elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10189 1.3 christos #endif
10190 1.1 christos elf_tdata (abfd)->core->program
10191 1.1 christos = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10192 1.1 christos sizeof (psinfo.pr_fname));
10193 1.1 christos
10194 1.1 christos elf_tdata (abfd)->core->command
10195 1.1 christos = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10196 1.1 christos sizeof (psinfo.pr_psargs));
10197 1.1 christos }
10198 1.1 christos #endif
10199 1.14 christos
10200 1.1 christos else
10201 1.1 christos {
10202 1.1 christos /* Fail - we don't know how to handle any other
10203 1.1 christos note size (ie. data object type). */
10204 1.1 christos return true;
10205 1.1 christos }
10206 1.1 christos
10207 1.3 christos /* Note that for some reason, a spurious space is tacked
10208 1.1 christos onto the end of the args in some (at least one anyway)
10209 1.1 christos implementations, so strip it off if it exists. */
10210 1.1 christos
10211 1.1 christos {
10212 1.1 christos char *command = elf_tdata (abfd)->core->command;
10213 1.1 christos int n = strlen (command);
10214 1.14 christos
10215 1.1 christos if (0 < n && command[n - 1] == ' ')
10216 1.1 christos command[n - 1] = '\0';
10217 1.1 christos }
10218 1.1 christos
10219 1.14 christos return true;
10220 1.1 christos }
10221 1.1 christos #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10222 1.1 christos
10223 1.1 christos #if defined (HAVE_PSTATUS_T)
10224 1.1 christos static bool
10225 1.1 christos elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
10226 1.1 christos {
10227 1.1 christos if (note->descsz == sizeof (pstatus_t)
10228 1.1 christos #if defined (HAVE_PXSTATUS_T)
10229 1.1 christos || note->descsz == sizeof (pxstatus_t)
10230 1.1 christos #endif
10231 1.1 christos )
10232 1.3 christos {
10233 1.1 christos pstatus_t pstat;
10234 1.1 christos
10235 1.1 christos memcpy (&pstat, note->descdata, sizeof (pstat));
10236 1.1 christos
10237 1.1 christos elf_tdata (abfd)->core->pid = pstat.pr_pid;
10238 1.1 christos }
10239 1.1 christos #if defined (HAVE_PSTATUS32_T)
10240 1.1 christos else if (note->descsz == sizeof (pstatus32_t))
10241 1.1 christos {
10242 1.3 christos /* 64-bit host, 32-bit corefile */
10243 1.1 christos pstatus32_t pstat;
10244 1.1 christos
10245 1.1 christos memcpy (&pstat, note->descdata, sizeof (pstat));
10246 1.1 christos
10247 1.1 christos elf_tdata (abfd)->core->pid = pstat.pr_pid;
10248 1.1 christos }
10249 1.14 christos #endif
10250 1.1 christos /* Could grab some more details from the "representative"
10251 1.1 christos lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10252 1.1 christos NT_LWPSTATUS note, presumably. */
10253 1.1 christos
10254 1.14 christos return true;
10255 1.1 christos }
10256 1.1 christos #endif /* defined (HAVE_PSTATUS_T) */
10257 1.1 christos
10258 1.1 christos #if defined (HAVE_LWPSTATUS_T)
10259 1.1 christos static bool
10260 1.1 christos elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
10261 1.1 christos {
10262 1.1 christos lwpstatus_t lwpstat;
10263 1.1 christos char buf[100];
10264 1.1 christos char *name;
10265 1.1 christos size_t len;
10266 1.1 christos asection *sect;
10267 1.1 christos
10268 1.14 christos if (note->descsz != sizeof (lwpstat)
10269 1.1 christos #if defined (HAVE_LWPXSTATUS_T)
10270 1.1 christos && note->descsz != sizeof (lwpxstatus_t)
10271 1.1 christos #endif
10272 1.3 christos )
10273 1.1 christos return true;
10274 1.1 christos
10275 1.3 christos memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10276 1.3 christos
10277 1.1 christos elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
10278 1.1 christos /* Do not overwrite the core signal if it has already been set by
10279 1.1 christos another thread. */
10280 1.1 christos if (elf_tdata (abfd)->core->signal == 0)
10281 1.1 christos elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
10282 1.1 christos
10283 1.1 christos /* Make a ".reg/999" section. */
10284 1.14 christos
10285 1.1 christos sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
10286 1.1 christos len = strlen (buf) + 1;
10287 1.1 christos name = bfd_alloc (abfd, len);
10288 1.1 christos if (name == NULL)
10289 1.14 christos return false;
10290 1.1 christos memcpy (name, buf, len);
10291 1.1 christos
10292 1.1 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10293 1.1 christos if (sect == NULL)
10294 1.1 christos return false;
10295 1.1 christos
10296 1.1 christos #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10297 1.1 christos sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
10298 1.1 christos sect->filepos = note->descpos
10299 1.1 christos + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10300 1.1 christos #endif
10301 1.1 christos
10302 1.1 christos #if defined (HAVE_LWPSTATUS_T_PR_REG)
10303 1.1 christos sect->size = sizeof (lwpstat.pr_reg);
10304 1.1 christos sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10305 1.14 christos #endif
10306 1.1 christos
10307 1.1 christos sect->alignment_power = 2;
10308 1.1 christos
10309 1.1 christos if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
10310 1.1 christos return false;
10311 1.1 christos
10312 1.1 christos /* Make a ".reg2/999" section */
10313 1.14 christos
10314 1.1 christos sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
10315 1.1 christos len = strlen (buf) + 1;
10316 1.1 christos name = bfd_alloc (abfd, len);
10317 1.1 christos if (name == NULL)
10318 1.14 christos return false;
10319 1.1 christos memcpy (name, buf, len);
10320 1.1 christos
10321 1.1 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10322 1.1 christos if (sect == NULL)
10323 1.1 christos return false;
10324 1.1 christos
10325 1.1 christos #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10326 1.1 christos sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
10327 1.1 christos sect->filepos = note->descpos
10328 1.1 christos + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
10329 1.1 christos #endif
10330 1.1 christos
10331 1.1 christos #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10332 1.1 christos sect->size = sizeof (lwpstat.pr_fpreg);
10333 1.1 christos sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
10334 1.1 christos #endif
10335 1.1 christos
10336 1.1 christos sect->alignment_power = 2;
10337 1.12 christos
10338 1.12 christos return elfcore_maybe_make_sect (abfd, ".reg2", sect);
10339 1.12 christos }
10340 1.12 christos #endif /* defined (HAVE_LWPSTATUS_T) */
10341 1.12 christos
10342 1.12 christos /* These constants, and the structure offsets used below, are defined by
10343 1.12 christos Cygwin's core_dump.h */
10344 1.14 christos #define NOTE_INFO_PROCESS 1
10345 1.1 christos #define NOTE_INFO_THREAD 2
10346 1.1 christos #define NOTE_INFO_MODULE 3
10347 1.1 christos #define NOTE_INFO_MODULE64 4
10348 1.1 christos
10349 1.1 christos static bool
10350 1.12 christos elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
10351 1.1 christos {
10352 1.12 christos char buf[30];
10353 1.1 christos char *name;
10354 1.1 christos size_t len;
10355 1.1 christos unsigned int name_size;
10356 1.12 christos asection *sect;
10357 1.14 christos unsigned int type;
10358 1.1 christos int is_active_thread;
10359 1.14 christos bfd_vma base_addr;
10360 1.14 christos
10361 1.1 christos if (note->descsz < 4)
10362 1.1 christos return true;
10363 1.1 christos
10364 1.14 christos if (! startswith (note->namedata, "win32"))
10365 1.14 christos return true;
10366 1.12 christos
10367 1.12 christos type = bfd_get_32 (abfd, note->descdata);
10368 1.12 christos
10369 1.12 christos struct
10370 1.12 christos {
10371 1.12 christos const char *type_name;
10372 1.12 christos unsigned long min_size;
10373 1.12 christos } size_check[] =
10374 1.12 christos {
10375 1.12 christos { "NOTE_INFO_PROCESS", 12 },
10376 1.14 christos { "NOTE_INFO_THREAD", 12 },
10377 1.14 christos { "NOTE_INFO_MODULE", 12 },
10378 1.12 christos { "NOTE_INFO_MODULE64", 16 },
10379 1.12 christos };
10380 1.12 christos
10381 1.14 christos if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
10382 1.14 christos return true;
10383 1.14 christos
10384 1.14 christos if (note->descsz < size_check[type - 1].min_size)
10385 1.12 christos {
10386 1.12 christos _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
10387 1.1 christos " is too small"),
10388 1.1 christos abfd, size_check[type - 1].type_name, note->descsz);
10389 1.12 christos return true;
10390 1.3 christos }
10391 1.12 christos
10392 1.12 christos switch (type)
10393 1.1 christos {
10394 1.1 christos case NOTE_INFO_PROCESS:
10395 1.12 christos /* FIXME: need to add ->core->command. */
10396 1.12 christos elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
10397 1.14 christos elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
10398 1.1 christos break;
10399 1.12 christos
10400 1.1 christos case NOTE_INFO_THREAD:
10401 1.1 christos /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
10402 1.1 christos structure. */
10403 1.1 christos /* thread_info.tid */
10404 1.14 christos sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
10405 1.1 christos
10406 1.1 christos len = strlen (buf) + 1;
10407 1.1 christos name = (char *) bfd_alloc (abfd, len);
10408 1.1 christos if (name == NULL)
10409 1.1 christos return false;
10410 1.14 christos
10411 1.1 christos memcpy (name, buf, len);
10412 1.1 christos
10413 1.12 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10414 1.1 christos if (sect == NULL)
10415 1.1 christos return false;
10416 1.1 christos
10417 1.1 christos /* sizeof (thread_info.thread_context) */
10418 1.1 christos sect->size = note->descsz - 12;
10419 1.1 christos /* offsetof (thread_info.thread_context) */
10420 1.1 christos sect->filepos = note->descpos + 12;
10421 1.1 christos sect->alignment_power = 2;
10422 1.1 christos
10423 1.14 christos /* thread_info.is_active_thread */
10424 1.1 christos is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
10425 1.1 christos
10426 1.12 christos if (is_active_thread)
10427 1.12 christos if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
10428 1.1 christos return false;
10429 1.12 christos break;
10430 1.14 christos
10431 1.14 christos case NOTE_INFO_MODULE:
10432 1.14 christos case NOTE_INFO_MODULE64:
10433 1.14 christos /* Make a ".module/xxxxxxxx" section. */
10434 1.14 christos if (type == NOTE_INFO_MODULE)
10435 1.14 christos {
10436 1.14 christos /* module_info.base_address */
10437 1.12 christos base_addr = bfd_get_32 (abfd, note->descdata + 4);
10438 1.14 christos sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
10439 1.14 christos /* module_info.module_name_size */
10440 1.14 christos name_size = bfd_get_32 (abfd, note->descdata + 8);
10441 1.14 christos }
10442 1.14 christos else /* NOTE_INFO_MODULE64 */
10443 1.14 christos {
10444 1.14 christos /* module_info.base_address */
10445 1.1 christos base_addr = bfd_get_64 (abfd, note->descdata + 4);
10446 1.1 christos sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
10447 1.1 christos /* module_info.module_name_size */
10448 1.1 christos name_size = bfd_get_32 (abfd, note->descdata + 12);
10449 1.14 christos }
10450 1.1 christos
10451 1.1 christos len = strlen (buf) + 1;
10452 1.1 christos name = (char *) bfd_alloc (abfd, len);
10453 1.1 christos if (name == NULL)
10454 1.1 christos return false;
10455 1.1 christos
10456 1.14 christos memcpy (name, buf, len);
10457 1.1 christos
10458 1.12 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10459 1.14 christos
10460 1.14 christos if (sect == NULL)
10461 1.14 christos return false;
10462 1.14 christos
10463 1.14 christos if (note->descsz < 12 + name_size)
10464 1.14 christos {
10465 1.12 christos _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
10466 1.1 christos " is too small to contain a name of size %u"),
10467 1.1 christos abfd, note->descsz, name_size);
10468 1.1 christos return true;
10469 1.1 christos }
10470 1.1 christos
10471 1.1 christos sect->size = note->descsz;
10472 1.14 christos sect->filepos = note->descpos;
10473 1.1 christos sect->alignment_power = 2;
10474 1.1 christos break;
10475 1.14 christos
10476 1.1 christos default:
10477 1.1 christos return true;
10478 1.14 christos }
10479 1.1 christos
10480 1.1 christos return true;
10481 1.1 christos }
10482 1.1 christos
10483 1.1 christos static bool
10484 1.1 christos elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
10485 1.1 christos {
10486 1.14 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10487 1.1 christos
10488 1.1 christos switch (note->type)
10489 1.1 christos {
10490 1.1 christos default:
10491 1.14 christos return true;
10492 1.1 christos
10493 1.1 christos case NT_PRSTATUS:
10494 1.1 christos if (bed->elf_backend_grok_prstatus)
10495 1.14 christos if ((*bed->elf_backend_grok_prstatus) (abfd, note))
10496 1.1 christos return true;
10497 1.1 christos #if defined (HAVE_PRSTATUS_T)
10498 1.1 christos return elfcore_grok_prstatus (abfd, note);
10499 1.1 christos #else
10500 1.1 christos return true;
10501 1.1 christos #endif
10502 1.1 christos
10503 1.1 christos #if defined (HAVE_PSTATUS_T)
10504 1.1 christos case NT_PSTATUS:
10505 1.1 christos return elfcore_grok_pstatus (abfd, note);
10506 1.1 christos #endif
10507 1.1 christos
10508 1.1 christos #if defined (HAVE_LWPSTATUS_T)
10509 1.1 christos case NT_LWPSTATUS:
10510 1.1 christos return elfcore_grok_lwpstatus (abfd, note);
10511 1.1 christos #endif
10512 1.1 christos
10513 1.1 christos case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
10514 1.1 christos return elfcore_grok_prfpreg (abfd, note);
10515 1.1 christos
10516 1.1 christos case NT_WIN32PSTATUS:
10517 1.1 christos return elfcore_grok_win32pstatus (abfd, note);
10518 1.1 christos
10519 1.14 christos case NT_PRXFPREG: /* Linux SSE extension */
10520 1.1 christos if (note->namesz == 6
10521 1.1 christos && strcmp (note->namedata, "LINUX") == 0)
10522 1.1 christos return elfcore_grok_prxfpreg (abfd, note);
10523 1.1 christos else
10524 1.1 christos return true;
10525 1.1 christos
10526 1.14 christos case NT_X86_XSTATE: /* Linux XSAVE extension */
10527 1.1 christos if (note->namesz == 6
10528 1.1 christos && strcmp (note->namedata, "LINUX") == 0)
10529 1.1 christos return elfcore_grok_xstatereg (abfd, note);
10530 1.1 christos else
10531 1.1 christos return true;
10532 1.1 christos
10533 1.14 christos case NT_PPC_VMX:
10534 1.1 christos if (note->namesz == 6
10535 1.1 christos && strcmp (note->namedata, "LINUX") == 0)
10536 1.1 christos return elfcore_grok_ppc_vmx (abfd, note);
10537 1.11 christos else
10538 1.11 christos return true;
10539 1.11 christos
10540 1.14 christos case NT_PPC_VSX:
10541 1.11 christos if (note->namesz == 6
10542 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
10543 1.11 christos return elfcore_grok_ppc_vsx (abfd, note);
10544 1.12 christos else
10545 1.12 christos return true;
10546 1.1 christos
10547 1.14 christos case NT_PPC_TAR:
10548 1.1 christos if (note->namesz == 6
10549 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
10550 1.11 christos return elfcore_grok_ppc_tar (abfd, note);
10551 1.12 christos else
10552 1.12 christos return true;
10553 1.11 christos
10554 1.14 christos case NT_PPC_PPR:
10555 1.11 christos if (note->namesz == 6
10556 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
10557 1.11 christos return elfcore_grok_ppc_ppr (abfd, note);
10558 1.12 christos else
10559 1.12 christos return true;
10560 1.11 christos
10561 1.14 christos case NT_PPC_DSCR:
10562 1.11 christos if (note->namesz == 6
10563 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
10564 1.11 christos return elfcore_grok_ppc_dscr (abfd, note);
10565 1.12 christos else
10566 1.12 christos return true;
10567 1.11 christos
10568 1.14 christos case NT_PPC_EBB:
10569 1.11 christos if (note->namesz == 6
10570 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
10571 1.1 christos return elfcore_grok_ppc_ebb (abfd, note);
10572 1.12 christos else
10573 1.12 christos return true;
10574 1.1 christos
10575 1.14 christos case NT_PPC_PMU:
10576 1.1 christos if (note->namesz == 6
10577 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
10578 1.1 christos return elfcore_grok_ppc_pmu (abfd, note);
10579 1.12 christos else
10580 1.12 christos return true;
10581 1.1 christos
10582 1.14 christos case NT_PPC_TM_CGPR:
10583 1.1 christos if (note->namesz == 6
10584 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
10585 1.1 christos return elfcore_grok_ppc_tm_cgpr (abfd, note);
10586 1.12 christos else
10587 1.12 christos return true;
10588 1.1 christos
10589 1.14 christos case NT_PPC_TM_CFPR:
10590 1.1 christos if (note->namesz == 6
10591 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
10592 1.1 christos return elfcore_grok_ppc_tm_cfpr (abfd, note);
10593 1.12 christos else
10594 1.12 christos return true;
10595 1.1 christos
10596 1.14 christos case NT_PPC_TM_CVMX:
10597 1.1 christos if (note->namesz == 6
10598 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
10599 1.1 christos return elfcore_grok_ppc_tm_cvmx (abfd, note);
10600 1.12 christos else
10601 1.12 christos return true;
10602 1.1 christos
10603 1.14 christos case NT_PPC_TM_CVSX:
10604 1.1 christos if (note->namesz == 6
10605 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
10606 1.1 christos return elfcore_grok_ppc_tm_cvsx (abfd, note);
10607 1.12 christos else
10608 1.12 christos return true;
10609 1.1 christos
10610 1.14 christos case NT_PPC_TM_SPR:
10611 1.1 christos if (note->namesz == 6
10612 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
10613 1.3 christos return elfcore_grok_ppc_tm_spr (abfd, note);
10614 1.12 christos else
10615 1.12 christos return true;
10616 1.3 christos
10617 1.14 christos case NT_PPC_TM_CTAR:
10618 1.3 christos if (note->namesz == 6
10619 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
10620 1.3 christos return elfcore_grok_ppc_tm_ctar (abfd, note);
10621 1.12 christos else
10622 1.12 christos return true;
10623 1.3 christos
10624 1.14 christos case NT_PPC_TM_CPPR:
10625 1.3 christos if (note->namesz == 6
10626 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
10627 1.3 christos return elfcore_grok_ppc_tm_cppr (abfd, note);
10628 1.12 christos else
10629 1.12 christos return true;
10630 1.3 christos
10631 1.14 christos case NT_PPC_TM_CDSCR:
10632 1.3 christos if (note->namesz == 6
10633 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
10634 1.11 christos return elfcore_grok_ppc_tm_cdscr (abfd, note);
10635 1.11 christos else
10636 1.11 christos return true;
10637 1.11 christos
10638 1.14 christos case NT_S390_HIGH_GPRS:
10639 1.11 christos if (note->namesz == 6
10640 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
10641 1.11 christos return elfcore_grok_s390_high_gprs (abfd, note);
10642 1.11 christos else
10643 1.11 christos return true;
10644 1.11 christos
10645 1.14 christos case NT_S390_TIMER:
10646 1.11 christos if (note->namesz == 6
10647 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
10648 1.11 christos return elfcore_grok_s390_timer (abfd, note);
10649 1.11 christos else
10650 1.11 christos return true;
10651 1.11 christos
10652 1.14 christos case NT_S390_TODCMP:
10653 1.11 christos if (note->namesz == 6
10654 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
10655 1.11 christos return elfcore_grok_s390_todcmp (abfd, note);
10656 1.11 christos else
10657 1.11 christos return true;
10658 1.11 christos
10659 1.14 christos case NT_S390_TODPREG:
10660 1.11 christos if (note->namesz == 6
10661 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
10662 1.11 christos return elfcore_grok_s390_todpreg (abfd, note);
10663 1.11 christos else
10664 1.11 christos return true;
10665 1.11 christos
10666 1.14 christos case NT_S390_CTRS:
10667 1.11 christos if (note->namesz == 6
10668 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
10669 1.11 christos return elfcore_grok_s390_ctrs (abfd, note);
10670 1.11 christos else
10671 1.11 christos return true;
10672 1.11 christos
10673 1.14 christos case NT_S390_PREFIX:
10674 1.11 christos if (note->namesz == 6
10675 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
10676 1.11 christos return elfcore_grok_s390_prefix (abfd, note);
10677 1.11 christos else
10678 1.11 christos return true;
10679 1.11 christos
10680 1.14 christos case NT_S390_LAST_BREAK:
10681 1.11 christos if (note->namesz == 6
10682 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
10683 1.11 christos return elfcore_grok_s390_last_break (abfd, note);
10684 1.11 christos else
10685 1.11 christos return true;
10686 1.11 christos
10687 1.14 christos case NT_S390_SYSTEM_CALL:
10688 1.11 christos if (note->namesz == 6
10689 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
10690 1.11 christos return elfcore_grok_s390_system_call (abfd, note);
10691 1.11 christos else
10692 1.11 christos return true;
10693 1.11 christos
10694 1.14 christos case NT_S390_TDB:
10695 1.11 christos if (note->namesz == 6
10696 1.6 christos && strcmp (note->namedata, "LINUX") == 0)
10697 1.6 christos return elfcore_grok_s390_tdb (abfd, note);
10698 1.6 christos else
10699 1.6 christos return true;
10700 1.6 christos
10701 1.14 christos case NT_S390_VXRS_LOW:
10702 1.6 christos if (note->namesz == 6
10703 1.6 christos && strcmp (note->namedata, "LINUX") == 0)
10704 1.6 christos return elfcore_grok_s390_vxrs_low (abfd, note);
10705 1.6 christos else
10706 1.6 christos return true;
10707 1.6 christos
10708 1.14 christos case NT_S390_VXRS_HIGH:
10709 1.6 christos if (note->namesz == 6
10710 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
10711 1.11 christos return elfcore_grok_s390_vxrs_high (abfd, note);
10712 1.11 christos else
10713 1.11 christos return true;
10714 1.11 christos
10715 1.14 christos case NT_S390_GS_CB:
10716 1.11 christos if (note->namesz == 6
10717 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
10718 1.11 christos return elfcore_grok_s390_gs_cb (abfd, note);
10719 1.11 christos else
10720 1.11 christos return true;
10721 1.11 christos
10722 1.14 christos case NT_S390_GS_BC:
10723 1.11 christos if (note->namesz == 6
10724 1.12 christos && strcmp (note->namedata, "LINUX") == 0)
10725 1.12 christos return elfcore_grok_s390_gs_bc (abfd, note);
10726 1.12 christos else
10727 1.12 christos return true;
10728 1.12 christos
10729 1.14 christos case NT_ARC_V2:
10730 1.12 christos if (note->namesz == 6
10731 1.3 christos && strcmp (note->namedata, "LINUX") == 0)
10732 1.3 christos return elfcore_grok_arc_v2 (abfd, note);
10733 1.3 christos else
10734 1.3 christos return true;
10735 1.3 christos
10736 1.14 christos case NT_ARM_VFP:
10737 1.3 christos if (note->namesz == 6
10738 1.3 christos && strcmp (note->namedata, "LINUX") == 0)
10739 1.3 christos return elfcore_grok_arm_vfp (abfd, note);
10740 1.3 christos else
10741 1.3 christos return true;
10742 1.3 christos
10743 1.14 christos case NT_ARM_TLS:
10744 1.3 christos if (note->namesz == 6
10745 1.3 christos && strcmp (note->namedata, "LINUX") == 0)
10746 1.3 christos return elfcore_grok_aarch_tls (abfd, note);
10747 1.3 christos else
10748 1.3 christos return true;
10749 1.3 christos
10750 1.14 christos case NT_ARM_HW_BREAK:
10751 1.3 christos if (note->namesz == 6
10752 1.3 christos && strcmp (note->namedata, "LINUX") == 0)
10753 1.3 christos return elfcore_grok_aarch_hw_break (abfd, note);
10754 1.3 christos else
10755 1.3 christos return true;
10756 1.3 christos
10757 1.14 christos case NT_ARM_HW_WATCH:
10758 1.3 christos if (note->namesz == 6
10759 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
10760 1.11 christos return elfcore_grok_aarch_hw_watch (abfd, note);
10761 1.11 christos else
10762 1.11 christos return true;
10763 1.11 christos
10764 1.14 christos case NT_ARM_SVE:
10765 1.11 christos if (note->namesz == 6
10766 1.11 christos && strcmp (note->namedata, "LINUX") == 0)
10767 1.11 christos return elfcore_grok_aarch_sve (abfd, note);
10768 1.11 christos else
10769 1.11 christos return true;
10770 1.11 christos
10771 1.14 christos case NT_ARM_PAC_MASK:
10772 1.14 christos if (note->namesz == 6
10773 1.14 christos && strcmp (note->namedata, "LINUX") == 0)
10774 1.14 christos return elfcore_grok_aarch_pauth (abfd, note);
10775 1.14 christos else
10776 1.14 christos return true;
10777 1.14 christos
10778 1.14 christos case NT_ARM_TAGGED_ADDR_CTRL:
10779 1.14 christos if (note->namesz == 6
10780 1.14 christos && strcmp (note->namedata, "LINUX") == 0)
10781 1.14 christos return elfcore_grok_aarch_mte (abfd, note);
10782 1.14 christos else
10783 1.14 christos return true;
10784 1.14 christos
10785 1.14 christos case NT_GDB_TDESC:
10786 1.14 christos if (note->namesz == 4
10787 1.14 christos && strcmp (note->namedata, "GDB") == 0)
10788 1.14 christos return elfcore_grok_gdb_tdesc (abfd, note);
10789 1.14 christos else
10790 1.14 christos return true;
10791 1.14 christos
10792 1.14 christos case NT_RISCV_CSR:
10793 1.14 christos if (note->namesz == 4
10794 1.14 christos && strcmp (note->namedata, "GDB") == 0)
10795 1.14 christos return elfcore_grok_riscv_csr (abfd, note);
10796 1.14 christos else
10797 1.14 christos return true;
10798 1.14 christos
10799 1.14 christos case NT_LARCH_CPUCFG:
10800 1.14 christos if (note->namesz == 6
10801 1.14 christos && strcmp (note->namedata, "LINUX") == 0)
10802 1.14 christos return elfcore_grok_loongarch_cpucfg (abfd, note);
10803 1.14 christos else
10804 1.14 christos return true;
10805 1.14 christos
10806 1.14 christos case NT_LARCH_LBT:
10807 1.14 christos if (note->namesz == 6
10808 1.14 christos && strcmp (note->namedata, "LINUX") == 0)
10809 1.14 christos return elfcore_grok_loongarch_lbt (abfd, note);
10810 1.14 christos else
10811 1.14 christos return true;
10812 1.14 christos
10813 1.14 christos case NT_LARCH_LSX:
10814 1.14 christos if (note->namesz == 6
10815 1.14 christos && strcmp (note->namedata, "LINUX") == 0)
10816 1.14 christos return elfcore_grok_loongarch_lsx (abfd, note);
10817 1.14 christos else
10818 1.14 christos return true;
10819 1.14 christos
10820 1.14 christos case NT_LARCH_LASX:
10821 1.11 christos if (note->namesz == 6
10822 1.1 christos && strcmp (note->namedata, "LINUX") == 0)
10823 1.1 christos return elfcore_grok_loongarch_lasx (abfd, note);
10824 1.1 christos else
10825 1.1 christos return true;
10826 1.14 christos
10827 1.1 christos case NT_PRPSINFO:
10828 1.1 christos case NT_PSINFO:
10829 1.1 christos if (bed->elf_backend_grok_psinfo)
10830 1.14 christos if ((*bed->elf_backend_grok_psinfo) (abfd, note))
10831 1.1 christos return true;
10832 1.1 christos #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10833 1.1 christos return elfcore_grok_psinfo (abfd, note);
10834 1.12 christos #else
10835 1.3 christos return true;
10836 1.3 christos #endif
10837 1.3 christos
10838 1.3 christos case NT_AUXV:
10839 1.3 christos return elfcore_make_auxv_note_section (abfd, note, 0);
10840 1.3 christos
10841 1.3 christos case NT_FILE:
10842 1.3 christos return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10843 1.8 christos note);
10844 1.1 christos
10845 1.1 christos case NT_SIGINFO:
10846 1.1 christos return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10847 1.14 christos note);
10848 1.1 christos
10849 1.1 christos }
10850 1.6 christos }
10851 1.3 christos
10852 1.3 christos static bool
10853 1.14 christos elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10854 1.1 christos {
10855 1.6 christos struct bfd_build_id* build_id;
10856 1.6 christos
10857 1.14 christos if (note->descsz == 0)
10858 1.3 christos return false;
10859 1.6 christos
10860 1.6 christos build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10861 1.6 christos if (build_id == NULL)
10862 1.1 christos return false;
10863 1.14 christos
10864 1.1 christos build_id->size = note->descsz;
10865 1.1 christos memcpy (build_id->data, note->descdata, note->descsz);
10866 1.14 christos abfd->build_id = build_id;
10867 1.1 christos
10868 1.1 christos return true;
10869 1.1 christos }
10870 1.1 christos
10871 1.1 christos static bool
10872 1.14 christos elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10873 1.1 christos {
10874 1.9 christos switch (note->type)
10875 1.9 christos {
10876 1.9 christos default:
10877 1.1 christos return true;
10878 1.1 christos
10879 1.1 christos case NT_GNU_PROPERTY_TYPE_0:
10880 1.1 christos return _bfd_elf_parse_gnu_properties (abfd, note);
10881 1.1 christos
10882 1.14 christos case NT_GNU_BUILD_ID:
10883 1.3 christos return elfobj_grok_gnu_build_id (abfd, note);
10884 1.3 christos }
10885 1.3 christos }
10886 1.12 christos
10887 1.12 christos static bool
10888 1.3 christos elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10889 1.3 christos {
10890 1.3 christos struct sdt_note *cur =
10891 1.3 christos (struct sdt_note *) bfd_alloc (abfd,
10892 1.3 christos sizeof (struct sdt_note) + note->descsz);
10893 1.3 christos
10894 1.3 christos cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10895 1.14 christos cur->size = (bfd_size_type) note->descsz;
10896 1.3 christos memcpy (cur->data, note->descdata, note->descsz);
10897 1.3 christos
10898 1.14 christos elf_tdata (abfd)->sdt_note_head = cur;
10899 1.3 christos
10900 1.3 christos return true;
10901 1.3 christos }
10902 1.3 christos
10903 1.3 christos static bool
10904 1.3 christos elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10905 1.3 christos {
10906 1.3 christos switch (note->type)
10907 1.14 christos {
10908 1.3 christos case NT_STAPSDT:
10909 1.3 christos return elfobj_grok_stapsdt_note_1 (abfd, note);
10910 1.3 christos
10911 1.14 christos default:
10912 1.8 christos return true;
10913 1.8 christos }
10914 1.8 christos }
10915 1.8 christos
10916 1.11 christos static bool
10917 1.8 christos elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
10918 1.11 christos {
10919 1.8 christos size_t offset;
10920 1.14 christos
10921 1.8 christos switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
10922 1.8 christos {
10923 1.11 christos case ELFCLASS32:
10924 1.8 christos if (note->descsz < 108)
10925 1.14 christos return false;
10926 1.8 christos break;
10927 1.8 christos
10928 1.8 christos case ELFCLASS64:
10929 1.14 christos if (note->descsz < 120)
10930 1.8 christos return false;
10931 1.8 christos break;
10932 1.8 christos
10933 1.8 christos default:
10934 1.14 christos return false;
10935 1.11 christos }
10936 1.8 christos
10937 1.8 christos /* Check for version 1 in pr_version. */
10938 1.8 christos if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10939 1.11 christos return false;
10940 1.8 christos
10941 1.8 christos offset = 4;
10942 1.8 christos
10943 1.8 christos /* Skip over pr_psinfosz. */
10944 1.8 christos if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
10945 1.8 christos offset += 4;
10946 1.8 christos else
10947 1.8 christos {
10948 1.8 christos offset += 4; /* Padding before pr_psinfosz. */
10949 1.8 christos offset += 8;
10950 1.8 christos }
10951 1.8 christos
10952 1.8 christos /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10953 1.8 christos elf_tdata (abfd)->core->program
10954 1.8 christos = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
10955 1.8 christos offset += 17;
10956 1.8 christos
10957 1.8 christos /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10958 1.8 christos elf_tdata (abfd)->core->command
10959 1.8 christos = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
10960 1.8 christos offset += 81;
10961 1.8 christos
10962 1.14 christos /* Padding before pr_pid. */
10963 1.8 christos offset += 2;
10964 1.8 christos
10965 1.8 christos /* The pr_pid field was added in version "1a". */
10966 1.8 christos if (note->descsz < offset + 4)
10967 1.14 christos return true;
10968 1.8 christos
10969 1.8 christos elf_tdata (abfd)->core->pid
10970 1.14 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10971 1.8 christos
10972 1.8 christos return true;
10973 1.8 christos }
10974 1.8 christos
10975 1.11 christos static bool
10976 1.8 christos elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
10977 1.11 christos {
10978 1.11 christos size_t offset;
10979 1.11 christos size_t size;
10980 1.11 christos size_t min_size;
10981 1.11 christos
10982 1.11 christos /* Compute offset of pr_getregsz, skipping over pr_statussz.
10983 1.11 christos Also compute minimum size of this note. */
10984 1.8 christos switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
10985 1.8 christos {
10986 1.11 christos case ELFCLASS32:
10987 1.11 christos offset = 4 + 4;
10988 1.11 christos min_size = offset + (4 * 2) + 4 + 4 + 4;
10989 1.8 christos break;
10990 1.8 christos
10991 1.8 christos case ELFCLASS64:
10992 1.14 christos offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
10993 1.8 christos min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
10994 1.8 christos break;
10995 1.11 christos
10996 1.14 christos default:
10997 1.11 christos return false;
10998 1.11 christos }
10999 1.11 christos
11000 1.14 christos if (note->descsz < min_size)
11001 1.11 christos return false;
11002 1.8 christos
11003 1.11 christos /* Check for version 1 in pr_version. */
11004 1.11 christos if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11005 1.11 christos return false;
11006 1.11 christos
11007 1.11 christos /* Extract size of pr_reg from pr_gregsetsz. */
11008 1.11 christos /* Skip over pr_gregsetsz and pr_fpregsetsz. */
11009 1.8 christos if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11010 1.11 christos {
11011 1.11 christos size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11012 1.11 christos offset += 4 * 2;
11013 1.11 christos }
11014 1.8 christos else
11015 1.11 christos {
11016 1.8 christos size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
11017 1.8 christos offset += 8 * 2;
11018 1.11 christos }
11019 1.8 christos
11020 1.8 christos /* Skip over pr_osreldate. */
11021 1.8 christos offset += 4;
11022 1.8 christos
11023 1.8 christos /* Read signal from pr_cursig. */
11024 1.11 christos if (elf_tdata (abfd)->core->signal == 0)
11025 1.8 christos elf_tdata (abfd)->core->signal
11026 1.8 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11027 1.8 christos offset += 4;
11028 1.8 christos
11029 1.11 christos /* Read TID from pr_pid. */
11030 1.11 christos elf_tdata (abfd)->core->lwpid
11031 1.8 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11032 1.8 christos offset += 4;
11033 1.11 christos
11034 1.11 christos /* Padding before pr_reg. */
11035 1.14 christos if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
11036 1.11 christos offset += 4;
11037 1.8 christos
11038 1.8 christos /* Make sure that there is enough data remaining in the note. */
11039 1.8 christos if ((note->descsz - offset) < size)
11040 1.8 christos return false;
11041 1.8 christos
11042 1.14 christos /* Make a ".reg/999" section and a ".reg" section. */
11043 1.8 christos return _bfd_elfcore_make_pseudosection (abfd, ".reg",
11044 1.8 christos size, note->descpos + offset);
11045 1.11 christos }
11046 1.11 christos
11047 1.8 christos static bool
11048 1.8 christos elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
11049 1.8 christos {
11050 1.11 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11051 1.11 christos
11052 1.14 christos switch (note->type)
11053 1.8 christos {
11054 1.8 christos case NT_PRSTATUS:
11055 1.8 christos if (bed->elf_backend_grok_freebsd_prstatus)
11056 1.8 christos if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
11057 1.8 christos return true;
11058 1.8 christos return elfcore_grok_freebsd_prstatus (abfd, note);
11059 1.8 christos
11060 1.8 christos case NT_FPREGSET:
11061 1.8 christos return elfcore_grok_prfpreg (abfd, note);
11062 1.14 christos
11063 1.8 christos case NT_PRPSINFO:
11064 1.11 christos return elfcore_grok_freebsd_psinfo (abfd, note);
11065 1.11 christos
11066 1.11 christos case NT_FREEBSD_THRMISC:
11067 1.11 christos return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
11068 1.11 christos
11069 1.11 christos case NT_FREEBSD_PROCSTAT_PROC:
11070 1.11 christos return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
11071 1.11 christos note);
11072 1.11 christos
11073 1.11 christos case NT_FREEBSD_PROCSTAT_FILES:
11074 1.11 christos return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11075 1.11 christos note);
11076 1.8 christos
11077 1.12 christos case NT_FREEBSD_PROCSTAT_VMMAP:
11078 1.8 christos return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
11079 1.14 christos note);
11080 1.14 christos
11081 1.14 christos case NT_FREEBSD_PROCSTAT_AUXV:
11082 1.8 christos return elfcore_make_auxv_note_section (abfd, note, 4);
11083 1.14 christos
11084 1.8 christos case NT_FREEBSD_X86_SEGBASES:
11085 1.11 christos return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
11086 1.11 christos
11087 1.11 christos case NT_X86_XSTATE:
11088 1.11 christos return elfcore_grok_xstatereg (abfd, note);
11089 1.14 christos
11090 1.14 christos case NT_FREEBSD_PTLWPINFO:
11091 1.14 christos return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
11092 1.11 christos note);
11093 1.11 christos
11094 1.11 christos case NT_ARM_TLS:
11095 1.8 christos return elfcore_grok_aarch_tls (abfd, note);
11096 1.14 christos
11097 1.8 christos case NT_ARM_VFP:
11098 1.8 christos return elfcore_grok_arm_vfp (abfd, note);
11099 1.8 christos
11100 1.14 christos default:
11101 1.1 christos return true;
11102 1.1 christos }
11103 1.1 christos }
11104 1.1 christos
11105 1.1 christos static bool
11106 1.1 christos elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
11107 1.1 christos {
11108 1.1 christos char *cp;
11109 1.14 christos
11110 1.1 christos cp = strchr (note->namedata, '@');
11111 1.14 christos if (cp != NULL)
11112 1.1 christos {
11113 1.1 christos *lwpidp = atoi(cp + 1);
11114 1.14 christos return true;
11115 1.1 christos }
11116 1.1 christos return false;
11117 1.11 christos }
11118 1.14 christos
11119 1.11 christos static bool
11120 1.1 christos elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11121 1.3 christos {
11122 1.1 christos if (note->descsz <= 0x7c + 31)
11123 1.1 christos return false;
11124 1.1 christos
11125 1.3 christos /* Signal number at offset 0x08. */
11126 1.1 christos elf_tdata (abfd)->core->signal
11127 1.1 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11128 1.1 christos
11129 1.3 christos /* Process ID at offset 0x50. */
11130 1.1 christos elf_tdata (abfd)->core->pid
11131 1.1 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
11132 1.1 christos
11133 1.1 christos /* Command name at 0x7c (max 32 bytes, including nul). */
11134 1.1 christos elf_tdata (abfd)->core->command
11135 1.1 christos = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
11136 1.14 christos
11137 1.1 christos return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
11138 1.1 christos note);
11139 1.1 christos }
11140 1.1 christos
11141 1.1 christos static bool
11142 1.3 christos elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
11143 1.1 christos {
11144 1.12 christos int lwp;
11145 1.1 christos
11146 1.12 christos if (elfcore_netbsd_get_lwpid (note, &lwp))
11147 1.1 christos elf_tdata (abfd)->core->lwpid = lwp;
11148 1.1 christos
11149 1.1 christos switch (note->type)
11150 1.1 christos {
11151 1.1 christos case NT_NETBSDCORE_PROCINFO:
11152 1.12 christos /* NetBSD-specific core "procinfo". Note that we expect to
11153 1.12 christos find this note before any of the others, which is fine,
11154 1.15 rin since the kernel writes this note out first when it
11155 1.12 christos creates a core file. */
11156 1.12 christos return elfcore_grok_netbsd_procinfo (abfd, note);
11157 1.12 christos case NT_NETBSDCORE_AUXV:
11158 1.12 christos /* NetBSD-specific Elf Auxiliary Vector data. */
11159 1.12 christos return elfcore_make_auxv_note_section (abfd, note, 0);
11160 1.12 christos case NT_NETBSDCORE_LWPSTATUS:
11161 1.1 christos return elfcore_make_note_pseudosection (abfd,
11162 1.1 christos ".note.netbsdcore.lwpstatus",
11163 1.7 christos note);
11164 1.7 christos default:
11165 1.7 christos break;
11166 1.7 christos }
11167 1.7 christos
11168 1.7 christos if (note->type == NT_NETBSDCORE_AUXV)
11169 1.14 christos {
11170 1.7 christos asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
11171 1.7 christos SEC_HAS_CONTENTS);
11172 1.7 christos
11173 1.7 christos if (sect == NULL)
11174 1.14 christos return false;
11175 1.7 christos sect->size = note->descsz;
11176 1.7 christos sect->filepos = note->descpos;
11177 1.12 christos sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
11178 1.1 christos
11179 1.1 christos return true;
11180 1.1 christos }
11181 1.1 christos
11182 1.1 christos /* As of March 2020 there are no other machine-independent notes
11183 1.14 christos defined for NetBSD core files. If the note type is less
11184 1.1 christos than the start of the machine-dependent note types, we don't
11185 1.1 christos understand it. */
11186 1.1 christos
11187 1.1 christos if (note->type < NT_NETBSDCORE_FIRSTMACH)
11188 1.1 christos return true;
11189 1.1 christos
11190 1.1 christos
11191 1.10 christos switch (bfd_get_arch (abfd))
11192 1.1 christos {
11193 1.1 christos /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11194 1.1 christos PT_GETFPREGS == mach+2. */
11195 1.1 christos
11196 1.1 christos case bfd_arch_aarch64:
11197 1.1 christos case bfd_arch_alpha:
11198 1.1 christos case bfd_arch_sparc:
11199 1.1 christos switch (note->type)
11200 1.1 christos {
11201 1.1 christos case NT_NETBSDCORE_FIRSTMACH+0:
11202 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg", note);
11203 1.14 christos
11204 1.1 christos case NT_NETBSDCORE_FIRSTMACH+2:
11205 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11206 1.2 uwe
11207 1.2 uwe default:
11208 1.2 uwe return true;
11209 1.2 uwe }
11210 1.2 uwe
11211 1.2 uwe /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11212 1.2 uwe There's also old PT___GETREGS40 == mach + 1 for old reg
11213 1.2 uwe structure which lacks GBR. */
11214 1.2 uwe
11215 1.2 uwe case bfd_arch_sh:
11216 1.2 uwe switch (note->type)
11217 1.2 uwe {
11218 1.2 uwe case NT_NETBSDCORE_FIRSTMACH+3:
11219 1.2 uwe return elfcore_make_note_pseudosection (abfd, ".reg", note);
11220 1.14 christos
11221 1.2 uwe case NT_NETBSDCORE_FIRSTMACH+5:
11222 1.2 uwe return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11223 1.1 christos
11224 1.1 christos default:
11225 1.1 christos return true;
11226 1.1 christos }
11227 1.1 christos
11228 1.1 christos /* On all other arch's, PT_GETREGS == mach+1 and
11229 1.1 christos PT_GETFPREGS == mach+3. */
11230 1.1 christos
11231 1.1 christos default:
11232 1.1 christos switch (note->type)
11233 1.1 christos {
11234 1.1 christos case NT_NETBSDCORE_FIRSTMACH+1:
11235 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg", note);
11236 1.14 christos
11237 1.1 christos case NT_NETBSDCORE_FIRSTMACH+3:
11238 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11239 1.1 christos
11240 1.1 christos default:
11241 1.1 christos return true;
11242 1.14 christos }
11243 1.1 christos }
11244 1.1 christos /* NOTREACHED */
11245 1.11 christos }
11246 1.14 christos
11247 1.11 christos static bool
11248 1.1 christos elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11249 1.3 christos {
11250 1.1 christos if (note->descsz <= 0x48 + 31)
11251 1.1 christos return false;
11252 1.1 christos
11253 1.3 christos /* Signal number at offset 0x08. */
11254 1.1 christos elf_tdata (abfd)->core->signal
11255 1.1 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11256 1.1 christos
11257 1.3 christos /* Process ID at offset 0x20. */
11258 1.1 christos elf_tdata (abfd)->core->pid
11259 1.1 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11260 1.14 christos
11261 1.14 christos /* Command name at 0x48 (max 32 bytes, including nul). */
11262 1.14 christos elf_tdata (abfd)->core->command
11263 1.14 christos = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11264 1.14 christos
11265 1.14 christos return true;
11266 1.14 christos }
11267 1.14 christos
11268 1.14 christos /* Processes Solaris's process status note.
11269 1.14 christos sig_off ~ offsetof(prstatus_t, pr_cursig)
11270 1.14 christos pid_off ~ offsetof(prstatus_t, pr_pid)
11271 1.14 christos lwpid_off ~ offsetof(prstatus_t, pr_who)
11272 1.14 christos gregset_size ~ sizeof(gregset_t)
11273 1.14 christos gregset_offset ~ offsetof(prstatus_t, pr_reg) */
11274 1.14 christos
11275 1.14 christos static bool
11276 1.14 christos elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
11277 1.14 christos int pid_off, int lwpid_off, size_t gregset_size,
11278 1.14 christos size_t gregset_offset)
11279 1.14 christos {
11280 1.14 christos asection *sect = NULL;
11281 1.14 christos elf_tdata (abfd)->core->signal
11282 1.14 christos = bfd_get_16 (abfd, note->descdata + sig_off);
11283 1.14 christos elf_tdata (abfd)->core->pid
11284 1.14 christos = bfd_get_32 (abfd, note->descdata + pid_off);
11285 1.14 christos elf_tdata (abfd)->core->lwpid
11286 1.14 christos = bfd_get_32 (abfd, note->descdata + lwpid_off);
11287 1.14 christos
11288 1.14 christos sect = bfd_get_section_by_name (abfd, ".reg");
11289 1.14 christos if (sect != NULL)
11290 1.14 christos sect->size = gregset_size;
11291 1.14 christos
11292 1.14 christos return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11293 1.14 christos note->descpos + gregset_offset);
11294 1.14 christos }
11295 1.14 christos
11296 1.14 christos /* Gets program and arguments from a core.
11297 1.14 christos prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11298 1.14 christos comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs) */
11299 1.14 christos
11300 1.14 christos static bool
11301 1.14 christos elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
11302 1.14 christos int prog_off, int comm_off)
11303 1.14 christos {
11304 1.14 christos elf_tdata (abfd)->core->program
11305 1.14 christos = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
11306 1.14 christos elf_tdata (abfd)->core->command
11307 1.14 christos = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
11308 1.14 christos
11309 1.14 christos return true;
11310 1.14 christos }
11311 1.14 christos
11312 1.14 christos /* Processes Solaris's LWP status note.
11313 1.14 christos gregset_size ~ sizeof(gregset_t)
11314 1.14 christos gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11315 1.14 christos fpregset_size ~ sizeof(fpregset_t)
11316 1.14 christos fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg) */
11317 1.14 christos
11318 1.14 christos static bool
11319 1.14 christos elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
11320 1.14 christos size_t gregset_size, int gregset_off,
11321 1.14 christos size_t fpregset_size, int fpregset_off)
11322 1.14 christos {
11323 1.14 christos asection *sect = NULL;
11324 1.14 christos char reg2_section_name[16] = { 0 };
11325 1.14 christos
11326 1.14 christos (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
11327 1.14 christos elf_tdata (abfd)->core->lwpid);
11328 1.14 christos
11329 1.14 christos /* offsetof(lwpstatus_t, pr_lwpid) */
11330 1.14 christos elf_tdata (abfd)->core->lwpid
11331 1.14 christos = bfd_get_32 (abfd, note->descdata + 4);
11332 1.14 christos /* offsetof(lwpstatus_t, pr_cursig) */
11333 1.14 christos elf_tdata (abfd)->core->signal
11334 1.14 christos = bfd_get_16 (abfd, note->descdata + 12);
11335 1.14 christos
11336 1.14 christos sect = bfd_get_section_by_name (abfd, ".reg");
11337 1.14 christos if (sect != NULL)
11338 1.14 christos sect->size = gregset_size;
11339 1.14 christos else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11340 1.14 christos note->descpos + gregset_off))
11341 1.14 christos return false;
11342 1.14 christos
11343 1.14 christos sect = bfd_get_section_by_name (abfd, reg2_section_name);
11344 1.14 christos if (sect != NULL)
11345 1.14 christos {
11346 1.14 christos sect->size = fpregset_size;
11347 1.14 christos sect->filepos = note->descpos + fpregset_off;
11348 1.14 christos sect->alignment_power = 2;
11349 1.14 christos }
11350 1.14 christos else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
11351 1.14 christos note->descpos + fpregset_off))
11352 1.14 christos return false;
11353 1.14 christos
11354 1.14 christos return true;
11355 1.14 christos }
11356 1.14 christos
11357 1.14 christos static bool
11358 1.14 christos elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
11359 1.14 christos {
11360 1.14 christos if (note == NULL)
11361 1.14 christos return false;
11362 1.14 christos
11363 1.14 christos /* core files are identified as 32- or 64-bit, SPARC or x86,
11364 1.14 christos by the size of the descsz which matches the sizeof()
11365 1.14 christos the type appropriate for that note type (e.g., prstatus_t for
11366 1.14 christos SOLARIS_NT_PRSTATUS) for the corresponding architecture
11367 1.14 christos on Solaris. The core file bitness may differ from the bitness of
11368 1.14 christos gdb itself, so fixed values are used instead of sizeof().
11369 1.14 christos Appropriate fixed offsets are also used to obtain data from
11370 1.14 christos the note. */
11371 1.14 christos
11372 1.14 christos switch ((int) note->type)
11373 1.14 christos {
11374 1.14 christos case SOLARIS_NT_PRSTATUS:
11375 1.14 christos switch (note->descsz)
11376 1.14 christos {
11377 1.14 christos case 508: /* sizeof(prstatus_t) SPARC 32-bit */
11378 1.14 christos return elfcore_grok_solaris_prstatus(abfd, note,
11379 1.14 christos 136, 216, 308, 152, 356);
11380 1.14 christos case 904: /* sizeof(prstatus_t) SPARC 64-bit */
11381 1.14 christos return elfcore_grok_solaris_prstatus(abfd, note,
11382 1.14 christos 264, 360, 520, 304, 600);
11383 1.14 christos case 432: /* sizeof(prstatus_t) Intel 32-bit */
11384 1.14 christos return elfcore_grok_solaris_prstatus(abfd, note,
11385 1.14 christos 136, 216, 308, 76, 356);
11386 1.14 christos case 824: /* sizeof(prstatus_t) Intel 64-bit */
11387 1.14 christos return elfcore_grok_solaris_prstatus(abfd, note,
11388 1.14 christos 264, 360, 520, 224, 600);
11389 1.14 christos default:
11390 1.14 christos return true;
11391 1.14 christos }
11392 1.14 christos
11393 1.14 christos case SOLARIS_NT_PSINFO:
11394 1.14 christos case SOLARIS_NT_PRPSINFO:
11395 1.14 christos switch (note->descsz)
11396 1.14 christos {
11397 1.14 christos case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
11398 1.14 christos return elfcore_grok_solaris_info(abfd, note, 84, 100);
11399 1.14 christos case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
11400 1.14 christos return elfcore_grok_solaris_info(abfd, note, 120, 136);
11401 1.14 christos case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
11402 1.14 christos return elfcore_grok_solaris_info(abfd, note, 88, 104);
11403 1.14 christos case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
11404 1.14 christos return elfcore_grok_solaris_info(abfd, note, 136, 152);
11405 1.14 christos default:
11406 1.14 christos return true;
11407 1.14 christos }
11408 1.14 christos
11409 1.14 christos case SOLARIS_NT_LWPSTATUS:
11410 1.14 christos switch (note->descsz)
11411 1.14 christos {
11412 1.14 christos case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
11413 1.14 christos return elfcore_grok_solaris_lwpstatus(abfd, note,
11414 1.14 christos 152, 344, 400, 496);
11415 1.14 christos case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
11416 1.14 christos return elfcore_grok_solaris_lwpstatus(abfd, note,
11417 1.14 christos 304, 544, 544, 848);
11418 1.14 christos case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
11419 1.14 christos return elfcore_grok_solaris_lwpstatus(abfd, note,
11420 1.14 christos 76, 344, 380, 420);
11421 1.14 christos case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
11422 1.14 christos return elfcore_grok_solaris_lwpstatus(abfd, note,
11423 1.14 christos 224, 544, 528, 768);
11424 1.14 christos default:
11425 1.14 christos return true;
11426 1.14 christos }
11427 1.14 christos
11428 1.14 christos case SOLARIS_NT_LWPSINFO:
11429 1.14 christos /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
11430 1.14 christos if (note->descsz == 128 || note->descsz == 152)
11431 1.14 christos elf_tdata (abfd)->core->lwpid =
11432 1.14 christos bfd_get_32 (abfd, note->descdata + 4);
11433 1.14 christos break;
11434 1.14 christos
11435 1.1 christos default:
11436 1.1 christos break;
11437 1.14 christos }
11438 1.14 christos
11439 1.14 christos return true;
11440 1.14 christos }
11441 1.14 christos
11442 1.14 christos /* For name starting with "CORE" this may be either a Solaris
11443 1.14 christos core file or a gdb-generated core file. Do Solaris-specific
11444 1.14 christos processing on selected note types first with
11445 1.14 christos elfcore_grok_solaris_note(), then process the note
11446 1.14 christos in elfcore_grok_note(). */
11447 1.14 christos
11448 1.14 christos static bool
11449 1.14 christos elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
11450 1.14 christos {
11451 1.14 christos if (!elfcore_grok_solaris_note_impl (abfd, note))
11452 1.14 christos return false;
11453 1.1 christos
11454 1.1 christos return elfcore_grok_note (abfd, note);
11455 1.1 christos }
11456 1.1 christos
11457 1.1 christos static bool
11458 1.1 christos elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
11459 1.1 christos {
11460 1.1 christos if (note->type == NT_OPENBSD_PROCINFO)
11461 1.1 christos return elfcore_grok_openbsd_procinfo (abfd, note);
11462 1.1 christos
11463 1.1 christos if (note->type == NT_OPENBSD_REGS)
11464 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg", note);
11465 1.1 christos
11466 1.1 christos if (note->type == NT_OPENBSD_FPREGS)
11467 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11468 1.12 christos
11469 1.1 christos if (note->type == NT_OPENBSD_XFPREGS)
11470 1.14 christos return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
11471 1.14 christos
11472 1.14 christos if (note->type == NT_OPENBSD_AUXV)
11473 1.14 christos return elfcore_make_auxv_note_section (abfd, note, 0);
11474 1.14 christos
11475 1.14 christos if (note->type == NT_OPENBSD_WCOOKIE)
11476 1.14 christos {
11477 1.14 christos asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
11478 1.14 christos SEC_HAS_CONTENTS);
11479 1.14 christos
11480 1.14 christos if (sect == NULL)
11481 1.14 christos return false;
11482 1.14 christos sect->size = note->descsz;
11483 1.14 christos sect->filepos = note->descpos;
11484 1.14 christos sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
11485 1.1 christos
11486 1.1 christos return true;
11487 1.14 christos }
11488 1.1 christos
11489 1.1 christos return true;
11490 1.1 christos }
11491 1.1 christos
11492 1.1 christos static bool
11493 1.1 christos elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
11494 1.1 christos {
11495 1.1 christos void *ddata = note->descdata;
11496 1.1 christos char buf[100];
11497 1.11 christos char *name;
11498 1.14 christos asection *sect;
11499 1.11 christos short sig;
11500 1.1 christos unsigned flags;
11501 1.3 christos
11502 1.1 christos if (note->descsz < 16)
11503 1.1 christos return false;
11504 1.1 christos
11505 1.1 christos /* nto_procfs_status 'pid' field is at offset 0. */
11506 1.1 christos elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
11507 1.1 christos
11508 1.1 christos /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
11509 1.1 christos *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
11510 1.1 christos
11511 1.1 christos /* nto_procfs_status 'flags' field is at offset 8. */
11512 1.3 christos flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
11513 1.3 christos
11514 1.1 christos /* nto_procfs_status 'what' field is at offset 14. */
11515 1.1 christos if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
11516 1.1 christos {
11517 1.1 christos elf_tdata (abfd)->core->signal = sig;
11518 1.1 christos elf_tdata (abfd)->core->lwpid = *tid;
11519 1.1 christos }
11520 1.3 christos
11521 1.1 christos /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
11522 1.1 christos do not come from signals so we make sure we set the current
11523 1.1 christos thread just in case. */
11524 1.1 christos if (flags & 0x00000080)
11525 1.1 christos elf_tdata (abfd)->core->lwpid = *tid;
11526 1.1 christos
11527 1.14 christos /* Make a ".qnx_core_status/%d" section. */
11528 1.1 christos sprintf (buf, ".qnx_core_status/%ld", *tid);
11529 1.1 christos
11530 1.1 christos name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
11531 1.1 christos if (name == NULL)
11532 1.14 christos return false;
11533 1.1 christos strcpy (name, buf);
11534 1.11 christos
11535 1.11 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11536 1.1 christos if (sect == NULL)
11537 1.1 christos return false;
11538 1.1 christos
11539 1.1 christos sect->size = note->descsz;
11540 1.1 christos sect->filepos = note->descpos;
11541 1.14 christos sect->alignment_power = 2;
11542 1.1 christos
11543 1.1 christos return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
11544 1.1 christos }
11545 1.1 christos
11546 1.1 christos static bool
11547 1.1 christos elfcore_grok_nto_regs (bfd *abfd,
11548 1.1 christos Elf_Internal_Note *note,
11549 1.1 christos long tid,
11550 1.1 christos char *base)
11551 1.1 christos {
11552 1.1 christos char buf[100];
11553 1.1 christos char *name;
11554 1.1 christos asection *sect;
11555 1.1 christos
11556 1.14 christos /* Make a "(base)/%d" section. */
11557 1.1 christos sprintf (buf, "%s/%ld", base, tid);
11558 1.1 christos
11559 1.1 christos name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
11560 1.1 christos if (name == NULL)
11561 1.14 christos return false;
11562 1.1 christos strcpy (name, buf);
11563 1.11 christos
11564 1.11 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11565 1.1 christos if (sect == NULL)
11566 1.1 christos return false;
11567 1.1 christos
11568 1.3 christos sect->size = note->descsz;
11569 1.1 christos sect->filepos = note->descpos;
11570 1.1 christos sect->alignment_power = 2;
11571 1.14 christos
11572 1.1 christos /* This is the current thread. */
11573 1.1 christos if (elf_tdata (abfd)->core->lwpid == tid)
11574 1.1 christos return elfcore_maybe_make_sect (abfd, base, sect);
11575 1.1 christos
11576 1.1 christos return true;
11577 1.1 christos }
11578 1.1 christos
11579 1.14 christos #define BFD_QNT_CORE_INFO 7
11580 1.1 christos #define BFD_QNT_CORE_STATUS 8
11581 1.1 christos #define BFD_QNT_CORE_GREG 9
11582 1.1 christos #define BFD_QNT_CORE_FPREG 10
11583 1.1 christos
11584 1.1 christos static bool
11585 1.1 christos elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
11586 1.1 christos {
11587 1.1 christos /* Every GREG section has a STATUS section before it. Store the
11588 1.1 christos tid from the previous call to pass down to the next gregs
11589 1.1 christos function. */
11590 1.1 christos static long tid = 1;
11591 1.1 christos
11592 1.1 christos switch (note->type)
11593 1.1 christos {
11594 1.1 christos case BFD_QNT_CORE_INFO:
11595 1.1 christos return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
11596 1.1 christos case BFD_QNT_CORE_STATUS:
11597 1.1 christos return elfcore_grok_nto_status (abfd, note, &tid);
11598 1.14 christos case BFD_QNT_CORE_GREG:
11599 1.1 christos return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
11600 1.1 christos case BFD_QNT_CORE_FPREG:
11601 1.1 christos return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
11602 1.14 christos default:
11603 1.1 christos return true;
11604 1.1 christos }
11605 1.1 christos }
11606 1.1 christos
11607 1.1 christos static bool
11608 1.1 christos elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
11609 1.1 christos {
11610 1.1 christos char *name;
11611 1.1 christos asection *sect;
11612 1.1 christos size_t len;
11613 1.14 christos
11614 1.1 christos /* Use note name as section name. */
11615 1.1 christos len = note->namesz;
11616 1.1 christos name = (char *) bfd_alloc (abfd, len);
11617 1.1 christos if (name == NULL)
11618 1.1 christos return false;
11619 1.14 christos memcpy (name, note->namedata, len);
11620 1.1 christos name[len - 1] = '\0';
11621 1.11 christos
11622 1.11 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11623 1.1 christos if (sect == NULL)
11624 1.1 christos return false;
11625 1.14 christos
11626 1.1 christos sect->size = note->descsz;
11627 1.1 christos sect->filepos = note->descpos;
11628 1.1 christos sect->alignment_power = 1;
11629 1.1 christos
11630 1.1 christos return true;
11631 1.1 christos }
11632 1.1 christos
11633 1.1 christos /* Function: elfcore_write_note
11634 1.1 christos
11635 1.1 christos Inputs:
11636 1.1 christos buffer to hold note, and current size of buffer
11637 1.1 christos name of note
11638 1.1 christos type of note
11639 1.1 christos data for note
11640 1.1 christos size of data for note
11641 1.1 christos
11642 1.1 christos Writes note to end of buffer. ELF64 notes are written exactly as
11643 1.1 christos for ELF32, despite the current (as of 2006) ELF gabi specifying
11644 1.1 christos that they ought to have 8-byte namesz and descsz field, and have
11645 1.1 christos 8-byte alignment. Other writers, eg. Linux kernel, do the same.
11646 1.1 christos
11647 1.1 christos Return:
11648 1.1 christos Pointer to realloc'd buffer, *BUFSIZ updated. */
11649 1.1 christos
11650 1.1 christos char *
11651 1.1 christos elfcore_write_note (bfd *abfd,
11652 1.1 christos char *buf,
11653 1.1 christos int *bufsiz,
11654 1.1 christos const char *name,
11655 1.1 christos int type,
11656 1.1 christos const void *input,
11657 1.1 christos int size)
11658 1.1 christos {
11659 1.1 christos Elf_External_Note *xnp;
11660 1.1 christos size_t namesz;
11661 1.1 christos size_t newspace;
11662 1.1 christos char *dest;
11663 1.1 christos
11664 1.1 christos namesz = 0;
11665 1.1 christos if (name != NULL)
11666 1.1 christos namesz = strlen (name) + 1;
11667 1.1 christos
11668 1.1 christos newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
11669 1.1 christos
11670 1.1 christos buf = (char *) realloc (buf, *bufsiz + newspace);
11671 1.1 christos if (buf == NULL)
11672 1.1 christos return buf;
11673 1.1 christos dest = buf + *bufsiz;
11674 1.1 christos *bufsiz += newspace;
11675 1.1 christos xnp = (Elf_External_Note *) dest;
11676 1.1 christos H_PUT_32 (abfd, namesz, xnp->namesz);
11677 1.1 christos H_PUT_32 (abfd, size, xnp->descsz);
11678 1.1 christos H_PUT_32 (abfd, type, xnp->type);
11679 1.1 christos dest = xnp->name;
11680 1.1 christos if (name != NULL)
11681 1.1 christos {
11682 1.1 christos memcpy (dest, name, namesz);
11683 1.1 christos dest += namesz;
11684 1.1 christos while (namesz & 3)
11685 1.1 christos {
11686 1.1 christos *dest++ = '\0';
11687 1.1 christos ++namesz;
11688 1.1 christos }
11689 1.1 christos }
11690 1.1 christos memcpy (dest, input, size);
11691 1.1 christos dest += size;
11692 1.1 christos while (size & 3)
11693 1.1 christos {
11694 1.1 christos *dest++ = '\0';
11695 1.11 christos ++size;
11696 1.11 christos }
11697 1.11 christos return buf;
11698 1.11 christos }
11699 1.11 christos
11700 1.11 christos /* gcc-8 warns (*) on all the strncpy calls in this function about
11701 1.11 christos possible string truncation. The "truncation" is not a bug. We
11702 1.11 christos have an external representation of structs with fields that are not
11703 1.11 christos necessarily NULL terminated and corresponding internal
11704 1.11 christos representation fields that are one larger so that they can always
11705 1.11 christos be NULL terminated.
11706 1.11 christos gcc versions between 4.2 and 4.6 do not allow pragma control of
11707 1.11 christos diagnostics inside functions, giving a hard error if you try to use
11708 1.11 christos the finer control available with later versions.
11709 1.11 christos gcc prior to 4.2 warns about diagnostic push and pop.
11710 1.11 christos gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11711 1.11 christos unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11712 1.1 christos (*) Depending on your system header files! */
11713 1.1 christos #if GCC_VERSION >= 8000
11714 1.1 christos # pragma GCC diagnostic push
11715 1.1 christos # pragma GCC diagnostic ignored "-Wstringop-truncation"
11716 1.1 christos #endif
11717 1.1 christos char *
11718 1.1 christos elfcore_write_prpsinfo (bfd *abfd,
11719 1.1 christos char *buf,
11720 1.1 christos int *bufsiz,
11721 1.1 christos const char *fname,
11722 1.1 christos const char *psargs)
11723 1.1 christos {
11724 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11725 1.1 christos
11726 1.1 christos if (bed->elf_backend_write_core_note != NULL)
11727 1.1 christos {
11728 1.1 christos char *ret;
11729 1.1 christos ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11730 1.3 christos NT_PRPSINFO, fname, psargs);
11731 1.11 christos if (ret != NULL)
11732 1.1 christos return ret;
11733 1.1 christos }
11734 1.11 christos
11735 1.1 christos #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11736 1.1 christos # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
11737 1.11 christos if (bed->s->elfclass == ELFCLASS32)
11738 1.1 christos {
11739 1.1 christos # if defined (HAVE_PSINFO32_T)
11740 1.11 christos psinfo32_t data;
11741 1.1 christos int note_type = NT_PSINFO;
11742 1.1 christos # else
11743 1.1 christos prpsinfo32_t data;
11744 1.1 christos int note_type = NT_PRPSINFO;
11745 1.1 christos # endif
11746 1.3 christos
11747 1.1 christos memset (&data, 0, sizeof (data));
11748 1.1 christos strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11749 1.11 christos strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11750 1.1 christos return elfcore_write_note (abfd, buf, bufsiz,
11751 1.11 christos "CORE", note_type, &data, sizeof (data));
11752 1.1 christos }
11753 1.1 christos else
11754 1.11 christos # endif
11755 1.1 christos {
11756 1.1 christos # if defined (HAVE_PSINFO_T)
11757 1.11 christos psinfo_t data;
11758 1.1 christos int note_type = NT_PSINFO;
11759 1.1 christos # else
11760 1.1 christos prpsinfo_t data;
11761 1.1 christos int note_type = NT_PRPSINFO;
11762 1.1 christos # endif
11763 1.3 christos
11764 1.1 christos memset (&data, 0, sizeof (data));
11765 1.3 christos strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11766 1.3 christos strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11767 1.3 christos return elfcore_write_note (abfd, buf, bufsiz,
11768 1.3 christos "CORE", note_type, &data, sizeof (data));
11769 1.3 christos }
11770 1.11 christos #endif /* PSINFO_T or PRPSINFO_T */
11771 1.11 christos
11772 1.11 christos free (buf);
11773 1.3 christos return NULL;
11774 1.3 christos }
11775 1.3 christos #if GCC_VERSION >= 8000
11776 1.3 christos # pragma GCC diagnostic pop
11777 1.3 christos #endif
11778 1.3 christos
11779 1.11 christos char *
11780 1.11 christos elfcore_write_linux_prpsinfo32
11781 1.11 christos (bfd *abfd, char *buf, int *bufsiz,
11782 1.3 christos const struct elf_internal_linux_prpsinfo *prpsinfo)
11783 1.11 christos {
11784 1.11 christos if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
11785 1.11 christos {
11786 1.11 christos struct elf_external_linux_prpsinfo32_ugid16 data;
11787 1.11 christos
11788 1.11 christos swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
11789 1.11 christos return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11790 1.11 christos &data, sizeof (data));
11791 1.11 christos }
11792 1.11 christos else
11793 1.11 christos {
11794 1.11 christos struct elf_external_linux_prpsinfo32_ugid32 data;
11795 1.3 christos
11796 1.3 christos swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
11797 1.3 christos return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11798 1.3 christos &data, sizeof (data));
11799 1.3 christos }
11800 1.3 christos }
11801 1.3 christos
11802 1.11 christos char *
11803 1.11 christos elfcore_write_linux_prpsinfo64
11804 1.11 christos (bfd *abfd, char *buf, int *bufsiz,
11805 1.3 christos const struct elf_internal_linux_prpsinfo *prpsinfo)
11806 1.11 christos {
11807 1.11 christos if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
11808 1.11 christos {
11809 1.11 christos struct elf_external_linux_prpsinfo64_ugid16 data;
11810 1.11 christos
11811 1.11 christos swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
11812 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
11813 1.11 christos "CORE", NT_PRPSINFO, &data, sizeof (data));
11814 1.11 christos }
11815 1.11 christos else
11816 1.11 christos {
11817 1.11 christos struct elf_external_linux_prpsinfo64_ugid32 data;
11818 1.1 christos
11819 1.1 christos swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
11820 1.1 christos return elfcore_write_note (abfd, buf, bufsiz,
11821 1.1 christos "CORE", NT_PRPSINFO, &data, sizeof (data));
11822 1.1 christos }
11823 1.1 christos }
11824 1.1 christos
11825 1.1 christos char *
11826 1.1 christos elfcore_write_prstatus (bfd *abfd,
11827 1.1 christos char *buf,
11828 1.1 christos int *bufsiz,
11829 1.1 christos long pid,
11830 1.1 christos int cursig,
11831 1.1 christos const void *gregs)
11832 1.1 christos {
11833 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11834 1.1 christos
11835 1.1 christos if (bed->elf_backend_write_core_note != NULL)
11836 1.1 christos {
11837 1.1 christos char *ret;
11838 1.1 christos ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11839 1.1 christos NT_PRSTATUS,
11840 1.3 christos pid, cursig, gregs);
11841 1.1 christos if (ret != NULL)
11842 1.1 christos return ret;
11843 1.1 christos }
11844 1.1 christos
11845 1.1 christos #if defined (HAVE_PRSTATUS_T)
11846 1.1 christos #if defined (HAVE_PRSTATUS32_T)
11847 1.1 christos if (bed->s->elfclass == ELFCLASS32)
11848 1.1 christos {
11849 1.1 christos prstatus32_t prstat;
11850 1.3 christos
11851 1.1 christos memset (&prstat, 0, sizeof (prstat));
11852 1.1 christos prstat.pr_pid = pid;
11853 1.1 christos prstat.pr_cursig = cursig;
11854 1.1 christos memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
11855 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, "CORE",
11856 1.1 christos NT_PRSTATUS, &prstat, sizeof (prstat));
11857 1.1 christos }
11858 1.1 christos else
11859 1.1 christos #endif
11860 1.1 christos {
11861 1.1 christos prstatus_t prstat;
11862 1.3 christos
11863 1.1 christos memset (&prstat, 0, sizeof (prstat));
11864 1.1 christos prstat.pr_pid = pid;
11865 1.3 christos prstat.pr_cursig = cursig;
11866 1.3 christos memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
11867 1.3 christos return elfcore_write_note (abfd, buf, bufsiz, "CORE",
11868 1.3 christos NT_PRSTATUS, &prstat, sizeof (prstat));
11869 1.1 christos }
11870 1.1 christos #endif /* HAVE_PRSTATUS_T */
11871 1.1 christos
11872 1.1 christos free (buf);
11873 1.1 christos return NULL;
11874 1.1 christos }
11875 1.1 christos
11876 1.1 christos #if defined (HAVE_LWPSTATUS_T)
11877 1.1 christos char *
11878 1.1 christos elfcore_write_lwpstatus (bfd *abfd,
11879 1.1 christos char *buf,
11880 1.1 christos int *bufsiz,
11881 1.1 christos long pid,
11882 1.1 christos int cursig,
11883 1.1 christos const void *gregs)
11884 1.1 christos {
11885 1.1 christos lwpstatus_t lwpstat;
11886 1.1 christos const char *note_name = "CORE";
11887 1.5 christos
11888 1.1 christos memset (&lwpstat, 0, sizeof (lwpstat));
11889 1.1 christos lwpstat.pr_lwpid = pid >> 16;
11890 1.1 christos lwpstat.pr_cursig = cursig;
11891 1.1 christos #if defined (HAVE_LWPSTATUS_T_PR_REG)
11892 1.1 christos memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
11893 1.1 christos #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11894 1.1 christos #if !defined(gregs)
11895 1.1 christos memcpy (lwpstat.pr_context.uc_mcontext.gregs,
11896 1.1 christos gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
11897 1.1 christos #else
11898 1.1 christos memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
11899 1.1 christos gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
11900 1.1 christos #endif
11901 1.1 christos #endif
11902 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, note_name,
11903 1.1 christos NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
11904 1.1 christos }
11905 1.1 christos #endif /* HAVE_LWPSTATUS_T */
11906 1.1 christos
11907 1.1 christos #if defined (HAVE_PSTATUS_T)
11908 1.1 christos char *
11909 1.1 christos elfcore_write_pstatus (bfd *abfd,
11910 1.1 christos char *buf,
11911 1.1 christos int *bufsiz,
11912 1.1 christos long pid,
11913 1.1 christos int cursig ATTRIBUTE_UNUSED,
11914 1.1 christos const void *gregs ATTRIBUTE_UNUSED)
11915 1.1 christos {
11916 1.1 christos const char *note_name = "CORE";
11917 1.1 christos #if defined (HAVE_PSTATUS32_T)
11918 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11919 1.1 christos
11920 1.1 christos if (bed->s->elfclass == ELFCLASS32)
11921 1.1 christos {
11922 1.1 christos pstatus32_t pstat;
11923 1.1 christos
11924 1.1 christos memset (&pstat, 0, sizeof (pstat));
11925 1.1 christos pstat.pr_pid = pid & 0xffff;
11926 1.1 christos buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11927 1.1 christos NT_PSTATUS, &pstat, sizeof (pstat));
11928 1.1 christos return buf;
11929 1.1 christos }
11930 1.1 christos else
11931 1.1 christos #endif
11932 1.1 christos {
11933 1.1 christos pstatus_t pstat;
11934 1.1 christos
11935 1.1 christos memset (&pstat, 0, sizeof (pstat));
11936 1.1 christos pstat.pr_pid = pid & 0xffff;
11937 1.1 christos buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11938 1.1 christos NT_PSTATUS, &pstat, sizeof (pstat));
11939 1.1 christos return buf;
11940 1.1 christos }
11941 1.1 christos }
11942 1.1 christos #endif /* HAVE_PSTATUS_T */
11943 1.1 christos
11944 1.1 christos char *
11945 1.1 christos elfcore_write_prfpreg (bfd *abfd,
11946 1.1 christos char *buf,
11947 1.1 christos int *bufsiz,
11948 1.1 christos const void *fpregs,
11949 1.1 christos int size)
11950 1.1 christos {
11951 1.1 christos const char *note_name = "CORE";
11952 1.1 christos return elfcore_write_note (abfd, buf, bufsiz,
11953 1.1 christos note_name, NT_FPREGSET, fpregs, size);
11954 1.1 christos }
11955 1.1 christos
11956 1.1 christos char *
11957 1.1 christos elfcore_write_prxfpreg (bfd *abfd,
11958 1.1 christos char *buf,
11959 1.1 christos int *bufsiz,
11960 1.1 christos const void *xfpregs,
11961 1.1 christos int size)
11962 1.1 christos {
11963 1.1 christos char *note_name = "LINUX";
11964 1.1 christos return elfcore_write_note (abfd, buf, bufsiz,
11965 1.1 christos note_name, NT_PRXFPREG, xfpregs, size);
11966 1.1 christos }
11967 1.6 christos
11968 1.6 christos char *
11969 1.6 christos elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
11970 1.6 christos const void *xfpregs, int size)
11971 1.6 christos {
11972 1.1 christos char *note_name;
11973 1.1 christos if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
11974 1.1 christos note_name = "FreeBSD";
11975 1.1 christos else
11976 1.1 christos note_name = "LINUX";
11977 1.14 christos return elfcore_write_note (abfd, buf, bufsiz,
11978 1.14 christos note_name, NT_X86_XSTATE, xfpregs, size);
11979 1.14 christos }
11980 1.14 christos
11981 1.14 christos char *
11982 1.14 christos elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
11983 1.14 christos const void *regs, int size)
11984 1.14 christos {
11985 1.14 christos char *note_name = "FreeBSD";
11986 1.1 christos return elfcore_write_note (abfd, buf, bufsiz,
11987 1.1 christos note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
11988 1.1 christos }
11989 1.1 christos
11990 1.1 christos char *
11991 1.1 christos elfcore_write_ppc_vmx (bfd *abfd,
11992 1.1 christos char *buf,
11993 1.1 christos int *bufsiz,
11994 1.1 christos const void *ppc_vmx,
11995 1.1 christos int size)
11996 1.1 christos {
11997 1.1 christos char *note_name = "LINUX";
11998 1.1 christos return elfcore_write_note (abfd, buf, bufsiz,
11999 1.11 christos note_name, NT_PPC_VMX, ppc_vmx, size);
12000 1.11 christos }
12001 1.11 christos
12002 1.11 christos char *
12003 1.11 christos elfcore_write_ppc_vsx (bfd *abfd,
12004 1.11 christos char *buf,
12005 1.11 christos int *bufsiz,
12006 1.11 christos const void *ppc_vsx,
12007 1.11 christos int size)
12008 1.11 christos {
12009 1.11 christos char *note_name = "LINUX";
12010 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12011 1.12 christos note_name, NT_PPC_VSX, ppc_vsx, size);
12012 1.12 christos }
12013 1.12 christos
12014 1.12 christos char *
12015 1.1 christos elfcore_write_ppc_tar (bfd *abfd,
12016 1.1 christos char *buf,
12017 1.1 christos int *bufsiz,
12018 1.12 christos const void *ppc_tar,
12019 1.11 christos int size)
12020 1.11 christos {
12021 1.11 christos char *note_name = "LINUX";
12022 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12023 1.12 christos note_name, NT_PPC_TAR, ppc_tar, size);
12024 1.12 christos }
12025 1.12 christos
12026 1.12 christos char *
12027 1.11 christos elfcore_write_ppc_ppr (bfd *abfd,
12028 1.11 christos char *buf,
12029 1.11 christos int *bufsiz,
12030 1.12 christos const void *ppc_ppr,
12031 1.11 christos int size)
12032 1.11 christos {
12033 1.11 christos char *note_name = "LINUX";
12034 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12035 1.12 christos note_name, NT_PPC_PPR, ppc_ppr, size);
12036 1.12 christos }
12037 1.12 christos
12038 1.12 christos char *
12039 1.11 christos elfcore_write_ppc_dscr (bfd *abfd,
12040 1.11 christos char *buf,
12041 1.11 christos int *bufsiz,
12042 1.12 christos const void *ppc_dscr,
12043 1.11 christos int size)
12044 1.11 christos {
12045 1.11 christos char *note_name = "LINUX";
12046 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12047 1.12 christos note_name, NT_PPC_DSCR, ppc_dscr, size);
12048 1.12 christos }
12049 1.12 christos
12050 1.12 christos char *
12051 1.11 christos elfcore_write_ppc_ebb (bfd *abfd,
12052 1.11 christos char *buf,
12053 1.11 christos int *bufsiz,
12054 1.12 christos const void *ppc_ebb,
12055 1.11 christos int size)
12056 1.11 christos {
12057 1.11 christos char *note_name = "LINUX";
12058 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12059 1.12 christos note_name, NT_PPC_EBB, ppc_ebb, size);
12060 1.12 christos }
12061 1.12 christos
12062 1.12 christos char *
12063 1.11 christos elfcore_write_ppc_pmu (bfd *abfd,
12064 1.11 christos char *buf,
12065 1.11 christos int *bufsiz,
12066 1.12 christos const void *ppc_pmu,
12067 1.11 christos int size)
12068 1.11 christos {
12069 1.11 christos char *note_name = "LINUX";
12070 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12071 1.12 christos note_name, NT_PPC_PMU, ppc_pmu, size);
12072 1.12 christos }
12073 1.12 christos
12074 1.12 christos char *
12075 1.11 christos elfcore_write_ppc_tm_cgpr (bfd *abfd,
12076 1.11 christos char *buf,
12077 1.11 christos int *bufsiz,
12078 1.12 christos const void *ppc_tm_cgpr,
12079 1.11 christos int size)
12080 1.11 christos {
12081 1.11 christos char *note_name = "LINUX";
12082 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12083 1.12 christos note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
12084 1.12 christos }
12085 1.12 christos
12086 1.12 christos char *
12087 1.11 christos elfcore_write_ppc_tm_cfpr (bfd *abfd,
12088 1.11 christos char *buf,
12089 1.11 christos int *bufsiz,
12090 1.12 christos const void *ppc_tm_cfpr,
12091 1.11 christos int size)
12092 1.11 christos {
12093 1.11 christos char *note_name = "LINUX";
12094 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12095 1.12 christos note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
12096 1.12 christos }
12097 1.12 christos
12098 1.12 christos char *
12099 1.11 christos elfcore_write_ppc_tm_cvmx (bfd *abfd,
12100 1.11 christos char *buf,
12101 1.11 christos int *bufsiz,
12102 1.12 christos const void *ppc_tm_cvmx,
12103 1.11 christos int size)
12104 1.11 christos {
12105 1.11 christos char *note_name = "LINUX";
12106 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12107 1.12 christos note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
12108 1.12 christos }
12109 1.12 christos
12110 1.12 christos char *
12111 1.11 christos elfcore_write_ppc_tm_cvsx (bfd *abfd,
12112 1.11 christos char *buf,
12113 1.11 christos int *bufsiz,
12114 1.12 christos const void *ppc_tm_cvsx,
12115 1.11 christos int size)
12116 1.11 christos {
12117 1.11 christos char *note_name = "LINUX";
12118 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12119 1.12 christos note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
12120 1.12 christos }
12121 1.12 christos
12122 1.12 christos char *
12123 1.11 christos elfcore_write_ppc_tm_spr (bfd *abfd,
12124 1.11 christos char *buf,
12125 1.11 christos int *bufsiz,
12126 1.12 christos const void *ppc_tm_spr,
12127 1.11 christos int size)
12128 1.11 christos {
12129 1.11 christos char *note_name = "LINUX";
12130 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12131 1.12 christos note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
12132 1.12 christos }
12133 1.12 christos
12134 1.12 christos char *
12135 1.11 christos elfcore_write_ppc_tm_ctar (bfd *abfd,
12136 1.11 christos char *buf,
12137 1.11 christos int *bufsiz,
12138 1.12 christos const void *ppc_tm_ctar,
12139 1.11 christos int size)
12140 1.11 christos {
12141 1.11 christos char *note_name = "LINUX";
12142 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12143 1.12 christos note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
12144 1.12 christos }
12145 1.12 christos
12146 1.12 christos char *
12147 1.11 christos elfcore_write_ppc_tm_cppr (bfd *abfd,
12148 1.11 christos char *buf,
12149 1.11 christos int *bufsiz,
12150 1.12 christos const void *ppc_tm_cppr,
12151 1.11 christos int size)
12152 1.11 christos {
12153 1.11 christos char *note_name = "LINUX";
12154 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12155 1.12 christos note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
12156 1.12 christos }
12157 1.12 christos
12158 1.12 christos char *
12159 1.11 christos elfcore_write_ppc_tm_cdscr (bfd *abfd,
12160 1.11 christos char *buf,
12161 1.11 christos int *bufsiz,
12162 1.12 christos const void *ppc_tm_cdscr,
12163 1.1 christos int size)
12164 1.1 christos {
12165 1.1 christos char *note_name = "LINUX";
12166 1.1 christos return elfcore_write_note (abfd, buf, bufsiz,
12167 1.1 christos note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
12168 1.1 christos }
12169 1.1 christos
12170 1.1 christos static char *
12171 1.1 christos elfcore_write_s390_high_gprs (bfd *abfd,
12172 1.1 christos char *buf,
12173 1.1 christos int *bufsiz,
12174 1.11 christos const void *s390_high_gprs,
12175 1.1 christos int size)
12176 1.1 christos {
12177 1.1 christos char *note_name = "LINUX";
12178 1.1 christos return elfcore_write_note (abfd, buf, bufsiz,
12179 1.1 christos note_name, NT_S390_HIGH_GPRS,
12180 1.11 christos s390_high_gprs, size);
12181 1.11 christos }
12182 1.11 christos
12183 1.11 christos char *
12184 1.1 christos elfcore_write_s390_timer (bfd *abfd,
12185 1.1 christos char *buf,
12186 1.1 christos int *bufsiz,
12187 1.11 christos const void *s390_timer,
12188 1.1 christos int size)
12189 1.1 christos {
12190 1.1 christos char *note_name = "LINUX";
12191 1.1 christos return elfcore_write_note (abfd, buf, bufsiz,
12192 1.11 christos note_name, NT_S390_TIMER, s390_timer, size);
12193 1.11 christos }
12194 1.11 christos
12195 1.11 christos char *
12196 1.1 christos elfcore_write_s390_todcmp (bfd *abfd,
12197 1.1 christos char *buf,
12198 1.1 christos int *bufsiz,
12199 1.11 christos const void *s390_todcmp,
12200 1.1 christos int size)
12201 1.1 christos {
12202 1.1 christos char *note_name = "LINUX";
12203 1.1 christos return elfcore_write_note (abfd, buf, bufsiz,
12204 1.11 christos note_name, NT_S390_TODCMP, s390_todcmp, size);
12205 1.11 christos }
12206 1.11 christos
12207 1.11 christos char *
12208 1.1 christos elfcore_write_s390_todpreg (bfd *abfd,
12209 1.1 christos char *buf,
12210 1.1 christos int *bufsiz,
12211 1.11 christos const void *s390_todpreg,
12212 1.1 christos int size)
12213 1.1 christos {
12214 1.1 christos char *note_name = "LINUX";
12215 1.1 christos return elfcore_write_note (abfd, buf, bufsiz,
12216 1.11 christos note_name, NT_S390_TODPREG, s390_todpreg, size);
12217 1.11 christos }
12218 1.11 christos
12219 1.11 christos char *
12220 1.1 christos elfcore_write_s390_ctrs (bfd *abfd,
12221 1.1 christos char *buf,
12222 1.1 christos int *bufsiz,
12223 1.11 christos const void *s390_ctrs,
12224 1.1 christos int size)
12225 1.1 christos {
12226 1.1 christos char *note_name = "LINUX";
12227 1.1 christos return elfcore_write_note (abfd, buf, bufsiz,
12228 1.11 christos note_name, NT_S390_CTRS, s390_ctrs, size);
12229 1.11 christos }
12230 1.11 christos
12231 1.11 christos char *
12232 1.1 christos elfcore_write_s390_prefix (bfd *abfd,
12233 1.1 christos char *buf,
12234 1.1 christos int *bufsiz,
12235 1.11 christos const void *s390_prefix,
12236 1.1 christos int size)
12237 1.1 christos {
12238 1.1 christos char *note_name = "LINUX";
12239 1.3 christos return elfcore_write_note (abfd, buf, bufsiz,
12240 1.3 christos note_name, NT_S390_PREFIX, s390_prefix, size);
12241 1.3 christos }
12242 1.3 christos
12243 1.3 christos char *
12244 1.3 christos elfcore_write_s390_last_break (bfd *abfd,
12245 1.3 christos char *buf,
12246 1.3 christos int *bufsiz,
12247 1.11 christos const void *s390_last_break,
12248 1.3 christos int size)
12249 1.3 christos {
12250 1.3 christos char *note_name = "LINUX";
12251 1.3 christos return elfcore_write_note (abfd, buf, bufsiz,
12252 1.3 christos note_name, NT_S390_LAST_BREAK,
12253 1.3 christos s390_last_break, size);
12254 1.3 christos }
12255 1.3 christos
12256 1.3 christos char *
12257 1.3 christos elfcore_write_s390_system_call (bfd *abfd,
12258 1.3 christos char *buf,
12259 1.3 christos int *bufsiz,
12260 1.11 christos const void *s390_system_call,
12261 1.3 christos int size)
12262 1.3 christos {
12263 1.3 christos char *note_name = "LINUX";
12264 1.3 christos return elfcore_write_note (abfd, buf, bufsiz,
12265 1.3 christos note_name, NT_S390_SYSTEM_CALL,
12266 1.3 christos s390_system_call, size);
12267 1.3 christos }
12268 1.3 christos
12269 1.3 christos char *
12270 1.3 christos elfcore_write_s390_tdb (bfd *abfd,
12271 1.3 christos char *buf,
12272 1.3 christos int *bufsiz,
12273 1.11 christos const void *s390_tdb,
12274 1.3 christos int size)
12275 1.3 christos {
12276 1.3 christos char *note_name = "LINUX";
12277 1.6 christos return elfcore_write_note (abfd, buf, bufsiz,
12278 1.6 christos note_name, NT_S390_TDB, s390_tdb, size);
12279 1.6 christos }
12280 1.6 christos
12281 1.6 christos char *
12282 1.6 christos elfcore_write_s390_vxrs_low (bfd *abfd,
12283 1.6 christos char *buf,
12284 1.6 christos int *bufsiz,
12285 1.6 christos const void *s390_vxrs_low,
12286 1.6 christos int size)
12287 1.6 christos {
12288 1.6 christos char *note_name = "LINUX";
12289 1.6 christos return elfcore_write_note (abfd, buf, bufsiz,
12290 1.6 christos note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
12291 1.6 christos }
12292 1.6 christos
12293 1.6 christos char *
12294 1.6 christos elfcore_write_s390_vxrs_high (bfd *abfd,
12295 1.6 christos char *buf,
12296 1.6 christos int *bufsiz,
12297 1.6 christos const void *s390_vxrs_high,
12298 1.6 christos int size)
12299 1.6 christos {
12300 1.6 christos char *note_name = "LINUX";
12301 1.6 christos return elfcore_write_note (abfd, buf, bufsiz,
12302 1.11 christos note_name, NT_S390_VXRS_HIGH,
12303 1.11 christos s390_vxrs_high, size);
12304 1.11 christos }
12305 1.11 christos
12306 1.11 christos char *
12307 1.11 christos elfcore_write_s390_gs_cb (bfd *abfd,
12308 1.11 christos char *buf,
12309 1.11 christos int *bufsiz,
12310 1.11 christos const void *s390_gs_cb,
12311 1.11 christos int size)
12312 1.11 christos {
12313 1.11 christos char *note_name = "LINUX";
12314 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12315 1.11 christos note_name, NT_S390_GS_CB,
12316 1.11 christos s390_gs_cb, size);
12317 1.11 christos }
12318 1.11 christos
12319 1.11 christos char *
12320 1.11 christos elfcore_write_s390_gs_bc (bfd *abfd,
12321 1.11 christos char *buf,
12322 1.11 christos int *bufsiz,
12323 1.11 christos const void *s390_gs_bc,
12324 1.11 christos int size)
12325 1.11 christos {
12326 1.11 christos char *note_name = "LINUX";
12327 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12328 1.3 christos note_name, NT_S390_GS_BC,
12329 1.3 christos s390_gs_bc, size);
12330 1.3 christos }
12331 1.3 christos
12332 1.3 christos char *
12333 1.3 christos elfcore_write_arm_vfp (bfd *abfd,
12334 1.3 christos char *buf,
12335 1.3 christos int *bufsiz,
12336 1.3 christos const void *arm_vfp,
12337 1.3 christos int size)
12338 1.3 christos {
12339 1.3 christos char *note_name = "LINUX";
12340 1.3 christos return elfcore_write_note (abfd, buf, bufsiz,
12341 1.3 christos note_name, NT_ARM_VFP, arm_vfp, size);
12342 1.3 christos }
12343 1.3 christos
12344 1.3 christos char *
12345 1.3 christos elfcore_write_aarch_tls (bfd *abfd,
12346 1.3 christos char *buf,
12347 1.3 christos int *bufsiz,
12348 1.3 christos const void *aarch_tls,
12349 1.3 christos int size)
12350 1.3 christos {
12351 1.3 christos char *note_name = "LINUX";
12352 1.3 christos return elfcore_write_note (abfd, buf, bufsiz,
12353 1.3 christos note_name, NT_ARM_TLS, aarch_tls, size);
12354 1.3 christos }
12355 1.3 christos
12356 1.3 christos char *
12357 1.3 christos elfcore_write_aarch_hw_break (bfd *abfd,
12358 1.3 christos char *buf,
12359 1.3 christos int *bufsiz,
12360 1.3 christos const void *aarch_hw_break,
12361 1.3 christos int size)
12362 1.3 christos {
12363 1.3 christos char *note_name = "LINUX";
12364 1.3 christos return elfcore_write_note (abfd, buf, bufsiz,
12365 1.3 christos note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
12366 1.3 christos }
12367 1.3 christos
12368 1.3 christos char *
12369 1.3 christos elfcore_write_aarch_hw_watch (bfd *abfd,
12370 1.3 christos char *buf,
12371 1.3 christos int *bufsiz,
12372 1.3 christos const void *aarch_hw_watch,
12373 1.3 christos int size)
12374 1.3 christos {
12375 1.3 christos char *note_name = "LINUX";
12376 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12377 1.11 christos note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
12378 1.11 christos }
12379 1.11 christos
12380 1.11 christos char *
12381 1.11 christos elfcore_write_aarch_sve (bfd *abfd,
12382 1.11 christos char *buf,
12383 1.11 christos int *bufsiz,
12384 1.11 christos const void *aarch_sve,
12385 1.11 christos int size)
12386 1.11 christos {
12387 1.11 christos char *note_name = "LINUX";
12388 1.11 christos return elfcore_write_note (abfd, buf, bufsiz,
12389 1.11 christos note_name, NT_ARM_SVE, aarch_sve, size);
12390 1.11 christos }
12391 1.11 christos
12392 1.11 christos char *
12393 1.11 christos elfcore_write_aarch_pauth (bfd *abfd,
12394 1.11 christos char *buf,
12395 1.11 christos int *bufsiz,
12396 1.11 christos const void *aarch_pauth,
12397 1.11 christos int size)
12398 1.11 christos {
12399 1.11 christos char *note_name = "LINUX";
12400 1.14 christos return elfcore_write_note (abfd, buf, bufsiz,
12401 1.14 christos note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
12402 1.14 christos }
12403 1.14 christos
12404 1.14 christos char *
12405 1.14 christos elfcore_write_aarch_mte (bfd *abfd,
12406 1.14 christos char *buf,
12407 1.14 christos int *bufsiz,
12408 1.14 christos const void *aarch_mte,
12409 1.14 christos int size)
12410 1.14 christos {
12411 1.14 christos char *note_name = "LINUX";
12412 1.14 christos return elfcore_write_note (abfd, buf, bufsiz,
12413 1.14 christos note_name, NT_ARM_TAGGED_ADDR_CTRL,
12414 1.12 christos aarch_mte,
12415 1.12 christos size);
12416 1.12 christos }
12417 1.12 christos
12418 1.12 christos char *
12419 1.12 christos elfcore_write_arc_v2 (bfd *abfd,
12420 1.12 christos char *buf,
12421 1.12 christos int *bufsiz,
12422 1.12 christos const void *arc_v2,
12423 1.12 christos int size)
12424 1.12 christos {
12425 1.12 christos char *note_name = "LINUX";
12426 1.14 christos return elfcore_write_note (abfd, buf, bufsiz,
12427 1.14 christos note_name, NT_ARC_V2, arc_v2, size);
12428 1.14 christos }
12429 1.14 christos
12430 1.14 christos char *
12431 1.14 christos elfcore_write_loongarch_cpucfg (bfd *abfd,
12432 1.14 christos char *buf,
12433 1.14 christos int *bufsiz,
12434 1.14 christos const void *loongarch_cpucfg,
12435 1.14 christos int size)
12436 1.14 christos {
12437 1.14 christos char *note_name = "LINUX";
12438 1.14 christos return elfcore_write_note (abfd, buf, bufsiz,
12439 1.14 christos note_name, NT_LARCH_CPUCFG,
12440 1.14 christos loongarch_cpucfg, size);
12441 1.14 christos }
12442 1.14 christos
12443 1.14 christos char *
12444 1.14 christos elfcore_write_loongarch_lbt (bfd *abfd,
12445 1.14 christos char *buf,
12446 1.14 christos int *bufsiz,
12447 1.14 christos const void *loongarch_lbt,
12448 1.14 christos int size)
12449 1.14 christos {
12450 1.14 christos char *note_name = "LINUX";
12451 1.14 christos return elfcore_write_note (abfd, buf, bufsiz,
12452 1.14 christos note_name, NT_LARCH_LBT, loongarch_lbt, size);
12453 1.14 christos }
12454 1.14 christos
12455 1.14 christos char *
12456 1.14 christos elfcore_write_loongarch_lsx (bfd *abfd,
12457 1.14 christos char *buf,
12458 1.14 christos int *bufsiz,
12459 1.14 christos const void *loongarch_lsx,
12460 1.14 christos int size)
12461 1.14 christos {
12462 1.14 christos char *note_name = "LINUX";
12463 1.14 christos return elfcore_write_note (abfd, buf, bufsiz,
12464 1.14 christos note_name, NT_LARCH_LSX, loongarch_lsx, size);
12465 1.14 christos }
12466 1.14 christos
12467 1.14 christos char *
12468 1.14 christos elfcore_write_loongarch_lasx (bfd *abfd,
12469 1.14 christos char *buf,
12470 1.14 christos int *bufsiz,
12471 1.14 christos const void *loongarch_lasx,
12472 1.14 christos int size)
12473 1.14 christos {
12474 1.14 christos char *note_name = "LINUX";
12475 1.14 christos return elfcore_write_note (abfd, buf, bufsiz,
12476 1.14 christos note_name, NT_LARCH_LASX, loongarch_lasx, size);
12477 1.14 christos }
12478 1.14 christos
12479 1.14 christos /* Write the buffer of csr values in CSRS (length SIZE) into the note
12480 1.14 christos buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
12481 1.14 christos written into. Return a pointer to the new start of the note buffer, to
12482 1.14 christos replace BUF which may no longer be valid. */
12483 1.14 christos
12484 1.14 christos char *
12485 1.14 christos elfcore_write_riscv_csr (bfd *abfd,
12486 1.14 christos char *buf,
12487 1.14 christos int *bufsiz,
12488 1.14 christos const void *csrs,
12489 1.14 christos int size)
12490 1.14 christos {
12491 1.14 christos const char *note_name = "GDB";
12492 1.14 christos return elfcore_write_note (abfd, buf, bufsiz,
12493 1.14 christos note_name, NT_RISCV_CSR, csrs, size);
12494 1.14 christos }
12495 1.14 christos
12496 1.14 christos /* Write the target description (a string) pointed to by TDESC, length
12497 1.14 christos SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the
12498 1.14 christos note is being written into. Return a pointer to the new start of the
12499 1.14 christos note buffer, to replace BUF which may no longer be valid. */
12500 1.14 christos
12501 1.14 christos char *
12502 1.14 christos elfcore_write_gdb_tdesc (bfd *abfd,
12503 1.14 christos char *buf,
12504 1.14 christos int *bufsiz,
12505 1.14 christos const void *tdesc,
12506 1.14 christos int size)
12507 1.14 christos {
12508 1.14 christos const char *note_name = "GDB";
12509 1.1 christos return elfcore_write_note (abfd, buf, bufsiz,
12510 1.1 christos note_name, NT_GDB_TDESC, tdesc, size);
12511 1.1 christos }
12512 1.1 christos
12513 1.1 christos char *
12514 1.1 christos elfcore_write_register_note (bfd *abfd,
12515 1.1 christos char *buf,
12516 1.1 christos int *bufsiz,
12517 1.1 christos const char *section,
12518 1.1 christos const void *data,
12519 1.1 christos int size)
12520 1.1 christos {
12521 1.1 christos if (strcmp (section, ".reg2") == 0)
12522 1.14 christos return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
12523 1.14 christos if (strcmp (section, ".reg-xfp") == 0)
12524 1.1 christos return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
12525 1.1 christos if (strcmp (section, ".reg-xstate") == 0)
12526 1.1 christos return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
12527 1.1 christos if (strcmp (section, ".reg-x86-segbases") == 0)
12528 1.11 christos return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
12529 1.11 christos if (strcmp (section, ".reg-ppc-vmx") == 0)
12530 1.11 christos return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
12531 1.11 christos if (strcmp (section, ".reg-ppc-vsx") == 0)
12532 1.11 christos return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
12533 1.11 christos if (strcmp (section, ".reg-ppc-tar") == 0)
12534 1.11 christos return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
12535 1.11 christos if (strcmp (section, ".reg-ppc-ppr") == 0)
12536 1.11 christos return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
12537 1.11 christos if (strcmp (section, ".reg-ppc-dscr") == 0)
12538 1.11 christos return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
12539 1.11 christos if (strcmp (section, ".reg-ppc-ebb") == 0)
12540 1.11 christos return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
12541 1.11 christos if (strcmp (section, ".reg-ppc-pmu") == 0)
12542 1.11 christos return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
12543 1.11 christos if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
12544 1.11 christos return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
12545 1.11 christos if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
12546 1.11 christos return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
12547 1.11 christos if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
12548 1.11 christos return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
12549 1.11 christos if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
12550 1.11 christos return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
12551 1.11 christos if (strcmp (section, ".reg-ppc-tm-spr") == 0)
12552 1.11 christos return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
12553 1.11 christos if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
12554 1.1 christos return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
12555 1.1 christos if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
12556 1.1 christos return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
12557 1.1 christos if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
12558 1.1 christos return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
12559 1.1 christos if (strcmp (section, ".reg-s390-high-gprs") == 0)
12560 1.1 christos return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
12561 1.1 christos if (strcmp (section, ".reg-s390-timer") == 0)
12562 1.1 christos return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
12563 1.1 christos if (strcmp (section, ".reg-s390-todcmp") == 0)
12564 1.1 christos return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
12565 1.1 christos if (strcmp (section, ".reg-s390-todpreg") == 0)
12566 1.3 christos return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
12567 1.3 christos if (strcmp (section, ".reg-s390-ctrs") == 0)
12568 1.3 christos return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
12569 1.3 christos if (strcmp (section, ".reg-s390-prefix") == 0)
12570 1.3 christos return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
12571 1.3 christos if (strcmp (section, ".reg-s390-last-break") == 0)
12572 1.6 christos return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
12573 1.6 christos if (strcmp (section, ".reg-s390-system-call") == 0)
12574 1.6 christos return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
12575 1.6 christos if (strcmp (section, ".reg-s390-tdb") == 0)
12576 1.11 christos return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
12577 1.11 christos if (strcmp (section, ".reg-s390-vxrs-low") == 0)
12578 1.11 christos return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
12579 1.11 christos if (strcmp (section, ".reg-s390-vxrs-high") == 0)
12580 1.3 christos return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
12581 1.3 christos if (strcmp (section, ".reg-s390-gs-cb") == 0)
12582 1.3 christos return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
12583 1.3 christos if (strcmp (section, ".reg-s390-gs-bc") == 0)
12584 1.3 christos return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
12585 1.3 christos if (strcmp (section, ".reg-arm-vfp") == 0)
12586 1.3 christos return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
12587 1.3 christos if (strcmp (section, ".reg-aarch-tls") == 0)
12588 1.11 christos return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
12589 1.11 christos if (strcmp (section, ".reg-aarch-hw-break") == 0)
12590 1.11 christos return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
12591 1.11 christos if (strcmp (section, ".reg-aarch-hw-watch") == 0)
12592 1.14 christos return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
12593 1.14 christos if (strcmp (section, ".reg-aarch-sve") == 0)
12594 1.12 christos return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
12595 1.12 christos if (strcmp (section, ".reg-aarch-pauth") == 0)
12596 1.14 christos return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
12597 1.14 christos if (strcmp (section, ".reg-aarch-mte") == 0)
12598 1.14 christos return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
12599 1.14 christos if (strcmp (section, ".reg-arc-v2") == 0)
12600 1.14 christos return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
12601 1.14 christos if (strcmp (section, ".gdb-tdesc") == 0)
12602 1.14 christos return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
12603 1.14 christos if (strcmp (section, ".reg-riscv-csr") == 0)
12604 1.14 christos return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
12605 1.14 christos if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
12606 1.14 christos return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
12607 1.14 christos if (strcmp (section, ".reg-loongarch-lbt") == 0)
12608 1.1 christos return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
12609 1.1 christos if (strcmp (section, ".reg-loongarch-lsx") == 0)
12610 1.1 christos return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
12611 1.14 christos if (strcmp (section, ".reg-loongarch-lasx") == 0)
12612 1.14 christos return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
12613 1.14 christos return NULL;
12614 1.14 christos }
12615 1.14 christos
12616 1.14 christos char *
12617 1.14 christos elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
12618 1.14 christos const void *buf, int bufsiz)
12619 1.14 christos {
12620 1.11 christos return elfcore_write_note (obfd, note_data, note_size,
12621 1.11 christos "CORE", NT_FILE, buf, bufsiz);
12622 1.1 christos }
12623 1.1 christos
12624 1.1 christos static bool
12625 1.11 christos elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
12626 1.11 christos size_t align)
12627 1.11 christos {
12628 1.11 christos char *p;
12629 1.11 christos
12630 1.11 christos /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
12631 1.11 christos gABI specifies that PT_NOTE alignment should be aligned to 4
12632 1.14 christos bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
12633 1.11 christos align is less than 4, we use 4 byte alignment. */
12634 1.1 christos if (align < 4)
12635 1.1 christos align = 4;
12636 1.1 christos if (align != 4 && align != 8)
12637 1.1 christos return false;
12638 1.1 christos
12639 1.1 christos p = buf;
12640 1.1 christos while (p < buf + size)
12641 1.14 christos {
12642 1.1 christos Elf_External_Note *xnp = (Elf_External_Note *) p;
12643 1.1 christos Elf_Internal_Note in;
12644 1.1 christos
12645 1.1 christos if (offsetof (Elf_External_Note, name) > buf - p + size)
12646 1.1 christos return false;
12647 1.1 christos
12648 1.14 christos in.type = H_GET_32 (abfd, xnp->type);
12649 1.1 christos
12650 1.1 christos in.namesz = H_GET_32 (abfd, xnp->namesz);
12651 1.11 christos in.namedata = xnp->name;
12652 1.1 christos if (in.namesz > buf - in.namedata + size)
12653 1.1 christos return false;
12654 1.1 christos
12655 1.1 christos in.descsz = H_GET_32 (abfd, xnp->descsz);
12656 1.14 christos in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
12657 1.1 christos in.descpos = offset + (in.descdata - buf);
12658 1.1 christos if (in.descsz != 0
12659 1.11 christos && (in.descdata >= buf + size
12660 1.1 christos || in.descsz > buf - in.descdata + size))
12661 1.14 christos return false;
12662 1.1 christos
12663 1.1 christos switch (bfd_get_format (abfd))
12664 1.5 christos {
12665 1.5 christos default:
12666 1.5 christos return true;
12667 1.1 christos
12668 1.5 christos case bfd_core:
12669 1.5 christos {
12670 1.14 christos #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
12671 1.1 christos struct
12672 1.5 christos {
12673 1.1 christos const char * string;
12674 1.5 christos size_t len;
12675 1.8 christos bool (*func) (bfd *, Elf_Internal_Note *);
12676 1.5 christos }
12677 1.14 christos grokers[] =
12678 1.5 christos {
12679 1.12 christos GROKER_ELEMENT ("", elfcore_grok_note),
12680 1.14 christos GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
12681 1.14 christos GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
12682 1.5 christos GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
12683 1.5 christos GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
12684 1.5 christos GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
12685 1.5 christos GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
12686 1.5 christos GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
12687 1.5 christos };
12688 1.5 christos #undef GROKER_ELEMENT
12689 1.5 christos int i;
12690 1.5 christos
12691 1.5 christos for (i = ARRAY_SIZE (grokers); i--;)
12692 1.5 christos {
12693 1.14 christos if (in.namesz >= grokers[i].len
12694 1.5 christos && strncmp (in.namedata, grokers[i].string,
12695 1.5 christos grokers[i].len) == 0)
12696 1.5 christos {
12697 1.5 christos if (! grokers[i].func (abfd, & in))
12698 1.5 christos return false;
12699 1.1 christos break;
12700 1.1 christos }
12701 1.1 christos }
12702 1.1 christos break;
12703 1.1 christos }
12704 1.14 christos
12705 1.1 christos case bfd_object:
12706 1.3 christos if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
12707 1.3 christos {
12708 1.3 christos if (! elfobj_grok_gnu_note (abfd, &in))
12709 1.3 christos return false;
12710 1.14 christos }
12711 1.3 christos else if (in.namesz == sizeof "stapsdt"
12712 1.1 christos && strcmp (in.namedata, "stapsdt") == 0)
12713 1.1 christos {
12714 1.1 christos if (! elfobj_grok_stapsdt_note (abfd, &in))
12715 1.11 christos return false;
12716 1.1 christos }
12717 1.1 christos break;
12718 1.14 christos }
12719 1.1 christos
12720 1.1 christos p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
12721 1.14 christos }
12722 1.11 christos
12723 1.11 christos return true;
12724 1.1 christos }
12725 1.1 christos
12726 1.1 christos bool
12727 1.11 christos elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
12728 1.14 christos size_t align)
12729 1.1 christos {
12730 1.1 christos char *buf;
12731 1.14 christos
12732 1.1 christos if (size == 0 || (size + 1) == 0)
12733 1.12 christos return true;
12734 1.1 christos
12735 1.14 christos if (bfd_seek (abfd, offset, SEEK_SET) != 0)
12736 1.1 christos return false;
12737 1.5 christos
12738 1.5 christos buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
12739 1.5 christos if (buf == NULL)
12740 1.5 christos return false;
12741 1.12 christos
12742 1.1 christos /* PR 17512: file: ec08f814
12743 1.1 christos 0-termintate the buffer so that string searches will not overflow. */
12744 1.14 christos buf[size] = 0;
12745 1.1 christos
12746 1.1 christos if (!elf_parse_notes (abfd, buf, size, offset, align))
12747 1.1 christos {
12748 1.14 christos free (buf);
12749 1.1 christos return false;
12750 1.1 christos }
12751 1.1 christos
12752 1.1 christos free (buf);
12753 1.1 christos return true;
12754 1.1 christos }
12755 1.1 christos
12756 1.1 christos /* Providing external access to the ELF program header table. */
12758 1.1 christos
12759 1.1 christos /* Return an upper bound on the number of bytes required to store a
12760 1.1 christos copy of ABFD's program header table entries. Return -1 if an error
12761 1.1 christos occurs; bfd_get_error will return an appropriate code. */
12762 1.1 christos
12763 1.1 christos long
12764 1.1 christos bfd_get_elf_phdr_upper_bound (bfd *abfd)
12765 1.1 christos {
12766 1.1 christos if (abfd->xvec->flavour != bfd_target_elf_flavour)
12767 1.1 christos {
12768 1.1 christos bfd_set_error (bfd_error_wrong_format);
12769 1.1 christos return -1;
12770 1.1 christos }
12771 1.1 christos
12772 1.1 christos return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
12773 1.1 christos }
12774 1.1 christos
12775 1.1 christos /* Copy ABFD's program header table entries to *PHDRS. The entries
12776 1.1 christos will be stored as an array of Elf_Internal_Phdr structures, as
12777 1.1 christos defined in include/elf/internal.h. To find out how large the
12778 1.1 christos buffer needs to be, call bfd_get_elf_phdr_upper_bound.
12779 1.1 christos
12780 1.1 christos Return the number of program header table entries read, or -1 if an
12781 1.1 christos error occurs; bfd_get_error will return an appropriate code. */
12782 1.1 christos
12783 1.1 christos int
12784 1.1 christos bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
12785 1.1 christos {
12786 1.1 christos int num_phdrs;
12787 1.1 christos
12788 1.1 christos if (abfd->xvec->flavour != bfd_target_elf_flavour)
12789 1.11 christos {
12790 1.11 christos bfd_set_error (bfd_error_wrong_format);
12791 1.11 christos return -1;
12792 1.1 christos }
12793 1.1 christos
12794 1.1 christos num_phdrs = elf_elfheader (abfd)->e_phnum;
12795 1.1 christos if (num_phdrs != 0)
12796 1.1 christos memcpy (phdrs, elf_tdata (abfd)->phdr,
12797 1.4 christos num_phdrs * sizeof (Elf_Internal_Phdr));
12798 1.4 christos
12799 1.4 christos return num_phdrs;
12800 1.1 christos }
12801 1.1 christos
12802 1.1 christos enum elf_reloc_type_class
12803 1.1 christos _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12804 1.1 christos const asection *rel_sec ATTRIBUTE_UNUSED,
12805 1.1 christos const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
12806 1.1 christos {
12807 1.1 christos return reloc_class_normal;
12808 1.1 christos }
12809 1.1 christos
12810 1.1 christos /* For RELA architectures, return the relocation value for a
12811 1.1 christos relocation against a local symbol. */
12812 1.1 christos
12813 1.1 christos bfd_vma
12814 1.1 christos _bfd_elf_rela_local_sym (bfd *abfd,
12815 1.1 christos Elf_Internal_Sym *sym,
12816 1.1 christos asection **psec,
12817 1.1 christos Elf_Internal_Rela *rel)
12818 1.1 christos {
12819 1.1 christos asection *sec = *psec;
12820 1.1 christos bfd_vma relocation;
12821 1.3 christos
12822 1.1 christos relocation = (sec->output_section->vma
12823 1.1 christos + sec->output_offset
12824 1.1 christos + sym->st_value);
12825 1.1 christos if ((sec->flags & SEC_MERGE)
12826 1.1 christos && ELF_ST_TYPE (sym->st_info) == STT_SECTION
12827 1.1 christos && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
12828 1.1 christos {
12829 1.1 christos rel->r_addend =
12830 1.1 christos _bfd_merged_section_offset (abfd, psec,
12831 1.1 christos elf_section_data (sec)->sec_info,
12832 1.1 christos sym->st_value + rel->r_addend);
12833 1.1 christos if (sec != *psec)
12834 1.1 christos {
12835 1.1 christos /* If we have changed the section, and our original section is
12836 1.1 christos marked with SEC_EXCLUDE, it means that the original
12837 1.1 christos SEC_MERGE section has been completely subsumed in some
12838 1.1 christos other SEC_MERGE section. In this case, we need to leave
12839 1.1 christos some info around for --emit-relocs. */
12840 1.1 christos if ((sec->flags & SEC_EXCLUDE) != 0)
12841 1.1 christos sec->kept_section = *psec;
12842 1.1 christos sec = *psec;
12843 1.1 christos }
12844 1.1 christos rel->r_addend -= relocation;
12845 1.1 christos rel->r_addend += sec->output_section->vma + sec->output_offset;
12846 1.1 christos }
12847 1.1 christos return relocation;
12848 1.1 christos }
12849 1.1 christos
12850 1.1 christos bfd_vma
12851 1.1 christos _bfd_elf_rel_local_sym (bfd *abfd,
12852 1.3 christos Elf_Internal_Sym *sym,
12853 1.1 christos asection **psec,
12854 1.1 christos bfd_vma addend)
12855 1.1 christos {
12856 1.1 christos asection *sec = *psec;
12857 1.1 christos
12858 1.1 christos if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
12859 1.1 christos return sym->st_value + addend;
12860 1.8 christos
12861 1.8 christos return _bfd_merged_section_offset (abfd, psec,
12862 1.8 christos elf_section_data (sec)->sec_info,
12863 1.8 christos sym->st_value + addend);
12864 1.8 christos }
12865 1.8 christos
12866 1.1 christos /* Adjust an address within a section. Given OFFSET within SEC, return
12867 1.1 christos the new offset within the section, based upon changes made to the
12868 1.1 christos section. Returns -1 if the offset is now invalid.
12869 1.1 christos The offset (in abnd out) is in target sized bytes, however big a
12870 1.1 christos byte may be. */
12871 1.1 christos
12872 1.1 christos bfd_vma
12873 1.1 christos _bfd_elf_section_offset (bfd *abfd,
12874 1.3 christos struct bfd_link_info *info,
12875 1.1 christos asection *sec,
12876 1.1 christos bfd_vma offset)
12877 1.3 christos {
12878 1.1 christos switch (sec->sec_info_type)
12879 1.8 christos {
12880 1.1 christos case SEC_INFO_TYPE_STABS:
12881 1.3 christos return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
12882 1.3 christos offset);
12883 1.8 christos case SEC_INFO_TYPE_EH_FRAME:
12884 1.3 christos return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
12885 1.3 christos
12886 1.8 christos default:
12887 1.8 christos if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
12888 1.8 christos {
12889 1.12 christos /* Reverse the offset. */
12890 1.12 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12891 1.3 christos bfd_size_type address_size = bed->s->arch_size / 8;
12892 1.1 christos
12893 1.1 christos /* address_size and sec->size are in octets. Convert
12894 1.1 christos to bytes before subtracting the original offset. */
12895 1.1 christos offset = ((sec->size - address_size)
12896 1.1 christos / bfd_octets_per_byte (abfd, sec) - offset);
12897 1.1 christos }
12898 1.1 christos return offset;
12899 1.1 christos }
12900 1.1 christos }
12901 1.1 christos
12902 1.1 christos /* Create a new BFD as if by bfd_openr. Rather than opening a file,
12904 1.1 christos reconstruct an ELF file by reading the segments out of remote memory
12905 1.1 christos based on the ELF file header at EHDR_VMA and the ELF program headers it
12906 1.1 christos points to. If not null, *LOADBASEP is filled in with the difference
12907 1.1 christos between the VMAs from which the segments were read, and the VMAs the
12908 1.1 christos file headers (and hence BFD's idea of each section's VMA) put them at.
12909 1.1 christos
12910 1.1 christos The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12911 1.1 christos remote memory at target address VMA into the local buffer at MYADDR; it
12912 1.1 christos should return zero on success or an `errno' code on failure. TEMPL must
12913 1.5 christos be a BFD for an ELF target with the word size and byte order found in
12914 1.1 christos the remote memory. */
12915 1.3 christos
12916 1.1 christos bfd *
12917 1.1 christos bfd_elf_bfd_from_remote_memory
12918 1.5 christos (bfd *templ,
12919 1.1 christos bfd_vma ehdr_vma,
12920 1.1 christos bfd_size_type size,
12921 1.1 christos bfd_vma *loadbasep,
12922 1.1 christos int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
12923 1.1 christos {
12924 1.1 christos return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
12925 1.1 christos (templ, ehdr_vma, size, loadbasep, target_read_memory);
12926 1.1 christos }
12927 1.1 christos
12928 1.1 christos long
12930 1.1 christos _bfd_elf_get_synthetic_symtab (bfd *abfd,
12931 1.1 christos long symcount ATTRIBUTE_UNUSED,
12932 1.1 christos asymbol **syms ATTRIBUTE_UNUSED,
12933 1.14 christos long dynsymcount,
12934 1.1 christos asymbol **dynsyms,
12935 1.1 christos asymbol **ret)
12936 1.1 christos {
12937 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12938 1.1 christos asection *relplt;
12939 1.1 christos asymbol *s;
12940 1.1 christos const char *relplt_name;
12941 1.1 christos bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
12942 1.1 christos arelent *p;
12943 1.1 christos long count, i, n;
12944 1.1 christos size_t size;
12945 1.1 christos Elf_Internal_Shdr *hdr;
12946 1.1 christos char *names;
12947 1.1 christos asection *plt;
12948 1.1 christos
12949 1.1 christos *ret = NULL;
12950 1.1 christos
12951 1.1 christos if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
12952 1.1 christos return 0;
12953 1.1 christos
12954 1.1 christos if (dynsymcount <= 0)
12955 1.1 christos return 0;
12956 1.1 christos
12957 1.1 christos if (!bed->plt_sym_val)
12958 1.1 christos return 0;
12959 1.1 christos
12960 1.1 christos relplt_name = bed->relplt_name;
12961 1.1 christos if (relplt_name == NULL)
12962 1.1 christos relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
12963 1.1 christos relplt = bfd_get_section_by_name (abfd, relplt_name);
12964 1.1 christos if (relplt == NULL)
12965 1.1 christos return 0;
12966 1.1 christos
12967 1.1 christos hdr = &elf_section_data (relplt)->this_hdr;
12968 1.1 christos if (hdr->sh_link != elf_dynsymtab (abfd)
12969 1.14 christos || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
12970 1.1 christos return 0;
12971 1.1 christos
12972 1.1 christos plt = bfd_get_section_by_name (abfd, ".plt");
12973 1.1 christos if (plt == NULL)
12974 1.1 christos return 0;
12975 1.1 christos
12976 1.1 christos slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
12977 1.1 christos if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
12978 1.1 christos return -1;
12979 1.1 christos
12980 1.1 christos count = relplt->size / hdr->sh_entsize;
12981 1.1 christos size = count * sizeof (asymbol);
12982 1.1 christos p = relplt->relocation;
12983 1.1 christos for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
12984 1.1 christos {
12985 1.1 christos size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
12986 1.1 christos if (p->addend != 0)
12987 1.1 christos {
12988 1.1 christos #ifdef BFD64
12989 1.1 christos size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
12990 1.1 christos #else
12991 1.1 christos size += sizeof ("+0x") - 1 + 8;
12992 1.1 christos #endif
12993 1.1 christos }
12994 1.1 christos }
12995 1.1 christos
12996 1.1 christos s = *ret = (asymbol *) bfd_malloc (size);
12997 1.1 christos if (s == NULL)
12998 1.1 christos return -1;
12999 1.1 christos
13000 1.1 christos names = (char *) (s + count);
13001 1.1 christos p = relplt->relocation;
13002 1.1 christos n = 0;
13003 1.1 christos for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13004 1.1 christos {
13005 1.1 christos size_t len;
13006 1.1 christos bfd_vma addr;
13007 1.1 christos
13008 1.1 christos addr = bed->plt_sym_val (i, plt, p);
13009 1.1 christos if (addr == (bfd_vma) -1)
13010 1.1 christos continue;
13011 1.1 christos
13012 1.1 christos *s = **p->sym_ptr_ptr;
13013 1.1 christos /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
13014 1.1 christos we are defining a symbol, ensure one of them is set. */
13015 1.1 christos if ((s->flags & BSF_LOCAL) == 0)
13016 1.1 christos s->flags |= BSF_GLOBAL;
13017 1.1 christos s->flags |= BSF_SYNTHETIC;
13018 1.1 christos s->section = plt;
13019 1.1 christos s->value = addr - plt->vma;
13020 1.3 christos s->name = names;
13021 1.1 christos s->udata.p = NULL;
13022 1.1 christos len = strlen ((*p->sym_ptr_ptr)->name);
13023 1.1 christos memcpy (names, (*p->sym_ptr_ptr)->name, len);
13024 1.1 christos names += len;
13025 1.1 christos if (p->addend != 0)
13026 1.1 christos {
13027 1.1 christos char buf[30], *a;
13028 1.1 christos
13029 1.1 christos memcpy (names, "+0x", sizeof ("+0x") - 1);
13030 1.1 christos names += sizeof ("+0x") - 1;
13031 1.1 christos bfd_sprintf_vma (abfd, buf, p->addend);
13032 1.1 christos for (a = buf; *a == '0'; ++a)
13033 1.1 christos ;
13034 1.1 christos len = strlen (a);
13035 1.1 christos memcpy (names, a, len);
13036 1.1 christos names += len;
13037 1.1 christos }
13038 1.9 christos memcpy (names, "@plt", sizeof ("@plt"));
13039 1.9 christos names += sizeof ("@plt");
13040 1.11 christos ++s, ++n;
13041 1.11 christos }
13042 1.11 christos
13043 1.1 christos return n;
13044 1.11 christos }
13045 1.9 christos
13046 1.1 christos /* It is only used by x86-64 so far.
13047 1.14 christos ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
13048 1.12 christos but current usage would allow all of _bfd_std_section to be zero. */
13049 1.1 christos static const asymbol lcomm_sym
13050 1.12 christos = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
13051 1.1 christos asection _bfd_elf_large_com_section
13052 1.1 christos = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
13053 1.1 christos "LARGE_COMMON", 0, SEC_IS_COMMON);
13054 1.12 christos
13055 1.12 christos bool
13056 1.1 christos _bfd_elf_final_write_processing (bfd *abfd)
13057 1.12 christos {
13058 1.14 christos Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
13059 1.14 christos
13060 1.12 christos i_ehdrp = elf_elfheader (abfd);
13061 1.12 christos
13062 1.12 christos if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13063 1.12 christos i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
13064 1.12 christos
13065 1.12 christos /* Set the osabi field to ELFOSABI_GNU if the binary contains
13066 1.12 christos SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13067 1.12 christos or STB_GNU_UNIQUE binding. */
13068 1.14 christos if (elf_tdata (abfd)->has_gnu_osabi != 0)
13069 1.14 christos {
13070 1.12 christos if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13071 1.14 christos i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
13072 1.14 christos else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
13073 1.12 christos && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
13074 1.14 christos {
13075 1.14 christos if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
13076 1.14 christos _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13077 1.14 christos "and FreeBSD targets"));
13078 1.14 christos if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
13079 1.12 christos _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13080 1.14 christos "only by GNU and FreeBSD targets"));
13081 1.12 christos if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
13082 1.12 christos _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13083 1.14 christos "only by GNU and FreeBSD targets"));
13084 1.1 christos if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
13085 1.1 christos _bfd_error_handler (_("GNU_RETAIN section is supported "
13086 1.1 christos "only by GNU and FreeBSD targets"));
13087 1.1 christos bfd_set_error (bfd_error_sorry);
13088 1.1 christos return false;
13089 1.1 christos }
13090 1.1 christos }
13091 1.14 christos return true;
13092 1.1 christos }
13093 1.1 christos
13094 1.1 christos
13095 1.1 christos /* Return TRUE for ELF symbol types that represent functions.
13096 1.1 christos This is the default version of this function, which is sufficient for
13097 1.3 christos most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
13098 1.3 christos
13099 1.3 christos bool
13100 1.3 christos _bfd_elf_is_function_type (unsigned int type)
13101 1.3 christos {
13102 1.3 christos return (type == STT_FUNC
13103 1.3 christos || type == STT_GNU_IFUNC);
13104 1.3 christos }
13105 1.3 christos
13106 1.3 christos /* If the ELF symbol SYM might be a function in SEC, return the
13107 1.14 christos function size and set *CODE_OFF to the function's entry point,
13108 1.3 christos otherwise return zero. */
13109 1.3 christos
13110 1.3 christos bfd_size_type
13111 1.3 christos _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13112 1.3 christos bfd_vma *code_off)
13113 1.3 christos {
13114 1.14 christos bfd_size_type size;
13115 1.14 christos elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
13116 1.14 christos
13117 1.14 christos if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
13118 1.14 christos | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13119 1.14 christos || sym->section != sec)
13120 1.14 christos return 0;
13121 1.14 christos
13122 1.14 christos size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
13123 1.14 christos
13124 1.14 christos /* In theory we should check that the symbol's type satisfies
13125 1.14 christos _bfd_elf_is_function_type(), but there are some function-like
13126 1.14 christos symbols which would fail this test. (eg _start). Instead
13127 1.14 christos we check for hidden, local, notype symbols with zero size.
13128 1.3 christos This type of symbol is generated by the annobin plugin for gcc
13129 1.14 christos and clang, and should not be considered to be a function symbol. */
13130 1.14 christos if (size == 0
13131 1.3 christos && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13132 1.12 christos && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13133 1.12 christos && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13134 1.12 christos return 0;
13135 1.12 christos
13136 1.12 christos *code_off = sym->value;
13137 1.12 christos /* Do not return 0 for the function's size. */
13138 1.12 christos return size ? size : 1;
13139 1.12 christos }
13140 1.12 christos
13141 1.12 christos /* Set to non-zero to enable some debug messages. */
13142 1.14 christos #define DEBUG_SECONDARY_RELOCS 0
13143 1.12 christos
13144 1.12 christos /* An internal-to-the-bfd-library only section type
13145 1.12 christos used to indicate a cached secondary reloc section. */
13146 1.12 christos #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
13147 1.12 christos
13148 1.12 christos /* Create a BFD section to hold a secondary reloc section. */
13149 1.12 christos
13150 1.14 christos bool
13151 1.12 christos _bfd_elf_init_secondary_reloc_section (bfd * abfd,
13152 1.12 christos Elf_Internal_Shdr *hdr,
13153 1.12 christos const char * name,
13154 1.12 christos unsigned int shindex)
13155 1.12 christos {
13156 1.12 christos /* We only support RELA secondary relocs. */
13157 1.12 christos if (hdr->sh_type != SHT_RELA)
13158 1.12 christos return false;
13159 1.12 christos
13160 1.12 christos #if DEBUG_SECONDARY_RELOCS
13161 1.14 christos fprintf (stderr, "secondary reloc section %s encountered\n", name);
13162 1.14 christos #endif
13163 1.14 christos hdr->sh_type = SHT_SECONDARY_RELOC;
13164 1.14 christos return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13165 1.14 christos }
13166 1.12 christos
13167 1.12 christos /* Read in any secondary relocs associated with SEC. */
13168 1.12 christos
13169 1.14 christos bool
13170 1.12 christos _bfd_elf_slurp_secondary_reloc_section (bfd * abfd,
13171 1.14 christos asection * sec,
13172 1.12 christos asymbol ** symbols,
13173 1.12 christos bool dynamic)
13174 1.12 christos {
13175 1.12 christos const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13176 1.12 christos asection * relsec;
13177 1.12 christos bool result = true;
13178 1.12 christos bfd_vma (*r_sym) (bfd_vma);
13179 1.14 christos ufile_ptr filesize;
13180 1.14 christos
13181 1.14 christos #if BFD_DEFAULT_TARGET_SIZE > 32
13182 1.14 christos if (bfd_arch_bits_per_address (abfd) != 32)
13183 1.12 christos r_sym = elf64_r_sym;
13184 1.12 christos else
13185 1.14 christos #endif
13186 1.12 christos r_sym = elf32_r_sym;
13187 1.12 christos
13188 1.12 christos if (!elf_section_data (sec)->has_secondary_relocs)
13189 1.12 christos return true;
13190 1.12 christos
13191 1.12 christos /* Discover if there are any secondary reloc sections
13192 1.12 christos associated with SEC. */
13193 1.12 christos filesize = bfd_get_file_size (abfd);
13194 1.12 christos for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13195 1.12 christos {
13196 1.12 christos Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13197 1.12 christos
13198 1.12 christos if (hdr->sh_type == SHT_SECONDARY_RELOC
13199 1.14 christos && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13200 1.12 christos && (hdr->sh_entsize == ebd->s->sizeof_rel
13201 1.12 christos || hdr->sh_entsize == ebd->s->sizeof_rela))
13202 1.14 christos {
13203 1.12 christos bfd_byte * native_relocs;
13204 1.12 christos bfd_byte * native_reloc;
13205 1.12 christos arelent * internal_relocs;
13206 1.14 christos arelent * internal_reloc;
13207 1.12 christos size_t i;
13208 1.12 christos unsigned int entsize;
13209 1.12 christos unsigned int symcount;
13210 1.12 christos bfd_size_type reloc_count;
13211 1.12 christos size_t amt;
13212 1.12 christos
13213 1.12 christos if (ebd->elf_info_to_howto == NULL)
13214 1.14 christos return false;
13215 1.14 christos
13216 1.14 christos #if DEBUG_SECONDARY_RELOCS
13217 1.14 christos fprintf (stderr, "read secondary relocs for %s from %s\n",
13218 1.14 christos sec->name, relsec->name);
13219 1.14 christos #endif
13220 1.14 christos entsize = hdr->sh_entsize;
13221 1.14 christos
13222 1.14 christos if (filesize != 0
13223 1.12 christos && ((ufile_ptr) hdr->sh_offset > filesize
13224 1.12 christos || hdr->sh_size > filesize - hdr->sh_offset))
13225 1.12 christos {
13226 1.14 christos bfd_set_error (bfd_error_file_truncated);
13227 1.12 christos result = false;
13228 1.12 christos continue;
13229 1.12 christos }
13230 1.12 christos
13231 1.12 christos native_relocs = bfd_malloc (hdr->sh_size);
13232 1.12 christos if (native_relocs == NULL)
13233 1.12 christos {
13234 1.12 christos result = false;
13235 1.14 christos continue;
13236 1.12 christos }
13237 1.12 christos
13238 1.12 christos reloc_count = NUM_SHDR_ENTRIES (hdr);
13239 1.12 christos if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
13240 1.12 christos {
13241 1.12 christos free (native_relocs);
13242 1.12 christos bfd_set_error (bfd_error_file_too_big);
13243 1.14 christos result = false;
13244 1.12 christos continue;
13245 1.12 christos }
13246 1.12 christos
13247 1.12 christos internal_relocs = (arelent *) bfd_alloc (abfd, amt);
13248 1.12 christos if (internal_relocs == NULL)
13249 1.12 christos {
13250 1.12 christos free (native_relocs);
13251 1.12 christos result = false;
13252 1.12 christos continue;
13253 1.12 christos }
13254 1.14 christos
13255 1.12 christos if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13256 1.12 christos || (bfd_bread (native_relocs, hdr->sh_size, abfd)
13257 1.12 christos != hdr->sh_size))
13258 1.14 christos {
13259 1.14 christos free (native_relocs);
13260 1.14 christos /* The internal_relocs will be freed when
13261 1.14 christos the memory for the bfd is released. */
13262 1.12 christos result = false;
13263 1.12 christos continue;
13264 1.12 christos }
13265 1.12 christos
13266 1.12 christos if (dynamic)
13267 1.12 christos symcount = bfd_get_dynamic_symcount (abfd);
13268 1.14 christos else
13269 1.12 christos symcount = bfd_get_symcount (abfd);
13270 1.12 christos
13271 1.14 christos for (i = 0, internal_reloc = internal_relocs,
13272 1.14 christos native_reloc = native_relocs;
13273 1.14 christos i < reloc_count;
13274 1.14 christos i++, internal_reloc++, native_reloc += entsize)
13275 1.12 christos {
13276 1.12 christos bool res;
13277 1.12 christos Elf_Internal_Rela rela;
13278 1.12 christos
13279 1.12 christos if (entsize == ebd->s->sizeof_rel)
13280 1.12 christos ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
13281 1.12 christos else /* entsize == ebd->s->sizeof_rela */
13282 1.12 christos ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
13283 1.12 christos
13284 1.12 christos /* The address of an ELF reloc is section relative for an object
13285 1.12 christos file, and absolute for an executable file or shared library.
13286 1.12 christos The address of a normal BFD reloc is always section relative,
13287 1.12 christos and the address of a dynamic reloc is absolute.. */
13288 1.12 christos if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
13289 1.12 christos internal_reloc->address = rela.r_offset;
13290 1.12 christos else
13291 1.12 christos internal_reloc->address = rela.r_offset - sec->vma;
13292 1.12 christos
13293 1.12 christos if (r_sym (rela.r_info) == STN_UNDEF)
13294 1.12 christos {
13295 1.12 christos /* FIXME: This and the error case below mean that we
13296 1.14 christos have a symbol on relocs that is not elf_symbol_type. */
13297 1.12 christos internal_reloc->sym_ptr_ptr =
13298 1.12 christos bfd_abs_section_ptr->symbol_ptr_ptr;
13299 1.12 christos }
13300 1.12 christos else if (r_sym (rela.r_info) > symcount)
13301 1.14 christos {
13302 1.12 christos _bfd_error_handler
13303 1.12 christos /* xgettext:c-format */
13304 1.12 christos (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
13305 1.12 christos abfd, sec, i, (long) r_sym (rela.r_info));
13306 1.12 christos bfd_set_error (bfd_error_bad_value);
13307 1.12 christos internal_reloc->sym_ptr_ptr =
13308 1.12 christos bfd_abs_section_ptr->symbol_ptr_ptr;
13309 1.12 christos result = false;
13310 1.12 christos }
13311 1.12 christos else
13312 1.12 christos {
13313 1.12 christos asymbol **ps;
13314 1.12 christos
13315 1.12 christos ps = symbols + r_sym (rela.r_info) - 1;
13316 1.12 christos internal_reloc->sym_ptr_ptr = ps;
13317 1.12 christos /* Make sure that this symbol is not removed by strip. */
13318 1.12 christos (*ps)->flags |= BSF_KEEP;
13319 1.14 christos }
13320 1.14 christos
13321 1.12 christos internal_reloc->addend = rela.r_addend;
13322 1.12 christos
13323 1.14 christos res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
13324 1.12 christos if (! res || internal_reloc->howto == NULL)
13325 1.12 christos {
13326 1.12 christos #if DEBUG_SECONDARY_RELOCS
13327 1.12 christos fprintf (stderr,
13328 1.12 christos "there is no howto associated with reloc %lx\n",
13329 1.12 christos rela.r_info);
13330 1.12 christos #endif
13331 1.12 christos result = false;
13332 1.12 christos }
13333 1.12 christos }
13334 1.12 christos
13335 1.12 christos free (native_relocs);
13336 1.12 christos /* Store the internal relocs. */
13337 1.12 christos elf_section_data (relsec)->sec_info = internal_relocs;
13338 1.14 christos }
13339 1.14 christos }
13340 1.14 christos
13341 1.14 christos return result;
13342 1.14 christos }
13343 1.12 christos
13344 1.12 christos /* Set the ELF section header fields of an output secondary reloc section. */
13345 1.12 christos
13346 1.12 christos bool
13347 1.12 christos _bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
13348 1.12 christos bfd *obfd ATTRIBUTE_UNUSED,
13349 1.14 christos const Elf_Internal_Shdr *isection,
13350 1.12 christos Elf_Internal_Shdr *osection)
13351 1.12 christos {
13352 1.14 christos asection * isec;
13353 1.12 christos asection * osec;
13354 1.12 christos struct bfd_elf_section_data * esd;
13355 1.12 christos
13356 1.14 christos if (isection == NULL)
13357 1.12 christos return false;
13358 1.12 christos
13359 1.12 christos if (isection->sh_type != SHT_SECONDARY_RELOC)
13360 1.14 christos return true;
13361 1.12 christos
13362 1.12 christos isec = isection->bfd_section;
13363 1.12 christos if (isec == NULL)
13364 1.12 christos return false;
13365 1.12 christos
13366 1.12 christos osec = osection->bfd_section;
13367 1.12 christos if (osec == NULL)
13368 1.12 christos return false;
13369 1.12 christos
13370 1.12 christos esd = elf_section_data (osec);
13371 1.12 christos BFD_ASSERT (esd->sec_info == NULL);
13372 1.14 christos esd->sec_info = elf_section_data (isec)->sec_info;
13373 1.14 christos osection->sh_type = SHT_RELA;
13374 1.12 christos osection->sh_link = elf_onesymtab (obfd);
13375 1.12 christos if (osection->sh_link == 0)
13376 1.14 christos {
13377 1.12 christos /* There is no symbol table - we are hosed... */
13378 1.12 christos _bfd_error_handler
13379 1.14 christos /* xgettext:c-format */
13380 1.14 christos (_("%pB(%pA): link section cannot be set"
13381 1.12 christos " because the output file does not have a symbol table"),
13382 1.12 christos obfd, osec);
13383 1.12 christos bfd_set_error (bfd_error_bad_value);
13384 1.12 christos return false;
13385 1.12 christos }
13386 1.12 christos
13387 1.12 christos /* Find the output section that corresponds to the isection's
13388 1.12 christos sh_info link. */
13389 1.14 christos if (isection->sh_info == 0
13390 1.12 christos || isection->sh_info >= elf_numsections (ibfd))
13391 1.12 christos {
13392 1.12 christos _bfd_error_handler
13393 1.12 christos /* xgettext:c-format */
13394 1.12 christos (_("%pB(%pA): info section index is invalid"),
13395 1.12 christos obfd, osec);
13396 1.12 christos bfd_set_error (bfd_error_bad_value);
13397 1.12 christos return false;
13398 1.12 christos }
13399 1.12 christos
13400 1.14 christos isection = elf_elfsections (ibfd)[isection->sh_info];
13401 1.14 christos
13402 1.12 christos if (isection == NULL
13403 1.12 christos || isection->bfd_section == NULL
13404 1.14 christos || isection->bfd_section->output_section == NULL)
13405 1.12 christos {
13406 1.12 christos _bfd_error_handler
13407 1.12 christos /* xgettext:c-format */
13408 1.12 christos (_("%pB(%pA): info section index cannot be set"
13409 1.12 christos " because the section is not in the output"),
13410 1.14 christos obfd, osec);
13411 1.12 christos bfd_set_error (bfd_error_bad_value);
13412 1.12 christos return false;
13413 1.12 christos }
13414 1.12 christos
13415 1.12 christos esd = elf_section_data (isection->bfd_section->output_section);
13416 1.12 christos BFD_ASSERT (esd != NULL);
13417 1.12 christos osection->sh_info = esd->this_idx;
13418 1.14 christos esd->has_secondary_relocs = true;
13419 1.12 christos #if DEBUG_SECONDARY_RELOCS
13420 1.12 christos fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
13421 1.12 christos osec->name, osection->sh_link, osection->sh_info);
13422 1.12 christos fprintf (stderr, "mark section %s as having secondary relocs\n",
13423 1.12 christos bfd_section_name (isection->bfd_section->output_section));
13424 1.12 christos #endif
13425 1.12 christos
13426 1.12 christos return true;
13427 1.12 christos }
13428 1.14 christos
13429 1.12 christos /* Write out a secondary reloc section.
13430 1.12 christos
13431 1.12 christos FIXME: Currently this function can result in a serious performance penalty
13432 1.12 christos for files with secondary relocs and lots of sections. The proper way to
13433 1.12 christos fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
13434 1.12 christos relocs together and then to have this function just walk that chain. */
13435 1.14 christos
13436 1.12 christos bool
13437 1.12 christos _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
13438 1.14 christos {
13439 1.12 christos const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13440 1.12 christos bfd_vma addr_offset;
13441 1.12 christos asection * relsec;
13442 1.12 christos bfd_vma (*r_info) (bfd_vma, bfd_vma);
13443 1.12 christos bool result = true;
13444 1.12 christos
13445 1.12 christos if (sec == NULL)
13446 1.12 christos return false;
13447 1.12 christos
13448 1.12 christos #if BFD_DEFAULT_TARGET_SIZE > 32
13449 1.12 christos if (bfd_arch_bits_per_address (abfd) != 32)
13450 1.12 christos r_info = elf64_r_info;
13451 1.12 christos else
13452 1.12 christos #endif
13453 1.12 christos r_info = elf32_r_info;
13454 1.12 christos
13455 1.12 christos /* The address of an ELF reloc is section relative for an object
13456 1.12 christos file, and absolute for an executable file or shared library.
13457 1.12 christos The address of a BFD reloc is always section relative. */
13458 1.12 christos addr_offset = 0;
13459 1.12 christos if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
13460 1.12 christos addr_offset = sec->vma;
13461 1.12 christos
13462 1.12 christos /* Discover if there are any secondary reloc sections
13463 1.12 christos associated with SEC. */
13464 1.12 christos for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13465 1.12 christos {
13466 1.14 christos const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
13467 1.14 christos Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
13468 1.14 christos
13469 1.12 christos if (hdr->sh_type == SHT_RELA
13470 1.12 christos && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
13471 1.12 christos {
13472 1.12 christos asymbol * last_sym;
13473 1.12 christos int last_sym_idx;
13474 1.12 christos size_t reloc_count;
13475 1.12 christos size_t idx;
13476 1.12 christos bfd_size_type entsize;
13477 1.12 christos arelent * src_irel;
13478 1.12 christos bfd_byte * dst_rela;
13479 1.14 christos
13480 1.12 christos if (hdr->contents != NULL)
13481 1.12 christos {
13482 1.12 christos _bfd_error_handler
13483 1.14 christos /* xgettext:c-format */
13484 1.14 christos (_("%pB(%pA): error: secondary reloc section processed twice"),
13485 1.12 christos abfd, relsec);
13486 1.12 christos bfd_set_error (bfd_error_bad_value);
13487 1.12 christos result = false;
13488 1.14 christos continue;
13489 1.14 christos }
13490 1.12 christos
13491 1.12 christos entsize = hdr->sh_entsize;
13492 1.14 christos if (entsize == 0)
13493 1.14 christos {
13494 1.14 christos _bfd_error_handler
13495 1.14 christos /* xgettext:c-format */
13496 1.14 christos (_("%pB(%pA): error: secondary reloc section"
13497 1.14 christos " has zero sized entries"),
13498 1.14 christos abfd, relsec);
13499 1.14 christos bfd_set_error (bfd_error_bad_value);
13500 1.14 christos result = false;
13501 1.14 christos continue;
13502 1.14 christos }
13503 1.14 christos else if (entsize != ebd->s->sizeof_rel
13504 1.14 christos && entsize != ebd->s->sizeof_rela)
13505 1.12 christos {
13506 1.12 christos _bfd_error_handler
13507 1.12 christos /* xgettext:c-format */
13508 1.14 christos (_("%pB(%pA): error: secondary reloc section"
13509 1.14 christos " has non-standard sized entries"),
13510 1.14 christos abfd, relsec);
13511 1.12 christos bfd_set_error (bfd_error_bad_value);
13512 1.12 christos result = false;
13513 1.12 christos continue;
13514 1.12 christos }
13515 1.12 christos
13516 1.12 christos reloc_count = hdr->sh_size / entsize;
13517 1.14 christos hdr->sh_size = entsize * reloc_count;
13518 1.12 christos if (reloc_count == 0)
13519 1.12 christos {
13520 1.12 christos _bfd_error_handler
13521 1.12 christos /* xgettext:c-format */
13522 1.12 christos (_("%pB(%pA): error: secondary reloc section is empty!"),
13523 1.12 christos abfd, relsec);
13524 1.12 christos bfd_set_error (bfd_error_bad_value);
13525 1.12 christos result = false;
13526 1.12 christos continue;
13527 1.12 christos }
13528 1.12 christos
13529 1.12 christos hdr->contents = bfd_alloc (abfd, hdr->sh_size);
13530 1.12 christos if (hdr->contents == NULL)
13531 1.12 christos continue;
13532 1.12 christos
13533 1.12 christos #if DEBUG_SECONDARY_RELOCS
13534 1.12 christos fprintf (stderr, "write %u secondary relocs for %s from %s\n",
13535 1.12 christos reloc_count, sec->name, relsec->name);
13536 1.12 christos #endif
13537 1.14 christos last_sym = NULL;
13538 1.14 christos last_sym_idx = 0;
13539 1.12 christos dst_rela = hdr->contents;
13540 1.12 christos src_irel = (arelent *) esd->sec_info;
13541 1.14 christos if (src_irel == NULL)
13542 1.12 christos {
13543 1.12 christos _bfd_error_handler
13544 1.12 christos /* xgettext:c-format */
13545 1.14 christos (_("%pB(%pA): error: internal relocs missing"
13546 1.12 christos " for secondary reloc section"),
13547 1.12 christos abfd, relsec);
13548 1.12 christos bfd_set_error (bfd_error_bad_value);
13549 1.12 christos result = false;
13550 1.12 christos continue;
13551 1.12 christos }
13552 1.12 christos
13553 1.12 christos for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
13554 1.12 christos {
13555 1.12 christos Elf_Internal_Rela src_rela;
13556 1.12 christos arelent *ptr;
13557 1.14 christos asymbol *sym;
13558 1.12 christos int n;
13559 1.12 christos
13560 1.14 christos ptr = src_irel + idx;
13561 1.12 christos if (ptr == NULL)
13562 1.12 christos {
13563 1.12 christos _bfd_error_handler
13564 1.12 christos /* xgettext:c-format */
13565 1.12 christos (_("%pB(%pA): error: reloc table entry %zu is empty"),
13566 1.12 christos abfd, relsec, idx);
13567 1.12 christos bfd_set_error (bfd_error_bad_value);
13568 1.12 christos result = false;
13569 1.12 christos break;
13570 1.12 christos }
13571 1.12 christos
13572 1.12 christos if (ptr->sym_ptr_ptr == NULL)
13573 1.12 christos {
13574 1.12 christos /* FIXME: Is this an error ? */
13575 1.12 christos n = 0;
13576 1.12 christos }
13577 1.12 christos else
13578 1.12 christos {
13579 1.12 christos sym = *ptr->sym_ptr_ptr;
13580 1.12 christos
13581 1.12 christos if (sym == last_sym)
13582 1.14 christos n = last_sym_idx;
13583 1.14 christos else
13584 1.12 christos {
13585 1.12 christos n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
13586 1.14 christos if (n < 0)
13587 1.12 christos {
13588 1.12 christos _bfd_error_handler
13589 1.12 christos /* xgettext:c-format */
13590 1.12 christos (_("%pB(%pA): error: secondary reloc %zu"
13591 1.12 christos " references a missing symbol"),
13592 1.12 christos abfd, relsec, idx);
13593 1.12 christos bfd_set_error (bfd_error_bad_value);
13594 1.12 christos result = false;
13595 1.12 christos n = 0;
13596 1.12 christos }
13597 1.12 christos
13598 1.12 christos last_sym = sym;
13599 1.12 christos last_sym_idx = n;
13600 1.14 christos }
13601 1.14 christos
13602 1.12 christos if (sym->the_bfd != NULL
13603 1.12 christos && sym->the_bfd->xvec != abfd->xvec
13604 1.14 christos && ! _bfd_elf_validate_reloc (abfd, ptr))
13605 1.12 christos {
13606 1.12 christos _bfd_error_handler
13607 1.12 christos /* xgettext:c-format */
13608 1.12 christos (_("%pB(%pA): error: secondary reloc %zu"
13609 1.12 christos " references a deleted symbol"),
13610 1.12 christos abfd, relsec, idx);
13611 1.12 christos bfd_set_error (bfd_error_bad_value);
13612 1.12 christos result = false;
13613 1.12 christos n = 0;
13614 1.14 christos }
13615 1.14 christos }
13616 1.12 christos
13617 1.12 christos src_rela.r_offset = ptr->address + addr_offset;
13618 1.14 christos if (ptr->howto == NULL)
13619 1.12 christos {
13620 1.12 christos _bfd_error_handler
13621 1.12 christos /* xgettext:c-format */
13622 1.12 christos (_("%pB(%pA): error: secondary reloc %zu"
13623 1.12 christos " is of an unknown type"),
13624 1.14 christos abfd, relsec, idx);
13625 1.14 christos bfd_set_error (bfd_error_bad_value);
13626 1.14 christos result = false;
13627 1.14 christos src_rela.r_info = r_info (0, 0);
13628 1.14 christos }
13629 1.12 christos else
13630 1.12 christos src_rela.r_info = r_info (n, ptr->howto->type);
13631 1.12 christos src_rela.r_addend = ptr->addend;
13632 1.12 christos
13633 1.12 christos if (entsize == ebd->s->sizeof_rel)
13634 1.12 christos ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
13635 else /* entsize == ebd->s->sizeof_rela */
13636 ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
13637 }
13638 }
13639 }
13640
13641 return result;
13642 }
13643