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