]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf.c
Tidy ld/plugin.c
[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
72a80a16 278static char *
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')
550 name = bfd_section_name (abfd, sym_sec);
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
f6fe1ccd
L
977static char *
978convert_debug_to_zdebug (bfd *abfd, const char *name)
979{
980 unsigned int len = strlen (name);
981 char *new_name = bfd_alloc (abfd, len + 2);
982 if (new_name == NULL)
983 return NULL;
984 new_name[0] = '.';
985 new_name[1] = 'z';
986 memcpy (new_name + 2, name + 1, len);
987 return new_name;
988}
989
990static char *
991convert_zdebug_to_debug (bfd *abfd, const char *name)
992{
993 unsigned int len = strlen (name);
994 char *new_name = bfd_alloc (abfd, len);
995 if (new_name == NULL)
996 return NULL;
997 new_name[0] = '.';
998 memcpy (new_name + 1, name + 2, len - 1);
999 return new_name;
1000}
1001
cc5277b1
ML
1002/* This a copy of lto_section defined in GCC (lto-streamer.h). */
1003
1004struct lto_section
1005{
1006 int16_t major_version;
1007 int16_t minor_version;
1008 unsigned char slim_object;
1009
1010 /* Flags is a private field that is not defined publicly. */
1011 uint16_t flags;
1012};
1013
252b5132
RH
1014/* Make a BFD section from an ELF section. We store a pointer to the
1015 BFD section in the bfd_section field of the header. */
1016
b34976b6 1017bfd_boolean
217aa764
AM
1018_bfd_elf_make_section_from_shdr (bfd *abfd,
1019 Elf_Internal_Shdr *hdr,
6dc132d9
L
1020 const char *name,
1021 int shindex)
252b5132
RH
1022{
1023 asection *newsect;
1024 flagword flags;
9c5bfbb7 1025 const struct elf_backend_data *bed;
252b5132
RH
1026
1027 if (hdr->bfd_section != NULL)
4e011fb5 1028 return TRUE;
252b5132
RH
1029
1030 newsect = bfd_make_section_anyway (abfd, name);
1031 if (newsect == NULL)
b34976b6 1032 return FALSE;
252b5132 1033
1829f4b2
AM
1034 hdr->bfd_section = newsect;
1035 elf_section_data (newsect)->this_hdr = *hdr;
6dc132d9 1036 elf_section_data (newsect)->this_idx = shindex;
1829f4b2 1037
2f89ff8d
L
1038 /* Always use the real type/flags. */
1039 elf_section_type (newsect) = hdr->sh_type;
1040 elf_section_flags (newsect) = hdr->sh_flags;
1041
252b5132
RH
1042 newsect->filepos = hdr->sh_offset;
1043
1044 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
1045 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
1046 || ! bfd_set_section_alignment (abfd, newsect,
72de5009 1047 bfd_log2 (hdr->sh_addralign)))
b34976b6 1048 return FALSE;
252b5132
RH
1049
1050 flags = SEC_NO_FLAGS;
1051 if (hdr->sh_type != SHT_NOBITS)
1052 flags |= SEC_HAS_CONTENTS;
dbb410c3 1053 if (hdr->sh_type == SHT_GROUP)
7bdf4127 1054 flags |= SEC_GROUP;
252b5132
RH
1055 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1056 {
1057 flags |= SEC_ALLOC;
1058 if (hdr->sh_type != SHT_NOBITS)
1059 flags |= SEC_LOAD;
1060 }
1061 if ((hdr->sh_flags & SHF_WRITE) == 0)
1062 flags |= SEC_READONLY;
1063 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1064 flags |= SEC_CODE;
1065 else if ((flags & SEC_LOAD) != 0)
1066 flags |= SEC_DATA;
f5fa8ca2
JJ
1067 if ((hdr->sh_flags & SHF_MERGE) != 0)
1068 {
1069 flags |= SEC_MERGE;
1070 newsect->entsize = hdr->sh_entsize;
f5fa8ca2 1071 }
84865015
NC
1072 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1073 flags |= SEC_STRINGS;
dbb410c3
AM
1074 if (hdr->sh_flags & SHF_GROUP)
1075 if (!setup_group (abfd, hdr, newsect))
b34976b6 1076 return FALSE;
13ae64f3
JJ
1077 if ((hdr->sh_flags & SHF_TLS) != 0)
1078 flags |= SEC_THREAD_LOCAL;
18ae9cc1
L
1079 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1080 flags |= SEC_EXCLUDE;
252b5132 1081
df3a023b
AM
1082 switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1083 {
1084 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1085 but binutils as of 2019-07-23 did not set the EI_OSABI header
1086 byte. */
1087 case ELFOSABI_NONE:
1088 case ELFOSABI_GNU:
1089 case ELFOSABI_FREEBSD:
1090 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1091 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1092 break;
1093 }
1094
3d2b39cf 1095 if ((flags & SEC_ALLOC) == 0)
7a6cc5fb 1096 {
3d2b39cf
L
1097 /* The debugging sections appear to be recognized only by name,
1098 not any sort of flag. Their SEC_ALLOC bits are cleared. */
3d2b39cf
L
1099 if (name [0] == '.')
1100 {
f073ced3
AM
1101 const char *p;
1102 int n;
1103 if (name[1] == 'd')
1104 p = ".debug", n = 6;
1105 else if (name[1] == 'g' && name[2] == 'n')
1106 p = ".gnu.linkonce.wi.", n = 17;
1107 else if (name[1] == 'g' && name[2] == 'd')
1108 p = ".gdb_index", n = 11; /* yes we really do mean 11. */
1109 else if (name[1] == 'l')
1110 p = ".line", n = 5;
1111 else if (name[1] == 's')
1112 p = ".stab", n = 5;
1113 else if (name[1] == 'z')
1114 p = ".zdebug", n = 7;
1115 else
1116 p = NULL, n = 0;
1117 if (p != NULL && strncmp (name, p, n) == 0)
3d2b39cf
L
1118 flags |= SEC_DEBUGGING;
1119 }
1120 }
252b5132
RH
1121
1122 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1123 only link a single copy of the section. This is used to support
1124 g++. g++ will emit each template expansion in its own section.
1125 The symbols will be defined as weak, so that multiple definitions
1126 are permitted. The GNU linker extension is to actually discard
1127 all but one of the sections. */
0112cd26 1128 if (CONST_STRNEQ (name, ".gnu.linkonce")
b885599b 1129 && elf_next_in_group (newsect) == NULL)
252b5132
RH
1130 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1131
fa152c49
JW
1132 bed = get_elf_backend_data (abfd);
1133 if (bed->elf_backend_section_flags)
1134 if (! bed->elf_backend_section_flags (&flags, hdr))
b34976b6 1135 return FALSE;
fa152c49 1136
252b5132 1137 if (! bfd_set_section_flags (abfd, newsect, flags))
b34976b6 1138 return FALSE;
252b5132 1139
718175fa
JK
1140 /* We do not parse the PT_NOTE segments as we are interested even in the
1141 separate debug info files which may have the segments offsets corrupted.
1142 PT_NOTEs from the core files are currently not parsed using BFD. */
1143 if (hdr->sh_type == SHT_NOTE)
1144 {
baea7ef1 1145 bfd_byte *contents;
718175fa 1146
baea7ef1 1147 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
718175fa
JK
1148 return FALSE;
1149
276da9b3
L
1150 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1151 hdr->sh_offset, hdr->sh_addralign);
718175fa
JK
1152 free (contents);
1153 }
1154
252b5132
RH
1155 if ((flags & SEC_ALLOC) != 0)
1156 {
1157 Elf_Internal_Phdr *phdr;
6ffd7900
AM
1158 unsigned int i, nload;
1159
1160 /* Some ELF linkers produce binaries with all the program header
1161 p_paddr fields zero. If we have such a binary with more than
1162 one PT_LOAD header, then leave the section lma equal to vma
1163 so that we don't create sections with overlapping lma. */
1164 phdr = elf_tdata (abfd)->phdr;
1165 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1166 if (phdr->p_paddr != 0)
1167 break;
1168 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1169 ++nload;
1170 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1171 return TRUE;
252b5132 1172
252b5132
RH
1173 phdr = elf_tdata (abfd)->phdr;
1174 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1175 {
86b2281f
AM
1176 if (((phdr->p_type == PT_LOAD
1177 && (hdr->sh_flags & SHF_TLS) == 0)
1178 || phdr->p_type == PT_TLS)
9a83a553 1179 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
252b5132 1180 {
88967714
AM
1181 if ((flags & SEC_LOAD) == 0)
1182 newsect->lma = (phdr->p_paddr
1183 + hdr->sh_addr - phdr->p_vaddr);
1184 else
1185 /* We used to use the same adjustment for SEC_LOAD
1186 sections, but that doesn't work if the segment
1187 is packed with code from multiple VMAs.
1188 Instead we calculate the section LMA based on
1189 the segment LMA. It is assumed that the
1190 segment will contain sections with contiguous
1191 LMAs, even if the VMAs are not. */
1192 newsect->lma = (phdr->p_paddr
1193 + hdr->sh_offset - phdr->p_offset);
1194
1195 /* With contiguous segments, we can't tell from file
1196 offsets whether a section with zero size should
1197 be placed at the end of one segment or the
1198 beginning of the next. Decide based on vaddr. */
1199 if (hdr->sh_addr >= phdr->p_vaddr
1200 && (hdr->sh_addr + hdr->sh_size
1201 <= phdr->p_vaddr + phdr->p_memsz))
1202 break;
252b5132
RH
1203 }
1204 }
1205 }
1206
4a114e3e
L
1207 /* Compress/decompress DWARF debug sections with names: .debug_* and
1208 .zdebug_*, after the section flags is set. */
1209 if ((flags & SEC_DEBUGGING)
1210 && ((name[1] == 'd' && name[6] == '_')
1211 || (name[1] == 'z' && name[7] == '_')))
1212 {
1213 enum { nothing, compress, decompress } action = nothing;
151411f8 1214 int compression_header_size;
dab394de 1215 bfd_size_type uncompressed_size;
4207142d 1216 unsigned int uncompressed_align_power;
151411f8
L
1217 bfd_boolean compressed
1218 = bfd_is_section_compressed_with_header (abfd, newsect,
dab394de 1219 &compression_header_size,
4207142d
MW
1220 &uncompressed_size,
1221 &uncompressed_align_power);
151411f8 1222 if (compressed)
4a114e3e
L
1223 {
1224 /* Compressed section. Check if we should decompress. */
1225 if ((abfd->flags & BFD_DECOMPRESS))
1226 action = decompress;
1227 }
151411f8
L
1228
1229 /* Compress the uncompressed section or convert from/to .zdebug*
1230 section. Check if we should compress. */
1231 if (action == nothing)
4a114e3e 1232 {
151411f8
L
1233 if (newsect->size != 0
1234 && (abfd->flags & BFD_COMPRESS)
1235 && compression_header_size >= 0
dab394de 1236 && uncompressed_size > 0
151411f8
L
1237 && (!compressed
1238 || ((compression_header_size > 0)
1239 != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
4a114e3e 1240 action = compress;
151411f8
L
1241 else
1242 return TRUE;
4a114e3e
L
1243 }
1244
151411f8 1245 if (action == compress)
4a114e3e 1246 {
4a114e3e
L
1247 if (!bfd_init_section_compress_status (abfd, newsect))
1248 {
4eca0228 1249 _bfd_error_handler
695344c0 1250 /* xgettext:c-format */
871b3ab2 1251 (_("%pB: unable to initialize compress status for section %s"),
4a114e3e
L
1252 abfd, name);
1253 return FALSE;
1254 }
151411f8
L
1255 }
1256 else
1257 {
4a114e3e
L
1258 if (!bfd_init_section_decompress_status (abfd, newsect))
1259 {
4eca0228 1260 _bfd_error_handler
695344c0 1261 /* xgettext:c-format */
871b3ab2 1262 (_("%pB: unable to initialize decompress status for section %s"),
4a114e3e
L
1263 abfd, name);
1264 return FALSE;
1265 }
151411f8
L
1266 }
1267
f6fe1ccd 1268 if (abfd->is_linker_input)
151411f8 1269 {
f6fe1ccd
L
1270 if (name[1] == 'z'
1271 && (action == decompress
1272 || (action == compress
1273 && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
4e011fb5 1274 {
f6fe1ccd
L
1275 /* Convert section name from .zdebug_* to .debug_* so
1276 that linker will consider this section as a debug
1277 section. */
1278 char *new_name = convert_zdebug_to_debug (abfd, name);
151411f8
L
1279 if (new_name == NULL)
1280 return FALSE;
f6fe1ccd 1281 bfd_rename_section (abfd, newsect, new_name);
151411f8 1282 }
4a114e3e 1283 }
f6fe1ccd
L
1284 else
1285 /* For objdump, don't rename the section. For objcopy, delay
1286 section rename to elf_fake_sections. */
1287 newsect->flags |= SEC_ELF_RENAME;
4a114e3e
L
1288 }
1289
cc5277b1
ML
1290 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1291 section. */
1292 const char *lto_section_name = ".gnu.lto_.lto.";
1293 if (strncmp (name, lto_section_name, strlen (lto_section_name)) == 0)
1294 {
1295 struct lto_section lsection;
1296 if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
1297 sizeof (struct lto_section)))
1298 abfd->lto_slim_object = lsection.slim_object;
1299 }
1300
b34976b6 1301 return TRUE;
252b5132
RH
1302}
1303
84865015
NC
1304const char *const bfd_elf_section_type_names[] =
1305{
252b5132
RH
1306 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1307 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1308 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1309};
1310
1049f94e 1311/* ELF relocs are against symbols. If we are producing relocatable
252b5132
RH
1312 output, and the reloc is against an external symbol, and nothing
1313 has given us any additional addend, the resulting reloc will also
1314 be against the same symbol. In such a case, we don't want to
1315 change anything about the way the reloc is handled, since it will
1316 all be done at final link time. Rather than put special case code
1317 into bfd_perform_relocation, all the reloc types use this howto
1318 function. It just short circuits the reloc if producing
1049f94e 1319 relocatable output against an external symbol. */
252b5132 1320
252b5132 1321bfd_reloc_status_type
217aa764
AM
1322bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1323 arelent *reloc_entry,
1324 asymbol *symbol,
1325 void *data ATTRIBUTE_UNUSED,
1326 asection *input_section,
1327 bfd *output_bfd,
1328 char **error_message ATTRIBUTE_UNUSED)
1329{
1330 if (output_bfd != NULL
252b5132
RH
1331 && (symbol->flags & BSF_SECTION_SYM) == 0
1332 && (! reloc_entry->howto->partial_inplace
1333 || reloc_entry->addend == 0))
1334 {
1335 reloc_entry->address += input_section->output_offset;
1336 return bfd_reloc_ok;
1337 }
1338
1339 return bfd_reloc_continue;
1340}
1341\f
84865015
NC
1342/* Returns TRUE if section A matches section B.
1343 Names, addresses and links may be different, but everything else
1344 should be the same. */
1345
1346static bfd_boolean
5522f910
NC
1347section_match (const Elf_Internal_Shdr * a,
1348 const Elf_Internal_Shdr * b)
84865015 1349{
ac85e67c
AM
1350 if (a->sh_type != b->sh_type
1351 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1352 || a->sh_addralign != b->sh_addralign
1353 || a->sh_entsize != b->sh_entsize)
1354 return FALSE;
1355 if (a->sh_type == SHT_SYMTAB
1356 || a->sh_type == SHT_STRTAB)
1357 return TRUE;
1358 return a->sh_size == b->sh_size;
84865015
NC
1359}
1360
1361/* Find a section in OBFD that has the same characteristics
1362 as IHEADER. Return the index of this section or SHN_UNDEF if
1363 none can be found. Check's section HINT first, as this is likely
1364 to be the correct section. */
1365
1366static unsigned int
5cc4ca83
ST
1367find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1368 const unsigned int hint)
84865015
NC
1369{
1370 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1371 unsigned int i;
1372
a55c9876
NC
1373 BFD_ASSERT (iheader != NULL);
1374
1375 /* See PR 20922 for a reproducer of the NULL test. */
5cc4ca83
ST
1376 if (hint < elf_numsections (obfd)
1377 && oheaders[hint] != NULL
a55c9876 1378 && section_match (oheaders[hint], iheader))
84865015
NC
1379 return hint;
1380
1381 for (i = 1; i < elf_numsections (obfd); i++)
1382 {
1383 Elf_Internal_Shdr * oheader = oheaders[i];
1384
a55c9876
NC
1385 if (oheader == NULL)
1386 continue;
84865015
NC
1387 if (section_match (oheader, iheader))
1388 /* FIXME: Do we care if there is a potential for
1389 multiple matches ? */
1390 return i;
1391 }
1392
1393 return SHN_UNDEF;
1394}
1395
5522f910
NC
1396/* PR 19938: Attempt to set the ELF section header fields of an OS or
1397 Processor specific section, based upon a matching input section.
1398 Returns TRUE upon success, FALSE otherwise. */
07d6d2b8 1399
5522f910
NC
1400static bfd_boolean
1401copy_special_section_fields (const bfd *ibfd,
1402 bfd *obfd,
1403 const Elf_Internal_Shdr *iheader,
1404 Elf_Internal_Shdr *oheader,
1405 const unsigned int secnum)
1406{
1407 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1408 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1409 bfd_boolean changed = FALSE;
1410 unsigned int sh_link;
1411
1412 if (oheader->sh_type == SHT_NOBITS)
1413 {
1414 /* This is a feature for objcopy --only-keep-debug:
1415 When a section's type is changed to NOBITS, we preserve
1416 the sh_link and sh_info fields so that they can be
1417 matched up with the original.
1418
1419 Note: Strictly speaking these assignments are wrong.
1420 The sh_link and sh_info fields should point to the
1421 relevent sections in the output BFD, which may not be in
1422 the same location as they were in the input BFD. But
1423 the whole point of this action is to preserve the
1424 original values of the sh_link and sh_info fields, so
1425 that they can be matched up with the section headers in
1426 the original file. So strictly speaking we may be
1427 creating an invalid ELF file, but it is only for a file
1428 that just contains debug info and only for sections
1429 without any contents. */
1430 if (oheader->sh_link == 0)
1431 oheader->sh_link = iheader->sh_link;
1432 if (oheader->sh_info == 0)
1433 oheader->sh_info = iheader->sh_info;
1434 return TRUE;
1435 }
1436
1437 /* Allow the target a chance to decide how these fields should be set. */
1438 if (bed->elf_backend_copy_special_section_fields != NULL
1439 && bed->elf_backend_copy_special_section_fields
1440 (ibfd, obfd, iheader, oheader))
1441 return TRUE;
1442
1443 /* We have an iheader which might match oheader, and which has non-zero
1444 sh_info and/or sh_link fields. Attempt to follow those links and find
1445 the section in the output bfd which corresponds to the linked section
1446 in the input bfd. */
1447 if (iheader->sh_link != SHN_UNDEF)
1448 {
4f3ca05b
NC
1449 /* See PR 20931 for a reproducer. */
1450 if (iheader->sh_link >= elf_numsections (ibfd))
1451 {
76cfced5 1452 _bfd_error_handler
4f3ca05b 1453 /* xgettext:c-format */
9793eb77 1454 (_("%pB: invalid sh_link field (%d) in section number %d"),
4f3ca05b
NC
1455 ibfd, iheader->sh_link, secnum);
1456 return FALSE;
1457 }
1458
5522f910
NC
1459 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1460 if (sh_link != SHN_UNDEF)
1461 {
1462 oheader->sh_link = sh_link;
1463 changed = TRUE;
1464 }
1465 else
1466 /* FIXME: Should we install iheader->sh_link
1467 if we could not find a match ? */
76cfced5 1468 _bfd_error_handler
695344c0 1469 /* xgettext:c-format */
9793eb77 1470 (_("%pB: failed to find link section for section %d"), obfd, secnum);
5522f910
NC
1471 }
1472
1473 if (iheader->sh_info)
1474 {
1475 /* The sh_info field can hold arbitrary information, but if the
1476 SHF_LINK_INFO flag is set then it should be interpreted as a
1477 section index. */
1478 if (iheader->sh_flags & SHF_INFO_LINK)
1479 {
1480 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1481 iheader->sh_info);
1482 if (sh_link != SHN_UNDEF)
1483 oheader->sh_flags |= SHF_INFO_LINK;
1484 }
1485 else
1486 /* No idea what it means - just copy it. */
1487 sh_link = iheader->sh_info;
1488
1489 if (sh_link != SHN_UNDEF)
1490 {
1491 oheader->sh_info = sh_link;
1492 changed = TRUE;
1493 }
1494 else
76cfced5 1495 _bfd_error_handler
695344c0 1496 /* xgettext:c-format */
9793eb77 1497 (_("%pB: failed to find info section for section %d"), obfd, secnum);
5522f910
NC
1498 }
1499
1500 return changed;
1501}
07d6d2b8 1502
0ac4564e
L
1503/* Copy the program header and other data from one object module to
1504 another. */
252b5132 1505
b34976b6 1506bfd_boolean
217aa764 1507_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
2d502050 1508{
5522f910
NC
1509 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1510 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1511 const struct elf_backend_data *bed;
84865015
NC
1512 unsigned int i;
1513
2d502050 1514 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
84865015 1515 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 1516 return TRUE;
2d502050 1517
57b828ef
L
1518 if (!elf_flags_init (obfd))
1519 {
1520 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1521 elf_flags_init (obfd) = TRUE;
1522 }
2d502050 1523
0ac4564e 1524 elf_gp (obfd) = elf_gp (ibfd);
57b828ef
L
1525
1526 /* Also copy the EI_OSABI field. */
1527 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1528 elf_elfheader (ibfd)->e_ident[EI_OSABI];
104d59d1 1529
5522f910
NC
1530 /* If set, copy the EI_ABIVERSION field. */
1531 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1532 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1533 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
07d6d2b8 1534
104d59d1
JM
1535 /* Copy object attributes. */
1536 _bfd_elf_copy_obj_attributes (ibfd, obfd);
63b9bbb7 1537
84865015
NC
1538 if (iheaders == NULL || oheaders == NULL)
1539 return TRUE;
63b9bbb7 1540
5522f910
NC
1541 bed = get_elf_backend_data (obfd);
1542
1543 /* Possibly copy other fields in the section header. */
84865015 1544 for (i = 1; i < elf_numsections (obfd); i++)
63b9bbb7 1545 {
84865015
NC
1546 unsigned int j;
1547 Elf_Internal_Shdr * oheader = oheaders[i];
63b9bbb7 1548
5522f910
NC
1549 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1550 because of a special case need for generating separate debug info
1551 files. See below for more details. */
84865015
NC
1552 if (oheader == NULL
1553 || (oheader->sh_type != SHT_NOBITS
5522f910
NC
1554 && oheader->sh_type < SHT_LOOS))
1555 continue;
1556
1557 /* Ignore empty sections, and sections whose
1558 fields have already been initialised. */
1559 if (oheader->sh_size == 0
84865015
NC
1560 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1561 continue;
63b9bbb7 1562
84865015 1563 /* Scan for the matching section in the input bfd.
5522f910
NC
1564 First we try for a direct mapping between the input and output sections. */
1565 for (j = 1; j < elf_numsections (ibfd); j++)
1566 {
1567 const Elf_Internal_Shdr * iheader = iheaders[j];
1568
1569 if (iheader == NULL)
1570 continue;
1571
1572 if (oheader->bfd_section != NULL
1573 && iheader->bfd_section != NULL
1574 && iheader->bfd_section->output_section != NULL
1575 && iheader->bfd_section->output_section == oheader->bfd_section)
1576 {
1577 /* We have found a connection from the input section to the
1578 output section. Attempt to copy the header fields. If
1579 this fails then do not try any further sections - there
1580 should only be a one-to-one mapping between input and output. */
1581 if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1582 j = elf_numsections (ibfd);
1583 break;
1584 }
1585 }
1586
1587 if (j < elf_numsections (ibfd))
1588 continue;
1589
1590 /* That failed. So try to deduce the corresponding input section.
84865015
NC
1591 Unfortunately we cannot compare names as the output string table
1592 is empty, so instead we check size, address and type. */
1593 for (j = 1; j < elf_numsections (ibfd); j++)
1594 {
5522f910 1595 const Elf_Internal_Shdr * iheader = iheaders[j];
84865015 1596
5522f910
NC
1597 if (iheader == NULL)
1598 continue;
1599
1600 /* Try matching fields in the input section's header.
1601 Since --only-keep-debug turns all non-debug sections into
84865015
NC
1602 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1603 input type. */
1604 if ((oheader->sh_type == SHT_NOBITS
1605 || iheader->sh_type == oheader->sh_type)
5522f910
NC
1606 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1607 == (oheader->sh_flags & ~ SHF_INFO_LINK)
84865015
NC
1608 && iheader->sh_addralign == oheader->sh_addralign
1609 && iheader->sh_entsize == oheader->sh_entsize
1610 && iheader->sh_size == oheader->sh_size
1611 && iheader->sh_addr == oheader->sh_addr
1612 && (iheader->sh_info != oheader->sh_info
1613 || iheader->sh_link != oheader->sh_link))
63b9bbb7 1614 {
5522f910
NC
1615 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1616 break;
63b9bbb7
NC
1617 }
1618 }
5522f910
NC
1619
1620 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1621 {
1622 /* Final attempt. Call the backend copy function
1623 with a NULL input section. */
1624 if (bed->elf_backend_copy_special_section_fields != NULL)
1625 bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader);
1626 }
63b9bbb7
NC
1627 }
1628
b34976b6 1629 return TRUE;
2d502050
L
1630}
1631
cedc298e
L
1632static const char *
1633get_segment_type (unsigned int p_type)
1634{
1635 const char *pt;
1636 switch (p_type)
1637 {
1638 case PT_NULL: pt = "NULL"; break;
1639 case PT_LOAD: pt = "LOAD"; break;
1640 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1641 case PT_INTERP: pt = "INTERP"; break;
1642 case PT_NOTE: pt = "NOTE"; break;
1643 case PT_SHLIB: pt = "SHLIB"; break;
1644 case PT_PHDR: pt = "PHDR"; break;
1645 case PT_TLS: pt = "TLS"; break;
1646 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
2b05f1b7 1647 case PT_GNU_STACK: pt = "STACK"; break;
cedc298e
L
1648 case PT_GNU_RELRO: pt = "RELRO"; break;
1649 default: pt = NULL; break;
1650 }
1651 return pt;
1652}
1653
f0b79d91
L
1654/* Print out the program headers. */
1655
b34976b6 1656bfd_boolean
217aa764 1657_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
252b5132 1658{
a50b1753 1659 FILE *f = (FILE *) farg;
252b5132
RH
1660 Elf_Internal_Phdr *p;
1661 asection *s;
1662 bfd_byte *dynbuf = NULL;
1663
1664 p = elf_tdata (abfd)->phdr;
1665 if (p != NULL)
1666 {
1667 unsigned int i, c;
1668
1669 fprintf (f, _("\nProgram Header:\n"));
1670 c = elf_elfheader (abfd)->e_phnum;
1671 for (i = 0; i < c; i++, p++)
1672 {
cedc298e 1673 const char *pt = get_segment_type (p->p_type);
252b5132
RH
1674 char buf[20];
1675
cedc298e 1676 if (pt == NULL)
252b5132 1677 {
cedc298e
L
1678 sprintf (buf, "0x%lx", p->p_type);
1679 pt = buf;
252b5132 1680 }
dc810e39 1681 fprintf (f, "%8s off 0x", pt);
60b89a18 1682 bfd_fprintf_vma (abfd, f, p->p_offset);
252b5132 1683 fprintf (f, " vaddr 0x");
60b89a18 1684 bfd_fprintf_vma (abfd, f, p->p_vaddr);
252b5132 1685 fprintf (f, " paddr 0x");
60b89a18 1686 bfd_fprintf_vma (abfd, f, p->p_paddr);
252b5132
RH
1687 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1688 fprintf (f, " filesz 0x");
60b89a18 1689 bfd_fprintf_vma (abfd, f, p->p_filesz);
252b5132 1690 fprintf (f, " memsz 0x");
60b89a18 1691 bfd_fprintf_vma (abfd, f, p->p_memsz);
252b5132
RH
1692 fprintf (f, " flags %c%c%c",
1693 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1694 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1695 (p->p_flags & PF_X) != 0 ? 'x' : '-');
dc810e39
AM
1696 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1697 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
252b5132
RH
1698 fprintf (f, "\n");
1699 }
1700 }
1701
1702 s = bfd_get_section_by_name (abfd, ".dynamic");
1703 if (s != NULL)
1704 {
cb33740c 1705 unsigned int elfsec;
dc810e39 1706 unsigned long shlink;
252b5132
RH
1707 bfd_byte *extdyn, *extdynend;
1708 size_t extdynsize;
217aa764 1709 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
252b5132
RH
1710
1711 fprintf (f, _("\nDynamic Section:\n"));
1712
eea6121a 1713 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
252b5132
RH
1714 goto error_return;
1715
1716 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 1717 if (elfsec == SHN_BAD)
252b5132 1718 goto error_return;
dc810e39 1719 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
252b5132
RH
1720
1721 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1722 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1723
1724 extdyn = dynbuf;
06614111
NC
1725 /* PR 17512: file: 6f427532. */
1726 if (s->size < extdynsize)
1727 goto error_return;
eea6121a 1728 extdynend = extdyn + s->size;
1036838a 1729 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
07d6d2b8 1730 Fix range check. */
1036838a 1731 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
252b5132
RH
1732 {
1733 Elf_Internal_Dyn dyn;
ad9563d6 1734 const char *name = "";
252b5132 1735 char ab[20];
b34976b6 1736 bfd_boolean stringp;
ad9563d6 1737 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 1738
217aa764 1739 (*swap_dyn_in) (abfd, extdyn, &dyn);
252b5132
RH
1740
1741 if (dyn.d_tag == DT_NULL)
1742 break;
1743
b34976b6 1744 stringp = FALSE;
252b5132
RH
1745 switch (dyn.d_tag)
1746 {
1747 default:
ad9563d6
CM
1748 if (bed->elf_backend_get_target_dtag)
1749 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1750
1751 if (!strcmp (name, ""))
1752 {
cd9af601 1753 sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag);
ad9563d6
CM
1754 name = ab;
1755 }
252b5132
RH
1756 break;
1757
b34976b6 1758 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
252b5132
RH
1759 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1760 case DT_PLTGOT: name = "PLTGOT"; break;
1761 case DT_HASH: name = "HASH"; break;
1762 case DT_STRTAB: name = "STRTAB"; break;
1763 case DT_SYMTAB: name = "SYMTAB"; break;
1764 case DT_RELA: name = "RELA"; break;
1765 case DT_RELASZ: name = "RELASZ"; break;
1766 case DT_RELAENT: name = "RELAENT"; break;
1767 case DT_STRSZ: name = "STRSZ"; break;
1768 case DT_SYMENT: name = "SYMENT"; break;
1769 case DT_INIT: name = "INIT"; break;
1770 case DT_FINI: name = "FINI"; break;
b34976b6
AM
1771 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1772 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
252b5132
RH
1773 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1774 case DT_REL: name = "REL"; break;
1775 case DT_RELSZ: name = "RELSZ"; break;
1776 case DT_RELENT: name = "RELENT"; break;
1777 case DT_PLTREL: name = "PLTREL"; break;
1778 case DT_DEBUG: name = "DEBUG"; break;
1779 case DT_TEXTREL: name = "TEXTREL"; break;
1780 case DT_JMPREL: name = "JMPREL"; break;
94558834
L
1781 case DT_BIND_NOW: name = "BIND_NOW"; break;
1782 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1783 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1784 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1785 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
b34976b6 1786 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
94558834
L
1787 case DT_FLAGS: name = "FLAGS"; break;
1788 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1789 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
d48188b9 1790 case DT_CHECKSUM: name = "CHECKSUM"; break;
94558834
L
1791 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1792 case DT_MOVEENT: name = "MOVEENT"; break;
1793 case DT_MOVESZ: name = "MOVESZ"; break;
1794 case DT_FEATURE: name = "FEATURE"; break;
1795 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1796 case DT_SYMINSZ: name = "SYMINSZ"; break;
1797 case DT_SYMINENT: name = "SYMINENT"; break;
b34976b6
AM
1798 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1799 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1800 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
94558834
L
1801 case DT_PLTPAD: name = "PLTPAD"; break;
1802 case DT_MOVETAB: name = "MOVETAB"; break;
1803 case DT_SYMINFO: name = "SYMINFO"; break;
1804 case DT_RELACOUNT: name = "RELACOUNT"; break;
1805 case DT_RELCOUNT: name = "RELCOUNT"; break;
1806 case DT_FLAGS_1: name = "FLAGS_1"; break;
252b5132
RH
1807 case DT_VERSYM: name = "VERSYM"; break;
1808 case DT_VERDEF: name = "VERDEF"; break;
1809 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1810 case DT_VERNEED: name = "VERNEED"; break;
1811 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
b34976b6 1812 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
94558834 1813 case DT_USED: name = "USED"; break;
b34976b6 1814 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
fdc90cb4 1815 case DT_GNU_HASH: name = "GNU_HASH"; break;
252b5132
RH
1816 }
1817
ad9563d6 1818 fprintf (f, " %-20s ", name);
252b5132 1819 if (! stringp)
a1f3c56e
AN
1820 {
1821 fprintf (f, "0x");
1822 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1823 }
252b5132
RH
1824 else
1825 {
1826 const char *string;
dc810e39 1827 unsigned int tagv = dyn.d_un.d_val;
252b5132 1828
dc810e39 1829 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
252b5132
RH
1830 if (string == NULL)
1831 goto error_return;
1832 fprintf (f, "%s", string);
1833 }
1834 fprintf (f, "\n");
1835 }
1836
1837 free (dynbuf);
1838 dynbuf = NULL;
1839 }
1840
1841 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1842 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1843 {
fc0e6df6 1844 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
b34976b6 1845 return FALSE;
252b5132
RH
1846 }
1847
1848 if (elf_dynverdef (abfd) != 0)
1849 {
1850 Elf_Internal_Verdef *t;
1851
1852 fprintf (f, _("\nVersion definitions:\n"));
1853 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1854 {
1855 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
d0fb9a8d
JJ
1856 t->vd_flags, t->vd_hash,
1857 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1858 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
252b5132
RH
1859 {
1860 Elf_Internal_Verdaux *a;
1861
1862 fprintf (f, "\t");
1863 for (a = t->vd_auxptr->vda_nextptr;
1864 a != NULL;
1865 a = a->vda_nextptr)
d0fb9a8d
JJ
1866 fprintf (f, "%s ",
1867 a->vda_nodename ? a->vda_nodename : "<corrupt>");
252b5132
RH
1868 fprintf (f, "\n");
1869 }
1870 }
1871 }
1872
1873 if (elf_dynverref (abfd) != 0)
1874 {
1875 Elf_Internal_Verneed *t;
1876
1877 fprintf (f, _("\nVersion References:\n"));
1878 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1879 {
1880 Elf_Internal_Vernaux *a;
1881
d0fb9a8d
JJ
1882 fprintf (f, _(" required from %s:\n"),
1883 t->vn_filename ? t->vn_filename : "<corrupt>");
252b5132
RH
1884 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1885 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
d0fb9a8d
JJ
1886 a->vna_flags, a->vna_other,
1887 a->vna_nodename ? a->vna_nodename : "<corrupt>");
252b5132
RH
1888 }
1889 }
1890
b34976b6 1891 return TRUE;
252b5132
RH
1892
1893 error_return:
1894 if (dynbuf != NULL)
1895 free (dynbuf);
b34976b6 1896 return FALSE;
252b5132
RH
1897}
1898
bb4d2ac2
L
1899/* Get version string. */
1900
1901const char *
60bb06bc
L
1902_bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1903 bfd_boolean *hidden)
bb4d2ac2
L
1904{
1905 const char *version_string = NULL;
1906 if (elf_dynversym (abfd) != 0
1907 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1908 {
1909 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1910
1911 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1912 vernum &= VERSYM_VERSION;
1913
1914 if (vernum == 0)
1915 version_string = "";
1f6f5dba
L
1916 else if (vernum == 1
1917 && (vernum > elf_tdata (abfd)->cverdefs
1918 || (elf_tdata (abfd)->verdef[0].vd_flags
1919 == VER_FLG_BASE)))
bb4d2ac2
L
1920 version_string = "Base";
1921 else if (vernum <= elf_tdata (abfd)->cverdefs)
1922 version_string =
1923 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1924 else
1925 {
1926 Elf_Internal_Verneed *t;
1927
7a815dd5 1928 version_string = _("<corrupt>");
bb4d2ac2
L
1929 for (t = elf_tdata (abfd)->verref;
1930 t != NULL;
1931 t = t->vn_nextref)
1932 {
1933 Elf_Internal_Vernaux *a;
1934
1935 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1936 {
1937 if (a->vna_other == vernum)
1938 {
1939 version_string = a->vna_nodename;
1940 break;
1941 }
1942 }
1943 }
1944 }
1945 }
1946 return version_string;
1947}
1948
252b5132
RH
1949/* Display ELF-specific fields of a symbol. */
1950
1951void
217aa764
AM
1952bfd_elf_print_symbol (bfd *abfd,
1953 void *filep,
1954 asymbol *symbol,
1955 bfd_print_symbol_type how)
252b5132 1956{
a50b1753 1957 FILE *file = (FILE *) filep;
252b5132
RH
1958 switch (how)
1959 {
1960 case bfd_print_symbol_name:
1961 fprintf (file, "%s", symbol->name);
1962 break;
1963 case bfd_print_symbol_more:
1964 fprintf (file, "elf ");
60b89a18 1965 bfd_fprintf_vma (abfd, file, symbol->value);
cd9af601 1966 fprintf (file, " %x", symbol->flags);
252b5132
RH
1967 break;
1968 case bfd_print_symbol_all:
1969 {
4e8a9624
AM
1970 const char *section_name;
1971 const char *name = NULL;
9c5bfbb7 1972 const struct elf_backend_data *bed;
7a13edea 1973 unsigned char st_other;
dbb410c3 1974 bfd_vma val;
bb4d2ac2
L
1975 const char *version_string;
1976 bfd_boolean hidden;
c044fabd 1977
252b5132 1978 section_name = symbol->section ? symbol->section->name : "(*none*)";
587ff49e
RH
1979
1980 bed = get_elf_backend_data (abfd);
1981 if (bed->elf_backend_print_symbol_all)
c044fabd 1982 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
587ff49e
RH
1983
1984 if (name == NULL)
1985 {
7ee38065 1986 name = symbol->name;
217aa764 1987 bfd_print_symbol_vandf (abfd, file, symbol);
587ff49e
RH
1988 }
1989
252b5132
RH
1990 fprintf (file, " %s\t", section_name);
1991 /* Print the "other" value for a symbol. For common symbols,
1992 we've already printed the size; now print the alignment.
1993 For other symbols, we have no specified alignment, and
1994 we've printed the address; now print the size. */
dcf6c779 1995 if (symbol->section && bfd_is_com_section (symbol->section))
dbb410c3
AM
1996 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1997 else
1998 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1999 bfd_fprintf_vma (abfd, file, val);
252b5132
RH
2000
2001 /* If we have version information, print it. */
60bb06bc
L
2002 version_string = _bfd_elf_get_symbol_version_string (abfd,
2003 symbol,
2004 &hidden);
bb4d2ac2 2005 if (version_string)
252b5132 2006 {
bb4d2ac2 2007 if (!hidden)
252b5132
RH
2008 fprintf (file, " %-11s", version_string);
2009 else
2010 {
2011 int i;
2012
2013 fprintf (file, " (%s)", version_string);
2014 for (i = 10 - strlen (version_string); i > 0; --i)
2015 putc (' ', file);
2016 }
2017 }
2018
2019 /* If the st_other field is not zero, print it. */
7a13edea 2020 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
c044fabd 2021
7a13edea
NC
2022 switch (st_other)
2023 {
2024 case 0: break;
2025 case STV_INTERNAL: fprintf (file, " .internal"); break;
2026 case STV_HIDDEN: fprintf (file, " .hidden"); break;
2027 case STV_PROTECTED: fprintf (file, " .protected"); break;
2028 default:
2029 /* Some other non-defined flags are also present, so print
2030 everything hex. */
2031 fprintf (file, " 0x%02x", (unsigned int) st_other);
2032 }
252b5132 2033
587ff49e 2034 fprintf (file, " %s", name);
252b5132
RH
2035 }
2036 break;
2037 }
2038}
252b5132
RH
2039\f
2040/* ELF .o/exec file reading */
2041
c044fabd 2042/* Create a new bfd section from an ELF section header. */
252b5132 2043
b34976b6 2044bfd_boolean
217aa764 2045bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
252b5132 2046{
4fbb74a6
AM
2047 Elf_Internal_Shdr *hdr;
2048 Elf_Internal_Ehdr *ehdr;
2049 const struct elf_backend_data *bed;
90937f86 2050 const char *name;
bf67003b
NC
2051 bfd_boolean ret = TRUE;
2052 static bfd_boolean * sections_being_created = NULL;
5a4b0ccc 2053 static bfd * sections_being_created_abfd = NULL;
bf67003b 2054 static unsigned int nesting = 0;
252b5132 2055
4fbb74a6
AM
2056 if (shindex >= elf_numsections (abfd))
2057 return FALSE;
2058
bf67003b
NC
2059 if (++ nesting > 3)
2060 {
2061 /* PR17512: A corrupt ELF binary might contain a recursive group of
67ce483b 2062 sections, with each the string indices pointing to the next in the
bf67003b
NC
2063 loop. Detect this here, by refusing to load a section that we are
2064 already in the process of loading. We only trigger this test if
2065 we have nested at least three sections deep as normal ELF binaries
5a4b0ccc
NC
2066 can expect to recurse at least once.
2067
2068 FIXME: It would be better if this array was attached to the bfd,
2069 rather than being held in a static pointer. */
2070
2071 if (sections_being_created_abfd != abfd)
2072 sections_being_created = NULL;
bf67003b
NC
2073 if (sections_being_created == NULL)
2074 {
bf67003b 2075 sections_being_created = (bfd_boolean *)
7a6e0d89 2076 bfd_zalloc2 (abfd, elf_numsections (abfd), sizeof (bfd_boolean));
5a4b0ccc 2077 sections_being_created_abfd = abfd;
bf67003b
NC
2078 }
2079 if (sections_being_created [shindex])
2080 {
4eca0228 2081 _bfd_error_handler
871b3ab2 2082 (_("%pB: warning: loop in section dependencies detected"), abfd);
bf67003b
NC
2083 return FALSE;
2084 }
2085 sections_being_created [shindex] = TRUE;
2086 }
2087
4fbb74a6
AM
2088 hdr = elf_elfsections (abfd)[shindex];
2089 ehdr = elf_elfheader (abfd);
2090 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1b3a8575 2091 hdr->sh_name);
933d961a 2092 if (name == NULL)
bf67003b 2093 goto fail;
252b5132 2094
4fbb74a6 2095 bed = get_elf_backend_data (abfd);
252b5132
RH
2096 switch (hdr->sh_type)
2097 {
2098 case SHT_NULL:
2099 /* Inactive section. Throw it away. */
bf67003b 2100 goto success;
252b5132 2101
bf67003b
NC
2102 case SHT_PROGBITS: /* Normal section with contents. */
2103 case SHT_NOBITS: /* .bss section. */
2104 case SHT_HASH: /* .hash section. */
2105 case SHT_NOTE: /* .note section. */
25e27870
L
2106 case SHT_INIT_ARRAY: /* .init_array section. */
2107 case SHT_FINI_ARRAY: /* .fini_array section. */
2108 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
7f1204bb 2109 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
fdc90cb4 2110 case SHT_GNU_HASH: /* .gnu.hash section. */
bf67003b
NC
2111 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2112 goto success;
252b5132 2113
797fc050 2114 case SHT_DYNAMIC: /* Dynamic linking information. */
6dc132d9 2115 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b
NC
2116 goto fail;
2117
cfcac11d
NC
2118 if (hdr->sh_link > elf_numsections (abfd))
2119 {
caa83f8b 2120 /* PR 10478: Accept Solaris binaries with a sh_link
cfcac11d
NC
2121 field set to SHN_BEFORE or SHN_AFTER. */
2122 switch (bfd_get_arch (abfd))
2123 {
caa83f8b 2124 case bfd_arch_i386:
cfcac11d
NC
2125 case bfd_arch_sparc:
2126 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
2127 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
2128 break;
2129 /* Otherwise fall through. */
2130 default:
bf67003b 2131 goto fail;
cfcac11d
NC
2132 }
2133 }
2134 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
bf67003b 2135 goto fail;
cfcac11d 2136 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
797fc050
AM
2137 {
2138 Elf_Internal_Shdr *dynsymhdr;
2139
2140 /* The shared libraries distributed with hpux11 have a bogus
2141 sh_link field for the ".dynamic" section. Find the
2142 string table for the ".dynsym" section instead. */
2143 if (elf_dynsymtab (abfd) != 0)
2144 {
2145 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2146 hdr->sh_link = dynsymhdr->sh_link;
2147 }
2148 else
2149 {
2150 unsigned int i, num_sec;
2151
2152 num_sec = elf_numsections (abfd);
2153 for (i = 1; i < num_sec; i++)
2154 {
2155 dynsymhdr = elf_elfsections (abfd)[i];
2156 if (dynsymhdr->sh_type == SHT_DYNSYM)
2157 {
2158 hdr->sh_link = dynsymhdr->sh_link;
2159 break;
2160 }
2161 }
2162 }
2163 }
bf67003b 2164 goto success;
797fc050 2165
bf67003b 2166 case SHT_SYMTAB: /* A symbol table. */
252b5132 2167 if (elf_onesymtab (abfd) == shindex)
bf67003b 2168 goto success;
252b5132 2169
a50b2160 2170 if (hdr->sh_entsize != bed->s->sizeof_sym)
bf67003b
NC
2171 goto fail;
2172
3337c1e5 2173 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
eee3b786
AM
2174 {
2175 if (hdr->sh_size != 0)
bf67003b 2176 goto fail;
eee3b786
AM
2177 /* Some assemblers erroneously set sh_info to one with a
2178 zero sh_size. ld sees this as a global symbol count
2179 of (unsigned) -1. Fix it here. */
2180 hdr->sh_info = 0;
bf67003b 2181 goto success;
eee3b786 2182 }
bf67003b 2183
16ad13ec
NC
2184 /* PR 18854: A binary might contain more than one symbol table.
2185 Unusual, but possible. Warn, but continue. */
2186 if (elf_onesymtab (abfd) != 0)
2187 {
4eca0228 2188 _bfd_error_handler
695344c0 2189 /* xgettext:c-format */
871b3ab2 2190 (_("%pB: warning: multiple symbol tables detected"
63a5468a 2191 " - ignoring the table in section %u"),
16ad13ec
NC
2192 abfd, shindex);
2193 goto success;
2194 }
252b5132 2195 elf_onesymtab (abfd) = shindex;
6a40cf0c
NC
2196 elf_symtab_hdr (abfd) = *hdr;
2197 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
252b5132
RH
2198 abfd->flags |= HAS_SYMS;
2199
2200 /* Sometimes a shared object will map in the symbol table. If
08a40648
AM
2201 SHF_ALLOC is set, and this is a shared object, then we also
2202 treat this section as a BFD section. We can not base the
2203 decision purely on SHF_ALLOC, because that flag is sometimes
2204 set in a relocatable object file, which would confuse the
2205 linker. */
252b5132
RH
2206 if ((hdr->sh_flags & SHF_ALLOC) != 0
2207 && (abfd->flags & DYNAMIC) != 0
6dc132d9
L
2208 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2209 shindex))
bf67003b 2210 goto fail;
252b5132 2211
1b3a8575
AM
2212 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2213 can't read symbols without that section loaded as well. It
2214 is most likely specified by the next section header. */
6a40cf0c
NC
2215 {
2216 elf_section_list * entry;
2217 unsigned int i, num_sec;
1b3a8575 2218
6a40cf0c
NC
2219 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2220 if (entry->hdr.sh_link == shindex)
2221 goto success;
2222
2223 num_sec = elf_numsections (abfd);
2224 for (i = shindex + 1; i < num_sec; i++)
2225 {
2226 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2227
2228 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2229 && hdr2->sh_link == shindex)
2230 break;
2231 }
2232
2233 if (i == num_sec)
2234 for (i = 1; i < shindex; i++)
1b3a8575
AM
2235 {
2236 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
6a40cf0c 2237
1b3a8575
AM
2238 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2239 && hdr2->sh_link == shindex)
2240 break;
2241 }
6a40cf0c
NC
2242
2243 if (i != shindex)
2244 ret = bfd_section_from_shdr (abfd, i);
2245 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2246 goto success;
2247 }
252b5132 2248
bf67003b 2249 case SHT_DYNSYM: /* A dynamic symbol table. */
252b5132 2250 if (elf_dynsymtab (abfd) == shindex)
bf67003b 2251 goto success;
252b5132 2252
a50b2160 2253 if (hdr->sh_entsize != bed->s->sizeof_sym)
bf67003b
NC
2254 goto fail;
2255
eee3b786
AM
2256 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2257 {
2258 if (hdr->sh_size != 0)
bf67003b
NC
2259 goto fail;
2260
eee3b786
AM
2261 /* Some linkers erroneously set sh_info to one with a
2262 zero sh_size. ld sees this as a global symbol count
2263 of (unsigned) -1. Fix it here. */
2264 hdr->sh_info = 0;
bf67003b 2265 goto success;
eee3b786 2266 }
bf67003b 2267
16ad13ec
NC
2268 /* PR 18854: A binary might contain more than one dynamic symbol table.
2269 Unusual, but possible. Warn, but continue. */
2270 if (elf_dynsymtab (abfd) != 0)
2271 {
4eca0228 2272 _bfd_error_handler
695344c0 2273 /* xgettext:c-format */
871b3ab2 2274 (_("%pB: warning: multiple dynamic symbol tables detected"
63a5468a 2275 " - ignoring the table in section %u"),
16ad13ec
NC
2276 abfd, shindex);
2277 goto success;
2278 }
252b5132
RH
2279 elf_dynsymtab (abfd) = shindex;
2280 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2281 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2282 abfd->flags |= HAS_SYMS;
2283
2284 /* Besides being a symbol table, we also treat this as a regular
2285 section, so that objcopy can handle it. */
bf67003b
NC
2286 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2287 goto success;
252b5132 2288
bf67003b 2289 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
6a40cf0c
NC
2290 {
2291 elf_section_list * entry;
9ad5cbcf 2292
6a40cf0c
NC
2293 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2294 if (entry->ndx == shindex)
2295 goto success;
07d6d2b8 2296
7a6e0d89 2297 entry = bfd_alloc (abfd, sizeof (*entry));
6a40cf0c
NC
2298 if (entry == NULL)
2299 goto fail;
2300 entry->ndx = shindex;
2301 entry->hdr = * hdr;
2302 entry->next = elf_symtab_shndx_list (abfd);
2303 elf_symtab_shndx_list (abfd) = entry;
2304 elf_elfsections (abfd)[shindex] = & entry->hdr;
2305 goto success;
2306 }
9ad5cbcf 2307
bf67003b 2308 case SHT_STRTAB: /* A string table. */
252b5132 2309 if (hdr->bfd_section != NULL)
bf67003b
NC
2310 goto success;
2311
252b5132
RH
2312 if (ehdr->e_shstrndx == shindex)
2313 {
2314 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2315 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
bf67003b 2316 goto success;
252b5132 2317 }
bf67003b 2318
1b3a8575
AM
2319 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2320 {
2321 symtab_strtab:
2322 elf_tdata (abfd)->strtab_hdr = *hdr;
2323 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
bf67003b 2324 goto success;
1b3a8575 2325 }
bf67003b 2326
1b3a8575
AM
2327 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2328 {
2329 dynsymtab_strtab:
2330 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2331 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2332 elf_elfsections (abfd)[shindex] = hdr;
2333 /* We also treat this as a regular section, so that objcopy
2334 can handle it. */
bf67003b
NC
2335 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2336 shindex);
2337 goto success;
1b3a8575 2338 }
252b5132 2339
1b3a8575
AM
2340 /* If the string table isn't one of the above, then treat it as a
2341 regular section. We need to scan all the headers to be sure,
2342 just in case this strtab section appeared before the above. */
2343 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2344 {
2345 unsigned int i, num_sec;
252b5132 2346
1b3a8575
AM
2347 num_sec = elf_numsections (abfd);
2348 for (i = 1; i < num_sec; i++)
2349 {
2350 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2351 if (hdr2->sh_link == shindex)
2352 {
933d961a
JJ
2353 /* Prevent endless recursion on broken objects. */
2354 if (i == shindex)
bf67003b 2355 goto fail;
1b3a8575 2356 if (! bfd_section_from_shdr (abfd, i))
bf67003b 2357 goto fail;
1b3a8575
AM
2358 if (elf_onesymtab (abfd) == i)
2359 goto symtab_strtab;
2360 if (elf_dynsymtab (abfd) == i)
2361 goto dynsymtab_strtab;
2362 }
2363 }
2364 }
bf67003b
NC
2365 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2366 goto success;
252b5132
RH
2367
2368 case SHT_REL:
2369 case SHT_RELA:
2370 /* *These* do a lot of work -- but build no sections! */
2371 {
2372 asection *target_sect;
d4730f92 2373 Elf_Internal_Shdr *hdr2, **p_hdr;
9ad5cbcf 2374 unsigned int num_sec = elf_numsections (abfd);
d4730f92 2375 struct bfd_elf_section_data *esdt;
252b5132 2376
aa2ca951
JJ
2377 if (hdr->sh_entsize
2378 != (bfd_size_type) (hdr->sh_type == SHT_REL
a50b2160 2379 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
bf67003b 2380 goto fail;
a50b2160 2381
03ae5f59 2382 /* Check for a bogus link to avoid crashing. */
4fbb74a6 2383 if (hdr->sh_link >= num_sec)
03ae5f59 2384 {
4eca0228 2385 _bfd_error_handler
695344c0 2386 /* xgettext:c-format */
871b3ab2 2387 (_("%pB: invalid link %u for reloc section %s (index %u)"),
4eca0228 2388 abfd, hdr->sh_link, name, shindex);
bf67003b
NC
2389 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2390 shindex);
2391 goto success;
03ae5f59
ILT
2392 }
2393
252b5132
RH
2394 /* For some incomprehensible reason Oracle distributes
2395 libraries for Solaris in which some of the objects have
2396 bogus sh_link fields. It would be nice if we could just
2397 reject them, but, unfortunately, some people need to use
2398 them. We scan through the section headers; if we find only
2399 one suitable symbol table, we clobber the sh_link to point
83b89087
L
2400 to it. I hope this doesn't break anything.
2401
2402 Don't do it on executable nor shared library. */
2403 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
2404 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
252b5132
RH
2405 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
2406 {
9ad5cbcf 2407 unsigned int scan;
252b5132
RH
2408 int found;
2409
2410 found = 0;
9ad5cbcf 2411 for (scan = 1; scan < num_sec; scan++)
252b5132
RH
2412 {
2413 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
2414 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
2415 {
2416 if (found != 0)
2417 {
2418 found = 0;
2419 break;
2420 }
2421 found = scan;
2422 }
2423 }
2424 if (found != 0)
2425 hdr->sh_link = found;
2426 }
2427
2428 /* Get the symbol table. */
1b3a8575
AM
2429 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2430 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
252b5132 2431 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
bf67003b 2432 goto fail;
252b5132 2433
a4bcd733
AM
2434 /* If this is an alloc section in an executable or shared
2435 library, or the reloc section does not use the main symbol
2436 table we don't treat it as a reloc section. BFD can't
2437 adequately represent such a section, so at least for now,
2438 we don't try. We just present it as a normal section. We
2439 also can't use it as a reloc section if it points to the
2440 null section, an invalid section, another reloc section, or
2441 its sh_link points to the null section. */
2442 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2443 && (hdr->sh_flags & SHF_ALLOC) != 0)
83b89087 2444 || hdr->sh_link == SHN_UNDEF
a4bcd733 2445 || hdr->sh_link != elf_onesymtab (abfd)
185ef66d 2446 || hdr->sh_info == SHN_UNDEF
185ef66d
AM
2447 || hdr->sh_info >= num_sec
2448 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2449 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
bf67003b
NC
2450 {
2451 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2452 shindex);
2453 goto success;
2454 }
252b5132
RH
2455
2456 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
bf67003b
NC
2457 goto fail;
2458
252b5132
RH
2459 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2460 if (target_sect == NULL)
bf67003b 2461 goto fail;
252b5132 2462
d4730f92
BS
2463 esdt = elf_section_data (target_sect);
2464 if (hdr->sh_type == SHT_RELA)
2465 p_hdr = &esdt->rela.hdr;
252b5132 2466 else
d4730f92
BS
2467 p_hdr = &esdt->rel.hdr;
2468
a7ba3896
NC
2469 /* PR 17512: file: 0b4f81b7.
2470 Also see PR 24456, for a file which deliberately has two reloc
2471 sections. */
06614111 2472 if (*p_hdr != NULL)
a7ba3896
NC
2473 {
2474 _bfd_error_handler
2475 /* xgettext:c-format */
2476 (_("%pB: warning: multiple relocation sections for section %pA \
2477found - ignoring all but the first"),
2478 abfd, target_sect);
2479 goto success;
2480 }
ef53be89 2481 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
d4730f92 2482 if (hdr2 == NULL)
bf67003b 2483 goto fail;
252b5132 2484 *hdr2 = *hdr;
d4730f92 2485 *p_hdr = hdr2;
252b5132 2486 elf_elfsections (abfd)[shindex] = hdr2;
056bafd4
MR
2487 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2488 * bed->s->int_rels_per_ext_rel);
252b5132
RH
2489 target_sect->flags |= SEC_RELOC;
2490 target_sect->relocation = NULL;
2491 target_sect->rel_filepos = hdr->sh_offset;
bf572ba0
MM
2492 /* In the section to which the relocations apply, mark whether
2493 its relocations are of the REL or RELA variety. */
72730e0c 2494 if (hdr->sh_size != 0)
d4730f92
BS
2495 {
2496 if (hdr->sh_type == SHT_RELA)
2497 target_sect->use_rela_p = 1;
2498 }
252b5132 2499 abfd->flags |= HAS_RELOC;
bf67003b 2500 goto success;
252b5132 2501 }
252b5132
RH
2502
2503 case SHT_GNU_verdef:
2504 elf_dynverdef (abfd) = shindex;
2505 elf_tdata (abfd)->dynverdef_hdr = *hdr;
bf67003b
NC
2506 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2507 goto success;
252b5132
RH
2508
2509 case SHT_GNU_versym:
a50b2160 2510 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
bf67003b
NC
2511 goto fail;
2512
252b5132
RH
2513 elf_dynversym (abfd) = shindex;
2514 elf_tdata (abfd)->dynversym_hdr = *hdr;
bf67003b
NC
2515 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2516 goto success;
252b5132
RH
2517
2518 case SHT_GNU_verneed:
2519 elf_dynverref (abfd) = shindex;
2520 elf_tdata (abfd)->dynverref_hdr = *hdr;
bf67003b
NC
2521 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2522 goto success;
252b5132
RH
2523
2524 case SHT_SHLIB:
bf67003b 2525 goto success;
252b5132 2526
dbb410c3 2527 case SHT_GROUP:
44534af3 2528 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
bf67003b
NC
2529 goto fail;
2530
6dc132d9 2531 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b
NC
2532 goto fail;
2533
bf67003b 2534 goto success;
dbb410c3 2535
252b5132 2536 default:
104d59d1
JM
2537 /* Possibly an attributes section. */
2538 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2539 || hdr->sh_type == bed->obj_attrs_section_type)
2540 {
2541 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2542 goto fail;
104d59d1 2543 _bfd_elf_parse_attributes (abfd, hdr);
bf67003b 2544 goto success;
104d59d1
JM
2545 }
2546
252b5132 2547 /* Check for any processor-specific section types. */
3eb70a79 2548 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2549 goto success;
3eb70a79
L
2550
2551 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2552 {
2553 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2554 /* FIXME: How to properly handle allocated section reserved
2555 for applications? */
4eca0228 2556 _bfd_error_handler
695344c0 2557 /* xgettext:c-format */
871b3ab2 2558 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2559 abfd, hdr->sh_type, name);
3eb70a79 2560 else
bf67003b
NC
2561 {
2562 /* Allow sections reserved for applications. */
2563 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2564 shindex);
2565 goto success;
2566 }
3eb70a79
L
2567 }
2568 else if (hdr->sh_type >= SHT_LOPROC
2569 && hdr->sh_type <= SHT_HIPROC)
2570 /* FIXME: We should handle this section. */
4eca0228 2571 _bfd_error_handler
695344c0 2572 /* xgettext:c-format */
871b3ab2 2573 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2574 abfd, hdr->sh_type, name);
3eb70a79 2575 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
ff15b240
NC
2576 {
2577 /* Unrecognised OS-specific sections. */
2578 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2579 /* SHF_OS_NONCONFORMING indicates that special knowledge is
08a40648 2580 required to correctly process the section and the file should
ff15b240 2581 be rejected with an error message. */
4eca0228 2582 _bfd_error_handler
695344c0 2583 /* xgettext:c-format */
871b3ab2 2584 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2585 abfd, hdr->sh_type, name);
ff15b240 2586 else
bf67003b
NC
2587 {
2588 /* Otherwise it should be processed. */
2589 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2590 goto success;
2591 }
ff15b240 2592 }
3eb70a79
L
2593 else
2594 /* FIXME: We should handle this section. */
4eca0228 2595 _bfd_error_handler
695344c0 2596 /* xgettext:c-format */
871b3ab2 2597 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2598 abfd, hdr->sh_type, name);
3eb70a79 2599
bf67003b 2600 goto fail;
252b5132
RH
2601 }
2602
bf67003b
NC
2603 fail:
2604 ret = FALSE;
2605 success:
e5b470e2 2606 if (sections_being_created && sections_being_created_abfd == abfd)
bf67003b
NC
2607 sections_being_created [shindex] = FALSE;
2608 if (-- nesting == 0)
5a4b0ccc
NC
2609 {
2610 sections_being_created = NULL;
2611 sections_being_created_abfd = abfd;
2612 }
bf67003b 2613 return ret;
252b5132
RH
2614}
2615
87d72d41 2616/* Return the local symbol specified by ABFD, R_SYMNDX. */
ec338859 2617
87d72d41
AM
2618Elf_Internal_Sym *
2619bfd_sym_from_r_symndx (struct sym_cache *cache,
2620 bfd *abfd,
2621 unsigned long r_symndx)
ec338859 2622{
ec338859
AM
2623 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2624
a5d1b3b5
AM
2625 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2626 {
2627 Elf_Internal_Shdr *symtab_hdr;
2628 unsigned char esym[sizeof (Elf64_External_Sym)];
2629 Elf_External_Sym_Shndx eshndx;
ec338859 2630
a5d1b3b5
AM
2631 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2632 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
87d72d41 2633 &cache->sym[ent], esym, &eshndx) == NULL)
a5d1b3b5 2634 return NULL;
9ad5cbcf 2635
a5d1b3b5
AM
2636 if (cache->abfd != abfd)
2637 {
2638 memset (cache->indx, -1, sizeof (cache->indx));
2639 cache->abfd = abfd;
2640 }
2641 cache->indx[ent] = r_symndx;
ec338859 2642 }
a5d1b3b5 2643
87d72d41 2644 return &cache->sym[ent];
ec338859
AM
2645}
2646
252b5132
RH
2647/* Given an ELF section number, retrieve the corresponding BFD
2648 section. */
2649
2650asection *
91d6fa6a 2651bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
252b5132 2652{
91d6fa6a 2653 if (sec_index >= elf_numsections (abfd))
252b5132 2654 return NULL;
91d6fa6a 2655 return elf_elfsections (abfd)[sec_index]->bfd_section;
252b5132
RH
2656}
2657
b35d266b 2658static const struct bfd_elf_special_section special_sections_b[] =
2f89ff8d 2659{
0112cd26 2660 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2661 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2662};
2663
b35d266b 2664static const struct bfd_elf_special_section special_sections_c[] =
7f4d3958 2665{
0112cd26 2666 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2667 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2668};
2669
b35d266b 2670static const struct bfd_elf_special_section special_sections_d[] =
7f4d3958 2671{
07d6d2b8
AM
2672 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2673 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
a9a72a65
DE
2674 /* There are more DWARF sections than these, but they needn't be added here
2675 unless you have to cope with broken compilers that don't emit section
2676 attributes or you want to help the user writing assembler. */
07d6d2b8
AM
2677 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2678 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2679 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2680 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
0112cd26 2681 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2682 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2683 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2684 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2685 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2686};
2687
b35d266b 2688static const struct bfd_elf_special_section special_sections_f[] =
7f4d3958 2689{
07d6d2b8 2690 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2691 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2692 { NULL, 0 , 0, 0, 0 }
7f4d3958
L
2693};
2694
b35d266b 2695static const struct bfd_elf_special_section special_sections_g[] =
7f4d3958 2696{
0112cd26 2697 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2698 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2699 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2700 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
0112cd26
NC
2701 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2702 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
07d6d2b8
AM
2703 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2704 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2705 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2706 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2707};
2708
b35d266b 2709static const struct bfd_elf_special_section special_sections_h[] =
7f4d3958 2710{
07d6d2b8
AM
2711 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2712 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2713};
2714
b35d266b 2715static const struct bfd_elf_special_section special_sections_i[] =
7f4d3958 2716{
07d6d2b8 2717 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2718 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2719 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2720 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2721};
2722
b35d266b 2723static const struct bfd_elf_special_section special_sections_l[] =
7f4d3958 2724{
0112cd26 2725 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2726 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2727};
2728
b35d266b 2729static const struct bfd_elf_special_section special_sections_n[] =
7f4d3958 2730{
0112cd26 2731 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2732 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2733 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2734};
2735
b35d266b 2736static const struct bfd_elf_special_section special_sections_p[] =
7f4d3958 2737{
6f9dbcd4 2738 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2739 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2740 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2741};
2742
b35d266b 2743static const struct bfd_elf_special_section special_sections_r[] =
7f4d3958 2744{
0112cd26
NC
2745 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2746 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
07d6d2b8
AM
2747 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2748 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2749 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2750};
2751
b35d266b 2752static const struct bfd_elf_special_section special_sections_s[] =
7f4d3958 2753{
0112cd26
NC
2754 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2755 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2756 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
60ff4dc4
HPN
2757 /* See struct bfd_elf_special_section declaration for the semantics of
2758 this special case where .prefix_length != strlen (.prefix). */
2759 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
07d6d2b8 2760 { NULL, 0, 0, 0, 0 }
2f89ff8d
L
2761};
2762
b35d266b 2763static const struct bfd_elf_special_section special_sections_t[] =
7f4d3958 2764{
07d6d2b8
AM
2765 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2766 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
0112cd26 2767 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
07d6d2b8 2768 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2769};
2770
1b315056
CS
2771static const struct bfd_elf_special_section special_sections_z[] =
2772{
07d6d2b8
AM
2773 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2774 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
1b315056
CS
2775 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2776 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2777 { NULL, 0, 0, 0, 0 }
1b315056
CS
2778};
2779
e4c93b56 2780static const struct bfd_elf_special_section * const special_sections[] =
7f4d3958 2781{
7f4d3958 2782 special_sections_b, /* 'b' */
98ece1b3 2783 special_sections_c, /* 'c' */
7f4d3958
L
2784 special_sections_d, /* 'd' */
2785 NULL, /* 'e' */
2786 special_sections_f, /* 'f' */
2787 special_sections_g, /* 'g' */
2788 special_sections_h, /* 'h' */
2789 special_sections_i, /* 'i' */
2790 NULL, /* 'j' */
2791 NULL, /* 'k' */
2792 special_sections_l, /* 'l' */
2793 NULL, /* 'm' */
2794 special_sections_n, /* 'n' */
2795 NULL, /* 'o' */
2796 special_sections_p, /* 'p' */
2797 NULL, /* 'q' */
2798 special_sections_r, /* 'r' */
2799 special_sections_s, /* 's' */
2800 special_sections_t, /* 't' */
1b315056
CS
2801 NULL, /* 'u' */
2802 NULL, /* 'v' */
2803 NULL, /* 'w' */
2804 NULL, /* 'x' */
2805 NULL, /* 'y' */
2806 special_sections_z /* 'z' */
7f4d3958
L
2807};
2808
551b43fd
AM
2809const struct bfd_elf_special_section *
2810_bfd_elf_get_special_section (const char *name,
2811 const struct bfd_elf_special_section *spec,
2812 unsigned int rela)
2f89ff8d
L
2813{
2814 int i;
7f4d3958 2815 int len;
7f4d3958 2816
551b43fd 2817 len = strlen (name);
7f4d3958 2818
551b43fd 2819 for (i = 0; spec[i].prefix != NULL; i++)
7dcb9820
AM
2820 {
2821 int suffix_len;
551b43fd 2822 int prefix_len = spec[i].prefix_length;
7dcb9820
AM
2823
2824 if (len < prefix_len)
2825 continue;
551b43fd 2826 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
7dcb9820
AM
2827 continue;
2828
551b43fd 2829 suffix_len = spec[i].suffix_length;
7dcb9820
AM
2830 if (suffix_len <= 0)
2831 {
2832 if (name[prefix_len] != 0)
2833 {
2834 if (suffix_len == 0)
2835 continue;
2836 if (name[prefix_len] != '.'
2837 && (suffix_len == -2
551b43fd 2838 || (rela && spec[i].type == SHT_REL)))
7dcb9820
AM
2839 continue;
2840 }
2841 }
2842 else
2843 {
2844 if (len < prefix_len + suffix_len)
2845 continue;
2846 if (memcmp (name + len - suffix_len,
551b43fd 2847 spec[i].prefix + prefix_len,
7dcb9820
AM
2848 suffix_len) != 0)
2849 continue;
2850 }
551b43fd 2851 return &spec[i];
7dcb9820 2852 }
2f89ff8d
L
2853
2854 return NULL;
2855}
2856
7dcb9820 2857const struct bfd_elf_special_section *
29ef7005 2858_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2f89ff8d 2859{
551b43fd
AM
2860 int i;
2861 const struct bfd_elf_special_section *spec;
29ef7005 2862 const struct elf_backend_data *bed;
2f89ff8d
L
2863
2864 /* See if this is one of the special sections. */
551b43fd
AM
2865 if (sec->name == NULL)
2866 return NULL;
2f89ff8d 2867
29ef7005
L
2868 bed = get_elf_backend_data (abfd);
2869 spec = bed->special_sections;
2870 if (spec)
2871 {
2872 spec = _bfd_elf_get_special_section (sec->name,
2873 bed->special_sections,
2874 sec->use_rela_p);
2875 if (spec != NULL)
2876 return spec;
2877 }
2878
551b43fd
AM
2879 if (sec->name[0] != '.')
2880 return NULL;
2f89ff8d 2881
551b43fd 2882 i = sec->name[1] - 'b';
1b315056 2883 if (i < 0 || i > 'z' - 'b')
551b43fd
AM
2884 return NULL;
2885
2886 spec = special_sections[i];
2f89ff8d 2887
551b43fd
AM
2888 if (spec == NULL)
2889 return NULL;
2890
2891 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2f89ff8d
L
2892}
2893
b34976b6 2894bfd_boolean
217aa764 2895_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
252b5132
RH
2896{
2897 struct bfd_elf_section_data *sdata;
551b43fd 2898 const struct elf_backend_data *bed;
7dcb9820 2899 const struct bfd_elf_special_section *ssect;
252b5132 2900
f0abc2a1
AM
2901 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2902 if (sdata == NULL)
2903 {
a50b1753 2904 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
07d6d2b8 2905 sizeof (*sdata));
f0abc2a1
AM
2906 if (sdata == NULL)
2907 return FALSE;
217aa764 2908 sec->used_by_bfd = sdata;
f0abc2a1 2909 }
bf572ba0 2910
551b43fd
AM
2911 /* Indicate whether or not this section should use RELA relocations. */
2912 bed = get_elf_backend_data (abfd);
2913 sec->use_rela_p = bed->default_use_rela_p;
2914
e843e0f8
L
2915 /* When we read a file, we don't need to set ELF section type and
2916 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2917 anyway. We will set ELF section type and flags for all linker
2918 created sections. If user specifies BFD section flags, we will
2919 set ELF section type and flags based on BFD section flags in
02ecc8e9
L
2920 elf_fake_sections. Special handling for .init_array/.fini_array
2921 output sections since they may contain .ctors/.dtors input
2922 sections. We don't want _bfd_elf_init_private_section_data to
2923 copy ELF section type from .ctors/.dtors input sections. */
2924 if (abfd->direction != read_direction
3496cb2a 2925 || (sec->flags & SEC_LINKER_CREATED) != 0)
2f89ff8d 2926 {
551b43fd 2927 ssect = (*bed->get_sec_type_attr) (abfd, sec);
02ecc8e9
L
2928 if (ssect != NULL
2929 && (!sec->flags
2930 || (sec->flags & SEC_LINKER_CREATED) != 0
2931 || ssect->type == SHT_INIT_ARRAY
2932 || ssect->type == SHT_FINI_ARRAY))
a31501e9
L
2933 {
2934 elf_section_type (sec) = ssect->type;
2935 elf_section_flags (sec) = ssect->attr;
2936 }
2f89ff8d
L
2937 }
2938
f592407e 2939 return _bfd_generic_new_section_hook (abfd, sec);
252b5132
RH
2940}
2941
2942/* Create a new bfd section from an ELF program header.
2943
2944 Since program segments have no names, we generate a synthetic name
2945 of the form segment<NUM>, where NUM is generally the index in the
2946 program header table. For segments that are split (see below) we
2947 generate the names segment<NUM>a and segment<NUM>b.
2948
2949 Note that some program segments may have a file size that is different than
2950 (less than) the memory size. All this means is that at execution the
2951 system must allocate the amount of memory specified by the memory size,
2952 but only initialize it with the first "file size" bytes read from the
2953 file. This would occur for example, with program segments consisting
2954 of combined data+bss.
2955
2956 To handle the above situation, this routine generates TWO bfd sections
2957 for the single program segment. The first has the length specified by
2958 the file size of the segment, and the second has the length specified
2959 by the difference between the two sizes. In effect, the segment is split
d5191d0c 2960 into its initialized and uninitialized parts.
252b5132
RH
2961
2962 */
2963
b34976b6 2964bfd_boolean
217aa764
AM
2965_bfd_elf_make_section_from_phdr (bfd *abfd,
2966 Elf_Internal_Phdr *hdr,
91d6fa6a 2967 int hdr_index,
a50b1753 2968 const char *type_name)
252b5132
RH
2969{
2970 asection *newsect;
2971 char *name;
2972 char namebuf[64];
d4c88bbb 2973 size_t len;
252b5132
RH
2974 int split;
2975
2976 split = ((hdr->p_memsz > 0)
2977 && (hdr->p_filesz > 0)
2978 && (hdr->p_memsz > hdr->p_filesz));
d5191d0c
AM
2979
2980 if (hdr->p_filesz > 0)
252b5132 2981 {
91d6fa6a 2982 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
d5191d0c 2983 len = strlen (namebuf) + 1;
a50b1753 2984 name = (char *) bfd_alloc (abfd, len);
d5191d0c
AM
2985 if (!name)
2986 return FALSE;
2987 memcpy (name, namebuf, len);
2988 newsect = bfd_make_section (abfd, name);
2989 if (newsect == NULL)
2990 return FALSE;
2991 newsect->vma = hdr->p_vaddr;
2992 newsect->lma = hdr->p_paddr;
2993 newsect->size = hdr->p_filesz;
2994 newsect->filepos = hdr->p_offset;
2995 newsect->flags |= SEC_HAS_CONTENTS;
2996 newsect->alignment_power = bfd_log2 (hdr->p_align);
2997 if (hdr->p_type == PT_LOAD)
252b5132 2998 {
d5191d0c
AM
2999 newsect->flags |= SEC_ALLOC;
3000 newsect->flags |= SEC_LOAD;
3001 if (hdr->p_flags & PF_X)
3002 {
3003 /* FIXME: all we known is that it has execute PERMISSION,
3004 may be data. */
3005 newsect->flags |= SEC_CODE;
3006 }
3007 }
3008 if (!(hdr->p_flags & PF_W))
3009 {
3010 newsect->flags |= SEC_READONLY;
252b5132 3011 }
252b5132
RH
3012 }
3013
d5191d0c 3014 if (hdr->p_memsz > hdr->p_filesz)
252b5132 3015 {
d5191d0c
AM
3016 bfd_vma align;
3017
91d6fa6a 3018 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
d4c88bbb 3019 len = strlen (namebuf) + 1;
a50b1753 3020 name = (char *) bfd_alloc (abfd, len);
252b5132 3021 if (!name)
b34976b6 3022 return FALSE;
d4c88bbb 3023 memcpy (name, namebuf, len);
252b5132
RH
3024 newsect = bfd_make_section (abfd, name);
3025 if (newsect == NULL)
b34976b6 3026 return FALSE;
252b5132
RH
3027 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
3028 newsect->lma = hdr->p_paddr + hdr->p_filesz;
eea6121a 3029 newsect->size = hdr->p_memsz - hdr->p_filesz;
d5191d0c
AM
3030 newsect->filepos = hdr->p_offset + hdr->p_filesz;
3031 align = newsect->vma & -newsect->vma;
3032 if (align == 0 || align > hdr->p_align)
3033 align = hdr->p_align;
3034 newsect->alignment_power = bfd_log2 (align);
252b5132
RH
3035 if (hdr->p_type == PT_LOAD)
3036 {
d5191d0c
AM
3037 /* Hack for gdb. Segments that have not been modified do
3038 not have their contents written to a core file, on the
3039 assumption that a debugger can find the contents in the
3040 executable. We flag this case by setting the fake
3041 section size to zero. Note that "real" bss sections will
3042 always have their contents dumped to the core file. */
3043 if (bfd_get_format (abfd) == bfd_core)
3044 newsect->size = 0;
252b5132
RH
3045 newsect->flags |= SEC_ALLOC;
3046 if (hdr->p_flags & PF_X)
3047 newsect->flags |= SEC_CODE;
3048 }
3049 if (!(hdr->p_flags & PF_W))
3050 newsect->flags |= SEC_READONLY;
3051 }
3052
b34976b6 3053 return TRUE;
252b5132
RH
3054}
3055
b34976b6 3056bfd_boolean
91d6fa6a 3057bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
20cfcaae 3058{
9c5bfbb7 3059 const struct elf_backend_data *bed;
20cfcaae
NC
3060
3061 switch (hdr->p_type)
3062 {
3063 case PT_NULL:
91d6fa6a 3064 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
20cfcaae
NC
3065
3066 case PT_LOAD:
91d6fa6a 3067 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
20cfcaae
NC
3068
3069 case PT_DYNAMIC:
91d6fa6a 3070 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
20cfcaae
NC
3071
3072 case PT_INTERP:
91d6fa6a 3073 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
20cfcaae
NC
3074
3075 case PT_NOTE:
91d6fa6a 3076 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
b34976b6 3077 return FALSE;
276da9b3
L
3078 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3079 hdr->p_align))
b34976b6
AM
3080 return FALSE;
3081 return TRUE;
20cfcaae
NC
3082
3083 case PT_SHLIB:
91d6fa6a 3084 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
20cfcaae
NC
3085
3086 case PT_PHDR:
91d6fa6a 3087 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
20cfcaae 3088
811072d8 3089 case PT_GNU_EH_FRAME:
91d6fa6a 3090 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
811072d8
RM
3091 "eh_frame_hdr");
3092
2b05f1b7 3093 case PT_GNU_STACK:
91d6fa6a 3094 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
9ee5e499 3095
8c37241b 3096 case PT_GNU_RELRO:
91d6fa6a 3097 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
8c37241b 3098
20cfcaae 3099 default:
8c1acd09 3100 /* Check for any processor-specific program segment types. */
20cfcaae 3101 bed = get_elf_backend_data (abfd);
91d6fa6a 3102 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
20cfcaae
NC
3103 }
3104}
3105
d4730f92
BS
3106/* Return the REL_HDR for SEC, assuming there is only a single one, either
3107 REL or RELA. */
3108
3109Elf_Internal_Shdr *
3110_bfd_elf_single_rel_hdr (asection *sec)
3111{
3112 if (elf_section_data (sec)->rel.hdr)
3113 {
3114 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3115 return elf_section_data (sec)->rel.hdr;
3116 }
3117 else
3118 return elf_section_data (sec)->rela.hdr;
3119}
3120
3e19fb8f
L
3121static bfd_boolean
3122_bfd_elf_set_reloc_sh_name (bfd *abfd,
3123 Elf_Internal_Shdr *rel_hdr,
3124 const char *sec_name,
3125 bfd_boolean use_rela_p)
3126{
3127 char *name = (char *) bfd_alloc (abfd,
3128 sizeof ".rela" + strlen (sec_name));
3129 if (name == NULL)
3130 return FALSE;
3131
3132 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3133 rel_hdr->sh_name =
3134 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3135 FALSE);
3136 if (rel_hdr->sh_name == (unsigned int) -1)
3137 return FALSE;
3138
3139 return TRUE;
3140}
3141
d4730f92
BS
3142/* Allocate and initialize a section-header for a new reloc section,
3143 containing relocations against ASECT. It is stored in RELDATA. If
3144 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3145 relocations. */
23bc299b 3146
5d13b3b3 3147static bfd_boolean
217aa764 3148_bfd_elf_init_reloc_shdr (bfd *abfd,
d4730f92 3149 struct bfd_elf_section_reloc_data *reldata,
f6fe1ccd 3150 const char *sec_name,
3e19fb8f
L
3151 bfd_boolean use_rela_p,
3152 bfd_boolean delay_st_name_p)
23bc299b 3153{
d4730f92 3154 Elf_Internal_Shdr *rel_hdr;
9c5bfbb7 3155 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3156
d4730f92 3157 BFD_ASSERT (reldata->hdr == NULL);
ef53be89 3158 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
d4730f92 3159 reldata->hdr = rel_hdr;
23bc299b 3160
3e19fb8f
L
3161 if (delay_st_name_p)
3162 rel_hdr->sh_name = (unsigned int) -1;
3163 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3164 use_rela_p))
b34976b6 3165 return FALSE;
23bc299b
MM
3166 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3167 rel_hdr->sh_entsize = (use_rela_p
3168 ? bed->s->sizeof_rela
3169 : bed->s->sizeof_rel);
72de5009 3170 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
28e07a05 3171 rel_hdr->sh_flags = 0;
23bc299b
MM
3172 rel_hdr->sh_addr = 0;
3173 rel_hdr->sh_size = 0;
3174 rel_hdr->sh_offset = 0;
3175
b34976b6 3176 return TRUE;
23bc299b
MM
3177}
3178
94be91de
JB
3179/* Return the default section type based on the passed in section flags. */
3180
3181int
3182bfd_elf_get_default_section_type (flagword flags)
3183{
0e41bebb 3184 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
2e76e85a 3185 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
94be91de
JB
3186 return SHT_NOBITS;
3187 return SHT_PROGBITS;
3188}
3189
d4730f92
BS
3190struct fake_section_arg
3191{
3192 struct bfd_link_info *link_info;
3193 bfd_boolean failed;
3194};
3195
252b5132
RH
3196/* Set up an ELF internal section header for a section. */
3197
252b5132 3198static void
d4730f92 3199elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
252b5132 3200{
d4730f92 3201 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
9c5bfbb7 3202 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3203 struct bfd_elf_section_data *esd = elf_section_data (asect);
252b5132 3204 Elf_Internal_Shdr *this_hdr;
0414f35b 3205 unsigned int sh_type;
0ce398f1 3206 const char *name = asect->name;
3e19fb8f 3207 bfd_boolean delay_st_name_p = FALSE;
252b5132 3208
d4730f92 3209 if (arg->failed)
252b5132
RH
3210 {
3211 /* We already failed; just get out of the bfd_map_over_sections
08a40648 3212 loop. */
252b5132
RH
3213 return;
3214 }
3215
d4730f92 3216 this_hdr = &esd->this_hdr;
252b5132 3217
f6fe1ccd 3218 if (arg->link_info)
0ce398f1 3219 {
f6fe1ccd
L
3220 /* ld: compress DWARF debug sections with names: .debug_*. */
3221 if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
3222 && (asect->flags & SEC_DEBUGGING)
3223 && name[1] == 'd'
3224 && name[6] == '_')
3225 {
3226 /* Set SEC_ELF_COMPRESS to indicate this section should be
3227 compressed. */
3228 asect->flags |= SEC_ELF_COMPRESS;
0ce398f1 3229
dd905818 3230 /* If this section will be compressed, delay adding section
3e19fb8f
L
3231 name to section name section after it is compressed in
3232 _bfd_elf_assign_file_positions_for_non_load. */
3233 delay_st_name_p = TRUE;
f6fe1ccd
L
3234 }
3235 }
3236 else if ((asect->flags & SEC_ELF_RENAME))
3237 {
3238 /* objcopy: rename output DWARF debug section. */
3239 if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
3240 {
3241 /* When we decompress or compress with SHF_COMPRESSED,
3242 convert section name from .zdebug_* to .debug_* if
3243 needed. */
3244 if (name[1] == 'z')
3245 {
3246 char *new_name = convert_zdebug_to_debug (abfd, name);
3247 if (new_name == NULL)
3248 {
3249 arg->failed = TRUE;
3250 return;
3251 }
3252 name = new_name;
3253 }
3254 }
3255 else if (asect->compress_status == COMPRESS_SECTION_DONE)
0ce398f1 3256 {
f6fe1ccd
L
3257 /* PR binutils/18087: Compression does not always make a
3258 section smaller. So only rename the section when
3259 compression has actually taken place. If input section
3260 name is .zdebug_*, we should never compress it again. */
3261 char *new_name = convert_debug_to_zdebug (abfd, name);
0ce398f1
L
3262 if (new_name == NULL)
3263 {
3264 arg->failed = TRUE;
3265 return;
3266 }
f6fe1ccd
L
3267 BFD_ASSERT (name[1] != 'z');
3268 name = new_name;
0ce398f1
L
3269 }
3270 }
3271
3e19fb8f
L
3272 if (delay_st_name_p)
3273 this_hdr->sh_name = (unsigned int) -1;
3274 else
252b5132 3275 {
3e19fb8f
L
3276 this_hdr->sh_name
3277 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3278 name, FALSE);
3279 if (this_hdr->sh_name == (unsigned int) -1)
3280 {
3281 arg->failed = TRUE;
3282 return;
3283 }
252b5132
RH
3284 }
3285
a4d8e49b 3286 /* Don't clear sh_flags. Assembler may set additional bits. */
252b5132
RH
3287
3288 if ((asect->flags & SEC_ALLOC) != 0
3289 || asect->user_set_vma)
3290 this_hdr->sh_addr = asect->vma;
3291 else
3292 this_hdr->sh_addr = 0;
3293
3294 this_hdr->sh_offset = 0;
eea6121a 3295 this_hdr->sh_size = asect->size;
252b5132 3296 this_hdr->sh_link = 0;
c86934ce
NC
3297 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3298 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3299 {
4eca0228 3300 _bfd_error_handler
695344c0 3301 /* xgettext:c-format */
9793eb77 3302 (_("%pB: error: alignment power %d of section `%pA' is too big"),
c08bb8dd 3303 abfd, asect->alignment_power, asect);
c86934ce
NC
3304 arg->failed = TRUE;
3305 return;
3306 }
72de5009 3307 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
252b5132
RH
3308 /* The sh_entsize and sh_info fields may have been set already by
3309 copy_private_section_data. */
3310
3311 this_hdr->bfd_section = asect;
3312 this_hdr->contents = NULL;
3313
3cddba1e
L
3314 /* If the section type is unspecified, we set it based on
3315 asect->flags. */
98ece1b3
AM
3316 if ((asect->flags & SEC_GROUP) != 0)
3317 sh_type = SHT_GROUP;
98ece1b3 3318 else
94be91de 3319 sh_type = bfd_elf_get_default_section_type (asect->flags);
98ece1b3 3320
3cddba1e 3321 if (this_hdr->sh_type == SHT_NULL)
98ece1b3
AM
3322 this_hdr->sh_type = sh_type;
3323 else if (this_hdr->sh_type == SHT_NOBITS
3324 && sh_type == SHT_PROGBITS
3325 && (asect->flags & SEC_ALLOC) != 0)
3cddba1e 3326 {
98ece1b3
AM
3327 /* Warn if we are changing a NOBITS section to PROGBITS, but
3328 allow the link to proceed. This can happen when users link
3329 non-bss input sections to bss output sections, or emit data
3330 to a bss output section via a linker script. */
4eca0228 3331 _bfd_error_handler
871b3ab2 3332 (_("warning: section `%pA' type changed to PROGBITS"), asect);
98ece1b3 3333 this_hdr->sh_type = sh_type;
3cddba1e
L
3334 }
3335
2f89ff8d 3336 switch (this_hdr->sh_type)
252b5132 3337 {
2f89ff8d 3338 default:
2f89ff8d
L
3339 break;
3340
3341 case SHT_STRTAB:
2f89ff8d
L
3342 case SHT_NOTE:
3343 case SHT_NOBITS:
3344 case SHT_PROGBITS:
3345 break;
606851fb
AM
3346
3347 case SHT_INIT_ARRAY:
3348 case SHT_FINI_ARRAY:
3349 case SHT_PREINIT_ARRAY:
3350 this_hdr->sh_entsize = bed->s->arch_size / 8;
3351 break;
2f89ff8d
L
3352
3353 case SHT_HASH:
c7ac6ff8 3354 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2f89ff8d 3355 break;
5de3bf90 3356
2f89ff8d 3357 case SHT_DYNSYM:
252b5132 3358 this_hdr->sh_entsize = bed->s->sizeof_sym;
2f89ff8d
L
3359 break;
3360
3361 case SHT_DYNAMIC:
252b5132 3362 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2f89ff8d
L
3363 break;
3364
3365 case SHT_RELA:
3366 if (get_elf_backend_data (abfd)->may_use_rela_p)
3367 this_hdr->sh_entsize = bed->s->sizeof_rela;
3368 break;
3369
3370 case SHT_REL:
3371 if (get_elf_backend_data (abfd)->may_use_rel_p)
3372 this_hdr->sh_entsize = bed->s->sizeof_rel;
3373 break;
3374
3375 case SHT_GNU_versym:
252b5132 3376 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2f89ff8d
L
3377 break;
3378
3379 case SHT_GNU_verdef:
252b5132
RH
3380 this_hdr->sh_entsize = 0;
3381 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3382 cverdefs. The linker will set cverdefs, but sh_info will be
3383 zero. */
252b5132
RH
3384 if (this_hdr->sh_info == 0)
3385 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3386 else
3387 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3388 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2f89ff8d
L
3389 break;
3390
3391 case SHT_GNU_verneed:
252b5132
RH
3392 this_hdr->sh_entsize = 0;
3393 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3394 cverrefs. The linker will set cverrefs, but sh_info will be
3395 zero. */
252b5132
RH
3396 if (this_hdr->sh_info == 0)
3397 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3398 else
3399 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3400 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2f89ff8d
L
3401 break;
3402
3403 case SHT_GROUP:
1783205a 3404 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2f89ff8d 3405 break;
fdc90cb4
JJ
3406
3407 case SHT_GNU_HASH:
3408 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3409 break;
dbb410c3 3410 }
252b5132
RH
3411
3412 if ((asect->flags & SEC_ALLOC) != 0)
3413 this_hdr->sh_flags |= SHF_ALLOC;
3414 if ((asect->flags & SEC_READONLY) == 0)
3415 this_hdr->sh_flags |= SHF_WRITE;
3416 if ((asect->flags & SEC_CODE) != 0)
3417 this_hdr->sh_flags |= SHF_EXECINSTR;
f5fa8ca2
JJ
3418 if ((asect->flags & SEC_MERGE) != 0)
3419 {
3420 this_hdr->sh_flags |= SHF_MERGE;
3421 this_hdr->sh_entsize = asect->entsize;
f5fa8ca2 3422 }
84865015
NC
3423 if ((asect->flags & SEC_STRINGS) != 0)
3424 this_hdr->sh_flags |= SHF_STRINGS;
1126897b 3425 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
dbb410c3 3426 this_hdr->sh_flags |= SHF_GROUP;
13ae64f3 3427 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
704afa60
JJ
3428 {
3429 this_hdr->sh_flags |= SHF_TLS;
3a800eb9
AM
3430 if (asect->size == 0
3431 && (asect->flags & SEC_HAS_CONTENTS) == 0)
704afa60 3432 {
3a800eb9 3433 struct bfd_link_order *o = asect->map_tail.link_order;
b34976b6 3434
704afa60 3435 this_hdr->sh_size = 0;
3a800eb9
AM
3436 if (o != NULL)
3437 {
704afa60 3438 this_hdr->sh_size = o->offset + o->size;
3a800eb9
AM
3439 if (this_hdr->sh_size != 0)
3440 this_hdr->sh_type = SHT_NOBITS;
3441 }
704afa60
JJ
3442 }
3443 }
18ae9cc1
L
3444 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3445 this_hdr->sh_flags |= SHF_EXCLUDE;
252b5132 3446
d4730f92
BS
3447 /* If the section has relocs, set up a section header for the
3448 SHT_REL[A] section. If two relocation sections are required for
3449 this section, it is up to the processor-specific back-end to
3450 create the other. */
3451 if ((asect->flags & SEC_RELOC) != 0)
3452 {
3453 /* When doing a relocatable link, create both REL and RELA sections if
3454 needed. */
3455 if (arg->link_info
3456 /* Do the normal setup if we wouldn't create any sections here. */
3457 && esd->rel.count + esd->rela.count > 0
0e1862bb
L
3458 && (bfd_link_relocatable (arg->link_info)
3459 || arg->link_info->emitrelocations))
d4730f92
BS
3460 {
3461 if (esd->rel.count && esd->rel.hdr == NULL
28e07a05 3462 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
db4677b8 3463 FALSE, delay_st_name_p))
d4730f92
BS
3464 {
3465 arg->failed = TRUE;
3466 return;
3467 }
3468 if (esd->rela.count && esd->rela.hdr == NULL
28e07a05 3469 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
db4677b8 3470 TRUE, delay_st_name_p))
d4730f92
BS
3471 {
3472 arg->failed = TRUE;
3473 return;
3474 }
3475 }
3476 else if (!_bfd_elf_init_reloc_shdr (abfd,
3477 (asect->use_rela_p
3478 ? &esd->rela : &esd->rel),
f6fe1ccd 3479 name,
3e19fb8f
L
3480 asect->use_rela_p,
3481 delay_st_name_p))
db4677b8 3482 {
d4730f92 3483 arg->failed = TRUE;
db4677b8
AM
3484 return;
3485 }
d4730f92
BS
3486 }
3487
252b5132 3488 /* Check for processor-specific section types. */
0414f35b 3489 sh_type = this_hdr->sh_type;
e1fddb6b
AO
3490 if (bed->elf_backend_fake_sections
3491 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
db4677b8
AM
3492 {
3493 arg->failed = TRUE;
3494 return;
3495 }
252b5132 3496
42bb2e33 3497 if (sh_type == SHT_NOBITS && asect->size != 0)
0414f35b
AM
3498 {
3499 /* Don't change the header type from NOBITS if we are being
42bb2e33 3500 called for objcopy --only-keep-debug. */
0414f35b
AM
3501 this_hdr->sh_type = sh_type;
3502 }
252b5132
RH
3503}
3504
bcacc0f5
AM
3505/* Fill in the contents of a SHT_GROUP section. Called from
3506 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3507 when ELF targets use the generic linker, ld. Called for ld -r
3508 from bfd_elf_final_link. */
dbb410c3 3509
1126897b 3510void
217aa764 3511bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
dbb410c3 3512{
a50b1753 3513 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
9dce4196 3514 asection *elt, *first;
dbb410c3 3515 unsigned char *loc;
b34976b6 3516 bfd_boolean gas;
dbb410c3 3517
7e4111ad
L
3518 /* Ignore linker created group section. See elfNN_ia64_object_p in
3519 elfxx-ia64.c. */
ce5aecf8
AM
3520 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3521 || sec->size == 0
dbb410c3
AM
3522 || *failedptr)
3523 return;
3524
bcacc0f5
AM
3525 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3526 {
3527 unsigned long symindx = 0;
3528
3529 /* elf_group_id will have been set up by objcopy and the
3530 generic linker. */
3531 if (elf_group_id (sec) != NULL)
3532 symindx = elf_group_id (sec)->udata.i;
1126897b 3533
bcacc0f5
AM
3534 if (symindx == 0)
3535 {
3536 /* If called from the assembler, swap_out_syms will have set up
3537 elf_section_syms. */
3538 BFD_ASSERT (elf_section_syms (abfd) != NULL);
3539 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3540 }
3541 elf_section_data (sec)->this_hdr.sh_info = symindx;
3542 }
3543 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
1126897b 3544 {
bcacc0f5
AM
3545 /* The ELF backend linker sets sh_info to -2 when the group
3546 signature symbol is global, and thus the index can't be
3547 set until all local symbols are output. */
53720c49
AM
3548 asection *igroup;
3549 struct bfd_elf_section_data *sec_data;
3550 unsigned long symndx;
3551 unsigned long extsymoff;
bcacc0f5
AM
3552 struct elf_link_hash_entry *h;
3553
53720c49
AM
3554 /* The point of this little dance to the first SHF_GROUP section
3555 then back to the SHT_GROUP section is that this gets us to
3556 the SHT_GROUP in the input object. */
3557 igroup = elf_sec_group (elf_next_in_group (sec));
3558 sec_data = elf_section_data (igroup);
3559 symndx = sec_data->this_hdr.sh_info;
3560 extsymoff = 0;
bcacc0f5
AM
3561 if (!elf_bad_symtab (igroup->owner))
3562 {
3563 Elf_Internal_Shdr *symtab_hdr;
3564
3565 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3566 extsymoff = symtab_hdr->sh_info;
3567 }
3568 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3569 while (h->root.type == bfd_link_hash_indirect
3570 || h->root.type == bfd_link_hash_warning)
3571 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3572
3573 elf_section_data (sec)->this_hdr.sh_info = h->indx;
1126897b 3574 }
dbb410c3 3575
1126897b 3576 /* The contents won't be allocated for "ld -r" or objcopy. */
b34976b6 3577 gas = TRUE;
dbb410c3
AM
3578 if (sec->contents == NULL)
3579 {
b34976b6 3580 gas = FALSE;
a50b1753 3581 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
9dce4196
AM
3582
3583 /* Arrange for the section to be written out. */
3584 elf_section_data (sec)->this_hdr.contents = sec->contents;
dbb410c3
AM
3585 if (sec->contents == NULL)
3586 {
b34976b6 3587 *failedptr = TRUE;
dbb410c3
AM
3588 return;
3589 }
3590 }
3591
eea6121a 3592 loc = sec->contents + sec->size;
dbb410c3 3593
9dce4196
AM
3594 /* Get the pointer to the first section in the group that gas
3595 squirreled away here. objcopy arranges for this to be set to the
3596 start of the input section group. */
3597 first = elt = elf_next_in_group (sec);
dbb410c3
AM
3598
3599 /* First element is a flag word. Rest of section is elf section
3600 indices for all the sections of the group. Write them backwards
3601 just to keep the group in the same order as given in .section
3602 directives, not that it matters. */
3603 while (elt != NULL)
3604 {
9dce4196 3605 asection *s;
9dce4196 3606
9dce4196 3607 s = elt;
415f38a6
AM
3608 if (!gas)
3609 s = s->output_section;
3610 if (s != NULL
3611 && !bfd_is_abs_section (s))
01e1a5bc 3612 {
db4677b8 3613 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
28e07a05
AM
3614 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3615
3616 if (elf_sec->rel.hdr != NULL
3617 && (gas
3618 || (input_elf_sec->rel.hdr != NULL
3619 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3620 {
28e07a05 3621 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
db4677b8
AM
3622 loc -= 4;
3623 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3624 }
28e07a05
AM
3625 if (elf_sec->rela.hdr != NULL
3626 && (gas
3627 || (input_elf_sec->rela.hdr != NULL
3628 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3629 {
28e07a05 3630 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
db4677b8
AM
3631 loc -= 4;
3632 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3633 }
01e1a5bc 3634 loc -= 4;
db4677b8 3635 H_PUT_32 (abfd, elf_sec->this_idx, loc);
01e1a5bc 3636 }
945906ff 3637 elt = elf_next_in_group (elt);
9dce4196
AM
3638 if (elt == first)
3639 break;
dbb410c3
AM
3640 }
3641
7bdf4127
AB
3642 loc -= 4;
3643 BFD_ASSERT (loc == sec->contents);
dbb410c3 3644
9dce4196 3645 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
dbb410c3
AM
3646}
3647
bce964aa
AM
3648/* Given NAME, the name of a relocation section stripped of its
3649 .rel/.rela prefix, return the section in ABFD to which the
3650 relocations apply. */
bd53a53a
L
3651
3652asection *
bce964aa
AM
3653_bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3654{
3655 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3656 section likely apply to .got.plt or .got section. */
3657 if (get_elf_backend_data (abfd)->want_got_plt
3658 && strcmp (name, ".plt") == 0)
3659 {
3660 asection *sec;
3661
3662 name = ".got.plt";
3663 sec = bfd_get_section_by_name (abfd, name);
3664 if (sec != NULL)
3665 return sec;
3666 name = ".got";
3667 }
3668
3669 return bfd_get_section_by_name (abfd, name);
3670}
3671
3672/* Return the section to which RELOC_SEC applies. */
3673
3674static asection *
3675elf_get_reloc_section (asection *reloc_sec)
bd53a53a
L
3676{
3677 const char *name;
3678 unsigned int type;
3679 bfd *abfd;
bce964aa 3680 const struct elf_backend_data *bed;
bd53a53a
L
3681
3682 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3683 if (type != SHT_REL && type != SHT_RELA)
3684 return NULL;
3685
3686 /* We look up the section the relocs apply to by name. */
3687 name = reloc_sec->name;
bce964aa
AM
3688 if (strncmp (name, ".rel", 4) != 0)
3689 return NULL;
3690 name += 4;
3691 if (type == SHT_RELA && *name++ != 'a')
3692 return NULL;
bd53a53a 3693
bd53a53a 3694 abfd = reloc_sec->owner;
bce964aa
AM
3695 bed = get_elf_backend_data (abfd);
3696 return bed->get_reloc_section (abfd, name);
bd53a53a
L
3697}
3698
252b5132
RH
3699/* Assign all ELF section numbers. The dummy first section is handled here
3700 too. The link/info pointers for the standard section types are filled
3701 in here too, while we're at it. */
3702
b34976b6 3703static bfd_boolean
da9f89d4 3704assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
252b5132
RH
3705{
3706 struct elf_obj_tdata *t = elf_tdata (abfd);
3707 asection *sec;
3e19fb8f 3708 unsigned int section_number;
252b5132 3709 Elf_Internal_Shdr **i_shdrp;
47cc2cf5 3710 struct bfd_elf_section_data *d;
3516e984 3711 bfd_boolean need_symtab;
252b5132
RH
3712
3713 section_number = 1;
3714
2b0f7ef9
JJ
3715 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3716
da9f89d4 3717 /* SHT_GROUP sections are in relocatable files only. */
7bdf4127 3718 if (link_info == NULL || !link_info->resolve_section_groups)
252b5132 3719 {
ef53be89 3720 size_t reloc_count = 0;
14f2c699 3721
da9f89d4 3722 /* Put SHT_GROUP sections first. */
04dd1667 3723 for (sec = abfd->sections; sec != NULL; sec = sec->next)
47cc2cf5 3724 {
5daa8fe7 3725 d = elf_section_data (sec);
da9f89d4
L
3726
3727 if (d->this_hdr.sh_type == SHT_GROUP)
08a40648 3728 {
5daa8fe7 3729 if (sec->flags & SEC_LINKER_CREATED)
da9f89d4
L
3730 {
3731 /* Remove the linker created SHT_GROUP sections. */
5daa8fe7 3732 bfd_section_list_remove (abfd, sec);
da9f89d4 3733 abfd->section_count--;
da9f89d4 3734 }
08a40648 3735 else
4fbb74a6 3736 d->this_idx = section_number++;
da9f89d4 3737 }
14f2c699
L
3738
3739 /* Count relocations. */
3740 reloc_count += sec->reloc_count;
47cc2cf5 3741 }
14f2c699
L
3742
3743 /* Clear HAS_RELOC if there are no relocations. */
3744 if (reloc_count == 0)
3745 abfd->flags &= ~HAS_RELOC;
47cc2cf5
PB
3746 }
3747
3748 for (sec = abfd->sections; sec; sec = sec->next)
3749 {
3750 d = elf_section_data (sec);
3751
3752 if (d->this_hdr.sh_type != SHT_GROUP)
4fbb74a6 3753 d->this_idx = section_number++;
3e19fb8f
L
3754 if (d->this_hdr.sh_name != (unsigned int) -1)
3755 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
d4730f92 3756 if (d->rel.hdr)
2b0f7ef9 3757 {
d4730f92 3758 d->rel.idx = section_number++;
3e19fb8f
L
3759 if (d->rel.hdr->sh_name != (unsigned int) -1)
3760 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
2b0f7ef9 3761 }
d4730f92
BS
3762 else
3763 d->rel.idx = 0;
23bc299b 3764
d4730f92 3765 if (d->rela.hdr)
2b0f7ef9 3766 {
d4730f92 3767 d->rela.idx = section_number++;
3e19fb8f
L
3768 if (d->rela.hdr->sh_name != (unsigned int) -1)
3769 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
2b0f7ef9 3770 }
23bc299b 3771 else
d4730f92 3772 d->rela.idx = 0;
252b5132
RH
3773 }
3774
3516e984
L
3775 need_symtab = (bfd_get_symcount (abfd) > 0
3776 || (link_info == NULL
3777 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3778 == HAS_RELOC)));
3779 if (need_symtab)
252b5132 3780 {
12bd6957 3781 elf_onesymtab (abfd) = section_number++;
2b0f7ef9 3782 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4fbb74a6 3783 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
9ad5cbcf 3784 {
7a6e0d89 3785 elf_section_list *entry;
6a40cf0c
NC
3786
3787 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3788
7a6e0d89 3789 entry = bfd_zalloc (abfd, sizeof (*entry));
6a40cf0c
NC
3790 entry->ndx = section_number++;
3791 elf_symtab_shndx_list (abfd) = entry;
3792 entry->hdr.sh_name
9ad5cbcf 3793 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
b34976b6 3794 ".symtab_shndx", FALSE);
6a40cf0c 3795 if (entry->hdr.sh_name == (unsigned int) -1)
b34976b6 3796 return FALSE;
9ad5cbcf 3797 }
12bd6957 3798 elf_strtab_sec (abfd) = section_number++;
2b0f7ef9 3799 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
252b5132
RH
3800 }
3801
dd905818
NC
3802 elf_shstrtab_sec (abfd) = section_number++;
3803 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3804 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3805
1c52a645
L
3806 if (section_number >= SHN_LORESERVE)
3807 {
695344c0 3808 /* xgettext:c-format */
871b3ab2 3809 _bfd_error_handler (_("%pB: too many sections: %u"),
1c52a645
L
3810 abfd, section_number);
3811 return FALSE;
3812 }
3813
9ad5cbcf 3814 elf_numsections (abfd) = section_number;
252b5132
RH
3815 elf_elfheader (abfd)->e_shnum = section_number;
3816
3817 /* Set up the list of section header pointers, in agreement with the
3818 indices. */
a50b1753 3819 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
07d6d2b8 3820 sizeof (Elf_Internal_Shdr *));
252b5132 3821 if (i_shdrp == NULL)
b34976b6 3822 return FALSE;
252b5132 3823
a50b1753 3824 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
07d6d2b8 3825 sizeof (Elf_Internal_Shdr));
252b5132
RH
3826 if (i_shdrp[0] == NULL)
3827 {
3828 bfd_release (abfd, i_shdrp);
b34976b6 3829 return FALSE;
252b5132 3830 }
252b5132
RH
3831
3832 elf_elfsections (abfd) = i_shdrp;
3833
12bd6957 3834 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3516e984 3835 if (need_symtab)
252b5132 3836 {
12bd6957 3837 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4fbb74a6 3838 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
9ad5cbcf 3839 {
6a40cf0c
NC
3840 elf_section_list * entry = elf_symtab_shndx_list (abfd);
3841 BFD_ASSERT (entry != NULL);
3842 i_shdrp[entry->ndx] = & entry->hdr;
3843 entry->hdr.sh_link = elf_onesymtab (abfd);
9ad5cbcf 3844 }
12bd6957
AM
3845 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3846 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
252b5132 3847 }
38ce5b11 3848
252b5132
RH
3849 for (sec = abfd->sections; sec; sec = sec->next)
3850 {
252b5132 3851 asection *s;
252b5132 3852
91d6fa6a
NC
3853 d = elf_section_data (sec);
3854
252b5132 3855 i_shdrp[d->this_idx] = &d->this_hdr;
d4730f92
BS
3856 if (d->rel.idx != 0)
3857 i_shdrp[d->rel.idx] = d->rel.hdr;
3858 if (d->rela.idx != 0)
3859 i_shdrp[d->rela.idx] = d->rela.hdr;
252b5132
RH
3860
3861 /* Fill in the sh_link and sh_info fields while we're at it. */
3862
3863 /* sh_link of a reloc section is the section index of the symbol
3864 table. sh_info is the section index of the section to which
3865 the relocation entries apply. */
d4730f92 3866 if (d->rel.idx != 0)
252b5132 3867 {
12bd6957 3868 d->rel.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3869 d->rel.hdr->sh_info = d->this_idx;
9ef5d938 3870 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
252b5132 3871 }
d4730f92 3872 if (d->rela.idx != 0)
23bc299b 3873 {
12bd6957 3874 d->rela.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3875 d->rela.hdr->sh_info = d->this_idx;
9ef5d938 3876 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
23bc299b 3877 }
252b5132 3878
38ce5b11
L
3879 /* We need to set up sh_link for SHF_LINK_ORDER. */
3880 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3881 {
3882 s = elf_linked_to_section (sec);
3883 if (s)
38ce5b11 3884 {
f2876037 3885 /* elf_linked_to_section points to the input section. */
ccd2ec6a 3886 if (link_info != NULL)
38ce5b11 3887 {
f2876037 3888 /* Check discarded linkonce section. */
dbaa2011 3889 if (discarded_section (s))
38ce5b11 3890 {
ccd2ec6a 3891 asection *kept;
4eca0228 3892 _bfd_error_handler
695344c0 3893 /* xgettext:c-format */
871b3ab2
AM
3894 (_("%pB: sh_link of section `%pA' points to"
3895 " discarded section `%pA' of `%pB'"),
ccd2ec6a
L
3896 abfd, d->this_hdr.bfd_section,
3897 s, s->owner);
3898 /* Point to the kept section if it has the same
3899 size as the discarded one. */
c0f00686 3900 kept = _bfd_elf_check_kept_section (s, link_info);
ccd2ec6a 3901 if (kept == NULL)
185d09ad 3902 {
ccd2ec6a
L
3903 bfd_set_error (bfd_error_bad_value);
3904 return FALSE;
185d09ad 3905 }
ccd2ec6a 3906 s = kept;
38ce5b11 3907 }
e424ecc8 3908
ccd2ec6a
L
3909 s = s->output_section;
3910 BFD_ASSERT (s != NULL);
38ce5b11 3911 }
f2876037
L
3912 else
3913 {
3914 /* Handle objcopy. */
3915 if (s->output_section == NULL)
3916 {
4eca0228 3917 _bfd_error_handler
695344c0 3918 /* xgettext:c-format */
871b3ab2
AM
3919 (_("%pB: sh_link of section `%pA' points to"
3920 " removed section `%pA' of `%pB'"),
f2876037
L
3921 abfd, d->this_hdr.bfd_section, s, s->owner);
3922 bfd_set_error (bfd_error_bad_value);
3923 return FALSE;
3924 }
3925 s = s->output_section;
3926 }
ccd2ec6a
L
3927 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3928 }
3929 else
3930 {
3931 /* PR 290:
3932 The Intel C compiler generates SHT_IA_64_UNWIND with
3933 SHF_LINK_ORDER. But it doesn't set the sh_link or
3934 sh_info fields. Hence we could get the situation
08a40648 3935 where s is NULL. */
ccd2ec6a
L
3936 const struct elf_backend_data *bed
3937 = get_elf_backend_data (abfd);
3938 if (bed->link_order_error_handler)
3939 bed->link_order_error_handler
695344c0 3940 /* xgettext:c-format */
871b3ab2 3941 (_("%pB: warning: sh_link not set for section `%pA'"),
ccd2ec6a 3942 abfd, sec);
38ce5b11
L
3943 }
3944 }
3945
252b5132
RH
3946 switch (d->this_hdr.sh_type)
3947 {
3948 case SHT_REL:
3949 case SHT_RELA:
3950 /* A reloc section which we are treating as a normal BFD
3951 section. sh_link is the section index of the symbol
3952 table. sh_info is the section index of the section to
3953 which the relocation entries apply. We assume that an
3954 allocated reloc section uses the dynamic symbol table.
3955 FIXME: How can we be sure? */
3956 s = bfd_get_section_by_name (abfd, ".dynsym");
3957 if (s != NULL)
3958 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3959
bce964aa 3960 s = elf_get_reloc_section (sec);
252b5132 3961 if (s != NULL)
9ef5d938
L
3962 {
3963 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3964 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3965 }
252b5132
RH
3966 break;
3967
3968 case SHT_STRTAB:
3969 /* We assume that a section named .stab*str is a stabs
3970 string section. We look for a section with the same name
3971 but without the trailing ``str'', and set its sh_link
3972 field to point to this section. */
0112cd26 3973 if (CONST_STRNEQ (sec->name, ".stab")
252b5132
RH
3974 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3975 {
3976 size_t len;
3977 char *alc;
3978
3979 len = strlen (sec->name);
a50b1753 3980 alc = (char *) bfd_malloc (len - 2);
252b5132 3981 if (alc == NULL)
b34976b6 3982 return FALSE;
d4c88bbb 3983 memcpy (alc, sec->name, len - 3);
252b5132
RH
3984 alc[len - 3] = '\0';
3985 s = bfd_get_section_by_name (abfd, alc);
3986 free (alc);
3987 if (s != NULL)
3988 {
3989 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3990
3991 /* This is a .stab section. */
0594c12d
AM
3992 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3993 elf_section_data (s)->this_hdr.sh_entsize
3994 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
252b5132
RH
3995 }
3996 }
3997 break;
3998
3999 case SHT_DYNAMIC:
4000 case SHT_DYNSYM:
4001 case SHT_GNU_verneed:
4002 case SHT_GNU_verdef:
4003 /* sh_link is the section header index of the string table
4004 used for the dynamic entries, or the symbol table, or the
4005 version strings. */
4006 s = bfd_get_section_by_name (abfd, ".dynstr");
4007 if (s != NULL)
4008 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4009 break;
4010
7f1204bb
JJ
4011 case SHT_GNU_LIBLIST:
4012 /* sh_link is the section header index of the prelink library
08a40648
AM
4013 list used for the dynamic entries, or the symbol table, or
4014 the version strings. */
7f1204bb
JJ
4015 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
4016 ? ".dynstr" : ".gnu.libstr");
4017 if (s != NULL)
4018 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4019 break;
4020
252b5132 4021 case SHT_HASH:
fdc90cb4 4022 case SHT_GNU_HASH:
252b5132
RH
4023 case SHT_GNU_versym:
4024 /* sh_link is the section header index of the symbol table
4025 this hash table or version table is for. */
4026 s = bfd_get_section_by_name (abfd, ".dynsym");
4027 if (s != NULL)
4028 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4029 break;
dbb410c3
AM
4030
4031 case SHT_GROUP:
12bd6957 4032 d->this_hdr.sh_link = elf_onesymtab (abfd);
252b5132
RH
4033 }
4034 }
4035
3e19fb8f
L
4036 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4037 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4038 debug section name from .debug_* to .zdebug_* if needed. */
4039
b34976b6 4040 return TRUE;
252b5132
RH
4041}
4042
5372391b 4043static bfd_boolean
217aa764 4044sym_is_global (bfd *abfd, asymbol *sym)
252b5132
RH
4045{
4046 /* If the backend has a special mapping, use it. */
9c5bfbb7 4047 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764
AM
4048 if (bed->elf_backend_sym_is_global)
4049 return (*bed->elf_backend_sym_is_global) (abfd, sym);
252b5132 4050
e47bf690 4051 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
252b5132
RH
4052 || bfd_is_und_section (bfd_get_section (sym))
4053 || bfd_is_com_section (bfd_get_section (sym)));
4054}
4055
76359541
TP
4056/* Filter global symbols of ABFD to include in the import library. All
4057 SYMCOUNT symbols of ABFD can be examined from their pointers in
4058 SYMS. Pointers of symbols to keep should be stored contiguously at
4059 the beginning of that array.
4060
4061 Returns the number of symbols to keep. */
4062
4063unsigned int
4064_bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4065 asymbol **syms, long symcount)
4066{
4067 long src_count, dst_count = 0;
4068
4069 for (src_count = 0; src_count < symcount; src_count++)
4070 {
4071 asymbol *sym = syms[src_count];
4072 char *name = (char *) bfd_asymbol_name (sym);
4073 struct bfd_link_hash_entry *h;
4074
4075 if (!sym_is_global (abfd, sym))
4076 continue;
4077
4078 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE);
5df1bc57
AM
4079 if (h == NULL)
4080 continue;
76359541
TP
4081 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4082 continue;
76359541
TP
4083 if (h->linker_def || h->ldscript_def)
4084 continue;
4085
4086 syms[dst_count++] = sym;
4087 }
4088
4089 syms[dst_count] = NULL;
4090
4091 return dst_count;
4092}
4093
5372391b 4094/* Don't output section symbols for sections that are not going to be
c6d8cab4 4095 output, that are duplicates or there is no BFD section. */
5372391b
AM
4096
4097static bfd_boolean
4098ignore_section_sym (bfd *abfd, asymbol *sym)
4099{
c6d8cab4
L
4100 elf_symbol_type *type_ptr;
4101
db0c309f
NC
4102 if (sym == NULL)
4103 return FALSE;
4104
c6d8cab4
L
4105 if ((sym->flags & BSF_SECTION_SYM) == 0)
4106 return FALSE;
4107
db0c309f
NC
4108 if (sym->section == NULL)
4109 return TRUE;
4110
c6d8cab4
L
4111 type_ptr = elf_symbol_from (abfd, sym);
4112 return ((type_ptr != NULL
4113 && type_ptr->internal_elf_sym.st_shndx != 0
4114 && bfd_is_abs_section (sym->section))
4115 || !(sym->section->owner == abfd
db0c309f
NC
4116 || (sym->section->output_section != NULL
4117 && sym->section->output_section->owner == abfd
2633a79c
AM
4118 && sym->section->output_offset == 0)
4119 || bfd_is_abs_section (sym->section)));
5372391b
AM
4120}
4121
2633a79c
AM
4122/* Map symbol from it's internal number to the external number, moving
4123 all local symbols to be at the head of the list. */
4124
b34976b6 4125static bfd_boolean
12bd6957 4126elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
252b5132 4127{
dc810e39 4128 unsigned int symcount = bfd_get_symcount (abfd);
252b5132
RH
4129 asymbol **syms = bfd_get_outsymbols (abfd);
4130 asymbol **sect_syms;
dc810e39
AM
4131 unsigned int num_locals = 0;
4132 unsigned int num_globals = 0;
4133 unsigned int num_locals2 = 0;
4134 unsigned int num_globals2 = 0;
7292b3ac 4135 unsigned int max_index = 0;
dc810e39 4136 unsigned int idx;
252b5132
RH
4137 asection *asect;
4138 asymbol **new_syms;
252b5132
RH
4139
4140#ifdef DEBUG
4141 fprintf (stderr, "elf_map_symbols\n");
4142 fflush (stderr);
4143#endif
4144
252b5132
RH
4145 for (asect = abfd->sections; asect; asect = asect->next)
4146 {
4147 if (max_index < asect->index)
4148 max_index = asect->index;
4149 }
4150
4151 max_index++;
a50b1753 4152 sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
252b5132 4153 if (sect_syms == NULL)
b34976b6 4154 return FALSE;
252b5132 4155 elf_section_syms (abfd) = sect_syms;
4e89ac30 4156 elf_num_section_syms (abfd) = max_index;
252b5132 4157
079e9a2f
AM
4158 /* Init sect_syms entries for any section symbols we have already
4159 decided to output. */
252b5132
RH
4160 for (idx = 0; idx < symcount; idx++)
4161 {
dc810e39 4162 asymbol *sym = syms[idx];
c044fabd 4163
252b5132 4164 if ((sym->flags & BSF_SECTION_SYM) != 0
0f0a5e58 4165 && sym->value == 0
2633a79c
AM
4166 && !ignore_section_sym (abfd, sym)
4167 && !bfd_is_abs_section (sym->section))
252b5132 4168 {
5372391b 4169 asection *sec = sym->section;
252b5132 4170
5372391b
AM
4171 if (sec->owner != abfd)
4172 sec = sec->output_section;
252b5132 4173
5372391b 4174 sect_syms[sec->index] = syms[idx];
252b5132
RH
4175 }
4176 }
4177
252b5132
RH
4178 /* Classify all of the symbols. */
4179 for (idx = 0; idx < symcount; idx++)
4180 {
2633a79c 4181 if (sym_is_global (abfd, syms[idx]))
252b5132 4182 num_globals++;
2633a79c
AM
4183 else if (!ignore_section_sym (abfd, syms[idx]))
4184 num_locals++;
252b5132 4185 }
079e9a2f 4186
5372391b 4187 /* We will be adding a section symbol for each normal BFD section. Most
079e9a2f
AM
4188 sections will already have a section symbol in outsymbols, but
4189 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4190 at least in that case. */
252b5132
RH
4191 for (asect = abfd->sections; asect; asect = asect->next)
4192 {
079e9a2f 4193 if (sect_syms[asect->index] == NULL)
252b5132 4194 {
079e9a2f 4195 if (!sym_is_global (abfd, asect->symbol))
252b5132
RH
4196 num_locals++;
4197 else
4198 num_globals++;
252b5132
RH
4199 }
4200 }
4201
4202 /* Now sort the symbols so the local symbols are first. */
a50b1753 4203 new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
07d6d2b8 4204 sizeof (asymbol *));
dc810e39 4205
252b5132 4206 if (new_syms == NULL)
b34976b6 4207 return FALSE;
252b5132
RH
4208
4209 for (idx = 0; idx < symcount; idx++)
4210 {
4211 asymbol *sym = syms[idx];
dc810e39 4212 unsigned int i;
252b5132 4213
2633a79c
AM
4214 if (sym_is_global (abfd, sym))
4215 i = num_locals + num_globals2++;
4216 else if (!ignore_section_sym (abfd, sym))
252b5132
RH
4217 i = num_locals2++;
4218 else
2633a79c 4219 continue;
252b5132
RH
4220 new_syms[i] = sym;
4221 sym->udata.i = i + 1;
4222 }
4223 for (asect = abfd->sections; asect; asect = asect->next)
4224 {
079e9a2f 4225 if (sect_syms[asect->index] == NULL)
252b5132 4226 {
079e9a2f 4227 asymbol *sym = asect->symbol;
dc810e39 4228 unsigned int i;
252b5132 4229
079e9a2f 4230 sect_syms[asect->index] = sym;
252b5132
RH
4231 if (!sym_is_global (abfd, sym))
4232 i = num_locals2++;
4233 else
4234 i = num_locals + num_globals2++;
4235 new_syms[i] = sym;
4236 sym->udata.i = i + 1;
4237 }
4238 }
4239
4240 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4241
12bd6957 4242 *pnum_locals = num_locals;
b34976b6 4243 return TRUE;
252b5132
RH
4244}
4245
4246/* Align to the maximum file alignment that could be required for any
4247 ELF data structure. */
4248
268b6b39 4249static inline file_ptr
217aa764 4250align_file_position (file_ptr off, int align)
252b5132
RH
4251{
4252 return (off + align - 1) & ~(align - 1);
4253}
4254
4255/* Assign a file position to a section, optionally aligning to the
4256 required section alignment. */
4257
217aa764
AM
4258file_ptr
4259_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4260 file_ptr offset,
4261 bfd_boolean align)
252b5132 4262{
72de5009
AM
4263 if (align && i_shdrp->sh_addralign > 1)
4264 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
252b5132
RH
4265 i_shdrp->sh_offset = offset;
4266 if (i_shdrp->bfd_section != NULL)
4267 i_shdrp->bfd_section->filepos = offset;
4268 if (i_shdrp->sh_type != SHT_NOBITS)
4269 offset += i_shdrp->sh_size;
4270 return offset;
4271}
4272
4273/* Compute the file positions we are going to put the sections at, and
4274 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4275 is not NULL, this is being called by the ELF backend linker. */
4276
b34976b6 4277bfd_boolean
217aa764
AM
4278_bfd_elf_compute_section_file_positions (bfd *abfd,
4279 struct bfd_link_info *link_info)
252b5132 4280{
9c5bfbb7 4281 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 4282 struct fake_section_arg fsargs;
b34976b6 4283 bfd_boolean failed;
ef10c3ac 4284 struct elf_strtab_hash *strtab = NULL;
252b5132 4285 Elf_Internal_Shdr *shstrtab_hdr;
3516e984 4286 bfd_boolean need_symtab;
252b5132
RH
4287
4288 if (abfd->output_has_begun)
b34976b6 4289 return TRUE;
252b5132
RH
4290
4291 /* Do any elf backend specific processing first. */
4292 if (bed->elf_backend_begin_write_processing)
4293 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4294
4295 if (! prep_headers (abfd))
b34976b6 4296 return FALSE;
252b5132 4297
e6c51ed4 4298 /* Post process the headers if necessary. */
78245035 4299 (*bed->elf_backend_post_process_headers) (abfd, link_info);
e6c51ed4 4300
d4730f92
BS
4301 fsargs.failed = FALSE;
4302 fsargs.link_info = link_info;
4303 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4304 if (fsargs.failed)
b34976b6 4305 return FALSE;
252b5132 4306
da9f89d4 4307 if (!assign_section_numbers (abfd, link_info))
b34976b6 4308 return FALSE;
252b5132
RH
4309
4310 /* The backend linker builds symbol table information itself. */
3516e984
L
4311 need_symtab = (link_info == NULL
4312 && (bfd_get_symcount (abfd) > 0
4313 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4314 == HAS_RELOC)));
4315 if (need_symtab)
252b5132
RH
4316 {
4317 /* Non-zero if doing a relocatable link. */
4318 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4319
4320 if (! swap_out_syms (abfd, &strtab, relocatable_p))
b34976b6 4321 return FALSE;
252b5132
RH
4322 }
4323
d4730f92 4324 failed = FALSE;
1126897b 4325 if (link_info == NULL)
dbb410c3 4326 {
1126897b 4327 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
dbb410c3 4328 if (failed)
b34976b6 4329 return FALSE;
dbb410c3
AM
4330 }
4331
252b5132
RH
4332 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4333 /* sh_name was set in prep_headers. */
4334 shstrtab_hdr->sh_type = SHT_STRTAB;
84865015 4335 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
252b5132 4336 shstrtab_hdr->sh_addr = 0;
946748d5 4337 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4338 shstrtab_hdr->sh_entsize = 0;
4339 shstrtab_hdr->sh_link = 0;
4340 shstrtab_hdr->sh_info = 0;
3e19fb8f 4341 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4342 shstrtab_hdr->sh_addralign = 1;
4343
c84fca4d 4344 if (!assign_file_positions_except_relocs (abfd, link_info))
b34976b6 4345 return FALSE;
252b5132 4346
3516e984 4347 if (need_symtab)
252b5132
RH
4348 {
4349 file_ptr off;
4350 Elf_Internal_Shdr *hdr;
4351
12bd6957 4352 off = elf_next_file_pos (abfd);
252b5132 4353
6a40cf0c 4354 hdr = & elf_symtab_hdr (abfd);
b34976b6 4355 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 4356
6a40cf0c
NC
4357 if (elf_symtab_shndx_list (abfd) != NULL)
4358 {
4359 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4360 if (hdr->sh_size != 0)
4361 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4362 /* FIXME: What about other symtab_shndx sections in the list ? */
4363 }
9ad5cbcf 4364
252b5132 4365 hdr = &elf_tdata (abfd)->strtab_hdr;
b34976b6 4366 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 4367
12bd6957 4368 elf_next_file_pos (abfd) = off;
252b5132
RH
4369
4370 /* Now that we know where the .strtab section goes, write it
08a40648 4371 out. */
252b5132 4372 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 4373 || ! _bfd_elf_strtab_emit (abfd, strtab))
b34976b6 4374 return FALSE;
ef10c3ac 4375 _bfd_elf_strtab_free (strtab);
252b5132
RH
4376 }
4377
b34976b6 4378 abfd->output_has_begun = TRUE;
252b5132 4379
b34976b6 4380 return TRUE;
252b5132
RH
4381}
4382
8ded5a0f
AM
4383/* Make an initial estimate of the size of the program header. If we
4384 get the number wrong here, we'll redo section placement. */
4385
4386static bfd_size_type
4387get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4388{
4389 size_t segs;
4390 asection *s;
2b05f1b7 4391 const struct elf_backend_data *bed;
8ded5a0f
AM
4392
4393 /* Assume we will need exactly two PT_LOAD segments: one for text
4394 and one for data. */
4395 segs = 2;
4396
4397 s = bfd_get_section_by_name (abfd, ".interp");
1b9e270b 4398 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
8ded5a0f
AM
4399 {
4400 /* If we have a loadable interpreter section, we need a
4401 PT_INTERP segment. In this case, assume we also need a
4402 PT_PHDR segment, although that may not be true for all
4403 targets. */
e9a38e0f 4404 segs += 2;
8ded5a0f
AM
4405 }
4406
4407 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4408 {
4409 /* We need a PT_DYNAMIC segment. */
4410 ++segs;
f210dcff 4411 }
08a40648 4412
ceae84aa 4413 if (info != NULL && info->relro)
f210dcff
L
4414 {
4415 /* We need a PT_GNU_RELRO segment. */
4416 ++segs;
8ded5a0f
AM
4417 }
4418
12bd6957 4419 if (elf_eh_frame_hdr (abfd))
8ded5a0f
AM
4420 {
4421 /* We need a PT_GNU_EH_FRAME segment. */
4422 ++segs;
4423 }
4424
12bd6957 4425 if (elf_stack_flags (abfd))
8ded5a0f 4426 {
2b05f1b7
L
4427 /* We need a PT_GNU_STACK segment. */
4428 ++segs;
4429 }
94b11780 4430
0a59decb
L
4431 s = bfd_get_section_by_name (abfd,
4432 NOTE_GNU_PROPERTY_SECTION_NAME);
4433 if (s != NULL && s->size != 0)
4434 {
4435 /* We need a PT_GNU_PROPERTY segment. */
4436 ++segs;
4437 }
4438
2b05f1b7
L
4439 for (s = abfd->sections; s != NULL; s = s->next)
4440 {
8ded5a0f 4441 if ((s->flags & SEC_LOAD) != 0
23e463ed 4442 && elf_section_type (s) == SHT_NOTE)
8ded5a0f 4443 {
23e463ed 4444 unsigned int alignment_power;
8ded5a0f
AM
4445 /* We need a PT_NOTE segment. */
4446 ++segs;
23e463ed
L
4447 /* Try to create just one PT_NOTE segment for all adjacent
4448 loadable SHT_NOTE sections. gABI requires that within a
4449 PT_NOTE segment (and also inside of each SHT_NOTE section)
4450 each note should have the same alignment. So we check
4451 whether the sections are correctly aligned. */
4452 alignment_power = s->alignment_power;
4453 while (s->next != NULL
4454 && s->next->alignment_power == alignment_power
4455 && (s->next->flags & SEC_LOAD) != 0
4456 && elf_section_type (s->next) == SHT_NOTE)
4457 s = s->next;
8ded5a0f
AM
4458 }
4459 }
4460
4461 for (s = abfd->sections; s != NULL; s = s->next)
4462 {
4463 if (s->flags & SEC_THREAD_LOCAL)
4464 {
4465 /* We need a PT_TLS segment. */
4466 ++segs;
4467 break;
4468 }
4469 }
4470
2b05f1b7 4471 bed = get_elf_backend_data (abfd);
a91e1603 4472
df3a023b
AM
4473 if ((abfd->flags & D_PAGED) != 0
4474 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4475 {
4476 /* Add a PT_GNU_MBIND segment for each mbind section. */
4477 unsigned int page_align_power = bfd_log2 (bed->commonpagesize);
4478 for (s = abfd->sections; s != NULL; s = s->next)
4479 if (elf_section_flags (s) & SHF_GNU_MBIND)
4480 {
4481 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4482 {
4483 _bfd_error_handler
4484 /* xgettext:c-format */
4485 (_("%pB: GNU_MBIND section `%pA' has invalid "
4486 "sh_info field: %d"),
4487 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4488 continue;
4489 }
4490 /* Align mbind section to page size. */
4491 if (s->alignment_power < page_align_power)
4492 s->alignment_power = page_align_power;
4493 segs ++;
4494 }
4495 }
4496
4497 /* Let the backend count up any program headers it might need. */
4498 if (bed->elf_backend_additional_program_headers)
8ded5a0f
AM
4499 {
4500 int a;
4501
4502 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4503 if (a == -1)
4504 abort ();
4505 segs += a;
4506 }
4507
4508 return segs * bed->s->sizeof_phdr;
4509}
4510
2ea37f1c
NC
4511/* Find the segment that contains the output_section of section. */
4512
4513Elf_Internal_Phdr *
4514_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4515{
4516 struct elf_segment_map *m;
4517 Elf_Internal_Phdr *p;
4518
12bd6957 4519 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
2ea37f1c
NC
4520 m != NULL;
4521 m = m->next, p++)
4522 {
4523 int i;
4524
4525 for (i = m->count - 1; i >= 0; i--)
4526 if (m->sections[i] == section)
4527 return p;
4528 }
4529
4530 return NULL;
4531}
4532
252b5132
RH
4533/* Create a mapping from a set of sections to a program segment. */
4534
217aa764
AM
4535static struct elf_segment_map *
4536make_mapping (bfd *abfd,
4537 asection **sections,
4538 unsigned int from,
4539 unsigned int to,
4540 bfd_boolean phdr)
252b5132
RH
4541{
4542 struct elf_segment_map *m;
4543 unsigned int i;
4544 asection **hdrpp;
dc810e39 4545 bfd_size_type amt;
252b5132 4546
00bee008
AM
4547 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4548 amt += (to - from) * sizeof (asection *);
a50b1753 4549 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
4550 if (m == NULL)
4551 return NULL;
4552 m->next = NULL;
4553 m->p_type = PT_LOAD;
4554 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4555 m->sections[i - from] = *hdrpp;
4556 m->count = to - from;
4557
4558 if (from == 0 && phdr)
4559 {
4560 /* Include the headers in the first PT_LOAD segment. */
4561 m->includes_filehdr = 1;
4562 m->includes_phdrs = 1;
4563 }
4564
4565 return m;
4566}
4567
229fcec5
MM
4568/* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4569 on failure. */
4570
4571struct elf_segment_map *
4572_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4573{
4574 struct elf_segment_map *m;
4575
a50b1753 4576 m = (struct elf_segment_map *) bfd_zalloc (abfd,
07d6d2b8 4577 sizeof (struct elf_segment_map));
229fcec5
MM
4578 if (m == NULL)
4579 return NULL;
4580 m->next = NULL;
4581 m->p_type = PT_DYNAMIC;
4582 m->count = 1;
4583 m->sections[0] = dynsec;
08a40648 4584
229fcec5
MM
4585 return m;
4586}
4587
8ded5a0f 4588/* Possibly add or remove segments from the segment map. */
252b5132 4589
b34976b6 4590static bfd_boolean
3dea8fca
AM
4591elf_modify_segment_map (bfd *abfd,
4592 struct bfd_link_info *info,
4593 bfd_boolean remove_empty_load)
252b5132 4594{
252e386e 4595 struct elf_segment_map **m;
8ded5a0f 4596 const struct elf_backend_data *bed;
252b5132 4597
8ded5a0f
AM
4598 /* The placement algorithm assumes that non allocated sections are
4599 not in PT_LOAD segments. We ensure this here by removing such
4600 sections from the segment map. We also remove excluded
252e386e
AM
4601 sections. Finally, any PT_LOAD segment without sections is
4602 removed. */
12bd6957 4603 m = &elf_seg_map (abfd);
252e386e 4604 while (*m)
8ded5a0f
AM
4605 {
4606 unsigned int i, new_count;
252b5132 4607
252e386e 4608 for (new_count = 0, i = 0; i < (*m)->count; i++)
8ded5a0f 4609 {
252e386e
AM
4610 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4611 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4612 || (*m)->p_type != PT_LOAD))
8ded5a0f 4613 {
252e386e
AM
4614 (*m)->sections[new_count] = (*m)->sections[i];
4615 new_count++;
8ded5a0f
AM
4616 }
4617 }
252e386e 4618 (*m)->count = new_count;
252b5132 4619
1a9ccd70
NC
4620 if (remove_empty_load
4621 && (*m)->p_type == PT_LOAD
4622 && (*m)->count == 0
4623 && !(*m)->includes_phdrs)
252e386e
AM
4624 *m = (*m)->next;
4625 else
4626 m = &(*m)->next;
8ded5a0f 4627 }
252b5132 4628
8ded5a0f
AM
4629 bed = get_elf_backend_data (abfd);
4630 if (bed->elf_backend_modify_segment_map != NULL)
252b5132 4631 {
252e386e 4632 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
8ded5a0f 4633 return FALSE;
252b5132 4634 }
252b5132 4635
8ded5a0f
AM
4636 return TRUE;
4637}
252b5132 4638
dbc88fc1
AM
4639#define IS_TBSS(s) \
4640 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4641
8ded5a0f 4642/* Set up a mapping from BFD sections to program segments. */
252b5132 4643
8ded5a0f
AM
4644bfd_boolean
4645_bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
4646{
4647 unsigned int count;
4648 struct elf_segment_map *m;
4649 asection **sections = NULL;
4650 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3dea8fca 4651 bfd_boolean no_user_phdrs;
252b5132 4652
12bd6957 4653 no_user_phdrs = elf_seg_map (abfd) == NULL;
d324f6d6
RM
4654
4655 if (info != NULL)
4656 info->user_phdrs = !no_user_phdrs;
4657
3dea8fca 4658 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
252b5132 4659 {
8ded5a0f
AM
4660 asection *s;
4661 unsigned int i;
4662 struct elf_segment_map *mfirst;
4663 struct elf_segment_map **pm;
4664 asection *last_hdr;
4665 bfd_vma last_size;
00bee008 4666 unsigned int hdr_index;
8ded5a0f
AM
4667 bfd_vma maxpagesize;
4668 asection **hdrpp;
64029e93 4669 bfd_boolean phdr_in_segment;
8ded5a0f 4670 bfd_boolean writable;
2888249f 4671 bfd_boolean executable;
8ded5a0f
AM
4672 int tls_count = 0;
4673 asection *first_tls = NULL;
a91e1603 4674 asection *first_mbind = NULL;
8ded5a0f
AM
4675 asection *dynsec, *eh_frame_hdr;
4676 bfd_size_type amt;
8d06853e 4677 bfd_vma addr_mask, wrap_to = 0;
64029e93 4678 bfd_size_type phdr_size;
252b5132 4679
8ded5a0f 4680 /* Select the allocated sections, and sort them. */
252b5132 4681
a50b1753 4682 sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
07d6d2b8 4683 sizeof (asection *));
8ded5a0f 4684 if (sections == NULL)
252b5132 4685 goto error_return;
252b5132 4686
8d06853e
AM
4687 /* Calculate top address, avoiding undefined behaviour of shift
4688 left operator when shift count is equal to size of type
4689 being shifted. */
4690 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4691 addr_mask = (addr_mask << 1) + 1;
4692
8ded5a0f
AM
4693 i = 0;
4694 for (s = abfd->sections; s != NULL; s = s->next)
4695 {
4696 if ((s->flags & SEC_ALLOC) != 0)
4697 {
4698 sections[i] = s;
4699 ++i;
8d06853e
AM
4700 /* A wrapping section potentially clashes with header. */
4701 if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
4702 wrap_to = (s->lma + s->size) & addr_mask;
8ded5a0f
AM
4703 }
4704 }
4705 BFD_ASSERT (i <= bfd_count_sections (abfd));
4706 count = i;
252b5132 4707
8ded5a0f 4708 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
252b5132 4709
64029e93
AM
4710 phdr_size = elf_program_header_size (abfd);
4711 if (phdr_size == (bfd_size_type) -1)
4712 phdr_size = get_program_header_size (abfd, info);
4713 phdr_size += bed->s->sizeof_ehdr;
4714 maxpagesize = bed->maxpagesize;
4715 if (maxpagesize == 0)
4716 maxpagesize = 1;
4717 phdr_in_segment = info != NULL && info->load_phdrs;
4718 if (count != 0
4719 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
4720 >= (phdr_size & (maxpagesize - 1))))
4721 /* For compatibility with old scripts that may not be using
4722 SIZEOF_HEADERS, add headers when it looks like space has
4723 been left for them. */
4724 phdr_in_segment = TRUE;
252b5132 4725
64029e93 4726 /* Build the mapping. */
8ded5a0f
AM
4727 mfirst = NULL;
4728 pm = &mfirst;
252b5132 4729
8ded5a0f
AM
4730 /* If we have a .interp section, then create a PT_PHDR segment for
4731 the program headers and a PT_INTERP segment for the .interp
4732 section. */
4733 s = bfd_get_section_by_name (abfd, ".interp");
1b9e270b 4734 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
8ded5a0f
AM
4735 {
4736 amt = sizeof (struct elf_segment_map);
a50b1753 4737 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4738 if (m == NULL)
4739 goto error_return;
4740 m->next = NULL;
4741 m->p_type = PT_PHDR;
f882209d 4742 m->p_flags = PF_R;
8ded5a0f
AM
4743 m->p_flags_valid = 1;
4744 m->includes_phdrs = 1;
64029e93 4745 phdr_in_segment = TRUE;
8ded5a0f
AM
4746 *pm = m;
4747 pm = &m->next;
252b5132 4748
8ded5a0f 4749 amt = sizeof (struct elf_segment_map);
a50b1753 4750 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4751 if (m == NULL)
4752 goto error_return;
4753 m->next = NULL;
4754 m->p_type = PT_INTERP;
4755 m->count = 1;
4756 m->sections[0] = s;
4757
4758 *pm = m;
4759 pm = &m->next;
252b5132 4760 }
8ded5a0f
AM
4761
4762 /* Look through the sections. We put sections in the same program
4763 segment when the start of the second section can be placed within
4764 a few bytes of the end of the first section. */
4765 last_hdr = NULL;
4766 last_size = 0;
00bee008 4767 hdr_index = 0;
8ded5a0f 4768 writable = FALSE;
2888249f 4769 executable = FALSE;
8ded5a0f
AM
4770 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4771 if (dynsec != NULL
4772 && (dynsec->flags & SEC_LOAD) == 0)
4773 dynsec = NULL;
4774
64029e93
AM
4775 if ((abfd->flags & D_PAGED) == 0)
4776 phdr_in_segment = FALSE;
4777
8ded5a0f
AM
4778 /* Deal with -Ttext or something similar such that the first section
4779 is not adjacent to the program headers. This is an
4780 approximation, since at this point we don't know exactly how many
4781 program headers we will need. */
64029e93 4782 if (phdr_in_segment && count > 0)
252b5132 4783 {
64029e93
AM
4784 bfd_vma phdr_lma;
4785 bfd_boolean separate_phdr = FALSE;
4786
4787 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
4788 if (info != NULL
4789 && info->separate_code
4790 && (sections[0]->flags & SEC_CODE) != 0)
1a9ccd70 4791 {
64029e93
AM
4792 /* If data sections should be separate from code and
4793 thus not executable, and the first section is
4794 executable then put the file and program headers in
4795 their own PT_LOAD. */
4796 separate_phdr = TRUE;
4797 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
4798 == (sections[0]->lma & addr_mask & -maxpagesize)))
4799 {
4800 /* The file and program headers are currently on the
4801 same page as the first section. Put them on the
4802 previous page if we can. */
4803 if (phdr_lma >= maxpagesize)
4804 phdr_lma -= maxpagesize;
4805 else
4806 separate_phdr = FALSE;
4807 }
4808 }
4809 if ((sections[0]->lma & addr_mask) < phdr_lma
4810 || (sections[0]->lma & addr_mask) < phdr_size)
4811 /* If file and program headers would be placed at the end
4812 of memory then it's probably better to omit them. */
4813 phdr_in_segment = FALSE;
4814 else if (phdr_lma < wrap_to)
4815 /* If a section wraps around to where we'll be placing
4816 file and program headers, then the headers will be
4817 overwritten. */
4818 phdr_in_segment = FALSE;
4819 else if (separate_phdr)
4820 {
4821 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
4822 if (m == NULL)
4823 goto error_return;
4824 m->p_paddr = phdr_lma;
4825 m->p_vaddr_offset
4826 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
4827 m->p_paddr_valid = 1;
4828 *pm = m;
4829 pm = &m->next;
4830 phdr_in_segment = FALSE;
1a9ccd70 4831 }
252b5132
RH
4832 }
4833
8ded5a0f 4834 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
252b5132 4835 {
8ded5a0f
AM
4836 asection *hdr;
4837 bfd_boolean new_segment;
4838
4839 hdr = *hdrpp;
4840
4841 /* See if this section and the last one will fit in the same
4842 segment. */
4843
4844 if (last_hdr == NULL)
4845 {
4846 /* If we don't have a segment yet, then we don't need a new
4847 one (we build the last one after this loop). */
4848 new_segment = FALSE;
4849 }
4850 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4851 {
4852 /* If this section has a different relation between the
4853 virtual address and the load address, then we need a new
4854 segment. */
4855 new_segment = TRUE;
4856 }
b5599592
AM
4857 else if (hdr->lma < last_hdr->lma + last_size
4858 || last_hdr->lma + last_size < last_hdr->lma)
4859 {
4860 /* If this section has a load address that makes it overlap
4861 the previous section, then we need a new segment. */
4862 new_segment = TRUE;
4863 }
76cb3a89
AM
4864 else if ((abfd->flags & D_PAGED) != 0
4865 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4866 == (hdr->lma & -maxpagesize)))
4867 {
4868 /* If we are demand paged then we can't map two disk
4869 pages onto the same memory page. */
4870 new_segment = FALSE;
4871 }
39948a60
NC
4872 /* In the next test we have to be careful when last_hdr->lma is close
4873 to the end of the address space. If the aligned address wraps
4874 around to the start of the address space, then there are no more
4875 pages left in memory and it is OK to assume that the current
4876 section can be included in the current segment. */
76cb3a89
AM
4877 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4878 + maxpagesize > last_hdr->lma)
4879 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4880 + maxpagesize <= hdr->lma))
8ded5a0f
AM
4881 {
4882 /* If putting this section in this segment would force us to
4883 skip a page in the segment, then we need a new segment. */
4884 new_segment = TRUE;
4885 }
4886 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
76cb3a89 4887 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
8ded5a0f 4888 {
e5654c0f
AM
4889 /* We don't want to put a loaded section after a
4890 nonloaded (ie. bss style) section in the same segment
4891 as that will force the non-loaded section to be loaded.
76cb3a89 4892 Consider .tbss sections as loaded for this purpose. */
8ded5a0f
AM
4893 new_segment = TRUE;
4894 }
4895 else if ((abfd->flags & D_PAGED) == 0)
4896 {
4897 /* If the file is not demand paged, which means that we
4898 don't require the sections to be correctly aligned in the
4899 file, then there is no other reason for a new segment. */
4900 new_segment = FALSE;
4901 }
2888249f
L
4902 else if (info != NULL
4903 && info->separate_code
4904 && executable != ((hdr->flags & SEC_CODE) != 0))
4905 {
4906 new_segment = TRUE;
4907 }
8ded5a0f 4908 else if (! writable
76cb3a89 4909 && (hdr->flags & SEC_READONLY) == 0)
8ded5a0f
AM
4910 {
4911 /* We don't want to put a writable section in a read only
76cb3a89 4912 segment. */
8ded5a0f
AM
4913 new_segment = TRUE;
4914 }
4915 else
4916 {
4917 /* Otherwise, we can use the same segment. */
4918 new_segment = FALSE;
4919 }
4920
2889e75b 4921 /* Allow interested parties a chance to override our decision. */
ceae84aa
AM
4922 if (last_hdr != NULL
4923 && info != NULL
4924 && info->callbacks->override_segment_assignment != NULL)
4925 new_segment
4926 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4927 last_hdr,
4928 new_segment);
2889e75b 4929
8ded5a0f
AM
4930 if (! new_segment)
4931 {
4932 if ((hdr->flags & SEC_READONLY) == 0)
4933 writable = TRUE;
2888249f
L
4934 if ((hdr->flags & SEC_CODE) != 0)
4935 executable = TRUE;
8ded5a0f
AM
4936 last_hdr = hdr;
4937 /* .tbss sections effectively have zero size. */
dbc88fc1 4938 last_size = !IS_TBSS (hdr) ? hdr->size : 0;
8ded5a0f
AM
4939 continue;
4940 }
4941
4942 /* We need a new program segment. We must create a new program
00bee008 4943 header holding all the sections from hdr_index until hdr. */
8ded5a0f 4944
00bee008 4945 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
8ded5a0f
AM
4946 if (m == NULL)
4947 goto error_return;
4948
4949 *pm = m;
4950 pm = &m->next;
4951
252b5132 4952 if ((hdr->flags & SEC_READONLY) == 0)
b34976b6 4953 writable = TRUE;
8ded5a0f
AM
4954 else
4955 writable = FALSE;
4956
2888249f
L
4957 if ((hdr->flags & SEC_CODE) == 0)
4958 executable = FALSE;
4959 else
4960 executable = TRUE;
4961
baaff79e
JJ
4962 last_hdr = hdr;
4963 /* .tbss sections effectively have zero size. */
dbc88fc1 4964 last_size = !IS_TBSS (hdr) ? hdr->size : 0;
00bee008 4965 hdr_index = i;
8ded5a0f 4966 phdr_in_segment = FALSE;
252b5132
RH
4967 }
4968
86b2281f
AM
4969 /* Create a final PT_LOAD program segment, but not if it's just
4970 for .tbss. */
4971 if (last_hdr != NULL
00bee008 4972 && (i - hdr_index != 1
dbc88fc1 4973 || !IS_TBSS (last_hdr)))
8ded5a0f 4974 {
00bee008 4975 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
8ded5a0f
AM
4976 if (m == NULL)
4977 goto error_return;
252b5132 4978
8ded5a0f
AM
4979 *pm = m;
4980 pm = &m->next;
4981 }
252b5132 4982
8ded5a0f
AM
4983 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4984 if (dynsec != NULL)
4985 {
4986 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4987 if (m == NULL)
4988 goto error_return;
4989 *pm = m;
4990 pm = &m->next;
4991 }
252b5132 4992
23e463ed 4993 /* For each batch of consecutive loadable SHT_NOTE sections,
1c5265b5
JJ
4994 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4995 because if we link together nonloadable .note sections and
4996 loadable .note sections, we will generate two .note sections
23e463ed 4997 in the output file. */
8ded5a0f
AM
4998 for (s = abfd->sections; s != NULL; s = s->next)
4999 {
5000 if ((s->flags & SEC_LOAD) != 0
23e463ed 5001 && elf_section_type (s) == SHT_NOTE)
8ded5a0f 5002 {
1c5265b5 5003 asection *s2;
23e463ed 5004 unsigned int alignment_power = s->alignment_power;
91d6fa6a
NC
5005
5006 count = 1;
23e463ed
L
5007 for (s2 = s; s2->next != NULL; s2 = s2->next)
5008 {
5009 if (s2->next->alignment_power == alignment_power
5010 && (s2->next->flags & SEC_LOAD) != 0
5011 && elf_section_type (s2->next) == SHT_NOTE
5012 && align_power (s2->lma + s2->size,
5013 alignment_power)
5014 == s2->next->lma)
5015 count++;
5016 else
5017 break;
5018 }
00bee008
AM
5019 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5020 amt += count * sizeof (asection *);
a50b1753 5021 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5022 if (m == NULL)
5023 goto error_return;
5024 m->next = NULL;
5025 m->p_type = PT_NOTE;
1c5265b5
JJ
5026 m->count = count;
5027 while (count > 1)
5028 {
5029 m->sections[m->count - count--] = s;
5030 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5031 s = s->next;
5032 }
5033 m->sections[m->count - 1] = s;
5034 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
8ded5a0f
AM
5035 *pm = m;
5036 pm = &m->next;
5037 }
5038 if (s->flags & SEC_THREAD_LOCAL)
5039 {
5040 if (! tls_count)
5041 first_tls = s;
5042 tls_count++;
5043 }
a91e1603
L
5044 if (first_mbind == NULL
5045 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5046 first_mbind = s;
8ded5a0f 5047 }
252b5132 5048
8ded5a0f
AM
5049 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5050 if (tls_count > 0)
5051 {
00bee008
AM
5052 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5053 amt += tls_count * sizeof (asection *);
a50b1753 5054 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5055 if (m == NULL)
5056 goto error_return;
5057 m->next = NULL;
5058 m->p_type = PT_TLS;
5059 m->count = tls_count;
5060 /* Mandated PF_R. */
5061 m->p_flags = PF_R;
5062 m->p_flags_valid = 1;
d923cae0 5063 s = first_tls;
91d6fa6a 5064 for (i = 0; i < (unsigned int) tls_count; ++i)
8ded5a0f 5065 {
d923cae0
L
5066 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5067 {
5068 _bfd_error_handler
871b3ab2 5069 (_("%pB: TLS sections are not adjacent:"), abfd);
d923cae0
L
5070 s = first_tls;
5071 i = 0;
5072 while (i < (unsigned int) tls_count)
5073 {
5074 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5075 {
871b3ab2 5076 _bfd_error_handler (_(" TLS: %pA"), s);
d923cae0
L
5077 i++;
5078 }
5079 else
871b3ab2 5080 _bfd_error_handler (_(" non-TLS: %pA"), s);
d923cae0
L
5081 s = s->next;
5082 }
5083 bfd_set_error (bfd_error_bad_value);
5084 goto error_return;
5085 }
5086 m->sections[i] = s;
5087 s = s->next;
8ded5a0f 5088 }
252b5132 5089
8ded5a0f
AM
5090 *pm = m;
5091 pm = &m->next;
5092 }
252b5132 5093
df3a023b
AM
5094 if (first_mbind
5095 && (abfd->flags & D_PAGED) != 0
5096 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
a91e1603
L
5097 for (s = first_mbind; s != NULL; s = s->next)
5098 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
df3a023b 5099 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
a91e1603
L
5100 {
5101 /* Mandated PF_R. */
5102 unsigned long p_flags = PF_R;
5103 if ((s->flags & SEC_READONLY) == 0)
5104 p_flags |= PF_W;
5105 if ((s->flags & SEC_CODE) != 0)
5106 p_flags |= PF_X;
5107
5108 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5109 m = bfd_zalloc (abfd, amt);
5110 if (m == NULL)
5111 goto error_return;
5112 m->next = NULL;
5113 m->p_type = (PT_GNU_MBIND_LO
5114 + elf_section_data (s)->this_hdr.sh_info);
5115 m->count = 1;
5116 m->p_flags_valid = 1;
5117 m->sections[0] = s;
5118 m->p_flags = p_flags;
5119
5120 *pm = m;
5121 pm = &m->next;
5122 }
5123
0a59decb
L
5124 s = bfd_get_section_by_name (abfd,
5125 NOTE_GNU_PROPERTY_SECTION_NAME);
5126 if (s != NULL && s->size != 0)
5127 {
5128 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5129 m = bfd_zalloc (abfd, amt);
5130 if (m == NULL)
5131 goto error_return;
5132 m->next = NULL;
5133 m->p_type = PT_GNU_PROPERTY;
5134 m->count = 1;
5135 m->p_flags_valid = 1;
5136 m->sections[0] = s;
5137 m->p_flags = PF_R;
5138 *pm = m;
5139 pm = &m->next;
5140 }
5141
8ded5a0f
AM
5142 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5143 segment. */
12bd6957 5144 eh_frame_hdr = elf_eh_frame_hdr (abfd);
8ded5a0f
AM
5145 if (eh_frame_hdr != NULL
5146 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
252b5132 5147 {
dc810e39 5148 amt = sizeof (struct elf_segment_map);
a50b1753 5149 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
5150 if (m == NULL)
5151 goto error_return;
5152 m->next = NULL;
8ded5a0f 5153 m->p_type = PT_GNU_EH_FRAME;
252b5132 5154 m->count = 1;
8ded5a0f 5155 m->sections[0] = eh_frame_hdr->output_section;
252b5132
RH
5156
5157 *pm = m;
5158 pm = &m->next;
5159 }
13ae64f3 5160
12bd6957 5161 if (elf_stack_flags (abfd))
13ae64f3 5162 {
8ded5a0f 5163 amt = sizeof (struct elf_segment_map);
a50b1753 5164 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5165 if (m == NULL)
5166 goto error_return;
5167 m->next = NULL;
2b05f1b7 5168 m->p_type = PT_GNU_STACK;
12bd6957 5169 m->p_flags = elf_stack_flags (abfd);
04c3a755 5170 m->p_align = bed->stack_align;
8ded5a0f 5171 m->p_flags_valid = 1;
04c3a755
NS
5172 m->p_align_valid = m->p_align != 0;
5173 if (info->stacksize > 0)
5174 {
5175 m->p_size = info->stacksize;
5176 m->p_size_valid = 1;
5177 }
252b5132 5178
8ded5a0f
AM
5179 *pm = m;
5180 pm = &m->next;
5181 }
65765700 5182
ceae84aa 5183 if (info != NULL && info->relro)
8ded5a0f 5184 {
f210dcff
L
5185 for (m = mfirst; m != NULL; m = m->next)
5186 {
3832a4d8
AM
5187 if (m->p_type == PT_LOAD
5188 && m->count != 0
5189 && m->sections[0]->vma >= info->relro_start
5190 && m->sections[0]->vma < info->relro_end)
f210dcff 5191 {
3832a4d8
AM
5192 i = m->count;
5193 while (--i != (unsigned) -1)
5194 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
5195 == (SEC_LOAD | SEC_HAS_CONTENTS))
5196 break;
5197
43a8475c 5198 if (i != (unsigned) -1)
f210dcff
L
5199 break;
5200 }
be01b344 5201 }
f210dcff
L
5202
5203 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5204 if (m != NULL)
5205 {
5206 amt = sizeof (struct elf_segment_map);
a50b1753 5207 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
f210dcff
L
5208 if (m == NULL)
5209 goto error_return;
5210 m->next = NULL;
5211 m->p_type = PT_GNU_RELRO;
f210dcff
L
5212 *pm = m;
5213 pm = &m->next;
5214 }
8ded5a0f 5215 }
9ee5e499 5216
8ded5a0f 5217 free (sections);
12bd6957 5218 elf_seg_map (abfd) = mfirst;
9ee5e499
JJ
5219 }
5220
3dea8fca 5221 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
8ded5a0f 5222 return FALSE;
8c37241b 5223
12bd6957 5224 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
8ded5a0f 5225 ++count;
12bd6957 5226 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
252b5132 5227
b34976b6 5228 return TRUE;
252b5132
RH
5229
5230 error_return:
5231 if (sections != NULL)
5232 free (sections);
b34976b6 5233 return FALSE;
252b5132
RH
5234}
5235
5236/* Sort sections by address. */
5237
5238static int
217aa764 5239elf_sort_sections (const void *arg1, const void *arg2)
252b5132
RH
5240{
5241 const asection *sec1 = *(const asection **) arg1;
5242 const asection *sec2 = *(const asection **) arg2;
eecdbe52 5243 bfd_size_type size1, size2;
252b5132
RH
5244
5245 /* Sort by LMA first, since this is the address used to
5246 place the section into a segment. */
5247 if (sec1->lma < sec2->lma)
5248 return -1;
5249 else if (sec1->lma > sec2->lma)
5250 return 1;
5251
5252 /* Then sort by VMA. Normally the LMA and the VMA will be
5253 the same, and this will do nothing. */
5254 if (sec1->vma < sec2->vma)
5255 return -1;
5256 else if (sec1->vma > sec2->vma)
5257 return 1;
5258
5259 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5260
07c6e936 5261#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
252b5132
RH
5262
5263 if (TOEND (sec1))
5264 {
5265 if (TOEND (sec2))
00a7cdc5 5266 {
67ce483b 5267 /* If the indices are the same, do not return 0
00a7cdc5
NC
5268 here, but continue to try the next comparison. */
5269 if (sec1->target_index - sec2->target_index != 0)
5270 return sec1->target_index - sec2->target_index;
5271 }
252b5132
RH
5272 else
5273 return 1;
5274 }
00a7cdc5 5275 else if (TOEND (sec2))
252b5132
RH
5276 return -1;
5277
5278#undef TOEND
5279
00a7cdc5
NC
5280 /* Sort by size, to put zero sized sections
5281 before others at the same address. */
252b5132 5282
eea6121a
AM
5283 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5284 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
eecdbe52
JJ
5285
5286 if (size1 < size2)
252b5132 5287 return -1;
eecdbe52 5288 if (size1 > size2)
252b5132
RH
5289 return 1;
5290
5291 return sec1->target_index - sec2->target_index;
5292}
5293
340b6d91
AC
5294/* Ian Lance Taylor writes:
5295
5296 We shouldn't be using % with a negative signed number. That's just
5297 not good. We have to make sure either that the number is not
5298 negative, or that the number has an unsigned type. When the types
5299 are all the same size they wind up as unsigned. When file_ptr is a
5300 larger signed type, the arithmetic winds up as signed long long,
5301 which is wrong.
5302
5303 What we're trying to say here is something like ``increase OFF by
5304 the least amount that will cause it to be equal to the VMA modulo
5305 the page size.'' */
5306/* In other words, something like:
5307
5308 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5309 off_offset = off % bed->maxpagesize;
5310 if (vma_offset < off_offset)
5311 adjustment = vma_offset + bed->maxpagesize - off_offset;
5312 else
5313 adjustment = vma_offset - off_offset;
08a40648 5314
de194d85 5315 which can be collapsed into the expression below. */
340b6d91
AC
5316
5317static file_ptr
5318vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5319{
dc9155b2
NC
5320 /* PR binutils/16199: Handle an alignment of zero. */
5321 if (maxpagesize == 0)
5322 maxpagesize = 1;
340b6d91
AC
5323 return ((vma - off) % maxpagesize);
5324}
5325
6d33f217
L
5326static void
5327print_segment_map (const struct elf_segment_map *m)
5328{
5329 unsigned int j;
5330 const char *pt = get_segment_type (m->p_type);
5331 char buf[32];
5332
5333 if (pt == NULL)
5334 {
5335 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5336 sprintf (buf, "LOPROC+%7.7x",
5337 (unsigned int) (m->p_type - PT_LOPROC));
5338 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5339 sprintf (buf, "LOOS+%7.7x",
5340 (unsigned int) (m->p_type - PT_LOOS));
5341 else
5342 snprintf (buf, sizeof (buf), "%8.8x",
5343 (unsigned int) m->p_type);
5344 pt = buf;
5345 }
4a97a0e5 5346 fflush (stdout);
6d33f217
L
5347 fprintf (stderr, "%s:", pt);
5348 for (j = 0; j < m->count; j++)
5349 fprintf (stderr, " %s", m->sections [j]->name);
5350 putc ('\n',stderr);
4a97a0e5 5351 fflush (stderr);
6d33f217
L
5352}
5353
32812159
AM
5354static bfd_boolean
5355write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
5356{
5357 void *buf;
5358 bfd_boolean ret;
5359
5360 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5361 return FALSE;
5362 buf = bfd_zmalloc (len);
5363 if (buf == NULL)
5364 return FALSE;
5365 ret = bfd_bwrite (buf, len, abfd) == len;
5366 free (buf);
5367 return ret;
5368}
5369
252b5132
RH
5370/* Assign file positions to the sections based on the mapping from
5371 sections to segments. This function also sets up some fields in
f3520d2f 5372 the file header. */
252b5132 5373
b34976b6 5374static bfd_boolean
f3520d2f
AM
5375assign_file_positions_for_load_sections (bfd *abfd,
5376 struct bfd_link_info *link_info)
252b5132
RH
5377{
5378 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 5379 struct elf_segment_map *m;
252b5132 5380 Elf_Internal_Phdr *phdrs;
252b5132 5381 Elf_Internal_Phdr *p;
02bf8d82 5382 file_ptr off;
3f570048 5383 bfd_size_type maxpagesize;
a8c75b76 5384 unsigned int pt_load_count = 0;
f3520d2f 5385 unsigned int alloc;
0920dee7 5386 unsigned int i, j;
2b0bc088 5387 bfd_vma header_pad = 0;
252b5132 5388
e36284ab 5389 if (link_info == NULL
ceae84aa 5390 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
8ded5a0f 5391 return FALSE;
252b5132 5392
8ded5a0f 5393 alloc = 0;
12bd6957 5394 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
2b0bc088
NC
5395 {
5396 ++alloc;
5397 if (m->header_size)
5398 header_pad = m->header_size;
5399 }
252b5132 5400
82f2dbf7
NC
5401 if (alloc)
5402 {
5403 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5404 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5405 }
5406 else
5407 {
5408 /* PR binutils/12467. */
5409 elf_elfheader (abfd)->e_phoff = 0;
5410 elf_elfheader (abfd)->e_phentsize = 0;
5411 }
d324f6d6 5412
8ded5a0f 5413 elf_elfheader (abfd)->e_phnum = alloc;
252b5132 5414
12bd6957
AM
5415 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5416 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
8ded5a0f 5417 else
12bd6957 5418 BFD_ASSERT (elf_program_header_size (abfd)
59e0647f 5419 >= alloc * bed->s->sizeof_phdr);
252b5132
RH
5420
5421 if (alloc == 0)
f3520d2f 5422 {
12bd6957 5423 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
8ded5a0f 5424 return TRUE;
f3520d2f 5425 }
252b5132 5426
12bd6957 5427 /* We're writing the size in elf_program_header_size (abfd),
57268894
HPN
5428 see assign_file_positions_except_relocs, so make sure we have
5429 that amount allocated, with trailing space cleared.
12bd6957
AM
5430 The variable alloc contains the computed need, while
5431 elf_program_header_size (abfd) contains the size used for the
57268894
HPN
5432 layout.
5433 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5434 where the layout is forced to according to a larger size in the
5435 last iterations for the testcase ld-elf/header. */
12bd6957 5436 BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr
57268894 5437 == 0);
a50b1753
NC
5438 phdrs = (Elf_Internal_Phdr *)
5439 bfd_zalloc2 (abfd,
07d6d2b8
AM
5440 (elf_program_header_size (abfd) / bed->s->sizeof_phdr),
5441 sizeof (Elf_Internal_Phdr));
f3520d2f 5442 elf_tdata (abfd)->phdr = phdrs;
252b5132 5443 if (phdrs == NULL)
b34976b6 5444 return FALSE;
252b5132 5445
3f570048
AM
5446 maxpagesize = 1;
5447 if ((abfd->flags & D_PAGED) != 0)
5448 maxpagesize = bed->maxpagesize;
5449
252b5132
RH
5450 off = bed->s->sizeof_ehdr;
5451 off += alloc * bed->s->sizeof_phdr;
2b0bc088
NC
5452 if (header_pad < (bfd_vma) off)
5453 header_pad = 0;
5454 else
5455 header_pad -= off;
5456 off += header_pad;
252b5132 5457
12bd6957 5458 for (m = elf_seg_map (abfd), p = phdrs, j = 0;
252b5132 5459 m != NULL;
0920dee7 5460 m = m->next, p++, j++)
252b5132 5461 {
252b5132 5462 asection **secpp;
bf988460
AM
5463 bfd_vma off_adjust;
5464 bfd_boolean no_contents;
252b5132
RH
5465
5466 /* If elf_segment_map is not from map_sections_to_segments, the
08a40648 5467 sections may not be correctly ordered. NOTE: sorting should
52e9b619
MS
5468 not be done to the PT_NOTE section of a corefile, which may
5469 contain several pseudo-sections artificially created by bfd.
5470 Sorting these pseudo-sections breaks things badly. */
47d9a591
AM
5471 if (m->count > 1
5472 && !(elf_elfheader (abfd)->e_type == ET_CORE
52e9b619 5473 && m->p_type == PT_NOTE))
252b5132
RH
5474 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5475 elf_sort_sections);
5476
b301b248
AM
5477 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5478 number of sections with contents contributing to both p_filesz
5479 and p_memsz, followed by a number of sections with no contents
5480 that just contribute to p_memsz. In this loop, OFF tracks next
02bf8d82 5481 available file offset for PT_LOAD and PT_NOTE segments. */
252b5132 5482 p->p_type = m->p_type;
28a7f3e7 5483 p->p_flags = m->p_flags;
252b5132 5484
3f570048 5485 if (m->count == 0)
5d695627 5486 p->p_vaddr = m->p_vaddr_offset;
3f570048 5487 else
5d695627 5488 p->p_vaddr = m->sections[0]->vma + m->p_vaddr_offset;
3f570048
AM
5489
5490 if (m->p_paddr_valid)
5491 p->p_paddr = m->p_paddr;
5492 else if (m->count == 0)
5493 p->p_paddr = 0;
5494 else
5d695627 5495 p->p_paddr = m->sections[0]->lma + m->p_vaddr_offset;
3f570048
AM
5496
5497 if (p->p_type == PT_LOAD
5498 && (abfd->flags & D_PAGED) != 0)
5499 {
5500 /* p_align in demand paged PT_LOAD segments effectively stores
5501 the maximum page size. When copying an executable with
5502 objcopy, we set m->p_align from the input file. Use this
5503 value for maxpagesize rather than bed->maxpagesize, which
5504 may be different. Note that we use maxpagesize for PT_TLS
5505 segment alignment later in this function, so we are relying
5506 on at least one PT_LOAD segment appearing before a PT_TLS
5507 segment. */
5508 if (m->p_align_valid)
5509 maxpagesize = m->p_align;
5510
5511 p->p_align = maxpagesize;
a8c75b76 5512 pt_load_count += 1;
3f570048 5513 }
3271a814
NS
5514 else if (m->p_align_valid)
5515 p->p_align = m->p_align;
e970b90a
DJ
5516 else if (m->count == 0)
5517 p->p_align = 1 << bed->s->log_file_align;
3f570048
AM
5518 else
5519 p->p_align = 0;
5520
bf988460
AM
5521 no_contents = FALSE;
5522 off_adjust = 0;
252b5132 5523 if (p->p_type == PT_LOAD
b301b248 5524 && m->count > 0)
252b5132 5525 {
b301b248 5526 bfd_size_type align;
a49e53ed 5527 unsigned int align_power = 0;
b301b248 5528
3271a814
NS
5529 if (m->p_align_valid)
5530 align = p->p_align;
5531 else
252b5132 5532 {
3271a814
NS
5533 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5534 {
5535 unsigned int secalign;
08a40648 5536
3271a814
NS
5537 secalign = bfd_get_section_alignment (abfd, *secpp);
5538 if (secalign > align_power)
5539 align_power = secalign;
5540 }
5541 align = (bfd_size_type) 1 << align_power;
5542 if (align < maxpagesize)
5543 align = maxpagesize;
b301b248 5544 }
252b5132 5545
02bf8d82
AM
5546 for (i = 0; i < m->count; i++)
5547 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5548 /* If we aren't making room for this section, then
5549 it must be SHT_NOBITS regardless of what we've
5550 set via struct bfd_elf_special_section. */
5551 elf_section_type (m->sections[i]) = SHT_NOBITS;
5552
bf988460 5553 /* Find out whether this segment contains any loadable
aea274d3
AM
5554 sections. */
5555 no_contents = TRUE;
5556 for (i = 0; i < m->count; i++)
5557 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5558 {
5559 no_contents = FALSE;
5560 break;
5561 }
bf988460 5562
85cfcbfb 5563 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
a8c75b76
AM
5564
5565 /* Broken hardware and/or kernel require that files do not
5566 map the same page with different permissions on some hppa
5567 processors. */
5568 if (pt_load_count > 1
5569 && bed->no_page_alias
5570 && (off & (maxpagesize - 1)) != 0
5571 && (off & -maxpagesize) == ((off + off_adjust) & -maxpagesize))
5572 off_adjust += maxpagesize;
bf988460
AM
5573 off += off_adjust;
5574 if (no_contents)
5575 {
5576 /* We shouldn't need to align the segment on disk since
5577 the segment doesn't need file space, but the gABI
5578 arguably requires the alignment and glibc ld.so
5579 checks it. So to comply with the alignment
5580 requirement but not waste file space, we adjust
5581 p_offset for just this segment. (OFF_ADJUST is
5582 subtracted from OFF later.) This may put p_offset
5583 past the end of file, but that shouldn't matter. */
5584 }
5585 else
5586 off_adjust = 0;
252b5132 5587 }
b1a6d0b1
NC
5588 /* Make sure the .dynamic section is the first section in the
5589 PT_DYNAMIC segment. */
5590 else if (p->p_type == PT_DYNAMIC
5591 && m->count > 1
5592 && strcmp (m->sections[0]->name, ".dynamic") != 0)
5593 {
5594 _bfd_error_handler
871b3ab2 5595 (_("%pB: The first section in the PT_DYNAMIC segment"
63a5468a 5596 " is not the .dynamic section"),
b301b248 5597 abfd);
b1a6d0b1
NC
5598 bfd_set_error (bfd_error_bad_value);
5599 return FALSE;
5600 }
3f001e84
JK
5601 /* Set the note section type to SHT_NOTE. */
5602 else if (p->p_type == PT_NOTE)
5603 for (i = 0; i < m->count; i++)
5604 elf_section_type (m->sections[i]) = SHT_NOTE;
252b5132 5605
252b5132
RH
5606 p->p_offset = 0;
5607 p->p_filesz = 0;
5608 p->p_memsz = 0;
5609
5610 if (m->includes_filehdr)
5611 {
bf988460 5612 if (!m->p_flags_valid)
252b5132 5613 p->p_flags |= PF_R;
252b5132
RH
5614 p->p_filesz = bed->s->sizeof_ehdr;
5615 p->p_memsz = bed->s->sizeof_ehdr;
5616 if (m->count > 0)
5617 {
9ab82472
L
5618 if (p->p_vaddr < (bfd_vma) off
5619 || (!m->p_paddr_valid
5620 && p->p_paddr < (bfd_vma) off))
252b5132 5621 {
4eca0228 5622 _bfd_error_handler
9793eb77 5623 (_("%pB: not enough room for program headers,"
63a5468a 5624 " try linking with -N"),
b301b248 5625 abfd);
252b5132 5626 bfd_set_error (bfd_error_bad_value);
b34976b6 5627 return FALSE;
252b5132
RH
5628 }
5629
5630 p->p_vaddr -= off;
bf988460 5631 if (!m->p_paddr_valid)
252b5132
RH
5632 p->p_paddr -= off;
5633 }
252b5132
RH
5634 }
5635
5636 if (m->includes_phdrs)
5637 {
bf988460 5638 if (!m->p_flags_valid)
252b5132
RH
5639 p->p_flags |= PF_R;
5640
f3520d2f 5641 if (!m->includes_filehdr)
252b5132
RH
5642 {
5643 p->p_offset = bed->s->sizeof_ehdr;
5644
5645 if (m->count > 0)
5646 {
252b5132 5647 p->p_vaddr -= off - p->p_offset;
bf988460 5648 if (!m->p_paddr_valid)
252b5132
RH
5649 p->p_paddr -= off - p->p_offset;
5650 }
252b5132
RH
5651 }
5652
5653 p->p_filesz += alloc * bed->s->sizeof_phdr;
5654 p->p_memsz += alloc * bed->s->sizeof_phdr;
2b0bc088
NC
5655 if (m->count)
5656 {
5657 p->p_filesz += header_pad;
5658 p->p_memsz += header_pad;
5659 }
252b5132
RH
5660 }
5661
5662 if (p->p_type == PT_LOAD
5663 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5664 {
bf988460 5665 if (!m->includes_filehdr && !m->includes_phdrs)
02bf8d82 5666 p->p_offset = off;
252b5132
RH
5667 else
5668 {
5669 file_ptr adjust;
5670
5671 adjust = off - (p->p_offset + p->p_filesz);
bf988460
AM
5672 if (!no_contents)
5673 p->p_filesz += adjust;
252b5132
RH
5674 p->p_memsz += adjust;
5675 }
5676 }
5677
1ea63fd2
AM
5678 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5679 maps. Set filepos for sections in PT_LOAD segments, and in
5680 core files, for sections in PT_NOTE segments.
5681 assign_file_positions_for_non_load_sections will set filepos
5682 for other sections and update p_filesz for other segments. */
252b5132
RH
5683 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5684 {
5685 asection *sec;
252b5132 5686 bfd_size_type align;
627b32bc 5687 Elf_Internal_Shdr *this_hdr;
252b5132
RH
5688
5689 sec = *secpp;
02bf8d82 5690 this_hdr = &elf_section_data (sec)->this_hdr;
3f570048 5691 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
252b5132 5692
88967714
AM
5693 if ((p->p_type == PT_LOAD
5694 || p->p_type == PT_TLS)
5695 && (this_hdr->sh_type != SHT_NOBITS
5696 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5697 && ((this_hdr->sh_flags & SHF_TLS) == 0
5698 || p->p_type == PT_TLS))))
252b5132 5699 {
b5599592
AM
5700 bfd_vma p_start = p->p_paddr;
5701 bfd_vma p_end = p_start + p->p_memsz;
5702 bfd_vma s_start = sec->lma;
5703 bfd_vma adjust = s_start - p_end;
252b5132 5704
a2d1e028
L
5705 if (adjust != 0
5706 && (s_start < p_end
5707 || p_end < p_start))
252b5132 5708 {
4eca0228 5709 _bfd_error_handler
695344c0 5710 /* xgettext:c-format */
2dcf00ce
AM
5711 (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64),
5712 abfd, sec, (uint64_t) s_start, (uint64_t) p_end);
88967714 5713 adjust = 0;
b5599592 5714 sec->lma = p_end;
1cfb7d1e 5715 }
3ac9b6c9 5716 p->p_memsz += adjust;
1cfb7d1e 5717
88967714
AM
5718 if (this_hdr->sh_type != SHT_NOBITS)
5719 {
32812159
AM
5720 if (p->p_filesz + adjust < p->p_memsz)
5721 {
5722 /* We have a PROGBITS section following NOBITS ones.
07d6d2b8 5723 Allocate file space for the NOBITS section(s) and
32812159
AM
5724 zero it. */
5725 adjust = p->p_memsz - p->p_filesz;
5726 if (!write_zeros (abfd, off, adjust))
5727 return FALSE;
5728 }
88967714
AM
5729 off += adjust;
5730 p->p_filesz += adjust;
252b5132 5731 }
252b5132
RH
5732 }
5733
5734 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5735 {
b301b248
AM
5736 /* The section at i == 0 is the one that actually contains
5737 everything. */
4a938328
MS
5738 if (i == 0)
5739 {
627b32bc 5740 this_hdr->sh_offset = sec->filepos = off;
6a3cd2b4
AM
5741 off += this_hdr->sh_size;
5742 p->p_filesz = this_hdr->sh_size;
b301b248
AM
5743 p->p_memsz = 0;
5744 p->p_align = 1;
252b5132 5745 }
4a938328 5746 else
252b5132 5747 {
b301b248 5748 /* The rest are fake sections that shouldn't be written. */
252b5132 5749 sec->filepos = 0;
eea6121a 5750 sec->size = 0;
b301b248
AM
5751 sec->flags = 0;
5752 continue;
252b5132 5753 }
252b5132
RH
5754 }
5755 else
5756 {
1e951488 5757 if (p->p_type == PT_LOAD)
b301b248 5758 {
1e951488
AM
5759 this_hdr->sh_offset = sec->filepos = off;
5760 if (this_hdr->sh_type != SHT_NOBITS)
5761 off += this_hdr->sh_size;
5762 }
5763 else if (this_hdr->sh_type == SHT_NOBITS
5764 && (this_hdr->sh_flags & SHF_TLS) != 0
5765 && this_hdr->sh_offset == 0)
5766 {
5767 /* This is a .tbss section that didn't get a PT_LOAD.
5768 (See _bfd_elf_map_sections_to_segments "Create a
5769 final PT_LOAD".) Set sh_offset to the value it
5770 would have if we had created a zero p_filesz and
5771 p_memsz PT_LOAD header for the section. This
5772 also makes the PT_TLS header have the same
5773 p_offset value. */
5774 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5775 off, align);
5776 this_hdr->sh_offset = sec->filepos = off + adjust;
b301b248 5777 }
252b5132 5778
02bf8d82 5779 if (this_hdr->sh_type != SHT_NOBITS)
b301b248 5780 {
6a3cd2b4 5781 p->p_filesz += this_hdr->sh_size;
02bf8d82
AM
5782 /* A load section without SHF_ALLOC is something like
5783 a note section in a PT_NOTE segment. These take
5784 file space but are not loaded into memory. */
5785 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6a3cd2b4 5786 p->p_memsz += this_hdr->sh_size;
b301b248 5787 }
6a3cd2b4 5788 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
13ae64f3 5789 {
6a3cd2b4
AM
5790 if (p->p_type == PT_TLS)
5791 p->p_memsz += this_hdr->sh_size;
5792
5793 /* .tbss is special. It doesn't contribute to p_memsz of
5794 normal segments. */
5795 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5796 p->p_memsz += this_hdr->sh_size;
13ae64f3
JJ
5797 }
5798
b10a8ae0
L
5799 if (align > p->p_align
5800 && !m->p_align_valid
5801 && (p->p_type != PT_LOAD
5802 || (abfd->flags & D_PAGED) == 0))
252b5132
RH
5803 p->p_align = align;
5804 }
5805
bf988460 5806 if (!m->p_flags_valid)
252b5132
RH
5807 {
5808 p->p_flags |= PF_R;
02bf8d82 5809 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
252b5132 5810 p->p_flags |= PF_X;
02bf8d82 5811 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
252b5132
RH
5812 p->p_flags |= PF_W;
5813 }
5814 }
43a8475c 5815
bf988460 5816 off -= off_adjust;
0920dee7 5817
7c928300
AM
5818 /* Check that all sections are in a PT_LOAD segment.
5819 Don't check funky gdb generated core files. */
5820 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
9a83a553
AM
5821 {
5822 bfd_boolean check_vma = TRUE;
5823
5824 for (i = 1; i < m->count; i++)
5825 if (m->sections[i]->vma == m->sections[i - 1]->vma
5826 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
5827 ->this_hdr), p) != 0
5828 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
5829 ->this_hdr), p) != 0)
0920dee7 5830 {
9a83a553
AM
5831 /* Looks like we have overlays packed into the segment. */
5832 check_vma = FALSE;
5833 break;
0920dee7 5834 }
9a83a553
AM
5835
5836 for (i = 0; i < m->count; i++)
5837 {
5838 Elf_Internal_Shdr *this_hdr;
5839 asection *sec;
5840
5841 sec = m->sections[i];
5842 this_hdr = &(elf_section_data(sec)->this_hdr);
86b2281f
AM
5843 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
5844 && !ELF_TBSS_SPECIAL (this_hdr, p))
9a83a553 5845 {
4eca0228 5846 _bfd_error_handler
695344c0 5847 /* xgettext:c-format */
871b3ab2 5848 (_("%pB: section `%pA' can't be allocated in segment %d"),
9a83a553
AM
5849 abfd, sec, j);
5850 print_segment_map (m);
5851 }
5852 }
5853 }
252b5132
RH
5854 }
5855
12bd6957 5856 elf_next_file_pos (abfd) = off;
f3520d2f
AM
5857 return TRUE;
5858}
5859
1faa385f
NC
5860/* Determine if a bfd is a debuginfo file. Unfortunately there
5861 is no defined method for detecting such files, so we have to
5862 use heuristics instead. */
5863
5864bfd_boolean
5865is_debuginfo_file (bfd *abfd)
5866{
5867 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
5868 return FALSE;
5869
5870 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
5871 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
5872 Elf_Internal_Shdr **headerp;
5873
5874 for (headerp = start_headers; headerp < end_headers; headerp ++)
5875 {
5876 Elf_Internal_Shdr *header = * headerp;
5877
5878 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
5879 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
5880 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
5881 && header->sh_type != SHT_NOBITS
5882 && header->sh_type != SHT_NOTE)
5883 return FALSE;
5884 }
5885
5886 return TRUE;
5887}
5888
f3520d2f
AM
5889/* Assign file positions for the other sections. */
5890
5891static bfd_boolean
5892assign_file_positions_for_non_load_sections (bfd *abfd,
5893 struct bfd_link_info *link_info)
5894{
5895 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5896 Elf_Internal_Shdr **i_shdrpp;
e06efbf1 5897 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
f3520d2f
AM
5898 Elf_Internal_Phdr *phdrs;
5899 Elf_Internal_Phdr *p;
5900 struct elf_segment_map *m;
62655c7b 5901 struct elf_segment_map *hdrs_segment;
f3520d2f
AM
5902 bfd_vma filehdr_vaddr, filehdr_paddr;
5903 bfd_vma phdrs_vaddr, phdrs_paddr;
5904 file_ptr off;
f3520d2f
AM
5905 unsigned int count;
5906
5c182d5f 5907 i_shdrpp = elf_elfsections (abfd);
e06efbf1 5908 end_hdrpp = i_shdrpp + elf_numsections (abfd);
12bd6957 5909 off = elf_next_file_pos (abfd);
e06efbf1 5910 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
5c182d5f 5911 {
5c182d5f
AM
5912 Elf_Internal_Shdr *hdr;
5913
5914 hdr = *hdrpp;
5915 if (hdr->bfd_section != NULL
252e386e
AM
5916 && (hdr->bfd_section->filepos != 0
5917 || (hdr->sh_type == SHT_NOBITS
5918 && hdr->contents == NULL)))
627b32bc 5919 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5c182d5f
AM
5920 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
5921 {
1faa385f
NC
5922 if (hdr->sh_size != 0
5923 /* PR 24717 - debuginfo files are known to be not strictly
5924 compliant with the ELF standard. In particular they often
5925 have .note.gnu.property sections that are outside of any
5926 loadable segment. This is not a problem for such files,
5927 so do not warn about them. */
5928 && ! is_debuginfo_file (abfd))
4eca0228 5929 _bfd_error_handler
695344c0 5930 /* xgettext:c-format */
871b3ab2 5931 (_("%pB: warning: allocated section `%s' not in segment"),
e8d2ba53
AM
5932 abfd,
5933 (hdr->bfd_section == NULL
5934 ? "*unknown*"
5935 : hdr->bfd_section->name));
3ba71138
L
5936 /* We don't need to page align empty sections. */
5937 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
5c182d5f
AM
5938 off += vma_page_aligned_bias (hdr->sh_addr, off,
5939 bed->maxpagesize);
5940 else
5941 off += vma_page_aligned_bias (hdr->sh_addr, off,
5942 hdr->sh_addralign);
5943 off = _bfd_elf_assign_file_position_for_section (hdr, off,
5944 FALSE);
5945 }
5946 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5947 && hdr->bfd_section == NULL)
0ce398f1
L
5948 || (hdr->bfd_section != NULL
5949 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
5950 /* Compress DWARF debug sections. */
12bd6957 5951 || hdr == i_shdrpp[elf_onesymtab (abfd)]
6a40cf0c
NC
5952 || (elf_symtab_shndx_list (abfd) != NULL
5953 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
5954 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
5955 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
5c182d5f
AM
5956 hdr->sh_offset = -1;
5957 else
5958 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5c182d5f
AM
5959 }
5960
252b5132
RH
5961 /* Now that we have set the section file positions, we can set up
5962 the file positions for the non PT_LOAD segments. */
f3520d2f
AM
5963 count = 0;
5964 filehdr_vaddr = 0;
5965 filehdr_paddr = 0;
5966 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
5967 phdrs_paddr = 0;
62655c7b 5968 hdrs_segment = NULL;
f3520d2f 5969 phdrs = elf_tdata (abfd)->phdr;
12bd6957 5970 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
f3520d2f
AM
5971 {
5972 ++count;
5973 if (p->p_type != PT_LOAD)
5974 continue;
5975
5976 if (m->includes_filehdr)
5977 {
5978 filehdr_vaddr = p->p_vaddr;
5979 filehdr_paddr = p->p_paddr;
5980 }
5981 if (m->includes_phdrs)
5982 {
5983 phdrs_vaddr = p->p_vaddr;
5984 phdrs_paddr = p->p_paddr;
5985 if (m->includes_filehdr)
5986 {
62655c7b 5987 hdrs_segment = m;
f3520d2f
AM
5988 phdrs_vaddr += bed->s->sizeof_ehdr;
5989 phdrs_paddr += bed->s->sizeof_ehdr;
5990 }
5991 }
5992 }
5993
62655c7b
RM
5994 if (hdrs_segment != NULL && link_info != NULL)
5995 {
5996 /* There is a segment that contains both the file headers and the
5997 program headers, so provide a symbol __ehdr_start pointing there.
5998 A program can use this to examine itself robustly. */
5999
6000 struct elf_link_hash_entry *hash
6001 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
6002 FALSE, FALSE, TRUE);
6003 /* If the symbol was referenced and not defined, define it. */
6004 if (hash != NULL
6005 && (hash->root.type == bfd_link_hash_new
6006 || hash->root.type == bfd_link_hash_undefined
6007 || hash->root.type == bfd_link_hash_undefweak
6008 || hash->root.type == bfd_link_hash_common))
6009 {
6010 asection *s = NULL;
6011 if (hdrs_segment->count != 0)
6012 /* The segment contains sections, so use the first one. */
6013 s = hdrs_segment->sections[0];
6014 else
6015 /* Use the first (i.e. lowest-addressed) section in any segment. */
12bd6957 6016 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
62655c7b
RM
6017 if (m->count != 0)
6018 {
6019 s = m->sections[0];
6020 break;
6021 }
6022
6023 if (s != NULL)
6024 {
6025 hash->root.u.def.value = filehdr_vaddr - s->vma;
6026 hash->root.u.def.section = s;
6027 }
6028 else
6029 {
6030 hash->root.u.def.value = filehdr_vaddr;
6031 hash->root.u.def.section = bfd_abs_section_ptr;
6032 }
6033
6034 hash->root.type = bfd_link_hash_defined;
6035 hash->def_regular = 1;
6036 hash->non_elf = 0;
6037 }
6038 }
6039
12bd6957 6040 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
252b5132 6041 {
129af99f 6042 if (p->p_type == PT_GNU_RELRO)
252b5132 6043 {
f2731e0c 6044 bfd_vma start, end;
01f7e10c 6045 bfd_boolean ok;
1ea63fd2 6046
129af99f 6047 if (link_info != NULL)
8c37241b 6048 {
129af99f 6049 /* During linking the range of the RELRO segment is passed
f2731e0c
AM
6050 in link_info. Note that there may be padding between
6051 relro_start and the first RELRO section. */
6052 start = link_info->relro_start;
6053 end = link_info->relro_end;
6054 }
6055 else if (m->count != 0)
6056 {
6057 if (!m->p_size_valid)
6058 abort ();
6059 start = m->sections[0]->vma;
6060 end = start + m->p_size;
6061 }
6062 else
6063 {
6064 start = 0;
6065 end = 0;
6066 }
6067
01f7e10c 6068 ok = FALSE;
f2731e0c
AM
6069 if (start < end)
6070 {
6071 struct elf_segment_map *lm;
6072 const Elf_Internal_Phdr *lp;
6073 unsigned int i;
6074
6075 /* Find a LOAD segment containing a section in the RELRO
6076 segment. */
12bd6957 6077 for (lm = elf_seg_map (abfd), lp = phdrs;
3146fac4
AM
6078 lm != NULL;
6079 lm = lm->next, lp++)
8c37241b
JJ
6080 {
6081 if (lp->p_type == PT_LOAD
3146fac4 6082 && lm->count != 0
dbc88fc1
AM
6083 && (lm->sections[lm->count - 1]->vma
6084 + (!IS_TBSS (lm->sections[lm->count - 1])
6085 ? lm->sections[lm->count - 1]->size
6086 : 0)) > start
f2731e0c 6087 && lm->sections[0]->vma < end)
8c37241b
JJ
6088 break;
6089 }
f2731e0c 6090
01f7e10c 6091 if (lm != NULL)
129af99f 6092 {
01f7e10c
AM
6093 /* Find the section starting the RELRO segment. */
6094 for (i = 0; i < lm->count; i++)
6095 {
6096 asection *s = lm->sections[i];
6097 if (s->vma >= start
6098 && s->vma < end
6099 && s->size != 0)
6100 break;
6101 }
6102
6103 if (i < lm->count)
6104 {
6105 p->p_vaddr = lm->sections[i]->vma;
6106 p->p_paddr = lm->sections[i]->lma;
6107 p->p_offset = lm->sections[i]->filepos;
6108 p->p_memsz = end - p->p_vaddr;
6109 p->p_filesz = p->p_memsz;
6110
6111 /* The RELRO segment typically ends a few bytes
6112 into .got.plt but other layouts are possible.
6113 In cases where the end does not match any
6114 loaded section (for instance is in file
6115 padding), trim p_filesz back to correspond to
6116 the end of loaded section contents. */
6117 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6118 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6119
6120 /* Preserve the alignment and flags if they are
6121 valid. The gold linker generates RW/4 for
6122 the PT_GNU_RELRO section. It is better for
6123 objcopy/strip to honor these attributes
6124 otherwise gdb will choke when using separate
6125 debug files. */
6126 if (!m->p_align_valid)
6127 p->p_align = 1;
6128 if (!m->p_flags_valid)
6129 p->p_flags = PF_R;
6130 ok = TRUE;
6131 }
129af99f 6132 }
b84a33b5 6133 }
01f7e10c
AM
6134 if (link_info != NULL)
6135 BFD_ASSERT (ok);
6136 if (!ok)
6137 memset (p, 0, sizeof *p);
129af99f 6138 }
04c3a755
NS
6139 else if (p->p_type == PT_GNU_STACK)
6140 {
6141 if (m->p_size_valid)
6142 p->p_memsz = m->p_size;
6143 }
129af99f
AS
6144 else if (m->count != 0)
6145 {
e06efbf1 6146 unsigned int i;
1a9ccd70 6147
129af99f
AS
6148 if (p->p_type != PT_LOAD
6149 && (p->p_type != PT_NOTE
6150 || bfd_get_format (abfd) != bfd_core))
6151 {
1a9ccd70
NC
6152 /* A user specified segment layout may include a PHDR
6153 segment that overlaps with a LOAD segment... */
6154 if (p->p_type == PT_PHDR)
6155 {
6156 m->count = 0;
6157 continue;
6158 }
6159
c86934ce
NC
6160 if (m->includes_filehdr || m->includes_phdrs)
6161 {
b1fa9dd6 6162 /* PR 17512: file: 2195325e. */
4eca0228 6163 _bfd_error_handler
871b3ab2 6164 (_("%pB: error: non-load segment %d includes file header "
76cfced5
AM
6165 "and/or program header"),
6166 abfd, (int) (p - phdrs));
c86934ce
NC
6167 return FALSE;
6168 }
129af99f 6169
86b2281f 6170 p->p_filesz = 0;
129af99f 6171 p->p_offset = m->sections[0]->filepos;
86b2281f
AM
6172 for (i = m->count; i-- != 0;)
6173 {
6174 asection *sect = m->sections[i];
6175 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6176 if (hdr->sh_type != SHT_NOBITS)
6177 {
6178 p->p_filesz = (sect->filepos - m->sections[0]->filepos
6179 + hdr->sh_size);
6180 break;
6181 }
6182 }
129af99f
AS
6183 }
6184 }
6185 else if (m->includes_filehdr)
6186 {
6187 p->p_vaddr = filehdr_vaddr;
6188 if (! m->p_paddr_valid)
6189 p->p_paddr = filehdr_paddr;
6190 }
6191 else if (m->includes_phdrs)
6192 {
6193 p->p_vaddr = phdrs_vaddr;
6194 if (! m->p_paddr_valid)
6195 p->p_paddr = phdrs_paddr;
252b5132
RH
6196 }
6197 }
6198
12bd6957 6199 elf_next_file_pos (abfd) = off;
252b5132 6200
b34976b6 6201 return TRUE;
252b5132
RH
6202}
6203
6a40cf0c
NC
6204static elf_section_list *
6205find_section_in_list (unsigned int i, elf_section_list * list)
6206{
6207 for (;list != NULL; list = list->next)
6208 if (list->ndx == i)
6209 break;
6210 return list;
6211}
6212
252b5132
RH
6213/* Work out the file positions of all the sections. This is called by
6214 _bfd_elf_compute_section_file_positions. All the section sizes and
6215 VMAs must be known before this is called.
6216
e0638f70
AM
6217 Reloc sections come in two flavours: Those processed specially as
6218 "side-channel" data attached to a section to which they apply, and
6219 those that bfd doesn't process as relocations. The latter sort are
6220 stored in a normal bfd section by bfd_section_from_shdr. We don't
6221 consider the former sort here, unless they form part of the loadable
6222 image. Reloc sections not assigned here will be handled later by
6223 assign_file_positions_for_relocs.
252b5132
RH
6224
6225 We also don't set the positions of the .symtab and .strtab here. */
6226
b34976b6 6227static bfd_boolean
c84fca4d
AO
6228assign_file_positions_except_relocs (bfd *abfd,
6229 struct bfd_link_info *link_info)
252b5132 6230{
5c182d5f
AM
6231 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6232 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
9c5bfbb7 6233 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
6234
6235 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6236 && bfd_get_format (abfd) != bfd_core)
6237 {
5c182d5f
AM
6238 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6239 unsigned int num_sec = elf_numsections (abfd);
252b5132
RH
6240 Elf_Internal_Shdr **hdrpp;
6241 unsigned int i;
a485e98e 6242 file_ptr off;
252b5132
RH
6243
6244 /* Start after the ELF header. */
6245 off = i_ehdrp->e_ehsize;
6246
6247 /* We are not creating an executable, which means that we are
6248 not creating a program header, and that the actual order of
6249 the sections in the file is unimportant. */
9ad5cbcf 6250 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
252b5132
RH
6251 {
6252 Elf_Internal_Shdr *hdr;
6253
6254 hdr = *hdrpp;
e0638f70
AM
6255 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6256 && hdr->bfd_section == NULL)
0ce398f1
L
6257 || (hdr->bfd_section != NULL
6258 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
6259 /* Compress DWARF debug sections. */
12bd6957 6260 || i == elf_onesymtab (abfd)
6a40cf0c
NC
6261 || (elf_symtab_shndx_list (abfd) != NULL
6262 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
6263 || i == elf_strtab_sec (abfd)
6264 || i == elf_shstrtab_sec (abfd))
252b5132
RH
6265 {
6266 hdr->sh_offset = -1;
252b5132 6267 }
9ad5cbcf 6268 else
b34976b6 6269 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 6270 }
a485e98e
AM
6271
6272 elf_next_file_pos (abfd) = off;
252b5132
RH
6273 }
6274 else
6275 {
f3520d2f
AM
6276 unsigned int alloc;
6277
252b5132 6278 /* Assign file positions for the loaded sections based on the
08a40648 6279 assignment of sections to segments. */
f3520d2f
AM
6280 if (!assign_file_positions_for_load_sections (abfd, link_info))
6281 return FALSE;
6282
6283 /* And for non-load sections. */
6284 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6285 return FALSE;
6286
e36284ab
AM
6287 if (bed->elf_backend_modify_program_headers != NULL)
6288 {
6289 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
6290 return FALSE;
6291 }
6292
58e7ebac 6293 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
0e1862bb 6294 if (link_info != NULL && bfd_link_pie (link_info))
58e7ebac
L
6295 {
6296 unsigned int num_segments = elf_elfheader (abfd)->e_phnum;
6297 Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr;
6298 Elf_Internal_Phdr *end_segment = &segment[num_segments];
6299
6300 /* Find the lowest p_vaddr in PT_LOAD segments. */
6301 bfd_vma p_vaddr = (bfd_vma) -1;
6302 for (; segment < end_segment; segment++)
6303 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6304 p_vaddr = segment->p_vaddr;
6305
6306 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6307 segments is non-zero. */
6308 if (p_vaddr)
6309 i_ehdrp->e_type = ET_EXEC;
6310 }
6311
f3520d2f 6312 /* Write out the program headers. */
6838f2be 6313 alloc = elf_elfheader (abfd)->e_phnum;
1a9ccd70
NC
6314 if (alloc == 0)
6315 return TRUE;
6316
1a9ccd70
NC
6317 /* PR ld/20815 - Check that the program header segment, if present, will
6318 be loaded into memory. FIXME: The check below is not sufficient as
6319 really all PT_LOAD segments should be checked before issuing an error
6320 message. Plus the PHDR segment does not have to be the first segment
6321 in the program header table. But this version of the check should
cd584857
NC
6322 catch all real world use cases.
6323
6324 FIXME: We used to have code here to sort the PT_LOAD segments into
6325 ascending order, as per the ELF spec. But this breaks some programs,
6326 including the Linux kernel. But really either the spec should be
07d6d2b8 6327 changed or the programs updated. */
1a9ccd70 6328 if (alloc > 1
cd584857 6329 && tdata->phdr[0].p_type == PT_PHDR
d00dd7dc
AM
6330 && (bed->elf_backend_allow_non_load_phdr == NULL
6331 || !bed->elf_backend_allow_non_load_phdr (abfd, tdata->phdr,
6332 alloc))
cd584857
NC
6333 && tdata->phdr[1].p_type == PT_LOAD
6334 && (tdata->phdr[1].p_vaddr > tdata->phdr[0].p_vaddr
6838f2be
AM
6335 || (tdata->phdr[1].p_vaddr + tdata->phdr[1].p_memsz
6336 < tdata->phdr[0].p_vaddr + tdata->phdr[0].p_memsz)))
1a9ccd70
NC
6337 {
6338 /* The fix for this error is usually to edit the linker script being
6339 used and set up the program headers manually. Either that or
6340 leave room for the headers at the start of the SECTIONS. */
6838f2be
AM
6341 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6342 " by LOAD segment"),
1a9ccd70 6343 abfd);
1a9ccd70
NC
6344 return FALSE;
6345 }
6346
f3520d2f 6347 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
cd584857
NC
6348 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6349 return FALSE;
252b5132
RH
6350 }
6351
b34976b6 6352 return TRUE;
252b5132
RH
6353}
6354
b34976b6 6355static bfd_boolean
217aa764 6356prep_headers (bfd *abfd)
252b5132 6357{
3d540e93 6358 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
2b0f7ef9 6359 struct elf_strtab_hash *shstrtab;
9c5bfbb7 6360 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
6361
6362 i_ehdrp = elf_elfheader (abfd);
252b5132 6363
2b0f7ef9 6364 shstrtab = _bfd_elf_strtab_init ();
252b5132 6365 if (shstrtab == NULL)
b34976b6 6366 return FALSE;
252b5132
RH
6367
6368 elf_shstrtab (abfd) = shstrtab;
6369
6370 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6371 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6372 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6373 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6374
6375 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6376 i_ehdrp->e_ident[EI_DATA] =
6377 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6378 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6379
252b5132
RH
6380 if ((abfd->flags & DYNAMIC) != 0)
6381 i_ehdrp->e_type = ET_DYN;
6382 else if ((abfd->flags & EXEC_P) != 0)
6383 i_ehdrp->e_type = ET_EXEC;
6384 else if (bfd_get_format (abfd) == bfd_core)
6385 i_ehdrp->e_type = ET_CORE;
6386 else
6387 i_ehdrp->e_type = ET_REL;
6388
6389 switch (bfd_get_arch (abfd))
6390 {
6391 case bfd_arch_unknown:
6392 i_ehdrp->e_machine = EM_NONE;
6393 break;
aa4f99bb
AO
6394
6395 /* There used to be a long list of cases here, each one setting
6396 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6397 in the corresponding bfd definition. To avoid duplication,
6398 the switch was removed. Machines that need special handling
6399 can generally do it in elf_backend_final_write_processing(),
6400 unless they need the information earlier than the final write.
6401 Such need can generally be supplied by replacing the tests for
6402 e_machine with the conditions used to determine it. */
252b5132 6403 default:
9c5bfbb7
AM
6404 i_ehdrp->e_machine = bed->elf_machine_code;
6405 }
aa4f99bb 6406
252b5132
RH
6407 i_ehdrp->e_version = bed->s->ev_current;
6408 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6409
c044fabd 6410 /* No program header, for now. */
252b5132
RH
6411 i_ehdrp->e_phoff = 0;
6412 i_ehdrp->e_phentsize = 0;
6413 i_ehdrp->e_phnum = 0;
6414
c044fabd 6415 /* Each bfd section is section header entry. */
252b5132
RH
6416 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6417 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6418
c044fabd 6419 /* If we're building an executable, we'll need a program header table. */
252b5132 6420 if (abfd->flags & EXEC_P)
0e71e495
BE
6421 /* It all happens later. */
6422 ;
252b5132
RH
6423 else
6424 {
6425 i_ehdrp->e_phentsize = 0;
252b5132
RH
6426 i_ehdrp->e_phoff = 0;
6427 }
6428
6429 elf_tdata (abfd)->symtab_hdr.sh_name =
b34976b6 6430 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
252b5132 6431 elf_tdata (abfd)->strtab_hdr.sh_name =
b34976b6 6432 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
252b5132 6433 elf_tdata (abfd)->shstrtab_hdr.sh_name =
b34976b6 6434 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
252b5132 6435 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
17ca87fc 6436 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
252b5132 6437 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
b34976b6 6438 return FALSE;
252b5132 6439
b34976b6 6440 return TRUE;
252b5132
RH
6441}
6442
6443/* Assign file positions for all the reloc sections which are not part
a485e98e 6444 of the loadable file image, and the file position of section headers. */
252b5132 6445
0ce398f1
L
6446static bfd_boolean
6447_bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
252b5132
RH
6448{
6449 file_ptr off;
e06efbf1 6450 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
3e19fb8f 6451 Elf_Internal_Shdr *shdrp;
a485e98e
AM
6452 Elf_Internal_Ehdr *i_ehdrp;
6453 const struct elf_backend_data *bed;
252b5132 6454
12bd6957 6455 off = elf_next_file_pos (abfd);
252b5132 6456
e06efbf1
L
6457 shdrpp = elf_elfsections (abfd);
6458 end_shdrpp = shdrpp + elf_numsections (abfd);
6459 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
252b5132 6460 {
252b5132 6461 shdrp = *shdrpp;
0ce398f1
L
6462 if (shdrp->sh_offset == -1)
6463 {
3e19fb8f 6464 asection *sec = shdrp->bfd_section;
0ce398f1
L
6465 bfd_boolean is_rel = (shdrp->sh_type == SHT_REL
6466 || shdrp->sh_type == SHT_RELA);
6467 if (is_rel
3e19fb8f 6468 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
0ce398f1
L
6469 {
6470 if (!is_rel)
6471 {
3e19fb8f
L
6472 const char *name = sec->name;
6473 struct bfd_elf_section_data *d;
6474
0ce398f1 6475 /* Compress DWARF debug sections. */
3e19fb8f 6476 if (!bfd_compress_section (abfd, sec,
0ce398f1
L
6477 shdrp->contents))
6478 return FALSE;
3e19fb8f
L
6479
6480 if (sec->compress_status == COMPRESS_SECTION_DONE
6481 && (abfd->flags & BFD_COMPRESS_GABI) == 0)
6482 {
6483 /* If section is compressed with zlib-gnu, convert
6484 section name from .debug_* to .zdebug_*. */
6485 char *new_name
6486 = convert_debug_to_zdebug (abfd, name);
6487 if (new_name == NULL)
6488 return FALSE;
6489 name = new_name;
6490 }
dd905818 6491 /* Add section name to section name section. */
3e19fb8f
L
6492 if (shdrp->sh_name != (unsigned int) -1)
6493 abort ();
6494 shdrp->sh_name
6495 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
6496 name, FALSE);
6497 d = elf_section_data (sec);
6498
dd905818 6499 /* Add reloc section name to section name section. */
3e19fb8f
L
6500 if (d->rel.hdr
6501 && !_bfd_elf_set_reloc_sh_name (abfd,
6502 d->rel.hdr,
6503 name, FALSE))
6504 return FALSE;
6505 if (d->rela.hdr
6506 && !_bfd_elf_set_reloc_sh_name (abfd,
6507 d->rela.hdr,
91cb26da 6508 name, TRUE))
3e19fb8f
L
6509 return FALSE;
6510
0ce398f1 6511 /* Update section size and contents. */
3e19fb8f
L
6512 shdrp->sh_size = sec->size;
6513 shdrp->contents = sec->contents;
0ce398f1
L
6514 shdrp->bfd_section->contents = NULL;
6515 }
6516 off = _bfd_elf_assign_file_position_for_section (shdrp,
6517 off,
6518 TRUE);
6519 }
6520 }
252b5132
RH
6521 }
6522
3e19fb8f
L
6523 /* Place section name section after DWARF debug sections have been
6524 compressed. */
6525 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6526 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6527 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
6528 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
6529
6530 /* Place the section headers. */
a485e98e
AM
6531 i_ehdrp = elf_elfheader (abfd);
6532 bed = get_elf_backend_data (abfd);
6533 off = align_file_position (off, 1 << bed->s->log_file_align);
6534 i_ehdrp->e_shoff = off;
6535 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
12bd6957 6536 elf_next_file_pos (abfd) = off;
0ce398f1
L
6537
6538 return TRUE;
252b5132
RH
6539}
6540
b34976b6 6541bfd_boolean
217aa764 6542_bfd_elf_write_object_contents (bfd *abfd)
252b5132 6543{
9c5bfbb7 6544 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 6545 Elf_Internal_Shdr **i_shdrp;
b34976b6 6546 bfd_boolean failed;
9ad5cbcf 6547 unsigned int count, num_sec;
30e8ee25 6548 struct elf_obj_tdata *t;
252b5132
RH
6549
6550 if (! abfd->output_has_begun
217aa764 6551 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
b34976b6 6552 return FALSE;
db727370
JL
6553 /* Do not rewrite ELF data when the BFD has been opened for update.
6554 abfd->output_has_begun was set to TRUE on opening, so creation of new
6555 sections, and modification of existing section sizes was restricted.
6556 This means the ELF header, program headers and section headers can't have
6557 changed.
6558 If the contents of any sections has been modified, then those changes have
6559 already been written to the BFD. */
6560 else if (abfd->direction == both_direction)
6561 {
6562 BFD_ASSERT (abfd->output_has_begun);
6563 return TRUE;
6564 }
252b5132
RH
6565
6566 i_shdrp = elf_elfsections (abfd);
252b5132 6567
b34976b6 6568 failed = FALSE;
252b5132
RH
6569 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6570 if (failed)
b34976b6 6571 return FALSE;
252b5132 6572
0ce398f1
L
6573 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
6574 return FALSE;
252b5132 6575
c044fabd 6576 /* After writing the headers, we need to write the sections too... */
9ad5cbcf
AM
6577 num_sec = elf_numsections (abfd);
6578 for (count = 1; count < num_sec; count++)
252b5132 6579 {
3e19fb8f
L
6580 i_shdrp[count]->sh_name
6581 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6582 i_shdrp[count]->sh_name);
252b5132 6583 if (bed->elf_backend_section_processing)
75506100
MR
6584 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
6585 return FALSE;
252b5132
RH
6586 if (i_shdrp[count]->contents)
6587 {
dc810e39
AM
6588 bfd_size_type amt = i_shdrp[count]->sh_size;
6589
252b5132 6590 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
dc810e39 6591 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
b34976b6 6592 return FALSE;
252b5132
RH
6593 }
6594 }
6595
6596 /* Write out the section header names. */
30e8ee25 6597 t = elf_tdata (abfd);
26ae6d5e 6598 if (elf_shstrtab (abfd) != NULL
30e8ee25 6599 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
08a40648 6600 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
b34976b6 6601 return FALSE;
252b5132 6602
cc364be6
AM
6603 if (!(*bed->elf_backend_final_write_processing) (abfd))
6604 return FALSE;
252b5132 6605
ff59fc36
RM
6606 if (!bed->s->write_shdrs_and_ehdr (abfd))
6607 return FALSE;
6608
6609 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
c0355132
AM
6610 if (t->o->build_id.after_write_object_contents != NULL)
6611 return (*t->o->build_id.after_write_object_contents) (abfd);
ff59fc36
RM
6612
6613 return TRUE;
252b5132
RH
6614}
6615
b34976b6 6616bfd_boolean
217aa764 6617_bfd_elf_write_corefile_contents (bfd *abfd)
252b5132 6618{
c044fabd 6619 /* Hopefully this can be done just like an object file. */
252b5132
RH
6620 return _bfd_elf_write_object_contents (abfd);
6621}
c044fabd
KH
6622
6623/* Given a section, search the header to find them. */
6624
cb33740c 6625unsigned int
198beae2 6626_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
252b5132 6627{
9c5bfbb7 6628 const struct elf_backend_data *bed;
91d6fa6a 6629 unsigned int sec_index;
252b5132 6630
9ad5cbcf
AM
6631 if (elf_section_data (asect) != NULL
6632 && elf_section_data (asect)->this_idx != 0)
6633 return elf_section_data (asect)->this_idx;
6634
6635 if (bfd_is_abs_section (asect))
91d6fa6a 6636 sec_index = SHN_ABS;
af746e92 6637 else if (bfd_is_com_section (asect))
91d6fa6a 6638 sec_index = SHN_COMMON;
af746e92 6639 else if (bfd_is_und_section (asect))
91d6fa6a 6640 sec_index = SHN_UNDEF;
af746e92 6641 else
91d6fa6a 6642 sec_index = SHN_BAD;
252b5132 6643
af746e92 6644 bed = get_elf_backend_data (abfd);
252b5132
RH
6645 if (bed->elf_backend_section_from_bfd_section)
6646 {
91d6fa6a 6647 int retval = sec_index;
9ad5cbcf 6648
af746e92
AM
6649 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6650 return retval;
252b5132
RH
6651 }
6652
91d6fa6a 6653 if (sec_index == SHN_BAD)
af746e92 6654 bfd_set_error (bfd_error_nonrepresentable_section);
252b5132 6655
91d6fa6a 6656 return sec_index;
252b5132
RH
6657}
6658
6659/* Given a BFD symbol, return the index in the ELF symbol table, or -1
6660 on error. */
6661
6662int
217aa764 6663_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
252b5132
RH
6664{
6665 asymbol *asym_ptr = *asym_ptr_ptr;
6666 int idx;
6667 flagword flags = asym_ptr->flags;
6668
6669 /* When gas creates relocations against local labels, it creates its
6670 own symbol for the section, but does put the symbol into the
6671 symbol chain, so udata is 0. When the linker is generating
6672 relocatable output, this section symbol may be for one of the
6673 input sections rather than the output section. */
6674 if (asym_ptr->udata.i == 0
6675 && (flags & BSF_SECTION_SYM)
6676 && asym_ptr->section)
6677 {
5372391b 6678 asection *sec;
252b5132
RH
6679 int indx;
6680
5372391b
AM
6681 sec = asym_ptr->section;
6682 if (sec->owner != abfd && sec->output_section != NULL)
6683 sec = sec->output_section;
6684 if (sec->owner == abfd
6685 && (indx = sec->index) < elf_num_section_syms (abfd)
4e89ac30 6686 && elf_section_syms (abfd)[indx] != NULL)
252b5132
RH
6687 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
6688 }
6689
6690 idx = asym_ptr->udata.i;
6691
6692 if (idx == 0)
6693 {
6694 /* This case can occur when using --strip-symbol on a symbol
08a40648 6695 which is used in a relocation entry. */
4eca0228 6696 _bfd_error_handler
695344c0 6697 /* xgettext:c-format */
871b3ab2 6698 (_("%pB: symbol `%s' required but not present"),
d003868e 6699 abfd, bfd_asymbol_name (asym_ptr));
252b5132
RH
6700 bfd_set_error (bfd_error_no_symbols);
6701 return -1;
6702 }
6703
6704#if DEBUG & 4
6705 {
6706 fprintf (stderr,
cd9af601
AM
6707 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6708 (long) asym_ptr, asym_ptr->name, idx, flags);
252b5132
RH
6709 fflush (stderr);
6710 }
6711#endif
6712
6713 return idx;
6714}
6715
84d1d650 6716/* Rewrite program header information. */
252b5132 6717
b34976b6 6718static bfd_boolean
84d1d650 6719rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
252b5132 6720{
b34976b6
AM
6721 Elf_Internal_Ehdr *iehdr;
6722 struct elf_segment_map *map;
6723 struct elf_segment_map *map_first;
6724 struct elf_segment_map **pointer_to_map;
6725 Elf_Internal_Phdr *segment;
6726 asection *section;
6727 unsigned int i;
6728 unsigned int num_segments;
6729 bfd_boolean phdr_included = FALSE;
5c44b38e 6730 bfd_boolean p_paddr_valid;
b34976b6
AM
6731 bfd_vma maxpagesize;
6732 struct elf_segment_map *phdr_adjust_seg = NULL;
6733 unsigned int phdr_adjust_num = 0;
9c5bfbb7 6734 const struct elf_backend_data *bed;
bc67d8a6 6735
caf47ea6 6736 bed = get_elf_backend_data (ibfd);
252b5132
RH
6737 iehdr = elf_elfheader (ibfd);
6738
bc67d8a6 6739 map_first = NULL;
c044fabd 6740 pointer_to_map = &map_first;
252b5132
RH
6741
6742 num_segments = elf_elfheader (ibfd)->e_phnum;
bc67d8a6
NC
6743 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
6744
6745 /* Returns the end address of the segment + 1. */
aecc8f8a
AM
6746#define SEGMENT_END(segment, start) \
6747 (start + (segment->p_memsz > segment->p_filesz \
6748 ? segment->p_memsz : segment->p_filesz))
bc67d8a6 6749
eecdbe52
JJ
6750#define SECTION_SIZE(section, segment) \
6751 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6752 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
eea6121a 6753 ? section->size : 0)
eecdbe52 6754
b34976b6 6755 /* Returns TRUE if the given section is contained within
bc67d8a6 6756 the given segment. VMA addresses are compared. */
aecc8f8a
AM
6757#define IS_CONTAINED_BY_VMA(section, segment) \
6758 (section->vma >= segment->p_vaddr \
eecdbe52 6759 && (section->vma + SECTION_SIZE (section, segment) \
aecc8f8a 6760 <= (SEGMENT_END (segment, segment->p_vaddr))))
c044fabd 6761
b34976b6 6762 /* Returns TRUE if the given section is contained within
bc67d8a6 6763 the given segment. LMA addresses are compared. */
aecc8f8a
AM
6764#define IS_CONTAINED_BY_LMA(section, segment, base) \
6765 (section->lma >= base \
beab4532 6766 && (section->lma + SECTION_SIZE (section, segment) >= section->lma) \
eecdbe52 6767 && (section->lma + SECTION_SIZE (section, segment) \
aecc8f8a 6768 <= SEGMENT_END (segment, base)))
252b5132 6769
0efc80c8
L
6770 /* Handle PT_NOTE segment. */
6771#define IS_NOTE(p, s) \
aecc8f8a 6772 (p->p_type == PT_NOTE \
0efc80c8 6773 && elf_section_type (s) == SHT_NOTE \
aecc8f8a 6774 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 6775 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 6776 <= p->p_offset + p->p_filesz))
252b5132 6777
0efc80c8
L
6778 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6779 etc. */
6780#define IS_COREFILE_NOTE(p, s) \
6781 (IS_NOTE (p, s) \
6782 && bfd_get_format (ibfd) == bfd_core \
6783 && s->vma == 0 \
6784 && s->lma == 0)
6785
252b5132
RH
6786 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6787 linker, which generates a PT_INTERP section with p_vaddr and
6788 p_memsz set to 0. */
aecc8f8a
AM
6789#define IS_SOLARIS_PT_INTERP(p, s) \
6790 (p->p_vaddr == 0 \
6791 && p->p_paddr == 0 \
6792 && p->p_memsz == 0 \
6793 && p->p_filesz > 0 \
6794 && (s->flags & SEC_HAS_CONTENTS) != 0 \
eea6121a 6795 && s->size > 0 \
aecc8f8a 6796 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 6797 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 6798 <= p->p_offset + p->p_filesz))
5c440b1e 6799
bc67d8a6
NC
6800 /* Decide if the given section should be included in the given segment.
6801 A section will be included if:
f5ffc919 6802 1. It is within the address space of the segment -- we use the LMA
08a40648 6803 if that is set for the segment and the VMA otherwise,
0efc80c8 6804 2. It is an allocated section or a NOTE section in a PT_NOTE
d324f6d6 6805 segment.
bc67d8a6 6806 3. There is an output section associated with it,
eecdbe52 6807 4. The section has not already been allocated to a previous segment.
2b05f1b7 6808 5. PT_GNU_STACK segments do not include any sections.
03394ac9 6809 6. PT_TLS segment includes only SHF_TLS sections.
6f79b219
JJ
6810 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6811 8. PT_DYNAMIC should not contain empty sections at the beginning
08a40648 6812 (with the possible exception of .dynamic). */
9f17e2a6 6813#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
2b05f1b7
L
6814 ((((segment->p_paddr \
6815 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6816 : IS_CONTAINED_BY_VMA (section, segment)) \
6817 && (section->flags & SEC_ALLOC) != 0) \
0efc80c8 6818 || IS_NOTE (segment, section)) \
2b05f1b7
L
6819 && segment->p_type != PT_GNU_STACK \
6820 && (segment->p_type != PT_TLS \
6821 || (section->flags & SEC_THREAD_LOCAL)) \
6822 && (segment->p_type == PT_LOAD \
6823 || segment->p_type == PT_TLS \
6824 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6825 && (segment->p_type != PT_DYNAMIC \
6826 || SECTION_SIZE (section, segment) > 0 \
6827 || (segment->p_paddr \
6828 ? segment->p_paddr != section->lma \
6829 : segment->p_vaddr != section->vma) \
6830 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
6831 == 0)) \
9933dc52 6832 && (segment->p_type != PT_LOAD || !section->segment_mark))
bc67d8a6 6833
9f17e2a6
L
6834/* If the output section of a section in the input segment is NULL,
6835 it is removed from the corresponding output segment. */
6836#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6837 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6838 && section->output_section != NULL)
6839
b34976b6 6840 /* Returns TRUE iff seg1 starts after the end of seg2. */
b5f852ea
NC
6841#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6842 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6843
6844 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6845 their VMA address ranges and their LMA address ranges overlap.
6846 It is possible to have overlapping VMA ranges without overlapping LMA
6847 ranges. RedBoot images for example can have both .data and .bss mapped
6848 to the same VMA range, but with the .data section mapped to a different
6849 LMA. */
aecc8f8a 6850#define SEGMENT_OVERLAPS(seg1, seg2) \
b5f852ea 6851 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
08a40648 6852 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
b5f852ea 6853 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
08a40648 6854 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
bc67d8a6
NC
6855
6856 /* Initialise the segment mark field. */
6857 for (section = ibfd->sections; section != NULL; section = section->next)
b34976b6 6858 section->segment_mark = FALSE;
bc67d8a6 6859
5c44b38e
AM
6860 /* The Solaris linker creates program headers in which all the
6861 p_paddr fields are zero. When we try to objcopy or strip such a
6862 file, we get confused. Check for this case, and if we find it
6863 don't set the p_paddr_valid fields. */
6864 p_paddr_valid = FALSE;
6865 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6866 i < num_segments;
6867 i++, segment++)
6868 if (segment->p_paddr != 0)
6869 {
6870 p_paddr_valid = TRUE;
6871 break;
6872 }
6873
252b5132 6874 /* Scan through the segments specified in the program header
bc67d8a6 6875 of the input BFD. For this first scan we look for overlaps
9ad5cbcf 6876 in the loadable segments. These can be created by weird
aecc8f8a 6877 parameters to objcopy. Also, fix some solaris weirdness. */
bc67d8a6
NC
6878 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6879 i < num_segments;
c044fabd 6880 i++, segment++)
252b5132 6881 {
252b5132 6882 unsigned int j;
c044fabd 6883 Elf_Internal_Phdr *segment2;
252b5132 6884
aecc8f8a
AM
6885 if (segment->p_type == PT_INTERP)
6886 for (section = ibfd->sections; section; section = section->next)
6887 if (IS_SOLARIS_PT_INTERP (segment, section))
6888 {
6889 /* Mininal change so that the normal section to segment
4cc11e76 6890 assignment code will work. */
aecc8f8a
AM
6891 segment->p_vaddr = section->vma;
6892 break;
6893 }
6894
bc67d8a6 6895 if (segment->p_type != PT_LOAD)
b10a8ae0
L
6896 {
6897 /* Remove PT_GNU_RELRO segment. */
6898 if (segment->p_type == PT_GNU_RELRO)
6899 segment->p_type = PT_NULL;
6900 continue;
6901 }
c044fabd 6902
bc67d8a6 6903 /* Determine if this segment overlaps any previous segments. */
0067a569 6904 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
bc67d8a6
NC
6905 {
6906 bfd_signed_vma extra_length;
c044fabd 6907
bc67d8a6 6908 if (segment2->p_type != PT_LOAD
0067a569 6909 || !SEGMENT_OVERLAPS (segment, segment2))
bc67d8a6 6910 continue;
c044fabd 6911
bc67d8a6
NC
6912 /* Merge the two segments together. */
6913 if (segment2->p_vaddr < segment->p_vaddr)
6914 {
c044fabd 6915 /* Extend SEGMENT2 to include SEGMENT and then delete
08a40648 6916 SEGMENT. */
0067a569
AM
6917 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
6918 - SEGMENT_END (segment2, segment2->p_vaddr));
c044fabd 6919
bc67d8a6
NC
6920 if (extra_length > 0)
6921 {
0067a569 6922 segment2->p_memsz += extra_length;
bc67d8a6
NC
6923 segment2->p_filesz += extra_length;
6924 }
c044fabd 6925
bc67d8a6 6926 segment->p_type = PT_NULL;
c044fabd 6927
bc67d8a6
NC
6928 /* Since we have deleted P we must restart the outer loop. */
6929 i = 0;
6930 segment = elf_tdata (ibfd)->phdr;
6931 break;
6932 }
6933 else
6934 {
c044fabd 6935 /* Extend SEGMENT to include SEGMENT2 and then delete
08a40648 6936 SEGMENT2. */
0067a569
AM
6937 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
6938 - SEGMENT_END (segment, segment->p_vaddr));
c044fabd 6939
bc67d8a6
NC
6940 if (extra_length > 0)
6941 {
0067a569 6942 segment->p_memsz += extra_length;
bc67d8a6
NC
6943 segment->p_filesz += extra_length;
6944 }
c044fabd 6945
bc67d8a6
NC
6946 segment2->p_type = PT_NULL;
6947 }
6948 }
6949 }
c044fabd 6950
bc67d8a6
NC
6951 /* The second scan attempts to assign sections to segments. */
6952 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6953 i < num_segments;
0067a569 6954 i++, segment++)
bc67d8a6 6955 {
0067a569
AM
6956 unsigned int section_count;
6957 asection **sections;
6958 asection *output_section;
6959 unsigned int isec;
9933dc52
AM
6960 asection *matching_lma;
6961 asection *suggested_lma;
0067a569 6962 unsigned int j;
dc810e39 6963 bfd_size_type amt;
0067a569 6964 asection *first_section;
bc67d8a6
NC
6965
6966 if (segment->p_type == PT_NULL)
6967 continue;
c044fabd 6968
9f17e2a6 6969 first_section = NULL;
bc67d8a6 6970 /* Compute how many sections might be placed into this segment. */
b5f852ea
NC
6971 for (section = ibfd->sections, section_count = 0;
6972 section != NULL;
6973 section = section->next)
9f17e2a6
L
6974 {
6975 /* Find the first section in the input segment, which may be
6976 removed from the corresponding output segment. */
6977 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
6978 {
6979 if (first_section == NULL)
6980 first_section = section;
6981 if (section->output_section != NULL)
6982 ++section_count;
6983 }
6984 }
811072d8 6985
b5f852ea
NC
6986 /* Allocate a segment map big enough to contain
6987 all of the sections we have selected. */
00bee008
AM
6988 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
6989 amt += (bfd_size_type) section_count * sizeof (asection *);
a50b1753 6990 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 6991 if (map == NULL)
b34976b6 6992 return FALSE;
252b5132
RH
6993
6994 /* Initialise the fields of the segment map. Default to
6995 using the physical address of the segment in the input BFD. */
0067a569
AM
6996 map->next = NULL;
6997 map->p_type = segment->p_type;
6998 map->p_flags = segment->p_flags;
bc67d8a6 6999 map->p_flags_valid = 1;
55d55ac7 7000
9f17e2a6
L
7001 /* If the first section in the input segment is removed, there is
7002 no need to preserve segment physical address in the corresponding
7003 output segment. */
945c025a 7004 if (!first_section || first_section->output_section != NULL)
9f17e2a6
L
7005 {
7006 map->p_paddr = segment->p_paddr;
5c44b38e 7007 map->p_paddr_valid = p_paddr_valid;
9f17e2a6 7008 }
252b5132
RH
7009
7010 /* Determine if this segment contains the ELF file header
7011 and if it contains the program headers themselves. */
bc67d8a6
NC
7012 map->includes_filehdr = (segment->p_offset == 0
7013 && segment->p_filesz >= iehdr->e_ehsize);
bc67d8a6 7014 map->includes_phdrs = 0;
252b5132 7015
0067a569 7016 if (!phdr_included || segment->p_type != PT_LOAD)
252b5132 7017 {
bc67d8a6
NC
7018 map->includes_phdrs =
7019 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7020 && (segment->p_offset + segment->p_filesz
252b5132
RH
7021 >= ((bfd_vma) iehdr->e_phoff
7022 + iehdr->e_phnum * iehdr->e_phentsize)));
c044fabd 7023
bc67d8a6 7024 if (segment->p_type == PT_LOAD && map->includes_phdrs)
b34976b6 7025 phdr_included = TRUE;
252b5132
RH
7026 }
7027
bc67d8a6 7028 if (section_count == 0)
252b5132
RH
7029 {
7030 /* Special segments, such as the PT_PHDR segment, may contain
7031 no sections, but ordinary, loadable segments should contain
1ed89aa9 7032 something. They are allowed by the ELF spec however, so only
07d6d2b8 7033 a warning is produced.
f98450c6
NC
7034 There is however the valid use case of embedded systems which
7035 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7036 flash memory with zeros. No warning is shown for that case. */
7037 if (segment->p_type == PT_LOAD
7038 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7039 /* xgettext:c-format */
9793eb77
AM
7040 _bfd_error_handler
7041 (_("%pB: warning: empty loadable segment detected"
7042 " at vaddr=%#" PRIx64 ", is this intentional?"),
7043 ibfd, (uint64_t) segment->p_vaddr);
252b5132 7044
5d695627 7045 map->p_vaddr_offset = segment->p_vaddr;
bc67d8a6 7046 map->count = 0;
c044fabd
KH
7047 *pointer_to_map = map;
7048 pointer_to_map = &map->next;
252b5132
RH
7049
7050 continue;
7051 }
7052
7053 /* Now scan the sections in the input BFD again and attempt
7054 to add their corresponding output sections to the segment map.
7055 The problem here is how to handle an output section which has
7056 been moved (ie had its LMA changed). There are four possibilities:
7057
7058 1. None of the sections have been moved.
7059 In this case we can continue to use the segment LMA from the
7060 input BFD.
7061
7062 2. All of the sections have been moved by the same amount.
7063 In this case we can change the segment's LMA to match the LMA
7064 of the first section.
7065
7066 3. Some of the sections have been moved, others have not.
7067 In this case those sections which have not been moved can be
7068 placed in the current segment which will have to have its size,
7069 and possibly its LMA changed, and a new segment or segments will
7070 have to be created to contain the other sections.
7071
b5f852ea 7072 4. The sections have been moved, but not by the same amount.
252b5132
RH
7073 In this case we can change the segment's LMA to match the LMA
7074 of the first section and we will have to create a new segment
7075 or segments to contain the other sections.
7076
7077 In order to save time, we allocate an array to hold the section
7078 pointers that we are interested in. As these sections get assigned
7079 to a segment, they are removed from this array. */
7080
a50b1753 7081 sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
252b5132 7082 if (sections == NULL)
b34976b6 7083 return FALSE;
252b5132
RH
7084
7085 /* Step One: Scan for segment vs section LMA conflicts.
7086 Also add the sections to the section array allocated above.
7087 Also add the sections to the current segment. In the common
7088 case, where the sections have not been moved, this means that
7089 we have completely filled the segment, and there is nothing
7090 more to do. */
252b5132 7091 isec = 0;
9933dc52
AM
7092 matching_lma = NULL;
7093 suggested_lma = NULL;
252b5132 7094
461c4b2e 7095 for (section = first_section, j = 0;
bc67d8a6
NC
7096 section != NULL;
7097 section = section->next)
252b5132 7098 {
caf47ea6 7099 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
c0f7859b 7100 {
bc67d8a6
NC
7101 output_section = section->output_section;
7102
0067a569 7103 sections[j++] = section;
252b5132
RH
7104
7105 /* The Solaris native linker always sets p_paddr to 0.
7106 We try to catch that case here, and set it to the
5e8d7549
NC
7107 correct value. Note - some backends require that
7108 p_paddr be left as zero. */
5c44b38e 7109 if (!p_paddr_valid
4455705d 7110 && segment->p_vaddr != 0
0067a569 7111 && !bed->want_p_paddr_set_to_zero
252b5132 7112 && isec == 0
bc67d8a6 7113 && output_section->lma != 0
9933dc52
AM
7114 && (align_power (segment->p_vaddr
7115 + (map->includes_filehdr
7116 ? iehdr->e_ehsize : 0)
7117 + (map->includes_phdrs
7118 ? iehdr->e_phnum * iehdr->e_phentsize
7119 : 0),
7120 output_section->alignment_power)
7121 == output_section->vma))
bc67d8a6 7122 map->p_paddr = segment->p_vaddr;
252b5132
RH
7123
7124 /* Match up the physical address of the segment with the
7125 LMA address of the output section. */
bc67d8a6 7126 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5e8d7549 7127 || IS_COREFILE_NOTE (segment, section)
0067a569
AM
7128 || (bed->want_p_paddr_set_to_zero
7129 && IS_CONTAINED_BY_VMA (output_section, segment)))
252b5132 7130 {
9933dc52
AM
7131 if (matching_lma == NULL
7132 || output_section->lma < matching_lma->lma)
7133 matching_lma = output_section;
252b5132
RH
7134
7135 /* We assume that if the section fits within the segment
bc67d8a6 7136 then it does not overlap any other section within that
252b5132 7137 segment. */
0067a569
AM
7138 map->sections[isec++] = output_section;
7139 }
9933dc52
AM
7140 else if (suggested_lma == NULL)
7141 suggested_lma = output_section;
147d51c2
L
7142
7143 if (j == section_count)
7144 break;
252b5132
RH
7145 }
7146 }
7147
bc67d8a6 7148 BFD_ASSERT (j == section_count);
252b5132
RH
7149
7150 /* Step Two: Adjust the physical address of the current segment,
7151 if necessary. */
bc67d8a6 7152 if (isec == section_count)
252b5132
RH
7153 {
7154 /* All of the sections fitted within the segment as currently
7155 specified. This is the default case. Add the segment to
7156 the list of built segments and carry on to process the next
7157 program header in the input BFD. */
bc67d8a6 7158 map->count = section_count;
c044fabd
KH
7159 *pointer_to_map = map;
7160 pointer_to_map = &map->next;
08a40648 7161
5c44b38e
AM
7162 if (p_paddr_valid
7163 && !bed->want_p_paddr_set_to_zero
9933dc52 7164 && matching_lma->lma != map->p_paddr
5c44b38e
AM
7165 && !map->includes_filehdr
7166 && !map->includes_phdrs)
3271a814
NS
7167 /* There is some padding before the first section in the
7168 segment. So, we must account for that in the output
7169 segment's vma. */
5d695627 7170 map->p_vaddr_offset = map->p_paddr - matching_lma->lma;
08a40648 7171
252b5132
RH
7172 free (sections);
7173 continue;
7174 }
252b5132
RH
7175 else
7176 {
9933dc52
AM
7177 /* Change the current segment's physical address to match
7178 the LMA of the first section that fitted, or if no
7179 section fitted, the first section. */
7180 if (matching_lma == NULL)
7181 matching_lma = suggested_lma;
7182
7183 map->p_paddr = matching_lma->lma;
72730e0c 7184
bc67d8a6
NC
7185 /* Offset the segment physical address from the lma
7186 to allow for space taken up by elf headers. */
9933dc52 7187 if (map->includes_phdrs)
010c8431 7188 {
9933dc52
AM
7189 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7190
7191 /* iehdr->e_phnum is just an estimate of the number
7192 of program headers that we will need. Make a note
7193 here of the number we used and the segment we chose
7194 to hold these headers, so that we can adjust the
7195 offset when we know the correct value. */
7196 phdr_adjust_num = iehdr->e_phnum;
7197 phdr_adjust_seg = map;
010c8431 7198 }
252b5132 7199
9933dc52 7200 if (map->includes_filehdr)
bc67d8a6 7201 {
9933dc52
AM
7202 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7203 map->p_paddr -= iehdr->e_ehsize;
7204 /* We've subtracted off the size of headers from the
7205 first section lma, but there may have been some
7206 alignment padding before that section too. Try to
7207 account for that by adjusting the segment lma down to
7208 the same alignment. */
7209 if (segment->p_align != 0 && segment->p_align < align)
7210 align = segment->p_align;
7211 map->p_paddr &= -align;
bc67d8a6 7212 }
252b5132
RH
7213 }
7214
7215 /* Step Three: Loop over the sections again, this time assigning
caf47ea6 7216 those that fit to the current segment and removing them from the
252b5132
RH
7217 sections array; but making sure not to leave large gaps. Once all
7218 possible sections have been assigned to the current segment it is
7219 added to the list of built segments and if sections still remain
7220 to be assigned, a new segment is constructed before repeating
7221 the loop. */
7222 isec = 0;
7223 do
7224 {
bc67d8a6 7225 map->count = 0;
9933dc52 7226 suggested_lma = NULL;
252b5132
RH
7227
7228 /* Fill the current segment with sections that fit. */
bc67d8a6 7229 for (j = 0; j < section_count; j++)
252b5132 7230 {
bc67d8a6 7231 section = sections[j];
252b5132 7232
bc67d8a6 7233 if (section == NULL)
252b5132
RH
7234 continue;
7235
bc67d8a6 7236 output_section = section->output_section;
252b5132 7237
bc67d8a6 7238 BFD_ASSERT (output_section != NULL);
c044fabd 7239
bc67d8a6
NC
7240 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
7241 || IS_COREFILE_NOTE (segment, section))
252b5132 7242 {
bc67d8a6 7243 if (map->count == 0)
252b5132
RH
7244 {
7245 /* If the first section in a segment does not start at
bc67d8a6
NC
7246 the beginning of the segment, then something is
7247 wrong. */
9933dc52
AM
7248 if (align_power (map->p_paddr
7249 + (map->includes_filehdr
7250 ? iehdr->e_ehsize : 0)
7251 + (map->includes_phdrs
7252 ? iehdr->e_phnum * iehdr->e_phentsize
7253 : 0),
7254 output_section->alignment_power)
7255 != output_section->lma)
252b5132
RH
7256 abort ();
7257 }
7258 else
7259 {
0067a569 7260 asection *prev_sec;
252b5132 7261
bc67d8a6 7262 prev_sec = map->sections[map->count - 1];
252b5132
RH
7263
7264 /* If the gap between the end of the previous section
bc67d8a6
NC
7265 and the start of this section is more than
7266 maxpagesize then we need to start a new segment. */
eea6121a 7267 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
079e9a2f 7268 maxpagesize)
caf47ea6 7269 < BFD_ALIGN (output_section->lma, maxpagesize))
0067a569 7270 || (prev_sec->lma + prev_sec->size
079e9a2f 7271 > output_section->lma))
252b5132 7272 {
9933dc52
AM
7273 if (suggested_lma == NULL)
7274 suggested_lma = output_section;
252b5132
RH
7275
7276 continue;
7277 }
7278 }
7279
bc67d8a6 7280 map->sections[map->count++] = output_section;
252b5132
RH
7281 ++isec;
7282 sections[j] = NULL;
9933dc52
AM
7283 if (segment->p_type == PT_LOAD)
7284 section->segment_mark = TRUE;
0067a569 7285 }
9933dc52
AM
7286 else if (suggested_lma == NULL)
7287 suggested_lma = output_section;
252b5132
RH
7288 }
7289
beab4532
NC
7290 /* PR 23932. A corrupt input file may contain sections that cannot
7291 be assigned to any segment - because for example they have a
7292 negative size - or segments that do not contain any sections. */
7293 if (map->count == 0)
7294 {
7295 bfd_set_error (bfd_error_bad_value);
7296 free (sections);
7297 return FALSE;
7298 }
252b5132
RH
7299
7300 /* Add the current segment to the list of built segments. */
c044fabd
KH
7301 *pointer_to_map = map;
7302 pointer_to_map = &map->next;
252b5132 7303
bc67d8a6 7304 if (isec < section_count)
252b5132
RH
7305 {
7306 /* We still have not allocated all of the sections to
7307 segments. Create a new segment here, initialise it
7308 and carry on looping. */
00bee008
AM
7309 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7310 amt += (bfd_size_type) section_count * sizeof (asection *);
5964fc3a 7311 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 7312 if (map == NULL)
5ed6aba4
NC
7313 {
7314 free (sections);
7315 return FALSE;
7316 }
252b5132
RH
7317
7318 /* Initialise the fields of the segment map. Set the physical
7319 physical address to the LMA of the first section that has
7320 not yet been assigned. */
0067a569
AM
7321 map->next = NULL;
7322 map->p_type = segment->p_type;
7323 map->p_flags = segment->p_flags;
7324 map->p_flags_valid = 1;
9933dc52 7325 map->p_paddr = suggested_lma->lma;
5c44b38e 7326 map->p_paddr_valid = p_paddr_valid;
bc67d8a6 7327 map->includes_filehdr = 0;
0067a569 7328 map->includes_phdrs = 0;
252b5132
RH
7329 }
7330 }
bc67d8a6 7331 while (isec < section_count);
252b5132
RH
7332
7333 free (sections);
7334 }
7335
12bd6957 7336 elf_seg_map (obfd) = map_first;
bc67d8a6
NC
7337
7338 /* If we had to estimate the number of program headers that were
9ad5cbcf 7339 going to be needed, then check our estimate now and adjust
bc67d8a6
NC
7340 the offset if necessary. */
7341 if (phdr_adjust_seg != NULL)
7342 {
7343 unsigned int count;
c044fabd 7344
bc67d8a6 7345 for (count = 0, map = map_first; map != NULL; map = map->next)
c044fabd 7346 count++;
252b5132 7347
bc67d8a6
NC
7348 if (count > phdr_adjust_num)
7349 phdr_adjust_seg->p_paddr
7350 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
9933dc52
AM
7351
7352 for (map = map_first; map != NULL; map = map->next)
7353 if (map->p_type == PT_PHDR)
7354 {
7355 bfd_vma adjust
7356 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7357 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7358 break;
7359 }
bc67d8a6 7360 }
c044fabd 7361
bc67d8a6 7362#undef SEGMENT_END
eecdbe52 7363#undef SECTION_SIZE
bc67d8a6
NC
7364#undef IS_CONTAINED_BY_VMA
7365#undef IS_CONTAINED_BY_LMA
0efc80c8 7366#undef IS_NOTE
252b5132 7367#undef IS_COREFILE_NOTE
bc67d8a6 7368#undef IS_SOLARIS_PT_INTERP
9f17e2a6 7369#undef IS_SECTION_IN_INPUT_SEGMENT
bc67d8a6
NC
7370#undef INCLUDE_SECTION_IN_SEGMENT
7371#undef SEGMENT_AFTER_SEGMENT
7372#undef SEGMENT_OVERLAPS
b34976b6 7373 return TRUE;
252b5132
RH
7374}
7375
84d1d650
L
7376/* Copy ELF program header information. */
7377
7378static bfd_boolean
7379copy_elf_program_header (bfd *ibfd, bfd *obfd)
7380{
7381 Elf_Internal_Ehdr *iehdr;
7382 struct elf_segment_map *map;
7383 struct elf_segment_map *map_first;
7384 struct elf_segment_map **pointer_to_map;
7385 Elf_Internal_Phdr *segment;
7386 unsigned int i;
7387 unsigned int num_segments;
7388 bfd_boolean phdr_included = FALSE;
88967714 7389 bfd_boolean p_paddr_valid;
84d1d650
L
7390
7391 iehdr = elf_elfheader (ibfd);
7392
7393 map_first = NULL;
7394 pointer_to_map = &map_first;
7395
88967714
AM
7396 /* If all the segment p_paddr fields are zero, don't set
7397 map->p_paddr_valid. */
7398 p_paddr_valid = FALSE;
84d1d650 7399 num_segments = elf_elfheader (ibfd)->e_phnum;
88967714
AM
7400 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7401 i < num_segments;
7402 i++, segment++)
7403 if (segment->p_paddr != 0)
7404 {
7405 p_paddr_valid = TRUE;
7406 break;
7407 }
7408
84d1d650
L
7409 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7410 i < num_segments;
7411 i++, segment++)
7412 {
7413 asection *section;
7414 unsigned int section_count;
7415 bfd_size_type amt;
7416 Elf_Internal_Shdr *this_hdr;
53020534 7417 asection *first_section = NULL;
a76e6f2f 7418 asection *lowest_section;
2542e49e 7419 bfd_boolean no_contents = TRUE;
84d1d650 7420
84d1d650
L
7421 /* Compute how many sections are in this segment. */
7422 for (section = ibfd->sections, section_count = 0;
7423 section != NULL;
7424 section = section->next)
7425 {
7426 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7427 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
3271a814 7428 {
a76e6f2f
AM
7429 if (first_section == NULL)
7430 first_section = section;
2542e49e
JL
7431 if (elf_section_type (section) != SHT_NOBITS)
7432 no_contents = FALSE;
3271a814
NS
7433 section_count++;
7434 }
84d1d650
L
7435 }
7436
7437 /* Allocate a segment map big enough to contain
7438 all of the sections we have selected. */
00bee008
AM
7439 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7440 amt += (bfd_size_type) section_count * sizeof (asection *);
a50b1753 7441 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
84d1d650
L
7442 if (map == NULL)
7443 return FALSE;
7444
7445 /* Initialize the fields of the output segment map with the
7446 input segment. */
7447 map->next = NULL;
7448 map->p_type = segment->p_type;
7449 map->p_flags = segment->p_flags;
7450 map->p_flags_valid = 1;
7451 map->p_paddr = segment->p_paddr;
88967714 7452 map->p_paddr_valid = p_paddr_valid;
3f570048
AM
7453 map->p_align = segment->p_align;
7454 map->p_align_valid = 1;
3271a814 7455 map->p_vaddr_offset = 0;
84d1d650 7456
04c3a755
NS
7457 if (map->p_type == PT_GNU_RELRO
7458 || map->p_type == PT_GNU_STACK)
b10a8ae0
L
7459 {
7460 /* The PT_GNU_RELRO segment may contain the first a few
7461 bytes in the .got.plt section even if the whole .got.plt
7462 section isn't in the PT_GNU_RELRO segment. We won't
04c3a755
NS
7463 change the size of the PT_GNU_RELRO segment.
7464 Similarly, PT_GNU_STACK size is significant on uclinux
7465 systems. */
9433b9b1 7466 map->p_size = segment->p_memsz;
b10a8ae0
L
7467 map->p_size_valid = 1;
7468 }
7469
84d1d650
L
7470 /* Determine if this segment contains the ELF file header
7471 and if it contains the program headers themselves. */
7472 map->includes_filehdr = (segment->p_offset == 0
7473 && segment->p_filesz >= iehdr->e_ehsize);
7474
7475 map->includes_phdrs = 0;
7476 if (! phdr_included || segment->p_type != PT_LOAD)
7477 {
7478 map->includes_phdrs =
7479 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7480 && (segment->p_offset + segment->p_filesz
7481 >= ((bfd_vma) iehdr->e_phoff
7482 + iehdr->e_phnum * iehdr->e_phentsize)));
7483
7484 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7485 phdr_included = TRUE;
7486 }
7487
bbefd0a9 7488 lowest_section = NULL;
84d1d650
L
7489 if (section_count != 0)
7490 {
7491 unsigned int isec = 0;
7492
53020534 7493 for (section = first_section;
84d1d650
L
7494 section != NULL;
7495 section = section->next)
7496 {
7497 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7498 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
53020534
L
7499 {
7500 map->sections[isec++] = section->output_section;
a76e6f2f
AM
7501 if ((section->flags & SEC_ALLOC) != 0)
7502 {
7503 bfd_vma seg_off;
7504
bbefd0a9
AM
7505 if (lowest_section == NULL
7506 || section->lma < lowest_section->lma)
fb8a5684
AM
7507 lowest_section = section;
7508
a76e6f2f
AM
7509 /* Section lmas are set up from PT_LOAD header
7510 p_paddr in _bfd_elf_make_section_from_shdr.
7511 If this header has a p_paddr that disagrees
7512 with the section lma, flag the p_paddr as
7513 invalid. */
7514 if ((section->flags & SEC_LOAD) != 0)
7515 seg_off = this_hdr->sh_offset - segment->p_offset;
7516 else
7517 seg_off = this_hdr->sh_addr - segment->p_vaddr;
7518 if (section->lma - segment->p_paddr != seg_off)
7519 map->p_paddr_valid = FALSE;
7520 }
53020534
L
7521 if (isec == section_count)
7522 break;
7523 }
84d1d650
L
7524 }
7525 }
7526
a76e6f2f 7527 if (map->includes_filehdr && lowest_section != NULL)
2542e49e
JL
7528 {
7529 /* Try to keep the space used by the headers plus any
7530 padding fixed. If there are sections with file contents
7531 in this segment then the lowest sh_offset is the best
7532 guess. Otherwise the segment only has file contents for
7533 the headers, and p_filesz is the best guess. */
7534 if (no_contents)
7535 map->header_size = segment->p_filesz;
7536 else
7537 map->header_size = lowest_section->filepos;
7538 }
d324f6d6 7539
5d695627
AM
7540 if (section_count == 0)
7541 map->p_vaddr_offset = segment->p_vaddr;
7542 else if (!map->includes_phdrs
7543 && !map->includes_filehdr
7544 && map->p_paddr_valid)
7545 /* Account for padding before the first section. */
7546 map->p_vaddr_offset = (segment->p_paddr
7547 - (lowest_section ? lowest_section->lma : 0));
a76e6f2f 7548
84d1d650
L
7549 map->count = section_count;
7550 *pointer_to_map = map;
7551 pointer_to_map = &map->next;
7552 }
7553
12bd6957 7554 elf_seg_map (obfd) = map_first;
84d1d650
L
7555 return TRUE;
7556}
7557
7558/* Copy private BFD data. This copies or rewrites ELF program header
7559 information. */
7560
7561static bfd_boolean
7562copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7563{
84d1d650
L
7564 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7565 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7566 return TRUE;
7567
7568 if (elf_tdata (ibfd)->phdr == NULL)
7569 return TRUE;
7570
7571 if (ibfd->xvec == obfd->xvec)
7572 {
cb3ff1e5
NC
7573 /* Check to see if any sections in the input BFD
7574 covered by ELF program header have changed. */
d55ce4e2 7575 Elf_Internal_Phdr *segment;
84d1d650
L
7576 asection *section, *osec;
7577 unsigned int i, num_segments;
7578 Elf_Internal_Shdr *this_hdr;
147d51c2
L
7579 const struct elf_backend_data *bed;
7580
7581 bed = get_elf_backend_data (ibfd);
7582
7583 /* Regenerate the segment map if p_paddr is set to 0. */
7584 if (bed->want_p_paddr_set_to_zero)
7585 goto rewrite;
84d1d650
L
7586
7587 /* Initialize the segment mark field. */
7588 for (section = obfd->sections; section != NULL;
7589 section = section->next)
7590 section->segment_mark = FALSE;
7591
7592 num_segments = elf_elfheader (ibfd)->e_phnum;
7593 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7594 i < num_segments;
7595 i++, segment++)
7596 {
5f6999aa
NC
7597 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7598 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7599 which severly confuses things, so always regenerate the segment
7600 map in this case. */
7601 if (segment->p_paddr == 0
7602 && segment->p_memsz == 0
7603 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
cb3ff1e5 7604 goto rewrite;
5f6999aa 7605
84d1d650
L
7606 for (section = ibfd->sections;
7607 section != NULL; section = section->next)
7608 {
7609 /* We mark the output section so that we know it comes
7610 from the input BFD. */
7611 osec = section->output_section;
7612 if (osec)
7613 osec->segment_mark = TRUE;
7614
7615 /* Check if this section is covered by the segment. */
7616 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7617 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
84d1d650
L
7618 {
7619 /* FIXME: Check if its output section is changed or
7620 removed. What else do we need to check? */
7621 if (osec == NULL
7622 || section->flags != osec->flags
7623 || section->lma != osec->lma
7624 || section->vma != osec->vma
7625 || section->size != osec->size
7626 || section->rawsize != osec->rawsize
7627 || section->alignment_power != osec->alignment_power)
7628 goto rewrite;
7629 }
7630 }
7631 }
7632
cb3ff1e5 7633 /* Check to see if any output section do not come from the
84d1d650
L
7634 input BFD. */
7635 for (section = obfd->sections; section != NULL;
7636 section = section->next)
7637 {
535b785f 7638 if (!section->segment_mark)
84d1d650
L
7639 goto rewrite;
7640 else
7641 section->segment_mark = FALSE;
7642 }
7643
7644 return copy_elf_program_header (ibfd, obfd);
7645 }
7646
7647rewrite:
f1d85785
L
7648 if (ibfd->xvec == obfd->xvec)
7649 {
7650 /* When rewriting program header, set the output maxpagesize to
7651 the maximum alignment of input PT_LOAD segments. */
7652 Elf_Internal_Phdr *segment;
7653 unsigned int i;
7654 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
7655 bfd_vma maxpagesize = 0;
7656
7657 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7658 i < num_segments;
7659 i++, segment++)
7660 if (segment->p_type == PT_LOAD
7661 && maxpagesize < segment->p_align)
c86934ce
NC
7662 {
7663 /* PR 17512: file: f17299af. */
7664 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
695344c0 7665 /* xgettext:c-format */
2dcf00ce
AM
7666 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7667 PRIx64 " is too large"),
7668 ibfd, (uint64_t) segment->p_align);
c86934ce
NC
7669 else
7670 maxpagesize = segment->p_align;
7671 }
f1d85785
L
7672
7673 if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
7674 bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
7675 }
7676
84d1d650
L
7677 return rewrite_elf_program_header (ibfd, obfd);
7678}
7679
ccd2ec6a
L
7680/* Initialize private output section information from input section. */
7681
7682bfd_boolean
7683_bfd_elf_init_private_section_data (bfd *ibfd,
7684 asection *isec,
7685 bfd *obfd,
7686 asection *osec,
7687 struct bfd_link_info *link_info)
7688
7689{
7690 Elf_Internal_Shdr *ihdr, *ohdr;
0e1862bb
L
7691 bfd_boolean final_link = (link_info != NULL
7692 && !bfd_link_relocatable (link_info));
ccd2ec6a
L
7693
7694 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7695 || obfd->xvec->flavour != bfd_target_elf_flavour)
7696 return TRUE;
7697
ba85c43e
NC
7698 BFD_ASSERT (elf_section_data (osec) != NULL);
7699
dfa7b0b8
AM
7700 /* For objcopy and relocatable link, don't copy the output ELF
7701 section type from input if the output BFD section flags have been
7702 set to something different. For a final link allow some flags
7703 that the linker clears to differ. */
42bb2e33 7704 if (elf_section_type (osec) == SHT_NULL
dfa7b0b8
AM
7705 && (osec->flags == isec->flags
7706 || (final_link
7707 && ((osec->flags ^ isec->flags)
0814be7d 7708 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
42bb2e33 7709 elf_section_type (osec) = elf_section_type (isec);
d270463e
L
7710
7711 /* FIXME: Is this correct for all OS/PROC specific flags? */
7712 elf_section_flags (osec) |= (elf_section_flags (isec)
7713 & (SHF_MASKOS | SHF_MASKPROC));
ccd2ec6a 7714
a91e1603 7715 /* Copy sh_info from input for mbind section. */
df3a023b
AM
7716 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
7717 && elf_section_flags (isec) & SHF_GNU_MBIND)
a91e1603
L
7718 elf_section_data (osec)->this_hdr.sh_info
7719 = elf_section_data (isec)->this_hdr.sh_info;
7720
ccd2ec6a
L
7721 /* Set things up for objcopy and relocatable link. The output
7722 SHT_GROUP section will have its elf_next_in_group pointing back
7723 to the input group members. Ignore linker created group section.
7724 See elfNN_ia64_object_p in elfxx-ia64.c. */
7bdf4127
AB
7725 if ((link_info == NULL
7726 || !link_info->resolve_section_groups)
7727 && (elf_sec_group (isec) == NULL
7728 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
ccd2ec6a 7729 {
7bdf4127
AB
7730 if (elf_section_flags (isec) & SHF_GROUP)
7731 elf_section_flags (osec) |= SHF_GROUP;
7732 elf_next_in_group (osec) = elf_next_in_group (isec);
7733 elf_section_data (osec)->group = elf_section_data (isec)->group;
ccd2ec6a
L
7734 }
7735
7bdf4127
AB
7736 /* If not decompress, preserve SHF_COMPRESSED. */
7737 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
7738 elf_section_flags (osec) |= (elf_section_flags (isec)
7739 & SHF_COMPRESSED);
7740
ccd2ec6a
L
7741 ihdr = &elf_section_data (isec)->this_hdr;
7742
7743 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7744 don't use the output section of the linked-to section since it
7745 may be NULL at this point. */
7746 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7747 {
7748 ohdr = &elf_section_data (osec)->this_hdr;
7749 ohdr->sh_flags |= SHF_LINK_ORDER;
7750 elf_linked_to_section (osec) = elf_linked_to_section (isec);
7751 }
7752
7753 osec->use_rela_p = isec->use_rela_p;
7754
7755 return TRUE;
7756}
7757
252b5132
RH
7758/* Copy private section information. This copies over the entsize
7759 field, and sometimes the info field. */
7760
b34976b6 7761bfd_boolean
217aa764
AM
7762_bfd_elf_copy_private_section_data (bfd *ibfd,
7763 asection *isec,
7764 bfd *obfd,
7765 asection *osec)
252b5132
RH
7766{
7767 Elf_Internal_Shdr *ihdr, *ohdr;
7768
7769 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7770 || obfd->xvec->flavour != bfd_target_elf_flavour)
b34976b6 7771 return TRUE;
252b5132 7772
252b5132
RH
7773 ihdr = &elf_section_data (isec)->this_hdr;
7774 ohdr = &elf_section_data (osec)->this_hdr;
7775
7776 ohdr->sh_entsize = ihdr->sh_entsize;
7777
7778 if (ihdr->sh_type == SHT_SYMTAB
7779 || ihdr->sh_type == SHT_DYNSYM
7780 || ihdr->sh_type == SHT_GNU_verneed
7781 || ihdr->sh_type == SHT_GNU_verdef)
7782 ohdr->sh_info = ihdr->sh_info;
7783
ccd2ec6a
L
7784 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
7785 NULL);
252b5132
RH
7786}
7787
d0bf826b
AM
7788/* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7789 necessary if we are removing either the SHT_GROUP section or any of
7790 the group member sections. DISCARDED is the value that a section's
7791 output_section has if the section will be discarded, NULL when this
7792 function is called from objcopy, bfd_abs_section_ptr when called
7793 from the linker. */
80fccad2
BW
7794
7795bfd_boolean
d0bf826b 7796_bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
80fccad2 7797{
30288845
AM
7798 asection *isec;
7799
30288845 7800 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
415f38a6 7801 if (elf_section_type (isec) == SHT_GROUP)
30288845
AM
7802 {
7803 asection *first = elf_next_in_group (isec);
7804 asection *s = first;
d0bf826b
AM
7805 bfd_size_type removed = 0;
7806
30288845
AM
7807 while (s != NULL)
7808 {
415f38a6
AM
7809 /* If this member section is being output but the
7810 SHT_GROUP section is not, then clear the group info
7811 set up by _bfd_elf_copy_private_section_data. */
d0bf826b
AM
7812 if (s->output_section != discarded
7813 && isec->output_section == discarded)
30288845
AM
7814 {
7815 elf_section_flags (s->output_section) &= ~SHF_GROUP;
7816 elf_group_name (s->output_section) = NULL;
7817 }
415f38a6
AM
7818 /* Conversely, if the member section is not being output
7819 but the SHT_GROUP section is, then adjust its size. */
d0bf826b
AM
7820 else if (s->output_section == discarded
7821 && isec->output_section != discarded)
6e5e9d58
AM
7822 {
7823 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
7824 removed += 4;
7825 if (elf_sec->rel.hdr != NULL
7826 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
7827 removed += 4;
7828 if (elf_sec->rela.hdr != NULL
7829 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
7830 removed += 4;
7831 }
30288845
AM
7832 s = elf_next_in_group (s);
7833 if (s == first)
7834 break;
7835 }
d0bf826b
AM
7836 if (removed != 0)
7837 {
7838 if (discarded != NULL)
7839 {
7840 /* If we've been called for ld -r, then we need to
6e5e9d58 7841 adjust the input section size. */
d0bf826b
AM
7842 if (isec->rawsize == 0)
7843 isec->rawsize = isec->size;
7844 isec->size = isec->rawsize - removed;
6e5e9d58
AM
7845 if (isec->size <= 4)
7846 {
7847 isec->size = 0;
7848 isec->flags |= SEC_EXCLUDE;
7849 }
d0bf826b
AM
7850 }
7851 else
7852 {
7853 /* Adjust the output section size when called from
7854 objcopy. */
7855 isec->output_section->size -= removed;
6e5e9d58
AM
7856 if (isec->output_section->size <= 4)
7857 {
7858 isec->output_section->size = 0;
7859 isec->output_section->flags |= SEC_EXCLUDE;
7860 }
d0bf826b
AM
7861 }
7862 }
30288845
AM
7863 }
7864
80fccad2
BW
7865 return TRUE;
7866}
7867
d0bf826b
AM
7868/* Copy private header information. */
7869
7870bfd_boolean
7871_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
7872{
7873 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7874 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7875 return TRUE;
7876
7877 /* Copy over private BFD data if it has not already been copied.
7878 This must be done here, rather than in the copy_private_bfd_data
7879 entry point, because the latter is called after the section
7880 contents have been set, which means that the program headers have
7881 already been worked out. */
12bd6957 7882 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
d0bf826b
AM
7883 {
7884 if (! copy_private_bfd_data (ibfd, obfd))
7885 return FALSE;
7886 }
7887
7888 return _bfd_elf_fixup_group_sections (ibfd, NULL);
7889}
7890
252b5132
RH
7891/* Copy private symbol information. If this symbol is in a section
7892 which we did not map into a BFD section, try to map the section
7893 index correctly. We use special macro definitions for the mapped
7894 section indices; these definitions are interpreted by the
7895 swap_out_syms function. */
7896
9ad5cbcf
AM
7897#define MAP_ONESYMTAB (SHN_HIOS + 1)
7898#define MAP_DYNSYMTAB (SHN_HIOS + 2)
7899#define MAP_STRTAB (SHN_HIOS + 3)
7900#define MAP_SHSTRTAB (SHN_HIOS + 4)
7901#define MAP_SYM_SHNDX (SHN_HIOS + 5)
252b5132 7902
b34976b6 7903bfd_boolean
217aa764
AM
7904_bfd_elf_copy_private_symbol_data (bfd *ibfd,
7905 asymbol *isymarg,
7906 bfd *obfd,
7907 asymbol *osymarg)
252b5132
RH
7908{
7909 elf_symbol_type *isym, *osym;
7910
7911 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7912 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 7913 return TRUE;
252b5132
RH
7914
7915 isym = elf_symbol_from (ibfd, isymarg);
7916 osym = elf_symbol_from (obfd, osymarg);
7917
7918 if (isym != NULL
8424d8f5 7919 && isym->internal_elf_sym.st_shndx != 0
252b5132
RH
7920 && osym != NULL
7921 && bfd_is_abs_section (isym->symbol.section))
7922 {
7923 unsigned int shndx;
7924
7925 shndx = isym->internal_elf_sym.st_shndx;
7926 if (shndx == elf_onesymtab (ibfd))
7927 shndx = MAP_ONESYMTAB;
7928 else if (shndx == elf_dynsymtab (ibfd))
7929 shndx = MAP_DYNSYMTAB;
12bd6957 7930 else if (shndx == elf_strtab_sec (ibfd))
252b5132 7931 shndx = MAP_STRTAB;
12bd6957 7932 else if (shndx == elf_shstrtab_sec (ibfd))
252b5132 7933 shndx = MAP_SHSTRTAB;
6a40cf0c 7934 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
9ad5cbcf 7935 shndx = MAP_SYM_SHNDX;
252b5132
RH
7936 osym->internal_elf_sym.st_shndx = shndx;
7937 }
7938
b34976b6 7939 return TRUE;
252b5132
RH
7940}
7941
7942/* Swap out the symbols. */
7943
b34976b6 7944static bfd_boolean
217aa764 7945swap_out_syms (bfd *abfd,
ef10c3ac 7946 struct elf_strtab_hash **sttp,
217aa764 7947 int relocatable_p)
252b5132 7948{
9c5bfbb7 7949 const struct elf_backend_data *bed;
079e9a2f
AM
7950 int symcount;
7951 asymbol **syms;
ef10c3ac 7952 struct elf_strtab_hash *stt;
079e9a2f 7953 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 7954 Elf_Internal_Shdr *symtab_shndx_hdr;
079e9a2f 7955 Elf_Internal_Shdr *symstrtab_hdr;
ef10c3ac 7956 struct elf_sym_strtab *symstrtab;
f075ee0c
AM
7957 bfd_byte *outbound_syms;
7958 bfd_byte *outbound_shndx;
ef10c3ac
L
7959 unsigned long outbound_syms_index;
7960 unsigned long outbound_shndx_index;
079e9a2f 7961 int idx;
12bd6957 7962 unsigned int num_locals;
079e9a2f 7963 bfd_size_type amt;
174fd7f9 7964 bfd_boolean name_local_sections;
252b5132 7965
12bd6957 7966 if (!elf_map_symbols (abfd, &num_locals))
b34976b6 7967 return FALSE;
252b5132 7968
c044fabd 7969 /* Dump out the symtabs. */
ef10c3ac 7970 stt = _bfd_elf_strtab_init ();
079e9a2f 7971 if (stt == NULL)
b34976b6 7972 return FALSE;
252b5132 7973
079e9a2f
AM
7974 bed = get_elf_backend_data (abfd);
7975 symcount = bfd_get_symcount (abfd);
7976 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7977 symtab_hdr->sh_type = SHT_SYMTAB;
7978 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7979 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
12bd6957 7980 symtab_hdr->sh_info = num_locals + 1;
72de5009 7981 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
079e9a2f
AM
7982
7983 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
7984 symstrtab_hdr->sh_type = SHT_STRTAB;
7985
ef10c3ac 7986 /* Allocate buffer to swap out the .strtab section. */
7a6e0d89
AM
7987 symstrtab = (struct elf_sym_strtab *) bfd_malloc2 (symcount + 1,
7988 sizeof (*symstrtab));
ef10c3ac
L
7989 if (symstrtab == NULL)
7990 {
7991 _bfd_elf_strtab_free (stt);
7992 return FALSE;
7993 }
7994
a50b1753 7995 outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
07d6d2b8 7996 bed->s->sizeof_sym);
079e9a2f 7997 if (outbound_syms == NULL)
5ed6aba4 7998 {
ef10c3ac
L
7999error_return:
8000 _bfd_elf_strtab_free (stt);
8001 free (symstrtab);
5ed6aba4
NC
8002 return FALSE;
8003 }
217aa764 8004 symtab_hdr->contents = outbound_syms;
ef10c3ac 8005 outbound_syms_index = 0;
252b5132 8006
9ad5cbcf 8007 outbound_shndx = NULL;
ef10c3ac 8008 outbound_shndx_index = 0;
6a40cf0c
NC
8009
8010 if (elf_symtab_shndx_list (abfd))
9ad5cbcf 8011 {
6a40cf0c
NC
8012 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8013 if (symtab_shndx_hdr->sh_name != 0)
8014 {
8015 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
8016 outbound_shndx = (bfd_byte *)
8017 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
8018 if (outbound_shndx == NULL)
8019 goto error_return;
5ed6aba4 8020
6a40cf0c
NC
8021 symtab_shndx_hdr->contents = outbound_shndx;
8022 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8023 symtab_shndx_hdr->sh_size = amt;
8024 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8025 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8026 }
8027 /* FIXME: What about any other headers in the list ? */
9ad5cbcf
AM
8028 }
8029
589e6347 8030 /* Now generate the data (for "contents"). */
079e9a2f
AM
8031 {
8032 /* Fill in zeroth symbol and swap it out. */
8033 Elf_Internal_Sym sym;
8034 sym.st_name = 0;
8035 sym.st_value = 0;
8036 sym.st_size = 0;
8037 sym.st_info = 0;
8038 sym.st_other = 0;
8039 sym.st_shndx = SHN_UNDEF;
35fc36a8 8040 sym.st_target_internal = 0;
ef10c3ac
L
8041 symstrtab[0].sym = sym;
8042 symstrtab[0].dest_index = outbound_syms_index;
8043 symstrtab[0].destshndx_index = outbound_shndx_index;
8044 outbound_syms_index++;
9ad5cbcf 8045 if (outbound_shndx != NULL)
ef10c3ac 8046 outbound_shndx_index++;
079e9a2f 8047 }
252b5132 8048
174fd7f9
RS
8049 name_local_sections
8050 = (bed->elf_backend_name_local_section_symbols
8051 && bed->elf_backend_name_local_section_symbols (abfd));
8052
079e9a2f 8053 syms = bfd_get_outsymbols (abfd);
ef10c3ac 8054 for (idx = 0; idx < symcount;)
252b5132 8055 {
252b5132 8056 Elf_Internal_Sym sym;
079e9a2f
AM
8057 bfd_vma value = syms[idx]->value;
8058 elf_symbol_type *type_ptr;
8059 flagword flags = syms[idx]->flags;
8060 int type;
252b5132 8061
174fd7f9
RS
8062 if (!name_local_sections
8063 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
079e9a2f
AM
8064 {
8065 /* Local section symbols have no name. */
ef10c3ac 8066 sym.st_name = (unsigned long) -1;
079e9a2f
AM
8067 }
8068 else
8069 {
ef10c3ac
L
8070 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8071 to get the final offset for st_name. */
8072 sym.st_name
8073 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
8074 FALSE);
079e9a2f 8075 if (sym.st_name == (unsigned long) -1)
ef10c3ac 8076 goto error_return;
079e9a2f 8077 }
252b5132 8078
079e9a2f 8079 type_ptr = elf_symbol_from (abfd, syms[idx]);
252b5132 8080
079e9a2f
AM
8081 if ((flags & BSF_SECTION_SYM) == 0
8082 && bfd_is_com_section (syms[idx]->section))
8083 {
8084 /* ELF common symbols put the alignment into the `value' field,
8085 and the size into the `size' field. This is backwards from
8086 how BFD handles it, so reverse it here. */
8087 sym.st_size = value;
8088 if (type_ptr == NULL
8089 || type_ptr->internal_elf_sym.st_value == 0)
8090 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8091 else
8092 sym.st_value = type_ptr->internal_elf_sym.st_value;
8093 sym.st_shndx = _bfd_elf_section_from_bfd_section
8094 (abfd, syms[idx]->section);
8095 }
8096 else
8097 {
8098 asection *sec = syms[idx]->section;
cb33740c 8099 unsigned int shndx;
252b5132 8100
079e9a2f
AM
8101 if (sec->output_section)
8102 {
8103 value += sec->output_offset;
8104 sec = sec->output_section;
8105 }
589e6347 8106
079e9a2f
AM
8107 /* Don't add in the section vma for relocatable output. */
8108 if (! relocatable_p)
8109 value += sec->vma;
8110 sym.st_value = value;
8111 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8112
8113 if (bfd_is_abs_section (sec)
8114 && type_ptr != NULL
8115 && type_ptr->internal_elf_sym.st_shndx != 0)
8116 {
8117 /* This symbol is in a real ELF section which we did
8118 not create as a BFD section. Undo the mapping done
8119 by copy_private_symbol_data. */
8120 shndx = type_ptr->internal_elf_sym.st_shndx;
8121 switch (shndx)
8122 {
8123 case MAP_ONESYMTAB:
8124 shndx = elf_onesymtab (abfd);
8125 break;
8126 case MAP_DYNSYMTAB:
8127 shndx = elf_dynsymtab (abfd);
8128 break;
8129 case MAP_STRTAB:
12bd6957 8130 shndx = elf_strtab_sec (abfd);
079e9a2f
AM
8131 break;
8132 case MAP_SHSTRTAB:
12bd6957 8133 shndx = elf_shstrtab_sec (abfd);
079e9a2f 8134 break;
9ad5cbcf 8135 case MAP_SYM_SHNDX:
6a40cf0c
NC
8136 if (elf_symtab_shndx_list (abfd))
8137 shndx = elf_symtab_shndx_list (abfd)->ndx;
9ad5cbcf 8138 break;
079e9a2f 8139 default:
15bc576a 8140 shndx = SHN_ABS;
079e9a2f
AM
8141 break;
8142 }
8143 }
8144 else
8145 {
8146 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
252b5132 8147
cb33740c 8148 if (shndx == SHN_BAD)
079e9a2f
AM
8149 {
8150 asection *sec2;
8151
8152 /* Writing this would be a hell of a lot easier if
8153 we had some decent documentation on bfd, and
8154 knew what to expect of the library, and what to
8155 demand of applications. For example, it
8156 appears that `objcopy' might not set the
8157 section of a symbol to be a section that is
8158 actually in the output file. */
8159 sec2 = bfd_get_section_by_name (abfd, sec->name);
5df1bc57
AM
8160 if (sec2 != NULL)
8161 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8162 if (shndx == SHN_BAD)
589e6347 8163 {
695344c0 8164 /* xgettext:c-format */
9793eb77
AM
8165 _bfd_error_handler
8166 (_("unable to find equivalent output section"
8167 " for symbol '%s' from section '%s'"),
8168 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8169 sec->name);
811072d8 8170 bfd_set_error (bfd_error_invalid_operation);
ef10c3ac 8171 goto error_return;
589e6347 8172 }
079e9a2f
AM
8173 }
8174 }
252b5132 8175
079e9a2f
AM
8176 sym.st_shndx = shndx;
8177 }
252b5132 8178
13ae64f3
JJ
8179 if ((flags & BSF_THREAD_LOCAL) != 0)
8180 type = STT_TLS;
d8045f23
NC
8181 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8182 type = STT_GNU_IFUNC;
13ae64f3 8183 else if ((flags & BSF_FUNCTION) != 0)
079e9a2f
AM
8184 type = STT_FUNC;
8185 else if ((flags & BSF_OBJECT) != 0)
8186 type = STT_OBJECT;
d9352518
DB
8187 else if ((flags & BSF_RELC) != 0)
8188 type = STT_RELC;
8189 else if ((flags & BSF_SRELC) != 0)
8190 type = STT_SRELC;
079e9a2f
AM
8191 else
8192 type = STT_NOTYPE;
252b5132 8193
13ae64f3
JJ
8194 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8195 type = STT_TLS;
8196
589e6347 8197 /* Processor-specific types. */
079e9a2f
AM
8198 if (type_ptr != NULL
8199 && bed->elf_backend_get_symbol_type)
8200 type = ((*bed->elf_backend_get_symbol_type)
8201 (&type_ptr->internal_elf_sym, type));
252b5132 8202
079e9a2f
AM
8203 if (flags & BSF_SECTION_SYM)
8204 {
8205 if (flags & BSF_GLOBAL)
8206 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8207 else
8208 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8209 }
8210 else if (bfd_is_com_section (syms[idx]->section))
0a40daed 8211 {
b8871f35
L
8212 if (type != STT_TLS)
8213 {
8214 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8215 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8216 ? STT_COMMON : STT_OBJECT);
8217 else
8218 type = ((flags & BSF_ELF_COMMON) != 0
8219 ? STT_COMMON : STT_OBJECT);
8220 }
8221 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
0a40daed 8222 }
079e9a2f
AM
8223 else if (bfd_is_und_section (syms[idx]->section))
8224 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8225 ? STB_WEAK
8226 : STB_GLOBAL),
8227 type);
8228 else if (flags & BSF_FILE)
8229 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8230 else
8231 {
8232 int bind = STB_LOCAL;
252b5132 8233
079e9a2f
AM
8234 if (flags & BSF_LOCAL)
8235 bind = STB_LOCAL;
3e7a7d11
NC
8236 else if (flags & BSF_GNU_UNIQUE)
8237 bind = STB_GNU_UNIQUE;
079e9a2f
AM
8238 else if (flags & BSF_WEAK)
8239 bind = STB_WEAK;
8240 else if (flags & BSF_GLOBAL)
8241 bind = STB_GLOBAL;
252b5132 8242
079e9a2f
AM
8243 sym.st_info = ELF_ST_INFO (bind, type);
8244 }
252b5132 8245
079e9a2f 8246 if (type_ptr != NULL)
35fc36a8
RS
8247 {
8248 sym.st_other = type_ptr->internal_elf_sym.st_other;
8249 sym.st_target_internal
8250 = type_ptr->internal_elf_sym.st_target_internal;
8251 }
079e9a2f 8252 else
35fc36a8
RS
8253 {
8254 sym.st_other = 0;
8255 sym.st_target_internal = 0;
8256 }
252b5132 8257
ef10c3ac
L
8258 idx++;
8259 symstrtab[idx].sym = sym;
8260 symstrtab[idx].dest_index = outbound_syms_index;
8261 symstrtab[idx].destshndx_index = outbound_shndx_index;
8262
8263 outbound_syms_index++;
9ad5cbcf 8264 if (outbound_shndx != NULL)
ef10c3ac
L
8265 outbound_shndx_index++;
8266 }
8267
8268 /* Finalize the .strtab section. */
8269 _bfd_elf_strtab_finalize (stt);
8270
8271 /* Swap out the .strtab section. */
8272 for (idx = 0; idx <= symcount; idx++)
8273 {
8274 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8275 if (elfsym->sym.st_name == (unsigned long) -1)
8276 elfsym->sym.st_name = 0;
8277 else
8278 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8279 elfsym->sym.st_name);
8280 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8281 (outbound_syms
8282 + (elfsym->dest_index
8283 * bed->s->sizeof_sym)),
8284 (outbound_shndx
8285 + (elfsym->destshndx_index
8286 * sizeof (Elf_External_Sym_Shndx))));
079e9a2f 8287 }
ef10c3ac 8288 free (symstrtab);
252b5132 8289
079e9a2f 8290 *sttp = stt;
ef10c3ac 8291 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
079e9a2f 8292 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 8293 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
079e9a2f
AM
8294 symstrtab_hdr->sh_addr = 0;
8295 symstrtab_hdr->sh_entsize = 0;
8296 symstrtab_hdr->sh_link = 0;
8297 symstrtab_hdr->sh_info = 0;
8298 symstrtab_hdr->sh_addralign = 1;
252b5132 8299
b34976b6 8300 return TRUE;
252b5132
RH
8301}
8302
8303/* Return the number of bytes required to hold the symtab vector.
8304
8305 Note that we base it on the count plus 1, since we will null terminate
8306 the vector allocated based on this size. However, the ELF symbol table
8307 always has a dummy entry as symbol #0, so it ends up even. */
8308
8309long
217aa764 8310_bfd_elf_get_symtab_upper_bound (bfd *abfd)
252b5132 8311{
3a551c7a 8312 bfd_size_type symcount;
252b5132
RH
8313 long symtab_size;
8314 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8315
8316 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3a551c7a
AM
8317 if (symcount >= LONG_MAX / sizeof (asymbol *))
8318 {
8319 bfd_set_error (bfd_error_file_too_big);
8320 return -1;
8321 }
b99d1833
AM
8322 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8323 if (symcount > 0)
8324 symtab_size -= sizeof (asymbol *);
252b5132
RH
8325
8326 return symtab_size;
8327}
8328
8329long
217aa764 8330_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
252b5132 8331{
3a551c7a 8332 bfd_size_type symcount;
252b5132
RH
8333 long symtab_size;
8334 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8335
8336 if (elf_dynsymtab (abfd) == 0)
8337 {
8338 bfd_set_error (bfd_error_invalid_operation);
8339 return -1;
8340 }
8341
8342 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3a551c7a
AM
8343 if (symcount >= LONG_MAX / sizeof (asymbol *))
8344 {
8345 bfd_set_error (bfd_error_file_too_big);
8346 return -1;
8347 }
b99d1833
AM
8348 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8349 if (symcount > 0)
8350 symtab_size -= sizeof (asymbol *);
252b5132
RH
8351
8352 return symtab_size;
8353}
8354
8355long
217aa764
AM
8356_bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
8357 sec_ptr asect)
252b5132 8358{
242a1159 8359#if SIZEOF_LONG == SIZEOF_INT
7a6e0d89
AM
8360 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
8361 {
8362 bfd_set_error (bfd_error_file_too_big);
8363 return -1;
8364 }
242a1159 8365#endif
252b5132
RH
8366 return (asect->reloc_count + 1) * sizeof (arelent *);
8367}
8368
8369/* Canonicalize the relocs. */
8370
8371long
217aa764
AM
8372_bfd_elf_canonicalize_reloc (bfd *abfd,
8373 sec_ptr section,
8374 arelent **relptr,
8375 asymbol **symbols)
252b5132
RH
8376{
8377 arelent *tblptr;
8378 unsigned int i;
9c5bfbb7 8379 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 8380
b34976b6 8381 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
252b5132
RH
8382 return -1;
8383
8384 tblptr = section->relocation;
8385 for (i = 0; i < section->reloc_count; i++)
8386 *relptr++ = tblptr++;
8387
8388 *relptr = NULL;
8389
8390 return section->reloc_count;
8391}
8392
8393long
6cee3f79 8394_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
252b5132 8395{
9c5bfbb7 8396 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764 8397 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
252b5132
RH
8398
8399 if (symcount >= 0)
8400 bfd_get_symcount (abfd) = symcount;
8401 return symcount;
8402}
8403
8404long
217aa764
AM
8405_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8406 asymbol **allocation)
252b5132 8407{
9c5bfbb7 8408 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764 8409 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
1f70368c
DJ
8410
8411 if (symcount >= 0)
8412 bfd_get_dynamic_symcount (abfd) = symcount;
8413 return symcount;
252b5132
RH
8414}
8415
8615f3f2
AM
8416/* Return the size required for the dynamic reloc entries. Any loadable
8417 section that was actually installed in the BFD, and has type SHT_REL
8418 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8419 dynamic reloc section. */
252b5132
RH
8420
8421long
217aa764 8422_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
252b5132 8423{
3a551c7a 8424 bfd_size_type count;
252b5132
RH
8425 asection *s;
8426
8427 if (elf_dynsymtab (abfd) == 0)
8428 {
8429 bfd_set_error (bfd_error_invalid_operation);
8430 return -1;
8431 }
8432
3a551c7a 8433 count = 1;
252b5132 8434 for (s = abfd->sections; s != NULL; s = s->next)
266b05cf 8435 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
8436 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8437 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
3a551c7a
AM
8438 {
8439 count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
8440 if (count > LONG_MAX / sizeof (arelent *))
8441 {
8442 bfd_set_error (bfd_error_file_too_big);
8443 return -1;
8444 }
8445 }
8446 return count * sizeof (arelent *);
252b5132
RH
8447}
8448
8615f3f2
AM
8449/* Canonicalize the dynamic relocation entries. Note that we return the
8450 dynamic relocations as a single block, although they are actually
8451 associated with particular sections; the interface, which was
8452 designed for SunOS style shared libraries, expects that there is only
8453 one set of dynamic relocs. Any loadable section that was actually
8454 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8455 dynamic symbol table, is considered to be a dynamic reloc section. */
252b5132
RH
8456
8457long
217aa764
AM
8458_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8459 arelent **storage,
8460 asymbol **syms)
252b5132 8461{
217aa764 8462 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
252b5132
RH
8463 asection *s;
8464 long ret;
8465
8466 if (elf_dynsymtab (abfd) == 0)
8467 {
8468 bfd_set_error (bfd_error_invalid_operation);
8469 return -1;
8470 }
8471
8472 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8473 ret = 0;
8474 for (s = abfd->sections; s != NULL; s = s->next)
8475 {
266b05cf 8476 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
8477 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8478 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8479 {
8480 arelent *p;
8481 long count, i;
8482
b34976b6 8483 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
252b5132 8484 return -1;
eea6121a 8485 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
252b5132
RH
8486 p = s->relocation;
8487 for (i = 0; i < count; i++)
8488 *storage++ = p++;
8489 ret += count;
8490 }
8491 }
8492
8493 *storage = NULL;
8494
8495 return ret;
8496}
8497\f
8498/* Read in the version information. */
8499
b34976b6 8500bfd_boolean
fc0e6df6 8501_bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
252b5132
RH
8502{
8503 bfd_byte *contents = NULL;
fc0e6df6
PB
8504 unsigned int freeidx = 0;
8505
8506 if (elf_dynverref (abfd) != 0)
8507 {
8508 Elf_Internal_Shdr *hdr;
8509 Elf_External_Verneed *everneed;
8510 Elf_Internal_Verneed *iverneed;
8511 unsigned int i;
d0fb9a8d 8512 bfd_byte *contents_end;
fc0e6df6
PB
8513
8514 hdr = &elf_tdata (abfd)->dynverref_hdr;
8515
bd61e135
AM
8516 if (hdr->sh_info == 0
8517 || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
d0fb9a8d 8518 {
601a03ba 8519error_return_bad_verref:
4eca0228 8520 _bfd_error_handler
871b3ab2 8521 (_("%pB: .gnu.version_r invalid entry"), abfd);
601a03ba 8522 bfd_set_error (bfd_error_bad_value);
d0fb9a8d
JJ
8523error_return_verref:
8524 elf_tdata (abfd)->verref = NULL;
8525 elf_tdata (abfd)->cverrefs = 0;
8526 goto error_return;
8527 }
601a03ba 8528
7e56c51c
NC
8529 ufile_ptr filesize = bfd_get_file_size (abfd);
8530 if (filesize > 0 && filesize < hdr->sh_size)
8531 {
8532 /* PR 24708: Avoid attempts to allocate a ridiculous amount
8533 of memory. */
8534 bfd_set_error (bfd_error_no_memory);
8535 _bfd_error_handler
8536 /* xgettext:c-format */
8537 (_("error: %pB version reference section is too large (%#" PRIx64 " bytes)"),
8538 abfd, (uint64_t) hdr->sh_size);
8539 goto error_return_verref;
8540 }
601a03ba
AM
8541 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
8542 if (contents == NULL)
8543 goto error_return_verref;
8544
fc0e6df6
PB
8545 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
8546 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
d0fb9a8d 8547 goto error_return_verref;
fc0e6df6 8548
601a03ba 8549 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
bd61e135 8550 bfd_alloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
601a03ba
AM
8551
8552 if (elf_tdata (abfd)->verref == NULL)
d0fb9a8d
JJ
8553 goto error_return_verref;
8554
8555 BFD_ASSERT (sizeof (Elf_External_Verneed)
8556 == sizeof (Elf_External_Vernaux));
8557 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
fc0e6df6
PB
8558 everneed = (Elf_External_Verneed *) contents;
8559 iverneed = elf_tdata (abfd)->verref;
8560 for (i = 0; i < hdr->sh_info; i++, iverneed++)
8561 {
8562 Elf_External_Vernaux *evernaux;
8563 Elf_Internal_Vernaux *ivernaux;
8564 unsigned int j;
8565
8566 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8567
8568 iverneed->vn_bfd = abfd;
8569
8570 iverneed->vn_filename =
8571 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8572 iverneed->vn_file);
8573 if (iverneed->vn_filename == NULL)
601a03ba 8574 goto error_return_bad_verref;
fc0e6df6 8575
d0fb9a8d
JJ
8576 if (iverneed->vn_cnt == 0)
8577 iverneed->vn_auxptr = NULL;
8578 else
8579 {
a50b1753 8580 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
07d6d2b8
AM
8581 bfd_alloc2 (abfd, iverneed->vn_cnt,
8582 sizeof (Elf_Internal_Vernaux));
d0fb9a8d
JJ
8583 if (iverneed->vn_auxptr == NULL)
8584 goto error_return_verref;
8585 }
8586
8587 if (iverneed->vn_aux
8588 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8589 goto error_return_bad_verref;
fc0e6df6
PB
8590
8591 evernaux = ((Elf_External_Vernaux *)
8592 ((bfd_byte *) everneed + iverneed->vn_aux));
8593 ivernaux = iverneed->vn_auxptr;
8594 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8595 {
8596 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8597
8598 ivernaux->vna_nodename =
8599 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8600 ivernaux->vna_name);
8601 if (ivernaux->vna_nodename == NULL)
601a03ba 8602 goto error_return_bad_verref;
fc0e6df6 8603
25ff461f
AM
8604 if (ivernaux->vna_other > freeidx)
8605 freeidx = ivernaux->vna_other;
8606
8607 ivernaux->vna_nextptr = NULL;
8608 if (ivernaux->vna_next == 0)
8609 {
8610 iverneed->vn_cnt = j + 1;
8611 break;
8612 }
fc0e6df6
PB
8613 if (j + 1 < iverneed->vn_cnt)
8614 ivernaux->vna_nextptr = ivernaux + 1;
fc0e6df6 8615
d0fb9a8d
JJ
8616 if (ivernaux->vna_next
8617 > (size_t) (contents_end - (bfd_byte *) evernaux))
601a03ba 8618 goto error_return_bad_verref;
d0fb9a8d 8619
fc0e6df6
PB
8620 evernaux = ((Elf_External_Vernaux *)
8621 ((bfd_byte *) evernaux + ivernaux->vna_next));
fc0e6df6
PB
8622 }
8623
25ff461f
AM
8624 iverneed->vn_nextref = NULL;
8625 if (iverneed->vn_next == 0)
8626 break;
fc0e6df6
PB
8627 if (i + 1 < hdr->sh_info)
8628 iverneed->vn_nextref = iverneed + 1;
fc0e6df6 8629
d0fb9a8d
JJ
8630 if (iverneed->vn_next
8631 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8632 goto error_return_bad_verref;
d0fb9a8d 8633
fc0e6df6
PB
8634 everneed = ((Elf_External_Verneed *)
8635 ((bfd_byte *) everneed + iverneed->vn_next));
8636 }
25ff461f 8637 elf_tdata (abfd)->cverrefs = i;
fc0e6df6
PB
8638
8639 free (contents);
8640 contents = NULL;
8641 }
252b5132
RH
8642
8643 if (elf_dynverdef (abfd) != 0)
8644 {
8645 Elf_Internal_Shdr *hdr;
8646 Elf_External_Verdef *everdef;
8647 Elf_Internal_Verdef *iverdef;
f631889e
UD
8648 Elf_Internal_Verdef *iverdefarr;
8649 Elf_Internal_Verdef iverdefmem;
252b5132 8650 unsigned int i;
062e2358 8651 unsigned int maxidx;
d0fb9a8d 8652 bfd_byte *contents_end_def, *contents_end_aux;
252b5132
RH
8653
8654 hdr = &elf_tdata (abfd)->dynverdef_hdr;
8655
601a03ba
AM
8656 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
8657 {
8658 error_return_bad_verdef:
4eca0228 8659 _bfd_error_handler
871b3ab2 8660 (_("%pB: .gnu.version_d invalid entry"), abfd);
601a03ba
AM
8661 bfd_set_error (bfd_error_bad_value);
8662 error_return_verdef:
8663 elf_tdata (abfd)->verdef = NULL;
8664 elf_tdata (abfd)->cverdefs = 0;
8665 goto error_return;
8666 }
8667
a50b1753 8668 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
252b5132 8669 if (contents == NULL)
601a03ba 8670 goto error_return_verdef;
252b5132 8671 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
217aa764 8672 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
601a03ba 8673 goto error_return_verdef;
d0fb9a8d
JJ
8674
8675 BFD_ASSERT (sizeof (Elf_External_Verdef)
8676 >= sizeof (Elf_External_Verdaux));
8677 contents_end_def = contents + hdr->sh_size
8678 - sizeof (Elf_External_Verdef);
8679 contents_end_aux = contents + hdr->sh_size
8680 - sizeof (Elf_External_Verdaux);
8681
f631889e
UD
8682 /* We know the number of entries in the section but not the maximum
8683 index. Therefore we have to run through all entries and find
8684 the maximum. */
252b5132 8685 everdef = (Elf_External_Verdef *) contents;
f631889e
UD
8686 maxidx = 0;
8687 for (i = 0; i < hdr->sh_info; ++i)
8688 {
8689 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8690
601a03ba
AM
8691 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
8692 goto error_return_bad_verdef;
062e2358
AM
8693 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
8694 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
f631889e 8695
25ff461f
AM
8696 if (iverdefmem.vd_next == 0)
8697 break;
8698
d0fb9a8d
JJ
8699 if (iverdefmem.vd_next
8700 > (size_t) (contents_end_def - (bfd_byte *) everdef))
601a03ba 8701 goto error_return_bad_verdef;
d0fb9a8d 8702
f631889e
UD
8703 everdef = ((Elf_External_Verdef *)
8704 ((bfd_byte *) everdef + iverdefmem.vd_next));
8705 }
8706
fc0e6df6
PB
8707 if (default_imported_symver)
8708 {
8709 if (freeidx > maxidx)
8710 maxidx = ++freeidx;
8711 else
8712 freeidx = ++maxidx;
8713 }
201159ec 8714
601a03ba
AM
8715 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
8716 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
f631889e 8717 if (elf_tdata (abfd)->verdef == NULL)
601a03ba 8718 goto error_return_verdef;
f631889e
UD
8719
8720 elf_tdata (abfd)->cverdefs = maxidx;
8721
8722 everdef = (Elf_External_Verdef *) contents;
8723 iverdefarr = elf_tdata (abfd)->verdef;
8724 for (i = 0; i < hdr->sh_info; i++)
252b5132
RH
8725 {
8726 Elf_External_Verdaux *everdaux;
8727 Elf_Internal_Verdaux *iverdaux;
8728 unsigned int j;
8729
f631889e
UD
8730 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8731
d0fb9a8d 8732 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
601a03ba 8733 goto error_return_bad_verdef;
d0fb9a8d 8734
f631889e 8735 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
595bce75 8736 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
252b5132
RH
8737
8738 iverdef->vd_bfd = abfd;
8739
d0fb9a8d
JJ
8740 if (iverdef->vd_cnt == 0)
8741 iverdef->vd_auxptr = NULL;
8742 else
8743 {
a50b1753 8744 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
07d6d2b8
AM
8745 bfd_alloc2 (abfd, iverdef->vd_cnt,
8746 sizeof (Elf_Internal_Verdaux));
d0fb9a8d
JJ
8747 if (iverdef->vd_auxptr == NULL)
8748 goto error_return_verdef;
8749 }
8750
8751 if (iverdef->vd_aux
8752 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
601a03ba 8753 goto error_return_bad_verdef;
252b5132
RH
8754
8755 everdaux = ((Elf_External_Verdaux *)
8756 ((bfd_byte *) everdef + iverdef->vd_aux));
8757 iverdaux = iverdef->vd_auxptr;
8758 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
8759 {
8760 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
8761
8762 iverdaux->vda_nodename =
8763 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8764 iverdaux->vda_name);
8765 if (iverdaux->vda_nodename == NULL)
601a03ba 8766 goto error_return_bad_verdef;
252b5132 8767
25ff461f
AM
8768 iverdaux->vda_nextptr = NULL;
8769 if (iverdaux->vda_next == 0)
8770 {
8771 iverdef->vd_cnt = j + 1;
8772 break;
8773 }
252b5132
RH
8774 if (j + 1 < iverdef->vd_cnt)
8775 iverdaux->vda_nextptr = iverdaux + 1;
252b5132 8776
d0fb9a8d
JJ
8777 if (iverdaux->vda_next
8778 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
601a03ba 8779 goto error_return_bad_verdef;
d0fb9a8d 8780
252b5132
RH
8781 everdaux = ((Elf_External_Verdaux *)
8782 ((bfd_byte *) everdaux + iverdaux->vda_next));
8783 }
8784
595bce75 8785 iverdef->vd_nodename = NULL;
d0fb9a8d
JJ
8786 if (iverdef->vd_cnt)
8787 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
252b5132 8788
25ff461f
AM
8789 iverdef->vd_nextdef = NULL;
8790 if (iverdef->vd_next == 0)
8791 break;
d0fb9a8d 8792 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
252b5132 8793 iverdef->vd_nextdef = iverdef + 1;
252b5132
RH
8794
8795 everdef = ((Elf_External_Verdef *)
8796 ((bfd_byte *) everdef + iverdef->vd_next));
8797 }
8798
8799 free (contents);
8800 contents = NULL;
8801 }
fc0e6df6 8802 else if (default_imported_symver)
252b5132 8803 {
fc0e6df6
PB
8804 if (freeidx < 3)
8805 freeidx = 3;
8806 else
8807 freeidx++;
252b5132 8808
a50b1753 8809 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
07d6d2b8 8810 bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
fc0e6df6 8811 if (elf_tdata (abfd)->verdef == NULL)
252b5132
RH
8812 goto error_return;
8813
fc0e6df6
PB
8814 elf_tdata (abfd)->cverdefs = freeidx;
8815 }
252b5132 8816
fc0e6df6
PB
8817 /* Create a default version based on the soname. */
8818 if (default_imported_symver)
8819 {
8820 Elf_Internal_Verdef *iverdef;
8821 Elf_Internal_Verdaux *iverdaux;
252b5132 8822
5bb3703f 8823 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
252b5132 8824
fc0e6df6
PB
8825 iverdef->vd_version = VER_DEF_CURRENT;
8826 iverdef->vd_flags = 0;
8827 iverdef->vd_ndx = freeidx;
8828 iverdef->vd_cnt = 1;
252b5132 8829
fc0e6df6 8830 iverdef->vd_bfd = abfd;
252b5132 8831
fc0e6df6
PB
8832 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
8833 if (iverdef->vd_nodename == NULL)
d0fb9a8d 8834 goto error_return_verdef;
fc0e6df6 8835 iverdef->vd_nextdef = NULL;
601a03ba
AM
8836 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
8837 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
d0fb9a8d
JJ
8838 if (iverdef->vd_auxptr == NULL)
8839 goto error_return_verdef;
252b5132 8840
fc0e6df6
PB
8841 iverdaux = iverdef->vd_auxptr;
8842 iverdaux->vda_nodename = iverdef->vd_nodename;
252b5132
RH
8843 }
8844
b34976b6 8845 return TRUE;
252b5132
RH
8846
8847 error_return:
5ed6aba4 8848 if (contents != NULL)
252b5132 8849 free (contents);
b34976b6 8850 return FALSE;
252b5132
RH
8851}
8852\f
8853asymbol *
217aa764 8854_bfd_elf_make_empty_symbol (bfd *abfd)
252b5132
RH
8855{
8856 elf_symbol_type *newsym;
8857
7a6e0d89 8858 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
252b5132
RH
8859 if (!newsym)
8860 return NULL;
201159ec
NC
8861 newsym->symbol.the_bfd = abfd;
8862 return &newsym->symbol;
252b5132
RH
8863}
8864
8865void
217aa764
AM
8866_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
8867 asymbol *symbol,
8868 symbol_info *ret)
252b5132
RH
8869{
8870 bfd_symbol_info (symbol, ret);
8871}
8872
8873/* Return whether a symbol name implies a local symbol. Most targets
8874 use this function for the is_local_label_name entry point, but some
8875 override it. */
8876
b34976b6 8877bfd_boolean
217aa764
AM
8878_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
8879 const char *name)
252b5132
RH
8880{
8881 /* Normal local symbols start with ``.L''. */
8882 if (name[0] == '.' && name[1] == 'L')
b34976b6 8883 return TRUE;
252b5132
RH
8884
8885 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8886 DWARF debugging symbols starting with ``..''. */
8887 if (name[0] == '.' && name[1] == '.')
b34976b6 8888 return TRUE;
252b5132
RH
8889
8890 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8891 emitting DWARF debugging output. I suspect this is actually a
8892 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8893 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8894 underscore to be emitted on some ELF targets). For ease of use,
8895 we treat such symbols as local. */
8896 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
b34976b6 8897 return TRUE;
252b5132 8898
b1fa9dd6
NC
8899 /* Treat assembler generated fake symbols, dollar local labels and
8900 forward-backward labels (aka local labels) as locals.
8901 These labels have the form:
8902
07d6d2b8 8903 L0^A.* (fake symbols)
b1fa9dd6
NC
8904
8905 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8906
8907 Versions which start with .L will have already been matched above,
8908 so we only need to match the rest. */
8909 if (name[0] == 'L' && ISDIGIT (name[1]))
8910 {
8911 bfd_boolean ret = FALSE;
8912 const char * p;
8913 char c;
8914
8915 for (p = name + 2; (c = *p); p++)
8916 {
8917 if (c == 1 || c == 2)
8918 {
8919 if (c == 1 && p == name + 2)
8920 /* A fake symbol. */
8921 return TRUE;
8922
8923 /* FIXME: We are being paranoid here and treating symbols like
8924 L0^Bfoo as if there were non-local, on the grounds that the
8925 assembler will never generate them. But can any symbol
8926 containing an ASCII value in the range 1-31 ever be anything
8927 other than some kind of local ? */
8928 ret = TRUE;
8929 }
8930
8931 if (! ISDIGIT (c))
8932 {
8933 ret = FALSE;
8934 break;
8935 }
8936 }
8937 return ret;
8938 }
ffa54770 8939
b34976b6 8940 return FALSE;
252b5132
RH
8941}
8942
8943alent *
217aa764
AM
8944_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
8945 asymbol *symbol ATTRIBUTE_UNUSED)
252b5132
RH
8946{
8947 abort ();
8948 return NULL;
8949}
8950
b34976b6 8951bfd_boolean
217aa764
AM
8952_bfd_elf_set_arch_mach (bfd *abfd,
8953 enum bfd_architecture arch,
8954 unsigned long machine)
252b5132
RH
8955{
8956 /* If this isn't the right architecture for this backend, and this
8957 isn't the generic backend, fail. */
8958 if (arch != get_elf_backend_data (abfd)->arch
8959 && arch != bfd_arch_unknown
8960 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
b34976b6 8961 return FALSE;
252b5132
RH
8962
8963 return bfd_default_set_arch_mach (abfd, arch, machine);
8964}
8965
d1fad7c6
NC
8966/* Find the nearest line to a particular section and offset,
8967 for error reporting. */
8968
b34976b6 8969bfd_boolean
217aa764 8970_bfd_elf_find_nearest_line (bfd *abfd,
217aa764 8971 asymbol **symbols,
fb167eb2 8972 asection *section,
217aa764
AM
8973 bfd_vma offset,
8974 const char **filename_ptr,
8975 const char **functionname_ptr,
fb167eb2
AM
8976 unsigned int *line_ptr,
8977 unsigned int *discriminator_ptr)
d1fad7c6 8978{
b34976b6 8979 bfd_boolean found;
d1fad7c6 8980
fb167eb2 8981 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
4e8a9624 8982 filename_ptr, functionname_ptr,
fb167eb2 8983 line_ptr, discriminator_ptr,
9defd221 8984 dwarf_debug_sections,
e00e8198
AM
8985 &elf_tdata (abfd)->dwarf2_find_line_info)
8986 || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
8987 filename_ptr, functionname_ptr,
8988 line_ptr))
d1fad7c6
NC
8989 {
8990 if (!*functionname_ptr)
e00e8198
AM
8991 _bfd_elf_find_function (abfd, symbols, section, offset,
8992 *filename_ptr ? NULL : filename_ptr,
8993 functionname_ptr);
b34976b6 8994 return TRUE;
d1fad7c6
NC
8995 }
8996
8997 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4e8a9624
AM
8998 &found, filename_ptr,
8999 functionname_ptr, line_ptr,
9000 &elf_tdata (abfd)->line_info))
b34976b6 9001 return FALSE;
dc43ada5 9002 if (found && (*functionname_ptr || *line_ptr))
b34976b6 9003 return TRUE;
d1fad7c6
NC
9004
9005 if (symbols == NULL)
b34976b6 9006 return FALSE;
d1fad7c6 9007
e00e8198
AM
9008 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9009 filename_ptr, functionname_ptr))
b34976b6 9010 return FALSE;
d1fad7c6 9011
252b5132 9012 *line_ptr = 0;
b34976b6 9013 return TRUE;
252b5132
RH
9014}
9015
5420f73d
L
9016/* Find the line for a symbol. */
9017
9018bfd_boolean
9019_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9020 const char **filename_ptr, unsigned int *line_ptr)
9b8d1a36 9021{
fb167eb2
AM
9022 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9023 filename_ptr, NULL, line_ptr, NULL,
9defd221 9024 dwarf_debug_sections,
fb167eb2 9025 &elf_tdata (abfd)->dwarf2_find_line_info);
5420f73d
L
9026}
9027
4ab527b0
FF
9028/* After a call to bfd_find_nearest_line, successive calls to
9029 bfd_find_inliner_info can be used to get source information about
9030 each level of function inlining that terminated at the address
9031 passed to bfd_find_nearest_line. Currently this is only supported
9032 for DWARF2 with appropriate DWARF3 extensions. */
9033
9034bfd_boolean
9035_bfd_elf_find_inliner_info (bfd *abfd,
9036 const char **filename_ptr,
9037 const char **functionname_ptr,
9038 unsigned int *line_ptr)
9039{
9040 bfd_boolean found;
9041 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9042 functionname_ptr, line_ptr,
9043 & elf_tdata (abfd)->dwarf2_find_line_info);
9044 return found;
9045}
9046
252b5132 9047int
a6b96beb 9048_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
252b5132 9049{
8ded5a0f
AM
9050 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9051 int ret = bed->s->sizeof_ehdr;
252b5132 9052
0e1862bb 9053 if (!bfd_link_relocatable (info))
8ded5a0f 9054 {
12bd6957 9055 bfd_size_type phdr_size = elf_program_header_size (abfd);
8ded5a0f 9056
62d7a5f6
AM
9057 if (phdr_size == (bfd_size_type) -1)
9058 {
9059 struct elf_segment_map *m;
9060
9061 phdr_size = 0;
12bd6957 9062 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
62d7a5f6 9063 phdr_size += bed->s->sizeof_phdr;
8ded5a0f 9064
62d7a5f6
AM
9065 if (phdr_size == 0)
9066 phdr_size = get_program_header_size (abfd, info);
9067 }
8ded5a0f 9068
12bd6957 9069 elf_program_header_size (abfd) = phdr_size;
8ded5a0f
AM
9070 ret += phdr_size;
9071 }
9072
252b5132
RH
9073 return ret;
9074}
9075
b34976b6 9076bfd_boolean
217aa764
AM
9077_bfd_elf_set_section_contents (bfd *abfd,
9078 sec_ptr section,
0f867abe 9079 const void *location,
217aa764
AM
9080 file_ptr offset,
9081 bfd_size_type count)
252b5132
RH
9082{
9083 Elf_Internal_Shdr *hdr;
1b6aeedb 9084 file_ptr pos;
252b5132
RH
9085
9086 if (! abfd->output_has_begun
217aa764 9087 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
b34976b6 9088 return FALSE;
252b5132 9089
0ce398f1
L
9090 if (!count)
9091 return TRUE;
9092
252b5132 9093 hdr = &elf_section_data (section)->this_hdr;
0ce398f1
L
9094 if (hdr->sh_offset == (file_ptr) -1)
9095 {
9096 /* We must compress this section. Write output to the buffer. */
9097 unsigned char *contents = hdr->contents;
9098 if ((offset + count) > hdr->sh_size
9099 || (section->flags & SEC_ELF_COMPRESS) == 0
9100 || contents == NULL)
9101 abort ();
9102 memcpy (contents + offset, location, count);
9103 return TRUE;
9104 }
dc810e39
AM
9105 pos = hdr->sh_offset + offset;
9106 if (bfd_seek (abfd, pos, SEEK_SET) != 0
9107 || bfd_bwrite (location, count, abfd) != count)
b34976b6 9108 return FALSE;
252b5132 9109
b34976b6 9110 return TRUE;
252b5132
RH
9111}
9112
f3185997 9113bfd_boolean
217aa764
AM
9114_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
9115 arelent *cache_ptr ATTRIBUTE_UNUSED,
9116 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
252b5132
RH
9117{
9118 abort ();
f3185997 9119 return FALSE;
252b5132
RH
9120}
9121
252b5132
RH
9122/* Try to convert a non-ELF reloc into an ELF one. */
9123
b34976b6 9124bfd_boolean
217aa764 9125_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
252b5132 9126{
c044fabd 9127 /* Check whether we really have an ELF howto. */
252b5132
RH
9128
9129 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
9130 {
9131 bfd_reloc_code_real_type code;
9132 reloc_howto_type *howto;
9133
9134 /* Alien reloc: Try to determine its type to replace it with an
c044fabd 9135 equivalent ELF reloc. */
252b5132
RH
9136
9137 if (areloc->howto->pc_relative)
9138 {
9139 switch (areloc->howto->bitsize)
9140 {
9141 case 8:
9142 code = BFD_RELOC_8_PCREL;
9143 break;
9144 case 12:
9145 code = BFD_RELOC_12_PCREL;
9146 break;
9147 case 16:
9148 code = BFD_RELOC_16_PCREL;
9149 break;
9150 case 24:
9151 code = BFD_RELOC_24_PCREL;
9152 break;
9153 case 32:
9154 code = BFD_RELOC_32_PCREL;
9155 break;
9156 case 64:
9157 code = BFD_RELOC_64_PCREL;
9158 break;
9159 default:
9160 goto fail;
9161 }
9162
9163 howto = bfd_reloc_type_lookup (abfd, code);
9164
9165 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
9166 {
9167 if (howto->pcrel_offset)
9168 areloc->addend += areloc->address;
9169 else
9170 areloc->addend -= areloc->address; /* addend is unsigned!! */
9171 }
9172 }
9173 else
9174 {
9175 switch (areloc->howto->bitsize)
9176 {
9177 case 8:
9178 code = BFD_RELOC_8;
9179 break;
9180 case 14:
9181 code = BFD_RELOC_14;
9182 break;
9183 case 16:
9184 code = BFD_RELOC_16;
9185 break;
9186 case 26:
9187 code = BFD_RELOC_26;
9188 break;
9189 case 32:
9190 code = BFD_RELOC_32;
9191 break;
9192 case 64:
9193 code = BFD_RELOC_64;
9194 break;
9195 default:
9196 goto fail;
9197 }
9198
9199 howto = bfd_reloc_type_lookup (abfd, code);
9200 }
9201
9202 if (howto)
9203 areloc->howto = howto;
9204 else
9205 goto fail;
9206 }
9207
b34976b6 9208 return TRUE;
252b5132
RH
9209
9210 fail:
0aa13fee
AM
9211 /* xgettext:c-format */
9212 _bfd_error_handler (_("%pB: %s unsupported"),
9213 abfd, areloc->howto->name);
252b5132 9214 bfd_set_error (bfd_error_bad_value);
b34976b6 9215 return FALSE;
252b5132
RH
9216}
9217
b34976b6 9218bfd_boolean
217aa764 9219_bfd_elf_close_and_cleanup (bfd *abfd)
252b5132 9220{
d9071b0c
TG
9221 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9222 if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
252b5132 9223 {
c0355132 9224 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
2b0f7ef9 9225 _bfd_elf_strtab_free (elf_shstrtab (abfd));
d9071b0c 9226 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
252b5132
RH
9227 }
9228
9229 return _bfd_generic_close_and_cleanup (abfd);
9230}
9231
9232/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9233 in the relocation's offset. Thus we cannot allow any sort of sanity
9234 range-checking to interfere. There is nothing else to do in processing
9235 this reloc. */
9236
9237bfd_reloc_status_type
217aa764
AM
9238_bfd_elf_rel_vtable_reloc_fn
9239 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
fc0a2244 9240 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
217aa764
AM
9241 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9242 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
252b5132
RH
9243{
9244 return bfd_reloc_ok;
9245}
252b5132
RH
9246\f
9247/* Elf core file support. Much of this only works on native
9248 toolchains, since we rely on knowing the
9249 machine-dependent procfs structure in order to pick
c044fabd 9250 out details about the corefile. */
252b5132
RH
9251
9252#ifdef HAVE_SYS_PROCFS_H
16231b7b
DG
9253/* Needed for new procfs interface on sparc-solaris. */
9254# define _STRUCTURED_PROC 1
252b5132
RH
9255# include <sys/procfs.h>
9256#endif
9257
261b8d08
PA
9258/* Return a PID that identifies a "thread" for threaded cores, or the
9259 PID of the main process for non-threaded cores. */
252b5132
RH
9260
9261static int
217aa764 9262elfcore_make_pid (bfd *abfd)
252b5132 9263{
261b8d08
PA
9264 int pid;
9265
228e534f 9266 pid = elf_tdata (abfd)->core->lwpid;
261b8d08 9267 if (pid == 0)
228e534f 9268 pid = elf_tdata (abfd)->core->pid;
261b8d08
PA
9269
9270 return pid;
252b5132
RH
9271}
9272
252b5132
RH
9273/* If there isn't a section called NAME, make one, using
9274 data from SECT. Note, this function will generate a
9275 reference to NAME, so you shouldn't deallocate or
c044fabd 9276 overwrite it. */
252b5132 9277
b34976b6 9278static bfd_boolean
217aa764 9279elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
252b5132 9280{
c044fabd 9281 asection *sect2;
252b5132
RH
9282
9283 if (bfd_get_section_by_name (abfd, name) != NULL)
b34976b6 9284 return TRUE;
252b5132 9285
117ed4f8 9286 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
252b5132 9287 if (sect2 == NULL)
b34976b6 9288 return FALSE;
252b5132 9289
eea6121a 9290 sect2->size = sect->size;
252b5132 9291 sect2->filepos = sect->filepos;
252b5132 9292 sect2->alignment_power = sect->alignment_power;
b34976b6 9293 return TRUE;
252b5132
RH
9294}
9295
bb0082d6
AM
9296/* Create a pseudosection containing SIZE bytes at FILEPOS. This
9297 actually creates up to two pseudosections:
9298 - For the single-threaded case, a section named NAME, unless
9299 such a section already exists.
9300 - For the multi-threaded case, a section named "NAME/PID", where
9301 PID is elfcore_make_pid (abfd).
24d3e51b 9302 Both pseudosections have identical contents. */
b34976b6 9303bfd_boolean
217aa764
AM
9304_bfd_elfcore_make_pseudosection (bfd *abfd,
9305 char *name,
9306 size_t size,
9307 ufile_ptr filepos)
bb0082d6
AM
9308{
9309 char buf[100];
9310 char *threaded_name;
d4c88bbb 9311 size_t len;
bb0082d6
AM
9312 asection *sect;
9313
9314 /* Build the section name. */
9315
9316 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
d4c88bbb 9317 len = strlen (buf) + 1;
a50b1753 9318 threaded_name = (char *) bfd_alloc (abfd, len);
bb0082d6 9319 if (threaded_name == NULL)
b34976b6 9320 return FALSE;
d4c88bbb 9321 memcpy (threaded_name, buf, len);
bb0082d6 9322
117ed4f8
AM
9323 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9324 SEC_HAS_CONTENTS);
bb0082d6 9325 if (sect == NULL)
b34976b6 9326 return FALSE;
eea6121a 9327 sect->size = size;
bb0082d6 9328 sect->filepos = filepos;
bb0082d6
AM
9329 sect->alignment_power = 2;
9330
936e320b 9331 return elfcore_maybe_make_sect (abfd, name, sect);
bb0082d6
AM
9332}
9333
58e07198
CZ
9334static bfd_boolean
9335elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
9336 size_t offs)
9337{
9338 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9339 SEC_HAS_CONTENTS);
9340
9341 if (sect == NULL)
9342 return FALSE;
9343
9344 sect->size = note->descsz - offs;
9345 sect->filepos = note->descpos + offs;
9346 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9347
9348 return TRUE;
9349}
9350
252b5132 9351/* prstatus_t exists on:
4a938328 9352 solaris 2.5+
252b5132
RH
9353 linux 2.[01] + glibc
9354 unixware 4.2
9355*/
9356
9357#if defined (HAVE_PRSTATUS_T)
a7b97311 9358
b34976b6 9359static bfd_boolean
217aa764 9360elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 9361{
eea6121a 9362 size_t size;
7ee38065 9363 int offset;
252b5132 9364
4a938328
MS
9365 if (note->descsz == sizeof (prstatus_t))
9366 {
9367 prstatus_t prstat;
252b5132 9368
eea6121a 9369 size = sizeof (prstat.pr_reg);
7ee38065 9370 offset = offsetof (prstatus_t, pr_reg);
4a938328 9371 memcpy (&prstat, note->descdata, sizeof (prstat));
252b5132 9372
fa49d224
NC
9373 /* Do not overwrite the core signal if it
9374 has already been set by another thread. */
228e534f
AM
9375 if (elf_tdata (abfd)->core->signal == 0)
9376 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9377 if (elf_tdata (abfd)->core->pid == 0)
9378 elf_tdata (abfd)->core->pid = prstat.pr_pid;
252b5132 9379
4a938328
MS
9380 /* pr_who exists on:
9381 solaris 2.5+
9382 unixware 4.2
9383 pr_who doesn't exist on:
9384 linux 2.[01]
9385 */
252b5132 9386#if defined (HAVE_PRSTATUS_T_PR_WHO)
228e534f 9387 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9388#else
228e534f 9389 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
252b5132 9390#endif
4a938328 9391 }
7ee38065 9392#if defined (HAVE_PRSTATUS32_T)
4a938328
MS
9393 else if (note->descsz == sizeof (prstatus32_t))
9394 {
9395 /* 64-bit host, 32-bit corefile */
9396 prstatus32_t prstat;
9397
eea6121a 9398 size = sizeof (prstat.pr_reg);
7ee38065 9399 offset = offsetof (prstatus32_t, pr_reg);
4a938328
MS
9400 memcpy (&prstat, note->descdata, sizeof (prstat));
9401
fa49d224
NC
9402 /* Do not overwrite the core signal if it
9403 has already been set by another thread. */
228e534f
AM
9404 if (elf_tdata (abfd)->core->signal == 0)
9405 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9406 if (elf_tdata (abfd)->core->pid == 0)
9407 elf_tdata (abfd)->core->pid = prstat.pr_pid;
4a938328
MS
9408
9409 /* pr_who exists on:
9410 solaris 2.5+
9411 unixware 4.2
9412 pr_who doesn't exist on:
9413 linux 2.[01]
9414 */
7ee38065 9415#if defined (HAVE_PRSTATUS32_T_PR_WHO)
228e534f 9416 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9417#else
228e534f 9418 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
4a938328
MS
9419#endif
9420 }
7ee38065 9421#endif /* HAVE_PRSTATUS32_T */
4a938328
MS
9422 else
9423 {
9424 /* Fail - we don't know how to handle any other
9425 note size (ie. data object type). */
b34976b6 9426 return TRUE;
4a938328 9427 }
252b5132 9428
bb0082d6 9429 /* Make a ".reg/999" section and a ".reg" section. */
936e320b 9430 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 9431 size, note->descpos + offset);
252b5132
RH
9432}
9433#endif /* defined (HAVE_PRSTATUS_T) */
9434
bb0082d6 9435/* Create a pseudosection containing the exact contents of NOTE. */
b34976b6 9436static bfd_boolean
217aa764
AM
9437elfcore_make_note_pseudosection (bfd *abfd,
9438 char *name,
9439 Elf_Internal_Note *note)
252b5132 9440{
936e320b
AM
9441 return _bfd_elfcore_make_pseudosection (abfd, name,
9442 note->descsz, note->descpos);
252b5132
RH
9443}
9444
ff08c6bb
JB
9445/* There isn't a consistent prfpregset_t across platforms,
9446 but it doesn't matter, because we don't have to pick this
c044fabd
KH
9447 data structure apart. */
9448
b34976b6 9449static bfd_boolean
217aa764 9450elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9451{
9452 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9453}
9454
ff08c6bb 9455/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
971d4640 9456 type of NT_PRXFPREG. Just include the whole note's contents
ff08c6bb 9457 literally. */
c044fabd 9458
b34976b6 9459static bfd_boolean
217aa764 9460elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9461{
9462 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9463}
9464
4339cae0
L
9465/* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9466 with a note type of NT_X86_XSTATE. Just include the whole note's
9467 contents literally. */
9468
9469static bfd_boolean
9470elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9471{
9472 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9473}
9474
97753bd5
AM
9475static bfd_boolean
9476elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9477{
9478 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9479}
9480
89eeb0bc
LM
9481static bfd_boolean
9482elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9483{
9484 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9485}
97753bd5 9486
cb2366c1
EBM
9487static bfd_boolean
9488elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9489{
9490 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9491}
9492
9493static bfd_boolean
9494elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9495{
9496 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9497}
9498
9499static bfd_boolean
9500elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9501{
9502 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9503}
9504
9505static bfd_boolean
9506elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9507{
9508 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9509}
9510
9511static bfd_boolean
9512elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9513{
9514 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9515}
9516
9517static bfd_boolean
9518elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9519{
9520 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9521}
9522
9523static bfd_boolean
9524elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
9525{
9526 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
9527}
9528
9529static bfd_boolean
9530elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
9531{
9532 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
9533}
9534
9535static bfd_boolean
9536elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
9537{
9538 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
9539}
9540
9541static bfd_boolean
9542elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
9543{
9544 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
9545}
9546
9547static bfd_boolean
9548elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
9549{
9550 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
9551}
9552
9553static bfd_boolean
9554elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
9555{
9556 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
9557}
9558
9559static bfd_boolean
9560elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
9561{
9562 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
9563}
9564
0675e188
UW
9565static bfd_boolean
9566elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9567{
9568 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9569}
9570
d7eeb400
MS
9571static bfd_boolean
9572elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9573{
9574 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9575}
9576
9577static bfd_boolean
9578elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9579{
9580 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9581}
9582
9583static bfd_boolean
9584elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
9585{
9586 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
9587}
9588
9589static bfd_boolean
9590elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
9591{
9592 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
9593}
9594
9595static bfd_boolean
9596elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
9597{
9598 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
9599}
9600
355b81d9
UW
9601static bfd_boolean
9602elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
9603{
9604 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
9605}
9606
9607static bfd_boolean
9608elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
9609{
9610 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
9611}
9612
abb3f6cc
NC
9613static bfd_boolean
9614elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
9615{
9616 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
9617}
9618
4ef9f41a
AA
9619static bfd_boolean
9620elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
9621{
9622 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
9623}
9624
9625static bfd_boolean
9626elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
9627{
9628 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
9629}
9630
88ab90e8
AA
9631static bfd_boolean
9632elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
9633{
9634 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
9635}
9636
9637static bfd_boolean
9638elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
9639{
9640 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
9641}
9642
faa9a424
UW
9643static bfd_boolean
9644elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
9645{
9646 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
9647}
9648
652451f8
YZ
9649static bfd_boolean
9650elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
9651{
9652 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
9653}
9654
9655static bfd_boolean
9656elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
9657{
9658 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
9659}
9660
9661static bfd_boolean
9662elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
9663{
9664 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
9665}
9666
ad1cc4e4
AH
9667static bfd_boolean
9668elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
9669{
9670 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
9671}
9672
e6c3b5bf
AH
9673static bfd_boolean
9674elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
9675{
9676 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
9677}
9678
252b5132 9679#if defined (HAVE_PRPSINFO_T)
4a938328 9680typedef prpsinfo_t elfcore_psinfo_t;
7ee38065 9681#if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
9682typedef prpsinfo32_t elfcore_psinfo32_t;
9683#endif
252b5132
RH
9684#endif
9685
9686#if defined (HAVE_PSINFO_T)
4a938328 9687typedef psinfo_t elfcore_psinfo_t;
7ee38065 9688#if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
9689typedef psinfo32_t elfcore_psinfo32_t;
9690#endif
252b5132
RH
9691#endif
9692
252b5132
RH
9693/* return a malloc'ed copy of a string at START which is at
9694 most MAX bytes long, possibly without a terminating '\0'.
c044fabd 9695 the copy will always have a terminating '\0'. */
252b5132 9696
936e320b 9697char *
217aa764 9698_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
252b5132 9699{
dc810e39 9700 char *dups;
a50b1753 9701 char *end = (char *) memchr (start, '\0', max);
dc810e39 9702 size_t len;
252b5132
RH
9703
9704 if (end == NULL)
9705 len = max;
9706 else
9707 len = end - start;
9708
a50b1753 9709 dups = (char *) bfd_alloc (abfd, len + 1);
dc810e39 9710 if (dups == NULL)
252b5132
RH
9711 return NULL;
9712
dc810e39
AM
9713 memcpy (dups, start, len);
9714 dups[len] = '\0';
252b5132 9715
dc810e39 9716 return dups;
252b5132
RH
9717}
9718
bb0082d6 9719#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
b34976b6 9720static bfd_boolean
217aa764 9721elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
252b5132 9722{
4a938328
MS
9723 if (note->descsz == sizeof (elfcore_psinfo_t))
9724 {
9725 elfcore_psinfo_t psinfo;
252b5132 9726
7ee38065 9727 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 9728
335e41d4 9729#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
228e534f 9730 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 9731#endif
228e534f 9732 elf_tdata (abfd)->core->program
936e320b
AM
9733 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9734 sizeof (psinfo.pr_fname));
252b5132 9735
228e534f 9736 elf_tdata (abfd)->core->command
936e320b
AM
9737 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9738 sizeof (psinfo.pr_psargs));
4a938328 9739 }
7ee38065 9740#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
4a938328
MS
9741 else if (note->descsz == sizeof (elfcore_psinfo32_t))
9742 {
9743 /* 64-bit host, 32-bit corefile */
9744 elfcore_psinfo32_t psinfo;
9745
7ee38065 9746 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 9747
335e41d4 9748#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
228e534f 9749 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 9750#endif
228e534f 9751 elf_tdata (abfd)->core->program
936e320b
AM
9752 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9753 sizeof (psinfo.pr_fname));
4a938328 9754
228e534f 9755 elf_tdata (abfd)->core->command
936e320b
AM
9756 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9757 sizeof (psinfo.pr_psargs));
4a938328
MS
9758 }
9759#endif
9760
9761 else
9762 {
9763 /* Fail - we don't know how to handle any other
9764 note size (ie. data object type). */
b34976b6 9765 return TRUE;
4a938328 9766 }
252b5132
RH
9767
9768 /* Note that for some reason, a spurious space is tacked
9769 onto the end of the args in some (at least one anyway)
c044fabd 9770 implementations, so strip it off if it exists. */
252b5132
RH
9771
9772 {
228e534f 9773 char *command = elf_tdata (abfd)->core->command;
252b5132
RH
9774 int n = strlen (command);
9775
9776 if (0 < n && command[n - 1] == ' ')
9777 command[n - 1] = '\0';
9778 }
9779
b34976b6 9780 return TRUE;
252b5132
RH
9781}
9782#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9783
252b5132 9784#if defined (HAVE_PSTATUS_T)
b34976b6 9785static bfd_boolean
217aa764 9786elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 9787{
f572a39d
AM
9788 if (note->descsz == sizeof (pstatus_t)
9789#if defined (HAVE_PXSTATUS_T)
9790 || note->descsz == sizeof (pxstatus_t)
9791#endif
9792 )
4a938328
MS
9793 {
9794 pstatus_t pstat;
252b5132 9795
4a938328 9796 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 9797
228e534f 9798 elf_tdata (abfd)->core->pid = pstat.pr_pid;
4a938328 9799 }
7ee38065 9800#if defined (HAVE_PSTATUS32_T)
4a938328
MS
9801 else if (note->descsz == sizeof (pstatus32_t))
9802 {
9803 /* 64-bit host, 32-bit corefile */
9804 pstatus32_t pstat;
252b5132 9805
4a938328 9806 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 9807
228e534f 9808 elf_tdata (abfd)->core->pid = pstat.pr_pid;
4a938328
MS
9809 }
9810#endif
252b5132
RH
9811 /* Could grab some more details from the "representative"
9812 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
c044fabd 9813 NT_LWPSTATUS note, presumably. */
252b5132 9814
b34976b6 9815 return TRUE;
252b5132
RH
9816}
9817#endif /* defined (HAVE_PSTATUS_T) */
9818
252b5132 9819#if defined (HAVE_LWPSTATUS_T)
b34976b6 9820static bfd_boolean
217aa764 9821elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132
RH
9822{
9823 lwpstatus_t lwpstat;
9824 char buf[100];
c044fabd 9825 char *name;
d4c88bbb 9826 size_t len;
c044fabd 9827 asection *sect;
252b5132 9828
f572a39d
AM
9829 if (note->descsz != sizeof (lwpstat)
9830#if defined (HAVE_LWPXSTATUS_T)
9831 && note->descsz != sizeof (lwpxstatus_t)
9832#endif
9833 )
b34976b6 9834 return TRUE;
252b5132
RH
9835
9836 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
9837
228e534f 9838 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
a1504221
JB
9839 /* Do not overwrite the core signal if it has already been set by
9840 another thread. */
228e534f
AM
9841 if (elf_tdata (abfd)->core->signal == 0)
9842 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
252b5132 9843
c044fabd 9844 /* Make a ".reg/999" section. */
252b5132
RH
9845
9846 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
d4c88bbb 9847 len = strlen (buf) + 1;
217aa764 9848 name = bfd_alloc (abfd, len);
252b5132 9849 if (name == NULL)
b34976b6 9850 return FALSE;
d4c88bbb 9851 memcpy (name, buf, len);
252b5132 9852
117ed4f8 9853 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 9854 if (sect == NULL)
b34976b6 9855 return FALSE;
252b5132
RH
9856
9857#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 9858 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
252b5132
RH
9859 sect->filepos = note->descpos
9860 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
9861#endif
9862
9863#if defined (HAVE_LWPSTATUS_T_PR_REG)
eea6121a 9864 sect->size = sizeof (lwpstat.pr_reg);
252b5132
RH
9865 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
9866#endif
9867
252b5132
RH
9868 sect->alignment_power = 2;
9869
9870 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
b34976b6 9871 return FALSE;
252b5132
RH
9872
9873 /* Make a ".reg2/999" section */
9874
9875 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
d4c88bbb 9876 len = strlen (buf) + 1;
217aa764 9877 name = bfd_alloc (abfd, len);
252b5132 9878 if (name == NULL)
b34976b6 9879 return FALSE;
d4c88bbb 9880 memcpy (name, buf, len);
252b5132 9881
117ed4f8 9882 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 9883 if (sect == NULL)
b34976b6 9884 return FALSE;
252b5132
RH
9885
9886#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 9887 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
252b5132
RH
9888 sect->filepos = note->descpos
9889 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
9890#endif
9891
9892#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
eea6121a 9893 sect->size = sizeof (lwpstat.pr_fpreg);
252b5132
RH
9894 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
9895#endif
9896
252b5132
RH
9897 sect->alignment_power = 2;
9898
936e320b 9899 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
252b5132
RH
9900}
9901#endif /* defined (HAVE_LWPSTATUS_T) */
9902
b34976b6 9903static bfd_boolean
217aa764 9904elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
16e9c715
NC
9905{
9906 char buf[30];
c044fabd 9907 char *name;
d4c88bbb 9908 size_t len;
c044fabd 9909 asection *sect;
4a6636fb
PA
9910 int type;
9911 int is_active_thread;
9912 bfd_vma base_addr;
16e9c715 9913
4a6636fb 9914 if (note->descsz < 728)
b34976b6 9915 return TRUE;
16e9c715 9916
4a6636fb
PA
9917 if (! CONST_STRNEQ (note->namedata, "win32"))
9918 return TRUE;
9919
9920 type = bfd_get_32 (abfd, note->descdata);
c044fabd 9921
4a6636fb 9922 switch (type)
16e9c715 9923 {
4a6636fb 9924 case 1 /* NOTE_INFO_PROCESS */:
228e534f 9925 /* FIXME: need to add ->core->command. */
4a6636fb 9926 /* process_info.pid */
228e534f 9927 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
4a6636fb 9928 /* process_info.signal */
228e534f 9929 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
c044fabd 9930 break;
16e9c715 9931
4a6636fb 9932 case 2 /* NOTE_INFO_THREAD */:
16e9c715 9933 /* Make a ".reg/999" section. */
4a6636fb
PA
9934 /* thread_info.tid */
9935 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
c044fabd 9936
d4c88bbb 9937 len = strlen (buf) + 1;
a50b1753 9938 name = (char *) bfd_alloc (abfd, len);
16e9c715 9939 if (name == NULL)
b34976b6 9940 return FALSE;
c044fabd 9941
d4c88bbb 9942 memcpy (name, buf, len);
16e9c715 9943
117ed4f8 9944 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
16e9c715 9945 if (sect == NULL)
b34976b6 9946 return FALSE;
c044fabd 9947
4a6636fb
PA
9948 /* sizeof (thread_info.thread_context) */
9949 sect->size = 716;
9950 /* offsetof (thread_info.thread_context) */
9951 sect->filepos = note->descpos + 12;
16e9c715
NC
9952 sect->alignment_power = 2;
9953
4a6636fb
PA
9954 /* thread_info.is_active_thread */
9955 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
9956
9957 if (is_active_thread)
16e9c715 9958 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
b34976b6 9959 return FALSE;
16e9c715
NC
9960 break;
9961
4a6636fb 9962 case 3 /* NOTE_INFO_MODULE */:
16e9c715 9963 /* Make a ".module/xxxxxxxx" section. */
4a6636fb
PA
9964 /* module_info.base_address */
9965 base_addr = bfd_get_32 (abfd, note->descdata + 4);
0af1713e 9966 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
c044fabd 9967
d4c88bbb 9968 len = strlen (buf) + 1;
a50b1753 9969 name = (char *) bfd_alloc (abfd, len);
16e9c715 9970 if (name == NULL)
b34976b6 9971 return FALSE;
c044fabd 9972
d4c88bbb 9973 memcpy (name, buf, len);
252b5132 9974
117ed4f8 9975 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
c044fabd 9976
16e9c715 9977 if (sect == NULL)
b34976b6 9978 return FALSE;
c044fabd 9979
eea6121a 9980 sect->size = note->descsz;
16e9c715 9981 sect->filepos = note->descpos;
16e9c715
NC
9982 sect->alignment_power = 2;
9983 break;
9984
9985 default:
b34976b6 9986 return TRUE;
16e9c715
NC
9987 }
9988
b34976b6 9989 return TRUE;
16e9c715 9990}
252b5132 9991
b34976b6 9992static bfd_boolean
217aa764 9993elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
252b5132 9994{
9c5bfbb7 9995 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
bb0082d6 9996
252b5132
RH
9997 switch (note->type)
9998 {
9999 default:
b34976b6 10000 return TRUE;
252b5132 10001
252b5132 10002 case NT_PRSTATUS:
bb0082d6
AM
10003 if (bed->elf_backend_grok_prstatus)
10004 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
b34976b6 10005 return TRUE;
bb0082d6 10006#if defined (HAVE_PRSTATUS_T)
252b5132 10007 return elfcore_grok_prstatus (abfd, note);
bb0082d6 10008#else
b34976b6 10009 return TRUE;
252b5132
RH
10010#endif
10011
10012#if defined (HAVE_PSTATUS_T)
10013 case NT_PSTATUS:
10014 return elfcore_grok_pstatus (abfd, note);
10015#endif
10016
10017#if defined (HAVE_LWPSTATUS_T)
10018 case NT_LWPSTATUS:
10019 return elfcore_grok_lwpstatus (abfd, note);
10020#endif
10021
10022 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
10023 return elfcore_grok_prfpreg (abfd, note);
10024
c044fabd 10025 case NT_WIN32PSTATUS:
16e9c715 10026 return elfcore_grok_win32pstatus (abfd, note);
16e9c715 10027
c044fabd 10028 case NT_PRXFPREG: /* Linux SSE extension */
e377ab71
MK
10029 if (note->namesz == 6
10030 && strcmp (note->namedata, "LINUX") == 0)
ff08c6bb
JB
10031 return elfcore_grok_prxfpreg (abfd, note);
10032 else
b34976b6 10033 return TRUE;
ff08c6bb 10034
4339cae0
L
10035 case NT_X86_XSTATE: /* Linux XSAVE extension */
10036 if (note->namesz == 6
10037 && strcmp (note->namedata, "LINUX") == 0)
10038 return elfcore_grok_xstatereg (abfd, note);
10039 else
10040 return TRUE;
10041
97753bd5
AM
10042 case NT_PPC_VMX:
10043 if (note->namesz == 6
10044 && strcmp (note->namedata, "LINUX") == 0)
10045 return elfcore_grok_ppc_vmx (abfd, note);
10046 else
10047 return TRUE;
10048
89eeb0bc
LM
10049 case NT_PPC_VSX:
10050 if (note->namesz == 6
07d6d2b8
AM
10051 && strcmp (note->namedata, "LINUX") == 0)
10052 return elfcore_grok_ppc_vsx (abfd, note);
89eeb0bc 10053 else
07d6d2b8 10054 return TRUE;
89eeb0bc 10055
cb2366c1
EBM
10056 case NT_PPC_TAR:
10057 if (note->namesz == 6
4b24dd1a
AM
10058 && strcmp (note->namedata, "LINUX") == 0)
10059 return elfcore_grok_ppc_tar (abfd, note);
cb2366c1 10060 else
4b24dd1a 10061 return TRUE;
cb2366c1
EBM
10062
10063 case NT_PPC_PPR:
10064 if (note->namesz == 6
4b24dd1a
AM
10065 && strcmp (note->namedata, "LINUX") == 0)
10066 return elfcore_grok_ppc_ppr (abfd, note);
cb2366c1 10067 else
4b24dd1a 10068 return TRUE;
cb2366c1
EBM
10069
10070 case NT_PPC_DSCR:
10071 if (note->namesz == 6
4b24dd1a
AM
10072 && strcmp (note->namedata, "LINUX") == 0)
10073 return elfcore_grok_ppc_dscr (abfd, note);
cb2366c1 10074 else
4b24dd1a 10075 return TRUE;
cb2366c1
EBM
10076
10077 case NT_PPC_EBB:
10078 if (note->namesz == 6
4b24dd1a
AM
10079 && strcmp (note->namedata, "LINUX") == 0)
10080 return elfcore_grok_ppc_ebb (abfd, note);
cb2366c1 10081 else
4b24dd1a 10082 return TRUE;
cb2366c1
EBM
10083
10084 case NT_PPC_PMU:
10085 if (note->namesz == 6
4b24dd1a
AM
10086 && strcmp (note->namedata, "LINUX") == 0)
10087 return elfcore_grok_ppc_pmu (abfd, note);
cb2366c1 10088 else
4b24dd1a 10089 return TRUE;
cb2366c1
EBM
10090
10091 case NT_PPC_TM_CGPR:
10092 if (note->namesz == 6
4b24dd1a
AM
10093 && strcmp (note->namedata, "LINUX") == 0)
10094 return elfcore_grok_ppc_tm_cgpr (abfd, note);
cb2366c1 10095 else
4b24dd1a 10096 return TRUE;
cb2366c1
EBM
10097
10098 case NT_PPC_TM_CFPR:
10099 if (note->namesz == 6
4b24dd1a
AM
10100 && strcmp (note->namedata, "LINUX") == 0)
10101 return elfcore_grok_ppc_tm_cfpr (abfd, note);
cb2366c1 10102 else
4b24dd1a 10103 return TRUE;
cb2366c1
EBM
10104
10105 case NT_PPC_TM_CVMX:
10106 if (note->namesz == 6
4b24dd1a
AM
10107 && strcmp (note->namedata, "LINUX") == 0)
10108 return elfcore_grok_ppc_tm_cvmx (abfd, note);
cb2366c1 10109 else
4b24dd1a 10110 return TRUE;
cb2366c1
EBM
10111
10112 case NT_PPC_TM_CVSX:
10113 if (note->namesz == 6
4b24dd1a
AM
10114 && strcmp (note->namedata, "LINUX") == 0)
10115 return elfcore_grok_ppc_tm_cvsx (abfd, note);
cb2366c1 10116 else
4b24dd1a 10117 return TRUE;
cb2366c1
EBM
10118
10119 case NT_PPC_TM_SPR:
10120 if (note->namesz == 6
4b24dd1a
AM
10121 && strcmp (note->namedata, "LINUX") == 0)
10122 return elfcore_grok_ppc_tm_spr (abfd, note);
cb2366c1 10123 else
4b24dd1a 10124 return TRUE;
cb2366c1
EBM
10125
10126 case NT_PPC_TM_CTAR:
10127 if (note->namesz == 6
4b24dd1a
AM
10128 && strcmp (note->namedata, "LINUX") == 0)
10129 return elfcore_grok_ppc_tm_ctar (abfd, note);
cb2366c1 10130 else
4b24dd1a 10131 return TRUE;
cb2366c1
EBM
10132
10133 case NT_PPC_TM_CPPR:
10134 if (note->namesz == 6
4b24dd1a
AM
10135 && strcmp (note->namedata, "LINUX") == 0)
10136 return elfcore_grok_ppc_tm_cppr (abfd, note);
cb2366c1 10137 else
4b24dd1a 10138 return TRUE;
cb2366c1
EBM
10139
10140 case NT_PPC_TM_CDSCR:
10141 if (note->namesz == 6
4b24dd1a
AM
10142 && strcmp (note->namedata, "LINUX") == 0)
10143 return elfcore_grok_ppc_tm_cdscr (abfd, note);
cb2366c1 10144 else
4b24dd1a 10145 return TRUE;
cb2366c1 10146
0675e188
UW
10147 case NT_S390_HIGH_GPRS:
10148 if (note->namesz == 6
07d6d2b8
AM
10149 && strcmp (note->namedata, "LINUX") == 0)
10150 return elfcore_grok_s390_high_gprs (abfd, note);
0675e188 10151 else
07d6d2b8 10152 return TRUE;
0675e188 10153
d7eeb400
MS
10154 case NT_S390_TIMER:
10155 if (note->namesz == 6
07d6d2b8
AM
10156 && strcmp (note->namedata, "LINUX") == 0)
10157 return elfcore_grok_s390_timer (abfd, note);
d7eeb400 10158 else
07d6d2b8 10159 return TRUE;
d7eeb400
MS
10160
10161 case NT_S390_TODCMP:
10162 if (note->namesz == 6
07d6d2b8
AM
10163 && strcmp (note->namedata, "LINUX") == 0)
10164 return elfcore_grok_s390_todcmp (abfd, note);
d7eeb400 10165 else
07d6d2b8 10166 return TRUE;
d7eeb400
MS
10167
10168 case NT_S390_TODPREG:
10169 if (note->namesz == 6
07d6d2b8
AM
10170 && strcmp (note->namedata, "LINUX") == 0)
10171 return elfcore_grok_s390_todpreg (abfd, note);
d7eeb400 10172 else
07d6d2b8 10173 return TRUE;
d7eeb400
MS
10174
10175 case NT_S390_CTRS:
10176 if (note->namesz == 6
07d6d2b8
AM
10177 && strcmp (note->namedata, "LINUX") == 0)
10178 return elfcore_grok_s390_ctrs (abfd, note);
d7eeb400 10179 else
07d6d2b8 10180 return TRUE;
d7eeb400
MS
10181
10182 case NT_S390_PREFIX:
10183 if (note->namesz == 6
07d6d2b8
AM
10184 && strcmp (note->namedata, "LINUX") == 0)
10185 return elfcore_grok_s390_prefix (abfd, note);
d7eeb400 10186 else
07d6d2b8 10187 return TRUE;
d7eeb400 10188
355b81d9
UW
10189 case NT_S390_LAST_BREAK:
10190 if (note->namesz == 6
07d6d2b8
AM
10191 && strcmp (note->namedata, "LINUX") == 0)
10192 return elfcore_grok_s390_last_break (abfd, note);
355b81d9 10193 else
07d6d2b8 10194 return TRUE;
355b81d9
UW
10195
10196 case NT_S390_SYSTEM_CALL:
10197 if (note->namesz == 6
07d6d2b8
AM
10198 && strcmp (note->namedata, "LINUX") == 0)
10199 return elfcore_grok_s390_system_call (abfd, note);
355b81d9 10200 else
07d6d2b8 10201 return TRUE;
355b81d9 10202
abb3f6cc
NC
10203 case NT_S390_TDB:
10204 if (note->namesz == 6
07d6d2b8
AM
10205 && strcmp (note->namedata, "LINUX") == 0)
10206 return elfcore_grok_s390_tdb (abfd, note);
abb3f6cc 10207 else
07d6d2b8 10208 return TRUE;
abb3f6cc 10209
4ef9f41a
AA
10210 case NT_S390_VXRS_LOW:
10211 if (note->namesz == 6
10212 && strcmp (note->namedata, "LINUX") == 0)
10213 return elfcore_grok_s390_vxrs_low (abfd, note);
10214 else
10215 return TRUE;
10216
10217 case NT_S390_VXRS_HIGH:
10218 if (note->namesz == 6
10219 && strcmp (note->namedata, "LINUX") == 0)
10220 return elfcore_grok_s390_vxrs_high (abfd, note);
10221 else
10222 return TRUE;
10223
88ab90e8
AA
10224 case NT_S390_GS_CB:
10225 if (note->namesz == 6
10226 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 10227 return elfcore_grok_s390_gs_cb (abfd, note);
88ab90e8
AA
10228 else
10229 return TRUE;
10230
10231 case NT_S390_GS_BC:
10232 if (note->namesz == 6
10233 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 10234 return elfcore_grok_s390_gs_bc (abfd, note);
88ab90e8
AA
10235 else
10236 return TRUE;
10237
faa9a424
UW
10238 case NT_ARM_VFP:
10239 if (note->namesz == 6
10240 && strcmp (note->namedata, "LINUX") == 0)
10241 return elfcore_grok_arm_vfp (abfd, note);
10242 else
10243 return TRUE;
10244
652451f8
YZ
10245 case NT_ARM_TLS:
10246 if (note->namesz == 6
10247 && strcmp (note->namedata, "LINUX") == 0)
10248 return elfcore_grok_aarch_tls (abfd, note);
10249 else
10250 return TRUE;
10251
10252 case NT_ARM_HW_BREAK:
10253 if (note->namesz == 6
10254 && strcmp (note->namedata, "LINUX") == 0)
10255 return elfcore_grok_aarch_hw_break (abfd, note);
10256 else
10257 return TRUE;
10258
10259 case NT_ARM_HW_WATCH:
10260 if (note->namesz == 6
10261 && strcmp (note->namedata, "LINUX") == 0)
10262 return elfcore_grok_aarch_hw_watch (abfd, note);
10263 else
10264 return TRUE;
10265
ad1cc4e4
AH
10266 case NT_ARM_SVE:
10267 if (note->namesz == 6
10268 && strcmp (note->namedata, "LINUX") == 0)
10269 return elfcore_grok_aarch_sve (abfd, note);
10270 else
10271 return TRUE;
10272
e6c3b5bf
AH
10273 case NT_ARM_PAC_MASK:
10274 if (note->namesz == 6
10275 && strcmp (note->namedata, "LINUX") == 0)
10276 return elfcore_grok_aarch_pauth (abfd, note);
10277 else
10278 return TRUE;
10279
252b5132
RH
10280 case NT_PRPSINFO:
10281 case NT_PSINFO:
bb0082d6
AM
10282 if (bed->elf_backend_grok_psinfo)
10283 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
b34976b6 10284 return TRUE;
bb0082d6 10285#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
252b5132 10286 return elfcore_grok_psinfo (abfd, note);
bb0082d6 10287#else
b34976b6 10288 return TRUE;
252b5132 10289#endif
3333a7c3
RM
10290
10291 case NT_AUXV:
58e07198 10292 return elfcore_make_auxv_note_section (abfd, note, 0);
9015683b 10293
451b7c33
TT
10294 case NT_FILE:
10295 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10296 note);
10297
9015683b
TT
10298 case NT_SIGINFO:
10299 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10300 note);
5b2c414d 10301
252b5132
RH
10302 }
10303}
10304
718175fa
JK
10305static bfd_boolean
10306elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10307{
c74f7d1c 10308 struct bfd_build_id* build_id;
30e8ee25
AM
10309
10310 if (note->descsz == 0)
10311 return FALSE;
10312
c74f7d1c
JT
10313 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10314 if (build_id == NULL)
718175fa
JK
10315 return FALSE;
10316
c74f7d1c
JT
10317 build_id->size = note->descsz;
10318 memcpy (build_id->data, note->descdata, note->descsz);
10319 abfd->build_id = build_id;
718175fa
JK
10320
10321 return TRUE;
10322}
10323
10324static bfd_boolean
10325elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10326{
10327 switch (note->type)
10328 {
10329 default:
10330 return TRUE;
10331
46bed679
L
10332 case NT_GNU_PROPERTY_TYPE_0:
10333 return _bfd_elf_parse_gnu_properties (abfd, note);
10334
718175fa
JK
10335 case NT_GNU_BUILD_ID:
10336 return elfobj_grok_gnu_build_id (abfd, note);
10337 }
10338}
10339
e21e5835
NC
10340static bfd_boolean
10341elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10342{
10343 struct sdt_note *cur =
7a6e0d89
AM
10344 (struct sdt_note *) bfd_alloc (abfd,
10345 sizeof (struct sdt_note) + note->descsz);
e21e5835
NC
10346
10347 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10348 cur->size = (bfd_size_type) note->descsz;
10349 memcpy (cur->data, note->descdata, note->descsz);
10350
10351 elf_tdata (abfd)->sdt_note_head = cur;
10352
10353 return TRUE;
10354}
10355
10356static bfd_boolean
10357elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10358{
10359 switch (note->type)
10360 {
10361 case NT_STAPSDT:
10362 return elfobj_grok_stapsdt_note_1 (abfd, note);
10363
10364 default:
10365 return TRUE;
10366 }
10367}
10368
aa1ed4a9
JB
10369static bfd_boolean
10370elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
10371{
10372 size_t offset;
10373
b5430a3c 10374 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 10375 {
b5430a3c 10376 case ELFCLASS32:
0064d223
JB
10377 if (note->descsz < 108)
10378 return FALSE;
aa1ed4a9
JB
10379 break;
10380
b5430a3c 10381 case ELFCLASS64:
0064d223
JB
10382 if (note->descsz < 120)
10383 return FALSE;
aa1ed4a9
JB
10384 break;
10385
10386 default:
10387 return FALSE;
10388 }
10389
0064d223
JB
10390 /* Check for version 1 in pr_version. */
10391 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10392 return FALSE;
80a04378 10393
0064d223
JB
10394 offset = 4;
10395
10396 /* Skip over pr_psinfosz. */
b5430a3c 10397 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
0064d223
JB
10398 offset += 4;
10399 else
10400 {
10401 offset += 4; /* Padding before pr_psinfosz. */
10402 offset += 8;
10403 }
10404
aa1ed4a9
JB
10405 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10406 elf_tdata (abfd)->core->program
10407 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
10408 offset += 17;
10409
10410 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10411 elf_tdata (abfd)->core->command
10412 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
0064d223
JB
10413 offset += 81;
10414
10415 /* Padding before pr_pid. */
10416 offset += 2;
10417
10418 /* The pr_pid field was added in version "1a". */
10419 if (note->descsz < offset + 4)
10420 return TRUE;
10421
10422 elf_tdata (abfd)->core->pid
10423 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
aa1ed4a9
JB
10424
10425 return TRUE;
10426}
10427
10428static bfd_boolean
10429elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
10430{
10431 size_t offset;
10432 size_t size;
24d3e51b 10433 size_t min_size;
aa1ed4a9 10434
24d3e51b
NC
10435 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10436 Also compute minimum size of this note. */
b5430a3c 10437 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 10438 {
b5430a3c 10439 case ELFCLASS32:
24d3e51b
NC
10440 offset = 4 + 4;
10441 min_size = offset + (4 * 2) + 4 + 4 + 4;
aa1ed4a9
JB
10442 break;
10443
b5430a3c 10444 case ELFCLASS64:
24d3e51b
NC
10445 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
10446 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
aa1ed4a9
JB
10447 break;
10448
10449 default:
10450 return FALSE;
10451 }
10452
24d3e51b
NC
10453 if (note->descsz < min_size)
10454 return FALSE;
10455
10456 /* Check for version 1 in pr_version. */
10457 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10458 return FALSE;
aa1ed4a9 10459
24d3e51b
NC
10460 /* Extract size of pr_reg from pr_gregsetsz. */
10461 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
b5430a3c 10462 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
24d3e51b
NC
10463 {
10464 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10465 offset += 4 * 2;
10466 }
b5430a3c 10467 else
24d3e51b
NC
10468 {
10469 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
10470 offset += 8 * 2;
10471 }
aa1ed4a9 10472
24d3e51b 10473 /* Skip over pr_osreldate. */
aa1ed4a9
JB
10474 offset += 4;
10475
24d3e51b 10476 /* Read signal from pr_cursig. */
aa1ed4a9
JB
10477 if (elf_tdata (abfd)->core->signal == 0)
10478 elf_tdata (abfd)->core->signal
10479 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10480 offset += 4;
10481
24d3e51b 10482 /* Read TID from pr_pid. */
aa1ed4a9
JB
10483 elf_tdata (abfd)->core->lwpid
10484 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10485 offset += 4;
10486
24d3e51b 10487 /* Padding before pr_reg. */
b5430a3c 10488 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
aa1ed4a9
JB
10489 offset += 4;
10490
24d3e51b
NC
10491 /* Make sure that there is enough data remaining in the note. */
10492 if ((note->descsz - offset) < size)
10493 return FALSE;
10494
aa1ed4a9
JB
10495 /* Make a ".reg/999" section and a ".reg" section. */
10496 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10497 size, note->descpos + offset);
10498}
10499
10500static bfd_boolean
10501elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
10502{
544c67cd
JB
10503 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10504
aa1ed4a9
JB
10505 switch (note->type)
10506 {
10507 case NT_PRSTATUS:
544c67cd
JB
10508 if (bed->elf_backend_grok_freebsd_prstatus)
10509 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
10510 return TRUE;
aa1ed4a9
JB
10511 return elfcore_grok_freebsd_prstatus (abfd, note);
10512
10513 case NT_FPREGSET:
10514 return elfcore_grok_prfpreg (abfd, note);
10515
10516 case NT_PRPSINFO:
10517 return elfcore_grok_freebsd_psinfo (abfd, note);
10518
10519 case NT_FREEBSD_THRMISC:
10520 if (note->namesz == 8)
10521 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
10522 else
10523 return TRUE;
10524
ddb2bbcf
JB
10525 case NT_FREEBSD_PROCSTAT_PROC:
10526 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
10527 note);
10528
10529 case NT_FREEBSD_PROCSTAT_FILES:
10530 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
10531 note);
10532
10533 case NT_FREEBSD_PROCSTAT_VMMAP:
10534 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
10535 note);
10536
3350c5f5 10537 case NT_FREEBSD_PROCSTAT_AUXV:
58e07198 10538 return elfcore_make_auxv_note_section (abfd, note, 4);
3350c5f5 10539
aa1ed4a9
JB
10540 case NT_X86_XSTATE:
10541 if (note->namesz == 8)
10542 return elfcore_grok_xstatereg (abfd, note);
10543 else
10544 return TRUE;
10545
e6f3b9c3
JB
10546 case NT_FREEBSD_PTLWPINFO:
10547 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
10548 note);
10549
6d5be5d6
JB
10550 case NT_ARM_VFP:
10551 return elfcore_grok_arm_vfp (abfd, note);
10552
aa1ed4a9
JB
10553 default:
10554 return TRUE;
10555 }
10556}
10557
b34976b6 10558static bfd_boolean
217aa764 10559elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
50b2bdb7
AM
10560{
10561 char *cp;
10562
10563 cp = strchr (note->namedata, '@');
10564 if (cp != NULL)
10565 {
d2b64500 10566 *lwpidp = atoi(cp + 1);
b34976b6 10567 return TRUE;
50b2bdb7 10568 }
b34976b6 10569 return FALSE;
50b2bdb7
AM
10570}
10571
b34976b6 10572static bfd_boolean
217aa764 10573elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7 10574{
80a04378
NC
10575 if (note->descsz <= 0x7c + 31)
10576 return FALSE;
10577
50b2bdb7 10578 /* Signal number at offset 0x08. */
228e534f 10579 elf_tdata (abfd)->core->signal
50b2bdb7
AM
10580 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10581
10582 /* Process ID at offset 0x50. */
228e534f 10583 elf_tdata (abfd)->core->pid
50b2bdb7
AM
10584 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
10585
10586 /* Command name at 0x7c (max 32 bytes, including nul). */
228e534f 10587 elf_tdata (abfd)->core->command
50b2bdb7
AM
10588 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
10589
7720ba9f
MK
10590 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
10591 note);
50b2bdb7
AM
10592}
10593
b34976b6 10594static bfd_boolean
217aa764 10595elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7
AM
10596{
10597 int lwp;
10598
10599 if (elfcore_netbsd_get_lwpid (note, &lwp))
228e534f 10600 elf_tdata (abfd)->core->lwpid = lwp;
50b2bdb7 10601
58e07198 10602 switch (note->type)
50b2bdb7 10603 {
58e07198 10604 case NT_NETBSDCORE_PROCINFO:
50b2bdb7 10605 /* NetBSD-specific core "procinfo". Note that we expect to
08a40648
AM
10606 find this note before any of the others, which is fine,
10607 since the kernel writes this note out first when it
10608 creates a core file. */
50b2bdb7 10609 return elfcore_grok_netbsd_procinfo (abfd, note);
58e07198
CZ
10610#ifdef NT_NETBSDCORE_AUXV
10611 case NT_NETBSDCORE_AUXV:
10612 /* NetBSD-specific Elf Auxiliary Vector data. */
10613 return elfcore_make_auxv_note_section (abfd, note, 4);
10614#endif
10615 default:
10616 break;
50b2bdb7
AM
10617 }
10618
58e07198 10619 /* As of March 2017 there are no other machine-independent notes
b4db1224
JT
10620 defined for NetBSD core files. If the note type is less
10621 than the start of the machine-dependent note types, we don't
10622 understand it. */
47d9a591 10623
b4db1224 10624 if (note->type < NT_NETBSDCORE_FIRSTMACH)
b34976b6 10625 return TRUE;
50b2bdb7
AM
10626
10627
10628 switch (bfd_get_arch (abfd))
10629 {
08a40648
AM
10630 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10631 PT_GETFPREGS == mach+2. */
50b2bdb7
AM
10632
10633 case bfd_arch_alpha:
10634 case bfd_arch_sparc:
10635 switch (note->type)
08a40648
AM
10636 {
10637 case NT_NETBSDCORE_FIRSTMACH+0:
10638 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 10639
08a40648
AM
10640 case NT_NETBSDCORE_FIRSTMACH+2:
10641 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 10642
08a40648
AM
10643 default:
10644 return TRUE;
10645 }
50b2bdb7 10646
58e07198
CZ
10647 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
10648 There's also old PT___GETREGS40 == mach + 1 for old reg
10649 structure which lacks GBR. */
10650
10651 case bfd_arch_sh:
10652 switch (note->type)
10653 {
10654 case NT_NETBSDCORE_FIRSTMACH+3:
10655 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10656
10657 case NT_NETBSDCORE_FIRSTMACH+5:
10658 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10659
10660 default:
10661 return TRUE;
10662 }
10663
08a40648
AM
10664 /* On all other arch's, PT_GETREGS == mach+1 and
10665 PT_GETFPREGS == mach+3. */
50b2bdb7
AM
10666
10667 default:
10668 switch (note->type)
08a40648
AM
10669 {
10670 case NT_NETBSDCORE_FIRSTMACH+1:
10671 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 10672
08a40648
AM
10673 case NT_NETBSDCORE_FIRSTMACH+3:
10674 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 10675
08a40648
AM
10676 default:
10677 return TRUE;
10678 }
50b2bdb7
AM
10679 }
10680 /* NOTREACHED */
10681}
10682
67cc5033
MK
10683static bfd_boolean
10684elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
10685{
80a04378
NC
10686 if (note->descsz <= 0x48 + 31)
10687 return FALSE;
10688
67cc5033 10689 /* Signal number at offset 0x08. */
228e534f 10690 elf_tdata (abfd)->core->signal
67cc5033
MK
10691 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10692
10693 /* Process ID at offset 0x20. */
228e534f 10694 elf_tdata (abfd)->core->pid
67cc5033
MK
10695 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
10696
10697 /* Command name at 0x48 (max 32 bytes, including nul). */
228e534f 10698 elf_tdata (abfd)->core->command
67cc5033
MK
10699 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
10700
10701 return TRUE;
10702}
10703
10704static bfd_boolean
10705elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
10706{
10707 if (note->type == NT_OPENBSD_PROCINFO)
10708 return elfcore_grok_openbsd_procinfo (abfd, note);
10709
10710 if (note->type == NT_OPENBSD_REGS)
10711 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10712
10713 if (note->type == NT_OPENBSD_FPREGS)
10714 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10715
10716 if (note->type == NT_OPENBSD_XFPREGS)
10717 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
10718
10719 if (note->type == NT_OPENBSD_AUXV)
58e07198 10720 return elfcore_make_auxv_note_section (abfd, note, 0);
67cc5033
MK
10721
10722 if (note->type == NT_OPENBSD_WCOOKIE)
10723 {
10724 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
10725 SEC_HAS_CONTENTS);
10726
10727 if (sect == NULL)
10728 return FALSE;
10729 sect->size = note->descsz;
10730 sect->filepos = note->descpos;
10731 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10732
10733 return TRUE;
10734 }
10735
10736 return TRUE;
10737}
10738
07c6e936 10739static bfd_boolean
d3fd4074 10740elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
07c6e936
NC
10741{
10742 void *ddata = note->descdata;
10743 char buf[100];
10744 char *name;
10745 asection *sect;
f8843e87
AM
10746 short sig;
10747 unsigned flags;
07c6e936 10748
80a04378
NC
10749 if (note->descsz < 16)
10750 return FALSE;
10751
07c6e936 10752 /* nto_procfs_status 'pid' field is at offset 0. */
228e534f 10753 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
07c6e936 10754
f8843e87
AM
10755 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
10756 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
10757
10758 /* nto_procfs_status 'flags' field is at offset 8. */
10759 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
07c6e936
NC
10760
10761 /* nto_procfs_status 'what' field is at offset 14. */
f8843e87
AM
10762 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
10763 {
228e534f
AM
10764 elf_tdata (abfd)->core->signal = sig;
10765 elf_tdata (abfd)->core->lwpid = *tid;
f8843e87 10766 }
07c6e936 10767
f8843e87
AM
10768 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
10769 do not come from signals so we make sure we set the current
10770 thread just in case. */
10771 if (flags & 0x00000080)
228e534f 10772 elf_tdata (abfd)->core->lwpid = *tid;
07c6e936
NC
10773
10774 /* Make a ".qnx_core_status/%d" section. */
d3fd4074 10775 sprintf (buf, ".qnx_core_status/%ld", *tid);
07c6e936 10776
a50b1753 10777 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936
NC
10778 if (name == NULL)
10779 return FALSE;
10780 strcpy (name, buf);
10781
117ed4f8 10782 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936
NC
10783 if (sect == NULL)
10784 return FALSE;
10785
07d6d2b8
AM
10786 sect->size = note->descsz;
10787 sect->filepos = note->descpos;
07c6e936
NC
10788 sect->alignment_power = 2;
10789
10790 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
10791}
10792
10793static bfd_boolean
d69f560c
KW
10794elfcore_grok_nto_regs (bfd *abfd,
10795 Elf_Internal_Note *note,
d3fd4074 10796 long tid,
d69f560c 10797 char *base)
07c6e936
NC
10798{
10799 char buf[100];
10800 char *name;
10801 asection *sect;
10802
d69f560c 10803 /* Make a "(base)/%d" section. */
d3fd4074 10804 sprintf (buf, "%s/%ld", base, tid);
07c6e936 10805
a50b1753 10806 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936
NC
10807 if (name == NULL)
10808 return FALSE;
10809 strcpy (name, buf);
10810
117ed4f8 10811 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936
NC
10812 if (sect == NULL)
10813 return FALSE;
10814
07d6d2b8
AM
10815 sect->size = note->descsz;
10816 sect->filepos = note->descpos;
07c6e936
NC
10817 sect->alignment_power = 2;
10818
f8843e87 10819 /* This is the current thread. */
228e534f 10820 if (elf_tdata (abfd)->core->lwpid == tid)
d69f560c 10821 return elfcore_maybe_make_sect (abfd, base, sect);
f8843e87
AM
10822
10823 return TRUE;
07c6e936
NC
10824}
10825
10826#define BFD_QNT_CORE_INFO 7
10827#define BFD_QNT_CORE_STATUS 8
10828#define BFD_QNT_CORE_GREG 9
10829#define BFD_QNT_CORE_FPREG 10
10830
10831static bfd_boolean
217aa764 10832elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
07c6e936
NC
10833{
10834 /* Every GREG section has a STATUS section before it. Store the
811072d8 10835 tid from the previous call to pass down to the next gregs
07c6e936 10836 function. */
d3fd4074 10837 static long tid = 1;
07c6e936
NC
10838
10839 switch (note->type)
10840 {
d69f560c
KW
10841 case BFD_QNT_CORE_INFO:
10842 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
10843 case BFD_QNT_CORE_STATUS:
10844 return elfcore_grok_nto_status (abfd, note, &tid);
10845 case BFD_QNT_CORE_GREG:
10846 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
10847 case BFD_QNT_CORE_FPREG:
10848 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
10849 default:
10850 return TRUE;
07c6e936
NC
10851 }
10852}
10853
b15fa79e
AM
10854static bfd_boolean
10855elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
10856{
10857 char *name;
10858 asection *sect;
10859 size_t len;
10860
10861 /* Use note name as section name. */
10862 len = note->namesz;
a50b1753 10863 name = (char *) bfd_alloc (abfd, len);
b15fa79e
AM
10864 if (name == NULL)
10865 return FALSE;
10866 memcpy (name, note->namedata, len);
10867 name[len - 1] = '\0';
10868
10869 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10870 if (sect == NULL)
10871 return FALSE;
10872
07d6d2b8
AM
10873 sect->size = note->descsz;
10874 sect->filepos = note->descpos;
b15fa79e
AM
10875 sect->alignment_power = 1;
10876
10877 return TRUE;
10878}
10879
7c76fa91
MS
10880/* Function: elfcore_write_note
10881
47d9a591 10882 Inputs:
a39f3346 10883 buffer to hold note, and current size of buffer
7c76fa91
MS
10884 name of note
10885 type of note
10886 data for note
10887 size of data for note
10888
a39f3346
AM
10889 Writes note to end of buffer. ELF64 notes are written exactly as
10890 for ELF32, despite the current (as of 2006) ELF gabi specifying
10891 that they ought to have 8-byte namesz and descsz field, and have
10892 8-byte alignment. Other writers, eg. Linux kernel, do the same.
10893
7c76fa91 10894 Return:
a39f3346 10895 Pointer to realloc'd buffer, *BUFSIZ updated. */
7c76fa91
MS
10896
10897char *
a39f3346 10898elfcore_write_note (bfd *abfd,
217aa764 10899 char *buf,
a39f3346 10900 int *bufsiz,
217aa764 10901 const char *name,
a39f3346 10902 int type,
217aa764 10903 const void *input,
a39f3346 10904 int size)
7c76fa91
MS
10905{
10906 Elf_External_Note *xnp;
d4c88bbb 10907 size_t namesz;
d4c88bbb 10908 size_t newspace;
a39f3346 10909 char *dest;
7c76fa91 10910
d4c88bbb 10911 namesz = 0;
d4c88bbb 10912 if (name != NULL)
a39f3346 10913 namesz = strlen (name) + 1;
d4c88bbb 10914
a39f3346 10915 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
d4c88bbb 10916
a50b1753 10917 buf = (char *) realloc (buf, *bufsiz + newspace);
14b1c01e
AM
10918 if (buf == NULL)
10919 return buf;
a39f3346 10920 dest = buf + *bufsiz;
7c76fa91
MS
10921 *bufsiz += newspace;
10922 xnp = (Elf_External_Note *) dest;
10923 H_PUT_32 (abfd, namesz, xnp->namesz);
10924 H_PUT_32 (abfd, size, xnp->descsz);
10925 H_PUT_32 (abfd, type, xnp->type);
d4c88bbb
AM
10926 dest = xnp->name;
10927 if (name != NULL)
10928 {
10929 memcpy (dest, name, namesz);
10930 dest += namesz;
a39f3346 10931 while (namesz & 3)
d4c88bbb
AM
10932 {
10933 *dest++ = '\0';
a39f3346 10934 ++namesz;
d4c88bbb
AM
10935 }
10936 }
10937 memcpy (dest, input, size);
a39f3346
AM
10938 dest += size;
10939 while (size & 3)
10940 {
10941 *dest++ = '\0';
10942 ++size;
10943 }
10944 return buf;
7c76fa91
MS
10945}
10946
602f1657
AM
10947/* gcc-8 warns (*) on all the strncpy calls in this function about
10948 possible string truncation. The "truncation" is not a bug. We
10949 have an external representation of structs with fields that are not
10950 necessarily NULL terminated and corresponding internal
10951 representation fields that are one larger so that they can always
10952 be NULL terminated.
10953 gcc versions between 4.2 and 4.6 do not allow pragma control of
10954 diagnostics inside functions, giving a hard error if you try to use
10955 the finer control available with later versions.
10956 gcc prior to 4.2 warns about diagnostic push and pop.
10957 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
10958 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
10959 (*) Depending on your system header files! */
d99b4b92 10960#if GCC_VERSION >= 8000
602f1657
AM
10961# pragma GCC diagnostic push
10962# pragma GCC diagnostic ignored "-Wstringop-truncation"
d99b4b92 10963#endif
7c76fa91 10964char *
217aa764
AM
10965elfcore_write_prpsinfo (bfd *abfd,
10966 char *buf,
10967 int *bufsiz,
10968 const char *fname,
10969 const char *psargs)
7c76fa91 10970{
183e98be
AM
10971 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10972
10973 if (bed->elf_backend_write_core_note != NULL)
10974 {
10975 char *ret;
10976 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
10977 NT_PRPSINFO, fname, psargs);
10978 if (ret != NULL)
10979 return ret;
10980 }
7c76fa91 10981
1f20dca5 10982#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
602f1657 10983# if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
183e98be
AM
10984 if (bed->s->elfclass == ELFCLASS32)
10985 {
602f1657 10986# if defined (HAVE_PSINFO32_T)
183e98be
AM
10987 psinfo32_t data;
10988 int note_type = NT_PSINFO;
602f1657 10989# else
183e98be
AM
10990 prpsinfo32_t data;
10991 int note_type = NT_PRPSINFO;
602f1657 10992# endif
183e98be
AM
10993
10994 memset (&data, 0, sizeof (data));
10995 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
10996 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
10997 return elfcore_write_note (abfd, buf, bufsiz,
1f20dca5 10998 "CORE", note_type, &data, sizeof (data));
183e98be
AM
10999 }
11000 else
602f1657 11001# endif
183e98be 11002 {
602f1657 11003# if defined (HAVE_PSINFO_T)
183e98be
AM
11004 psinfo_t data;
11005 int note_type = NT_PSINFO;
602f1657 11006# else
183e98be
AM
11007 prpsinfo_t data;
11008 int note_type = NT_PRPSINFO;
602f1657 11009# endif
7c76fa91 11010
183e98be
AM
11011 memset (&data, 0, sizeof (data));
11012 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11013 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11014 return elfcore_write_note (abfd, buf, bufsiz,
1f20dca5 11015 "CORE", note_type, &data, sizeof (data));
183e98be 11016 }
7c76fa91
MS
11017#endif /* PSINFO_T or PRPSINFO_T */
11018
1f20dca5
UW
11019 free (buf);
11020 return NULL;
11021}
d99b4b92 11022#if GCC_VERSION >= 8000
602f1657 11023# pragma GCC diagnostic pop
d99b4b92 11024#endif
1f20dca5 11025
70a38d42
SDJ
11026char *
11027elfcore_write_linux_prpsinfo32
11028 (bfd *abfd, char *buf, int *bufsiz,
11029 const struct elf_internal_linux_prpsinfo *prpsinfo)
11030{
a2f63b2e
MR
11031 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
11032 {
11033 struct elf_external_linux_prpsinfo32_ugid16 data;
11034
11035 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
11036 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11037 &data, sizeof (data));
11038 }
11039 else
11040 {
11041 struct elf_external_linux_prpsinfo32_ugid32 data;
70a38d42 11042
a2f63b2e
MR
11043 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
11044 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11045 &data, sizeof (data));
11046 }
70a38d42
SDJ
11047}
11048
11049char *
11050elfcore_write_linux_prpsinfo64
11051 (bfd *abfd, char *buf, int *bufsiz,
11052 const struct elf_internal_linux_prpsinfo *prpsinfo)
11053{
3c9a7b0d
MR
11054 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
11055 {
11056 struct elf_external_linux_prpsinfo64_ugid16 data;
11057
11058 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
11059 return elfcore_write_note (abfd, buf, bufsiz,
11060 "CORE", NT_PRPSINFO, &data, sizeof (data));
11061 }
11062 else
11063 {
11064 struct elf_external_linux_prpsinfo64_ugid32 data;
70a38d42 11065
3c9a7b0d
MR
11066 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
11067 return elfcore_write_note (abfd, buf, bufsiz,
11068 "CORE", NT_PRPSINFO, &data, sizeof (data));
11069 }
70a38d42
SDJ
11070}
11071
7c76fa91 11072char *
217aa764
AM
11073elfcore_write_prstatus (bfd *abfd,
11074 char *buf,
11075 int *bufsiz,
11076 long pid,
11077 int cursig,
11078 const void *gregs)
7c76fa91 11079{
183e98be 11080 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 11081
183e98be
AM
11082 if (bed->elf_backend_write_core_note != NULL)
11083 {
11084 char *ret;
11085 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11086 NT_PRSTATUS,
11087 pid, cursig, gregs);
11088 if (ret != NULL)
11089 return ret;
11090 }
11091
1f20dca5 11092#if defined (HAVE_PRSTATUS_T)
183e98be
AM
11093#if defined (HAVE_PRSTATUS32_T)
11094 if (bed->s->elfclass == ELFCLASS32)
11095 {
11096 prstatus32_t prstat;
11097
11098 memset (&prstat, 0, sizeof (prstat));
11099 prstat.pr_pid = pid;
11100 prstat.pr_cursig = cursig;
11101 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 11102 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
11103 NT_PRSTATUS, &prstat, sizeof (prstat));
11104 }
11105 else
11106#endif
11107 {
11108 prstatus_t prstat;
11109
11110 memset (&prstat, 0, sizeof (prstat));
11111 prstat.pr_pid = pid;
11112 prstat.pr_cursig = cursig;
11113 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 11114 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
11115 NT_PRSTATUS, &prstat, sizeof (prstat));
11116 }
7c76fa91
MS
11117#endif /* HAVE_PRSTATUS_T */
11118
1f20dca5
UW
11119 free (buf);
11120 return NULL;
11121}
11122
51316059
MS
11123#if defined (HAVE_LWPSTATUS_T)
11124char *
217aa764
AM
11125elfcore_write_lwpstatus (bfd *abfd,
11126 char *buf,
11127 int *bufsiz,
11128 long pid,
11129 int cursig,
11130 const void *gregs)
51316059
MS
11131{
11132 lwpstatus_t lwpstat;
183e98be 11133 const char *note_name = "CORE";
51316059
MS
11134
11135 memset (&lwpstat, 0, sizeof (lwpstat));
11136 lwpstat.pr_lwpid = pid >> 16;
11137 lwpstat.pr_cursig = cursig;
11138#if defined (HAVE_LWPSTATUS_T_PR_REG)
d1e8523e 11139 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
51316059
MS
11140#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11141#if !defined(gregs)
11142 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
11143 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
11144#else
11145 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
11146 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
11147#endif
11148#endif
47d9a591 11149 return elfcore_write_note (abfd, buf, bufsiz, note_name,
51316059
MS
11150 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
11151}
11152#endif /* HAVE_LWPSTATUS_T */
11153
7c76fa91
MS
11154#if defined (HAVE_PSTATUS_T)
11155char *
217aa764
AM
11156elfcore_write_pstatus (bfd *abfd,
11157 char *buf,
11158 int *bufsiz,
11159 long pid,
6c10990d
NC
11160 int cursig ATTRIBUTE_UNUSED,
11161 const void *gregs ATTRIBUTE_UNUSED)
7c76fa91 11162{
183e98be
AM
11163 const char *note_name = "CORE";
11164#if defined (HAVE_PSTATUS32_T)
11165 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 11166
183e98be
AM
11167 if (bed->s->elfclass == ELFCLASS32)
11168 {
11169 pstatus32_t pstat;
11170
11171 memset (&pstat, 0, sizeof (pstat));
11172 pstat.pr_pid = pid & 0xffff;
11173 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11174 NT_PSTATUS, &pstat, sizeof (pstat));
11175 return buf;
11176 }
11177 else
11178#endif
11179 {
11180 pstatus_t pstat;
11181
11182 memset (&pstat, 0, sizeof (pstat));
11183 pstat.pr_pid = pid & 0xffff;
11184 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11185 NT_PSTATUS, &pstat, sizeof (pstat));
11186 return buf;
11187 }
7c76fa91
MS
11188}
11189#endif /* HAVE_PSTATUS_T */
11190
11191char *
217aa764
AM
11192elfcore_write_prfpreg (bfd *abfd,
11193 char *buf,
11194 int *bufsiz,
11195 const void *fpregs,
11196 int size)
7c76fa91 11197{
183e98be 11198 const char *note_name = "CORE";
47d9a591 11199 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
11200 note_name, NT_FPREGSET, fpregs, size);
11201}
11202
11203char *
217aa764
AM
11204elfcore_write_prxfpreg (bfd *abfd,
11205 char *buf,
11206 int *bufsiz,
11207 const void *xfpregs,
11208 int size)
7c76fa91
MS
11209{
11210 char *note_name = "LINUX";
47d9a591 11211 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
11212 note_name, NT_PRXFPREG, xfpregs, size);
11213}
11214
4339cae0
L
11215char *
11216elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
11217 const void *xfpregs, int size)
11218{
97de3545
JB
11219 char *note_name;
11220 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
11221 note_name = "FreeBSD";
11222 else
11223 note_name = "LINUX";
4339cae0
L
11224 return elfcore_write_note (abfd, buf, bufsiz,
11225 note_name, NT_X86_XSTATE, xfpregs, size);
11226}
11227
97753bd5
AM
11228char *
11229elfcore_write_ppc_vmx (bfd *abfd,
11230 char *buf,
11231 int *bufsiz,
11232 const void *ppc_vmx,
11233 int size)
11234{
11235 char *note_name = "LINUX";
11236 return elfcore_write_note (abfd, buf, bufsiz,
11237 note_name, NT_PPC_VMX, ppc_vmx, size);
11238}
11239
89eeb0bc
LM
11240char *
11241elfcore_write_ppc_vsx (bfd *abfd,
07d6d2b8
AM
11242 char *buf,
11243 int *bufsiz,
11244 const void *ppc_vsx,
11245 int size)
89eeb0bc
LM
11246{
11247 char *note_name = "LINUX";
11248 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11249 note_name, NT_PPC_VSX, ppc_vsx, size);
89eeb0bc
LM
11250}
11251
cb2366c1
EBM
11252char *
11253elfcore_write_ppc_tar (bfd *abfd,
4b24dd1a
AM
11254 char *buf,
11255 int *bufsiz,
11256 const void *ppc_tar,
11257 int size)
cb2366c1
EBM
11258{
11259 char *note_name = "LINUX";
11260 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11261 note_name, NT_PPC_TAR, ppc_tar, size);
cb2366c1
EBM
11262}
11263
11264char *
11265elfcore_write_ppc_ppr (bfd *abfd,
4b24dd1a
AM
11266 char *buf,
11267 int *bufsiz,
11268 const void *ppc_ppr,
11269 int size)
cb2366c1
EBM
11270{
11271 char *note_name = "LINUX";
11272 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11273 note_name, NT_PPC_PPR, ppc_ppr, size);
cb2366c1
EBM
11274}
11275
11276char *
11277elfcore_write_ppc_dscr (bfd *abfd,
4b24dd1a
AM
11278 char *buf,
11279 int *bufsiz,
11280 const void *ppc_dscr,
11281 int size)
cb2366c1
EBM
11282{
11283 char *note_name = "LINUX";
11284 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11285 note_name, NT_PPC_DSCR, ppc_dscr, size);
cb2366c1
EBM
11286}
11287
11288char *
11289elfcore_write_ppc_ebb (bfd *abfd,
4b24dd1a
AM
11290 char *buf,
11291 int *bufsiz,
11292 const void *ppc_ebb,
11293 int size)
cb2366c1
EBM
11294{
11295 char *note_name = "LINUX";
11296 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11297 note_name, NT_PPC_EBB, ppc_ebb, size);
cb2366c1
EBM
11298}
11299
11300char *
11301elfcore_write_ppc_pmu (bfd *abfd,
4b24dd1a
AM
11302 char *buf,
11303 int *bufsiz,
11304 const void *ppc_pmu,
11305 int size)
cb2366c1
EBM
11306{
11307 char *note_name = "LINUX";
11308 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11309 note_name, NT_PPC_PMU, ppc_pmu, size);
cb2366c1
EBM
11310}
11311
11312char *
11313elfcore_write_ppc_tm_cgpr (bfd *abfd,
4b24dd1a
AM
11314 char *buf,
11315 int *bufsiz,
11316 const void *ppc_tm_cgpr,
11317 int size)
cb2366c1
EBM
11318{
11319 char *note_name = "LINUX";
11320 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11321 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
cb2366c1
EBM
11322}
11323
11324char *
11325elfcore_write_ppc_tm_cfpr (bfd *abfd,
4b24dd1a
AM
11326 char *buf,
11327 int *bufsiz,
11328 const void *ppc_tm_cfpr,
11329 int size)
cb2366c1
EBM
11330{
11331 char *note_name = "LINUX";
11332 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11333 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
cb2366c1
EBM
11334}
11335
11336char *
11337elfcore_write_ppc_tm_cvmx (bfd *abfd,
4b24dd1a
AM
11338 char *buf,
11339 int *bufsiz,
11340 const void *ppc_tm_cvmx,
11341 int size)
cb2366c1
EBM
11342{
11343 char *note_name = "LINUX";
11344 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11345 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
cb2366c1
EBM
11346}
11347
11348char *
11349elfcore_write_ppc_tm_cvsx (bfd *abfd,
4b24dd1a
AM
11350 char *buf,
11351 int *bufsiz,
11352 const void *ppc_tm_cvsx,
11353 int size)
cb2366c1
EBM
11354{
11355 char *note_name = "LINUX";
11356 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11357 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
cb2366c1
EBM
11358}
11359
11360char *
11361elfcore_write_ppc_tm_spr (bfd *abfd,
4b24dd1a
AM
11362 char *buf,
11363 int *bufsiz,
11364 const void *ppc_tm_spr,
11365 int size)
cb2366c1
EBM
11366{
11367 char *note_name = "LINUX";
11368 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11369 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
cb2366c1
EBM
11370}
11371
11372char *
11373elfcore_write_ppc_tm_ctar (bfd *abfd,
4b24dd1a
AM
11374 char *buf,
11375 int *bufsiz,
11376 const void *ppc_tm_ctar,
11377 int size)
cb2366c1
EBM
11378{
11379 char *note_name = "LINUX";
11380 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11381 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
cb2366c1
EBM
11382}
11383
11384char *
11385elfcore_write_ppc_tm_cppr (bfd *abfd,
4b24dd1a
AM
11386 char *buf,
11387 int *bufsiz,
11388 const void *ppc_tm_cppr,
11389 int size)
cb2366c1
EBM
11390{
11391 char *note_name = "LINUX";
11392 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11393 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
cb2366c1
EBM
11394}
11395
11396char *
11397elfcore_write_ppc_tm_cdscr (bfd *abfd,
4b24dd1a
AM
11398 char *buf,
11399 int *bufsiz,
11400 const void *ppc_tm_cdscr,
11401 int size)
cb2366c1
EBM
11402{
11403 char *note_name = "LINUX";
11404 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11405 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
cb2366c1
EBM
11406}
11407
0675e188
UW
11408static char *
11409elfcore_write_s390_high_gprs (bfd *abfd,
11410 char *buf,
11411 int *bufsiz,
11412 const void *s390_high_gprs,
11413 int size)
11414{
11415 char *note_name = "LINUX";
11416 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11417 note_name, NT_S390_HIGH_GPRS,
0675e188
UW
11418 s390_high_gprs, size);
11419}
11420
d7eeb400
MS
11421char *
11422elfcore_write_s390_timer (bfd *abfd,
07d6d2b8
AM
11423 char *buf,
11424 int *bufsiz,
11425 const void *s390_timer,
11426 int size)
d7eeb400
MS
11427{
11428 char *note_name = "LINUX";
11429 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11430 note_name, NT_S390_TIMER, s390_timer, size);
d7eeb400
MS
11431}
11432
11433char *
11434elfcore_write_s390_todcmp (bfd *abfd,
07d6d2b8
AM
11435 char *buf,
11436 int *bufsiz,
11437 const void *s390_todcmp,
11438 int size)
d7eeb400
MS
11439{
11440 char *note_name = "LINUX";
11441 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11442 note_name, NT_S390_TODCMP, s390_todcmp, size);
d7eeb400
MS
11443}
11444
11445char *
11446elfcore_write_s390_todpreg (bfd *abfd,
07d6d2b8
AM
11447 char *buf,
11448 int *bufsiz,
11449 const void *s390_todpreg,
11450 int size)
d7eeb400
MS
11451{
11452 char *note_name = "LINUX";
11453 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11454 note_name, NT_S390_TODPREG, s390_todpreg, size);
d7eeb400
MS
11455}
11456
11457char *
11458elfcore_write_s390_ctrs (bfd *abfd,
07d6d2b8
AM
11459 char *buf,
11460 int *bufsiz,
11461 const void *s390_ctrs,
11462 int size)
d7eeb400
MS
11463{
11464 char *note_name = "LINUX";
11465 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11466 note_name, NT_S390_CTRS, s390_ctrs, size);
d7eeb400
MS
11467}
11468
11469char *
11470elfcore_write_s390_prefix (bfd *abfd,
07d6d2b8
AM
11471 char *buf,
11472 int *bufsiz,
11473 const void *s390_prefix,
11474 int size)
d7eeb400
MS
11475{
11476 char *note_name = "LINUX";
11477 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11478 note_name, NT_S390_PREFIX, s390_prefix, size);
d7eeb400
MS
11479}
11480
355b81d9
UW
11481char *
11482elfcore_write_s390_last_break (bfd *abfd,
11483 char *buf,
11484 int *bufsiz,
11485 const void *s390_last_break,
11486 int size)
11487{
11488 char *note_name = "LINUX";
11489 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11490 note_name, NT_S390_LAST_BREAK,
355b81d9
UW
11491 s390_last_break, size);
11492}
11493
11494char *
11495elfcore_write_s390_system_call (bfd *abfd,
11496 char *buf,
11497 int *bufsiz,
11498 const void *s390_system_call,
11499 int size)
11500{
11501 char *note_name = "LINUX";
11502 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11503 note_name, NT_S390_SYSTEM_CALL,
355b81d9
UW
11504 s390_system_call, size);
11505}
11506
abb3f6cc
NC
11507char *
11508elfcore_write_s390_tdb (bfd *abfd,
11509 char *buf,
11510 int *bufsiz,
11511 const void *s390_tdb,
11512 int size)
11513{
11514 char *note_name = "LINUX";
11515 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11516 note_name, NT_S390_TDB, s390_tdb, size);
abb3f6cc
NC
11517}
11518
4ef9f41a
AA
11519char *
11520elfcore_write_s390_vxrs_low (bfd *abfd,
11521 char *buf,
11522 int *bufsiz,
11523 const void *s390_vxrs_low,
11524 int size)
11525{
11526 char *note_name = "LINUX";
11527 return elfcore_write_note (abfd, buf, bufsiz,
11528 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
11529}
11530
11531char *
11532elfcore_write_s390_vxrs_high (bfd *abfd,
11533 char *buf,
11534 int *bufsiz,
11535 const void *s390_vxrs_high,
11536 int size)
11537{
11538 char *note_name = "LINUX";
11539 return elfcore_write_note (abfd, buf, bufsiz,
11540 note_name, NT_S390_VXRS_HIGH,
11541 s390_vxrs_high, size);
11542}
11543
88ab90e8
AA
11544char *
11545elfcore_write_s390_gs_cb (bfd *abfd,
11546 char *buf,
11547 int *bufsiz,
11548 const void *s390_gs_cb,
11549 int size)
11550{
11551 char *note_name = "LINUX";
11552 return elfcore_write_note (abfd, buf, bufsiz,
11553 note_name, NT_S390_GS_CB,
11554 s390_gs_cb, size);
11555}
11556
11557char *
11558elfcore_write_s390_gs_bc (bfd *abfd,
11559 char *buf,
11560 int *bufsiz,
11561 const void *s390_gs_bc,
11562 int size)
11563{
11564 char *note_name = "LINUX";
11565 return elfcore_write_note (abfd, buf, bufsiz,
11566 note_name, NT_S390_GS_BC,
11567 s390_gs_bc, size);
11568}
11569
faa9a424
UW
11570char *
11571elfcore_write_arm_vfp (bfd *abfd,
11572 char *buf,
11573 int *bufsiz,
11574 const void *arm_vfp,
11575 int size)
11576{
11577 char *note_name = "LINUX";
11578 return elfcore_write_note (abfd, buf, bufsiz,
11579 note_name, NT_ARM_VFP, arm_vfp, size);
11580}
11581
652451f8
YZ
11582char *
11583elfcore_write_aarch_tls (bfd *abfd,
11584 char *buf,
11585 int *bufsiz,
11586 const void *aarch_tls,
11587 int size)
11588{
11589 char *note_name = "LINUX";
11590 return elfcore_write_note (abfd, buf, bufsiz,
11591 note_name, NT_ARM_TLS, aarch_tls, size);
11592}
11593
11594char *
11595elfcore_write_aarch_hw_break (bfd *abfd,
11596 char *buf,
11597 int *bufsiz,
11598 const void *aarch_hw_break,
11599 int size)
11600{
11601 char *note_name = "LINUX";
11602 return elfcore_write_note (abfd, buf, bufsiz,
11603 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
11604}
11605
11606char *
11607elfcore_write_aarch_hw_watch (bfd *abfd,
11608 char *buf,
11609 int *bufsiz,
11610 const void *aarch_hw_watch,
11611 int size)
11612{
11613 char *note_name = "LINUX";
11614 return elfcore_write_note (abfd, buf, bufsiz,
11615 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
11616}
11617
ad1cc4e4
AH
11618char *
11619elfcore_write_aarch_sve (bfd *abfd,
11620 char *buf,
11621 int *bufsiz,
11622 const void *aarch_sve,
11623 int size)
11624{
11625 char *note_name = "LINUX";
11626 return elfcore_write_note (abfd, buf, bufsiz,
11627 note_name, NT_ARM_SVE, aarch_sve, size);
11628}
11629
e6c3b5bf
AH
11630char *
11631elfcore_write_aarch_pauth (bfd *abfd,
11632 char *buf,
11633 int *bufsiz,
11634 const void *aarch_pauth,
11635 int size)
11636{
11637 char *note_name = "LINUX";
11638 return elfcore_write_note (abfd, buf, bufsiz,
11639 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
11640}
11641
bb864ac1
CES
11642char *
11643elfcore_write_register_note (bfd *abfd,
11644 char *buf,
11645 int *bufsiz,
11646 const char *section,
11647 const void *data,
11648 int size)
11649{
11650 if (strcmp (section, ".reg2") == 0)
11651 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
11652 if (strcmp (section, ".reg-xfp") == 0)
11653 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
4339cae0
L
11654 if (strcmp (section, ".reg-xstate") == 0)
11655 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
bb864ac1
CES
11656 if (strcmp (section, ".reg-ppc-vmx") == 0)
11657 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
89eeb0bc
LM
11658 if (strcmp (section, ".reg-ppc-vsx") == 0)
11659 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
cb2366c1
EBM
11660 if (strcmp (section, ".reg-ppc-tar") == 0)
11661 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
11662 if (strcmp (section, ".reg-ppc-ppr") == 0)
11663 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
11664 if (strcmp (section, ".reg-ppc-dscr") == 0)
11665 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
11666 if (strcmp (section, ".reg-ppc-ebb") == 0)
11667 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
11668 if (strcmp (section, ".reg-ppc-pmu") == 0)
11669 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
11670 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
11671 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
11672 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
11673 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
11674 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
11675 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
11676 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
11677 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
11678 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
11679 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
11680 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
11681 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
11682 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
11683 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
11684 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
11685 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
0675e188
UW
11686 if (strcmp (section, ".reg-s390-high-gprs") == 0)
11687 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
d7eeb400
MS
11688 if (strcmp (section, ".reg-s390-timer") == 0)
11689 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
11690 if (strcmp (section, ".reg-s390-todcmp") == 0)
11691 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
11692 if (strcmp (section, ".reg-s390-todpreg") == 0)
11693 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
11694 if (strcmp (section, ".reg-s390-ctrs") == 0)
11695 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
11696 if (strcmp (section, ".reg-s390-prefix") == 0)
11697 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
355b81d9
UW
11698 if (strcmp (section, ".reg-s390-last-break") == 0)
11699 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
11700 if (strcmp (section, ".reg-s390-system-call") == 0)
11701 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
abb3f6cc
NC
11702 if (strcmp (section, ".reg-s390-tdb") == 0)
11703 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
4ef9f41a
AA
11704 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
11705 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
11706 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
11707 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
88ab90e8
AA
11708 if (strcmp (section, ".reg-s390-gs-cb") == 0)
11709 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
11710 if (strcmp (section, ".reg-s390-gs-bc") == 0)
11711 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
faa9a424
UW
11712 if (strcmp (section, ".reg-arm-vfp") == 0)
11713 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
652451f8
YZ
11714 if (strcmp (section, ".reg-aarch-tls") == 0)
11715 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
11716 if (strcmp (section, ".reg-aarch-hw-break") == 0)
11717 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
11718 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
11719 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
ad1cc4e4
AH
11720 if (strcmp (section, ".reg-aarch-sve") == 0)
11721 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
e6c3b5bf
AH
11722 if (strcmp (section, ".reg-aarch-pauth") == 0)
11723 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
bb864ac1
CES
11724 return NULL;
11725}
11726
b34976b6 11727static bfd_boolean
276da9b3
L
11728elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
11729 size_t align)
252b5132 11730{
c044fabd 11731 char *p;
252b5132 11732
276da9b3
L
11733 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
11734 gABI specifies that PT_NOTE alignment should be aligned to 4
11735 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
11736 align is less than 4, we use 4 byte alignment. */
11737 if (align < 4)
11738 align = 4;
ef135d43
NC
11739 if (align != 4 && align != 8)
11740 return FALSE;
276da9b3 11741
252b5132
RH
11742 p = buf;
11743 while (p < buf + size)
11744 {
c044fabd 11745 Elf_External_Note *xnp = (Elf_External_Note *) p;
252b5132
RH
11746 Elf_Internal_Note in;
11747
baea7ef1
AM
11748 if (offsetof (Elf_External_Note, name) > buf - p + size)
11749 return FALSE;
11750
dc810e39 11751 in.type = H_GET_32 (abfd, xnp->type);
252b5132 11752
dc810e39 11753 in.namesz = H_GET_32 (abfd, xnp->namesz);
252b5132 11754 in.namedata = xnp->name;
baea7ef1
AM
11755 if (in.namesz > buf - in.namedata + size)
11756 return FALSE;
252b5132 11757
dc810e39 11758 in.descsz = H_GET_32 (abfd, xnp->descsz);
276da9b3 11759 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
252b5132 11760 in.descpos = offset + (in.descdata - buf);
baea7ef1
AM
11761 if (in.descsz != 0
11762 && (in.descdata >= buf + size
11763 || in.descsz > buf - in.descdata + size))
11764 return FALSE;
252b5132 11765
718175fa 11766 switch (bfd_get_format (abfd))
07d6d2b8 11767 {
718175fa
JK
11768 default:
11769 return TRUE;
11770
11771 case bfd_core:
f64e188b 11772 {
8acbedd6 11773#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
f64e188b 11774 struct
718175fa 11775 {
f64e188b 11776 const char * string;
8acbedd6 11777 size_t len;
f64e188b 11778 bfd_boolean (* func)(bfd *, Elf_Internal_Note *);
718175fa 11779 }
f64e188b 11780 grokers[] =
b15fa79e 11781 {
8acbedd6 11782 GROKER_ELEMENT ("", elfcore_grok_note),
aa1ed4a9 11783 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
8acbedd6
KS
11784 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
11785 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
11786 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
11787 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note)
f64e188b 11788 };
8acbedd6 11789#undef GROKER_ELEMENT
f64e188b
NC
11790 int i;
11791
11792 for (i = ARRAY_SIZE (grokers); i--;)
8acbedd6
KS
11793 {
11794 if (in.namesz >= grokers[i].len
11795 && strncmp (in.namedata, grokers[i].string,
11796 grokers[i].len) == 0)
11797 {
11798 if (! grokers[i].func (abfd, & in))
11799 return FALSE;
11800 break;
11801 }
11802 }
f64e188b
NC
11803 break;
11804 }
718175fa
JK
11805
11806 case bfd_object:
11807 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
11808 {
11809 if (! elfobj_grok_gnu_note (abfd, &in))
11810 return FALSE;
11811 }
e21e5835
NC
11812 else if (in.namesz == sizeof "stapsdt"
11813 && strcmp (in.namedata, "stapsdt") == 0)
11814 {
11815 if (! elfobj_grok_stapsdt_note (abfd, &in))
11816 return FALSE;
11817 }
718175fa 11818 break;
08a40648 11819 }
252b5132 11820
276da9b3 11821 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
252b5132
RH
11822 }
11823
718175fa
JK
11824 return TRUE;
11825}
11826
11827static bfd_boolean
276da9b3
L
11828elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
11829 size_t align)
718175fa
JK
11830{
11831 char *buf;
11832
957e1fc1 11833 if (size == 0 || (size + 1) == 0)
718175fa
JK
11834 return TRUE;
11835
11836 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
11837 return FALSE;
11838
f64e188b 11839 buf = (char *) bfd_malloc (size + 1);
718175fa
JK
11840 if (buf == NULL)
11841 return FALSE;
11842
f64e188b
NC
11843 /* PR 17512: file: ec08f814
11844 0-termintate the buffer so that string searches will not overflow. */
11845 buf[size] = 0;
11846
718175fa 11847 if (bfd_bread (buf, size, abfd) != size
276da9b3 11848 || !elf_parse_notes (abfd, buf, size, offset, align))
718175fa
JK
11849 {
11850 free (buf);
11851 return FALSE;
11852 }
11853
252b5132 11854 free (buf);
b34976b6 11855 return TRUE;
252b5132 11856}
98d8431c
JB
11857\f
11858/* Providing external access to the ELF program header table. */
11859
11860/* Return an upper bound on the number of bytes required to store a
11861 copy of ABFD's program header table entries. Return -1 if an error
11862 occurs; bfd_get_error will return an appropriate code. */
c044fabd 11863
98d8431c 11864long
217aa764 11865bfd_get_elf_phdr_upper_bound (bfd *abfd)
98d8431c
JB
11866{
11867 if (abfd->xvec->flavour != bfd_target_elf_flavour)
11868 {
11869 bfd_set_error (bfd_error_wrong_format);
11870 return -1;
11871 }
11872
936e320b 11873 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
98d8431c
JB
11874}
11875
98d8431c
JB
11876/* Copy ABFD's program header table entries to *PHDRS. The entries
11877 will be stored as an array of Elf_Internal_Phdr structures, as
11878 defined in include/elf/internal.h. To find out how large the
11879 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
11880
11881 Return the number of program header table entries read, or -1 if an
11882 error occurs; bfd_get_error will return an appropriate code. */
c044fabd 11883
98d8431c 11884int
217aa764 11885bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
98d8431c
JB
11886{
11887 int num_phdrs;
11888
11889 if (abfd->xvec->flavour != bfd_target_elf_flavour)
11890 {
11891 bfd_set_error (bfd_error_wrong_format);
11892 return -1;
11893 }
11894
11895 num_phdrs = elf_elfheader (abfd)->e_phnum;
01bcaf63
TT
11896 if (num_phdrs != 0)
11897 memcpy (phdrs, elf_tdata (abfd)->phdr,
11898 num_phdrs * sizeof (Elf_Internal_Phdr));
98d8431c
JB
11899
11900 return num_phdrs;
11901}
ae4221d7 11902
db6751f2 11903enum elf_reloc_type_class
7e612e98
AM
11904_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
11905 const asection *rel_sec ATTRIBUTE_UNUSED,
11906 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
db6751f2
JJ
11907{
11908 return reloc_class_normal;
11909}
f8df10f4 11910
47d9a591 11911/* For RELA architectures, return the relocation value for a
f8df10f4
JJ
11912 relocation against a local symbol. */
11913
11914bfd_vma
217aa764
AM
11915_bfd_elf_rela_local_sym (bfd *abfd,
11916 Elf_Internal_Sym *sym,
8517fae7 11917 asection **psec,
217aa764 11918 Elf_Internal_Rela *rel)
f8df10f4 11919{
8517fae7 11920 asection *sec = *psec;
f8df10f4
JJ
11921 bfd_vma relocation;
11922
6835821b
AM
11923 relocation = (sec->output_section->vma
11924 + sec->output_offset
11925 + sym->st_value);
f8df10f4 11926 if ((sec->flags & SEC_MERGE)
c629eae0 11927 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
dbaa2011 11928 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
f8df10f4 11929 {
f8df10f4 11930 rel->r_addend =
8517fae7 11931 _bfd_merged_section_offset (abfd, psec,
65765700 11932 elf_section_data (sec)->sec_info,
753731ee
AM
11933 sym->st_value + rel->r_addend);
11934 if (sec != *psec)
11935 {
11936 /* If we have changed the section, and our original section is
11937 marked with SEC_EXCLUDE, it means that the original
11938 SEC_MERGE section has been completely subsumed in some
11939 other SEC_MERGE section. In this case, we need to leave
11940 some info around for --emit-relocs. */
11941 if ((sec->flags & SEC_EXCLUDE) != 0)
11942 sec->kept_section = *psec;
11943 sec = *psec;
11944 }
8517fae7
AM
11945 rel->r_addend -= relocation;
11946 rel->r_addend += sec->output_section->vma + sec->output_offset;
f8df10f4
JJ
11947 }
11948 return relocation;
11949}
c629eae0
JJ
11950
11951bfd_vma
217aa764
AM
11952_bfd_elf_rel_local_sym (bfd *abfd,
11953 Elf_Internal_Sym *sym,
11954 asection **psec,
11955 bfd_vma addend)
47d9a591 11956{
c629eae0
JJ
11957 asection *sec = *psec;
11958
6835821b 11959 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
c629eae0
JJ
11960 return sym->st_value + addend;
11961
11962 return _bfd_merged_section_offset (abfd, psec,
65765700 11963 elf_section_data (sec)->sec_info,
753731ee 11964 sym->st_value + addend);
c629eae0
JJ
11965}
11966
37b01f6a
DG
11967/* Adjust an address within a section. Given OFFSET within SEC, return
11968 the new offset within the section, based upon changes made to the
11969 section. Returns -1 if the offset is now invalid.
11970 The offset (in abnd out) is in target sized bytes, however big a
11971 byte may be. */
11972
c629eae0 11973bfd_vma
217aa764 11974_bfd_elf_section_offset (bfd *abfd,
92e4ec35 11975 struct bfd_link_info *info,
217aa764
AM
11976 asection *sec,
11977 bfd_vma offset)
c629eae0 11978{
68bfbfcc 11979 switch (sec->sec_info_type)
65765700 11980 {
dbaa2011 11981 case SEC_INFO_TYPE_STABS:
eea6121a
AM
11982 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
11983 offset);
dbaa2011 11984 case SEC_INFO_TYPE_EH_FRAME:
92e4ec35 11985 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
37b01f6a 11986
65765700 11987 default:
310fd250
L
11988 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
11989 {
37b01f6a 11990 /* Reverse the offset. */
310fd250
L
11991 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11992 bfd_size_type address_size = bed->s->arch_size / 8;
37b01f6a
DG
11993
11994 /* address_size and sec->size are in octets. Convert
11995 to bytes before subtracting the original offset. */
11996 offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset;
310fd250 11997 }
65765700
JJ
11998 return offset;
11999 }
c629eae0 12000}
3333a7c3
RM
12001\f
12002/* Create a new BFD as if by bfd_openr. Rather than opening a file,
12003 reconstruct an ELF file by reading the segments out of remote memory
12004 based on the ELF file header at EHDR_VMA and the ELF program headers it
12005 points to. If not null, *LOADBASEP is filled in with the difference
12006 between the VMAs from which the segments were read, and the VMAs the
12007 file headers (and hence BFD's idea of each section's VMA) put them at.
12008
12009 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12010 remote memory at target address VMA into the local buffer at MYADDR; it
12011 should return zero on success or an `errno' code on failure. TEMPL must
12012 be a BFD for an ELF target with the word size and byte order found in
12013 the remote memory. */
12014
12015bfd *
217aa764
AM
12016bfd_elf_bfd_from_remote_memory
12017 (bfd *templ,
12018 bfd_vma ehdr_vma,
f0a5d95a 12019 bfd_size_type size,
217aa764 12020 bfd_vma *loadbasep,
fe78531d 12021 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
3333a7c3
RM
12022{
12023 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
5979d6b6 12024 (templ, ehdr_vma, size, loadbasep, target_read_memory);
3333a7c3 12025}
4c45e5c9
JJ
12026\f
12027long
c9727e01
AM
12028_bfd_elf_get_synthetic_symtab (bfd *abfd,
12029 long symcount ATTRIBUTE_UNUSED,
12030 asymbol **syms ATTRIBUTE_UNUSED,
8615f3f2 12031 long dynsymcount,
c9727e01
AM
12032 asymbol **dynsyms,
12033 asymbol **ret)
4c45e5c9
JJ
12034{
12035 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12036 asection *relplt;
12037 asymbol *s;
12038 const char *relplt_name;
12039 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
12040 arelent *p;
12041 long count, i, n;
12042 size_t size;
12043 Elf_Internal_Shdr *hdr;
12044 char *names;
12045 asection *plt;
12046
8615f3f2
AM
12047 *ret = NULL;
12048
90e3cdf2
JJ
12049 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
12050 return 0;
12051
8615f3f2
AM
12052 if (dynsymcount <= 0)
12053 return 0;
12054
4c45e5c9
JJ
12055 if (!bed->plt_sym_val)
12056 return 0;
12057
12058 relplt_name = bed->relplt_name;
12059 if (relplt_name == NULL)
d35fd659 12060 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
4c45e5c9
JJ
12061 relplt = bfd_get_section_by_name (abfd, relplt_name);
12062 if (relplt == NULL)
12063 return 0;
12064
12065 hdr = &elf_section_data (relplt)->this_hdr;
12066 if (hdr->sh_link != elf_dynsymtab (abfd)
12067 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
12068 return 0;
12069
12070 plt = bfd_get_section_by_name (abfd, ".plt");
12071 if (plt == NULL)
12072 return 0;
12073
12074 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
c9727e01 12075 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
4c45e5c9
JJ
12076 return -1;
12077
eea6121a 12078 count = relplt->size / hdr->sh_entsize;
4c45e5c9
JJ
12079 size = count * sizeof (asymbol);
12080 p = relplt->relocation;
cb53bf42 12081 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
041de40d
AM
12082 {
12083 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
12084 if (p->addend != 0)
12085 {
12086#ifdef BFD64
12087 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
12088#else
12089 size += sizeof ("+0x") - 1 + 8;
12090#endif
12091 }
12092 }
4c45e5c9 12093
a50b1753 12094 s = *ret = (asymbol *) bfd_malloc (size);
4c45e5c9
JJ
12095 if (s == NULL)
12096 return -1;
12097
12098 names = (char *) (s + count);
12099 p = relplt->relocation;
12100 n = 0;
cb53bf42 12101 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
4c45e5c9
JJ
12102 {
12103 size_t len;
12104 bfd_vma addr;
12105
12106 addr = bed->plt_sym_val (i, plt, p);
12107 if (addr == (bfd_vma) -1)
12108 continue;
12109
12110 *s = **p->sym_ptr_ptr;
65a7a66f
AM
12111 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12112 we are defining a symbol, ensure one of them is set. */
12113 if ((s->flags & BSF_LOCAL) == 0)
12114 s->flags |= BSF_GLOBAL;
6ba2a415 12115 s->flags |= BSF_SYNTHETIC;
4c45e5c9
JJ
12116 s->section = plt;
12117 s->value = addr - plt->vma;
12118 s->name = names;
8f39ba8e 12119 s->udata.p = NULL;
4c45e5c9
JJ
12120 len = strlen ((*p->sym_ptr_ptr)->name);
12121 memcpy (names, (*p->sym_ptr_ptr)->name, len);
12122 names += len;
041de40d
AM
12123 if (p->addend != 0)
12124 {
1d770845 12125 char buf[30], *a;
d324f6d6 12126
041de40d
AM
12127 memcpy (names, "+0x", sizeof ("+0x") - 1);
12128 names += sizeof ("+0x") - 1;
1d770845
L
12129 bfd_sprintf_vma (abfd, buf, p->addend);
12130 for (a = buf; *a == '0'; ++a)
12131 ;
12132 len = strlen (a);
12133 memcpy (names, a, len);
12134 names += len;
041de40d 12135 }
4c45e5c9
JJ
12136 memcpy (names, "@plt", sizeof ("@plt"));
12137 names += sizeof ("@plt");
8f39ba8e 12138 ++s, ++n;
4c45e5c9
JJ
12139 }
12140
12141 return n;
12142}
3d7f7666 12143
821e6ff6
AM
12144/* It is only used by x86-64 so far.
12145 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
7eacd66b
AM
12146 but current usage would allow all of _bfd_std_section to be zero. */
12147static const asymbol lcomm_sym
12148 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
3b22753a 12149asection _bfd_elf_large_com_section
7eacd66b 12150 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
821e6ff6 12151 "LARGE_COMMON", 0, SEC_IS_COMMON);
ecca9871 12152
d1036acb 12153void
06f44071
AM
12154_bfd_elf_post_process_headers (bfd *abfd ATTRIBUTE_UNUSED,
12155 struct bfd_link_info *info ATTRIBUTE_UNUSED)
d1036acb 12156{
06f44071
AM
12157}
12158
cc364be6
AM
12159bfd_boolean
12160_bfd_elf_final_write_processing (bfd *abfd)
06f44071
AM
12161{
12162 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
d1036acb
L
12163
12164 i_ehdrp = elf_elfheader (abfd);
12165
06f44071
AM
12166 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12167 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
d8045f23 12168
df3a023b
AM
12169 /* Set the osabi field to ELFOSABI_GNU if the binary contains
12170 SHF_GNU_MBIND sections or symbols of STT_GNU_IFUNC type or
12171 STB_GNU_UNIQUE binding. */
cc364be6
AM
12172 if (elf_tdata (abfd)->has_gnu_osabi != 0)
12173 {
12174 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12175 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
12176 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
12177 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
12178 {
12179 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
12180 _bfd_error_handler (_("GNU_MBIND section is unsupported"));
12181 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
12182 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is unsupported"));
12183 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
12184 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is unsupported"));
12185 bfd_set_error (bfd_error_bad_value);
12186 return FALSE;
12187 }
12188 }
12189 return TRUE;
d1036acb 12190}
fcb93ecf
PB
12191
12192
12193/* Return TRUE for ELF symbol types that represent functions.
12194 This is the default version of this function, which is sufficient for
d8045f23 12195 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
fcb93ecf
PB
12196
12197bfd_boolean
12198_bfd_elf_is_function_type (unsigned int type)
12199{
d8045f23
NC
12200 return (type == STT_FUNC
12201 || type == STT_GNU_IFUNC);
fcb93ecf 12202}
9f296da3 12203
aef36ac1
AM
12204/* If the ELF symbol SYM might be a function in SEC, return the
12205 function size and set *CODE_OFF to the function's entry point,
12206 otherwise return zero. */
9f296da3 12207
aef36ac1
AM
12208bfd_size_type
12209_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
12210 bfd_vma *code_off)
9f296da3 12211{
aef36ac1
AM
12212 bfd_size_type size;
12213
ff9e0f5b 12214 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
aef36ac1
AM
12215 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
12216 || sym->section != sec)
12217 return 0;
ff9e0f5b 12218
ff9e0f5b 12219 *code_off = sym->value;
aef36ac1
AM
12220 size = 0;
12221 if (!(sym->flags & BSF_SYNTHETIC))
12222 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
12223 if (size == 0)
12224 size = 1;
12225 return size;
9f296da3 12226}