Complain about mbind, ifunc, and unique in final_write
[deliverable/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
cc364be6
AM
6571 if (!(*bed->elf_backend_final_write_processing) (abfd))
6572 return FALSE;
252b5132 6573
ff59fc36
RM
6574 if (!bed->s->write_shdrs_and_ehdr (abfd))
6575 return FALSE;
6576
6577 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
c0355132
AM
6578 if (t->o->build_id.after_write_object_contents != NULL)
6579 return (*t->o->build_id.after_write_object_contents) (abfd);
ff59fc36
RM
6580
6581 return TRUE;
252b5132
RH
6582}
6583
b34976b6 6584bfd_boolean
217aa764 6585_bfd_elf_write_corefile_contents (bfd *abfd)
252b5132 6586{
c044fabd 6587 /* Hopefully this can be done just like an object file. */
252b5132
RH
6588 return _bfd_elf_write_object_contents (abfd);
6589}
c044fabd
KH
6590
6591/* Given a section, search the header to find them. */
6592
cb33740c 6593unsigned int
198beae2 6594_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
252b5132 6595{
9c5bfbb7 6596 const struct elf_backend_data *bed;
91d6fa6a 6597 unsigned int sec_index;
252b5132 6598
9ad5cbcf
AM
6599 if (elf_section_data (asect) != NULL
6600 && elf_section_data (asect)->this_idx != 0)
6601 return elf_section_data (asect)->this_idx;
6602
6603 if (bfd_is_abs_section (asect))
91d6fa6a 6604 sec_index = SHN_ABS;
af746e92 6605 else if (bfd_is_com_section (asect))
91d6fa6a 6606 sec_index = SHN_COMMON;
af746e92 6607 else if (bfd_is_und_section (asect))
91d6fa6a 6608 sec_index = SHN_UNDEF;
af746e92 6609 else
91d6fa6a 6610 sec_index = SHN_BAD;
252b5132 6611
af746e92 6612 bed = get_elf_backend_data (abfd);
252b5132
RH
6613 if (bed->elf_backend_section_from_bfd_section)
6614 {
91d6fa6a 6615 int retval = sec_index;
9ad5cbcf 6616
af746e92
AM
6617 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6618 return retval;
252b5132
RH
6619 }
6620
91d6fa6a 6621 if (sec_index == SHN_BAD)
af746e92 6622 bfd_set_error (bfd_error_nonrepresentable_section);
252b5132 6623
91d6fa6a 6624 return sec_index;
252b5132
RH
6625}
6626
6627/* Given a BFD symbol, return the index in the ELF symbol table, or -1
6628 on error. */
6629
6630int
217aa764 6631_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
252b5132
RH
6632{
6633 asymbol *asym_ptr = *asym_ptr_ptr;
6634 int idx;
6635 flagword flags = asym_ptr->flags;
6636
6637 /* When gas creates relocations against local labels, it creates its
6638 own symbol for the section, but does put the symbol into the
6639 symbol chain, so udata is 0. When the linker is generating
6640 relocatable output, this section symbol may be for one of the
6641 input sections rather than the output section. */
6642 if (asym_ptr->udata.i == 0
6643 && (flags & BSF_SECTION_SYM)
6644 && asym_ptr->section)
6645 {
5372391b 6646 asection *sec;
252b5132
RH
6647 int indx;
6648
5372391b
AM
6649 sec = asym_ptr->section;
6650 if (sec->owner != abfd && sec->output_section != NULL)
6651 sec = sec->output_section;
6652 if (sec->owner == abfd
6653 && (indx = sec->index) < elf_num_section_syms (abfd)
4e89ac30 6654 && elf_section_syms (abfd)[indx] != NULL)
252b5132
RH
6655 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
6656 }
6657
6658 idx = asym_ptr->udata.i;
6659
6660 if (idx == 0)
6661 {
6662 /* This case can occur when using --strip-symbol on a symbol
08a40648 6663 which is used in a relocation entry. */
4eca0228 6664 _bfd_error_handler
695344c0 6665 /* xgettext:c-format */
871b3ab2 6666 (_("%pB: symbol `%s' required but not present"),
d003868e 6667 abfd, bfd_asymbol_name (asym_ptr));
252b5132
RH
6668 bfd_set_error (bfd_error_no_symbols);
6669 return -1;
6670 }
6671
6672#if DEBUG & 4
6673 {
6674 fprintf (stderr,
cd9af601
AM
6675 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6676 (long) asym_ptr, asym_ptr->name, idx, flags);
252b5132
RH
6677 fflush (stderr);
6678 }
6679#endif
6680
6681 return idx;
6682}
6683
84d1d650 6684/* Rewrite program header information. */
252b5132 6685
b34976b6 6686static bfd_boolean
84d1d650 6687rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
252b5132 6688{
b34976b6
AM
6689 Elf_Internal_Ehdr *iehdr;
6690 struct elf_segment_map *map;
6691 struct elf_segment_map *map_first;
6692 struct elf_segment_map **pointer_to_map;
6693 Elf_Internal_Phdr *segment;
6694 asection *section;
6695 unsigned int i;
6696 unsigned int num_segments;
6697 bfd_boolean phdr_included = FALSE;
5c44b38e 6698 bfd_boolean p_paddr_valid;
b34976b6
AM
6699 bfd_vma maxpagesize;
6700 struct elf_segment_map *phdr_adjust_seg = NULL;
6701 unsigned int phdr_adjust_num = 0;
9c5bfbb7 6702 const struct elf_backend_data *bed;
bc67d8a6 6703
caf47ea6 6704 bed = get_elf_backend_data (ibfd);
252b5132
RH
6705 iehdr = elf_elfheader (ibfd);
6706
bc67d8a6 6707 map_first = NULL;
c044fabd 6708 pointer_to_map = &map_first;
252b5132
RH
6709
6710 num_segments = elf_elfheader (ibfd)->e_phnum;
bc67d8a6
NC
6711 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
6712
6713 /* Returns the end address of the segment + 1. */
aecc8f8a
AM
6714#define SEGMENT_END(segment, start) \
6715 (start + (segment->p_memsz > segment->p_filesz \
6716 ? segment->p_memsz : segment->p_filesz))
bc67d8a6 6717
eecdbe52
JJ
6718#define SECTION_SIZE(section, segment) \
6719 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6720 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
eea6121a 6721 ? section->size : 0)
eecdbe52 6722
b34976b6 6723 /* Returns TRUE if the given section is contained within
bc67d8a6 6724 the given segment. VMA addresses are compared. */
aecc8f8a
AM
6725#define IS_CONTAINED_BY_VMA(section, segment) \
6726 (section->vma >= segment->p_vaddr \
eecdbe52 6727 && (section->vma + SECTION_SIZE (section, segment) \
aecc8f8a 6728 <= (SEGMENT_END (segment, segment->p_vaddr))))
c044fabd 6729
b34976b6 6730 /* Returns TRUE if the given section is contained within
bc67d8a6 6731 the given segment. LMA addresses are compared. */
aecc8f8a
AM
6732#define IS_CONTAINED_BY_LMA(section, segment, base) \
6733 (section->lma >= base \
beab4532 6734 && (section->lma + SECTION_SIZE (section, segment) >= section->lma) \
eecdbe52 6735 && (section->lma + SECTION_SIZE (section, segment) \
aecc8f8a 6736 <= SEGMENT_END (segment, base)))
252b5132 6737
0efc80c8
L
6738 /* Handle PT_NOTE segment. */
6739#define IS_NOTE(p, s) \
aecc8f8a 6740 (p->p_type == PT_NOTE \
0efc80c8 6741 && elf_section_type (s) == SHT_NOTE \
aecc8f8a 6742 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 6743 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 6744 <= p->p_offset + p->p_filesz))
252b5132 6745
0efc80c8
L
6746 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6747 etc. */
6748#define IS_COREFILE_NOTE(p, s) \
6749 (IS_NOTE (p, s) \
6750 && bfd_get_format (ibfd) == bfd_core \
6751 && s->vma == 0 \
6752 && s->lma == 0)
6753
252b5132
RH
6754 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6755 linker, which generates a PT_INTERP section with p_vaddr and
6756 p_memsz set to 0. */
aecc8f8a
AM
6757#define IS_SOLARIS_PT_INTERP(p, s) \
6758 (p->p_vaddr == 0 \
6759 && p->p_paddr == 0 \
6760 && p->p_memsz == 0 \
6761 && p->p_filesz > 0 \
6762 && (s->flags & SEC_HAS_CONTENTS) != 0 \
eea6121a 6763 && s->size > 0 \
aecc8f8a 6764 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 6765 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 6766 <= p->p_offset + p->p_filesz))
5c440b1e 6767
bc67d8a6
NC
6768 /* Decide if the given section should be included in the given segment.
6769 A section will be included if:
f5ffc919 6770 1. It is within the address space of the segment -- we use the LMA
08a40648 6771 if that is set for the segment and the VMA otherwise,
0efc80c8 6772 2. It is an allocated section or a NOTE section in a PT_NOTE
d324f6d6 6773 segment.
bc67d8a6 6774 3. There is an output section associated with it,
eecdbe52 6775 4. The section has not already been allocated to a previous segment.
2b05f1b7 6776 5. PT_GNU_STACK segments do not include any sections.
03394ac9 6777 6. PT_TLS segment includes only SHF_TLS sections.
6f79b219
JJ
6778 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6779 8. PT_DYNAMIC should not contain empty sections at the beginning
08a40648 6780 (with the possible exception of .dynamic). */
9f17e2a6 6781#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
2b05f1b7
L
6782 ((((segment->p_paddr \
6783 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6784 : IS_CONTAINED_BY_VMA (section, segment)) \
6785 && (section->flags & SEC_ALLOC) != 0) \
0efc80c8 6786 || IS_NOTE (segment, section)) \
2b05f1b7
L
6787 && segment->p_type != PT_GNU_STACK \
6788 && (segment->p_type != PT_TLS \
6789 || (section->flags & SEC_THREAD_LOCAL)) \
6790 && (segment->p_type == PT_LOAD \
6791 || segment->p_type == PT_TLS \
6792 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6793 && (segment->p_type != PT_DYNAMIC \
6794 || SECTION_SIZE (section, segment) > 0 \
6795 || (segment->p_paddr \
6796 ? segment->p_paddr != section->lma \
6797 : segment->p_vaddr != section->vma) \
6798 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
6799 == 0)) \
9933dc52 6800 && (segment->p_type != PT_LOAD || !section->segment_mark))
bc67d8a6 6801
9f17e2a6
L
6802/* If the output section of a section in the input segment is NULL,
6803 it is removed from the corresponding output segment. */
6804#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6805 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6806 && section->output_section != NULL)
6807
b34976b6 6808 /* Returns TRUE iff seg1 starts after the end of seg2. */
b5f852ea
NC
6809#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6810 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6811
6812 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6813 their VMA address ranges and their LMA address ranges overlap.
6814 It is possible to have overlapping VMA ranges without overlapping LMA
6815 ranges. RedBoot images for example can have both .data and .bss mapped
6816 to the same VMA range, but with the .data section mapped to a different
6817 LMA. */
aecc8f8a 6818#define SEGMENT_OVERLAPS(seg1, seg2) \
b5f852ea 6819 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
08a40648 6820 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
b5f852ea 6821 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
08a40648 6822 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
bc67d8a6
NC
6823
6824 /* Initialise the segment mark field. */
6825 for (section = ibfd->sections; section != NULL; section = section->next)
b34976b6 6826 section->segment_mark = FALSE;
bc67d8a6 6827
5c44b38e
AM
6828 /* The Solaris linker creates program headers in which all the
6829 p_paddr fields are zero. When we try to objcopy or strip such a
6830 file, we get confused. Check for this case, and if we find it
6831 don't set the p_paddr_valid fields. */
6832 p_paddr_valid = FALSE;
6833 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6834 i < num_segments;
6835 i++, segment++)
6836 if (segment->p_paddr != 0)
6837 {
6838 p_paddr_valid = TRUE;
6839 break;
6840 }
6841
252b5132 6842 /* Scan through the segments specified in the program header
bc67d8a6 6843 of the input BFD. For this first scan we look for overlaps
9ad5cbcf 6844 in the loadable segments. These can be created by weird
aecc8f8a 6845 parameters to objcopy. Also, fix some solaris weirdness. */
bc67d8a6
NC
6846 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6847 i < num_segments;
c044fabd 6848 i++, segment++)
252b5132 6849 {
252b5132 6850 unsigned int j;
c044fabd 6851 Elf_Internal_Phdr *segment2;
252b5132 6852
aecc8f8a
AM
6853 if (segment->p_type == PT_INTERP)
6854 for (section = ibfd->sections; section; section = section->next)
6855 if (IS_SOLARIS_PT_INTERP (segment, section))
6856 {
6857 /* Mininal change so that the normal section to segment
4cc11e76 6858 assignment code will work. */
aecc8f8a
AM
6859 segment->p_vaddr = section->vma;
6860 break;
6861 }
6862
bc67d8a6 6863 if (segment->p_type != PT_LOAD)
b10a8ae0
L
6864 {
6865 /* Remove PT_GNU_RELRO segment. */
6866 if (segment->p_type == PT_GNU_RELRO)
6867 segment->p_type = PT_NULL;
6868 continue;
6869 }
c044fabd 6870
bc67d8a6 6871 /* Determine if this segment overlaps any previous segments. */
0067a569 6872 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
bc67d8a6
NC
6873 {
6874 bfd_signed_vma extra_length;
c044fabd 6875
bc67d8a6 6876 if (segment2->p_type != PT_LOAD
0067a569 6877 || !SEGMENT_OVERLAPS (segment, segment2))
bc67d8a6 6878 continue;
c044fabd 6879
bc67d8a6
NC
6880 /* Merge the two segments together. */
6881 if (segment2->p_vaddr < segment->p_vaddr)
6882 {
c044fabd 6883 /* Extend SEGMENT2 to include SEGMENT and then delete
08a40648 6884 SEGMENT. */
0067a569
AM
6885 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
6886 - SEGMENT_END (segment2, segment2->p_vaddr));
c044fabd 6887
bc67d8a6
NC
6888 if (extra_length > 0)
6889 {
0067a569 6890 segment2->p_memsz += extra_length;
bc67d8a6
NC
6891 segment2->p_filesz += extra_length;
6892 }
c044fabd 6893
bc67d8a6 6894 segment->p_type = PT_NULL;
c044fabd 6895
bc67d8a6
NC
6896 /* Since we have deleted P we must restart the outer loop. */
6897 i = 0;
6898 segment = elf_tdata (ibfd)->phdr;
6899 break;
6900 }
6901 else
6902 {
c044fabd 6903 /* Extend SEGMENT to include SEGMENT2 and then delete
08a40648 6904 SEGMENT2. */
0067a569
AM
6905 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
6906 - SEGMENT_END (segment, segment->p_vaddr));
c044fabd 6907
bc67d8a6
NC
6908 if (extra_length > 0)
6909 {
0067a569 6910 segment->p_memsz += extra_length;
bc67d8a6
NC
6911 segment->p_filesz += extra_length;
6912 }
c044fabd 6913
bc67d8a6
NC
6914 segment2->p_type = PT_NULL;
6915 }
6916 }
6917 }
c044fabd 6918
bc67d8a6
NC
6919 /* The second scan attempts to assign sections to segments. */
6920 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6921 i < num_segments;
0067a569 6922 i++, segment++)
bc67d8a6 6923 {
0067a569
AM
6924 unsigned int section_count;
6925 asection **sections;
6926 asection *output_section;
6927 unsigned int isec;
9933dc52
AM
6928 asection *matching_lma;
6929 asection *suggested_lma;
0067a569 6930 unsigned int j;
dc810e39 6931 bfd_size_type amt;
0067a569 6932 asection *first_section;
bc67d8a6
NC
6933
6934 if (segment->p_type == PT_NULL)
6935 continue;
c044fabd 6936
9f17e2a6 6937 first_section = NULL;
bc67d8a6 6938 /* Compute how many sections might be placed into this segment. */
b5f852ea
NC
6939 for (section = ibfd->sections, section_count = 0;
6940 section != NULL;
6941 section = section->next)
9f17e2a6
L
6942 {
6943 /* Find the first section in the input segment, which may be
6944 removed from the corresponding output segment. */
6945 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
6946 {
6947 if (first_section == NULL)
6948 first_section = section;
6949 if (section->output_section != NULL)
6950 ++section_count;
6951 }
6952 }
811072d8 6953
b5f852ea
NC
6954 /* Allocate a segment map big enough to contain
6955 all of the sections we have selected. */
00bee008
AM
6956 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
6957 amt += (bfd_size_type) section_count * sizeof (asection *);
a50b1753 6958 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 6959 if (map == NULL)
b34976b6 6960 return FALSE;
252b5132
RH
6961
6962 /* Initialise the fields of the segment map. Default to
6963 using the physical address of the segment in the input BFD. */
0067a569
AM
6964 map->next = NULL;
6965 map->p_type = segment->p_type;
6966 map->p_flags = segment->p_flags;
bc67d8a6 6967 map->p_flags_valid = 1;
55d55ac7 6968
9f17e2a6
L
6969 /* If the first section in the input segment is removed, there is
6970 no need to preserve segment physical address in the corresponding
6971 output segment. */
945c025a 6972 if (!first_section || first_section->output_section != NULL)
9f17e2a6
L
6973 {
6974 map->p_paddr = segment->p_paddr;
5c44b38e 6975 map->p_paddr_valid = p_paddr_valid;
9f17e2a6 6976 }
252b5132
RH
6977
6978 /* Determine if this segment contains the ELF file header
6979 and if it contains the program headers themselves. */
bc67d8a6
NC
6980 map->includes_filehdr = (segment->p_offset == 0
6981 && segment->p_filesz >= iehdr->e_ehsize);
bc67d8a6 6982 map->includes_phdrs = 0;
252b5132 6983
0067a569 6984 if (!phdr_included || segment->p_type != PT_LOAD)
252b5132 6985 {
bc67d8a6
NC
6986 map->includes_phdrs =
6987 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
6988 && (segment->p_offset + segment->p_filesz
252b5132
RH
6989 >= ((bfd_vma) iehdr->e_phoff
6990 + iehdr->e_phnum * iehdr->e_phentsize)));
c044fabd 6991
bc67d8a6 6992 if (segment->p_type == PT_LOAD && map->includes_phdrs)
b34976b6 6993 phdr_included = TRUE;
252b5132
RH
6994 }
6995
bc67d8a6 6996 if (section_count == 0)
252b5132
RH
6997 {
6998 /* Special segments, such as the PT_PHDR segment, may contain
6999 no sections, but ordinary, loadable segments should contain
1ed89aa9 7000 something. They are allowed by the ELF spec however, so only
07d6d2b8 7001 a warning is produced.
f98450c6
NC
7002 There is however the valid use case of embedded systems which
7003 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7004 flash memory with zeros. No warning is shown for that case. */
7005 if (segment->p_type == PT_LOAD
7006 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7007 /* xgettext:c-format */
9793eb77
AM
7008 _bfd_error_handler
7009 (_("%pB: warning: empty loadable segment detected"
7010 " at vaddr=%#" PRIx64 ", is this intentional?"),
7011 ibfd, (uint64_t) segment->p_vaddr);
252b5132 7012
5d695627 7013 map->p_vaddr_offset = segment->p_vaddr;
bc67d8a6 7014 map->count = 0;
c044fabd
KH
7015 *pointer_to_map = map;
7016 pointer_to_map = &map->next;
252b5132
RH
7017
7018 continue;
7019 }
7020
7021 /* Now scan the sections in the input BFD again and attempt
7022 to add their corresponding output sections to the segment map.
7023 The problem here is how to handle an output section which has
7024 been moved (ie had its LMA changed). There are four possibilities:
7025
7026 1. None of the sections have been moved.
7027 In this case we can continue to use the segment LMA from the
7028 input BFD.
7029
7030 2. All of the sections have been moved by the same amount.
7031 In this case we can change the segment's LMA to match the LMA
7032 of the first section.
7033
7034 3. Some of the sections have been moved, others have not.
7035 In this case those sections which have not been moved can be
7036 placed in the current segment which will have to have its size,
7037 and possibly its LMA changed, and a new segment or segments will
7038 have to be created to contain the other sections.
7039
b5f852ea 7040 4. The sections have been moved, but not by the same amount.
252b5132
RH
7041 In this case we can change the segment's LMA to match the LMA
7042 of the first section and we will have to create a new segment
7043 or segments to contain the other sections.
7044
7045 In order to save time, we allocate an array to hold the section
7046 pointers that we are interested in. As these sections get assigned
7047 to a segment, they are removed from this array. */
7048
a50b1753 7049 sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
252b5132 7050 if (sections == NULL)
b34976b6 7051 return FALSE;
252b5132
RH
7052
7053 /* Step One: Scan for segment vs section LMA conflicts.
7054 Also add the sections to the section array allocated above.
7055 Also add the sections to the current segment. In the common
7056 case, where the sections have not been moved, this means that
7057 we have completely filled the segment, and there is nothing
7058 more to do. */
252b5132 7059 isec = 0;
9933dc52
AM
7060 matching_lma = NULL;
7061 suggested_lma = NULL;
252b5132 7062
461c4b2e 7063 for (section = first_section, j = 0;
bc67d8a6
NC
7064 section != NULL;
7065 section = section->next)
252b5132 7066 {
caf47ea6 7067 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
c0f7859b 7068 {
bc67d8a6
NC
7069 output_section = section->output_section;
7070
0067a569 7071 sections[j++] = section;
252b5132
RH
7072
7073 /* The Solaris native linker always sets p_paddr to 0.
7074 We try to catch that case here, and set it to the
5e8d7549
NC
7075 correct value. Note - some backends require that
7076 p_paddr be left as zero. */
5c44b38e 7077 if (!p_paddr_valid
4455705d 7078 && segment->p_vaddr != 0
0067a569 7079 && !bed->want_p_paddr_set_to_zero
252b5132 7080 && isec == 0
bc67d8a6 7081 && output_section->lma != 0
9933dc52
AM
7082 && (align_power (segment->p_vaddr
7083 + (map->includes_filehdr
7084 ? iehdr->e_ehsize : 0)
7085 + (map->includes_phdrs
7086 ? iehdr->e_phnum * iehdr->e_phentsize
7087 : 0),
7088 output_section->alignment_power)
7089 == output_section->vma))
bc67d8a6 7090 map->p_paddr = segment->p_vaddr;
252b5132
RH
7091
7092 /* Match up the physical address of the segment with the
7093 LMA address of the output section. */
bc67d8a6 7094 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5e8d7549 7095 || IS_COREFILE_NOTE (segment, section)
0067a569
AM
7096 || (bed->want_p_paddr_set_to_zero
7097 && IS_CONTAINED_BY_VMA (output_section, segment)))
252b5132 7098 {
9933dc52
AM
7099 if (matching_lma == NULL
7100 || output_section->lma < matching_lma->lma)
7101 matching_lma = output_section;
252b5132
RH
7102
7103 /* We assume that if the section fits within the segment
bc67d8a6 7104 then it does not overlap any other section within that
252b5132 7105 segment. */
0067a569
AM
7106 map->sections[isec++] = output_section;
7107 }
9933dc52
AM
7108 else if (suggested_lma == NULL)
7109 suggested_lma = output_section;
147d51c2
L
7110
7111 if (j == section_count)
7112 break;
252b5132
RH
7113 }
7114 }
7115
bc67d8a6 7116 BFD_ASSERT (j == section_count);
252b5132
RH
7117
7118 /* Step Two: Adjust the physical address of the current segment,
7119 if necessary. */
bc67d8a6 7120 if (isec == section_count)
252b5132
RH
7121 {
7122 /* All of the sections fitted within the segment as currently
7123 specified. This is the default case. Add the segment to
7124 the list of built segments and carry on to process the next
7125 program header in the input BFD. */
bc67d8a6 7126 map->count = section_count;
c044fabd
KH
7127 *pointer_to_map = map;
7128 pointer_to_map = &map->next;
08a40648 7129
5c44b38e
AM
7130 if (p_paddr_valid
7131 && !bed->want_p_paddr_set_to_zero
9933dc52 7132 && matching_lma->lma != map->p_paddr
5c44b38e
AM
7133 && !map->includes_filehdr
7134 && !map->includes_phdrs)
3271a814
NS
7135 /* There is some padding before the first section in the
7136 segment. So, we must account for that in the output
7137 segment's vma. */
5d695627 7138 map->p_vaddr_offset = map->p_paddr - matching_lma->lma;
08a40648 7139
252b5132
RH
7140 free (sections);
7141 continue;
7142 }
252b5132
RH
7143 else
7144 {
9933dc52
AM
7145 /* Change the current segment's physical address to match
7146 the LMA of the first section that fitted, or if no
7147 section fitted, the first section. */
7148 if (matching_lma == NULL)
7149 matching_lma = suggested_lma;
7150
7151 map->p_paddr = matching_lma->lma;
72730e0c 7152
bc67d8a6
NC
7153 /* Offset the segment physical address from the lma
7154 to allow for space taken up by elf headers. */
9933dc52 7155 if (map->includes_phdrs)
010c8431 7156 {
9933dc52
AM
7157 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7158
7159 /* iehdr->e_phnum is just an estimate of the number
7160 of program headers that we will need. Make a note
7161 here of the number we used and the segment we chose
7162 to hold these headers, so that we can adjust the
7163 offset when we know the correct value. */
7164 phdr_adjust_num = iehdr->e_phnum;
7165 phdr_adjust_seg = map;
010c8431 7166 }
252b5132 7167
9933dc52 7168 if (map->includes_filehdr)
bc67d8a6 7169 {
9933dc52
AM
7170 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7171 map->p_paddr -= iehdr->e_ehsize;
7172 /* We've subtracted off the size of headers from the
7173 first section lma, but there may have been some
7174 alignment padding before that section too. Try to
7175 account for that by adjusting the segment lma down to
7176 the same alignment. */
7177 if (segment->p_align != 0 && segment->p_align < align)
7178 align = segment->p_align;
7179 map->p_paddr &= -align;
bc67d8a6 7180 }
252b5132
RH
7181 }
7182
7183 /* Step Three: Loop over the sections again, this time assigning
caf47ea6 7184 those that fit to the current segment and removing them from the
252b5132
RH
7185 sections array; but making sure not to leave large gaps. Once all
7186 possible sections have been assigned to the current segment it is
7187 added to the list of built segments and if sections still remain
7188 to be assigned, a new segment is constructed before repeating
7189 the loop. */
7190 isec = 0;
7191 do
7192 {
bc67d8a6 7193 map->count = 0;
9933dc52 7194 suggested_lma = NULL;
252b5132
RH
7195
7196 /* Fill the current segment with sections that fit. */
bc67d8a6 7197 for (j = 0; j < section_count; j++)
252b5132 7198 {
bc67d8a6 7199 section = sections[j];
252b5132 7200
bc67d8a6 7201 if (section == NULL)
252b5132
RH
7202 continue;
7203
bc67d8a6 7204 output_section = section->output_section;
252b5132 7205
bc67d8a6 7206 BFD_ASSERT (output_section != NULL);
c044fabd 7207
bc67d8a6
NC
7208 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
7209 || IS_COREFILE_NOTE (segment, section))
252b5132 7210 {
bc67d8a6 7211 if (map->count == 0)
252b5132
RH
7212 {
7213 /* If the first section in a segment does not start at
bc67d8a6
NC
7214 the beginning of the segment, then something is
7215 wrong. */
9933dc52
AM
7216 if (align_power (map->p_paddr
7217 + (map->includes_filehdr
7218 ? iehdr->e_ehsize : 0)
7219 + (map->includes_phdrs
7220 ? iehdr->e_phnum * iehdr->e_phentsize
7221 : 0),
7222 output_section->alignment_power)
7223 != output_section->lma)
252b5132
RH
7224 abort ();
7225 }
7226 else
7227 {
0067a569 7228 asection *prev_sec;
252b5132 7229
bc67d8a6 7230 prev_sec = map->sections[map->count - 1];
252b5132
RH
7231
7232 /* If the gap between the end of the previous section
bc67d8a6
NC
7233 and the start of this section is more than
7234 maxpagesize then we need to start a new segment. */
eea6121a 7235 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
079e9a2f 7236 maxpagesize)
caf47ea6 7237 < BFD_ALIGN (output_section->lma, maxpagesize))
0067a569 7238 || (prev_sec->lma + prev_sec->size
079e9a2f 7239 > output_section->lma))
252b5132 7240 {
9933dc52
AM
7241 if (suggested_lma == NULL)
7242 suggested_lma = output_section;
252b5132
RH
7243
7244 continue;
7245 }
7246 }
7247
bc67d8a6 7248 map->sections[map->count++] = output_section;
252b5132
RH
7249 ++isec;
7250 sections[j] = NULL;
9933dc52
AM
7251 if (segment->p_type == PT_LOAD)
7252 section->segment_mark = TRUE;
0067a569 7253 }
9933dc52
AM
7254 else if (suggested_lma == NULL)
7255 suggested_lma = output_section;
252b5132
RH
7256 }
7257
beab4532
NC
7258 /* PR 23932. A corrupt input file may contain sections that cannot
7259 be assigned to any segment - because for example they have a
7260 negative size - or segments that do not contain any sections. */
7261 if (map->count == 0)
7262 {
7263 bfd_set_error (bfd_error_bad_value);
7264 free (sections);
7265 return FALSE;
7266 }
252b5132
RH
7267
7268 /* Add the current segment to the list of built segments. */
c044fabd
KH
7269 *pointer_to_map = map;
7270 pointer_to_map = &map->next;
252b5132 7271
bc67d8a6 7272 if (isec < section_count)
252b5132
RH
7273 {
7274 /* We still have not allocated all of the sections to
7275 segments. Create a new segment here, initialise it
7276 and carry on looping. */
00bee008
AM
7277 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7278 amt += (bfd_size_type) section_count * sizeof (asection *);
5964fc3a 7279 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 7280 if (map == NULL)
5ed6aba4
NC
7281 {
7282 free (sections);
7283 return FALSE;
7284 }
252b5132
RH
7285
7286 /* Initialise the fields of the segment map. Set the physical
7287 physical address to the LMA of the first section that has
7288 not yet been assigned. */
0067a569
AM
7289 map->next = NULL;
7290 map->p_type = segment->p_type;
7291 map->p_flags = segment->p_flags;
7292 map->p_flags_valid = 1;
9933dc52 7293 map->p_paddr = suggested_lma->lma;
5c44b38e 7294 map->p_paddr_valid = p_paddr_valid;
bc67d8a6 7295 map->includes_filehdr = 0;
0067a569 7296 map->includes_phdrs = 0;
252b5132
RH
7297 }
7298 }
bc67d8a6 7299 while (isec < section_count);
252b5132
RH
7300
7301 free (sections);
7302 }
7303
12bd6957 7304 elf_seg_map (obfd) = map_first;
bc67d8a6
NC
7305
7306 /* If we had to estimate the number of program headers that were
9ad5cbcf 7307 going to be needed, then check our estimate now and adjust
bc67d8a6
NC
7308 the offset if necessary. */
7309 if (phdr_adjust_seg != NULL)
7310 {
7311 unsigned int count;
c044fabd 7312
bc67d8a6 7313 for (count = 0, map = map_first; map != NULL; map = map->next)
c044fabd 7314 count++;
252b5132 7315
bc67d8a6
NC
7316 if (count > phdr_adjust_num)
7317 phdr_adjust_seg->p_paddr
7318 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
9933dc52
AM
7319
7320 for (map = map_first; map != NULL; map = map->next)
7321 if (map->p_type == PT_PHDR)
7322 {
7323 bfd_vma adjust
7324 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7325 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7326 break;
7327 }
bc67d8a6 7328 }
c044fabd 7329
bc67d8a6 7330#undef SEGMENT_END
eecdbe52 7331#undef SECTION_SIZE
bc67d8a6
NC
7332#undef IS_CONTAINED_BY_VMA
7333#undef IS_CONTAINED_BY_LMA
0efc80c8 7334#undef IS_NOTE
252b5132 7335#undef IS_COREFILE_NOTE
bc67d8a6 7336#undef IS_SOLARIS_PT_INTERP
9f17e2a6 7337#undef IS_SECTION_IN_INPUT_SEGMENT
bc67d8a6
NC
7338#undef INCLUDE_SECTION_IN_SEGMENT
7339#undef SEGMENT_AFTER_SEGMENT
7340#undef SEGMENT_OVERLAPS
b34976b6 7341 return TRUE;
252b5132
RH
7342}
7343
84d1d650
L
7344/* Copy ELF program header information. */
7345
7346static bfd_boolean
7347copy_elf_program_header (bfd *ibfd, bfd *obfd)
7348{
7349 Elf_Internal_Ehdr *iehdr;
7350 struct elf_segment_map *map;
7351 struct elf_segment_map *map_first;
7352 struct elf_segment_map **pointer_to_map;
7353 Elf_Internal_Phdr *segment;
7354 unsigned int i;
7355 unsigned int num_segments;
7356 bfd_boolean phdr_included = FALSE;
88967714 7357 bfd_boolean p_paddr_valid;
84d1d650
L
7358
7359 iehdr = elf_elfheader (ibfd);
7360
7361 map_first = NULL;
7362 pointer_to_map = &map_first;
7363
88967714
AM
7364 /* If all the segment p_paddr fields are zero, don't set
7365 map->p_paddr_valid. */
7366 p_paddr_valid = FALSE;
84d1d650 7367 num_segments = elf_elfheader (ibfd)->e_phnum;
88967714
AM
7368 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7369 i < num_segments;
7370 i++, segment++)
7371 if (segment->p_paddr != 0)
7372 {
7373 p_paddr_valid = TRUE;
7374 break;
7375 }
7376
84d1d650
L
7377 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7378 i < num_segments;
7379 i++, segment++)
7380 {
7381 asection *section;
7382 unsigned int section_count;
7383 bfd_size_type amt;
7384 Elf_Internal_Shdr *this_hdr;
53020534 7385 asection *first_section = NULL;
a76e6f2f 7386 asection *lowest_section;
2542e49e 7387 bfd_boolean no_contents = TRUE;
84d1d650 7388
84d1d650
L
7389 /* Compute how many sections are in this segment. */
7390 for (section = ibfd->sections, section_count = 0;
7391 section != NULL;
7392 section = section->next)
7393 {
7394 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7395 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
3271a814 7396 {
a76e6f2f
AM
7397 if (first_section == NULL)
7398 first_section = section;
2542e49e
JL
7399 if (elf_section_type (section) != SHT_NOBITS)
7400 no_contents = FALSE;
3271a814
NS
7401 section_count++;
7402 }
84d1d650
L
7403 }
7404
7405 /* Allocate a segment map big enough to contain
7406 all of the sections we have selected. */
00bee008
AM
7407 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7408 amt += (bfd_size_type) section_count * sizeof (asection *);
a50b1753 7409 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
84d1d650
L
7410 if (map == NULL)
7411 return FALSE;
7412
7413 /* Initialize the fields of the output segment map with the
7414 input segment. */
7415 map->next = NULL;
7416 map->p_type = segment->p_type;
7417 map->p_flags = segment->p_flags;
7418 map->p_flags_valid = 1;
7419 map->p_paddr = segment->p_paddr;
88967714 7420 map->p_paddr_valid = p_paddr_valid;
3f570048
AM
7421 map->p_align = segment->p_align;
7422 map->p_align_valid = 1;
3271a814 7423 map->p_vaddr_offset = 0;
84d1d650 7424
04c3a755
NS
7425 if (map->p_type == PT_GNU_RELRO
7426 || map->p_type == PT_GNU_STACK)
b10a8ae0
L
7427 {
7428 /* The PT_GNU_RELRO segment may contain the first a few
7429 bytes in the .got.plt section even if the whole .got.plt
7430 section isn't in the PT_GNU_RELRO segment. We won't
04c3a755
NS
7431 change the size of the PT_GNU_RELRO segment.
7432 Similarly, PT_GNU_STACK size is significant on uclinux
7433 systems. */
9433b9b1 7434 map->p_size = segment->p_memsz;
b10a8ae0
L
7435 map->p_size_valid = 1;
7436 }
7437
84d1d650
L
7438 /* Determine if this segment contains the ELF file header
7439 and if it contains the program headers themselves. */
7440 map->includes_filehdr = (segment->p_offset == 0
7441 && segment->p_filesz >= iehdr->e_ehsize);
7442
7443 map->includes_phdrs = 0;
7444 if (! phdr_included || segment->p_type != PT_LOAD)
7445 {
7446 map->includes_phdrs =
7447 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7448 && (segment->p_offset + segment->p_filesz
7449 >= ((bfd_vma) iehdr->e_phoff
7450 + iehdr->e_phnum * iehdr->e_phentsize)));
7451
7452 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7453 phdr_included = TRUE;
7454 }
7455
bbefd0a9 7456 lowest_section = NULL;
84d1d650
L
7457 if (section_count != 0)
7458 {
7459 unsigned int isec = 0;
7460
53020534 7461 for (section = first_section;
84d1d650
L
7462 section != NULL;
7463 section = section->next)
7464 {
7465 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7466 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
53020534
L
7467 {
7468 map->sections[isec++] = section->output_section;
a76e6f2f
AM
7469 if ((section->flags & SEC_ALLOC) != 0)
7470 {
7471 bfd_vma seg_off;
7472
bbefd0a9
AM
7473 if (lowest_section == NULL
7474 || section->lma < lowest_section->lma)
fb8a5684
AM
7475 lowest_section = section;
7476
a76e6f2f
AM
7477 /* Section lmas are set up from PT_LOAD header
7478 p_paddr in _bfd_elf_make_section_from_shdr.
7479 If this header has a p_paddr that disagrees
7480 with the section lma, flag the p_paddr as
7481 invalid. */
7482 if ((section->flags & SEC_LOAD) != 0)
7483 seg_off = this_hdr->sh_offset - segment->p_offset;
7484 else
7485 seg_off = this_hdr->sh_addr - segment->p_vaddr;
7486 if (section->lma - segment->p_paddr != seg_off)
7487 map->p_paddr_valid = FALSE;
7488 }
53020534
L
7489 if (isec == section_count)
7490 break;
7491 }
84d1d650
L
7492 }
7493 }
7494
a76e6f2f 7495 if (map->includes_filehdr && lowest_section != NULL)
2542e49e
JL
7496 {
7497 /* Try to keep the space used by the headers plus any
7498 padding fixed. If there are sections with file contents
7499 in this segment then the lowest sh_offset is the best
7500 guess. Otherwise the segment only has file contents for
7501 the headers, and p_filesz is the best guess. */
7502 if (no_contents)
7503 map->header_size = segment->p_filesz;
7504 else
7505 map->header_size = lowest_section->filepos;
7506 }
d324f6d6 7507
5d695627
AM
7508 if (section_count == 0)
7509 map->p_vaddr_offset = segment->p_vaddr;
7510 else if (!map->includes_phdrs
7511 && !map->includes_filehdr
7512 && map->p_paddr_valid)
7513 /* Account for padding before the first section. */
7514 map->p_vaddr_offset = (segment->p_paddr
7515 - (lowest_section ? lowest_section->lma : 0));
a76e6f2f 7516
84d1d650
L
7517 map->count = section_count;
7518 *pointer_to_map = map;
7519 pointer_to_map = &map->next;
7520 }
7521
12bd6957 7522 elf_seg_map (obfd) = map_first;
84d1d650
L
7523 return TRUE;
7524}
7525
7526/* Copy private BFD data. This copies or rewrites ELF program header
7527 information. */
7528
7529static bfd_boolean
7530copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7531{
84d1d650
L
7532 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7533 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7534 return TRUE;
7535
7536 if (elf_tdata (ibfd)->phdr == NULL)
7537 return TRUE;
7538
7539 if (ibfd->xvec == obfd->xvec)
7540 {
cb3ff1e5
NC
7541 /* Check to see if any sections in the input BFD
7542 covered by ELF program header have changed. */
d55ce4e2 7543 Elf_Internal_Phdr *segment;
84d1d650
L
7544 asection *section, *osec;
7545 unsigned int i, num_segments;
7546 Elf_Internal_Shdr *this_hdr;
147d51c2
L
7547 const struct elf_backend_data *bed;
7548
7549 bed = get_elf_backend_data (ibfd);
7550
7551 /* Regenerate the segment map if p_paddr is set to 0. */
7552 if (bed->want_p_paddr_set_to_zero)
7553 goto rewrite;
84d1d650
L
7554
7555 /* Initialize the segment mark field. */
7556 for (section = obfd->sections; section != NULL;
7557 section = section->next)
7558 section->segment_mark = FALSE;
7559
7560 num_segments = elf_elfheader (ibfd)->e_phnum;
7561 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7562 i < num_segments;
7563 i++, segment++)
7564 {
5f6999aa
NC
7565 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7566 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7567 which severly confuses things, so always regenerate the segment
7568 map in this case. */
7569 if (segment->p_paddr == 0
7570 && segment->p_memsz == 0
7571 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
cb3ff1e5 7572 goto rewrite;
5f6999aa 7573
84d1d650
L
7574 for (section = ibfd->sections;
7575 section != NULL; section = section->next)
7576 {
7577 /* We mark the output section so that we know it comes
7578 from the input BFD. */
7579 osec = section->output_section;
7580 if (osec)
7581 osec->segment_mark = TRUE;
7582
7583 /* Check if this section is covered by the segment. */
7584 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7585 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
84d1d650
L
7586 {
7587 /* FIXME: Check if its output section is changed or
7588 removed. What else do we need to check? */
7589 if (osec == NULL
7590 || section->flags != osec->flags
7591 || section->lma != osec->lma
7592 || section->vma != osec->vma
7593 || section->size != osec->size
7594 || section->rawsize != osec->rawsize
7595 || section->alignment_power != osec->alignment_power)
7596 goto rewrite;
7597 }
7598 }
7599 }
7600
cb3ff1e5 7601 /* Check to see if any output section do not come from the
84d1d650
L
7602 input BFD. */
7603 for (section = obfd->sections; section != NULL;
7604 section = section->next)
7605 {
535b785f 7606 if (!section->segment_mark)
84d1d650
L
7607 goto rewrite;
7608 else
7609 section->segment_mark = FALSE;
7610 }
7611
7612 return copy_elf_program_header (ibfd, obfd);
7613 }
7614
7615rewrite:
f1d85785
L
7616 if (ibfd->xvec == obfd->xvec)
7617 {
7618 /* When rewriting program header, set the output maxpagesize to
7619 the maximum alignment of input PT_LOAD segments. */
7620 Elf_Internal_Phdr *segment;
7621 unsigned int i;
7622 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
7623 bfd_vma maxpagesize = 0;
7624
7625 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7626 i < num_segments;
7627 i++, segment++)
7628 if (segment->p_type == PT_LOAD
7629 && maxpagesize < segment->p_align)
c86934ce
NC
7630 {
7631 /* PR 17512: file: f17299af. */
7632 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
695344c0 7633 /* xgettext:c-format */
2dcf00ce
AM
7634 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7635 PRIx64 " is too large"),
7636 ibfd, (uint64_t) segment->p_align);
c86934ce
NC
7637 else
7638 maxpagesize = segment->p_align;
7639 }
f1d85785
L
7640
7641 if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
7642 bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
7643 }
7644
84d1d650
L
7645 return rewrite_elf_program_header (ibfd, obfd);
7646}
7647
ccd2ec6a
L
7648/* Initialize private output section information from input section. */
7649
7650bfd_boolean
7651_bfd_elf_init_private_section_data (bfd *ibfd,
7652 asection *isec,
7653 bfd *obfd,
7654 asection *osec,
7655 struct bfd_link_info *link_info)
7656
7657{
7658 Elf_Internal_Shdr *ihdr, *ohdr;
0e1862bb
L
7659 bfd_boolean final_link = (link_info != NULL
7660 && !bfd_link_relocatable (link_info));
ccd2ec6a
L
7661
7662 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7663 || obfd->xvec->flavour != bfd_target_elf_flavour)
7664 return TRUE;
7665
ba85c43e
NC
7666 BFD_ASSERT (elf_section_data (osec) != NULL);
7667
dfa7b0b8
AM
7668 /* For objcopy and relocatable link, don't copy the output ELF
7669 section type from input if the output BFD section flags have been
7670 set to something different. For a final link allow some flags
7671 that the linker clears to differ. */
42bb2e33 7672 if (elf_section_type (osec) == SHT_NULL
dfa7b0b8
AM
7673 && (osec->flags == isec->flags
7674 || (final_link
7675 && ((osec->flags ^ isec->flags)
0814be7d 7676 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
42bb2e33 7677 elf_section_type (osec) = elf_section_type (isec);
d270463e
L
7678
7679 /* FIXME: Is this correct for all OS/PROC specific flags? */
7680 elf_section_flags (osec) |= (elf_section_flags (isec)
7681 & (SHF_MASKOS | SHF_MASKPROC));
ccd2ec6a 7682
a91e1603 7683 /* Copy sh_info from input for mbind section. */
df3a023b
AM
7684 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
7685 && elf_section_flags (isec) & SHF_GNU_MBIND)
a91e1603
L
7686 elf_section_data (osec)->this_hdr.sh_info
7687 = elf_section_data (isec)->this_hdr.sh_info;
7688
ccd2ec6a
L
7689 /* Set things up for objcopy and relocatable link. The output
7690 SHT_GROUP section will have its elf_next_in_group pointing back
7691 to the input group members. Ignore linker created group section.
7692 See elfNN_ia64_object_p in elfxx-ia64.c. */
7bdf4127
AB
7693 if ((link_info == NULL
7694 || !link_info->resolve_section_groups)
7695 && (elf_sec_group (isec) == NULL
7696 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
ccd2ec6a 7697 {
7bdf4127
AB
7698 if (elf_section_flags (isec) & SHF_GROUP)
7699 elf_section_flags (osec) |= SHF_GROUP;
7700 elf_next_in_group (osec) = elf_next_in_group (isec);
7701 elf_section_data (osec)->group = elf_section_data (isec)->group;
ccd2ec6a
L
7702 }
7703
7bdf4127
AB
7704 /* If not decompress, preserve SHF_COMPRESSED. */
7705 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
7706 elf_section_flags (osec) |= (elf_section_flags (isec)
7707 & SHF_COMPRESSED);
7708
ccd2ec6a
L
7709 ihdr = &elf_section_data (isec)->this_hdr;
7710
7711 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7712 don't use the output section of the linked-to section since it
7713 may be NULL at this point. */
7714 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7715 {
7716 ohdr = &elf_section_data (osec)->this_hdr;
7717 ohdr->sh_flags |= SHF_LINK_ORDER;
7718 elf_linked_to_section (osec) = elf_linked_to_section (isec);
7719 }
7720
7721 osec->use_rela_p = isec->use_rela_p;
7722
7723 return TRUE;
7724}
7725
252b5132
RH
7726/* Copy private section information. This copies over the entsize
7727 field, and sometimes the info field. */
7728
b34976b6 7729bfd_boolean
217aa764
AM
7730_bfd_elf_copy_private_section_data (bfd *ibfd,
7731 asection *isec,
7732 bfd *obfd,
7733 asection *osec)
252b5132
RH
7734{
7735 Elf_Internal_Shdr *ihdr, *ohdr;
7736
7737 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7738 || obfd->xvec->flavour != bfd_target_elf_flavour)
b34976b6 7739 return TRUE;
252b5132 7740
252b5132
RH
7741 ihdr = &elf_section_data (isec)->this_hdr;
7742 ohdr = &elf_section_data (osec)->this_hdr;
7743
7744 ohdr->sh_entsize = ihdr->sh_entsize;
7745
7746 if (ihdr->sh_type == SHT_SYMTAB
7747 || ihdr->sh_type == SHT_DYNSYM
7748 || ihdr->sh_type == SHT_GNU_verneed
7749 || ihdr->sh_type == SHT_GNU_verdef)
7750 ohdr->sh_info = ihdr->sh_info;
7751
ccd2ec6a
L
7752 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
7753 NULL);
252b5132
RH
7754}
7755
d0bf826b
AM
7756/* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7757 necessary if we are removing either the SHT_GROUP section or any of
7758 the group member sections. DISCARDED is the value that a section's
7759 output_section has if the section will be discarded, NULL when this
7760 function is called from objcopy, bfd_abs_section_ptr when called
7761 from the linker. */
80fccad2
BW
7762
7763bfd_boolean
d0bf826b 7764_bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
80fccad2 7765{
30288845
AM
7766 asection *isec;
7767
30288845 7768 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
415f38a6 7769 if (elf_section_type (isec) == SHT_GROUP)
30288845
AM
7770 {
7771 asection *first = elf_next_in_group (isec);
7772 asection *s = first;
d0bf826b
AM
7773 bfd_size_type removed = 0;
7774
30288845
AM
7775 while (s != NULL)
7776 {
415f38a6
AM
7777 /* If this member section is being output but the
7778 SHT_GROUP section is not, then clear the group info
7779 set up by _bfd_elf_copy_private_section_data. */
d0bf826b
AM
7780 if (s->output_section != discarded
7781 && isec->output_section == discarded)
30288845
AM
7782 {
7783 elf_section_flags (s->output_section) &= ~SHF_GROUP;
7784 elf_group_name (s->output_section) = NULL;
7785 }
415f38a6
AM
7786 /* Conversely, if the member section is not being output
7787 but the SHT_GROUP section is, then adjust its size. */
d0bf826b
AM
7788 else if (s->output_section == discarded
7789 && isec->output_section != discarded)
6e5e9d58
AM
7790 {
7791 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
7792 removed += 4;
7793 if (elf_sec->rel.hdr != NULL
7794 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
7795 removed += 4;
7796 if (elf_sec->rela.hdr != NULL
7797 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
7798 removed += 4;
7799 }
30288845
AM
7800 s = elf_next_in_group (s);
7801 if (s == first)
7802 break;
7803 }
d0bf826b
AM
7804 if (removed != 0)
7805 {
7806 if (discarded != NULL)
7807 {
7808 /* If we've been called for ld -r, then we need to
6e5e9d58 7809 adjust the input section size. */
d0bf826b
AM
7810 if (isec->rawsize == 0)
7811 isec->rawsize = isec->size;
7812 isec->size = isec->rawsize - removed;
6e5e9d58
AM
7813 if (isec->size <= 4)
7814 {
7815 isec->size = 0;
7816 isec->flags |= SEC_EXCLUDE;
7817 }
d0bf826b
AM
7818 }
7819 else
7820 {
7821 /* Adjust the output section size when called from
7822 objcopy. */
7823 isec->output_section->size -= removed;
6e5e9d58
AM
7824 if (isec->output_section->size <= 4)
7825 {
7826 isec->output_section->size = 0;
7827 isec->output_section->flags |= SEC_EXCLUDE;
7828 }
d0bf826b
AM
7829 }
7830 }
30288845
AM
7831 }
7832
80fccad2
BW
7833 return TRUE;
7834}
7835
d0bf826b
AM
7836/* Copy private header information. */
7837
7838bfd_boolean
7839_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
7840{
7841 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7842 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7843 return TRUE;
7844
7845 /* Copy over private BFD data if it has not already been copied.
7846 This must be done here, rather than in the copy_private_bfd_data
7847 entry point, because the latter is called after the section
7848 contents have been set, which means that the program headers have
7849 already been worked out. */
12bd6957 7850 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
d0bf826b
AM
7851 {
7852 if (! copy_private_bfd_data (ibfd, obfd))
7853 return FALSE;
7854 }
7855
7856 return _bfd_elf_fixup_group_sections (ibfd, NULL);
7857}
7858
252b5132
RH
7859/* Copy private symbol information. If this symbol is in a section
7860 which we did not map into a BFD section, try to map the section
7861 index correctly. We use special macro definitions for the mapped
7862 section indices; these definitions are interpreted by the
7863 swap_out_syms function. */
7864
9ad5cbcf
AM
7865#define MAP_ONESYMTAB (SHN_HIOS + 1)
7866#define MAP_DYNSYMTAB (SHN_HIOS + 2)
7867#define MAP_STRTAB (SHN_HIOS + 3)
7868#define MAP_SHSTRTAB (SHN_HIOS + 4)
7869#define MAP_SYM_SHNDX (SHN_HIOS + 5)
252b5132 7870
b34976b6 7871bfd_boolean
217aa764
AM
7872_bfd_elf_copy_private_symbol_data (bfd *ibfd,
7873 asymbol *isymarg,
7874 bfd *obfd,
7875 asymbol *osymarg)
252b5132
RH
7876{
7877 elf_symbol_type *isym, *osym;
7878
7879 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7880 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 7881 return TRUE;
252b5132
RH
7882
7883 isym = elf_symbol_from (ibfd, isymarg);
7884 osym = elf_symbol_from (obfd, osymarg);
7885
7886 if (isym != NULL
8424d8f5 7887 && isym->internal_elf_sym.st_shndx != 0
252b5132
RH
7888 && osym != NULL
7889 && bfd_is_abs_section (isym->symbol.section))
7890 {
7891 unsigned int shndx;
7892
7893 shndx = isym->internal_elf_sym.st_shndx;
7894 if (shndx == elf_onesymtab (ibfd))
7895 shndx = MAP_ONESYMTAB;
7896 else if (shndx == elf_dynsymtab (ibfd))
7897 shndx = MAP_DYNSYMTAB;
12bd6957 7898 else if (shndx == elf_strtab_sec (ibfd))
252b5132 7899 shndx = MAP_STRTAB;
12bd6957 7900 else if (shndx == elf_shstrtab_sec (ibfd))
252b5132 7901 shndx = MAP_SHSTRTAB;
6a40cf0c 7902 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
9ad5cbcf 7903 shndx = MAP_SYM_SHNDX;
252b5132
RH
7904 osym->internal_elf_sym.st_shndx = shndx;
7905 }
7906
b34976b6 7907 return TRUE;
252b5132
RH
7908}
7909
7910/* Swap out the symbols. */
7911
b34976b6 7912static bfd_boolean
217aa764 7913swap_out_syms (bfd *abfd,
ef10c3ac 7914 struct elf_strtab_hash **sttp,
217aa764 7915 int relocatable_p)
252b5132 7916{
9c5bfbb7 7917 const struct elf_backend_data *bed;
079e9a2f
AM
7918 int symcount;
7919 asymbol **syms;
ef10c3ac 7920 struct elf_strtab_hash *stt;
079e9a2f 7921 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 7922 Elf_Internal_Shdr *symtab_shndx_hdr;
079e9a2f 7923 Elf_Internal_Shdr *symstrtab_hdr;
ef10c3ac 7924 struct elf_sym_strtab *symstrtab;
f075ee0c
AM
7925 bfd_byte *outbound_syms;
7926 bfd_byte *outbound_shndx;
ef10c3ac
L
7927 unsigned long outbound_syms_index;
7928 unsigned long outbound_shndx_index;
079e9a2f 7929 int idx;
12bd6957 7930 unsigned int num_locals;
079e9a2f 7931 bfd_size_type amt;
174fd7f9 7932 bfd_boolean name_local_sections;
252b5132 7933
12bd6957 7934 if (!elf_map_symbols (abfd, &num_locals))
b34976b6 7935 return FALSE;
252b5132 7936
c044fabd 7937 /* Dump out the symtabs. */
ef10c3ac 7938 stt = _bfd_elf_strtab_init ();
079e9a2f 7939 if (stt == NULL)
b34976b6 7940 return FALSE;
252b5132 7941
079e9a2f
AM
7942 bed = get_elf_backend_data (abfd);
7943 symcount = bfd_get_symcount (abfd);
7944 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7945 symtab_hdr->sh_type = SHT_SYMTAB;
7946 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7947 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
12bd6957 7948 symtab_hdr->sh_info = num_locals + 1;
72de5009 7949 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
079e9a2f
AM
7950
7951 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
7952 symstrtab_hdr->sh_type = SHT_STRTAB;
7953
ef10c3ac 7954 /* Allocate buffer to swap out the .strtab section. */
7a6e0d89
AM
7955 symstrtab = (struct elf_sym_strtab *) bfd_malloc2 (symcount + 1,
7956 sizeof (*symstrtab));
ef10c3ac
L
7957 if (symstrtab == NULL)
7958 {
7959 _bfd_elf_strtab_free (stt);
7960 return FALSE;
7961 }
7962
a50b1753 7963 outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
07d6d2b8 7964 bed->s->sizeof_sym);
079e9a2f 7965 if (outbound_syms == NULL)
5ed6aba4 7966 {
ef10c3ac
L
7967error_return:
7968 _bfd_elf_strtab_free (stt);
7969 free (symstrtab);
5ed6aba4
NC
7970 return FALSE;
7971 }
217aa764 7972 symtab_hdr->contents = outbound_syms;
ef10c3ac 7973 outbound_syms_index = 0;
252b5132 7974
9ad5cbcf 7975 outbound_shndx = NULL;
ef10c3ac 7976 outbound_shndx_index = 0;
6a40cf0c
NC
7977
7978 if (elf_symtab_shndx_list (abfd))
9ad5cbcf 7979 {
6a40cf0c
NC
7980 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
7981 if (symtab_shndx_hdr->sh_name != 0)
7982 {
7983 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
7984 outbound_shndx = (bfd_byte *)
7985 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
7986 if (outbound_shndx == NULL)
7987 goto error_return;
5ed6aba4 7988
6a40cf0c
NC
7989 symtab_shndx_hdr->contents = outbound_shndx;
7990 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
7991 symtab_shndx_hdr->sh_size = amt;
7992 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
7993 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
7994 }
7995 /* FIXME: What about any other headers in the list ? */
9ad5cbcf
AM
7996 }
7997
589e6347 7998 /* Now generate the data (for "contents"). */
079e9a2f
AM
7999 {
8000 /* Fill in zeroth symbol and swap it out. */
8001 Elf_Internal_Sym sym;
8002 sym.st_name = 0;
8003 sym.st_value = 0;
8004 sym.st_size = 0;
8005 sym.st_info = 0;
8006 sym.st_other = 0;
8007 sym.st_shndx = SHN_UNDEF;
35fc36a8 8008 sym.st_target_internal = 0;
ef10c3ac
L
8009 symstrtab[0].sym = sym;
8010 symstrtab[0].dest_index = outbound_syms_index;
8011 symstrtab[0].destshndx_index = outbound_shndx_index;
8012 outbound_syms_index++;
9ad5cbcf 8013 if (outbound_shndx != NULL)
ef10c3ac 8014 outbound_shndx_index++;
079e9a2f 8015 }
252b5132 8016
174fd7f9
RS
8017 name_local_sections
8018 = (bed->elf_backend_name_local_section_symbols
8019 && bed->elf_backend_name_local_section_symbols (abfd));
8020
079e9a2f 8021 syms = bfd_get_outsymbols (abfd);
ef10c3ac 8022 for (idx = 0; idx < symcount;)
252b5132 8023 {
252b5132 8024 Elf_Internal_Sym sym;
079e9a2f
AM
8025 bfd_vma value = syms[idx]->value;
8026 elf_symbol_type *type_ptr;
8027 flagword flags = syms[idx]->flags;
8028 int type;
252b5132 8029
174fd7f9
RS
8030 if (!name_local_sections
8031 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
079e9a2f
AM
8032 {
8033 /* Local section symbols have no name. */
ef10c3ac 8034 sym.st_name = (unsigned long) -1;
079e9a2f
AM
8035 }
8036 else
8037 {
ef10c3ac
L
8038 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8039 to get the final offset for st_name. */
8040 sym.st_name
8041 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
8042 FALSE);
079e9a2f 8043 if (sym.st_name == (unsigned long) -1)
ef10c3ac 8044 goto error_return;
079e9a2f 8045 }
252b5132 8046
079e9a2f 8047 type_ptr = elf_symbol_from (abfd, syms[idx]);
252b5132 8048
079e9a2f
AM
8049 if ((flags & BSF_SECTION_SYM) == 0
8050 && bfd_is_com_section (syms[idx]->section))
8051 {
8052 /* ELF common symbols put the alignment into the `value' field,
8053 and the size into the `size' field. This is backwards from
8054 how BFD handles it, so reverse it here. */
8055 sym.st_size = value;
8056 if (type_ptr == NULL
8057 || type_ptr->internal_elf_sym.st_value == 0)
8058 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8059 else
8060 sym.st_value = type_ptr->internal_elf_sym.st_value;
8061 sym.st_shndx = _bfd_elf_section_from_bfd_section
8062 (abfd, syms[idx]->section);
8063 }
8064 else
8065 {
8066 asection *sec = syms[idx]->section;
cb33740c 8067 unsigned int shndx;
252b5132 8068
079e9a2f
AM
8069 if (sec->output_section)
8070 {
8071 value += sec->output_offset;
8072 sec = sec->output_section;
8073 }
589e6347 8074
079e9a2f
AM
8075 /* Don't add in the section vma for relocatable output. */
8076 if (! relocatable_p)
8077 value += sec->vma;
8078 sym.st_value = value;
8079 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8080
8081 if (bfd_is_abs_section (sec)
8082 && type_ptr != NULL
8083 && type_ptr->internal_elf_sym.st_shndx != 0)
8084 {
8085 /* This symbol is in a real ELF section which we did
8086 not create as a BFD section. Undo the mapping done
8087 by copy_private_symbol_data. */
8088 shndx = type_ptr->internal_elf_sym.st_shndx;
8089 switch (shndx)
8090 {
8091 case MAP_ONESYMTAB:
8092 shndx = elf_onesymtab (abfd);
8093 break;
8094 case MAP_DYNSYMTAB:
8095 shndx = elf_dynsymtab (abfd);
8096 break;
8097 case MAP_STRTAB:
12bd6957 8098 shndx = elf_strtab_sec (abfd);
079e9a2f
AM
8099 break;
8100 case MAP_SHSTRTAB:
12bd6957 8101 shndx = elf_shstrtab_sec (abfd);
079e9a2f 8102 break;
9ad5cbcf 8103 case MAP_SYM_SHNDX:
6a40cf0c
NC
8104 if (elf_symtab_shndx_list (abfd))
8105 shndx = elf_symtab_shndx_list (abfd)->ndx;
9ad5cbcf 8106 break;
079e9a2f 8107 default:
15bc576a 8108 shndx = SHN_ABS;
079e9a2f
AM
8109 break;
8110 }
8111 }
8112 else
8113 {
8114 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
252b5132 8115
cb33740c 8116 if (shndx == SHN_BAD)
079e9a2f
AM
8117 {
8118 asection *sec2;
8119
8120 /* Writing this would be a hell of a lot easier if
8121 we had some decent documentation on bfd, and
8122 knew what to expect of the library, and what to
8123 demand of applications. For example, it
8124 appears that `objcopy' might not set the
8125 section of a symbol to be a section that is
8126 actually in the output file. */
8127 sec2 = bfd_get_section_by_name (abfd, sec->name);
5df1bc57
AM
8128 if (sec2 != NULL)
8129 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8130 if (shndx == SHN_BAD)
589e6347 8131 {
695344c0 8132 /* xgettext:c-format */
9793eb77
AM
8133 _bfd_error_handler
8134 (_("unable to find equivalent output section"
8135 " for symbol '%s' from section '%s'"),
8136 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8137 sec->name);
811072d8 8138 bfd_set_error (bfd_error_invalid_operation);
ef10c3ac 8139 goto error_return;
589e6347 8140 }
079e9a2f
AM
8141 }
8142 }
252b5132 8143
079e9a2f
AM
8144 sym.st_shndx = shndx;
8145 }
252b5132 8146
13ae64f3
JJ
8147 if ((flags & BSF_THREAD_LOCAL) != 0)
8148 type = STT_TLS;
d8045f23
NC
8149 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8150 type = STT_GNU_IFUNC;
13ae64f3 8151 else if ((flags & BSF_FUNCTION) != 0)
079e9a2f
AM
8152 type = STT_FUNC;
8153 else if ((flags & BSF_OBJECT) != 0)
8154 type = STT_OBJECT;
d9352518
DB
8155 else if ((flags & BSF_RELC) != 0)
8156 type = STT_RELC;
8157 else if ((flags & BSF_SRELC) != 0)
8158 type = STT_SRELC;
079e9a2f
AM
8159 else
8160 type = STT_NOTYPE;
252b5132 8161
13ae64f3
JJ
8162 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8163 type = STT_TLS;
8164
589e6347 8165 /* Processor-specific types. */
079e9a2f
AM
8166 if (type_ptr != NULL
8167 && bed->elf_backend_get_symbol_type)
8168 type = ((*bed->elf_backend_get_symbol_type)
8169 (&type_ptr->internal_elf_sym, type));
252b5132 8170
079e9a2f
AM
8171 if (flags & BSF_SECTION_SYM)
8172 {
8173 if (flags & BSF_GLOBAL)
8174 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8175 else
8176 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8177 }
8178 else if (bfd_is_com_section (syms[idx]->section))
0a40daed 8179 {
b8871f35
L
8180 if (type != STT_TLS)
8181 {
8182 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8183 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8184 ? STT_COMMON : STT_OBJECT);
8185 else
8186 type = ((flags & BSF_ELF_COMMON) != 0
8187 ? STT_COMMON : STT_OBJECT);
8188 }
8189 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
0a40daed 8190 }
079e9a2f
AM
8191 else if (bfd_is_und_section (syms[idx]->section))
8192 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8193 ? STB_WEAK
8194 : STB_GLOBAL),
8195 type);
8196 else if (flags & BSF_FILE)
8197 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8198 else
8199 {
8200 int bind = STB_LOCAL;
252b5132 8201
079e9a2f
AM
8202 if (flags & BSF_LOCAL)
8203 bind = STB_LOCAL;
3e7a7d11
NC
8204 else if (flags & BSF_GNU_UNIQUE)
8205 bind = STB_GNU_UNIQUE;
079e9a2f
AM
8206 else if (flags & BSF_WEAK)
8207 bind = STB_WEAK;
8208 else if (flags & BSF_GLOBAL)
8209 bind = STB_GLOBAL;
252b5132 8210
079e9a2f
AM
8211 sym.st_info = ELF_ST_INFO (bind, type);
8212 }
252b5132 8213
079e9a2f 8214 if (type_ptr != NULL)
35fc36a8
RS
8215 {
8216 sym.st_other = type_ptr->internal_elf_sym.st_other;
8217 sym.st_target_internal
8218 = type_ptr->internal_elf_sym.st_target_internal;
8219 }
079e9a2f 8220 else
35fc36a8
RS
8221 {
8222 sym.st_other = 0;
8223 sym.st_target_internal = 0;
8224 }
252b5132 8225
ef10c3ac
L
8226 idx++;
8227 symstrtab[idx].sym = sym;
8228 symstrtab[idx].dest_index = outbound_syms_index;
8229 symstrtab[idx].destshndx_index = outbound_shndx_index;
8230
8231 outbound_syms_index++;
9ad5cbcf 8232 if (outbound_shndx != NULL)
ef10c3ac
L
8233 outbound_shndx_index++;
8234 }
8235
8236 /* Finalize the .strtab section. */
8237 _bfd_elf_strtab_finalize (stt);
8238
8239 /* Swap out the .strtab section. */
8240 for (idx = 0; idx <= symcount; idx++)
8241 {
8242 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8243 if (elfsym->sym.st_name == (unsigned long) -1)
8244 elfsym->sym.st_name = 0;
8245 else
8246 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8247 elfsym->sym.st_name);
8248 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8249 (outbound_syms
8250 + (elfsym->dest_index
8251 * bed->s->sizeof_sym)),
8252 (outbound_shndx
8253 + (elfsym->destshndx_index
8254 * sizeof (Elf_External_Sym_Shndx))));
079e9a2f 8255 }
ef10c3ac 8256 free (symstrtab);
252b5132 8257
079e9a2f 8258 *sttp = stt;
ef10c3ac 8259 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
079e9a2f 8260 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 8261 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
079e9a2f
AM
8262 symstrtab_hdr->sh_addr = 0;
8263 symstrtab_hdr->sh_entsize = 0;
8264 symstrtab_hdr->sh_link = 0;
8265 symstrtab_hdr->sh_info = 0;
8266 symstrtab_hdr->sh_addralign = 1;
252b5132 8267
b34976b6 8268 return TRUE;
252b5132
RH
8269}
8270
8271/* Return the number of bytes required to hold the symtab vector.
8272
8273 Note that we base it on the count plus 1, since we will null terminate
8274 the vector allocated based on this size. However, the ELF symbol table
8275 always has a dummy entry as symbol #0, so it ends up even. */
8276
8277long
217aa764 8278_bfd_elf_get_symtab_upper_bound (bfd *abfd)
252b5132 8279{
3a551c7a 8280 bfd_size_type symcount;
252b5132
RH
8281 long symtab_size;
8282 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8283
8284 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3a551c7a
AM
8285 if (symcount >= LONG_MAX / sizeof (asymbol *))
8286 {
8287 bfd_set_error (bfd_error_file_too_big);
8288 return -1;
8289 }
b99d1833
AM
8290 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8291 if (symcount > 0)
8292 symtab_size -= sizeof (asymbol *);
252b5132
RH
8293
8294 return symtab_size;
8295}
8296
8297long
217aa764 8298_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
252b5132 8299{
3a551c7a 8300 bfd_size_type symcount;
252b5132
RH
8301 long symtab_size;
8302 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8303
8304 if (elf_dynsymtab (abfd) == 0)
8305 {
8306 bfd_set_error (bfd_error_invalid_operation);
8307 return -1;
8308 }
8309
8310 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3a551c7a
AM
8311 if (symcount >= LONG_MAX / sizeof (asymbol *))
8312 {
8313 bfd_set_error (bfd_error_file_too_big);
8314 return -1;
8315 }
b99d1833
AM
8316 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8317 if (symcount > 0)
8318 symtab_size -= sizeof (asymbol *);
252b5132
RH
8319
8320 return symtab_size;
8321}
8322
8323long
217aa764
AM
8324_bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
8325 sec_ptr asect)
252b5132 8326{
242a1159 8327#if SIZEOF_LONG == SIZEOF_INT
7a6e0d89
AM
8328 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
8329 {
8330 bfd_set_error (bfd_error_file_too_big);
8331 return -1;
8332 }
242a1159 8333#endif
252b5132
RH
8334 return (asect->reloc_count + 1) * sizeof (arelent *);
8335}
8336
8337/* Canonicalize the relocs. */
8338
8339long
217aa764
AM
8340_bfd_elf_canonicalize_reloc (bfd *abfd,
8341 sec_ptr section,
8342 arelent **relptr,
8343 asymbol **symbols)
252b5132
RH
8344{
8345 arelent *tblptr;
8346 unsigned int i;
9c5bfbb7 8347 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 8348
b34976b6 8349 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
252b5132
RH
8350 return -1;
8351
8352 tblptr = section->relocation;
8353 for (i = 0; i < section->reloc_count; i++)
8354 *relptr++ = tblptr++;
8355
8356 *relptr = NULL;
8357
8358 return section->reloc_count;
8359}
8360
8361long
6cee3f79 8362_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
252b5132 8363{
9c5bfbb7 8364 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764 8365 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
252b5132
RH
8366
8367 if (symcount >= 0)
8368 bfd_get_symcount (abfd) = symcount;
8369 return symcount;
8370}
8371
8372long
217aa764
AM
8373_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8374 asymbol **allocation)
252b5132 8375{
9c5bfbb7 8376 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764 8377 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
1f70368c
DJ
8378
8379 if (symcount >= 0)
8380 bfd_get_dynamic_symcount (abfd) = symcount;
8381 return symcount;
252b5132
RH
8382}
8383
8615f3f2
AM
8384/* Return the size required for the dynamic reloc entries. Any loadable
8385 section that was actually installed in the BFD, and has type SHT_REL
8386 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8387 dynamic reloc section. */
252b5132
RH
8388
8389long
217aa764 8390_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
252b5132 8391{
3a551c7a 8392 bfd_size_type count;
252b5132
RH
8393 asection *s;
8394
8395 if (elf_dynsymtab (abfd) == 0)
8396 {
8397 bfd_set_error (bfd_error_invalid_operation);
8398 return -1;
8399 }
8400
3a551c7a 8401 count = 1;
252b5132 8402 for (s = abfd->sections; s != NULL; s = s->next)
266b05cf 8403 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
8404 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8405 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
3a551c7a
AM
8406 {
8407 count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
8408 if (count > LONG_MAX / sizeof (arelent *))
8409 {
8410 bfd_set_error (bfd_error_file_too_big);
8411 return -1;
8412 }
8413 }
8414 return count * sizeof (arelent *);
252b5132
RH
8415}
8416
8615f3f2
AM
8417/* Canonicalize the dynamic relocation entries. Note that we return the
8418 dynamic relocations as a single block, although they are actually
8419 associated with particular sections; the interface, which was
8420 designed for SunOS style shared libraries, expects that there is only
8421 one set of dynamic relocs. Any loadable section that was actually
8422 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8423 dynamic symbol table, is considered to be a dynamic reloc section. */
252b5132
RH
8424
8425long
217aa764
AM
8426_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8427 arelent **storage,
8428 asymbol **syms)
252b5132 8429{
217aa764 8430 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
252b5132
RH
8431 asection *s;
8432 long ret;
8433
8434 if (elf_dynsymtab (abfd) == 0)
8435 {
8436 bfd_set_error (bfd_error_invalid_operation);
8437 return -1;
8438 }
8439
8440 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8441 ret = 0;
8442 for (s = abfd->sections; s != NULL; s = s->next)
8443 {
266b05cf 8444 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
8445 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8446 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8447 {
8448 arelent *p;
8449 long count, i;
8450
b34976b6 8451 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
252b5132 8452 return -1;
eea6121a 8453 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
252b5132
RH
8454 p = s->relocation;
8455 for (i = 0; i < count; i++)
8456 *storage++ = p++;
8457 ret += count;
8458 }
8459 }
8460
8461 *storage = NULL;
8462
8463 return ret;
8464}
8465\f
8466/* Read in the version information. */
8467
b34976b6 8468bfd_boolean
fc0e6df6 8469_bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
252b5132
RH
8470{
8471 bfd_byte *contents = NULL;
fc0e6df6
PB
8472 unsigned int freeidx = 0;
8473
8474 if (elf_dynverref (abfd) != 0)
8475 {
8476 Elf_Internal_Shdr *hdr;
8477 Elf_External_Verneed *everneed;
8478 Elf_Internal_Verneed *iverneed;
8479 unsigned int i;
d0fb9a8d 8480 bfd_byte *contents_end;
fc0e6df6
PB
8481
8482 hdr = &elf_tdata (abfd)->dynverref_hdr;
8483
bd61e135
AM
8484 if (hdr->sh_info == 0
8485 || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
d0fb9a8d 8486 {
601a03ba 8487error_return_bad_verref:
4eca0228 8488 _bfd_error_handler
871b3ab2 8489 (_("%pB: .gnu.version_r invalid entry"), abfd);
601a03ba 8490 bfd_set_error (bfd_error_bad_value);
d0fb9a8d
JJ
8491error_return_verref:
8492 elf_tdata (abfd)->verref = NULL;
8493 elf_tdata (abfd)->cverrefs = 0;
8494 goto error_return;
8495 }
601a03ba 8496
7e56c51c
NC
8497 ufile_ptr filesize = bfd_get_file_size (abfd);
8498 if (filesize > 0 && filesize < hdr->sh_size)
8499 {
8500 /* PR 24708: Avoid attempts to allocate a ridiculous amount
8501 of memory. */
8502 bfd_set_error (bfd_error_no_memory);
8503 _bfd_error_handler
8504 /* xgettext:c-format */
8505 (_("error: %pB version reference section is too large (%#" PRIx64 " bytes)"),
8506 abfd, (uint64_t) hdr->sh_size);
8507 goto error_return_verref;
8508 }
601a03ba
AM
8509 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
8510 if (contents == NULL)
8511 goto error_return_verref;
8512
fc0e6df6
PB
8513 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
8514 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
d0fb9a8d 8515 goto error_return_verref;
fc0e6df6 8516
601a03ba 8517 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
bd61e135 8518 bfd_alloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
601a03ba
AM
8519
8520 if (elf_tdata (abfd)->verref == NULL)
d0fb9a8d
JJ
8521 goto error_return_verref;
8522
8523 BFD_ASSERT (sizeof (Elf_External_Verneed)
8524 == sizeof (Elf_External_Vernaux));
8525 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
fc0e6df6
PB
8526 everneed = (Elf_External_Verneed *) contents;
8527 iverneed = elf_tdata (abfd)->verref;
8528 for (i = 0; i < hdr->sh_info; i++, iverneed++)
8529 {
8530 Elf_External_Vernaux *evernaux;
8531 Elf_Internal_Vernaux *ivernaux;
8532 unsigned int j;
8533
8534 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8535
8536 iverneed->vn_bfd = abfd;
8537
8538 iverneed->vn_filename =
8539 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8540 iverneed->vn_file);
8541 if (iverneed->vn_filename == NULL)
601a03ba 8542 goto error_return_bad_verref;
fc0e6df6 8543
d0fb9a8d
JJ
8544 if (iverneed->vn_cnt == 0)
8545 iverneed->vn_auxptr = NULL;
8546 else
8547 {
a50b1753 8548 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
07d6d2b8
AM
8549 bfd_alloc2 (abfd, iverneed->vn_cnt,
8550 sizeof (Elf_Internal_Vernaux));
d0fb9a8d
JJ
8551 if (iverneed->vn_auxptr == NULL)
8552 goto error_return_verref;
8553 }
8554
8555 if (iverneed->vn_aux
8556 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8557 goto error_return_bad_verref;
fc0e6df6
PB
8558
8559 evernaux = ((Elf_External_Vernaux *)
8560 ((bfd_byte *) everneed + iverneed->vn_aux));
8561 ivernaux = iverneed->vn_auxptr;
8562 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8563 {
8564 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8565
8566 ivernaux->vna_nodename =
8567 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8568 ivernaux->vna_name);
8569 if (ivernaux->vna_nodename == NULL)
601a03ba 8570 goto error_return_bad_verref;
fc0e6df6 8571
25ff461f
AM
8572 if (ivernaux->vna_other > freeidx)
8573 freeidx = ivernaux->vna_other;
8574
8575 ivernaux->vna_nextptr = NULL;
8576 if (ivernaux->vna_next == 0)
8577 {
8578 iverneed->vn_cnt = j + 1;
8579 break;
8580 }
fc0e6df6
PB
8581 if (j + 1 < iverneed->vn_cnt)
8582 ivernaux->vna_nextptr = ivernaux + 1;
fc0e6df6 8583
d0fb9a8d
JJ
8584 if (ivernaux->vna_next
8585 > (size_t) (contents_end - (bfd_byte *) evernaux))
601a03ba 8586 goto error_return_bad_verref;
d0fb9a8d 8587
fc0e6df6
PB
8588 evernaux = ((Elf_External_Vernaux *)
8589 ((bfd_byte *) evernaux + ivernaux->vna_next));
fc0e6df6
PB
8590 }
8591
25ff461f
AM
8592 iverneed->vn_nextref = NULL;
8593 if (iverneed->vn_next == 0)
8594 break;
fc0e6df6
PB
8595 if (i + 1 < hdr->sh_info)
8596 iverneed->vn_nextref = iverneed + 1;
fc0e6df6 8597
d0fb9a8d
JJ
8598 if (iverneed->vn_next
8599 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8600 goto error_return_bad_verref;
d0fb9a8d 8601
fc0e6df6
PB
8602 everneed = ((Elf_External_Verneed *)
8603 ((bfd_byte *) everneed + iverneed->vn_next));
8604 }
25ff461f 8605 elf_tdata (abfd)->cverrefs = i;
fc0e6df6
PB
8606
8607 free (contents);
8608 contents = NULL;
8609 }
252b5132
RH
8610
8611 if (elf_dynverdef (abfd) != 0)
8612 {
8613 Elf_Internal_Shdr *hdr;
8614 Elf_External_Verdef *everdef;
8615 Elf_Internal_Verdef *iverdef;
f631889e
UD
8616 Elf_Internal_Verdef *iverdefarr;
8617 Elf_Internal_Verdef iverdefmem;
252b5132 8618 unsigned int i;
062e2358 8619 unsigned int maxidx;
d0fb9a8d 8620 bfd_byte *contents_end_def, *contents_end_aux;
252b5132
RH
8621
8622 hdr = &elf_tdata (abfd)->dynverdef_hdr;
8623
601a03ba
AM
8624 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
8625 {
8626 error_return_bad_verdef:
4eca0228 8627 _bfd_error_handler
871b3ab2 8628 (_("%pB: .gnu.version_d invalid entry"), abfd);
601a03ba
AM
8629 bfd_set_error (bfd_error_bad_value);
8630 error_return_verdef:
8631 elf_tdata (abfd)->verdef = NULL;
8632 elf_tdata (abfd)->cverdefs = 0;
8633 goto error_return;
8634 }
8635
a50b1753 8636 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
252b5132 8637 if (contents == NULL)
601a03ba 8638 goto error_return_verdef;
252b5132 8639 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
217aa764 8640 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
601a03ba 8641 goto error_return_verdef;
d0fb9a8d
JJ
8642
8643 BFD_ASSERT (sizeof (Elf_External_Verdef)
8644 >= sizeof (Elf_External_Verdaux));
8645 contents_end_def = contents + hdr->sh_size
8646 - sizeof (Elf_External_Verdef);
8647 contents_end_aux = contents + hdr->sh_size
8648 - sizeof (Elf_External_Verdaux);
8649
f631889e
UD
8650 /* We know the number of entries in the section but not the maximum
8651 index. Therefore we have to run through all entries and find
8652 the maximum. */
252b5132 8653 everdef = (Elf_External_Verdef *) contents;
f631889e
UD
8654 maxidx = 0;
8655 for (i = 0; i < hdr->sh_info; ++i)
8656 {
8657 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8658
601a03ba
AM
8659 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
8660 goto error_return_bad_verdef;
062e2358
AM
8661 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
8662 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
f631889e 8663
25ff461f
AM
8664 if (iverdefmem.vd_next == 0)
8665 break;
8666
d0fb9a8d
JJ
8667 if (iverdefmem.vd_next
8668 > (size_t) (contents_end_def - (bfd_byte *) everdef))
601a03ba 8669 goto error_return_bad_verdef;
d0fb9a8d 8670
f631889e
UD
8671 everdef = ((Elf_External_Verdef *)
8672 ((bfd_byte *) everdef + iverdefmem.vd_next));
8673 }
8674
fc0e6df6
PB
8675 if (default_imported_symver)
8676 {
8677 if (freeidx > maxidx)
8678 maxidx = ++freeidx;
8679 else
8680 freeidx = ++maxidx;
8681 }
201159ec 8682
601a03ba
AM
8683 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
8684 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
f631889e 8685 if (elf_tdata (abfd)->verdef == NULL)
601a03ba 8686 goto error_return_verdef;
f631889e
UD
8687
8688 elf_tdata (abfd)->cverdefs = maxidx;
8689
8690 everdef = (Elf_External_Verdef *) contents;
8691 iverdefarr = elf_tdata (abfd)->verdef;
8692 for (i = 0; i < hdr->sh_info; i++)
252b5132
RH
8693 {
8694 Elf_External_Verdaux *everdaux;
8695 Elf_Internal_Verdaux *iverdaux;
8696 unsigned int j;
8697
f631889e
UD
8698 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8699
d0fb9a8d 8700 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
601a03ba 8701 goto error_return_bad_verdef;
d0fb9a8d 8702
f631889e 8703 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
595bce75 8704 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
252b5132
RH
8705
8706 iverdef->vd_bfd = abfd;
8707
d0fb9a8d
JJ
8708 if (iverdef->vd_cnt == 0)
8709 iverdef->vd_auxptr = NULL;
8710 else
8711 {
a50b1753 8712 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
07d6d2b8
AM
8713 bfd_alloc2 (abfd, iverdef->vd_cnt,
8714 sizeof (Elf_Internal_Verdaux));
d0fb9a8d
JJ
8715 if (iverdef->vd_auxptr == NULL)
8716 goto error_return_verdef;
8717 }
8718
8719 if (iverdef->vd_aux
8720 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
601a03ba 8721 goto error_return_bad_verdef;
252b5132
RH
8722
8723 everdaux = ((Elf_External_Verdaux *)
8724 ((bfd_byte *) everdef + iverdef->vd_aux));
8725 iverdaux = iverdef->vd_auxptr;
8726 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
8727 {
8728 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
8729
8730 iverdaux->vda_nodename =
8731 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8732 iverdaux->vda_name);
8733 if (iverdaux->vda_nodename == NULL)
601a03ba 8734 goto error_return_bad_verdef;
252b5132 8735
25ff461f
AM
8736 iverdaux->vda_nextptr = NULL;
8737 if (iverdaux->vda_next == 0)
8738 {
8739 iverdef->vd_cnt = j + 1;
8740 break;
8741 }
252b5132
RH
8742 if (j + 1 < iverdef->vd_cnt)
8743 iverdaux->vda_nextptr = iverdaux + 1;
252b5132 8744
d0fb9a8d
JJ
8745 if (iverdaux->vda_next
8746 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
601a03ba 8747 goto error_return_bad_verdef;
d0fb9a8d 8748
252b5132
RH
8749 everdaux = ((Elf_External_Verdaux *)
8750 ((bfd_byte *) everdaux + iverdaux->vda_next));
8751 }
8752
595bce75 8753 iverdef->vd_nodename = NULL;
d0fb9a8d
JJ
8754 if (iverdef->vd_cnt)
8755 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
252b5132 8756
25ff461f
AM
8757 iverdef->vd_nextdef = NULL;
8758 if (iverdef->vd_next == 0)
8759 break;
d0fb9a8d 8760 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
252b5132 8761 iverdef->vd_nextdef = iverdef + 1;
252b5132
RH
8762
8763 everdef = ((Elf_External_Verdef *)
8764 ((bfd_byte *) everdef + iverdef->vd_next));
8765 }
8766
8767 free (contents);
8768 contents = NULL;
8769 }
fc0e6df6 8770 else if (default_imported_symver)
252b5132 8771 {
fc0e6df6
PB
8772 if (freeidx < 3)
8773 freeidx = 3;
8774 else
8775 freeidx++;
252b5132 8776
a50b1753 8777 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
07d6d2b8 8778 bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
fc0e6df6 8779 if (elf_tdata (abfd)->verdef == NULL)
252b5132
RH
8780 goto error_return;
8781
fc0e6df6
PB
8782 elf_tdata (abfd)->cverdefs = freeidx;
8783 }
252b5132 8784
fc0e6df6
PB
8785 /* Create a default version based on the soname. */
8786 if (default_imported_symver)
8787 {
8788 Elf_Internal_Verdef *iverdef;
8789 Elf_Internal_Verdaux *iverdaux;
252b5132 8790
5bb3703f 8791 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
252b5132 8792
fc0e6df6
PB
8793 iverdef->vd_version = VER_DEF_CURRENT;
8794 iverdef->vd_flags = 0;
8795 iverdef->vd_ndx = freeidx;
8796 iverdef->vd_cnt = 1;
252b5132 8797
fc0e6df6 8798 iverdef->vd_bfd = abfd;
252b5132 8799
fc0e6df6
PB
8800 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
8801 if (iverdef->vd_nodename == NULL)
d0fb9a8d 8802 goto error_return_verdef;
fc0e6df6 8803 iverdef->vd_nextdef = NULL;
601a03ba
AM
8804 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
8805 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
d0fb9a8d
JJ
8806 if (iverdef->vd_auxptr == NULL)
8807 goto error_return_verdef;
252b5132 8808
fc0e6df6
PB
8809 iverdaux = iverdef->vd_auxptr;
8810 iverdaux->vda_nodename = iverdef->vd_nodename;
252b5132
RH
8811 }
8812
b34976b6 8813 return TRUE;
252b5132
RH
8814
8815 error_return:
5ed6aba4 8816 if (contents != NULL)
252b5132 8817 free (contents);
b34976b6 8818 return FALSE;
252b5132
RH
8819}
8820\f
8821asymbol *
217aa764 8822_bfd_elf_make_empty_symbol (bfd *abfd)
252b5132
RH
8823{
8824 elf_symbol_type *newsym;
8825
7a6e0d89 8826 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
252b5132
RH
8827 if (!newsym)
8828 return NULL;
201159ec
NC
8829 newsym->symbol.the_bfd = abfd;
8830 return &newsym->symbol;
252b5132
RH
8831}
8832
8833void
217aa764
AM
8834_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
8835 asymbol *symbol,
8836 symbol_info *ret)
252b5132
RH
8837{
8838 bfd_symbol_info (symbol, ret);
8839}
8840
8841/* Return whether a symbol name implies a local symbol. Most targets
8842 use this function for the is_local_label_name entry point, but some
8843 override it. */
8844
b34976b6 8845bfd_boolean
217aa764
AM
8846_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
8847 const char *name)
252b5132
RH
8848{
8849 /* Normal local symbols start with ``.L''. */
8850 if (name[0] == '.' && name[1] == 'L')
b34976b6 8851 return TRUE;
252b5132
RH
8852
8853 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8854 DWARF debugging symbols starting with ``..''. */
8855 if (name[0] == '.' && name[1] == '.')
b34976b6 8856 return TRUE;
252b5132
RH
8857
8858 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8859 emitting DWARF debugging output. I suspect this is actually a
8860 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8861 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8862 underscore to be emitted on some ELF targets). For ease of use,
8863 we treat such symbols as local. */
8864 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
b34976b6 8865 return TRUE;
252b5132 8866
b1fa9dd6
NC
8867 /* Treat assembler generated fake symbols, dollar local labels and
8868 forward-backward labels (aka local labels) as locals.
8869 These labels have the form:
8870
07d6d2b8 8871 L0^A.* (fake symbols)
b1fa9dd6
NC
8872
8873 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8874
8875 Versions which start with .L will have already been matched above,
8876 so we only need to match the rest. */
8877 if (name[0] == 'L' && ISDIGIT (name[1]))
8878 {
8879 bfd_boolean ret = FALSE;
8880 const char * p;
8881 char c;
8882
8883 for (p = name + 2; (c = *p); p++)
8884 {
8885 if (c == 1 || c == 2)
8886 {
8887 if (c == 1 && p == name + 2)
8888 /* A fake symbol. */
8889 return TRUE;
8890
8891 /* FIXME: We are being paranoid here and treating symbols like
8892 L0^Bfoo as if there were non-local, on the grounds that the
8893 assembler will never generate them. But can any symbol
8894 containing an ASCII value in the range 1-31 ever be anything
8895 other than some kind of local ? */
8896 ret = TRUE;
8897 }
8898
8899 if (! ISDIGIT (c))
8900 {
8901 ret = FALSE;
8902 break;
8903 }
8904 }
8905 return ret;
8906 }
ffa54770 8907
b34976b6 8908 return FALSE;
252b5132
RH
8909}
8910
8911alent *
217aa764
AM
8912_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
8913 asymbol *symbol ATTRIBUTE_UNUSED)
252b5132
RH
8914{
8915 abort ();
8916 return NULL;
8917}
8918
b34976b6 8919bfd_boolean
217aa764
AM
8920_bfd_elf_set_arch_mach (bfd *abfd,
8921 enum bfd_architecture arch,
8922 unsigned long machine)
252b5132
RH
8923{
8924 /* If this isn't the right architecture for this backend, and this
8925 isn't the generic backend, fail. */
8926 if (arch != get_elf_backend_data (abfd)->arch
8927 && arch != bfd_arch_unknown
8928 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
b34976b6 8929 return FALSE;
252b5132
RH
8930
8931 return bfd_default_set_arch_mach (abfd, arch, machine);
8932}
8933
d1fad7c6
NC
8934/* Find the nearest line to a particular section and offset,
8935 for error reporting. */
8936
b34976b6 8937bfd_boolean
217aa764 8938_bfd_elf_find_nearest_line (bfd *abfd,
217aa764 8939 asymbol **symbols,
fb167eb2 8940 asection *section,
217aa764
AM
8941 bfd_vma offset,
8942 const char **filename_ptr,
8943 const char **functionname_ptr,
fb167eb2
AM
8944 unsigned int *line_ptr,
8945 unsigned int *discriminator_ptr)
d1fad7c6 8946{
b34976b6 8947 bfd_boolean found;
d1fad7c6 8948
fb167eb2 8949 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
4e8a9624 8950 filename_ptr, functionname_ptr,
fb167eb2
AM
8951 line_ptr, discriminator_ptr,
8952 dwarf_debug_sections, 0,
e00e8198
AM
8953 &elf_tdata (abfd)->dwarf2_find_line_info)
8954 || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
8955 filename_ptr, functionname_ptr,
8956 line_ptr))
d1fad7c6
NC
8957 {
8958 if (!*functionname_ptr)
e00e8198
AM
8959 _bfd_elf_find_function (abfd, symbols, section, offset,
8960 *filename_ptr ? NULL : filename_ptr,
8961 functionname_ptr);
b34976b6 8962 return TRUE;
d1fad7c6
NC
8963 }
8964
8965 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4e8a9624
AM
8966 &found, filename_ptr,
8967 functionname_ptr, line_ptr,
8968 &elf_tdata (abfd)->line_info))
b34976b6 8969 return FALSE;
dc43ada5 8970 if (found && (*functionname_ptr || *line_ptr))
b34976b6 8971 return TRUE;
d1fad7c6
NC
8972
8973 if (symbols == NULL)
b34976b6 8974 return FALSE;
d1fad7c6 8975
e00e8198
AM
8976 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
8977 filename_ptr, functionname_ptr))
b34976b6 8978 return FALSE;
d1fad7c6 8979
252b5132 8980 *line_ptr = 0;
b34976b6 8981 return TRUE;
252b5132
RH
8982}
8983
5420f73d
L
8984/* Find the line for a symbol. */
8985
8986bfd_boolean
8987_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
8988 const char **filename_ptr, unsigned int *line_ptr)
9b8d1a36 8989{
fb167eb2
AM
8990 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
8991 filename_ptr, NULL, line_ptr, NULL,
8992 dwarf_debug_sections, 0,
8993 &elf_tdata (abfd)->dwarf2_find_line_info);
5420f73d
L
8994}
8995
4ab527b0
FF
8996/* After a call to bfd_find_nearest_line, successive calls to
8997 bfd_find_inliner_info can be used to get source information about
8998 each level of function inlining that terminated at the address
8999 passed to bfd_find_nearest_line. Currently this is only supported
9000 for DWARF2 with appropriate DWARF3 extensions. */
9001
9002bfd_boolean
9003_bfd_elf_find_inliner_info (bfd *abfd,
9004 const char **filename_ptr,
9005 const char **functionname_ptr,
9006 unsigned int *line_ptr)
9007{
9008 bfd_boolean found;
9009 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9010 functionname_ptr, line_ptr,
9011 & elf_tdata (abfd)->dwarf2_find_line_info);
9012 return found;
9013}
9014
252b5132 9015int
a6b96beb 9016_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
252b5132 9017{
8ded5a0f
AM
9018 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9019 int ret = bed->s->sizeof_ehdr;
252b5132 9020
0e1862bb 9021 if (!bfd_link_relocatable (info))
8ded5a0f 9022 {
12bd6957 9023 bfd_size_type phdr_size = elf_program_header_size (abfd);
8ded5a0f 9024
62d7a5f6
AM
9025 if (phdr_size == (bfd_size_type) -1)
9026 {
9027 struct elf_segment_map *m;
9028
9029 phdr_size = 0;
12bd6957 9030 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
62d7a5f6 9031 phdr_size += bed->s->sizeof_phdr;
8ded5a0f 9032
62d7a5f6
AM
9033 if (phdr_size == 0)
9034 phdr_size = get_program_header_size (abfd, info);
9035 }
8ded5a0f 9036
12bd6957 9037 elf_program_header_size (abfd) = phdr_size;
8ded5a0f
AM
9038 ret += phdr_size;
9039 }
9040
252b5132
RH
9041 return ret;
9042}
9043
b34976b6 9044bfd_boolean
217aa764
AM
9045_bfd_elf_set_section_contents (bfd *abfd,
9046 sec_ptr section,
0f867abe 9047 const void *location,
217aa764
AM
9048 file_ptr offset,
9049 bfd_size_type count)
252b5132
RH
9050{
9051 Elf_Internal_Shdr *hdr;
1b6aeedb 9052 file_ptr pos;
252b5132
RH
9053
9054 if (! abfd->output_has_begun
217aa764 9055 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
b34976b6 9056 return FALSE;
252b5132 9057
0ce398f1
L
9058 if (!count)
9059 return TRUE;
9060
252b5132 9061 hdr = &elf_section_data (section)->this_hdr;
0ce398f1
L
9062 if (hdr->sh_offset == (file_ptr) -1)
9063 {
9064 /* We must compress this section. Write output to the buffer. */
9065 unsigned char *contents = hdr->contents;
9066 if ((offset + count) > hdr->sh_size
9067 || (section->flags & SEC_ELF_COMPRESS) == 0
9068 || contents == NULL)
9069 abort ();
9070 memcpy (contents + offset, location, count);
9071 return TRUE;
9072 }
dc810e39
AM
9073 pos = hdr->sh_offset + offset;
9074 if (bfd_seek (abfd, pos, SEEK_SET) != 0
9075 || bfd_bwrite (location, count, abfd) != count)
b34976b6 9076 return FALSE;
252b5132 9077
b34976b6 9078 return TRUE;
252b5132
RH
9079}
9080
f3185997 9081bfd_boolean
217aa764
AM
9082_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
9083 arelent *cache_ptr ATTRIBUTE_UNUSED,
9084 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
252b5132
RH
9085{
9086 abort ();
f3185997 9087 return FALSE;
252b5132
RH
9088}
9089
252b5132
RH
9090/* Try to convert a non-ELF reloc into an ELF one. */
9091
b34976b6 9092bfd_boolean
217aa764 9093_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
252b5132 9094{
c044fabd 9095 /* Check whether we really have an ELF howto. */
252b5132
RH
9096
9097 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
9098 {
9099 bfd_reloc_code_real_type code;
9100 reloc_howto_type *howto;
9101
9102 /* Alien reloc: Try to determine its type to replace it with an
c044fabd 9103 equivalent ELF reloc. */
252b5132
RH
9104
9105 if (areloc->howto->pc_relative)
9106 {
9107 switch (areloc->howto->bitsize)
9108 {
9109 case 8:
9110 code = BFD_RELOC_8_PCREL;
9111 break;
9112 case 12:
9113 code = BFD_RELOC_12_PCREL;
9114 break;
9115 case 16:
9116 code = BFD_RELOC_16_PCREL;
9117 break;
9118 case 24:
9119 code = BFD_RELOC_24_PCREL;
9120 break;
9121 case 32:
9122 code = BFD_RELOC_32_PCREL;
9123 break;
9124 case 64:
9125 code = BFD_RELOC_64_PCREL;
9126 break;
9127 default:
9128 goto fail;
9129 }
9130
9131 howto = bfd_reloc_type_lookup (abfd, code);
9132
9133 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
9134 {
9135 if (howto->pcrel_offset)
9136 areloc->addend += areloc->address;
9137 else
9138 areloc->addend -= areloc->address; /* addend is unsigned!! */
9139 }
9140 }
9141 else
9142 {
9143 switch (areloc->howto->bitsize)
9144 {
9145 case 8:
9146 code = BFD_RELOC_8;
9147 break;
9148 case 14:
9149 code = BFD_RELOC_14;
9150 break;
9151 case 16:
9152 code = BFD_RELOC_16;
9153 break;
9154 case 26:
9155 code = BFD_RELOC_26;
9156 break;
9157 case 32:
9158 code = BFD_RELOC_32;
9159 break;
9160 case 64:
9161 code = BFD_RELOC_64;
9162 break;
9163 default:
9164 goto fail;
9165 }
9166
9167 howto = bfd_reloc_type_lookup (abfd, code);
9168 }
9169
9170 if (howto)
9171 areloc->howto = howto;
9172 else
9173 goto fail;
9174 }
9175
b34976b6 9176 return TRUE;
252b5132
RH
9177
9178 fail:
0aa13fee
AM
9179 /* xgettext:c-format */
9180 _bfd_error_handler (_("%pB: %s unsupported"),
9181 abfd, areloc->howto->name);
252b5132 9182 bfd_set_error (bfd_error_bad_value);
b34976b6 9183 return FALSE;
252b5132
RH
9184}
9185
b34976b6 9186bfd_boolean
217aa764 9187_bfd_elf_close_and_cleanup (bfd *abfd)
252b5132 9188{
d9071b0c
TG
9189 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9190 if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
252b5132 9191 {
c0355132 9192 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
2b0f7ef9 9193 _bfd_elf_strtab_free (elf_shstrtab (abfd));
d9071b0c 9194 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
252b5132
RH
9195 }
9196
9197 return _bfd_generic_close_and_cleanup (abfd);
9198}
9199
9200/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9201 in the relocation's offset. Thus we cannot allow any sort of sanity
9202 range-checking to interfere. There is nothing else to do in processing
9203 this reloc. */
9204
9205bfd_reloc_status_type
217aa764
AM
9206_bfd_elf_rel_vtable_reloc_fn
9207 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
fc0a2244 9208 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
217aa764
AM
9209 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9210 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
252b5132
RH
9211{
9212 return bfd_reloc_ok;
9213}
252b5132
RH
9214\f
9215/* Elf core file support. Much of this only works on native
9216 toolchains, since we rely on knowing the
9217 machine-dependent procfs structure in order to pick
c044fabd 9218 out details about the corefile. */
252b5132
RH
9219
9220#ifdef HAVE_SYS_PROCFS_H
16231b7b
DG
9221/* Needed for new procfs interface on sparc-solaris. */
9222# define _STRUCTURED_PROC 1
252b5132
RH
9223# include <sys/procfs.h>
9224#endif
9225
261b8d08
PA
9226/* Return a PID that identifies a "thread" for threaded cores, or the
9227 PID of the main process for non-threaded cores. */
252b5132
RH
9228
9229static int
217aa764 9230elfcore_make_pid (bfd *abfd)
252b5132 9231{
261b8d08
PA
9232 int pid;
9233
228e534f 9234 pid = elf_tdata (abfd)->core->lwpid;
261b8d08 9235 if (pid == 0)
228e534f 9236 pid = elf_tdata (abfd)->core->pid;
261b8d08
PA
9237
9238 return pid;
252b5132
RH
9239}
9240
252b5132
RH
9241/* If there isn't a section called NAME, make one, using
9242 data from SECT. Note, this function will generate a
9243 reference to NAME, so you shouldn't deallocate or
c044fabd 9244 overwrite it. */
252b5132 9245
b34976b6 9246static bfd_boolean
217aa764 9247elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
252b5132 9248{
c044fabd 9249 asection *sect2;
252b5132
RH
9250
9251 if (bfd_get_section_by_name (abfd, name) != NULL)
b34976b6 9252 return TRUE;
252b5132 9253
117ed4f8 9254 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
252b5132 9255 if (sect2 == NULL)
b34976b6 9256 return FALSE;
252b5132 9257
eea6121a 9258 sect2->size = sect->size;
252b5132 9259 sect2->filepos = sect->filepos;
252b5132 9260 sect2->alignment_power = sect->alignment_power;
b34976b6 9261 return TRUE;
252b5132
RH
9262}
9263
bb0082d6
AM
9264/* Create a pseudosection containing SIZE bytes at FILEPOS. This
9265 actually creates up to two pseudosections:
9266 - For the single-threaded case, a section named NAME, unless
9267 such a section already exists.
9268 - For the multi-threaded case, a section named "NAME/PID", where
9269 PID is elfcore_make_pid (abfd).
24d3e51b 9270 Both pseudosections have identical contents. */
b34976b6 9271bfd_boolean
217aa764
AM
9272_bfd_elfcore_make_pseudosection (bfd *abfd,
9273 char *name,
9274 size_t size,
9275 ufile_ptr filepos)
bb0082d6
AM
9276{
9277 char buf[100];
9278 char *threaded_name;
d4c88bbb 9279 size_t len;
bb0082d6
AM
9280 asection *sect;
9281
9282 /* Build the section name. */
9283
9284 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
d4c88bbb 9285 len = strlen (buf) + 1;
a50b1753 9286 threaded_name = (char *) bfd_alloc (abfd, len);
bb0082d6 9287 if (threaded_name == NULL)
b34976b6 9288 return FALSE;
d4c88bbb 9289 memcpy (threaded_name, buf, len);
bb0082d6 9290
117ed4f8
AM
9291 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9292 SEC_HAS_CONTENTS);
bb0082d6 9293 if (sect == NULL)
b34976b6 9294 return FALSE;
eea6121a 9295 sect->size = size;
bb0082d6 9296 sect->filepos = filepos;
bb0082d6
AM
9297 sect->alignment_power = 2;
9298
936e320b 9299 return elfcore_maybe_make_sect (abfd, name, sect);
bb0082d6
AM
9300}
9301
58e07198
CZ
9302static bfd_boolean
9303elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
9304 size_t offs)
9305{
9306 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9307 SEC_HAS_CONTENTS);
9308
9309 if (sect == NULL)
9310 return FALSE;
9311
9312 sect->size = note->descsz - offs;
9313 sect->filepos = note->descpos + offs;
9314 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9315
9316 return TRUE;
9317}
9318
252b5132 9319/* prstatus_t exists on:
4a938328 9320 solaris 2.5+
252b5132
RH
9321 linux 2.[01] + glibc
9322 unixware 4.2
9323*/
9324
9325#if defined (HAVE_PRSTATUS_T)
a7b97311 9326
b34976b6 9327static bfd_boolean
217aa764 9328elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 9329{
eea6121a 9330 size_t size;
7ee38065 9331 int offset;
252b5132 9332
4a938328
MS
9333 if (note->descsz == sizeof (prstatus_t))
9334 {
9335 prstatus_t prstat;
252b5132 9336
eea6121a 9337 size = sizeof (prstat.pr_reg);
7ee38065 9338 offset = offsetof (prstatus_t, pr_reg);
4a938328 9339 memcpy (&prstat, note->descdata, sizeof (prstat));
252b5132 9340
fa49d224
NC
9341 /* Do not overwrite the core signal if it
9342 has already been set by another thread. */
228e534f
AM
9343 if (elf_tdata (abfd)->core->signal == 0)
9344 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9345 if (elf_tdata (abfd)->core->pid == 0)
9346 elf_tdata (abfd)->core->pid = prstat.pr_pid;
252b5132 9347
4a938328
MS
9348 /* pr_who exists on:
9349 solaris 2.5+
9350 unixware 4.2
9351 pr_who doesn't exist on:
9352 linux 2.[01]
9353 */
252b5132 9354#if defined (HAVE_PRSTATUS_T_PR_WHO)
228e534f 9355 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9356#else
228e534f 9357 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
252b5132 9358#endif
4a938328 9359 }
7ee38065 9360#if defined (HAVE_PRSTATUS32_T)
4a938328
MS
9361 else if (note->descsz == sizeof (prstatus32_t))
9362 {
9363 /* 64-bit host, 32-bit corefile */
9364 prstatus32_t prstat;
9365
eea6121a 9366 size = sizeof (prstat.pr_reg);
7ee38065 9367 offset = offsetof (prstatus32_t, pr_reg);
4a938328
MS
9368 memcpy (&prstat, note->descdata, sizeof (prstat));
9369
fa49d224
NC
9370 /* Do not overwrite the core signal if it
9371 has already been set by another thread. */
228e534f
AM
9372 if (elf_tdata (abfd)->core->signal == 0)
9373 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9374 if (elf_tdata (abfd)->core->pid == 0)
9375 elf_tdata (abfd)->core->pid = prstat.pr_pid;
4a938328
MS
9376
9377 /* pr_who exists on:
9378 solaris 2.5+
9379 unixware 4.2
9380 pr_who doesn't exist on:
9381 linux 2.[01]
9382 */
7ee38065 9383#if defined (HAVE_PRSTATUS32_T_PR_WHO)
228e534f 9384 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9385#else
228e534f 9386 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
4a938328
MS
9387#endif
9388 }
7ee38065 9389#endif /* HAVE_PRSTATUS32_T */
4a938328
MS
9390 else
9391 {
9392 /* Fail - we don't know how to handle any other
9393 note size (ie. data object type). */
b34976b6 9394 return TRUE;
4a938328 9395 }
252b5132 9396
bb0082d6 9397 /* Make a ".reg/999" section and a ".reg" section. */
936e320b 9398 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 9399 size, note->descpos + offset);
252b5132
RH
9400}
9401#endif /* defined (HAVE_PRSTATUS_T) */
9402
bb0082d6 9403/* Create a pseudosection containing the exact contents of NOTE. */
b34976b6 9404static bfd_boolean
217aa764
AM
9405elfcore_make_note_pseudosection (bfd *abfd,
9406 char *name,
9407 Elf_Internal_Note *note)
252b5132 9408{
936e320b
AM
9409 return _bfd_elfcore_make_pseudosection (abfd, name,
9410 note->descsz, note->descpos);
252b5132
RH
9411}
9412
ff08c6bb
JB
9413/* There isn't a consistent prfpregset_t across platforms,
9414 but it doesn't matter, because we don't have to pick this
c044fabd
KH
9415 data structure apart. */
9416
b34976b6 9417static bfd_boolean
217aa764 9418elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9419{
9420 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9421}
9422
ff08c6bb 9423/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
971d4640 9424 type of NT_PRXFPREG. Just include the whole note's contents
ff08c6bb 9425 literally. */
c044fabd 9426
b34976b6 9427static bfd_boolean
217aa764 9428elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9429{
9430 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9431}
9432
4339cae0
L
9433/* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9434 with a note type of NT_X86_XSTATE. Just include the whole note's
9435 contents literally. */
9436
9437static bfd_boolean
9438elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9439{
9440 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9441}
9442
97753bd5
AM
9443static bfd_boolean
9444elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9445{
9446 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9447}
9448
89eeb0bc
LM
9449static bfd_boolean
9450elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9451{
9452 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9453}
97753bd5 9454
cb2366c1
EBM
9455static bfd_boolean
9456elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9457{
9458 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9459}
9460
9461static bfd_boolean
9462elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9463{
9464 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9465}
9466
9467static bfd_boolean
9468elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9469{
9470 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9471}
9472
9473static bfd_boolean
9474elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9475{
9476 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9477}
9478
9479static bfd_boolean
9480elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9481{
9482 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9483}
9484
9485static bfd_boolean
9486elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9487{
9488 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9489}
9490
9491static bfd_boolean
9492elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
9493{
9494 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
9495}
9496
9497static bfd_boolean
9498elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
9499{
9500 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
9501}
9502
9503static bfd_boolean
9504elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
9505{
9506 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
9507}
9508
9509static bfd_boolean
9510elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
9511{
9512 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
9513}
9514
9515static bfd_boolean
9516elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
9517{
9518 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
9519}
9520
9521static bfd_boolean
9522elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
9523{
9524 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
9525}
9526
9527static bfd_boolean
9528elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
9529{
9530 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
9531}
9532
0675e188
UW
9533static bfd_boolean
9534elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9535{
9536 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9537}
9538
d7eeb400
MS
9539static bfd_boolean
9540elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9541{
9542 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9543}
9544
9545static bfd_boolean
9546elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9547{
9548 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9549}
9550
9551static bfd_boolean
9552elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
9553{
9554 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
9555}
9556
9557static bfd_boolean
9558elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
9559{
9560 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
9561}
9562
9563static bfd_boolean
9564elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
9565{
9566 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
9567}
9568
355b81d9
UW
9569static bfd_boolean
9570elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
9571{
9572 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
9573}
9574
9575static bfd_boolean
9576elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
9577{
9578 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
9579}
9580
abb3f6cc
NC
9581static bfd_boolean
9582elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
9583{
9584 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
9585}
9586
4ef9f41a
AA
9587static bfd_boolean
9588elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
9589{
9590 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
9591}
9592
9593static bfd_boolean
9594elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
9595{
9596 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
9597}
9598
88ab90e8
AA
9599static bfd_boolean
9600elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
9601{
9602 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
9603}
9604
9605static bfd_boolean
9606elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
9607{
9608 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
9609}
9610
faa9a424
UW
9611static bfd_boolean
9612elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
9613{
9614 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
9615}
9616
652451f8
YZ
9617static bfd_boolean
9618elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
9619{
9620 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
9621}
9622
9623static bfd_boolean
9624elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
9625{
9626 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
9627}
9628
9629static bfd_boolean
9630elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
9631{
9632 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
9633}
9634
ad1cc4e4
AH
9635static bfd_boolean
9636elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
9637{
9638 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
9639}
9640
e6c3b5bf
AH
9641static bfd_boolean
9642elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
9643{
9644 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
9645}
9646
252b5132 9647#if defined (HAVE_PRPSINFO_T)
4a938328 9648typedef prpsinfo_t elfcore_psinfo_t;
7ee38065 9649#if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
9650typedef prpsinfo32_t elfcore_psinfo32_t;
9651#endif
252b5132
RH
9652#endif
9653
9654#if defined (HAVE_PSINFO_T)
4a938328 9655typedef psinfo_t elfcore_psinfo_t;
7ee38065 9656#if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
9657typedef psinfo32_t elfcore_psinfo32_t;
9658#endif
252b5132
RH
9659#endif
9660
252b5132
RH
9661/* return a malloc'ed copy of a string at START which is at
9662 most MAX bytes long, possibly without a terminating '\0'.
c044fabd 9663 the copy will always have a terminating '\0'. */
252b5132 9664
936e320b 9665char *
217aa764 9666_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
252b5132 9667{
dc810e39 9668 char *dups;
a50b1753 9669 char *end = (char *) memchr (start, '\0', max);
dc810e39 9670 size_t len;
252b5132
RH
9671
9672 if (end == NULL)
9673 len = max;
9674 else
9675 len = end - start;
9676
a50b1753 9677 dups = (char *) bfd_alloc (abfd, len + 1);
dc810e39 9678 if (dups == NULL)
252b5132
RH
9679 return NULL;
9680
dc810e39
AM
9681 memcpy (dups, start, len);
9682 dups[len] = '\0';
252b5132 9683
dc810e39 9684 return dups;
252b5132
RH
9685}
9686
bb0082d6 9687#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
b34976b6 9688static bfd_boolean
217aa764 9689elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
252b5132 9690{
4a938328
MS
9691 if (note->descsz == sizeof (elfcore_psinfo_t))
9692 {
9693 elfcore_psinfo_t psinfo;
252b5132 9694
7ee38065 9695 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 9696
335e41d4 9697#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
228e534f 9698 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 9699#endif
228e534f 9700 elf_tdata (abfd)->core->program
936e320b
AM
9701 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9702 sizeof (psinfo.pr_fname));
252b5132 9703
228e534f 9704 elf_tdata (abfd)->core->command
936e320b
AM
9705 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9706 sizeof (psinfo.pr_psargs));
4a938328 9707 }
7ee38065 9708#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
4a938328
MS
9709 else if (note->descsz == sizeof (elfcore_psinfo32_t))
9710 {
9711 /* 64-bit host, 32-bit corefile */
9712 elfcore_psinfo32_t psinfo;
9713
7ee38065 9714 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 9715
335e41d4 9716#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
228e534f 9717 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 9718#endif
228e534f 9719 elf_tdata (abfd)->core->program
936e320b
AM
9720 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9721 sizeof (psinfo.pr_fname));
4a938328 9722
228e534f 9723 elf_tdata (abfd)->core->command
936e320b
AM
9724 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9725 sizeof (psinfo.pr_psargs));
4a938328
MS
9726 }
9727#endif
9728
9729 else
9730 {
9731 /* Fail - we don't know how to handle any other
9732 note size (ie. data object type). */
b34976b6 9733 return TRUE;
4a938328 9734 }
252b5132
RH
9735
9736 /* Note that for some reason, a spurious space is tacked
9737 onto the end of the args in some (at least one anyway)
c044fabd 9738 implementations, so strip it off if it exists. */
252b5132
RH
9739
9740 {
228e534f 9741 char *command = elf_tdata (abfd)->core->command;
252b5132
RH
9742 int n = strlen (command);
9743
9744 if (0 < n && command[n - 1] == ' ')
9745 command[n - 1] = '\0';
9746 }
9747
b34976b6 9748 return TRUE;
252b5132
RH
9749}
9750#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9751
252b5132 9752#if defined (HAVE_PSTATUS_T)
b34976b6 9753static bfd_boolean
217aa764 9754elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 9755{
f572a39d
AM
9756 if (note->descsz == sizeof (pstatus_t)
9757#if defined (HAVE_PXSTATUS_T)
9758 || note->descsz == sizeof (pxstatus_t)
9759#endif
9760 )
4a938328
MS
9761 {
9762 pstatus_t pstat;
252b5132 9763
4a938328 9764 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 9765
228e534f 9766 elf_tdata (abfd)->core->pid = pstat.pr_pid;
4a938328 9767 }
7ee38065 9768#if defined (HAVE_PSTATUS32_T)
4a938328
MS
9769 else if (note->descsz == sizeof (pstatus32_t))
9770 {
9771 /* 64-bit host, 32-bit corefile */
9772 pstatus32_t pstat;
252b5132 9773
4a938328 9774 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 9775
228e534f 9776 elf_tdata (abfd)->core->pid = pstat.pr_pid;
4a938328
MS
9777 }
9778#endif
252b5132
RH
9779 /* Could grab some more details from the "representative"
9780 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
c044fabd 9781 NT_LWPSTATUS note, presumably. */
252b5132 9782
b34976b6 9783 return TRUE;
252b5132
RH
9784}
9785#endif /* defined (HAVE_PSTATUS_T) */
9786
252b5132 9787#if defined (HAVE_LWPSTATUS_T)
b34976b6 9788static bfd_boolean
217aa764 9789elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132
RH
9790{
9791 lwpstatus_t lwpstat;
9792 char buf[100];
c044fabd 9793 char *name;
d4c88bbb 9794 size_t len;
c044fabd 9795 asection *sect;
252b5132 9796
f572a39d
AM
9797 if (note->descsz != sizeof (lwpstat)
9798#if defined (HAVE_LWPXSTATUS_T)
9799 && note->descsz != sizeof (lwpxstatus_t)
9800#endif
9801 )
b34976b6 9802 return TRUE;
252b5132
RH
9803
9804 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
9805
228e534f 9806 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
a1504221
JB
9807 /* Do not overwrite the core signal if it has already been set by
9808 another thread. */
228e534f
AM
9809 if (elf_tdata (abfd)->core->signal == 0)
9810 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
252b5132 9811
c044fabd 9812 /* Make a ".reg/999" section. */
252b5132
RH
9813
9814 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
d4c88bbb 9815 len = strlen (buf) + 1;
217aa764 9816 name = bfd_alloc (abfd, len);
252b5132 9817 if (name == NULL)
b34976b6 9818 return FALSE;
d4c88bbb 9819 memcpy (name, buf, len);
252b5132 9820
117ed4f8 9821 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 9822 if (sect == NULL)
b34976b6 9823 return FALSE;
252b5132
RH
9824
9825#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 9826 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
252b5132
RH
9827 sect->filepos = note->descpos
9828 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
9829#endif
9830
9831#if defined (HAVE_LWPSTATUS_T_PR_REG)
eea6121a 9832 sect->size = sizeof (lwpstat.pr_reg);
252b5132
RH
9833 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
9834#endif
9835
252b5132
RH
9836 sect->alignment_power = 2;
9837
9838 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
b34976b6 9839 return FALSE;
252b5132
RH
9840
9841 /* Make a ".reg2/999" section */
9842
9843 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
d4c88bbb 9844 len = strlen (buf) + 1;
217aa764 9845 name = bfd_alloc (abfd, len);
252b5132 9846 if (name == NULL)
b34976b6 9847 return FALSE;
d4c88bbb 9848 memcpy (name, buf, len);
252b5132 9849
117ed4f8 9850 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 9851 if (sect == NULL)
b34976b6 9852 return FALSE;
252b5132
RH
9853
9854#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 9855 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
252b5132
RH
9856 sect->filepos = note->descpos
9857 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
9858#endif
9859
9860#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
eea6121a 9861 sect->size = sizeof (lwpstat.pr_fpreg);
252b5132
RH
9862 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
9863#endif
9864
252b5132
RH
9865 sect->alignment_power = 2;
9866
936e320b 9867 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
252b5132
RH
9868}
9869#endif /* defined (HAVE_LWPSTATUS_T) */
9870
b34976b6 9871static bfd_boolean
217aa764 9872elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
16e9c715
NC
9873{
9874 char buf[30];
c044fabd 9875 char *name;
d4c88bbb 9876 size_t len;
c044fabd 9877 asection *sect;
4a6636fb
PA
9878 int type;
9879 int is_active_thread;
9880 bfd_vma base_addr;
16e9c715 9881
4a6636fb 9882 if (note->descsz < 728)
b34976b6 9883 return TRUE;
16e9c715 9884
4a6636fb
PA
9885 if (! CONST_STRNEQ (note->namedata, "win32"))
9886 return TRUE;
9887
9888 type = bfd_get_32 (abfd, note->descdata);
c044fabd 9889
4a6636fb 9890 switch (type)
16e9c715 9891 {
4a6636fb 9892 case 1 /* NOTE_INFO_PROCESS */:
228e534f 9893 /* FIXME: need to add ->core->command. */
4a6636fb 9894 /* process_info.pid */
228e534f 9895 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
4a6636fb 9896 /* process_info.signal */
228e534f 9897 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
c044fabd 9898 break;
16e9c715 9899
4a6636fb 9900 case 2 /* NOTE_INFO_THREAD */:
16e9c715 9901 /* Make a ".reg/999" section. */
4a6636fb
PA
9902 /* thread_info.tid */
9903 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
c044fabd 9904
d4c88bbb 9905 len = strlen (buf) + 1;
a50b1753 9906 name = (char *) bfd_alloc (abfd, len);
16e9c715 9907 if (name == NULL)
b34976b6 9908 return FALSE;
c044fabd 9909
d4c88bbb 9910 memcpy (name, buf, len);
16e9c715 9911
117ed4f8 9912 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
16e9c715 9913 if (sect == NULL)
b34976b6 9914 return FALSE;
c044fabd 9915
4a6636fb
PA
9916 /* sizeof (thread_info.thread_context) */
9917 sect->size = 716;
9918 /* offsetof (thread_info.thread_context) */
9919 sect->filepos = note->descpos + 12;
16e9c715
NC
9920 sect->alignment_power = 2;
9921
4a6636fb
PA
9922 /* thread_info.is_active_thread */
9923 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
9924
9925 if (is_active_thread)
16e9c715 9926 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
b34976b6 9927 return FALSE;
16e9c715
NC
9928 break;
9929
4a6636fb 9930 case 3 /* NOTE_INFO_MODULE */:
16e9c715 9931 /* Make a ".module/xxxxxxxx" section. */
4a6636fb
PA
9932 /* module_info.base_address */
9933 base_addr = bfd_get_32 (abfd, note->descdata + 4);
0af1713e 9934 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
c044fabd 9935
d4c88bbb 9936 len = strlen (buf) + 1;
a50b1753 9937 name = (char *) bfd_alloc (abfd, len);
16e9c715 9938 if (name == NULL)
b34976b6 9939 return FALSE;
c044fabd 9940
d4c88bbb 9941 memcpy (name, buf, len);
252b5132 9942
117ed4f8 9943 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
c044fabd 9944
16e9c715 9945 if (sect == NULL)
b34976b6 9946 return FALSE;
c044fabd 9947
eea6121a 9948 sect->size = note->descsz;
16e9c715 9949 sect->filepos = note->descpos;
16e9c715
NC
9950 sect->alignment_power = 2;
9951 break;
9952
9953 default:
b34976b6 9954 return TRUE;
16e9c715
NC
9955 }
9956
b34976b6 9957 return TRUE;
16e9c715 9958}
252b5132 9959
b34976b6 9960static bfd_boolean
217aa764 9961elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
252b5132 9962{
9c5bfbb7 9963 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
bb0082d6 9964
252b5132
RH
9965 switch (note->type)
9966 {
9967 default:
b34976b6 9968 return TRUE;
252b5132 9969
252b5132 9970 case NT_PRSTATUS:
bb0082d6
AM
9971 if (bed->elf_backend_grok_prstatus)
9972 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
b34976b6 9973 return TRUE;
bb0082d6 9974#if defined (HAVE_PRSTATUS_T)
252b5132 9975 return elfcore_grok_prstatus (abfd, note);
bb0082d6 9976#else
b34976b6 9977 return TRUE;
252b5132
RH
9978#endif
9979
9980#if defined (HAVE_PSTATUS_T)
9981 case NT_PSTATUS:
9982 return elfcore_grok_pstatus (abfd, note);
9983#endif
9984
9985#if defined (HAVE_LWPSTATUS_T)
9986 case NT_LWPSTATUS:
9987 return elfcore_grok_lwpstatus (abfd, note);
9988#endif
9989
9990 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
9991 return elfcore_grok_prfpreg (abfd, note);
9992
c044fabd 9993 case NT_WIN32PSTATUS:
16e9c715 9994 return elfcore_grok_win32pstatus (abfd, note);
16e9c715 9995
c044fabd 9996 case NT_PRXFPREG: /* Linux SSE extension */
e377ab71
MK
9997 if (note->namesz == 6
9998 && strcmp (note->namedata, "LINUX") == 0)
ff08c6bb
JB
9999 return elfcore_grok_prxfpreg (abfd, note);
10000 else
b34976b6 10001 return TRUE;
ff08c6bb 10002
4339cae0
L
10003 case NT_X86_XSTATE: /* Linux XSAVE extension */
10004 if (note->namesz == 6
10005 && strcmp (note->namedata, "LINUX") == 0)
10006 return elfcore_grok_xstatereg (abfd, note);
10007 else
10008 return TRUE;
10009
97753bd5
AM
10010 case NT_PPC_VMX:
10011 if (note->namesz == 6
10012 && strcmp (note->namedata, "LINUX") == 0)
10013 return elfcore_grok_ppc_vmx (abfd, note);
10014 else
10015 return TRUE;
10016
89eeb0bc
LM
10017 case NT_PPC_VSX:
10018 if (note->namesz == 6
07d6d2b8
AM
10019 && strcmp (note->namedata, "LINUX") == 0)
10020 return elfcore_grok_ppc_vsx (abfd, note);
89eeb0bc 10021 else
07d6d2b8 10022 return TRUE;
89eeb0bc 10023
cb2366c1
EBM
10024 case NT_PPC_TAR:
10025 if (note->namesz == 6
4b24dd1a
AM
10026 && strcmp (note->namedata, "LINUX") == 0)
10027 return elfcore_grok_ppc_tar (abfd, note);
cb2366c1 10028 else
4b24dd1a 10029 return TRUE;
cb2366c1
EBM
10030
10031 case NT_PPC_PPR:
10032 if (note->namesz == 6
4b24dd1a
AM
10033 && strcmp (note->namedata, "LINUX") == 0)
10034 return elfcore_grok_ppc_ppr (abfd, note);
cb2366c1 10035 else
4b24dd1a 10036 return TRUE;
cb2366c1
EBM
10037
10038 case NT_PPC_DSCR:
10039 if (note->namesz == 6
4b24dd1a
AM
10040 && strcmp (note->namedata, "LINUX") == 0)
10041 return elfcore_grok_ppc_dscr (abfd, note);
cb2366c1 10042 else
4b24dd1a 10043 return TRUE;
cb2366c1
EBM
10044
10045 case NT_PPC_EBB:
10046 if (note->namesz == 6
4b24dd1a
AM
10047 && strcmp (note->namedata, "LINUX") == 0)
10048 return elfcore_grok_ppc_ebb (abfd, note);
cb2366c1 10049 else
4b24dd1a 10050 return TRUE;
cb2366c1
EBM
10051
10052 case NT_PPC_PMU:
10053 if (note->namesz == 6
4b24dd1a
AM
10054 && strcmp (note->namedata, "LINUX") == 0)
10055 return elfcore_grok_ppc_pmu (abfd, note);
cb2366c1 10056 else
4b24dd1a 10057 return TRUE;
cb2366c1
EBM
10058
10059 case NT_PPC_TM_CGPR:
10060 if (note->namesz == 6
4b24dd1a
AM
10061 && strcmp (note->namedata, "LINUX") == 0)
10062 return elfcore_grok_ppc_tm_cgpr (abfd, note);
cb2366c1 10063 else
4b24dd1a 10064 return TRUE;
cb2366c1
EBM
10065
10066 case NT_PPC_TM_CFPR:
10067 if (note->namesz == 6
4b24dd1a
AM
10068 && strcmp (note->namedata, "LINUX") == 0)
10069 return elfcore_grok_ppc_tm_cfpr (abfd, note);
cb2366c1 10070 else
4b24dd1a 10071 return TRUE;
cb2366c1
EBM
10072
10073 case NT_PPC_TM_CVMX:
10074 if (note->namesz == 6
4b24dd1a
AM
10075 && strcmp (note->namedata, "LINUX") == 0)
10076 return elfcore_grok_ppc_tm_cvmx (abfd, note);
cb2366c1 10077 else
4b24dd1a 10078 return TRUE;
cb2366c1
EBM
10079
10080 case NT_PPC_TM_CVSX:
10081 if (note->namesz == 6
4b24dd1a
AM
10082 && strcmp (note->namedata, "LINUX") == 0)
10083 return elfcore_grok_ppc_tm_cvsx (abfd, note);
cb2366c1 10084 else
4b24dd1a 10085 return TRUE;
cb2366c1
EBM
10086
10087 case NT_PPC_TM_SPR:
10088 if (note->namesz == 6
4b24dd1a
AM
10089 && strcmp (note->namedata, "LINUX") == 0)
10090 return elfcore_grok_ppc_tm_spr (abfd, note);
cb2366c1 10091 else
4b24dd1a 10092 return TRUE;
cb2366c1
EBM
10093
10094 case NT_PPC_TM_CTAR:
10095 if (note->namesz == 6
4b24dd1a
AM
10096 && strcmp (note->namedata, "LINUX") == 0)
10097 return elfcore_grok_ppc_tm_ctar (abfd, note);
cb2366c1 10098 else
4b24dd1a 10099 return TRUE;
cb2366c1
EBM
10100
10101 case NT_PPC_TM_CPPR:
10102 if (note->namesz == 6
4b24dd1a
AM
10103 && strcmp (note->namedata, "LINUX") == 0)
10104 return elfcore_grok_ppc_tm_cppr (abfd, note);
cb2366c1 10105 else
4b24dd1a 10106 return TRUE;
cb2366c1
EBM
10107
10108 case NT_PPC_TM_CDSCR:
10109 if (note->namesz == 6
4b24dd1a
AM
10110 && strcmp (note->namedata, "LINUX") == 0)
10111 return elfcore_grok_ppc_tm_cdscr (abfd, note);
cb2366c1 10112 else
4b24dd1a 10113 return TRUE;
cb2366c1 10114
0675e188
UW
10115 case NT_S390_HIGH_GPRS:
10116 if (note->namesz == 6
07d6d2b8
AM
10117 && strcmp (note->namedata, "LINUX") == 0)
10118 return elfcore_grok_s390_high_gprs (abfd, note);
0675e188 10119 else
07d6d2b8 10120 return TRUE;
0675e188 10121
d7eeb400
MS
10122 case NT_S390_TIMER:
10123 if (note->namesz == 6
07d6d2b8
AM
10124 && strcmp (note->namedata, "LINUX") == 0)
10125 return elfcore_grok_s390_timer (abfd, note);
d7eeb400 10126 else
07d6d2b8 10127 return TRUE;
d7eeb400
MS
10128
10129 case NT_S390_TODCMP:
10130 if (note->namesz == 6
07d6d2b8
AM
10131 && strcmp (note->namedata, "LINUX") == 0)
10132 return elfcore_grok_s390_todcmp (abfd, note);
d7eeb400 10133 else
07d6d2b8 10134 return TRUE;
d7eeb400
MS
10135
10136 case NT_S390_TODPREG:
10137 if (note->namesz == 6
07d6d2b8
AM
10138 && strcmp (note->namedata, "LINUX") == 0)
10139 return elfcore_grok_s390_todpreg (abfd, note);
d7eeb400 10140 else
07d6d2b8 10141 return TRUE;
d7eeb400
MS
10142
10143 case NT_S390_CTRS:
10144 if (note->namesz == 6
07d6d2b8
AM
10145 && strcmp (note->namedata, "LINUX") == 0)
10146 return elfcore_grok_s390_ctrs (abfd, note);
d7eeb400 10147 else
07d6d2b8 10148 return TRUE;
d7eeb400
MS
10149
10150 case NT_S390_PREFIX:
10151 if (note->namesz == 6
07d6d2b8
AM
10152 && strcmp (note->namedata, "LINUX") == 0)
10153 return elfcore_grok_s390_prefix (abfd, note);
d7eeb400 10154 else
07d6d2b8 10155 return TRUE;
d7eeb400 10156
355b81d9
UW
10157 case NT_S390_LAST_BREAK:
10158 if (note->namesz == 6
07d6d2b8
AM
10159 && strcmp (note->namedata, "LINUX") == 0)
10160 return elfcore_grok_s390_last_break (abfd, note);
355b81d9 10161 else
07d6d2b8 10162 return TRUE;
355b81d9
UW
10163
10164 case NT_S390_SYSTEM_CALL:
10165 if (note->namesz == 6
07d6d2b8
AM
10166 && strcmp (note->namedata, "LINUX") == 0)
10167 return elfcore_grok_s390_system_call (abfd, note);
355b81d9 10168 else
07d6d2b8 10169 return TRUE;
355b81d9 10170
abb3f6cc
NC
10171 case NT_S390_TDB:
10172 if (note->namesz == 6
07d6d2b8
AM
10173 && strcmp (note->namedata, "LINUX") == 0)
10174 return elfcore_grok_s390_tdb (abfd, note);
abb3f6cc 10175 else
07d6d2b8 10176 return TRUE;
abb3f6cc 10177
4ef9f41a
AA
10178 case NT_S390_VXRS_LOW:
10179 if (note->namesz == 6
10180 && strcmp (note->namedata, "LINUX") == 0)
10181 return elfcore_grok_s390_vxrs_low (abfd, note);
10182 else
10183 return TRUE;
10184
10185 case NT_S390_VXRS_HIGH:
10186 if (note->namesz == 6
10187 && strcmp (note->namedata, "LINUX") == 0)
10188 return elfcore_grok_s390_vxrs_high (abfd, note);
10189 else
10190 return TRUE;
10191
88ab90e8
AA
10192 case NT_S390_GS_CB:
10193 if (note->namesz == 6
10194 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 10195 return elfcore_grok_s390_gs_cb (abfd, note);
88ab90e8
AA
10196 else
10197 return TRUE;
10198
10199 case NT_S390_GS_BC:
10200 if (note->namesz == 6
10201 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 10202 return elfcore_grok_s390_gs_bc (abfd, note);
88ab90e8
AA
10203 else
10204 return TRUE;
10205
faa9a424
UW
10206 case NT_ARM_VFP:
10207 if (note->namesz == 6
10208 && strcmp (note->namedata, "LINUX") == 0)
10209 return elfcore_grok_arm_vfp (abfd, note);
10210 else
10211 return TRUE;
10212
652451f8
YZ
10213 case NT_ARM_TLS:
10214 if (note->namesz == 6
10215 && strcmp (note->namedata, "LINUX") == 0)
10216 return elfcore_grok_aarch_tls (abfd, note);
10217 else
10218 return TRUE;
10219
10220 case NT_ARM_HW_BREAK:
10221 if (note->namesz == 6
10222 && strcmp (note->namedata, "LINUX") == 0)
10223 return elfcore_grok_aarch_hw_break (abfd, note);
10224 else
10225 return TRUE;
10226
10227 case NT_ARM_HW_WATCH:
10228 if (note->namesz == 6
10229 && strcmp (note->namedata, "LINUX") == 0)
10230 return elfcore_grok_aarch_hw_watch (abfd, note);
10231 else
10232 return TRUE;
10233
ad1cc4e4
AH
10234 case NT_ARM_SVE:
10235 if (note->namesz == 6
10236 && strcmp (note->namedata, "LINUX") == 0)
10237 return elfcore_grok_aarch_sve (abfd, note);
10238 else
10239 return TRUE;
10240
e6c3b5bf
AH
10241 case NT_ARM_PAC_MASK:
10242 if (note->namesz == 6
10243 && strcmp (note->namedata, "LINUX") == 0)
10244 return elfcore_grok_aarch_pauth (abfd, note);
10245 else
10246 return TRUE;
10247
252b5132
RH
10248 case NT_PRPSINFO:
10249 case NT_PSINFO:
bb0082d6
AM
10250 if (bed->elf_backend_grok_psinfo)
10251 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
b34976b6 10252 return TRUE;
bb0082d6 10253#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
252b5132 10254 return elfcore_grok_psinfo (abfd, note);
bb0082d6 10255#else
b34976b6 10256 return TRUE;
252b5132 10257#endif
3333a7c3
RM
10258
10259 case NT_AUXV:
58e07198 10260 return elfcore_make_auxv_note_section (abfd, note, 0);
9015683b 10261
451b7c33
TT
10262 case NT_FILE:
10263 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10264 note);
10265
9015683b
TT
10266 case NT_SIGINFO:
10267 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10268 note);
5b2c414d 10269
252b5132
RH
10270 }
10271}
10272
718175fa
JK
10273static bfd_boolean
10274elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10275{
c74f7d1c 10276 struct bfd_build_id* build_id;
30e8ee25
AM
10277
10278 if (note->descsz == 0)
10279 return FALSE;
10280
c74f7d1c
JT
10281 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10282 if (build_id == NULL)
718175fa
JK
10283 return FALSE;
10284
c74f7d1c
JT
10285 build_id->size = note->descsz;
10286 memcpy (build_id->data, note->descdata, note->descsz);
10287 abfd->build_id = build_id;
718175fa
JK
10288
10289 return TRUE;
10290}
10291
10292static bfd_boolean
10293elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10294{
10295 switch (note->type)
10296 {
10297 default:
10298 return TRUE;
10299
46bed679
L
10300 case NT_GNU_PROPERTY_TYPE_0:
10301 return _bfd_elf_parse_gnu_properties (abfd, note);
10302
718175fa
JK
10303 case NT_GNU_BUILD_ID:
10304 return elfobj_grok_gnu_build_id (abfd, note);
10305 }
10306}
10307
e21e5835
NC
10308static bfd_boolean
10309elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10310{
10311 struct sdt_note *cur =
7a6e0d89
AM
10312 (struct sdt_note *) bfd_alloc (abfd,
10313 sizeof (struct sdt_note) + note->descsz);
e21e5835
NC
10314
10315 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10316 cur->size = (bfd_size_type) note->descsz;
10317 memcpy (cur->data, note->descdata, note->descsz);
10318
10319 elf_tdata (abfd)->sdt_note_head = cur;
10320
10321 return TRUE;
10322}
10323
10324static bfd_boolean
10325elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10326{
10327 switch (note->type)
10328 {
10329 case NT_STAPSDT:
10330 return elfobj_grok_stapsdt_note_1 (abfd, note);
10331
10332 default:
10333 return TRUE;
10334 }
10335}
10336
aa1ed4a9
JB
10337static bfd_boolean
10338elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
10339{
10340 size_t offset;
10341
b5430a3c 10342 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 10343 {
b5430a3c 10344 case ELFCLASS32:
0064d223
JB
10345 if (note->descsz < 108)
10346 return FALSE;
aa1ed4a9
JB
10347 break;
10348
b5430a3c 10349 case ELFCLASS64:
0064d223
JB
10350 if (note->descsz < 120)
10351 return FALSE;
aa1ed4a9
JB
10352 break;
10353
10354 default:
10355 return FALSE;
10356 }
10357
0064d223
JB
10358 /* Check for version 1 in pr_version. */
10359 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10360 return FALSE;
80a04378 10361
0064d223
JB
10362 offset = 4;
10363
10364 /* Skip over pr_psinfosz. */
b5430a3c 10365 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
0064d223
JB
10366 offset += 4;
10367 else
10368 {
10369 offset += 4; /* Padding before pr_psinfosz. */
10370 offset += 8;
10371 }
10372
aa1ed4a9
JB
10373 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10374 elf_tdata (abfd)->core->program
10375 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
10376 offset += 17;
10377
10378 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10379 elf_tdata (abfd)->core->command
10380 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
0064d223
JB
10381 offset += 81;
10382
10383 /* Padding before pr_pid. */
10384 offset += 2;
10385
10386 /* The pr_pid field was added in version "1a". */
10387 if (note->descsz < offset + 4)
10388 return TRUE;
10389
10390 elf_tdata (abfd)->core->pid
10391 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
aa1ed4a9
JB
10392
10393 return TRUE;
10394}
10395
10396static bfd_boolean
10397elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
10398{
10399 size_t offset;
10400 size_t size;
24d3e51b 10401 size_t min_size;
aa1ed4a9 10402
24d3e51b
NC
10403 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10404 Also compute minimum size of this note. */
b5430a3c 10405 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 10406 {
b5430a3c 10407 case ELFCLASS32:
24d3e51b
NC
10408 offset = 4 + 4;
10409 min_size = offset + (4 * 2) + 4 + 4 + 4;
aa1ed4a9
JB
10410 break;
10411
b5430a3c 10412 case ELFCLASS64:
24d3e51b
NC
10413 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
10414 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
aa1ed4a9
JB
10415 break;
10416
10417 default:
10418 return FALSE;
10419 }
10420
24d3e51b
NC
10421 if (note->descsz < min_size)
10422 return FALSE;
10423
10424 /* Check for version 1 in pr_version. */
10425 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10426 return FALSE;
aa1ed4a9 10427
24d3e51b
NC
10428 /* Extract size of pr_reg from pr_gregsetsz. */
10429 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
b5430a3c 10430 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
24d3e51b
NC
10431 {
10432 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10433 offset += 4 * 2;
10434 }
b5430a3c 10435 else
24d3e51b
NC
10436 {
10437 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
10438 offset += 8 * 2;
10439 }
aa1ed4a9 10440
24d3e51b 10441 /* Skip over pr_osreldate. */
aa1ed4a9
JB
10442 offset += 4;
10443
24d3e51b 10444 /* Read signal from pr_cursig. */
aa1ed4a9
JB
10445 if (elf_tdata (abfd)->core->signal == 0)
10446 elf_tdata (abfd)->core->signal
10447 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10448 offset += 4;
10449
24d3e51b 10450 /* Read TID from pr_pid. */
aa1ed4a9
JB
10451 elf_tdata (abfd)->core->lwpid
10452 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10453 offset += 4;
10454
24d3e51b 10455 /* Padding before pr_reg. */
b5430a3c 10456 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
aa1ed4a9
JB
10457 offset += 4;
10458
24d3e51b
NC
10459 /* Make sure that there is enough data remaining in the note. */
10460 if ((note->descsz - offset) < size)
10461 return FALSE;
10462
aa1ed4a9
JB
10463 /* Make a ".reg/999" section and a ".reg" section. */
10464 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
10465 size, note->descpos + offset);
10466}
10467
10468static bfd_boolean
10469elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
10470{
544c67cd
JB
10471 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10472
aa1ed4a9
JB
10473 switch (note->type)
10474 {
10475 case NT_PRSTATUS:
544c67cd
JB
10476 if (bed->elf_backend_grok_freebsd_prstatus)
10477 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
10478 return TRUE;
aa1ed4a9
JB
10479 return elfcore_grok_freebsd_prstatus (abfd, note);
10480
10481 case NT_FPREGSET:
10482 return elfcore_grok_prfpreg (abfd, note);
10483
10484 case NT_PRPSINFO:
10485 return elfcore_grok_freebsd_psinfo (abfd, note);
10486
10487 case NT_FREEBSD_THRMISC:
10488 if (note->namesz == 8)
10489 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
10490 else
10491 return TRUE;
10492
ddb2bbcf
JB
10493 case NT_FREEBSD_PROCSTAT_PROC:
10494 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
10495 note);
10496
10497 case NT_FREEBSD_PROCSTAT_FILES:
10498 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
10499 note);
10500
10501 case NT_FREEBSD_PROCSTAT_VMMAP:
10502 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
10503 note);
10504
3350c5f5 10505 case NT_FREEBSD_PROCSTAT_AUXV:
58e07198 10506 return elfcore_make_auxv_note_section (abfd, note, 4);
3350c5f5 10507
aa1ed4a9
JB
10508 case NT_X86_XSTATE:
10509 if (note->namesz == 8)
10510 return elfcore_grok_xstatereg (abfd, note);
10511 else
10512 return TRUE;
10513
e6f3b9c3
JB
10514 case NT_FREEBSD_PTLWPINFO:
10515 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
10516 note);
10517
6d5be5d6
JB
10518 case NT_ARM_VFP:
10519 return elfcore_grok_arm_vfp (abfd, note);
10520
aa1ed4a9
JB
10521 default:
10522 return TRUE;
10523 }
10524}
10525
b34976b6 10526static bfd_boolean
217aa764 10527elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
50b2bdb7
AM
10528{
10529 char *cp;
10530
10531 cp = strchr (note->namedata, '@');
10532 if (cp != NULL)
10533 {
d2b64500 10534 *lwpidp = atoi(cp + 1);
b34976b6 10535 return TRUE;
50b2bdb7 10536 }
b34976b6 10537 return FALSE;
50b2bdb7
AM
10538}
10539
b34976b6 10540static bfd_boolean
217aa764 10541elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7 10542{
80a04378
NC
10543 if (note->descsz <= 0x7c + 31)
10544 return FALSE;
10545
50b2bdb7 10546 /* Signal number at offset 0x08. */
228e534f 10547 elf_tdata (abfd)->core->signal
50b2bdb7
AM
10548 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10549
10550 /* Process ID at offset 0x50. */
228e534f 10551 elf_tdata (abfd)->core->pid
50b2bdb7
AM
10552 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
10553
10554 /* Command name at 0x7c (max 32 bytes, including nul). */
228e534f 10555 elf_tdata (abfd)->core->command
50b2bdb7
AM
10556 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
10557
7720ba9f
MK
10558 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
10559 note);
50b2bdb7
AM
10560}
10561
b34976b6 10562static bfd_boolean
217aa764 10563elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7
AM
10564{
10565 int lwp;
10566
10567 if (elfcore_netbsd_get_lwpid (note, &lwp))
228e534f 10568 elf_tdata (abfd)->core->lwpid = lwp;
50b2bdb7 10569
58e07198 10570 switch (note->type)
50b2bdb7 10571 {
58e07198 10572 case NT_NETBSDCORE_PROCINFO:
50b2bdb7 10573 /* NetBSD-specific core "procinfo". Note that we expect to
08a40648
AM
10574 find this note before any of the others, which is fine,
10575 since the kernel writes this note out first when it
10576 creates a core file. */
50b2bdb7 10577 return elfcore_grok_netbsd_procinfo (abfd, note);
58e07198
CZ
10578#ifdef NT_NETBSDCORE_AUXV
10579 case NT_NETBSDCORE_AUXV:
10580 /* NetBSD-specific Elf Auxiliary Vector data. */
10581 return elfcore_make_auxv_note_section (abfd, note, 4);
10582#endif
10583 default:
10584 break;
50b2bdb7
AM
10585 }
10586
58e07198 10587 /* As of March 2017 there are no other machine-independent notes
b4db1224
JT
10588 defined for NetBSD core files. If the note type is less
10589 than the start of the machine-dependent note types, we don't
10590 understand it. */
47d9a591 10591
b4db1224 10592 if (note->type < NT_NETBSDCORE_FIRSTMACH)
b34976b6 10593 return TRUE;
50b2bdb7
AM
10594
10595
10596 switch (bfd_get_arch (abfd))
10597 {
08a40648
AM
10598 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10599 PT_GETFPREGS == mach+2. */
50b2bdb7
AM
10600
10601 case bfd_arch_alpha:
10602 case bfd_arch_sparc:
10603 switch (note->type)
08a40648
AM
10604 {
10605 case NT_NETBSDCORE_FIRSTMACH+0:
10606 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 10607
08a40648
AM
10608 case NT_NETBSDCORE_FIRSTMACH+2:
10609 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 10610
08a40648
AM
10611 default:
10612 return TRUE;
10613 }
50b2bdb7 10614
58e07198
CZ
10615 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
10616 There's also old PT___GETREGS40 == mach + 1 for old reg
10617 structure which lacks GBR. */
10618
10619 case bfd_arch_sh:
10620 switch (note->type)
10621 {
10622 case NT_NETBSDCORE_FIRSTMACH+3:
10623 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10624
10625 case NT_NETBSDCORE_FIRSTMACH+5:
10626 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10627
10628 default:
10629 return TRUE;
10630 }
10631
08a40648
AM
10632 /* On all other arch's, PT_GETREGS == mach+1 and
10633 PT_GETFPREGS == mach+3. */
50b2bdb7
AM
10634
10635 default:
10636 switch (note->type)
08a40648
AM
10637 {
10638 case NT_NETBSDCORE_FIRSTMACH+1:
10639 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 10640
08a40648
AM
10641 case NT_NETBSDCORE_FIRSTMACH+3:
10642 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 10643
08a40648
AM
10644 default:
10645 return TRUE;
10646 }
50b2bdb7
AM
10647 }
10648 /* NOTREACHED */
10649}
10650
67cc5033
MK
10651static bfd_boolean
10652elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
10653{
80a04378
NC
10654 if (note->descsz <= 0x48 + 31)
10655 return FALSE;
10656
67cc5033 10657 /* Signal number at offset 0x08. */
228e534f 10658 elf_tdata (abfd)->core->signal
67cc5033
MK
10659 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10660
10661 /* Process ID at offset 0x20. */
228e534f 10662 elf_tdata (abfd)->core->pid
67cc5033
MK
10663 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
10664
10665 /* Command name at 0x48 (max 32 bytes, including nul). */
228e534f 10666 elf_tdata (abfd)->core->command
67cc5033
MK
10667 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
10668
10669 return TRUE;
10670}
10671
10672static bfd_boolean
10673elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
10674{
10675 if (note->type == NT_OPENBSD_PROCINFO)
10676 return elfcore_grok_openbsd_procinfo (abfd, note);
10677
10678 if (note->type == NT_OPENBSD_REGS)
10679 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10680
10681 if (note->type == NT_OPENBSD_FPREGS)
10682 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10683
10684 if (note->type == NT_OPENBSD_XFPREGS)
10685 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
10686
10687 if (note->type == NT_OPENBSD_AUXV)
58e07198 10688 return elfcore_make_auxv_note_section (abfd, note, 0);
67cc5033
MK
10689
10690 if (note->type == NT_OPENBSD_WCOOKIE)
10691 {
10692 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
10693 SEC_HAS_CONTENTS);
10694
10695 if (sect == NULL)
10696 return FALSE;
10697 sect->size = note->descsz;
10698 sect->filepos = note->descpos;
10699 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10700
10701 return TRUE;
10702 }
10703
10704 return TRUE;
10705}
10706
07c6e936 10707static bfd_boolean
d3fd4074 10708elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
07c6e936
NC
10709{
10710 void *ddata = note->descdata;
10711 char buf[100];
10712 char *name;
10713 asection *sect;
f8843e87
AM
10714 short sig;
10715 unsigned flags;
07c6e936 10716
80a04378
NC
10717 if (note->descsz < 16)
10718 return FALSE;
10719
07c6e936 10720 /* nto_procfs_status 'pid' field is at offset 0. */
228e534f 10721 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
07c6e936 10722
f8843e87
AM
10723 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
10724 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
10725
10726 /* nto_procfs_status 'flags' field is at offset 8. */
10727 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
07c6e936
NC
10728
10729 /* nto_procfs_status 'what' field is at offset 14. */
f8843e87
AM
10730 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
10731 {
228e534f
AM
10732 elf_tdata (abfd)->core->signal = sig;
10733 elf_tdata (abfd)->core->lwpid = *tid;
f8843e87 10734 }
07c6e936 10735
f8843e87
AM
10736 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
10737 do not come from signals so we make sure we set the current
10738 thread just in case. */
10739 if (flags & 0x00000080)
228e534f 10740 elf_tdata (abfd)->core->lwpid = *tid;
07c6e936
NC
10741
10742 /* Make a ".qnx_core_status/%d" section. */
d3fd4074 10743 sprintf (buf, ".qnx_core_status/%ld", *tid);
07c6e936 10744
a50b1753 10745 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936
NC
10746 if (name == NULL)
10747 return FALSE;
10748 strcpy (name, buf);
10749
117ed4f8 10750 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936
NC
10751 if (sect == NULL)
10752 return FALSE;
10753
07d6d2b8
AM
10754 sect->size = note->descsz;
10755 sect->filepos = note->descpos;
07c6e936
NC
10756 sect->alignment_power = 2;
10757
10758 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
10759}
10760
10761static bfd_boolean
d69f560c
KW
10762elfcore_grok_nto_regs (bfd *abfd,
10763 Elf_Internal_Note *note,
d3fd4074 10764 long tid,
d69f560c 10765 char *base)
07c6e936
NC
10766{
10767 char buf[100];
10768 char *name;
10769 asection *sect;
10770
d69f560c 10771 /* Make a "(base)/%d" section. */
d3fd4074 10772 sprintf (buf, "%s/%ld", base, tid);
07c6e936 10773
a50b1753 10774 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936
NC
10775 if (name == NULL)
10776 return FALSE;
10777 strcpy (name, buf);
10778
117ed4f8 10779 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936
NC
10780 if (sect == NULL)
10781 return FALSE;
10782
07d6d2b8
AM
10783 sect->size = note->descsz;
10784 sect->filepos = note->descpos;
07c6e936
NC
10785 sect->alignment_power = 2;
10786
f8843e87 10787 /* This is the current thread. */
228e534f 10788 if (elf_tdata (abfd)->core->lwpid == tid)
d69f560c 10789 return elfcore_maybe_make_sect (abfd, base, sect);
f8843e87
AM
10790
10791 return TRUE;
07c6e936
NC
10792}
10793
10794#define BFD_QNT_CORE_INFO 7
10795#define BFD_QNT_CORE_STATUS 8
10796#define BFD_QNT_CORE_GREG 9
10797#define BFD_QNT_CORE_FPREG 10
10798
10799static bfd_boolean
217aa764 10800elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
07c6e936
NC
10801{
10802 /* Every GREG section has a STATUS section before it. Store the
811072d8 10803 tid from the previous call to pass down to the next gregs
07c6e936 10804 function. */
d3fd4074 10805 static long tid = 1;
07c6e936
NC
10806
10807 switch (note->type)
10808 {
d69f560c
KW
10809 case BFD_QNT_CORE_INFO:
10810 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
10811 case BFD_QNT_CORE_STATUS:
10812 return elfcore_grok_nto_status (abfd, note, &tid);
10813 case BFD_QNT_CORE_GREG:
10814 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
10815 case BFD_QNT_CORE_FPREG:
10816 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
10817 default:
10818 return TRUE;
07c6e936
NC
10819 }
10820}
10821
b15fa79e
AM
10822static bfd_boolean
10823elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
10824{
10825 char *name;
10826 asection *sect;
10827 size_t len;
10828
10829 /* Use note name as section name. */
10830 len = note->namesz;
a50b1753 10831 name = (char *) bfd_alloc (abfd, len);
b15fa79e
AM
10832 if (name == NULL)
10833 return FALSE;
10834 memcpy (name, note->namedata, len);
10835 name[len - 1] = '\0';
10836
10837 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10838 if (sect == NULL)
10839 return FALSE;
10840
07d6d2b8
AM
10841 sect->size = note->descsz;
10842 sect->filepos = note->descpos;
b15fa79e
AM
10843 sect->alignment_power = 1;
10844
10845 return TRUE;
10846}
10847
7c76fa91
MS
10848/* Function: elfcore_write_note
10849
47d9a591 10850 Inputs:
a39f3346 10851 buffer to hold note, and current size of buffer
7c76fa91
MS
10852 name of note
10853 type of note
10854 data for note
10855 size of data for note
10856
a39f3346
AM
10857 Writes note to end of buffer. ELF64 notes are written exactly as
10858 for ELF32, despite the current (as of 2006) ELF gabi specifying
10859 that they ought to have 8-byte namesz and descsz field, and have
10860 8-byte alignment. Other writers, eg. Linux kernel, do the same.
10861
7c76fa91 10862 Return:
a39f3346 10863 Pointer to realloc'd buffer, *BUFSIZ updated. */
7c76fa91
MS
10864
10865char *
a39f3346 10866elfcore_write_note (bfd *abfd,
217aa764 10867 char *buf,
a39f3346 10868 int *bufsiz,
217aa764 10869 const char *name,
a39f3346 10870 int type,
217aa764 10871 const void *input,
a39f3346 10872 int size)
7c76fa91
MS
10873{
10874 Elf_External_Note *xnp;
d4c88bbb 10875 size_t namesz;
d4c88bbb 10876 size_t newspace;
a39f3346 10877 char *dest;
7c76fa91 10878
d4c88bbb 10879 namesz = 0;
d4c88bbb 10880 if (name != NULL)
a39f3346 10881 namesz = strlen (name) + 1;
d4c88bbb 10882
a39f3346 10883 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
d4c88bbb 10884
a50b1753 10885 buf = (char *) realloc (buf, *bufsiz + newspace);
14b1c01e
AM
10886 if (buf == NULL)
10887 return buf;
a39f3346 10888 dest = buf + *bufsiz;
7c76fa91
MS
10889 *bufsiz += newspace;
10890 xnp = (Elf_External_Note *) dest;
10891 H_PUT_32 (abfd, namesz, xnp->namesz);
10892 H_PUT_32 (abfd, size, xnp->descsz);
10893 H_PUT_32 (abfd, type, xnp->type);
d4c88bbb
AM
10894 dest = xnp->name;
10895 if (name != NULL)
10896 {
10897 memcpy (dest, name, namesz);
10898 dest += namesz;
a39f3346 10899 while (namesz & 3)
d4c88bbb
AM
10900 {
10901 *dest++ = '\0';
a39f3346 10902 ++namesz;
d4c88bbb
AM
10903 }
10904 }
10905 memcpy (dest, input, size);
a39f3346
AM
10906 dest += size;
10907 while (size & 3)
10908 {
10909 *dest++ = '\0';
10910 ++size;
10911 }
10912 return buf;
7c76fa91
MS
10913}
10914
602f1657
AM
10915/* gcc-8 warns (*) on all the strncpy calls in this function about
10916 possible string truncation. The "truncation" is not a bug. We
10917 have an external representation of structs with fields that are not
10918 necessarily NULL terminated and corresponding internal
10919 representation fields that are one larger so that they can always
10920 be NULL terminated.
10921 gcc versions between 4.2 and 4.6 do not allow pragma control of
10922 diagnostics inside functions, giving a hard error if you try to use
10923 the finer control available with later versions.
10924 gcc prior to 4.2 warns about diagnostic push and pop.
10925 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
10926 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
10927 (*) Depending on your system header files! */
d99b4b92 10928#if GCC_VERSION >= 8000
602f1657
AM
10929# pragma GCC diagnostic push
10930# pragma GCC diagnostic ignored "-Wstringop-truncation"
d99b4b92 10931#endif
7c76fa91 10932char *
217aa764
AM
10933elfcore_write_prpsinfo (bfd *abfd,
10934 char *buf,
10935 int *bufsiz,
10936 const char *fname,
10937 const char *psargs)
7c76fa91 10938{
183e98be
AM
10939 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10940
10941 if (bed->elf_backend_write_core_note != NULL)
10942 {
10943 char *ret;
10944 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
10945 NT_PRPSINFO, fname, psargs);
10946 if (ret != NULL)
10947 return ret;
10948 }
7c76fa91 10949
1f20dca5 10950#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
602f1657 10951# if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
183e98be
AM
10952 if (bed->s->elfclass == ELFCLASS32)
10953 {
602f1657 10954# if defined (HAVE_PSINFO32_T)
183e98be
AM
10955 psinfo32_t data;
10956 int note_type = NT_PSINFO;
602f1657 10957# else
183e98be
AM
10958 prpsinfo32_t data;
10959 int note_type = NT_PRPSINFO;
602f1657 10960# endif
183e98be
AM
10961
10962 memset (&data, 0, sizeof (data));
10963 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
10964 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
10965 return elfcore_write_note (abfd, buf, bufsiz,
1f20dca5 10966 "CORE", note_type, &data, sizeof (data));
183e98be
AM
10967 }
10968 else
602f1657 10969# endif
183e98be 10970 {
602f1657 10971# if defined (HAVE_PSINFO_T)
183e98be
AM
10972 psinfo_t data;
10973 int note_type = NT_PSINFO;
602f1657 10974# else
183e98be
AM
10975 prpsinfo_t data;
10976 int note_type = NT_PRPSINFO;
602f1657 10977# endif
7c76fa91 10978
183e98be
AM
10979 memset (&data, 0, sizeof (data));
10980 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
10981 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
10982 return elfcore_write_note (abfd, buf, bufsiz,
1f20dca5 10983 "CORE", note_type, &data, sizeof (data));
183e98be 10984 }
7c76fa91
MS
10985#endif /* PSINFO_T or PRPSINFO_T */
10986
1f20dca5
UW
10987 free (buf);
10988 return NULL;
10989}
d99b4b92 10990#if GCC_VERSION >= 8000
602f1657 10991# pragma GCC diagnostic pop
d99b4b92 10992#endif
1f20dca5 10993
70a38d42
SDJ
10994char *
10995elfcore_write_linux_prpsinfo32
10996 (bfd *abfd, char *buf, int *bufsiz,
10997 const struct elf_internal_linux_prpsinfo *prpsinfo)
10998{
a2f63b2e
MR
10999 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
11000 {
11001 struct elf_external_linux_prpsinfo32_ugid16 data;
11002
11003 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
11004 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11005 &data, sizeof (data));
11006 }
11007 else
11008 {
11009 struct elf_external_linux_prpsinfo32_ugid32 data;
70a38d42 11010
a2f63b2e
MR
11011 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
11012 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11013 &data, sizeof (data));
11014 }
70a38d42
SDJ
11015}
11016
11017char *
11018elfcore_write_linux_prpsinfo64
11019 (bfd *abfd, char *buf, int *bufsiz,
11020 const struct elf_internal_linux_prpsinfo *prpsinfo)
11021{
3c9a7b0d
MR
11022 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
11023 {
11024 struct elf_external_linux_prpsinfo64_ugid16 data;
11025
11026 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
11027 return elfcore_write_note (abfd, buf, bufsiz,
11028 "CORE", NT_PRPSINFO, &data, sizeof (data));
11029 }
11030 else
11031 {
11032 struct elf_external_linux_prpsinfo64_ugid32 data;
70a38d42 11033
3c9a7b0d
MR
11034 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
11035 return elfcore_write_note (abfd, buf, bufsiz,
11036 "CORE", NT_PRPSINFO, &data, sizeof (data));
11037 }
70a38d42
SDJ
11038}
11039
7c76fa91 11040char *
217aa764
AM
11041elfcore_write_prstatus (bfd *abfd,
11042 char *buf,
11043 int *bufsiz,
11044 long pid,
11045 int cursig,
11046 const void *gregs)
7c76fa91 11047{
183e98be 11048 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 11049
183e98be
AM
11050 if (bed->elf_backend_write_core_note != NULL)
11051 {
11052 char *ret;
11053 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11054 NT_PRSTATUS,
11055 pid, cursig, gregs);
11056 if (ret != NULL)
11057 return ret;
11058 }
11059
1f20dca5 11060#if defined (HAVE_PRSTATUS_T)
183e98be
AM
11061#if defined (HAVE_PRSTATUS32_T)
11062 if (bed->s->elfclass == ELFCLASS32)
11063 {
11064 prstatus32_t prstat;
11065
11066 memset (&prstat, 0, sizeof (prstat));
11067 prstat.pr_pid = pid;
11068 prstat.pr_cursig = cursig;
11069 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 11070 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
11071 NT_PRSTATUS, &prstat, sizeof (prstat));
11072 }
11073 else
11074#endif
11075 {
11076 prstatus_t prstat;
11077
11078 memset (&prstat, 0, sizeof (prstat));
11079 prstat.pr_pid = pid;
11080 prstat.pr_cursig = cursig;
11081 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 11082 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
11083 NT_PRSTATUS, &prstat, sizeof (prstat));
11084 }
7c76fa91
MS
11085#endif /* HAVE_PRSTATUS_T */
11086
1f20dca5
UW
11087 free (buf);
11088 return NULL;
11089}
11090
51316059
MS
11091#if defined (HAVE_LWPSTATUS_T)
11092char *
217aa764
AM
11093elfcore_write_lwpstatus (bfd *abfd,
11094 char *buf,
11095 int *bufsiz,
11096 long pid,
11097 int cursig,
11098 const void *gregs)
51316059
MS
11099{
11100 lwpstatus_t lwpstat;
183e98be 11101 const char *note_name = "CORE";
51316059
MS
11102
11103 memset (&lwpstat, 0, sizeof (lwpstat));
11104 lwpstat.pr_lwpid = pid >> 16;
11105 lwpstat.pr_cursig = cursig;
11106#if defined (HAVE_LWPSTATUS_T_PR_REG)
d1e8523e 11107 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
51316059
MS
11108#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11109#if !defined(gregs)
11110 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
11111 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
11112#else
11113 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
11114 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
11115#endif
11116#endif
47d9a591 11117 return elfcore_write_note (abfd, buf, bufsiz, note_name,
51316059
MS
11118 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
11119}
11120#endif /* HAVE_LWPSTATUS_T */
11121
7c76fa91
MS
11122#if defined (HAVE_PSTATUS_T)
11123char *
217aa764
AM
11124elfcore_write_pstatus (bfd *abfd,
11125 char *buf,
11126 int *bufsiz,
11127 long pid,
6c10990d
NC
11128 int cursig ATTRIBUTE_UNUSED,
11129 const void *gregs ATTRIBUTE_UNUSED)
7c76fa91 11130{
183e98be
AM
11131 const char *note_name = "CORE";
11132#if defined (HAVE_PSTATUS32_T)
11133 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 11134
183e98be
AM
11135 if (bed->s->elfclass == ELFCLASS32)
11136 {
11137 pstatus32_t pstat;
11138
11139 memset (&pstat, 0, sizeof (pstat));
11140 pstat.pr_pid = pid & 0xffff;
11141 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11142 NT_PSTATUS, &pstat, sizeof (pstat));
11143 return buf;
11144 }
11145 else
11146#endif
11147 {
11148 pstatus_t pstat;
11149
11150 memset (&pstat, 0, sizeof (pstat));
11151 pstat.pr_pid = pid & 0xffff;
11152 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11153 NT_PSTATUS, &pstat, sizeof (pstat));
11154 return buf;
11155 }
7c76fa91
MS
11156}
11157#endif /* HAVE_PSTATUS_T */
11158
11159char *
217aa764
AM
11160elfcore_write_prfpreg (bfd *abfd,
11161 char *buf,
11162 int *bufsiz,
11163 const void *fpregs,
11164 int size)
7c76fa91 11165{
183e98be 11166 const char *note_name = "CORE";
47d9a591 11167 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
11168 note_name, NT_FPREGSET, fpregs, size);
11169}
11170
11171char *
217aa764
AM
11172elfcore_write_prxfpreg (bfd *abfd,
11173 char *buf,
11174 int *bufsiz,
11175 const void *xfpregs,
11176 int size)
7c76fa91
MS
11177{
11178 char *note_name = "LINUX";
47d9a591 11179 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
11180 note_name, NT_PRXFPREG, xfpregs, size);
11181}
11182
4339cae0
L
11183char *
11184elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
11185 const void *xfpregs, int size)
11186{
97de3545
JB
11187 char *note_name;
11188 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
11189 note_name = "FreeBSD";
11190 else
11191 note_name = "LINUX";
4339cae0
L
11192 return elfcore_write_note (abfd, buf, bufsiz,
11193 note_name, NT_X86_XSTATE, xfpregs, size);
11194}
11195
97753bd5
AM
11196char *
11197elfcore_write_ppc_vmx (bfd *abfd,
11198 char *buf,
11199 int *bufsiz,
11200 const void *ppc_vmx,
11201 int size)
11202{
11203 char *note_name = "LINUX";
11204 return elfcore_write_note (abfd, buf, bufsiz,
11205 note_name, NT_PPC_VMX, ppc_vmx, size);
11206}
11207
89eeb0bc
LM
11208char *
11209elfcore_write_ppc_vsx (bfd *abfd,
07d6d2b8
AM
11210 char *buf,
11211 int *bufsiz,
11212 const void *ppc_vsx,
11213 int size)
89eeb0bc
LM
11214{
11215 char *note_name = "LINUX";
11216 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11217 note_name, NT_PPC_VSX, ppc_vsx, size);
89eeb0bc
LM
11218}
11219
cb2366c1
EBM
11220char *
11221elfcore_write_ppc_tar (bfd *abfd,
4b24dd1a
AM
11222 char *buf,
11223 int *bufsiz,
11224 const void *ppc_tar,
11225 int size)
cb2366c1
EBM
11226{
11227 char *note_name = "LINUX";
11228 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11229 note_name, NT_PPC_TAR, ppc_tar, size);
cb2366c1
EBM
11230}
11231
11232char *
11233elfcore_write_ppc_ppr (bfd *abfd,
4b24dd1a
AM
11234 char *buf,
11235 int *bufsiz,
11236 const void *ppc_ppr,
11237 int size)
cb2366c1
EBM
11238{
11239 char *note_name = "LINUX";
11240 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11241 note_name, NT_PPC_PPR, ppc_ppr, size);
cb2366c1
EBM
11242}
11243
11244char *
11245elfcore_write_ppc_dscr (bfd *abfd,
4b24dd1a
AM
11246 char *buf,
11247 int *bufsiz,
11248 const void *ppc_dscr,
11249 int size)
cb2366c1
EBM
11250{
11251 char *note_name = "LINUX";
11252 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11253 note_name, NT_PPC_DSCR, ppc_dscr, size);
cb2366c1
EBM
11254}
11255
11256char *
11257elfcore_write_ppc_ebb (bfd *abfd,
4b24dd1a
AM
11258 char *buf,
11259 int *bufsiz,
11260 const void *ppc_ebb,
11261 int size)
cb2366c1
EBM
11262{
11263 char *note_name = "LINUX";
11264 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11265 note_name, NT_PPC_EBB, ppc_ebb, size);
cb2366c1
EBM
11266}
11267
11268char *
11269elfcore_write_ppc_pmu (bfd *abfd,
4b24dd1a
AM
11270 char *buf,
11271 int *bufsiz,
11272 const void *ppc_pmu,
11273 int size)
cb2366c1
EBM
11274{
11275 char *note_name = "LINUX";
11276 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11277 note_name, NT_PPC_PMU, ppc_pmu, size);
cb2366c1
EBM
11278}
11279
11280char *
11281elfcore_write_ppc_tm_cgpr (bfd *abfd,
4b24dd1a
AM
11282 char *buf,
11283 int *bufsiz,
11284 const void *ppc_tm_cgpr,
11285 int size)
cb2366c1
EBM
11286{
11287 char *note_name = "LINUX";
11288 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11289 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
cb2366c1
EBM
11290}
11291
11292char *
11293elfcore_write_ppc_tm_cfpr (bfd *abfd,
4b24dd1a
AM
11294 char *buf,
11295 int *bufsiz,
11296 const void *ppc_tm_cfpr,
11297 int size)
cb2366c1
EBM
11298{
11299 char *note_name = "LINUX";
11300 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11301 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
cb2366c1
EBM
11302}
11303
11304char *
11305elfcore_write_ppc_tm_cvmx (bfd *abfd,
4b24dd1a
AM
11306 char *buf,
11307 int *bufsiz,
11308 const void *ppc_tm_cvmx,
11309 int size)
cb2366c1
EBM
11310{
11311 char *note_name = "LINUX";
11312 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11313 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
cb2366c1
EBM
11314}
11315
11316char *
11317elfcore_write_ppc_tm_cvsx (bfd *abfd,
4b24dd1a
AM
11318 char *buf,
11319 int *bufsiz,
11320 const void *ppc_tm_cvsx,
11321 int size)
cb2366c1
EBM
11322{
11323 char *note_name = "LINUX";
11324 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11325 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
cb2366c1
EBM
11326}
11327
11328char *
11329elfcore_write_ppc_tm_spr (bfd *abfd,
4b24dd1a
AM
11330 char *buf,
11331 int *bufsiz,
11332 const void *ppc_tm_spr,
11333 int size)
cb2366c1
EBM
11334{
11335 char *note_name = "LINUX";
11336 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11337 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
cb2366c1
EBM
11338}
11339
11340char *
11341elfcore_write_ppc_tm_ctar (bfd *abfd,
4b24dd1a
AM
11342 char *buf,
11343 int *bufsiz,
11344 const void *ppc_tm_ctar,
11345 int size)
cb2366c1
EBM
11346{
11347 char *note_name = "LINUX";
11348 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11349 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
cb2366c1
EBM
11350}
11351
11352char *
11353elfcore_write_ppc_tm_cppr (bfd *abfd,
4b24dd1a
AM
11354 char *buf,
11355 int *bufsiz,
11356 const void *ppc_tm_cppr,
11357 int size)
cb2366c1
EBM
11358{
11359 char *note_name = "LINUX";
11360 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11361 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
cb2366c1
EBM
11362}
11363
11364char *
11365elfcore_write_ppc_tm_cdscr (bfd *abfd,
4b24dd1a
AM
11366 char *buf,
11367 int *bufsiz,
11368 const void *ppc_tm_cdscr,
11369 int size)
cb2366c1
EBM
11370{
11371 char *note_name = "LINUX";
11372 return elfcore_write_note (abfd, buf, bufsiz,
4b24dd1a 11373 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
cb2366c1
EBM
11374}
11375
0675e188
UW
11376static char *
11377elfcore_write_s390_high_gprs (bfd *abfd,
11378 char *buf,
11379 int *bufsiz,
11380 const void *s390_high_gprs,
11381 int size)
11382{
11383 char *note_name = "LINUX";
11384 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11385 note_name, NT_S390_HIGH_GPRS,
0675e188
UW
11386 s390_high_gprs, size);
11387}
11388
d7eeb400
MS
11389char *
11390elfcore_write_s390_timer (bfd *abfd,
07d6d2b8
AM
11391 char *buf,
11392 int *bufsiz,
11393 const void *s390_timer,
11394 int size)
d7eeb400
MS
11395{
11396 char *note_name = "LINUX";
11397 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11398 note_name, NT_S390_TIMER, s390_timer, size);
d7eeb400
MS
11399}
11400
11401char *
11402elfcore_write_s390_todcmp (bfd *abfd,
07d6d2b8
AM
11403 char *buf,
11404 int *bufsiz,
11405 const void *s390_todcmp,
11406 int size)
d7eeb400
MS
11407{
11408 char *note_name = "LINUX";
11409 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11410 note_name, NT_S390_TODCMP, s390_todcmp, size);
d7eeb400
MS
11411}
11412
11413char *
11414elfcore_write_s390_todpreg (bfd *abfd,
07d6d2b8
AM
11415 char *buf,
11416 int *bufsiz,
11417 const void *s390_todpreg,
11418 int size)
d7eeb400
MS
11419{
11420 char *note_name = "LINUX";
11421 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11422 note_name, NT_S390_TODPREG, s390_todpreg, size);
d7eeb400
MS
11423}
11424
11425char *
11426elfcore_write_s390_ctrs (bfd *abfd,
07d6d2b8
AM
11427 char *buf,
11428 int *bufsiz,
11429 const void *s390_ctrs,
11430 int size)
d7eeb400
MS
11431{
11432 char *note_name = "LINUX";
11433 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11434 note_name, NT_S390_CTRS, s390_ctrs, size);
d7eeb400
MS
11435}
11436
11437char *
11438elfcore_write_s390_prefix (bfd *abfd,
07d6d2b8
AM
11439 char *buf,
11440 int *bufsiz,
11441 const void *s390_prefix,
11442 int size)
d7eeb400
MS
11443{
11444 char *note_name = "LINUX";
11445 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11446 note_name, NT_S390_PREFIX, s390_prefix, size);
d7eeb400
MS
11447}
11448
355b81d9
UW
11449char *
11450elfcore_write_s390_last_break (bfd *abfd,
11451 char *buf,
11452 int *bufsiz,
11453 const void *s390_last_break,
11454 int size)
11455{
11456 char *note_name = "LINUX";
11457 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11458 note_name, NT_S390_LAST_BREAK,
355b81d9
UW
11459 s390_last_break, size);
11460}
11461
11462char *
11463elfcore_write_s390_system_call (bfd *abfd,
11464 char *buf,
11465 int *bufsiz,
11466 const void *s390_system_call,
11467 int size)
11468{
11469 char *note_name = "LINUX";
11470 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11471 note_name, NT_S390_SYSTEM_CALL,
355b81d9
UW
11472 s390_system_call, size);
11473}
11474
abb3f6cc
NC
11475char *
11476elfcore_write_s390_tdb (bfd *abfd,
11477 char *buf,
11478 int *bufsiz,
11479 const void *s390_tdb,
11480 int size)
11481{
11482 char *note_name = "LINUX";
11483 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 11484 note_name, NT_S390_TDB, s390_tdb, size);
abb3f6cc
NC
11485}
11486
4ef9f41a
AA
11487char *
11488elfcore_write_s390_vxrs_low (bfd *abfd,
11489 char *buf,
11490 int *bufsiz,
11491 const void *s390_vxrs_low,
11492 int size)
11493{
11494 char *note_name = "LINUX";
11495 return elfcore_write_note (abfd, buf, bufsiz,
11496 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
11497}
11498
11499char *
11500elfcore_write_s390_vxrs_high (bfd *abfd,
11501 char *buf,
11502 int *bufsiz,
11503 const void *s390_vxrs_high,
11504 int size)
11505{
11506 char *note_name = "LINUX";
11507 return elfcore_write_note (abfd, buf, bufsiz,
11508 note_name, NT_S390_VXRS_HIGH,
11509 s390_vxrs_high, size);
11510}
11511
88ab90e8
AA
11512char *
11513elfcore_write_s390_gs_cb (bfd *abfd,
11514 char *buf,
11515 int *bufsiz,
11516 const void *s390_gs_cb,
11517 int size)
11518{
11519 char *note_name = "LINUX";
11520 return elfcore_write_note (abfd, buf, bufsiz,
11521 note_name, NT_S390_GS_CB,
11522 s390_gs_cb, size);
11523}
11524
11525char *
11526elfcore_write_s390_gs_bc (bfd *abfd,
11527 char *buf,
11528 int *bufsiz,
11529 const void *s390_gs_bc,
11530 int size)
11531{
11532 char *note_name = "LINUX";
11533 return elfcore_write_note (abfd, buf, bufsiz,
11534 note_name, NT_S390_GS_BC,
11535 s390_gs_bc, size);
11536}
11537
faa9a424
UW
11538char *
11539elfcore_write_arm_vfp (bfd *abfd,
11540 char *buf,
11541 int *bufsiz,
11542 const void *arm_vfp,
11543 int size)
11544{
11545 char *note_name = "LINUX";
11546 return elfcore_write_note (abfd, buf, bufsiz,
11547 note_name, NT_ARM_VFP, arm_vfp, size);
11548}
11549
652451f8
YZ
11550char *
11551elfcore_write_aarch_tls (bfd *abfd,
11552 char *buf,
11553 int *bufsiz,
11554 const void *aarch_tls,
11555 int size)
11556{
11557 char *note_name = "LINUX";
11558 return elfcore_write_note (abfd, buf, bufsiz,
11559 note_name, NT_ARM_TLS, aarch_tls, size);
11560}
11561
11562char *
11563elfcore_write_aarch_hw_break (bfd *abfd,
11564 char *buf,
11565 int *bufsiz,
11566 const void *aarch_hw_break,
11567 int size)
11568{
11569 char *note_name = "LINUX";
11570 return elfcore_write_note (abfd, buf, bufsiz,
11571 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
11572}
11573
11574char *
11575elfcore_write_aarch_hw_watch (bfd *abfd,
11576 char *buf,
11577 int *bufsiz,
11578 const void *aarch_hw_watch,
11579 int size)
11580{
11581 char *note_name = "LINUX";
11582 return elfcore_write_note (abfd, buf, bufsiz,
11583 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
11584}
11585
ad1cc4e4
AH
11586char *
11587elfcore_write_aarch_sve (bfd *abfd,
11588 char *buf,
11589 int *bufsiz,
11590 const void *aarch_sve,
11591 int size)
11592{
11593 char *note_name = "LINUX";
11594 return elfcore_write_note (abfd, buf, bufsiz,
11595 note_name, NT_ARM_SVE, aarch_sve, size);
11596}
11597
e6c3b5bf
AH
11598char *
11599elfcore_write_aarch_pauth (bfd *abfd,
11600 char *buf,
11601 int *bufsiz,
11602 const void *aarch_pauth,
11603 int size)
11604{
11605 char *note_name = "LINUX";
11606 return elfcore_write_note (abfd, buf, bufsiz,
11607 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
11608}
11609
bb864ac1
CES
11610char *
11611elfcore_write_register_note (bfd *abfd,
11612 char *buf,
11613 int *bufsiz,
11614 const char *section,
11615 const void *data,
11616 int size)
11617{
11618 if (strcmp (section, ".reg2") == 0)
11619 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
11620 if (strcmp (section, ".reg-xfp") == 0)
11621 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
4339cae0
L
11622 if (strcmp (section, ".reg-xstate") == 0)
11623 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
bb864ac1
CES
11624 if (strcmp (section, ".reg-ppc-vmx") == 0)
11625 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
89eeb0bc
LM
11626 if (strcmp (section, ".reg-ppc-vsx") == 0)
11627 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
cb2366c1
EBM
11628 if (strcmp (section, ".reg-ppc-tar") == 0)
11629 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
11630 if (strcmp (section, ".reg-ppc-ppr") == 0)
11631 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
11632 if (strcmp (section, ".reg-ppc-dscr") == 0)
11633 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
11634 if (strcmp (section, ".reg-ppc-ebb") == 0)
11635 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
11636 if (strcmp (section, ".reg-ppc-pmu") == 0)
11637 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
11638 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
11639 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
11640 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
11641 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
11642 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
11643 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
11644 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
11645 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
11646 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
11647 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
11648 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
11649 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
11650 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
11651 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
11652 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
11653 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
0675e188
UW
11654 if (strcmp (section, ".reg-s390-high-gprs") == 0)
11655 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
d7eeb400
MS
11656 if (strcmp (section, ".reg-s390-timer") == 0)
11657 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
11658 if (strcmp (section, ".reg-s390-todcmp") == 0)
11659 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
11660 if (strcmp (section, ".reg-s390-todpreg") == 0)
11661 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
11662 if (strcmp (section, ".reg-s390-ctrs") == 0)
11663 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
11664 if (strcmp (section, ".reg-s390-prefix") == 0)
11665 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
355b81d9
UW
11666 if (strcmp (section, ".reg-s390-last-break") == 0)
11667 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
11668 if (strcmp (section, ".reg-s390-system-call") == 0)
11669 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
abb3f6cc
NC
11670 if (strcmp (section, ".reg-s390-tdb") == 0)
11671 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
4ef9f41a
AA
11672 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
11673 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
11674 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
11675 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
88ab90e8
AA
11676 if (strcmp (section, ".reg-s390-gs-cb") == 0)
11677 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
11678 if (strcmp (section, ".reg-s390-gs-bc") == 0)
11679 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
faa9a424
UW
11680 if (strcmp (section, ".reg-arm-vfp") == 0)
11681 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
652451f8
YZ
11682 if (strcmp (section, ".reg-aarch-tls") == 0)
11683 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
11684 if (strcmp (section, ".reg-aarch-hw-break") == 0)
11685 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
11686 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
11687 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
ad1cc4e4
AH
11688 if (strcmp (section, ".reg-aarch-sve") == 0)
11689 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
e6c3b5bf
AH
11690 if (strcmp (section, ".reg-aarch-pauth") == 0)
11691 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
bb864ac1
CES
11692 return NULL;
11693}
11694
b34976b6 11695static bfd_boolean
276da9b3
L
11696elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
11697 size_t align)
252b5132 11698{
c044fabd 11699 char *p;
252b5132 11700
276da9b3
L
11701 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
11702 gABI specifies that PT_NOTE alignment should be aligned to 4
11703 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
11704 align is less than 4, we use 4 byte alignment. */
11705 if (align < 4)
11706 align = 4;
ef135d43
NC
11707 if (align != 4 && align != 8)
11708 return FALSE;
276da9b3 11709
252b5132
RH
11710 p = buf;
11711 while (p < buf + size)
11712 {
c044fabd 11713 Elf_External_Note *xnp = (Elf_External_Note *) p;
252b5132
RH
11714 Elf_Internal_Note in;
11715
baea7ef1
AM
11716 if (offsetof (Elf_External_Note, name) > buf - p + size)
11717 return FALSE;
11718
dc810e39 11719 in.type = H_GET_32 (abfd, xnp->type);
252b5132 11720
dc810e39 11721 in.namesz = H_GET_32 (abfd, xnp->namesz);
252b5132 11722 in.namedata = xnp->name;
baea7ef1
AM
11723 if (in.namesz > buf - in.namedata + size)
11724 return FALSE;
252b5132 11725
dc810e39 11726 in.descsz = H_GET_32 (abfd, xnp->descsz);
276da9b3 11727 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
252b5132 11728 in.descpos = offset + (in.descdata - buf);
baea7ef1
AM
11729 if (in.descsz != 0
11730 && (in.descdata >= buf + size
11731 || in.descsz > buf - in.descdata + size))
11732 return FALSE;
252b5132 11733
718175fa 11734 switch (bfd_get_format (abfd))
07d6d2b8 11735 {
718175fa
JK
11736 default:
11737 return TRUE;
11738
11739 case bfd_core:
f64e188b 11740 {
8acbedd6 11741#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
f64e188b 11742 struct
718175fa 11743 {
f64e188b 11744 const char * string;
8acbedd6 11745 size_t len;
f64e188b 11746 bfd_boolean (* func)(bfd *, Elf_Internal_Note *);
718175fa 11747 }
f64e188b 11748 grokers[] =
b15fa79e 11749 {
8acbedd6 11750 GROKER_ELEMENT ("", elfcore_grok_note),
aa1ed4a9 11751 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
8acbedd6
KS
11752 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
11753 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
11754 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
11755 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note)
f64e188b 11756 };
8acbedd6 11757#undef GROKER_ELEMENT
f64e188b
NC
11758 int i;
11759
11760 for (i = ARRAY_SIZE (grokers); i--;)
8acbedd6
KS
11761 {
11762 if (in.namesz >= grokers[i].len
11763 && strncmp (in.namedata, grokers[i].string,
11764 grokers[i].len) == 0)
11765 {
11766 if (! grokers[i].func (abfd, & in))
11767 return FALSE;
11768 break;
11769 }
11770 }
f64e188b
NC
11771 break;
11772 }
718175fa
JK
11773
11774 case bfd_object:
11775 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
11776 {
11777 if (! elfobj_grok_gnu_note (abfd, &in))
11778 return FALSE;
11779 }
e21e5835
NC
11780 else if (in.namesz == sizeof "stapsdt"
11781 && strcmp (in.namedata, "stapsdt") == 0)
11782 {
11783 if (! elfobj_grok_stapsdt_note (abfd, &in))
11784 return FALSE;
11785 }
718175fa 11786 break;
08a40648 11787 }
252b5132 11788
276da9b3 11789 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
252b5132
RH
11790 }
11791
718175fa
JK
11792 return TRUE;
11793}
11794
11795static bfd_boolean
276da9b3
L
11796elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
11797 size_t align)
718175fa
JK
11798{
11799 char *buf;
11800
957e1fc1 11801 if (size == 0 || (size + 1) == 0)
718175fa
JK
11802 return TRUE;
11803
11804 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
11805 return FALSE;
11806
f64e188b 11807 buf = (char *) bfd_malloc (size + 1);
718175fa
JK
11808 if (buf == NULL)
11809 return FALSE;
11810
f64e188b
NC
11811 /* PR 17512: file: ec08f814
11812 0-termintate the buffer so that string searches will not overflow. */
11813 buf[size] = 0;
11814
718175fa 11815 if (bfd_bread (buf, size, abfd) != size
276da9b3 11816 || !elf_parse_notes (abfd, buf, size, offset, align))
718175fa
JK
11817 {
11818 free (buf);
11819 return FALSE;
11820 }
11821
252b5132 11822 free (buf);
b34976b6 11823 return TRUE;
252b5132 11824}
98d8431c
JB
11825\f
11826/* Providing external access to the ELF program header table. */
11827
11828/* Return an upper bound on the number of bytes required to store a
11829 copy of ABFD's program header table entries. Return -1 if an error
11830 occurs; bfd_get_error will return an appropriate code. */
c044fabd 11831
98d8431c 11832long
217aa764 11833bfd_get_elf_phdr_upper_bound (bfd *abfd)
98d8431c
JB
11834{
11835 if (abfd->xvec->flavour != bfd_target_elf_flavour)
11836 {
11837 bfd_set_error (bfd_error_wrong_format);
11838 return -1;
11839 }
11840
936e320b 11841 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
98d8431c
JB
11842}
11843
98d8431c
JB
11844/* Copy ABFD's program header table entries to *PHDRS. The entries
11845 will be stored as an array of Elf_Internal_Phdr structures, as
11846 defined in include/elf/internal.h. To find out how large the
11847 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
11848
11849 Return the number of program header table entries read, or -1 if an
11850 error occurs; bfd_get_error will return an appropriate code. */
c044fabd 11851
98d8431c 11852int
217aa764 11853bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
98d8431c
JB
11854{
11855 int num_phdrs;
11856
11857 if (abfd->xvec->flavour != bfd_target_elf_flavour)
11858 {
11859 bfd_set_error (bfd_error_wrong_format);
11860 return -1;
11861 }
11862
11863 num_phdrs = elf_elfheader (abfd)->e_phnum;
01bcaf63
TT
11864 if (num_phdrs != 0)
11865 memcpy (phdrs, elf_tdata (abfd)->phdr,
11866 num_phdrs * sizeof (Elf_Internal_Phdr));
98d8431c
JB
11867
11868 return num_phdrs;
11869}
ae4221d7 11870
db6751f2 11871enum elf_reloc_type_class
7e612e98
AM
11872_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
11873 const asection *rel_sec ATTRIBUTE_UNUSED,
11874 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
db6751f2
JJ
11875{
11876 return reloc_class_normal;
11877}
f8df10f4 11878
47d9a591 11879/* For RELA architectures, return the relocation value for a
f8df10f4
JJ
11880 relocation against a local symbol. */
11881
11882bfd_vma
217aa764
AM
11883_bfd_elf_rela_local_sym (bfd *abfd,
11884 Elf_Internal_Sym *sym,
8517fae7 11885 asection **psec,
217aa764 11886 Elf_Internal_Rela *rel)
f8df10f4 11887{
8517fae7 11888 asection *sec = *psec;
f8df10f4
JJ
11889 bfd_vma relocation;
11890
6835821b
AM
11891 relocation = (sec->output_section->vma
11892 + sec->output_offset
11893 + sym->st_value);
f8df10f4 11894 if ((sec->flags & SEC_MERGE)
c629eae0 11895 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
dbaa2011 11896 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
f8df10f4 11897 {
f8df10f4 11898 rel->r_addend =
8517fae7 11899 _bfd_merged_section_offset (abfd, psec,
65765700 11900 elf_section_data (sec)->sec_info,
753731ee
AM
11901 sym->st_value + rel->r_addend);
11902 if (sec != *psec)
11903 {
11904 /* If we have changed the section, and our original section is
11905 marked with SEC_EXCLUDE, it means that the original
11906 SEC_MERGE section has been completely subsumed in some
11907 other SEC_MERGE section. In this case, we need to leave
11908 some info around for --emit-relocs. */
11909 if ((sec->flags & SEC_EXCLUDE) != 0)
11910 sec->kept_section = *psec;
11911 sec = *psec;
11912 }
8517fae7
AM
11913 rel->r_addend -= relocation;
11914 rel->r_addend += sec->output_section->vma + sec->output_offset;
f8df10f4
JJ
11915 }
11916 return relocation;
11917}
c629eae0
JJ
11918
11919bfd_vma
217aa764
AM
11920_bfd_elf_rel_local_sym (bfd *abfd,
11921 Elf_Internal_Sym *sym,
11922 asection **psec,
11923 bfd_vma addend)
47d9a591 11924{
c629eae0
JJ
11925 asection *sec = *psec;
11926
6835821b 11927 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
c629eae0
JJ
11928 return sym->st_value + addend;
11929
11930 return _bfd_merged_section_offset (abfd, psec,
65765700 11931 elf_section_data (sec)->sec_info,
753731ee 11932 sym->st_value + addend);
c629eae0
JJ
11933}
11934
37b01f6a
DG
11935/* Adjust an address within a section. Given OFFSET within SEC, return
11936 the new offset within the section, based upon changes made to the
11937 section. Returns -1 if the offset is now invalid.
11938 The offset (in abnd out) is in target sized bytes, however big a
11939 byte may be. */
11940
c629eae0 11941bfd_vma
217aa764 11942_bfd_elf_section_offset (bfd *abfd,
92e4ec35 11943 struct bfd_link_info *info,
217aa764
AM
11944 asection *sec,
11945 bfd_vma offset)
c629eae0 11946{
68bfbfcc 11947 switch (sec->sec_info_type)
65765700 11948 {
dbaa2011 11949 case SEC_INFO_TYPE_STABS:
eea6121a
AM
11950 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
11951 offset);
dbaa2011 11952 case SEC_INFO_TYPE_EH_FRAME:
92e4ec35 11953 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
37b01f6a 11954
65765700 11955 default:
310fd250
L
11956 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
11957 {
37b01f6a 11958 /* Reverse the offset. */
310fd250
L
11959 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11960 bfd_size_type address_size = bed->s->arch_size / 8;
37b01f6a
DG
11961
11962 /* address_size and sec->size are in octets. Convert
11963 to bytes before subtracting the original offset. */
11964 offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset;
310fd250 11965 }
65765700
JJ
11966 return offset;
11967 }
c629eae0 11968}
3333a7c3
RM
11969\f
11970/* Create a new BFD as if by bfd_openr. Rather than opening a file,
11971 reconstruct an ELF file by reading the segments out of remote memory
11972 based on the ELF file header at EHDR_VMA and the ELF program headers it
11973 points to. If not null, *LOADBASEP is filled in with the difference
11974 between the VMAs from which the segments were read, and the VMAs the
11975 file headers (and hence BFD's idea of each section's VMA) put them at.
11976
11977 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
11978 remote memory at target address VMA into the local buffer at MYADDR; it
11979 should return zero on success or an `errno' code on failure. TEMPL must
11980 be a BFD for an ELF target with the word size and byte order found in
11981 the remote memory. */
11982
11983bfd *
217aa764
AM
11984bfd_elf_bfd_from_remote_memory
11985 (bfd *templ,
11986 bfd_vma ehdr_vma,
f0a5d95a 11987 bfd_size_type size,
217aa764 11988 bfd_vma *loadbasep,
fe78531d 11989 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
3333a7c3
RM
11990{
11991 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
5979d6b6 11992 (templ, ehdr_vma, size, loadbasep, target_read_memory);
3333a7c3 11993}
4c45e5c9
JJ
11994\f
11995long
c9727e01
AM
11996_bfd_elf_get_synthetic_symtab (bfd *abfd,
11997 long symcount ATTRIBUTE_UNUSED,
11998 asymbol **syms ATTRIBUTE_UNUSED,
8615f3f2 11999 long dynsymcount,
c9727e01
AM
12000 asymbol **dynsyms,
12001 asymbol **ret)
4c45e5c9
JJ
12002{
12003 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12004 asection *relplt;
12005 asymbol *s;
12006 const char *relplt_name;
12007 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
12008 arelent *p;
12009 long count, i, n;
12010 size_t size;
12011 Elf_Internal_Shdr *hdr;
12012 char *names;
12013 asection *plt;
12014
8615f3f2
AM
12015 *ret = NULL;
12016
90e3cdf2
JJ
12017 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
12018 return 0;
12019
8615f3f2
AM
12020 if (dynsymcount <= 0)
12021 return 0;
12022
4c45e5c9
JJ
12023 if (!bed->plt_sym_val)
12024 return 0;
12025
12026 relplt_name = bed->relplt_name;
12027 if (relplt_name == NULL)
d35fd659 12028 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
4c45e5c9
JJ
12029 relplt = bfd_get_section_by_name (abfd, relplt_name);
12030 if (relplt == NULL)
12031 return 0;
12032
12033 hdr = &elf_section_data (relplt)->this_hdr;
12034 if (hdr->sh_link != elf_dynsymtab (abfd)
12035 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
12036 return 0;
12037
12038 plt = bfd_get_section_by_name (abfd, ".plt");
12039 if (plt == NULL)
12040 return 0;
12041
12042 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
c9727e01 12043 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
4c45e5c9
JJ
12044 return -1;
12045
eea6121a 12046 count = relplt->size / hdr->sh_entsize;
4c45e5c9
JJ
12047 size = count * sizeof (asymbol);
12048 p = relplt->relocation;
cb53bf42 12049 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
041de40d
AM
12050 {
12051 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
12052 if (p->addend != 0)
12053 {
12054#ifdef BFD64
12055 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
12056#else
12057 size += sizeof ("+0x") - 1 + 8;
12058#endif
12059 }
12060 }
4c45e5c9 12061
a50b1753 12062 s = *ret = (asymbol *) bfd_malloc (size);
4c45e5c9
JJ
12063 if (s == NULL)
12064 return -1;
12065
12066 names = (char *) (s + count);
12067 p = relplt->relocation;
12068 n = 0;
cb53bf42 12069 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
4c45e5c9
JJ
12070 {
12071 size_t len;
12072 bfd_vma addr;
12073
12074 addr = bed->plt_sym_val (i, plt, p);
12075 if (addr == (bfd_vma) -1)
12076 continue;
12077
12078 *s = **p->sym_ptr_ptr;
65a7a66f
AM
12079 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12080 we are defining a symbol, ensure one of them is set. */
12081 if ((s->flags & BSF_LOCAL) == 0)
12082 s->flags |= BSF_GLOBAL;
6ba2a415 12083 s->flags |= BSF_SYNTHETIC;
4c45e5c9
JJ
12084 s->section = plt;
12085 s->value = addr - plt->vma;
12086 s->name = names;
8f39ba8e 12087 s->udata.p = NULL;
4c45e5c9
JJ
12088 len = strlen ((*p->sym_ptr_ptr)->name);
12089 memcpy (names, (*p->sym_ptr_ptr)->name, len);
12090 names += len;
041de40d
AM
12091 if (p->addend != 0)
12092 {
1d770845 12093 char buf[30], *a;
d324f6d6 12094
041de40d
AM
12095 memcpy (names, "+0x", sizeof ("+0x") - 1);
12096 names += sizeof ("+0x") - 1;
1d770845
L
12097 bfd_sprintf_vma (abfd, buf, p->addend);
12098 for (a = buf; *a == '0'; ++a)
12099 ;
12100 len = strlen (a);
12101 memcpy (names, a, len);
12102 names += len;
041de40d 12103 }
4c45e5c9
JJ
12104 memcpy (names, "@plt", sizeof ("@plt"));
12105 names += sizeof ("@plt");
8f39ba8e 12106 ++s, ++n;
4c45e5c9
JJ
12107 }
12108
12109 return n;
12110}
3d7f7666 12111
821e6ff6
AM
12112/* It is only used by x86-64 so far.
12113 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
7eacd66b
AM
12114 but current usage would allow all of _bfd_std_section to be zero. */
12115static const asymbol lcomm_sym
12116 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
3b22753a 12117asection _bfd_elf_large_com_section
7eacd66b 12118 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
821e6ff6 12119 "LARGE_COMMON", 0, SEC_IS_COMMON);
ecca9871 12120
d1036acb 12121void
06f44071
AM
12122_bfd_elf_post_process_headers (bfd *abfd ATTRIBUTE_UNUSED,
12123 struct bfd_link_info *info ATTRIBUTE_UNUSED)
d1036acb 12124{
06f44071
AM
12125}
12126
cc364be6
AM
12127bfd_boolean
12128_bfd_elf_final_write_processing (bfd *abfd)
06f44071
AM
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. */
cc364be6
AM
12140 if (elf_tdata (abfd)->has_gnu_osabi != 0)
12141 {
12142 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
12143 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
12144 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
12145 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
12146 {
12147 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
12148 _bfd_error_handler (_("GNU_MBIND section is unsupported"));
12149 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
12150 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is unsupported"));
12151 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
12152 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is unsupported"));
12153 bfd_set_error (bfd_error_bad_value);
12154 return FALSE;
12155 }
12156 }
12157 return TRUE;
d1036acb 12158}
fcb93ecf
PB
12159
12160
12161/* Return TRUE for ELF symbol types that represent functions.
12162 This is the default version of this function, which is sufficient for
d8045f23 12163 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
fcb93ecf
PB
12164
12165bfd_boolean
12166_bfd_elf_is_function_type (unsigned int type)
12167{
d8045f23
NC
12168 return (type == STT_FUNC
12169 || type == STT_GNU_IFUNC);
fcb93ecf 12170}
9f296da3 12171
aef36ac1
AM
12172/* If the ELF symbol SYM might be a function in SEC, return the
12173 function size and set *CODE_OFF to the function's entry point,
12174 otherwise return zero. */
9f296da3 12175
aef36ac1
AM
12176bfd_size_type
12177_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
12178 bfd_vma *code_off)
9f296da3 12179{
aef36ac1
AM
12180 bfd_size_type size;
12181
ff9e0f5b 12182 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
aef36ac1
AM
12183 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
12184 || sym->section != sec)
12185 return 0;
ff9e0f5b 12186
ff9e0f5b 12187 *code_off = sym->value;
aef36ac1
AM
12188 size = 0;
12189 if (!(sym->flags & BSF_SYNTHETIC))
12190 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
12191 if (size == 0)
12192 size = 1;
12193 return size;
9f296da3 12194}
This page took 2.521019 seconds and 4 git commands to generate.