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