]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf.c
readelf PT_PHDR check
[thirdparty/binutils-gdb.git] / bfd / elf.c
CommitLineData
252b5132 1/* ELF executable support for BFD.
340b6d91 2
82704155 3 Copyright (C) 1993-2019 Free Software Foundation, Inc.
252b5132 4
5e8d7549 5 This file is part of BFD, the Binary File Descriptor library.
252b5132 6
5e8d7549
NC
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
cd123cb7 9 the Free Software Foundation; either version 3 of the License, or
5e8d7549 10 (at your option) any later version.
252b5132 11
5e8d7549
NC
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
252b5132 16
5e8d7549 17 You should have received a copy of the GNU General Public License
b34976b6 18 along with this program; if not, write to the Free Software
cd123cb7
NC
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
252b5132 22
1b74d094
BW
23/*
24SECTION
252b5132
RH
25 ELF backends
26
27 BFD support for ELF formats is being worked on.
28 Currently, the best supported back ends are for sparc and i386
29 (running svr4 or Solaris 2).
30
31 Documentation of the internals of the support code still needs
32 to be written. The code is changing quickly enough that we
661a3fd4 33 haven't bothered yet. */
252b5132 34
7ee38065
MS
35/* For sparc64-cross-sparc32. */
36#define _SYSCALL32
252b5132 37#include "sysdep.h"
3a551c7a 38#include <limits.h>
3db64b00 39#include "bfd.h"
252b5132
RH
40#include "bfdlink.h"
41#include "libbfd.h"
42#define ARCH_SIZE 0
43#include "elf-bfd.h"
e0e8c97f 44#include "libiberty.h"
ff59fc36 45#include "safe-ctype.h"
de64ce13 46#include "elf-linux-core.h"
252b5132 47
8bc7f138
L
48#ifdef CORE_HEADER
49#include CORE_HEADER
50#endif
51
217aa764 52static int elf_sort_sections (const void *, const void *);
c84fca4d 53static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
217aa764 54static bfd_boolean prep_headers (bfd *);
ef10c3ac 55static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ;
276da9b3
L
56static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type,
57 size_t align) ;
718175fa 58static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
276da9b3 59 file_ptr offset, size_t align);
50b2bdb7 60
252b5132
RH
61/* Swap version information in and out. The version information is
62 currently size independent. If that ever changes, this code will
63 need to move into elfcode.h. */
64
65/* Swap in a Verdef structure. */
66
67void
217aa764
AM
68_bfd_elf_swap_verdef_in (bfd *abfd,
69 const Elf_External_Verdef *src,
70 Elf_Internal_Verdef *dst)
252b5132 71{
dc810e39
AM
72 dst->vd_version = H_GET_16 (abfd, src->vd_version);
73 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
74 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
75 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
76 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
77 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
78 dst->vd_next = H_GET_32 (abfd, src->vd_next);
252b5132
RH
79}
80
81/* Swap out a Verdef structure. */
82
83void
217aa764
AM
84_bfd_elf_swap_verdef_out (bfd *abfd,
85 const Elf_Internal_Verdef *src,
86 Elf_External_Verdef *dst)
252b5132 87{
dc810e39
AM
88 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
89 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
90 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
91 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
92 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
93 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
94 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
252b5132
RH
95}
96
97/* Swap in a Verdaux structure. */
98
99void
217aa764
AM
100_bfd_elf_swap_verdaux_in (bfd *abfd,
101 const Elf_External_Verdaux *src,
102 Elf_Internal_Verdaux *dst)
252b5132 103{
dc810e39
AM
104 dst->vda_name = H_GET_32 (abfd, src->vda_name);
105 dst->vda_next = H_GET_32 (abfd, src->vda_next);
252b5132
RH
106}
107
108/* Swap out a Verdaux structure. */
109
110void
217aa764
AM
111_bfd_elf_swap_verdaux_out (bfd *abfd,
112 const Elf_Internal_Verdaux *src,
113 Elf_External_Verdaux *dst)
252b5132 114{
dc810e39
AM
115 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
116 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
252b5132
RH
117}
118
119/* Swap in a Verneed structure. */
120
121void
217aa764
AM
122_bfd_elf_swap_verneed_in (bfd *abfd,
123 const Elf_External_Verneed *src,
124 Elf_Internal_Verneed *dst)
252b5132 125{
dc810e39
AM
126 dst->vn_version = H_GET_16 (abfd, src->vn_version);
127 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
128 dst->vn_file = H_GET_32 (abfd, src->vn_file);
129 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
130 dst->vn_next = H_GET_32 (abfd, src->vn_next);
252b5132
RH
131}
132
133/* Swap out a Verneed structure. */
134
135void
217aa764
AM
136_bfd_elf_swap_verneed_out (bfd *abfd,
137 const Elf_Internal_Verneed *src,
138 Elf_External_Verneed *dst)
252b5132 139{
dc810e39
AM
140 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
141 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
142 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
143 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
144 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
252b5132
RH
145}
146
147/* Swap in a Vernaux structure. */
148
149void
217aa764
AM
150_bfd_elf_swap_vernaux_in (bfd *abfd,
151 const Elf_External_Vernaux *src,
152 Elf_Internal_Vernaux *dst)
252b5132 153{
dc810e39
AM
154 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
155 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
156 dst->vna_other = H_GET_16 (abfd, src->vna_other);
157 dst->vna_name = H_GET_32 (abfd, src->vna_name);
158 dst->vna_next = H_GET_32 (abfd, src->vna_next);
252b5132
RH
159}
160
161/* Swap out a Vernaux structure. */
162
163void
217aa764
AM
164_bfd_elf_swap_vernaux_out (bfd *abfd,
165 const Elf_Internal_Vernaux *src,
166 Elf_External_Vernaux *dst)
252b5132 167{
dc810e39
AM
168 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
169 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
170 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
171 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
172 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
252b5132
RH
173}
174
175/* Swap in a Versym structure. */
176
177void
217aa764
AM
178_bfd_elf_swap_versym_in (bfd *abfd,
179 const Elf_External_Versym *src,
180 Elf_Internal_Versym *dst)
252b5132 181{
dc810e39 182 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
252b5132
RH
183}
184
185/* Swap out a Versym structure. */
186
187void
217aa764
AM
188_bfd_elf_swap_versym_out (bfd *abfd,
189 const Elf_Internal_Versym *src,
190 Elf_External_Versym *dst)
252b5132 191{
dc810e39 192 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
252b5132
RH
193}
194
195/* Standard ELF hash function. Do not change this function; you will
196 cause invalid hash tables to be generated. */
3a99b017 197
252b5132 198unsigned long
217aa764 199bfd_elf_hash (const char *namearg)
252b5132 200{
3a99b017 201 const unsigned char *name = (const unsigned char *) namearg;
252b5132
RH
202 unsigned long h = 0;
203 unsigned long g;
204 int ch;
205
206 while ((ch = *name++) != '\0')
207 {
208 h = (h << 4) + ch;
209 if ((g = (h & 0xf0000000)) != 0)
210 {
211 h ^= g >> 24;
212 /* The ELF ABI says `h &= ~g', but this is equivalent in
213 this case and on some machines one insn instead of two. */
214 h ^= g;
215 }
216 }
32dfa85d 217 return h & 0xffffffff;
252b5132
RH
218}
219
fdc90cb4
JJ
220/* DT_GNU_HASH hash function. Do not change this function; you will
221 cause invalid hash tables to be generated. */
222
223unsigned long
224bfd_elf_gnu_hash (const char *namearg)
225{
226 const unsigned char *name = (const unsigned char *) namearg;
227 unsigned long h = 5381;
228 unsigned char ch;
229
230 while ((ch = *name++) != '\0')
231 h = (h << 5) + h + ch;
232 return h & 0xffffffff;
233}
234
0c8d6e5c
AM
235/* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
236 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
b34976b6 237bfd_boolean
0c8d6e5c 238bfd_elf_allocate_object (bfd *abfd,
0ffa91dd 239 size_t object_size,
4dfe6ac6 240 enum elf_target_id object_id)
252b5132 241{
0ffa91dd
NC
242 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
243 abfd->tdata.any = bfd_zalloc (abfd, object_size);
244 if (abfd->tdata.any == NULL)
245 return FALSE;
252b5132 246
0ffa91dd 247 elf_object_id (abfd) = object_id;
c0355132
AM
248 if (abfd->direction != read_direction)
249 {
250 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
251 if (o == NULL)
252 return FALSE;
253 elf_tdata (abfd)->o = o;
254 elf_program_header_size (abfd) = (bfd_size_type) -1;
255 }
b34976b6 256 return TRUE;
252b5132
RH
257}
258
0ffa91dd
NC
259
260bfd_boolean
ae95ffa6 261bfd_elf_make_object (bfd *abfd)
0ffa91dd 262{
ae95ffa6 263 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0ffa91dd 264 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
ae95ffa6 265 bed->target_id);
0ffa91dd
NC
266}
267
b34976b6 268bfd_boolean
217aa764 269bfd_elf_mkcorefile (bfd *abfd)
252b5132 270{
c044fabd 271 /* I think this can be done just like an object file. */
228e534f
AM
272 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
273 return FALSE;
274 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
275 return elf_tdata (abfd)->core != NULL;
252b5132
RH
276}
277
6d5944fc 278char *
217aa764 279bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
252b5132
RH
280{
281 Elf_Internal_Shdr **i_shdrp;
f075ee0c 282 bfd_byte *shstrtab = NULL;
dc810e39
AM
283 file_ptr offset;
284 bfd_size_type shstrtabsize;
252b5132
RH
285
286 i_shdrp = elf_elfsections (abfd);
74f2e02b
AM
287 if (i_shdrp == 0
288 || shindex >= elf_numsections (abfd)
289 || i_shdrp[shindex] == 0)
f075ee0c 290 return NULL;
252b5132 291
f075ee0c 292 shstrtab = i_shdrp[shindex]->contents;
252b5132
RH
293 if (shstrtab == NULL)
294 {
c044fabd 295 /* No cached one, attempt to read, and cache what we read. */
252b5132
RH
296 offset = i_shdrp[shindex]->sh_offset;
297 shstrtabsize = i_shdrp[shindex]->sh_size;
c6c60d09
JJ
298
299 /* Allocate and clear an extra byte at the end, to prevent crashes
300 in case the string table is not terminated. */
3471d59d 301 if (shstrtabsize + 1 <= 1
95a6d235 302 || shstrtabsize > bfd_get_file_size (abfd)
06614111
NC
303 || bfd_seek (abfd, offset, SEEK_SET) != 0
304 || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL)
c6c60d09
JJ
305 shstrtab = NULL;
306 else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
307 {
308 if (bfd_get_error () != bfd_error_system_call)
309 bfd_set_error (bfd_error_file_truncated);
06614111 310 bfd_release (abfd, shstrtab);
c6c60d09 311 shstrtab = NULL;
3471d59d
CC
312 /* Once we've failed to read it, make sure we don't keep
313 trying. Otherwise, we'll keep allocating space for
314 the string table over and over. */
315 i_shdrp[shindex]->sh_size = 0;
c6c60d09
JJ
316 }
317 else
318 shstrtab[shstrtabsize] = '\0';
217aa764 319 i_shdrp[shindex]->contents = shstrtab;
252b5132 320 }
f075ee0c 321 return (char *) shstrtab;
252b5132
RH
322}
323
324char *
217aa764
AM
325bfd_elf_string_from_elf_section (bfd *abfd,
326 unsigned int shindex,
327 unsigned int strindex)
252b5132
RH
328{
329 Elf_Internal_Shdr *hdr;
330
331 if (strindex == 0)
332 return "";
333
74f2e02b
AM
334 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
335 return NULL;
336
252b5132
RH
337 hdr = elf_elfsections (abfd)[shindex];
338
06614111
NC
339 if (hdr->contents == NULL)
340 {
341 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
342 {
343 /* PR 17512: file: f057ec89. */
695344c0 344 /* xgettext:c-format */
871b3ab2 345 _bfd_error_handler (_("%pB: attempt to load strings from"
63a5468a 346 " a non-string section (number %d)"),
06614111
NC
347 abfd, shindex);
348 return NULL;
349 }
b1fa9dd6 350
06614111
NC
351 if (bfd_elf_get_str_section (abfd, shindex) == NULL)
352 return NULL;
353 }
eed5def8
NC
354 else
355 {
356 /* PR 24273: The string section's contents may have already
357 been loaded elsewhere, eg because a corrupt file has the
358 string section index in the ELF header pointing at a group
359 section. So be paranoid, and test that the last byte of
360 the section is zero. */
361 if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
362 return NULL;
363 }
252b5132
RH
364
365 if (strindex >= hdr->sh_size)
366 {
1b3a8575 367 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
4eca0228 368 _bfd_error_handler
695344c0 369 /* xgettext:c-format */
2dcf00ce
AM
370 (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
371 abfd, strindex, (uint64_t) hdr->sh_size,
1b3a8575 372 (shindex == shstrndx && strindex == hdr->sh_name
252b5132 373 ? ".shstrtab"
1b3a8575 374 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
45b222d6 375 return NULL;
252b5132
RH
376 }
377
378 return ((char *) hdr->contents) + strindex;
379}
380
6cdc0ccc
AM
381/* Read and convert symbols to internal format.
382 SYMCOUNT specifies the number of symbols to read, starting from
383 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
384 are non-NULL, they are used to store the internal symbols, external
b7c368d0
NC
385 symbols, and symbol section index extensions, respectively.
386 Returns a pointer to the internal symbol buffer (malloced if necessary)
387 or NULL if there were no symbols or some kind of problem. */
6cdc0ccc
AM
388
389Elf_Internal_Sym *
217aa764
AM
390bfd_elf_get_elf_syms (bfd *ibfd,
391 Elf_Internal_Shdr *symtab_hdr,
392 size_t symcount,
393 size_t symoffset,
394 Elf_Internal_Sym *intsym_buf,
395 void *extsym_buf,
396 Elf_External_Sym_Shndx *extshndx_buf)
6cdc0ccc
AM
397{
398 Elf_Internal_Shdr *shndx_hdr;
217aa764 399 void *alloc_ext;
df622259 400 const bfd_byte *esym;
6cdc0ccc
AM
401 Elf_External_Sym_Shndx *alloc_extshndx;
402 Elf_External_Sym_Shndx *shndx;
4dd07732 403 Elf_Internal_Sym *alloc_intsym;
6cdc0ccc
AM
404 Elf_Internal_Sym *isym;
405 Elf_Internal_Sym *isymend;
9c5bfbb7 406 const struct elf_backend_data *bed;
6cdc0ccc
AM
407 size_t extsym_size;
408 bfd_size_type amt;
409 file_ptr pos;
410
e44a2c9c
AM
411 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
412 abort ();
413
6cdc0ccc
AM
414 if (symcount == 0)
415 return intsym_buf;
416
417 /* Normal syms might have section extension entries. */
418 shndx_hdr = NULL;
6a40cf0c
NC
419 if (elf_symtab_shndx_list (ibfd) != NULL)
420 {
421 elf_section_list * entry;
422 Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
423
424 /* Find an index section that is linked to this symtab section. */
425 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
315350be
NC
426 {
427 /* PR 20063. */
428 if (entry->hdr.sh_link >= elf_numsections (ibfd))
429 continue;
430
431 if (sections[entry->hdr.sh_link] == symtab_hdr)
432 {
433 shndx_hdr = & entry->hdr;
434 break;
435 };
436 }
6a40cf0c
NC
437
438 if (shndx_hdr == NULL)
439 {
440 if (symtab_hdr == & elf_symtab_hdr (ibfd))
441 /* Not really accurate, but this was how the old code used to work. */
442 shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr;
443 /* Otherwise we do nothing. The assumption is that
444 the index table will not be needed. */
445 }
446 }
6cdc0ccc
AM
447
448 /* Read the symbols. */
449 alloc_ext = NULL;
450 alloc_extshndx = NULL;
4dd07732 451 alloc_intsym = NULL;
6cdc0ccc
AM
452 bed = get_elf_backend_data (ibfd);
453 extsym_size = bed->s->sizeof_sym;
ef53be89 454 amt = (bfd_size_type) symcount * extsym_size;
6cdc0ccc
AM
455 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
456 if (extsym_buf == NULL)
457 {
d0fb9a8d 458 alloc_ext = bfd_malloc2 (symcount, extsym_size);
6cdc0ccc
AM
459 extsym_buf = alloc_ext;
460 }
461 if (extsym_buf == NULL
462 || bfd_seek (ibfd, pos, SEEK_SET) != 0
463 || bfd_bread (extsym_buf, amt, ibfd) != amt)
464 {
465 intsym_buf = NULL;
466 goto out;
467 }
468
469 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
470 extshndx_buf = NULL;
471 else
472 {
ef53be89 473 amt = (bfd_size_type) symcount * sizeof (Elf_External_Sym_Shndx);
6cdc0ccc
AM
474 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
475 if (extshndx_buf == NULL)
476 {
a50b1753 477 alloc_extshndx = (Elf_External_Sym_Shndx *)
07d6d2b8 478 bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx));
6cdc0ccc
AM
479 extshndx_buf = alloc_extshndx;
480 }
481 if (extshndx_buf == NULL
482 || bfd_seek (ibfd, pos, SEEK_SET) != 0
483 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
484 {
485 intsym_buf = NULL;
486 goto out;
487 }
488 }
489
490 if (intsym_buf == NULL)
491 {
a50b1753 492 alloc_intsym = (Elf_Internal_Sym *)
07d6d2b8 493 bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
4dd07732 494 intsym_buf = alloc_intsym;
6cdc0ccc
AM
495 if (intsym_buf == NULL)
496 goto out;
497 }
498
499 /* Convert the symbols to internal form. */
500 isymend = intsym_buf + symcount;
a50b1753 501 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
07d6d2b8 502 shndx = extshndx_buf;
6cdc0ccc
AM
503 isym < isymend;
504 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
8384fb8f
AM
505 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
506 {
507 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
695344c0 508 /* xgettext:c-format */
871b3ab2 509 _bfd_error_handler (_("%pB symbol number %lu references"
63a5468a 510 " nonexistent SHT_SYMTAB_SHNDX section"),
4eca0228 511 ibfd, (unsigned long) symoffset);
4dd07732
AM
512 if (alloc_intsym != NULL)
513 free (alloc_intsym);
8384fb8f
AM
514 intsym_buf = NULL;
515 goto out;
516 }
6cdc0ccc
AM
517
518 out:
519 if (alloc_ext != NULL)
520 free (alloc_ext);
521 if (alloc_extshndx != NULL)
522 free (alloc_extshndx);
523
524 return intsym_buf;
525}
526
5cab59f6
AM
527/* Look up a symbol name. */
528const char *
be8dd2ca
AM
529bfd_elf_sym_name (bfd *abfd,
530 Elf_Internal_Shdr *symtab_hdr,
26c61ae5
L
531 Elf_Internal_Sym *isym,
532 asection *sym_sec)
5cab59f6 533{
26c61ae5 534 const char *name;
5cab59f6 535 unsigned int iname = isym->st_name;
be8dd2ca 536 unsigned int shindex = symtab_hdr->sh_link;
26c61ae5 537
138f35cc
JJ
538 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
539 /* Check for a bogus st_shndx to avoid crashing. */
4fbb74a6 540 && isym->st_shndx < elf_numsections (abfd))
5cab59f6
AM
541 {
542 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
543 shindex = elf_elfheader (abfd)->e_shstrndx;
544 }
545
26c61ae5
L
546 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
547 if (name == NULL)
548 name = "(null)";
549 else if (sym_sec && *name == '\0')
fd361982 550 name = bfd_section_name (sym_sec);
26c61ae5
L
551
552 return name;
5cab59f6
AM
553}
554
dbb410c3
AM
555/* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
556 sections. The first element is the flags, the rest are section
557 pointers. */
558
559typedef union elf_internal_group {
560 Elf_Internal_Shdr *shdr;
561 unsigned int flags;
562} Elf_Internal_Group;
563
b885599b
AM
564/* Return the name of the group signature symbol. Why isn't the
565 signature just a string? */
566
567static const char *
217aa764 568group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
b885599b 569{
9dce4196 570 Elf_Internal_Shdr *hdr;
9dce4196
AM
571 unsigned char esym[sizeof (Elf64_External_Sym)];
572 Elf_External_Sym_Shndx eshndx;
573 Elf_Internal_Sym isym;
b885599b 574
13792e9d
L
575 /* First we need to ensure the symbol table is available. Make sure
576 that it is a symbol table section. */
4fbb74a6
AM
577 if (ghdr->sh_link >= elf_numsections (abfd))
578 return NULL;
13792e9d
L
579 hdr = elf_elfsections (abfd) [ghdr->sh_link];
580 if (hdr->sh_type != SHT_SYMTAB
581 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
b885599b
AM
582 return NULL;
583
9dce4196
AM
584 /* Go read the symbol. */
585 hdr = &elf_tdata (abfd)->symtab_hdr;
6cdc0ccc
AM
586 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
587 &isym, esym, &eshndx) == NULL)
b885599b 588 return NULL;
9dce4196 589
26c61ae5 590 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
b885599b
AM
591}
592
dbb410c3
AM
593/* Set next_in_group list pointer, and group name for NEWSECT. */
594
b34976b6 595static bfd_boolean
217aa764 596setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
dbb410c3
AM
597{
598 unsigned int num_group = elf_tdata (abfd)->num_group;
599
600 /* If num_group is zero, read in all SHT_GROUP sections. The count
601 is set to -1 if there are no SHT_GROUP sections. */
602 if (num_group == 0)
603 {
604 unsigned int i, shnum;
605
606 /* First count the number of groups. If we have a SHT_GROUP
607 section with just a flag word (ie. sh_size is 4), ignore it. */
9ad5cbcf 608 shnum = elf_numsections (abfd);
dbb410c3 609 num_group = 0;
08a40648 610
44534af3 611#define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
1783205a 612 ( (shdr)->sh_type == SHT_GROUP \
44534af3 613 && (shdr)->sh_size >= minsize \
1783205a
NC
614 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
615 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
08a40648 616
dbb410c3
AM
617 for (i = 0; i < shnum; i++)
618 {
619 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
1783205a 620
44534af3 621 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
dbb410c3
AM
622 num_group += 1;
623 }
624
625 if (num_group == 0)
20dbb49d
L
626 {
627 num_group = (unsigned) -1;
628 elf_tdata (abfd)->num_group = num_group;
ce497010 629 elf_tdata (abfd)->group_sect_ptr = NULL;
20dbb49d
L
630 }
631 else
dbb410c3
AM
632 {
633 /* We keep a list of elf section headers for group sections,
634 so we can find them quickly. */
20dbb49d 635 bfd_size_type amt;
d0fb9a8d 636
20dbb49d 637 elf_tdata (abfd)->num_group = num_group;
a50b1753 638 elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **)
07d6d2b8 639 bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
dbb410c3 640 if (elf_tdata (abfd)->group_sect_ptr == NULL)
b34976b6 641 return FALSE;
4bba0fb1
AM
642 memset (elf_tdata (abfd)->group_sect_ptr, 0,
643 num_group * sizeof (Elf_Internal_Shdr *));
dbb410c3 644 num_group = 0;
ce497010 645
dbb410c3
AM
646 for (i = 0; i < shnum; i++)
647 {
648 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
1783205a 649
44534af3 650 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
dbb410c3 651 {
973ffd63 652 unsigned char *src;
dbb410c3
AM
653 Elf_Internal_Group *dest;
654
07d6d2b8
AM
655 /* Make sure the group section has a BFD section
656 attached to it. */
657 if (!bfd_section_from_shdr (abfd, i))
658 return FALSE;
659
dbb410c3
AM
660 /* Add to list of sections. */
661 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
662 num_group += 1;
663
664 /* Read the raw contents. */
665 BFD_ASSERT (sizeof (*dest) >= 4);
666 amt = shdr->sh_size * sizeof (*dest) / 4;
a50b1753 667 shdr->contents = (unsigned char *)
eed5def8 668 bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4);
1783205a
NC
669 /* PR binutils/4110: Handle corrupt group headers. */
670 if (shdr->contents == NULL)
671 {
672 _bfd_error_handler
695344c0 673 /* xgettext:c-format */
871b3ab2 674 (_("%pB: corrupt size field in group section"
2dcf00ce
AM
675 " header: %#" PRIx64),
676 abfd, (uint64_t) shdr->sh_size);
1783205a 677 bfd_set_error (bfd_error_bad_value);
493a3386
NC
678 -- num_group;
679 continue;
1783205a
NC
680 }
681
682 memset (shdr->contents, 0, amt);
683
684 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
dbb410c3
AM
685 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
686 != shdr->sh_size))
493a3386
NC
687 {
688 _bfd_error_handler
695344c0 689 /* xgettext:c-format */
871b3ab2 690 (_("%pB: invalid size field in group section"
2dcf00ce
AM
691 " header: %#" PRIx64 ""),
692 abfd, (uint64_t) shdr->sh_size);
493a3386
NC
693 bfd_set_error (bfd_error_bad_value);
694 -- num_group;
63a5468a
AM
695 /* PR 17510: If the group contents are even
696 partially corrupt, do not allow any of the
697 contents to be used. */
493a3386
NC
698 memset (shdr->contents, 0, amt);
699 continue;
700 }
708d7d0d 701
dbb410c3
AM
702 /* Translate raw contents, a flag word followed by an
703 array of elf section indices all in target byte order,
704 to the flag word followed by an array of elf section
705 pointers. */
706 src = shdr->contents + shdr->sh_size;
707 dest = (Elf_Internal_Group *) (shdr->contents + amt);
06614111 708
dbb410c3
AM
709 while (1)
710 {
711 unsigned int idx;
712
713 src -= 4;
714 --dest;
715 idx = H_GET_32 (abfd, src);
716 if (src == shdr->contents)
717 {
718 dest->flags = idx;
b885599b
AM
719 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
720 shdr->bfd_section->flags
721 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
dbb410c3
AM
722 break;
723 }
4bba0fb1 724 if (idx < shnum)
bae363f1
L
725 {
726 dest->shdr = elf_elfsections (abfd)[idx];
727 /* PR binutils/23199: All sections in a
728 section group should be marked with
729 SHF_GROUP. But some tools generate
730 broken objects without SHF_GROUP. Fix
731 them up here. */
732 dest->shdr->sh_flags |= SHF_GROUP;
733 }
4bba0fb1
AM
734 if (idx >= shnum
735 || dest->shdr->sh_type == SHT_GROUP)
dbb410c3 736 {
4eca0228 737 _bfd_error_handler
4bba0fb1
AM
738 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
739 abfd, i);
740 dest->shdr = NULL;
dbb410c3 741 }
dbb410c3
AM
742 }
743 }
744 }
493a3386
NC
745
746 /* PR 17510: Corrupt binaries might contain invalid groups. */
747 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
748 {
749 elf_tdata (abfd)->num_group = num_group;
750
751 /* If all groups are invalid then fail. */
752 if (num_group == 0)
753 {
754 elf_tdata (abfd)->group_sect_ptr = NULL;
755 elf_tdata (abfd)->num_group = num_group = -1;
4eca0228 756 _bfd_error_handler
871b3ab2 757 (_("%pB: no valid group sections found"), abfd);
493a3386
NC
758 bfd_set_error (bfd_error_bad_value);
759 }
760 }
dbb410c3
AM
761 }
762 }
763
764 if (num_group != (unsigned) -1)
765 {
564e11c9
JW
766 unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
767 unsigned int j;
dbb410c3 768
564e11c9 769 for (j = 0; j < num_group; j++)
dbb410c3 770 {
564e11c9
JW
771 /* Begin search from previous found group. */
772 unsigned i = (j + search_offset) % num_group;
773
dbb410c3 774 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
ce497010 775 Elf_Internal_Group *idx;
0c54f692 776 bfd_size_type n_elt;
ce497010
NC
777
778 if (shdr == NULL)
779 continue;
780
781 idx = (Elf_Internal_Group *) shdr->contents;
0c54f692
NC
782 if (idx == NULL || shdr->sh_size < 4)
783 {
784 /* See PR 21957 for a reproducer. */
785 /* xgettext:c-format */
871b3ab2 786 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
0c54f692
NC
787 abfd, shdr->bfd_section);
788 elf_tdata (abfd)->group_sect_ptr[i] = NULL;
789 bfd_set_error (bfd_error_bad_value);
790 return FALSE;
791 }
ce497010 792 n_elt = shdr->sh_size / 4;
dbb410c3
AM
793
794 /* Look through this group's sections to see if current
795 section is a member. */
796 while (--n_elt != 0)
797 if ((++idx)->shdr == hdr)
798 {
e0e8c97f 799 asection *s = NULL;
dbb410c3
AM
800
801 /* We are a member of this group. Go looking through
802 other members to see if any others are linked via
803 next_in_group. */
804 idx = (Elf_Internal_Group *) shdr->contents;
805 n_elt = shdr->sh_size / 4;
806 while (--n_elt != 0)
4bba0fb1
AM
807 if ((++idx)->shdr != NULL
808 && (s = idx->shdr->bfd_section) != NULL
945906ff 809 && elf_next_in_group (s) != NULL)
dbb410c3
AM
810 break;
811 if (n_elt != 0)
812 {
dbb410c3
AM
813 /* Snarf the group name from other member, and
814 insert current section in circular list. */
945906ff
AM
815 elf_group_name (newsect) = elf_group_name (s);
816 elf_next_in_group (newsect) = elf_next_in_group (s);
817 elf_next_in_group (s) = newsect;
dbb410c3
AM
818 }
819 else
820 {
dbb410c3
AM
821 const char *gname;
822
b885599b
AM
823 gname = group_signature (abfd, shdr);
824 if (gname == NULL)
b34976b6 825 return FALSE;
945906ff 826 elf_group_name (newsect) = gname;
dbb410c3
AM
827
828 /* Start a circular list with one element. */
945906ff 829 elf_next_in_group (newsect) = newsect;
dbb410c3 830 }
b885599b 831
9dce4196
AM
832 /* If the group section has been created, point to the
833 new member. */
dbb410c3 834 if (shdr->bfd_section != NULL)
945906ff 835 elf_next_in_group (shdr->bfd_section) = newsect;
b885599b 836
564e11c9
JW
837 elf_tdata (abfd)->group_search_offset = i;
838 j = num_group - 1;
dbb410c3
AM
839 break;
840 }
841 }
842 }
843
945906ff 844 if (elf_group_name (newsect) == NULL)
dbb410c3 845 {
695344c0 846 /* xgettext:c-format */
871b3ab2 847 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
4eca0228 848 abfd, newsect);
493a3386 849 return FALSE;
dbb410c3 850 }
b34976b6 851 return TRUE;
dbb410c3
AM
852}
853
3d7f7666 854bfd_boolean
dd863624 855_bfd_elf_setup_sections (bfd *abfd)
3d7f7666
L
856{
857 unsigned int i;
858 unsigned int num_group = elf_tdata (abfd)->num_group;
859 bfd_boolean result = TRUE;
dd863624
L
860 asection *s;
861
862 /* Process SHF_LINK_ORDER. */
863 for (s = abfd->sections; s != NULL; s = s->next)
864 {
865 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
866 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
867 {
868 unsigned int elfsec = this_hdr->sh_link;
869 /* FIXME: The old Intel compiler and old strip/objcopy may
870 not set the sh_link or sh_info fields. Hence we could
871 get the situation where elfsec is 0. */
872 if (elfsec == 0)
873 {
4fbb74a6 874 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
dd863624
L
875 if (bed->link_order_error_handler)
876 bed->link_order_error_handler
695344c0 877 /* xgettext:c-format */
871b3ab2 878 (_("%pB: warning: sh_link not set for section `%pA'"),
dd863624
L
879 abfd, s);
880 }
881 else
882 {
91d6fa6a 883 asection *linksec = NULL;
25bbc984 884
4fbb74a6
AM
885 if (elfsec < elf_numsections (abfd))
886 {
887 this_hdr = elf_elfsections (abfd)[elfsec];
91d6fa6a 888 linksec = this_hdr->bfd_section;
4fbb74a6 889 }
25bbc984
L
890
891 /* PR 1991, 2008:
892 Some strip/objcopy may leave an incorrect value in
893 sh_link. We don't want to proceed. */
91d6fa6a 894 if (linksec == NULL)
25bbc984 895 {
4eca0228 896 _bfd_error_handler
695344c0 897 /* xgettext:c-format */
871b3ab2 898 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
c08bb8dd 899 s->owner, elfsec, s);
25bbc984
L
900 result = FALSE;
901 }
902
91d6fa6a 903 elf_linked_to_section (s) = linksec;
dd863624
L
904 }
905 }
53720c49
AM
906 else if (this_hdr->sh_type == SHT_GROUP
907 && elf_next_in_group (s) == NULL)
908 {
4eca0228 909 _bfd_error_handler
695344c0 910 /* xgettext:c-format */
871b3ab2 911 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
53720c49
AM
912 abfd, elf_section_data (s)->this_idx);
913 result = FALSE;
914 }
dd863624 915 }
3d7f7666 916
dd863624 917 /* Process section groups. */
3d7f7666
L
918 if (num_group == (unsigned) -1)
919 return result;
920
921 for (i = 0; i < num_group; i++)
922 {
923 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
4b0e8a5f
NC
924 Elf_Internal_Group *idx;
925 unsigned int n_elt;
3d7f7666 926
4b0e8a5f
NC
927 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
928 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
929 {
4eca0228 930 _bfd_error_handler
695344c0 931 /* xgettext:c-format */
871b3ab2 932 (_("%pB: section group entry number %u is corrupt"),
4b0e8a5f
NC
933 abfd, i);
934 result = FALSE;
935 continue;
936 }
937
938 idx = (Elf_Internal_Group *) shdr->contents;
939 n_elt = shdr->sh_size / 4;
1b786873 940
3d7f7666 941 while (--n_elt != 0)
24d3e51b
NC
942 {
943 ++ idx;
944
945 if (idx->shdr == NULL)
946 continue;
947 else if (idx->shdr->bfd_section)
948 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
db4677b8
AM
949 else if (idx->shdr->sh_type != SHT_RELA
950 && idx->shdr->sh_type != SHT_REL)
24d3e51b
NC
951 {
952 /* There are some unknown sections in the group. */
953 _bfd_error_handler
954 /* xgettext:c-format */
871b3ab2 955 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
24d3e51b
NC
956 abfd,
957 idx->shdr->sh_type,
958 bfd_elf_string_from_elf_section (abfd,
959 (elf_elfheader (abfd)
960 ->e_shstrndx),
961 idx->shdr->sh_name),
962 shdr->bfd_section);
963 result = FALSE;
964 }
965 }
3d7f7666 966 }
24d3e51b 967
3d7f7666
L
968 return result;
969}
970
72adc230
AM
971bfd_boolean
972bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
973{
974 return elf_next_in_group (sec) != NULL;
975}
976
cb7f4b29
AM
977const char *
978bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
979{
980 if (elf_sec_group (sec) != NULL)
981 return elf_group_name (sec);
982 return NULL;
983}
984
f6fe1ccd
L
985static char *
986convert_debug_to_zdebug (bfd *abfd, const char *name)
987{
988 unsigned int len = strlen (name);
989 char *new_name = bfd_alloc (abfd, len + 2);
990 if (new_name == NULL)
991 return NULL;
992 new_name[0] = '.';
993 new_name[1] = 'z';
994 memcpy (new_name + 2, name + 1, len);
995 return new_name;
996}
997
998static char *
999convert_zdebug_to_debug (bfd *abfd, const char *name)
1000{
1001 unsigned int len = strlen (name);
1002 char *new_name = bfd_alloc (abfd, len);
1003 if (new_name == NULL)
1004 return NULL;
1005 new_name[0] = '.';
1006 memcpy (new_name + 1, name + 2, len - 1);
1007 return new_name;
1008}
1009
cc5277b1
ML
1010/* This a copy of lto_section defined in GCC (lto-streamer.h). */
1011
1012struct lto_section
1013{
1014 int16_t major_version;
1015 int16_t minor_version;
1016 unsigned char slim_object;
1017
1018 /* Flags is a private field that is not defined publicly. */
1019 uint16_t flags;
1020};
1021
252b5132
RH
1022/* Make a BFD section from an ELF section. We store a pointer to the
1023 BFD section in the bfd_section field of the header. */
1024
b34976b6 1025bfd_boolean
217aa764
AM
1026_bfd_elf_make_section_from_shdr (bfd *abfd,
1027 Elf_Internal_Shdr *hdr,
6dc132d9
L
1028 const char *name,
1029 int shindex)
252b5132
RH
1030{
1031 asection *newsect;
1032 flagword flags;
9c5bfbb7 1033 const struct elf_backend_data *bed;
252b5132
RH
1034
1035 if (hdr->bfd_section != NULL)
4e011fb5 1036 return TRUE;
252b5132
RH
1037
1038 newsect = bfd_make_section_anyway (abfd, name);
1039 if (newsect == NULL)
b34976b6 1040 return FALSE;
252b5132 1041
1829f4b2
AM
1042 hdr->bfd_section = newsect;
1043 elf_section_data (newsect)->this_hdr = *hdr;
6dc132d9 1044 elf_section_data (newsect)->this_idx = shindex;
1829f4b2 1045
2f89ff8d
L
1046 /* Always use the real type/flags. */
1047 elf_section_type (newsect) = hdr->sh_type;
1048 elf_section_flags (newsect) = hdr->sh_flags;
1049
252b5132
RH
1050 newsect->filepos = hdr->sh_offset;
1051
fd361982
AM
1052 if (!bfd_set_section_vma (newsect, hdr->sh_addr)
1053 || !bfd_set_section_size (newsect, hdr->sh_size)
1054 || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign)))
b34976b6 1055 return FALSE;
252b5132
RH
1056
1057 flags = SEC_NO_FLAGS;
1058 if (hdr->sh_type != SHT_NOBITS)
1059 flags |= SEC_HAS_CONTENTS;
dbb410c3 1060 if (hdr->sh_type == SHT_GROUP)
7bdf4127 1061 flags |= SEC_GROUP;
252b5132
RH
1062 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1063 {
1064 flags |= SEC_ALLOC;
1065 if (hdr->sh_type != SHT_NOBITS)
1066 flags |= SEC_LOAD;
1067 }
1068 if ((hdr->sh_flags & SHF_WRITE) == 0)
1069 flags |= SEC_READONLY;
1070 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1071 flags |= SEC_CODE;
1072 else if ((flags & SEC_LOAD) != 0)
1073 flags |= SEC_DATA;
f5fa8ca2
JJ
1074 if ((hdr->sh_flags & SHF_MERGE) != 0)
1075 {
1076 flags |= SEC_MERGE;
1077 newsect->entsize = hdr->sh_entsize;
f5fa8ca2 1078 }
84865015
NC
1079 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1080 flags |= SEC_STRINGS;
dbb410c3
AM
1081 if (hdr->sh_flags & SHF_GROUP)
1082 if (!setup_group (abfd, hdr, newsect))
b34976b6 1083 return FALSE;
13ae64f3
JJ
1084 if ((hdr->sh_flags & SHF_TLS) != 0)
1085 flags |= SEC_THREAD_LOCAL;
18ae9cc1
L
1086 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1087 flags |= SEC_EXCLUDE;
252b5132 1088
df3a023b
AM
1089 switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1090 {
1091 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1092 but binutils as of 2019-07-23 did not set the EI_OSABI header
1093 byte. */
1094 case ELFOSABI_NONE:
1095 case ELFOSABI_GNU:
1096 case ELFOSABI_FREEBSD:
1097 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1098 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1099 break;
1100 }
1101
3d2b39cf 1102 if ((flags & SEC_ALLOC) == 0)
7a6cc5fb 1103 {
3d2b39cf
L
1104 /* The debugging sections appear to be recognized only by name,
1105 not any sort of flag. Their SEC_ALLOC bits are cleared. */
3d2b39cf
L
1106 if (name [0] == '.')
1107 {
f073ced3
AM
1108 const char *p;
1109 int n;
1110 if (name[1] == 'd')
1111 p = ".debug", n = 6;
1112 else if (name[1] == 'g' && name[2] == 'n')
1113 p = ".gnu.linkonce.wi.", n = 17;
1114 else if (name[1] == 'g' && name[2] == 'd')
1115 p = ".gdb_index", n = 11; /* yes we really do mean 11. */
1116 else if (name[1] == 'l')
1117 p = ".line", n = 5;
1118 else if (name[1] == 's')
1119 p = ".stab", n = 5;
1120 else if (name[1] == 'z')
1121 p = ".zdebug", n = 7;
1122 else
1123 p = NULL, n = 0;
1124 if (p != NULL && strncmp (name, p, n) == 0)
3d2b39cf
L
1125 flags |= SEC_DEBUGGING;
1126 }
1127 }
252b5132
RH
1128
1129 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1130 only link a single copy of the section. This is used to support
1131 g++. g++ will emit each template expansion in its own section.
1132 The symbols will be defined as weak, so that multiple definitions
1133 are permitted. The GNU linker extension is to actually discard
1134 all but one of the sections. */
0112cd26 1135 if (CONST_STRNEQ (name, ".gnu.linkonce")
b885599b 1136 && elf_next_in_group (newsect) == NULL)
252b5132
RH
1137 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1138
fa152c49
JW
1139 bed = get_elf_backend_data (abfd);
1140 if (bed->elf_backend_section_flags)
1141 if (! bed->elf_backend_section_flags (&flags, hdr))
b34976b6 1142 return FALSE;
fa152c49 1143
fd361982 1144 if (!bfd_set_section_flags (newsect, flags))
b34976b6 1145 return FALSE;
252b5132 1146
718175fa
JK
1147 /* We do not parse the PT_NOTE segments as we are interested even in the
1148 separate debug info files which may have the segments offsets corrupted.
1149 PT_NOTEs from the core files are currently not parsed using BFD. */
1150 if (hdr->sh_type == SHT_NOTE)
1151 {
baea7ef1 1152 bfd_byte *contents;
718175fa 1153
baea7ef1 1154 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
718175fa
JK
1155 return FALSE;
1156
276da9b3
L
1157 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1158 hdr->sh_offset, hdr->sh_addralign);
718175fa
JK
1159 free (contents);
1160 }
1161
252b5132
RH
1162 if ((flags & SEC_ALLOC) != 0)
1163 {
1164 Elf_Internal_Phdr *phdr;
6ffd7900
AM
1165 unsigned int i, nload;
1166
1167 /* Some ELF linkers produce binaries with all the program header
1168 p_paddr fields zero. If we have such a binary with more than
1169 one PT_LOAD header, then leave the section lma equal to vma
1170 so that we don't create sections with overlapping lma. */
1171 phdr = elf_tdata (abfd)->phdr;
1172 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1173 if (phdr->p_paddr != 0)
1174 break;
1175 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1176 ++nload;
1177 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1178 return TRUE;
252b5132 1179
252b5132
RH
1180 phdr = elf_tdata (abfd)->phdr;
1181 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1182 {
86b2281f
AM
1183 if (((phdr->p_type == PT_LOAD
1184 && (hdr->sh_flags & SHF_TLS) == 0)
1185 || phdr->p_type == PT_TLS)
9a83a553 1186 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
252b5132 1187 {
88967714
AM
1188 if ((flags & SEC_LOAD) == 0)
1189 newsect->lma = (phdr->p_paddr
1190 + hdr->sh_addr - phdr->p_vaddr);
1191 else
1192 /* We used to use the same adjustment for SEC_LOAD
1193 sections, but that doesn't work if the segment
1194 is packed with code from multiple VMAs.
1195 Instead we calculate the section LMA based on
1196 the segment LMA. It is assumed that the
1197 segment will contain sections with contiguous
1198 LMAs, even if the VMAs are not. */
1199 newsect->lma = (phdr->p_paddr
1200 + hdr->sh_offset - phdr->p_offset);
1201
1202 /* With contiguous segments, we can't tell from file
1203 offsets whether a section with zero size should
1204 be placed at the end of one segment or the
1205 beginning of the next. Decide based on vaddr. */
1206 if (hdr->sh_addr >= phdr->p_vaddr
1207 && (hdr->sh_addr + hdr->sh_size
1208 <= phdr->p_vaddr + phdr->p_memsz))
1209 break;
252b5132
RH
1210 }
1211 }
1212 }
1213
4a114e3e
L
1214 /* Compress/decompress DWARF debug sections with names: .debug_* and
1215 .zdebug_*, after the section flags is set. */
1216 if ((flags & SEC_DEBUGGING)
1217 && ((name[1] == 'd' && name[6] == '_')
1218 || (name[1] == 'z' && name[7] == '_')))
1219 {
1220 enum { nothing, compress, decompress } action = nothing;
151411f8 1221 int compression_header_size;
dab394de 1222 bfd_size_type uncompressed_size;
4207142d 1223 unsigned int uncompressed_align_power;
151411f8
L
1224 bfd_boolean compressed
1225 = bfd_is_section_compressed_with_header (abfd, newsect,
dab394de 1226 &compression_header_size,
4207142d
MW
1227 &uncompressed_size,
1228 &uncompressed_align_power);
151411f8 1229 if (compressed)
4a114e3e
L
1230 {
1231 /* Compressed section. Check if we should decompress. */
1232 if ((abfd->flags & BFD_DECOMPRESS))
1233 action = decompress;
1234 }
151411f8
L
1235
1236 /* Compress the uncompressed section or convert from/to .zdebug*
1237 section. Check if we should compress. */
1238 if (action == nothing)
4a114e3e 1239 {
151411f8
L
1240 if (newsect->size != 0
1241 && (abfd->flags & BFD_COMPRESS)
1242 && compression_header_size >= 0
dab394de 1243 && uncompressed_size > 0
151411f8
L
1244 && (!compressed
1245 || ((compression_header_size > 0)
1246 != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
4a114e3e 1247 action = compress;
151411f8
L
1248 else
1249 return TRUE;
4a114e3e
L
1250 }
1251
151411f8 1252 if (action == compress)
4a114e3e 1253 {
4a114e3e
L
1254 if (!bfd_init_section_compress_status (abfd, newsect))
1255 {
4eca0228 1256 _bfd_error_handler
695344c0 1257 /* xgettext:c-format */
871b3ab2 1258 (_("%pB: unable to initialize compress status for section %s"),
4a114e3e
L
1259 abfd, name);
1260 return FALSE;
1261 }
151411f8
L
1262 }
1263 else
1264 {
4a114e3e
L
1265 if (!bfd_init_section_decompress_status (abfd, newsect))
1266 {
4eca0228 1267 _bfd_error_handler
695344c0 1268 /* xgettext:c-format */
871b3ab2 1269 (_("%pB: unable to initialize decompress status for section %s"),
4a114e3e
L
1270 abfd, name);
1271 return FALSE;
1272 }
151411f8
L
1273 }
1274
f6fe1ccd 1275 if (abfd->is_linker_input)
151411f8 1276 {
f6fe1ccd
L
1277 if (name[1] == 'z'
1278 && (action == decompress
1279 || (action == compress
1280 && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
4e011fb5 1281 {
f6fe1ccd
L
1282 /* Convert section name from .zdebug_* to .debug_* so
1283 that linker will consider this section as a debug
1284 section. */
1285 char *new_name = convert_zdebug_to_debug (abfd, name);
151411f8
L
1286 if (new_name == NULL)
1287 return FALSE;
fd361982 1288 bfd_rename_section (newsect, new_name);
151411f8 1289 }
4a114e3e 1290 }
f6fe1ccd
L
1291 else
1292 /* For objdump, don't rename the section. For objcopy, delay
1293 section rename to elf_fake_sections. */
1294 newsect->flags |= SEC_ELF_RENAME;
4a114e3e
L
1295 }
1296
cc5277b1
ML
1297 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1298 section. */
1299 const char *lto_section_name = ".gnu.lto_.lto.";
1300 if (strncmp (name, lto_section_name, strlen (lto_section_name)) == 0)
1301 {
1302 struct lto_section lsection;
1303 if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
1304 sizeof (struct lto_section)))
1305 abfd->lto_slim_object = lsection.slim_object;
1306 }
1307
b34976b6 1308 return TRUE;
252b5132
RH
1309}
1310
84865015
NC
1311const char *const bfd_elf_section_type_names[] =
1312{
252b5132
RH
1313 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1314 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1315 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1316};
1317
1049f94e 1318/* ELF relocs are against symbols. If we are producing relocatable
252b5132
RH
1319 output, and the reloc is against an external symbol, and nothing
1320 has given us any additional addend, the resulting reloc will also
1321 be against the same symbol. In such a case, we don't want to
1322 change anything about the way the reloc is handled, since it will
1323 all be done at final link time. Rather than put special case code
1324 into bfd_perform_relocation, all the reloc types use this howto
1325 function. It just short circuits the reloc if producing
1049f94e 1326 relocatable output against an external symbol. */
252b5132 1327
252b5132 1328bfd_reloc_status_type
217aa764
AM
1329bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1330 arelent *reloc_entry,
1331 asymbol *symbol,
1332 void *data ATTRIBUTE_UNUSED,
1333 asection *input_section,
1334 bfd *output_bfd,
1335 char **error_message ATTRIBUTE_UNUSED)
1336{
1337 if (output_bfd != NULL
252b5132
RH
1338 && (symbol->flags & BSF_SECTION_SYM) == 0
1339 && (! reloc_entry->howto->partial_inplace
1340 || reloc_entry->addend == 0))
1341 {
1342 reloc_entry->address += input_section->output_offset;
1343 return bfd_reloc_ok;
1344 }
1345
1346 return bfd_reloc_continue;
1347}
1348\f
84865015
NC
1349/* Returns TRUE if section A matches section B.
1350 Names, addresses and links may be different, but everything else
1351 should be the same. */
1352
1353static bfd_boolean
5522f910
NC
1354section_match (const Elf_Internal_Shdr * a,
1355 const Elf_Internal_Shdr * b)
84865015 1356{
ac85e67c
AM
1357 if (a->sh_type != b->sh_type
1358 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1359 || a->sh_addralign != b->sh_addralign
1360 || a->sh_entsize != b->sh_entsize)
1361 return FALSE;
1362 if (a->sh_type == SHT_SYMTAB
1363 || a->sh_type == SHT_STRTAB)
1364 return TRUE;
1365 return a->sh_size == b->sh_size;
84865015
NC
1366}
1367
1368/* Find a section in OBFD that has the same characteristics
1369 as IHEADER. Return the index of this section or SHN_UNDEF if
1370 none can be found. Check's section HINT first, as this is likely
1371 to be the correct section. */
1372
1373static unsigned int
5cc4ca83
ST
1374find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1375 const unsigned int hint)
84865015
NC
1376{
1377 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1378 unsigned int i;
1379
a55c9876
NC
1380 BFD_ASSERT (iheader != NULL);
1381
1382 /* See PR 20922 for a reproducer of the NULL test. */
5cc4ca83
ST
1383 if (hint < elf_numsections (obfd)
1384 && oheaders[hint] != NULL
a55c9876 1385 && section_match (oheaders[hint], iheader))
84865015
NC
1386 return hint;
1387
1388 for (i = 1; i < elf_numsections (obfd); i++)
1389 {
1390 Elf_Internal_Shdr * oheader = oheaders[i];
1391
a55c9876
NC
1392 if (oheader == NULL)
1393 continue;
84865015
NC
1394 if (section_match (oheader, iheader))
1395 /* FIXME: Do we care if there is a potential for
1396 multiple matches ? */
1397 return i;
1398 }
1399
1400 return SHN_UNDEF;
1401}
1402
5522f910
NC
1403/* PR 19938: Attempt to set the ELF section header fields of an OS or
1404 Processor specific section, based upon a matching input section.
1405 Returns TRUE upon success, FALSE otherwise. */
07d6d2b8 1406
5522f910
NC
1407static bfd_boolean
1408copy_special_section_fields (const bfd *ibfd,
1409 bfd *obfd,
1410 const Elf_Internal_Shdr *iheader,
1411 Elf_Internal_Shdr *oheader,
1412 const unsigned int secnum)
1413{
1414 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1415 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1416 bfd_boolean changed = FALSE;
1417 unsigned int sh_link;
1418
1419 if (oheader->sh_type == SHT_NOBITS)
1420 {
1421 /* This is a feature for objcopy --only-keep-debug:
1422 When a section's type is changed to NOBITS, we preserve
1423 the sh_link and sh_info fields so that they can be
1424 matched up with the original.
1425
1426 Note: Strictly speaking these assignments are wrong.
1427 The sh_link and sh_info fields should point to the
1428 relevent sections in the output BFD, which may not be in
1429 the same location as they were in the input BFD. But
1430 the whole point of this action is to preserve the
1431 original values of the sh_link and sh_info fields, so
1432 that they can be matched up with the section headers in
1433 the original file. So strictly speaking we may be
1434 creating an invalid ELF file, but it is only for a file
1435 that just contains debug info and only for sections
1436 without any contents. */
1437 if (oheader->sh_link == 0)
1438 oheader->sh_link = iheader->sh_link;
1439 if (oheader->sh_info == 0)
1440 oheader->sh_info = iheader->sh_info;
1441 return TRUE;
1442 }
1443
1444 /* Allow the target a chance to decide how these fields should be set. */
1445 if (bed->elf_backend_copy_special_section_fields != NULL
1446 && bed->elf_backend_copy_special_section_fields
1447 (ibfd, obfd, iheader, oheader))
1448 return TRUE;
1449
1450 /* We have an iheader which might match oheader, and which has non-zero
1451 sh_info and/or sh_link fields. Attempt to follow those links and find
1452 the section in the output bfd which corresponds to the linked section
1453 in the input bfd. */
1454 if (iheader->sh_link != SHN_UNDEF)
1455 {
4f3ca05b
NC
1456 /* See PR 20931 for a reproducer. */
1457 if (iheader->sh_link >= elf_numsections (ibfd))
1458 {
76cfced5 1459 _bfd_error_handler
4f3ca05b 1460 /* xgettext:c-format */
9793eb77 1461 (_("%pB: invalid sh_link field (%d) in section number %d"),
4f3ca05b
NC
1462 ibfd, iheader->sh_link, secnum);
1463 return FALSE;
1464 }
1465
5522f910
NC
1466 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1467 if (sh_link != SHN_UNDEF)
1468 {
1469 oheader->sh_link = sh_link;
1470 changed = TRUE;
1471 }
1472 else
1473 /* FIXME: Should we install iheader->sh_link
1474 if we could not find a match ? */
76cfced5 1475 _bfd_error_handler
695344c0 1476 /* xgettext:c-format */
9793eb77 1477 (_("%pB: failed to find link section for section %d"), obfd, secnum);
5522f910
NC
1478 }
1479
1480 if (iheader->sh_info)
1481 {
1482 /* The sh_info field can hold arbitrary information, but if the
1483 SHF_LINK_INFO flag is set then it should be interpreted as a
1484 section index. */
1485 if (iheader->sh_flags & SHF_INFO_LINK)
1486 {
1487 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1488 iheader->sh_info);
1489 if (sh_link != SHN_UNDEF)
1490 oheader->sh_flags |= SHF_INFO_LINK;
1491 }
1492 else
1493 /* No idea what it means - just copy it. */
1494 sh_link = iheader->sh_info;
1495
1496 if (sh_link != SHN_UNDEF)
1497 {
1498 oheader->sh_info = sh_link;
1499 changed = TRUE;
1500 }
1501 else
76cfced5 1502 _bfd_error_handler
695344c0 1503 /* xgettext:c-format */
9793eb77 1504 (_("%pB: failed to find info section for section %d"), obfd, secnum);
5522f910
NC
1505 }
1506
1507 return changed;
1508}
07d6d2b8 1509
0ac4564e
L
1510/* Copy the program header and other data from one object module to
1511 another. */
252b5132 1512
b34976b6 1513bfd_boolean
217aa764 1514_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
2d502050 1515{
5522f910
NC
1516 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1517 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1518 const struct elf_backend_data *bed;
84865015
NC
1519 unsigned int i;
1520
2d502050 1521 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
84865015 1522 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 1523 return TRUE;
2d502050 1524
57b828ef
L
1525 if (!elf_flags_init (obfd))
1526 {
1527 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1528 elf_flags_init (obfd) = TRUE;
1529 }
2d502050 1530
0ac4564e 1531 elf_gp (obfd) = elf_gp (ibfd);
57b828ef
L
1532
1533 /* Also copy the EI_OSABI field. */
1534 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1535 elf_elfheader (ibfd)->e_ident[EI_OSABI];
104d59d1 1536
5522f910
NC
1537 /* If set, copy the EI_ABIVERSION field. */
1538 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1539 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1540 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
07d6d2b8 1541
104d59d1
JM
1542 /* Copy object attributes. */
1543 _bfd_elf_copy_obj_attributes (ibfd, obfd);
63b9bbb7 1544
84865015
NC
1545 if (iheaders == NULL || oheaders == NULL)
1546 return TRUE;
63b9bbb7 1547
5522f910
NC
1548 bed = get_elf_backend_data (obfd);
1549
1550 /* Possibly copy other fields in the section header. */
84865015 1551 for (i = 1; i < elf_numsections (obfd); i++)
63b9bbb7 1552 {
84865015
NC
1553 unsigned int j;
1554 Elf_Internal_Shdr * oheader = oheaders[i];
63b9bbb7 1555
5522f910
NC
1556 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1557 because of a special case need for generating separate debug info
1558 files. See below for more details. */
84865015
NC
1559 if (oheader == NULL
1560 || (oheader->sh_type != SHT_NOBITS
5522f910
NC
1561 && oheader->sh_type < SHT_LOOS))
1562 continue;
1563
1564 /* Ignore empty sections, and sections whose
1565 fields have already been initialised. */
1566 if (oheader->sh_size == 0
84865015
NC
1567 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1568 continue;
63b9bbb7 1569
84865015 1570 /* Scan for the matching section in the input bfd.
5522f910
NC
1571 First we try for a direct mapping between the input and output sections. */
1572 for (j = 1; j < elf_numsections (ibfd); j++)
1573 {
1574 const Elf_Internal_Shdr * iheader = iheaders[j];
1575
1576 if (iheader == NULL)
1577 continue;
1578
1579 if (oheader->bfd_section != NULL
1580 && iheader->bfd_section != NULL
1581 && iheader->bfd_section->output_section != NULL
1582 && iheader->bfd_section->output_section == oheader->bfd_section)
1583 {
1584 /* We have found a connection from the input section to the
1585 output section. Attempt to copy the header fields. If
1586 this fails then do not try any further sections - there
1587 should only be a one-to-one mapping between input and output. */
1588 if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1589 j = elf_numsections (ibfd);
1590 break;
1591 }
1592 }
1593
1594 if (j < elf_numsections (ibfd))
1595 continue;
1596
1597 /* That failed. So try to deduce the corresponding input section.
84865015
NC
1598 Unfortunately we cannot compare names as the output string table
1599 is empty, so instead we check size, address and type. */
1600 for (j = 1; j < elf_numsections (ibfd); j++)
1601 {
5522f910 1602 const Elf_Internal_Shdr * iheader = iheaders[j];
84865015 1603
5522f910
NC
1604 if (iheader == NULL)
1605 continue;
1606
1607 /* Try matching fields in the input section's header.
1608 Since --only-keep-debug turns all non-debug sections into
84865015
NC
1609 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1610 input type. */
1611 if ((oheader->sh_type == SHT_NOBITS
1612 || iheader->sh_type == oheader->sh_type)
5522f910
NC
1613 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1614 == (oheader->sh_flags & ~ SHF_INFO_LINK)
84865015
NC
1615 && iheader->sh_addralign == oheader->sh_addralign
1616 && iheader->sh_entsize == oheader->sh_entsize
1617 && iheader->sh_size == oheader->sh_size
1618 && iheader->sh_addr == oheader->sh_addr
1619 && (iheader->sh_info != oheader->sh_info
1620 || iheader->sh_link != oheader->sh_link))
63b9bbb7 1621 {
5522f910
NC
1622 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1623 break;
63b9bbb7
NC
1624 }
1625 }
5522f910
NC
1626
1627 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1628 {
1629 /* Final attempt. Call the backend copy function
1630 with a NULL input section. */
1631 if (bed->elf_backend_copy_special_section_fields != NULL)
1632 bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader);
1633 }
63b9bbb7
NC
1634 }
1635
b34976b6 1636 return TRUE;
2d502050
L
1637}
1638
cedc298e
L
1639static const char *
1640get_segment_type (unsigned int p_type)
1641{
1642 const char *pt;
1643 switch (p_type)
1644 {
1645 case PT_NULL: pt = "NULL"; break;
1646 case PT_LOAD: pt = "LOAD"; break;
1647 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1648 case PT_INTERP: pt = "INTERP"; break;
1649 case PT_NOTE: pt = "NOTE"; break;
1650 case PT_SHLIB: pt = "SHLIB"; break;
1651 case PT_PHDR: pt = "PHDR"; break;
1652 case PT_TLS: pt = "TLS"; break;
1653 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
2b05f1b7 1654 case PT_GNU_STACK: pt = "STACK"; break;
cedc298e
L
1655 case PT_GNU_RELRO: pt = "RELRO"; break;
1656 default: pt = NULL; break;
1657 }
1658 return pt;
1659}
1660
f0b79d91
L
1661/* Print out the program headers. */
1662
b34976b6 1663bfd_boolean
217aa764 1664_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
252b5132 1665{
a50b1753 1666 FILE *f = (FILE *) farg;
252b5132
RH
1667 Elf_Internal_Phdr *p;
1668 asection *s;
1669 bfd_byte *dynbuf = NULL;
1670
1671 p = elf_tdata (abfd)->phdr;
1672 if (p != NULL)
1673 {
1674 unsigned int i, c;
1675
1676 fprintf (f, _("\nProgram Header:\n"));
1677 c = elf_elfheader (abfd)->e_phnum;
1678 for (i = 0; i < c; i++, p++)
1679 {
cedc298e 1680 const char *pt = get_segment_type (p->p_type);
252b5132
RH
1681 char buf[20];
1682
cedc298e 1683 if (pt == NULL)
252b5132 1684 {
cedc298e
L
1685 sprintf (buf, "0x%lx", p->p_type);
1686 pt = buf;
252b5132 1687 }
dc810e39 1688 fprintf (f, "%8s off 0x", pt);
60b89a18 1689 bfd_fprintf_vma (abfd, f, p->p_offset);
252b5132 1690 fprintf (f, " vaddr 0x");
60b89a18 1691 bfd_fprintf_vma (abfd, f, p->p_vaddr);
252b5132 1692 fprintf (f, " paddr 0x");
60b89a18 1693 bfd_fprintf_vma (abfd, f, p->p_paddr);
252b5132
RH
1694 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1695 fprintf (f, " filesz 0x");
60b89a18 1696 bfd_fprintf_vma (abfd, f, p->p_filesz);
252b5132 1697 fprintf (f, " memsz 0x");
60b89a18 1698 bfd_fprintf_vma (abfd, f, p->p_memsz);
252b5132
RH
1699 fprintf (f, " flags %c%c%c",
1700 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1701 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1702 (p->p_flags & PF_X) != 0 ? 'x' : '-');
dc810e39
AM
1703 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1704 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
252b5132
RH
1705 fprintf (f, "\n");
1706 }
1707 }
1708
1709 s = bfd_get_section_by_name (abfd, ".dynamic");
1710 if (s != NULL)
1711 {
cb33740c 1712 unsigned int elfsec;
dc810e39 1713 unsigned long shlink;
252b5132
RH
1714 bfd_byte *extdyn, *extdynend;
1715 size_t extdynsize;
217aa764 1716 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
252b5132
RH
1717
1718 fprintf (f, _("\nDynamic Section:\n"));
1719
eea6121a 1720 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
252b5132
RH
1721 goto error_return;
1722
1723 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 1724 if (elfsec == SHN_BAD)
252b5132 1725 goto error_return;
dc810e39 1726 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
252b5132
RH
1727
1728 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1729 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1730
1731 extdyn = dynbuf;
06614111
NC
1732 /* PR 17512: file: 6f427532. */
1733 if (s->size < extdynsize)
1734 goto error_return;
eea6121a 1735 extdynend = extdyn + s->size;
1036838a 1736 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
07d6d2b8 1737 Fix range check. */
1036838a 1738 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
252b5132
RH
1739 {
1740 Elf_Internal_Dyn dyn;
ad9563d6 1741 const char *name = "";
252b5132 1742 char ab[20];
b34976b6 1743 bfd_boolean stringp;
ad9563d6 1744 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 1745
217aa764 1746 (*swap_dyn_in) (abfd, extdyn, &dyn);
252b5132
RH
1747
1748 if (dyn.d_tag == DT_NULL)
1749 break;
1750
b34976b6 1751 stringp = FALSE;
252b5132
RH
1752 switch (dyn.d_tag)
1753 {
1754 default:
ad9563d6
CM
1755 if (bed->elf_backend_get_target_dtag)
1756 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1757
1758 if (!strcmp (name, ""))
1759 {
cd9af601 1760 sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag);
ad9563d6
CM
1761 name = ab;
1762 }
252b5132
RH
1763 break;
1764
b34976b6 1765 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
252b5132
RH
1766 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1767 case DT_PLTGOT: name = "PLTGOT"; break;
1768 case DT_HASH: name = "HASH"; break;
1769 case DT_STRTAB: name = "STRTAB"; break;
1770 case DT_SYMTAB: name = "SYMTAB"; break;
1771 case DT_RELA: name = "RELA"; break;
1772 case DT_RELASZ: name = "RELASZ"; break;
1773 case DT_RELAENT: name = "RELAENT"; break;
1774 case DT_STRSZ: name = "STRSZ"; break;
1775 case DT_SYMENT: name = "SYMENT"; break;
1776 case DT_INIT: name = "INIT"; break;
1777 case DT_FINI: name = "FINI"; break;
b34976b6
AM
1778 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1779 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
252b5132
RH
1780 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1781 case DT_REL: name = "REL"; break;
1782 case DT_RELSZ: name = "RELSZ"; break;
1783 case DT_RELENT: name = "RELENT"; break;
1784 case DT_PLTREL: name = "PLTREL"; break;
1785 case DT_DEBUG: name = "DEBUG"; break;
1786 case DT_TEXTREL: name = "TEXTREL"; break;
1787 case DT_JMPREL: name = "JMPREL"; break;
94558834
L
1788 case DT_BIND_NOW: name = "BIND_NOW"; break;
1789 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1790 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1791 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1792 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
b34976b6 1793 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
94558834
L
1794 case DT_FLAGS: name = "FLAGS"; break;
1795 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1796 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
d48188b9 1797 case DT_CHECKSUM: name = "CHECKSUM"; break;
94558834
L
1798 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1799 case DT_MOVEENT: name = "MOVEENT"; break;
1800 case DT_MOVESZ: name = "MOVESZ"; break;
1801 case DT_FEATURE: name = "FEATURE"; break;
1802 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1803 case DT_SYMINSZ: name = "SYMINSZ"; break;
1804 case DT_SYMINENT: name = "SYMINENT"; break;
b34976b6
AM
1805 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1806 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1807 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
94558834
L
1808 case DT_PLTPAD: name = "PLTPAD"; break;
1809 case DT_MOVETAB: name = "MOVETAB"; break;
1810 case DT_SYMINFO: name = "SYMINFO"; break;
1811 case DT_RELACOUNT: name = "RELACOUNT"; break;
1812 case DT_RELCOUNT: name = "RELCOUNT"; break;
1813 case DT_FLAGS_1: name = "FLAGS_1"; break;
252b5132
RH
1814 case DT_VERSYM: name = "VERSYM"; break;
1815 case DT_VERDEF: name = "VERDEF"; break;
1816 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1817 case DT_VERNEED: name = "VERNEED"; break;
1818 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
b34976b6 1819 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
94558834 1820 case DT_USED: name = "USED"; break;
b34976b6 1821 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
fdc90cb4 1822 case DT_GNU_HASH: name = "GNU_HASH"; break;
252b5132
RH
1823 }
1824
ad9563d6 1825 fprintf (f, " %-20s ", name);
252b5132 1826 if (! stringp)
a1f3c56e
AN
1827 {
1828 fprintf (f, "0x");
1829 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1830 }
252b5132
RH
1831 else
1832 {
1833 const char *string;
dc810e39 1834 unsigned int tagv = dyn.d_un.d_val;
252b5132 1835
dc810e39 1836 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
252b5132
RH
1837 if (string == NULL)
1838 goto error_return;
1839 fprintf (f, "%s", string);
1840 }
1841 fprintf (f, "\n");
1842 }
1843
1844 free (dynbuf);
1845 dynbuf = NULL;
1846 }
1847
1848 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1849 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1850 {
fc0e6df6 1851 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
b34976b6 1852 return FALSE;
252b5132
RH
1853 }
1854
1855 if (elf_dynverdef (abfd) != 0)
1856 {
1857 Elf_Internal_Verdef *t;
1858
1859 fprintf (f, _("\nVersion definitions:\n"));
1860 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1861 {
1862 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
d0fb9a8d
JJ
1863 t->vd_flags, t->vd_hash,
1864 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1865 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
252b5132
RH
1866 {
1867 Elf_Internal_Verdaux *a;
1868
1869 fprintf (f, "\t");
1870 for (a = t->vd_auxptr->vda_nextptr;
1871 a != NULL;
1872 a = a->vda_nextptr)
d0fb9a8d
JJ
1873 fprintf (f, "%s ",
1874 a->vda_nodename ? a->vda_nodename : "<corrupt>");
252b5132
RH
1875 fprintf (f, "\n");
1876 }
1877 }
1878 }
1879
1880 if (elf_dynverref (abfd) != 0)
1881 {
1882 Elf_Internal_Verneed *t;
1883
1884 fprintf (f, _("\nVersion References:\n"));
1885 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1886 {
1887 Elf_Internal_Vernaux *a;
1888
d0fb9a8d
JJ
1889 fprintf (f, _(" required from %s:\n"),
1890 t->vn_filename ? t->vn_filename : "<corrupt>");
252b5132
RH
1891 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1892 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
d0fb9a8d
JJ
1893 a->vna_flags, a->vna_other,
1894 a->vna_nodename ? a->vna_nodename : "<corrupt>");
252b5132
RH
1895 }
1896 }
1897
b34976b6 1898 return TRUE;
252b5132
RH
1899
1900 error_return:
1901 if (dynbuf != NULL)
1902 free (dynbuf);
b34976b6 1903 return FALSE;
252b5132
RH
1904}
1905
bb4d2ac2
L
1906/* Get version string. */
1907
1908const char *
60bb06bc
L
1909_bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1910 bfd_boolean *hidden)
bb4d2ac2
L
1911{
1912 const char *version_string = NULL;
1913 if (elf_dynversym (abfd) != 0
1914 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1915 {
1916 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1917
1918 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1919 vernum &= VERSYM_VERSION;
1920
1921 if (vernum == 0)
1922 version_string = "";
1f6f5dba
L
1923 else if (vernum == 1
1924 && (vernum > elf_tdata (abfd)->cverdefs
1925 || (elf_tdata (abfd)->verdef[0].vd_flags
1926 == VER_FLG_BASE)))
bb4d2ac2
L
1927 version_string = "Base";
1928 else if (vernum <= elf_tdata (abfd)->cverdefs)
1929 version_string =
1930 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1931 else
1932 {
1933 Elf_Internal_Verneed *t;
1934
7a815dd5 1935 version_string = _("<corrupt>");
bb4d2ac2
L
1936 for (t = elf_tdata (abfd)->verref;
1937 t != NULL;
1938 t = t->vn_nextref)
1939 {
1940 Elf_Internal_Vernaux *a;
1941
1942 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1943 {
1944 if (a->vna_other == vernum)
1945 {
1946 version_string = a->vna_nodename;
1947 break;
1948 }
1949 }
1950 }
1951 }
1952 }
1953 return version_string;
1954}
1955
252b5132
RH
1956/* Display ELF-specific fields of a symbol. */
1957
1958void
217aa764
AM
1959bfd_elf_print_symbol (bfd *abfd,
1960 void *filep,
1961 asymbol *symbol,
1962 bfd_print_symbol_type how)
252b5132 1963{
a50b1753 1964 FILE *file = (FILE *) filep;
252b5132
RH
1965 switch (how)
1966 {
1967 case bfd_print_symbol_name:
1968 fprintf (file, "%s", symbol->name);
1969 break;
1970 case bfd_print_symbol_more:
1971 fprintf (file, "elf ");
60b89a18 1972 bfd_fprintf_vma (abfd, file, symbol->value);
cd9af601 1973 fprintf (file, " %x", symbol->flags);
252b5132
RH
1974 break;
1975 case bfd_print_symbol_all:
1976 {
4e8a9624
AM
1977 const char *section_name;
1978 const char *name = NULL;
9c5bfbb7 1979 const struct elf_backend_data *bed;
7a13edea 1980 unsigned char st_other;
dbb410c3 1981 bfd_vma val;
bb4d2ac2
L
1982 const char *version_string;
1983 bfd_boolean hidden;
c044fabd 1984
252b5132 1985 section_name = symbol->section ? symbol->section->name : "(*none*)";
587ff49e
RH
1986
1987 bed = get_elf_backend_data (abfd);
1988 if (bed->elf_backend_print_symbol_all)
c044fabd 1989 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
587ff49e
RH
1990
1991 if (name == NULL)
1992 {
7ee38065 1993 name = symbol->name;
217aa764 1994 bfd_print_symbol_vandf (abfd, file, symbol);
587ff49e
RH
1995 }
1996
252b5132
RH
1997 fprintf (file, " %s\t", section_name);
1998 /* Print the "other" value for a symbol. For common symbols,
1999 we've already printed the size; now print the alignment.
2000 For other symbols, we have no specified alignment, and
2001 we've printed the address; now print the size. */
dcf6c779 2002 if (symbol->section && bfd_is_com_section (symbol->section))
dbb410c3
AM
2003 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
2004 else
2005 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
2006 bfd_fprintf_vma (abfd, file, val);
252b5132
RH
2007
2008 /* If we have version information, print it. */
60bb06bc
L
2009 version_string = _bfd_elf_get_symbol_version_string (abfd,
2010 symbol,
2011 &hidden);
bb4d2ac2 2012 if (version_string)
252b5132 2013 {
bb4d2ac2 2014 if (!hidden)
252b5132
RH
2015 fprintf (file, " %-11s", version_string);
2016 else
2017 {
2018 int i;
2019
2020 fprintf (file, " (%s)", version_string);
2021 for (i = 10 - strlen (version_string); i > 0; --i)
2022 putc (' ', file);
2023 }
2024 }
2025
2026 /* If the st_other field is not zero, print it. */
7a13edea 2027 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
c044fabd 2028
7a13edea
NC
2029 switch (st_other)
2030 {
2031 case 0: break;
2032 case STV_INTERNAL: fprintf (file, " .internal"); break;
2033 case STV_HIDDEN: fprintf (file, " .hidden"); break;
2034 case STV_PROTECTED: fprintf (file, " .protected"); break;
2035 default:
2036 /* Some other non-defined flags are also present, so print
2037 everything hex. */
2038 fprintf (file, " 0x%02x", (unsigned int) st_other);
2039 }
252b5132 2040
587ff49e 2041 fprintf (file, " %s", name);
252b5132
RH
2042 }
2043 break;
2044 }
2045}
252b5132
RH
2046\f
2047/* ELF .o/exec file reading */
2048
c044fabd 2049/* Create a new bfd section from an ELF section header. */
252b5132 2050
b34976b6 2051bfd_boolean
217aa764 2052bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
252b5132 2053{
4fbb74a6
AM
2054 Elf_Internal_Shdr *hdr;
2055 Elf_Internal_Ehdr *ehdr;
2056 const struct elf_backend_data *bed;
90937f86 2057 const char *name;
bf67003b
NC
2058 bfd_boolean ret = TRUE;
2059 static bfd_boolean * sections_being_created = NULL;
5a4b0ccc 2060 static bfd * sections_being_created_abfd = NULL;
bf67003b 2061 static unsigned int nesting = 0;
252b5132 2062
4fbb74a6
AM
2063 if (shindex >= elf_numsections (abfd))
2064 return FALSE;
2065
bf67003b
NC
2066 if (++ nesting > 3)
2067 {
2068 /* PR17512: A corrupt ELF binary might contain a recursive group of
67ce483b 2069 sections, with each the string indices pointing to the next in the
bf67003b
NC
2070 loop. Detect this here, by refusing to load a section that we are
2071 already in the process of loading. We only trigger this test if
2072 we have nested at least three sections deep as normal ELF binaries
5a4b0ccc
NC
2073 can expect to recurse at least once.
2074
2075 FIXME: It would be better if this array was attached to the bfd,
2076 rather than being held in a static pointer. */
2077
2078 if (sections_being_created_abfd != abfd)
2079 sections_being_created = NULL;
bf67003b
NC
2080 if (sections_being_created == NULL)
2081 {
bf67003b 2082 sections_being_created = (bfd_boolean *)
7a6e0d89 2083 bfd_zalloc2 (abfd, elf_numsections (abfd), sizeof (bfd_boolean));
5a4b0ccc 2084 sections_being_created_abfd = abfd;
bf67003b
NC
2085 }
2086 if (sections_being_created [shindex])
2087 {
4eca0228 2088 _bfd_error_handler
871b3ab2 2089 (_("%pB: warning: loop in section dependencies detected"), abfd);
bf67003b
NC
2090 return FALSE;
2091 }
2092 sections_being_created [shindex] = TRUE;
2093 }
2094
4fbb74a6
AM
2095 hdr = elf_elfsections (abfd)[shindex];
2096 ehdr = elf_elfheader (abfd);
2097 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1b3a8575 2098 hdr->sh_name);
933d961a 2099 if (name == NULL)
bf67003b 2100 goto fail;
252b5132 2101
4fbb74a6 2102 bed = get_elf_backend_data (abfd);
252b5132
RH
2103 switch (hdr->sh_type)
2104 {
2105 case SHT_NULL:
2106 /* Inactive section. Throw it away. */
bf67003b 2107 goto success;
252b5132 2108
bf67003b
NC
2109 case SHT_PROGBITS: /* Normal section with contents. */
2110 case SHT_NOBITS: /* .bss section. */
2111 case SHT_HASH: /* .hash section. */
2112 case SHT_NOTE: /* .note section. */
25e27870
L
2113 case SHT_INIT_ARRAY: /* .init_array section. */
2114 case SHT_FINI_ARRAY: /* .fini_array section. */
2115 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
7f1204bb 2116 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
fdc90cb4 2117 case SHT_GNU_HASH: /* .gnu.hash section. */
bf67003b
NC
2118 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2119 goto success;
252b5132 2120
797fc050 2121 case SHT_DYNAMIC: /* Dynamic linking information. */
6dc132d9 2122 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b
NC
2123 goto fail;
2124
cfcac11d
NC
2125 if (hdr->sh_link > elf_numsections (abfd))
2126 {
caa83f8b 2127 /* PR 10478: Accept Solaris binaries with a sh_link
cfcac11d
NC
2128 field set to SHN_BEFORE or SHN_AFTER. */
2129 switch (bfd_get_arch (abfd))
2130 {
caa83f8b 2131 case bfd_arch_i386:
cfcac11d
NC
2132 case bfd_arch_sparc:
2133 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
2134 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
2135 break;
2136 /* Otherwise fall through. */
2137 default:
bf67003b 2138 goto fail;
cfcac11d
NC
2139 }
2140 }
2141 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
bf67003b 2142 goto fail;
cfcac11d 2143 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
797fc050
AM
2144 {
2145 Elf_Internal_Shdr *dynsymhdr;
2146
2147 /* The shared libraries distributed with hpux11 have a bogus
2148 sh_link field for the ".dynamic" section. Find the
2149 string table for the ".dynsym" section instead. */
2150 if (elf_dynsymtab (abfd) != 0)
2151 {
2152 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2153 hdr->sh_link = dynsymhdr->sh_link;
2154 }
2155 else
2156 {
2157 unsigned int i, num_sec;
2158
2159 num_sec = elf_numsections (abfd);
2160 for (i = 1; i < num_sec; i++)
2161 {
2162 dynsymhdr = elf_elfsections (abfd)[i];
2163 if (dynsymhdr->sh_type == SHT_DYNSYM)
2164 {
2165 hdr->sh_link = dynsymhdr->sh_link;
2166 break;
2167 }
2168 }
2169 }
2170 }
bf67003b 2171 goto success;
797fc050 2172
bf67003b 2173 case SHT_SYMTAB: /* A symbol table. */
252b5132 2174 if (elf_onesymtab (abfd) == shindex)
bf67003b 2175 goto success;
252b5132 2176
a50b2160 2177 if (hdr->sh_entsize != bed->s->sizeof_sym)
bf67003b
NC
2178 goto fail;
2179
3337c1e5 2180 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
eee3b786
AM
2181 {
2182 if (hdr->sh_size != 0)
bf67003b 2183 goto fail;
eee3b786
AM
2184 /* Some assemblers erroneously set sh_info to one with a
2185 zero sh_size. ld sees this as a global symbol count
2186 of (unsigned) -1. Fix it here. */
2187 hdr->sh_info = 0;
bf67003b 2188 goto success;
eee3b786 2189 }
bf67003b 2190
16ad13ec
NC
2191 /* PR 18854: A binary might contain more than one symbol table.
2192 Unusual, but possible. Warn, but continue. */
2193 if (elf_onesymtab (abfd) != 0)
2194 {
4eca0228 2195 _bfd_error_handler
695344c0 2196 /* xgettext:c-format */
871b3ab2 2197 (_("%pB: warning: multiple symbol tables detected"
63a5468a 2198 " - ignoring the table in section %u"),
16ad13ec
NC
2199 abfd, shindex);
2200 goto success;
2201 }
252b5132 2202 elf_onesymtab (abfd) = shindex;
6a40cf0c
NC
2203 elf_symtab_hdr (abfd) = *hdr;
2204 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
252b5132
RH
2205 abfd->flags |= HAS_SYMS;
2206
2207 /* Sometimes a shared object will map in the symbol table. If
08a40648
AM
2208 SHF_ALLOC is set, and this is a shared object, then we also
2209 treat this section as a BFD section. We can not base the
2210 decision purely on SHF_ALLOC, because that flag is sometimes
2211 set in a relocatable object file, which would confuse the
2212 linker. */
252b5132
RH
2213 if ((hdr->sh_flags & SHF_ALLOC) != 0
2214 && (abfd->flags & DYNAMIC) != 0
6dc132d9
L
2215 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2216 shindex))
bf67003b 2217 goto fail;
252b5132 2218
1b3a8575
AM
2219 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2220 can't read symbols without that section loaded as well. It
2221 is most likely specified by the next section header. */
6a40cf0c
NC
2222 {
2223 elf_section_list * entry;
2224 unsigned int i, num_sec;
1b3a8575 2225
6a40cf0c
NC
2226 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2227 if (entry->hdr.sh_link == shindex)
2228 goto success;
2229
2230 num_sec = elf_numsections (abfd);
2231 for (i = shindex + 1; i < num_sec; i++)
2232 {
2233 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2234
2235 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2236 && hdr2->sh_link == shindex)
2237 break;
2238 }
2239
2240 if (i == num_sec)
2241 for (i = 1; i < shindex; i++)
1b3a8575
AM
2242 {
2243 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
6a40cf0c 2244
1b3a8575
AM
2245 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2246 && hdr2->sh_link == shindex)
2247 break;
2248 }
6a40cf0c
NC
2249
2250 if (i != shindex)
2251 ret = bfd_section_from_shdr (abfd, i);
2252 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2253 goto success;
2254 }
252b5132 2255
bf67003b 2256 case SHT_DYNSYM: /* A dynamic symbol table. */
252b5132 2257 if (elf_dynsymtab (abfd) == shindex)
bf67003b 2258 goto success;
252b5132 2259
a50b2160 2260 if (hdr->sh_entsize != bed->s->sizeof_sym)
bf67003b
NC
2261 goto fail;
2262
eee3b786
AM
2263 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2264 {
2265 if (hdr->sh_size != 0)
bf67003b
NC
2266 goto fail;
2267
eee3b786
AM
2268 /* Some linkers erroneously set sh_info to one with a
2269 zero sh_size. ld sees this as a global symbol count
2270 of (unsigned) -1. Fix it here. */
2271 hdr->sh_info = 0;
bf67003b 2272 goto success;
eee3b786 2273 }
bf67003b 2274
16ad13ec
NC
2275 /* PR 18854: A binary might contain more than one dynamic symbol table.
2276 Unusual, but possible. Warn, but continue. */
2277 if (elf_dynsymtab (abfd) != 0)
2278 {
4eca0228 2279 _bfd_error_handler
695344c0 2280 /* xgettext:c-format */
871b3ab2 2281 (_("%pB: warning: multiple dynamic symbol tables detected"
63a5468a 2282 " - ignoring the table in section %u"),
16ad13ec
NC
2283 abfd, shindex);
2284 goto success;
2285 }
252b5132
RH
2286 elf_dynsymtab (abfd) = shindex;
2287 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2288 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2289 abfd->flags |= HAS_SYMS;
2290
2291 /* Besides being a symbol table, we also treat this as a regular
2292 section, so that objcopy can handle it. */
bf67003b
NC
2293 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2294 goto success;
252b5132 2295
bf67003b 2296 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
6a40cf0c
NC
2297 {
2298 elf_section_list * entry;
9ad5cbcf 2299
6a40cf0c
NC
2300 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2301 if (entry->ndx == shindex)
2302 goto success;
07d6d2b8 2303
7a6e0d89 2304 entry = bfd_alloc (abfd, sizeof (*entry));
6a40cf0c
NC
2305 if (entry == NULL)
2306 goto fail;
2307 entry->ndx = shindex;
2308 entry->hdr = * hdr;
2309 entry->next = elf_symtab_shndx_list (abfd);
2310 elf_symtab_shndx_list (abfd) = entry;
2311 elf_elfsections (abfd)[shindex] = & entry->hdr;
2312 goto success;
2313 }
9ad5cbcf 2314
bf67003b 2315 case SHT_STRTAB: /* A string table. */
252b5132 2316 if (hdr->bfd_section != NULL)
bf67003b
NC
2317 goto success;
2318
252b5132
RH
2319 if (ehdr->e_shstrndx == shindex)
2320 {
2321 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2322 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
bf67003b 2323 goto success;
252b5132 2324 }
bf67003b 2325
1b3a8575
AM
2326 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2327 {
2328 symtab_strtab:
2329 elf_tdata (abfd)->strtab_hdr = *hdr;
2330 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
bf67003b 2331 goto success;
1b3a8575 2332 }
bf67003b 2333
1b3a8575
AM
2334 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2335 {
2336 dynsymtab_strtab:
2337 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2338 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2339 elf_elfsections (abfd)[shindex] = hdr;
2340 /* We also treat this as a regular section, so that objcopy
2341 can handle it. */
bf67003b
NC
2342 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2343 shindex);
2344 goto success;
1b3a8575 2345 }
252b5132 2346
1b3a8575
AM
2347 /* If the string table isn't one of the above, then treat it as a
2348 regular section. We need to scan all the headers to be sure,
2349 just in case this strtab section appeared before the above. */
2350 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2351 {
2352 unsigned int i, num_sec;
252b5132 2353
1b3a8575
AM
2354 num_sec = elf_numsections (abfd);
2355 for (i = 1; i < num_sec; i++)
2356 {
2357 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2358 if (hdr2->sh_link == shindex)
2359 {
933d961a
JJ
2360 /* Prevent endless recursion on broken objects. */
2361 if (i == shindex)
bf67003b 2362 goto fail;
1b3a8575 2363 if (! bfd_section_from_shdr (abfd, i))
bf67003b 2364 goto fail;
1b3a8575
AM
2365 if (elf_onesymtab (abfd) == i)
2366 goto symtab_strtab;
2367 if (elf_dynsymtab (abfd) == i)
2368 goto dynsymtab_strtab;
2369 }
2370 }
2371 }
bf67003b
NC
2372 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2373 goto success;
252b5132
RH
2374
2375 case SHT_REL:
2376 case SHT_RELA:
2377 /* *These* do a lot of work -- but build no sections! */
2378 {
2379 asection *target_sect;
d4730f92 2380 Elf_Internal_Shdr *hdr2, **p_hdr;
9ad5cbcf 2381 unsigned int num_sec = elf_numsections (abfd);
d4730f92 2382 struct bfd_elf_section_data *esdt;
252b5132 2383
aa2ca951
JJ
2384 if (hdr->sh_entsize
2385 != (bfd_size_type) (hdr->sh_type == SHT_REL
a50b2160 2386 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
bf67003b 2387 goto fail;
a50b2160 2388
03ae5f59 2389 /* Check for a bogus link to avoid crashing. */
4fbb74a6 2390 if (hdr->sh_link >= num_sec)
03ae5f59 2391 {
4eca0228 2392 _bfd_error_handler
695344c0 2393 /* xgettext:c-format */
871b3ab2 2394 (_("%pB: invalid link %u for reloc section %s (index %u)"),
4eca0228 2395 abfd, hdr->sh_link, name, shindex);
bf67003b
NC
2396 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2397 shindex);
2398 goto success;
03ae5f59
ILT
2399 }
2400
252b5132
RH
2401 /* For some incomprehensible reason Oracle distributes
2402 libraries for Solaris in which some of the objects have
2403 bogus sh_link fields. It would be nice if we could just
2404 reject them, but, unfortunately, some people need to use
2405 them. We scan through the section headers; if we find only
2406 one suitable symbol table, we clobber the sh_link to point
83b89087
L
2407 to it. I hope this doesn't break anything.
2408
2409 Don't do it on executable nor shared library. */
2410 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
2411 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
252b5132
RH
2412 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
2413 {
9ad5cbcf 2414 unsigned int scan;
252b5132
RH
2415 int found;
2416
2417 found = 0;
9ad5cbcf 2418 for (scan = 1; scan < num_sec; scan++)
252b5132
RH
2419 {
2420 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
2421 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
2422 {
2423 if (found != 0)
2424 {
2425 found = 0;
2426 break;
2427 }
2428 found = scan;
2429 }
2430 }
2431 if (found != 0)
2432 hdr->sh_link = found;
2433 }
2434
2435 /* Get the symbol table. */
1b3a8575
AM
2436 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2437 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
252b5132 2438 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
bf67003b 2439 goto fail;
252b5132 2440
a4bcd733
AM
2441 /* If this is an alloc section in an executable or shared
2442 library, or the reloc section does not use the main symbol
2443 table we don't treat it as a reloc section. BFD can't
2444 adequately represent such a section, so at least for now,
2445 we don't try. We just present it as a normal section. We
2446 also can't use it as a reloc section if it points to the
2447 null section, an invalid section, another reloc section, or
2448 its sh_link points to the null section. */
2449 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2450 && (hdr->sh_flags & SHF_ALLOC) != 0)
83b89087 2451 || hdr->sh_link == SHN_UNDEF
a4bcd733 2452 || hdr->sh_link != elf_onesymtab (abfd)
185ef66d 2453 || hdr->sh_info == SHN_UNDEF
185ef66d
AM
2454 || hdr->sh_info >= num_sec
2455 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2456 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
bf67003b
NC
2457 {
2458 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2459 shindex);
2460 goto success;
2461 }
252b5132
RH
2462
2463 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
bf67003b
NC
2464 goto fail;
2465
252b5132
RH
2466 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2467 if (target_sect == NULL)
bf67003b 2468 goto fail;
252b5132 2469
d4730f92
BS
2470 esdt = elf_section_data (target_sect);
2471 if (hdr->sh_type == SHT_RELA)
2472 p_hdr = &esdt->rela.hdr;
252b5132 2473 else
d4730f92
BS
2474 p_hdr = &esdt->rel.hdr;
2475
a7ba3896
NC
2476 /* PR 17512: file: 0b4f81b7.
2477 Also see PR 24456, for a file which deliberately has two reloc
2478 sections. */
06614111 2479 if (*p_hdr != NULL)
a7ba3896
NC
2480 {
2481 _bfd_error_handler
2482 /* xgettext:c-format */
2483 (_("%pB: warning: multiple relocation sections for section %pA \
2484found - ignoring all but the first"),
2485 abfd, target_sect);
2486 goto success;
2487 }
ef53be89 2488 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
d4730f92 2489 if (hdr2 == NULL)
bf67003b 2490 goto fail;
252b5132 2491 *hdr2 = *hdr;
d4730f92 2492 *p_hdr = hdr2;
252b5132 2493 elf_elfsections (abfd)[shindex] = hdr2;
056bafd4
MR
2494 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2495 * bed->s->int_rels_per_ext_rel);
252b5132
RH
2496 target_sect->flags |= SEC_RELOC;
2497 target_sect->relocation = NULL;
2498 target_sect->rel_filepos = hdr->sh_offset;
bf572ba0
MM
2499 /* In the section to which the relocations apply, mark whether
2500 its relocations are of the REL or RELA variety. */
72730e0c 2501 if (hdr->sh_size != 0)
d4730f92
BS
2502 {
2503 if (hdr->sh_type == SHT_RELA)
2504 target_sect->use_rela_p = 1;
2505 }
252b5132 2506 abfd->flags |= HAS_RELOC;
bf67003b 2507 goto success;
252b5132 2508 }
252b5132
RH
2509
2510 case SHT_GNU_verdef:
2511 elf_dynverdef (abfd) = shindex;
2512 elf_tdata (abfd)->dynverdef_hdr = *hdr;
bf67003b
NC
2513 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2514 goto success;
252b5132
RH
2515
2516 case SHT_GNU_versym:
a50b2160 2517 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
bf67003b
NC
2518 goto fail;
2519
252b5132
RH
2520 elf_dynversym (abfd) = shindex;
2521 elf_tdata (abfd)->dynversym_hdr = *hdr;
bf67003b
NC
2522 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2523 goto success;
252b5132
RH
2524
2525 case SHT_GNU_verneed:
2526 elf_dynverref (abfd) = shindex;
2527 elf_tdata (abfd)->dynverref_hdr = *hdr;
bf67003b
NC
2528 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2529 goto success;
252b5132
RH
2530
2531 case SHT_SHLIB:
bf67003b 2532 goto success;
252b5132 2533
dbb410c3 2534 case SHT_GROUP:
44534af3 2535 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
bf67003b
NC
2536 goto fail;
2537
6dc132d9 2538 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b
NC
2539 goto fail;
2540
bf67003b 2541 goto success;
dbb410c3 2542
252b5132 2543 default:
104d59d1
JM
2544 /* Possibly an attributes section. */
2545 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2546 || hdr->sh_type == bed->obj_attrs_section_type)
2547 {
2548 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2549 goto fail;
104d59d1 2550 _bfd_elf_parse_attributes (abfd, hdr);
bf67003b 2551 goto success;
104d59d1
JM
2552 }
2553
252b5132 2554 /* Check for any processor-specific section types. */
3eb70a79 2555 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2556 goto success;
3eb70a79
L
2557
2558 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2559 {
2560 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2561 /* FIXME: How to properly handle allocated section reserved
2562 for applications? */
4eca0228 2563 _bfd_error_handler
695344c0 2564 /* xgettext:c-format */
871b3ab2 2565 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2566 abfd, hdr->sh_type, name);
3eb70a79 2567 else
bf67003b
NC
2568 {
2569 /* Allow sections reserved for applications. */
2570 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2571 shindex);
2572 goto success;
2573 }
3eb70a79
L
2574 }
2575 else if (hdr->sh_type >= SHT_LOPROC
2576 && hdr->sh_type <= SHT_HIPROC)
2577 /* FIXME: We should handle this section. */
4eca0228 2578 _bfd_error_handler
695344c0 2579 /* xgettext:c-format */
871b3ab2 2580 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2581 abfd, hdr->sh_type, name);
3eb70a79 2582 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
ff15b240
NC
2583 {
2584 /* Unrecognised OS-specific sections. */
2585 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2586 /* SHF_OS_NONCONFORMING indicates that special knowledge is
08a40648 2587 required to correctly process the section and the file should
ff15b240 2588 be rejected with an error message. */
4eca0228 2589 _bfd_error_handler
695344c0 2590 /* xgettext:c-format */
871b3ab2 2591 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2592 abfd, hdr->sh_type, name);
ff15b240 2593 else
bf67003b
NC
2594 {
2595 /* Otherwise it should be processed. */
2596 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2597 goto success;
2598 }
ff15b240 2599 }
3eb70a79
L
2600 else
2601 /* FIXME: We should handle this section. */
4eca0228 2602 _bfd_error_handler
695344c0 2603 /* xgettext:c-format */
871b3ab2 2604 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2605 abfd, hdr->sh_type, name);
3eb70a79 2606
bf67003b 2607 goto fail;
252b5132
RH
2608 }
2609
bf67003b
NC
2610 fail:
2611 ret = FALSE;
2612 success:
e5b470e2 2613 if (sections_being_created && sections_being_created_abfd == abfd)
bf67003b
NC
2614 sections_being_created [shindex] = FALSE;
2615 if (-- nesting == 0)
5a4b0ccc
NC
2616 {
2617 sections_being_created = NULL;
2618 sections_being_created_abfd = abfd;
2619 }
bf67003b 2620 return ret;
252b5132
RH
2621}
2622
87d72d41 2623/* Return the local symbol specified by ABFD, R_SYMNDX. */
ec338859 2624
87d72d41
AM
2625Elf_Internal_Sym *
2626bfd_sym_from_r_symndx (struct sym_cache *cache,
2627 bfd *abfd,
2628 unsigned long r_symndx)
ec338859 2629{
ec338859
AM
2630 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2631
a5d1b3b5
AM
2632 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2633 {
2634 Elf_Internal_Shdr *symtab_hdr;
2635 unsigned char esym[sizeof (Elf64_External_Sym)];
2636 Elf_External_Sym_Shndx eshndx;
ec338859 2637
a5d1b3b5
AM
2638 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2639 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
87d72d41 2640 &cache->sym[ent], esym, &eshndx) == NULL)
a5d1b3b5 2641 return NULL;
9ad5cbcf 2642
a5d1b3b5
AM
2643 if (cache->abfd != abfd)
2644 {
2645 memset (cache->indx, -1, sizeof (cache->indx));
2646 cache->abfd = abfd;
2647 }
2648 cache->indx[ent] = r_symndx;
ec338859 2649 }
a5d1b3b5 2650
87d72d41 2651 return &cache->sym[ent];
ec338859
AM
2652}
2653
252b5132
RH
2654/* Given an ELF section number, retrieve the corresponding BFD
2655 section. */
2656
2657asection *
91d6fa6a 2658bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
252b5132 2659{
91d6fa6a 2660 if (sec_index >= elf_numsections (abfd))
252b5132 2661 return NULL;
91d6fa6a 2662 return elf_elfsections (abfd)[sec_index]->bfd_section;
252b5132
RH
2663}
2664
b35d266b 2665static const struct bfd_elf_special_section special_sections_b[] =
2f89ff8d 2666{
0112cd26 2667 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2668 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2669};
2670
b35d266b 2671static const struct bfd_elf_special_section special_sections_c[] =
7f4d3958 2672{
0112cd26 2673 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
1ff6de03 2674 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2675 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2676};
2677
b35d266b 2678static const struct bfd_elf_special_section special_sections_d[] =
7f4d3958 2679{
07d6d2b8
AM
2680 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2681 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
a9a72a65
DE
2682 /* There are more DWARF sections than these, but they needn't be added here
2683 unless you have to cope with broken compilers that don't emit section
2684 attributes or you want to help the user writing assembler. */
07d6d2b8
AM
2685 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2686 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2687 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2688 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
0112cd26 2689 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2690 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2691 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2692 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2693 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2694};
2695
b35d266b 2696static const struct bfd_elf_special_section special_sections_f[] =
7f4d3958 2697{
07d6d2b8 2698 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2699 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2700 { NULL, 0 , 0, 0, 0 }
7f4d3958
L
2701};
2702
b35d266b 2703static const struct bfd_elf_special_section special_sections_g[] =
7f4d3958 2704{
0112cd26 2705 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2706 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2707 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2708 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
0112cd26
NC
2709 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2710 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
07d6d2b8
AM
2711 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2712 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2713 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2714 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2715};
2716
b35d266b 2717static const struct bfd_elf_special_section special_sections_h[] =
7f4d3958 2718{
07d6d2b8
AM
2719 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2720 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2721};
2722
b35d266b 2723static const struct bfd_elf_special_section special_sections_i[] =
7f4d3958 2724{
07d6d2b8 2725 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2726 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2727 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2728 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2729};
2730
b35d266b 2731static const struct bfd_elf_special_section special_sections_l[] =
7f4d3958 2732{
0112cd26 2733 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2734 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2735};
2736
b35d266b 2737static const struct bfd_elf_special_section special_sections_n[] =
7f4d3958 2738{
0112cd26 2739 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2740 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2741 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2742};
2743
b35d266b 2744static const struct bfd_elf_special_section special_sections_p[] =
7f4d3958 2745{
6f9dbcd4 2746 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2747 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2748 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2749};
2750
b35d266b 2751static const struct bfd_elf_special_section special_sections_r[] =
7f4d3958 2752{
0112cd26
NC
2753 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2754 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
07d6d2b8
AM
2755 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2756 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2757 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2758};
2759
b35d266b 2760static const struct bfd_elf_special_section special_sections_s[] =
7f4d3958 2761{
0112cd26
NC
2762 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2763 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2764 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
60ff4dc4
HPN
2765 /* See struct bfd_elf_special_section declaration for the semantics of
2766 this special case where .prefix_length != strlen (.prefix). */
2767 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
07d6d2b8 2768 { NULL, 0, 0, 0, 0 }
2f89ff8d
L
2769};
2770
b35d266b 2771static const struct bfd_elf_special_section special_sections_t[] =
7f4d3958 2772{
07d6d2b8
AM
2773 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2774 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
0112cd26 2775 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
07d6d2b8 2776 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2777};
2778
1b315056
CS
2779static const struct bfd_elf_special_section special_sections_z[] =
2780{
07d6d2b8
AM
2781 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2782 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
1b315056
CS
2783 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2784 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2785 { NULL, 0, 0, 0, 0 }
1b315056
CS
2786};
2787
e4c93b56 2788static const struct bfd_elf_special_section * const special_sections[] =
7f4d3958 2789{
7f4d3958 2790 special_sections_b, /* 'b' */
98ece1b3 2791 special_sections_c, /* 'c' */
7f4d3958
L
2792 special_sections_d, /* 'd' */
2793 NULL, /* 'e' */
2794 special_sections_f, /* 'f' */
2795 special_sections_g, /* 'g' */
2796 special_sections_h, /* 'h' */
2797 special_sections_i, /* 'i' */
2798 NULL, /* 'j' */
2799 NULL, /* 'k' */
2800 special_sections_l, /* 'l' */
2801 NULL, /* 'm' */
2802 special_sections_n, /* 'n' */
2803 NULL, /* 'o' */
2804 special_sections_p, /* 'p' */
2805 NULL, /* 'q' */
2806 special_sections_r, /* 'r' */
2807 special_sections_s, /* 's' */
2808 special_sections_t, /* 't' */
1b315056
CS
2809 NULL, /* 'u' */
2810 NULL, /* 'v' */
2811 NULL, /* 'w' */
2812 NULL, /* 'x' */
2813 NULL, /* 'y' */
2814 special_sections_z /* 'z' */
7f4d3958
L
2815};
2816
551b43fd
AM
2817const struct bfd_elf_special_section *
2818_bfd_elf_get_special_section (const char *name,
2819 const struct bfd_elf_special_section *spec,
2820 unsigned int rela)
2f89ff8d
L
2821{
2822 int i;
7f4d3958 2823 int len;
7f4d3958 2824
551b43fd 2825 len = strlen (name);
7f4d3958 2826
551b43fd 2827 for (i = 0; spec[i].prefix != NULL; i++)
7dcb9820
AM
2828 {
2829 int suffix_len;
551b43fd 2830 int prefix_len = spec[i].prefix_length;
7dcb9820
AM
2831
2832 if (len < prefix_len)
2833 continue;
551b43fd 2834 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
7dcb9820
AM
2835 continue;
2836
551b43fd 2837 suffix_len = spec[i].suffix_length;
7dcb9820
AM
2838 if (suffix_len <= 0)
2839 {
2840 if (name[prefix_len] != 0)
2841 {
2842 if (suffix_len == 0)
2843 continue;
2844 if (name[prefix_len] != '.'
2845 && (suffix_len == -2
551b43fd 2846 || (rela && spec[i].type == SHT_REL)))
7dcb9820
AM
2847 continue;
2848 }
2849 }
2850 else
2851 {
2852 if (len < prefix_len + suffix_len)
2853 continue;
2854 if (memcmp (name + len - suffix_len,
551b43fd 2855 spec[i].prefix + prefix_len,
7dcb9820
AM
2856 suffix_len) != 0)
2857 continue;
2858 }
551b43fd 2859 return &spec[i];
7dcb9820 2860 }
2f89ff8d
L
2861
2862 return NULL;
2863}
2864
7dcb9820 2865const struct bfd_elf_special_section *
29ef7005 2866_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2f89ff8d 2867{
551b43fd
AM
2868 int i;
2869 const struct bfd_elf_special_section *spec;
29ef7005 2870 const struct elf_backend_data *bed;
2f89ff8d
L
2871
2872 /* See if this is one of the special sections. */
551b43fd
AM
2873 if (sec->name == NULL)
2874 return NULL;
2f89ff8d 2875
29ef7005
L
2876 bed = get_elf_backend_data (abfd);
2877 spec = bed->special_sections;
2878 if (spec)
2879 {
2880 spec = _bfd_elf_get_special_section (sec->name,
2881 bed->special_sections,
2882 sec->use_rela_p);
2883 if (spec != NULL)
2884 return spec;
2885 }
2886
551b43fd
AM
2887 if (sec->name[0] != '.')
2888 return NULL;
2f89ff8d 2889
551b43fd 2890 i = sec->name[1] - 'b';
1b315056 2891 if (i < 0 || i > 'z' - 'b')
551b43fd
AM
2892 return NULL;
2893
2894 spec = special_sections[i];
2f89ff8d 2895
551b43fd
AM
2896 if (spec == NULL)
2897 return NULL;
2898
2899 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2f89ff8d
L
2900}
2901
b34976b6 2902bfd_boolean
217aa764 2903_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
252b5132
RH
2904{
2905 struct bfd_elf_section_data *sdata;
551b43fd 2906 const struct elf_backend_data *bed;
7dcb9820 2907 const struct bfd_elf_special_section *ssect;
252b5132 2908
f0abc2a1
AM
2909 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2910 if (sdata == NULL)
2911 {
a50b1753 2912 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
07d6d2b8 2913 sizeof (*sdata));
f0abc2a1
AM
2914 if (sdata == NULL)
2915 return FALSE;
217aa764 2916 sec->used_by_bfd = sdata;
f0abc2a1 2917 }
bf572ba0 2918
551b43fd
AM
2919 /* Indicate whether or not this section should use RELA relocations. */
2920 bed = get_elf_backend_data (abfd);
2921 sec->use_rela_p = bed->default_use_rela_p;
2922
e843e0f8
L
2923 /* When we read a file, we don't need to set ELF section type and
2924 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2925 anyway. We will set ELF section type and flags for all linker
2926 created sections. If user specifies BFD section flags, we will
2927 set ELF section type and flags based on BFD section flags in
02ecc8e9
L
2928 elf_fake_sections. Special handling for .init_array/.fini_array
2929 output sections since they may contain .ctors/.dtors input
2930 sections. We don't want _bfd_elf_init_private_section_data to
2931 copy ELF section type from .ctors/.dtors input sections. */
2932 if (abfd->direction != read_direction
3496cb2a 2933 || (sec->flags & SEC_LINKER_CREATED) != 0)
2f89ff8d 2934 {
551b43fd 2935 ssect = (*bed->get_sec_type_attr) (abfd, sec);
02ecc8e9
L
2936 if (ssect != NULL
2937 && (!sec->flags
2938 || (sec->flags & SEC_LINKER_CREATED) != 0
2939 || ssect->type == SHT_INIT_ARRAY
2940 || ssect->type == SHT_FINI_ARRAY))
a31501e9
L
2941 {
2942 elf_section_type (sec) = ssect->type;
2943 elf_section_flags (sec) = ssect->attr;
2944 }
2f89ff8d
L
2945 }
2946
f592407e 2947 return _bfd_generic_new_section_hook (abfd, sec);
252b5132
RH
2948}
2949
2950/* Create a new bfd section from an ELF program header.
2951
2952 Since program segments have no names, we generate a synthetic name
2953 of the form segment<NUM>, where NUM is generally the index in the
2954 program header table. For segments that are split (see below) we
2955 generate the names segment<NUM>a and segment<NUM>b.
2956
2957 Note that some program segments may have a file size that is different than
2958 (less than) the memory size. All this means is that at execution the
2959 system must allocate the amount of memory specified by the memory size,
2960 but only initialize it with the first "file size" bytes read from the
2961 file. This would occur for example, with program segments consisting
2962 of combined data+bss.
2963
2964 To handle the above situation, this routine generates TWO bfd sections
2965 for the single program segment. The first has the length specified by
2966 the file size of the segment, and the second has the length specified
2967 by the difference between the two sizes. In effect, the segment is split
d5191d0c 2968 into its initialized and uninitialized parts.
252b5132
RH
2969
2970 */
2971
b34976b6 2972bfd_boolean
217aa764
AM
2973_bfd_elf_make_section_from_phdr (bfd *abfd,
2974 Elf_Internal_Phdr *hdr,
91d6fa6a 2975 int hdr_index,
a50b1753 2976 const char *type_name)
252b5132
RH
2977{
2978 asection *newsect;
2979 char *name;
2980 char namebuf[64];
d4c88bbb 2981 size_t len;
252b5132
RH
2982 int split;
2983
2984 split = ((hdr->p_memsz > 0)
2985 && (hdr->p_filesz > 0)
2986 && (hdr->p_memsz > hdr->p_filesz));
d5191d0c
AM
2987
2988 if (hdr->p_filesz > 0)
252b5132 2989 {
91d6fa6a 2990 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
d5191d0c 2991 len = strlen (namebuf) + 1;
a50b1753 2992 name = (char *) bfd_alloc (abfd, len);
d5191d0c
AM
2993 if (!name)
2994 return FALSE;
2995 memcpy (name, namebuf, len);
2996 newsect = bfd_make_section (abfd, name);
2997 if (newsect == NULL)
2998 return FALSE;
2999 newsect->vma = hdr->p_vaddr;
3000 newsect->lma = hdr->p_paddr;
3001 newsect->size = hdr->p_filesz;
3002 newsect->filepos = hdr->p_offset;
3003 newsect->flags |= SEC_HAS_CONTENTS;
3004 newsect->alignment_power = bfd_log2 (hdr->p_align);
3005 if (hdr->p_type == PT_LOAD)
252b5132 3006 {
d5191d0c
AM
3007 newsect->flags |= SEC_ALLOC;
3008 newsect->flags |= SEC_LOAD;
3009 if (hdr->p_flags & PF_X)
3010 {
3011 /* FIXME: all we known is that it has execute PERMISSION,
3012 may be data. */
3013 newsect->flags |= SEC_CODE;
3014 }
3015 }
3016 if (!(hdr->p_flags & PF_W))
3017 {
3018 newsect->flags |= SEC_READONLY;
252b5132 3019 }
252b5132
RH
3020 }
3021
d5191d0c 3022 if (hdr->p_memsz > hdr->p_filesz)
252b5132 3023 {
d5191d0c
AM
3024 bfd_vma align;
3025
91d6fa6a 3026 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
d4c88bbb 3027 len = strlen (namebuf) + 1;
a50b1753 3028 name = (char *) bfd_alloc (abfd, len);
252b5132 3029 if (!name)
b34976b6 3030 return FALSE;
d4c88bbb 3031 memcpy (name, namebuf, len);
252b5132
RH
3032 newsect = bfd_make_section (abfd, name);
3033 if (newsect == NULL)
b34976b6 3034 return FALSE;
252b5132
RH
3035 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
3036 newsect->lma = hdr->p_paddr + hdr->p_filesz;
eea6121a 3037 newsect->size = hdr->p_memsz - hdr->p_filesz;
d5191d0c
AM
3038 newsect->filepos = hdr->p_offset + hdr->p_filesz;
3039 align = newsect->vma & -newsect->vma;
3040 if (align == 0 || align > hdr->p_align)
3041 align = hdr->p_align;
3042 newsect->alignment_power = bfd_log2 (align);
252b5132
RH
3043 if (hdr->p_type == PT_LOAD)
3044 {
d5191d0c
AM
3045 /* Hack for gdb. Segments that have not been modified do
3046 not have their contents written to a core file, on the
3047 assumption that a debugger can find the contents in the
3048 executable. We flag this case by setting the fake
3049 section size to zero. Note that "real" bss sections will
3050 always have their contents dumped to the core file. */
3051 if (bfd_get_format (abfd) == bfd_core)
3052 newsect->size = 0;
252b5132
RH
3053 newsect->flags |= SEC_ALLOC;
3054 if (hdr->p_flags & PF_X)
3055 newsect->flags |= SEC_CODE;
3056 }
3057 if (!(hdr->p_flags & PF_W))
3058 newsect->flags |= SEC_READONLY;
3059 }
3060
b34976b6 3061 return TRUE;
252b5132
RH
3062}
3063
b34976b6 3064bfd_boolean
91d6fa6a 3065bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
20cfcaae 3066{
9c5bfbb7 3067 const struct elf_backend_data *bed;
20cfcaae
NC
3068
3069 switch (hdr->p_type)
3070 {
3071 case PT_NULL:
91d6fa6a 3072 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
20cfcaae
NC
3073
3074 case PT_LOAD:
91d6fa6a 3075 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
20cfcaae
NC
3076
3077 case PT_DYNAMIC:
91d6fa6a 3078 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
20cfcaae
NC
3079
3080 case PT_INTERP:
91d6fa6a 3081 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
20cfcaae
NC
3082
3083 case PT_NOTE:
91d6fa6a 3084 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
b34976b6 3085 return FALSE;
276da9b3
L
3086 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3087 hdr->p_align))
b34976b6
AM
3088 return FALSE;
3089 return TRUE;
20cfcaae
NC
3090
3091 case PT_SHLIB:
91d6fa6a 3092 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
20cfcaae
NC
3093
3094 case PT_PHDR:
91d6fa6a 3095 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
20cfcaae 3096
811072d8 3097 case PT_GNU_EH_FRAME:
91d6fa6a 3098 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
811072d8
RM
3099 "eh_frame_hdr");
3100
2b05f1b7 3101 case PT_GNU_STACK:
91d6fa6a 3102 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
9ee5e499 3103
8c37241b 3104 case PT_GNU_RELRO:
91d6fa6a 3105 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
8c37241b 3106
20cfcaae 3107 default:
8c1acd09 3108 /* Check for any processor-specific program segment types. */
20cfcaae 3109 bed = get_elf_backend_data (abfd);
91d6fa6a 3110 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
20cfcaae
NC
3111 }
3112}
3113
d4730f92
BS
3114/* Return the REL_HDR for SEC, assuming there is only a single one, either
3115 REL or RELA. */
3116
3117Elf_Internal_Shdr *
3118_bfd_elf_single_rel_hdr (asection *sec)
3119{
3120 if (elf_section_data (sec)->rel.hdr)
3121 {
3122 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3123 return elf_section_data (sec)->rel.hdr;
3124 }
3125 else
3126 return elf_section_data (sec)->rela.hdr;
3127}
3128
3e19fb8f
L
3129static bfd_boolean
3130_bfd_elf_set_reloc_sh_name (bfd *abfd,
3131 Elf_Internal_Shdr *rel_hdr,
3132 const char *sec_name,
3133 bfd_boolean use_rela_p)
3134{
3135 char *name = (char *) bfd_alloc (abfd,
3136 sizeof ".rela" + strlen (sec_name));
3137 if (name == NULL)
3138 return FALSE;
3139
3140 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3141 rel_hdr->sh_name =
3142 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3143 FALSE);
3144 if (rel_hdr->sh_name == (unsigned int) -1)
3145 return FALSE;
3146
3147 return TRUE;
3148}
3149
d4730f92
BS
3150/* Allocate and initialize a section-header for a new reloc section,
3151 containing relocations against ASECT. It is stored in RELDATA. If
3152 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3153 relocations. */
23bc299b 3154
5d13b3b3 3155static bfd_boolean
217aa764 3156_bfd_elf_init_reloc_shdr (bfd *abfd,
d4730f92 3157 struct bfd_elf_section_reloc_data *reldata,
f6fe1ccd 3158 const char *sec_name,
3e19fb8f
L
3159 bfd_boolean use_rela_p,
3160 bfd_boolean delay_st_name_p)
23bc299b 3161{
d4730f92 3162 Elf_Internal_Shdr *rel_hdr;
9c5bfbb7 3163 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3164
d4730f92 3165 BFD_ASSERT (reldata->hdr == NULL);
ef53be89 3166 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
d4730f92 3167 reldata->hdr = rel_hdr;
23bc299b 3168
3e19fb8f
L
3169 if (delay_st_name_p)
3170 rel_hdr->sh_name = (unsigned int) -1;
3171 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3172 use_rela_p))
b34976b6 3173 return FALSE;
23bc299b
MM
3174 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3175 rel_hdr->sh_entsize = (use_rela_p
3176 ? bed->s->sizeof_rela
3177 : bed->s->sizeof_rel);
72de5009 3178 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
28e07a05 3179 rel_hdr->sh_flags = 0;
23bc299b
MM
3180 rel_hdr->sh_addr = 0;
3181 rel_hdr->sh_size = 0;
3182 rel_hdr->sh_offset = 0;
3183
b34976b6 3184 return TRUE;
23bc299b
MM
3185}
3186
94be91de
JB
3187/* Return the default section type based on the passed in section flags. */
3188
3189int
3190bfd_elf_get_default_section_type (flagword flags)
3191{
0e41bebb 3192 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
2e76e85a 3193 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
94be91de
JB
3194 return SHT_NOBITS;
3195 return SHT_PROGBITS;
3196}
3197
d4730f92
BS
3198struct fake_section_arg
3199{
3200 struct bfd_link_info *link_info;
3201 bfd_boolean failed;
3202};
3203
252b5132
RH
3204/* Set up an ELF internal section header for a section. */
3205
252b5132 3206static void
d4730f92 3207elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
252b5132 3208{
d4730f92 3209 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
9c5bfbb7 3210 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3211 struct bfd_elf_section_data *esd = elf_section_data (asect);
252b5132 3212 Elf_Internal_Shdr *this_hdr;
0414f35b 3213 unsigned int sh_type;
0ce398f1 3214 const char *name = asect->name;
3e19fb8f 3215 bfd_boolean delay_st_name_p = FALSE;
252b5132 3216
d4730f92 3217 if (arg->failed)
252b5132
RH
3218 {
3219 /* We already failed; just get out of the bfd_map_over_sections
08a40648 3220 loop. */
252b5132
RH
3221 return;
3222 }
3223
d4730f92 3224 this_hdr = &esd->this_hdr;
252b5132 3225
f6fe1ccd 3226 if (arg->link_info)
0ce398f1 3227 {
f6fe1ccd
L
3228 /* ld: compress DWARF debug sections with names: .debug_*. */
3229 if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
3230 && (asect->flags & SEC_DEBUGGING)
3231 && name[1] == 'd'
3232 && name[6] == '_')
3233 {
3234 /* Set SEC_ELF_COMPRESS to indicate this section should be
3235 compressed. */
3236 asect->flags |= SEC_ELF_COMPRESS;
0ce398f1 3237
dd905818 3238 /* If this section will be compressed, delay adding section
3e19fb8f
L
3239 name to section name section after it is compressed in
3240 _bfd_elf_assign_file_positions_for_non_load. */
3241 delay_st_name_p = TRUE;
f6fe1ccd
L
3242 }
3243 }
3244 else if ((asect->flags & SEC_ELF_RENAME))
3245 {
3246 /* objcopy: rename output DWARF debug section. */
3247 if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
3248 {
3249 /* When we decompress or compress with SHF_COMPRESSED,
3250 convert section name from .zdebug_* to .debug_* if
3251 needed. */
3252 if (name[1] == 'z')
3253 {
3254 char *new_name = convert_zdebug_to_debug (abfd, name);
3255 if (new_name == NULL)
3256 {
3257 arg->failed = TRUE;
3258 return;
3259 }
3260 name = new_name;
3261 }
3262 }
3263 else if (asect->compress_status == COMPRESS_SECTION_DONE)
0ce398f1 3264 {
f6fe1ccd
L
3265 /* PR binutils/18087: Compression does not always make a
3266 section smaller. So only rename the section when
3267 compression has actually taken place. If input section
3268 name is .zdebug_*, we should never compress it again. */
3269 char *new_name = convert_debug_to_zdebug (abfd, name);
0ce398f1
L
3270 if (new_name == NULL)
3271 {
3272 arg->failed = TRUE;
3273 return;
3274 }
f6fe1ccd
L
3275 BFD_ASSERT (name[1] != 'z');
3276 name = new_name;
0ce398f1
L
3277 }
3278 }
3279
3e19fb8f
L
3280 if (delay_st_name_p)
3281 this_hdr->sh_name = (unsigned int) -1;
3282 else
252b5132 3283 {
3e19fb8f
L
3284 this_hdr->sh_name
3285 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3286 name, FALSE);
3287 if (this_hdr->sh_name == (unsigned int) -1)
3288 {
3289 arg->failed = TRUE;
3290 return;
3291 }
252b5132
RH
3292 }
3293
a4d8e49b 3294 /* Don't clear sh_flags. Assembler may set additional bits. */
252b5132
RH
3295
3296 if ((asect->flags & SEC_ALLOC) != 0
3297 || asect->user_set_vma)
3298 this_hdr->sh_addr = asect->vma;
3299 else
3300 this_hdr->sh_addr = 0;
3301
3302 this_hdr->sh_offset = 0;
eea6121a 3303 this_hdr->sh_size = asect->size;
252b5132 3304 this_hdr->sh_link = 0;
c86934ce
NC
3305 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3306 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3307 {
4eca0228 3308 _bfd_error_handler
695344c0 3309 /* xgettext:c-format */
9793eb77 3310 (_("%pB: error: alignment power %d of section `%pA' is too big"),
c08bb8dd 3311 abfd, asect->alignment_power, asect);
c86934ce
NC
3312 arg->failed = TRUE;
3313 return;
3314 }
72de5009 3315 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
252b5132
RH
3316 /* The sh_entsize and sh_info fields may have been set already by
3317 copy_private_section_data. */
3318
3319 this_hdr->bfd_section = asect;
3320 this_hdr->contents = NULL;
3321
3cddba1e
L
3322 /* If the section type is unspecified, we set it based on
3323 asect->flags. */
98ece1b3
AM
3324 if ((asect->flags & SEC_GROUP) != 0)
3325 sh_type = SHT_GROUP;
98ece1b3 3326 else
94be91de 3327 sh_type = bfd_elf_get_default_section_type (asect->flags);
98ece1b3 3328
3cddba1e 3329 if (this_hdr->sh_type == SHT_NULL)
98ece1b3
AM
3330 this_hdr->sh_type = sh_type;
3331 else if (this_hdr->sh_type == SHT_NOBITS
3332 && sh_type == SHT_PROGBITS
3333 && (asect->flags & SEC_ALLOC) != 0)
3cddba1e 3334 {
98ece1b3
AM
3335 /* Warn if we are changing a NOBITS section to PROGBITS, but
3336 allow the link to proceed. This can happen when users link
3337 non-bss input sections to bss output sections, or emit data
3338 to a bss output section via a linker script. */
4eca0228 3339 _bfd_error_handler
871b3ab2 3340 (_("warning: section `%pA' type changed to PROGBITS"), asect);
98ece1b3 3341 this_hdr->sh_type = sh_type;
3cddba1e
L
3342 }
3343
2f89ff8d 3344 switch (this_hdr->sh_type)
252b5132 3345 {
2f89ff8d 3346 default:
2f89ff8d
L
3347 break;
3348
3349 case SHT_STRTAB:
2f89ff8d
L
3350 case SHT_NOTE:
3351 case SHT_NOBITS:
3352 case SHT_PROGBITS:
3353 break;
606851fb
AM
3354
3355 case SHT_INIT_ARRAY:
3356 case SHT_FINI_ARRAY:
3357 case SHT_PREINIT_ARRAY:
3358 this_hdr->sh_entsize = bed->s->arch_size / 8;
3359 break;
2f89ff8d
L
3360
3361 case SHT_HASH:
c7ac6ff8 3362 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2f89ff8d 3363 break;
5de3bf90 3364
2f89ff8d 3365 case SHT_DYNSYM:
252b5132 3366 this_hdr->sh_entsize = bed->s->sizeof_sym;
2f89ff8d
L
3367 break;
3368
3369 case SHT_DYNAMIC:
252b5132 3370 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2f89ff8d
L
3371 break;
3372
3373 case SHT_RELA:
3374 if (get_elf_backend_data (abfd)->may_use_rela_p)
3375 this_hdr->sh_entsize = bed->s->sizeof_rela;
3376 break;
3377
3378 case SHT_REL:
3379 if (get_elf_backend_data (abfd)->may_use_rel_p)
3380 this_hdr->sh_entsize = bed->s->sizeof_rel;
3381 break;
3382
3383 case SHT_GNU_versym:
252b5132 3384 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2f89ff8d
L
3385 break;
3386
3387 case SHT_GNU_verdef:
252b5132
RH
3388 this_hdr->sh_entsize = 0;
3389 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3390 cverdefs. The linker will set cverdefs, but sh_info will be
3391 zero. */
252b5132
RH
3392 if (this_hdr->sh_info == 0)
3393 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3394 else
3395 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3396 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2f89ff8d
L
3397 break;
3398
3399 case SHT_GNU_verneed:
252b5132
RH
3400 this_hdr->sh_entsize = 0;
3401 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3402 cverrefs. The linker will set cverrefs, but sh_info will be
3403 zero. */
252b5132
RH
3404 if (this_hdr->sh_info == 0)
3405 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3406 else
3407 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3408 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2f89ff8d
L
3409 break;
3410
3411 case SHT_GROUP:
1783205a 3412 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2f89ff8d 3413 break;
fdc90cb4
JJ
3414
3415 case SHT_GNU_HASH:
3416 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3417 break;
dbb410c3 3418 }
252b5132
RH
3419
3420 if ((asect->flags & SEC_ALLOC) != 0)
3421 this_hdr->sh_flags |= SHF_ALLOC;
3422 if ((asect->flags & SEC_READONLY) == 0)
3423 this_hdr->sh_flags |= SHF_WRITE;
3424 if ((asect->flags & SEC_CODE) != 0)
3425 this_hdr->sh_flags |= SHF_EXECINSTR;
f5fa8ca2
JJ
3426 if ((asect->flags & SEC_MERGE) != 0)
3427 {
3428 this_hdr->sh_flags |= SHF_MERGE;
3429 this_hdr->sh_entsize = asect->entsize;
f5fa8ca2 3430 }
84865015
NC
3431 if ((asect->flags & SEC_STRINGS) != 0)
3432 this_hdr->sh_flags |= SHF_STRINGS;
1126897b 3433 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
dbb410c3 3434 this_hdr->sh_flags |= SHF_GROUP;
13ae64f3 3435 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
704afa60
JJ
3436 {
3437 this_hdr->sh_flags |= SHF_TLS;
3a800eb9
AM
3438 if (asect->size == 0
3439 && (asect->flags & SEC_HAS_CONTENTS) == 0)
704afa60 3440 {
3a800eb9 3441 struct bfd_link_order *o = asect->map_tail.link_order;
b34976b6 3442
704afa60 3443 this_hdr->sh_size = 0;
3a800eb9
AM
3444 if (o != NULL)
3445 {
704afa60 3446 this_hdr->sh_size = o->offset + o->size;
3a800eb9
AM
3447 if (this_hdr->sh_size != 0)
3448 this_hdr->sh_type = SHT_NOBITS;
3449 }
704afa60
JJ
3450 }
3451 }
18ae9cc1
L
3452 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3453 this_hdr->sh_flags |= SHF_EXCLUDE;
252b5132 3454
d4730f92
BS
3455 /* If the section has relocs, set up a section header for the
3456 SHT_REL[A] section. If two relocation sections are required for
3457 this section, it is up to the processor-specific back-end to
3458 create the other. */
3459 if ((asect->flags & SEC_RELOC) != 0)
3460 {
3461 /* When doing a relocatable link, create both REL and RELA sections if
3462 needed. */
3463 if (arg->link_info
3464 /* Do the normal setup if we wouldn't create any sections here. */
3465 && esd->rel.count + esd->rela.count > 0
0e1862bb
L
3466 && (bfd_link_relocatable (arg->link_info)
3467 || arg->link_info->emitrelocations))
d4730f92
BS
3468 {
3469 if (esd->rel.count && esd->rel.hdr == NULL
28e07a05 3470 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
db4677b8 3471 FALSE, delay_st_name_p))
d4730f92
BS
3472 {
3473 arg->failed = TRUE;
3474 return;
3475 }
3476 if (esd->rela.count && esd->rela.hdr == NULL
28e07a05 3477 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
db4677b8 3478 TRUE, delay_st_name_p))
d4730f92
BS
3479 {
3480 arg->failed = TRUE;
3481 return;
3482 }
3483 }
3484 else if (!_bfd_elf_init_reloc_shdr (abfd,
3485 (asect->use_rela_p
3486 ? &esd->rela : &esd->rel),
f6fe1ccd 3487 name,
3e19fb8f
L
3488 asect->use_rela_p,
3489 delay_st_name_p))
db4677b8 3490 {
d4730f92 3491 arg->failed = TRUE;
db4677b8
AM
3492 return;
3493 }
d4730f92
BS
3494 }
3495
252b5132 3496 /* Check for processor-specific section types. */
0414f35b 3497 sh_type = this_hdr->sh_type;
e1fddb6b
AO
3498 if (bed->elf_backend_fake_sections
3499 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
db4677b8
AM
3500 {
3501 arg->failed = TRUE;
3502 return;
3503 }
252b5132 3504
42bb2e33 3505 if (sh_type == SHT_NOBITS && asect->size != 0)
0414f35b
AM
3506 {
3507 /* Don't change the header type from NOBITS if we are being
42bb2e33 3508 called for objcopy --only-keep-debug. */
0414f35b
AM
3509 this_hdr->sh_type = sh_type;
3510 }
252b5132
RH
3511}
3512
bcacc0f5
AM
3513/* Fill in the contents of a SHT_GROUP section. Called from
3514 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3515 when ELF targets use the generic linker, ld. Called for ld -r
3516 from bfd_elf_final_link. */
dbb410c3 3517
1126897b 3518void
217aa764 3519bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
dbb410c3 3520{
a50b1753 3521 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
9dce4196 3522 asection *elt, *first;
dbb410c3 3523 unsigned char *loc;
b34976b6 3524 bfd_boolean gas;
dbb410c3 3525
7e4111ad
L
3526 /* Ignore linker created group section. See elfNN_ia64_object_p in
3527 elfxx-ia64.c. */
ce5aecf8
AM
3528 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3529 || sec->size == 0
dbb410c3
AM
3530 || *failedptr)
3531 return;
3532
bcacc0f5
AM
3533 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3534 {
3535 unsigned long symindx = 0;
3536
3537 /* elf_group_id will have been set up by objcopy and the
3538 generic linker. */
3539 if (elf_group_id (sec) != NULL)
3540 symindx = elf_group_id (sec)->udata.i;
1126897b 3541
bcacc0f5
AM
3542 if (symindx == 0)
3543 {
3544 /* If called from the assembler, swap_out_syms will have set up
3545 elf_section_syms. */
3546 BFD_ASSERT (elf_section_syms (abfd) != NULL);
3547 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3548 }
3549 elf_section_data (sec)->this_hdr.sh_info = symindx;
3550 }
3551 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
1126897b 3552 {
bcacc0f5
AM
3553 /* The ELF backend linker sets sh_info to -2 when the group
3554 signature symbol is global, and thus the index can't be
3555 set until all local symbols are output. */
53720c49
AM
3556 asection *igroup;
3557 struct bfd_elf_section_data *sec_data;
3558 unsigned long symndx;
3559 unsigned long extsymoff;
bcacc0f5
AM
3560 struct elf_link_hash_entry *h;
3561
53720c49
AM
3562 /* The point of this little dance to the first SHF_GROUP section
3563 then back to the SHT_GROUP section is that this gets us to
3564 the SHT_GROUP in the input object. */
3565 igroup = elf_sec_group (elf_next_in_group (sec));
3566 sec_data = elf_section_data (igroup);
3567 symndx = sec_data->this_hdr.sh_info;
3568 extsymoff = 0;
bcacc0f5
AM
3569 if (!elf_bad_symtab (igroup->owner))
3570 {
3571 Elf_Internal_Shdr *symtab_hdr;
3572
3573 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3574 extsymoff = symtab_hdr->sh_info;
3575 }
3576 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3577 while (h->root.type == bfd_link_hash_indirect
3578 || h->root.type == bfd_link_hash_warning)
3579 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3580
3581 elf_section_data (sec)->this_hdr.sh_info = h->indx;
1126897b 3582 }
dbb410c3 3583
1126897b 3584 /* The contents won't be allocated for "ld -r" or objcopy. */
b34976b6 3585 gas = TRUE;
dbb410c3
AM
3586 if (sec->contents == NULL)
3587 {
b34976b6 3588 gas = FALSE;
a50b1753 3589 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
9dce4196
AM
3590
3591 /* Arrange for the section to be written out. */
3592 elf_section_data (sec)->this_hdr.contents = sec->contents;
dbb410c3
AM
3593 if (sec->contents == NULL)
3594 {
b34976b6 3595 *failedptr = TRUE;
dbb410c3
AM
3596 return;
3597 }
3598 }
3599
eea6121a 3600 loc = sec->contents + sec->size;
dbb410c3 3601
9dce4196
AM
3602 /* Get the pointer to the first section in the group that gas
3603 squirreled away here. objcopy arranges for this to be set to the
3604 start of the input section group. */
3605 first = elt = elf_next_in_group (sec);
dbb410c3
AM
3606
3607 /* First element is a flag word. Rest of section is elf section
3608 indices for all the sections of the group. Write them backwards
3609 just to keep the group in the same order as given in .section
3610 directives, not that it matters. */
3611 while (elt != NULL)
3612 {
9dce4196 3613 asection *s;
9dce4196 3614
9dce4196 3615 s = elt;
415f38a6
AM
3616 if (!gas)
3617 s = s->output_section;
3618 if (s != NULL
3619 && !bfd_is_abs_section (s))
01e1a5bc 3620 {
db4677b8 3621 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
28e07a05
AM
3622 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3623
3624 if (elf_sec->rel.hdr != NULL
3625 && (gas
3626 || (input_elf_sec->rel.hdr != NULL
3627 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3628 {
28e07a05 3629 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
db4677b8
AM
3630 loc -= 4;
3631 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3632 }
28e07a05
AM
3633 if (elf_sec->rela.hdr != NULL
3634 && (gas
3635 || (input_elf_sec->rela.hdr != NULL
3636 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3637 {
28e07a05 3638 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
db4677b8
AM
3639 loc -= 4;
3640 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3641 }
01e1a5bc 3642 loc -= 4;
db4677b8 3643 H_PUT_32 (abfd, elf_sec->this_idx, loc);
01e1a5bc 3644 }
945906ff 3645 elt = elf_next_in_group (elt);
9dce4196
AM
3646 if (elt == first)
3647 break;
dbb410c3
AM
3648 }
3649
7bdf4127
AB
3650 loc -= 4;
3651 BFD_ASSERT (loc == sec->contents);
dbb410c3 3652
9dce4196 3653 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
dbb410c3
AM
3654}
3655
bce964aa
AM
3656/* Given NAME, the name of a relocation section stripped of its
3657 .rel/.rela prefix, return the section in ABFD to which the
3658 relocations apply. */
bd53a53a
L
3659
3660asection *
bce964aa
AM
3661_bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3662{
3663 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3664 section likely apply to .got.plt or .got section. */
3665 if (get_elf_backend_data (abfd)->want_got_plt
3666 && strcmp (name, ".plt") == 0)
3667 {
3668 asection *sec;
3669
3670 name = ".got.plt";
3671 sec = bfd_get_section_by_name (abfd, name);
3672 if (sec != NULL)
3673 return sec;
3674 name = ".got";
3675 }
3676
3677 return bfd_get_section_by_name (abfd, name);
3678}
3679
3680/* Return the section to which RELOC_SEC applies. */
3681
3682static asection *
3683elf_get_reloc_section (asection *reloc_sec)
bd53a53a
L
3684{
3685 const char *name;
3686 unsigned int type;
3687 bfd *abfd;
bce964aa 3688 const struct elf_backend_data *bed;
bd53a53a
L
3689
3690 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3691 if (type != SHT_REL && type != SHT_RELA)
3692 return NULL;
3693
3694 /* We look up the section the relocs apply to by name. */
3695 name = reloc_sec->name;
bce964aa
AM
3696 if (strncmp (name, ".rel", 4) != 0)
3697 return NULL;
3698 name += 4;
3699 if (type == SHT_RELA && *name++ != 'a')
3700 return NULL;
bd53a53a 3701
bd53a53a 3702 abfd = reloc_sec->owner;
bce964aa
AM
3703 bed = get_elf_backend_data (abfd);
3704 return bed->get_reloc_section (abfd, name);
bd53a53a
L
3705}
3706
252b5132
RH
3707/* Assign all ELF section numbers. The dummy first section is handled here
3708 too. The link/info pointers for the standard section types are filled
3709 in here too, while we're at it. */
3710
b34976b6 3711static bfd_boolean
da9f89d4 3712assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
252b5132
RH
3713{
3714 struct elf_obj_tdata *t = elf_tdata (abfd);
3715 asection *sec;
3e19fb8f 3716 unsigned int section_number;
252b5132 3717 Elf_Internal_Shdr **i_shdrp;
47cc2cf5 3718 struct bfd_elf_section_data *d;
3516e984 3719 bfd_boolean need_symtab;
252b5132
RH
3720
3721 section_number = 1;
3722
2b0f7ef9
JJ
3723 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3724
da9f89d4 3725 /* SHT_GROUP sections are in relocatable files only. */
7bdf4127 3726 if (link_info == NULL || !link_info->resolve_section_groups)
252b5132 3727 {
ef53be89 3728 size_t reloc_count = 0;
14f2c699 3729
da9f89d4 3730 /* Put SHT_GROUP sections first. */
04dd1667 3731 for (sec = abfd->sections; sec != NULL; sec = sec->next)
47cc2cf5 3732 {
5daa8fe7 3733 d = elf_section_data (sec);
da9f89d4
L
3734
3735 if (d->this_hdr.sh_type == SHT_GROUP)
08a40648 3736 {
5daa8fe7 3737 if (sec->flags & SEC_LINKER_CREATED)
da9f89d4
L
3738 {
3739 /* Remove the linker created SHT_GROUP sections. */
5daa8fe7 3740 bfd_section_list_remove (abfd, sec);
da9f89d4 3741 abfd->section_count--;
da9f89d4 3742 }
08a40648 3743 else
4fbb74a6 3744 d->this_idx = section_number++;
da9f89d4 3745 }
14f2c699
L
3746
3747 /* Count relocations. */
3748 reloc_count += sec->reloc_count;
47cc2cf5 3749 }
14f2c699
L
3750
3751 /* Clear HAS_RELOC if there are no relocations. */
3752 if (reloc_count == 0)
3753 abfd->flags &= ~HAS_RELOC;
47cc2cf5
PB
3754 }
3755
3756 for (sec = abfd->sections; sec; sec = sec->next)
3757 {
3758 d = elf_section_data (sec);
3759
3760 if (d->this_hdr.sh_type != SHT_GROUP)
4fbb74a6 3761 d->this_idx = section_number++;
3e19fb8f
L
3762 if (d->this_hdr.sh_name != (unsigned int) -1)
3763 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
d4730f92 3764 if (d->rel.hdr)
2b0f7ef9 3765 {
d4730f92 3766 d->rel.idx = section_number++;
3e19fb8f
L
3767 if (d->rel.hdr->sh_name != (unsigned int) -1)
3768 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
2b0f7ef9 3769 }
d4730f92
BS
3770 else
3771 d->rel.idx = 0;
23bc299b 3772
d4730f92 3773 if (d->rela.hdr)
2b0f7ef9 3774 {
d4730f92 3775 d->rela.idx = section_number++;
3e19fb8f
L
3776 if (d->rela.hdr->sh_name != (unsigned int) -1)
3777 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
2b0f7ef9 3778 }
23bc299b 3779 else
d4730f92 3780 d->rela.idx = 0;
252b5132
RH
3781 }
3782
3516e984
L
3783 need_symtab = (bfd_get_symcount (abfd) > 0
3784 || (link_info == NULL
3785 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3786 == HAS_RELOC)));
3787 if (need_symtab)
252b5132 3788 {
12bd6957 3789 elf_onesymtab (abfd) = section_number++;
2b0f7ef9 3790 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4fbb74a6 3791 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
9ad5cbcf 3792 {
7a6e0d89 3793 elf_section_list *entry;
6a40cf0c
NC
3794
3795 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3796
7a6e0d89 3797 entry = bfd_zalloc (abfd, sizeof (*entry));
6a40cf0c
NC
3798 entry->ndx = section_number++;
3799 elf_symtab_shndx_list (abfd) = entry;
3800 entry->hdr.sh_name
9ad5cbcf 3801 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
b34976b6 3802 ".symtab_shndx", FALSE);
6a40cf0c 3803 if (entry->hdr.sh_name == (unsigned int) -1)
b34976b6 3804 return FALSE;
9ad5cbcf 3805 }
12bd6957 3806 elf_strtab_sec (abfd) = section_number++;
2b0f7ef9 3807 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
252b5132
RH
3808 }
3809
dd905818
NC
3810 elf_shstrtab_sec (abfd) = section_number++;
3811 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3812 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3813
1c52a645
L
3814 if (section_number >= SHN_LORESERVE)
3815 {
695344c0 3816 /* xgettext:c-format */
871b3ab2 3817 _bfd_error_handler (_("%pB: too many sections: %u"),
1c52a645
L
3818 abfd, section_number);
3819 return FALSE;
3820 }
3821
9ad5cbcf 3822 elf_numsections (abfd) = section_number;
252b5132
RH
3823 elf_elfheader (abfd)->e_shnum = section_number;
3824
3825 /* Set up the list of section header pointers, in agreement with the
3826 indices. */
a50b1753 3827 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
07d6d2b8 3828 sizeof (Elf_Internal_Shdr *));
252b5132 3829 if (i_shdrp == NULL)
b34976b6 3830 return FALSE;
252b5132 3831
a50b1753 3832 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
07d6d2b8 3833 sizeof (Elf_Internal_Shdr));
252b5132
RH
3834 if (i_shdrp[0] == NULL)
3835 {
3836 bfd_release (abfd, i_shdrp);
b34976b6 3837 return FALSE;
252b5132 3838 }
252b5132
RH
3839
3840 elf_elfsections (abfd) = i_shdrp;
3841
12bd6957 3842 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3516e984 3843 if (need_symtab)
252b5132 3844 {
12bd6957 3845 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4fbb74a6 3846 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
9ad5cbcf 3847 {
6a40cf0c
NC
3848 elf_section_list * entry = elf_symtab_shndx_list (abfd);
3849 BFD_ASSERT (entry != NULL);
3850 i_shdrp[entry->ndx] = & entry->hdr;
3851 entry->hdr.sh_link = elf_onesymtab (abfd);
9ad5cbcf 3852 }
12bd6957
AM
3853 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3854 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
252b5132 3855 }
38ce5b11 3856
252b5132
RH
3857 for (sec = abfd->sections; sec; sec = sec->next)
3858 {
252b5132 3859 asection *s;
252b5132 3860
91d6fa6a
NC
3861 d = elf_section_data (sec);
3862
252b5132 3863 i_shdrp[d->this_idx] = &d->this_hdr;
d4730f92
BS
3864 if (d->rel.idx != 0)
3865 i_shdrp[d->rel.idx] = d->rel.hdr;
3866 if (d->rela.idx != 0)
3867 i_shdrp[d->rela.idx] = d->rela.hdr;
252b5132
RH
3868
3869 /* Fill in the sh_link and sh_info fields while we're at it. */
3870
3871 /* sh_link of a reloc section is the section index of the symbol
3872 table. sh_info is the section index of the section to which
3873 the relocation entries apply. */
d4730f92 3874 if (d->rel.idx != 0)
252b5132 3875 {
12bd6957 3876 d->rel.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3877 d->rel.hdr->sh_info = d->this_idx;
9ef5d938 3878 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
252b5132 3879 }
d4730f92 3880 if (d->rela.idx != 0)
23bc299b 3881 {
12bd6957 3882 d->rela.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3883 d->rela.hdr->sh_info = d->this_idx;
9ef5d938 3884 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
23bc299b 3885 }
252b5132 3886
38ce5b11
L
3887 /* We need to set up sh_link for SHF_LINK_ORDER. */
3888 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3889 {
3890 s = elf_linked_to_section (sec);
3891 if (s)
38ce5b11 3892 {
f2876037 3893 /* elf_linked_to_section points to the input section. */
ccd2ec6a 3894 if (link_info != NULL)
38ce5b11 3895 {
f2876037 3896 /* Check discarded linkonce section. */
dbaa2011 3897 if (discarded_section (s))
38ce5b11 3898 {
ccd2ec6a 3899 asection *kept;
4eca0228 3900 _bfd_error_handler
695344c0 3901 /* xgettext:c-format */
871b3ab2
AM
3902 (_("%pB: sh_link of section `%pA' points to"
3903 " discarded section `%pA' of `%pB'"),
ccd2ec6a
L
3904 abfd, d->this_hdr.bfd_section,
3905 s, s->owner);
3906 /* Point to the kept section if it has the same
3907 size as the discarded one. */
c0f00686 3908 kept = _bfd_elf_check_kept_section (s, link_info);
ccd2ec6a 3909 if (kept == NULL)
185d09ad 3910 {
ccd2ec6a
L
3911 bfd_set_error (bfd_error_bad_value);
3912 return FALSE;
185d09ad 3913 }
ccd2ec6a 3914 s = kept;
38ce5b11 3915 }
e424ecc8 3916
ccd2ec6a
L
3917 s = s->output_section;
3918 BFD_ASSERT (s != NULL);
38ce5b11 3919 }
f2876037
L
3920 else
3921 {
3922 /* Handle objcopy. */
3923 if (s->output_section == NULL)
3924 {
4eca0228 3925 _bfd_error_handler
695344c0 3926 /* xgettext:c-format */
871b3ab2
AM
3927 (_("%pB: sh_link of section `%pA' points to"
3928 " removed section `%pA' of `%pB'"),
f2876037
L
3929 abfd, d->this_hdr.bfd_section, s, s->owner);
3930 bfd_set_error (bfd_error_bad_value);
3931 return FALSE;
3932 }
3933 s = s->output_section;
3934 }
ccd2ec6a
L
3935 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3936 }
3937 else
3938 {
3939 /* PR 290:
3940 The Intel C compiler generates SHT_IA_64_UNWIND with
3941 SHF_LINK_ORDER. But it doesn't set the sh_link or
3942 sh_info fields. Hence we could get the situation
08a40648 3943 where s is NULL. */
ccd2ec6a
L
3944 const struct elf_backend_data *bed
3945 = get_elf_backend_data (abfd);
3946 if (bed->link_order_error_handler)
3947 bed->link_order_error_handler
695344c0 3948 /* xgettext:c-format */
871b3ab2 3949 (_("%pB: warning: sh_link not set for section `%pA'"),
ccd2ec6a 3950 abfd, sec);
38ce5b11
L
3951 }
3952 }
3953
252b5132
RH
3954 switch (d->this_hdr.sh_type)
3955 {
3956 case SHT_REL:
3957 case SHT_RELA:
3958 /* A reloc section which we are treating as a normal BFD
3959 section. sh_link is the section index of the symbol
3960 table. sh_info is the section index of the section to
3961 which the relocation entries apply. We assume that an
3962 allocated reloc section uses the dynamic symbol table.
3963 FIXME: How can we be sure? */
3964 s = bfd_get_section_by_name (abfd, ".dynsym");
3965 if (s != NULL)
3966 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3967
bce964aa 3968 s = elf_get_reloc_section (sec);
252b5132 3969 if (s != NULL)
9ef5d938
L
3970 {
3971 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3972 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3973 }
252b5132
RH
3974 break;
3975
3976 case SHT_STRTAB:
3977 /* We assume that a section named .stab*str is a stabs
3978 string section. We look for a section with the same name
3979 but without the trailing ``str'', and set its sh_link
3980 field to point to this section. */
0112cd26 3981 if (CONST_STRNEQ (sec->name, ".stab")
252b5132
RH
3982 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3983 {
3984 size_t len;
3985 char *alc;
3986
3987 len = strlen (sec->name);
a50b1753 3988 alc = (char *) bfd_malloc (len - 2);
252b5132 3989 if (alc == NULL)
b34976b6 3990 return FALSE;
d4c88bbb 3991 memcpy (alc, sec->name, len - 3);
252b5132
RH
3992 alc[len - 3] = '\0';
3993 s = bfd_get_section_by_name (abfd, alc);
3994 free (alc);
3995 if (s != NULL)
3996 {
3997 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3998
3999 /* This is a .stab section. */
0594c12d
AM
4000 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
4001 elf_section_data (s)->this_hdr.sh_entsize
4002 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
252b5132
RH
4003 }
4004 }
4005 break;
4006
4007 case SHT_DYNAMIC:
4008 case SHT_DYNSYM:
4009 case SHT_GNU_verneed:
4010 case SHT_GNU_verdef:
4011 /* sh_link is the section header index of the string table
4012 used for the dynamic entries, or the symbol table, or the
4013 version strings. */
4014 s = bfd_get_section_by_name (abfd, ".dynstr");
4015 if (s != NULL)
4016 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4017 break;
4018
7f1204bb
JJ
4019 case SHT_GNU_LIBLIST:
4020 /* sh_link is the section header index of the prelink library
08a40648
AM
4021 list used for the dynamic entries, or the symbol table, or
4022 the version strings. */
7f1204bb
JJ
4023 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
4024 ? ".dynstr" : ".gnu.libstr");
4025 if (s != NULL)
4026 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4027 break;
4028
252b5132 4029 case SHT_HASH:
fdc90cb4 4030 case SHT_GNU_HASH:
252b5132
RH
4031 case SHT_GNU_versym:
4032 /* sh_link is the section header index of the symbol table
4033 this hash table or version table is for. */
4034 s = bfd_get_section_by_name (abfd, ".dynsym");
4035 if (s != NULL)
4036 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4037 break;
dbb410c3
AM
4038
4039 case SHT_GROUP:
12bd6957 4040 d->this_hdr.sh_link = elf_onesymtab (abfd);
252b5132
RH
4041 }
4042 }
4043
3e19fb8f
L
4044 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4045 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4046 debug section name from .debug_* to .zdebug_* if needed. */
4047
b34976b6 4048 return TRUE;
252b5132
RH
4049}
4050
5372391b 4051static bfd_boolean
217aa764 4052sym_is_global (bfd *abfd, asymbol *sym)
252b5132
RH
4053{
4054 /* If the backend has a special mapping, use it. */
9c5bfbb7 4055 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764
AM
4056 if (bed->elf_backend_sym_is_global)
4057 return (*bed->elf_backend_sym_is_global) (abfd, sym);
252b5132 4058
e47bf690 4059 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
e6f7f6d1
AM
4060 || bfd_is_und_section (bfd_asymbol_section (sym))
4061 || bfd_is_com_section (bfd_asymbol_section (sym)));
252b5132
RH
4062}
4063
76359541
TP
4064/* Filter global symbols of ABFD to include in the import library. All
4065 SYMCOUNT symbols of ABFD can be examined from their pointers in
4066 SYMS. Pointers of symbols to keep should be stored contiguously at
4067 the beginning of that array.
4068
4069 Returns the number of symbols to keep. */
4070
4071unsigned int
4072_bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4073 asymbol **syms, long symcount)
4074{
4075 long src_count, dst_count = 0;
4076
4077 for (src_count = 0; src_count < symcount; src_count++)
4078 {
4079 asymbol *sym = syms[src_count];
4080 char *name = (char *) bfd_asymbol_name (sym);
4081 struct bfd_link_hash_entry *h;
4082
4083 if (!sym_is_global (abfd, sym))
4084 continue;
4085
4086 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE);
5df1bc57
AM
4087 if (h == NULL)
4088 continue;
76359541
TP
4089 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4090 continue;
76359541
TP
4091 if (h->linker_def || h->ldscript_def)
4092 continue;
4093
4094 syms[dst_count++] = sym;
4095 }
4096
4097 syms[dst_count] = NULL;
4098
4099 return dst_count;
4100}
4101
5372391b 4102/* Don't output section symbols for sections that are not going to be
c6d8cab4 4103 output, that are duplicates or there is no BFD section. */
5372391b
AM
4104
4105static bfd_boolean
4106ignore_section_sym (bfd *abfd, asymbol *sym)
4107{
c6d8cab4
L
4108 elf_symbol_type *type_ptr;
4109
db0c309f
NC
4110 if (sym == NULL)
4111 return FALSE;
4112
c6d8cab4
L
4113 if ((sym->flags & BSF_SECTION_SYM) == 0)
4114 return FALSE;
4115
db0c309f
NC
4116 if (sym->section == NULL)
4117 return TRUE;
4118
c6d8cab4
L
4119 type_ptr = elf_symbol_from (abfd, sym);
4120 return ((type_ptr != NULL
4121 && type_ptr->internal_elf_sym.st_shndx != 0
4122 && bfd_is_abs_section (sym->section))
4123 || !(sym->section->owner == abfd
db0c309f
NC
4124 || (sym->section->output_section != NULL
4125 && sym->section->output_section->owner == abfd
2633a79c
AM
4126 && sym->section->output_offset == 0)
4127 || bfd_is_abs_section (sym->section)));
5372391b
AM
4128}
4129
2633a79c
AM
4130/* Map symbol from it's internal number to the external number, moving
4131 all local symbols to be at the head of the list. */
4132
b34976b6 4133static bfd_boolean
12bd6957 4134elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
252b5132 4135{
dc810e39 4136 unsigned int symcount = bfd_get_symcount (abfd);
252b5132
RH
4137 asymbol **syms = bfd_get_outsymbols (abfd);
4138 asymbol **sect_syms;
dc810e39
AM
4139 unsigned int num_locals = 0;
4140 unsigned int num_globals = 0;
4141 unsigned int num_locals2 = 0;
4142 unsigned int num_globals2 = 0;
7292b3ac 4143 unsigned int max_index = 0;
dc810e39 4144 unsigned int idx;
252b5132
RH
4145 asection *asect;
4146 asymbol **new_syms;
252b5132
RH
4147
4148#ifdef DEBUG
4149 fprintf (stderr, "elf_map_symbols\n");
4150 fflush (stderr);
4151#endif
4152
252b5132
RH
4153 for (asect = abfd->sections; asect; asect = asect->next)
4154 {
4155 if (max_index < asect->index)
4156 max_index = asect->index;
4157 }
4158
4159 max_index++;
a50b1753 4160 sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
252b5132 4161 if (sect_syms == NULL)
b34976b6 4162 return FALSE;
252b5132 4163 elf_section_syms (abfd) = sect_syms;
4e89ac30 4164 elf_num_section_syms (abfd) = max_index;
252b5132 4165
079e9a2f
AM
4166 /* Init sect_syms entries for any section symbols we have already
4167 decided to output. */
252b5132
RH
4168 for (idx = 0; idx < symcount; idx++)
4169 {
dc810e39 4170 asymbol *sym = syms[idx];
c044fabd 4171
252b5132 4172 if ((sym->flags & BSF_SECTION_SYM) != 0
0f0a5e58 4173 && sym->value == 0
2633a79c
AM
4174 && !ignore_section_sym (abfd, sym)
4175 && !bfd_is_abs_section (sym->section))
252b5132 4176 {
5372391b 4177 asection *sec = sym->section;
252b5132 4178
5372391b
AM
4179 if (sec->owner != abfd)
4180 sec = sec->output_section;
252b5132 4181
5372391b 4182 sect_syms[sec->index] = syms[idx];
252b5132
RH
4183 }
4184 }
4185
252b5132
RH
4186 /* Classify all of the symbols. */
4187 for (idx = 0; idx < symcount; idx++)
4188 {
2633a79c 4189 if (sym_is_global (abfd, syms[idx]))
252b5132 4190 num_globals++;
2633a79c
AM
4191 else if (!ignore_section_sym (abfd, syms[idx]))
4192 num_locals++;
252b5132 4193 }
079e9a2f 4194
5372391b 4195 /* We will be adding a section symbol for each normal BFD section. Most
079e9a2f
AM
4196 sections will already have a section symbol in outsymbols, but
4197 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4198 at least in that case. */
252b5132
RH
4199 for (asect = abfd->sections; asect; asect = asect->next)
4200 {
079e9a2f 4201 if (sect_syms[asect->index] == NULL)
252b5132 4202 {
079e9a2f 4203 if (!sym_is_global (abfd, asect->symbol))
252b5132
RH
4204 num_locals++;
4205 else
4206 num_globals++;
252b5132
RH
4207 }
4208 }
4209
4210 /* Now sort the symbols so the local symbols are first. */
a50b1753 4211 new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
07d6d2b8 4212 sizeof (asymbol *));
dc810e39 4213
252b5132 4214 if (new_syms == NULL)
b34976b6 4215 return FALSE;
252b5132
RH
4216
4217 for (idx = 0; idx < symcount; idx++)
4218 {
4219 asymbol *sym = syms[idx];
dc810e39 4220 unsigned int i;
252b5132 4221
2633a79c
AM
4222 if (sym_is_global (abfd, sym))
4223 i = num_locals + num_globals2++;
4224 else if (!ignore_section_sym (abfd, sym))
252b5132
RH
4225 i = num_locals2++;
4226 else
2633a79c 4227 continue;
252b5132
RH
4228 new_syms[i] = sym;
4229 sym->udata.i = i + 1;
4230 }
4231 for (asect = abfd->sections; asect; asect = asect->next)
4232 {
079e9a2f 4233 if (sect_syms[asect->index] == NULL)
252b5132 4234 {
079e9a2f 4235 asymbol *sym = asect->symbol;
dc810e39 4236 unsigned int i;
252b5132 4237
079e9a2f 4238 sect_syms[asect->index] = sym;
252b5132
RH
4239 if (!sym_is_global (abfd, sym))
4240 i = num_locals2++;
4241 else
4242 i = num_locals + num_globals2++;
4243 new_syms[i] = sym;
4244 sym->udata.i = i + 1;
4245 }
4246 }
4247
4248 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4249
12bd6957 4250 *pnum_locals = num_locals;
b34976b6 4251 return TRUE;
252b5132
RH
4252}
4253
4254/* Align to the maximum file alignment that could be required for any
4255 ELF data structure. */
4256
268b6b39 4257static inline file_ptr
217aa764 4258align_file_position (file_ptr off, int align)
252b5132
RH
4259{
4260 return (off + align - 1) & ~(align - 1);
4261}
4262
4263/* Assign a file position to a section, optionally aligning to the
4264 required section alignment. */
4265
217aa764
AM
4266file_ptr
4267_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4268 file_ptr offset,
4269 bfd_boolean align)
252b5132 4270{
72de5009
AM
4271 if (align && i_shdrp->sh_addralign > 1)
4272 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
252b5132
RH
4273 i_shdrp->sh_offset = offset;
4274 if (i_shdrp->bfd_section != NULL)
4275 i_shdrp->bfd_section->filepos = offset;
4276 if (i_shdrp->sh_type != SHT_NOBITS)
4277 offset += i_shdrp->sh_size;
4278 return offset;
4279}
4280
4281/* Compute the file positions we are going to put the sections at, and
4282 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4283 is not NULL, this is being called by the ELF backend linker. */
4284
b34976b6 4285bfd_boolean
217aa764
AM
4286_bfd_elf_compute_section_file_positions (bfd *abfd,
4287 struct bfd_link_info *link_info)
252b5132 4288{
9c5bfbb7 4289 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 4290 struct fake_section_arg fsargs;
b34976b6 4291 bfd_boolean failed;
ef10c3ac 4292 struct elf_strtab_hash *strtab = NULL;
252b5132 4293 Elf_Internal_Shdr *shstrtab_hdr;
3516e984 4294 bfd_boolean need_symtab;
252b5132
RH
4295
4296 if (abfd->output_has_begun)
b34976b6 4297 return TRUE;
252b5132
RH
4298
4299 /* Do any elf backend specific processing first. */
4300 if (bed->elf_backend_begin_write_processing)
4301 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4302
4303 if (! prep_headers (abfd))
b34976b6 4304 return FALSE;
252b5132 4305
e6c51ed4 4306 /* Post process the headers if necessary. */
78245035 4307 (*bed->elf_backend_post_process_headers) (abfd, link_info);
e6c51ed4 4308
d4730f92
BS
4309 fsargs.failed = FALSE;
4310 fsargs.link_info = link_info;
4311 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4312 if (fsargs.failed)
b34976b6 4313 return FALSE;
252b5132 4314
da9f89d4 4315 if (!assign_section_numbers (abfd, link_info))
b34976b6 4316 return FALSE;
252b5132
RH
4317
4318 /* The backend linker builds symbol table information itself. */
3516e984
L
4319 need_symtab = (link_info == NULL
4320 && (bfd_get_symcount (abfd) > 0
4321 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4322 == HAS_RELOC)));
4323 if (need_symtab)
252b5132
RH
4324 {
4325 /* Non-zero if doing a relocatable link. */
4326 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4327
4328 if (! swap_out_syms (abfd, &strtab, relocatable_p))
b34976b6 4329 return FALSE;
252b5132
RH
4330 }
4331
d4730f92 4332 failed = FALSE;
1126897b 4333 if (link_info == NULL)
dbb410c3 4334 {
1126897b 4335 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
dbb410c3 4336 if (failed)
b34976b6 4337 return FALSE;
dbb410c3
AM
4338 }
4339
252b5132
RH
4340 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4341 /* sh_name was set in prep_headers. */
4342 shstrtab_hdr->sh_type = SHT_STRTAB;
84865015 4343 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
252b5132 4344 shstrtab_hdr->sh_addr = 0;
946748d5 4345 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4346 shstrtab_hdr->sh_entsize = 0;
4347 shstrtab_hdr->sh_link = 0;
4348 shstrtab_hdr->sh_info = 0;
3e19fb8f 4349 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4350 shstrtab_hdr->sh_addralign = 1;
4351
c84fca4d 4352 if (!assign_file_positions_except_relocs (abfd, link_info))
b34976b6 4353 return FALSE;
252b5132 4354
3516e984 4355 if (need_symtab)
252b5132
RH
4356 {
4357 file_ptr off;
4358 Elf_Internal_Shdr *hdr;
4359
12bd6957 4360 off = elf_next_file_pos (abfd);
252b5132 4361
6a40cf0c 4362 hdr = & elf_symtab_hdr (abfd);
b34976b6 4363 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 4364
6a40cf0c
NC
4365 if (elf_symtab_shndx_list (abfd) != NULL)
4366 {
4367 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4368 if (hdr->sh_size != 0)
4369 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4370 /* FIXME: What about other symtab_shndx sections in the list ? */
4371 }
9ad5cbcf 4372
252b5132 4373 hdr = &elf_tdata (abfd)->strtab_hdr;
b34976b6 4374 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 4375
12bd6957 4376 elf_next_file_pos (abfd) = off;
252b5132
RH
4377
4378 /* Now that we know where the .strtab section goes, write it
08a40648 4379 out. */
252b5132 4380 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 4381 || ! _bfd_elf_strtab_emit (abfd, strtab))
b34976b6 4382 return FALSE;
ef10c3ac 4383 _bfd_elf_strtab_free (strtab);
252b5132
RH
4384 }
4385
b34976b6 4386 abfd->output_has_begun = TRUE;
252b5132 4387
b34976b6 4388 return TRUE;
252b5132
RH
4389}
4390
8ded5a0f
AM
4391/* Make an initial estimate of the size of the program header. If we
4392 get the number wrong here, we'll redo section placement. */
4393
4394static bfd_size_type
4395get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4396{
4397 size_t segs;
4398 asection *s;
2b05f1b7 4399 const struct elf_backend_data *bed;
8ded5a0f
AM
4400
4401 /* Assume we will need exactly two PT_LOAD segments: one for text
4402 and one for data. */
4403 segs = 2;
4404
4405 s = bfd_get_section_by_name (abfd, ".interp");
1b9e270b 4406 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
8ded5a0f
AM
4407 {
4408 /* If we have a loadable interpreter section, we need a
4409 PT_INTERP segment. In this case, assume we also need a
4410 PT_PHDR segment, although that may not be true for all
4411 targets. */
e9a38e0f 4412 segs += 2;
8ded5a0f
AM
4413 }
4414
4415 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4416 {
4417 /* We need a PT_DYNAMIC segment. */
4418 ++segs;
f210dcff 4419 }
08a40648 4420
ceae84aa 4421 if (info != NULL && info->relro)
f210dcff
L
4422 {
4423 /* We need a PT_GNU_RELRO segment. */
4424 ++segs;
8ded5a0f
AM
4425 }
4426
12bd6957 4427 if (elf_eh_frame_hdr (abfd))
8ded5a0f
AM
4428 {
4429 /* We need a PT_GNU_EH_FRAME segment. */
4430 ++segs;
4431 }
4432
12bd6957 4433 if (elf_stack_flags (abfd))
8ded5a0f 4434 {
2b05f1b7
L
4435 /* We need a PT_GNU_STACK segment. */
4436 ++segs;
4437 }
94b11780 4438
0a59decb
L
4439 s = bfd_get_section_by_name (abfd,
4440 NOTE_GNU_PROPERTY_SECTION_NAME);
4441 if (s != NULL && s->size != 0)
4442 {
4443 /* We need a PT_GNU_PROPERTY segment. */
4444 ++segs;
4445 }
4446
2b05f1b7
L
4447 for (s = abfd->sections; s != NULL; s = s->next)
4448 {
8ded5a0f 4449 if ((s->flags & SEC_LOAD) != 0
23e463ed 4450 && elf_section_type (s) == SHT_NOTE)
8ded5a0f 4451 {
23e463ed 4452 unsigned int alignment_power;
8ded5a0f
AM
4453 /* We need a PT_NOTE segment. */
4454 ++segs;
23e463ed
L
4455 /* Try to create just one PT_NOTE segment for all adjacent
4456 loadable SHT_NOTE sections. gABI requires that within a
4457 PT_NOTE segment (and also inside of each SHT_NOTE section)
4458 each note should have the same alignment. So we check
4459 whether the sections are correctly aligned. */
4460 alignment_power = s->alignment_power;
4461 while (s->next != NULL
4462 && s->next->alignment_power == alignment_power
4463 && (s->next->flags & SEC_LOAD) != 0
4464 && elf_section_type (s->next) == SHT_NOTE)
4465 s = s->next;
8ded5a0f
AM
4466 }
4467 }
4468
4469 for (s = abfd->sections; s != NULL; s = s->next)
4470 {
4471 if (s->flags & SEC_THREAD_LOCAL)
4472 {
4473 /* We need a PT_TLS segment. */
4474 ++segs;
4475 break;
4476 }
4477 }
4478
2b05f1b7 4479 bed = get_elf_backend_data (abfd);
a91e1603 4480
df3a023b
AM
4481 if ((abfd->flags & D_PAGED) != 0
4482 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4483 {
4484 /* Add a PT_GNU_MBIND segment for each mbind section. */
4485 unsigned int page_align_power = bfd_log2 (bed->commonpagesize);
4486 for (s = abfd->sections; s != NULL; s = s->next)
4487 if (elf_section_flags (s) & SHF_GNU_MBIND)
4488 {
4489 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4490 {
4491 _bfd_error_handler
4492 /* xgettext:c-format */
4493 (_("%pB: GNU_MBIND section `%pA' has invalid "
4494 "sh_info field: %d"),
4495 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4496 continue;
4497 }
4498 /* Align mbind section to page size. */
4499 if (s->alignment_power < page_align_power)
4500 s->alignment_power = page_align_power;
4501 segs ++;
4502 }
4503 }
4504
4505 /* Let the backend count up any program headers it might need. */
4506 if (bed->elf_backend_additional_program_headers)
8ded5a0f
AM
4507 {
4508 int a;
4509
4510 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4511 if (a == -1)
4512 abort ();
4513 segs += a;
4514 }
4515
4516 return segs * bed->s->sizeof_phdr;
4517}
4518
2ea37f1c
NC
4519/* Find the segment that contains the output_section of section. */
4520
4521Elf_Internal_Phdr *
4522_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4523{
4524 struct elf_segment_map *m;
4525 Elf_Internal_Phdr *p;
4526
12bd6957 4527 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
2ea37f1c
NC
4528 m != NULL;
4529 m = m->next, p++)
4530 {
4531 int i;
4532
4533 for (i = m->count - 1; i >= 0; i--)
4534 if (m->sections[i] == section)
4535 return p;
4536 }
4537
4538 return NULL;
4539}
4540
252b5132
RH
4541/* Create a mapping from a set of sections to a program segment. */
4542
217aa764
AM
4543static struct elf_segment_map *
4544make_mapping (bfd *abfd,
4545 asection **sections,
4546 unsigned int from,
4547 unsigned int to,
4548 bfd_boolean phdr)
252b5132
RH
4549{
4550 struct elf_segment_map *m;
4551 unsigned int i;
4552 asection **hdrpp;
dc810e39 4553 bfd_size_type amt;
252b5132 4554
00bee008
AM
4555 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4556 amt += (to - from) * sizeof (asection *);
a50b1753 4557 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
4558 if (m == NULL)
4559 return NULL;
4560 m->next = NULL;
4561 m->p_type = PT_LOAD;
4562 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4563 m->sections[i - from] = *hdrpp;
4564 m->count = to - from;
4565
4566 if (from == 0 && phdr)
4567 {
4568 /* Include the headers in the first PT_LOAD segment. */
4569 m->includes_filehdr = 1;
4570 m->includes_phdrs = 1;
4571 }
4572
4573 return m;
4574}
4575
229fcec5
MM
4576/* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4577 on failure. */
4578
4579struct elf_segment_map *
4580_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4581{
4582 struct elf_segment_map *m;
4583
a50b1753 4584 m = (struct elf_segment_map *) bfd_zalloc (abfd,
07d6d2b8 4585 sizeof (struct elf_segment_map));
229fcec5
MM
4586 if (m == NULL)
4587 return NULL;
4588 m->next = NULL;
4589 m->p_type = PT_DYNAMIC;
4590 m->count = 1;
4591 m->sections[0] = dynsec;
08a40648 4592
229fcec5
MM
4593 return m;
4594}
4595
8ded5a0f 4596/* Possibly add or remove segments from the segment map. */
252b5132 4597
b34976b6 4598static bfd_boolean
3dea8fca
AM
4599elf_modify_segment_map (bfd *abfd,
4600 struct bfd_link_info *info,
4601 bfd_boolean remove_empty_load)
252b5132 4602{
252e386e 4603 struct elf_segment_map **m;
8ded5a0f 4604 const struct elf_backend_data *bed;
252b5132 4605
8ded5a0f
AM
4606 /* The placement algorithm assumes that non allocated sections are
4607 not in PT_LOAD segments. We ensure this here by removing such
4608 sections from the segment map. We also remove excluded
252e386e
AM
4609 sections. Finally, any PT_LOAD segment without sections is
4610 removed. */
12bd6957 4611 m = &elf_seg_map (abfd);
252e386e 4612 while (*m)
8ded5a0f
AM
4613 {
4614 unsigned int i, new_count;
252b5132 4615
252e386e 4616 for (new_count = 0, i = 0; i < (*m)->count; i++)
8ded5a0f 4617 {
252e386e
AM
4618 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4619 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4620 || (*m)->p_type != PT_LOAD))
8ded5a0f 4621 {
252e386e
AM
4622 (*m)->sections[new_count] = (*m)->sections[i];
4623 new_count++;
8ded5a0f
AM
4624 }
4625 }
252e386e 4626 (*m)->count = new_count;
252b5132 4627
1a9ccd70
NC
4628 if (remove_empty_load
4629 && (*m)->p_type == PT_LOAD
4630 && (*m)->count == 0
4631 && !(*m)->includes_phdrs)
252e386e
AM
4632 *m = (*m)->next;
4633 else
4634 m = &(*m)->next;
8ded5a0f 4635 }
252b5132 4636
8ded5a0f
AM
4637 bed = get_elf_backend_data (abfd);
4638 if (bed->elf_backend_modify_segment_map != NULL)
252b5132 4639 {
252e386e 4640 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
8ded5a0f 4641 return FALSE;
252b5132 4642 }
252b5132 4643
8ded5a0f
AM
4644 return TRUE;
4645}
252b5132 4646
dbc88fc1
AM
4647#define IS_TBSS(s) \
4648 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4649
8ded5a0f 4650/* Set up a mapping from BFD sections to program segments. */
252b5132 4651
8ded5a0f
AM
4652bfd_boolean
4653_bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
4654{
4655 unsigned int count;
4656 struct elf_segment_map *m;
4657 asection **sections = NULL;
4658 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3dea8fca 4659 bfd_boolean no_user_phdrs;
252b5132 4660
12bd6957 4661 no_user_phdrs = elf_seg_map (abfd) == NULL;
d324f6d6
RM
4662
4663 if (info != NULL)
4664 info->user_phdrs = !no_user_phdrs;
4665
3dea8fca 4666 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
252b5132 4667 {
8ded5a0f
AM
4668 asection *s;
4669 unsigned int i;
4670 struct elf_segment_map *mfirst;
4671 struct elf_segment_map **pm;
4672 asection *last_hdr;
4673 bfd_vma last_size;
00bee008 4674 unsigned int hdr_index;
8ded5a0f
AM
4675 bfd_vma maxpagesize;
4676 asection **hdrpp;
64029e93 4677 bfd_boolean phdr_in_segment;
8ded5a0f 4678 bfd_boolean writable;
2888249f 4679 bfd_boolean executable;
8ded5a0f
AM
4680 int tls_count = 0;
4681 asection *first_tls = NULL;
a91e1603 4682 asection *first_mbind = NULL;
8ded5a0f
AM
4683 asection *dynsec, *eh_frame_hdr;
4684 bfd_size_type amt;
8d06853e 4685 bfd_vma addr_mask, wrap_to = 0;
64029e93 4686 bfd_size_type phdr_size;
252b5132 4687
8ded5a0f 4688 /* Select the allocated sections, and sort them. */
252b5132 4689
a50b1753 4690 sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
07d6d2b8 4691 sizeof (asection *));
8ded5a0f 4692 if (sections == NULL)
252b5132 4693 goto error_return;
252b5132 4694
8d06853e
AM
4695 /* Calculate top address, avoiding undefined behaviour of shift
4696 left operator when shift count is equal to size of type
4697 being shifted. */
4698 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4699 addr_mask = (addr_mask << 1) + 1;
4700
8ded5a0f
AM
4701 i = 0;
4702 for (s = abfd->sections; s != NULL; s = s->next)
4703 {
4704 if ((s->flags & SEC_ALLOC) != 0)
4705 {
48db3297
AM
4706 /* target_index is unused until bfd_elf_final_link
4707 starts output of section symbols. Use it to make
4708 qsort stable. */
4709 s->target_index = i;
8ded5a0f
AM
4710 sections[i] = s;
4711 ++i;
8d06853e
AM
4712 /* A wrapping section potentially clashes with header. */
4713 if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
4714 wrap_to = (s->lma + s->size) & addr_mask;
8ded5a0f
AM
4715 }
4716 }
4717 BFD_ASSERT (i <= bfd_count_sections (abfd));
4718 count = i;
252b5132 4719
8ded5a0f 4720 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
252b5132 4721
64029e93
AM
4722 phdr_size = elf_program_header_size (abfd);
4723 if (phdr_size == (bfd_size_type) -1)
4724 phdr_size = get_program_header_size (abfd, info);
4725 phdr_size += bed->s->sizeof_ehdr;
4726 maxpagesize = bed->maxpagesize;
4727 if (maxpagesize == 0)
4728 maxpagesize = 1;
4729 phdr_in_segment = info != NULL && info->load_phdrs;
4730 if (count != 0
4731 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
4732 >= (phdr_size & (maxpagesize - 1))))
4733 /* For compatibility with old scripts that may not be using
4734 SIZEOF_HEADERS, add headers when it looks like space has
4735 been left for them. */
4736 phdr_in_segment = TRUE;
252b5132 4737
64029e93 4738 /* Build the mapping. */
8ded5a0f
AM
4739 mfirst = NULL;
4740 pm = &mfirst;
252b5132 4741
8ded5a0f
AM
4742 /* If we have a .interp section, then create a PT_PHDR segment for
4743 the program headers and a PT_INTERP segment for the .interp
4744 section. */
4745 s = bfd_get_section_by_name (abfd, ".interp");
1b9e270b 4746 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
8ded5a0f
AM
4747 {
4748 amt = sizeof (struct elf_segment_map);
a50b1753 4749 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4750 if (m == NULL)
4751 goto error_return;
4752 m->next = NULL;
4753 m->p_type = PT_PHDR;
f882209d 4754 m->p_flags = PF_R;
8ded5a0f
AM
4755 m->p_flags_valid = 1;
4756 m->includes_phdrs = 1;
64029e93 4757 phdr_in_segment = TRUE;
8ded5a0f
AM
4758 *pm = m;
4759 pm = &m->next;
252b5132 4760
8ded5a0f 4761 amt = sizeof (struct elf_segment_map);
a50b1753 4762 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4763 if (m == NULL)
4764 goto error_return;
4765 m->next = NULL;
4766 m->p_type = PT_INTERP;
4767 m->count = 1;
4768 m->sections[0] = s;
4769
4770 *pm = m;
4771 pm = &m->next;
252b5132 4772 }
8ded5a0f
AM
4773
4774 /* Look through the sections. We put sections in the same program
4775 segment when the start of the second section can be placed within
4776 a few bytes of the end of the first section. */
4777 last_hdr = NULL;
4778 last_size = 0;
00bee008 4779 hdr_index = 0;
8ded5a0f 4780 writable = FALSE;
2888249f 4781 executable = FALSE;
8ded5a0f
AM
4782 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4783 if (dynsec != NULL
4784 && (dynsec->flags & SEC_LOAD) == 0)
4785 dynsec = NULL;
4786
64029e93
AM
4787 if ((abfd->flags & D_PAGED) == 0)
4788 phdr_in_segment = FALSE;
4789
8ded5a0f
AM
4790 /* Deal with -Ttext or something similar such that the first section
4791 is not adjacent to the program headers. This is an
4792 approximation, since at this point we don't know exactly how many
4793 program headers we will need. */
64029e93 4794 if (phdr_in_segment && count > 0)
252b5132 4795 {
64029e93
AM
4796 bfd_vma phdr_lma;
4797 bfd_boolean separate_phdr = FALSE;
4798
4799 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
4800 if (info != NULL
4801 && info->separate_code
4802 && (sections[0]->flags & SEC_CODE) != 0)
1a9ccd70 4803 {
64029e93
AM
4804 /* If data sections should be separate from code and
4805 thus not executable, and the first section is
4806 executable then put the file and program headers in
4807 their own PT_LOAD. */
4808 separate_phdr = TRUE;
4809 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
4810 == (sections[0]->lma & addr_mask & -maxpagesize)))
4811 {
4812 /* The file and program headers are currently on the
4813 same page as the first section. Put them on the
4814 previous page if we can. */
4815 if (phdr_lma >= maxpagesize)
4816 phdr_lma -= maxpagesize;
4817 else
4818 separate_phdr = FALSE;
4819 }
4820 }
4821 if ((sections[0]->lma & addr_mask) < phdr_lma
4822 || (sections[0]->lma & addr_mask) < phdr_size)
4823 /* If file and program headers would be placed at the end
4824 of memory then it's probably better to omit them. */
4825 phdr_in_segment = FALSE;
4826 else if (phdr_lma < wrap_to)
4827 /* If a section wraps around to where we'll be placing
4828 file and program headers, then the headers will be
4829 overwritten. */
4830 phdr_in_segment = FALSE;
4831 else if (separate_phdr)
4832 {
4833 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
4834 if (m == NULL)
4835 goto error_return;
4836 m->p_paddr = phdr_lma;
4837 m->p_vaddr_offset
4838 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
4839 m->p_paddr_valid = 1;
4840 *pm = m;
4841 pm = &m->next;
4842 phdr_in_segment = FALSE;
1a9ccd70 4843 }
252b5132
RH
4844 }
4845
8ded5a0f 4846 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
252b5132 4847 {
8ded5a0f
AM
4848 asection *hdr;
4849 bfd_boolean new_segment;
4850
4851 hdr = *hdrpp;
4852
4853 /* See if this section and the last one will fit in the same
4854 segment. */
4855
4856 if (last_hdr == NULL)
4857 {
4858 /* If we don't have a segment yet, then we don't need a new
4859 one (we build the last one after this loop). */
4860 new_segment = FALSE;
4861 }
4862 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4863 {
4864 /* If this section has a different relation between the
4865 virtual address and the load address, then we need a new
4866 segment. */
4867 new_segment = TRUE;
4868 }
b5599592
AM
4869 else if (hdr->lma < last_hdr->lma + last_size
4870 || last_hdr->lma + last_size < last_hdr->lma)
4871 {
4872 /* If this section has a load address that makes it overlap
4873 the previous section, then we need a new segment. */
4874 new_segment = TRUE;
4875 }
76cb3a89
AM
4876 else if ((abfd->flags & D_PAGED) != 0
4877 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4878 == (hdr->lma & -maxpagesize)))
4879 {
4880 /* If we are demand paged then we can't map two disk
4881 pages onto the same memory page. */
4882 new_segment = FALSE;
4883 }
39948a60
NC
4884 /* In the next test we have to be careful when last_hdr->lma is close
4885 to the end of the address space. If the aligned address wraps
4886 around to the start of the address space, then there are no more
4887 pages left in memory and it is OK to assume that the current
4888 section can be included in the current segment. */
76cb3a89
AM
4889 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4890 + maxpagesize > last_hdr->lma)
4891 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4892 + maxpagesize <= hdr->lma))
8ded5a0f
AM
4893 {
4894 /* If putting this section in this segment would force us to
4895 skip a page in the segment, then we need a new segment. */
4896 new_segment = TRUE;
4897 }
4898 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
76cb3a89 4899 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
8ded5a0f 4900 {
e5654c0f
AM
4901 /* We don't want to put a loaded section after a
4902 nonloaded (ie. bss style) section in the same segment
4903 as that will force the non-loaded section to be loaded.
76cb3a89 4904 Consider .tbss sections as loaded for this purpose. */
8ded5a0f
AM
4905 new_segment = TRUE;
4906 }
4907 else if ((abfd->flags & D_PAGED) == 0)
4908 {
4909 /* If the file is not demand paged, which means that we
4910 don't require the sections to be correctly aligned in the
4911 file, then there is no other reason for a new segment. */
4912 new_segment = FALSE;
4913 }
2888249f
L
4914 else if (info != NULL
4915 && info->separate_code
4916 && executable != ((hdr->flags & SEC_CODE) != 0))
4917 {
4918 new_segment = TRUE;
4919 }
8ded5a0f 4920 else if (! writable
76cb3a89 4921 && (hdr->flags & SEC_READONLY) == 0)
8ded5a0f
AM
4922 {
4923 /* We don't want to put a writable section in a read only
76cb3a89 4924 segment. */
8ded5a0f
AM
4925 new_segment = TRUE;
4926 }
4927 else
4928 {
4929 /* Otherwise, we can use the same segment. */
4930 new_segment = FALSE;
4931 }
4932
2889e75b 4933 /* Allow interested parties a chance to override our decision. */
ceae84aa
AM
4934 if (last_hdr != NULL
4935 && info != NULL
4936 && info->callbacks->override_segment_assignment != NULL)
4937 new_segment
4938 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4939 last_hdr,
4940 new_segment);
2889e75b 4941
8ded5a0f
AM
4942 if (! new_segment)
4943 {
4944 if ((hdr->flags & SEC_READONLY) == 0)
4945 writable = TRUE;
2888249f
L
4946 if ((hdr->flags & SEC_CODE) != 0)
4947 executable = TRUE;
8ded5a0f
AM
4948 last_hdr = hdr;
4949 /* .tbss sections effectively have zero size. */
dbc88fc1 4950 last_size = !IS_TBSS (hdr) ? hdr->size : 0;
8ded5a0f
AM
4951 continue;
4952 }
4953
4954 /* We need a new program segment. We must create a new program
00bee008 4955 header holding all the sections from hdr_index until hdr. */
8ded5a0f 4956
00bee008 4957 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
8ded5a0f
AM
4958 if (m == NULL)
4959 goto error_return;
4960
4961 *pm = m;
4962 pm = &m->next;
4963
252b5132 4964 if ((hdr->flags & SEC_READONLY) == 0)
b34976b6 4965 writable = TRUE;
8ded5a0f
AM
4966 else
4967 writable = FALSE;
4968
2888249f
L
4969 if ((hdr->flags & SEC_CODE) == 0)
4970 executable = FALSE;
4971 else
4972 executable = TRUE;
4973
baaff79e
JJ
4974 last_hdr = hdr;
4975 /* .tbss sections effectively have zero size. */
dbc88fc1 4976 last_size = !IS_TBSS (hdr) ? hdr->size : 0;
00bee008 4977 hdr_index = i;
8ded5a0f 4978 phdr_in_segment = FALSE;
252b5132
RH
4979 }
4980
86b2281f
AM
4981 /* Create a final PT_LOAD program segment, but not if it's just
4982 for .tbss. */
4983 if (last_hdr != NULL
00bee008 4984 && (i - hdr_index != 1
dbc88fc1 4985 || !IS_TBSS (last_hdr)))
8ded5a0f 4986 {
00bee008 4987 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
8ded5a0f
AM
4988 if (m == NULL)
4989 goto error_return;
252b5132 4990
8ded5a0f
AM
4991 *pm = m;
4992 pm = &m->next;
4993 }
252b5132 4994
8ded5a0f
AM
4995 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4996 if (dynsec != NULL)
4997 {
4998 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4999 if (m == NULL)
5000 goto error_return;
5001 *pm = m;
5002 pm = &m->next;
5003 }
252b5132 5004
23e463ed 5005 /* For each batch of consecutive loadable SHT_NOTE sections,
1c5265b5
JJ
5006 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5007 because if we link together nonloadable .note sections and
5008 loadable .note sections, we will generate two .note sections
23e463ed 5009 in the output file. */
8ded5a0f
AM
5010 for (s = abfd->sections; s != NULL; s = s->next)
5011 {
5012 if ((s->flags & SEC_LOAD) != 0
23e463ed 5013 && elf_section_type (s) == SHT_NOTE)
8ded5a0f 5014 {
1c5265b5 5015 asection *s2;
23e463ed 5016 unsigned int alignment_power = s->alignment_power;
91d6fa6a
NC
5017
5018 count = 1;
23e463ed
L
5019 for (s2 = s; s2->next != NULL; s2 = s2->next)
5020 {
5021 if (s2->next->alignment_power == alignment_power
5022 && (s2->next->flags & SEC_LOAD) != 0
5023 && elf_section_type (s2->next) == SHT_NOTE
5024 && align_power (s2->lma + s2->size,
5025 alignment_power)
5026 == s2->next->lma)
5027 count++;
5028 else
5029 break;
5030 }
00bee008
AM
5031 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5032 amt += count * sizeof (asection *);
a50b1753 5033 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5034 if (m == NULL)
5035 goto error_return;
5036 m->next = NULL;
5037 m->p_type = PT_NOTE;
1c5265b5
JJ
5038 m->count = count;
5039 while (count > 1)
5040 {
5041 m->sections[m->count - count--] = s;
5042 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5043 s = s->next;
5044 }
5045 m->sections[m->count - 1] = s;
5046 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
8ded5a0f
AM
5047 *pm = m;
5048 pm = &m->next;
5049 }
5050 if (s->flags & SEC_THREAD_LOCAL)
5051 {
5052 if (! tls_count)
5053 first_tls = s;
5054 tls_count++;
5055 }
a91e1603
L
5056 if (first_mbind == NULL
5057 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5058 first_mbind = s;
8ded5a0f 5059 }
252b5132 5060
8ded5a0f
AM
5061 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5062 if (tls_count > 0)
5063 {
00bee008
AM
5064 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5065 amt += tls_count * sizeof (asection *);
a50b1753 5066 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5067 if (m == NULL)
5068 goto error_return;
5069 m->next = NULL;
5070 m->p_type = PT_TLS;
5071 m->count = tls_count;
5072 /* Mandated PF_R. */
5073 m->p_flags = PF_R;
5074 m->p_flags_valid = 1;
d923cae0 5075 s = first_tls;
91d6fa6a 5076 for (i = 0; i < (unsigned int) tls_count; ++i)
8ded5a0f 5077 {
d923cae0
L
5078 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5079 {
5080 _bfd_error_handler
871b3ab2 5081 (_("%pB: TLS sections are not adjacent:"), abfd);
d923cae0
L
5082 s = first_tls;
5083 i = 0;
5084 while (i < (unsigned int) tls_count)
5085 {
5086 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5087 {
871b3ab2 5088 _bfd_error_handler (_(" TLS: %pA"), s);
d923cae0
L
5089 i++;
5090 }
5091 else
871b3ab2 5092 _bfd_error_handler (_(" non-TLS: %pA"), s);
d923cae0
L
5093 s = s->next;
5094 }
5095 bfd_set_error (bfd_error_bad_value);
5096 goto error_return;
5097 }
5098 m->sections[i] = s;
5099 s = s->next;
8ded5a0f 5100 }
252b5132 5101
8ded5a0f
AM
5102 *pm = m;
5103 pm = &m->next;
5104 }
252b5132 5105
df3a023b
AM
5106 if (first_mbind
5107 && (abfd->flags & D_PAGED) != 0
5108 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
a91e1603
L
5109 for (s = first_mbind; s != NULL; s = s->next)
5110 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
df3a023b 5111 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
a91e1603
L
5112 {
5113 /* Mandated PF_R. */
5114 unsigned long p_flags = PF_R;
5115 if ((s->flags & SEC_READONLY) == 0)
5116 p_flags |= PF_W;
5117 if ((s->flags & SEC_CODE) != 0)
5118 p_flags |= PF_X;
5119
5120 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5121 m = bfd_zalloc (abfd, amt);
5122 if (m == NULL)
5123 goto error_return;
5124 m->next = NULL;
5125 m->p_type = (PT_GNU_MBIND_LO
5126 + elf_section_data (s)->this_hdr.sh_info);
5127 m->count = 1;
5128 m->p_flags_valid = 1;
5129 m->sections[0] = s;
5130 m->p_flags = p_flags;
5131
5132 *pm = m;
5133 pm = &m->next;
5134 }
5135
0a59decb
L
5136 s = bfd_get_section_by_name (abfd,
5137 NOTE_GNU_PROPERTY_SECTION_NAME);
5138 if (s != NULL && s->size != 0)
5139 {
5140 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5141 m = bfd_zalloc (abfd, amt);
5142 if (m == NULL)
5143 goto error_return;
5144 m->next = NULL;
5145 m->p_type = PT_GNU_PROPERTY;
5146 m->count = 1;
5147 m->p_flags_valid = 1;
5148 m->sections[0] = s;
5149 m->p_flags = PF_R;
5150 *pm = m;
5151 pm = &m->next;
5152 }
5153
8ded5a0f
AM
5154 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5155 segment. */
12bd6957 5156 eh_frame_hdr = elf_eh_frame_hdr (abfd);
8ded5a0f
AM
5157 if (eh_frame_hdr != NULL
5158 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
252b5132 5159 {
dc810e39 5160 amt = sizeof (struct elf_segment_map);
a50b1753 5161 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
5162 if (m == NULL)
5163 goto error_return;
5164 m->next = NULL;
8ded5a0f 5165 m->p_type = PT_GNU_EH_FRAME;
252b5132 5166 m->count = 1;
8ded5a0f 5167 m->sections[0] = eh_frame_hdr->output_section;
252b5132
RH
5168
5169 *pm = m;
5170 pm = &m->next;
5171 }
13ae64f3 5172
12bd6957 5173 if (elf_stack_flags (abfd))
13ae64f3 5174 {
8ded5a0f 5175 amt = sizeof (struct elf_segment_map);
a50b1753 5176 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5177 if (m == NULL)
5178 goto error_return;
5179 m->next = NULL;
2b05f1b7 5180 m->p_type = PT_GNU_STACK;
12bd6957 5181 m->p_flags = elf_stack_flags (abfd);
04c3a755 5182 m->p_align = bed->stack_align;
8ded5a0f 5183 m->p_flags_valid = 1;
04c3a755
NS
5184 m->p_align_valid = m->p_align != 0;
5185 if (info->stacksize > 0)
5186 {
5187 m->p_size = info->stacksize;
5188 m->p_size_valid = 1;
5189 }
252b5132 5190
8ded5a0f
AM
5191 *pm = m;
5192 pm = &m->next;
5193 }
65765700 5194
ceae84aa 5195 if (info != NULL && info->relro)
8ded5a0f 5196 {
f210dcff
L
5197 for (m = mfirst; m != NULL; m = m->next)
5198 {
3832a4d8
AM
5199 if (m->p_type == PT_LOAD
5200 && m->count != 0
5201 && m->sections[0]->vma >= info->relro_start
5202 && m->sections[0]->vma < info->relro_end)
f210dcff 5203 {
3832a4d8
AM
5204 i = m->count;
5205 while (--i != (unsigned) -1)
5206 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
5207 == (SEC_LOAD | SEC_HAS_CONTENTS))
5208 break;
5209
43a8475c 5210 if (i != (unsigned) -1)
f210dcff
L
5211 break;
5212 }
be01b344 5213 }
f210dcff
L
5214
5215 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5216 if (m != NULL)
5217 {
5218 amt = sizeof (struct elf_segment_map);
a50b1753 5219 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
f210dcff
L
5220 if (m == NULL)
5221 goto error_return;
5222 m->next = NULL;
5223 m->p_type = PT_GNU_RELRO;
f210dcff
L
5224 *pm = m;
5225 pm = &m->next;
5226 }
8ded5a0f 5227 }
9ee5e499 5228
8ded5a0f 5229 free (sections);
12bd6957 5230 elf_seg_map (abfd) = mfirst;
9ee5e499
JJ
5231 }
5232
3dea8fca 5233 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
8ded5a0f 5234 return FALSE;
8c37241b 5235
12bd6957 5236 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
8ded5a0f 5237 ++count;
12bd6957 5238 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
252b5132 5239
b34976b6 5240 return TRUE;
252b5132
RH
5241
5242 error_return:
5243 if (sections != NULL)
5244 free (sections);
b34976b6 5245 return FALSE;
252b5132
RH
5246}
5247
5248/* Sort sections by address. */
5249
5250static int
217aa764 5251elf_sort_sections (const void *arg1, const void *arg2)
252b5132
RH
5252{
5253 const asection *sec1 = *(const asection **) arg1;
5254 const asection *sec2 = *(const asection **) arg2;
eecdbe52 5255 bfd_size_type size1, size2;
252b5132
RH
5256
5257 /* Sort by LMA first, since this is the address used to
5258 place the section into a segment. */
5259 if (sec1->lma < sec2->lma)
5260 return -1;
5261 else if (sec1->lma > sec2->lma)
5262 return 1;
5263
5264 /* Then sort by VMA. Normally the LMA and the VMA will be
5265 the same, and this will do nothing. */
5266 if (sec1->vma < sec2->vma)
5267 return -1;
5268 else if (sec1->vma > sec2->vma)
5269 return 1;
5270
5271 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5272
07c6e936 5273#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
252b5132
RH
5274
5275 if (TOEND (sec1))
5276 {
48db3297 5277 if (!TOEND (sec2))
252b5132
RH
5278 return 1;
5279 }
00a7cdc5 5280 else if (TOEND (sec2))
252b5132
RH
5281 return -1;
5282
5283#undef TOEND
5284
00a7cdc5
NC
5285 /* Sort by size, to put zero sized sections
5286 before others at the same address. */
252b5132 5287
eea6121a
AM
5288 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5289 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
eecdbe52
JJ
5290
5291 if (size1 < size2)
252b5132 5292 return -1;
eecdbe52 5293 if (size1 > size2)
252b5132
RH
5294 return 1;
5295
5296 return sec1->target_index - sec2->target_index;
5297}
5298
340b6d91
AC
5299/* Ian Lance Taylor writes:
5300
5301 We shouldn't be using % with a negative signed number. That's just
5302 not good. We have to make sure either that the number is not
5303 negative, or that the number has an unsigned type. When the types
5304 are all the same size they wind up as unsigned. When file_ptr is a
5305 larger signed type, the arithmetic winds up as signed long long,
5306 which is wrong.
5307
5308 What we're trying to say here is something like ``increase OFF by
5309 the least amount that will cause it to be equal to the VMA modulo
5310 the page size.'' */
5311/* In other words, something like:
5312
5313 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5314 off_offset = off % bed->maxpagesize;
5315 if (vma_offset < off_offset)
5316 adjustment = vma_offset + bed->maxpagesize - off_offset;
5317 else
5318 adjustment = vma_offset - off_offset;
08a40648 5319
de194d85 5320 which can be collapsed into the expression below. */
340b6d91
AC
5321
5322static file_ptr
5323vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5324{
dc9155b2
NC
5325 /* PR binutils/16199: Handle an alignment of zero. */
5326 if (maxpagesize == 0)
5327 maxpagesize = 1;
340b6d91
AC
5328 return ((vma - off) % maxpagesize);
5329}
5330
6d33f217
L
5331static void
5332print_segment_map (const struct elf_segment_map *m)
5333{
5334 unsigned int j;
5335 const char *pt = get_segment_type (m->p_type);
5336 char buf[32];
5337
5338 if (pt == NULL)
5339 {
5340 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5341 sprintf (buf, "LOPROC+%7.7x",
5342 (unsigned int) (m->p_type - PT_LOPROC));
5343 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5344 sprintf (buf, "LOOS+%7.7x",
5345 (unsigned int) (m->p_type - PT_LOOS));
5346 else
5347 snprintf (buf, sizeof (buf), "%8.8x",
5348 (unsigned int) m->p_type);
5349 pt = buf;
5350 }
4a97a0e5 5351 fflush (stdout);
6d33f217
L
5352 fprintf (stderr, "%s:", pt);
5353 for (j = 0; j < m->count; j++)
5354 fprintf (stderr, " %s", m->sections [j]->name);
5355 putc ('\n',stderr);
4a97a0e5 5356 fflush (stderr);
6d33f217
L
5357}
5358
32812159
AM
5359static bfd_boolean
5360write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
5361{
5362 void *buf;
5363 bfd_boolean ret;
5364
5365 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5366 return FALSE;
5367 buf = bfd_zmalloc (len);
5368 if (buf == NULL)
5369 return FALSE;
5370 ret = bfd_bwrite (buf, len, abfd) == len;
5371 free (buf);
5372 return ret;
5373}
5374
252b5132
RH
5375/* Assign file positions to the sections based on the mapping from
5376 sections to segments. This function also sets up some fields in
f3520d2f 5377 the file header. */
252b5132 5378
b34976b6 5379static bfd_boolean
f3520d2f
AM
5380assign_file_positions_for_load_sections (bfd *abfd,
5381 struct bfd_link_info *link_info)
252b5132
RH
5382{
5383 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 5384 struct elf_segment_map *m;
252b5132 5385 Elf_Internal_Phdr *phdrs;
252b5132 5386 Elf_Internal_Phdr *p;
02bf8d82 5387 file_ptr off;
3f570048 5388 bfd_size_type maxpagesize;
a8c75b76 5389 unsigned int pt_load_count = 0;
f3520d2f 5390 unsigned int alloc;
0920dee7 5391 unsigned int i, j;
2b0bc088 5392 bfd_vma header_pad = 0;
252b5132 5393
e36284ab 5394 if (link_info == NULL
ceae84aa 5395 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
8ded5a0f 5396 return FALSE;
252b5132 5397
8ded5a0f 5398 alloc = 0;
12bd6957 5399 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
2b0bc088
NC
5400 {
5401 ++alloc;
5402 if (m->header_size)
5403 header_pad = m->header_size;
5404 }
252b5132 5405
82f2dbf7
NC
5406 if (alloc)
5407 {
5408 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5409 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5410 }
5411 else
5412 {
5413 /* PR binutils/12467. */
5414 elf_elfheader (abfd)->e_phoff = 0;
5415 elf_elfheader (abfd)->e_phentsize = 0;
5416 }
d324f6d6 5417
8ded5a0f 5418 elf_elfheader (abfd)->e_phnum = alloc;
252b5132 5419
12bd6957
AM
5420 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5421 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
8ded5a0f 5422 else
12bd6957 5423 BFD_ASSERT (elf_program_header_size (abfd)
59e0647f 5424 >= alloc * bed->s->sizeof_phdr);
252b5132
RH
5425
5426 if (alloc == 0)
f3520d2f 5427 {
12bd6957 5428 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
8ded5a0f 5429 return TRUE;
f3520d2f 5430 }
252b5132 5431
12bd6957 5432 /* We're writing the size in elf_program_header_size (abfd),
57268894
HPN
5433 see assign_file_positions_except_relocs, so make sure we have
5434 that amount allocated, with trailing space cleared.
12bd6957
AM
5435 The variable alloc contains the computed need, while
5436 elf_program_header_size (abfd) contains the size used for the
57268894
HPN
5437 layout.
5438 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5439 where the layout is forced to according to a larger size in the
5440 last iterations for the testcase ld-elf/header. */
12bd6957 5441 BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr
57268894 5442 == 0);
a50b1753
NC
5443 phdrs = (Elf_Internal_Phdr *)
5444 bfd_zalloc2 (abfd,
07d6d2b8
AM
5445 (elf_program_header_size (abfd) / bed->s->sizeof_phdr),
5446 sizeof (Elf_Internal_Phdr));
f3520d2f 5447 elf_tdata (abfd)->phdr = phdrs;
252b5132 5448 if (phdrs == NULL)
b34976b6 5449 return FALSE;
252b5132 5450
3f570048
AM
5451 maxpagesize = 1;
5452 if ((abfd->flags & D_PAGED) != 0)
5453 maxpagesize = bed->maxpagesize;
5454
252b5132
RH
5455 off = bed->s->sizeof_ehdr;
5456 off += alloc * bed->s->sizeof_phdr;
2b0bc088
NC
5457 if (header_pad < (bfd_vma) off)
5458 header_pad = 0;
5459 else
5460 header_pad -= off;
5461 off += header_pad;
252b5132 5462
12bd6957 5463 for (m = elf_seg_map (abfd), p = phdrs, j = 0;
252b5132 5464 m != NULL;
0920dee7 5465 m = m->next, p++, j++)
252b5132 5466 {
252b5132 5467 asection **secpp;
bf988460
AM
5468 bfd_vma off_adjust;
5469 bfd_boolean no_contents;
252b5132
RH
5470
5471 /* If elf_segment_map is not from map_sections_to_segments, the
08a40648 5472 sections may not be correctly ordered. NOTE: sorting should
52e9b619
MS
5473 not be done to the PT_NOTE section of a corefile, which may
5474 contain several pseudo-sections artificially created by bfd.
5475 Sorting these pseudo-sections breaks things badly. */
47d9a591
AM
5476 if (m->count > 1
5477 && !(elf_elfheader (abfd)->e_type == ET_CORE
52e9b619 5478 && m->p_type == PT_NOTE))
48db3297
AM
5479 {
5480 for (i = 0; i < m->count; i++)
5481 m->sections[i]->target_index = i;
5482 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5483 elf_sort_sections);
5484 }
252b5132 5485
b301b248
AM
5486 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5487 number of sections with contents contributing to both p_filesz
5488 and p_memsz, followed by a number of sections with no contents
5489 that just contribute to p_memsz. In this loop, OFF tracks next
02bf8d82 5490 available file offset for PT_LOAD and PT_NOTE segments. */
252b5132 5491 p->p_type = m->p_type;
28a7f3e7 5492 p->p_flags = m->p_flags;
252b5132 5493
3f570048 5494 if (m->count == 0)
5d695627 5495 p->p_vaddr = m->p_vaddr_offset;
3f570048 5496 else
5d695627 5497 p->p_vaddr = m->sections[0]->vma + m->p_vaddr_offset;
3f570048
AM
5498
5499 if (m->p_paddr_valid)
5500 p->p_paddr = m->p_paddr;
5501 else if (m->count == 0)
5502 p->p_paddr = 0;
5503 else
5d695627 5504 p->p_paddr = m->sections[0]->lma + m->p_vaddr_offset;
3f570048
AM
5505
5506 if (p->p_type == PT_LOAD
5507 && (abfd->flags & D_PAGED) != 0)
5508 {
5509 /* p_align in demand paged PT_LOAD segments effectively stores
5510 the maximum page size. When copying an executable with
5511 objcopy, we set m->p_align from the input file. Use this
5512 value for maxpagesize rather than bed->maxpagesize, which
5513 may be different. Note that we use maxpagesize for PT_TLS
5514 segment alignment later in this function, so we are relying
5515 on at least one PT_LOAD segment appearing before a PT_TLS
5516 segment. */
5517 if (m->p_align_valid)
5518 maxpagesize = m->p_align;
5519
5520 p->p_align = maxpagesize;
a8c75b76 5521 pt_load_count += 1;
3f570048 5522 }
3271a814
NS
5523 else if (m->p_align_valid)
5524 p->p_align = m->p_align;
e970b90a
DJ
5525 else if (m->count == 0)
5526 p->p_align = 1 << bed->s->log_file_align;
3f570048
AM
5527 else
5528 p->p_align = 0;
5529
bf988460
AM
5530 no_contents = FALSE;
5531 off_adjust = 0;
252b5132 5532 if (p->p_type == PT_LOAD
b301b248 5533 && m->count > 0)
252b5132 5534 {
b301b248 5535 bfd_size_type align;
a49e53ed 5536 unsigned int align_power = 0;
b301b248 5537
3271a814
NS
5538 if (m->p_align_valid)
5539 align = p->p_align;
5540 else
252b5132 5541 {
3271a814
NS
5542 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5543 {
5544 unsigned int secalign;
08a40648 5545
fd361982 5546 secalign = bfd_section_alignment (*secpp);
3271a814
NS
5547 if (secalign > align_power)
5548 align_power = secalign;
5549 }
5550 align = (bfd_size_type) 1 << align_power;
5551 if (align < maxpagesize)
5552 align = maxpagesize;
b301b248 5553 }
252b5132 5554
02bf8d82
AM
5555 for (i = 0; i < m->count; i++)
5556 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5557 /* If we aren't making room for this section, then
5558 it must be SHT_NOBITS regardless of what we've
5559 set via struct bfd_elf_special_section. */
5560 elf_section_type (m->sections[i]) = SHT_NOBITS;
5561
bf988460 5562 /* Find out whether this segment contains any loadable
aea274d3
AM
5563 sections. */
5564 no_contents = TRUE;
5565 for (i = 0; i < m->count; i++)
5566 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5567 {
5568 no_contents = FALSE;
5569 break;
5570 }
bf988460 5571
85cfcbfb 5572 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
a8c75b76
AM
5573
5574 /* Broken hardware and/or kernel require that files do not
5575 map the same page with different permissions on some hppa
5576 processors. */
5577 if (pt_load_count > 1
5578 && bed->no_page_alias
5579 && (off & (maxpagesize - 1)) != 0
5580 && (off & -maxpagesize) == ((off + off_adjust) & -maxpagesize))
5581 off_adjust += maxpagesize;
bf988460
AM
5582 off += off_adjust;
5583 if (no_contents)
5584 {
5585 /* We shouldn't need to align the segment on disk since
5586 the segment doesn't need file space, but the gABI
5587 arguably requires the alignment and glibc ld.so
5588 checks it. So to comply with the alignment
5589 requirement but not waste file space, we adjust
5590 p_offset for just this segment. (OFF_ADJUST is
5591 subtracted from OFF later.) This may put p_offset
5592 past the end of file, but that shouldn't matter. */
5593 }
5594 else
5595 off_adjust = 0;
252b5132 5596 }
b1a6d0b1
NC
5597 /* Make sure the .dynamic section is the first section in the
5598 PT_DYNAMIC segment. */
5599 else if (p->p_type == PT_DYNAMIC
5600 && m->count > 1
5601 && strcmp (m->sections[0]->name, ".dynamic") != 0)
5602 {
5603 _bfd_error_handler
871b3ab2 5604 (_("%pB: The first section in the PT_DYNAMIC segment"
63a5468a 5605 " is not the .dynamic section"),
b301b248 5606 abfd);
b1a6d0b1
NC
5607 bfd_set_error (bfd_error_bad_value);
5608 return FALSE;
5609 }
3f001e84
JK
5610 /* Set the note section type to SHT_NOTE. */
5611 else if (p->p_type == PT_NOTE)
5612 for (i = 0; i < m->count; i++)
5613 elf_section_type (m->sections[i]) = SHT_NOTE;
252b5132 5614
252b5132
RH
5615 p->p_offset = 0;
5616 p->p_filesz = 0;
5617 p->p_memsz = 0;
5618
5619 if (m->includes_filehdr)
5620 {
bf988460 5621 if (!m->p_flags_valid)
252b5132 5622 p->p_flags |= PF_R;
252b5132
RH
5623 p->p_filesz = bed->s->sizeof_ehdr;
5624 p->p_memsz = bed->s->sizeof_ehdr;
5625 if (m->count > 0)
5626 {
9ab82472
L
5627 if (p->p_vaddr < (bfd_vma) off
5628 || (!m->p_paddr_valid
5629 && p->p_paddr < (bfd_vma) off))
252b5132 5630 {
4eca0228 5631 _bfd_error_handler
9793eb77 5632 (_("%pB: not enough room for program headers,"
63a5468a 5633 " try linking with -N"),
b301b248 5634 abfd);
252b5132 5635 bfd_set_error (bfd_error_bad_value);
b34976b6 5636 return FALSE;
252b5132
RH
5637 }
5638
5639 p->p_vaddr -= off;
bf988460 5640 if (!m->p_paddr_valid)
252b5132
RH
5641 p->p_paddr -= off;
5642 }
252b5132
RH
5643 }
5644
5645 if (m->includes_phdrs)
5646 {
bf988460 5647 if (!m->p_flags_valid)
252b5132
RH
5648 p->p_flags |= PF_R;
5649
f3520d2f 5650 if (!m->includes_filehdr)
252b5132
RH
5651 {
5652 p->p_offset = bed->s->sizeof_ehdr;
5653
5654 if (m->count > 0)
5655 {
252b5132 5656 p->p_vaddr -= off - p->p_offset;
bf988460 5657 if (!m->p_paddr_valid)
252b5132
RH
5658 p->p_paddr -= off - p->p_offset;
5659 }
252b5132
RH
5660 }
5661
5662 p->p_filesz += alloc * bed->s->sizeof_phdr;
5663 p->p_memsz += alloc * bed->s->sizeof_phdr;
2b0bc088
NC
5664 if (m->count)
5665 {
5666 p->p_filesz += header_pad;
5667 p->p_memsz += header_pad;
5668 }
252b5132
RH
5669 }
5670
5671 if (p->p_type == PT_LOAD
5672 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5673 {
bf988460 5674 if (!m->includes_filehdr && !m->includes_phdrs)
02bf8d82 5675 p->p_offset = off;
252b5132
RH
5676 else
5677 {
5678 file_ptr adjust;
5679
5680 adjust = off - (p->p_offset + p->p_filesz);
bf988460
AM
5681 if (!no_contents)
5682 p->p_filesz += adjust;
252b5132
RH
5683 p->p_memsz += adjust;
5684 }
5685 }
5686
1ea63fd2
AM
5687 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5688 maps. Set filepos for sections in PT_LOAD segments, and in
5689 core files, for sections in PT_NOTE segments.
5690 assign_file_positions_for_non_load_sections will set filepos
5691 for other sections and update p_filesz for other segments. */
252b5132
RH
5692 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5693 {
5694 asection *sec;
252b5132 5695 bfd_size_type align;
627b32bc 5696 Elf_Internal_Shdr *this_hdr;
252b5132
RH
5697
5698 sec = *secpp;
02bf8d82 5699 this_hdr = &elf_section_data (sec)->this_hdr;
fd361982 5700 align = (bfd_size_type) 1 << bfd_section_alignment (sec);
252b5132 5701
88967714
AM
5702 if ((p->p_type == PT_LOAD
5703 || p->p_type == PT_TLS)
5704 && (this_hdr->sh_type != SHT_NOBITS
5705 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5706 && ((this_hdr->sh_flags & SHF_TLS) == 0
5707 || p->p_type == PT_TLS))))
252b5132 5708 {
b5599592
AM
5709 bfd_vma p_start = p->p_paddr;
5710 bfd_vma p_end = p_start + p->p_memsz;
5711 bfd_vma s_start = sec->lma;
5712 bfd_vma adjust = s_start - p_end;
252b5132 5713
a2d1e028
L
5714 if (adjust != 0
5715 && (s_start < p_end
5716 || p_end < p_start))
252b5132 5717 {
4eca0228 5718 _bfd_error_handler
695344c0 5719 /* xgettext:c-format */
2dcf00ce
AM
5720 (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64),
5721 abfd, sec, (uint64_t) s_start, (uint64_t) p_end);
88967714 5722 adjust = 0;
b5599592 5723 sec->lma = p_end;
1cfb7d1e 5724 }
3ac9b6c9 5725 p->p_memsz += adjust;
1cfb7d1e 5726
88967714
AM
5727 if (this_hdr->sh_type != SHT_NOBITS)
5728 {
32812159
AM
5729 if (p->p_filesz + adjust < p->p_memsz)
5730 {
5731 /* We have a PROGBITS section following NOBITS ones.
07d6d2b8 5732 Allocate file space for the NOBITS section(s) and
32812159
AM
5733 zero it. */
5734 adjust = p->p_memsz - p->p_filesz;
5735 if (!write_zeros (abfd, off, adjust))
5736 return FALSE;
5737 }
88967714
AM
5738 off += adjust;
5739 p->p_filesz += adjust;
252b5132 5740 }
252b5132
RH
5741 }
5742
5743 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5744 {
b301b248
AM
5745 /* The section at i == 0 is the one that actually contains
5746 everything. */
4a938328
MS
5747 if (i == 0)
5748 {
627b32bc 5749 this_hdr->sh_offset = sec->filepos = off;
6a3cd2b4
AM
5750 off += this_hdr->sh_size;
5751 p->p_filesz = this_hdr->sh_size;
b301b248
AM
5752 p->p_memsz = 0;
5753 p->p_align = 1;
252b5132 5754 }
4a938328 5755 else
252b5132 5756 {
b301b248 5757 /* The rest are fake sections that shouldn't be written. */
252b5132 5758 sec->filepos = 0;
eea6121a 5759 sec->size = 0;
b301b248
AM
5760 sec->flags = 0;
5761 continue;
252b5132 5762 }
252b5132
RH
5763 }
5764 else
5765 {
1e951488 5766 if (p->p_type == PT_LOAD)
b301b248 5767 {
1e951488
AM
5768 this_hdr->sh_offset = sec->filepos = off;
5769 if (this_hdr->sh_type != SHT_NOBITS)
5770 off += this_hdr->sh_size;
5771 }
5772 else if (this_hdr->sh_type == SHT_NOBITS
5773 && (this_hdr->sh_flags & SHF_TLS) != 0
5774 && this_hdr->sh_offset == 0)
5775 {
5776 /* This is a .tbss section that didn't get a PT_LOAD.
5777 (See _bfd_elf_map_sections_to_segments "Create a
5778 final PT_LOAD".) Set sh_offset to the value it
5779 would have if we had created a zero p_filesz and
5780 p_memsz PT_LOAD header for the section. This
5781 also makes the PT_TLS header have the same
5782 p_offset value. */
5783 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5784 off, align);
5785 this_hdr->sh_offset = sec->filepos = off + adjust;
b301b248 5786 }
252b5132 5787
02bf8d82 5788 if (this_hdr->sh_type != SHT_NOBITS)
b301b248 5789 {
6a3cd2b4 5790 p->p_filesz += this_hdr->sh_size;
02bf8d82
AM
5791 /* A load section without SHF_ALLOC is something like
5792 a note section in a PT_NOTE segment. These take
5793 file space but are not loaded into memory. */
5794 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6a3cd2b4 5795 p->p_memsz += this_hdr->sh_size;
b301b248 5796 }
6a3cd2b4 5797 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
13ae64f3 5798 {
6a3cd2b4
AM
5799 if (p->p_type == PT_TLS)
5800 p->p_memsz += this_hdr->sh_size;
5801
5802 /* .tbss is special. It doesn't contribute to p_memsz of
5803 normal segments. */
5804 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5805 p->p_memsz += this_hdr->sh_size;
13ae64f3
JJ
5806 }
5807
b10a8ae0
L
5808 if (align > p->p_align
5809 && !m->p_align_valid
5810 && (p->p_type != PT_LOAD
5811 || (abfd->flags & D_PAGED) == 0))
252b5132
RH
5812 p->p_align = align;
5813 }
5814
bf988460 5815 if (!m->p_flags_valid)
252b5132
RH
5816 {
5817 p->p_flags |= PF_R;
02bf8d82 5818 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
252b5132 5819 p->p_flags |= PF_X;
02bf8d82 5820 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
252b5132
RH
5821 p->p_flags |= PF_W;
5822 }
5823 }
43a8475c 5824
bf988460 5825 off -= off_adjust;
0920dee7 5826
7c928300
AM
5827 /* Check that all sections are in a PT_LOAD segment.
5828 Don't check funky gdb generated core files. */
5829 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
9a83a553
AM
5830 {
5831 bfd_boolean check_vma = TRUE;
5832
5833 for (i = 1; i < m->count; i++)
5834 if (m->sections[i]->vma == m->sections[i - 1]->vma
5835 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
5836 ->this_hdr), p) != 0
5837 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
5838 ->this_hdr), p) != 0)
0920dee7 5839 {
9a83a553
AM
5840 /* Looks like we have overlays packed into the segment. */
5841 check_vma = FALSE;
5842 break;
0920dee7 5843 }
9a83a553
AM
5844
5845 for (i = 0; i < m->count; i++)
5846 {
5847 Elf_Internal_Shdr *this_hdr;
5848 asection *sec;
5849
5850 sec = m->sections[i];
5851 this_hdr = &(elf_section_data(sec)->this_hdr);
86b2281f
AM
5852 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
5853 && !ELF_TBSS_SPECIAL (this_hdr, p))
9a83a553 5854 {
4eca0228 5855 _bfd_error_handler
695344c0 5856 /* xgettext:c-format */
871b3ab2 5857 (_("%pB: section `%pA' can't be allocated in segment %d"),
9a83a553
AM
5858 abfd, sec, j);
5859 print_segment_map (m);
5860 }
5861 }
5862 }
252b5132
RH
5863 }
5864
12bd6957 5865 elf_next_file_pos (abfd) = off;
f3520d2f
AM
5866 return TRUE;
5867}
5868
1faa385f
NC
5869/* Determine if a bfd is a debuginfo file. Unfortunately there
5870 is no defined method for detecting such files, so we have to
5871 use heuristics instead. */
5872
5873bfd_boolean
5874is_debuginfo_file (bfd *abfd)
5875{
5876 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
5877 return FALSE;
5878
5879 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
5880 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
5881 Elf_Internal_Shdr **headerp;
5882
5883 for (headerp = start_headers; headerp < end_headers; headerp ++)
5884 {
5885 Elf_Internal_Shdr *header = * headerp;
5886
5887 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
5888 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
5889 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
5890 && header->sh_type != SHT_NOBITS
5891 && header->sh_type != SHT_NOTE)
5892 return FALSE;
5893 }
5894
5895 return TRUE;
5896}
5897
1ff6de03
NA
5898/* Assign file positions for the other sections, except for compressed debugging
5899 and other sections assigned in _bfd_elf_assign_file_positions_for_non_load(). */
f3520d2f
AM
5900
5901static bfd_boolean
5902assign_file_positions_for_non_load_sections (bfd *abfd,
5903 struct bfd_link_info *link_info)
5904{
5905 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5906 Elf_Internal_Shdr **i_shdrpp;
e06efbf1 5907 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
f3520d2f
AM
5908 Elf_Internal_Phdr *phdrs;
5909 Elf_Internal_Phdr *p;
5910 struct elf_segment_map *m;
62655c7b 5911 struct elf_segment_map *hdrs_segment;
f3520d2f
AM
5912 bfd_vma filehdr_vaddr, filehdr_paddr;
5913 bfd_vma phdrs_vaddr, phdrs_paddr;
5914 file_ptr off;
f3520d2f
AM
5915 unsigned int count;
5916
5c182d5f 5917 i_shdrpp = elf_elfsections (abfd);
e06efbf1 5918 end_hdrpp = i_shdrpp + elf_numsections (abfd);
12bd6957 5919 off = elf_next_file_pos (abfd);
e06efbf1 5920 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
5c182d5f 5921 {
5c182d5f
AM
5922 Elf_Internal_Shdr *hdr;
5923
5924 hdr = *hdrpp;
5925 if (hdr->bfd_section != NULL
252e386e
AM
5926 && (hdr->bfd_section->filepos != 0
5927 || (hdr->sh_type == SHT_NOBITS
5928 && hdr->contents == NULL)))
627b32bc 5929 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5c182d5f
AM
5930 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
5931 {
1faa385f
NC
5932 if (hdr->sh_size != 0
5933 /* PR 24717 - debuginfo files are known to be not strictly
5934 compliant with the ELF standard. In particular they often
5935 have .note.gnu.property sections that are outside of any
5936 loadable segment. This is not a problem for such files,
5937 so do not warn about them. */
5938 && ! is_debuginfo_file (abfd))
4eca0228 5939 _bfd_error_handler
695344c0 5940 /* xgettext:c-format */
871b3ab2 5941 (_("%pB: warning: allocated section `%s' not in segment"),
e8d2ba53
AM
5942 abfd,
5943 (hdr->bfd_section == NULL
5944 ? "*unknown*"
5945 : hdr->bfd_section->name));
3ba71138
L
5946 /* We don't need to page align empty sections. */
5947 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
5c182d5f
AM
5948 off += vma_page_aligned_bias (hdr->sh_addr, off,
5949 bed->maxpagesize);
5950 else
5951 off += vma_page_aligned_bias (hdr->sh_addr, off,
5952 hdr->sh_addralign);
5953 off = _bfd_elf_assign_file_position_for_section (hdr, off,
5954 FALSE);
5955 }
5956 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5957 && hdr->bfd_section == NULL)
1ff6de03
NA
5958 /* We don't know the offset of these sections yet: their size has
5959 not been decided. */
0ce398f1 5960 || (hdr->bfd_section != NULL
1ff6de03
NA
5961 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
5962 || (bfd_section_is_ctf (hdr->bfd_section)
5963 && abfd->is_linker_output)))
12bd6957 5964 || hdr == i_shdrpp[elf_onesymtab (abfd)]
6a40cf0c
NC
5965 || (elf_symtab_shndx_list (abfd) != NULL
5966 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
5967 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
5968 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
5c182d5f
AM
5969 hdr->sh_offset = -1;
5970 else
5971 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5c182d5f
AM
5972 }
5973
252b5132
RH
5974 /* Now that we have set the section file positions, we can set up
5975 the file positions for the non PT_LOAD segments. */
f3520d2f
AM
5976 count = 0;
5977 filehdr_vaddr = 0;
5978 filehdr_paddr = 0;
5979 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
5980 phdrs_paddr = 0;
62655c7b 5981 hdrs_segment = NULL;
f3520d2f 5982 phdrs = elf_tdata (abfd)->phdr;
12bd6957 5983 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
f3520d2f
AM
5984 {
5985 ++count;
5986 if (p->p_type != PT_LOAD)
5987 continue;
5988
5989 if (m->includes_filehdr)
5990 {
5991 filehdr_vaddr = p->p_vaddr;
5992 filehdr_paddr = p->p_paddr;
5993 }
5994 if (m->includes_phdrs)
5995 {
5996 phdrs_vaddr = p->p_vaddr;
5997 phdrs_paddr = p->p_paddr;
5998 if (m->includes_filehdr)
5999 {
62655c7b 6000 hdrs_segment = m;
f3520d2f
AM
6001 phdrs_vaddr += bed->s->sizeof_ehdr;
6002 phdrs_paddr += bed->s->sizeof_ehdr;
6003 }
6004 }
6005 }
6006
62655c7b
RM
6007 if (hdrs_segment != NULL && link_info != NULL)
6008 {
6009 /* There is a segment that contains both the file headers and the
6010 program headers, so provide a symbol __ehdr_start pointing there.
6011 A program can use this to examine itself robustly. */
6012
6013 struct elf_link_hash_entry *hash
6014 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
6015 FALSE, FALSE, TRUE);
6016 /* If the symbol was referenced and not defined, define it. */
6017 if (hash != NULL
6018 && (hash->root.type == bfd_link_hash_new
6019 || hash->root.type == bfd_link_hash_undefined
6020 || hash->root.type == bfd_link_hash_undefweak
6021 || hash->root.type == bfd_link_hash_common))
6022 {
6023 asection *s = NULL;
6024 if (hdrs_segment->count != 0)
6025 /* The segment contains sections, so use the first one. */
6026 s = hdrs_segment->sections[0];
6027 else
6028 /* Use the first (i.e. lowest-addressed) section in any segment. */
12bd6957 6029 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
62655c7b
RM
6030 if (m->count != 0)
6031 {
6032 s = m->sections[0];
6033 break;
6034 }
6035
6036 if (s != NULL)
6037 {
6038 hash->root.u.def.value = filehdr_vaddr - s->vma;
6039 hash->root.u.def.section = s;
6040 }
6041 else
6042 {
6043 hash->root.u.def.value = filehdr_vaddr;
6044 hash->root.u.def.section = bfd_abs_section_ptr;
6045 }
6046
6047 hash->root.type = bfd_link_hash_defined;
6048 hash->def_regular = 1;
6049 hash->non_elf = 0;
6050 }
6051 }
6052
12bd6957 6053 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
252b5132 6054 {
129af99f 6055 if (p->p_type == PT_GNU_RELRO)
252b5132 6056 {
f2731e0c 6057 bfd_vma start, end;
01f7e10c 6058 bfd_boolean ok;
1ea63fd2 6059
129af99f 6060 if (link_info != NULL)
8c37241b 6061 {
129af99f 6062 /* During linking the range of the RELRO segment is passed
f2731e0c
AM
6063 in link_info. Note that there may be padding between
6064 relro_start and the first RELRO section. */
6065 start = link_info->relro_start;
6066 end = link_info->relro_end;
6067 }
6068 else if (m->count != 0)
6069 {
6070 if (!m->p_size_valid)
6071 abort ();
6072 start = m->sections[0]->vma;
6073 end = start + m->p_size;
6074 }
6075 else
6076 {
6077 start = 0;
6078 end = 0;
6079 }
6080
01f7e10c 6081 ok = FALSE;
f2731e0c
AM
6082 if (start < end)
6083 {
6084 struct elf_segment_map *lm;
6085 const Elf_Internal_Phdr *lp;
6086 unsigned int i;
6087
6088 /* Find a LOAD segment containing a section in the RELRO
6089 segment. */
12bd6957 6090 for (lm = elf_seg_map (abfd), lp = phdrs;
3146fac4
AM
6091 lm != NULL;
6092 lm = lm->next, lp++)
8c37241b
JJ
6093 {
6094 if (lp->p_type == PT_LOAD
3146fac4 6095 && lm->count != 0
dbc88fc1
AM
6096 && (lm->sections[lm->count - 1]->vma
6097 + (!IS_TBSS (lm->sections[lm->count - 1])
6098 ? lm->sections[lm->count - 1]->size
6099 : 0)) > start
f2731e0c 6100 && lm->sections[0]->vma < end)
8c37241b
JJ
6101 break;
6102 }
f2731e0c 6103
01f7e10c 6104 if (lm != NULL)
129af99f 6105 {
01f7e10c
AM
6106 /* Find the section starting the RELRO segment. */
6107 for (i = 0; i < lm->count; i++)
6108 {
6109 asection *s = lm->sections[i];
6110 if (s->vma >= start
6111 && s->vma < end
6112 && s->size != 0)
6113 break;
6114 }
6115
6116 if (i < lm->count)
6117 {
6118 p->p_vaddr = lm->sections[i]->vma;
6119 p->p_paddr = lm->sections[i]->lma;
6120 p->p_offset = lm->sections[i]->filepos;
6121 p->p_memsz = end - p->p_vaddr;
6122 p->p_filesz = p->p_memsz;
6123
6124 /* The RELRO segment typically ends a few bytes
6125 into .got.plt but other layouts are possible.
6126 In cases where the end does not match any
6127 loaded section (for instance is in file
6128 padding), trim p_filesz back to correspond to
6129 the end of loaded section contents. */
6130 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6131 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6132
6133 /* Preserve the alignment and flags if they are
6134 valid. The gold linker generates RW/4 for
6135 the PT_GNU_RELRO section. It is better for
6136 objcopy/strip to honor these attributes
6137 otherwise gdb will choke when using separate
6138 debug files. */
6139 if (!m->p_align_valid)
6140 p->p_align = 1;
6141 if (!m->p_flags_valid)
6142 p->p_flags = PF_R;
6143 ok = TRUE;
6144 }
129af99f 6145 }
b84a33b5 6146 }
01f7e10c
AM
6147 if (link_info != NULL)
6148 BFD_ASSERT (ok);
6149 if (!ok)
6150 memset (p, 0, sizeof *p);
129af99f 6151 }
04c3a755
NS
6152 else if (p->p_type == PT_GNU_STACK)
6153 {
6154 if (m->p_size_valid)
6155 p->p_memsz = m->p_size;
6156 }
129af99f
AS
6157 else if (m->count != 0)
6158 {
e06efbf1 6159 unsigned int i;
1a9ccd70 6160
129af99f
AS
6161 if (p->p_type != PT_LOAD
6162 && (p->p_type != PT_NOTE
6163 || bfd_get_format (abfd) != bfd_core))
6164 {
1a9ccd70
NC
6165 /* A user specified segment layout may include a PHDR
6166 segment that overlaps with a LOAD segment... */
6167 if (p->p_type == PT_PHDR)
6168 {
6169 m->count = 0;
6170 continue;
6171 }
6172
c86934ce
NC
6173 if (m->includes_filehdr || m->includes_phdrs)
6174 {
b1fa9dd6 6175 /* PR 17512: file: 2195325e. */
4eca0228 6176 _bfd_error_handler
871b3ab2 6177 (_("%pB: error: non-load segment %d includes file header "
76cfced5
AM
6178 "and/or program header"),
6179 abfd, (int) (p - phdrs));
c86934ce
NC
6180 return FALSE;
6181 }
129af99f 6182
86b2281f 6183 p->p_filesz = 0;
129af99f 6184 p->p_offset = m->sections[0]->filepos;
86b2281f
AM
6185 for (i = m->count; i-- != 0;)
6186 {
6187 asection *sect = m->sections[i];
6188 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6189 if (hdr->sh_type != SHT_NOBITS)
6190 {
6191 p->p_filesz = (sect->filepos - m->sections[0]->filepos
6192 + hdr->sh_size);
6193 break;
6194 }
6195 }
129af99f
AS
6196 }
6197 }
6198 else if (m->includes_filehdr)
6199 {
6200 p->p_vaddr = filehdr_vaddr;
6201 if (! m->p_paddr_valid)
6202 p->p_paddr = filehdr_paddr;
6203 }
6204 else if (m->includes_phdrs)
6205 {
6206 p->p_vaddr = phdrs_vaddr;
6207 if (! m->p_paddr_valid)
6208 p->p_paddr = phdrs_paddr;
252b5132
RH
6209 }
6210 }
6211
12bd6957 6212 elf_next_file_pos (abfd) = off;
252b5132 6213
b34976b6 6214 return TRUE;
252b5132
RH
6215}
6216
6a40cf0c
NC
6217static elf_section_list *
6218find_section_in_list (unsigned int i, elf_section_list * list)
6219{
6220 for (;list != NULL; list = list->next)
6221 if (list->ndx == i)
6222 break;
6223 return list;
6224}
6225
252b5132
RH
6226/* Work out the file positions of all the sections. This is called by
6227 _bfd_elf_compute_section_file_positions. All the section sizes and
6228 VMAs must be known before this is called.
6229
e0638f70 6230 Reloc sections come in two flavours: Those processed specially as
1ff6de03
NA
6231 "side-channel" data attached to a section to which they apply, and those that
6232 bfd doesn't process as relocations. The latter sort are stored in a normal
6233 bfd section by bfd_section_from_shdr. We don't consider the former sort
6234 here, unless they form part of the loadable image. Reloc sections not
6235 assigned here (and compressed debugging sections and CTF sections which
6236 nothing else in the file can rely upon) will be handled later by
e0638f70 6237 assign_file_positions_for_relocs.
252b5132
RH
6238
6239 We also don't set the positions of the .symtab and .strtab here. */
6240
b34976b6 6241static bfd_boolean
c84fca4d
AO
6242assign_file_positions_except_relocs (bfd *abfd,
6243 struct bfd_link_info *link_info)
252b5132 6244{
5c182d5f
AM
6245 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6246 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
9c5bfbb7 6247 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
6248
6249 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6250 && bfd_get_format (abfd) != bfd_core)
6251 {
5c182d5f
AM
6252 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6253 unsigned int num_sec = elf_numsections (abfd);
252b5132
RH
6254 Elf_Internal_Shdr **hdrpp;
6255 unsigned int i;
a485e98e 6256 file_ptr off;
252b5132
RH
6257
6258 /* Start after the ELF header. */
6259 off = i_ehdrp->e_ehsize;
6260
6261 /* We are not creating an executable, which means that we are
6262 not creating a program header, and that the actual order of
6263 the sections in the file is unimportant. */
9ad5cbcf 6264 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
252b5132
RH
6265 {
6266 Elf_Internal_Shdr *hdr;
6267
6268 hdr = *hdrpp;
e0638f70
AM
6269 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6270 && hdr->bfd_section == NULL)
1ff6de03
NA
6271 /* Do not assign offsets for these sections yet: we don't know
6272 their sizes. */
0ce398f1 6273 || (hdr->bfd_section != NULL
1ff6de03
NA
6274 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6275 || (bfd_section_is_ctf (hdr->bfd_section)
6276 && abfd->is_linker_output)))
12bd6957 6277 || i == elf_onesymtab (abfd)
6a40cf0c
NC
6278 || (elf_symtab_shndx_list (abfd) != NULL
6279 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
6280 || i == elf_strtab_sec (abfd)
6281 || i == elf_shstrtab_sec (abfd))
252b5132
RH
6282 {
6283 hdr->sh_offset = -1;
252b5132 6284 }
9ad5cbcf 6285 else
b34976b6 6286 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 6287 }
a485e98e
AM
6288
6289 elf_next_file_pos (abfd) = off;
252b5132
RH
6290 }
6291 else
6292 {
f3520d2f
AM
6293 unsigned int alloc;
6294
252b5132 6295 /* Assign file positions for the loaded sections based on the
08a40648 6296 assignment of sections to segments. */
f3520d2f
AM
6297 if (!assign_file_positions_for_load_sections (abfd, link_info))
6298 return FALSE;
6299
6300 /* And for non-load sections. */
6301 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6302 return FALSE;
6303
e36284ab
AM
6304 if (bed->elf_backend_modify_program_headers != NULL)
6305 {
6306 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
6307 return FALSE;
6308 }
6309
58e7ebac 6310 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
0e1862bb 6311 if (link_info != NULL && bfd_link_pie (link_info))
58e7ebac
L
6312 {
6313 unsigned int num_segments = elf_elfheader (abfd)->e_phnum;
6314 Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr;
6315 Elf_Internal_Phdr *end_segment = &segment[num_segments];
6316
6317 /* Find the lowest p_vaddr in PT_LOAD segments. */
6318 bfd_vma p_vaddr = (bfd_vma) -1;
6319 for (; segment < end_segment; segment++)
6320 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6321 p_vaddr = segment->p_vaddr;
6322
6323 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6324 segments is non-zero. */
6325 if (p_vaddr)
6326 i_ehdrp->e_type = ET_EXEC;
6327 }
6328
f3520d2f 6329 /* Write out the program headers. */
6838f2be 6330 alloc = elf_elfheader (abfd)->e_phnum;
1a9ccd70
NC
6331 if (alloc == 0)
6332 return TRUE;
6333
1a9ccd70
NC
6334 /* PR ld/20815 - Check that the program header segment, if present, will
6335 be loaded into memory. FIXME: The check below is not sufficient as
6336 really all PT_LOAD segments should be checked before issuing an error
6337 message. Plus the PHDR segment does not have to be the first segment
6338 in the program header table. But this version of the check should
cd584857
NC
6339 catch all real world use cases.
6340
6341 FIXME: We used to have code here to sort the PT_LOAD segments into
6342 ascending order, as per the ELF spec. But this breaks some programs,
6343 including the Linux kernel. But really either the spec should be
07d6d2b8 6344 changed or the programs updated. */
1a9ccd70 6345 if (alloc > 1
cd584857 6346 && tdata->phdr[0].p_type == PT_PHDR
d00dd7dc
AM
6347 && (bed->elf_backend_allow_non_load_phdr == NULL
6348 || !bed->elf_backend_allow_non_load_phdr (abfd, tdata->phdr,
6349 alloc))
cd584857
NC
6350 && tdata->phdr[1].p_type == PT_LOAD
6351 && (tdata->phdr[1].p_vaddr > tdata->phdr[0].p_vaddr
6838f2be
AM
6352 || (tdata->phdr[1].p_vaddr + tdata->phdr[1].p_memsz
6353 < tdata->phdr[0].p_vaddr + tdata->phdr[0].p_memsz)))
1a9ccd70
NC
6354 {
6355 /* The fix for this error is usually to edit the linker script being
6356 used and set up the program headers manually. Either that or
6357 leave room for the headers at the start of the SECTIONS. */
6838f2be
AM
6358 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6359 " by LOAD segment"),
1a9ccd70 6360 abfd);
1a9ccd70
NC
6361 return FALSE;
6362 }
6363
f3520d2f 6364 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
cd584857
NC
6365 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6366 return FALSE;
252b5132
RH
6367 }
6368
b34976b6 6369 return TRUE;
252b5132
RH
6370}
6371
b34976b6 6372static bfd_boolean
217aa764 6373prep_headers (bfd *abfd)
252b5132 6374{
3d540e93 6375 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
2b0f7ef9 6376 struct elf_strtab_hash *shstrtab;
9c5bfbb7 6377 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
6378
6379 i_ehdrp = elf_elfheader (abfd);
252b5132 6380
2b0f7ef9 6381 shstrtab = _bfd_elf_strtab_init ();
252b5132 6382 if (shstrtab == NULL)
b34976b6 6383 return FALSE;
252b5132
RH
6384
6385 elf_shstrtab (abfd) = shstrtab;
6386
6387 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6388 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6389 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6390 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6391
6392 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6393 i_ehdrp->e_ident[EI_DATA] =
6394 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6395 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6396
252b5132
RH
6397 if ((abfd->flags & DYNAMIC) != 0)
6398 i_ehdrp->e_type = ET_DYN;
6399 else if ((abfd->flags & EXEC_P) != 0)
6400 i_ehdrp->e_type = ET_EXEC;
6401 else if (bfd_get_format (abfd) == bfd_core)
6402 i_ehdrp->e_type = ET_CORE;
6403 else
6404 i_ehdrp->e_type = ET_REL;
6405
6406 switch (bfd_get_arch (abfd))
6407 {
6408 case bfd_arch_unknown:
6409 i_ehdrp->e_machine = EM_NONE;
6410 break;
aa4f99bb
AO
6411
6412 /* There used to be a long list of cases here, each one setting
6413 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6414 in the corresponding bfd definition. To avoid duplication,
6415 the switch was removed. Machines that need special handling
6416 can generally do it in elf_backend_final_write_processing(),
6417 unless they need the information earlier than the final write.
6418 Such need can generally be supplied by replacing the tests for
6419 e_machine with the conditions used to determine it. */
252b5132 6420 default:
9c5bfbb7
AM
6421 i_ehdrp->e_machine = bed->elf_machine_code;
6422 }
aa4f99bb 6423
252b5132
RH
6424 i_ehdrp->e_version = bed->s->ev_current;
6425 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6426
c044fabd 6427 /* No program header, for now. */
252b5132
RH
6428 i_ehdrp->e_phoff = 0;
6429 i_ehdrp->e_phentsize = 0;
6430 i_ehdrp->e_phnum = 0;
6431
c044fabd 6432 /* Each bfd section is section header entry. */
252b5132
RH
6433 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6434 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6435
c044fabd 6436 /* If we're building an executable, we'll need a program header table. */
252b5132 6437 if (abfd->flags & EXEC_P)
0e71e495
BE
6438 /* It all happens later. */
6439 ;
252b5132
RH
6440 else
6441 {
6442 i_ehdrp->e_phentsize = 0;
252b5132
RH
6443 i_ehdrp->e_phoff = 0;
6444 }
6445
6446 elf_tdata (abfd)->symtab_hdr.sh_name =
b34976b6 6447 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
252b5132 6448 elf_tdata (abfd)->strtab_hdr.sh_name =
b34976b6 6449 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
252b5132 6450 elf_tdata (abfd)->shstrtab_hdr.sh_name =
b34976b6 6451 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
252b5132 6452 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
17ca87fc 6453 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
252b5132 6454 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
b34976b6 6455 return FALSE;
252b5132 6456
b34976b6 6457 return TRUE;
252b5132
RH
6458}
6459
6460/* Assign file positions for all the reloc sections which are not part
a485e98e 6461 of the loadable file image, and the file position of section headers. */
252b5132 6462
0ce398f1
L
6463static bfd_boolean
6464_bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
252b5132
RH
6465{
6466 file_ptr off;
e06efbf1 6467 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
3e19fb8f 6468 Elf_Internal_Shdr *shdrp;
a485e98e
AM
6469 Elf_Internal_Ehdr *i_ehdrp;
6470 const struct elf_backend_data *bed;
252b5132 6471
12bd6957 6472 off = elf_next_file_pos (abfd);
252b5132 6473
e06efbf1
L
6474 shdrpp = elf_elfsections (abfd);
6475 end_shdrpp = shdrpp + elf_numsections (abfd);
6476 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
252b5132 6477 {
252b5132 6478 shdrp = *shdrpp;
0ce398f1
L
6479 if (shdrp->sh_offset == -1)
6480 {
3e19fb8f 6481 asection *sec = shdrp->bfd_section;
0ce398f1
L
6482 bfd_boolean is_rel = (shdrp->sh_type == SHT_REL
6483 || shdrp->sh_type == SHT_RELA);
1ff6de03 6484 bfd_boolean is_ctf = sec && bfd_section_is_ctf (sec);
0ce398f1 6485 if (is_rel
1ff6de03 6486 || is_ctf
3e19fb8f 6487 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
0ce398f1 6488 {
1ff6de03 6489 if (!is_rel && !is_ctf)
0ce398f1 6490 {
3e19fb8f
L
6491 const char *name = sec->name;
6492 struct bfd_elf_section_data *d;
6493
0ce398f1 6494 /* Compress DWARF debug sections. */
3e19fb8f 6495 if (!bfd_compress_section (abfd, sec,
0ce398f1
L
6496 shdrp->contents))
6497 return FALSE;
3e19fb8f
L
6498
6499 if (sec->compress_status == COMPRESS_SECTION_DONE
6500 && (abfd->flags & BFD_COMPRESS_GABI) == 0)
6501 {
6502 /* If section is compressed with zlib-gnu, convert
6503 section name from .debug_* to .zdebug_*. */
6504 char *new_name
6505 = convert_debug_to_zdebug (abfd, name);
6506 if (new_name == NULL)
6507 return FALSE;
6508 name = new_name;
6509 }
dd905818 6510 /* Add section name to section name section. */
3e19fb8f
L
6511 if (shdrp->sh_name != (unsigned int) -1)
6512 abort ();
6513 shdrp->sh_name
6514 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
6515 name, FALSE);
6516 d = elf_section_data (sec);
6517
dd905818 6518 /* Add reloc section name to section name section. */
3e19fb8f
L
6519 if (d->rel.hdr
6520 && !_bfd_elf_set_reloc_sh_name (abfd,
6521 d->rel.hdr,
6522 name, FALSE))
6523 return FALSE;
6524 if (d->rela.hdr
6525 && !_bfd_elf_set_reloc_sh_name (abfd,
6526 d->rela.hdr,
91cb26da 6527 name, TRUE))
3e19fb8f
L
6528 return FALSE;
6529
0ce398f1 6530 /* Update section size and contents. */
3e19fb8f
L
6531 shdrp->sh_size = sec->size;
6532 shdrp->contents = sec->contents;
0ce398f1
L
6533 shdrp->bfd_section->contents = NULL;
6534 }
1ff6de03
NA
6535 else if (is_ctf)
6536 {
6537 /* Update section size and contents. */
6538 shdrp->sh_size = sec->size;
6539 shdrp->contents = sec->contents;
6540 }
6541
0ce398f1
L
6542 off = _bfd_elf_assign_file_position_for_section (shdrp,
6543 off,
6544 TRUE);
6545 }
6546 }
252b5132
RH
6547 }
6548
3e19fb8f
L
6549 /* Place section name section after DWARF debug sections have been
6550 compressed. */
6551 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6552 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6553 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
6554 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
6555
6556 /* Place the section headers. */
a485e98e
AM
6557 i_ehdrp = elf_elfheader (abfd);
6558 bed = get_elf_backend_data (abfd);
6559 off = align_file_position (off, 1 << bed->s->log_file_align);
6560 i_ehdrp->e_shoff = off;
6561 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
12bd6957 6562 elf_next_file_pos (abfd) = off;
0ce398f1
L
6563
6564 return TRUE;
252b5132
RH
6565}
6566
b34976b6 6567bfd_boolean
217aa764 6568_bfd_elf_write_object_contents (bfd *abfd)
252b5132 6569{
9c5bfbb7 6570 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 6571 Elf_Internal_Shdr **i_shdrp;
b34976b6 6572 bfd_boolean failed;
9ad5cbcf 6573 unsigned int count, num_sec;
30e8ee25 6574 struct elf_obj_tdata *t;
252b5132
RH
6575
6576 if (! abfd->output_has_begun
217aa764 6577 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
b34976b6 6578 return FALSE;
db727370
JL
6579 /* Do not rewrite ELF data when the BFD has been opened for update.
6580 abfd->output_has_begun was set to TRUE on opening, so creation of new
6581 sections, and modification of existing section sizes was restricted.
6582 This means the ELF header, program headers and section headers can't have
6583 changed.
6584 If the contents of any sections has been modified, then those changes have
6585 already been written to the BFD. */
6586 else if (abfd->direction == both_direction)
6587 {
6588 BFD_ASSERT (abfd->output_has_begun);
6589 return TRUE;
6590 }
252b5132
RH
6591
6592 i_shdrp = elf_elfsections (abfd);
252b5132 6593
b34976b6 6594 failed = FALSE;
252b5132
RH
6595 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6596 if (failed)
b34976b6 6597 return FALSE;
252b5132 6598
0ce398f1
L
6599 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
6600 return FALSE;
252b5132 6601
c044fabd 6602 /* After writing the headers, we need to write the sections too... */
9ad5cbcf
AM
6603 num_sec = elf_numsections (abfd);
6604 for (count = 1; count < num_sec; count++)
252b5132 6605 {
3e19fb8f
L
6606 i_shdrp[count]->sh_name
6607 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6608 i_shdrp[count]->sh_name);
252b5132 6609 if (bed->elf_backend_section_processing)
75506100
MR
6610 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
6611 return FALSE;
252b5132
RH
6612 if (i_shdrp[count]->contents)
6613 {
dc810e39
AM
6614 bfd_size_type amt = i_shdrp[count]->sh_size;
6615
252b5132 6616 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
dc810e39 6617 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
b34976b6 6618 return FALSE;
252b5132
RH
6619 }
6620 }
6621
6622 /* Write out the section header names. */
30e8ee25 6623 t = elf_tdata (abfd);
26ae6d5e 6624 if (elf_shstrtab (abfd) != NULL
30e8ee25 6625 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
08a40648 6626 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
b34976b6 6627 return FALSE;
252b5132 6628
cc364be6
AM
6629 if (!(*bed->elf_backend_final_write_processing) (abfd))
6630 return FALSE;
252b5132 6631
ff59fc36
RM
6632 if (!bed->s->write_shdrs_and_ehdr (abfd))
6633 return FALSE;
6634
6635 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
c0355132
AM
6636 if (t->o->build_id.after_write_object_contents != NULL)
6637 return (*t->o->build_id.after_write_object_contents) (abfd);
ff59fc36
RM
6638
6639 return TRUE;
252b5132
RH
6640}
6641
b34976b6 6642bfd_boolean
217aa764 6643_bfd_elf_write_corefile_contents (bfd *abfd)
252b5132 6644{
c044fabd 6645 /* Hopefully this can be done just like an object file. */
252b5132
RH
6646 return _bfd_elf_write_object_contents (abfd);
6647}
c044fabd
KH
6648
6649/* Given a section, search the header to find them. */
6650
cb33740c 6651unsigned int
198beae2 6652_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
252b5132 6653{
9c5bfbb7 6654 const struct elf_backend_data *bed;
91d6fa6a 6655 unsigned int sec_index;
252b5132 6656
9ad5cbcf
AM
6657 if (elf_section_data (asect) != NULL
6658 && elf_section_data (asect)->this_idx != 0)
6659 return elf_section_data (asect)->this_idx;
6660
6661 if (bfd_is_abs_section (asect))
91d6fa6a 6662 sec_index = SHN_ABS;
af746e92 6663 else if (bfd_is_com_section (asect))
91d6fa6a 6664 sec_index = SHN_COMMON;
af746e92 6665 else if (bfd_is_und_section (asect))
91d6fa6a 6666 sec_index = SHN_UNDEF;
af746e92 6667 else
91d6fa6a 6668 sec_index = SHN_BAD;
252b5132 6669
af746e92 6670 bed = get_elf_backend_data (abfd);
252b5132
RH
6671 if (bed->elf_backend_section_from_bfd_section)
6672 {
91d6fa6a 6673 int retval = sec_index;
9ad5cbcf 6674
af746e92
AM
6675 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6676 return retval;
252b5132
RH
6677 }
6678
91d6fa6a 6679 if (sec_index == SHN_BAD)
af746e92 6680 bfd_set_error (bfd_error_nonrepresentable_section);
252b5132 6681
91d6fa6a 6682 return sec_index;
252b5132
RH
6683}
6684
6685/* Given a BFD symbol, return the index in the ELF symbol table, or -1
6686 on error. */
6687
6688int
217aa764 6689_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
252b5132
RH
6690{
6691 asymbol *asym_ptr = *asym_ptr_ptr;
6692 int idx;
6693 flagword flags = asym_ptr->flags;
6694
6695 /* When gas creates relocations against local labels, it creates its
6696 own symbol for the section, but does put the symbol into the
6697 symbol chain, so udata is 0. When the linker is generating
6698 relocatable output, this section symbol may be for one of the
6699 input sections rather than the output section. */
6700 if (asym_ptr->udata.i == 0
6701 && (flags & BSF_SECTION_SYM)
6702 && asym_ptr->section)
6703 {
5372391b 6704 asection *sec;
252b5132
RH
6705 int indx;
6706
5372391b
AM
6707 sec = asym_ptr->section;
6708 if (sec->owner != abfd && sec->output_section != NULL)
6709 sec = sec->output_section;
6710 if (sec->owner == abfd
6711 && (indx = sec->index) < elf_num_section_syms (abfd)
4e89ac30 6712 && elf_section_syms (abfd)[indx] != NULL)
252b5132
RH
6713 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
6714 }
6715
6716 idx = asym_ptr->udata.i;
6717
6718 if (idx == 0)
6719 {
6720 /* This case can occur when using --strip-symbol on a symbol
08a40648 6721 which is used in a relocation entry. */
4eca0228 6722 _bfd_error_handler
695344c0 6723 /* xgettext:c-format */
871b3ab2 6724 (_("%pB: symbol `%s' required but not present"),
d003868e 6725 abfd, bfd_asymbol_name (asym_ptr));
252b5132
RH
6726 bfd_set_error (bfd_error_no_symbols);
6727 return -1;
6728 }
6729
6730#if DEBUG & 4
6731 {
6732 fprintf (stderr,
cd9af601
AM
6733 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6734 (long) asym_ptr, asym_ptr->name, idx, flags);
252b5132
RH
6735 fflush (stderr);
6736 }
6737#endif
6738
6739 return idx;
6740}
6741
84d1d650 6742/* Rewrite program header information. */
252b5132 6743
b34976b6 6744static bfd_boolean
84d1d650 6745rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
252b5132 6746{
b34976b6
AM
6747 Elf_Internal_Ehdr *iehdr;
6748 struct elf_segment_map *map;
6749 struct elf_segment_map *map_first;
6750 struct elf_segment_map **pointer_to_map;
6751 Elf_Internal_Phdr *segment;
6752 asection *section;
6753 unsigned int i;
6754 unsigned int num_segments;
6755 bfd_boolean phdr_included = FALSE;
5c44b38e 6756 bfd_boolean p_paddr_valid;
b34976b6
AM
6757 bfd_vma maxpagesize;
6758 struct elf_segment_map *phdr_adjust_seg = NULL;
6759 unsigned int phdr_adjust_num = 0;
9c5bfbb7 6760 const struct elf_backend_data *bed;
bc67d8a6 6761
caf47ea6 6762 bed = get_elf_backend_data (ibfd);
252b5132
RH
6763 iehdr = elf_elfheader (ibfd);
6764
bc67d8a6 6765 map_first = NULL;
c044fabd 6766 pointer_to_map = &map_first;
252b5132
RH
6767
6768 num_segments = elf_elfheader (ibfd)->e_phnum;
bc67d8a6
NC
6769 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
6770
6771 /* Returns the end address of the segment + 1. */
aecc8f8a
AM
6772#define SEGMENT_END(segment, start) \
6773 (start + (segment->p_memsz > segment->p_filesz \
6774 ? segment->p_memsz : segment->p_filesz))
bc67d8a6 6775
eecdbe52
JJ
6776#define SECTION_SIZE(section, segment) \
6777 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6778 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
eea6121a 6779 ? section->size : 0)
eecdbe52 6780
b34976b6 6781 /* Returns TRUE if the given section is contained within
bc67d8a6 6782 the given segment. VMA addresses are compared. */
aecc8f8a
AM
6783#define IS_CONTAINED_BY_VMA(section, segment) \
6784 (section->vma >= segment->p_vaddr \
eecdbe52 6785 && (section->vma + SECTION_SIZE (section, segment) \
aecc8f8a 6786 <= (SEGMENT_END (segment, segment->p_vaddr))))
c044fabd 6787
b34976b6 6788 /* Returns TRUE if the given section is contained within
bc67d8a6 6789 the given segment. LMA addresses are compared. */
aecc8f8a
AM
6790#define IS_CONTAINED_BY_LMA(section, segment, base) \
6791 (section->lma >= base \
beab4532 6792 && (section->lma + SECTION_SIZE (section, segment) >= section->lma) \
eecdbe52 6793 && (section->lma + SECTION_SIZE (section, segment) \
aecc8f8a 6794 <= SEGMENT_END (segment, base)))
252b5132 6795
0efc80c8
L
6796 /* Handle PT_NOTE segment. */
6797#define IS_NOTE(p, s) \
aecc8f8a 6798 (p->p_type == PT_NOTE \
0efc80c8 6799 && elf_section_type (s) == SHT_NOTE \
aecc8f8a 6800 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 6801 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 6802 <= p->p_offset + p->p_filesz))
252b5132 6803
0efc80c8
L
6804 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6805 etc. */
6806#define IS_COREFILE_NOTE(p, s) \
6807 (IS_NOTE (p, s) \
6808 && bfd_get_format (ibfd) == bfd_core \
6809 && s->vma == 0 \
6810 && s->lma == 0)
6811
252b5132
RH
6812 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6813 linker, which generates a PT_INTERP section with p_vaddr and
6814 p_memsz set to 0. */
aecc8f8a
AM
6815#define IS_SOLARIS_PT_INTERP(p, s) \
6816 (p->p_vaddr == 0 \
6817 && p->p_paddr == 0 \
6818 && p->p_memsz == 0 \
6819 && p->p_filesz > 0 \
6820 && (s->flags & SEC_HAS_CONTENTS) != 0 \
eea6121a 6821 && s->size > 0 \
aecc8f8a 6822 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 6823 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 6824 <= p->p_offset + p->p_filesz))
5c440b1e 6825
bc67d8a6
NC
6826 /* Decide if the given section should be included in the given segment.
6827 A section will be included if:
f5ffc919 6828 1. It is within the address space of the segment -- we use the LMA
08a40648 6829 if that is set for the segment and the VMA otherwise,
0efc80c8 6830 2. It is an allocated section or a NOTE section in a PT_NOTE
d324f6d6 6831 segment.
bc67d8a6 6832 3. There is an output section associated with it,
eecdbe52 6833 4. The section has not already been allocated to a previous segment.
2b05f1b7 6834 5. PT_GNU_STACK segments do not include any sections.
03394ac9 6835 6. PT_TLS segment includes only SHF_TLS sections.
6f79b219
JJ
6836 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6837 8. PT_DYNAMIC should not contain empty sections at the beginning
08a40648 6838 (with the possible exception of .dynamic). */
9f17e2a6 6839#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
2b05f1b7
L
6840 ((((segment->p_paddr \
6841 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6842 : IS_CONTAINED_BY_VMA (section, segment)) \
6843 && (section->flags & SEC_ALLOC) != 0) \
0efc80c8 6844 || IS_NOTE (segment, section)) \
2b05f1b7
L
6845 && segment->p_type != PT_GNU_STACK \
6846 && (segment->p_type != PT_TLS \
6847 || (section->flags & SEC_THREAD_LOCAL)) \
6848 && (segment->p_type == PT_LOAD \
6849 || segment->p_type == PT_TLS \
6850 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6851 && (segment->p_type != PT_DYNAMIC \
6852 || SECTION_SIZE (section, segment) > 0 \
6853 || (segment->p_paddr \
6854 ? segment->p_paddr != section->lma \
6855 : segment->p_vaddr != section->vma) \
fd361982 6856 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
9933dc52 6857 && (segment->p_type != PT_LOAD || !section->segment_mark))
bc67d8a6 6858
9f17e2a6
L
6859/* If the output section of a section in the input segment is NULL,
6860 it is removed from the corresponding output segment. */
6861#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6862 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6863 && section->output_section != NULL)
6864
b34976b6 6865 /* Returns TRUE iff seg1 starts after the end of seg2. */
b5f852ea
NC
6866#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6867 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6868
6869 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6870 their VMA address ranges and their LMA address ranges overlap.
6871 It is possible to have overlapping VMA ranges without overlapping LMA
6872 ranges. RedBoot images for example can have both .data and .bss mapped
6873 to the same VMA range, but with the .data section mapped to a different
6874 LMA. */
aecc8f8a 6875#define SEGMENT_OVERLAPS(seg1, seg2) \
b5f852ea 6876 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
08a40648 6877 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
b5f852ea 6878 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
08a40648 6879 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
bc67d8a6
NC
6880
6881 /* Initialise the segment mark field. */
6882 for (section = ibfd->sections; section != NULL; section = section->next)
b34976b6 6883 section->segment_mark = FALSE;
bc67d8a6 6884
5c44b38e
AM
6885 /* The Solaris linker creates program headers in which all the
6886 p_paddr fields are zero. When we try to objcopy or strip such a
6887 file, we get confused. Check for this case, and if we find it
6888 don't set the p_paddr_valid fields. */
6889 p_paddr_valid = FALSE;
6890 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6891 i < num_segments;
6892 i++, segment++)
6893 if (segment->p_paddr != 0)
6894 {
6895 p_paddr_valid = TRUE;
6896 break;
6897 }
6898
252b5132 6899 /* Scan through the segments specified in the program header
bc67d8a6 6900 of the input BFD. For this first scan we look for overlaps
9ad5cbcf 6901 in the loadable segments. These can be created by weird
aecc8f8a 6902 parameters to objcopy. Also, fix some solaris weirdness. */
bc67d8a6
NC
6903 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6904 i < num_segments;
c044fabd 6905 i++, segment++)
252b5132 6906 {
252b5132 6907 unsigned int j;
c044fabd 6908 Elf_Internal_Phdr *segment2;
252b5132 6909
aecc8f8a
AM
6910 if (segment->p_type == PT_INTERP)
6911 for (section = ibfd->sections; section; section = section->next)
6912 if (IS_SOLARIS_PT_INTERP (segment, section))
6913 {
6914 /* Mininal change so that the normal section to segment
4cc11e76 6915 assignment code will work. */
aecc8f8a
AM
6916 segment->p_vaddr = section->vma;
6917 break;
6918 }
6919
bc67d8a6 6920 if (segment->p_type != PT_LOAD)
b10a8ae0
L
6921 {
6922 /* Remove PT_GNU_RELRO segment. */
6923 if (segment->p_type == PT_GNU_RELRO)
6924 segment->p_type = PT_NULL;
6925 continue;
6926 }
c044fabd 6927
bc67d8a6 6928 /* Determine if this segment overlaps any previous segments. */
0067a569 6929 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
bc67d8a6
NC
6930 {
6931 bfd_signed_vma extra_length;
c044fabd 6932
bc67d8a6 6933 if (segment2->p_type != PT_LOAD
0067a569 6934 || !SEGMENT_OVERLAPS (segment, segment2))
bc67d8a6 6935 continue;
c044fabd 6936
bc67d8a6
NC
6937 /* Merge the two segments together. */
6938 if (segment2->p_vaddr < segment->p_vaddr)
6939 {
c044fabd 6940 /* Extend SEGMENT2 to include SEGMENT and then delete
08a40648 6941 SEGMENT. */
0067a569
AM
6942 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
6943 - SEGMENT_END (segment2, segment2->p_vaddr));
c044fabd 6944
bc67d8a6
NC
6945 if (extra_length > 0)
6946 {
0067a569 6947 segment2->p_memsz += extra_length;
bc67d8a6
NC
6948 segment2->p_filesz += extra_length;
6949 }
c044fabd 6950
bc67d8a6 6951 segment->p_type = PT_NULL;
c044fabd 6952
bc67d8a6
NC
6953 /* Since we have deleted P we must restart the outer loop. */
6954 i = 0;
6955 segment = elf_tdata (ibfd)->phdr;
6956 break;
6957 }
6958 else
6959 {
c044fabd 6960 /* Extend SEGMENT to include SEGMENT2 and then delete
08a40648 6961 SEGMENT2. */
0067a569
AM
6962 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
6963 - SEGMENT_END (segment, segment->p_vaddr));
c044fabd 6964
bc67d8a6
NC
6965 if (extra_length > 0)
6966 {
0067a569 6967 segment->p_memsz += extra_length;
bc67d8a6
NC
6968 segment->p_filesz += extra_length;
6969 }
c044fabd 6970
bc67d8a6
NC
6971 segment2->p_type = PT_NULL;
6972 }
6973 }
6974 }
c044fabd 6975
bc67d8a6
NC
6976 /* The second scan attempts to assign sections to segments. */
6977 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6978 i < num_segments;
0067a569 6979 i++, segment++)
bc67d8a6 6980 {
0067a569
AM
6981 unsigned int section_count;
6982 asection **sections;
6983 asection *output_section;
6984 unsigned int isec;
9933dc52
AM
6985 asection *matching_lma;
6986 asection *suggested_lma;
0067a569 6987 unsigned int j;
dc810e39 6988 bfd_size_type amt;
0067a569 6989 asection *first_section;
bc67d8a6
NC
6990
6991 if (segment->p_type == PT_NULL)
6992 continue;
c044fabd 6993
9f17e2a6 6994 first_section = NULL;
bc67d8a6 6995 /* Compute how many sections might be placed into this segment. */
b5f852ea
NC
6996 for (section = ibfd->sections, section_count = 0;
6997 section != NULL;
6998 section = section->next)
9f17e2a6
L
6999 {
7000 /* Find the first section in the input segment, which may be
7001 removed from the corresponding output segment. */
7002 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
7003 {
7004 if (first_section == NULL)
7005 first_section = section;
7006 if (section->output_section != NULL)
7007 ++section_count;
7008 }
7009 }
811072d8 7010
b5f852ea
NC
7011 /* Allocate a segment map big enough to contain
7012 all of the sections we have selected. */
00bee008
AM
7013 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7014 amt += (bfd_size_type) section_count * sizeof (asection *);
a50b1753 7015 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 7016 if (map == NULL)
b34976b6 7017 return FALSE;
252b5132
RH
7018
7019 /* Initialise the fields of the segment map. Default to
7020 using the physical address of the segment in the input BFD. */
0067a569
AM
7021 map->next = NULL;
7022 map->p_type = segment->p_type;
7023 map->p_flags = segment->p_flags;
bc67d8a6 7024 map->p_flags_valid = 1;
55d55ac7 7025
9f17e2a6
L
7026 /* If the first section in the input segment is removed, there is
7027 no need to preserve segment physical address in the corresponding
7028 output segment. */
945c025a 7029 if (!first_section || first_section->output_section != NULL)
9f17e2a6
L
7030 {
7031 map->p_paddr = segment->p_paddr;
5c44b38e 7032 map->p_paddr_valid = p_paddr_valid;
9f17e2a6 7033 }
252b5132
RH
7034
7035 /* Determine if this segment contains the ELF file header
7036 and if it contains the program headers themselves. */
bc67d8a6
NC
7037 map->includes_filehdr = (segment->p_offset == 0
7038 && segment->p_filesz >= iehdr->e_ehsize);
bc67d8a6 7039 map->includes_phdrs = 0;
252b5132 7040
0067a569 7041 if (!phdr_included || segment->p_type != PT_LOAD)
252b5132 7042 {
bc67d8a6
NC
7043 map->includes_phdrs =
7044 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7045 && (segment->p_offset + segment->p_filesz
252b5132
RH
7046 >= ((bfd_vma) iehdr->e_phoff
7047 + iehdr->e_phnum * iehdr->e_phentsize)));
c044fabd 7048
bc67d8a6 7049 if (segment->p_type == PT_LOAD && map->includes_phdrs)
b34976b6 7050 phdr_included = TRUE;
252b5132
RH
7051 }
7052
bc67d8a6 7053 if (section_count == 0)
252b5132
RH
7054 {
7055 /* Special segments, such as the PT_PHDR segment, may contain
7056 no sections, but ordinary, loadable segments should contain
1ed89aa9 7057 something. They are allowed by the ELF spec however, so only
07d6d2b8 7058 a warning is produced.
f98450c6
NC
7059 There is however the valid use case of embedded systems which
7060 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7061 flash memory with zeros. No warning is shown for that case. */
7062 if (segment->p_type == PT_LOAD
7063 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7064 /* xgettext:c-format */
9793eb77
AM
7065 _bfd_error_handler
7066 (_("%pB: warning: empty loadable segment detected"
7067 " at vaddr=%#" PRIx64 ", is this intentional?"),
7068 ibfd, (uint64_t) segment->p_vaddr);
252b5132 7069
5d695627 7070 map->p_vaddr_offset = segment->p_vaddr;
bc67d8a6 7071 map->count = 0;
c044fabd
KH
7072 *pointer_to_map = map;
7073 pointer_to_map = &map->next;
252b5132
RH
7074
7075 continue;
7076 }
7077
7078 /* Now scan the sections in the input BFD again and attempt
7079 to add their corresponding output sections to the segment map.
7080 The problem here is how to handle an output section which has
7081 been moved (ie had its LMA changed). There are four possibilities:
7082
7083 1. None of the sections have been moved.
7084 In this case we can continue to use the segment LMA from the
7085 input BFD.
7086
7087 2. All of the sections have been moved by the same amount.
7088 In this case we can change the segment's LMA to match the LMA
7089 of the first section.
7090
7091 3. Some of the sections have been moved, others have not.
7092 In this case those sections which have not been moved can be
7093 placed in the current segment which will have to have its size,
7094 and possibly its LMA changed, and a new segment or segments will
7095 have to be created to contain the other sections.
7096
b5f852ea 7097 4. The sections have been moved, but not by the same amount.
252b5132
RH
7098 In this case we can change the segment's LMA to match the LMA
7099 of the first section and we will have to create a new segment
7100 or segments to contain the other sections.
7101
7102 In order to save time, we allocate an array to hold the section
7103 pointers that we are interested in. As these sections get assigned
7104 to a segment, they are removed from this array. */
7105
a50b1753 7106 sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
252b5132 7107 if (sections == NULL)
b34976b6 7108 return FALSE;
252b5132
RH
7109
7110 /* Step One: Scan for segment vs section LMA conflicts.
7111 Also add the sections to the section array allocated above.
7112 Also add the sections to the current segment. In the common
7113 case, where the sections have not been moved, this means that
7114 we have completely filled the segment, and there is nothing
7115 more to do. */
252b5132 7116 isec = 0;
9933dc52
AM
7117 matching_lma = NULL;
7118 suggested_lma = NULL;
252b5132 7119
461c4b2e 7120 for (section = first_section, j = 0;
bc67d8a6
NC
7121 section != NULL;
7122 section = section->next)
252b5132 7123 {
caf47ea6 7124 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
c0f7859b 7125 {
bc67d8a6
NC
7126 output_section = section->output_section;
7127
0067a569 7128 sections[j++] = section;
252b5132
RH
7129
7130 /* The Solaris native linker always sets p_paddr to 0.
7131 We try to catch that case here, and set it to the
5e8d7549
NC
7132 correct value. Note - some backends require that
7133 p_paddr be left as zero. */
5c44b38e 7134 if (!p_paddr_valid
4455705d 7135 && segment->p_vaddr != 0
0067a569 7136 && !bed->want_p_paddr_set_to_zero
252b5132 7137 && isec == 0
bc67d8a6 7138 && output_section->lma != 0
9933dc52
AM
7139 && (align_power (segment->p_vaddr
7140 + (map->includes_filehdr
7141 ? iehdr->e_ehsize : 0)
7142 + (map->includes_phdrs
7143 ? iehdr->e_phnum * iehdr->e_phentsize
7144 : 0),
7145 output_section->alignment_power)
7146 == output_section->vma))
bc67d8a6 7147 map->p_paddr = segment->p_vaddr;
252b5132
RH
7148
7149 /* Match up the physical address of the segment with the
7150 LMA address of the output section. */
bc67d8a6 7151 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5e8d7549 7152 || IS_COREFILE_NOTE (segment, section)
0067a569
AM
7153 || (bed->want_p_paddr_set_to_zero
7154 && IS_CONTAINED_BY_VMA (output_section, segment)))
252b5132 7155 {
9933dc52
AM
7156 if (matching_lma == NULL
7157 || output_section->lma < matching_lma->lma)
7158 matching_lma = output_section;
252b5132
RH
7159
7160 /* We assume that if the section fits within the segment
bc67d8a6 7161 then it does not overlap any other section within that
252b5132 7162 segment. */
0067a569
AM
7163 map->sections[isec++] = output_section;
7164 }
9933dc52
AM
7165 else if (suggested_lma == NULL)
7166 suggested_lma = output_section;
147d51c2
L
7167
7168 if (j == section_count)
7169 break;
252b5132
RH
7170 }
7171 }
7172
bc67d8a6 7173 BFD_ASSERT (j == section_count);
252b5132
RH
7174
7175 /* Step Two: Adjust the physical address of the current segment,
7176 if necessary. */
bc67d8a6 7177 if (isec == section_count)
252b5132
RH
7178 {
7179 /* All of the sections fitted within the segment as currently
7180 specified. This is the default case. Add the segment to
7181 the list of built segments and carry on to process the next
7182 program header in the input BFD. */
bc67d8a6 7183 map->count = section_count;
c044fabd
KH
7184 *pointer_to_map = map;
7185 pointer_to_map = &map->next;
08a40648 7186
5c44b38e
AM
7187 if (p_paddr_valid
7188 && !bed->want_p_paddr_set_to_zero
9933dc52 7189 && matching_lma->lma != map->p_paddr
5c44b38e
AM
7190 && !map->includes_filehdr
7191 && !map->includes_phdrs)
3271a814
NS
7192 /* There is some padding before the first section in the
7193 segment. So, we must account for that in the output
7194 segment's vma. */
5d695627 7195 map->p_vaddr_offset = map->p_paddr - matching_lma->lma;
08a40648 7196
252b5132
RH
7197 free (sections);
7198 continue;
7199 }
252b5132
RH
7200 else
7201 {
9933dc52
AM
7202 /* Change the current segment's physical address to match
7203 the LMA of the first section that fitted, or if no
7204 section fitted, the first section. */
7205 if (matching_lma == NULL)
7206 matching_lma = suggested_lma;
7207
7208 map->p_paddr = matching_lma->lma;
72730e0c 7209
bc67d8a6
NC
7210 /* Offset the segment physical address from the lma
7211 to allow for space taken up by elf headers. */
9933dc52 7212 if (map->includes_phdrs)
010c8431 7213 {
9933dc52
AM
7214 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7215
7216 /* iehdr->e_phnum is just an estimate of the number
7217 of program headers that we will need. Make a note
7218 here of the number we used and the segment we chose
7219 to hold these headers, so that we can adjust the
7220 offset when we know the correct value. */
7221 phdr_adjust_num = iehdr->e_phnum;
7222 phdr_adjust_seg = map;
010c8431 7223 }
252b5132 7224
9933dc52 7225 if (map->includes_filehdr)
bc67d8a6 7226 {
9933dc52
AM
7227 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7228 map->p_paddr -= iehdr->e_ehsize;
7229 /* We've subtracted off the size of headers from the
7230 first section lma, but there may have been some
7231 alignment padding before that section too. Try to
7232 account for that by adjusting the segment lma down to
7233 the same alignment. */
7234 if (segment->p_align != 0 && segment->p_align < align)
7235 align = segment->p_align;
7236 map->p_paddr &= -align;
bc67d8a6 7237 }
252b5132
RH
7238 }
7239
7240 /* Step Three: Loop over the sections again, this time assigning
caf47ea6 7241 those that fit to the current segment and removing them from the
252b5132
RH
7242 sections array; but making sure not to leave large gaps. Once all
7243 possible sections have been assigned to the current segment it is
7244 added to the list of built segments and if sections still remain
7245 to be assigned, a new segment is constructed before repeating
7246 the loop. */
7247 isec = 0;
7248 do
7249 {
bc67d8a6 7250 map->count = 0;
9933dc52 7251 suggested_lma = NULL;
252b5132
RH
7252
7253 /* Fill the current segment with sections that fit. */
bc67d8a6 7254 for (j = 0; j < section_count; j++)
252b5132 7255 {
bc67d8a6 7256 section = sections[j];
252b5132 7257
bc67d8a6 7258 if (section == NULL)
252b5132
RH
7259 continue;
7260
bc67d8a6 7261 output_section = section->output_section;
252b5132 7262
bc67d8a6 7263 BFD_ASSERT (output_section != NULL);
c044fabd 7264
bc67d8a6
NC
7265 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
7266 || IS_COREFILE_NOTE (segment, section))
252b5132 7267 {
bc67d8a6 7268 if (map->count == 0)
252b5132
RH
7269 {
7270 /* If the first section in a segment does not start at
bc67d8a6
NC
7271 the beginning of the segment, then something is
7272 wrong. */
9933dc52
AM
7273 if (align_power (map->p_paddr
7274 + (map->includes_filehdr
7275 ? iehdr->e_ehsize : 0)
7276 + (map->includes_phdrs
7277 ? iehdr->e_phnum * iehdr->e_phentsize
7278 : 0),
7279 output_section->alignment_power)
7280 != output_section->lma)
252b5132
RH
7281 abort ();
7282 }
7283 else
7284 {
0067a569 7285 asection *prev_sec;
252b5132 7286
bc67d8a6 7287 prev_sec = map->sections[map->count - 1];
252b5132
RH
7288
7289 /* If the gap between the end of the previous section
bc67d8a6
NC
7290 and the start of this section is more than
7291 maxpagesize then we need to start a new segment. */
eea6121a 7292 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
079e9a2f 7293 maxpagesize)
caf47ea6 7294 < BFD_ALIGN (output_section->lma, maxpagesize))
0067a569 7295 || (prev_sec->lma + prev_sec->size
079e9a2f 7296 > output_section->lma))
252b5132 7297 {
9933dc52
AM
7298 if (suggested_lma == NULL)
7299 suggested_lma = output_section;
252b5132
RH
7300
7301 continue;
7302 }
7303 }
7304
bc67d8a6 7305 map->sections[map->count++] = output_section;
252b5132
RH
7306 ++isec;
7307 sections[j] = NULL;
9933dc52
AM
7308 if (segment->p_type == PT_LOAD)
7309 section->segment_mark = TRUE;
0067a569 7310 }
9933dc52
AM
7311 else if (suggested_lma == NULL)
7312 suggested_lma = output_section;
252b5132
RH
7313 }
7314
beab4532
NC
7315 /* PR 23932. A corrupt input file may contain sections that cannot
7316 be assigned to any segment - because for example they have a
7317 negative size - or segments that do not contain any sections. */
7318 if (map->count == 0)
7319 {
7320 bfd_set_error (bfd_error_bad_value);
7321 free (sections);
7322 return FALSE;
7323 }
252b5132
RH
7324
7325 /* Add the current segment to the list of built segments. */
c044fabd
KH
7326 *pointer_to_map = map;
7327 pointer_to_map = &map->next;
252b5132 7328
bc67d8a6 7329 if (isec < section_count)
252b5132
RH
7330 {
7331 /* We still have not allocated all of the sections to
7332 segments. Create a new segment here, initialise it
7333 and carry on looping. */
00bee008
AM
7334 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7335 amt += (bfd_size_type) section_count * sizeof (asection *);
5964fc3a 7336 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 7337 if (map == NULL)
5ed6aba4
NC
7338 {
7339 free (sections);
7340 return FALSE;
7341 }
252b5132
RH
7342
7343 /* Initialise the fields of the segment map. Set the physical
7344 physical address to the LMA of the first section that has
7345 not yet been assigned. */
0067a569
AM
7346 map->next = NULL;
7347 map->p_type = segment->p_type;
7348 map->p_flags = segment->p_flags;
7349 map->p_flags_valid = 1;
9933dc52 7350 map->p_paddr = suggested_lma->lma;
5c44b38e 7351 map->p_paddr_valid = p_paddr_valid;
bc67d8a6 7352 map->includes_filehdr = 0;
0067a569 7353 map->includes_phdrs = 0;
252b5132
RH
7354 }
7355 }
bc67d8a6 7356 while (isec < section_count);
252b5132
RH
7357
7358 free (sections);
7359 }
7360
12bd6957 7361 elf_seg_map (obfd) = map_first;
bc67d8a6
NC
7362
7363 /* If we had to estimate the number of program headers that were
9ad5cbcf 7364 going to be needed, then check our estimate now and adjust
bc67d8a6
NC
7365 the offset if necessary. */
7366 if (phdr_adjust_seg != NULL)
7367 {
7368 unsigned int count;
c044fabd 7369
bc67d8a6 7370 for (count = 0, map = map_first; map != NULL; map = map->next)
c044fabd 7371 count++;
252b5132 7372
bc67d8a6
NC
7373 if (count > phdr_adjust_num)
7374 phdr_adjust_seg->p_paddr
7375 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
9933dc52
AM
7376
7377 for (map = map_first; map != NULL; map = map->next)
7378 if (map->p_type == PT_PHDR)
7379 {
7380 bfd_vma adjust
7381 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7382 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7383 break;
7384 }
bc67d8a6 7385 }
c044fabd 7386
bc67d8a6 7387#undef SEGMENT_END
eecdbe52 7388#undef SECTION_SIZE
bc67d8a6
NC
7389#undef IS_CONTAINED_BY_VMA
7390#undef IS_CONTAINED_BY_LMA
0efc80c8 7391#undef IS_NOTE
252b5132 7392#undef IS_COREFILE_NOTE
bc67d8a6 7393#undef IS_SOLARIS_PT_INTERP
9f17e2a6 7394#undef IS_SECTION_IN_INPUT_SEGMENT
bc67d8a6
NC
7395#undef INCLUDE_SECTION_IN_SEGMENT
7396#undef SEGMENT_AFTER_SEGMENT
7397#undef SEGMENT_OVERLAPS
b34976b6 7398 return TRUE;
252b5132
RH
7399}
7400
84d1d650
L
7401/* Copy ELF program header information. */
7402
7403static bfd_boolean
7404copy_elf_program_header (bfd *ibfd, bfd *obfd)
7405{
7406 Elf_Internal_Ehdr *iehdr;
7407 struct elf_segment_map *map;
7408 struct elf_segment_map *map_first;
7409 struct elf_segment_map **pointer_to_map;
7410 Elf_Internal_Phdr *segment;
7411 unsigned int i;
7412 unsigned int num_segments;
7413 bfd_boolean phdr_included = FALSE;
88967714 7414 bfd_boolean p_paddr_valid;
84d1d650
L
7415
7416 iehdr = elf_elfheader (ibfd);
7417
7418 map_first = NULL;
7419 pointer_to_map = &map_first;
7420
88967714
AM
7421 /* If all the segment p_paddr fields are zero, don't set
7422 map->p_paddr_valid. */
7423 p_paddr_valid = FALSE;
84d1d650 7424 num_segments = elf_elfheader (ibfd)->e_phnum;
88967714
AM
7425 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7426 i < num_segments;
7427 i++, segment++)
7428 if (segment->p_paddr != 0)
7429 {
7430 p_paddr_valid = TRUE;
7431 break;
7432 }
7433
84d1d650
L
7434 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7435 i < num_segments;
7436 i++, segment++)
7437 {
7438 asection *section;
7439 unsigned int section_count;
7440 bfd_size_type amt;
7441 Elf_Internal_Shdr *this_hdr;
53020534 7442 asection *first_section = NULL;
a76e6f2f 7443 asection *lowest_section;
2542e49e 7444 bfd_boolean no_contents = TRUE;
84d1d650 7445
84d1d650
L
7446 /* Compute how many sections are in this segment. */
7447 for (section = ibfd->sections, section_count = 0;
7448 section != NULL;
7449 section = section->next)
7450 {
7451 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7452 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
3271a814 7453 {
a76e6f2f
AM
7454 if (first_section == NULL)
7455 first_section = section;
2542e49e
JL
7456 if (elf_section_type (section) != SHT_NOBITS)
7457 no_contents = FALSE;
3271a814
NS
7458 section_count++;
7459 }
84d1d650
L
7460 }
7461
7462 /* Allocate a segment map big enough to contain
7463 all of the sections we have selected. */
00bee008
AM
7464 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7465 amt += (bfd_size_type) section_count * sizeof (asection *);
a50b1753 7466 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
84d1d650
L
7467 if (map == NULL)
7468 return FALSE;
7469
7470 /* Initialize the fields of the output segment map with the
7471 input segment. */
7472 map->next = NULL;
7473 map->p_type = segment->p_type;
7474 map->p_flags = segment->p_flags;
7475 map->p_flags_valid = 1;
7476 map->p_paddr = segment->p_paddr;
88967714 7477 map->p_paddr_valid = p_paddr_valid;
3f570048
AM
7478 map->p_align = segment->p_align;
7479 map->p_align_valid = 1;
3271a814 7480 map->p_vaddr_offset = 0;
84d1d650 7481
04c3a755
NS
7482 if (map->p_type == PT_GNU_RELRO
7483 || map->p_type == PT_GNU_STACK)
b10a8ae0
L
7484 {
7485 /* The PT_GNU_RELRO segment may contain the first a few
7486 bytes in the .got.plt section even if the whole .got.plt
7487 section isn't in the PT_GNU_RELRO segment. We won't
04c3a755
NS
7488 change the size of the PT_GNU_RELRO segment.
7489 Similarly, PT_GNU_STACK size is significant on uclinux
7490 systems. */
9433b9b1 7491 map->p_size = segment->p_memsz;
b10a8ae0
L
7492 map->p_size_valid = 1;
7493 }
7494
84d1d650
L
7495 /* Determine if this segment contains the ELF file header
7496 and if it contains the program headers themselves. */
7497 map->includes_filehdr = (segment->p_offset == 0
7498 && segment->p_filesz >= iehdr->e_ehsize);
7499
7500 map->includes_phdrs = 0;
7501 if (! phdr_included || segment->p_type != PT_LOAD)
7502 {
7503 map->includes_phdrs =
7504 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7505 && (segment->p_offset + segment->p_filesz
7506 >= ((bfd_vma) iehdr->e_phoff
7507 + iehdr->e_phnum * iehdr->e_phentsize)));
7508
7509 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7510 phdr_included = TRUE;
7511 }
7512
bbefd0a9 7513 lowest_section = NULL;
84d1d650
L
7514 if (section_count != 0)
7515 {
7516 unsigned int isec = 0;
7517
53020534 7518 for (section = first_section;
84d1d650
L
7519 section != NULL;
7520 section = section->next)
7521 {
7522 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7523 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
53020534
L
7524 {
7525 map->sections[isec++] = section->output_section;
a76e6f2f
AM
7526 if ((section->flags & SEC_ALLOC) != 0)
7527 {
7528 bfd_vma seg_off;
7529
bbefd0a9
AM
7530 if (lowest_section == NULL
7531 || section->lma < lowest_section->lma)
fb8a5684
AM
7532 lowest_section = section;
7533
a76e6f2f
AM
7534 /* Section lmas are set up from PT_LOAD header
7535 p_paddr in _bfd_elf_make_section_from_shdr.
7536 If this header has a p_paddr that disagrees
7537 with the section lma, flag the p_paddr as
7538 invalid. */
7539 if ((section->flags & SEC_LOAD) != 0)
7540 seg_off = this_hdr->sh_offset - segment->p_offset;
7541 else
7542 seg_off = this_hdr->sh_addr - segment->p_vaddr;
7543 if (section->lma - segment->p_paddr != seg_off)
7544 map->p_paddr_valid = FALSE;
7545 }
53020534
L
7546 if (isec == section_count)
7547 break;
7548 }
84d1d650
L
7549 }
7550 }
7551
a76e6f2f 7552 if (map->includes_filehdr && lowest_section != NULL)
2542e49e
JL
7553 {
7554 /* Try to keep the space used by the headers plus any
7555 padding fixed. If there are sections with file contents
7556 in this segment then the lowest sh_offset is the best
7557 guess. Otherwise the segment only has file contents for
7558 the headers, and p_filesz is the best guess. */
7559 if (no_contents)
7560 map->header_size = segment->p_filesz;
7561 else
7562 map->header_size = lowest_section->filepos;
7563 }
d324f6d6 7564
5d695627
AM
7565 if (section_count == 0)
7566 map->p_vaddr_offset = segment->p_vaddr;
7567 else if (!map->includes_phdrs
7568 && !map->includes_filehdr
7569 && map->p_paddr_valid)
7570 /* Account for padding before the first section. */
7571 map->p_vaddr_offset = (segment->p_paddr
7572 - (lowest_section ? lowest_section->lma : 0));
a76e6f2f 7573
84d1d650
L
7574 map->count = section_count;
7575 *pointer_to_map = map;
7576 pointer_to_map = &map->next;
7577 }
7578
12bd6957 7579 elf_seg_map (obfd) = map_first;
84d1d650
L
7580 return TRUE;
7581}
7582
7583/* Copy private BFD data. This copies or rewrites ELF program header
7584 information. */
7585
7586static bfd_boolean
7587copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7588{
84d1d650
L
7589 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7590 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7591 return TRUE;
7592
7593 if (elf_tdata (ibfd)->phdr == NULL)
7594 return TRUE;
7595
7596 if (ibfd->xvec == obfd->xvec)
7597 {
cb3ff1e5
NC
7598 /* Check to see if any sections in the input BFD
7599 covered by ELF program header have changed. */
d55ce4e2 7600 Elf_Internal_Phdr *segment;
84d1d650
L
7601 asection *section, *osec;
7602 unsigned int i, num_segments;
7603 Elf_Internal_Shdr *this_hdr;
147d51c2
L
7604 const struct elf_backend_data *bed;
7605
7606 bed = get_elf_backend_data (ibfd);
7607
7608 /* Regenerate the segment map if p_paddr is set to 0. */
7609 if (bed->want_p_paddr_set_to_zero)
7610 goto rewrite;
84d1d650
L
7611
7612 /* Initialize the segment mark field. */
7613 for (section = obfd->sections; section != NULL;
7614 section = section->next)
7615 section->segment_mark = FALSE;
7616
7617 num_segments = elf_elfheader (ibfd)->e_phnum;
7618 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7619 i < num_segments;
7620 i++, segment++)
7621 {
5f6999aa
NC
7622 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7623 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7624 which severly confuses things, so always regenerate the segment
7625 map in this case. */
7626 if (segment->p_paddr == 0
7627 && segment->p_memsz == 0
7628 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
cb3ff1e5 7629 goto rewrite;
5f6999aa 7630
84d1d650
L
7631 for (section = ibfd->sections;
7632 section != NULL; section = section->next)
7633 {
7634 /* We mark the output section so that we know it comes
7635 from the input BFD. */
7636 osec = section->output_section;
7637 if (osec)
7638 osec->segment_mark = TRUE;
7639
7640 /* Check if this section is covered by the segment. */
7641 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7642 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
84d1d650
L
7643 {
7644 /* FIXME: Check if its output section is changed or
7645 removed. What else do we need to check? */
7646 if (osec == NULL
7647 || section->flags != osec->flags
7648 || section->lma != osec->lma
7649 || section->vma != osec->vma
7650 || section->size != osec->size
7651 || section->rawsize != osec->rawsize
7652 || section->alignment_power != osec->alignment_power)
7653 goto rewrite;
7654 }
7655 }
7656 }
7657
cb3ff1e5 7658 /* Check to see if any output section do not come from the
84d1d650
L
7659 input BFD. */
7660 for (section = obfd->sections; section != NULL;
7661 section = section->next)
7662 {
535b785f 7663 if (!section->segment_mark)
84d1d650
L
7664 goto rewrite;
7665 else
7666 section->segment_mark = FALSE;
7667 }
7668
7669 return copy_elf_program_header (ibfd, obfd);
7670 }
7671
7672rewrite:
f1d85785
L
7673 if (ibfd->xvec == obfd->xvec)
7674 {
7675 /* When rewriting program header, set the output maxpagesize to
7676 the maximum alignment of input PT_LOAD segments. */
7677 Elf_Internal_Phdr *segment;
7678 unsigned int i;
7679 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
7680 bfd_vma maxpagesize = 0;
7681
7682 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7683 i < num_segments;
7684 i++, segment++)
7685 if (segment->p_type == PT_LOAD
7686 && maxpagesize < segment->p_align)
c86934ce
NC
7687 {
7688 /* PR 17512: file: f17299af. */
7689 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
695344c0 7690 /* xgettext:c-format */
2dcf00ce
AM
7691 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7692 PRIx64 " is too large"),
7693 ibfd, (uint64_t) segment->p_align);
c86934ce
NC
7694 else
7695 maxpagesize = segment->p_align;
7696 }
f1d85785
L
7697
7698 if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
7699 bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
7700 }
7701
84d1d650
L
7702 return rewrite_elf_program_header (ibfd, obfd);
7703}
7704
ccd2ec6a
L
7705/* Initialize private output section information from input section. */
7706
7707bfd_boolean
7708_bfd_elf_init_private_section_data (bfd *ibfd,
7709 asection *isec,
7710 bfd *obfd,
7711 asection *osec,
7712 struct bfd_link_info *link_info)
7713
7714{
7715 Elf_Internal_Shdr *ihdr, *ohdr;
0e1862bb
L
7716 bfd_boolean final_link = (link_info != NULL
7717 && !bfd_link_relocatable (link_info));
ccd2ec6a
L
7718
7719 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7720 || obfd->xvec->flavour != bfd_target_elf_flavour)
7721 return TRUE;
7722
ba85c43e
NC
7723 BFD_ASSERT (elf_section_data (osec) != NULL);
7724
dfa7b0b8
AM
7725 /* For objcopy and relocatable link, don't copy the output ELF
7726 section type from input if the output BFD section flags have been
7727 set to something different. For a final link allow some flags
7728 that the linker clears to differ. */
42bb2e33 7729 if (elf_section_type (osec) == SHT_NULL
dfa7b0b8
AM
7730 && (osec->flags == isec->flags
7731 || (final_link
7732 && ((osec->flags ^ isec->flags)
0814be7d 7733 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
42bb2e33 7734 elf_section_type (osec) = elf_section_type (isec);
d270463e
L
7735
7736 /* FIXME: Is this correct for all OS/PROC specific flags? */
7737 elf_section_flags (osec) |= (elf_section_flags (isec)
7738 & (SHF_MASKOS | SHF_MASKPROC));
ccd2ec6a 7739
a91e1603 7740 /* Copy sh_info from input for mbind section. */
df3a023b
AM
7741 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
7742 && elf_section_flags (isec) & SHF_GNU_MBIND)
a91e1603
L
7743 elf_section_data (osec)->this_hdr.sh_info
7744 = elf_section_data (isec)->this_hdr.sh_info;
7745
ccd2ec6a
L
7746 /* Set things up for objcopy and relocatable link. The output
7747 SHT_GROUP section will have its elf_next_in_group pointing back
7748 to the input group members. Ignore linker created group section.
7749 See elfNN_ia64_object_p in elfxx-ia64.c. */
7bdf4127
AB
7750 if ((link_info == NULL
7751 || !link_info->resolve_section_groups)
7752 && (elf_sec_group (isec) == NULL
7753 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
ccd2ec6a 7754 {
7bdf4127
AB
7755 if (elf_section_flags (isec) & SHF_GROUP)
7756 elf_section_flags (osec) |= SHF_GROUP;
7757 elf_next_in_group (osec) = elf_next_in_group (isec);
7758 elf_section_data (osec)->group = elf_section_data (isec)->group;
ccd2ec6a
L
7759 }
7760
7bdf4127
AB
7761 /* If not decompress, preserve SHF_COMPRESSED. */
7762 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
7763 elf_section_flags (osec) |= (elf_section_flags (isec)
7764 & SHF_COMPRESSED);
7765
ccd2ec6a
L
7766 ihdr = &elf_section_data (isec)->this_hdr;
7767
7768 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7769 don't use the output section of the linked-to section since it
7770 may be NULL at this point. */
7771 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7772 {
7773 ohdr = &elf_section_data (osec)->this_hdr;
7774 ohdr->sh_flags |= SHF_LINK_ORDER;
7775 elf_linked_to_section (osec) = elf_linked_to_section (isec);
7776 }
7777
7778 osec->use_rela_p = isec->use_rela_p;
7779
7780 return TRUE;
7781}
7782
252b5132
RH
7783/* Copy private section information. This copies over the entsize
7784 field, and sometimes the info field. */
7785
b34976b6 7786bfd_boolean
217aa764
AM
7787_bfd_elf_copy_private_section_data (bfd *ibfd,
7788 asection *isec,
7789 bfd *obfd,
7790 asection *osec)
252b5132
RH
7791{
7792 Elf_Internal_Shdr *ihdr, *ohdr;
7793
7794 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7795 || obfd->xvec->flavour != bfd_target_elf_flavour)
b34976b6 7796 return TRUE;
252b5132 7797
252b5132
RH
7798 ihdr = &elf_section_data (isec)->this_hdr;
7799 ohdr = &elf_section_data (osec)->this_hdr;
7800
7801 ohdr->sh_entsize = ihdr->sh_entsize;
7802
7803 if (ihdr->sh_type == SHT_SYMTAB
7804 || ihdr->sh_type == SHT_DYNSYM
7805 || ihdr->sh_type == SHT_GNU_verneed
7806 || ihdr->sh_type == SHT_GNU_verdef)
7807 ohdr->sh_info = ihdr->sh_info;
7808
ccd2ec6a
L
7809 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
7810 NULL);
252b5132
RH
7811}
7812
d0bf826b
AM
7813/* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7814 necessary if we are removing either the SHT_GROUP section or any of
7815 the group member sections. DISCARDED is the value that a section's
7816 output_section has if the section will be discarded, NULL when this
7817 function is called from objcopy, bfd_abs_section_ptr when called
7818 from the linker. */
80fccad2
BW
7819
7820bfd_boolean
d0bf826b 7821_bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
80fccad2 7822{
30288845
AM
7823 asection *isec;
7824
30288845 7825 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
415f38a6 7826 if (elf_section_type (isec) == SHT_GROUP)
30288845
AM
7827 {
7828 asection *first = elf_next_in_group (isec);
7829 asection *s = first;
d0bf826b
AM
7830 bfd_size_type removed = 0;
7831
30288845
AM
7832 while (s != NULL)
7833 {
415f38a6
AM
7834 /* If this member section is being output but the
7835 SHT_GROUP section is not, then clear the group info
7836 set up by _bfd_elf_copy_private_section_data. */
d0bf826b
AM
7837 if (s->output_section != discarded
7838 && isec->output_section == discarded)
30288845
AM
7839 {
7840 elf_section_flags (s->output_section) &= ~SHF_GROUP;
7841 elf_group_name (s->output_section) = NULL;
7842 }
415f38a6
AM
7843 /* Conversely, if the member section is not being output
7844 but the SHT_GROUP section is, then adjust its size. */
d0bf826b
AM
7845 else if (s->output_section == discarded
7846 && isec->output_section != discarded)
6e5e9d58
AM
7847 {
7848 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
7849 removed += 4;
7850 if (elf_sec->rel.hdr != NULL
7851 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
7852 removed += 4;
7853 if (elf_sec->rela.hdr != NULL
7854 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
7855 removed += 4;
7856 }
30288845
AM
7857 s = elf_next_in_group (s);
7858 if (s == first)
7859 break;
7860 }
d0bf826b
AM
7861 if (removed != 0)
7862 {
7863 if (discarded != NULL)
7864 {
7865 /* If we've been called for ld -r, then we need to
6e5e9d58 7866 adjust the input section size. */
d0bf826b
AM
7867 if (isec->rawsize == 0)
7868 isec->rawsize = isec->size;
7869 isec->size = isec->rawsize - removed;
6e5e9d58
AM
7870 if (isec->size <= 4)
7871 {
7872 isec->size = 0;
7873 isec->flags |= SEC_EXCLUDE;
7874 }
d0bf826b
AM
7875 }
7876 else
7877 {
7878 /* Adjust the output section size when called from
7879 objcopy. */
7880 isec->output_section->size -= removed;
6e5e9d58
AM
7881 if (isec->output_section->size <= 4)
7882 {
7883 isec->output_section->size = 0;
7884 isec->output_section->flags |= SEC_EXCLUDE;
7885 }
d0bf826b
AM
7886 }
7887 }
30288845
AM
7888 }
7889
80fccad2
BW
7890 return TRUE;
7891}
7892
d0bf826b
AM
7893/* Copy private header information. */
7894
7895bfd_boolean
7896_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
7897{
7898 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7899 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7900 return TRUE;
7901
7902 /* Copy over private BFD data if it has not already been copied.
7903 This must be done here, rather than in the copy_private_bfd_data
7904 entry point, because the latter is called after the section
7905 contents have been set, which means that the program headers have
7906 already been worked out. */
12bd6957 7907 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
d0bf826b
AM
7908 {
7909 if (! copy_private_bfd_data (ibfd, obfd))
7910 return FALSE;
7911 }
7912
7913 return _bfd_elf_fixup_group_sections (ibfd, NULL);
7914}
7915
252b5132
RH
7916/* Copy private symbol information. If this symbol is in a section
7917 which we did not map into a BFD section, try to map the section
7918 index correctly. We use special macro definitions for the mapped
7919 section indices; these definitions are interpreted by the
7920 swap_out_syms function. */
7921
9ad5cbcf
AM
7922#define MAP_ONESYMTAB (SHN_HIOS + 1)
7923#define MAP_DYNSYMTAB (SHN_HIOS + 2)
7924#define MAP_STRTAB (SHN_HIOS + 3)
7925#define MAP_SHSTRTAB (SHN_HIOS + 4)
7926#define MAP_SYM_SHNDX (SHN_HIOS + 5)
252b5132 7927
b34976b6 7928bfd_boolean
217aa764
AM
7929_bfd_elf_copy_private_symbol_data (bfd *ibfd,
7930 asymbol *isymarg,
7931 bfd *obfd,
7932 asymbol *osymarg)
252b5132
RH
7933{
7934 elf_symbol_type *isym, *osym;
7935
7936 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7937 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 7938 return TRUE;
252b5132
RH
7939
7940 isym = elf_symbol_from (ibfd, isymarg);
7941 osym = elf_symbol_from (obfd, osymarg);
7942
7943 if (isym != NULL
8424d8f5 7944 && isym->internal_elf_sym.st_shndx != 0
252b5132
RH
7945 && osym != NULL
7946 && bfd_is_abs_section (isym->symbol.section))
7947 {
7948 unsigned int shndx;
7949
7950 shndx = isym->internal_elf_sym.st_shndx;
7951 if (shndx == elf_onesymtab (ibfd))
7952 shndx = MAP_ONESYMTAB;
7953 else if (shndx == elf_dynsymtab (ibfd))
7954 shndx = MAP_DYNSYMTAB;
12bd6957 7955 else if (shndx == elf_strtab_sec (ibfd))
252b5132 7956 shndx = MAP_STRTAB;
12bd6957 7957 else if (shndx == elf_shstrtab_sec (ibfd))
252b5132 7958 shndx = MAP_SHSTRTAB;
6a40cf0c 7959 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
9ad5cbcf 7960 shndx = MAP_SYM_SHNDX;
252b5132
RH
7961 osym->internal_elf_sym.st_shndx = shndx;
7962 }
7963
b34976b6 7964 return TRUE;
252b5132
RH
7965}
7966
7967/* Swap out the symbols. */
7968
b34976b6 7969static bfd_boolean
217aa764 7970swap_out_syms (bfd *abfd,
ef10c3ac 7971 struct elf_strtab_hash **sttp,
217aa764 7972 int relocatable_p)
252b5132 7973{
9c5bfbb7 7974 const struct elf_backend_data *bed;
079e9a2f
AM
7975 int symcount;
7976 asymbol **syms;
ef10c3ac 7977 struct elf_strtab_hash *stt;
079e9a2f 7978 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 7979 Elf_Internal_Shdr *symtab_shndx_hdr;
079e9a2f 7980 Elf_Internal_Shdr *symstrtab_hdr;
ef10c3ac 7981 struct elf_sym_strtab *symstrtab;
f075ee0c
AM
7982 bfd_byte *outbound_syms;
7983 bfd_byte *outbound_shndx;
ef10c3ac
L
7984 unsigned long outbound_syms_index;
7985 unsigned long outbound_shndx_index;
079e9a2f 7986 int idx;
12bd6957 7987 unsigned int num_locals;
079e9a2f 7988 bfd_size_type amt;
174fd7f9 7989 bfd_boolean name_local_sections;
252b5132 7990
12bd6957 7991 if (!elf_map_symbols (abfd, &num_locals))
b34976b6 7992 return FALSE;
252b5132 7993
c044fabd 7994 /* Dump out the symtabs. */
ef10c3ac 7995 stt = _bfd_elf_strtab_init ();
079e9a2f 7996 if (stt == NULL)
b34976b6 7997 return FALSE;
252b5132 7998
079e9a2f
AM
7999 bed = get_elf_backend_data (abfd);
8000 symcount = bfd_get_symcount (abfd);
8001 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8002 symtab_hdr->sh_type = SHT_SYMTAB;
8003 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8004 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
12bd6957 8005 symtab_hdr->sh_info = num_locals + 1;
72de5009 8006 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
079e9a2f
AM
8007
8008 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8009 symstrtab_hdr->sh_type = SHT_STRTAB;
8010
ef10c3ac 8011 /* Allocate buffer to swap out the .strtab section. */
7a6e0d89
AM
8012 symstrtab = (struct elf_sym_strtab *) bfd_malloc2 (symcount + 1,
8013 sizeof (*symstrtab));
ef10c3ac
L
8014 if (symstrtab == NULL)
8015 {
8016 _bfd_elf_strtab_free (stt);
8017 return FALSE;
8018 }
8019
a50b1753 8020 outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
07d6d2b8 8021 bed->s->sizeof_sym);
079e9a2f 8022 if (outbound_syms == NULL)
5ed6aba4 8023 {
ef10c3ac
L
8024error_return:
8025 _bfd_elf_strtab_free (stt);
8026 free (symstrtab);
5ed6aba4
NC
8027 return FALSE;
8028 }
217aa764 8029 symtab_hdr->contents = outbound_syms;
ef10c3ac 8030 outbound_syms_index = 0;
252b5132 8031
9ad5cbcf 8032 outbound_shndx = NULL;
ef10c3ac 8033 outbound_shndx_index = 0;
6a40cf0c
NC
8034
8035 if (elf_symtab_shndx_list (abfd))
9ad5cbcf 8036 {
6a40cf0c
NC
8037 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8038 if (symtab_shndx_hdr->sh_name != 0)
8039 {
8040 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
8041 outbound_shndx = (bfd_byte *)
8042 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
8043 if (outbound_shndx == NULL)
8044 goto error_return;
5ed6aba4 8045
6a40cf0c
NC
8046 symtab_shndx_hdr->contents = outbound_shndx;
8047 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8048 symtab_shndx_hdr->sh_size = amt;
8049 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8050 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8051 }
8052 /* FIXME: What about any other headers in the list ? */
9ad5cbcf
AM
8053 }
8054
589e6347 8055 /* Now generate the data (for "contents"). */
079e9a2f
AM
8056 {
8057 /* Fill in zeroth symbol and swap it out. */
8058 Elf_Internal_Sym sym;
8059 sym.st_name = 0;
8060 sym.st_value = 0;
8061 sym.st_size = 0;
8062 sym.st_info = 0;
8063 sym.st_other = 0;
8064 sym.st_shndx = SHN_UNDEF;
35fc36a8 8065 sym.st_target_internal = 0;
ef10c3ac
L
8066 symstrtab[0].sym = sym;
8067 symstrtab[0].dest_index = outbound_syms_index;
8068 symstrtab[0].destshndx_index = outbound_shndx_index;
8069 outbound_syms_index++;
9ad5cbcf 8070 if (outbound_shndx != NULL)
ef10c3ac 8071 outbound_shndx_index++;
079e9a2f 8072 }
252b5132 8073
174fd7f9
RS
8074 name_local_sections
8075 = (bed->elf_backend_name_local_section_symbols
8076 && bed->elf_backend_name_local_section_symbols (abfd));
8077
079e9a2f 8078 syms = bfd_get_outsymbols (abfd);
ef10c3ac 8079 for (idx = 0; idx < symcount;)
252b5132 8080 {
252b5132 8081 Elf_Internal_Sym sym;
079e9a2f
AM
8082 bfd_vma value = syms[idx]->value;
8083 elf_symbol_type *type_ptr;
8084 flagword flags = syms[idx]->flags;
8085 int type;
252b5132 8086
174fd7f9
RS
8087 if (!name_local_sections
8088 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
079e9a2f
AM
8089 {
8090 /* Local section symbols have no name. */
ef10c3ac 8091 sym.st_name = (unsigned long) -1;
079e9a2f
AM
8092 }
8093 else
8094 {
ef10c3ac
L
8095 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8096 to get the final offset for st_name. */
8097 sym.st_name
8098 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
8099 FALSE);
079e9a2f 8100 if (sym.st_name == (unsigned long) -1)
ef10c3ac 8101 goto error_return;
079e9a2f 8102 }
252b5132 8103
079e9a2f 8104 type_ptr = elf_symbol_from (abfd, syms[idx]);
252b5132 8105
079e9a2f
AM
8106 if ((flags & BSF_SECTION_SYM) == 0
8107 && bfd_is_com_section (syms[idx]->section))
8108 {
8109 /* ELF common symbols put the alignment into the `value' field,
8110 and the size into the `size' field. This is backwards from
8111 how BFD handles it, so reverse it here. */
8112 sym.st_size = value;
8113 if (type_ptr == NULL
8114 || type_ptr->internal_elf_sym.st_value == 0)
8115 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8116 else
8117 sym.st_value = type_ptr->internal_elf_sym.st_value;
8118 sym.st_shndx = _bfd_elf_section_from_bfd_section
8119 (abfd, syms[idx]->section);
8120 }
8121 else
8122 {
8123 asection *sec = syms[idx]->section;
cb33740c 8124 unsigned int shndx;
252b5132 8125
079e9a2f
AM
8126 if (sec->output_section)
8127 {
8128 value += sec->output_offset;
8129 sec = sec->output_section;
8130 }
589e6347 8131
079e9a2f
AM
8132 /* Don't add in the section vma for relocatable output. */
8133 if (! relocatable_p)
8134 value += sec->vma;
8135 sym.st_value = value;
8136 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8137
8138 if (bfd_is_abs_section (sec)
8139 && type_ptr != NULL
8140 && type_ptr->internal_elf_sym.st_shndx != 0)
8141 {
8142 /* This symbol is in a real ELF section which we did
8143 not create as a BFD section. Undo the mapping done
8144 by copy_private_symbol_data. */
8145 shndx = type_ptr->internal_elf_sym.st_shndx;
8146 switch (shndx)
8147 {
8148 case MAP_ONESYMTAB:
8149 shndx = elf_onesymtab (abfd);
8150 break;
8151 case MAP_DYNSYMTAB:
8152 shndx = elf_dynsymtab (abfd);
8153 break;
8154 case MAP_STRTAB:
12bd6957 8155 shndx = elf_strtab_sec (abfd);
079e9a2f
AM
8156 break;
8157 case MAP_SHSTRTAB:
12bd6957 8158 shndx = elf_shstrtab_sec (abfd);
079e9a2f 8159 break;
9ad5cbcf 8160 case MAP_SYM_SHNDX:
6a40cf0c
NC
8161 if (elf_symtab_shndx_list (abfd))
8162 shndx = elf_symtab_shndx_list (abfd)->ndx;
9ad5cbcf 8163 break;
079e9a2f 8164 default:
15bc576a 8165 shndx = SHN_ABS;
079e9a2f
AM
8166 break;
8167 }
8168 }
8169 else
8170 {
8171 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
252b5132 8172
cb33740c 8173 if (shndx == SHN_BAD)
079e9a2f
AM
8174 {
8175 asection *sec2;
8176
8177 /* Writing this would be a hell of a lot easier if
8178 we had some decent documentation on bfd, and
8179 knew what to expect of the library, and what to
8180 demand of applications. For example, it
8181 appears that `objcopy' might not set the
8182 section of a symbol to be a section that is
8183 actually in the output file. */
8184 sec2 = bfd_get_section_by_name (abfd, sec->name);
5df1bc57
AM
8185 if (sec2 != NULL)
8186 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8187 if (shndx == SHN_BAD)
589e6347 8188 {
695344c0 8189 /* xgettext:c-format */
9793eb77
AM
8190 _bfd_error_handler
8191 (_("unable to find equivalent output section"
8192 " for symbol '%s' from section '%s'"),
8193 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8194 sec->name);
811072d8 8195 bfd_set_error (bfd_error_invalid_operation);
ef10c3ac 8196 goto error_return;
589e6347 8197 }
079e9a2f
AM
8198 }
8199 }
252b5132 8200
079e9a2f
AM
8201 sym.st_shndx = shndx;
8202 }
252b5132 8203
13ae64f3
JJ
8204 if ((flags & BSF_THREAD_LOCAL) != 0)
8205 type = STT_TLS;
d8045f23
NC
8206 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8207 type = STT_GNU_IFUNC;
13ae64f3 8208 else if ((flags & BSF_FUNCTION) != 0)
079e9a2f
AM
8209 type = STT_FUNC;
8210 else if ((flags & BSF_OBJECT) != 0)
8211 type = STT_OBJECT;
d9352518
DB
8212 else if ((flags & BSF_RELC) != 0)
8213 type = STT_RELC;
8214 else if ((flags & BSF_SRELC) != 0)
8215 type = STT_SRELC;
079e9a2f
AM
8216 else
8217 type = STT_NOTYPE;
252b5132 8218
13ae64f3
JJ
8219 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8220 type = STT_TLS;
8221
589e6347 8222 /* Processor-specific types. */
079e9a2f
AM
8223 if (type_ptr != NULL
8224 && bed->elf_backend_get_symbol_type)
8225 type = ((*bed->elf_backend_get_symbol_type)
8226 (&type_ptr->internal_elf_sym, type));
252b5132 8227
079e9a2f
AM
8228 if (flags & BSF_SECTION_SYM)
8229 {
8230 if (flags & BSF_GLOBAL)
8231 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8232 else
8233 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8234 }
8235 else if (bfd_is_com_section (syms[idx]->section))
0a40daed 8236 {
b8871f35
L
8237 if (type != STT_TLS)
8238 {
8239 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8240 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8241 ? STT_COMMON : STT_OBJECT);
8242 else
8243 type = ((flags & BSF_ELF_COMMON) != 0
8244 ? STT_COMMON : STT_OBJECT);
8245 }
8246 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
0a40daed 8247 }
079e9a2f
AM
8248 else if (bfd_is_und_section (syms[idx]->section))
8249 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8250 ? STB_WEAK
8251 : STB_GLOBAL),
8252 type);
8253 else if (flags & BSF_FILE)
8254 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8255 else
8256 {
8257 int bind = STB_LOCAL;
252b5132 8258
079e9a2f
AM
8259 if (flags & BSF_LOCAL)
8260 bind = STB_LOCAL;
3e7a7d11
NC
8261 else if (flags & BSF_GNU_UNIQUE)
8262 bind = STB_GNU_UNIQUE;
079e9a2f
AM
8263 else if (flags & BSF_WEAK)
8264 bind = STB_WEAK;
8265 else if (flags & BSF_GLOBAL)
8266 bind = STB_GLOBAL;
252b5132 8267
079e9a2f
AM
8268 sym.st_info = ELF_ST_INFO (bind, type);
8269 }
252b5132 8270
079e9a2f 8271 if (type_ptr != NULL)
35fc36a8
RS
8272 {
8273 sym.st_other = type_ptr->internal_elf_sym.st_other;
8274 sym.st_target_internal
8275 = type_ptr->internal_elf_sym.st_target_internal;
8276 }
079e9a2f 8277 else
35fc36a8
RS
8278 {
8279 sym.st_other = 0;
8280 sym.st_target_internal = 0;
8281 }
252b5132 8282
ef10c3ac
L
8283 idx++;
8284 symstrtab[idx].sym = sym;
8285 symstrtab[idx].dest_index = outbound_syms_index;
8286 symstrtab[idx].destshndx_index = outbound_shndx_index;
8287
8288 outbound_syms_index++;
9ad5cbcf 8289 if (outbound_shndx != NULL)
ef10c3ac
L
8290 outbound_shndx_index++;
8291 }
8292
8293 /* Finalize the .strtab section. */
8294 _bfd_elf_strtab_finalize (stt);
8295
8296 /* Swap out the .strtab section. */
8297 for (idx = 0; idx <= symcount; idx++)
8298 {
8299 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8300 if (elfsym->sym.st_name == (unsigned long) -1)
8301 elfsym->sym.st_name = 0;
8302 else
8303 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8304 elfsym->sym.st_name);
8305 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8306 (outbound_syms
8307 + (elfsym->dest_index
8308 * bed->s->sizeof_sym)),
8309 (outbound_shndx
8310 + (elfsym->destshndx_index
8311 * sizeof (Elf_External_Sym_Shndx))));
079e9a2f 8312 }
ef10c3ac 8313 free (symstrtab);
252b5132 8314
079e9a2f 8315 *sttp = stt;
ef10c3ac 8316 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
079e9a2f 8317 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 8318 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
079e9a2f
AM
8319 symstrtab_hdr->sh_addr = 0;
8320 symstrtab_hdr->sh_entsize = 0;
8321 symstrtab_hdr->sh_link = 0;
8322 symstrtab_hdr->sh_info = 0;
8323 symstrtab_hdr->sh_addralign = 1;
252b5132 8324
b34976b6 8325 return TRUE;
252b5132
RH
8326}
8327
8328/* Return the number of bytes required to hold the symtab vector.
8329
8330 Note that we base it on the count plus 1, since we will null terminate
8331 the vector allocated based on this size. However, the ELF symbol table
8332 always has a dummy entry as symbol #0, so it ends up even. */
8333
8334long
217aa764 8335_bfd_elf_get_symtab_upper_bound (bfd *abfd)
252b5132 8336{
3a551c7a 8337 bfd_size_type symcount;
252b5132
RH
8338 long symtab_size;
8339 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8340
8341 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3a551c7a
AM
8342 if (symcount >= LONG_MAX / sizeof (asymbol *))
8343 {
8344 bfd_set_error (bfd_error_file_too_big);
8345 return -1;
8346 }
b99d1833
AM
8347 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8348 if (symcount > 0)
8349 symtab_size -= sizeof (asymbol *);
252b5132
RH
8350
8351 return symtab_size;
8352}
8353
8354long
217aa764 8355_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
252b5132 8356{
3a551c7a 8357 bfd_size_type symcount;
252b5132
RH
8358 long symtab_size;
8359 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8360
8361 if (elf_dynsymtab (abfd) == 0)
8362 {
8363 bfd_set_error (bfd_error_invalid_operation);
8364 return -1;
8365 }
8366
8367 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3a551c7a
AM
8368 if (symcount >= LONG_MAX / sizeof (asymbol *))
8369 {
8370 bfd_set_error (bfd_error_file_too_big);
8371 return -1;
8372 }
b99d1833
AM
8373 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8374 if (symcount > 0)
8375 symtab_size -= sizeof (asymbol *);
252b5132
RH
8376
8377 return symtab_size;
8378}
8379
8380long
217aa764
AM
8381_bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
8382 sec_ptr asect)
252b5132 8383{
242a1159 8384#if SIZEOF_LONG == SIZEOF_INT
7a6e0d89
AM
8385 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
8386 {
8387 bfd_set_error (bfd_error_file_too_big);
8388 return -1;
8389 }
242a1159 8390#endif
252b5132
RH
8391 return (asect->reloc_count + 1) * sizeof (arelent *);
8392}
8393
8394/* Canonicalize the relocs. */
8395
8396long
217aa764
AM
8397_bfd_elf_canonicalize_reloc (bfd *abfd,
8398 sec_ptr section,
8399 arelent **relptr,
8400 asymbol **symbols)
252b5132
RH
8401{
8402 arelent *tblptr;
8403 unsigned int i;
9c5bfbb7 8404 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 8405
b34976b6 8406 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
252b5132
RH
8407 return -1;
8408
8409 tblptr = section->relocation;
8410 for (i = 0; i < section->reloc_count; i++)
8411 *relptr++ = tblptr++;
8412
8413 *relptr = NULL;
8414
8415 return section->reloc_count;
8416}
8417
8418long
6cee3f79 8419_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
252b5132 8420{
9c5bfbb7 8421 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764 8422 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
252b5132
RH
8423
8424 if (symcount >= 0)
ed48ec2e 8425 abfd->symcount = symcount;
252b5132
RH
8426 return symcount;
8427}
8428
8429long
217aa764
AM
8430_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8431 asymbol **allocation)
252b5132 8432{
9c5bfbb7 8433 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764 8434 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
1f70368c
DJ
8435
8436 if (symcount >= 0)
ed48ec2e 8437 abfd->dynsymcount = symcount;
1f70368c 8438 return symcount;
252b5132
RH
8439}
8440
8615f3f2
AM
8441/* Return the size required for the dynamic reloc entries. Any loadable
8442 section that was actually installed in the BFD, and has type SHT_REL
8443 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8444 dynamic reloc section. */
252b5132
RH
8445
8446long
217aa764 8447_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
252b5132 8448{
3a551c7a 8449 bfd_size_type count;
252b5132
RH
8450 asection *s;
8451
8452 if (elf_dynsymtab (abfd) == 0)
8453 {
8454 bfd_set_error (bfd_error_invalid_operation);
8455 return -1;
8456 }
8457
3a551c7a 8458 count = 1;
252b5132 8459 for (s = abfd->sections; s != NULL; s = s->next)
266b05cf 8460 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
8461 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8462 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
3a551c7a
AM
8463 {
8464 count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
8465 if (count > LONG_MAX / sizeof (arelent *))
8466 {
8467 bfd_set_error (bfd_error_file_too_big);
8468 return -1;
8469 }
8470 }
8471 return count * sizeof (arelent *);
252b5132
RH
8472}
8473
8615f3f2
AM
8474/* Canonicalize the dynamic relocation entries. Note that we return the
8475 dynamic relocations as a single block, although they are actually
8476 associated with particular sections; the interface, which was
8477 designed for SunOS style shared libraries, expects that there is only
8478 one set of dynamic relocs. Any loadable section that was actually
8479 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8480 dynamic symbol table, is considered to be a dynamic reloc section. */
252b5132
RH
8481
8482long
217aa764
AM
8483_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8484 arelent **storage,
8485 asymbol **syms)
252b5132 8486{
217aa764 8487 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
252b5132
RH
8488 asection *s;
8489 long ret;
8490
8491 if (elf_dynsymtab (abfd) == 0)
8492 {
8493 bfd_set_error (bfd_error_invalid_operation);
8494 return -1;
8495 }
8496
8497 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8498 ret = 0;
8499 for (s = abfd->sections; s != NULL; s = s->next)
8500 {
266b05cf 8501 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
8502 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8503 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8504 {
8505 arelent *p;
8506 long count, i;
8507
b34976b6 8508 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
252b5132 8509 return -1;
eea6121a 8510 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
252b5132
RH
8511 p = s->relocation;
8512 for (i = 0; i < count; i++)
8513 *storage++ = p++;
8514 ret += count;
8515 }
8516 }
8517
8518 *storage = NULL;
8519
8520 return ret;
8521}
8522\f
8523/* Read in the version information. */
8524
b34976b6 8525bfd_boolean
fc0e6df6 8526_bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
252b5132
RH
8527{
8528 bfd_byte *contents = NULL;
fc0e6df6
PB
8529 unsigned int freeidx = 0;
8530
8531 if (elf_dynverref (abfd) != 0)
8532 {
8533 Elf_Internal_Shdr *hdr;
8534 Elf_External_Verneed *everneed;
8535 Elf_Internal_Verneed *iverneed;
8536 unsigned int i;
d0fb9a8d 8537 bfd_byte *contents_end;
fc0e6df6
PB
8538
8539 hdr = &elf_tdata (abfd)->dynverref_hdr;
8540
bd61e135
AM
8541 if (hdr->sh_info == 0
8542 || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
d0fb9a8d 8543 {
601a03ba 8544error_return_bad_verref:
4eca0228 8545 _bfd_error_handler
871b3ab2 8546 (_("%pB: .gnu.version_r invalid entry"), abfd);
601a03ba 8547 bfd_set_error (bfd_error_bad_value);
d0fb9a8d
JJ
8548error_return_verref:
8549 elf_tdata (abfd)->verref = NULL;
8550 elf_tdata (abfd)->cverrefs = 0;
8551 goto error_return;
8552 }
601a03ba 8553
7e56c51c
NC
8554 ufile_ptr filesize = bfd_get_file_size (abfd);
8555 if (filesize > 0 && filesize < hdr->sh_size)
8556 {
8557 /* PR 24708: Avoid attempts to allocate a ridiculous amount
8558 of memory. */
8559 bfd_set_error (bfd_error_no_memory);
8560 _bfd_error_handler
8561 /* xgettext:c-format */
8562 (_("error: %pB version reference section is too large (%#" PRIx64 " bytes)"),
8563 abfd, (uint64_t) hdr->sh_size);
8564 goto error_return_verref;
8565 }
601a03ba
AM
8566 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
8567 if (contents == NULL)
8568 goto error_return_verref;
8569
fc0e6df6
PB
8570 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
8571 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
d0fb9a8d 8572 goto error_return_verref;
fc0e6df6 8573
601a03ba 8574 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
bd61e135 8575 bfd_alloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
601a03ba
AM
8576
8577 if (elf_tdata (abfd)->verref == NULL)
d0fb9a8d
JJ
8578 goto error_return_verref;
8579
8580 BFD_ASSERT (sizeof (Elf_External_Verneed)
8581 == sizeof (Elf_External_Vernaux));
8582 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
fc0e6df6
PB
8583 everneed = (Elf_External_Verneed *) contents;
8584 iverneed = elf_tdata (abfd)->verref;
8585 for (i = 0; i < hdr->sh_info; i++, iverneed++)
8586 {
8587 Elf_External_Vernaux *evernaux;
8588 Elf_Internal_Vernaux *ivernaux;
8589 unsigned int j;
8590
8591 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8592
8593 iverneed->vn_bfd = abfd;
8594
8595 iverneed->vn_filename =
8596 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8597 iverneed->vn_file);
8598 if (iverneed->vn_filename == NULL)
601a03ba 8599 goto error_return_bad_verref;
fc0e6df6 8600
d0fb9a8d
JJ
8601 if (iverneed->vn_cnt == 0)
8602 iverneed->vn_auxptr = NULL;
8603 else
8604 {
a50b1753 8605 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
07d6d2b8
AM
8606 bfd_alloc2 (abfd, iverneed->vn_cnt,
8607 sizeof (Elf_Internal_Vernaux));
d0fb9a8d
JJ
8608 if (iverneed->vn_auxptr == NULL)
8609 goto error_return_verref;
8610 }
8611
8612 if (iverneed->vn_aux
8613 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8614 goto error_return_bad_verref;
fc0e6df6
PB
8615
8616 evernaux = ((Elf_External_Vernaux *)
8617 ((bfd_byte *) everneed + iverneed->vn_aux));
8618 ivernaux = iverneed->vn_auxptr;
8619 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8620 {
8621 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8622
8623 ivernaux->vna_nodename =
8624 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8625 ivernaux->vna_name);
8626 if (ivernaux->vna_nodename == NULL)
601a03ba 8627 goto error_return_bad_verref;
fc0e6df6 8628
25ff461f
AM
8629 if (ivernaux->vna_other > freeidx)
8630 freeidx = ivernaux->vna_other;
8631
8632 ivernaux->vna_nextptr = NULL;
8633 if (ivernaux->vna_next == 0)
8634 {
8635 iverneed->vn_cnt = j + 1;
8636 break;
8637 }
fc0e6df6
PB
8638 if (j + 1 < iverneed->vn_cnt)
8639 ivernaux->vna_nextptr = ivernaux + 1;
fc0e6df6 8640
d0fb9a8d
JJ
8641 if (ivernaux->vna_next
8642 > (size_t) (contents_end - (bfd_byte *) evernaux))
601a03ba 8643 goto error_return_bad_verref;
d0fb9a8d 8644
fc0e6df6
PB
8645 evernaux = ((Elf_External_Vernaux *)
8646 ((bfd_byte *) evernaux + ivernaux->vna_next));
fc0e6df6
PB
8647 }
8648
25ff461f
AM
8649 iverneed->vn_nextref = NULL;
8650 if (iverneed->vn_next == 0)
8651 break;
fc0e6df6
PB
8652 if (i + 1 < hdr->sh_info)
8653 iverneed->vn_nextref = iverneed + 1;
fc0e6df6 8654
d0fb9a8d
JJ
8655 if (iverneed->vn_next
8656 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8657 goto error_return_bad_verref;
d0fb9a8d 8658
fc0e6df6
PB
8659 everneed = ((Elf_External_Verneed *)
8660 ((bfd_byte *) everneed + iverneed->vn_next));
8661 }
25ff461f 8662 elf_tdata (abfd)->cverrefs = i;
fc0e6df6
PB
8663
8664 free (contents);
8665 contents = NULL;
8666 }
252b5132
RH
8667
8668 if (elf_dynverdef (abfd) != 0)
8669 {
8670 Elf_Internal_Shdr *hdr;
8671 Elf_External_Verdef *everdef;
8672 Elf_Internal_Verdef *iverdef;
f631889e
UD
8673 Elf_Internal_Verdef *iverdefarr;
8674 Elf_Internal_Verdef iverdefmem;
252b5132 8675 unsigned int i;
062e2358 8676 unsigned int maxidx;
d0fb9a8d 8677 bfd_byte *contents_end_def, *contents_end_aux;
252b5132
RH
8678
8679 hdr = &elf_tdata (abfd)->dynverdef_hdr;
8680
601a03ba
AM
8681 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
8682 {
8683 error_return_bad_verdef:
4eca0228 8684 _bfd_error_handler
871b3ab2 8685 (_("%pB: .gnu.version_d invalid entry"), abfd);
601a03ba
AM
8686 bfd_set_error (bfd_error_bad_value);
8687 error_return_verdef:
8688 elf_tdata (abfd)->verdef = NULL;
8689 elf_tdata (abfd)->cverdefs = 0;
8690 goto error_return;
8691 }
8692
a50b1753 8693 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
252b5132 8694 if (contents == NULL)
601a03ba 8695 goto error_return_verdef;
252b5132 8696 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
217aa764 8697 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
601a03ba 8698 goto error_return_verdef;
d0fb9a8d
JJ
8699
8700 BFD_ASSERT (sizeof (Elf_External_Verdef)
8701 >= sizeof (Elf_External_Verdaux));
8702 contents_end_def = contents + hdr->sh_size
8703 - sizeof (Elf_External_Verdef);
8704 contents_end_aux = contents + hdr->sh_size
8705 - sizeof (Elf_External_Verdaux);
8706
f631889e
UD
8707 /* We know the number of entries in the section but not the maximum
8708 index. Therefore we have to run through all entries and find
8709 the maximum. */
252b5132 8710 everdef = (Elf_External_Verdef *) contents;
f631889e
UD
8711 maxidx = 0;
8712 for (i = 0; i < hdr->sh_info; ++i)
8713 {
8714 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8715
601a03ba
AM
8716 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
8717 goto error_return_bad_verdef;
062e2358
AM
8718 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
8719 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
f631889e 8720
25ff461f
AM
8721 if (iverdefmem.vd_next == 0)
8722 break;
8723
d0fb9a8d
JJ
8724 if (iverdefmem.vd_next
8725 > (size_t) (contents_end_def - (bfd_byte *) everdef))
601a03ba 8726 goto error_return_bad_verdef;
d0fb9a8d 8727
f631889e
UD
8728 everdef = ((Elf_External_Verdef *)
8729 ((bfd_byte *) everdef + iverdefmem.vd_next));
8730 }
8731
fc0e6df6
PB
8732 if (default_imported_symver)
8733 {
8734 if (freeidx > maxidx)
8735 maxidx = ++freeidx;
8736 else
8737 freeidx = ++maxidx;
8738 }
201159ec 8739
601a03ba
AM
8740 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
8741 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
f631889e 8742 if (elf_tdata (abfd)->verdef == NULL)
601a03ba 8743 goto error_return_verdef;
f631889e
UD
8744
8745 elf_tdata (abfd)->cverdefs = maxidx;
8746
8747 everdef = (Elf_External_Verdef *) contents;
8748 iverdefarr = elf_tdata (abfd)->verdef;
8749 for (i = 0; i < hdr->sh_info; i++)
252b5132
RH
8750 {
8751 Elf_External_Verdaux *everdaux;
8752 Elf_Internal_Verdaux *iverdaux;
8753 unsigned int j;
8754
f631889e
UD
8755 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8756
d0fb9a8d 8757 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
601a03ba 8758 goto error_return_bad_verdef;
d0fb9a8d 8759
f631889e 8760 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
595bce75 8761 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
252b5132
RH
8762
8763 iverdef->vd_bfd = abfd;
8764
d0fb9a8d
JJ
8765 if (iverdef->vd_cnt == 0)
8766 iverdef->vd_auxptr = NULL;
8767 else
8768 {
a50b1753 8769 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
07d6d2b8
AM
8770 bfd_alloc2 (abfd, iverdef->vd_cnt,
8771 sizeof (Elf_Internal_Verdaux));
d0fb9a8d
JJ
8772 if (iverdef->vd_auxptr == NULL)
8773 goto error_return_verdef;
8774 }
8775
8776 if (iverdef->vd_aux
8777 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
601a03ba 8778 goto error_return_bad_verdef;
252b5132
RH
8779
8780 everdaux = ((Elf_External_Verdaux *)
8781 ((bfd_byte *) everdef + iverdef->vd_aux));
8782 iverdaux = iverdef->vd_auxptr;
8783 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
8784 {
8785 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
8786
8787 iverdaux->vda_nodename =
8788 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8789 iverdaux->vda_name);
8790 if (iverdaux->vda_nodename == NULL)
601a03ba 8791 goto error_return_bad_verdef;
252b5132 8792
25ff461f
AM
8793 iverdaux->vda_nextptr = NULL;
8794 if (iverdaux->vda_next == 0)
8795 {
8796 iverdef->vd_cnt = j + 1;
8797 break;
8798 }
252b5132
RH
8799 if (j + 1 < iverdef->vd_cnt)
8800 iverdaux->vda_nextptr = iverdaux + 1;
252b5132 8801
d0fb9a8d
JJ
8802 if (iverdaux->vda_next
8803 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
601a03ba 8804 goto error_return_bad_verdef;
d0fb9a8d 8805
252b5132
RH
8806 everdaux = ((Elf_External_Verdaux *)
8807 ((bfd_byte *) everdaux + iverdaux->vda_next));
8808 }
8809
595bce75 8810 iverdef->vd_nodename = NULL;
d0fb9a8d
JJ
8811 if (iverdef->vd_cnt)
8812 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
252b5132 8813
25ff461f
AM
8814 iverdef->vd_nextdef = NULL;
8815 if (iverdef->vd_next == 0)
8816 break;
d0fb9a8d 8817 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
252b5132 8818 iverdef->vd_nextdef = iverdef + 1;
252b5132
RH
8819
8820 everdef = ((Elf_External_Verdef *)
8821 ((bfd_byte *) everdef + iverdef->vd_next));
8822 }
8823
8824 free (contents);
8825 contents = NULL;
8826 }
fc0e6df6 8827 else if (default_imported_symver)
252b5132 8828 {
fc0e6df6
PB
8829 if (freeidx < 3)
8830 freeidx = 3;
8831 else
8832 freeidx++;
252b5132 8833
a50b1753 8834 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
07d6d2b8 8835 bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
fc0e6df6 8836 if (elf_tdata (abfd)->verdef == NULL)
252b5132
RH
8837 goto error_return;
8838
fc0e6df6
PB
8839 elf_tdata (abfd)->cverdefs = freeidx;
8840 }
252b5132 8841
fc0e6df6
PB
8842 /* Create a default version based on the soname. */
8843 if (default_imported_symver)
8844 {
8845 Elf_Internal_Verdef *iverdef;
8846 Elf_Internal_Verdaux *iverdaux;
252b5132 8847
5bb3703f 8848 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
252b5132 8849
fc0e6df6
PB
8850 iverdef->vd_version = VER_DEF_CURRENT;
8851 iverdef->vd_flags = 0;
8852 iverdef->vd_ndx = freeidx;
8853 iverdef->vd_cnt = 1;
252b5132 8854
fc0e6df6 8855 iverdef->vd_bfd = abfd;
252b5132 8856
fc0e6df6
PB
8857 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
8858 if (iverdef->vd_nodename == NULL)
d0fb9a8d 8859 goto error_return_verdef;
fc0e6df6 8860 iverdef->vd_nextdef = NULL;
601a03ba
AM
8861 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
8862 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
d0fb9a8d
JJ
8863 if (iverdef->vd_auxptr == NULL)
8864 goto error_return_verdef;
252b5132 8865
fc0e6df6
PB
8866 iverdaux = iverdef->vd_auxptr;
8867 iverdaux->vda_nodename = iverdef->vd_nodename;
252b5132
RH
8868 }
8869
b34976b6 8870 return TRUE;
252b5132
RH
8871
8872 error_return:
5ed6aba4 8873 if (contents != NULL)
252b5132 8874 free (contents);
b34976b6 8875 return FALSE;
252b5132
RH
8876}
8877\f
8878asymbol *
217aa764 8879_bfd_elf_make_empty_symbol (bfd *abfd)
252b5132
RH
8880{
8881 elf_symbol_type *newsym;
8882
7a6e0d89 8883 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
252b5132
RH
8884 if (!newsym)
8885 return NULL;
201159ec
NC
8886 newsym->symbol.the_bfd = abfd;
8887 return &newsym->symbol;
252b5132
RH
8888}
8889
8890void
217aa764
AM
8891_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
8892 asymbol *symbol,
8893 symbol_info *ret)
252b5132
RH
8894{
8895 bfd_symbol_info (symbol, ret);
8896}
8897
8898/* Return whether a symbol name implies a local symbol. Most targets
8899 use this function for the is_local_label_name entry point, but some
8900 override it. */
8901
b34976b6 8902bfd_boolean
217aa764
AM
8903_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
8904 const char *name)
252b5132
RH
8905{
8906 /* Normal local symbols start with ``.L''. */
8907 if (name[0] == '.' && name[1] == 'L')
b34976b6 8908 return TRUE;
252b5132
RH
8909
8910 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8911 DWARF debugging symbols starting with ``..''. */
8912 if (name[0] == '.' && name[1] == '.')
b34976b6 8913 return TRUE;
252b5132
RH
8914
8915 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8916 emitting DWARF debugging output. I suspect this is actually a
8917 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8918 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8919 underscore to be emitted on some ELF targets). For ease of use,
8920 we treat such symbols as local. */
8921 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
b34976b6 8922 return TRUE;
252b5132 8923
b1fa9dd6
NC
8924 /* Treat assembler generated fake symbols, dollar local labels and
8925 forward-backward labels (aka local labels) as locals.
8926 These labels have the form:
8927
07d6d2b8 8928 L0^A.* (fake symbols)
b1fa9dd6
NC
8929
8930 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8931
8932 Versions which start with .L will have already been matched above,
8933 so we only need to match the rest. */
8934 if (name[0] == 'L' && ISDIGIT (name[1]))
8935 {
8936 bfd_boolean ret = FALSE;
8937 const char * p;
8938 char c;
8939
8940 for (p = name + 2; (c = *p); p++)
8941 {
8942 if (c == 1 || c == 2)
8943 {
8944 if (c == 1 && p == name + 2)
8945 /* A fake symbol. */
8946 return TRUE;
8947
8948 /* FIXME: We are being paranoid here and treating symbols like
8949 L0^Bfoo as if there were non-local, on the grounds that the
8950 assembler will never generate them. But can any symbol
8951 containing an ASCII value in the range 1-31 ever be anything
8952 other than some kind of local ? */
8953 ret = TRUE;
8954 }
8955
8956 if (! ISDIGIT (c))
8957 {
8958 ret = FALSE;
8959 break;
8960 }
8961 }
8962 return ret;
8963 }
ffa54770 8964
b34976b6 8965 return FALSE;
252b5132
RH
8966}
8967
8968alent *
217aa764
AM
8969_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
8970 asymbol *symbol ATTRIBUTE_UNUSED)
252b5132
RH
8971{
8972 abort ();
8973 return NULL;
8974}
8975
b34976b6 8976bfd_boolean
217aa764
AM
8977_bfd_elf_set_arch_mach (bfd *abfd,
8978 enum bfd_architecture arch,
8979 unsigned long machine)
252b5132
RH
8980{
8981 /* If this isn't the right architecture for this backend, and this
8982 isn't the generic backend, fail. */
8983 if (arch != get_elf_backend_data (abfd)->arch
8984 && arch != bfd_arch_unknown
8985 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
b34976b6 8986 return FALSE;
252b5132
RH
8987
8988 return bfd_default_set_arch_mach (abfd, arch, machine);
8989}
8990
d1fad7c6
NC
8991/* Find the nearest line to a particular section and offset,
8992 for error reporting. */
8993
b34976b6 8994bfd_boolean
217aa764 8995_bfd_elf_find_nearest_line (bfd *abfd,
217aa764 8996 asymbol **symbols,
fb167eb2 8997 asection *section,
217aa764
AM
8998 bfd_vma offset,
8999 const char **filename_ptr,
9000 const char **functionname_ptr,
fb167eb2
AM
9001 unsigned int *line_ptr,
9002 unsigned int *discriminator_ptr)
d1fad7c6 9003{
b34976b6 9004 bfd_boolean found;
d1fad7c6 9005
fb167eb2 9006 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
4e8a9624 9007 filename_ptr, functionname_ptr,
fb167eb2 9008 line_ptr, discriminator_ptr,
9defd221 9009 dwarf_debug_sections,
e00e8198
AM
9010 &elf_tdata (abfd)->dwarf2_find_line_info)
9011 || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9012 filename_ptr, functionname_ptr,
9013 line_ptr))
d1fad7c6
NC
9014 {
9015 if (!*functionname_ptr)
e00e8198
AM
9016 _bfd_elf_find_function (abfd, symbols, section, offset,
9017 *filename_ptr ? NULL : filename_ptr,
9018 functionname_ptr);
b34976b6 9019 return TRUE;
d1fad7c6
NC
9020 }
9021
9022 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4e8a9624
AM
9023 &found, filename_ptr,
9024 functionname_ptr, line_ptr,
9025 &elf_tdata (abfd)->line_info))
b34976b6 9026 return FALSE;
dc43ada5 9027 if (found && (*functionname_ptr || *line_ptr))
b34976b6 9028 return TRUE;
d1fad7c6
NC
9029
9030 if (symbols == NULL)
b34976b6 9031 return FALSE;
d1fad7c6 9032
e00e8198
AM
9033 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9034 filename_ptr, functionname_ptr))
b34976b6 9035 return FALSE;
d1fad7c6 9036
252b5132 9037 *line_ptr = 0;
b34976b6 9038 return TRUE;
252b5132
RH
9039}
9040
5420f73d
L
9041/* Find the line for a symbol. */
9042
9043bfd_boolean
9044_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9045 const char **filename_ptr, unsigned int *line_ptr)
9b8d1a36 9046{
fb167eb2
AM
9047 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9048 filename_ptr, NULL, line_ptr, NULL,
9defd221 9049 dwarf_debug_sections,
fb167eb2 9050 &elf_tdata (abfd)->dwarf2_find_line_info);
5420f73d
L
9051}
9052
4ab527b0
FF
9053/* After a call to bfd_find_nearest_line, successive calls to
9054 bfd_find_inliner_info can be used to get source information about
9055 each level of function inlining that terminated at the address
9056 passed to bfd_find_nearest_line. Currently this is only supported
9057 for DWARF2 with appropriate DWARF3 extensions. */
9058
9059bfd_boolean
9060_bfd_elf_find_inliner_info (bfd *abfd,
9061 const char **filename_ptr,
9062 const char **functionname_ptr,
9063 unsigned int *line_ptr)
9064{
9065 bfd_boolean found;
9066 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9067 functionname_ptr, line_ptr,
9068 & elf_tdata (abfd)->dwarf2_find_line_info);
9069 return found;
9070}
9071
252b5132 9072int
a6b96beb 9073_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
252b5132 9074{
8ded5a0f
AM
9075 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9076 int ret = bed->s->sizeof_ehdr;
252b5132 9077
0e1862bb 9078 if (!bfd_link_relocatable (info))
8ded5a0f 9079 {
12bd6957 9080 bfd_size_type phdr_size = elf_program_header_size (abfd);
8ded5a0f 9081
62d7a5f6
AM
9082 if (phdr_size == (bfd_size_type) -1)
9083 {
9084 struct elf_segment_map *m;
9085
9086 phdr_size = 0;
12bd6957 9087 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
62d7a5f6 9088 phdr_size += bed->s->sizeof_phdr;
8ded5a0f 9089
62d7a5f6
AM
9090 if (phdr_size == 0)
9091 phdr_size = get_program_header_size (abfd, info);
9092 }
8ded5a0f 9093
12bd6957 9094 elf_program_header_size (abfd) = phdr_size;
8ded5a0f
AM
9095 ret += phdr_size;
9096 }
9097
252b5132
RH
9098 return ret;
9099}
9100
b34976b6 9101bfd_boolean
217aa764
AM
9102_bfd_elf_set_section_contents (bfd *abfd,
9103 sec_ptr section,
0f867abe 9104 const void *location,
217aa764
AM
9105 file_ptr offset,
9106 bfd_size_type count)
252b5132
RH
9107{
9108 Elf_Internal_Shdr *hdr;
1b6aeedb 9109 file_ptr pos;
252b5132
RH
9110
9111 if (! abfd->output_has_begun
217aa764 9112 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
b34976b6 9113 return FALSE;
252b5132 9114
0ce398f1
L
9115 if (!count)
9116 return TRUE;
9117
252b5132 9118 hdr = &elf_section_data (section)->this_hdr;
0ce398f1
L
9119 if (hdr->sh_offset == (file_ptr) -1)
9120 {
1ff6de03
NA
9121 if (bfd_section_is_ctf (section))
9122 /* Nothing to do with this section: the contents are generated
9123 later. */
9124 return TRUE;
9125
0ce398f1
L
9126 /* We must compress this section. Write output to the buffer. */
9127 unsigned char *contents = hdr->contents;
9128 if ((offset + count) > hdr->sh_size
9129 || (section->flags & SEC_ELF_COMPRESS) == 0
9130 || contents == NULL)
9131 abort ();
9132 memcpy (contents + offset, location, count);
9133 return TRUE;
9134 }
dc810e39
AM
9135 pos = hdr->sh_offset + offset;
9136 if (bfd_seek (abfd, pos, SEEK_SET) != 0
9137 || bfd_bwrite (location, count, abfd) != count)
b34976b6 9138 return FALSE;
252b5132 9139
b34976b6 9140 return TRUE;
252b5132
RH
9141}
9142
f3185997 9143bfd_boolean
217aa764
AM
9144_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
9145 arelent *cache_ptr ATTRIBUTE_UNUSED,
9146 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
252b5132
RH
9147{
9148 abort ();
f3185997 9149 return FALSE;
252b5132
RH
9150}
9151
252b5132
RH
9152/* Try to convert a non-ELF reloc into an ELF one. */
9153
b34976b6 9154bfd_boolean
217aa764 9155_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
252b5132 9156{
c044fabd 9157 /* Check whether we really have an ELF howto. */
252b5132
RH
9158
9159 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
9160 {
9161 bfd_reloc_code_real_type code;
9162 reloc_howto_type *howto;
9163
9164 /* Alien reloc: Try to determine its type to replace it with an
c044fabd 9165 equivalent ELF reloc. */
252b5132
RH
9166
9167 if (areloc->howto->pc_relative)
9168 {
9169 switch (areloc->howto->bitsize)
9170 {
9171 case 8:
9172 code = BFD_RELOC_8_PCREL;
9173 break;
9174 case 12:
9175 code = BFD_RELOC_12_PCREL;
9176 break;
9177 case 16:
9178 code = BFD_RELOC_16_PCREL;
9179 break;
9180 case 24:
9181 code = BFD_RELOC_24_PCREL;
9182 break;
9183 case 32:
9184 code = BFD_RELOC_32_PCREL;
9185 break;
9186 case 64:
9187 code = BFD_RELOC_64_PCREL;
9188 break;
9189 default:
9190 goto fail;
9191 }
9192
9193 howto = bfd_reloc_type_lookup (abfd, code);
9194
9195 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
9196 {
9197 if (howto->pcrel_offset)
9198 areloc->addend += areloc->address;
9199 else
9200 areloc->addend -= areloc->address; /* addend is unsigned!! */
9201 }
9202 }
9203 else
9204 {
9205 switch (areloc->howto->bitsize)
9206 {
9207 case 8:
9208 code = BFD_RELOC_8;
9209 break;
9210 case 14:
9211 code = BFD_RELOC_14;
9212 break;
9213 case 16:
9214 code = BFD_RELOC_16;
9215 break;
9216 case 26:
9217 code = BFD_RELOC_26;
9218 break;
9219 case 32:
9220 code = BFD_RELOC_32;
9221 break;
9222 case 64:
9223 code = BFD_RELOC_64;
9224 break;
9225 default:
9226 goto fail;
9227 }
9228
9229 howto = bfd_reloc_type_lookup (abfd, code);
9230 }
9231
9232 if (howto)
9233 areloc->howto = howto;
9234 else
9235 goto fail;
9236 }
9237
b34976b6 9238 return TRUE;
252b5132
RH
9239
9240 fail:
0aa13fee
AM
9241 /* xgettext:c-format */
9242 _bfd_error_handler (_("%pB: %s unsupported"),
9243 abfd, areloc->howto->name);
252b5132 9244 bfd_set_error (bfd_error_bad_value);
b34976b6 9245 return FALSE;
252b5132
RH
9246}
9247
b34976b6 9248bfd_boolean
217aa764 9249_bfd_elf_close_and_cleanup (bfd *abfd)
252b5132 9250{
d9071b0c
TG
9251 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9252 if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
252b5132 9253 {
c0355132 9254 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
2b0f7ef9 9255 _bfd_elf_strtab_free (elf_shstrtab (abfd));
d9071b0c 9256 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
252b5132
RH
9257 }
9258
9259 return _bfd_generic_close_and_cleanup (abfd);
9260}
9261
9262/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9263 in the relocation's offset. Thus we cannot allow any sort of sanity
9264 range-checking to interfere. There is nothing else to do in processing
9265 this reloc. */
9266
9267bfd_reloc_status_type
217aa764
AM
9268_bfd_elf_rel_vtable_reloc_fn
9269 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
fc0a2244 9270 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
217aa764
AM
9271 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9272 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
252b5132
RH
9273{
9274 return bfd_reloc_ok;
9275}
252b5132
RH
9276\f
9277/* Elf core file support. Much of this only works on native
9278 toolchains, since we rely on knowing the
9279 machine-dependent procfs structure in order to pick
c044fabd 9280 out details about the corefile. */
252b5132
RH
9281
9282#ifdef HAVE_SYS_PROCFS_H
16231b7b
DG
9283/* Needed for new procfs interface on sparc-solaris. */
9284# define _STRUCTURED_PROC 1
252b5132
RH
9285# include <sys/procfs.h>
9286#endif
9287
261b8d08
PA
9288/* Return a PID that identifies a "thread" for threaded cores, or the
9289 PID of the main process for non-threaded cores. */
252b5132
RH
9290
9291static int
217aa764 9292elfcore_make_pid (bfd *abfd)
252b5132 9293{
261b8d08
PA
9294 int pid;
9295
228e534f 9296 pid = elf_tdata (abfd)->core->lwpid;
261b8d08 9297 if (pid == 0)
228e534f 9298 pid = elf_tdata (abfd)->core->pid;
261b8d08
PA
9299
9300 return pid;
252b5132
RH
9301}
9302
252b5132
RH
9303/* If there isn't a section called NAME, make one, using
9304 data from SECT. Note, this function will generate a
9305 reference to NAME, so you shouldn't deallocate or
c044fabd 9306 overwrite it. */
252b5132 9307
b34976b6 9308static bfd_boolean
217aa764 9309elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
252b5132 9310{
c044fabd 9311 asection *sect2;
252b5132
RH
9312
9313 if (bfd_get_section_by_name (abfd, name) != NULL)
b34976b6 9314 return TRUE;
252b5132 9315
117ed4f8 9316 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
252b5132 9317 if (sect2 == NULL)
b34976b6 9318 return FALSE;
252b5132 9319
eea6121a 9320 sect2->size = sect->size;
252b5132 9321 sect2->filepos = sect->filepos;
252b5132 9322 sect2->alignment_power = sect->alignment_power;
b34976b6 9323 return TRUE;
252b5132
RH
9324}
9325
bb0082d6
AM
9326/* Create a pseudosection containing SIZE bytes at FILEPOS. This
9327 actually creates up to two pseudosections:
9328 - For the single-threaded case, a section named NAME, unless
9329 such a section already exists.
9330 - For the multi-threaded case, a section named "NAME/PID", where
9331 PID is elfcore_make_pid (abfd).
24d3e51b 9332 Both pseudosections have identical contents. */
b34976b6 9333bfd_boolean
217aa764
AM
9334_bfd_elfcore_make_pseudosection (bfd *abfd,
9335 char *name,
9336 size_t size,
9337 ufile_ptr filepos)
bb0082d6
AM
9338{
9339 char buf[100];
9340 char *threaded_name;
d4c88bbb 9341 size_t len;
bb0082d6
AM
9342 asection *sect;
9343
9344 /* Build the section name. */
9345
9346 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
d4c88bbb 9347 len = strlen (buf) + 1;
a50b1753 9348 threaded_name = (char *) bfd_alloc (abfd, len);
bb0082d6 9349 if (threaded_name == NULL)
b34976b6 9350 return FALSE;
d4c88bbb 9351 memcpy (threaded_name, buf, len);
bb0082d6 9352
117ed4f8
AM
9353 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9354 SEC_HAS_CONTENTS);
bb0082d6 9355 if (sect == NULL)
b34976b6 9356 return FALSE;
eea6121a 9357 sect->size = size;
bb0082d6 9358 sect->filepos = filepos;
bb0082d6
AM
9359 sect->alignment_power = 2;
9360
936e320b 9361 return elfcore_maybe_make_sect (abfd, name, sect);
bb0082d6
AM
9362}
9363
58e07198
CZ
9364static bfd_boolean
9365elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
9366 size_t offs)
9367{
9368 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9369 SEC_HAS_CONTENTS);
9370
9371 if (sect == NULL)
9372 return FALSE;
9373
9374 sect->size = note->descsz - offs;
9375 sect->filepos = note->descpos + offs;
9376 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9377
9378 return TRUE;
9379}
9380
252b5132 9381/* prstatus_t exists on:
4a938328 9382 solaris 2.5+
252b5132
RH
9383 linux 2.[01] + glibc
9384 unixware 4.2
9385*/
9386
9387#if defined (HAVE_PRSTATUS_T)
a7b97311 9388
b34976b6 9389static bfd_boolean
217aa764 9390elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 9391{
eea6121a 9392 size_t size;
7ee38065 9393 int offset;
252b5132 9394
4a938328
MS
9395 if (note->descsz == sizeof (prstatus_t))
9396 {
9397 prstatus_t prstat;
252b5132 9398
eea6121a 9399 size = sizeof (prstat.pr_reg);
7ee38065 9400 offset = offsetof (prstatus_t, pr_reg);
4a938328 9401 memcpy (&prstat, note->descdata, sizeof (prstat));
252b5132 9402
fa49d224
NC
9403 /* Do not overwrite the core signal if it
9404 has already been set by another thread. */
228e534f
AM
9405 if (elf_tdata (abfd)->core->signal == 0)
9406 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9407 if (elf_tdata (abfd)->core->pid == 0)
9408 elf_tdata (abfd)->core->pid = prstat.pr_pid;
252b5132 9409
4a938328
MS
9410 /* pr_who exists on:
9411 solaris 2.5+
9412 unixware 4.2
9413 pr_who doesn't exist on:
9414 linux 2.[01]
9415 */
252b5132 9416#if defined (HAVE_PRSTATUS_T_PR_WHO)
228e534f 9417 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9418#else
228e534f 9419 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
252b5132 9420#endif
4a938328 9421 }
7ee38065 9422#if defined (HAVE_PRSTATUS32_T)
4a938328
MS
9423 else if (note->descsz == sizeof (prstatus32_t))
9424 {
9425 /* 64-bit host, 32-bit corefile */
9426 prstatus32_t prstat;
9427
eea6121a 9428 size = sizeof (prstat.pr_reg);
7ee38065 9429 offset = offsetof (prstatus32_t, pr_reg);
4a938328
MS
9430 memcpy (&prstat, note->descdata, sizeof (prstat));
9431
fa49d224
NC
9432 /* Do not overwrite the core signal if it
9433 has already been set by another thread. */
228e534f
AM
9434 if (elf_tdata (abfd)->core->signal == 0)
9435 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9436 if (elf_tdata (abfd)->core->pid == 0)
9437 elf_tdata (abfd)->core->pid = prstat.pr_pid;
4a938328
MS
9438
9439 /* pr_who exists on:
9440 solaris 2.5+
9441 unixware 4.2
9442 pr_who doesn't exist on:
9443 linux 2.[01]
9444 */
7ee38065 9445#if defined (HAVE_PRSTATUS32_T_PR_WHO)
228e534f 9446 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9447#else
228e534f 9448 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
4a938328
MS
9449#endif
9450 }
7ee38065 9451#endif /* HAVE_PRSTATUS32_T */
4a938328
MS
9452 else
9453 {
9454 /* Fail - we don't know how to handle any other
9455 note size (ie. data object type). */
b34976b6 9456 return TRUE;
4a938328 9457 }
252b5132 9458
bb0082d6 9459 /* Make a ".reg/999" section and a ".reg" section. */
936e320b 9460 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 9461 size, note->descpos + offset);
252b5132
RH
9462}
9463#endif /* defined (HAVE_PRSTATUS_T) */
9464
bb0082d6 9465/* Create a pseudosection containing the exact contents of NOTE. */
b34976b6 9466static bfd_boolean
217aa764
AM
9467elfcore_make_note_pseudosection (bfd *abfd,
9468 char *name,
9469 Elf_Internal_Note *note)
252b5132 9470{
936e320b
AM
9471 return _bfd_elfcore_make_pseudosection (abfd, name,
9472 note->descsz, note->descpos);
252b5132
RH
9473}
9474
ff08c6bb
JB
9475/* There isn't a consistent prfpregset_t across platforms,
9476 but it doesn't matter, because we don't have to pick this
c044fabd
KH
9477 data structure apart. */
9478
b34976b6 9479static bfd_boolean
217aa764 9480elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9481{
9482 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9483}
9484
ff08c6bb 9485/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
971d4640 9486 type of NT_PRXFPREG. Just include the whole note's contents
ff08c6bb 9487 literally. */
c044fabd 9488
b34976b6 9489static bfd_boolean
217aa764 9490elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9491{
9492 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9493}
9494
4339cae0
L
9495/* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9496 with a note type of NT_X86_XSTATE. Just include the whole note's
9497 contents literally. */
9498
9499static bfd_boolean
9500elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9501{
9502 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9503}
9504
97753bd5
AM
9505static bfd_boolean
9506elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9507{
9508 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9509}
9510
89eeb0bc
LM
9511static bfd_boolean
9512elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9513{
9514 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9515}
97753bd5 9516
cb2366c1
EBM
9517static bfd_boolean
9518elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9519{
9520 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9521}
9522
9523static bfd_boolean
9524elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9525{
9526 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9527}
9528
9529static bfd_boolean
9530elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9531{
9532 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9533}
9534
9535static bfd_boolean
9536elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9537{
9538 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9539}
9540
9541static bfd_boolean
9542elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9543{
9544 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9545}
9546
9547static bfd_boolean
9548elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9549{
9550 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9551}
9552
9553static bfd_boolean
9554elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
9555{
9556 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
9557}
9558
9559static bfd_boolean
9560elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
9561{
9562 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
9563}
9564
9565static bfd_boolean
9566elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
9567{
9568 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
9569}
9570
9571static bfd_boolean
9572elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
9573{
9574 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
9575}
9576
9577static bfd_boolean
9578elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
9579{
9580 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
9581}
9582
9583static bfd_boolean
9584elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
9585{
9586 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
9587}
9588
9589static bfd_boolean
9590elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
9591{
9592 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
9593}
9594
0675e188
UW
9595static bfd_boolean
9596elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9597{
9598 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9599}
9600
d7eeb400
MS
9601static bfd_boolean
9602elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9603{
9604 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9605}
9606
9607static bfd_boolean
9608elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9609{
9610 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9611}
9612
9613static bfd_boolean
9614elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
9615{
9616 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
9617}
9618
9619static bfd_boolean
9620elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
9621{
9622 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
9623}
9624
9625static bfd_boolean
9626elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
9627{
9628 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
9629}
9630
355b81d9
UW
9631static bfd_boolean
9632elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
9633{
9634 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
9635}
9636
9637static bfd_boolean
9638elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
9639{
9640 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
9641}
9642
abb3f6cc
NC
9643static bfd_boolean
9644elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
9645{
9646 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
9647}
9648
4ef9f41a
AA
9649static bfd_boolean
9650elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
9651{
9652 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
9653}
9654
9655static bfd_boolean
9656elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
9657{
9658 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
9659}
9660
88ab90e8
AA
9661static bfd_boolean
9662elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
9663{
9664 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
9665}
9666
9667static bfd_boolean
9668elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
9669{
9670 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
9671}
9672
faa9a424
UW
9673static bfd_boolean
9674elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
9675{
9676 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
9677}
9678
652451f8
YZ
9679static bfd_boolean
9680elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
9681{
9682 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
9683}
9684
9685static bfd_boolean
9686elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
9687{
9688 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
9689}
9690
9691static bfd_boolean
9692elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
9693{
9694 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
9695}
9696
ad1cc4e4
AH
9697static bfd_boolean
9698elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
9699{
9700 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
9701}
9702
e6c3b5bf
AH
9703static bfd_boolean
9704elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
9705{
9706 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
9707}
9708
252b5132 9709#if defined (HAVE_PRPSINFO_T)
4a938328 9710typedef prpsinfo_t elfcore_psinfo_t;
7ee38065 9711#if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
9712typedef prpsinfo32_t elfcore_psinfo32_t;
9713#endif
252b5132
RH
9714#endif
9715
9716#if defined (HAVE_PSINFO_T)
4a938328 9717typedef psinfo_t elfcore_psinfo_t;
7ee38065 9718#if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
9719typedef psinfo32_t elfcore_psinfo32_t;
9720#endif
252b5132
RH
9721#endif
9722
252b5132
RH
9723/* return a malloc'ed copy of a string at START which is at
9724 most MAX bytes long, possibly without a terminating '\0'.
c044fabd 9725 the copy will always have a terminating '\0'. */
252b5132 9726
936e320b 9727char *
217aa764 9728_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
252b5132 9729{
dc810e39 9730 char *dups;
a50b1753 9731 char *end = (char *) memchr (start, '\0', max);
dc810e39 9732 size_t len;
252b5132
RH
9733
9734 if (end == NULL)
9735 len = max;
9736 else
9737 len = end - start;
9738
a50b1753 9739 dups = (char *) bfd_alloc (abfd, len + 1);
dc810e39 9740 if (dups == NULL)
252b5132
RH
9741 return NULL;
9742
dc810e39
AM
9743 memcpy (dups, start, len);
9744 dups[len] = '\0';
252b5132 9745
dc810e39 9746 return dups;
252b5132
RH
9747}
9748
bb0082d6 9749#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
b34976b6 9750static bfd_boolean
217aa764 9751elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
252b5132 9752{
4a938328
MS
9753 if (note->descsz == sizeof (elfcore_psinfo_t))
9754 {
9755 elfcore_psinfo_t psinfo;
252b5132 9756
7ee38065 9757 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 9758
335e41d4 9759#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
228e534f 9760 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 9761#endif
228e534f 9762 elf_tdata (abfd)->core->program
936e320b
AM
9763 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9764 sizeof (psinfo.pr_fname));
252b5132 9765
228e534f 9766 elf_tdata (abfd)->core->command
936e320b
AM
9767 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9768 sizeof (psinfo.pr_psargs));
4a938328 9769 }
7ee38065 9770#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
4a938328
MS
9771 else if (note->descsz == sizeof (elfcore_psinfo32_t))
9772 {
9773 /* 64-bit host, 32-bit corefile */
9774 elfcore_psinfo32_t psinfo;
9775
7ee38065 9776 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 9777
335e41d4 9778#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
228e534f 9779 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 9780#endif
228e534f 9781 elf_tdata (abfd)->core->program
936e320b
AM
9782 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9783 sizeof (psinfo.pr_fname));
4a938328 9784
228e534f 9785 elf_tdata (abfd)->core->command
936e320b
AM
9786 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9787 sizeof (psinfo.pr_psargs));
4a938328
MS
9788 }
9789#endif
9790
9791 else
9792 {
9793 /* Fail - we don't know how to handle any other
9794 note size (ie. data object type). */
b34976b6 9795 return TRUE;
4a938328 9796 }
252b5132
RH
9797
9798 /* Note that for some reason, a spurious space is tacked
9799 onto the end of the args in some (at least one anyway)
c044fabd 9800 implementations, so strip it off if it exists. */
252b5132
RH
9801
9802 {
228e534f 9803 char *command = elf_tdata (abfd)->core->command;
252b5132
RH
9804 int n = strlen (command);
9805
9806 if (0 < n && command[n - 1] == ' ')
9807 command[n - 1] = '\0';
9808 }
9809
b34976b6 9810 return TRUE;
252b5132
RH
9811}
9812#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9813
252b5132 9814#if defined (HAVE_PSTATUS_T)
b34976b6 9815static bfd_boolean
217aa764 9816elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 9817{
f572a39d
AM
9818 if (note->descsz == sizeof (pstatus_t)
9819#if defined (HAVE_PXSTATUS_T)
9820 || note->descsz == sizeof (pxstatus_t)
9821#endif
9822 )
4a938328
MS
9823 {
9824 pstatus_t pstat;
252b5132 9825
4a938328 9826 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 9827
228e534f 9828 elf_tdata (abfd)->core->pid = pstat.pr_pid;
4a938328 9829 }
7ee38065 9830#if defined (HAVE_PSTATUS32_T)
4a938328
MS
9831 else if (note->descsz == sizeof (pstatus32_t))
9832 {
9833 /* 64-bit host, 32-bit corefile */
9834 pstatus32_t pstat;
252b5132 9835
4a938328 9836 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 9837
228e534f 9838 elf_tdata (abfd)->core->pid = pstat.pr_pid;
4a938328
MS
9839 }
9840#endif
252b5132
RH
9841 /* Could grab some more details from the "representative"
9842 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
c044fabd 9843 NT_LWPSTATUS note, presumably. */
252b5132 9844
b34976b6 9845 return TRUE;
252b5132
RH
9846}
9847#endif /* defined (HAVE_PSTATUS_T) */
9848
252b5132 9849#if defined (HAVE_LWPSTATUS_T)
b34976b6 9850static bfd_boolean
217aa764 9851elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132
RH
9852{
9853 lwpstatus_t lwpstat;
9854 char buf[100];
c044fabd 9855 char *name;
d4c88bbb 9856 size_t len;
c044fabd 9857 asection *sect;
252b5132 9858
f572a39d
AM
9859 if (note->descsz != sizeof (lwpstat)
9860#if defined (HAVE_LWPXSTATUS_T)
9861 && note->descsz != sizeof (lwpxstatus_t)
9862#endif
9863 )
b34976b6 9864 return TRUE;
252b5132
RH
9865
9866 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
9867
228e534f 9868 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
a1504221
JB
9869 /* Do not overwrite the core signal if it has already been set by
9870 another thread. */
228e534f
AM
9871 if (elf_tdata (abfd)->core->signal == 0)
9872 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
252b5132 9873
c044fabd 9874 /* Make a ".reg/999" section. */
252b5132
RH
9875
9876 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
d4c88bbb 9877 len = strlen (buf) + 1;
217aa764 9878 name = bfd_alloc (abfd, len);
252b5132 9879 if (name == NULL)
b34976b6 9880 return FALSE;
d4c88bbb 9881 memcpy (name, buf, len);
252b5132 9882
117ed4f8 9883 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 9884 if (sect == NULL)
b34976b6 9885 return FALSE;
252b5132
RH
9886
9887#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 9888 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
252b5132
RH
9889 sect->filepos = note->descpos
9890 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
9891#endif
9892
9893#if defined (HAVE_LWPSTATUS_T_PR_REG)
eea6121a 9894 sect->size = sizeof (lwpstat.pr_reg);
252b5132
RH
9895 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
9896#endif
9897
252b5132
RH
9898 sect->alignment_power = 2;
9899
9900 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
b34976b6 9901 return FALSE;
252b5132
RH
9902
9903 /* Make a ".reg2/999" section */
9904
9905 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
d4c88bbb 9906 len = strlen (buf) + 1;
217aa764 9907 name = bfd_alloc (abfd, len);
252b5132 9908 if (name == NULL)
b34976b6 9909 return FALSE;
d4c88bbb 9910 memcpy (name, buf, len);
252b5132 9911
117ed4f8 9912 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 9913 if (sect == NULL)
b34976b6 9914 return FALSE;
252b5132
RH
9915
9916#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 9917 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
252b5132
RH
9918 sect->filepos = note->descpos
9919 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
9920#endif
9921
9922#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
eea6121a 9923 sect->size = sizeof (lwpstat.pr_fpreg);
252b5132
RH
9924 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
9925#endif
9926
252b5132
RH
9927 sect->alignment_power = 2;
9928
936e320b 9929 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
252b5132
RH
9930}
9931#endif /* defined (HAVE_LWPSTATUS_T) */
9932
b34976b6 9933static bfd_boolean
217aa764 9934elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
16e9c715
NC
9935{
9936 char buf[30];
c044fabd 9937 char *name;
d4c88bbb 9938 size_t len;
c044fabd 9939 asection *sect;
4a6636fb
PA
9940 int type;
9941 int is_active_thread;
9942 bfd_vma base_addr;
16e9c715 9943
4a6636fb 9944 if (note->descsz < 728)
b34976b6 9945 return TRUE;
16e9c715 9946
4a6636fb
PA
9947 if (! CONST_STRNEQ (note->namedata, "win32"))
9948 return TRUE;
9949
9950 type = bfd_get_32 (abfd, note->descdata);
c044fabd 9951
4a6636fb 9952 switch (type)
16e9c715 9953 {
4a6636fb 9954 case 1 /* NOTE_INFO_PROCESS */:
228e534f 9955 /* FIXME: need to add ->core->command. */
4a6636fb 9956 /* process_info.pid */
228e534f 9957 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
4a6636fb 9958 /* process_info.signal */
228e534f 9959 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
c044fabd 9960 break;
16e9c715 9961
4a6636fb 9962 case 2 /* NOTE_INFO_THREAD */:
16e9c715 9963 /* Make a ".reg/999" section. */
4a6636fb
PA
9964 /* thread_info.tid */
9965 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
c044fabd 9966
d4c88bbb 9967 len = strlen (buf) + 1;
a50b1753 9968 name = (char *) bfd_alloc (abfd, len);
16e9c715 9969 if (name == NULL)
b34976b6 9970 return FALSE;
c044fabd 9971
d4c88bbb 9972 memcpy (name, buf, len);
16e9c715 9973
117ed4f8 9974 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
16e9c715 9975 if (sect == NULL)
b34976b6 9976 return FALSE;
c044fabd 9977
4a6636fb
PA
9978 /* sizeof (thread_info.thread_context) */
9979 sect->size = 716;
9980 /* offsetof (thread_info.thread_context) */
9981 sect->filepos = note->descpos + 12;
16e9c715
NC
9982 sect->alignment_power = 2;
9983
4a6636fb
PA
9984 /* thread_info.is_active_thread */
9985 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
9986
9987 if (is_active_thread)
16e9c715 9988 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
b34976b6 9989 return FALSE;
16e9c715
NC
9990 break;
9991
4a6636fb 9992 case 3 /* NOTE_INFO_MODULE */:
16e9c715 9993 /* Make a ".module/xxxxxxxx" section. */
4a6636fb
PA
9994 /* module_info.base_address */
9995 base_addr = bfd_get_32 (abfd, note->descdata + 4);
0af1713e 9996 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
c044fabd 9997
d4c88bbb 9998 len = strlen (buf) + 1;
a50b1753 9999 name = (char *) bfd_alloc (abfd, len);
16e9c715 10000 if (name == NULL)
b34976b6 10001 return FALSE;
c044fabd 10002
d4c88bbb 10003 memcpy (name, buf, len);
252b5132 10004
117ed4f8 10005 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
c044fabd 10006
16e9c715 10007 if (sect == NULL)
b34976b6 10008 return FALSE;
c044fabd 10009
eea6121a 10010 sect->size = note->descsz;
16e9c715 10011 sect->filepos = note->descpos;
16e9c715
NC
10012 sect->alignment_power = 2;
10013 break;
10014
10015 default:
b34976b6 10016 return TRUE;
16e9c715
NC
10017 }
10018
b34976b6 10019 return TRUE;
16e9c715 10020}
252b5132 10021
b34976b6 10022static bfd_boolean
217aa764 10023elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
252b5132 10024{
9c5bfbb7 10025 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
bb0082d6 10026
252b5132
RH
10027 switch (note->type)
10028 {
10029 default:
b34976b6 10030 return TRUE;
252b5132 10031
252b5132 10032 case NT_PRSTATUS:
bb0082d6
AM
10033 if (bed->elf_backend_grok_prstatus)
10034 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
b34976b6 10035 return TRUE;
bb0082d6 10036#if defined (HAVE_PRSTATUS_T)
252b5132 10037 return elfcore_grok_prstatus (abfd, note);
bb0082d6 10038#else
b34976b6 10039 return TRUE;
252b5132
RH
10040#endif
10041
10042#if defined (HAVE_PSTATUS_T)
10043 case NT_PSTATUS:
10044 return elfcore_grok_pstatus (abfd, note);
10045#endif
10046
10047#if defined (HAVE_LWPSTATUS_T)
10048 case NT_LWPSTATUS:
10049 return elfcore_grok_lwpstatus (abfd, note);
10050#endif
10051
10052 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
10053 return elfcore_grok_prfpreg (abfd, note);
10054
c044fabd 10055 case NT_WIN32PSTATUS:
16e9c715 10056 return elfcore_grok_win32pstatus (abfd, note);
16e9c715 10057
c044fabd 10058 case NT_PRXFPREG: /* Linux SSE extension */
e377ab71
MK
10059 if (note->namesz == 6
10060 && strcmp (note->namedata, "LINUX") == 0)
ff08c6bb
JB
10061 return elfcore_grok_prxfpreg (abfd, note);
10062 else
b34976b6 10063 return TRUE;
ff08c6bb 10064
4339cae0
L
10065 case NT_X86_XSTATE: /* Linux XSAVE extension */
10066 if (note->namesz == 6
10067 && strcmp (note->namedata, "LINUX") == 0)
10068 return elfcore_grok_xstatereg (abfd, note);
10069 else
10070 return TRUE;
10071
97753bd5
AM
10072 case NT_PPC_VMX:
10073 if (note->namesz == 6
10074 && strcmp (note->namedata, "LINUX") == 0)
10075 return elfcore_grok_ppc_vmx (abfd, note);
10076 else
10077 return TRUE;
10078
89eeb0bc
LM
10079 case NT_PPC_VSX:
10080 if (note->namesz == 6
07d6d2b8
AM
10081 && strcmp (note->namedata, "LINUX") == 0)
10082 return elfcore_grok_ppc_vsx (abfd, note);
89eeb0bc 10083 else
07d6d2b8 10084 return TRUE;
89eeb0bc 10085
cb2366c1
EBM
10086 case NT_PPC_TAR:
10087 if (note->namesz == 6
4b24dd1a
AM
10088 && strcmp (note->namedata, "LINUX") == 0)
10089 return elfcore_grok_ppc_tar (abfd, note);
cb2366c1 10090 else
4b24dd1a 10091 return TRUE;
cb2366c1
EBM
10092
10093 case NT_PPC_PPR:
10094 if (note->namesz == 6
4b24dd1a
AM
10095 && strcmp (note->namedata, "LINUX") == 0)
10096 return elfcore_grok_ppc_ppr (abfd, note);
cb2366c1 10097 else
4b24dd1a 10098 return TRUE;
cb2366c1
EBM
10099
10100 case NT_PPC_DSCR:
10101 if (note->namesz == 6
4b24dd1a
AM
10102 && strcmp (note->namedata, "LINUX") == 0)
10103 return elfcore_grok_ppc_dscr (abfd, note);
cb2366c1 10104 else
4b24dd1a 10105 return TRUE;
cb2366c1
EBM
10106
10107 case NT_PPC_EBB:
10108 if (note->namesz == 6
4b24dd1a
AM
10109 && strcmp (note->namedata, "LINUX") == 0)
10110 return elfcore_grok_ppc_ebb (abfd, note);
cb2366c1 10111 else
4b24dd1a 10112 return TRUE;
cb2366c1
EBM
10113
10114 case NT_PPC_PMU:
10115 if (note->namesz == 6
4b24dd1a
AM
10116 && strcmp (note->namedata, "LINUX") == 0)
10117 return elfcore_grok_ppc_pmu (abfd, note);
cb2366c1 10118 else
4b24dd1a 10119 return TRUE;
cb2366c1
EBM
10120
10121 case NT_PPC_TM_CGPR:
10122 if (note->namesz == 6
4b24dd1a
AM
10123 && strcmp (note->namedata, "LINUX") == 0)
10124 return elfcore_grok_ppc_tm_cgpr (abfd, note);
cb2366c1 10125 else
4b24dd1a 10126 return TRUE;
cb2366c1
EBM
10127
10128 case NT_PPC_TM_CFPR:
10129 if (note->namesz == 6
4b24dd1a
AM
10130 && strcmp (note->namedata, "LINUX") == 0)
10131 return elfcore_grok_ppc_tm_cfpr (abfd, note);
cb2366c1 10132 else
4b24dd1a 10133 return TRUE;
cb2366c1
EBM
10134
10135 case NT_PPC_TM_CVMX:
10136 if (note->namesz == 6
4b24dd1a
AM
10137 && strcmp (note->namedata, "LINUX") == 0)
10138 return elfcore_grok_ppc_tm_cvmx (abfd, note);
cb2366c1 10139 else
4b24dd1a 10140 return TRUE;
cb2366c1
EBM
10141
10142 case NT_PPC_TM_CVSX:
10143 if (note->namesz == 6
4b24dd1a
AM
10144 && strcmp (note->namedata, "LINUX") == 0)
10145 return elfcore_grok_ppc_tm_cvsx (abfd, note);
cb2366c1 10146 else
4b24dd1a 10147 return TRUE;
cb2366c1
EBM
10148
10149 case NT_PPC_TM_SPR:
10150 if (note->namesz == 6
4b24dd1a
AM
10151 && strcmp (note->namedata, "LINUX") == 0)
10152 return elfcore_grok_ppc_tm_spr (abfd, note);
cb2366c1 10153 else
4b24dd1a 10154 return TRUE;
cb2366c1
EBM
10155
10156 case NT_PPC_TM_CTAR:
10157 if (note->namesz == 6
4b24dd1a
AM
10158 && strcmp (note->namedata, "LINUX") == 0)
10159 return elfcore_grok_ppc_tm_ctar (abfd, note);
cb2366c1 10160 else
4b24dd1a 10161 return TRUE;
cb2366c1
EBM
10162
10163 case NT_PPC_TM_CPPR:
10164 if (note->namesz == 6
4b24dd1a
AM
10165 && strcmp (note->namedata, "LINUX") == 0)
10166 return elfcore_grok_ppc_tm_cppr (abfd, note);
cb2366c1 10167 else
4b24dd1a 10168 return TRUE;
cb2366c1
EBM
10169
10170 case NT_PPC_TM_CDSCR:
10171 if (note->namesz == 6
4b24dd1a
AM
10172 && strcmp (note->namedata, "LINUX") == 0)
10173 return elfcore_grok_ppc_tm_cdscr (abfd, note);
cb2366c1 10174 else
4b24dd1a 10175 return TRUE;
cb2366c1 10176
0675e188
UW
10177 case NT_S390_HIGH_GPRS:
10178 if (note->namesz == 6
07d6d2b8
AM
10179 && strcmp (note->namedata, "LINUX") == 0)
10180 return elfcore_grok_s390_high_gprs (abfd, note);
0675e188 10181 else
07d6d2b8 10182 return TRUE;
0675e188 10183
d7eeb400
MS
10184 case NT_S390_TIMER:
10185 if (note->namesz == 6
07d6d2b8
AM
10186 && strcmp (note->namedata, "LINUX") == 0)
10187 return elfcore_grok_s390_timer (abfd, note);
d7eeb400 10188 else
07d6d2b8 10189 return TRUE;
d7eeb400
MS
10190
10191 case NT_S390_TODCMP:
10192 if (note->namesz == 6
07d6d2b8
AM
10193 && strcmp (note->namedata, "LINUX") == 0)
10194 return elfcore_grok_s390_todcmp (abfd, note);
d7eeb400 10195 else
07d6d2b8 10196 return TRUE;
d7eeb400
MS
10197
10198 case NT_S390_TODPREG:
10199 if (note->namesz == 6
07d6d2b8
AM
10200 && strcmp (note->namedata, "LINUX") == 0)
10201 return elfcore_grok_s390_todpreg (abfd, note);
d7eeb400 10202 else
07d6d2b8 10203 return TRUE;
d7eeb400
MS
10204
10205 case NT_S390_CTRS:
10206 if (note->namesz == 6
07d6d2b8
AM
10207 && strcmp (note->namedata, "LINUX") == 0)
10208 return elfcore_grok_s390_ctrs (abfd, note);
d7eeb400 10209 else
07d6d2b8 10210 return TRUE;
d7eeb400
MS
10211
10212 case NT_S390_PREFIX:
10213 if (note->namesz == 6
07d6d2b8
AM
10214 && strcmp (note->namedata, "LINUX") == 0)
10215 return elfcore_grok_s390_prefix (abfd, note);
d7eeb400 10216 else
07d6d2b8 10217 return TRUE;
d7eeb400 10218
355b81d9
UW
10219 case NT_S390_LAST_BREAK:
10220 if (note->namesz == 6
07d6d2b8
AM
10221 && strcmp (note->namedata, "LINUX") == 0)
10222 return elfcore_grok_s390_last_break (abfd, note);
355b81d9 10223 else
07d6d2b8 10224 return TRUE;
355b81d9
UW
10225
10226 case NT_S390_SYSTEM_CALL:
10227 if (note->namesz == 6
07d6d2b8
AM
10228 && strcmp (note->namedata, "LINUX") == 0)
10229 return elfcore_grok_s390_system_call (abfd, note);
355b81d9 10230 else
07d6d2b8 10231 return TRUE;
355b81d9 10232
abb3f6cc
NC
10233 case NT_S390_TDB:
10234 if (note->namesz == 6
07d6d2b8
AM
10235 && strcmp (note->namedata, "LINUX") == 0)
10236 return elfcore_grok_s390_tdb (abfd, note);
abb3f6cc 10237 else
07d6d2b8 10238 return TRUE;
abb3f6cc 10239
4ef9f41a
AA
10240 case NT_S390_VXRS_LOW:
10241 if (note->namesz == 6
10242 && strcmp (note->namedata, "LINUX") == 0)
10243 return elfcore_grok_s390_vxrs_low (abfd, note);
10244 else
10245 return TRUE;
10246
10247 case NT_S390_VXRS_HIGH:
10248 if (note->namesz == 6
10249 && strcmp (note->namedata, "LINUX") == 0)
10250 return elfcore_grok_s390_vxrs_high (abfd, note);
10251 else
10252 return TRUE;
10253
88ab90e8
AA
10254 case NT_S390_GS_CB:
10255 if (note->namesz == 6
10256 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 10257 return elfcore_grok_s390_gs_cb (abfd, note);
88ab90e8
AA
10258 else
10259 return TRUE;
10260
10261 case NT_S390_GS_BC:
10262 if (note->namesz == 6
10263 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 10264 return elfcore_grok_s390_gs_bc (abfd, note);
88ab90e8
AA
10265 else
10266 return TRUE;
10267
faa9a424
UW
10268 case NT_ARM_VFP:
10269 if (note->namesz == 6
10270 && strcmp (note->namedata, "LINUX") == 0)
10271 return elfcore_grok_arm_vfp (abfd, note);
10272 else
10273 return TRUE;
10274
652451f8
YZ
10275 case NT_ARM_TLS:
10276 if (note->namesz == 6
10277 && strcmp (note->namedata, "LINUX") == 0)
10278 return elfcore_grok_aarch_tls (abfd, note);
10279 else
10280 return TRUE;
10281
10282 case NT_ARM_HW_BREAK:
10283 if (note->namesz == 6
10284 && strcmp (note->namedata, "LINUX") == 0)
10285 return elfcore_grok_aarch_hw_break (abfd, note);
10286 else
10287 return TRUE;
10288
10289 case NT_ARM_HW_WATCH:
10290 if (note->namesz == 6
10291 && strcmp (note->namedata, "LINUX") == 0)
10292 return elfcore_grok_aarch_hw_watch (abfd, note);
10293 else
10294 return TRUE;
10295
ad1cc4e4
AH
10296 case NT_ARM_SVE:
10297 if (note->namesz == 6
10298 && strcmp (note->namedata, "LINUX") == 0)
10299 return elfcore_grok_aarch_sve (abfd, note);
10300 else
10301 return TRUE;
10302
e6c3b5bf
AH
10303 case NT_ARM_PAC_MASK:
10304 if (note->namesz == 6
10305 && strcmp (note->namedata, "LINUX") == 0)
10306 return elfcore_grok_aarch_pauth (abfd, note);
10307 else
10308 return TRUE;
10309
252b5132
RH
10310 case NT_PRPSINFO:
10311 case NT_PSINFO:
bb0082d6
AM
10312 if (bed->elf_backend_grok_psinfo)
10313 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
b34976b6 10314 return TRUE;
bb0082d6 10315#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
252b5132 10316 return elfcore_grok_psinfo (abfd, note);
bb0082d6 10317#else
b34976b6 10318 return TRUE;
252b5132 10319#endif
3333a7c3
RM
10320
10321 case NT_AUXV:
58e07198 10322 return elfcore_make_auxv_note_section (abfd, note, 0);
9015683b 10323
451b7c33
TT
10324 case NT_FILE:
10325 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10326 note);
10327
9015683b
TT
10328 case NT_SIGINFO:
10329 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10330 note);
5b2c414d 10331
252b5132
RH
10332 }
10333}
10334
718175fa
JK
10335static bfd_boolean
10336elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10337{
c74f7d1c 10338 struct bfd_build_id* build_id;
30e8ee25
AM
10339
10340 if (note->descsz == 0)
10341 return FALSE;
10342
c74f7d1c
JT
10343 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10344 if (build_id == NULL)
718175fa
JK
10345 return FALSE;
10346
c74f7d1c
JT
10347 build_id->size = note->descsz;
10348 memcpy (build_id->data, note->descdata, note->descsz);
10349 abfd->build_id = build_id;
718175fa
JK
10350
10351 return TRUE;
10352}
10353
10354static bfd_boolean
10355elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10356{
10357 switch (note->type)
10358 {
10359 default:
10360 return TRUE;
10361
46bed679
L
10362 case NT_GNU_PROPERTY_TYPE_0:
10363 return _bfd_elf_parse_gnu_properties (abfd, note);
10364
718175fa
JK
10365 case NT_GNU_BUILD_ID:
10366 return elfobj_grok_gnu_build_id (abfd, note);
10367 }
10368}
10369
e21e5835
NC
10370static bfd_boolean
10371elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10372{
10373 struct sdt_note *cur =
7a6e0d89
AM
10374 (struct sdt_note *) bfd_alloc (abfd,
10375 sizeof (struct sdt_note) + note->descsz);
e21e5835
NC
10376
10377 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10378 cur->size = (bfd_size_type) note->descsz;
10379 memcpy (cur->data, note->descdata, note->descsz);
10380
10381 elf_tdata (abfd)->sdt_note_head = cur;
10382
10383 return TRUE;
10384}
10385
10386static bfd_boolean
10387elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10388{
10389 switch (note->type)
10390 {
10391 case NT_STAPSDT:
10392 return elfobj_grok_stapsdt_note_1 (abfd, note);
10393
10394 default:
10395 return TRUE;
10396 }
10397}
10398
aa1ed4a9
JB
10399static bfd_boolean
10400elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
10401{
10402 size_t offset;
10403
b5430a3c 10404 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 10405 {
b5430a3c 10406 case ELFCLASS32:
0064d223
JB
10407 if (note->descsz < 108)
10408 return FALSE;
aa1ed4a9
JB
10409 break;
10410
b5430a3c 10411 case ELFCLASS64:
0064d223
JB
10412 if (note->descsz < 120)
10413 return FALSE;
aa1ed4a9
JB
10414 break;
10415
10416 default:
10417 return FALSE;
10418 }
10419
0064d223
JB
10420 /* Check for version 1 in pr_version. */
10421 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10422 return FALSE;
80a04378 10423
0064d223
JB
10424 offset = 4;
10425
10426 /* Skip over pr_psinfosz. */
b5430a3c 10427 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
0064d223
JB
10428 offset += 4;
10429 else
10430 {
10431 offset += 4; /* Padding before pr_psinfosz. */
10432 offset += 8;
10433 }
10434
aa1ed4a9
JB
10435 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10436 elf_tdata (abfd)->core->program
10437 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
10438 offset += 17;
10439
10440 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10441 elf_tdata (abfd)->core->command
10442 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
0064d223
JB
10443 offset += 81;
10444
10445 /* Padding before pr_pid. */
10446 offset += 2;
10447
10448 /* The pr_pid field was added in version "1a". */
10449 if (note->descsz < offset + 4)
10450 return TRUE;
10451
10452 elf_tdata (abfd)->core->pid
10453 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
aa1ed4a9
JB
10454
10455 return TRUE;
10456}
10457
10458static bfd_boolean
10459elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
10460{
10461 size_t offset;
10462 size_t size;
24d3e51b 10463 size_t min_size;
aa1ed4a9 10464
24d3e51b
NC
10465 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10466 Also compute minimum size of this note. */
b5430a3c 10467 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 10468 {
b5430a3c 10469 case ELFCLASS32:
24d3e51b
NC
10470 offset = 4 + 4;
10471 min_size = offset + (4 * 2) + 4 + 4 + 4;
aa1ed4a9
JB
10472 break;
10473
b5430a3c 10474 case ELFCLASS64:
24d3e51b
NC
10475 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
10476 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
aa1ed4a9
JB
10477 break;
10478
10479 default:
10480 return FALSE;
10481 }
10482
24d3e51b
NC
10483 if (note->descsz < min_size)
10484 return FALSE;
10485
10486 /* Check for version 1 in pr_version. */
10487 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10488 return FALSE;
aa1ed4a9 10489
24d3e51b
NC
10490 /* Extract size of pr_reg from pr_gregsetsz. */
10491 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
b5430a3c 10492 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
24d3e51b
NC
10493 {
10494 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10495 offset += 4 * 2;
10496 }
b5430a3c 10497 else
24d3e51b
NC
10498 {
10499 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
10500 offset += 8 * 2;
10501 }
aa1ed4a9 10502
24d3e51b 10503 /* Skip over pr_osreldate. */
aa1ed4a9
JB
10504 offset += 4;
10505
24d3e51b 10506 /* Read signal from pr_cursig. */
aa1ed4a9
JB
10507 if (elf_tdata (abfd)->core->signal == 0)
10508 elf_tdata (abfd)->core->signal
10509 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10510 offset += 4;
10511
24d3e51b 10512 /* Read TID from pr_pid. */
aa1ed4a9
JB
10513 elf_tdata (abfd)->core->lwpid
10514 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10515 offset += 4;
10516
24d3e51b 10517 /* Padding before pr_reg. */
b5430a3c 10518 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
aa1ed4a9
JB
10519 offset += 4;
10520
24d3e51b
NC
10521 /* Make sure that there is enough data remaining in the note. */
10522 if ((note->descsz - offset) < size)
10523 return FALSE;
10524
aa1ed4a9
JB
10525 /* Make a ".reg/999" section and a ".reg" section. */
10526 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10527 size, note->descpos + offset);
10528}
10529
10530static bfd_boolean
10531elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
10532{
544c67cd
JB
10533 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10534
aa1ed4a9
JB
10535 switch (note->type)
10536 {
10537 case NT_PRSTATUS:
544c67cd
JB
10538 if (bed->elf_backend_grok_freebsd_prstatus)
10539 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
10540 return TRUE;
aa1ed4a9
JB
10541 return elfcore_grok_freebsd_prstatus (abfd, note);
10542
10543 case NT_FPREGSET:
10544 return elfcore_grok_prfpreg (abfd, note);
10545
10546 case NT_PRPSINFO:
10547 return elfcore_grok_freebsd_psinfo (abfd, note);
10548
10549 case NT_FREEBSD_THRMISC:
10550 if (note->namesz == 8)
10551 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
10552 else
10553 return TRUE;
10554
ddb2bbcf
JB
10555 case NT_FREEBSD_PROCSTAT_PROC:
10556 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
10557 note);
10558
10559 case NT_FREEBSD_PROCSTAT_FILES:
10560 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
10561 note);
10562
10563 case NT_FREEBSD_PROCSTAT_VMMAP:
10564 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
10565 note);
10566
3350c5f5 10567 case NT_FREEBSD_PROCSTAT_AUXV:
58e07198 10568 return elfcore_make_auxv_note_section (abfd, note, 4);
3350c5f5 10569
aa1ed4a9
JB
10570 case NT_X86_XSTATE:
10571 if (note->namesz == 8)
10572 return elfcore_grok_xstatereg (abfd, note);
10573 else
10574 return TRUE;
10575
e6f3b9c3
JB
10576 case NT_FREEBSD_PTLWPINFO:
10577 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
10578 note);
10579
6d5be5d6
JB
10580 case NT_ARM_VFP:
10581 return elfcore_grok_arm_vfp (abfd, note);
10582
aa1ed4a9
JB
10583 default:
10584 return TRUE;
10585 }
10586}
10587
b34976b6 10588static bfd_boolean
217aa764 10589elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
50b2bdb7
AM
10590{
10591 char *cp;
10592
10593 cp = strchr (note->namedata, '@');
10594 if (cp != NULL)
10595 {
d2b64500 10596 *lwpidp = atoi(cp + 1);
b34976b6 10597 return TRUE;
50b2bdb7 10598 }
b34976b6 10599 return FALSE;
50b2bdb7
AM
10600}
10601
b34976b6 10602static bfd_boolean
217aa764 10603elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7 10604{
80a04378
NC
10605 if (note->descsz <= 0x7c + 31)
10606 return FALSE;
10607
50b2bdb7 10608 /* Signal number at offset 0x08. */
228e534f 10609 elf_tdata (abfd)->core->signal
50b2bdb7
AM
10610 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10611
10612 /* Process ID at offset 0x50. */
228e534f 10613 elf_tdata (abfd)->core->pid
50b2bdb7
AM
10614 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
10615
10616 /* Command name at 0x7c (max 32 bytes, including nul). */
228e534f 10617 elf_tdata (abfd)->core->command
50b2bdb7
AM
10618 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
10619
7720ba9f
MK
10620 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
10621 note);
50b2bdb7
AM
10622}
10623
b34976b6 10624static bfd_boolean
217aa764 10625elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7
AM
10626{
10627 int lwp;
10628
10629 if (elfcore_netbsd_get_lwpid (note, &lwp))
228e534f 10630 elf_tdata (abfd)->core->lwpid = lwp;
50b2bdb7 10631
58e07198 10632 switch (note->type)
50b2bdb7 10633 {
58e07198 10634 case NT_NETBSDCORE_PROCINFO:
50b2bdb7 10635 /* NetBSD-specific core "procinfo". Note that we expect to
08a40648
AM
10636 find this note before any of the others, which is fine,
10637 since the kernel writes this note out first when it
10638 creates a core file. */
50b2bdb7 10639 return elfcore_grok_netbsd_procinfo (abfd, note);
58e07198
CZ
10640#ifdef NT_NETBSDCORE_AUXV
10641 case NT_NETBSDCORE_AUXV:
10642 /* NetBSD-specific Elf Auxiliary Vector data. */
10643 return elfcore_make_auxv_note_section (abfd, note, 4);
10644#endif
10645 default:
10646 break;
50b2bdb7
AM
10647 }
10648
58e07198 10649 /* As of March 2017 there are no other machine-independent notes
b4db1224
JT
10650 defined for NetBSD core files. If the note type is less
10651 than the start of the machine-dependent note types, we don't
10652 understand it. */
47d9a591 10653
b4db1224 10654 if (note->type < NT_NETBSDCORE_FIRSTMACH)
b34976b6 10655 return TRUE;
50b2bdb7
AM
10656
10657
10658 switch (bfd_get_arch (abfd))
10659 {
08a40648
AM
10660 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10661 PT_GETFPREGS == mach+2. */
50b2bdb7
AM
10662
10663 case bfd_arch_alpha:
10664 case bfd_arch_sparc:
10665 switch (note->type)
08a40648
AM
10666 {
10667 case NT_NETBSDCORE_FIRSTMACH+0:
10668 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 10669
08a40648
AM
10670 case NT_NETBSDCORE_FIRSTMACH+2:
10671 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 10672
08a40648
AM
10673 default:
10674 return TRUE;
10675 }
50b2bdb7 10676
58e07198
CZ
10677 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
10678 There's also old PT___GETREGS40 == mach + 1 for old reg
10679 structure which lacks GBR. */
10680
10681 case bfd_arch_sh:
10682 switch (note->type)
10683 {
10684 case NT_NETBSDCORE_FIRSTMACH+3:
10685 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10686
10687 case NT_NETBSDCORE_FIRSTMACH+5:
10688 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10689
10690 default:
10691 return TRUE;
10692 }
10693
08a40648
AM
10694 /* On all other arch's, PT_GETREGS == mach+1 and
10695 PT_GETFPREGS == mach+3. */
50b2bdb7
AM
10696
10697 default:
10698 switch (note->type)
08a40648
AM
10699 {
10700 case NT_NETBSDCORE_FIRSTMACH+1:
10701 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 10702
08a40648
AM
10703 case NT_NETBSDCORE_FIRSTMACH+3:
10704 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 10705
08a40648
AM
10706 default:
10707 return TRUE;
10708 }
50b2bdb7
AM
10709 }
10710 /* NOTREACHED */
10711}
10712
67cc5033
MK
10713static bfd_boolean
10714elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
10715{
80a04378
NC
10716 if (note->descsz <= 0x48 + 31)
10717 return FALSE;
10718
67cc5033 10719 /* Signal number at offset 0x08. */
228e534f 10720 elf_tdata (abfd)->core->signal
67cc5033
MK
10721 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10722
10723 /* Process ID at offset 0x20. */
228e534f 10724 elf_tdata (abfd)->core->pid
67cc5033
MK
10725 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
10726
10727 /* Command name at 0x48 (max 32 bytes, including nul). */
228e534f 10728 elf_tdata (abfd)->core->command
67cc5033
MK
10729 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
10730
10731 return TRUE;
10732}
10733
10734static bfd_boolean
10735elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
10736{
10737 if (note->type == NT_OPENBSD_PROCINFO)
10738 return elfcore_grok_openbsd_procinfo (abfd, note);
10739
10740 if (note->type == NT_OPENBSD_REGS)
10741 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10742
10743 if (note->type == NT_OPENBSD_FPREGS)
10744 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10745
10746 if (note->type == NT_OPENBSD_XFPREGS)
10747 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
10748
10749 if (note->type == NT_OPENBSD_AUXV)
58e07198 10750 return elfcore_make_auxv_note_section (abfd, note, 0);
67cc5033
MK
10751
10752 if (note->type == NT_OPENBSD_WCOOKIE)
10753 {
10754 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
10755 SEC_HAS_CONTENTS);
10756
10757 if (sect == NULL)
10758 return FALSE;
10759 sect->size = note->descsz;
10760 sect->filepos = note->descpos;
10761 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10762
10763 return TRUE;
10764 }
10765
10766 return TRUE;
10767}
10768
07c6e936 10769static bfd_boolean
d3fd4074 10770elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
07c6e936
NC
10771{
10772 void *ddata = note->descdata;
10773 char buf[100];
10774 char *name;
10775 asection *sect;
f8843e87
AM
10776 short sig;
10777 unsigned flags;
07c6e936 10778
80a04378
NC
10779 if (note->descsz < 16)
10780 return FALSE;
10781
07c6e936 10782 /* nto_procfs_status 'pid' field is at offset 0. */
228e534f 10783 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
07c6e936 10784
f8843e87
AM
10785 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
10786 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
10787
10788 /* nto_procfs_status 'flags' field is at offset 8. */
10789 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
07c6e936
NC
10790
10791 /* nto_procfs_status 'what' field is at offset 14. */
f8843e87
AM
10792 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
10793 {
228e534f
AM
10794 elf_tdata (abfd)->core->signal = sig;
10795 elf_tdata (abfd)->core->lwpid = *tid;
f8843e87 10796 }
07c6e936 10797
f8843e87
AM
10798 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
10799 do not come from signals so we make sure we set the current
10800 thread just in case. */
10801 if (flags & 0x00000080)
228e534f 10802 elf_tdata (abfd)->core->lwpid = *tid;
07c6e936
NC
10803
10804 /* Make a ".qnx_core_status/%d" section. */
d3fd4074 10805 sprintf (buf, ".qnx_core_status/%ld", *tid);
07c6e936 10806
a50b1753 10807 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936
NC
10808 if (name == NULL)
10809 return FALSE;
10810 strcpy (name, buf);
10811
117ed4f8 10812 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936
NC
10813 if (sect == NULL)
10814 return FALSE;
10815
07d6d2b8
AM
10816 sect->size = note->descsz;
10817 sect->filepos = note->descpos;
07c6e936
NC
10818 sect->alignment_power = 2;
10819
10820 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
10821}
10822
10823static bfd_boolean
d69f560c
KW
10824elfcore_grok_nto_regs (bfd *abfd,
10825 Elf_Internal_Note *note,
d3fd4074 10826 long tid,
d69f560c 10827 char *base)
07c6e936
NC
10828{
10829 char buf[100];
10830 char *name;
10831 asection *sect;
10832
d69f560c 10833 /* Make a "(base)/%d" section. */
d3fd4074 10834 sprintf (buf, "%s/%ld", base, tid);
07c6e936 10835
a50b1753 10836 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936
NC
10837 if (name == NULL)
10838 return FALSE;
10839 strcpy (name, buf);
10840
117ed4f8 10841 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936
NC
10842 if (sect == NULL)
10843 return FALSE;
10844
07d6d2b8
AM
10845 sect->size = note->descsz;
10846 sect->filepos = note->descpos;
07c6e936
NC
10847 sect->alignment_power = 2;
10848
f8843e87 10849 /* This is the current thread. */
228e534f 10850 if (elf_tdata (abfd)->core->lwpid == tid)
d69f560c 10851 return elfcore_maybe_make_sect (abfd, base, sect);
f8843e87
AM
10852
10853 return TRUE;
07c6e936
NC
10854}
10855
10856#define BFD_QNT_CORE_INFO 7
10857#define BFD_QNT_CORE_STATUS 8
10858#define BFD_QNT_CORE_GREG 9
10859#define BFD_QNT_CORE_FPREG 10
10860
10861static bfd_boolean
217aa764 10862elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
07c6e936
NC
10863{
10864 /* Every GREG section has a STATUS section before it. Store the
811072d8 10865 tid from the previous call to pass down to the next gregs
07c6e936 10866 function. */
d3fd4074 10867 static long tid = 1;
07c6e936
NC
10868
10869 switch (note->type)
10870 {
d69f560c
KW
10871 case BFD_QNT_CORE_INFO:
10872 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
10873 case BFD_QNT_CORE_STATUS:
10874 return elfcore_grok_nto_status (abfd, note, &tid);
10875 case BFD_QNT_CORE_GREG:
10876 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
10877 case BFD_QNT_CORE_FPREG:
10878 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
10879 default:
10880 return TRUE;
07c6e936
NC
10881 }
10882}
10883
b15fa79e
AM
10884static bfd_boolean
10885elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
10886{
10887 char *name;
10888 asection *sect;
10889 size_t len;
10890
10891 /* Use note name as section name. */
10892 len = note->namesz;
a50b1753 10893 name = (char *) bfd_alloc (abfd, len);
b15fa79e
AM
10894 if (name == NULL)
10895 return FALSE;
10896 memcpy (name, note->namedata, len);
10897 name[len - 1] = '\0';
10898
10899 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10900 if (sect == NULL)
10901 return FALSE;
10902
07d6d2b8
AM
10903 sect->size = note->descsz;
10904 sect->filepos = note->descpos;
b15fa79e
AM
10905 sect->alignment_power = 1;
10906
10907 return TRUE;
10908}
10909
7c76fa91
MS
10910/* Function: elfcore_write_note
10911
47d9a591 10912 Inputs:
a39f3346 10913 buffer to hold note, and current size of buffer
7c76fa91
MS
10914 name of note
10915 type of note
10916 data for note
10917 size of data for note
10918
a39f3346
AM
10919 Writes note to end of buffer. ELF64 notes are written exactly as
10920 for ELF32, despite the current (as of 2006) ELF gabi specifying
10921 that they ought to have 8-byte namesz and descsz field, and have
10922 8-byte alignment. Other writers, eg. Linux kernel, do the same.
10923
7c76fa91 10924 Return:
a39f3346 10925 Pointer to realloc'd buffer, *BUFSIZ updated. */
7c76fa91
MS
10926
10927char *
a39f3346 10928elfcore_write_note (bfd *abfd,
217aa764 10929 char *buf,
a39f3346 10930 int *bufsiz,
217aa764 10931 const char *name,
a39f3346 10932 int type,
217aa764 10933 const void *input,
a39f3346 10934 int size)
7c76fa91
MS
10935{
10936 Elf_External_Note *xnp;
d4c88bbb 10937 size_t namesz;
d4c88bbb 10938 size_t newspace;
a39f3346 10939 char *dest;
7c76fa91 10940
d4c88bbb 10941 namesz = 0;
d4c88bbb 10942 if (name != NULL)
a39f3346 10943 namesz = strlen (name) + 1;
d4c88bbb 10944
a39f3346 10945 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
d4c88bbb 10946
a50b1753 10947 buf = (char *) realloc (buf, *bufsiz + newspace);
14b1c01e
AM
10948 if (buf == NULL)
10949 return buf;
a39f3346 10950 dest = buf + *bufsiz;
7c76fa91
MS
10951 *bufsiz += newspace;
10952 xnp = (Elf_External_Note *) dest;
10953 H_PUT_32 (abfd, namesz, xnp->namesz);
10954 H_PUT_32 (abfd, size, xnp->descsz);
10955 H_PUT_32 (abfd, type, xnp->type);
d4c88bbb
AM
10956 dest = xnp->name;
10957 if (name != NULL)
10958 {
10959 memcpy (dest, name, namesz);
10960 dest += namesz;
a39f3346 10961 while (namesz & 3)
d4c88bbb
AM
10962 {
10963 *dest++ = '\0';
a39f3346 10964 ++namesz;
d4c88bbb
AM
10965 }
10966 }
10967 memcpy (dest, input, size);
a39f3346
AM
10968 dest += size;
10969 while (size & 3)
10970 {
10971 *dest++ = '\0';
10972 ++size;
10973 }
10974 return buf;
7c76fa91
MS
10975}
10976
602f1657
AM
10977/* gcc-8 warns (*) on all the strncpy calls in this function about
10978 possible string truncation. The "truncation" is not a bug. We
10979 have an external representation of structs with fields that are not
10980 necessarily NULL terminated and corresponding internal
10981 representation fields that are one larger so that they can always
10982 be NULL terminated.
10983 gcc versions between 4.2 and 4.6 do not allow pragma control of
10984 diagnostics inside functions, giving a hard error if you try to use
10985 the finer control available with later versions.
10986 gcc prior to 4.2 warns about diagnostic push and pop.
10987 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
10988 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
10989 (*) Depending on your system header files! */
d99b4b92 10990#if GCC_VERSION >= 8000
602f1657
AM
10991# pragma GCC diagnostic push
10992# pragma GCC diagnostic ignored "-Wstringop-truncation"
d99b4b92 10993#endif
7c76fa91 10994char *
217aa764
AM
10995elfcore_write_prpsinfo (bfd *abfd,
10996 char *buf,
10997 int *bufsiz,
10998 const char *fname,
10999 const char *psargs)
7c76fa91 11000{
183e98be
AM
11001 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11002
11003 if (bed->elf_backend_write_core_note != NULL)
11004 {
11005 char *ret;
11006 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11007 NT_PRPSINFO, fname, psargs);
11008 if (ret != NULL)
11009 return ret;
11010 }
7c76fa91 11011
1f20dca5 11012#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
602f1657 11013# if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
183e98be
AM
11014 if (bed->s->elfclass == ELFCLASS32)
11015 {
602f1657 11016# if defined (HAVE_PSINFO32_T)
183e98be
AM
11017 psinfo32_t data;
11018 int note_type = NT_PSINFO;
602f1657 11019# else
183e98be
AM
11020 prpsinfo32_t data;
11021 int note_type = NT_PRPSINFO;
602f1657 11022# endif
183e98be
AM
11023
11024 memset (&data, 0, sizeof (data));
11025 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11026 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11027 return elfcore_write_note (abfd, buf, bufsiz,
1f20dca5 11028 "CORE", note_type, &data, sizeof (data));
183e98be
AM
11029 }
11030 else
602f1657 11031# endif
183e98be 11032 {
602f1657 11033# if defined (HAVE_PSINFO_T)
183e98be
AM
11034 psinfo_t data;
11035 int note_type = NT_PSINFO;
602f1657 11036# else
183e98be
AM
11037 prpsinfo_t data;
11038 int note_type = NT_PRPSINFO;
602f1657 11039# endif
7c76fa91 11040
183e98be
AM
11041 memset (&data, 0, sizeof (data));
11042 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11043 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11044 return elfcore_write_note (abfd, buf, bufsiz,
1f20dca5 11045 "CORE", note_type, &data, sizeof (data));
183e98be 11046 }
7c76fa91
MS
11047#endif /* PSINFO_T or PRPSINFO_T */
11048
1f20dca5
UW
11049 free (buf);
11050 return NULL;
11051}
d99b4b92 11052#if GCC_VERSION >= 8000
602f1657 11053# pragma GCC diagnostic pop
d99b4b92 11054#endif
1f20dca5 11055
70a38d42
SDJ
11056char *
11057elfcore_write_linux_prpsinfo32
11058 (bfd *abfd, char *buf, int *bufsiz,
11059 const struct elf_internal_linux_prpsinfo *prpsinfo)
11060{
a2f63b2e
MR
11061 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
11062 {
11063 struct elf_external_linux_prpsinfo32_ugid16 data;
11064
11065 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
11066 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11067 &data, sizeof (data));
11068 }
11069 else
11070 {
11071 struct elf_external_linux_prpsinfo32_ugid32 data;
70a38d42 11072
a2f63b2e
MR
11073 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
11074 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11075 &data, sizeof (data));
11076 }
70a38d42
SDJ
11077}
11078
11079char *
11080elfcore_write_linux_prpsinfo64
11081 (bfd *abfd, char *buf, int *bufsiz,
11082 const struct elf_internal_linux_prpsinfo *prpsinfo)
11083{
3c9a7b0d
MR
11084 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
11085 {
11086 struct elf_external_linux_prpsinfo64_ugid16 data;
11087
11088 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
11089 return elfcore_write_note (abfd, buf, bufsiz,
11090 "CORE", NT_PRPSINFO, &data, sizeof (data));
11091 }
11092 else
11093 {
11094 struct elf_external_linux_prpsinfo64_ugid32 data;
70a38d42 11095
3c9a7b0d
MR
11096 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
11097 return elfcore_write_note (abfd, buf, bufsiz,
11098 "CORE", NT_PRPSINFO, &data, sizeof (data));
11099 }
70a38d42
SDJ
11100}
11101
7c76fa91 11102char *
217aa764
AM
11103elfcore_write_prstatus (bfd *abfd,
11104 char *buf,
11105 int *bufsiz,
11106 long pid,
11107 int cursig,
11108 const void *gregs)
7c76fa91 11109{
183e98be 11110 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 11111
183e98be
AM
11112 if (bed->elf_backend_write_core_note != NULL)
11113 {
11114 char *ret;
11115 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11116 NT_PRSTATUS,
11117 pid, cursig, gregs);
11118 if (ret != NULL)
11119 return ret;
11120 }
11121
1f20dca5 11122#if defined (HAVE_PRSTATUS_T)
183e98be
AM
11123#if defined (HAVE_PRSTATUS32_T)
11124 if (bed->s->elfclass == ELFCLASS32)
11125 {
11126 prstatus32_t prstat;
11127
11128 memset (&prstat, 0, sizeof (prstat));
11129 prstat.pr_pid = pid;
11130 prstat.pr_cursig = cursig;
11131 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 11132 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
11133 NT_PRSTATUS, &prstat, sizeof (prstat));
11134 }
11135 else
11136#endif
11137 {
11138 prstatus_t prstat;
11139
11140 memset (&prstat, 0, sizeof (prstat));
11141 prstat.pr_pid = pid;
11142 prstat.pr_cursig = cursig;
11143 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 11144 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
11145 NT_PRSTATUS, &prstat, sizeof (prstat));
11146 }
7c76fa91
MS
11147#endif /* HAVE_PRSTATUS_T */
11148
1f20dca5
UW
11149 free (buf);
11150 return NULL;
11151}
11152
51316059
MS
11153#if defined (HAVE_LWPSTATUS_T)
11154char *
217aa764
AM
11155elfcore_write_lwpstatus (bfd *abfd,
11156 char *buf,
11157 int *bufsiz,
11158 long pid,
11159 int cursig,
11160 const void *gregs)
51316059
MS
11161{
11162 lwpstatus_t lwpstat;
183e98be 11163 const char *note_name = "CORE";
51316059
MS
11164
11165 memset (&lwpstat, 0, sizeof (lwpstat));
11166 lwpstat.pr_lwpid = pid >> 16;
11167 lwpstat.pr_cursig = cursig;
11168#if defined (HAVE_LWPSTATUS_T_PR_REG)
d1e8523e 11169 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
51316059
MS
11170#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11171#if !defined(gregs)
11172 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
11173 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
11174#else
11175 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
11176 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
11177#endif
11178#endif
47d9a591 11179 return elfcore_write_note (abfd, buf, bufsiz, note_name,
51316059
MS
11180 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
11181}
11182#endif /* HAVE_LWPSTATUS_T */
11183
7c76fa91
MS
11184#if defined (HAVE_PSTATUS_T)
11185char *
217aa764
AM
11186elfcore_write_pstatus (bfd *abfd,
11187 char *buf,
11188 int *bufsiz,
11189 long pid,
6c10990d
NC
11190 int cursig ATTRIBUTE_UNUSED,
11191 const void *gregs ATTRIBUTE_UNUSED)
7c76fa91 11192{
183e98be
AM
11193 const char *note_name = "CORE";
11194#if defined (HAVE_PSTATUS32_T)
11195 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 11196
183e98be
AM
11197 if (bed->s->elfclass == ELFCLASS32)
11198 {
11199 pstatus32_t pstat;
11200
11201 memset (&pstat, 0, sizeof (pstat));
11202 pstat.pr_pid = pid & 0xffff;
11203 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11204 NT_PSTATUS, &pstat, sizeof (pstat));
11205 return buf;
11206 }
11207 else
11208#endif
11209 {
11210 pstatus_t pstat;
11211
11212 memset (&pstat, 0, sizeof (pstat));
11213 pstat.pr_pid = pid & 0xffff;
11214 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11215 NT_PSTATUS, &pstat, sizeof (pstat));
11216 return buf;
11217 }
7c76fa91
MS
11218}
11219#endif /* HAVE_PSTATUS_T */
11220
11221char *
217aa764
AM
11222elfcore_write_prfpreg (bfd *abfd,
11223 char *buf,
11224 int *bufsiz,
11225 const void *fpregs,
11226 int size)
7c76fa91 11227{
183e98be 11228 const char *note_name = "CORE";
47d9a591 11229 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
11230 note_name, NT_FPREGSET, fpregs, size);
11231}
11232
11233char *
217aa764
AM
11234elfcore_write_prxfpreg (bfd *abfd,
11235 char *buf,
11236 int *bufsiz,
11237 const void *xfpregs,
11238 int size)
7c76fa91
MS
11239{
11240 char *note_name = "LINUX";
47d9a591 11241 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
11242 note_name, NT_PRXFPREG, xfpregs, size);
11243}
11244
4339cae0
L
11245char *
11246elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
11247 const void *xfpregs, int size)
11248{
97de3545
JB
11249 char *note_name;
11250 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
11251 note_name = "FreeBSD";
11252 else
11253 note_name = "LINUX";
4339cae0
L
11254 return elfcore_write_note (abfd, buf, bufsiz,
11255 note_name, NT_X86_XSTATE, xfpregs, size);
11256}
11257
97753bd5
AM
11258char *
11259elfcore_write_ppc_vmx (bfd *abfd,
11260 char *buf,
11261 int *bufsiz,
11262 const void *ppc_vmx,
11263 int size)
11264{
11265 char *note_name = "LINUX";
11266 return elfcore_write_note (abfd, buf, bufsiz,
11267 note_name, NT_PPC_VMX, ppc_vmx, size);
11268}
11269
89eeb0bc
LM
11270char *
11271elfcore_write_ppc_vsx (bfd *abfd,
07d6d2b8
AM
11272 char *buf,
11273 int *bufsiz,
11274 const void *ppc_vsx,
11275 int size)
89eeb0bc
LM
11276{
11277 char *note_name = "LINUX";
11278 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11279 note_name, NT_PPC_VSX, ppc_vsx, size);
89eeb0bc
LM
11280}
11281
cb2366c1
EBM
11282char *
11283elfcore_write_ppc_tar (bfd *abfd,
4b24dd1a
AM
11284 char *buf,
11285 int *bufsiz,
11286 const void *ppc_tar,
11287 int size)
cb2366c1
EBM
11288{
11289 char *note_name = "LINUX";
11290 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11291 note_name, NT_PPC_TAR, ppc_tar, size);
cb2366c1
EBM
11292}
11293
11294char *
11295elfcore_write_ppc_ppr (bfd *abfd,
4b24dd1a
AM
11296 char *buf,
11297 int *bufsiz,
11298 const void *ppc_ppr,
11299 int size)
cb2366c1
EBM
11300{
11301 char *note_name = "LINUX";
11302 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11303 note_name, NT_PPC_PPR, ppc_ppr, size);
cb2366c1
EBM
11304}
11305
11306char *
11307elfcore_write_ppc_dscr (bfd *abfd,
4b24dd1a
AM
11308 char *buf,
11309 int *bufsiz,
11310 const void *ppc_dscr,
11311 int size)
cb2366c1
EBM
11312{
11313 char *note_name = "LINUX";
11314 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11315 note_name, NT_PPC_DSCR, ppc_dscr, size);
cb2366c1
EBM
11316}
11317
11318char *
11319elfcore_write_ppc_ebb (bfd *abfd,
4b24dd1a
AM
11320 char *buf,
11321 int *bufsiz,
11322 const void *ppc_ebb,
11323 int size)
cb2366c1
EBM
11324{
11325 char *note_name = "LINUX";
11326 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11327 note_name, NT_PPC_EBB, ppc_ebb, size);
cb2366c1
EBM
11328}
11329
11330char *
11331elfcore_write_ppc_pmu (bfd *abfd,
4b24dd1a
AM
11332 char *buf,
11333 int *bufsiz,
11334 const void *ppc_pmu,
11335 int size)
cb2366c1
EBM
11336{
11337 char *note_name = "LINUX";
11338 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11339 note_name, NT_PPC_PMU, ppc_pmu, size);
cb2366c1
EBM
11340}
11341
11342char *
11343elfcore_write_ppc_tm_cgpr (bfd *abfd,
4b24dd1a
AM
11344 char *buf,
11345 int *bufsiz,
11346 const void *ppc_tm_cgpr,
11347 int size)
cb2366c1
EBM
11348{
11349 char *note_name = "LINUX";
11350 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11351 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
cb2366c1
EBM
11352}
11353
11354char *
11355elfcore_write_ppc_tm_cfpr (bfd *abfd,
4b24dd1a
AM
11356 char *buf,
11357 int *bufsiz,
11358 const void *ppc_tm_cfpr,
11359 int size)
cb2366c1
EBM
11360{
11361 char *note_name = "LINUX";
11362 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11363 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
cb2366c1
EBM
11364}
11365
11366char *
11367elfcore_write_ppc_tm_cvmx (bfd *abfd,
4b24dd1a
AM
11368 char *buf,
11369 int *bufsiz,
11370 const void *ppc_tm_cvmx,
11371 int size)
cb2366c1
EBM
11372{
11373 char *note_name = "LINUX";
11374 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11375 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
cb2366c1
EBM
11376}
11377
11378char *
11379elfcore_write_ppc_tm_cvsx (bfd *abfd,
4b24dd1a
AM
11380 char *buf,
11381 int *bufsiz,
11382 const void *ppc_tm_cvsx,
11383 int size)
cb2366c1
EBM
11384{
11385 char *note_name = "LINUX";
11386 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11387 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
cb2366c1
EBM
11388}
11389
11390char *
11391elfcore_write_ppc_tm_spr (bfd *abfd,
4b24dd1a
AM
11392 char *buf,
11393 int *bufsiz,
11394 const void *ppc_tm_spr,
11395 int size)
cb2366c1
EBM
11396{
11397 char *note_name = "LINUX";
11398 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11399 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
cb2366c1
EBM
11400}
11401
11402char *
11403elfcore_write_ppc_tm_ctar (bfd *abfd,
4b24dd1a
AM
11404 char *buf,
11405 int *bufsiz,
11406 const void *ppc_tm_ctar,
11407 int size)
cb2366c1
EBM
11408{
11409 char *note_name = "LINUX";
11410 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11411 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
cb2366c1
EBM
11412}
11413
11414char *
11415elfcore_write_ppc_tm_cppr (bfd *abfd,
4b24dd1a
AM
11416 char *buf,
11417 int *bufsiz,
11418 const void *ppc_tm_cppr,
11419 int size)
cb2366c1
EBM
11420{
11421 char *note_name = "LINUX";
11422 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11423 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
cb2366c1
EBM
11424}
11425
11426char *
11427elfcore_write_ppc_tm_cdscr (bfd *abfd,
4b24dd1a
AM
11428 char *buf,
11429 int *bufsiz,
11430 const void *ppc_tm_cdscr,
11431 int size)
cb2366c1
EBM
11432{
11433 char *note_name = "LINUX";
11434 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11435 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
cb2366c1
EBM
11436}
11437
0675e188
UW
11438static char *
11439elfcore_write_s390_high_gprs (bfd *abfd,
11440 char *buf,
11441 int *bufsiz,
11442 const void *s390_high_gprs,
11443 int size)
11444{
11445 char *note_name = "LINUX";
11446 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11447 note_name, NT_S390_HIGH_GPRS,
0675e188
UW
11448 s390_high_gprs, size);
11449}
11450
d7eeb400
MS
11451char *
11452elfcore_write_s390_timer (bfd *abfd,
07d6d2b8
AM
11453 char *buf,
11454 int *bufsiz,
11455 const void *s390_timer,
11456 int size)
d7eeb400
MS
11457{
11458 char *note_name = "LINUX";
11459 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11460 note_name, NT_S390_TIMER, s390_timer, size);
d7eeb400
MS
11461}
11462
11463char *
11464elfcore_write_s390_todcmp (bfd *abfd,
07d6d2b8
AM
11465 char *buf,
11466 int *bufsiz,
11467 const void *s390_todcmp,
11468 int size)
d7eeb400
MS
11469{
11470 char *note_name = "LINUX";
11471 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11472 note_name, NT_S390_TODCMP, s390_todcmp, size);
d7eeb400
MS
11473}
11474
11475char *
11476elfcore_write_s390_todpreg (bfd *abfd,
07d6d2b8
AM
11477 char *buf,
11478 int *bufsiz,
11479 const void *s390_todpreg,
11480 int size)
d7eeb400
MS
11481{
11482 char *note_name = "LINUX";
11483 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11484 note_name, NT_S390_TODPREG, s390_todpreg, size);
d7eeb400
MS
11485}
11486
11487char *
11488elfcore_write_s390_ctrs (bfd *abfd,
07d6d2b8
AM
11489 char *buf,
11490 int *bufsiz,
11491 const void *s390_ctrs,
11492 int size)
d7eeb400
MS
11493{
11494 char *note_name = "LINUX";
11495 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11496 note_name, NT_S390_CTRS, s390_ctrs, size);
d7eeb400
MS
11497}
11498
11499char *
11500elfcore_write_s390_prefix (bfd *abfd,
07d6d2b8
AM
11501 char *buf,
11502 int *bufsiz,
11503 const void *s390_prefix,
11504 int size)
d7eeb400
MS
11505{
11506 char *note_name = "LINUX";
11507 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11508 note_name, NT_S390_PREFIX, s390_prefix, size);
d7eeb400
MS
11509}
11510
355b81d9
UW
11511char *
11512elfcore_write_s390_last_break (bfd *abfd,
11513 char *buf,
11514 int *bufsiz,
11515 const void *s390_last_break,
11516 int size)
11517{
11518 char *note_name = "LINUX";
11519 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11520 note_name, NT_S390_LAST_BREAK,
355b81d9
UW
11521 s390_last_break, size);
11522}
11523
11524char *
11525elfcore_write_s390_system_call (bfd *abfd,
11526 char *buf,
11527 int *bufsiz,
11528 const void *s390_system_call,
11529 int size)
11530{
11531 char *note_name = "LINUX";
11532 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11533 note_name, NT_S390_SYSTEM_CALL,
355b81d9
UW
11534 s390_system_call, size);
11535}
11536
abb3f6cc
NC
11537char *
11538elfcore_write_s390_tdb (bfd *abfd,
11539 char *buf,
11540 int *bufsiz,
11541 const void *s390_tdb,
11542 int size)
11543{
11544 char *note_name = "LINUX";
11545 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11546 note_name, NT_S390_TDB, s390_tdb, size);
abb3f6cc
NC
11547}
11548
4ef9f41a
AA
11549char *
11550elfcore_write_s390_vxrs_low (bfd *abfd,
11551 char *buf,
11552 int *bufsiz,
11553 const void *s390_vxrs_low,
11554 int size)
11555{
11556 char *note_name = "LINUX";
11557 return elfcore_write_note (abfd, buf, bufsiz,
11558 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
11559}
11560
11561char *
11562elfcore_write_s390_vxrs_high (bfd *abfd,
11563 char *buf,
11564 int *bufsiz,
11565 const void *s390_vxrs_high,
11566 int size)
11567{
11568 char *note_name = "LINUX";
11569 return elfcore_write_note (abfd, buf, bufsiz,
11570 note_name, NT_S390_VXRS_HIGH,
11571 s390_vxrs_high, size);
11572}
11573
88ab90e8
AA
11574char *
11575elfcore_write_s390_gs_cb (bfd *abfd,
11576 char *buf,
11577 int *bufsiz,
11578 const void *s390_gs_cb,
11579 int size)
11580{
11581 char *note_name = "LINUX";
11582 return elfcore_write_note (abfd, buf, bufsiz,
11583 note_name, NT_S390_GS_CB,
11584 s390_gs_cb, size);
11585}
11586
11587char *
11588elfcore_write_s390_gs_bc (bfd *abfd,
11589 char *buf,
11590 int *bufsiz,
11591 const void *s390_gs_bc,
11592 int size)
11593{
11594 char *note_name = "LINUX";
11595 return elfcore_write_note (abfd, buf, bufsiz,
11596 note_name, NT_S390_GS_BC,
11597 s390_gs_bc, size);
11598}
11599
faa9a424
UW
11600char *
11601elfcore_write_arm_vfp (bfd *abfd,
11602 char *buf,
11603 int *bufsiz,
11604 const void *arm_vfp,
11605 int size)
11606{
11607 char *note_name = "LINUX";
11608 return elfcore_write_note (abfd, buf, bufsiz,
11609 note_name, NT_ARM_VFP, arm_vfp, size);
11610}
11611
652451f8
YZ
11612char *
11613elfcore_write_aarch_tls (bfd *abfd,
11614 char *buf,
11615 int *bufsiz,
11616 const void *aarch_tls,
11617 int size)
11618{
11619 char *note_name = "LINUX";
11620 return elfcore_write_note (abfd, buf, bufsiz,
11621 note_name, NT_ARM_TLS, aarch_tls, size);
11622}
11623
11624char *
11625elfcore_write_aarch_hw_break (bfd *abfd,
11626 char *buf,
11627 int *bufsiz,
11628 const void *aarch_hw_break,
11629 int size)
11630{
11631 char *note_name = "LINUX";
11632 return elfcore_write_note (abfd, buf, bufsiz,
11633 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
11634}
11635
11636char *
11637elfcore_write_aarch_hw_watch (bfd *abfd,
11638 char *buf,
11639 int *bufsiz,
11640 const void *aarch_hw_watch,
11641 int size)
11642{
11643 char *note_name = "LINUX";
11644 return elfcore_write_note (abfd, buf, bufsiz,
11645 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
11646}
11647
ad1cc4e4
AH
11648char *
11649elfcore_write_aarch_sve (bfd *abfd,
11650 char *buf,
11651 int *bufsiz,
11652 const void *aarch_sve,
11653 int size)
11654{
11655 char *note_name = "LINUX";
11656 return elfcore_write_note (abfd, buf, bufsiz,
11657 note_name, NT_ARM_SVE, aarch_sve, size);
11658}
11659
e6c3b5bf
AH
11660char *
11661elfcore_write_aarch_pauth (bfd *abfd,
11662 char *buf,
11663 int *bufsiz,
11664 const void *aarch_pauth,
11665 int size)
11666{
11667 char *note_name = "LINUX";
11668 return elfcore_write_note (abfd, buf, bufsiz,
11669 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
11670}
11671
bb864ac1
CES
11672char *
11673elfcore_write_register_note (bfd *abfd,
11674 char *buf,
11675 int *bufsiz,
11676 const char *section,
11677 const void *data,
11678 int size)
11679{
11680 if (strcmp (section, ".reg2") == 0)
11681 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
11682 if (strcmp (section, ".reg-xfp") == 0)
11683 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
4339cae0
L
11684 if (strcmp (section, ".reg-xstate") == 0)
11685 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
bb864ac1
CES
11686 if (strcmp (section, ".reg-ppc-vmx") == 0)
11687 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
89eeb0bc
LM
11688 if (strcmp (section, ".reg-ppc-vsx") == 0)
11689 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
cb2366c1
EBM
11690 if (strcmp (section, ".reg-ppc-tar") == 0)
11691 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
11692 if (strcmp (section, ".reg-ppc-ppr") == 0)
11693 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
11694 if (strcmp (section, ".reg-ppc-dscr") == 0)
11695 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
11696 if (strcmp (section, ".reg-ppc-ebb") == 0)
11697 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
11698 if (strcmp (section, ".reg-ppc-pmu") == 0)
11699 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
11700 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
11701 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
11702 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
11703 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
11704 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
11705 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
11706 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
11707 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
11708 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
11709 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
11710 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
11711 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
11712 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
11713 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
11714 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
11715 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
0675e188
UW
11716 if (strcmp (section, ".reg-s390-high-gprs") == 0)
11717 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
d7eeb400
MS
11718 if (strcmp (section, ".reg-s390-timer") == 0)
11719 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
11720 if (strcmp (section, ".reg-s390-todcmp") == 0)
11721 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
11722 if (strcmp (section, ".reg-s390-todpreg") == 0)
11723 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
11724 if (strcmp (section, ".reg-s390-ctrs") == 0)
11725 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
11726 if (strcmp (section, ".reg-s390-prefix") == 0)
11727 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
355b81d9
UW
11728 if (strcmp (section, ".reg-s390-last-break") == 0)
11729 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
11730 if (strcmp (section, ".reg-s390-system-call") == 0)
11731 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
abb3f6cc
NC
11732 if (strcmp (section, ".reg-s390-tdb") == 0)
11733 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
4ef9f41a
AA
11734 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
11735 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
11736 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
11737 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
88ab90e8
AA
11738 if (strcmp (section, ".reg-s390-gs-cb") == 0)
11739 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
11740 if (strcmp (section, ".reg-s390-gs-bc") == 0)
11741 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
faa9a424
UW
11742 if (strcmp (section, ".reg-arm-vfp") == 0)
11743 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
652451f8
YZ
11744 if (strcmp (section, ".reg-aarch-tls") == 0)
11745 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
11746 if (strcmp (section, ".reg-aarch-hw-break") == 0)
11747 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
11748 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
11749 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
ad1cc4e4
AH
11750 if (strcmp (section, ".reg-aarch-sve") == 0)
11751 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
e6c3b5bf
AH
11752 if (strcmp (section, ".reg-aarch-pauth") == 0)
11753 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
bb864ac1
CES
11754 return NULL;
11755}
11756
b34976b6 11757static bfd_boolean
276da9b3
L
11758elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
11759 size_t align)
252b5132 11760{
c044fabd 11761 char *p;
252b5132 11762
276da9b3
L
11763 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
11764 gABI specifies that PT_NOTE alignment should be aligned to 4
11765 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
11766 align is less than 4, we use 4 byte alignment. */
11767 if (align < 4)
11768 align = 4;
ef135d43
NC
11769 if (align != 4 && align != 8)
11770 return FALSE;
276da9b3 11771
252b5132
RH
11772 p = buf;
11773 while (p < buf + size)
11774 {
c044fabd 11775 Elf_External_Note *xnp = (Elf_External_Note *) p;
252b5132
RH
11776 Elf_Internal_Note in;
11777
baea7ef1
AM
11778 if (offsetof (Elf_External_Note, name) > buf - p + size)
11779 return FALSE;
11780
dc810e39 11781 in.type = H_GET_32 (abfd, xnp->type);
252b5132 11782
dc810e39 11783 in.namesz = H_GET_32 (abfd, xnp->namesz);
252b5132 11784 in.namedata = xnp->name;
baea7ef1
AM
11785 if (in.namesz > buf - in.namedata + size)
11786 return FALSE;
252b5132 11787
dc810e39 11788 in.descsz = H_GET_32 (abfd, xnp->descsz);
276da9b3 11789 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
252b5132 11790 in.descpos = offset + (in.descdata - buf);
baea7ef1
AM
11791 if (in.descsz != 0
11792 && (in.descdata >= buf + size
11793 || in.descsz > buf - in.descdata + size))
11794 return FALSE;
252b5132 11795
718175fa 11796 switch (bfd_get_format (abfd))
07d6d2b8 11797 {
718175fa
JK
11798 default:
11799 return TRUE;
11800
11801 case bfd_core:
f64e188b 11802 {
8acbedd6 11803#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
f64e188b 11804 struct
718175fa 11805 {
f64e188b 11806 const char * string;
8acbedd6 11807 size_t len;
f64e188b 11808 bfd_boolean (* func)(bfd *, Elf_Internal_Note *);
718175fa 11809 }
f64e188b 11810 grokers[] =
b15fa79e 11811 {
8acbedd6 11812 GROKER_ELEMENT ("", elfcore_grok_note),
aa1ed4a9 11813 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
8acbedd6
KS
11814 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
11815 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
11816 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
11817 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note)
f64e188b 11818 };
8acbedd6 11819#undef GROKER_ELEMENT
f64e188b
NC
11820 int i;
11821
11822 for (i = ARRAY_SIZE (grokers); i--;)
8acbedd6
KS
11823 {
11824 if (in.namesz >= grokers[i].len
11825 && strncmp (in.namedata, grokers[i].string,
11826 grokers[i].len) == 0)
11827 {
11828 if (! grokers[i].func (abfd, & in))
11829 return FALSE;
11830 break;
11831 }
11832 }
f64e188b
NC
11833 break;
11834 }
718175fa
JK
11835
11836 case bfd_object:
11837 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
11838 {
11839 if (! elfobj_grok_gnu_note (abfd, &in))
11840 return FALSE;
11841 }
e21e5835
NC
11842 else if (in.namesz == sizeof "stapsdt"
11843 && strcmp (in.namedata, "stapsdt") == 0)
11844 {
11845 if (! elfobj_grok_stapsdt_note (abfd, &in))
11846 return FALSE;
11847 }
718175fa 11848 break;
08a40648 11849 }
252b5132 11850
276da9b3 11851 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
252b5132
RH
11852 }
11853
718175fa
JK
11854 return TRUE;
11855}
11856
11857static bfd_boolean
276da9b3
L
11858elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
11859 size_t align)
718175fa
JK
11860{
11861 char *buf;
11862
957e1fc1 11863 if (size == 0 || (size + 1) == 0)
718175fa
JK
11864 return TRUE;
11865
11866 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
11867 return FALSE;
11868
f64e188b 11869 buf = (char *) bfd_malloc (size + 1);
718175fa
JK
11870 if (buf == NULL)
11871 return FALSE;
11872
f64e188b
NC
11873 /* PR 17512: file: ec08f814
11874 0-termintate the buffer so that string searches will not overflow. */
11875 buf[size] = 0;
11876
718175fa 11877 if (bfd_bread (buf, size, abfd) != size
276da9b3 11878 || !elf_parse_notes (abfd, buf, size, offset, align))
718175fa
JK
11879 {
11880 free (buf);
11881 return FALSE;
11882 }
11883
252b5132 11884 free (buf);
b34976b6 11885 return TRUE;
252b5132 11886}
98d8431c
JB
11887\f
11888/* Providing external access to the ELF program header table. */
11889
11890/* Return an upper bound on the number of bytes required to store a
11891 copy of ABFD's program header table entries. Return -1 if an error
11892 occurs; bfd_get_error will return an appropriate code. */
c044fabd 11893
98d8431c 11894long
217aa764 11895bfd_get_elf_phdr_upper_bound (bfd *abfd)
98d8431c
JB
11896{
11897 if (abfd->xvec->flavour != bfd_target_elf_flavour)
11898 {
11899 bfd_set_error (bfd_error_wrong_format);
11900 return -1;
11901 }
11902
936e320b 11903 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
98d8431c
JB
11904}
11905
98d8431c
JB
11906/* Copy ABFD's program header table entries to *PHDRS. The entries
11907 will be stored as an array of Elf_Internal_Phdr structures, as
11908 defined in include/elf/internal.h. To find out how large the
11909 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
11910
11911 Return the number of program header table entries read, or -1 if an
11912 error occurs; bfd_get_error will return an appropriate code. */
c044fabd 11913
98d8431c 11914int
217aa764 11915bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
98d8431c
JB
11916{
11917 int num_phdrs;
11918
11919 if (abfd->xvec->flavour != bfd_target_elf_flavour)
11920 {
11921 bfd_set_error (bfd_error_wrong_format);
11922 return -1;
11923 }
11924
11925 num_phdrs = elf_elfheader (abfd)->e_phnum;
01bcaf63
TT
11926 if (num_phdrs != 0)
11927 memcpy (phdrs, elf_tdata (abfd)->phdr,
11928 num_phdrs * sizeof (Elf_Internal_Phdr));
98d8431c
JB
11929
11930 return num_phdrs;
11931}
ae4221d7 11932
db6751f2 11933enum elf_reloc_type_class
7e612e98
AM
11934_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
11935 const asection *rel_sec ATTRIBUTE_UNUSED,
11936 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
db6751f2
JJ
11937{
11938 return reloc_class_normal;
11939}
f8df10f4 11940
47d9a591 11941/* For RELA architectures, return the relocation value for a
f8df10f4
JJ
11942 relocation against a local symbol. */
11943
11944bfd_vma
217aa764
AM
11945_bfd_elf_rela_local_sym (bfd *abfd,
11946 Elf_Internal_Sym *sym,
8517fae7 11947 asection **psec,
217aa764 11948 Elf_Internal_Rela *rel)
f8df10f4 11949{
8517fae7 11950 asection *sec = *psec;
f8df10f4
JJ
11951 bfd_vma relocation;
11952
6835821b
AM
11953 relocation = (sec->output_section->vma
11954 + sec->output_offset
11955 + sym->st_value);
f8df10f4 11956 if ((sec->flags & SEC_MERGE)
c629eae0 11957 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
dbaa2011 11958 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
f8df10f4 11959 {
f8df10f4 11960 rel->r_addend =
8517fae7 11961 _bfd_merged_section_offset (abfd, psec,
65765700 11962 elf_section_data (sec)->sec_info,
753731ee
AM
11963 sym->st_value + rel->r_addend);
11964 if (sec != *psec)
11965 {
11966 /* If we have changed the section, and our original section is
11967 marked with SEC_EXCLUDE, it means that the original
11968 SEC_MERGE section has been completely subsumed in some
11969 other SEC_MERGE section. In this case, we need to leave
11970 some info around for --emit-relocs. */
11971 if ((sec->flags & SEC_EXCLUDE) != 0)
11972 sec->kept_section = *psec;
11973 sec = *psec;
11974 }
8517fae7
AM
11975 rel->r_addend -= relocation;
11976 rel->r_addend += sec->output_section->vma + sec->output_offset;
f8df10f4
JJ
11977 }
11978 return relocation;
11979}
c629eae0
JJ
11980
11981bfd_vma
217aa764
AM
11982_bfd_elf_rel_local_sym (bfd *abfd,
11983 Elf_Internal_Sym *sym,
11984 asection **psec,
11985 bfd_vma addend)
47d9a591 11986{
c629eae0
JJ
11987 asection *sec = *psec;
11988
6835821b 11989 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
c629eae0
JJ
11990 return sym->st_value + addend;
11991
11992 return _bfd_merged_section_offset (abfd, psec,
65765700 11993 elf_section_data (sec)->sec_info,
753731ee 11994 sym->st_value + addend);
c629eae0
JJ
11995}
11996
37b01f6a
DG
11997/* Adjust an address within a section. Given OFFSET within SEC, return
11998 the new offset within the section, based upon changes made to the
11999 section. Returns -1 if the offset is now invalid.
12000 The offset (in abnd out) is in target sized bytes, however big a
12001 byte may be. */
12002
c629eae0 12003bfd_vma
217aa764 12004_bfd_elf_section_offset (bfd *abfd,
92e4ec35 12005 struct bfd_link_info *info,
217aa764
AM
12006 asection *sec,
12007 bfd_vma offset)
c629eae0 12008{
68bfbfcc 12009 switch (sec->sec_info_type)
65765700 12010 {
dbaa2011 12011 case SEC_INFO_TYPE_STABS:
eea6121a
AM
12012 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
12013 offset);
dbaa2011 12014 case SEC_INFO_TYPE_EH_FRAME:
92e4ec35 12015 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
37b01f6a 12016
65765700 12017 default:
310fd250
L
12018 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
12019 {
37b01f6a 12020 /* Reverse the offset. */
310fd250
L
12021 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12022 bfd_size_type address_size = bed->s->arch_size / 8;
37b01f6a
DG
12023
12024 /* address_size and sec->size are in octets. Convert
12025 to bytes before subtracting the original offset. */
12026 offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset;
310fd250 12027 }
65765700
JJ
12028 return offset;
12029 }
c629eae0 12030}
3333a7c3
RM
12031\f
12032/* Create a new BFD as if by bfd_openr. Rather than opening a file,
12033 reconstruct an ELF file by reading the segments out of remote memory
12034 based on the ELF file header at EHDR_VMA and the ELF program headers it
12035 points to. If not null, *LOADBASEP is filled in with the difference
12036 between the VMAs from which the segments were read, and the VMAs the
12037 file headers (and hence BFD's idea of each section's VMA) put them at.
12038
12039 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12040 remote memory at target address VMA into the local buffer at MYADDR; it
12041 should return zero on success or an `errno' code on failure. TEMPL must
12042 be a BFD for an ELF target with the word size and byte order found in
12043 the remote memory. */
12044
12045bfd *
217aa764
AM
12046bfd_elf_bfd_from_remote_memory
12047 (bfd *templ,
12048 bfd_vma ehdr_vma,
f0a5d95a 12049 bfd_size_type size,
217aa764 12050 bfd_vma *loadbasep,
fe78531d 12051 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
3333a7c3
RM
12052{
12053 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
5979d6b6 12054 (templ, ehdr_vma, size, loadbasep, target_read_memory);
3333a7c3 12055}
4c45e5c9
JJ
12056\f
12057long
c9727e01
AM
12058_bfd_elf_get_synthetic_symtab (bfd *abfd,
12059 long symcount ATTRIBUTE_UNUSED,
12060 asymbol **syms ATTRIBUTE_UNUSED,
8615f3f2 12061 long dynsymcount,
c9727e01
AM
12062 asymbol **dynsyms,
12063 asymbol **ret)
4c45e5c9
JJ
12064{
12065 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12066 asection *relplt;
12067 asymbol *s;
12068 const char *relplt_name;
12069 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
12070 arelent *p;
12071 long count, i, n;
12072 size_t size;
12073 Elf_Internal_Shdr *hdr;
12074 char *names;
12075 asection *plt;
12076
8615f3f2
AM
12077 *ret = NULL;
12078
90e3cdf2
JJ
12079 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
12080 return 0;
12081
8615f3f2
AM
12082 if (dynsymcount <= 0)
12083 return 0;
12084
4c45e5c9
JJ
12085 if (!bed->plt_sym_val)
12086 return 0;
12087
12088 relplt_name = bed->relplt_name;
12089 if (relplt_name == NULL)
d35fd659 12090 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
4c45e5c9
JJ
12091 relplt = bfd_get_section_by_name (abfd, relplt_name);
12092 if (relplt == NULL)
12093 return 0;
12094
12095 hdr = &elf_section_data (relplt)->this_hdr;
12096 if (hdr->sh_link != elf_dynsymtab (abfd)
12097 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
12098 return 0;
12099
12100 plt = bfd_get_section_by_name (abfd, ".plt");
12101 if (plt == NULL)
12102 return 0;
12103
12104 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
c9727e01 12105 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
4c45e5c9
JJ
12106 return -1;
12107
eea6121a 12108 count = relplt->size / hdr->sh_entsize;
4c45e5c9
JJ
12109 size = count * sizeof (asymbol);
12110 p = relplt->relocation;
cb53bf42 12111 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
041de40d
AM
12112 {
12113 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
12114 if (p->addend != 0)
12115 {
12116#ifdef BFD64
12117 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
12118#else
12119 size += sizeof ("+0x") - 1 + 8;
12120#endif
12121 }
12122 }
4c45e5c9 12123
a50b1753 12124 s = *ret = (asymbol *) bfd_malloc (size);
4c45e5c9
JJ
12125 if (s == NULL)
12126 return -1;
12127
12128 names = (char *) (s + count);
12129 p = relplt->relocation;
12130 n = 0;
cb53bf42 12131 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
4c45e5c9
JJ
12132 {
12133 size_t len;
12134 bfd_vma addr;
12135
12136 addr = bed->plt_sym_val (i, plt, p);
12137 if (addr == (bfd_vma) -1)
12138 continue;
12139
12140 *s = **p->sym_ptr_ptr;
65a7a66f
AM
12141 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12142 we are defining a symbol, ensure one of them is set. */
12143 if ((s->flags & BSF_LOCAL) == 0)
12144 s->flags |= BSF_GLOBAL;
6ba2a415 12145 s->flags |= BSF_SYNTHETIC;
4c45e5c9
JJ
12146 s->section = plt;
12147 s->value = addr - plt->vma;
12148 s->name = names;
8f39ba8e 12149 s->udata.p = NULL;
4c45e5c9
JJ
12150 len = strlen ((*p->sym_ptr_ptr)->name);
12151 memcpy (names, (*p->sym_ptr_ptr)->name, len);
12152 names += len;
041de40d
AM
12153 if (p->addend != 0)
12154 {
1d770845 12155 char buf[30], *a;
d324f6d6 12156
041de40d
AM
12157 memcpy (names, "+0x", sizeof ("+0x") - 1);
12158 names += sizeof ("+0x") - 1;
1d770845
L
12159 bfd_sprintf_vma (abfd, buf, p->addend);
12160 for (a = buf; *a == '0'; ++a)
12161 ;
12162 len = strlen (a);
12163 memcpy (names, a, len);
12164 names += len;
041de40d 12165 }
4c45e5c9
JJ
12166 memcpy (names, "@plt", sizeof ("@plt"));
12167 names += sizeof ("@plt");
8f39ba8e 12168 ++s, ++n;
4c45e5c9
JJ
12169 }
12170
12171 return n;
12172}
3d7f7666 12173
821e6ff6
AM
12174/* It is only used by x86-64 so far.
12175 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
7eacd66b
AM
12176 but current usage would allow all of _bfd_std_section to be zero. */
12177static const asymbol lcomm_sym
12178 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
3b22753a 12179asection _bfd_elf_large_com_section
7eacd66b 12180 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
821e6ff6 12181 "LARGE_COMMON", 0, SEC_IS_COMMON);
ecca9871 12182
d1036acb 12183void
06f44071
AM
12184_bfd_elf_post_process_headers (bfd *abfd ATTRIBUTE_UNUSED,
12185 struct bfd_link_info *info ATTRIBUTE_UNUSED)
d1036acb 12186{
06f44071
AM
12187}
12188
cc364be6
AM
12189bfd_boolean
12190_bfd_elf_final_write_processing (bfd *abfd)
06f44071
AM
12191{
12192 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
d1036acb
L
12193
12194 i_ehdrp = elf_elfheader (abfd);
12195
06f44071
AM
12196 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12197 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
d8045f23 12198
df3a023b
AM
12199 /* Set the osabi field to ELFOSABI_GNU if the binary contains
12200 SHF_GNU_MBIND sections or symbols of STT_GNU_IFUNC type or
12201 STB_GNU_UNIQUE binding. */
cc364be6
AM
12202 if (elf_tdata (abfd)->has_gnu_osabi != 0)
12203 {
12204 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12205 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
12206 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
12207 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
12208 {
12209 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
12210 _bfd_error_handler (_("GNU_MBIND section is unsupported"));
12211 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
12212 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is unsupported"));
12213 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
12214 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is unsupported"));
12215 bfd_set_error (bfd_error_bad_value);
12216 return FALSE;
12217 }
12218 }
12219 return TRUE;
d1036acb 12220}
fcb93ecf
PB
12221
12222
12223/* Return TRUE for ELF symbol types that represent functions.
12224 This is the default version of this function, which is sufficient for
d8045f23 12225 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
fcb93ecf
PB
12226
12227bfd_boolean
12228_bfd_elf_is_function_type (unsigned int type)
12229{
d8045f23
NC
12230 return (type == STT_FUNC
12231 || type == STT_GNU_IFUNC);
fcb93ecf 12232}
9f296da3 12233
aef36ac1
AM
12234/* If the ELF symbol SYM might be a function in SEC, return the
12235 function size and set *CODE_OFF to the function's entry point,
12236 otherwise return zero. */
9f296da3 12237
aef36ac1
AM
12238bfd_size_type
12239_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
12240 bfd_vma *code_off)
9f296da3 12241{
aef36ac1
AM
12242 bfd_size_type size;
12243
ff9e0f5b 12244 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
aef36ac1
AM
12245 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
12246 || sym->section != sec)
12247 return 0;
ff9e0f5b 12248
ff9e0f5b 12249 *code_off = sym->value;
aef36ac1
AM
12250 size = 0;
12251 if (!(sym->flags & BSF_SYNTHETIC))
12252 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
12253 if (size == 0)
12254 size = 1;
12255 return size;
9f296da3 12256}